@@ -1044,45 +1044,12 @@ static Constant *constantFoldOperationIntoSelectOperand(
1044
1044
return ConstantFoldInstOperands (&I, ConstOps, I.getModule ()->getDataLayout ());
1045
1045
}
1046
1046
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;
1086
1053
}
1087
1054
1088
1055
Instruction *InstCombinerImpl::FoldOpIntoSelect (Instruction &Op, SelectInst *SI,
@@ -1162,9 +1129,9 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
1162
1129
1163
1130
// Create an instruction for the arm that did not fold.
1164
1131
if (!NewTV)
1165
- NewTV = foldOperationIntoSelectOperand (Op, TV, Builder );
1132
+ NewTV = foldOperationIntoSelectOperand (Op, SI, TV, * this );
1166
1133
if (!NewFV)
1167
- NewFV = foldOperationIntoSelectOperand (Op, FV, Builder );
1134
+ NewFV = foldOperationIntoSelectOperand (Op, SI, FV, * this );
1168
1135
return SelectInst::Create (SI->getCondition (), NewTV, NewFV, " " , nullptr , SI);
1169
1136
}
1170
1137
0 commit comments