Skip to content

Commit 7fab23b

Browse files
committed
LoopRotate requires canonical loop form, so it always has preheaders
and latch blocks. Reorder entry conditions to make hte pass faster and more logical. llvm-svn: 123069
1 parent d62691f commit 7fab23b

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

llvm/lib/Transforms/Scalar/LoopRotation.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,22 @@ bool LoopRotate::runOnLoop(Loop *L, LPPassManager &LPM) {
9898

9999
/// Rotate loop LP. Return true if the loop is rotated.
100100
bool LoopRotate::rotateLoop(Loop *L) {
101-
BasicBlock *OrigPreHeader = L->getLoopPreheader();
102-
if (!OrigPreHeader) return false;
103-
104-
BasicBlock *OrigLatch = L->getLoopLatch();
105-
if (!OrigLatch) return false;
106-
107-
BasicBlock *OrigHeader = L->getHeader();
108-
109101
// If the loop has only one block then there is not much to rotate.
110102
if (L->getBlocks().size() == 1)
111103
return false;
112-
104+
105+
BasicBlock *OrigHeader = L->getHeader();
106+
107+
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
108+
if (BI == 0 || BI->isUnconditional())
109+
return false;
110+
113111
// If the loop header is not one of the loop exiting blocks then
114112
// either this loop is already rotated or it is not
115113
// suitable for loop rotation transformations.
116114
if (!L->isLoopExiting(OrigHeader))
117115
return false;
118116

119-
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
120-
if (!BI)
121-
return false;
122-
assert(BI->isConditional() && "Branch Instruction is not conditional");
123-
124117
// Updating PHInodes in loops with multiple exits adds complexity.
125118
// Keep it simple, and restrict loop rotation to loops with one exit only.
126119
// In future, lift this restriction and support for multiple exits if
@@ -139,6 +132,9 @@ bool LoopRotate::rotateLoop(Loop *L) {
139132
}
140133

141134
// Now, this loop is suitable for rotation.
135+
BasicBlock *OrigPreHeader = L->getLoopPreheader();
136+
BasicBlock *OrigLatch = L->getLoopLatch();
137+
assert(OrigPreHeader && OrigLatch && "Loop not in canonical form?");
142138

143139
// Anything ScalarEvolution may know about this loop or the PHI nodes
144140
// in its header will soon be invalidated.
@@ -300,7 +296,7 @@ bool LoopRotate::rotateLoop(Loop *L) {
300296
// Also, since this original header only has one predecessor, zap its
301297
// PHI nodes, which are now trivial.
302298
FoldSingleEntryPHINodes(OrigHeader);
303-
299+
304300
// TODO: We could just go ahead and merge OrigHeader into its predecessor
305301
// at this point, if we don't mind updating dominator info.
306302

0 commit comments

Comments
 (0)