2006-05-26 21:11:53 +00:00
|
|
|
//===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
|
2006-05-26 13:58:26 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-05-26 13:58:26 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass transforms loops by placing phi nodes at the end of the loops for
|
|
|
|
// all values that are live across the loop boundary. For example, it turns
|
|
|
|
// the left into the right code:
|
|
|
|
//
|
|
|
|
// for (...) for (...)
|
2007-05-11 21:10:54 +00:00
|
|
|
// if (c) if (c)
|
2006-05-26 13:58:26 +00:00
|
|
|
// X1 = ... X1 = ...
|
|
|
|
// else else
|
|
|
|
// X2 = ... X2 = ...
|
|
|
|
// X3 = phi(X1, X2) X3 = phi(X1, X2)
|
2008-06-03 00:57:21 +00:00
|
|
|
// ... = X3 + 4 X4 = phi(X3)
|
|
|
|
// ... = X4 + 4
|
2006-05-26 13:58:26 +00:00
|
|
|
//
|
|
|
|
// This is still valid LLVM; the extra phi nodes are purely redundant, and will
|
|
|
|
// be trivially eliminated by InstCombine. The major benefit of this
|
|
|
|
// transformation is that it makes many other loop optimizations, such as
|
|
|
|
// LoopUnswitching, simpler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-05-26 21:11:53 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-02-10 19:39:35 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2007-07-13 23:57:11 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-03-04 12:09:19 +00:00
|
|
|
#include "llvm/IR/PredIteratorCache.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2014-01-25 04:07:24 +00:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2006-05-26 13:58:26 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "lcssa"
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
STATISTIC(NumLCSSA, "Number of live out of a loop variables");
|
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
/// Return true if the specified block is in the list.
|
2009-10-11 02:53:37 +00:00
|
|
|
static bool isExitBlock(BasicBlock *BB,
|
2014-01-25 04:07:24 +00:00
|
|
|
const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
|
2009-10-11 02:53:37 +00:00
|
|
|
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
|
|
|
|
if (ExitBlocks[i] == BB)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-02 00:06:09 +00:00
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
/// Given an instruction in the loop, check to see if it has any uses that are
|
|
|
|
/// outside the current loop. If so, insert LCSSA PHI nodes and rewrite the
|
|
|
|
/// uses.
|
|
|
|
static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &ExitBlocks,
|
|
|
|
PredIteratorCache &PredCache) {
|
|
|
|
SmallVector<Use *, 16> UsesToRewrite;
|
|
|
|
|
|
|
|
BasicBlock *InstBB = Inst.getParent();
|
|
|
|
|
2014-03-09 03:16:01 +00:00
|
|
|
for (Use &U : Inst.uses()) {
|
|
|
|
Instruction *User = cast<Instruction>(U.getUser());
|
|
|
|
BasicBlock *UserBB = User->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(User))
|
|
|
|
UserBB = PN->getIncomingBlock(U);
|
2014-01-25 04:07:24 +00:00
|
|
|
|
|
|
|
if (InstBB != UserBB && !L.contains(UserBB))
|
2014-03-09 03:16:01 +00:00
|
|
|
UsesToRewrite.push_back(&U);
|
2009-10-11 02:53:37 +00:00
|
|
|
}
|
2010-07-09 14:29:14 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// If there are no uses outside the loop, exit with no change.
|
2014-01-25 04:07:24 +00:00
|
|
|
if (UsesToRewrite.empty())
|
|
|
|
return false;
|
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
++NumLCSSA; // We are applying the transformation
|
2009-06-26 00:31:13 +00:00
|
|
|
|
|
|
|
// Invoke instructions are special in that their result value is not available
|
2014-01-25 04:07:24 +00:00
|
|
|
// along their unwind edge. The code below tests to see whether DomBB
|
|
|
|
// dominates
|
2009-06-26 00:31:13 +00:00
|
|
|
// the value, so adjust DomBB to the normal destination block, which is
|
|
|
|
// effectively where the value is first usable.
|
2014-01-25 04:07:24 +00:00
|
|
|
BasicBlock *DomBB = Inst.getParent();
|
|
|
|
if (InvokeInst *Inv = dyn_cast<InvokeInst>(&Inst))
|
2009-06-26 00:31:13 +00:00
|
|
|
DomBB = Inv->getNormalDest();
|
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
DomTreeNode *DomNode = DT.getNode(DomBB);
|
2006-08-02 00:06:09 +00:00
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
SmallVector<PHINode *, 16> AddedPHIs;
|
2011-03-15 07:41:25 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
SSAUpdater SSAUpdate;
|
2014-01-25 04:07:24 +00:00
|
|
|
SSAUpdate.Initialize(Inst.getType(), Inst.getName());
|
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// Insert the LCSSA phi's into all of the exit blocks dominated by the
|
2009-11-09 18:28:24 +00:00
|
|
|
// value, and add them to the Phi's map.
|
2014-01-25 04:07:24 +00:00
|
|
|
for (SmallVectorImpl<BasicBlock *>::const_iterator BBI = ExitBlocks.begin(),
|
|
|
|
BBE = ExitBlocks.end();
|
|
|
|
BBI != BBE; ++BBI) {
|
2009-10-11 02:53:37 +00:00
|
|
|
BasicBlock *ExitBB = *BBI;
|
2014-01-25 04:07:24 +00:00
|
|
|
if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
|
|
|
|
continue;
|
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// If we already inserted something for this BB, don't reprocess it.
|
2014-01-25 04:07:24 +00:00
|
|
|
if (SSAUpdate.HasValueForBlock(ExitBB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PHINode *PN = PHINode::Create(Inst.getType(), PredCache.GetNumPreds(ExitBB),
|
|
|
|
Inst.getName() + ".lcssa", ExitBB->begin());
|
2006-08-02 00:06:09 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// Add inputs from inside the loop for this PHI.
|
2009-11-09 18:28:24 +00:00
|
|
|
for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
|
2014-01-25 04:07:24 +00:00
|
|
|
PN->addIncoming(&Inst, *PI);
|
2009-11-09 18:28:24 +00:00
|
|
|
|
|
|
|
// If the exit block has a predecessor not within the loop, arrange for
|
2009-11-09 18:59:22 +00:00
|
|
|
// the incoming value use corresponding to that predecessor to be
|
2009-11-09 18:28:24 +00:00
|
|
|
// rewritten in terms of a different LCSSA PHI.
|
2014-01-25 04:07:24 +00:00
|
|
|
if (!L.contains(*PI))
|
2009-11-09 18:28:24 +00:00
|
|
|
UsesToRewrite.push_back(
|
2014-01-25 04:07:24 +00:00
|
|
|
&PN->getOperandUse(PN->getOperandNumForIncomingValue(
|
|
|
|
PN->getNumIncomingValues() - 1)));
|
2009-11-09 18:28:24 +00:00
|
|
|
}
|
2011-03-15 07:41:25 +00:00
|
|
|
|
|
|
|
AddedPHIs.push_back(PN);
|
2014-01-25 04:07:24 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// Remember that this phi makes the value alive in this block.
|
|
|
|
SSAUpdate.AddAvailableValue(ExitBB, PN);
|
2006-06-03 23:22:50 +00:00
|
|
|
}
|
2012-10-31 10:01:29 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// Rewrite all uses outside the loop in terms of the new PHIs we just
|
|
|
|
// inserted.
|
|
|
|
for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
|
|
|
|
// If this use is in an exit block, rewrite to use the newly inserted PHI.
|
|
|
|
// This is required for correctness because SSAUpdate doesn't handle uses in
|
|
|
|
// the same block. It assumes the PHI we inserted is at the end of the
|
|
|
|
// block.
|
|
|
|
Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
|
|
|
|
BasicBlock *UserBB = User->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(User))
|
|
|
|
UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
|
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
|
2012-10-31 16:30:03 +00:00
|
|
|
// Tell the VHs that the uses changed. This updates SCEV's caches.
|
|
|
|
if (UsesToRewrite[i]->get()->hasValueHandle())
|
|
|
|
ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
|
2009-10-11 02:53:37 +00:00
|
|
|
UsesToRewrite[i]->set(UserBB->begin());
|
2006-08-02 00:06:09 +00:00
|
|
|
continue;
|
2006-06-01 06:05:47 +00:00
|
|
|
}
|
2014-01-25 04:07:24 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
// Otherwise, do full PHI insertion.
|
|
|
|
SSAUpdate.RewriteUse(*UsesToRewrite[i]);
|
2006-08-02 00:06:09 +00:00
|
|
|
}
|
2011-03-15 07:41:25 +00:00
|
|
|
|
|
|
|
// Remove PHI nodes that did not have any uses rewritten.
|
|
|
|
for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
|
2011-03-15 18:42:33 +00:00
|
|
|
if (AddedPHIs[i]->use_empty())
|
2011-03-15 07:41:25 +00:00
|
|
|
AddedPHIs[i]->eraseFromParent();
|
|
|
|
}
|
2014-01-25 04:07:24 +00:00
|
|
|
|
2009-10-11 02:53:37 +00:00
|
|
|
return true;
|
2006-05-31 20:55:06 +00:00
|
|
|
}
|
2006-08-02 00:06:09 +00:00
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
/// Return true if the specified block dominates at least
|
|
|
|
/// one of the blocks in the specified list.
|
|
|
|
static bool
|
|
|
|
blockDominatesAnExit(BasicBlock *BB,
|
|
|
|
DominatorTree &DT,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
|
|
|
|
DomTreeNode *DomNode = DT.getNode(BB);
|
|
|
|
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
|
|
|
|
if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i])))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Get the set of exiting blocks.
|
|
|
|
SmallVector<BasicBlock *, 8> ExitBlocks;
|
|
|
|
L.getExitBlocks(ExitBlocks);
|
|
|
|
|
|
|
|
if (ExitBlocks.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PredIteratorCache PredCache;
|
|
|
|
|
|
|
|
// Look at all the instructions in the loop, checking to see if they have uses
|
|
|
|
// outside the loop. If so, rewrite those uses.
|
|
|
|
for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end();
|
|
|
|
BBI != BBE; ++BBI) {
|
|
|
|
BasicBlock *BB = *BBI;
|
|
|
|
|
|
|
|
// For large loops, avoid use-scanning by using dominance information: In
|
|
|
|
// particular, if a block does not dominate any of the loop exits, then none
|
|
|
|
// of the values defined in the block could be used outside the loop.
|
|
|
|
if (!blockDominatesAnExit(BB, DT, ExitBlocks))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
|
|
|
// Reject two common cases fast: instructions with no uses (like stores)
|
|
|
|
// and instructions with one use that is in the same block as this.
|
|
|
|
if (I->use_empty() ||
|
2014-03-09 03:16:01 +00:00
|
|
|
(I->hasOneUse() && I->user_back()->getParent() == BB &&
|
|
|
|
!isa<PHINode>(I->user_back())))
|
2014-01-25 04:07:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we modified the code, remove any caches about the loop from SCEV to
|
|
|
|
// avoid dangling entries.
|
|
|
|
// FIXME: This is a big hammer, can we clear the cache more selectively?
|
|
|
|
if (SE && Changed)
|
|
|
|
SE->forgetLoop(&L);
|
|
|
|
|
|
|
|
assert(L.isLCSSAForm(DT));
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-01-28 01:25:38 +00:00
|
|
|
/// Process a loop nest depth first.
|
|
|
|
bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT,
|
|
|
|
ScalarEvolution *SE) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Recurse depth-first through inner loops.
|
|
|
|
for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
|
|
|
|
Changed |= formLCSSARecursively(**LI, DT, SE);
|
|
|
|
|
|
|
|
Changed |= formLCSSA(L, DT, SE);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-01-25 04:07:24 +00:00
|
|
|
namespace {
|
|
|
|
struct LCSSA : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
LCSSA() : FunctionPass(ID) {
|
|
|
|
initializeLCSSAPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cached analysis information for the current function.
|
|
|
|
DominatorTree *DT;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2014-01-25 04:07:24 +00:00
|
|
|
|
|
|
|
/// This transformation requires natural loop information & requires that
|
|
|
|
/// loop preheaders be inserted into the CFG. It maintains both of these,
|
|
|
|
/// as well as the CFG. It also requires dominator information.
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-25 04:07:24 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2014-02-10 19:39:35 +00:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2014-01-25 04:07:24 +00:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-05 09:10:37 +00:00
|
|
|
void verifyAnalysis() const override;
|
2014-01-25 04:07:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char LCSSA::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createLCSSAPass() { return new LCSSA(); }
|
|
|
|
char &llvm::LCSSAID = LCSSA::ID;
|
|
|
|
|
|
|
|
|
|
|
|
/// Process all loops in the function, inner-most out.
|
|
|
|
bool LCSSA::runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
SE = getAnalysisIfAvailable<ScalarEvolution>();
|
|
|
|
|
|
|
|
// Simplify each loop nest in the function.
|
|
|
|
for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
|
2014-01-28 01:25:38 +00:00
|
|
|
Changed |= formLCSSARecursively(**I, *DT, SE);
|
2014-01-25 04:07:24 +00:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void verifyLoop(Loop &L, DominatorTree &DT) {
|
|
|
|
// Recurse depth-first through inner loops.
|
|
|
|
for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
|
|
|
|
verifyLoop(**LI, DT);
|
|
|
|
|
|
|
|
// Check the special guarantees that LCSSA makes.
|
|
|
|
//assert(L.isLCSSAForm(DT) && "LCSSA form not preserved!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void LCSSA::verifyAnalysis() const {
|
|
|
|
// Verify each loop nest in the function, assuming LI still points at that
|
|
|
|
// function's loop info.
|
|
|
|
for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
|
|
|
|
verifyLoop(**I, *DT);
|
|
|
|
}
|