Skip to content

Commit d0de2c5

Browse files
committed
[InstCombine] Simplify foldOperationIntoSelectOperand() (NFCI)
Rather than handling all instruction types separately, clone the original instruction and replace the select operand.
1 parent 9f48562 commit d0de2c5

File tree

1 file changed

+8
-41
lines changed

1 file changed

+8
-41
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,45 +1044,12 @@ static Constant *constantFoldOperationIntoSelectOperand(
10441044
return ConstantFoldInstOperands(&I, ConstOps, I.getModule()->getDataLayout());
10451045
}
10461046

1047-
static Value *foldOperationIntoSelectOperand(Instruction &I, Value *SO,
1048-
InstCombiner::BuilderTy &Builder) {
1049-
if (auto *Cast = dyn_cast<CastInst>(&I))
1050-
return Builder.CreateCast(Cast->getOpcode(), SO, I.getType());
1051-
1052-
if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
1053-
assert(canConstantFoldCallTo(II, cast<Function>(II->getCalledOperand())) &&
1054-
"Expected constant-foldable intrinsic");
1055-
Intrinsic::ID IID = II->getIntrinsicID();
1056-
if (II->arg_size() == 1)
1057-
return Builder.CreateUnaryIntrinsic(IID, SO);
1058-
1059-
// This works for real binary ops like min/max (where we always expect the
1060-
// constant operand to be canonicalized as op1) and unary ops with a bonus
1061-
// constant argument like ctlz/cttz.
1062-
// TODO: Handle non-commutative binary intrinsics as below for binops.
1063-
assert(II->arg_size() == 2 && "Expected binary intrinsic");
1064-
assert(isa<Constant>(II->getArgOperand(1)) && "Expected constant operand");
1065-
return Builder.CreateBinaryIntrinsic(IID, SO, II->getArgOperand(1));
1066-
}
1067-
1068-
if (auto *EI = dyn_cast<ExtractElementInst>(&I))
1069-
return Builder.CreateExtractElement(SO, EI->getIndexOperand());
1070-
1071-
assert(I.isBinaryOp() && "Unexpected opcode for select folding");
1072-
1073-
// Figure out if the constant is the left or the right argument.
1074-
bool ConstIsRHS = isa<Constant>(I.getOperand(1));
1075-
Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
1076-
1077-
Value *Op0 = SO, *Op1 = ConstOperand;
1078-
if (!ConstIsRHS)
1079-
std::swap(Op0, Op1);
1080-
1081-
Value *NewBO = Builder.CreateBinOp(cast<BinaryOperator>(&I)->getOpcode(), Op0,
1082-
Op1, SO->getName() + ".op");
1083-
if (auto *NewBOI = dyn_cast<Instruction>(NewBO))
1084-
NewBOI->copyIRFlags(&I);
1085-
return NewBO;
1047+
static Value *foldOperationIntoSelectOperand(Instruction &I, SelectInst *SI,
1048+
Value *NewOp, InstCombiner &IC) {
1049+
Instruction *Clone = I.clone();
1050+
Clone->replaceUsesOfWith(SI, NewOp);
1051+
IC.InsertNewInstBefore(Clone, *SI);
1052+
return Clone;
10861053
}
10871054

10881055
Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
@@ -1162,9 +1129,9 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
11621129

11631130
// Create an instruction for the arm that did not fold.
11641131
if (!NewTV)
1165-
NewTV = foldOperationIntoSelectOperand(Op, TV, Builder);
1132+
NewTV = foldOperationIntoSelectOperand(Op, SI, TV, *this);
11661133
if (!NewFV)
1167-
NewFV = foldOperationIntoSelectOperand(Op, FV, Builder);
1134+
NewFV = foldOperationIntoSelectOperand(Op, SI, FV, *this);
11681135
return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
11691136
}
11701137

0 commit comments

Comments
 (0)