From 1e363023b823d399a4eac311e846a078cb329ceb Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 29 Mar 2020 17:08:04 +0200 Subject: [PATCH] [InstCombine] Use replaceOperand() in a few more places To make sure the old operands get DCEd. NFC apart from worklist order changes. --- .../Transforms/InstCombine/InstCombineCompares.cpp | 9 ++++---- .../InstCombine/InstCombineMulDivRem.cpp | 24 +++++++++------------- .../Transforms/InstCombine/InstCombineSelect.cpp | 12 ++++++----- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 62b99e8..d1fa802 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5963,16 +5963,15 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI, } /// Optimize fabs(X) compared with zero. -static Instruction *foldFabsWithFcmpZero(FCmpInst &I) { +static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombiner &IC) { Value *X; if (!match(I.getOperand(0), m_Intrinsic(m_Value(X))) || !match(I.getOperand(1), m_PosZeroFP())) return nullptr; - auto replacePredAndOp0 = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) { + auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) { I->setPredicate(P); - I->setOperand(0, X); - return I; + return IC.replaceOperand(*I, 0, X); }; switch (I.getPredicate()) { @@ -6137,7 +6136,7 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { } } - if (Instruction *R = foldFabsWithFcmpZero(I)) + if (Instruction *R = foldFabsWithFcmpZero(I, *this)) return R; if (match(Op0, m_FNeg(m_Value(X)))) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 183ab1a..899e8b8 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -682,10 +682,8 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { Type *Ty = I.getType(); // The RHS is known non-zero. - if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { - I.setOperand(1, V); - return &I; - } + if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) + return replaceOperand(I, 1, V); // Handle cases involving: [su]div X, (select Cond, Y, Z) // This does not apply for fdiv. @@ -799,8 +797,8 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { bool HasNSW = cast(Op1)->hasNoSignedWrap(); bool HasNUW = cast(Op1)->hasNoUnsignedWrap(); if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) { - I.setOperand(0, ConstantInt::get(Ty, 1)); - I.setOperand(1, Y); + replaceOperand(I, 0, ConstantInt::get(Ty, 1)); + replaceOperand(I, 1, Y); return &I; } } @@ -1276,8 +1274,8 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { // -X / -Y -> X / Y Value *X, *Y; if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) { - I.setOperand(0, X); - I.setOperand(1, Y); + replaceOperand(I, 0, X); + replaceOperand(I, 1, Y); return &I; } @@ -1286,8 +1284,8 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { // We can ignore the possibility that X is infinity because INF/INF is NaN. if (I.hasNoNaNs() && I.hasAllowReassoc() && match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) { - I.setOperand(0, ConstantFP::get(I.getType(), 1.0)); - I.setOperand(1, Y); + replaceOperand(I, 0, ConstantFP::get(I.getType(), 1.0)); + replaceOperand(I, 1, Y); return &I; } @@ -1313,10 +1311,8 @@ Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); // The RHS is known non-zero. - if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { - I.setOperand(1, V); - return &I; - } + if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) + return replaceOperand(I, 1, V); // Handle cases involving: rem X, (select Cond, Y, Z) if (simplifyDivRemOfSelectWithZeroOp(I)) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 0e4d113..571fcaf 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1036,7 +1036,7 @@ canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp, /// Canonicalize all these variants to 1 pattern. /// This makes CSE more likely. static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp, - InstCombiner::BuilderTy &Builder) { + InstCombiner &IC) { if (!Cmp.hasOneUse() || !isa(Cmp.getOperand(1))) return nullptr; @@ -1085,12 +1085,14 @@ static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp, // Create the canonical RHS: RHS = sub (0, LHS). if (!RHSCanonicalized) { assert(RHS->hasOneUse() && "RHS use number is not right"); - RHS = Builder.CreateNeg(LHS); + RHS = IC.Builder.CreateNeg(LHS); if (TVal == LHS) { - Sel.setFalseValue(RHS); + // Replace false value. + IC.replaceOperand(Sel, 2, RHS); FVal = RHS; } else { - Sel.setTrueValue(RHS); + // Replace true value. + IC.replaceOperand(Sel, 1, RHS); TVal = RHS; } } @@ -1398,7 +1400,7 @@ Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, *this)) return NewSel; - if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder)) + if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, *this)) return NewAbs; if (Instruction *NewAbs = canonicalizeClampLike(SI, *ICI, Builder)) -- 2.7.4