From 8914d29008a98dd8bef764240d45b5bdb3cc8f9d Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Wed, 10 Feb 2016 22:47:15 +0000 Subject: [PATCH] Simplify handleOperandChangeImpl() removing last argument (NFC) The Use argument was used to compute the operand number for a fast path when replacing only one operand. However we always have to go through all the operands. So the argument number can be recomputed locally anyway. From: Mehdi Amini llvm-svn: 260454 --- llvm/include/llvm/IR/Constant.h | 2 +- llvm/include/llvm/IR/Constants.h | 24 ++++---- llvm/include/llvm/IR/GlobalValue.h | 2 +- llvm/lib/IR/Constants.cpp | 90 ++++++++++++++---------------- llvm/lib/IR/Globals.cpp | 2 +- llvm/lib/IR/Value.cpp | 2 +- 6 files changed, 57 insertions(+), 65 deletions(-) diff --git a/llvm/include/llvm/IR/Constant.h b/llvm/include/llvm/IR/Constant.h index 7c0437df14c1..3c5fe556d50f 100644 --- a/llvm/include/llvm/IR/Constant.h +++ b/llvm/include/llvm/IR/Constant.h @@ -131,7 +131,7 @@ public: /// use Value::replaceAllUsesWith, which automatically dispatches to this /// method as needed. /// - void handleOperandChange(Value *, Value *, Use *); + void handleOperandChange(Value *, Value *); static Constant *getNullValue(Type* Ty); diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h index ff230af5b474..f97fe8ce7115 100644 --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -53,7 +53,7 @@ class ConstantInt : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: // allocate space for exactly zero operands @@ -239,7 +239,7 @@ class ConstantFP : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: ConstantFP(Type *Ty, const APFloat& V); @@ -310,7 +310,7 @@ class ConstantAggregateZero : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: explicit ConstantAggregateZero(Type *ty) @@ -357,7 +357,7 @@ class ConstantArray : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: ConstantArray(ArrayType *T, ArrayRef Val); @@ -400,7 +400,7 @@ class ConstantStruct : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: ConstantStruct(StructType *T, ArrayRef Val); @@ -459,7 +459,7 @@ class ConstantVector : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: ConstantVector(VectorType *T, ArrayRef Val); @@ -509,7 +509,7 @@ class ConstantPointerNull : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: explicit ConstantPointerNull(PointerType *T) @@ -562,7 +562,7 @@ class ConstantDataSequential : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data) @@ -786,7 +786,7 @@ class ConstantTokenNone : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: explicit ConstantTokenNone(LLVMContext &Context) @@ -813,7 +813,7 @@ class BlockAddress : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); public: /// Return a BlockAddress for the specified function and basic block. @@ -860,7 +860,7 @@ class ConstantExpr : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps) @@ -1229,7 +1229,7 @@ class UndefValue : public Constant { friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {} diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h index fa6469aa0ade..38d22907129c 100644 --- a/llvm/include/llvm/IR/GlobalValue.h +++ b/llvm/include/llvm/IR/GlobalValue.h @@ -93,7 +93,7 @@ private: friend class Constant; void destroyConstantImpl(); - Value *handleOperandChangeImpl(Value *From, Value *To, Use *U); + Value *handleOperandChangeImpl(Value *From, Value *To); protected: /// \brief The intrinsic ID for this subclass (which must be a Function). diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 98151f5d8e55..87e870e627b3 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -1466,16 +1466,18 @@ void BlockAddress::destroyConstantImpl() { getBasicBlock()->AdjustBlockAddressRefCount(-1); } -Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) { // This could be replacing either the Basic Block or the Function. In either // case, we have to remove the map entry. Function *NewF = getFunction(); BasicBlock *NewBB = getBasicBlock(); - if (U == &Op<0>()) + if (From == NewF) NewF = cast(To->stripPointerCasts()); - else + else { + assert(From == NewBB && "From does not match any operand"); NewBB = cast(To); + } // See if the 'new' entry already exists, if not, just update this in place // and return early. @@ -2787,14 +2789,14 @@ Constant *ConstantDataVector::getSplatValue() const { /// work, but would be really slow because it would have to unique each updated /// array instance. /// -void Constant::handleOperandChange(Value *From, Value *To, Use *U) { +void Constant::handleOperandChange(Value *From, Value *To) { Value *Replacement = nullptr; switch (getValueID()) { default: llvm_unreachable("Not a constant!"); #define HANDLE_CONSTANT(Name) \ case Value::Name##Val: \ - Replacement = cast(this)->handleOperandChangeImpl(From, To, U); \ + Replacement = cast(this)->handleOperandChangeImpl(From, To); \ break; #include "llvm/IR/Value.def" } @@ -2814,39 +2816,35 @@ void Constant::handleOperandChange(Value *From, Value *To, Use *U) { destroyConstant(); } -Value *ConstantInt::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *ConstantInt::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantFP::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *ConstantFP::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantTokenNone::handleOperandChangeImpl(Value *From, Value *To, - Use *U) { +Value *ConstantTokenNone::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *UndefValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *UndefValue::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantPointerNull::handleOperandChangeImpl(Value *From, Value *To, - Use *U) { +Value *ConstantPointerNull::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantAggregateZero::handleOperandChangeImpl(Value *From, Value *To, - Use *U) { +Value *ConstantAggregateZero::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantDataSequential::handleOperandChangeImpl(Value *From, Value *To, - Use *U) { +Value *ConstantDataSequential::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } -Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast(To); @@ -2860,9 +2858,11 @@ Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To, Use *U) { // Keep track of whether all the values in the array are "ToC". bool AllSame = true; Use *OperandList = getOperandList(); + unsigned OperandNo = 0; for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { Constant *Val = cast(O->get()); if (Val == From) { + OperandNo = (O - OperandList); Val = ToC; ++NumUpdated; } @@ -2882,65 +2882,57 @@ Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To, Use *U) { // Update to the new value. return getContext().pImpl->ArrayConstants.replaceOperandsInPlace( - Values, this, From, ToC, NumUpdated, U - OperandList); + Values, this, From, ToC, NumUpdated, OperandNo); } -Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast(To); Use *OperandList = getOperandList(); - unsigned OperandToUpdate = U-OperandList; - assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); SmallVector Values; Values.reserve(getNumOperands()); // Build replacement struct. // Fill values with the modified operands of the constant struct. Also, // compute whether this turns into an all-zeros struct. - bool isAllZeros = false; - bool isAllUndef = false; - if (ToC->isNullValue()) { - isAllZeros = true; - for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { - Constant *Val = cast(O->get()); - Values.push_back(Val); - if (isAllZeros) isAllZeros = Val->isNullValue(); + unsigned NumUpdated = 0; + bool AllSame = true; + unsigned OperandNo = 0; + for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) { + Constant *Val = cast(O->get()); + if (Val == From) { + OperandNo = (O - OperandList); + Val = ToC; + ++NumUpdated; } - } else if (isa(ToC)) { - isAllUndef = true; - for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { - Constant *Val = cast(O->get()); - Values.push_back(Val); - if (isAllUndef) isAllUndef = isa(Val); - } - } else { - for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) - Values.push_back(cast(O->get())); + Values.push_back(Val); + AllSame &= Val == ToC; } - Values[OperandToUpdate] = ToC; - if (isAllZeros) + if (AllSame && ToC->isNullValue()) return ConstantAggregateZero::get(getType()); - if (isAllUndef) + if (AllSame && isa(ToC)) return UndefValue::get(getType()); // Update to the new value. return getContext().pImpl->StructConstants.replaceOperandsInPlace( - Values, this, From, ToC); + Values, this, From, ToC, NumUpdated, OperandNo); } -Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast(To); SmallVector Values; Values.reserve(getNumOperands()); // Build replacement array... unsigned NumUpdated = 0; + unsigned OperandNo = 0; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { Constant *Val = getOperand(i); if (Val == From) { + OperandNo = i; ++NumUpdated; Val = ToC; } @@ -2951,20 +2943,21 @@ Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To, Use *U) { return C; // Update to the new value. - Use *OperandList = getOperandList(); return getContext().pImpl->VectorConstants.replaceOperandsInPlace( - Values, this, From, ToC, NumUpdated, U - OperandList); + Values, this, From, ToC, NumUpdated, OperandNo); } -Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV, Use *U) { +Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) { assert(isa(ToV) && "Cannot make Constant refer to non-constant!"); Constant *To = cast(ToV); SmallVector NewOps; unsigned NumUpdated = 0; + unsigned OperandNo = 0; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { Constant *Op = getOperand(i); if (Op == From) { + OperandNo = i; ++NumUpdated; Op = To; } @@ -2976,9 +2969,8 @@ Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV, Use *U) { return C; // Update to the new value. - Use *OperandList = getOperandList(); return getContext().pImpl->ExprConstants.replaceOperandsInPlace( - NewOps, this, From, To, NumUpdated, U - OperandList); + NewOps, this, From, To, NumUpdated, OperandNo); } Instruction *ConstantExpr::getAsInstruction() { diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 2b8a4cf029ad..7f4b6234b0b7 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -43,7 +43,7 @@ void GlobalValue::destroyConstantImpl() { llvm_unreachable("You can't GV->destroyConstantImpl()!"); } -Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) { +Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) { llvm_unreachable("Unsupported class for handleOperandChange()!"); } diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index 955204a1bc2e..2e12d91e7a36 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -380,7 +380,7 @@ void Value::replaceAllUsesWith(Value *New) { // constant because they are uniqued. if (auto *C = dyn_cast(U.getUser())) { if (!isa(C)) { - C->handleOperandChange(this, New, &U); + C->handleOperandChange(this, New); continue; } }