12
12
// ===----------------------------------------------------------------------===//
13
13
14
14
#define DEBUG_TYPE " loop-instsimplify"
15
+ #include " llvm/Instructions.h"
15
16
#include " llvm/Analysis/Dominators.h"
16
17
#include " llvm/Analysis/InstructionSimplify.h"
17
18
#include " llvm/Analysis/LoopInfo.h"
@@ -68,7 +69,10 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
68
69
69
70
SmallPtrSet<const Instruction*, 8 > S1, S2, *ToSimplify = &S1, *Next = &S2;
70
71
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;
72
76
SmallPtrSet<BasicBlock*, 32 > Visited;
73
77
74
78
bool Changed = false ;
@@ -79,10 +83,12 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
79
83
VisitStack.clear ();
80
84
Visited.clear ();
81
85
82
- VisitStack.push_back (L->getHeader ());
86
+ VisitStack.push_back (WorklistItem ( L->getHeader (), false ));
83
87
84
88
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 ();
86
92
87
93
// Simplify instructions in the current basic block.
88
94
for (BasicBlock::iterator BI = BB->begin (), BE = BB->end (); BI != BE;) {
@@ -109,16 +115,44 @@ bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
109
115
}
110
116
}
111
117
LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions (I);
118
+
119
+ if (IsSubloopHeader && !isa<PHINode>(I))
120
+ break ;
112
121
}
113
122
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.
115
127
for (succ_iterator SI = succ_begin (BB), SE = succ_end (BB); SI != SE;
116
128
++SI) {
117
129
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
+
118
150
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 ));
122
156
}
123
157
}
124
158
0 commit comments