Skip to content

Commit

Permalink
[InstCombine] Use replaceOperand() in a few more places
Browse files Browse the repository at this point in the history
To make sure the old operands get DCEd.

NFC apart from worklist order changes.
  • Loading branch information
nikic committed Mar 29, 2020
1 parent 7734e4b commit 1e36302
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Expand Up @@ -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<Intrinsic::fabs>(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()) {
Expand Down Expand Up @@ -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)))) {
Expand Down
24 changes: 10 additions & 14 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -799,8 +797,8 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
bool HasNUW = cast<OverflowingBinaryOperator>(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;
}
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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))
Expand Down
12 changes: 7 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Expand Up @@ -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<Constant>(Cmp.getOperand(1)))
return nullptr;

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 1e36302

Please sign in to comment.