/src/llvm-project/llvm/lib/CodeGen/MachineLoopInfo.cpp
Line | Count | Source (jump to first uncovered line) |
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/CodeGen/MachineDominators.h" |
18 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
19 | | #include "llvm/CodeGen/TargetInstrInfo.h" |
20 | | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
21 | | #include "llvm/Config/llvm-config.h" |
22 | | #include "llvm/InitializePasses.h" |
23 | | #include "llvm/Pass.h" |
24 | | #include "llvm/PassRegistry.h" |
25 | | #include "llvm/Support/GenericLoopInfoImpl.h" |
26 | | |
27 | | using namespace llvm; |
28 | | |
29 | | // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops. |
30 | | template class llvm::LoopBase<MachineBasicBlock, MachineLoop>; |
31 | | template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>; |
32 | | |
33 | | char MachineLoopInfo::ID = 0; |
34 | 239k | MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) { |
35 | 239k | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
36 | 239k | } |
37 | 62 | INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops", |
38 | 62 | "Machine Natural Loop Construction", true, true) |
39 | 62 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
40 | 62 | INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops", |
41 | | "Machine Natural Loop Construction", true, true) |
42 | | |
43 | | char &llvm::MachineLoopInfoID = MachineLoopInfo::ID; |
44 | | |
45 | 642k | bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) { |
46 | 642k | calculate(getAnalysis<MachineDominatorTree>()); |
47 | 642k | return false; |
48 | 642k | } |
49 | | |
50 | 642k | void MachineLoopInfo::calculate(MachineDominatorTree &MDT) { |
51 | 642k | releaseMemory(); |
52 | 642k | LI.analyze(MDT.getBase()); |
53 | 642k | } |
54 | | |
55 | 225k | void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
56 | 225k | AU.setPreservesAll(); |
57 | 225k | AU.addRequired<MachineDominatorTree>(); |
58 | 225k | MachineFunctionPass::getAnalysisUsage(AU); |
59 | 225k | } |
60 | | |
61 | 3.52k | MachineBasicBlock *MachineLoop::getTopBlock() { |
62 | 3.52k | MachineBasicBlock *TopMBB = getHeader(); |
63 | 3.52k | MachineFunction::iterator Begin = TopMBB->getParent()->begin(); |
64 | 3.52k | if (TopMBB->getIterator() != Begin) { |
65 | 3.52k | MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator()); |
66 | 3.52k | while (contains(PriorMBB)) { |
67 | 0 | TopMBB = PriorMBB; |
68 | 0 | if (TopMBB->getIterator() == Begin) |
69 | 0 | break; |
70 | 0 | PriorMBB = &*std::prev(TopMBB->getIterator()); |
71 | 0 | } |
72 | 3.52k | } |
73 | 3.52k | return TopMBB; |
74 | 3.52k | } |
75 | | |
76 | 0 | MachineBasicBlock *MachineLoop::getBottomBlock() { |
77 | 0 | MachineBasicBlock *BotMBB = getHeader(); |
78 | 0 | MachineFunction::iterator End = BotMBB->getParent()->end(); |
79 | 0 | if (BotMBB->getIterator() != std::prev(End)) { |
80 | 0 | MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator()); |
81 | 0 | while (contains(NextMBB)) { |
82 | 0 | BotMBB = NextMBB; |
83 | 0 | if (BotMBB == &*std::next(BotMBB->getIterator())) |
84 | 0 | break; |
85 | 0 | NextMBB = &*std::next(BotMBB->getIterator()); |
86 | 0 | } |
87 | 0 | } |
88 | 0 | return BotMBB; |
89 | 0 | } |
90 | | |
91 | 28.0k | MachineBasicBlock *MachineLoop::findLoopControlBlock() const { |
92 | 28.0k | if (MachineBasicBlock *Latch = getLoopLatch()) { |
93 | 21.4k | if (isLoopExiting(Latch)) |
94 | 16.2k | return Latch; |
95 | 5.17k | else |
96 | 5.17k | return getExitingBlock(); |
97 | 21.4k | } |
98 | 6.67k | return nullptr; |
99 | 28.0k | } |
100 | | |
101 | 0 | DebugLoc MachineLoop::getStartLoc() const { |
102 | | // Try the pre-header first. |
103 | 0 | if (MachineBasicBlock *PHeadMBB = getLoopPreheader()) |
104 | 0 | if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock()) |
105 | 0 | if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc()) |
106 | 0 | return DL; |
107 | | |
108 | | // If we have no pre-header or there are no instructions with debug |
109 | | // info in it, try the header. |
110 | 0 | if (MachineBasicBlock *HeadMBB = getHeader()) |
111 | 0 | if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock()) |
112 | 0 | return HeadBB->getTerminator()->getDebugLoc(); |
113 | | |
114 | 0 | return DebugLoc(); |
115 | 0 | } |
116 | | |
117 | | MachineBasicBlock * |
118 | | MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader, |
119 | 0 | bool FindMultiLoopPreheader) const { |
120 | 0 | if (MachineBasicBlock *PB = L->getLoopPreheader()) |
121 | 0 | return PB; |
122 | | |
123 | 0 | if (!SpeculativePreheader) |
124 | 0 | return nullptr; |
125 | | |
126 | 0 | MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch(); |
127 | 0 | if (HB->pred_size() != 2 || HB->hasAddressTaken()) |
128 | 0 | return nullptr; |
129 | | // Find the predecessor of the header that is not the latch block. |
130 | 0 | MachineBasicBlock *Preheader = nullptr; |
131 | 0 | for (MachineBasicBlock *P : HB->predecessors()) { |
132 | 0 | if (P == LB) |
133 | 0 | continue; |
134 | | // Sanity. |
135 | 0 | if (Preheader) |
136 | 0 | return nullptr; |
137 | 0 | Preheader = P; |
138 | 0 | } |
139 | | |
140 | | // Check if the preheader candidate is a successor of any other loop |
141 | | // headers. We want to avoid having two loop setups in the same block. |
142 | 0 | if (!FindMultiLoopPreheader) { |
143 | 0 | for (MachineBasicBlock *S : Preheader->successors()) { |
144 | 0 | if (S == HB) |
145 | 0 | continue; |
146 | 0 | MachineLoop *T = getLoopFor(S); |
147 | 0 | if (T && T->getHeader() == S) |
148 | 0 | return nullptr; |
149 | 0 | } |
150 | 0 | } |
151 | 0 | return Preheader; |
152 | 0 | } |
153 | | |
154 | 26.4k | MDNode *MachineLoop::getLoopID() const { |
155 | 26.4k | MDNode *LoopID = nullptr; |
156 | 26.4k | if (const auto *MBB = findLoopControlBlock()) { |
157 | | // If there is a single latch block, then the metadata |
158 | | // node is attached to its terminating instruction. |
159 | 15.3k | const auto *BB = MBB->getBasicBlock(); |
160 | 15.3k | if (!BB) |
161 | 14 | return nullptr; |
162 | 15.3k | if (const auto *TI = BB->getTerminator()) |
163 | 15.3k | LoopID = TI->getMetadata(LLVMContext::MD_loop); |
164 | 15.3k | } else if (const auto *MBB = getHeader()) { |
165 | | // There seem to be multiple latch blocks, so we have to |
166 | | // visit all predecessors of the loop header and check |
167 | | // their terminating instructions for the metadata. |
168 | 11.1k | if (const auto *Header = MBB->getBasicBlock()) { |
169 | | // Walk over all blocks in the loop. |
170 | 11.1k | for (const auto *MBB : this->blocks()) { |
171 | 11.1k | const auto *BB = MBB->getBasicBlock(); |
172 | 11.1k | if (!BB) |
173 | 5 | return nullptr; |
174 | 11.1k | const auto *TI = BB->getTerminator(); |
175 | 11.1k | if (!TI) |
176 | 0 | return nullptr; |
177 | 11.1k | MDNode *MD = nullptr; |
178 | | // Check if this terminating instruction jumps to the loop header. |
179 | 18.8k | for (const auto *Succ : successors(TI)) { |
180 | 18.8k | if (Succ == Header) { |
181 | | // This is a jump to the header - gather the metadata from it. |
182 | 9.00k | MD = TI->getMetadata(LLVMContext::MD_loop); |
183 | 9.00k | break; |
184 | 9.00k | } |
185 | 18.8k | } |
186 | 11.1k | if (!MD) |
187 | 11.0k | return nullptr; |
188 | 54 | if (!LoopID) |
189 | 54 | LoopID = MD; |
190 | 0 | else if (MD != LoopID) |
191 | 0 | return nullptr; |
192 | 54 | } |
193 | 11.1k | } |
194 | 11.1k | } |
195 | 15.3k | if (LoopID && |
196 | 15.3k | (LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID)) |
197 | 7 | LoopID = nullptr; |
198 | 15.3k | return LoopID; |
199 | 26.4k | } |
200 | | |
201 | 442k | bool MachineLoop::isLoopInvariant(MachineInstr &I) const { |
202 | 442k | MachineFunction *MF = I.getParent()->getParent(); |
203 | 442k | MachineRegisterInfo *MRI = &MF->getRegInfo(); |
204 | 442k | const TargetSubtargetInfo &ST = MF->getSubtarget(); |
205 | 442k | const TargetRegisterInfo *TRI = ST.getRegisterInfo(); |
206 | 442k | const TargetInstrInfo *TII = ST.getInstrInfo(); |
207 | | |
208 | | // The instruction is loop invariant if all of its operands are. |
209 | 1.04M | for (const MachineOperand &MO : I.operands()) { |
210 | 1.04M | if (!MO.isReg()) |
211 | 179k | continue; |
212 | | |
213 | 870k | Register Reg = MO.getReg(); |
214 | 870k | if (Reg == 0) continue; |
215 | | |
216 | | // An instruction that uses or defines a physical register can't e.g. be |
217 | | // hoisted, so mark this as not invariant. |
218 | 867k | if (Reg.isPhysical()) { |
219 | 171k | if (MO.isUse()) { |
220 | | // If the physreg has no defs anywhere, it's just an ambient register |
221 | | // and we can freely move its uses. Alternatively, if it's allocatable, |
222 | | // it could get allocated to something with a def during allocation. |
223 | | // However, if the physreg is known to always be caller saved/restored |
224 | | // then this use is safe to hoist. |
225 | 73.3k | if (!MRI->isConstantPhysReg(Reg) && |
226 | 73.3k | !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) && |
227 | 73.3k | !TII->isIgnorableUse(MO)) |
228 | 56.0k | return false; |
229 | | // Otherwise it's safe to move. |
230 | 17.3k | continue; |
231 | 97.7k | } else if (!MO.isDead()) { |
232 | | // A def that isn't dead can't be moved. |
233 | 39.1k | return false; |
234 | 58.5k | } else if (getHeader()->isLiveIn(Reg)) { |
235 | | // If the reg is live into the loop, we can't hoist an instruction |
236 | | // which would clobber it. |
237 | 0 | return false; |
238 | 0 | } |
239 | 171k | } |
240 | | |
241 | 754k | if (!MO.isUse()) |
242 | 436k | continue; |
243 | | |
244 | 318k | assert(MRI->getVRegDef(Reg) && |
245 | 318k | "Machine instr not mapped for this vreg?!"); |
246 | | |
247 | | // If the loop contains the definition of an operand, then the instruction |
248 | | // isn't loop invariant. |
249 | 318k | if (contains(MRI->getVRegDef(Reg))) |
250 | 223k | return false; |
251 | 318k | } |
252 | | |
253 | | // If we got this far, the instruction is loop invariant! |
254 | 124k | return true; |
255 | 442k | } |
256 | | |
257 | | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
258 | 0 | LLVM_DUMP_METHOD void MachineLoop::dump() const { |
259 | 0 | print(dbgs()); |
260 | 0 | } |
261 | | #endif |