[PATCH] Handle resolvable branches in complete loop unroll heuristic.

Michael Zolotukhin mzolotukhin at apple.com
Fri Jun 5 21:08:05 PDT 2015


- Rebase.
- Rename SCEVSimplify to simplifyInstWithSCEV in visitCmpInst.
- Fallback to Base visitor if failed to constant fold (and only then call simplifyInstWithSCEV).
- Rewrite conditional expression for adding successors.


http://reviews.llvm.org/D10206

Files:
  lib/Transforms/Scalar/LoopUnrollPass.cpp

Index: lib/Transforms/Scalar/LoopUnrollPass.cpp
===================================================================
--- lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -439,6 +439,49 @@
 
     return true;
   }
+
+  bool visitCmpInst(CmpInst &I) {
+    Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
+
+    // First try to handle simplified comparisons.
+    if (!isa<Constant>(LHS))
+      if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
+        LHS = SimpleLHS;
+    if (!isa<Constant>(RHS))
+      if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
+        RHS = SimpleRHS;
+
+    if (!isa<Constant>(LHS) && !isa<Constant>(RHS))
+      if (!simplifyUsingOffsets(LHS, RHS))
+        return Base::visitCmpInst(I);
+
+    if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
+      if (Constant *CRHS = dyn_cast<Constant>(RHS))
+        if (Constant *C = ConstantExpr::getCompare(I.getPredicate(), CLHS, CRHS)) {
+          SimplifiedValues[&I] = C;
+          return true;
+        }
+    }
+
+    return Base::visitCmpInst(I);
+  }
+
+  /// \brief Check if \p LHS and \p RHS correspond to address expressions with
+  /// the same base and their offsets could be folded to constants.
+  ///
+  /// \returns true and updates \p LHS and \p RHS if they both are present in
+  /// SimplifiedOffsets and have the same base address.
+  bool simplifyUsingOffsets(Value *&LHS, Value *&RHS) {
+    if (!SimplifiedOffsets.count(LHS) || !SimplifiedOffsets.count(RHS))
+      return false;
+    SimplifiedAddress LHSAddr = SimplifiedOffsets.lookup(LHS);
+    SimplifiedAddress RHSAddr = SimplifiedOffsets.lookup(RHS);
+    if (LHSAddr.Base != RHSAddr.Base)
+      return false;
+    LHS = LHSAddr.Offset;
+    RHS = RHSAddr.Offset;
+    return true;
+  }
 };
 } // namespace
 
@@ -528,6 +571,28 @@
           return None;
       }
 
+      TerminatorInst *TI = BB->getTerminator();
+
+      // Add in the live successors by first checking whether we have terminator
+      // that may be simplified based on the values simplified by this call.
+      if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
+        if (BI->isConditional()) {
+          if (Constant *SimpleCond =
+                  SimplifiedValues.lookup(BI->getCondition())) {
+            BBWorklist.insert(BI->getSuccessor(
+                cast<ConstantInt>(SimpleCond)->isZero() ? 1 : 0));
+            continue;
+          }
+        }
+      } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
+        if (Constant *SimpleCond =
+                SimplifiedValues.lookup(SI->getCondition())) {
+          BBWorklist.insert(
+              SI->getSuccessor(cast<ConstantInt>(SimpleCond)->getSExtValue()));
+          continue;
+        }
+      }
+
       // Add BB's successors to the worklist.
       for (BasicBlock *Succ : successors(BB))
         if (L->contains(Succ))

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10206.27260.patch
Type: text/x-patch
Size: 2894 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150606/dbe12ae5/attachment.bin>


More information about the llvm-commits mailing list