2007-04-07 01:25:15 +00:00
|
|
|
//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2007-04-07 01:25:15 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Loop Rotation Pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-04-09 16:11:48 +00:00
|
|
|
#define DEBUG_TYPE "loop-rotate"
|
2007-04-07 01:25:15 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
2009-03-06 03:51:30 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2007-04-07 01:25:15 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2007-07-11 23:47:28 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2007-04-07 01:25:15 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2007-07-11 23:47:28 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2009-10-24 23:19:52 +00:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2007-04-07 01:25:15 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define MAX_HEADER_SIZE 16
|
|
|
|
|
|
|
|
STATISTIC(NumRotated, "Number of loops rotated");
|
|
|
|
namespace {
|
|
|
|
|
2009-09-02 06:11:42 +00:00
|
|
|
class LoopRotate : public LoopPass {
|
2007-04-07 01:25:15 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
LoopRotate() : LoopPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2007-04-09 16:11:48 +00:00
|
|
|
// Rotate Loop L as many times as possible. Return true if
|
|
|
|
// loop is rotated at least once.
|
2007-04-07 01:25:15 +00:00
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM);
|
2007-04-09 16:11:48 +00:00
|
|
|
|
|
|
|
// LCSSA form makes instruction renaming easier.
|
2007-04-07 01:25:15 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2010-07-16 17:58:45 +00:00
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
AU.addPreserved<DominanceFrontier>();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addPreserved<LoopInfo>();
|
2008-02-15 01:24:49 +00:00
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2007-04-07 01:25:15 +00:00
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addPreservedID(LCSSAID);
|
2007-07-11 23:47:28 +00:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2007-04-07 01:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
/// Do actual work
|
|
|
|
bool rotateLoop(Loop *L, LPPassManager &LPM);
|
|
|
|
|
|
|
|
/// Initialize local data
|
|
|
|
void initialize();
|
|
|
|
|
2007-04-09 20:19:46 +00:00
|
|
|
/// After loop rotation, loop pre-header has multiple sucessors.
|
|
|
|
/// Insert one forwarding basic block to ensure that loop pre-header
|
|
|
|
/// has only one successor.
|
|
|
|
void preserveCanonicalLoopForm(LPPassManager &LPM);
|
|
|
|
|
2007-04-07 01:25:15 +00:00
|
|
|
private:
|
|
|
|
Loop *L;
|
|
|
|
BasicBlock *OrigHeader;
|
|
|
|
BasicBlock *OrigPreHeader;
|
|
|
|
BasicBlock *OrigLatch;
|
|
|
|
BasicBlock *NewHeader;
|
|
|
|
BasicBlock *Exit;
|
2007-07-11 23:47:28 +00:00
|
|
|
LPPassManager *LPM_Ptr;
|
2007-04-07 01:25:15 +00:00
|
|
|
};
|
|
|
|
}
|
2008-05-13 00:00:25 +00:00
|
|
|
|
|
|
|
char LoopRotate::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(LoopRotate, "loop-rotate", "Rotate Loops", false, false);
|
2007-04-07 01:25:15 +00:00
|
|
|
|
2008-10-22 23:32:42 +00:00
|
|
|
Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
|
2007-04-07 01:25:15 +00:00
|
|
|
|
2007-04-09 16:11:48 +00:00
|
|
|
/// Rotate Loop L as many times as possible. Return true if
|
2009-06-25 00:22:44 +00:00
|
|
|
/// the loop is rotated at least once.
|
2007-04-07 01:25:15 +00:00
|
|
|
bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
|
2007-07-11 23:47:28 +00:00
|
|
|
|
2007-04-07 01:25:15 +00:00
|
|
|
bool RotatedOneLoop = false;
|
|
|
|
initialize();
|
2007-07-11 23:47:28 +00:00
|
|
|
LPM_Ptr = &LPM;
|
2007-04-07 01:25:15 +00:00
|
|
|
|
|
|
|
// One loop can be rotated multiple times.
|
|
|
|
while (rotateLoop(Lp,LPM)) {
|
|
|
|
RotatedOneLoop = true;
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return RotatedOneLoop;
|
|
|
|
}
|
|
|
|
|
2007-05-11 21:10:54 +00:00
|
|
|
/// Rotate loop LP. Return true if the loop is rotated.
|
2007-04-07 01:25:15 +00:00
|
|
|
bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
|
|
|
|
L = Lp;
|
|
|
|
|
|
|
|
OrigPreHeader = L->getLoopPreheader();
|
2009-11-05 21:11:53 +00:00
|
|
|
if (!OrigPreHeader) return false;
|
|
|
|
|
2007-04-07 01:25:15 +00:00
|
|
|
OrigLatch = L->getLoopLatch();
|
2009-11-05 21:11:53 +00:00
|
|
|
if (!OrigLatch) return false;
|
|
|
|
|
|
|
|
OrigHeader = L->getHeader();
|
2007-04-07 01:25:15 +00:00
|
|
|
|
2009-06-25 00:22:44 +00:00
|
|
|
// If the loop has only one block then there is not much to rotate.
|
2007-04-09 16:11:48 +00:00
|
|
|
if (L->getBlocks().size() == 1)
|
2007-04-07 01:25:15 +00:00
|
|
|
return false;
|
|
|
|
|
2009-06-25 00:22:44 +00:00
|
|
|
// If the loop header is not one of the loop exiting blocks then
|
|
|
|
// either this loop is already rotated or it is not
|
2007-04-07 01:25:15 +00:00
|
|
|
// suitable for loop rotation transformations.
|
2009-10-24 23:34:26 +00:00
|
|
|
if (!L->isLoopExiting(OrigHeader))
|
2007-04-07 01:25:15 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
|
|
|
|
if (!BI)
|
|
|
|
return false;
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(BI->isConditional() && "Branch Instruction is not conditional");
|
2007-04-07 01:25:15 +00:00
|
|
|
|
2007-04-09 16:11:48 +00:00
|
|
|
// Updating PHInodes in loops with multiple exits adds complexity.
|
|
|
|
// Keep it simple, and restrict loop rotation to loops with one exit only.
|
|
|
|
// In future, lift this restriction and support for multiple exits if
|
|
|
|
// required.
|
2007-08-21 00:31:24 +00:00
|
|
|
SmallVector<BasicBlock*, 8> ExitBlocks;
|
2007-04-07 01:25:15 +00:00
|
|
|
L->getExitBlocks(ExitBlocks);
|
|
|
|
if (ExitBlocks.size() > 1)
|
|
|
|
return false;
|
|
|
|
|
2007-07-11 23:47:28 +00:00
|
|
|
// Check size of original header and reject
|
|
|
|
// loop if it is very big.
|
2009-03-06 03:51:30 +00:00
|
|
|
unsigned Size = 0;
|
|
|
|
|
|
|
|
// FIXME: Use common api to estimate size.
|
|
|
|
for (BasicBlock::const_iterator OI = OrigHeader->begin(),
|
|
|
|
OE = OrigHeader->end(); OI != OE; ++OI) {
|
|
|
|
if (isa<PHINode>(OI))
|
|
|
|
continue; // PHI nodes don't count.
|
|
|
|
if (isa<DbgInfoIntrinsic>(OI))
|
|
|
|
continue; // Debug intrinsics don't count as size.
|
2010-06-22 15:08:57 +00:00
|
|
|
++Size;
|
2009-03-06 03:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Size > MAX_HEADER_SIZE)
|
2007-07-11 23:47:28 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now, this loop is suitable for rotation.
|
|
|
|
|
2009-09-27 15:37:03 +00:00
|
|
|
// Anything ScalarEvolution may know about this loop or the PHI nodes
|
|
|
|
// in its header will soon be invalidated.
|
|
|
|
if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
|
2009-10-31 15:04:55 +00:00
|
|
|
SE->forgetLoop(L);
|
2009-09-27 15:37:03 +00:00
|
|
|
|
2007-04-07 01:25:15 +00:00
|
|
|
// Find new Loop header. NewHeader is a Header's one and only successor
|
2009-01-26 01:57:01 +00:00
|
|
|
// that is inside loop. Header's other successor is outside the
|
|
|
|
// loop. Otherwise loop is not suitable for rotation.
|
2007-04-09 16:11:48 +00:00
|
|
|
Exit = BI->getSuccessor(0);
|
|
|
|
NewHeader = BI->getSuccessor(1);
|
|
|
|
if (L->contains(Exit))
|
|
|
|
std::swap(Exit, NewHeader);
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(NewHeader && "Unable to determine new loop header");
|
2007-04-09 16:11:48 +00:00
|
|
|
assert(L->contains(NewHeader) && !L->contains(Exit) &&
|
|
|
|
"Unable to determine loop header and exit blocks");
|
2009-01-26 02:11:30 +00:00
|
|
|
|
2009-06-25 00:22:44 +00:00
|
|
|
// This code assumes that the new header has exactly one predecessor.
|
|
|
|
// Remove any single-entry PHI nodes in it.
|
2009-01-26 02:11:30 +00:00
|
|
|
assert(NewHeader->getSinglePredecessor() &&
|
|
|
|
"New header doesn't have one pred!");
|
|
|
|
FoldSingleEntryPHINodes(NewHeader);
|
2007-04-07 01:25:15 +00:00
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// Begin by walking OrigHeader and populating ValueMap with an entry for
|
|
|
|
// each Instruction.
|
2007-04-09 16:11:48 +00:00
|
|
|
BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
|
2009-10-24 23:19:52 +00:00
|
|
|
DenseMap<const Value *, Value *> ValueMap;
|
2007-04-09 16:11:48 +00:00
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// For PHI nodes, the value available in OldPreHeader is just the
|
|
|
|
// incoming value from OldPreHeader.
|
|
|
|
for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
|
|
|
|
ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
|
2007-04-09 19:04:21 +00:00
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// For the rest of the instructions, create a clone in the OldPreHeader.
|
|
|
|
TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
Instruction *C = I->clone();
|
|
|
|
C->setName(I->getName());
|
|
|
|
C->insertBefore(LoopEntryBranch);
|
|
|
|
ValueMap[I] = C;
|
2007-04-07 01:25:15 +00:00
|
|
|
}
|
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// Along with all the other instructions, we just cloned OrigHeader's
|
|
|
|
// terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
|
|
|
|
// successors by duplicating their incoming values for OrigHeader.
|
|
|
|
TerminatorInst *TI = OrigHeader->getTerminator();
|
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
|
|
|
for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
|
|
|
|
PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
|
|
|
|
|
|
|
|
// Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
|
|
|
|
// OrigPreHeader's old terminator (the original branch into the loop), and
|
|
|
|
// remove the corresponding incoming values from the PHI nodes in OrigHeader.
|
|
|
|
LoopEntryBranch->eraseFromParent();
|
|
|
|
for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
|
|
|
|
PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
|
|
|
|
|
2009-10-26 15:55:24 +00:00
|
|
|
// Now fix up users of the instructions in OrigHeader, inserting PHI nodes
|
2009-10-24 23:19:52 +00:00
|
|
|
// as necessary.
|
|
|
|
SSAUpdater SSA;
|
|
|
|
for (I = OrigHeader->begin(); I != E; ++I) {
|
|
|
|
Value *OrigHeaderVal = I;
|
|
|
|
Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
|
|
|
|
|
|
|
|
// The value now exits in two versions: the initial value in the preheader
|
|
|
|
// and the loop "next" value in the original header.
|
|
|
|
SSA.Initialize(OrigHeaderVal);
|
|
|
|
SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
|
|
|
|
SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
|
|
|
|
|
|
|
|
// Visit each use of the OrigHeader instruction.
|
|
|
|
for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
|
|
|
|
UE = OrigHeaderVal->use_end(); UI != UE; ) {
|
|
|
|
// Grab the use before incrementing the iterator.
|
|
|
|
Use &U = UI.getUse();
|
|
|
|
|
|
|
|
// Increment the iterator before removing the use from the list.
|
|
|
|
++UI;
|
|
|
|
|
|
|
|
// SSAUpdater can't handle a non-PHI use in the same block as an
|
|
|
|
// earlier def. We can easily handle those cases manually.
|
|
|
|
Instruction *UserInst = cast<Instruction>(U.getUser());
|
|
|
|
if (!isa<PHINode>(UserInst)) {
|
|
|
|
BasicBlock *UserBB = UserInst->getParent();
|
|
|
|
|
|
|
|
// The original users in the OrigHeader are already using the
|
|
|
|
// original definitions.
|
|
|
|
if (UserBB == OrigHeader)
|
2007-04-09 16:21:29 +00:00
|
|
|
continue;
|
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// Users in the OrigPreHeader need to use the value to which the
|
|
|
|
// original definitions are mapped.
|
|
|
|
if (UserBB == OrigPreHeader) {
|
|
|
|
U = OrigPreHeaderVal;
|
2007-04-07 01:25:15 +00:00
|
|
|
continue;
|
2009-10-24 23:19:52 +00:00
|
|
|
}
|
2007-04-07 01:25:15 +00:00
|
|
|
}
|
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// Anything else can be handled by SSAUpdater.
|
|
|
|
SSA.RewriteUse(U);
|
2007-04-07 01:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-24 23:19:52 +00:00
|
|
|
// NewHeader is now the header of the loop.
|
2007-04-07 01:25:15 +00:00
|
|
|
L->moveToHeader(NewHeader);
|
|
|
|
|
2007-04-09 20:19:46 +00:00
|
|
|
preserveCanonicalLoopForm(LPM);
|
|
|
|
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumRotated;
|
2007-04-07 01:25:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize local data
|
|
|
|
void LoopRotate::initialize() {
|
|
|
|
L = NULL;
|
|
|
|
OrigHeader = NULL;
|
|
|
|
OrigPreHeader = NULL;
|
|
|
|
NewHeader = NULL;
|
|
|
|
Exit = NULL;
|
|
|
|
}
|
2007-04-09 20:19:46 +00:00
|
|
|
|
|
|
|
/// After loop rotation, loop pre-header has multiple sucessors.
|
|
|
|
/// Insert one forwarding basic block to ensure that loop pre-header
|
|
|
|
/// has only one successor.
|
|
|
|
void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
|
|
|
|
|
|
|
|
// Right now original pre-header has two successors, new header and
|
|
|
|
// exit block. Insert new block between original pre-header and
|
|
|
|
// new header such that loop's new pre-header has only one successor.
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
|
|
|
|
"bb.nph",
|
2008-05-15 10:04:30 +00:00
|
|
|
OrigHeader->getParent(),
|
2008-04-06 20:25:17 +00:00
|
|
|
NewHeader);
|
2009-11-05 19:43:25 +00:00
|
|
|
LoopInfo &LI = getAnalysis<LoopInfo>();
|
2007-04-09 20:19:46 +00:00
|
|
|
if (Loop *PL = LI.getLoopFor(OrigPreHeader))
|
2007-11-27 03:43:35 +00:00
|
|
|
PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(NewHeader, NewPreHeader);
|
2007-04-09 20:19:46 +00:00
|
|
|
|
|
|
|
BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
|
|
|
|
if (OrigPH_BI->getSuccessor(0) == NewHeader)
|
|
|
|
OrigPH_BI->setSuccessor(0, NewPreHeader);
|
|
|
|
else {
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
|
|
|
|
"Unexpected original pre-header terminator");
|
2007-04-09 20:19:46 +00:00
|
|
|
OrigPH_BI->setSuccessor(1, NewPreHeader);
|
|
|
|
}
|
|
|
|
|
2009-06-27 21:23:40 +00:00
|
|
|
PHINode *PN;
|
|
|
|
for (BasicBlock::iterator I = NewHeader->begin();
|
|
|
|
(PN = dyn_cast<PHINode>(I)); ++I) {
|
2007-04-09 20:19:46 +00:00
|
|
|
int index = PN->getBasicBlockIndex(OrigPreHeader);
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(index != -1 && "Expected incoming value from Original PreHeader");
|
2007-04-09 20:19:46 +00:00
|
|
|
PN->setIncomingBlock(index, NewPreHeader);
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
|
|
|
|
"Expected only one incoming value from Original PreHeader");
|
2007-04-09 20:19:46 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
|
2007-07-11 23:47:28 +00:00
|
|
|
DT->addNewBlock(NewPreHeader, OrigPreHeader);
|
|
|
|
DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
|
|
|
|
DT->changeImmediateDominator(Exit, OrigPreHeader);
|
|
|
|
for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
BasicBlock *B = *BI;
|
|
|
|
if (L->getHeader() != B) {
|
|
|
|
DomTreeNode *Node = DT->getNode(B);
|
|
|
|
if (Node && Node->getBlock() == OrigHeader)
|
|
|
|
DT->changeImmediateDominator(*BI, L->getHeader());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DT->changeImmediateDominator(OrigHeader, OrigLatch);
|
|
|
|
}
|
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
|
2007-07-11 23:47:28 +00:00
|
|
|
// New Preheader's dominance frontier is Exit block.
|
|
|
|
DominanceFrontier::DomSetType NewPHSet;
|
|
|
|
NewPHSet.insert(Exit);
|
|
|
|
DF->addBasicBlock(NewPreHeader, NewPHSet);
|
|
|
|
|
|
|
|
// New Header's dominance frontier now includes itself and Exit block
|
|
|
|
DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
|
|
|
|
if (HeadI != DF->end()) {
|
|
|
|
DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
|
|
|
|
HeaderSet.clear();
|
|
|
|
HeaderSet.insert(L->getHeader());
|
|
|
|
HeaderSet.insert(Exit);
|
|
|
|
} else {
|
|
|
|
DominanceFrontier::DomSetType HeaderSet;
|
|
|
|
HeaderSet.insert(L->getHeader());
|
|
|
|
HeaderSet.insert(Exit);
|
|
|
|
DF->addBasicBlock(L->getHeader(), HeaderSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Original header (new Loop Latch)'s dominance frontier is Exit.
|
|
|
|
DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
|
|
|
|
if (LatchI != DF->end()) {
|
|
|
|
DominanceFrontier::DomSetType &LatchSet = LatchI->second;
|
|
|
|
LatchSet = LatchI->second;
|
|
|
|
LatchSet.clear();
|
|
|
|
LatchSet.insert(Exit);
|
|
|
|
} else {
|
|
|
|
DominanceFrontier::DomSetType LatchSet;
|
|
|
|
LatchSet.insert(Exit);
|
|
|
|
DF->addBasicBlock(L->getHeader(), LatchSet);
|
|
|
|
}
|
|
|
|
|
2009-08-07 17:16:44 +00:00
|
|
|
// If a loop block dominates new loop latch then add to its frontiers
|
|
|
|
// new header and Exit and remove new latch (which is equal to original
|
|
|
|
// header).
|
2007-07-11 23:47:28 +00:00
|
|
|
BasicBlock *NewLatch = L->getLoopLatch();
|
2009-08-07 17:16:44 +00:00
|
|
|
|
|
|
|
assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
|
|
|
|
|
|
|
|
if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
|
|
|
|
for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
BasicBlock *B = *BI;
|
|
|
|
if (DT->dominates(B, NewLatch)) {
|
|
|
|
DominanceFrontier::iterator BDFI = DF->find(B);
|
|
|
|
if (BDFI != DF->end()) {
|
|
|
|
DominanceFrontier::DomSetType &BSet = BDFI->second;
|
|
|
|
BSet.erase(NewLatch);
|
|
|
|
BSet.insert(L->getHeader());
|
|
|
|
BSet.insert(Exit);
|
|
|
|
} else {
|
|
|
|
DominanceFrontier::DomSetType BSet;
|
|
|
|
BSet.insert(L->getHeader());
|
|
|
|
BSet.insert(Exit);
|
|
|
|
DF->addBasicBlock(B, BSet);
|
|
|
|
}
|
2007-07-11 23:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve canonical loop form, which means Exit block should
|
|
|
|
// have only one predecessor.
|
2009-09-09 18:18:18 +00:00
|
|
|
SplitEdge(L->getLoopLatch(), Exit, this);
|
2007-07-11 23:47:28 +00:00
|
|
|
|
2009-01-26 01:38:24 +00:00
|
|
|
assert(NewHeader && L->getHeader() == NewHeader &&
|
|
|
|
"Invalid loop header after loop rotation");
|
|
|
|
assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
|
|
|
|
"Invalid loop preheader after loop rotation");
|
|
|
|
assert(L->getLoopLatch() &&
|
|
|
|
"Invalid loop latch after loop rotation");
|
2007-04-09 20:19:46 +00:00
|
|
|
}
|