@@ -79,7 +79,7 @@ static Value *generateMinMaxSelectPattern(InstCombiner::BuilderTy *Builder,
79
79
// / a bitmask indicating which operands of this instruction are foldable if they
80
80
// / equal the other incoming value of the select.
81
81
// /
82
- static unsigned GetSelectFoldableOperands (Instruction *I) {
82
+ static unsigned getSelectFoldableOperands (Instruction *I) {
83
83
switch (I->getOpcode ()) {
84
84
case Instruction::Add:
85
85
case Instruction::Mul:
@@ -99,7 +99,7 @@ static unsigned GetSelectFoldableOperands(Instruction *I) {
99
99
100
100
// / For the same transformation as the previous function, return the identity
101
101
// / constant that goes into the select.
102
- static Constant *GetSelectFoldableConstant (Instruction *I) {
102
+ static Constant *getSelectFoldableConstant (Instruction *I) {
103
103
switch (I->getOpcode ()) {
104
104
default : llvm_unreachable (" This cannot happen!" );
105
105
case Instruction::Add:
@@ -118,7 +118,7 @@ static Constant *GetSelectFoldableConstant(Instruction *I) {
118
118
}
119
119
120
120
// / We have (select c, TI, FI), and we know that TI and FI have the same opcode.
121
- Instruction *InstCombiner::FoldSelectOpOp (SelectInst &SI, Instruction *TI,
121
+ Instruction *InstCombiner::foldSelectOpOp (SelectInst &SI, Instruction *TI,
122
122
Instruction *FI) {
123
123
// If this is a cast from the same type, merge.
124
124
if (TI->getNumOperands () == 1 && TI->isCast ()) {
@@ -228,14 +228,14 @@ static bool isSelect01(Constant *C1, Constant *C2) {
228
228
229
229
// / Try to fold the select into one of the operands to allow further
230
230
// / optimization.
231
- Instruction *InstCombiner::FoldSelectIntoOp (SelectInst &SI, Value *TrueVal,
231
+ Instruction *InstCombiner::foldSelectIntoOp (SelectInst &SI, Value *TrueVal,
232
232
Value *FalseVal) {
233
233
// See the comment above GetSelectFoldableOperands for a description of the
234
234
// transformation we are doing here.
235
235
if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
236
236
if (TVI->hasOneUse () && TVI->getNumOperands () == 2 &&
237
237
!isa<Constant>(FalseVal)) {
238
- if (unsigned SFO = GetSelectFoldableOperands (TVI)) {
238
+ if (unsigned SFO = getSelectFoldableOperands (TVI)) {
239
239
unsigned OpToFold = 0 ;
240
240
if ((SFO & 1 ) && FalseVal == TVI->getOperand (0 )) {
241
241
OpToFold = 1 ;
@@ -244,7 +244,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
244
244
}
245
245
246
246
if (OpToFold) {
247
- Constant *C = GetSelectFoldableConstant (TVI);
247
+ Constant *C = getSelectFoldableConstant (TVI);
248
248
Value *OOp = TVI->getOperand (2 -OpToFold);
249
249
// Avoid creating select between 2 constants unless it's selecting
250
250
// between 0, 1 and -1.
@@ -265,7 +265,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
265
265
if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
266
266
if (FVI->hasOneUse () && FVI->getNumOperands () == 2 &&
267
267
!isa<Constant>(TrueVal)) {
268
- if (unsigned SFO = GetSelectFoldableOperands (FVI)) {
268
+ if (unsigned SFO = getSelectFoldableOperands (FVI)) {
269
269
unsigned OpToFold = 0 ;
270
270
if ((SFO & 1 ) && TrueVal == FVI->getOperand (0 )) {
271
271
OpToFold = 1 ;
@@ -274,7 +274,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
274
274
}
275
275
276
276
if (OpToFold) {
277
- Constant *C = GetSelectFoldableConstant (FVI);
277
+ Constant *C = getSelectFoldableConstant (FVI);
278
278
Value *OOp = FVI->getOperand (2 -OpToFold);
279
279
// Avoid creating select between 2 constants unless it's selecting
280
280
// between 0, 1 and -1.
@@ -414,7 +414,7 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
414
414
}
415
415
416
416
// / Visit a SelectInst that has an ICmpInst as its first operand.
417
- Instruction *InstCombiner::visitSelectInstWithICmp (SelectInst &SI,
417
+ Instruction *InstCombiner::foldSelectInstWithICmp (SelectInst &SI,
418
418
ICmpInst *ICI) {
419
419
bool Changed = false ;
420
420
ICmpInst::Predicate Pred = ICI->getPredicate ();
@@ -626,7 +626,7 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
626
626
// /
627
627
// / because Y is not live in BB1/BB2.
628
628
// /
629
- static bool CanSelectOperandBeMappingIntoPredBlock (const Value *V,
629
+ static bool canSelectOperandBeMappingIntoPredBlock (const Value *V,
630
630
const SelectInst &SI) {
631
631
// If the value is a non-instruction value like a constant or argument, it
632
632
// can always be mapped.
@@ -654,7 +654,7 @@ static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
654
654
655
655
// / We have an SPF (e.g. a min or max) of an SPF of the form:
656
656
// / SPF2(SPF1(A, B), C)
657
- Instruction *InstCombiner::FoldSPFofSPF (Instruction *Inner,
657
+ Instruction *InstCombiner::foldSPFofSPF (Instruction *Inner,
658
658
SelectPatternFlavor SPF1,
659
659
Value *A, Value *B,
660
660
Instruction &Outer,
@@ -919,30 +919,32 @@ static Instruction *foldAddSubSelect(SelectInst &SI,
919
919
// / If one of the operands is a sext/zext from i1 and the other is a constant,
920
920
// / we may be able to create an i1 select which can be further folded to
921
921
// / logical ops.
922
- static Instruction *foldSelectExtConst ( InstCombiner::BuilderTy &Builder ,
923
- SelectInst &SI, Instruction *ExtInst,
924
- const APInt &C, bool IsExtTrueVal,
925
- bool IsSigned) {
922
+ Instruction *InstCombiner::foldSelectExtConst (SelectInst &Sel ,
923
+ Instruction *ExtInst,
924
+ const APInt &C) {
925
+ // TODO: Handle larger types? That requires adjusting FoldOpIntoSelect too.
926
926
Value *SmallVal = ExtInst->getOperand (0 );
927
927
Type *SmallType = SmallVal->getType ();
928
-
929
- // TODO: Handle larger types? That requires adjusting FoldOpIntoSelect too.
930
928
if (!SmallType->getScalarType ()->isIntegerTy (1 ))
931
929
return nullptr ;
932
930
933
- if (C != 0 && (IsSigned || C != 1 ) && (!IsSigned || !C.isAllOnesValue ()))
934
- return nullptr ;
931
+ Value *Cond = Sel.getCondition ();
932
+ bool IsExtTrueVal = Sel.getTrueValue () == ExtInst;
933
+ bool IsSext = ExtInst->getOpcode () == Instruction::SExt;
934
+ if (C == 0 || (!IsSext && C == 1 ) || (IsSext && C.isAllOnesValue ())) {
935
+ Value *SmallConst = ConstantInt::get (SmallType, C.trunc (1 ));
936
+ Value *TrueVal = IsExtTrueVal ? SmallVal : SmallConst;
937
+ Value *FalseVal = IsExtTrueVal ? SmallConst : SmallVal;
938
+ Value *NewSel = Builder->CreateSelect (Cond, TrueVal, FalseVal,
939
+ " fold." + Sel.getName (), &Sel);
935
940
936
- Value *SmallConst = ConstantInt::get (SmallType, C.trunc (1 ));
937
- Value *TrueVal = IsExtTrueVal ? SmallVal : SmallConst;
938
- Value *FalseVal = IsExtTrueVal ? SmallConst : SmallVal;
939
- Value *Select = Builder.CreateSelect (SI.getOperand (0 ), TrueVal, FalseVal,
940
- " fold." + SI.getName (), &SI);
941
+ if (IsSext)
942
+ return new SExtInst (NewSel, Sel.getType ());
941
943
942
- if (IsSigned)
943
- return new SExtInst (Select, SI. getType ());
944
+ return new ZExtInst (NewSel, Sel. getType ());
945
+ }
944
946
945
- return new ZExtInst (Select, SI. getType ()) ;
947
+ return nullptr ;
946
948
}
947
949
948
950
// / Try to transform a vector select with a constant condition vector into a
@@ -1157,7 +1159,7 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
1157
1159
1158
1160
// See if we are selecting two values based on a comparison of the two values.
1159
1161
if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
1160
- if (Instruction *Result = visitSelectInstWithICmp (SI, ICI))
1162
+ if (Instruction *Result = foldSelectInstWithICmp (SI, ICI))
1161
1163
return Result;
1162
1164
1163
1165
if (Instruction *Add = foldAddSubSelect (SI, *Builder))
@@ -1167,32 +1169,30 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
1167
1169
auto *TI = dyn_cast<Instruction>(TrueVal);
1168
1170
auto *FI = dyn_cast<Instruction>(FalseVal);
1169
1171
if (TI && FI && TI->getOpcode () == FI->getOpcode ())
1170
- if (Instruction *IV = FoldSelectOpOp (SI, TI, FI))
1172
+ if (Instruction *IV = foldSelectOpOp (SI, TI, FI))
1171
1173
return IV;
1172
1174
1173
1175
// (select C, (ext X), const) -> (ext (select C, X, const')) and variations
1174
1176
// thereof when extending from i1, as that allows further folding into logic
1175
1177
// ops. When the sext is from a larger type, prefer to have it as an operand.
1176
1178
if (TI && (TI->getOpcode () == Instruction::ZExt ||
1177
1179
TI->getOpcode () == Instruction::SExt)) {
1178
- bool IsSExt = TI->getOpcode () == Instruction::SExt;
1179
1180
const APInt *C;
1180
1181
if (match (FalseVal, m_APInt (C)))
1181
- if (auto *I = foldSelectExtConst (*Builder, SI, TI, *C, true , IsSExt ))
1182
+ if (auto *I = foldSelectExtConst (SI, TI, *C))
1182
1183
return I;
1183
1184
}
1184
1185
if (FI && (FI->getOpcode () == Instruction::ZExt ||
1185
1186
FI->getOpcode () == Instruction::SExt)) {
1186
- bool IsSExt = FI->getOpcode () == Instruction::SExt;
1187
1187
const APInt *C;
1188
1188
if (match (TrueVal, m_APInt (C)))
1189
- if (auto *I = foldSelectExtConst (*Builder, SI, FI, *C, false , IsSExt ))
1189
+ if (auto *I = foldSelectExtConst (SI, FI, *C))
1190
1190
return I;
1191
1191
}
1192
1192
1193
1193
// See if we can fold the select into one of our operands.
1194
1194
if (SelType->isIntOrIntVectorTy () || SelType->isFPOrFPVectorTy ()) {
1195
- if (Instruction *FoldI = FoldSelectIntoOp (SI, TrueVal, FalseVal))
1195
+ if (Instruction *FoldI = foldSelectIntoOp (SI, TrueVal, FalseVal))
1196
1196
return FoldI;
1197
1197
1198
1198
Value *LHS, *RHS, *LHS2, *RHS2;
@@ -1231,11 +1231,11 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
1231
1231
// ABS(ABS(a)) -> ABS(a)
1232
1232
// NABS(NABS(a)) -> NABS(a)
1233
1233
if (SelectPatternFlavor SPF2 = matchSelectPattern (LHS, LHS2, RHS2).Flavor )
1234
- if (Instruction *R = FoldSPFofSPF (cast<Instruction>(LHS),SPF2,LHS2,RHS2,
1234
+ if (Instruction *R = foldSPFofSPF (cast<Instruction>(LHS),SPF2,LHS2,RHS2,
1235
1235
SI, SPF, RHS))
1236
1236
return R;
1237
1237
if (SelectPatternFlavor SPF2 = matchSelectPattern (RHS, LHS2, RHS2).Flavor )
1238
- if (Instruction *R = FoldSPFofSPF (cast<Instruction>(RHS),SPF2,LHS2,RHS2,
1238
+ if (Instruction *R = foldSPFofSPF (cast<Instruction>(RHS),SPF2,LHS2,RHS2,
1239
1239
SI, SPF, LHS))
1240
1240
return R;
1241
1241
}
@@ -1274,8 +1274,8 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
1274
1274
// See if we can fold the select into a phi node if the condition is a select.
1275
1275
if (isa<PHINode>(SI.getCondition ()))
1276
1276
// The true/false values have to be live in the PHI predecessor's blocks.
1277
- if (CanSelectOperandBeMappingIntoPredBlock (TrueVal, SI) &&
1278
- CanSelectOperandBeMappingIntoPredBlock (FalseVal, SI))
1277
+ if (canSelectOperandBeMappingIntoPredBlock (TrueVal, SI) &&
1278
+ canSelectOperandBeMappingIntoPredBlock (FalseVal, SI))
1279
1279
if (Instruction *NV = FoldOpIntoPhi (SI))
1280
1280
return NV;
1281
1281
0 commit comments