+#include "llvm/Analysis/LoopPass.h"
+#include "llvm/Support/CFG.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Transforms/Utils/Local.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
+
+STATISTIC(NumRemoved , "Number of aux indvars removed");
+STATISTIC(NumPointer , "Number of pointer indvars promoted");
+STATISTIC(NumInserted, "Number of canonical indvars added");
+STATISTIC(NumReplaced, "Number of exit values replaced");
+STATISTIC(NumLFTR , "Number of loop exit tests replaced");
+
+namespace {
+ class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
+ LoopInfo *LI;
+ ScalarEvolution *SE;
+ bool Changed;
+ public:
+
+ static char ID; // Pass identification, replacement for typeid
+ IndVarSimplify() : LoopPass((intptr_t)&ID) {}
+
+ bool runOnLoop(Loop *L, LPPassManager &LPM);
+ bool doInitialization(Loop *L, LPPassManager &LPM);
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<ScalarEvolution>();
+ AU.addRequiredID(LCSSAID);
+ AU.addRequiredID(LoopSimplifyID);
+ AU.addRequired<LoopInfo>();
+ AU.addPreservedID(LoopSimplifyID);
+ AU.addPreservedID(LCSSAID);
+ AU.setPreservesCFG();
+ }
+
+ private:
+
+ void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
+ std::set<Instruction*> &DeadInsts);
+ Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
+ SCEVExpander &RW);
+ void RewriteLoopExitValues(Loop *L);
+
+ void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
+ };
+}
+
+char IndVarSimplify::ID = 0;
+static RegisterPass<IndVarSimplify>
+X("indvars", "Canonicalize Induction Variables");
+
+LoopPass *llvm::createIndVarSimplifyPass() {
+ return new IndVarSimplify();
+}
+
+/// DeleteTriviallyDeadInstructions - If any of the instructions is the
+/// specified set are trivially dead, delete them and see if this makes any of
+/// their operands subsequently dead.
+void IndVarSimplify::
+DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
+ while (!Insts.empty()) {
+ Instruction *I = *Insts.begin();
+ Insts.erase(Insts.begin());
+ if (isInstructionTriviallyDead(I)) {
+ for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
+ if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
+ Insts.insert(U);
+ SE->deleteValueFromRecords(I);
+ DOUT << "INDVARS: Deleting: " << *I;
+ I->eraseFromParent();
+ Changed = true;
+ }
+ }
+}
+
+
+/// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
+/// recurrence. If so, change it into an integer recurrence, permitting
+/// analysis by the SCEV routines.
+void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
+ BasicBlock *Preheader,
+ std::set<Instruction*> &DeadInsts) {
+ assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
+ unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
+ unsigned BackedgeIdx = PreheaderIdx^1;
+ if (GetElementPtrInst *GEPI =
+ dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
+ if (GEPI->getOperand(0) == PN) {
+ assert(GEPI->getNumOperands() == 2 && "GEP types must match!");
+ DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI;
+
+ // Okay, we found a pointer recurrence. Transform this pointer
+ // recurrence into an integer recurrence. Compute the value that gets
+ // added to the pointer at every iteration.
+ Value *AddedVal = GEPI->getOperand(1);
+
+ // Insert a new integer PHI node into the top of the block.
+ PHINode *NewPhi = PHINode::Create(AddedVal->getType(),
+ PN->getName()+".rec", PN);
+ NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
+
+ // Create the new add instruction.
+ Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
+ GEPI->getName()+".rec", GEPI);
+ NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
+
+ // Update the existing GEP to use the recurrence.
+ GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
+
+ // Update the GEP to use the new recurrence we just inserted.
+ GEPI->setOperand(1, NewAdd);
+
+ // If the incoming value is a constant expr GEP, try peeling out the array
+ // 0 index if possible to make things simpler.
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0)))
+ if (CE->getOpcode() == Instruction::GetElementPtr) {
+ unsigned NumOps = CE->getNumOperands();
+ assert(NumOps > 1 && "CE folding didn't work!");
+ if (CE->getOperand(NumOps-1)->isNullValue()) {
+ // Check to make sure the last index really is an array index.
+ gep_type_iterator GTI = gep_type_begin(CE);
+ for (unsigned i = 1, e = CE->getNumOperands()-1;
+ i != e; ++i, ++GTI)
+ /*empty*/;
+ if (isa<SequentialType>(*GTI)) {
+ // Pull the last index out of the constant expr GEP.
+ SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1);
+ Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
+ &CEIdxs[0],
+ CEIdxs.size());
+ Value *Idx[2];
+ Idx[0] = Constant::getNullValue(Type::Int32Ty);
+ Idx[1] = NewAdd;
+ GetElementPtrInst *NGEPI = GetElementPtrInst::Create(
+ NCE, Idx, Idx + 2,
+ GEPI->getName(), GEPI);
+ SE->deleteValueFromRecords(GEPI);
+ GEPI->replaceAllUsesWith(NGEPI);
+ GEPI->eraseFromParent();
+ GEPI = NGEPI;
+ }
+ }
+ }
+
+
+ // Finally, if there are any other users of the PHI node, we must
+ // insert a new GEP instruction that uses the pre-incremented version
+ // of the induction amount.
+ if (!PN->use_empty()) {
+ BasicBlock::iterator InsertPos = PN; ++InsertPos;
+ while (isa<PHINode>(InsertPos)) ++InsertPos;
+ Value *PreInc =
+ GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx),
+ NewPhi, "", InsertPos);
+ PreInc->takeName(PN);
+ PN->replaceAllUsesWith(PreInc);
+ }
+
+ // Delete the old PHI for sure, and the GEP if its otherwise unused.
+ DeadInsts.insert(PN);
+
+ ++NumPointer;
+ Changed = true;
+ }
+}
+
+/// LinearFunctionTestReplace - This method rewrites the exit condition of the
+/// loop to be a canonical != comparison against the incremented loop induction
+/// variable. This pass is able to rewrite the exit tests of any loop where the
+/// SCEV analysis can determine a loop-invariant trip count of the loop, which
+/// is actually a much broader range than just linear tests.
+///
+/// This method returns a "potentially dead" instruction whose computation chain
+/// should be deleted when convenient.
+Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
+ SCEV *IterationCount,
+ SCEVExpander &RW) {
+ // Find the exit block for the loop. We can currently only handle loops with
+ // a single exit.
+ SmallVector<BasicBlock*, 8> ExitBlocks;
+ L->getExitBlocks(ExitBlocks);
+ if (ExitBlocks.size() != 1) return 0;
+ BasicBlock *ExitBlock = ExitBlocks[0];
+
+ // Make sure there is only one predecessor block in the loop.
+ BasicBlock *ExitingBlock = 0;
+ for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
+ PI != PE; ++PI)
+ if (L->contains(*PI)) {
+ if (ExitingBlock == 0)
+ ExitingBlock = *PI;
+ else
+ return 0; // Multiple exits from loop to this block.
+ }
+ assert(ExitingBlock && "Loop info is broken");
+
+ if (!isa<BranchInst>(ExitingBlock->getTerminator()))
+ return 0; // Can't rewrite non-branch yet
+ BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator());
+ assert(BI->isConditional() && "Must be conditional to be part of loop!");
+
+ Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition());