Newer
Older
Cameron Zwarich
committed
//===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass performs lightweight instruction simplification on loop bodies.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "loop-instsimplify"
Cameron Zwarich
committed
#include "llvm/Instructions.h"
Cameron Zwarich
committed
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwarich
committed
#include "llvm/Analysis/LoopInfo.h"
Cameron Zwarich
committed
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Support/Debug.h"
Cameron Zwarich
committed
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/ADT/Statistic.h"
using namespace llvm;
STATISTIC(NumSimplified, "Number of redundant instructions simplified");
namespace {
Cameron Zwarich
committed
class LoopInstSimplify : public LoopPass {
Cameron Zwarich
committed
public:
static char ID; // Pass ID, replacement for typeid
Cameron Zwarich
committed
LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarich
committed
initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
}
Cameron Zwarich
committed
bool runOnLoop(Loop*, LPPassManager&);
Cameron Zwarich
committed
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Cameron Zwarich
committed
AU.setPreservesCFG();
AU.addRequired<LoopInfo>();
Cameron Zwarich
committed
AU.addRequiredID(LoopSimplifyID);
AU.addPreservedID(LoopSimplifyID);
Cameron Zwarich
committed
AU.addPreservedID(LCSSAID);
AU.addPreserved("scalar-evolution");
Cameron Zwarich
committed
}
};
}
char LoopInstSimplify::ID = 0;
INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
"Simplify instructions in loops", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
INITIALIZE_PASS_DEPENDENCY(LCSSA)
INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
"Simplify instructions in loops", false, false)
Cameron Zwarich
committed
return new LoopInstSimplify();
}
Cameron Zwarich
committed
bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
LoopInfo *LI = &getAnalysis<LoopInfo>();
const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Cameron Zwarich
committed
Cameron Zwarich
committed
SmallVector<BasicBlock*, 8> ExitBlocks;
L->getUniqueExitBlocks(ExitBlocks);
array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
Cameron Zwarich
committed
SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Cameron Zwarich
committed
// The bit we are stealing from the pointer represents whether this basic
// block is the header of a subloop, in which case we only process its phis.
typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwarich
committed
SmallPtrSet<BasicBlock*, 32> Visited;
Cameron Zwarich
committed
bool Changed = false;
bool LocalChanged;
do {
LocalChanged = false;
Cameron Zwarich
committed
VisitStack.clear();
Visited.clear();
Cameron Zwarich
committed
VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich
committed
while (!VisitStack.empty()) {
Cameron Zwarich
committed
WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarich
committed
bool IsSubloopHeader = Item.getInt();
Cameron Zwarich
committed
// Simplify instructions in the current basic block.
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarich
committed
// The first time through the loop ToSimplify is empty and we try to
// simplify all instructions. On later iterations ToSimplify is not
// empty and we only bother simplifying instructions that are in it.
if (!ToSimplify->empty() && !ToSimplify->count(I))
continue;
Cameron Zwarich
committed
// Don't bother simplifying unused instructions.
if (!I->use_empty()) {
Value *V = SimplifyInstruction(I, TD, DT);
Cameron Zwarich
committed
if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich
committed
// Mark all uses for resimplification next time round the loop.
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
UI != UE; ++UI)
Next->insert(cast<Instruction>(*UI));
Cameron Zwarich
committed
I->replaceAllUsesWith(V);
LocalChanged = true;
++NumSimplified;
}
}
LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
Cameron Zwarich
committed
if (IsSubloopHeader && !isa<PHINode>(I))
break;
Cameron Zwarich
committed
}
Cameron Zwarich
committed
// Add all successors to the worklist, except for loop exit blocks and the
// bodies of subloops. We visit the headers of loops so that we can process
// their phis, but we contract the rest of the subloop body and only follow
// edges leading back to the original loop.
Cameron Zwarich
committed
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
++SI) {
BasicBlock *SuccBB = *SI;
Cameron Zwarich
committed
if (!Visited.insert(SuccBB))
continue;
const Loop *SuccLoop = LI->getLoopFor(SuccBB);
if (SuccLoop && SuccLoop->getHeader() == SuccBB
&& L->contains(SuccLoop)) {
VisitStack.push_back(WorklistItem(SuccBB, true));
SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
SuccLoop->getExitBlocks(SubLoopExitBlocks);
for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
BasicBlock *ExitBB = SubLoopExitBlocks[i];
if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB))
VisitStack.push_back(WorklistItem(ExitBB, false));
}
continue;
}
Cameron Zwarich
committed
bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich
committed
ExitBlocks.end(), SuccBB);
if (IsExitBlock)
continue;
VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich
committed
}
}
Cameron Zwarich
committed
// Place the list of instructions to simplify on the next loop iteration
// into ToSimplify.
std::swap(ToSimplify, Next);
Next->clear();
Cameron Zwarich
committed
Changed |= LocalChanged;
Cameron Zwarich
committed
} while (LocalChanged);
return Changed;
}