Mercurial > hg > CbC > CbC_llvm
comparison lib/Analysis/LoopInfo.cpp @ 77:54457678186b LLVM3.6
LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Sep 2014 22:06:00 +0900 |
parents | 95c75e76d11b |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
34:e874dbf0ad9d | 77:54457678186b |
---|---|
15 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
16 | 16 |
17 #include "llvm/Analysis/LoopInfo.h" | 17 #include "llvm/Analysis/LoopInfo.h" |
18 #include "llvm/ADT/DepthFirstIterator.h" | 18 #include "llvm/ADT/DepthFirstIterator.h" |
19 #include "llvm/ADT/SmallPtrSet.h" | 19 #include "llvm/ADT/SmallPtrSet.h" |
20 #include "llvm/Analysis/Dominators.h" | |
21 #include "llvm/Analysis/LoopInfoImpl.h" | 20 #include "llvm/Analysis/LoopInfoImpl.h" |
22 #include "llvm/Analysis/LoopIterator.h" | 21 #include "llvm/Analysis/LoopIterator.h" |
23 #include "llvm/Analysis/ValueTracking.h" | 22 #include "llvm/Analysis/ValueTracking.h" |
24 #include "llvm/Assembly/Writer.h" | 23 #include "llvm/IR/CFG.h" |
25 #include "llvm/IR/Constants.h" | 24 #include "llvm/IR/Constants.h" |
25 #include "llvm/IR/Dominators.h" | |
26 #include "llvm/IR/Instructions.h" | 26 #include "llvm/IR/Instructions.h" |
27 #include "llvm/IR/Metadata.h" | 27 #include "llvm/IR/Metadata.h" |
28 #include "llvm/Support/CFG.h" | |
29 #include "llvm/Support/CommandLine.h" | 28 #include "llvm/Support/CommandLine.h" |
30 #include "llvm/Support/Debug.h" | 29 #include "llvm/Support/Debug.h" |
31 #include <algorithm> | 30 #include <algorithm> |
32 using namespace llvm; | 31 using namespace llvm; |
33 | 32 |
45 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), | 44 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
46 cl::desc("Verify loop info (time consuming)")); | 45 cl::desc("Verify loop info (time consuming)")); |
47 | 46 |
48 char LoopInfo::ID = 0; | 47 char LoopInfo::ID = 0; |
49 INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) | 48 INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) |
50 INITIALIZE_PASS_DEPENDENCY(DominatorTree) | 49 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
51 INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) | 50 INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) |
52 | 51 |
53 // Loop identifier metadata name. | 52 // Loop identifier metadata name. |
54 static const char *const LoopMDName = "llvm.loop"; | 53 static const char *const LoopMDName = "llvm.loop"; |
55 | 54 |
140 /// variable. | 139 /// variable. |
141 /// | 140 /// |
142 PHINode *Loop::getCanonicalInductionVariable() const { | 141 PHINode *Loop::getCanonicalInductionVariable() const { |
143 BasicBlock *H = getHeader(); | 142 BasicBlock *H = getHeader(); |
144 | 143 |
145 BasicBlock *Incoming = 0, *Backedge = 0; | 144 BasicBlock *Incoming = nullptr, *Backedge = nullptr; |
146 pred_iterator PI = pred_begin(H); | 145 pred_iterator PI = pred_begin(H); |
147 assert(PI != pred_end(H) && | 146 assert(PI != pred_end(H) && |
148 "Loop must have at least one backedge!"); | 147 "Loop must have at least one backedge!"); |
149 Backedge = *PI++; | 148 Backedge = *PI++; |
150 if (PI == pred_end(H)) return 0; // dead loop | 149 if (PI == pred_end(H)) return nullptr; // dead loop |
151 Incoming = *PI++; | 150 Incoming = *PI++; |
152 if (PI != pred_end(H)) return 0; // multiple backedges? | 151 if (PI != pred_end(H)) return nullptr; // multiple backedges? |
153 | 152 |
154 if (contains(Incoming)) { | 153 if (contains(Incoming)) { |
155 if (contains(Backedge)) | 154 if (contains(Backedge)) |
156 return 0; | 155 return nullptr; |
157 std::swap(Incoming, Backedge); | 156 std::swap(Incoming, Backedge); |
158 } else if (!contains(Backedge)) | 157 } else if (!contains(Backedge)) |
159 return 0; | 158 return nullptr; |
160 | 159 |
161 // Loop over all of the PHI nodes, looking for a canonical indvar. | 160 // Loop over all of the PHI nodes, looking for a canonical indvar. |
162 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { | 161 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
163 PHINode *PN = cast<PHINode>(I); | 162 PHINode *PN = cast<PHINode>(I); |
164 if (ConstantInt *CI = | 163 if (ConstantInt *CI = |
170 Inc->getOperand(0) == PN) | 169 Inc->getOperand(0) == PN) |
171 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) | 170 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
172 if (CI->equalsInt(1)) | 171 if (CI->equalsInt(1)) |
173 return PN; | 172 return PN; |
174 } | 173 } |
175 return 0; | 174 return nullptr; |
176 } | 175 } |
177 | 176 |
178 /// isLCSSAForm - Return true if the Loop is in LCSSA form | 177 /// isLCSSAForm - Return true if the Loop is in LCSSA form |
179 bool Loop::isLCSSAForm(DominatorTree &DT) const { | 178 bool Loop::isLCSSAForm(DominatorTree &DT) const { |
180 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { | 179 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
181 BasicBlock *BB = *BI; | 180 BasicBlock *BB = *BI; |
182 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) | 181 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
183 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; | 182 for (Use &U : I->uses()) { |
184 ++UI) { | 183 Instruction *UI = cast<Instruction>(U.getUser()); |
185 User *U = *UI; | 184 BasicBlock *UserBB = UI->getParent(); |
186 BasicBlock *UserBB = cast<Instruction>(U)->getParent(); | 185 if (PHINode *P = dyn_cast<PHINode>(UI)) |
187 if (PHINode *P = dyn_cast<PHINode>(U)) | 186 UserBB = P->getIncomingBlock(U); |
188 UserBB = P->getIncomingBlock(UI); | |
189 | 187 |
190 // Check the current block, as a fast-path, before checking whether | 188 // Check the current block, as a fast-path, before checking whether |
191 // the use is anywhere in the loop. Most values are used in the same | 189 // the use is anywhere in the loop. Most values are used in the same |
192 // block they are defined in. Also, blocks not reachable from the | 190 // block they are defined in. Also, blocks not reachable from the |
193 // entry are special; uses in them don't need to go through PHIs. | 191 // entry are special; uses in them don't need to go through PHIs. |
218 for (Loop::block_iterator I = block_begin(), E = block_end(); I != E; ++I) { | 216 for (Loop::block_iterator I = block_begin(), E = block_end(); I != E; ++I) { |
219 if (isa<IndirectBrInst>((*I)->getTerminator())) | 217 if (isa<IndirectBrInst>((*I)->getTerminator())) |
220 return false; | 218 return false; |
221 | 219 |
222 if (const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator())) | 220 if (const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator())) |
223 if (II->hasFnAttr(Attribute::NoDuplicate)) | 221 if (II->cannotDuplicate()) |
224 return false; | 222 return false; |
225 | 223 |
226 for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); BI != BE; ++BI) { | 224 for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); BI != BE; ++BI) { |
227 if (const CallInst *CI = dyn_cast<CallInst>(BI)) { | 225 if (const CallInst *CI = dyn_cast<CallInst>(BI)) { |
228 if (CI->hasFnAttr(Attribute::NoDuplicate)) | 226 if (CI->cannotDuplicate()) |
229 return false; | 227 return false; |
230 } | 228 } |
231 } | 229 } |
232 } | 230 } |
233 return true; | 231 return true; |
234 } | 232 } |
235 | 233 |
236 MDNode *Loop::getLoopID() const { | 234 MDNode *Loop::getLoopID() const { |
237 MDNode *LoopID = 0; | 235 MDNode *LoopID = nullptr; |
238 if (isLoopSimplifyForm()) { | 236 if (isLoopSimplifyForm()) { |
239 LoopID = getLoopLatch()->getTerminator()->getMetadata(LoopMDName); | 237 LoopID = getLoopLatch()->getTerminator()->getMetadata(LoopMDName); |
240 } else { | 238 } else { |
241 // Go through each predecessor of the loop header and check the | 239 // Go through each predecessor of the loop header and check the |
242 // terminator for the metadata. | 240 // terminator for the metadata. |
243 BasicBlock *H = getHeader(); | 241 BasicBlock *H = getHeader(); |
244 for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) { | 242 for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) { |
245 TerminatorInst *TI = (*I)->getTerminator(); | 243 TerminatorInst *TI = (*I)->getTerminator(); |
246 MDNode *MD = 0; | 244 MDNode *MD = nullptr; |
247 | 245 |
248 // Check if this terminator branches to the loop header. | 246 // Check if this terminator branches to the loop header. |
249 for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) { | 247 for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) { |
250 if (TI->getSuccessor(i) == H) { | 248 if (TI->getSuccessor(i) == H) { |
251 MD = TI->getMetadata(LoopMDName); | 249 MD = TI->getMetadata(LoopMDName); |
252 break; | 250 break; |
253 } | 251 } |
254 } | 252 } |
255 if (!MD) | 253 if (!MD) |
256 return 0; | 254 return nullptr; |
257 | 255 |
258 if (!LoopID) | 256 if (!LoopID) |
259 LoopID = MD; | 257 LoopID = MD; |
260 else if (MD != LoopID) | 258 else if (MD != LoopID) |
261 return 0; | 259 return nullptr; |
262 } | 260 } |
263 } | 261 } |
264 if (!LoopID || LoopID->getNumOperands() == 0 || | 262 if (!LoopID || LoopID->getNumOperands() == 0 || |
265 LoopID->getOperand(0) != LoopID) | 263 LoopID->getOperand(0) != LoopID) |
266 return 0; | 264 return nullptr; |
267 return LoopID; | 265 return LoopID; |
268 } | 266 } |
269 | 267 |
270 void Loop::setLoopID(MDNode *LoopID) const { | 268 void Loop::setLoopID(MDNode *LoopID) const { |
271 assert(LoopID && "Loop ID should not be null"); | 269 assert(LoopID && "Loop ID should not be null"); |
402 BasicBlock *Loop::getUniqueExitBlock() const { | 400 BasicBlock *Loop::getUniqueExitBlock() const { |
403 SmallVector<BasicBlock *, 8> UniqueExitBlocks; | 401 SmallVector<BasicBlock *, 8> UniqueExitBlocks; |
404 getUniqueExitBlocks(UniqueExitBlocks); | 402 getUniqueExitBlocks(UniqueExitBlocks); |
405 if (UniqueExitBlocks.size() == 1) | 403 if (UniqueExitBlocks.size() == 1) |
406 return UniqueExitBlocks[0]; | 404 return UniqueExitBlocks[0]; |
407 return 0; | 405 return nullptr; |
408 } | 406 } |
409 | 407 |
410 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | 408 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
411 void Loop::dump() const { | 409 void Loop::dump() const { |
412 print(dbgs()); | 410 print(dbgs()); |
526 | 524 |
527 /// updateSubloopParents - Update the parent loop for all subloops directly | 525 /// updateSubloopParents - Update the parent loop for all subloops directly |
528 /// nested within unloop. | 526 /// nested within unloop. |
529 void UnloopUpdater::updateSubloopParents() { | 527 void UnloopUpdater::updateSubloopParents() { |
530 while (!Unloop->empty()) { | 528 while (!Unloop->empty()) { |
531 Loop *Subloop = *llvm::prior(Unloop->end()); | 529 Loop *Subloop = *std::prev(Unloop->end()); |
532 Unloop->removeChildLoop(llvm::prior(Unloop->end())); | 530 Unloop->removeChildLoop(std::prev(Unloop->end())); |
533 | 531 |
534 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); | 532 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); |
535 if (Loop *Parent = SubloopParents[Subloop]) | 533 if (Loop *Parent = SubloopParents[Subloop]) |
536 Parent->addChildLoop(Subloop); | 534 Parent->addChildLoop(Subloop); |
537 else | 535 else |
548 | 546 |
549 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and | 547 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and |
550 // is considered uninitialized. | 548 // is considered uninitialized. |
551 Loop *NearLoop = BBLoop; | 549 Loop *NearLoop = BBLoop; |
552 | 550 |
553 Loop *Subloop = 0; | 551 Loop *Subloop = nullptr; |
554 if (NearLoop != Unloop && Unloop->contains(NearLoop)) { | 552 if (NearLoop != Unloop && Unloop->contains(NearLoop)) { |
555 Subloop = NearLoop; | 553 Subloop = NearLoop; |
556 // Find the subloop ancestor that is directly contained within Unloop. | 554 // Find the subloop ancestor that is directly contained within Unloop. |
557 while (Subloop->getParentLoop() != Unloop) { | 555 while (Subloop->getParentLoop() != Unloop) { |
558 Subloop = Subloop->getParentLoop(); | 556 Subloop = Subloop->getParentLoop(); |
564 } | 562 } |
565 | 563 |
566 succ_iterator I = succ_begin(BB), E = succ_end(BB); | 564 succ_iterator I = succ_begin(BB), E = succ_end(BB); |
567 if (I == E) { | 565 if (I == E) { |
568 assert(!Subloop && "subloop blocks must have a successor"); | 566 assert(!Subloop && "subloop blocks must have a successor"); |
569 NearLoop = 0; // unloop blocks may now exit the function. | 567 NearLoop = nullptr; // unloop blocks may now exit the function. |
570 } | 568 } |
571 for (; I != E; ++I) { | 569 for (; I != E; ++I) { |
572 if (*I == BB) | 570 if (*I == BB) |
573 continue; // self loops are uninteresting | 571 continue; // self loops are uninteresting |
574 | 572 |
612 //===----------------------------------------------------------------------===// | 610 //===----------------------------------------------------------------------===// |
613 // LoopInfo implementation | 611 // LoopInfo implementation |
614 // | 612 // |
615 bool LoopInfo::runOnFunction(Function &) { | 613 bool LoopInfo::runOnFunction(Function &) { |
616 releaseMemory(); | 614 releaseMemory(); |
617 LI.Analyze(getAnalysis<DominatorTree>().getBase()); | 615 LI.Analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree()); |
618 return false; | 616 return false; |
619 } | 617 } |
620 | 618 |
621 /// updateUnloop - The last backedge has been removed from a loop--now the | 619 /// updateUnloop - The last backedge has been removed from a loop--now the |
622 /// "unloop". Find a new parent for the blocks contained within unloop and | 620 /// "unloop". Find a new parent for the blocks contained within unloop and |
637 if (getLoopFor(*I) != Unloop) | 635 if (getLoopFor(*I) != Unloop) |
638 continue; | 636 continue; |
639 | 637 |
640 // Blocks no longer have a parent but are still referenced by Unloop until | 638 // Blocks no longer have a parent but are still referenced by Unloop until |
641 // the Unloop object is deleted. | 639 // the Unloop object is deleted. |
642 LI.changeLoopFor(*I, 0); | 640 LI.changeLoopFor(*I, nullptr); |
643 } | 641 } |
644 | 642 |
645 // Remove the loop from the top-level LoopInfo object. | 643 // Remove the loop from the top-level LoopInfo object. |
646 for (LoopInfo::iterator I = LI.begin();; ++I) { | 644 for (LoopInfo::iterator I = LI.begin();; ++I) { |
647 assert(I != LI.end() && "Couldn't find loop"); | 645 assert(I != LI.end() && "Couldn't find loop"); |
651 } | 649 } |
652 } | 650 } |
653 | 651 |
654 // Move all of the subloops to the top-level. | 652 // Move all of the subloops to the top-level. |
655 while (!Unloop->empty()) | 653 while (!Unloop->empty()) |
656 LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end()))); | 654 LI.addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end()))); |
657 | 655 |
658 return; | 656 return; |
659 } | 657 } |
660 | 658 |
661 // Update the parent loop for all blocks within the loop. Blocks within | 659 // Update the parent loop for all blocks within the loop. Blocks within |
703 } | 701 } |
704 } | 702 } |
705 | 703 |
706 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { | 704 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
707 AU.setPreservesAll(); | 705 AU.setPreservesAll(); |
708 AU.addRequired<DominatorTree>(); | 706 AU.addRequired<DominatorTreeWrapperPass>(); |
709 } | 707 } |
710 | 708 |
711 void LoopInfo::print(raw_ostream &OS, const Module*) const { | 709 void LoopInfo::print(raw_ostream &OS, const Module*) const { |
712 LI.print(OS); | 710 LI.print(OS); |
713 } | 711 } |