Skip to content

Commit 840c626

Browse files
committed
Track visited PHI nodes
this fixes the hang in cam4.
1 parent e037b3e commit 840c626

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
9999
SmallDenseSet<std::pair<const BasicBlock *, const BasicBlock *>, 8> BackEdges;
100100
bool ComputedBackEdges = false;
101101

102+
/// Phis for which we already attempted to fold a freeze away. If a freeze is
103+
/// reintroduced on one of these phis we avoid repeating the transform to
104+
/// prevent endless combine cycles.
105+
SmallPtrSet<PHINode *, 16> FreezePhisVisited;
106+
102107
public:
103108
InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F,
104109
AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI,

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
181181
Instruction *visitExtractValueInst(ExtractValueInst &EV);
182182
Instruction *visitLandingPadInst(LandingPadInst &LI);
183183
Instruction *visitVAEndInst(VAEndInst &I);
184-
Value *pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI);
184+
Value *pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI,
185+
bool PushThruPhis = false);
185186
bool freezeOtherUses(FreezeInst &FI);
186187
Instruction *foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN);
187188
Instruction *visitFreeze(FreezeInst &I);

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,7 +5014,8 @@ Instruction *InstCombinerImpl::visitLandingPadInst(LandingPadInst &LI) {
50145014
}
50155015

50165016
Value *
5017-
InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating(FreezeInst &OrigFI) {
5017+
InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating(FreezeInst &OrigFI,
5018+
bool PushThruPhis) {
50185019
// Try to push freeze through instructions that propagate but don't produce
50195020
// poison as far as possible. If an operand of freeze does not produce poison
50205021
// then push the freeze through to the operands that are not guaranteed
@@ -5027,8 +5028,8 @@ InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating(FreezeInst &OrigFI) {
50275028
// Op1.fr = Freeze(Op1)
50285029
// ... = Inst(Op1.fr, NonPoisonOps...)
50295030

5030-
auto CanPushFreeze = [this](Value *V) {
5031-
if (!isa<Instruction>(V))
5031+
auto CanPushFreeze = [&](Value *V) {
5032+
if (!isa<Instruction>(V) || (isa<PHINode>(V) && !PushThruPhis))
50325033
return false;
50335034

50345035
if (auto *PN = dyn_cast<PHINode>(V)) {
@@ -5231,15 +5232,19 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
52315232
if (Value *V = simplifyFreezeInst(Op0, SQ.getWithInstruction(&I)))
52325233
return replaceInstUsesWith(I, V);
52335234

5235+
bool PushThruPhis = false;
52345236
// freeze (phi const, x) --> phi const, (freeze x)
52355237
if (auto *PN = dyn_cast<PHINode>(Op0)) {
52365238
if (Instruction *NV = foldOpIntoPhi(I, PN))
52375239
return NV;
5238-
if (Instruction *NV = foldFreezeIntoRecurrence(I, PN))
5239-
return NV;
5240+
if (FreezePhisVisited.insert(PN).second) {
5241+
PushThruPhis = true;
5242+
if (Instruction *NV = foldFreezeIntoRecurrence(I, PN))
5243+
return NV;
5244+
}
52405245
}
52415246

5242-
if (Value *NI = pushFreezeToPreventPoisonFromPropagating(I))
5247+
if (Value *NI = pushFreezeToPreventPoisonFromPropagating(I, PushThruPhis))
52435248
return replaceInstUsesWith(I, NI);
52445249

52455250
// If I is freeze(undef), check its uses and fold it to a fixed constant.

0 commit comments

Comments
 (0)