Skip to content
LoopStrengthReduce.cpp 83.4 KiB
Newer Older
  SE = &getAnalysis<ScalarEvolution>();
  TD = &getAnalysis<TargetData>();
  UIntPtrTy = TD->getIntPtrType();
  Changed = false;

  // Find all uses of induction variables in this loop, and catagorize
  // them by stride.  Start by finding all of the PHI nodes in the header for
  // this loop.  If they are induction variables, inspect their uses.
Evan Cheng's avatar
Evan Cheng committed
  SmallPtrSet<Instruction*,16> Processed;   // Don't reprocess instructions.
  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
    AddUsersIfInteresting(I, L, Processed);
Evan Cheng's avatar
Evan Cheng committed
  if (!IVUsesByStride.empty()) {
    // Optimize induction variables.  Some indvar uses can be transformed to use
    // strides that will be needed for other purposes.  A common example of this
    // is the exit test for the loop, which can often be rewritten to use the
    // computation of some other indvar to decide when to terminate the loop.
    OptimizeIndvars(L);
Evan Cheng's avatar
Evan Cheng committed
    // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
    // doing computation in byte values, promote to 32-bit values if safe.
Evan Cheng's avatar
Evan Cheng committed
    // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
    // could have something like "for(i) { foo(i*8); bar(i*16) }", which should
    // be codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.
    // Need to be careful that IV's are all the same type.  Only works for
    // intptr_t indvars.
Evan Cheng's avatar
Evan Cheng committed
    // If we only have one stride, we can more aggressively eliminate some
    // things.
    bool HasOneStride = IVUsesByStride.size() == 1;
Evan Cheng's avatar
Evan Cheng committed
    DOUT << "\nLSR on ";
    DEBUG(L->dump());
Evan Cheng's avatar
Evan Cheng committed
    // IVsByStride keeps IVs for one particular loop.
    assert(IVsByStride.empty() && "Stale entries in IVsByStride?");

    // Sort the StrideOrder so we process larger strides first.
    std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());

    // Note: this processes each stride/type pair individually.  All users
    // passed into StrengthReduceStridedIVUsers have the same type AND stride.
    // Also, note that we iterate over IVUsesByStride indirectly by using
    // StrideOrder. This extra layer of indirection makes the ordering of
    // strides deterministic - not dependent on map order.
    for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
      std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = 
        IVUsesByStride.find(StrideOrder[Stride]);
      assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
      StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
    }
  // We're done analyzing this loop; release all the state we built up for it.
  CastedPointers.clear();
  IVUsesByStride.clear();
  IVsByStride.clear();
  StrideOrder.clear();

  // Clean up after ourselves
  if (!DeadInsts.empty()) {
    DeleteTriviallyDeadInstructions(DeadInsts);

    BasicBlock::iterator I = L->getHeader()->begin();
    while (PHINode *PN = dyn_cast<PHINode>(I++)) {
      // At this point, we know that we have killed one or more IV users.
      // It is worth checking to see if the cann indvar is also
      // dead, so that we can remove it as well.
      //
      // We can remove a PHI if it is on a cycle in the def-use graph
      // where each node in the cycle has degree one, i.e. only one use,
      // and is an instruction with no side effects.
      //
      // FIXME: this needs to eliminate an induction variable even if it's being
      // compared against some value to decide loop termination.
      if (!PN->hasOneUse())
        continue;
      
      SmallPtrSet<PHINode *, 4> PHIs;
      for (Instruction *J = dyn_cast<Instruction>(*PN->use_begin());
           J && J->hasOneUse() && !J->mayWriteToMemory();
           J = dyn_cast<Instruction>(*J->use_begin())) {
        // If we find the original PHI, we've discovered a cycle.
        if (J == PN) {
          // Break the cycle and mark the PHI for deletion.
          SE->deleteValueFromRecords(PN);
          PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
          DeadInsts.insert(PN);
          Changed = true;
          break;
        // If we find a PHI more than once, we're on a cycle that
        // won't prove fruitful.
        if (isa<PHINode>(J) && !PHIs.insert(cast<PHINode>(J)))
          break;
    DeleteTriviallyDeadInstructions(DeadInsts);
Evan Cheng's avatar
Evan Cheng committed
  return Changed;