Skip to content

Commit 779fd39

Browse files
committed
Reapply [InstCombine] Switch foldOpIntoPhi() to use InstSimplify
Relative to the previous attempt, this is rebased over the InstSimplify fix in ac74e7a, which addresses the miscompile reported in PR58401. ----- foldOpIntoPhi() currently only folds operations into the phi if all but one operands constant-fold. The two exceptions to this are freeze and select, where we allow more general simplification. This patch makes foldOpIntoPhi() generally simplification based and removes all the instruction-specific logic. We just try to simplify the instruction for each operand, and for the (potentially) one non-simplified operand, we move it into the new block with adjusted operands. This fixes #57448, which was my original motivation for the change. Differential Revision: https://reviews.llvm.org/D134954
1 parent efd0d66 commit 779fd39

File tree

8 files changed

+80
-148
lines changed

8 files changed

+80
-148
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+55-115
Original file line numberDiff line numberDiff line change
@@ -1155,22 +1155,6 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
11551155
return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
11561156
}
11571157

1158-
static Value *foldOperationIntoPhiValue(BinaryOperator *I, Value *InV,
1159-
InstCombiner::BuilderTy &Builder) {
1160-
bool ConstIsRHS = isa<Constant>(I->getOperand(1));
1161-
Constant *C = cast<Constant>(I->getOperand(ConstIsRHS));
1162-
1163-
Value *Op0 = InV, *Op1 = C;
1164-
if (!ConstIsRHS)
1165-
std::swap(Op0, Op1);
1166-
1167-
Value *RI = Builder.CreateBinOp(I->getOpcode(), Op0, Op1, "phi.bo");
1168-
auto *FPInst = dyn_cast<Instruction>(RI);
1169-
if (FPInst && isa<FPMathOperator>(FPInst))
1170-
FPInst->copyFastMathFlags(I);
1171-
return RI;
1172-
}
1173-
11741158
Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
11751159
unsigned NumPHIValues = PN->getNumIncomingValues();
11761160
if (NumPHIValues == 0)
@@ -1189,48 +1173,69 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
11891173
// Otherwise, we can replace *all* users with the new PHI we form.
11901174
}
11911175

1192-
// Check to see if all of the operands of the PHI are simple constants
1193-
// (constantint/constantfp/undef). If there is one non-constant value,
1194-
// remember the BB it is in. If there is more than one or if *it* is a PHI,
1195-
// bail out. We don't do arbitrary constant expressions here because moving
1196-
// their computation can be expensive without a cost model.
1197-
BasicBlock *NonConstBB = nullptr;
1176+
// Check to see whether the instruction can be folded into each phi operand.
1177+
// If there is one operand that does not fold, remember the BB it is in.
1178+
// If there is more than one or if *it* is a PHI, bail out.
1179+
SmallVector<Value *> NewPhiValues;
1180+
BasicBlock *NonSimplifiedBB = nullptr;
1181+
Value *NonSimplifiedInVal = nullptr;
11981182
for (unsigned i = 0; i != NumPHIValues; ++i) {
11991183
Value *InVal = PN->getIncomingValue(i);
1200-
// For non-freeze, require constant operand
1201-
// For freeze, require non-undef, non-poison operand
1202-
if (!isa<FreezeInst>(I) && match(InVal, m_ImmConstant()))
1203-
continue;
1204-
if (isa<FreezeInst>(I) && isGuaranteedNotToBeUndefOrPoison(InVal))
1184+
BasicBlock *InBB = PN->getIncomingBlock(i);
1185+
1186+
// NB: It is a precondition of this transform that the operands be
1187+
// phi translatable! This is usually trivially satisfied by limiting it
1188+
// to constant ops, and for selects we do a more sophisticated check.
1189+
SmallVector<Value *> Ops;
1190+
for (Value *Op : I.operands()) {
1191+
if (Op == PN)
1192+
Ops.push_back(InVal);
1193+
else
1194+
Ops.push_back(Op->DoPHITranslation(PN->getParent(), InBB));
1195+
}
1196+
1197+
// Don't consider the simplification successful if we get back a constant
1198+
// expression. That's just an instruction in hiding.
1199+
// Also reject the case where we simplify back to the phi node. We wouldn't
1200+
// be able to remove it in that case.
1201+
Value *NewVal = simplifyInstructionWithOperands(
1202+
&I, Ops, SQ.getWithInstruction(InBB->getTerminator()));
1203+
if (NewVal && NewVal != PN && !match(NewVal, m_ConstantExpr())) {
1204+
NewPhiValues.push_back(NewVal);
12051205
continue;
1206+
}
12061207

12071208
if (isa<PHINode>(InVal)) return nullptr; // Itself a phi.
1208-
if (NonConstBB) return nullptr; // More than one non-const value.
1209+
if (NonSimplifiedBB) return nullptr; // More than one non-simplified value.
12091210

1210-
NonConstBB = PN->getIncomingBlock(i);
1211+
NonSimplifiedBB = InBB;
1212+
NonSimplifiedInVal = InVal;
1213+
NewPhiValues.push_back(nullptr);
12111214

12121215
// If the InVal is an invoke at the end of the pred block, then we can't
12131216
// insert a computation after it without breaking the edge.
12141217
if (isa<InvokeInst>(InVal))
1215-
if (cast<Instruction>(InVal)->getParent() == NonConstBB)
1218+
if (cast<Instruction>(InVal)->getParent() == NonSimplifiedBB)
12161219
return nullptr;
12171220

12181221
// If the incoming non-constant value is reachable from the phis block,
12191222
// we'll push the operation across a loop backedge. This could result in
12201223
// an infinite combine loop, and is generally non-profitable (especially
12211224
// if the operation was originally outside the loop).
1222-
if (isPotentiallyReachable(PN->getParent(), NonConstBB, nullptr, &DT, LI))
1225+
if (isPotentiallyReachable(PN->getParent(), NonSimplifiedBB, nullptr, &DT,
1226+
LI))
12231227
return nullptr;
12241228
}
12251229

1226-
// If there is exactly one non-constant value, we can insert a copy of the
1230+
// If there is exactly one non-simplified value, we can insert a copy of the
12271231
// operation in that block. However, if this is a critical edge, we would be
12281232
// inserting the computation on some other paths (e.g. inside a loop). Only
12291233
// do this if the pred block is unconditionally branching into the phi block.
12301234
// Also, make sure that the pred block is not dead code.
1231-
if (NonConstBB != nullptr) {
1232-
BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
1233-
if (!BI || !BI->isUnconditional() || !DT.isReachableFromEntry(NonConstBB))
1235+
if (NonSimplifiedBB != nullptr) {
1236+
BranchInst *BI = dyn_cast<BranchInst>(NonSimplifiedBB->getTerminator());
1237+
if (!BI || !BI->isUnconditional() ||
1238+
!DT.isReachableFromEntry(NonSimplifiedBB))
12341239
return nullptr;
12351240
}
12361241

@@ -1241,88 +1246,23 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
12411246

12421247
// If we are going to have to insert a new computation, do so right before the
12431248
// predecessor's terminator.
1244-
if (NonConstBB)
1245-
Builder.SetInsertPoint(NonConstBB->getTerminator());
1246-
1247-
// Next, add all of the operands to the PHI.
1248-
if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
1249-
// We only currently try to fold the condition of a select when it is a phi,
1250-
// not the true/false values.
1251-
Value *TrueV = SI->getTrueValue();
1252-
Value *FalseV = SI->getFalseValue();
1253-
BasicBlock *PhiTransBB = PN->getParent();
1254-
for (unsigned i = 0; i != NumPHIValues; ++i) {
1255-
BasicBlock *ThisBB = PN->getIncomingBlock(i);
1256-
Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
1257-
Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
1258-
Value *InV = nullptr;
1259-
// Beware of ConstantExpr: it may eventually evaluate to getNullValue,
1260-
// even if currently isNullValue gives false.
1261-
Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i));
1262-
// For vector constants, we cannot use isNullValue to fold into
1263-
// FalseVInPred versus TrueVInPred. When we have individual nonzero
1264-
// elements in the vector, we will incorrectly fold InC to
1265-
// `TrueVInPred`.
1266-
if (InC && isa<ConstantInt>(InC))
1267-
InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
1268-
else {
1269-
// Generate the select in the same block as PN's current incoming block.
1270-
// Note: ThisBB need not be the NonConstBB because vector constants
1271-
// which are constants by definition are handled here.
1272-
// FIXME: This can lead to an increase in IR generation because we might
1273-
// generate selects for vector constant phi operand, that could not be
1274-
// folded to TrueVInPred or FalseVInPred as done for ConstantInt. For
1275-
// non-vector phis, this transformation was always profitable because
1276-
// the select would be generated exactly once in the NonConstBB.
1277-
Builder.SetInsertPoint(ThisBB->getTerminator());
1278-
InV = Builder.CreateSelect(PN->getIncomingValue(i), TrueVInPred,
1279-
FalseVInPred, "phi.sel");
1280-
}
1281-
NewPN->addIncoming(InV, ThisBB);
1282-
}
1283-
} else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) {
1284-
Constant *C = cast<Constant>(I.getOperand(1));
1285-
for (unsigned i = 0; i != NumPHIValues; ++i) {
1286-
Value *InV = nullptr;
1287-
if (auto *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
1288-
InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
1289-
else
1290-
InV = Builder.CreateCmp(CI->getPredicate(), PN->getIncomingValue(i),
1291-
C, "phi.cmp");
1292-
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
1293-
}
1294-
} else if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
1295-
for (unsigned i = 0; i != NumPHIValues; ++i) {
1296-
Value *InV = foldOperationIntoPhiValue(BO, PN->getIncomingValue(i),
1297-
Builder);
1298-
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
1299-
}
1300-
} else if (isa<FreezeInst>(&I)) {
1301-
for (unsigned i = 0; i != NumPHIValues; ++i) {
1302-
Value *InV;
1303-
if (NonConstBB == PN->getIncomingBlock(i))
1304-
InV = Builder.CreateFreeze(PN->getIncomingValue(i), "phi.fr");
1249+
Instruction *Clone = nullptr;
1250+
if (NonSimplifiedBB) {
1251+
Clone = I.clone();
1252+
for (Use &U : Clone->operands()) {
1253+
if (U == PN)
1254+
U = NonSimplifiedInVal;
13051255
else
1306-
InV = PN->getIncomingValue(i);
1307-
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
1308-
}
1309-
} else if (auto *EV = dyn_cast<ExtractValueInst>(&I)) {
1310-
for (unsigned i = 0; i != NumPHIValues; ++i)
1311-
NewPN->addIncoming(Builder.CreateExtractValue(PN->getIncomingValue(i),
1312-
EV->getIndices(), "phi.ev"),
1313-
PN->getIncomingBlock(i));
1314-
} else {
1315-
CastInst *CI = cast<CastInst>(&I);
1316-
Type *RetTy = CI->getType();
1317-
for (unsigned i = 0; i != NumPHIValues; ++i) {
1318-
Value *InV;
1319-
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
1320-
InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
1321-
else
1322-
InV = Builder.CreateCast(CI->getOpcode(), PN->getIncomingValue(i),
1323-
I.getType(), "phi.cast");
1324-
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
1256+
U = U->DoPHITranslation(PN->getParent(), NonSimplifiedBB);
13251257
}
1258+
InsertNewInstBefore(Clone, *NonSimplifiedBB->getTerminator());
1259+
}
1260+
1261+
for (unsigned i = 0; i != NumPHIValues; ++i) {
1262+
if (NewPhiValues[i])
1263+
NewPN->addIncoming(NewPhiValues[i], PN->getIncomingBlock(i));
1264+
else
1265+
NewPN->addIncoming(Clone, PN->getIncomingBlock(i));
13261266
}
13271267

13281268
for (User *U : make_early_inc_range(PN->users())) {

llvm/test/Transforms/InstCombine/intptr1.ll

+4-5
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,18 @@ define void @test1_neg(ptr %a, ptr readnone %a_end, ptr %b.i64) {
5757
; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_END:%.*]]
5858
; CHECK: for.body.preheader:
5959
; CHECK-NEXT: [[B:%.*]] = load i64, ptr [[B_I64:%.*]], align 8
60+
; CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[B]] to ptr
6061
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
6162
; CHECK: for.body:
6263
; CHECK-NEXT: [[A_ADDR_03:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[BB:%.*]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ]
63-
; CHECK-NEXT: [[B_ADDR_02:%.*]] = phi i64 [ [[ADD_INT:%.*]], [[BB]] ], [ [[B]], [[FOR_BODY_PREHEADER]] ]
64-
; CHECK-NEXT: [[TMP:%.*]] = inttoptr i64 [[B_ADDR_02]] to ptr
65-
; CHECK-NEXT: [[PTRCMP:%.*]] = icmp ult ptr [[TMP]], [[A_END]]
64+
; CHECK-NEXT: [[B_ADDR_02:%.*]] = phi ptr [ [[ADD:%.*]], [[BB]] ], [ [[TMP0]], [[FOR_BODY_PREHEADER]] ]
65+
; CHECK-NEXT: [[PTRCMP:%.*]] = icmp ult ptr [[B_ADDR_02]], [[A_END]]
6666
; CHECK-NEXT: br i1 [[PTRCMP]], label [[FOR_END]], label [[BB]]
6767
; CHECK: bb:
6868
; CHECK-NEXT: [[I1:%.*]] = load float, ptr [[A]], align 4
6969
; CHECK-NEXT: [[MUL_I:%.*]] = fmul float [[I1]], 4.200000e+01
7070
; CHECK-NEXT: store float [[MUL_I]], ptr [[A_ADDR_03]], align 4
71-
; CHECK-NEXT: [[ADD:%.*]] = getelementptr inbounds float, ptr [[A]], i64 1
72-
; CHECK-NEXT: [[ADD_INT]] = ptrtoint ptr [[ADD]] to i64
71+
; CHECK-NEXT: [[ADD]] = getelementptr inbounds float, ptr [[A]], i64 1
7372
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds float, ptr [[A_ADDR_03]], i64 1
7473
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[INCDEC_PTR]], [[A_END]]
7574
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END]]

llvm/test/Transforms/InstCombine/intptr4.ll

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ define void @test(ptr %a, ptr readnone %a_end, i64 %b, ptr %bf) unnamed_addr {
88
; CHECK-NEXT: [[B_FLOAT:%.*]] = inttoptr i64 [[B:%.*]] to ptr
99
; CHECK-NEXT: br i1 [[CMP1]], label [[BB1:%.*]], label [[BB2:%.*]]
1010
; CHECK: bb1:
11+
; CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[B]] to ptr
1112
; CHECK-NEXT: br label [[FOR_BODY_PREHEADER:%.*]]
1213
; CHECK: bb2:
13-
; CHECK-NEXT: [[BFI:%.*]] = ptrtoint ptr [[BF:%.*]] to i64
1414
; CHECK-NEXT: br label [[FOR_BODY_PREHEADER]]
1515
; CHECK: for.body.preheader:
16-
; CHECK-NEXT: [[B_PHI:%.*]] = phi i64 [ [[B]], [[BB1]] ], [ [[BFI]], [[BB2]] ]
17-
; CHECK-NEXT: [[B_PHI_PTR:%.*]] = inttoptr i64 [[B_PHI]] to ptr
16+
; CHECK-NEXT: [[B_PHI:%.*]] = phi ptr [ [[TMP0]], [[BB1]] ], [ [[BF:%.*]], [[BB2]] ]
1817
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
1918
; CHECK: for.body:
2019
; CHECK-NEXT: [[A_ADDR_03:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ]
2120
; CHECK-NEXT: [[B_ADDR_FLOAT:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC:%.*]], [[FOR_BODY]] ], [ [[B_FLOAT]], [[FOR_BODY_PREHEADER]] ]
22-
; CHECK-NEXT: [[B_ADDR_I64_PTR:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC]], [[FOR_BODY]] ], [ [[B_PHI_PTR]], [[FOR_BODY_PREHEADER]] ]
21+
; CHECK-NEXT: [[B_ADDR_I64_PTR:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC]], [[FOR_BODY]] ], [ [[B_PHI]], [[FOR_BODY_PREHEADER]] ]
2322
; CHECK-NEXT: [[L:%.*]] = load float, ptr [[B_ADDR_FLOAT]], align 4
2423
; CHECK-NEXT: [[MUL_I:%.*]] = fmul float [[L]], 4.200000e+01
2524
; CHECK-NEXT: store float [[MUL_I]], ptr [[A_ADDR_03]], align 4

llvm/test/Transforms/InstCombine/intptr5.ll

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ define void @test(ptr %a, ptr readnone %a_end, i64 %b, ptr %bf) unnamed_addr {
88
; CHECK-NEXT: [[B_FLOAT:%.*]] = inttoptr i64 [[B:%.*]] to ptr
99
; CHECK-NEXT: br i1 [[CMP1]], label [[BB1:%.*]], label [[BB2:%.*]]
1010
; CHECK: bb1:
11+
; CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[B]] to ptr
1112
; CHECK-NEXT: br label [[FOR_BODY_PREHEADER:%.*]]
1213
; CHECK: bb2:
13-
; CHECK-NEXT: [[BFI:%.*]] = ptrtoint ptr [[BF:%.*]] to i64
1414
; CHECK-NEXT: br label [[FOR_BODY_PREHEADER]]
1515
; CHECK: for.body.preheader:
16-
; CHECK-NEXT: [[B_PHI:%.*]] = phi i64 [ [[B]], [[BB1]] ], [ [[BFI]], [[BB2]] ]
17-
; CHECK-NEXT: [[B_PHI_PTR:%.*]] = inttoptr i64 [[B_PHI]] to ptr
16+
; CHECK-NEXT: [[B_PHI:%.*]] = phi ptr [ [[TMP0]], [[BB1]] ], [ [[BF:%.*]], [[BB2]] ]
1817
; CHECK-NEXT: switch i64 [[B]], label [[FOR_BODY:%.*]] [
1918
; CHECK-NEXT: i64 1, label [[FOR_BODY]]
2019
; CHECK-NEXT: ]
2120
; CHECK: for.body:
2221
; CHECK-NEXT: [[A_ADDR_03:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ], [ [[A]], [[FOR_BODY_PREHEADER]] ]
2322
; CHECK-NEXT: [[B_ADDR_FLOAT:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC:%.*]], [[FOR_BODY]] ], [ [[B_FLOAT]], [[FOR_BODY_PREHEADER]] ], [ [[B_FLOAT]], [[FOR_BODY_PREHEADER]] ]
24-
; CHECK-NEXT: [[B_ADDR_I64_PTR:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC]], [[FOR_BODY]] ], [ [[B_PHI_PTR]], [[FOR_BODY_PREHEADER]] ], [ [[B_PHI_PTR]], [[FOR_BODY_PREHEADER]] ]
23+
; CHECK-NEXT: [[B_ADDR_I64_PTR:%.*]] = phi ptr [ [[B_ADDR_FLOAT_INC]], [[FOR_BODY]] ], [ [[B_PHI]], [[FOR_BODY_PREHEADER]] ], [ [[B_PHI]], [[FOR_BODY_PREHEADER]] ]
2524
; CHECK-NEXT: [[L:%.*]] = load float, ptr [[B_ADDR_FLOAT]], align 4
2625
; CHECK-NEXT: [[MUL_I:%.*]] = fmul float [[L]], 4.200000e+01
2726
; CHECK-NEXT: store float [[MUL_I]], ptr [[A_ADDR_03]], align 4

llvm/test/Transforms/InstCombine/intptr7.ll

+3-4
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ define void @no_matching_phi(i64 %a, ptr %b, i1 %cond) {
5151
; CHECK-NEXT: [[ADDB:%.*]] = getelementptr inbounds float, ptr [[B:%.*]], i64 2
5252
; CHECK-NEXT: br i1 [[COND:%.*]], label [[B:%.*]], label [[A:%.*]]
5353
; CHECK: A:
54+
; CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[ADD_INT]] to ptr
5455
; CHECK-NEXT: br label [[C:%.*]]
5556
; CHECK: B:
56-
; CHECK-NEXT: [[ADDB_INT:%.*]] = ptrtoint ptr [[ADDB]] to i64
5757
; CHECK-NEXT: [[ADD:%.*]] = inttoptr i64 [[ADD_INT]] to ptr
5858
; CHECK-NEXT: store float 1.000000e+01, ptr [[ADD]], align 4
5959
; CHECK-NEXT: br label [[C]]
6060
; CHECK: C:
6161
; CHECK-NEXT: [[A_ADDR_03:%.*]] = phi ptr [ [[ADDB]], [[A]] ], [ [[ADD]], [[B]] ]
62-
; CHECK-NEXT: [[B_ADDR_02:%.*]] = phi i64 [ [[ADD_INT]], [[A]] ], [ [[ADDB_INT]], [[B]] ]
63-
; CHECK-NEXT: [[I0:%.*]] = inttoptr i64 [[B_ADDR_02]] to ptr
64-
; CHECK-NEXT: [[I1:%.*]] = load float, ptr [[I0]], align 4
62+
; CHECK-NEXT: [[B_ADDR_02:%.*]] = phi ptr [ [[TMP0]], [[A]] ], [ [[ADDB]], [[B]] ]
63+
; CHECK-NEXT: [[I1:%.*]] = load float, ptr [[B_ADDR_02]], align 4
6564
; CHECK-NEXT: [[MUL_I:%.*]] = fmul float [[I1]], 4.200000e+01
6665
; CHECK-NEXT: store float [[MUL_I]], ptr [[A_ADDR_03]], align 4
6766
; CHECK-NEXT: ret void

llvm/test/Transforms/InstCombine/phi-select-constant.ll

+3-5
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,15 @@ final:
7777
define <2 x i8> @vec3(i1 %cond1, i1 %cond2, <2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {
7878
; CHECK-LABEL: @vec3(
7979
; CHECK-NEXT: entry:
80-
; CHECK-NEXT: [[PHI_SEL1:%.*]] = shufflevector <2 x i8> [[Z:%.*]], <2 x i8> [[Y:%.*]], <2 x i32> <i32 0, i32 3>
8180
; CHECK-NEXT: br i1 [[COND1:%.*]], label [[IF1:%.*]], label [[ELSE:%.*]]
8281
; CHECK: if1:
83-
; CHECK-NEXT: [[PHI_SEL2:%.*]] = shufflevector <2 x i8> [[Y]], <2 x i8> [[Z]], <2 x i32> <i32 0, i32 3>
8482
; CHECK-NEXT: br i1 [[COND2:%.*]], label [[IF2:%.*]], label [[ELSE]]
8583
; CHECK: if2:
86-
; CHECK-NEXT: [[PHI_SEL:%.*]] = select <2 x i1> [[X:%.*]], <2 x i8> [[Y]], <2 x i8> [[Z]]
8784
; CHECK-NEXT: br label [[ELSE]]
8885
; CHECK: else:
89-
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i8> [ [[PHI_SEL]], [[IF2]] ], [ [[PHI_SEL1]], [[ENTRY:%.*]] ], [ [[PHI_SEL2]], [[IF1]] ]
90-
; CHECK-NEXT: ret <2 x i8> [[PHI]]
86+
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i1> [ [[X:%.*]], [[IF2]] ], [ <i1 false, i1 true>, [[ENTRY:%.*]] ], [ <i1 true, i1 false>, [[IF1]] ]
87+
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[PHI]], <2 x i8> [[Y:%.*]], <2 x i8> [[Z:%.*]]
88+
; CHECK-NEXT: ret <2 x i8> [[SEL]]
9189
;
9290
entry:
9391
br i1 %cond1, label %if1, label %else

llvm/test/Transforms/InstCombine/phi.ll

+7-9
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,10 @@ ret:
697697
define i32 @test23(i32 %A, i1 %pb, ptr %P) {
698698
; CHECK-LABEL: @test23(
699699
; CHECK-NEXT: BB0:
700-
; CHECK-NEXT: [[PHI_BO:%.*]] = add i32 [[A:%.*]], 19
700+
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[A:%.*]], 19
701701
; CHECK-NEXT: br label [[LOOP:%.*]]
702702
; CHECK: Loop:
703-
; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[PHI_BO]], [[BB0:%.*]] ], [ 61, [[LOOP]] ]
703+
; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[TMP0]], [[BB0:%.*]] ], [ 61, [[LOOP]] ]
704704
; CHECK-NEXT: store i32 [[B]], ptr [[P:%.*]], align 4
705705
; CHECK-NEXT: br i1 [[PB:%.*]], label [[LOOP]], label [[EXIT:%.*]]
706706
; CHECK: Exit:
@@ -1280,14 +1280,12 @@ define i1 @pr57488_icmp_of_phi(ptr %ptr.base, i64 %len) {
12801280
; CHECK-NEXT: [[LEN_ZERO:%.*]] = icmp eq i64 [[LEN]], 0
12811281
; CHECK-NEXT: br i1 [[LEN_ZERO]], label [[EXIT:%.*]], label [[LOOP:%.*]]
12821282
; CHECK: loop:
1283-
; CHECK-NEXT: [[ACCUM:%.*]] = phi i8 [ [[ACCUM_NEXT:%.*]], [[LOOP]] ], [ 1, [[START:%.*]] ]
1283+
; CHECK-NEXT: [[ACCUM:%.*]] = phi i1 [ [[AND:%.*]], [[LOOP]] ], [ true, [[START:%.*]] ]
12841284
; CHECK-NEXT: [[PTR:%.*]] = phi ptr [ [[PTR_NEXT:%.*]], [[LOOP]] ], [ [[PTR_BASE]], [[START]] ]
12851285
; CHECK-NEXT: [[PTR_NEXT]] = getelementptr inbounds i64, ptr [[PTR]], i64 1
1286-
; CHECK-NEXT: [[ACCUM_BOOL:%.*]] = icmp ne i8 [[ACCUM]], 0
12871286
; CHECK-NEXT: [[VAL:%.*]] = load i64, ptr [[PTR]], align 8
12881287
; CHECK-NEXT: [[VAL_ZERO:%.*]] = icmp eq i64 [[VAL]], 0
1289-
; CHECK-NEXT: [[AND:%.*]] = and i1 [[ACCUM_BOOL]], [[VAL_ZERO]]
1290-
; CHECK-NEXT: [[ACCUM_NEXT]] = zext i1 [[AND]] to i8
1288+
; CHECK-NEXT: [[AND]] = and i1 [[ACCUM]], [[VAL_ZERO]]
12911289
; CHECK-NEXT: [[EXIT_COND:%.*]] = icmp eq ptr [[PTR_NEXT]], [[END]]
12921290
; CHECK-NEXT: br i1 [[EXIT_COND]], label [[EXIT]], label [[LOOP]]
12931291
; CHECK: exit:
@@ -1344,17 +1342,17 @@ loop:
13441342
define i32 @phi_op_self_simplify_2(i32 %x) {
13451343
; CHECK-LABEL: @phi_op_self_simplify_2(
13461344
; CHECK-NEXT: entry:
1345+
; CHECK-NEXT: [[TMP0:%.*]] = or i32 [[X:%.*]], 1
13471346
; CHECK-NEXT: br label [[LOOP:%.*]]
13481347
; CHECK: loop:
1349-
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ [[OR:%.*]], [[LOOP]] ], [ 10, [[LOOP_LATCH:%.*]] ]
1350-
; CHECK-NEXT: [[OR]] = or i32 [[PHI]], 1
1348+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[TMP0]], [[ENTRY:%.*]] ], [ [[PHI]], [[LOOP]] ], [ 11, [[LOOP_LATCH:%.*]] ]
13511349
; CHECK-NEXT: [[C1:%.*]] = call i1 @get.i1()
13521350
; CHECK-NEXT: br i1 [[C1]], label [[LOOP_LATCH]], label [[LOOP]]
13531351
; CHECK: loop.latch:
13541352
; CHECK-NEXT: [[C2:%.*]] = call i1 @get.i1()
13551353
; CHECK-NEXT: br i1 [[C2]], label [[EXIT:%.*]], label [[LOOP]]
13561354
; CHECK: exit:
1357-
; CHECK-NEXT: ret i32 [[OR]]
1355+
; CHECK-NEXT: ret i32 [[PHI]]
13581356
;
13591357
entry:
13601358
br label %loop

0 commit comments

Comments
 (0)