]> granicus.if.org Git - llvm/commitdiff
[LoopInfo] Faster implementation of setLoopID. NFC.
authorKeno Fischer <keno@alumni.harvard.edu>
Wed, 1 May 2019 14:39:11 +0000 (14:39 +0000)
committerKeno Fischer <keno@alumni.harvard.edu>
Wed, 1 May 2019 14:39:11 +0000 (14:39 +0000)
Summary:
This change was part of D46460. However, in the meantime rL341926 fixed the
correctness issue here. What remained was the performance issue in setLoopID
where it would iterate through all blocks in the loop and their successors,
rather than just the predecessor of the header (the later presumably being
much faster). We already have the `getLoopLatches` to compute precisely these
basic blocks in an efficient manner, so just use it (as the original commit
did for `getLoopID`).

Reviewed By: jdoerfert
Differential Revision: https://reviews.llvm.org/D61215

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359684 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/LoopInfo.cpp

index 6b93234b487f882ef695bb6538fc180484ca91a4..b9789394d4109f674eebac0369a3dae5216b956b 100644 (file)
@@ -254,16 +254,10 @@ void Loop::setLoopID(MDNode *LoopID) const {
   assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
          "Loop ID should refer to itself");
 
-  BasicBlock *H = getHeader();
-  for (BasicBlock *BB : this->blocks()) {
-    Instruction *TI = BB->getTerminator();
-    for (BasicBlock *Successor : successors(TI)) {
-      if (Successor == H) {
-        TI->setMetadata(LLVMContext::MD_loop, LoopID);
-        break;
-      }
-    }
-  }
+  SmallVector<BasicBlock *, 4> LoopLatches;
+  getLoopLatches(LoopLatches);
+  for (BasicBlock *BB : LoopLatches)
+    BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
 }
 
 void Loop::setLoopAlreadyUnrolled() {