llvm.org GIT mirror llvm / 7dd46b0
Fix bug: LoopPreheaders/2003-08-15-PreheadersFail.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7915 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
2 changed file(s) with 89 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
3131 class Loop {
3232 Loop *ParentLoop;
3333 std::vector SubLoops; // Loops contained entirely within this one
34 std::vector Blocks; // First entry is the header node
35 std::vector *> ExitBlocks; // Reachable blocks outside the loop
34 std::vector*> Blocks; // First entry is the header node
35 std::vector ExitBlocks; // Reachable blocks outside the loop
3636 unsigned LoopDepth; // Nesting depth of this loop
3737
3838 Loop(const Loop &); // DO NOT IMPLEMENT
3939 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
4040 public:
4141
42 inline unsigned getLoopDepth() const { return LoopDepth; }
43 inline BasicBlock *getHeader() const { return Blocks.front(); }
44 inline Loop *getParentLoop() const { return ParentLoop; }
42 unsigned getLoopDepth() const { return LoopDepth; }
43 BasicBlock *getHeader() const { return Blocks.front(); }
44 Loop *getParentLoop() const { return ParentLoop; }
4545
4646 /// contains - Return true of the specified basic block is in this loop
4747 bool contains(const BasicBlock *BB) const;
104104 ///
105105 void changeExitBlock(BasicBlock *Old, BasicBlock *New);
106106
107 void print(std::ostream &O) const;
107 void print(std::ostream &O, unsigned Depth = 0) const;
108108 void dump() const;
109109 private:
110110 friend class LoopInfo;
180180 private:
181181 void Calculate(const DominatorSet &DS);
182182 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
183 void MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent);
184 void InsertLoopInto(Loop *L, Loop *Parent);
183185 };
184186
185187
4646 return NumBackEdges;
4747 }
4848
49 void Loop::print(std::ostream &OS) const {
50 OS << std::string(getLoopDepth()*2, ' ') << "Loop Containing: ";
49 void Loop::print(std::ostream &OS, unsigned Depth) const {
50 OS << std::string(Depth*2, ' ') << "Loop Containing: ";
5151
5252 for (unsigned i = 0; i < getBlocks().size(); ++i) {
5353 if (i) OS << ",";
6464 OS << "\n";
6565
6666 for (unsigned i = 0, e = getSubLoops().size(); i != e; ++i)
67 getSubLoops()[i]->print(OS);
67 getSubLoops()[i]->print(OS, Depth+2);
6868 }
6969
7070 void Loop::dump() const {
185185 NewLoop->ParentLoop = L;
186186 }
187187
188
189188 // Add the basic blocks that comprise this loop to the BBMap so that this
190189 // loop can be found for them.
191190 //
196195 BBMap.insert(BBMI, std::make_pair(*I, L)); // Must be at this level
197196 }
198197
198 // Now that we have a list of all of the child loops of this loop, check to
199 // see if any of them should actually be nested inside of each other. We can
200 // accidentally pull loops our of their parents, so we must make sure to
201 // organize the loop nests correctly now.
202 {
203 std::map ContainingLoops;
204 for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
205 Loop *Child = L->SubLoops[i];
206 assert(Child->getParentLoop() == L && "Not proper child loop?");
207
208 if (Loop *ContainingLoop = ContainingLoops[Child->getHeader()]) {
209 // If there is already a loop which contains this loop, move this loop
210 // into the containing loop.
211 MoveSiblingLoopInto(Child, ContainingLoop);
212 --i; // The loop got removed from the SubLoops list.
213 } else {
214 // This is currently considered to be a top-level loop. Check to see if
215 // any of the contained blocks are loop headers for subloops we have
216 // already processed.
217 for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
218 Loop *&BlockLoop = ContainingLoops[Child->Blocks[b]];
219 if (BlockLoop == 0) { // Child block not processed yet...
220 BlockLoop = Child;
221 } else if (BlockLoop != Child) {
222 // There is already a loop which contains this block, that means
223 // that we should reparent the loop which the block is currently
224 // considered to belong to to be a child of this loop.
225 MoveSiblingLoopInto(BlockLoop, Child);
226
227 // Reparent all of the blocks which used to belong to BlockLoops
228 for (unsigned j = 0, e = BlockLoop->Blocks.size(); j != e; ++j)
229 ContainingLoops[BlockLoop->Blocks[j]] = Child;
230
231 --i; // We just shrunk the SubLoops list.
232 }
233 }
234 }
235 }
236 }
237
199238 // Now that we know all of the blocks that make up this loop, see if there are
200239 // any branches to outside of the loop... building the ExitBlocks list.
201240 for (std::vector::iterator BI = L->Blocks.begin(),
206245
207246 return L;
208247 }
248
249 /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside of
250 /// the NewParent Loop, instead of being a sibling of it.
251 void LoopInfo::MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent) {
252 Loop *OldParent = NewChild->getParentLoop();
253 assert(OldParent && OldParent == NewParent->getParentLoop() &&
254 NewChild != NewParent && "Not sibling loops!");
255
256 // Remove NewChild from being a child of OldParent
257 std::vector::iterator I =
258 std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(), NewChild);
259 assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
260 OldParent->SubLoops.erase(I); // Remove from parent's subloops list
261 NewChild->ParentLoop = 0;
262
263 InsertLoopInto(NewChild, NewParent);
264 }
265
266 /// InsertLoopInto - This inserts loop L into the specified parent loop. If the
267 /// parent loop contains a loop which should contain L, the loop gets inserted
268 /// into L instead.
269 void LoopInfo::InsertLoopInto(Loop *L, Loop *Parent) {
270 BasicBlock *LHeader = L->getHeader();
271 assert(Parent->contains(LHeader) && "This loop should not be inserted here!");
272
273 // Check to see if it belongs in a child loop...
274 for (unsigned i = 0, e = Parent->SubLoops.size(); i != e; ++i)
275 if (Parent->SubLoops[i]->contains(LHeader)) {
276 InsertLoopInto(L, Parent->SubLoops[i]);
277 return;
278 }
279
280 // If not, insert it here!
281 Parent->SubLoops.push_back(L);
282 L->ParentLoop = Parent;
283 }
284
285
209286
210287 /// getLoopPreheader - If there is a preheader for this loop, return it. A
211288 /// loop has a preheader if there is only one edge to the header of the loop