diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-08-19 19:13:30 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-08-19 19:13:30 +0000 |
commit | bb69ce8c70f6a392fedc6ec520705f55b2e4fb0f (patch) | |
tree | ae4d645c1b50a01edd7095d7070b66d5a0ea7a43 /lib/IR/ConstantsContext.h | |
parent | f08cddcf560b1eb8401d405501e26cf3c3843fb4 (diff) |
IR: De-duplicate code for replacing operands in place
This is non-trivial and sits in three places. Move it to
ConstantUniqueMap.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216007 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/IR/ConstantsContext.h')
-rw-r--r-- | lib/IR/ConstantsContext.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/IR/ConstantsContext.h b/lib/IR/ConstantsContext.h index 092de718776..571dec2e0f6 100644 --- a/lib/IR/ConstantsContext.h +++ b/lib/IR/ConstantsContext.h @@ -341,6 +341,8 @@ template <> struct ConstantInfo<ConstantVector> { template <class ConstantClass> struct ConstantAggrKeyType { ArrayRef<Constant *> Operands; ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {} + ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *) + : Operands(Operands) {} ConstantAggrKeyType(const ConstantClass *C, SmallVectorImpl<Constant *> &Storage) { assert(Storage.empty() && "Expected empty storage"); @@ -425,6 +427,11 @@ struct ConstantExprKeyType { ArrayRef<unsigned> Indexes = None) : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData), SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {} + ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE) + : Opcode(CE->getOpcode()), + SubclassOptionalData(CE->getRawSubclassOptionalData()), + SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands), + Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {} ConstantExprKeyType(const ConstantExpr *CE, SmallVectorImpl<Constant *> &Storage) : Opcode(CE->getOpcode()), @@ -594,6 +601,31 @@ public: Map.erase(I); } + ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands, + ConstantClass *CP, Value *From, + Constant *To, unsigned NumUpdated = 0, + unsigned OperandNo = ~0u) { + LookupKey Lookup(CP->getType(), ValType(Operands, CP)); + auto I = find(Lookup); + if (I != Map.end()) + return I->first; + + // Update to the new value. Optimize for the case when we have a single + // operand that we're changing, but handle bulk updates efficiently. + remove(CP); + if (NumUpdated == 1) { + assert(OperandNo < CP->getNumOperands() && "Invalid index"); + assert(CP->getOperand(OperandNo) != To && "I didn't contain From!"); + CP->setOperand(OperandNo, To); + } else { + for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I) + if (CP->getOperand(I) == From) + CP->setOperand(I, To); + } + insert(CP); + return nullptr; + } + void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); } }; |