[PATCH] D24840: Search for all predecessors of loop header while getting loop metadata

Aditya Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 12:58:41 PDT 2016


hiraditya created this revision.
hiraditya added reviewers: paul.redmond, sanjoy, sebpop.
hiraditya added a subscriber: llvm-commits.
Herald added a subscriber: mzolotukhin.

The current implementation of getLoopID seems incorrect as it returns nullptr only after looking into the successors of first block in the loop.
In the setLoopID, there is no need to iterate over all the basic blocks of loop as loop metadata is only associated with predecessor (s) of loop header.

https://reviews.llvm.org/D24840

Files:
  llvm/lib/Analysis/LoopInfo.cpp

Index: llvm/lib/Analysis/LoopInfo.cpp
===================================================================
--- llvm/lib/Analysis/LoopInfo.cpp
+++ llvm/lib/Analysis/LoopInfo.cpp
@@ -211,24 +211,18 @@
     // Go through each predecessor of the loop header and check the
     // terminator for the metadata.
     BasicBlock *H = getHeader();
-    for (BasicBlock *BB : this->blocks()) {
-      TerminatorInst *TI = BB->getTerminator();
-      MDNode *MD = nullptr;
-
+    for (BasicBlock *BB : predecessors(H)) {
       // Check if this terminator branches to the loop header.
-      for (BasicBlock *Successor : TI->successors()) {
-        if (Successor == H) {
-          MD = TI->getMetadata(LLVMContext::MD_loop);
-          break;
-        }
-      }
-      if (!MD)
-        return nullptr;
+      if (!this->contains(BB))
+        continue;
 
-      if (!LoopID)
-        LoopID = MD;
-      else if (MD != LoopID)
-        return nullptr;
+      TerminatorInst *TI = BB->getTerminator();
+      if (MDNode *MD = TI->getMetadata(LLVMContext::MD_loop)) {
+        if (!LoopID)
+          LoopID = MD;
+        else if (MD != LoopID) // Multiple MD_loop found => corrupt metadata.
+          return nullptr;
+      }
     }
   }
   if (!LoopID || LoopID->getNumOperands() == 0 ||
@@ -248,12 +242,16 @@
   }
 
   BasicBlock *H = getHeader();
-  for (BasicBlock *BB : this->blocks()) {
+  bool MDSet = false;
+  for (BasicBlock *BB : predecessors(H)) {
+    // Check if this terminator branches to the loop header.
+    if (!this->contains(BB))
+      continue;
+
     TerminatorInst *TI = BB->getTerminator();
-    for (BasicBlock *Successor : TI->successors()) {
-      if (Successor == H)
-        TI->setMetadata(LLVMContext::MD_loop, LoopID);
-    }
+    TI->setMetadata(LLVMContext::MD_loop, LoopID);
+    assert(!MDSet && "Multiple branches to loop header from within loop.");
+    MDSet = true;
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24840.72203.patch
Type: text/x-patch
Size: 1913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160922/f4b32c86/attachment.bin>


More information about the llvm-commits mailing list