2006-05-27 05:11:53 +08:00
|
|
|
//===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
|
2006-05-26 21:58:26 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Owen Anderson and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 (...)
|
|
|
|
// if (c) if(c)
|
|
|
|
// X1 = ... X1 = ...
|
|
|
|
// else else
|
|
|
|
// X2 = ... X2 = ...
|
|
|
|
// X3 = phi(X1, X2) X3 = phi(X1, X2)
|
|
|
|
// ... = X3 + 4 X4 = phi(X3)
|
|
|
|
// ... = X4 + 4
|
|
|
|
//
|
|
|
|
// 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-27 05:11:53 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2006-05-26 21:58:26 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2006-06-04 08:02:23 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2006-05-27 08:31:37 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-05-27 05:11:53 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2006-05-26 21:58:26 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
2006-05-27 05:11:53 +08:00
|
|
|
#include <algorithm>
|
2006-05-28 02:47:11 +08:00
|
|
|
#include <map>
|
2006-05-26 21:58:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2006-05-29 03:33:28 +08:00
|
|
|
static Statistic<> NumLCSSA("lcssa",
|
|
|
|
"Number of live out of a loop variables");
|
2006-05-27 08:31:37 +08:00
|
|
|
|
2006-05-26 21:58:26 +08:00
|
|
|
class LCSSA : public FunctionPass {
|
|
|
|
public:
|
2006-05-27 08:31:37 +08:00
|
|
|
|
|
|
|
|
2006-05-26 21:58:26 +08:00
|
|
|
LoopInfo *LI; // Loop information
|
2006-06-04 08:55:19 +08:00
|
|
|
DominatorTree *DT; // Dominator Tree for the current Function...
|
2006-05-27 05:11:53 +08:00
|
|
|
DominanceFrontier *DF; // Current Dominance Frontier
|
2006-06-06 12:28:30 +08:00
|
|
|
std::vector<BasicBlock*> LoopBlocks;
|
2006-05-26 21:58:26 +08:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F);
|
2006-05-27 05:19:17 +08:00
|
|
|
bool visitSubloop(Loop* L);
|
2006-06-01 04:55:06 +08:00
|
|
|
void processInstruction(Instruction* Instr,
|
|
|
|
const std::vector<BasicBlock*>& exitBlocks);
|
2006-05-26 21:58:26 +08:00
|
|
|
|
|
|
|
/// This transformation requires natural loop information & requires that
|
2006-05-27 08:31:37 +08:00
|
|
|
/// loop preheaders be inserted into the CFG. It maintains both of these,
|
|
|
|
/// as well as the CFG. It also requires dominator information.
|
2006-05-26 21:58:26 +08:00
|
|
|
///
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2006-05-27 05:11:53 +08:00
|
|
|
AU.setPreservesCFG();
|
2006-05-26 21:58:26 +08:00
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
|
|
|
AU.addRequired<LoopInfo>();
|
2006-05-28 02:47:11 +08:00
|
|
|
AU.addRequired<DominatorTree>();
|
2006-05-27 05:11:53 +08:00
|
|
|
AU.addRequired<DominanceFrontier>();
|
2006-05-26 21:58:26 +08:00
|
|
|
}
|
|
|
|
private:
|
2006-06-04 08:02:23 +08:00
|
|
|
SetVector<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L);
|
2006-06-01 04:55:06 +08:00
|
|
|
Instruction *getValueDominatingBlock(BasicBlock *BB,
|
2006-06-04 08:02:23 +08:00
|
|
|
std::map<BasicBlock*, Instruction*>& PotDoms);
|
|
|
|
|
2006-06-06 12:28:30 +08:00
|
|
|
/// inLoop - returns true if the given block is within the current loop
|
|
|
|
const bool inLoop(BasicBlock* B) {
|
2006-06-06 12:36:36 +08:00
|
|
|
return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B);
|
|
|
|
}
|
2006-05-26 21:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass");
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); }
|
2006-06-09 04:02:53 +08:00
|
|
|
const PassInfo *llvm::LCSSAID = X.getPassInfo();
|
2006-05-26 21:58:26 +08:00
|
|
|
|
2006-06-09 04:02:53 +08:00
|
|
|
/// runOnFunction - Process all loops in the function, inner-most out.
|
2006-05-26 21:58:26 +08:00
|
|
|
bool LCSSA::runOnFunction(Function &F) {
|
|
|
|
bool changed = false;
|
|
|
|
LI = &getAnalysis<LoopInfo>();
|
2006-05-27 05:11:53 +08:00
|
|
|
DF = &getAnalysis<DominanceFrontier>();
|
|
|
|
DT = &getAnalysis<DominatorTree>();
|
2006-05-26 21:58:26 +08:00
|
|
|
|
|
|
|
for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) {
|
2006-05-27 05:11:53 +08:00
|
|
|
changed |= visitSubloop(*I);
|
2006-05-26 21:58:26 +08:00
|
|
|
}
|
2006-06-11 17:32:57 +08:00
|
|
|
|
2006-05-26 21:58:26 +08:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2006-06-09 04:02:53 +08:00
|
|
|
/// visitSubloop - Recursively process all subloops, and then process the given
|
|
|
|
/// loop if it has live-out values.
|
2006-05-27 05:11:53 +08:00
|
|
|
bool LCSSA::visitSubloop(Loop* L) {
|
|
|
|
for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
|
|
|
|
visitSubloop(*I);
|
|
|
|
|
|
|
|
// Speed up queries by creating a sorted list of blocks
|
2006-06-06 12:28:30 +08:00
|
|
|
LoopBlocks.clear();
|
|
|
|
LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
|
|
|
|
std::sort(LoopBlocks.begin(), LoopBlocks.end());
|
2006-05-27 05:11:53 +08:00
|
|
|
|
2006-06-04 08:02:23 +08:00
|
|
|
SetVector<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L);
|
2006-05-26 21:58:26 +08:00
|
|
|
|
2006-06-01 04:55:06 +08:00
|
|
|
// If no values are affected, we can save a lot of work, since we know that
|
|
|
|
// nothing will be changed.
|
|
|
|
if (AffectedValues.empty())
|
|
|
|
return false;
|
|
|
|
|
2006-05-26 21:58:26 +08:00
|
|
|
std::vector<BasicBlock*> exitBlocks;
|
|
|
|
L->getExitBlocks(exitBlocks);
|
|
|
|
|
2006-05-28 02:47:11 +08:00
|
|
|
|
|
|
|
// Iterate over all affected values for this loop and insert Phi nodes
|
|
|
|
// for them in the appropriate exit blocks
|
2006-06-01 04:55:06 +08:00
|
|
|
|
2006-06-04 08:02:23 +08:00
|
|
|
for (SetVector<Instruction*>::iterator I = AffectedValues.begin(),
|
2006-05-27 05:11:53 +08:00
|
|
|
E = AffectedValues.end(); I != E; ++I) {
|
2006-06-04 08:02:23 +08:00
|
|
|
processInstruction(*I, exitBlocks);
|
2006-06-01 04:55:06 +08:00
|
|
|
}
|
|
|
|
|
2006-06-06 12:28:30 +08:00
|
|
|
return true;
|
2006-06-01 04:55:06 +08:00
|
|
|
}
|
|
|
|
|
2006-06-09 04:02:53 +08:00
|
|
|
/// processInstruction - Given a live-out instruction, insert LCSSA Phi nodes,
|
|
|
|
/// eliminate all out-of-loop uses.
|
2006-06-01 04:55:06 +08:00
|
|
|
void LCSSA::processInstruction(Instruction* Instr,
|
|
|
|
const std::vector<BasicBlock*>& exitBlocks)
|
|
|
|
{
|
|
|
|
++NumLCSSA; // We are applying the transformation
|
|
|
|
|
|
|
|
std::map<BasicBlock*, Instruction*> Phis;
|
2006-06-04 07:22:50 +08:00
|
|
|
|
|
|
|
// Add the base instruction to the Phis list. This makes tracking down
|
|
|
|
// the dominating values easier when we're filling in Phi nodes. This will
|
|
|
|
// be removed later, before we perform use replacement.
|
2006-06-01 04:55:06 +08:00
|
|
|
Phis[Instr->getParent()] = Instr;
|
|
|
|
|
|
|
|
// Phi nodes that need to be IDF-processed
|
|
|
|
std::vector<PHINode*> workList;
|
|
|
|
|
|
|
|
for (std::vector<BasicBlock*>::const_iterator BBI = exitBlocks.begin(),
|
2006-06-01 14:05:47 +08:00
|
|
|
BBE = exitBlocks.end(); BBI != BBE; ++BBI)
|
|
|
|
if (DT->getNode(Instr->getParent())->dominates(DT->getNode(*BBI))) {
|
2006-06-04 08:55:19 +08:00
|
|
|
PHINode *phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
|
|
|
|
(*BBI)->begin());
|
2006-06-01 14:05:47 +08:00
|
|
|
workList.push_back(phi);
|
|
|
|
Phis[*BBI] = phi;
|
|
|
|
}
|
2006-05-26 21:58:26 +08:00
|
|
|
|
2006-06-04 07:22:50 +08:00
|
|
|
// Phi nodes that need to have their incoming values filled.
|
|
|
|
std::vector<PHINode*> needIncomingValues;
|
|
|
|
|
2006-06-01 04:55:06 +08:00
|
|
|
// Calculate the IDF of these LCSSA Phi nodes, inserting new Phi's where
|
2006-06-04 08:02:23 +08:00
|
|
|
// necessary. Keep track of these new Phi's in the "Phis" map.
|
2006-06-01 04:55:06 +08:00
|
|
|
while (!workList.empty()) {
|
|
|
|
PHINode *CurPHI = workList.back();
|
|
|
|
workList.pop_back();
|
2006-05-28 02:47:11 +08:00
|
|
|
|
2006-06-04 07:22:50 +08:00
|
|
|
// Even though we've removed this Phi from the work list, we still need
|
|
|
|
// to fill in its incoming values.
|
|
|
|
needIncomingValues.push_back(CurPHI);
|
|
|
|
|
2006-06-01 04:55:06 +08:00
|
|
|
// Get the current Phi's DF, and insert Phi nodes. Add these new
|
|
|
|
// nodes to our worklist.
|
|
|
|
DominanceFrontier::const_iterator it = DF->find(CurPHI->getParent());
|
|
|
|
if (it != DF->end()) {
|
|
|
|
const DominanceFrontier::DomSetType &S = it->second;
|
|
|
|
for (DominanceFrontier::DomSetType::const_iterator P = S.begin(),
|
|
|
|
PE = S.end(); P != PE; ++P) {
|
2006-06-04 08:55:19 +08:00
|
|
|
if (DT->getNode(Instr->getParent())->dominates(DT->getNode(*P))) {
|
|
|
|
Instruction *&Phi = Phis[*P];
|
|
|
|
if (Phi == 0) {
|
|
|
|
// Still doesn't have operands...
|
|
|
|
Phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
|
|
|
|
(*P)->begin());
|
2006-05-28 02:47:11 +08:00
|
|
|
|
2006-06-04 08:55:19 +08:00
|
|
|
workList.push_back(cast<PHINode>(Phi));
|
|
|
|
}
|
2006-05-28 02:47:11 +08:00
|
|
|
}
|
|
|
|
}
|
2006-05-29 03:33:28 +08:00
|
|
|
}
|
2006-06-04 07:22:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in all Phis we've inserted that need their incoming values filled in.
|
|
|
|
for (std::vector<PHINode*>::iterator IVI = needIncomingValues.begin(),
|
|
|
|
IVE = needIncomingValues.end(); IVI != IVE; ++IVI) {
|
|
|
|
for (pred_iterator PI = pred_begin((*IVI)->getParent()),
|
|
|
|
E = pred_end((*IVI)->getParent()); PI != E; ++PI)
|
|
|
|
(*IVI)->addIncoming(getValueDominatingBlock(*PI, Phis),
|
2006-06-01 04:55:06 +08:00
|
|
|
*PI);
|
2006-05-29 03:33:28 +08:00
|
|
|
}
|
2006-05-29 09:00:00 +08:00
|
|
|
|
2006-06-01 04:55:06 +08:00
|
|
|
// Find all uses of the affected value, and replace them with the
|
|
|
|
// appropriate Phi.
|
|
|
|
std::vector<Instruction*> Uses;
|
|
|
|
for (Instruction::use_iterator UI = Instr->use_begin(), UE = Instr->use_end();
|
|
|
|
UI != UE; ++UI) {
|
|
|
|
Instruction* use = cast<Instruction>(*UI);
|
2006-06-04 08:55:19 +08:00
|
|
|
// Don't need to update uses within the loop body.
|
2006-06-06 12:28:30 +08:00
|
|
|
if (!inLoop(use->getParent()))
|
2006-06-01 04:55:06 +08:00
|
|
|
Uses.push_back(use);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<Instruction*>::iterator II = Uses.begin(), IE = Uses.end();
|
|
|
|
II != IE; ++II) {
|
2006-06-01 14:05:47 +08:00
|
|
|
if (PHINode* phi = dyn_cast<PHINode>(*II)) {
|
|
|
|
for (unsigned int i = 0; i < phi->getNumIncomingValues(); ++i) {
|
2006-06-04 08:02:23 +08:00
|
|
|
if (phi->getIncomingValue(i) == Instr) {
|
|
|
|
Instruction* dominator =
|
2006-06-01 14:05:47 +08:00
|
|
|
getValueDominatingBlock(phi->getIncomingBlock(i), Phis);
|
|
|
|
phi->setIncomingValue(i, dominator);
|
2006-06-04 08:02:23 +08:00
|
|
|
}
|
2006-06-01 14:05:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2006-06-04 08:02:23 +08:00
|
|
|
Value *NewVal = getValueDominatingBlock((*II)->getParent(), Phis);
|
|
|
|
(*II)->replaceUsesOfWith(Instr, NewVal);
|
2006-06-01 14:05:47 +08:00
|
|
|
}
|
2006-06-01 04:55:06 +08:00
|
|
|
}
|
2006-05-26 21:58:26 +08:00
|
|
|
}
|
|
|
|
|
2006-05-27 05:11:53 +08:00
|
|
|
/// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that
|
|
|
|
/// are used by instructions outside of it.
|
2006-06-04 08:02:23 +08:00
|
|
|
SetVector<Instruction*> LCSSA::getLoopValuesUsedOutsideLoop(Loop *L) {
|
2006-06-01 04:55:06 +08:00
|
|
|
|
|
|
|
// FIXME: For large loops, we may be able to avoid a lot of 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.
|
|
|
|
|
2006-06-04 08:02:23 +08:00
|
|
|
SetVector<Instruction*> AffectedValues;
|
2006-05-26 21:58:26 +08:00
|
|
|
for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
|
|
|
|
BB != E; ++BB) {
|
|
|
|
for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I)
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
|
|
|
|
++UI) {
|
|
|
|
BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
|
2006-06-06 12:36:36 +08:00
|
|
|
if (!inLoop(UserBB)) {
|
2006-05-26 21:58:26 +08:00
|
|
|
AffectedValues.insert(I);
|
2006-06-01 04:55:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-05-26 21:58:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return AffectedValues;
|
2006-05-27 05:11:53 +08:00
|
|
|
}
|
2006-06-01 04:55:06 +08:00
|
|
|
|
2006-06-09 04:02:53 +08:00
|
|
|
/// getValueDominatingBlock - Return the value within the potential dominators
|
|
|
|
/// map that dominates the given block.
|
2006-06-01 04:55:06 +08:00
|
|
|
Instruction *LCSSA::getValueDominatingBlock(BasicBlock *BB,
|
2006-06-04 08:02:23 +08:00
|
|
|
std::map<BasicBlock*, Instruction*>& PotDoms) {
|
2006-06-01 14:05:47 +08:00
|
|
|
DominatorTree::Node* bbNode = DT->getNode(BB);
|
|
|
|
while (bbNode != 0) {
|
|
|
|
std::map<BasicBlock*, Instruction*>::iterator I =
|
|
|
|
PotDoms.find(bbNode->getBlock());
|
|
|
|
if (I != PotDoms.end()) {
|
|
|
|
return (*I).second;
|
|
|
|
}
|
|
|
|
bbNode = bbNode->getIDom();
|
|
|
|
}
|
2006-06-01 04:55:06 +08:00
|
|
|
|
2006-06-01 14:05:47 +08:00
|
|
|
assert(0 && "No dominating value found.");
|
2006-06-01 04:55:06 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|