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

Aditya Kumar via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 12 09:29:01 PDT 2016


hiraditya updated this revision to Diff 74396.
hiraditya added a comment.

Addressed Michael's comments.


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
@@ -210,25 +210,17 @@
   } else {
     // 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(getHeader())) {
+      if (!this->contains(BB))
+        continue;
 
-      // Check if this terminator branches to the loop header.
-      for (BasicBlock *Successor : TI->successors()) {
-        if (Successor == H) {
-          MD = TI->getMetadata(LLVMContext::MD_loop);
-          break;
-        }
+      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 (!MD)
-        return nullptr;
-
-      if (!LoopID)
-        LoopID = MD;
-      else if (MD != LoopID)
-        return nullptr;
     }
   }
   if (!LoopID || LoopID->getNumOperands() == 0 ||
@@ -247,13 +239,13 @@
     return;
   }
 
-  BasicBlock *H = getHeader();
-  for (BasicBlock *BB : this->blocks()) {
+  // Set metadata on all the back-edges.
+  for (BasicBlock *BB : predecessors(getHeader())) {
+    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);
   }
 }
 


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


More information about the llvm-commits mailing list