@@ -1033,8 +1033,8 @@ struct LoopFuser {
10331033 FuseCounter);
10341034
10351035 FusionCandidate FusedCand (
1036- performFusion ((Peel ? FC0Copy : *FC0), *FC1, SafeToSink ), DT,
1037- &PDT, ORE, FC0Copy.PP );
1036+ performFusion ((Peel ? FC0Copy : *FC0), *FC1), DT, &PDT, ORE ,
1037+ FC0Copy.PP );
10381038 FusedCand.verify ();
10391039 assert (FusedCand.isEligibleForFusion (SE) &&
10401040 " Fused candidate should be eligible for fusion!" );
@@ -1176,18 +1176,19 @@ struct LoopFuser {
11761176 return true ;
11771177 }
11781178
1179- // This function fixes sunk PHI nodes after fusion.
1180- void fixPHINodes (SmallVector<Instruction *, 4 > &SafeToSink,
1179+ // / This function fixes PHI nodes after fusion in \p SafeToSink.
1180+ // / \p SafeToSink instructions are the instructions that are to be moved past
1181+ // / the fused loop. Thus, the PHI nodes in \p SafeToSink should be updated to
1182+ // / receive values from the fused loop if they are currently taking values
1183+ // / from the first loop (i.e. FC0)'s latch.
1184+ void fixPHINodes (ArrayRef<Instruction *> SafeToSink,
11811185 const FusionCandidate &FC0,
11821186 const FusionCandidate &FC1) const {
1183- // Iterate over SafeToSink instructions and update PHI nodes
1184- // to take values from the latch block of FC0 if they are taking
1185- // from the latch block of FC1.
11861187 for (Instruction *Inst : SafeToSink) {
1187- // Continue if the instruction is not a PHI node.
1188- if (!isa<PHINode>(Inst))
1189- continue ;
1188+ // No update needed for non-PHI nodes.
11901189 PHINode *Phi = dyn_cast<PHINode>(Inst);
1190+ if (!Phi)
1191+ continue ;
11911192 for (unsigned I = 0 ; I < Phi->getNumIncomingValues (); I++) {
11921193 if (Phi->getIncomingBlock (I) != FC0.Latch )
11931194 continue ;
@@ -1502,6 +1503,9 @@ struct LoopFuser {
15021503 assert (I->getParent () == FC1.Preheader );
15031504 I->moveBefore (*FC1.ExitBlock , FC1.ExitBlock ->getFirstInsertionPt ());
15041505 }
1506+ // PHI nodes in SinkInsts need to be updated to receive values from the
1507+ // fused loop.
1508+ fixPHINodes (SinkInsts, FC0, FC1);
15051509 }
15061510
15071511 // / Determine if two fusion candidates have identical guards
@@ -1590,8 +1594,7 @@ struct LoopFuser {
15901594 // / two loops could also be fused into a single block. This will require
15911595 // / analysis to prove it is safe to move the contents of the block past
15921596 // / existing code, which currently has not been implemented.
1593- Loop *performFusion (const FusionCandidate &FC0, const FusionCandidate &FC1,
1594- SmallVector<Instruction *, 4 > &SafeToSink) {
1597+ Loop *performFusion (const FusionCandidate &FC0, const FusionCandidate &FC1) {
15951598 assert (FC0.isValid () && FC1.isValid () &&
15961599 " Expecting valid fusion candidates" );
15971600
@@ -1733,9 +1736,6 @@ struct LoopFuser {
17331736 TreeUpdates.emplace_back (DominatorTree::UpdateType (DominatorTree::Delete,
17341737 FC1.Latch , FC1.Header ));
17351738
1736- // Fix PHI nodes that are sunk into the body of the loop.
1737- fixPHINodes (SafeToSink, FC0, FC1);
1738-
17391739 // Update DT/PDT
17401740 DTU.applyUpdates (TreeUpdates);
17411741
0 commit comments