@@ -53,6 +53,34 @@ void InstCombinerImpl::PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN) {
53
53
}
54
54
}
55
55
56
+ // / If the phi is within a phi web, which is formed by the def-use chain
57
+ // / of phis and all the phis in the web are only used in the other phis.
58
+ // / In this case, these phis are dead and we will remove all of them.
59
+ bool InstCombinerImpl::foldDeadPhiWeb (PHINode &PN) {
60
+ SmallVector<PHINode *, 16 > Stack;
61
+ SmallPtrSet<PHINode *, 16 > Visited;
62
+ Stack.push_back (&PN);
63
+ while (!Stack.empty ()) {
64
+ PHINode *Phi = Stack.pop_back_val ();
65
+ if (!Visited.insert (Phi).second )
66
+ continue ;
67
+ // Early stop if the set of PHIs is large
68
+ if (Visited.size () == 16 )
69
+ return false ;
70
+ for (User *Use : Phi->users ()) {
71
+ if (PHINode *PhiUse = dyn_cast<PHINode>(Use))
72
+ Stack.push_back (PhiUse);
73
+ else
74
+ return false ;
75
+ }
76
+ }
77
+ for (PHINode *Phi : Visited)
78
+ replaceInstUsesWith (*Phi, PoisonValue::get (Phi->getType ()));
79
+ for (PHINode *Phi : Visited)
80
+ eraseInstFromFunction (*Phi);
81
+ return true ;
82
+ }
83
+
56
84
// Replace Integer typed PHI PN if the PHI's value is used as a pointer value.
57
85
// If there is an existing pointer typed PHI that produces the same value as PN,
58
86
// replace PN and the IntToPtr operation with it. Otherwise, synthesize a new
@@ -976,26 +1004,6 @@ Instruction *InstCombinerImpl::foldPHIArgOpIntoPHI(PHINode &PN) {
976
1004
return NewCI;
977
1005
}
978
1006
979
- // / Return true if this PHI node is only used by a PHI node cycle that is dead.
980
- static bool isDeadPHICycle (PHINode *PN,
981
- SmallPtrSetImpl<PHINode *> &PotentiallyDeadPHIs) {
982
- if (PN->use_empty ()) return true ;
983
- if (!PN->hasOneUse ()) return false ;
984
-
985
- // Remember this node, and if we find the cycle, return.
986
- if (!PotentiallyDeadPHIs.insert (PN).second )
987
- return true ;
988
-
989
- // Don't scan crazily complex things.
990
- if (PotentiallyDeadPHIs.size () == 16 )
991
- return false ;
992
-
993
- if (PHINode *PU = dyn_cast<PHINode>(PN->user_back ()))
994
- return isDeadPHICycle (PU, PotentiallyDeadPHIs);
995
-
996
- return false ;
997
- }
998
-
999
1007
// / Return true if this phi node is always equal to NonPhiInVal.
1000
1008
// / This happens with mutually cyclic phi nodes like:
1001
1009
// / z = some value; x = phi (y, z); y = phi (x, z)
@@ -1474,27 +1482,21 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
1474
1482
}
1475
1483
}
1476
1484
1477
- // If this is a trivial cycle in the PHI node graph, remove it. Basically, if
1478
- // this PHI only has a single use (a PHI), and if that PHI only has one use (a
1479
- // PHI)... break the cycle.
1485
+ if (foldDeadPhiWeb (PN))
1486
+ return nullptr ;
1487
+
1488
+ // Optimization when the phi only has one use
1480
1489
if (PN.hasOneUse ()) {
1481
1490
if (foldIntegerTypedPHI (PN))
1482
1491
return nullptr ;
1483
1492
1484
- Instruction *PHIUser = cast<Instruction>(PN.user_back ());
1485
- if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
1486
- SmallPtrSet<PHINode*, 16 > PotentiallyDeadPHIs;
1487
- PotentiallyDeadPHIs.insert (&PN);
1488
- if (isDeadPHICycle (PU, PotentiallyDeadPHIs))
1489
- return replaceInstUsesWith (PN, PoisonValue::get (PN.getType ()));
1490
- }
1491
-
1492
1493
// If this phi has a single use, and if that use just computes a value for
1493
1494
// the next iteration of a loop, delete the phi. This occurs with unused
1494
1495
// induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
1495
1496
// common case here is good because the only other things that catch this
1496
1497
// are induction variable analysis (sometimes) and ADCE, which is only run
1497
1498
// late.
1499
+ Instruction *PHIUser = cast<Instruction>(PN.user_back ());
1498
1500
if (PHIUser->hasOneUse () &&
1499
1501
(isa<BinaryOperator>(PHIUser) || isa<UnaryOperator>(PHIUser) ||
1500
1502
isa<GetElementPtrInst>(PHIUser)) &&
0 commit comments