diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1954,10 +1954,9 @@ // Canonicalize a shift amount constant operand to modulo the bit-width. Constant *WidthC = ConstantInt::get(Ty, BitWidth); Constant *ModuloC = ConstantExpr::getURem(ShAmtC, WidthC); - if (ModuloC != ShAmtC) { - II->setArgOperand(2, ModuloC); - return II; - } + if (ModuloC != ShAmtC) + return replaceOperand(*II, 2, ModuloC); + assert(ConstantExpr::getICmp(ICmpInst::ICMP_UGT, WidthC, ShAmtC) == ConstantInt::getTrue(CmpInst::makeCmpResultType(Ty)) && "Shift amount expected to be modulo bitwidth"); @@ -2256,16 +2255,16 @@ Value *Src1 = II->getArgOperand(1); Value *X, *Y; if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y)))) { - II->setArgOperand(0, X); - II->setArgOperand(1, Y); + replaceOperand(*II, 0, X); + replaceOperand(*II, 1, Y); return II; } // fma fabs(x), fabs(x), z -> fma x, x, z if (match(Src0, m_FAbs(m_Value(X))) && match(Src1, m_FAbs(m_Specific(X)))) { - II->setArgOperand(0, X); - II->setArgOperand(1, X); + replaceOperand(*II, 0, X); + replaceOperand(*II, 1, X); return II; } @@ -2303,10 +2302,8 @@ // copysign X, (copysign ?, SignArg) --> copysign X, SignArg Value *SignArg; if (match(II->getArgOperand(1), - m_Intrinsic(m_Value(), m_Value(SignArg)))) { - II->setArgOperand(1, SignArg); - return II; - } + m_Intrinsic(m_Value(), m_Value(SignArg)))) + return replaceOperand(*II, 1, SignArg); break; } @@ -2343,8 +2340,7 @@ if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) { // cos(-x) -> cos(x) // cos(fabs(x)) -> cos(x) - II->setArgOperand(0, X); - return II; + return replaceOperand(*II, 0, X); } break; } @@ -2703,8 +2699,8 @@ cast(Arg0)->getFastMathFlags().noInfs())) { if (Arg0IsZero) std::swap(A, B); - II->setArgOperand(0, A); - II->setArgOperand(1, B); + replaceOperand(*II, 0, A); + replaceOperand(*II, 1, B); return II; } break; @@ -3327,12 +3323,10 @@ getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT); unsigned AlignArg = II->getNumArgOperands() - 1; ConstantInt *IntrAlign = dyn_cast(II->getArgOperand(AlignArg)); - if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) { - II->setArgOperand(AlignArg, - ConstantInt::get(Type::getInt32Ty(II->getContext()), - MemAlign, false)); - return II; - } + if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) + return replaceOperand(*II, AlignArg, + ConstantInt::get(Type::getInt32Ty(II->getContext()), + MemAlign, false)); break; } @@ -3391,8 +3385,8 @@ Value *Data, *Key; if (match(KeyArg, m_ZeroInt()) && match(DataArg, m_Xor(m_Value(Data), m_Value(Key)))) { - II->setArgOperand(0, Data); - II->setArgOperand(1, Key); + replaceOperand(*II, 0, Data); + replaceOperand(*II, 1, Key); return II; } break; @@ -3559,11 +3553,9 @@ } // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other - if (((Mask & S_NAN) || (Mask & Q_NAN)) && isKnownNeverNaN(Src0, &TLI)) { - II->setArgOperand(1, ConstantInt::get(Src1->getType(), - Mask & ~(S_NAN | Q_NAN))); - return II; - } + if (((Mask & S_NAN) || (Mask & Q_NAN)) && isKnownNeverNaN(Src0, &TLI)) + return replaceOperand(*II, 1, ConstantInt::get(Src1->getType(), + Mask & ~(S_NAN | Q_NAN))); const ConstantFP *CVal = dyn_cast(Src0); if (!CVal) { @@ -3653,23 +3645,19 @@ if ((Width & (IntSize - 1)) == 0) return replaceInstUsesWith(*II, ConstantInt::getNullValue(Ty)); - if (Width >= IntSize) { - // Hardware ignores high bits, so remove those. - II->setArgOperand(2, ConstantInt::get(CWidth->getType(), - Width & (IntSize - 1))); - return II; - } + // Hardware ignores high bits, so remove those. + if (Width >= IntSize) + return replaceOperand(*II, 2, ConstantInt::get(CWidth->getType(), + Width & (IntSize - 1))); } unsigned Offset; ConstantInt *COffset = dyn_cast(II->getArgOperand(1)); if (COffset) { Offset = COffset->getZExtValue(); - if (Offset >= IntSize) { - II->setArgOperand(1, ConstantInt::get(COffset->getType(), - Offset & (IntSize - 1))); - return II; - } + if (Offset >= IntSize) + return replaceOperand(*II, 1, ConstantInt::get(COffset->getType(), + Offset & (IntSize - 1))); } bool Signed = IID == Intrinsic::amdgcn_sbfe; @@ -3712,7 +3700,7 @@ (IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) { Value *Src = II->getArgOperand(I + 2); if (!isa(Src)) { - II->setArgOperand(I + 2, UndefValue::get(Src->getType())); + replaceOperand(*II, I + 2, UndefValue::get(Src->getType())); Changed = true; } } @@ -3851,8 +3839,8 @@ ((match(Src1, m_One()) && match(Src0, m_ZExt(m_Value(ExtSrc)))) || (match(Src1, m_AllOnes()) && match(Src0, m_SExt(m_Value(ExtSrc))))) && ExtSrc->getType()->isIntegerTy(1)) { - II->setArgOperand(1, ConstantInt::getNullValue(Src1->getType())); - II->setArgOperand(2, ConstantInt::get(CC->getType(), CmpInst::ICMP_NE)); + replaceOperand(*II, 1, ConstantInt::getNullValue(Src1->getType())); + replaceOperand(*II, 2, ConstantInt::get(CC->getType(), CmpInst::ICMP_NE)); return II; } @@ -4196,7 +4184,7 @@ MoveI = MoveI->getNextNonDebugInstruction(); Temp->moveBefore(II); } - II->setArgOperand(0, Builder.CreateAnd(CurrCond, NextCond)); + replaceOperand(*II, 0, Builder.CreateAnd(CurrCond, NextCond)); } eraseInstFromFunction(*NextInst); return II; diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -351,8 +351,8 @@ // Does "B op C" simplify? if (Value *V = SimplifyBinOp(Opcode, B, C, SQ.getWithInstruction(&I))) { // It simplifies to V. Form "A op V". - I.setOperand(0, A); - I.setOperand(1, V); + replaceOperand(I, 0, A); + replaceOperand(I, 1, V); bool IsNUW = hasNoUnsignedWrap(I) && hasNoUnsignedWrap(*Op0); bool IsNSW = maintainNoSignedWrap(I, B, C) && hasNoSignedWrap(*Op0); @@ -384,8 +384,8 @@ // Does "A op B" simplify? if (Value *V = SimplifyBinOp(Opcode, A, B, SQ.getWithInstruction(&I))) { // It simplifies to V. Form "V op C". - I.setOperand(0, V); - I.setOperand(1, C); + replaceOperand(I, 0, V); + replaceOperand(I, 1, C); // Conservatively clear the optional flags, since they may not be // preserved by the reassociation. ClearSubclassDataAfterReassociation(I); @@ -412,8 +412,8 @@ // Does "C op A" simplify? if (Value *V = SimplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) { // It simplifies to V. Form "V op B". - I.setOperand(0, V); - I.setOperand(1, B); + replaceOperand(I, 0, V); + replaceOperand(I, 1, B); // Conservatively clear the optional flags, since they may not be // preserved by the reassociation. ClearSubclassDataAfterReassociation(I); @@ -432,8 +432,8 @@ // Does "C op A" simplify? if (Value *V = SimplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) { // It simplifies to V. Form "B op V". - I.setOperand(0, B); - I.setOperand(1, V); + replaceOperand(I, 0, B); + replaceOperand(I, 1, V); // Conservatively clear the optional flags, since they may not be // preserved by the reassociation. ClearSubclassDataAfterReassociation(I); @@ -466,8 +466,8 @@ } InsertNewInstWith(NewBO, I); NewBO->takeName(Op1); - I.setOperand(0, NewBO); - I.setOperand(1, ConstantExpr::get(Opcode, C1, C2)); + replaceOperand(I, 0, NewBO); + replaceOperand(I, 1, ConstantExpr::get(Opcode, C1, C2)); // Conservatively clear the optional flags, since they may not be // preserved by the reassociation. ClearSubclassDataAfterReassociation(I); @@ -2756,18 +2756,16 @@ if (match(&BI, m_Br(m_Not(m_Value(X)), m_BasicBlock(), m_BasicBlock())) && !isa(X)) { // Swap Destinations and condition... - BI.setCondition(X); BI.swapSuccessors(); - return &BI; + return replaceOperand(BI, 0, X); } // If the condition is irrelevant, remove the use so that other // transforms on the condition become more effective. if (BI.isConditional() && !isa(BI.getCondition()) && - BI.getSuccessor(0) == BI.getSuccessor(1)) { - BI.setCondition(ConstantInt::getFalse(BI.getCondition()->getType())); - return &BI; - } + BI.getSuccessor(0) == BI.getSuccessor(1)) + return replaceOperand( + BI, 0, ConstantInt::getFalse(BI.getCondition()->getType())); // Canonicalize, for example, icmp_ne -> icmp_eq or fcmp_one -> fcmp_oeq. CmpInst::Predicate Pred; @@ -2797,8 +2795,7 @@ "Result of expression should be constant"); Case.setValue(cast(NewCase)); } - SI.setCondition(Op0); - return &SI; + return replaceOperand(SI, 0, Op0); } KnownBits Known = computeKnownBits(Cond, 0, &SI); @@ -2825,13 +2822,12 @@ IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth); Builder.SetInsertPoint(&SI); Value *NewCond = Builder.CreateTrunc(Cond, Ty, "trunc"); - SI.setCondition(NewCond); for (auto Case : SI.cases()) { APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth); Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase)); } - return &SI; + return replaceOperand(SI, 0, NewCond); } return nullptr;