IndVarSimplify.cpp revision 2b994c7206b3872d908468fc926ba8db483cf0a3
16148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
5b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// This file was developed by the LLVM research group and is distributed under
6b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
7b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
96148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//
1040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// This transformation analyzes and transforms the induction variables (and
1140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// computations derived from them) into simpler forms suitable for subsequent
1240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// analysis and transformation.
1340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
1440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// This transformation make the following changes to each loop with an
1540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// identifiable induction variable:
1640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   1. All loops are transformed to have a SINGLE canonical induction variable
1740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      which starts at zero and steps by one.
1840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   2. The canonical induction variable is guaranteed to be the first PHI node
1940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      in the loop header block.
2040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   3. Any pointer arithmetic recurrences are raised to use array subscripts.
2140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
2240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// If the trip count of a loop is computable, this pass also makes the following
2340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// changes:
2440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   1. The exit condition for the loop is canonicalized to compare the
2540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      induction value against the exit value.  This turns loops like:
2640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//        'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
2740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   2. Any use outside of the loop of an expression derived from the indvar
2840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      is changed to compute the derived value outside of the loop, eliminating
2940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      the dependence on the exit value of the induction variable.  If the only
3040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      purpose of the loop is to compute the exit value of some derived
3140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      expression, this transformation will make the loop dead.
3240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
3340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// This transformation should be followed by strength reduction after all of the
3440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// desired loop transformations have been performed.  Additionally, on targets
3540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// where it is profitable, the loop could be transformed to count down to zero
3640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// (the "do loop" optimization).
376148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//
386148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//===----------------------------------------------------------------------===//
396148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner
40022103b3f33febb7e54b8fdf2c9bc461eea78cbaChris Lattner#include "llvm/Transforms/Scalar.h"
4140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner#include "llvm/BasicBlock.h"
4259fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner#include "llvm/Constants.h"
4318b3c97bc773b24a66eb779e85da1820b0f16b31Chris Lattner#include "llvm/Instructions.h"
4440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner#include "llvm/Type.h"
4559fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner#include "llvm/Analysis/ScalarEvolutionExpressions.h"
4647df12d80db90e125e9f2ff764286ee11665476dJohn Criswell#include "llvm/Analysis/LoopInfo.h"
47455889aa79e3463a4b0f2161e3d9d72a683268b6Chris Lattner#include "llvm/Support/CFG.h"
4847df12d80db90e125e9f2ff764286ee11665476dJohn Criswell#include "llvm/Transforms/Utils/Local.h"
4940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner#include "Support/CommandLine.h"
50a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner#include "Support/Statistic.h"
5147df12d80db90e125e9f2ff764286ee11665476dJohn Criswellusing namespace llvm;
52d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
535e76140536ba66fadeced1cd892f79616f407e3cChris Lattnernamespace {
544a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  /// SCEVExpander - This class uses information about analyze scalars to
554a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  /// rewrite expressions in canonical form.
564a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  ///
574a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  /// Clients should create an instance of this class when rewriting is needed,
584a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  /// and destroying it when finished to allow the release of the associated
594a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  /// memory.
604a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
614a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    ScalarEvolution &SE;
624a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    LoopInfo &LI;
634a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    std::map<SCEVHandle, Value*> InsertedExpressions;
644a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    std::set<Instruction*> InsertedInstructions;
654a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
664a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Instruction *InsertPt;
674a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
684a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    friend class SCEVVisitor<SCEVExpander, Value*>;
694a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  public:
704a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
714a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
724a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// isInsertedInstruction - Return true if the specified instruction was
734a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// inserted by the code rewriter.  If so, the client should not modify the
744a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// instruction.
754a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    bool isInsertedInstruction(Instruction *I) const {
764a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return InsertedInstructions.count(I);
774a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
784a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
794a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// getOrInsertCanonicalInductionVariable - This method returns the
804a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// canonical induction variable of the specified type for the specified
814a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// loop (inserting one if there is none).  A canonical induction variable
824a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// starts at zero and steps by one on each iteration.
834a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
844a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      assert((Ty->isInteger() || Ty->isFloatingPoint()) &&
854a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner             "Can only insert integer or floating point induction variables!");
864a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      SCEVHandle H = SCEVAddRecExpr::get(SCEVUnknown::getIntegerSCEV(0, Ty),
874a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                         SCEVUnknown::getIntegerSCEV(1, Ty), L);
884a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return expand(H);
894a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
904a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
914a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// addInsertedValue - Remember the specified instruction as being the
924a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// canonical form for the specified SCEV.
934a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    void addInsertedValue(Instruction *I, SCEV *S) {
944a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      InsertedExpressions[S] = (Value*)I;
954a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      InsertedInstructions.insert(I);
964a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
974a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
984a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// expandCodeFor - Insert code to directly compute the specified SCEV
994a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// expression into the program.  The inserted code is inserted into the
1004a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// specified block.
1014a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    ///
1024a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// If a particular value sign is required, a type may be specified for the
1034a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    /// result.
1044a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *expandCodeFor(SCEVHandle SH, Instruction *IP, const Type *Ty = 0) {
1054a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      // Expand the code for this SCEV.
1064a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      this->InsertPt = IP;
1074a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return expandInTy(SH, Ty);
1084a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1094a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1104a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  protected:
1114a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *expand(SCEV *S) {
1124a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      // Check to see if we already expanded this.
1134a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
1144a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      if (I != InsertedExpressions.end())
1154a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        return I->second;
1164a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1174a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *V = visit(S);
1184a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      InsertedExpressions[S] = V;
1194a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return V;
1204a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1214a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1224a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *expandInTy(SCEV *S, const Type *Ty) {
1234a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *V = expand(S);
1244a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      if (Ty && V->getType() != Ty) {
1254a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        // FIXME: keep track of the cast instruction.
1264a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        if (Constant *C = dyn_cast<Constant>(V))
1274a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          return ConstantExpr::getCast(C, Ty);
1284a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        else if (Instruction *I = dyn_cast<Instruction>(V)) {
1294a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          // Check to see if there is already a cast.  If there is, use it.
1304a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1314a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner               UI != E; ++UI) {
1324a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner            if ((*UI)->getType() == Ty)
1334a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner              if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
1344a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                BasicBlock::iterator It = I; ++It;
1354a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                while (isa<PHINode>(It)) ++It;
1364a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                if (It != BasicBlock::iterator(CI)) {
1374a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                  // Splice the cast immediately after the operand in question.
1384a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                  I->getParent()->getInstList().splice(It,
1394a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                                       CI->getParent()->getInstList(),
1404a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                                       CI);
1414a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                }
1424a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                return CI;
1434a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner              }
1444a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          }
1454a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          BasicBlock::iterator IP = I; ++IP;
1464a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          if (InvokeInst *II = dyn_cast<InvokeInst>(I))
1474a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner            IP = II->getNormalDest()->begin();
1484a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          while (isa<PHINode>(IP)) ++IP;
1494a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          return new CastInst(V, Ty, V->getName(), IP);
1504a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        } else {
1514a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          // FIXME: check to see if there is already a cast!
1524a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          return new CastInst(V, Ty, V->getName(), InsertPt);
1534a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        }
1544a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      }
1554a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return V;
1564a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1574a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1584a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitConstant(SCEVConstant *S) {
1594a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return S->getValue();
1604a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1614a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1624a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitTruncateExpr(SCEVTruncateExpr *S) {
1634a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *V = expand(S->getOperand());
1644a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return new CastInst(V, S->getType(), "tmp.", InsertPt);
1654a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1664a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1674a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
1682b994c7206b3872d908468fc926ba8db483cf0a3Chris Lattner      Value *V = expandInTy(S->getOperand(),S->getType()->getUnsignedVersion());
1694a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return new CastInst(V, S->getType(), "tmp.", InsertPt);
1704a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1714a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1724a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitAddExpr(SCEVAddExpr *S) {
1734a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      const Type *Ty = S->getType();
1744a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *V = expandInTy(S->getOperand(S->getNumOperands()-1), Ty);
1754a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1764a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      // Emit a bunch of add instructions
1774a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      for (int i = S->getNumOperands()-2; i >= 0; --i)
1784a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner        V = BinaryOperator::create(Instruction::Add, V,
1794a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                   expandInTy(S->getOperand(i), Ty),
1804a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                   "tmp.", InsertPt);
1814a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return V;
1824a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1834a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1844a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitMulExpr(SCEVMulExpr *S);
1854a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1864a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitUDivExpr(SCEVUDivExpr *S) {
1874a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      const Type *Ty = S->getType();
1884a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *LHS = expandInTy(S->getLHS(), Ty);
1894a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Value *RHS = expandInTy(S->getRHS(), Ty);
1904a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return BinaryOperator::create(Instruction::Div, LHS, RHS, "tmp.",
1914a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                    InsertPt);
1924a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1934a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1944a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitAddRecExpr(SCEVAddRecExpr *S);
1954a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
1964a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *visitUnknown(SCEVUnknown *S) {
1974a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      return S->getValue();
1984a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    }
1994a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  };
2004a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner}
2014a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2024a7553e2da506a718f59869c03c5ce113eb40f7aChris LattnerValue *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
2034a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  const Type *Ty = S->getType();
2044a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  int FirstOp = 0;  // Set if we should emit a subtract.
2054a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
2064a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    if (SC->getValue()->isAllOnesValue())
2074a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      FirstOp = 1;
2084a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2094a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  int i = S->getNumOperands()-2;
2104a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  Value *V = expandInTy(S->getOperand(i+1), Ty);
2114a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2124a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // Emit a bunch of multiply instructions
2134a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  for (; i >= FirstOp; --i)
2144a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    V = BinaryOperator::create(Instruction::Mul, V,
2154a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                               expandInTy(S->getOperand(i), Ty),
2164a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                               "tmp.", InsertPt);
2174a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // -1 * ...  --->  0 - ...
2184a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  if (FirstOp == 1)
2194a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    V = BinaryOperator::create(Instruction::Sub, Constant::getNullValue(Ty),
2204a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                               V, "tmp.", InsertPt);
2214a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  return V;
2224a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner}
2234a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2244a7553e2da506a718f59869c03c5ce113eb40f7aChris LattnerValue *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
2254a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  const Type *Ty = S->getType();
2264a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  const Loop *L = S->getLoop();
2274a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
2284a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!");
2294a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2304a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // {X,+,F} --> X + {0,+,F}
2314a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  if (!isa<SCEVConstant>(S->getStart()) ||
2324a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      !cast<SCEVConstant>(S->getStart())->getValue()->isNullValue()) {
2334a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *Start = expandInTy(S->getStart(), Ty);
2344a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
2354a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty);
2364a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty);
2374a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2384a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    // FIXME: look for an existing add to use.
2394a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    return BinaryOperator::create(Instruction::Add, Rest, Start, "tmp.",
2404a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                  InsertPt);
2414a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  }
2424a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2434a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // {0,+,1} --> Insert a canonical induction variable into the loop!
2444a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  if (S->getNumOperands() == 2 &&
2454a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) {
2464a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    // Create and insert the PHI node for the induction variable in the
2474a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    // specified loop.
2484a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    BasicBlock *Header = L->getHeader();
2494a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    PHINode *PN = new PHINode(Ty, "indvar", Header->begin());
2504a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
2514a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2524a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    pred_iterator HPI = pred_begin(Header);
2534a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    assert(HPI != pred_end(Header) && "Loop with zero preds???");
2544a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    if (!L->contains(*HPI)) ++HPI;
2554a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    assert(HPI != pred_end(Header) && L->contains(*HPI) &&
2564a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner           "No backedge in loop?");
2574a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2584a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    // Insert a unit add instruction right before the terminator corresponding
2594a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    // to the back-edge.
2604a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0)
2614a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                          : ConstantInt::get(Ty, 1);
2624a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Instruction *Add = BinaryOperator::create(Instruction::Add, PN, One,
2634a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                              "indvar.next",
2644a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                              (*HPI)->getTerminator());
2654a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2664a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    pred_iterator PI = pred_begin(Header);
2674a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    if (*PI == L->getLoopPreheader())
2684a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      ++PI;
2694a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    PN->addIncoming(Add, *PI);
2704a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    return PN;
2714a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  }
2724a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2734a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // Get the canonical induction variable I for this loop.
2744a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
2754a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2764a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  if (S->getNumOperands() == 2) {   // {0,+,F} --> i*F
2774a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *F = expandInTy(S->getOperand(1), Ty);
2784a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    return BinaryOperator::create(Instruction::Mul, I, F, "tmp.", InsertPt);
2794a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  }
2804a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2814a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // If this is a chain of recurrences, turn it into a closed form, using the
2824a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // folders, then expandCodeFor the closed form.  This allows the folders to
2834a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // simplify the expression without having to build a bunch of special code
2844a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  // into this folder.
2854a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  SCEVHandle IH = SCEVUnknown::get(I);   // Get I as a "symbolic" SCEV.
2864a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2874a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  SCEVHandle V = S->evaluateAtIteration(IH);
2884a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  //std::cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
2894a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2904a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  return expandInTy(V, Ty);
2914a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner}
2924a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2934a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner
2944a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattnernamespace {
295a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner  Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
29640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Statistic<> NumPointer ("indvars", "Number of pointer indvars promoted");
2973adf51d022348b06a1adeef7649fa35928ad9358Chris Lattner  Statistic<> NumInserted("indvars", "Number of canonical indvars added");
29840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Statistic<> NumReplaced("indvars", "Number of exit values replaced");
29940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Statistic<> NumLFTR    ("indvars", "Number of loop exit tests replaced");
3003324e718bc9ac2ede08a14c325848b576849542bChris Lattner
3013324e718bc9ac2ede08a14c325848b576849542bChris Lattner  class IndVarSimplify : public FunctionPass {
30240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    LoopInfo        *LI;
30340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    ScalarEvolution *SE;
30415cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner    bool Changed;
3053324e718bc9ac2ede08a14c325848b576849542bChris Lattner  public:
3063324e718bc9ac2ede08a14c325848b576849542bChris Lattner    virtual bool runOnFunction(Function &) {
30740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      LI = &getAnalysis<LoopInfo>();
30840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      SE = &getAnalysis<ScalarEvolution>();
30915cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner      Changed = false;
31015cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
3113324e718bc9ac2ede08a14c325848b576849542bChris Lattner      // Induction Variables live in the header nodes of loops
31240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
313329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner        runOnLoop(*I);
3143324e718bc9ac2ede08a14c325848b576849542bChris Lattner      return Changed;
3153324e718bc9ac2ede08a14c325848b576849542bChris Lattner    }
3163324e718bc9ac2ede08a14c325848b576849542bChris Lattner
3173324e718bc9ac2ede08a14c325848b576849542bChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
3183324e718bc9ac2ede08a14c325848b576849542bChris Lattner      AU.addRequiredID(LoopSimplifyID);
31940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      AU.addRequired<ScalarEvolution>();
32040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      AU.addRequired<LoopInfo>();
3213324e718bc9ac2ede08a14c325848b576849542bChris Lattner      AU.addPreservedID(LoopSimplifyID);
3223324e718bc9ac2ede08a14c325848b576849542bChris Lattner      AU.setPreservesCFG();
3233324e718bc9ac2ede08a14c325848b576849542bChris Lattner    }
32440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  private:
32540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    void runOnLoop(Loop *L);
32640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
32740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                    std::set<Instruction*> &DeadInsts);
32840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    void LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
3294a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                   SCEVExpander &RW);
33040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    void RewriteLoopExitValues(Loop *L);
33140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
33240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
3333324e718bc9ac2ede08a14c325848b576849542bChris Lattner  };
3343324e718bc9ac2ede08a14c325848b576849542bChris Lattner  RegisterOpt<IndVarSimplify> X("indvars", "Canonicalize Induction Variables");
3355e76140536ba66fadeced1cd892f79616f407e3cChris Lattner}
336394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
3373324e718bc9ac2ede08a14c325848b576849542bChris LattnerPass *llvm::createIndVarSimplifyPass() {
3383324e718bc9ac2ede08a14c325848b576849542bChris Lattner  return new IndVarSimplify();
339394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner}
340394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
34140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// DeleteTriviallyDeadInstructions - If any of the instructions is the
34240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// specified set are trivially dead, delete them and see if this makes any of
34340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// their operands subsequently dead.
34440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattnervoid IndVarSimplify::
34540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris LattnerDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
34640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  while (!Insts.empty()) {
34740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    Instruction *I = *Insts.begin();
34840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    Insts.erase(Insts.begin());
34940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (isInstructionTriviallyDead(I)) {
35040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
35140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
35240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          Insts.insert(U);
35340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      SE->deleteInstructionFromRecords(I);
35440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      I->getParent()->getInstList().erase(I);
35540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      Changed = true;
35640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    }
35740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  }
35840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
3593324e718bc9ac2ede08a14c325848b576849542bChris Lattner
3606148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner
36140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
36240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// recurrence.  If so, change it into an integer recurrence, permitting
36340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// analysis by the SCEV routines.
36440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattnervoid IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
36540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                                BasicBlock *Preheader,
36640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                            std::set<Instruction*> &DeadInsts) {
36740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
36840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
36940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  unsigned BackedgeIdx = PreheaderIdx^1;
37040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (GetElementPtrInst *GEPI =
37140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
37240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (GEPI->getOperand(0) == PN) {
37340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      assert(GEPI->getNumOperands() == 2 && "GEP types must mismatch!");
37440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
37540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Okay, we found a pointer recurrence.  Transform this pointer
37640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // recurrence into an integer recurrence.  Compute the value that gets
37740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // added to the pointer at every iteration.
37840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      Value *AddedVal = GEPI->getOperand(1);
37940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
38040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Insert a new integer PHI node into the top of the block.
38140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      PHINode *NewPhi = new PHINode(AddedVal->getType(),
38240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                    PN->getName()+".rec", PN);
38340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()),
38440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                          Preheader);
38540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Create the new add instruction.
38640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      Value *NewAdd = BinaryOperator::create(Instruction::Add, NewPhi,
38740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                             AddedVal,
38840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                             GEPI->getName()+".rec", GEPI);
38940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
39040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
39140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Update the existing GEP to use the recurrence.
39240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
39340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
39440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Update the GEP to use the new recurrence we just inserted.
39540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      GEPI->setOperand(1, NewAdd);
39640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
39740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Finally, if there are any other users of the PHI node, we must
39840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // insert a new GEP instruction that uses the pre-incremented version
39940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // of the induction amount.
40040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      if (!PN->use_empty()) {
40140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        BasicBlock::iterator InsertPos = PN; ++InsertPos;
40240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        while (isa<PHINode>(InsertPos)) ++InsertPos;
40340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        std::string Name = PN->getName(); PN->setName("");
40440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        Value *PreInc =
40540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          new GetElementPtrInst(PN->getIncomingValue(PreheaderIdx),
40640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                std::vector<Value*>(1, NewPhi), Name,
40740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                InsertPos);
40840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        PN->replaceAllUsesWith(PreInc);
40940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      }
41040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
41140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      // Delete the old PHI for sure, and the GEP if its otherwise unused.
41240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      DeadInsts.insert(PN);
4133324e718bc9ac2ede08a14c325848b576849542bChris Lattner
41440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      ++NumPointer;
41540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      Changed = true;
41640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    }
41740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
4183324e718bc9ac2ede08a14c325848b576849542bChris Lattner
41940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// LinearFunctionTestReplace - This method rewrites the exit condition of the
42059fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// loop to be a canonical != comparison against the incremented loop induction
42159fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// variable.  This pass is able to rewrite the exit tests of any loop where the
42259fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// SCEV analysis can determine a loop-invariant trip count of the loop, which
42359fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// is actually a much broader range than just linear tests.
42440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattnervoid IndVarSimplify::LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
4254a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                                               SCEVExpander &RW) {
42640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Find the exit block for the loop.  We can currently only handle loops with
42740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // a single exit.
428f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  std::vector<BasicBlock*> ExitBlocks;
429f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  L->getExitBlocks(ExitBlocks);
430f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  if (ExitBlocks.size() != 1) return;
431f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  BasicBlock *ExitBlock = ExitBlocks[0];
43240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
43340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Make sure there is only one predecessor block in the loop.
43440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *ExitingBlock = 0;
43540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
43640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner       PI != PE; ++PI)
43740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (L->contains(*PI)) {
43840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      if (ExitingBlock == 0)
43940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        ExitingBlock = *PI;
44040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      else
44140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        return;  // Multiple exits from loop to this block.
44240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    }
44340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  assert(ExitingBlock && "Loop info is broken");
44440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
44540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (!isa<BranchInst>(ExitingBlock->getTerminator()))
44640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    return;  // Can't rewrite non-branch yet
44740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator());
44840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  assert(BI->isConditional() && "Must be conditional to be part of loop!");
44940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
45040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  std::set<Instruction*> InstructionsToDelete;
45140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()))
45240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    InstructionsToDelete.insert(Cond);
45340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
454d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // If the exiting block is not the same as the backedge block, we must compare
455d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // against the preincremented value, otherwise we prefer to compare against
456d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // the post-incremented value.
457d244057a48660c3cd30d219118ece3f947947790Chris Lattner  BasicBlock *Header = L->getHeader();
458d244057a48660c3cd30d219118ece3f947947790Chris Lattner  pred_iterator HPI = pred_begin(Header);
459d244057a48660c3cd30d219118ece3f947947790Chris Lattner  assert(HPI != pred_end(Header) && "Loop with zero preds???");
460d244057a48660c3cd30d219118ece3f947947790Chris Lattner  if (!L->contains(*HPI)) ++HPI;
461d244057a48660c3cd30d219118ece3f947947790Chris Lattner  assert(HPI != pred_end(Header) && L->contains(*HPI) &&
462d244057a48660c3cd30d219118ece3f947947790Chris Lattner         "No backedge in loop?");
463d244057a48660c3cd30d219118ece3f947947790Chris Lattner
464d244057a48660c3cd30d219118ece3f947947790Chris Lattner  SCEVHandle TripCount = IterationCount;
465d244057a48660c3cd30d219118ece3f947947790Chris Lattner  Value *IndVar;
466d244057a48660c3cd30d219118ece3f947947790Chris Lattner  if (*HPI == ExitingBlock) {
467d244057a48660c3cd30d219118ece3f947947790Chris Lattner    // The IterationCount expression contains the number of times that the
468d244057a48660c3cd30d219118ece3f947947790Chris Lattner    // backedge actually branches to the loop header.  This is one less than the
469d244057a48660c3cd30d219118ece3f947947790Chris Lattner    // number of times the loop executes, so add one to it.
470d244057a48660c3cd30d219118ece3f947947790Chris Lattner    Constant *OneC = ConstantInt::get(IterationCount->getType(), 1);
471d244057a48660c3cd30d219118ece3f947947790Chris Lattner    TripCount = SCEVAddExpr::get(IterationCount, SCEVUnknown::get(OneC));
472d244057a48660c3cd30d219118ece3f947947790Chris Lattner    IndVar = L->getCanonicalInductionVariableIncrement();
473d244057a48660c3cd30d219118ece3f947947790Chris Lattner  } else {
474d244057a48660c3cd30d219118ece3f947947790Chris Lattner    // We have to use the preincremented value...
475d244057a48660c3cd30d219118ece3f947947790Chris Lattner    IndVar = L->getCanonicalInductionVariable();
476d244057a48660c3cd30d219118ece3f947947790Chris Lattner  }
47759fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner
47840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Expand the code for the iteration count into the preheader of the loop.
47940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *Preheader = L->getLoopPreheader();
4804a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator(),
48140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                    IndVar->getType());
48240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
48340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Insert a new setne or seteq instruction before the branch.
48440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Instruction::BinaryOps Opcode;
48540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (L->contains(BI->getSuccessor(0)))
48640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    Opcode = Instruction::SetNE;
48740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  else
48840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    Opcode = Instruction::SetEQ;
48940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
49040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Value *Cond = new SetCondInst(Opcode, IndVar, ExitCnt, "exitcond", BI);
49140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BI->setCondition(Cond);
49240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  ++NumLFTR;
49340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Changed = true;
49440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
49540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  DeleteTriviallyDeadInstructions(InstructionsToDelete);
49640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
4973324e718bc9ac2ede08a14c325848b576849542bChris Lattner
4983324e718bc9ac2ede08a14c325848b576849542bChris Lattner
49940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// RewriteLoopExitValues - Check to see if this loop has a computable
50040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// loop-invariant execution count.  If so, this means that we can compute the
50140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// final value of any expressions that are recurrent in the loop, and
50240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// substitute the exit values from the loop into any instructions outside of
50340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// the loop that use the final values of the current expressions.
50440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattnervoid IndVarSimplify::RewriteLoopExitValues(Loop *L) {
50540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *Preheader = L->getLoopPreheader();
50640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
50740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Scan all of the instructions in the loop, looking at those that have
50840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // extra-loop users and which are recurrences.
5094a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  SCEVExpander Rewriter(*SE, *LI);
51040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
51140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // We insert the code into the preheader of the loop if the loop contains
51240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // multiple exit blocks, or in the exit block if there is exactly one.
51340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *BlockToInsertInto;
514f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  std::vector<BasicBlock*> ExitBlocks;
515f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  L->getExitBlocks(ExitBlocks);
516f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  if (ExitBlocks.size() == 1)
517f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner    BlockToInsertInto = ExitBlocks[0];
51840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  else
51940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    BlockToInsertInto = Preheader;
52040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock::iterator InsertPt = BlockToInsertInto->begin();
52140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  while (isa<PHINode>(InsertPt)) ++InsertPt;
52240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
52320aa098ba694aa7e3f5fb5a52d22dba7c1e857aeChris Lattner  bool HasConstantItCount = isa<SCEVConstant>(SE->getIterationCount(L));
52420aa098ba694aa7e3f5fb5a52d22dba7c1e857aeChris Lattner
52540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  std::set<Instruction*> InstructionsToDelete;
52640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
52740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i)
52840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (LI->getLoopFor(L->getBlocks()[i]) == L) {  // Not in a subloop...
52940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      BasicBlock *BB = L->getBlocks()[i];
53040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
53140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        if (I->getType()->isInteger()) {      // Is an integer instruction
53240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          SCEVHandle SH = SE->getSCEV(I);
53320aa098ba694aa7e3f5fb5a52d22dba7c1e857aeChris Lattner          if (SH->hasComputableLoopEvolution(L) ||    // Varies predictably
53420aa098ba694aa7e3f5fb5a52d22dba7c1e857aeChris Lattner              HasConstantItCount) {
53540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            // Find out if this predictably varying value is actually used
53640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            // outside of the loop.  "extra" as opposed to "intra".
53740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            std::vector<User*> ExtraLoopUsers;
53840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
53940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                 UI != E; ++UI)
54040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              if (!L->contains(cast<Instruction>(*UI)->getParent()))
54140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                ExtraLoopUsers.push_back(*UI);
54240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            if (!ExtraLoopUsers.empty()) {
54340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              // Okay, this instruction has a user outside of the current loop
54440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              // and varies predictably in this loop.  Evaluate the value it
54540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              // contains when the loop exits, and insert code for it.
54620aa098ba694aa7e3f5fb5a52d22dba7c1e857aeChris Lattner              SCEVHandle ExitValue = SE->getSCEVAtScope(I, L->getParentLoop());
54740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              if (!isa<SCEVCouldNotCompute>(ExitValue)) {
54840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                Changed = true;
54940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                ++NumReplaced;
5504a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner                Value *NewVal = Rewriter.expandCodeFor(ExitValue, InsertPt,
55140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                                                       I->getType());
55240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
55340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                // Rewrite any users of the computed value outside of the loop
55440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                // with the newly computed value.
55540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                for (unsigned i = 0, e = ExtraLoopUsers.size(); i != e; ++i)
55640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                  ExtraLoopUsers[i]->replaceUsesOfWith(I, NewVal);
55740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
55840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                // If this instruction is dead now, schedule it to be removed.
55940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                if (I->use_empty())
56040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner                  InstructionsToDelete.insert(I);
56140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner              }
56240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            }
56340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          }
56440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        }
5653324e718bc9ac2ede08a14c325848b576849542bChris Lattner    }
5666148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner
56740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  DeleteTriviallyDeadInstructions(InstructionsToDelete);
56840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
56915cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
57015cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
57140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattnervoid IndVarSimplify::runOnLoop(Loop *L) {
57240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // First step.  Check to see if there are any trivial GEP pointer recurrences.
57340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // If there are, change them into integer recurrences, permitting analysis by
57440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // the SCEV routines.
57515cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner  //
57640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *Header    = L->getHeader();
57740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *Preheader = L->getLoopPreheader();
57840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
57940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  std::set<Instruction*> DeadInsts;
58040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (BasicBlock::iterator I = Header->begin();
58140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner       PHINode *PN = dyn_cast<PHINode>(I); ++I)
58240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (isa<PointerType>(PN->getType()))
58340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      EliminatePointerRecurrence(PN, Preheader, DeadInsts);
58415cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
58540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (!DeadInsts.empty())
58640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    DeleteTriviallyDeadInstructions(DeadInsts);
587394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
588394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
58940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Next, transform all loops nesting inside of this loop.
59040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
59140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    runOnLoop(*I);
592394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
59340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Check to see if this loop has a computable loop-invariant execution count.
59440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // If so, this means that we can compute the final value of any expressions
59540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // that are recurrent in the loop, and substitute the exit values from the
59640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // loop into any instructions outside of the loop that use the final values of
59740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // the current expressions.
598394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner  //
59940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  SCEVHandle IterationCount = SE->getIterationCount(L);
60040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (!isa<SCEVCouldNotCompute>(IterationCount))
60140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    RewriteLoopExitValues(L);
60240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
60340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Next, analyze all of the induction variables in the loop, canonicalizing
60440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // auxillary induction variables.
60540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  std::vector<std::pair<PHINode*, SCEVHandle> > IndVars;
60640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
60740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (BasicBlock::iterator I = Header->begin();
60840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner       PHINode *PN = dyn_cast<PHINode>(I); ++I)
60940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (PN->getType()->isInteger()) {  // FIXME: when we have fast-math, enable!
61040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      SCEVHandle SCEV = SE->getSCEV(PN);
61140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      if (SCEV->hasComputableLoopEvolution(L))
61240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        if (SE->shouldSubstituteIndVar(SCEV))  // HACK!
61340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          IndVars.push_back(std::make_pair(PN, SCEV));
61440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    }
615f016ea4ff80c56c467247a90567dd07bddb590f3Chris Lattner
61640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // If there are no induction variables in the loop, there is nothing more to
61740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // do.
618f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner  if (IndVars.empty()) {
619f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner    // Actually, if we know how many times the loop iterates, lets insert a
620f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner    // canonical induction variable to help subsequent passes.
621f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner    if (!isa<SCEVCouldNotCompute>(IterationCount)) {
6224a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      SCEVExpander Rewriter(*SE, *LI);
6234a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner      Rewriter.getOrInsertCanonicalInductionVariable(L,
624f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner                                                     IterationCount->getType());
625f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner      LinearFunctionTestReplace(L, IterationCount, Rewriter);
626f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner    }
627f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner    return;
628f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner  }
629f016ea4ff80c56c467247a90567dd07bddb590f3Chris Lattner
63040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Compute the type of the largest recurrence expression.
63140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  //
63240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  const Type *LargestType = IndVars[0].first->getType();
633fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  bool DifferingSizes = false;
63440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (unsigned i = 1, e = IndVars.size(); i != e; ++i) {
63540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    const Type *Ty = IndVars[i].first->getType();
636fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    DifferingSizes |= Ty->getPrimitiveSize() != LargestType->getPrimitiveSize();
63740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (Ty->getPrimitiveSize() > LargestType->getPrimitiveSize())
63840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      LargestType = Ty;
639500597a1c39e91a3020587318ed61e737b6c613aChris Lattner  }
640394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
64140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Create a rewriter object which we'll use to transform the code with.
6424a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  SCEVExpander Rewriter(*SE, *LI);
64340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
64440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Now that we know the largest of of the induction variables in this loop,
64540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // insert a canonical induction variable of the largest size.
646006118fe8c73d8009d7952b84cabd50882ed0033Chris Lattner  LargestType = LargestType->getUnsignedVersion();
6474a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner  Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
64840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  ++NumInserted;
64940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Changed = true;
65040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
65140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (!isa<SCEVCouldNotCompute>(IterationCount))
65259fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner    LinearFunctionTestReplace(L, IterationCount, Rewriter);
65340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
65440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Now that we have a canonical induction variable, we can rewrite any
65540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // recurrences in terms of the induction variable.  Start with the auxillary
65640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // induction variables, and recursively rewrite any of their uses.
65740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock::iterator InsertPt = Header->begin();
65840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  while (isa<PHINode>(InsertPt)) ++InsertPt;
65940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
6605d461d20aea308471f2a31b718a274bfee28b60cChris Lattner  // If there were induction variables of other sizes, cast the primary
6615d461d20aea308471f2a31b718a274bfee28b60cChris Lattner  // induction variable to the right size for them, avoiding the need for the
6625d461d20aea308471f2a31b718a274bfee28b60cChris Lattner  // code evaluation methods to insert induction variables of different sizes.
663fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  if (DifferingSizes) {
664fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    bool InsertedSizes[17] = { false };
665fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    InsertedSizes[LargestType->getPrimitiveSize()] = true;
666fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    for (unsigned i = 0, e = IndVars.size(); i != e; ++i)
667fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner      if (!InsertedSizes[IndVars[i].first->getType()->getPrimitiveSize()]) {
668fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner        PHINode *PN = IndVars[i].first;
669fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner        InsertedSizes[PN->getType()->getPrimitiveSize()] = true;
670fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner        Instruction *New = new CastInst(IndVar,
671fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner                                        PN->getType()->getUnsignedVersion(),
672fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner                                        "indvar", InsertPt);
673fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner        Rewriter.addInsertedValue(New, SE->getSCEV(New));
674fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner      }
675fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  }
676fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner
677fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  // If there were induction variables of other sizes, cast the primary
678fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  // induction variable to the right size for them, avoiding the need for the
679fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner  // code evaluation methods to insert induction variables of different sizes.
6805d461d20aea308471f2a31b718a274bfee28b60cChris Lattner  std::map<unsigned, Value*> InsertedSizes;
68140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  while (!IndVars.empty()) {
68240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    PHINode *PN = IndVars.back().first;
6834a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner    Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt,
684fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner                                           PN->getType());
685fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    std::string Name = PN->getName();
686fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    PN->setName("");
687fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    NewVal->setName(Name);
6885d461d20aea308471f2a31b718a274bfee28b60cChris Lattner
68940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    // Replace the old PHI Node with the inserted computation.
690fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner    PN->replaceAllUsesWith(NewVal);
69140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    DeadInsts.insert(PN);
69240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    IndVars.pop_back();
69340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    ++NumRemoved;
69440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    Changed = true;
695500597a1c39e91a3020587318ed61e737b6c613aChris Lattner  }
696ba4f3f6a419326df190599421fa149c90235cb72Chris Lattner
697b4782d13d1444d9d18c0a681292cf0d0a32cf3efChris Lattner#if 0
6981363e85df74627530ceede53280613c62a4cdbe3Chris Lattner  // Now replace all derived expressions in the loop body with simpler
6991363e85df74627530ceede53280613c62a4cdbe3Chris Lattner  // expressions.
70040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i)
70140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    if (LI->getLoopFor(L->getBlocks()[i]) == L) {  // Not in a subloop...
70240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      BasicBlock *BB = L->getBlocks()[i];
70340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
70440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        if (I->getType()->isInteger() &&      // Is an integer instruction
7051363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            !I->use_empty() &&
70640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner            !Rewriter.isInsertedInstruction(I)) {
70740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner          SCEVHandle SH = SE->getSCEV(I);
7084a7553e2da506a718f59869c03c5ce113eb40f7aChris Lattner          Value *V = Rewriter.expandCodeFor(SH, I, I->getType());
7091363e85df74627530ceede53280613c62a4cdbe3Chris Lattner          if (V != I) {
7101363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            if (isa<Instruction>(V)) {
7111363e85df74627530ceede53280613c62a4cdbe3Chris Lattner              std::string Name = I->getName();
7121363e85df74627530ceede53280613c62a4cdbe3Chris Lattner              I->setName("");
7131363e85df74627530ceede53280613c62a4cdbe3Chris Lattner              V->setName(Name);
7141363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            }
7151363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            I->replaceAllUsesWith(V);
7161363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            DeadInsts.insert(I);
7171363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            ++NumRemoved;
7181363e85df74627530ceede53280613c62a4cdbe3Chris Lattner            Changed = true;
7191363e85df74627530ceede53280613c62a4cdbe3Chris Lattner          }
72040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner        }
721394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner    }
722b4782d13d1444d9d18c0a681292cf0d0a32cf3efChris Lattner#endif
7231363e85df74627530ceede53280613c62a4cdbe3Chris Lattner
7241363e85df74627530ceede53280613c62a4cdbe3Chris Lattner  DeleteTriviallyDeadInstructions(DeadInsts);
7256148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner}
726