[PATCH] D66477: Simplify main statement flag in ScopBuilder::buildEqivClassBlockStmts

bin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 07:57:26 PDT 2019


bin.narwal created this revision.
bin.narwal added a reviewer: Meinersbur.
bin.narwal added a project: Polly.
Herald added a reviewer: bollu.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Hi,
When reading code in ScopBuilder::buildEqivClassBlockStmts, I think the main statement flag computation can be simplified, here is the patch.  It's based on two simple facts that: 1, Instruction won't be removed once it's inserted into UnionFind. 2, Main statement must be set if there is non-trivial statement besides the last one.  The patch also saves std::find call.  Any comments?

BTW, this is my first patch to LLVM/Polly.  Please correct me if there is anything wrong.  Thanks in advance.

Thanks,


Repository:
  rPLO Polly

https://reviews.llvm.org/D66477

Files:
  lib/Analysis/ScopBuilder.cpp


Index: lib/Analysis/ScopBuilder.cpp
===================================================================
--- lib/Analysis/ScopBuilder.cpp
+++ lib/Analysis/ScopBuilder.cpp
@@ -2095,7 +2095,7 @@
   // shouldModelInst() repeatedly.
   SmallVector<Instruction *, 32> ModeledInsts;
   EquivalenceClasses<Instruction *> UnionFind;
-  Instruction *MainInst = nullptr;
+  Instruction *MainInst = nullptr, *MainLeader = nullptr;
   for (Instruction &Inst : *BB) {
     if (!shouldModelInst(&Inst, L))
       continue;
@@ -2129,6 +2129,8 @@
     if (LeaderIt == UnionFind.member_end())
       continue;
 
+    if (&Inst == MainInst)
+      MainLeader = *LeaderIt;
     std::vector<Instruction *> &InstList = LeaderToInstList[*LeaderIt];
     InstList.push_back(&Inst);
   }
@@ -2136,19 +2138,11 @@
   // Finally build the statements.
   int Count = 0;
   long BBIdx = scop->getNextStmtIdx();
-  bool MainFound = false;
   for (auto &Instructions : reverse(LeaderToInstList)) {
     std::vector<Instruction *> &InstList = Instructions.second;
 
-    // If there is no main instruction, make the first statement the main.
-    bool IsMain;
-    if (MainInst)
-      IsMain = std::find(InstList.begin(), InstList.end(), MainInst) !=
-               InstList.end();
-    else
-      IsMain = (Count == 0);
-    if (IsMain)
-      MainFound = true;
+    // Make the first statement the main if there is no main statement yet.
+    bool IsMain = (MainInst ? MainLeader == Instructions.first : Count == 0);
 
     std::reverse(InstList.begin(), InstList.end());
     std::string Name = makeStmtName(BB, BBIdx, Count, IsMain);
@@ -2160,7 +2154,7 @@
   // instructions, but holds the PHI write accesses for successor basic blocks,
   // if the incoming value is not defined in another statement if the same BB.
   // The epilogue will be removed if no PHIWrite is added to it.
-  std::string EpilogueName = makeStmtName(BB, BBIdx, Count, !MainFound, true);
+  std::string EpilogueName = makeStmtName(BB, BBIdx, Count, Count == 0, true);
   scop->addScopStmt(BB, EpilogueName, L, {});
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66477.216147.patch
Type: text/x-patch
Size: 2069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190820/58bd34f0/attachment.bin>


More information about the llvm-commits mailing list