[llvm] [GreedyRA] Improve RA for nested loop induction variables (PR #72093)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 07:18:39 PST 2023


================
@@ -767,10 +767,27 @@ bool RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
     if (Cand.PhysReg) {
       if (!addThroughConstraints(Cand.Intf, NewBlocks))
         return false;
-    } else
-      // Provide a strong negative bias on through blocks to prevent unwanted
-      // liveness on loop backedges.
-      SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
+    } else {
+      // Providing that the variable being spilled does not look like a loop
+      // induction variable, which is expensive to spill around and better
+      // pushed into a condition inside the loop if possible, provide a strong
+      // negative bias on through blocks to prevent unwanted liveness on loop
+      // backedges.
+      bool PrefSpill = true;
+      if (SA->looksLikeLoopIV() && NewBlocks.size() == 3) {
+        // NewBlocks==3 means we are (possible) adding a Header + start+end of
+        // two loop-internal blocks. If the block is indeed a header, don't make
+        // the newblocks as PrefSpill to allow the variable to be live in
+        // header<->latch.
+        MachineLoop *L = Loops->getLoopFor(MF->getBlockNumbered(NewBlocks[0]));
+        if (L && L->getHeader()->getNumber() == NewBlocks[0] &&
+            L == Loops->getLoopFor(MF->getBlockNumbered(NewBlocks[1])) &&
+            L == Loops->getLoopFor(MF->getBlockNumbered(NewBlocks[2])))
----------------
qcolombet wrote:

I don't quite get the argument about the number 3.

What would happen if we had say 5 loop-internal blocks?
Why do we want to do something differently in this case?

https://github.com/llvm/llvm-project/pull/72093


More information about the llvm-commits mailing list