File: | llvm/lib/CodeGen/MachineLoopInfo.cpp |
Warning: | line 135, column 31 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines the MachineLoopInfo class that is used to identify natural | |||
10 | // loops and determine the loop depth of various nodes of the CFG. Note that | |||
11 | // the loops identified may actually be several natural loops that share the | |||
12 | // same header node... not just a single natural loop. | |||
13 | // | |||
14 | //===----------------------------------------------------------------------===// | |||
15 | ||||
16 | #include "llvm/CodeGen/MachineLoopInfo.h" | |||
17 | #include "llvm/Analysis/LoopInfoImpl.h" | |||
18 | #include "llvm/CodeGen/MachineDominators.h" | |||
19 | #include "llvm/CodeGen/Passes.h" | |||
20 | #include "llvm/Config/llvm-config.h" | |||
21 | #include "llvm/Support/Debug.h" | |||
22 | #include "llvm/Support/raw_ostream.h" | |||
23 | using namespace llvm; | |||
24 | ||||
25 | // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops. | |||
26 | template class llvm::LoopBase<MachineBasicBlock, MachineLoop>; | |||
27 | template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>; | |||
28 | ||||
29 | char MachineLoopInfo::ID = 0; | |||
30 | INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",static void *initializeMachineLoopInfoPassOnce(PassRegistry & Registry) { | |||
31 | "Machine Natural Loop Construction", true, true)static void *initializeMachineLoopInfoPassOnce(PassRegistry & Registry) { | |||
32 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)initializeMachineDominatorTreePass(Registry); | |||
33 | INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",PassInfo *PI = new PassInfo( "Machine Natural Loop Construction" , "machine-loops", &MachineLoopInfo::ID, PassInfo::NormalCtor_t (callDefaultCtor<MachineLoopInfo>), true, true); Registry .registerPass(*PI, true); return PI; } static llvm::once_flag InitializeMachineLoopInfoPassFlag; void llvm::initializeMachineLoopInfoPass (PassRegistry &Registry) { llvm::call_once(InitializeMachineLoopInfoPassFlag , initializeMachineLoopInfoPassOnce, std::ref(Registry)); } | |||
34 | "Machine Natural Loop Construction", true, true)PassInfo *PI = new PassInfo( "Machine Natural Loop Construction" , "machine-loops", &MachineLoopInfo::ID, PassInfo::NormalCtor_t (callDefaultCtor<MachineLoopInfo>), true, true); Registry .registerPass(*PI, true); return PI; } static llvm::once_flag InitializeMachineLoopInfoPassFlag; void llvm::initializeMachineLoopInfoPass (PassRegistry &Registry) { llvm::call_once(InitializeMachineLoopInfoPassFlag , initializeMachineLoopInfoPassOnce, std::ref(Registry)); } | |||
35 | ||||
36 | char &llvm::MachineLoopInfoID = MachineLoopInfo::ID; | |||
37 | ||||
38 | bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) { | |||
39 | calculate(getAnalysis<MachineDominatorTree>()); | |||
40 | return false; | |||
41 | } | |||
42 | ||||
43 | void MachineLoopInfo::calculate(MachineDominatorTree &MDT) { | |||
44 | releaseMemory(); | |||
45 | LI.analyze(MDT.getBase()); | |||
46 | } | |||
47 | ||||
48 | void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { | |||
49 | AU.setPreservesAll(); | |||
50 | AU.addRequired<MachineDominatorTree>(); | |||
51 | MachineFunctionPass::getAnalysisUsage(AU); | |||
52 | } | |||
53 | ||||
54 | MachineBasicBlock *MachineLoop::getTopBlock() { | |||
55 | MachineBasicBlock *TopMBB = getHeader(); | |||
56 | MachineFunction::iterator Begin = TopMBB->getParent()->begin(); | |||
57 | if (TopMBB->getIterator() != Begin) { | |||
58 | MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator()); | |||
59 | while (contains(PriorMBB)) { | |||
60 | TopMBB = PriorMBB; | |||
61 | if (TopMBB->getIterator() == Begin) | |||
62 | break; | |||
63 | PriorMBB = &*std::prev(TopMBB->getIterator()); | |||
64 | } | |||
65 | } | |||
66 | return TopMBB; | |||
67 | } | |||
68 | ||||
69 | MachineBasicBlock *MachineLoop::getBottomBlock() { | |||
70 | MachineBasicBlock *BotMBB = getHeader(); | |||
71 | MachineFunction::iterator End = BotMBB->getParent()->end(); | |||
72 | if (BotMBB->getIterator() != std::prev(End)) { | |||
73 | MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator()); | |||
74 | while (contains(NextMBB)) { | |||
75 | BotMBB = NextMBB; | |||
76 | if (BotMBB == &*std::next(BotMBB->getIterator())) | |||
77 | break; | |||
78 | NextMBB = &*std::next(BotMBB->getIterator()); | |||
79 | } | |||
80 | } | |||
81 | return BotMBB; | |||
82 | } | |||
83 | ||||
84 | MachineBasicBlock *MachineLoop::findLoopControlBlock() { | |||
85 | if (MachineBasicBlock *Latch = getLoopLatch()) { | |||
86 | if (isLoopExiting(Latch)) | |||
87 | return Latch; | |||
88 | else | |||
89 | return getExitingBlock(); | |||
90 | } | |||
91 | return nullptr; | |||
92 | } | |||
93 | ||||
94 | DebugLoc MachineLoop::getStartLoc() const { | |||
95 | // Try the pre-header first. | |||
96 | if (MachineBasicBlock *PHeadMBB = getLoopPreheader()) | |||
97 | if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock()) | |||
98 | if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc()) | |||
99 | return DL; | |||
100 | ||||
101 | // If we have no pre-header or there are no instructions with debug | |||
102 | // info in it, try the header. | |||
103 | if (MachineBasicBlock *HeadMBB = getHeader()) | |||
104 | if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock()) | |||
105 | return HeadBB->getTerminator()->getDebugLoc(); | |||
106 | ||||
107 | return DebugLoc(); | |||
108 | } | |||
109 | ||||
110 | MachineBasicBlock * | |||
111 | MachineLoopInfo::findLoopPreheader(MachineLoop *L, | |||
112 | bool SpeculativePreheader) const { | |||
113 | if (MachineBasicBlock *PB = L->getLoopPreheader()) | |||
| ||||
114 | return PB; | |||
115 | ||||
116 | if (!SpeculativePreheader) | |||
117 | return nullptr; | |||
118 | ||||
119 | MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch(); | |||
120 | if (HB->pred_size() != 2 || HB->hasAddressTaken()) | |||
121 | return nullptr; | |||
122 | // Find the predecessor of the header that is not the latch block. | |||
123 | MachineBasicBlock *Preheader = nullptr; | |||
124 | for (MachineBasicBlock *P : HB->predecessors()) { | |||
125 | if (P == LB) | |||
126 | continue; | |||
127 | // Sanity. | |||
128 | if (Preheader) | |||
129 | return nullptr; | |||
130 | Preheader = P; | |||
131 | } | |||
132 | ||||
133 | // Check if the preheader candidate is a successor of any other loop | |||
134 | // headers. We want to avoid having two loop setups in the same block. | |||
135 | for (MachineBasicBlock *S : Preheader->successors()) { | |||
| ||||
136 | if (S == HB) | |||
137 | continue; | |||
138 | MachineLoop *T = getLoopFor(S); | |||
139 | if (T && T->getHeader() == S) | |||
140 | return nullptr; | |||
141 | } | |||
142 | return Preheader; | |||
143 | } | |||
144 | ||||
145 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | |||
146 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineLoop::dump() const { | |||
147 | print(dbgs()); | |||
148 | } | |||
149 | #endif |