Skip to content

Commit 80bd9af

Browse files
author
Cameron Zwarich
committed
Contract subloop bodies. However, it is still important to visit the phis at the
top of subloop headers, as the phi uses logically occur outside of the subloop. llvm-svn: 123062
1 parent 6a1fb8f commit 80bd9af

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

llvm/lib/Transforms/Scalar/LoopInstSimplify.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#define DEBUG_TYPE "loop-instsimplify"
15+
#include "llvm/Instructions.h"
1516
#include "llvm/Analysis/Dominators.h"
1617
#include "llvm/Analysis/InstructionSimplify.h"
1718
#include "llvm/Analysis/LoopInfo.h"
@@ -68,7 +69,10 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
6869

6970
SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
7071

71-
SmallVector<BasicBlock*, 16> VisitStack;
72+
// The bit we are stealing from the pointer represents whether this basic
73+
// block is the header of a subloop, in which case we only process its phis.
74+
typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
75+
SmallVector<WorklistItem, 16> VisitStack;
7276
SmallPtrSet<BasicBlock*, 32> Visited;
7377

7478
bool Changed = false;
@@ -79,10 +83,12 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
7983
VisitStack.clear();
8084
Visited.clear();
8185

82-
VisitStack.push_back(L->getHeader());
86+
VisitStack.push_back(WorklistItem(L->getHeader(), false));
8387

8488
while (!VisitStack.empty()) {
85-
BasicBlock *BB = VisitStack.pop_back_val();
89+
WorklistItem Item = VisitStack.pop_back_val();
90+
BasicBlock* BB = Item.getPointer();
91+
bool IsSubloopHeader = Item.getInt();
8692

8793
// Simplify instructions in the current basic block.
8894
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
@@ -109,16 +115,44 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
109115
}
110116
}
111117
LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
118+
119+
if (IsSubloopHeader && !isa<PHINode>(I))
120+
break;
112121
}
113122

114-
// Add all successors to the worklist, except for loop exit blocks.
123+
// Add all successors to the worklist, except for loop exit blocks and the
124+
// bodies of subloops. We visit the headers of loops so that we can process
125+
// their phis, but we contract the rest of the subloop body and only follow
126+
// edges leading back to the original loop.
115127
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
116128
++SI) {
117129
BasicBlock *SuccBB = *SI;
130+
if (!Visited.insert(SuccBB))
131+
continue;
132+
133+
const Loop *SuccLoop = LI->getLoopFor(SuccBB);
134+
if (SuccLoop && SuccLoop->getHeader() == SuccBB
135+
&& L->contains(SuccLoop)) {
136+
VisitStack.push_back(WorklistItem(SuccBB, true));
137+
138+
SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
139+
SuccLoop->getExitBlocks(SubLoopExitBlocks);
140+
141+
for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
142+
BasicBlock *ExitBB = SubLoopExitBlocks[i];
143+
if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB))
144+
VisitStack.push_back(WorklistItem(ExitBB, false));
145+
}
146+
147+
continue;
148+
}
149+
118150
bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
119-
ExitBlocks.end(), SuccBB);
120-
if (!IsExitBlock && Visited.insert(SuccBB))
121-
VisitStack.push_back(SuccBB);
151+
ExitBlocks.end(), SuccBB);
152+
if (IsExitBlock)
153+
continue;
154+
155+
VisitStack.push_back(WorklistItem(SuccBB, false));
122156
}
123157
}
124158

0 commit comments

Comments
 (0)