Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8a50db9

Browse files
committed
[InstCombine] fix function names; NFC
Also, make foldSelectExtConst() a member of InstCombiner, remove unnecessary parameters from its interface, and group visitSelectInst helpers together in the header file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@282796 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d4dee42 commit 8a50db9

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
258258
Instruction *visitIntToPtr(IntToPtrInst &CI);
259259
Instruction *visitBitCast(BitCastInst &CI);
260260
Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
261-
Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
262-
Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
263-
Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
264-
Value *A, Value *B, Instruction &Outer,
265-
SelectPatternFlavor SPF2, Value *C);
266261
Instruction *FoldItoFPtoI(Instruction &FI);
267262
Instruction *visitSelectInst(SelectInst &SI);
268-
Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
269263
Instruction *visitCallInst(CallInst &CI);
270264
Instruction *visitInvokeInst(InvokeInst &II);
271265

@@ -595,6 +589,16 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
595589
const APInt *C);
596590
Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C);
597591

592+
// Helpers of visitSelectInst().
593+
Instruction *foldSelectExtConst(SelectInst &Sel, Instruction *ExtInst,
594+
const APInt &C);
595+
Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
596+
Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
597+
Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
598+
Value *A, Value *B, Instruction &Outer,
599+
SelectPatternFlavor SPF2, Value *C);
600+
Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
601+
598602
Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
599603
ConstantInt *AndRHS, BinaryOperator &TheAnd);
600604

lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static Value *generateMinMaxSelectPattern(InstCombiner::BuilderTy *Builder,
7979
/// a bitmask indicating which operands of this instruction are foldable if they
8080
/// equal the other incoming value of the select.
8181
///
82-
static unsigned GetSelectFoldableOperands(Instruction *I) {
82+
static unsigned getSelectFoldableOperands(Instruction *I) {
8383
switch (I->getOpcode()) {
8484
case Instruction::Add:
8585
case Instruction::Mul:
@@ -99,7 +99,7 @@ static unsigned GetSelectFoldableOperands(Instruction *I) {
9999

100100
/// For the same transformation as the previous function, return the identity
101101
/// constant that goes into the select.
102-
static Constant *GetSelectFoldableConstant(Instruction *I) {
102+
static Constant *getSelectFoldableConstant(Instruction *I) {
103103
switch (I->getOpcode()) {
104104
default: llvm_unreachable("This cannot happen!");
105105
case Instruction::Add:
@@ -118,7 +118,7 @@ static Constant *GetSelectFoldableConstant(Instruction *I) {
118118
}
119119

120120
/// 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,
122122
Instruction *FI) {
123123
// If this is a cast from the same type, merge.
124124
if (TI->getNumOperands() == 1 && TI->isCast()) {
@@ -228,14 +228,14 @@ static bool isSelect01(Constant *C1, Constant *C2) {
228228

229229
/// Try to fold the select into one of the operands to allow further
230230
/// optimization.
231-
Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
231+
Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
232232
Value *FalseVal) {
233233
// See the comment above GetSelectFoldableOperands for a description of the
234234
// transformation we are doing here.
235235
if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
236236
if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
237237
!isa<Constant>(FalseVal)) {
238-
if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
238+
if (unsigned SFO = getSelectFoldableOperands(TVI)) {
239239
unsigned OpToFold = 0;
240240
if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
241241
OpToFold = 1;
@@ -244,7 +244,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
244244
}
245245

246246
if (OpToFold) {
247-
Constant *C = GetSelectFoldableConstant(TVI);
247+
Constant *C = getSelectFoldableConstant(TVI);
248248
Value *OOp = TVI->getOperand(2-OpToFold);
249249
// Avoid creating select between 2 constants unless it's selecting
250250
// between 0, 1 and -1.
@@ -265,7 +265,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
265265
if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
266266
if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
267267
!isa<Constant>(TrueVal)) {
268-
if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
268+
if (unsigned SFO = getSelectFoldableOperands(FVI)) {
269269
unsigned OpToFold = 0;
270270
if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
271271
OpToFold = 1;
@@ -274,7 +274,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
274274
}
275275

276276
if (OpToFold) {
277-
Constant *C = GetSelectFoldableConstant(FVI);
277+
Constant *C = getSelectFoldableConstant(FVI);
278278
Value *OOp = FVI->getOperand(2-OpToFold);
279279
// Avoid creating select between 2 constants unless it's selecting
280280
// between 0, 1 and -1.
@@ -414,7 +414,7 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
414414
}
415415

416416
/// Visit a SelectInst that has an ICmpInst as its first operand.
417-
Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
417+
Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI,
418418
ICmpInst *ICI) {
419419
bool Changed = false;
420420
ICmpInst::Predicate Pred = ICI->getPredicate();
@@ -626,7 +626,7 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
626626
///
627627
/// because Y is not live in BB1/BB2.
628628
///
629-
static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
629+
static bool canSelectOperandBeMappingIntoPredBlock(const Value *V,
630630
const SelectInst &SI) {
631631
// If the value is a non-instruction value like a constant or argument, it
632632
// can always be mapped.
@@ -654,7 +654,7 @@ static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
654654

655655
/// We have an SPF (e.g. a min or max) of an SPF of the form:
656656
/// SPF2(SPF1(A, B), C)
657-
Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner,
657+
Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner,
658658
SelectPatternFlavor SPF1,
659659
Value *A, Value *B,
660660
Instruction &Outer,
@@ -919,30 +919,32 @@ static Instruction *foldAddSubSelect(SelectInst &SI,
919919
/// If one of the operands is a sext/zext from i1 and the other is a constant,
920920
/// we may be able to create an i1 select which can be further folded to
921921
/// 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.
926926
Value *SmallVal = ExtInst->getOperand(0);
927927
Type *SmallType = SmallVal->getType();
928-
929-
// TODO: Handle larger types? That requires adjusting FoldOpIntoSelect too.
930928
if (!SmallType->getScalarType()->isIntegerTy(1))
931929
return nullptr;
932930

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);
935940

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());
941943

942-
if (IsSigned)
943-
return new SExtInst(Select, SI.getType());
944+
return new ZExtInst(NewSel, Sel.getType());
945+
}
944946

945-
return new ZExtInst(Select, SI.getType());
947+
return nullptr;
946948
}
947949

948950
/// Try to transform a vector select with a constant condition vector into a
@@ -1157,7 +1159,7 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
11571159

11581160
// See if we are selecting two values based on a comparison of the two values.
11591161
if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
1160-
if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
1162+
if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
11611163
return Result;
11621164

11631165
if (Instruction *Add = foldAddSubSelect(SI, *Builder))
@@ -1167,32 +1169,30 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
11671169
auto *TI = dyn_cast<Instruction>(TrueVal);
11681170
auto *FI = dyn_cast<Instruction>(FalseVal);
11691171
if (TI && FI && TI->getOpcode() == FI->getOpcode())
1170-
if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
1172+
if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
11711173
return IV;
11721174

11731175
// (select C, (ext X), const) -> (ext (select C, X, const')) and variations
11741176
// thereof when extending from i1, as that allows further folding into logic
11751177
// ops. When the sext is from a larger type, prefer to have it as an operand.
11761178
if (TI && (TI->getOpcode() == Instruction::ZExt ||
11771179
TI->getOpcode() == Instruction::SExt)) {
1178-
bool IsSExt = TI->getOpcode() == Instruction::SExt;
11791180
const APInt *C;
11801181
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))
11821183
return I;
11831184
}
11841185
if (FI && (FI->getOpcode() == Instruction::ZExt ||
11851186
FI->getOpcode() == Instruction::SExt)) {
1186-
bool IsSExt = FI->getOpcode() == Instruction::SExt;
11871187
const APInt *C;
11881188
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))
11901190
return I;
11911191
}
11921192

11931193
// See if we can fold the select into one of our operands.
11941194
if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
1195-
if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
1195+
if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
11961196
return FoldI;
11971197

11981198
Value *LHS, *RHS, *LHS2, *RHS2;
@@ -1231,11 +1231,11 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
12311231
// ABS(ABS(a)) -> ABS(a)
12321232
// NABS(NABS(a)) -> NABS(a)
12331233
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,
12351235
SI, SPF, RHS))
12361236
return R;
12371237
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,
12391239
SI, SPF, LHS))
12401240
return R;
12411241
}
@@ -1274,8 +1274,8 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
12741274
// See if we can fold the select into a phi node if the condition is a select.
12751275
if (isa<PHINode>(SI.getCondition()))
12761276
// 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))
12791279
if (Instruction *NV = FoldOpIntoPhi(SI))
12801280
return NV;
12811281

0 commit comments

Comments
 (0)