Don't attempt aggressive post-inc uses if TargetLowering is not available,
[oota-llvm.git] / lib / Transforms / Scalar / LoopStrengthReduce.cpp
index 73d3f9db8965402347df6678473aff42b38b0a92..0c65e33c61d267778f546c24d3a5783a28c4f838 100644 (file)
@@ -579,6 +579,10 @@ private:
                     SmallPtrSet<const SCEV *, 16> &Regs,
                     const Loop *L,
                     ScalarEvolution &SE, DominatorTree &DT);
+  void RatePrimaryRegister(const SCEV *Reg,
+                           SmallPtrSet<const SCEV *, 16> &Regs,
+                           const Loop *L,
+                           ScalarEvolution &SE, DominatorTree &DT);
 };
 
 }
@@ -588,49 +592,59 @@ void Cost::RateRegister(const SCEV *Reg,
                         SmallPtrSet<const SCEV *, 16> &Regs,
                         const Loop *L,
                         ScalarEvolution &SE, DominatorTree &DT) {
-  if (Regs.insert(Reg)) {
-    if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
-      if (AR->getLoop() == L)
-        AddRecCost += 1; /// TODO: This should be a function of the stride.
-
-      // If this is an addrec for a loop that's already been visited by LSR,
-      // don't second-guess its addrec phi nodes. LSR isn't currently smart
-      // enough to reason about more than one loop at a time. Consider these
-      // registers free and leave them alone.
-      else if (L->contains(AR->getLoop()) ||
-               (!AR->getLoop()->contains(L) &&
-                DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))) {
-        for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin();
-             PHINode *PN = dyn_cast<PHINode>(I); ++I)
-          if (SE.isSCEVable(PN->getType()) &&
-              (SE.getEffectiveSCEVType(PN->getType()) ==
-               SE.getEffectiveSCEVType(AR->getType())) &&
-              SE.getSCEV(PN) == AR)
-            goto no_cost;
-
-        // If this isn't one of the addrecs that the loop already has, it
-        // would require a costly new phi and add.
-        ++NumBaseAdds;
+  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
+    if (AR->getLoop() == L)
+      AddRecCost += 1; /// TODO: This should be a function of the stride.
+
+    // If this is an addrec for a loop that's already been visited by LSR,
+    // don't second-guess its addrec phi nodes. LSR isn't currently smart
+    // enough to reason about more than one loop at a time. Consider these
+    // registers free and leave them alone.
+    else if (L->contains(AR->getLoop()) ||
+             (!AR->getLoop()->contains(L) &&
+              DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))) {
+      for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin();
+           PHINode *PN = dyn_cast<PHINode>(I); ++I)
+        if (SE.isSCEVable(PN->getType()) &&
+            (SE.getEffectiveSCEVType(PN->getType()) ==
+             SE.getEffectiveSCEVType(AR->getType())) &&
+            SE.getSCEV(PN) == AR)
+          return;
+
+      // If this isn't one of the addrecs that the loop already has, it
+      // would require a costly new phi and add. TODO: This isn't
+      // precisely modeled right now.
+      ++NumBaseAdds;
+      if (!Regs.count(AR->getStart()))
         RateRegister(AR->getStart(), Regs, L, SE, DT);
-      }
-
-      // Add the step value register, if it needs one.
-      // TODO: The non-affine case isn't precisely modeled here.
-      if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1)))
-        RateRegister(AR->getOperand(1), Regs, L, SE, DT);
     }
-    ++NumRegs;
 
-    // Rough heuristic; favor registers which don't require extra setup
-    // instructions in the preheader.
-    if (!isa<SCEVUnknown>(Reg) &&
-        !isa<SCEVConstant>(Reg) &&
-        !(isa<SCEVAddRecExpr>(Reg) &&
-          (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) ||
-           isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart()))))
-      ++SetupCost;
-  no_cost:;
+    // Add the step value register, if it needs one.
+    // TODO: The non-affine case isn't precisely modeled here.
+    if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1)))
+      if (!Regs.count(AR->getStart()))
+        RateRegister(AR->getOperand(1), Regs, L, SE, DT);
   }
+  ++NumRegs;
+
+  // Rough heuristic; favor registers which don't require extra setup
+  // instructions in the preheader.
+  if (!isa<SCEVUnknown>(Reg) &&
+      !isa<SCEVConstant>(Reg) &&
+      !(isa<SCEVAddRecExpr>(Reg) &&
+        (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) ||
+         isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart()))))
+    ++SetupCost;
+}
+
+/// RatePrimaryRegister - Record this register in the set. If we haven't seen it
+/// before, rate it.
+void Cost::RatePrimaryRegister(const SCEV *Reg,
+                         SmallPtrSet<const SCEV *, 16> &Regs,
+                         const Loop *L,
+                         ScalarEvolution &SE, DominatorTree &DT) {
+  if (Regs.insert(Reg))
+    RateRegister(Reg, Regs, L, SE, DT);
 }
 
 void Cost::RateFormula(const Formula &F,
@@ -645,7 +659,7 @@ void Cost::RateFormula(const Formula &F,
       Loose();
       return;
     }
-    RateRegister(ScaledReg, Regs, L, SE, DT);
+    RatePrimaryRegister(ScaledReg, Regs, L, SE, DT);
   }
   for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
        E = F.BaseRegs.end(); I != E; ++I) {
@@ -654,7 +668,7 @@ void Cost::RateFormula(const Formula &F,
       Loose();
       return;
     }
-    RateRegister(BaseReg, Regs, L, SE, DT);
+    RatePrimaryRegister(BaseReg, Regs, L, SE, DT);
 
     NumIVMuls += isa<SCEVMulExpr>(BaseReg) &&
                  BaseReg->hasComputableLoopEvolution(L);
@@ -1489,7 +1503,11 @@ LSRInstance::OptimizeLoopTermCond() {
 
     // Conservatively avoid trying to use the post-inc value in non-latch
     // exits if there may be pre-inc users in intervening blocks.
-    if (LatchBlock != ExitingBlock)
+    if (LatchBlock != ExitingBlock) {
+      // Without target lowering, we won't be able to query about valid reuse.
+      if (!TLI)
+        continue;
+
       for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
         // Test if the use is reachable from the exiting block. This dominator
         // query is a conservative approximation of reachability.
@@ -1528,6 +1546,7 @@ LSRInstance::OptimizeLoopTermCond() {
               goto decline_post_inc;
           }
         }
+    }
 
     DEBUG(dbgs() << "  Change loop exiting icmp to use postinc iv: "
                  << *Cond << '\n');
@@ -1902,10 +1921,10 @@ static void CollectSubexprs(const SCEV *S, const SCEVConstant *C,
   } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
     // Split a non-zero base out of an addrec.
     if (!AR->getStart()->isZero()) {
-      CollectSubexprs(AR->getStart(), C, Ops, SE);
       CollectSubexprs(SE.getAddRecExpr(SE.getIntegerSCEV(0, AR->getType()),
                                        AR->getStepRecurrence(SE),
                                        AR->getLoop()), C, Ops, SE);
+      CollectSubexprs(AR->getStart(), C, Ops, SE);
       return;
     }
   } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
@@ -2271,6 +2290,10 @@ void LSRInstance::GenerateCrossUseConstantOffsets() {
     const SCEV *Reg = *I;
     const ImmMapTy &Imms = Map.find(Reg)->second;
 
+    // It's not worthwhile looking for reuse if there's only one offset.
+    if (Imms.size() == 1)
+      continue;
+
     DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
           for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
                J != JE; ++J)
@@ -2299,7 +2322,7 @@ void LSRInstance::GenerateCrossUseConstantOffsets() {
       };
       for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
         ImmMapTy::const_iterator M = OtherImms[i];
-        if (M == J) continue;
+        if (M == J || M == JE) continue;
 
         // Compute the difference between the two.
         int64_t Imm = (uint64_t)JImm - M->first;
@@ -2489,7 +2512,8 @@ void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
   }
 
   DEBUG(if (Changed) {
-          dbgs() << "After filtering out undesirable candidates:\n";
+          dbgs() << "\n"
+                    "After filtering out undesirable candidates:\n";
           print_uses(dbgs());
         });
 }
@@ -2606,13 +2630,13 @@ void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
   SmallSetVector<const SCEV *, 4> ReqRegs;
   for (SmallPtrSet<const SCEV *, 16>::const_iterator I = CurRegs.begin(),
        E = CurRegs.end(); I != E; ++I)
-    if (LU.Regs.count(*I)) {
+    if (LU.Regs.count(*I))
       ReqRegs.insert(*I);
-      break;
-    }
 
+  bool AnySatisfiedReqRegs = false;
   SmallPtrSet<const SCEV *, 16> NewRegs;
   Cost NewCost;
+retry:
   for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
        E = LU.Formulae.end(); I != E; ++I) {
     const Formula &F = *I;
@@ -2626,6 +2650,7 @@ void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
           F.BaseRegs.end())
         goto skip;
     }
+    AnySatisfiedReqRegs = true;
 
     // Evaluate the cost of the current formula. If it's already worse than
     // the current best, prune the search at that point.
@@ -2654,6 +2679,13 @@ void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
     }
   skip:;
   }
+
+  // If none of the formulae had all of the required registers, relax the
+  // constraint so that we don't exclude all formulae.
+  if (!AnySatisfiedReqRegs) {
+    ReqRegs.clear();
+    goto retry;
+  }
 }
 
 void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {