2007-04-07 09:25:15 +08:00
|
|
|
//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-07 09:25:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Loop Rotation Pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-04-10 00:11:48 +08:00
|
|
|
#define DEBUG_TYPE "loop-rotate"
|
2007-04-07 09:25:15 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
2011-01-02 15:35:53 +08:00
|
|
|
#include "llvm/Analysis/CodeMetrics.h"
|
2011-01-08 16:24:46 +08:00
|
|
|
#include "llvm/Analysis/DominanceFrontier.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2007-07-12 07:47:28 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2007-04-07 09:25:15 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2007-07-12 07:47:28 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2009-10-25 07:19:52 +08:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2011-01-08 15:21:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2007-04-07 09:25:15 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define MAX_HEADER_SIZE 16
|
|
|
|
|
|
|
|
STATISTIC(NumRotated, "Number of loops rotated");
|
|
|
|
namespace {
|
|
|
|
|
2009-09-02 14:11:42 +08:00
|
|
|
class LoopRotate : public LoopPass {
|
2007-04-07 09:25:15 +08:00
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
LoopRotate() : LoopPass(ID) {
|
|
|
|
initializeLoopRotatePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2007-04-10 00:11:48 +08:00
|
|
|
// Rotate Loop L as many times as possible. Return true if
|
|
|
|
// loop is rotated at least once.
|
2007-04-07 09:25:15 +08:00
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM);
|
2007-04-10 00:11:48 +08:00
|
|
|
|
|
|
|
// LCSSA form makes instruction renaming easier.
|
2007-04-07 09:25:15 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2010-07-17 01:58:45 +08:00
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
AU.addPreserved<DominanceFrontier>();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addPreserved<LoopInfo>();
|
2008-02-15 09:24:49 +08:00
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2007-04-07 09:25:15 +08:00
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addPreservedID(LCSSAID);
|
2007-07-12 07:47:28 +08:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2007-04-07 09:25:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
/// Do actual work
|
2011-01-09 01:48:33 +08:00
|
|
|
bool rotateLoop(Loop *L);
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2007-04-10 04:19:46 +08: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.
|
2011-01-09 01:48:33 +08:00
|
|
|
void preserveCanonicalLoopForm(Loop *L, BasicBlock *OrigHeader,
|
|
|
|
BasicBlock *OrigPreHeader,
|
|
|
|
BasicBlock *OrigLatch, BasicBlock *NewHeader,
|
|
|
|
BasicBlock *Exit);
|
2007-04-10 04:19:46 +08:00
|
|
|
|
2007-04-07 09:25:15 +08:00
|
|
|
private:
|
2011-01-09 01:38:45 +08:00
|
|
|
LoopInfo *LI;
|
2007-04-07 09:25:15 +08:00
|
|
|
};
|
|
|
|
}
|
2008-05-13 08:00:25 +08:00
|
|
|
|
|
|
|
char LoopRotate::ID = 0;
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LCSSA)
|
|
|
|
INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2008-10-23 07:32:42 +08:00
|
|
|
Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2007-04-10 00:11:48 +08:00
|
|
|
/// Rotate Loop L as many times as possible. Return true if
|
2009-06-25 08:22:44 +08:00
|
|
|
/// the loop is rotated at least once.
|
2011-01-09 01:48:33 +08:00
|
|
|
bool LoopRotate::runOnLoop(Loop *L, LPPassManager &LPM) {
|
2011-01-09 01:38:45 +08:00
|
|
|
LI = &getAnalysis<LoopInfo>();
|
2007-07-12 07:47:28 +08:00
|
|
|
|
2007-04-07 09:25:15 +08:00
|
|
|
// One loop can be rotated multiple times.
|
2011-01-09 01:38:45 +08:00
|
|
|
bool MadeChange = false;
|
2011-01-09 01:48:33 +08:00
|
|
|
while (rotateLoop(L))
|
2011-01-09 01:38:45 +08:00
|
|
|
MadeChange = true;
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2011-01-09 01:38:45 +08:00
|
|
|
return MadeChange;
|
2007-04-07 09:25:15 +08:00
|
|
|
}
|
|
|
|
|
2007-05-12 05:10:54 +08:00
|
|
|
/// Rotate loop LP. Return true if the loop is rotated.
|
2011-01-09 01:48:33 +08:00
|
|
|
bool LoopRotate::rotateLoop(Loop *L) {
|
|
|
|
BasicBlock *OrigPreHeader = L->getLoopPreheader();
|
2009-11-06 05:11:53 +08:00
|
|
|
if (!OrigPreHeader) return false;
|
|
|
|
|
2011-01-09 01:48:33 +08:00
|
|
|
BasicBlock *OrigLatch = L->getLoopLatch();
|
2009-11-06 05:11:53 +08:00
|
|
|
if (!OrigLatch) return false;
|
|
|
|
|
2011-01-09 01:48:33 +08:00
|
|
|
BasicBlock *OrigHeader = L->getHeader();
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2009-06-25 08:22:44 +08:00
|
|
|
// If the loop has only one block then there is not much to rotate.
|
2007-04-10 00:11:48 +08:00
|
|
|
if (L->getBlocks().size() == 1)
|
2007-04-07 09:25:15 +08:00
|
|
|
return false;
|
|
|
|
|
2009-06-25 08:22:44 +08: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 09:25:15 +08:00
|
|
|
// suitable for loop rotation transformations.
|
2009-10-25 07:34:26 +08:00
|
|
|
if (!L->isLoopExiting(OrigHeader))
|
2007-04-07 09:25:15 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
|
|
|
|
if (!BI)
|
|
|
|
return false;
|
2009-01-26 09:38:24 +08:00
|
|
|
assert(BI->isConditional() && "Branch Instruction is not conditional");
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2007-04-10 00:11:48 +08: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 08:31:24 +08:00
|
|
|
SmallVector<BasicBlock*, 8> ExitBlocks;
|
2007-04-07 09:25:15 +08:00
|
|
|
L->getExitBlocks(ExitBlocks);
|
|
|
|
if (ExitBlocks.size() > 1)
|
|
|
|
return false;
|
|
|
|
|
2011-01-02 15:35:53 +08:00
|
|
|
// Check size of original header and reject loop if it is very big.
|
|
|
|
{
|
|
|
|
CodeMetrics Metrics;
|
|
|
|
Metrics.analyzeBasicBlock(OrigHeader);
|
|
|
|
if (Metrics.NumInsts > MAX_HEADER_SIZE)
|
|
|
|
return false;
|
2009-03-06 11:51:30 +08:00
|
|
|
}
|
|
|
|
|
2007-07-12 07:47:28 +08:00
|
|
|
// Now, this loop is suitable for rotation.
|
|
|
|
|
2009-09-27 23:37:03 +08: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 23:04:55 +08:00
|
|
|
SE->forgetLoop(L);
|
2009-09-27 23:37:03 +08:00
|
|
|
|
2007-04-07 09:25:15 +08:00
|
|
|
// Find new Loop header. NewHeader is a Header's one and only successor
|
2009-01-26 09:57:01 +08:00
|
|
|
// that is inside loop. Header's other successor is outside the
|
|
|
|
// loop. Otherwise loop is not suitable for rotation.
|
2011-01-09 01:48:33 +08:00
|
|
|
BasicBlock *Exit = BI->getSuccessor(0);
|
|
|
|
BasicBlock *NewHeader = BI->getSuccessor(1);
|
2007-04-10 00:11:48 +08:00
|
|
|
if (L->contains(Exit))
|
|
|
|
std::swap(Exit, NewHeader);
|
2009-01-26 09:38:24 +08:00
|
|
|
assert(NewHeader && "Unable to determine new loop header");
|
2007-04-10 00:11:48 +08:00
|
|
|
assert(L->contains(NewHeader) && !L->contains(Exit) &&
|
|
|
|
"Unable to determine loop header and exit blocks");
|
2009-01-26 10:11:30 +08:00
|
|
|
|
2009-06-25 08:22:44 +08:00
|
|
|
// This code assumes that the new header has exactly one predecessor.
|
|
|
|
// Remove any single-entry PHI nodes in it.
|
2009-01-26 10:11:30 +08:00
|
|
|
assert(NewHeader->getSinglePredecessor() &&
|
|
|
|
"New header doesn't have one pred!");
|
|
|
|
FoldSingleEntryPHINodes(NewHeader);
|
2007-04-07 09:25:15 +08:00
|
|
|
|
2009-10-25 07:19:52 +08:00
|
|
|
// Begin by walking OrigHeader and populating ValueMap with an entry for
|
|
|
|
// each Instruction.
|
2007-04-10 00:11:48 +08:00
|
|
|
BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
|
2011-01-08 15:21:31 +08:00
|
|
|
ValueToValueMapTy ValueMap;
|
2007-04-10 00:11:48 +08:00
|
|
|
|
2009-10-25 07:19:52 +08: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-10 03:04:21 +08:00
|
|
|
|
2010-09-06 09:10:22 +08:00
|
|
|
// For the rest of the instructions, either hoist to the OrigPreheader if
|
|
|
|
// possible or create a clone in the OldPreHeader if not.
|
2009-10-25 07:19:52 +08:00
|
|
|
TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
|
2010-09-06 09:10:22 +08:00
|
|
|
while (I != E) {
|
|
|
|
Instruction *Inst = I++;
|
|
|
|
|
|
|
|
// If the instruction's operands are invariant and it doesn't read or write
|
|
|
|
// memory, then it is safe to hoist. Doing this doesn't change the order of
|
|
|
|
// execution in the preheader, but does prevent the instruction from
|
|
|
|
// executing in each iteration of the loop. This means it is safe to hoist
|
|
|
|
// something that might trap, but isn't safe to hoist something that reads
|
|
|
|
// memory (without proving that the loop doesn't write).
|
|
|
|
if (L->hasLoopInvariantOperands(Inst) &&
|
|
|
|
!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() &&
|
|
|
|
!isa<TerminatorInst>(Inst)) {
|
|
|
|
Inst->moveBefore(LoopEntryBranch);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create a duplicate of the instruction.
|
|
|
|
Instruction *C = Inst->clone();
|
2011-01-08 16:15:20 +08:00
|
|
|
|
2011-01-08 16:24:46 +08:00
|
|
|
// Eagerly remap the operands of the instruction.
|
|
|
|
RemapInstruction(C, ValueMap,
|
|
|
|
RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
|
|
|
|
|
|
|
|
// With the operands remapped, see if the instruction constant folds or is
|
|
|
|
// otherwise simplifyable. This commonly occurs because the entry from PHI
|
|
|
|
// nodes allows icmps and other instructions to fold.
|
2011-01-09 01:38:45 +08:00
|
|
|
Value *V = SimplifyInstruction(C);
|
|
|
|
if (V && LI->replacementPreservesLCSSAForm(C, V)) {
|
2011-01-08 16:24:46 +08:00
|
|
|
// If so, then delete the temporary instruction and stick the folded value
|
|
|
|
// in the map.
|
|
|
|
delete C;
|
|
|
|
ValueMap[Inst] = V;
|
|
|
|
} else {
|
|
|
|
// Otherwise, stick the new instruction into the new block!
|
|
|
|
C->setName(Inst->getName());
|
|
|
|
C->insertBefore(LoopEntryBranch);
|
|
|
|
ValueMap[Inst] = C;
|
|
|
|
}
|
2007-04-07 09:25:15 +08:00
|
|
|
}
|
|
|
|
|
2009-10-25 07:19:52 +08: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 23:55:24 +08:00
|
|
|
// Now fix up users of the instructions in OrigHeader, inserting PHI nodes
|
2009-10-25 07:19:52 +08:00
|
|
|
// as necessary.
|
|
|
|
SSAUpdater SSA;
|
|
|
|
for (I = OrigHeader->begin(); I != E; ++I) {
|
|
|
|
Value *OrigHeaderVal = I;
|
|
|
|
Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
|
|
|
|
|
2011-01-08 15:21:31 +08:00
|
|
|
// If there are no uses of the value (e.g. because it returns void), there
|
|
|
|
// is nothing to rewrite.
|
|
|
|
if (OrigHeaderVal->use_empty() && OrigPreHeaderVal->use_empty())
|
|
|
|
continue;
|
|
|
|
|
2009-10-25 07:19:52 +08:00
|
|
|
// The value now exits in two versions: the initial value in the preheader
|
|
|
|
// and the loop "next" value in the original header.
|
2010-09-02 16:14:03 +08:00
|
|
|
SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
|
2009-10-25 07:19:52 +08:00
|
|
|
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-10 00:21:29 +08:00
|
|
|
continue;
|
|
|
|
|
2009-10-25 07:19:52 +08: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 09:25:15 +08:00
|
|
|
continue;
|
2009-10-25 07:19:52 +08:00
|
|
|
}
|
2007-04-07 09:25:15 +08:00
|
|
|
}
|
|
|
|
|
2009-10-25 07:19:52 +08:00
|
|
|
// Anything else can be handled by SSAUpdater.
|
|
|
|
SSA.RewriteUse(U);
|
2007-04-07 09:25:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-25 07:19:52 +08:00
|
|
|
// NewHeader is now the header of the loop.
|
2007-04-07 09:25:15 +08:00
|
|
|
L->moveToHeader(NewHeader);
|
|
|
|
|
2010-08-18 01:39:21 +08:00
|
|
|
// Move the original header to the bottom of the loop, where it now more
|
|
|
|
// naturally belongs. This isn't necessary for correctness, and CodeGen can
|
|
|
|
// usually reorder blocks on its own to fix things like this up, but it's
|
|
|
|
// still nice to keep the IR readable.
|
|
|
|
//
|
|
|
|
// The original header should have only one predecessor at this point, since
|
|
|
|
// we checked that the loop had a proper preheader and unique backedge before
|
|
|
|
// we started.
|
|
|
|
assert(OrigHeader->getSinglePredecessor() &&
|
|
|
|
"Original loop header has too many predecessors after loop rotation!");
|
|
|
|
OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
|
|
|
|
|
|
|
|
// Also, since this original header only has one predecessor, zap its
|
|
|
|
// PHI nodes, which are now trivial.
|
|
|
|
FoldSingleEntryPHINodes(OrigHeader);
|
|
|
|
|
|
|
|
// TODO: We could just go ahead and merge OrigHeader into its predecessor
|
|
|
|
// at this point, if we don't mind updating dominator info.
|
|
|
|
|
|
|
|
// Establish a new preheader, update dominators, etc.
|
2011-01-09 01:48:33 +08:00
|
|
|
preserveCanonicalLoopForm(L, OrigHeader, OrigPreHeader, OrigLatch,
|
|
|
|
NewHeader, Exit);
|
2007-04-10 04:19:46 +08:00
|
|
|
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumRotated;
|
2007-04-07 09:25:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-10 04:19:46 +08: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.
|
2011-01-09 01:48:33 +08:00
|
|
|
void LoopRotate::preserveCanonicalLoopForm(Loop *L, BasicBlock *OrigHeader,
|
|
|
|
BasicBlock *OrigPreHeader,
|
|
|
|
BasicBlock *OrigLatch,
|
|
|
|
BasicBlock *NewHeader,
|
|
|
|
BasicBlock *Exit) {
|
2007-04-10 04:19:46 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-01-09 01:48:33 +08:00
|
|
|
BasicBlock *NewPreHeader =
|
|
|
|
BasicBlock::Create(OrigHeader->getContext(), "bb.nph",
|
|
|
|
OrigHeader->getParent(), NewHeader);
|
2011-01-09 01:49:51 +08:00
|
|
|
if (Loop *PL = LI->getLoopFor(OrigPreHeader))
|
|
|
|
PL->addBasicBlockToLoop(NewPreHeader, LI->getBase());
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(NewHeader, NewPreHeader);
|
2007-04-10 04:19:46 +08:00
|
|
|
|
|
|
|
BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
|
|
|
|
if (OrigPH_BI->getSuccessor(0) == NewHeader)
|
|
|
|
OrigPH_BI->setSuccessor(0, NewPreHeader);
|
|
|
|
else {
|
2009-01-26 09:38:24 +08:00
|
|
|
assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
|
|
|
|
"Unexpected original pre-header terminator");
|
2007-04-10 04:19:46 +08:00
|
|
|
OrigPH_BI->setSuccessor(1, NewPreHeader);
|
|
|
|
}
|
|
|
|
|
2009-06-28 05:23:40 +08:00
|
|
|
PHINode *PN;
|
|
|
|
for (BasicBlock::iterator I = NewHeader->begin();
|
|
|
|
(PN = dyn_cast<PHINode>(I)); ++I) {
|
2007-04-10 04:19:46 +08:00
|
|
|
int index = PN->getBasicBlockIndex(OrigPreHeader);
|
2009-01-26 09:38:24 +08:00
|
|
|
assert(index != -1 && "Expected incoming value from Original PreHeader");
|
2007-04-10 04:19:46 +08:00
|
|
|
PN->setIncomingBlock(index, NewPreHeader);
|
2009-01-26 09:38:24 +08:00
|
|
|
assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
|
|
|
|
"Expected only one incoming value from Original PreHeader");
|
2007-04-10 04:19:46 +08:00
|
|
|
}
|
|
|
|
|
2009-01-28 21:14:17 +08:00
|
|
|
if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
|
2007-07-12 07:47:28 +08: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 21:14:17 +08:00
|
|
|
if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
|
2007-07-12 07:47:28 +08: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-08 01:16:44 +08: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-12 07:47:28 +08:00
|
|
|
BasicBlock *NewLatch = L->getLoopLatch();
|
2009-08-08 01:16:44 +08: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;
|
2011-01-09 01:48:33 +08:00
|
|
|
if (!DT->dominates(B, NewLatch)) continue;
|
|
|
|
|
|
|
|
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-12 07:47:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve canonical loop form, which means Exit block should
|
|
|
|
// have only one predecessor.
|
2009-09-10 02:18:18 +08:00
|
|
|
SplitEdge(L->getLoopLatch(), Exit, this);
|
2007-07-12 07:47:28 +08:00
|
|
|
|
2009-01-26 09:38:24 +08: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-10 04:19:46 +08:00
|
|
|
}
|