Skip to content

Commit

Permalink
[InstCombine] Use replaceOperand() in more places
Browse files Browse the repository at this point in the history
Followup to D73919 with another batch of replacements of
setOperand() -> replaceOperand(), to make sure the old
operand gets DCEd right away.

Differential Revision: https://reviews.llvm.org/D74932
  • Loading branch information
nikic committed Feb 21, 2020
1 parent deb0a8b commit 656dff9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 64 deletions.
76 changes: 32 additions & 44 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,10 +1954,9 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
// 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");
Expand Down Expand Up @@ -2256,16 +2255,16 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
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;
}

Expand Down Expand Up @@ -2303,10 +2302,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
// copysign X, (copysign ?, SignArg) --> copysign X, SignArg
Value *SignArg;
if (match(II->getArgOperand(1),
m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(SignArg)))) {
II->setArgOperand(1, SignArg);
return II;
}
m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(SignArg))))
return replaceOperand(*II, 1, SignArg);

break;
}
Expand Down Expand Up @@ -2343,8 +2340,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
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;
}
Expand Down Expand Up @@ -2703,8 +2699,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
cast<Instruction>(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;
Expand Down Expand Up @@ -3327,12 +3323,10 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT);
unsigned AlignArg = II->getNumArgOperands() - 1;
ConstantInt *IntrAlign = dyn_cast<ConstantInt>(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;
}

Expand Down Expand Up @@ -3391,8 +3385,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
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;
Expand Down Expand Up @@ -3559,11 +3553,9 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
}

// 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<ConstantFP>(Src0);
if (!CVal) {
Expand Down Expand Up @@ -3653,23 +3645,19 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
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<ConstantInt>(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;
Expand Down Expand Up @@ -3712,7 +3700,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
(IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) {
Value *Src = II->getArgOperand(I + 2);
if (!isa<UndefValue>(Src)) {
II->setArgOperand(I + 2, UndefValue::get(Src->getType()));
replaceOperand(*II, I + 2, UndefValue::get(Src->getType()));
Changed = true;
}
}
Expand Down Expand Up @@ -3851,8 +3839,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
((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;
}

Expand Down Expand Up @@ -4196,7 +4184,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
MoveI = MoveI->getNextNonDebugInstruction();
Temp->moveBefore(II);
}
II->setArgOperand(0, Builder.CreateAnd(CurrCond, NextCond));
replaceOperand(*II, 0, Builder.CreateAnd(CurrCond, NextCond));
}
eraseInstFromFunction(*NextInst);
return II;
Expand Down
36 changes: 16 additions & 20 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
// 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);

Expand Down Expand Up @@ -384,8 +384,8 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
// 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);
Expand All @@ -412,8 +412,8 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
// 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);
Expand All @@ -432,8 +432,8 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
// 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);
Expand Down Expand Up @@ -466,8 +466,8 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
}
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);
Expand Down Expand Up @@ -2756,18 +2756,16 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
if (match(&BI, m_Br(m_Not(m_Value(X)), m_BasicBlock(), m_BasicBlock())) &&
!isa<Constant>(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<ConstantInt>(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;
Expand Down Expand Up @@ -2797,8 +2795,7 @@ Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
"Result of expression should be constant");
Case.setValue(cast<ConstantInt>(NewCase));
}
SI.setCondition(Op0);
return &SI;
return replaceOperand(SI, 0, Op0);
}

KnownBits Known = computeKnownBits(Cond, 0, &SI);
Expand All @@ -2825,13 +2822,12 @@ Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
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;
Expand Down

0 comments on commit 656dff9

Please sign in to comment.