File: | llvm/lib/CodeGen/MachineFunction.cpp |
Warning: | line 992, column 3 Value stored to 'MaxOperand' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- MachineFunction.cpp ------------------------------------------------===// |
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 | // Collect native machine code information for a function. This allows |
10 | // target-specific information about the generated code to be stored with each |
11 | // function. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #include "llvm/CodeGen/MachineFunction.h" |
16 | #include "llvm/ADT/BitVector.h" |
17 | #include "llvm/ADT/DenseMap.h" |
18 | #include "llvm/ADT/DenseSet.h" |
19 | #include "llvm/ADT/STLExtras.h" |
20 | #include "llvm/ADT/SmallString.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | #include "llvm/ADT/StringRef.h" |
23 | #include "llvm/ADT/Twine.h" |
24 | #include "llvm/Analysis/ConstantFolding.h" |
25 | #include "llvm/Analysis/EHPersonalities.h" |
26 | #include "llvm/CodeGen/MachineBasicBlock.h" |
27 | #include "llvm/CodeGen/MachineConstantPool.h" |
28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
29 | #include "llvm/CodeGen/MachineInstr.h" |
30 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
31 | #include "llvm/CodeGen/MachineMemOperand.h" |
32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
34 | #include "llvm/CodeGen/PseudoSourceValue.h" |
35 | #include "llvm/CodeGen/TargetFrameLowering.h" |
36 | #include "llvm/CodeGen/TargetInstrInfo.h" |
37 | #include "llvm/CodeGen/TargetLowering.h" |
38 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
39 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
40 | #include "llvm/CodeGen/WasmEHFuncInfo.h" |
41 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
42 | #include "llvm/Config/llvm-config.h" |
43 | #include "llvm/IR/Attributes.h" |
44 | #include "llvm/IR/BasicBlock.h" |
45 | #include "llvm/IR/Constant.h" |
46 | #include "llvm/IR/DataLayout.h" |
47 | #include "llvm/IR/DebugInfoMetadata.h" |
48 | #include "llvm/IR/DerivedTypes.h" |
49 | #include "llvm/IR/Function.h" |
50 | #include "llvm/IR/GlobalValue.h" |
51 | #include "llvm/IR/Instruction.h" |
52 | #include "llvm/IR/Instructions.h" |
53 | #include "llvm/IR/Metadata.h" |
54 | #include "llvm/IR/Module.h" |
55 | #include "llvm/IR/ModuleSlotTracker.h" |
56 | #include "llvm/IR/Value.h" |
57 | #include "llvm/MC/MCContext.h" |
58 | #include "llvm/MC/MCSymbol.h" |
59 | #include "llvm/MC/SectionKind.h" |
60 | #include "llvm/Support/Casting.h" |
61 | #include "llvm/Support/CommandLine.h" |
62 | #include "llvm/Support/Compiler.h" |
63 | #include "llvm/Support/DOTGraphTraits.h" |
64 | #include "llvm/Support/Debug.h" |
65 | #include "llvm/Support/ErrorHandling.h" |
66 | #include "llvm/Support/GraphWriter.h" |
67 | #include "llvm/Support/raw_ostream.h" |
68 | #include "llvm/Target/TargetMachine.h" |
69 | #include <algorithm> |
70 | #include <cassert> |
71 | #include <cstddef> |
72 | #include <cstdint> |
73 | #include <iterator> |
74 | #include <string> |
75 | #include <type_traits> |
76 | #include <utility> |
77 | #include <vector> |
78 | |
79 | using namespace llvm; |
80 | |
81 | #define DEBUG_TYPE"codegen" "codegen" |
82 | |
83 | static cl::opt<unsigned> AlignAllFunctions( |
84 | "align-all-functions", |
85 | cl::desc("Force the alignment of all functions in log2 format (e.g. 4 " |
86 | "means align on 16B boundaries)."), |
87 | cl::init(0), cl::Hidden); |
88 | |
89 | static const char *getPropertyName(MachineFunctionProperties::Property Prop) { |
90 | using P = MachineFunctionProperties::Property; |
91 | |
92 | switch(Prop) { |
93 | case P::FailedISel: return "FailedISel"; |
94 | case P::IsSSA: return "IsSSA"; |
95 | case P::Legalized: return "Legalized"; |
96 | case P::NoPHIs: return "NoPHIs"; |
97 | case P::NoVRegs: return "NoVRegs"; |
98 | case P::RegBankSelected: return "RegBankSelected"; |
99 | case P::Selected: return "Selected"; |
100 | case P::TracksLiveness: return "TracksLiveness"; |
101 | case P::TiedOpsRewritten: return "TiedOpsRewritten"; |
102 | } |
103 | llvm_unreachable("Invalid machine function property")::llvm::llvm_unreachable_internal("Invalid machine function property" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 103); |
104 | } |
105 | |
106 | // Pin the vtable to this file. |
107 | void MachineFunction::Delegate::anchor() {} |
108 | |
109 | void MachineFunctionProperties::print(raw_ostream &OS) const { |
110 | const char *Separator = ""; |
111 | for (BitVector::size_type I = 0; I < Properties.size(); ++I) { |
112 | if (!Properties[I]) |
113 | continue; |
114 | OS << Separator << getPropertyName(static_cast<Property>(I)); |
115 | Separator = ", "; |
116 | } |
117 | } |
118 | |
119 | //===----------------------------------------------------------------------===// |
120 | // MachineFunction implementation |
121 | //===----------------------------------------------------------------------===// |
122 | |
123 | // Out-of-line virtual method. |
124 | MachineFunctionInfo::~MachineFunctionInfo() = default; |
125 | |
126 | void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { |
127 | MBB->getParent()->DeleteMachineBasicBlock(MBB); |
128 | } |
129 | |
130 | static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, |
131 | const Function &F) { |
132 | if (F.hasFnAttribute(Attribute::StackAlignment)) |
133 | return F.getFnStackAlignment(); |
134 | return STI->getFrameLowering()->getStackAlign().value(); |
135 | } |
136 | |
137 | MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target, |
138 | const TargetSubtargetInfo &STI, |
139 | unsigned FunctionNum, MachineModuleInfo &mmi) |
140 | : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { |
141 | FunctionNumber = FunctionNum; |
142 | init(); |
143 | } |
144 | |
145 | void MachineFunction::handleInsertion(MachineInstr &MI) { |
146 | if (TheDelegate) |
147 | TheDelegate->MF_HandleInsertion(MI); |
148 | } |
149 | |
150 | void MachineFunction::handleRemoval(MachineInstr &MI) { |
151 | if (TheDelegate) |
152 | TheDelegate->MF_HandleRemoval(MI); |
153 | } |
154 | |
155 | void MachineFunction::init() { |
156 | // Assume the function starts in SSA form with correct liveness. |
157 | Properties.set(MachineFunctionProperties::Property::IsSSA); |
158 | Properties.set(MachineFunctionProperties::Property::TracksLiveness); |
159 | if (STI->getRegisterInfo()) |
160 | RegInfo = new (Allocator) MachineRegisterInfo(this); |
161 | else |
162 | RegInfo = nullptr; |
163 | |
164 | MFInfo = nullptr; |
165 | // We can realign the stack if the target supports it and the user hasn't |
166 | // explicitly asked us not to. |
167 | bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && |
168 | !F.hasFnAttribute("no-realign-stack"); |
169 | FrameInfo = new (Allocator) MachineFrameInfo( |
170 | getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, |
171 | /*ForcedRealign=*/CanRealignSP && |
172 | F.hasFnAttribute(Attribute::StackAlignment)); |
173 | |
174 | if (F.hasFnAttribute(Attribute::StackAlignment)) |
175 | FrameInfo->ensureMaxAlignment(*F.getFnStackAlign()); |
176 | |
177 | ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); |
178 | Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); |
179 | |
180 | // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. |
181 | // FIXME: Use Function::hasOptSize(). |
182 | if (!F.hasFnAttribute(Attribute::OptimizeForSize)) |
183 | Alignment = std::max(Alignment, |
184 | STI->getTargetLowering()->getPrefFunctionAlignment()); |
185 | |
186 | if (AlignAllFunctions) |
187 | Alignment = Align(1ULL << AlignAllFunctions); |
188 | |
189 | JumpTableInfo = nullptr; |
190 | |
191 | if (isFuncletEHPersonality(classifyEHPersonality( |
192 | F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { |
193 | WinEHInfo = new (Allocator) WinEHFuncInfo(); |
194 | } |
195 | |
196 | if (isScopedEHPersonality(classifyEHPersonality( |
197 | F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { |
198 | WasmEHInfo = new (Allocator) WasmEHFuncInfo(); |
199 | } |
200 | |
201 | assert(Target.isCompatibleDataLayout(getDataLayout()) &&(static_cast <bool> (Target.isCompatibleDataLayout(getDataLayout ()) && "Can't create a MachineFunction using a Module with a " "Target-incompatible DataLayout attached\n") ? void (0) : __assert_fail ("Target.isCompatibleDataLayout(getDataLayout()) && \"Can't create a MachineFunction using a Module with a \" \"Target-incompatible DataLayout attached\\n\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 203, __extension__ __PRETTY_FUNCTION__)) |
202 | "Can't create a MachineFunction using a Module with a "(static_cast <bool> (Target.isCompatibleDataLayout(getDataLayout ()) && "Can't create a MachineFunction using a Module with a " "Target-incompatible DataLayout attached\n") ? void (0) : __assert_fail ("Target.isCompatibleDataLayout(getDataLayout()) && \"Can't create a MachineFunction using a Module with a \" \"Target-incompatible DataLayout attached\\n\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 203, __extension__ __PRETTY_FUNCTION__)) |
203 | "Target-incompatible DataLayout attached\n")(static_cast <bool> (Target.isCompatibleDataLayout(getDataLayout ()) && "Can't create a MachineFunction using a Module with a " "Target-incompatible DataLayout attached\n") ? void (0) : __assert_fail ("Target.isCompatibleDataLayout(getDataLayout()) && \"Can't create a MachineFunction using a Module with a \" \"Target-incompatible DataLayout attached\\n\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 203, __extension__ __PRETTY_FUNCTION__)); |
204 | |
205 | PSVManager = |
206 | std::make_unique<PseudoSourceValueManager>(*(getSubtarget(). |
207 | getInstrInfo())); |
208 | } |
209 | |
210 | MachineFunction::~MachineFunction() { |
211 | clear(); |
212 | } |
213 | |
214 | void MachineFunction::clear() { |
215 | Properties.reset(); |
216 | // Don't call destructors on MachineInstr and MachineOperand. All of their |
217 | // memory comes from the BumpPtrAllocator which is about to be purged. |
218 | // |
219 | // Do call MachineBasicBlock destructors, it contains std::vectors. |
220 | for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) |
221 | I->Insts.clearAndLeakNodesUnsafely(); |
222 | MBBNumbering.clear(); |
223 | |
224 | InstructionRecycler.clear(Allocator); |
225 | OperandRecycler.clear(Allocator); |
226 | BasicBlockRecycler.clear(Allocator); |
227 | CodeViewAnnotations.clear(); |
228 | VariableDbgInfos.clear(); |
229 | if (RegInfo) { |
230 | RegInfo->~MachineRegisterInfo(); |
231 | Allocator.Deallocate(RegInfo); |
232 | } |
233 | if (MFInfo) { |
234 | MFInfo->~MachineFunctionInfo(); |
235 | Allocator.Deallocate(MFInfo); |
236 | } |
237 | |
238 | FrameInfo->~MachineFrameInfo(); |
239 | Allocator.Deallocate(FrameInfo); |
240 | |
241 | ConstantPool->~MachineConstantPool(); |
242 | Allocator.Deallocate(ConstantPool); |
243 | |
244 | if (JumpTableInfo) { |
245 | JumpTableInfo->~MachineJumpTableInfo(); |
246 | Allocator.Deallocate(JumpTableInfo); |
247 | } |
248 | |
249 | if (WinEHInfo) { |
250 | WinEHInfo->~WinEHFuncInfo(); |
251 | Allocator.Deallocate(WinEHInfo); |
252 | } |
253 | |
254 | if (WasmEHInfo) { |
255 | WasmEHInfo->~WasmEHFuncInfo(); |
256 | Allocator.Deallocate(WasmEHInfo); |
257 | } |
258 | } |
259 | |
260 | const DataLayout &MachineFunction::getDataLayout() const { |
261 | return F.getParent()->getDataLayout(); |
262 | } |
263 | |
264 | /// Get the JumpTableInfo for this function. |
265 | /// If it does not already exist, allocate one. |
266 | MachineJumpTableInfo *MachineFunction:: |
267 | getOrCreateJumpTableInfo(unsigned EntryKind) { |
268 | if (JumpTableInfo) return JumpTableInfo; |
269 | |
270 | JumpTableInfo = new (Allocator) |
271 | MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); |
272 | return JumpTableInfo; |
273 | } |
274 | |
275 | DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const { |
276 | return F.getDenormalMode(FPType); |
277 | } |
278 | |
279 | /// Should we be emitting segmented stack stuff for the function |
280 | bool MachineFunction::shouldSplitStack() const { |
281 | return getFunction().hasFnAttribute("split-stack"); |
282 | } |
283 | |
284 | LLVM_NODISCARD[[clang::warn_unused_result]] unsigned |
285 | MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { |
286 | FrameInstructions.push_back(Inst); |
287 | return FrameInstructions.size() - 1; |
288 | } |
289 | |
290 | /// This discards all of the MachineBasicBlock numbers and recomputes them. |
291 | /// This guarantees that the MBB numbers are sequential, dense, and match the |
292 | /// ordering of the blocks within the function. If a specific MachineBasicBlock |
293 | /// is specified, only that block and those after it are renumbered. |
294 | void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { |
295 | if (empty()) { MBBNumbering.clear(); return; } |
296 | MachineFunction::iterator MBBI, E = end(); |
297 | if (MBB == nullptr) |
298 | MBBI = begin(); |
299 | else |
300 | MBBI = MBB->getIterator(); |
301 | |
302 | // Figure out the block number this should have. |
303 | unsigned BlockNo = 0; |
304 | if (MBBI != begin()) |
305 | BlockNo = std::prev(MBBI)->getNumber() + 1; |
306 | |
307 | for (; MBBI != E; ++MBBI, ++BlockNo) { |
308 | if (MBBI->getNumber() != (int)BlockNo) { |
309 | // Remove use of the old number. |
310 | if (MBBI->getNumber() != -1) { |
311 | assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&(static_cast <bool> (MBBNumbering[MBBI->getNumber()] == &*MBBI && "MBB number mismatch!") ? void (0) : __assert_fail ("MBBNumbering[MBBI->getNumber()] == &*MBBI && \"MBB number mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 312, __extension__ __PRETTY_FUNCTION__)) |
312 | "MBB number mismatch!")(static_cast <bool> (MBBNumbering[MBBI->getNumber()] == &*MBBI && "MBB number mismatch!") ? void (0) : __assert_fail ("MBBNumbering[MBBI->getNumber()] == &*MBBI && \"MBB number mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 312, __extension__ __PRETTY_FUNCTION__)); |
313 | MBBNumbering[MBBI->getNumber()] = nullptr; |
314 | } |
315 | |
316 | // If BlockNo is already taken, set that block's number to -1. |
317 | if (MBBNumbering[BlockNo]) |
318 | MBBNumbering[BlockNo]->setNumber(-1); |
319 | |
320 | MBBNumbering[BlockNo] = &*MBBI; |
321 | MBBI->setNumber(BlockNo); |
322 | } |
323 | } |
324 | |
325 | // Okay, all the blocks are renumbered. If we have compactified the block |
326 | // numbering, shrink MBBNumbering now. |
327 | assert(BlockNo <= MBBNumbering.size() && "Mismatch!")(static_cast <bool> (BlockNo <= MBBNumbering.size() && "Mismatch!") ? void (0) : __assert_fail ("BlockNo <= MBBNumbering.size() && \"Mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 327, __extension__ __PRETTY_FUNCTION__)); |
328 | MBBNumbering.resize(BlockNo); |
329 | } |
330 | |
331 | /// This method iterates over the basic blocks and assigns their IsBeginSection |
332 | /// and IsEndSection fields. This must be called after MBB layout is finalized |
333 | /// and the SectionID's are assigned to MBBs. |
334 | void MachineFunction::assignBeginEndSections() { |
335 | front().setIsBeginSection(); |
336 | auto CurrentSectionID = front().getSectionID(); |
337 | for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { |
338 | if (MBBI->getSectionID() == CurrentSectionID) |
339 | continue; |
340 | MBBI->setIsBeginSection(); |
341 | std::prev(MBBI)->setIsEndSection(); |
342 | CurrentSectionID = MBBI->getSectionID(); |
343 | } |
344 | back().setIsEndSection(); |
345 | } |
346 | |
347 | /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. |
348 | MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, |
349 | const DebugLoc &DL, |
350 | bool NoImplicit) { |
351 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
352 | MachineInstr(*this, MCID, DL, NoImplicit); |
353 | } |
354 | |
355 | /// Create a new MachineInstr which is a copy of the 'Orig' instruction, |
356 | /// identical in all ways except the instruction has no parent, prev, or next. |
357 | MachineInstr * |
358 | MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { |
359 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
360 | MachineInstr(*this, *Orig); |
361 | } |
362 | |
363 | MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, |
364 | MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { |
365 | MachineInstr *FirstClone = nullptr; |
366 | MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); |
367 | while (true) { |
368 | MachineInstr *Cloned = CloneMachineInstr(&*I); |
369 | MBB.insert(InsertBefore, Cloned); |
370 | if (FirstClone == nullptr) { |
371 | FirstClone = Cloned; |
372 | } else { |
373 | Cloned->bundleWithPred(); |
374 | } |
375 | |
376 | if (!I->isBundledWithSucc()) |
377 | break; |
378 | ++I; |
379 | } |
380 | // Copy over call site info to the cloned instruction if needed. If Orig is in |
381 | // a bundle, copyCallSiteInfo takes care of finding the call instruction in |
382 | // the bundle. |
383 | if (Orig.shouldUpdateCallSiteInfo()) |
384 | copyCallSiteInfo(&Orig, FirstClone); |
385 | return *FirstClone; |
386 | } |
387 | |
388 | /// Delete the given MachineInstr. |
389 | /// |
390 | /// This function also serves as the MachineInstr destructor - the real |
391 | /// ~MachineInstr() destructor must be empty. |
392 | void |
393 | MachineFunction::DeleteMachineInstr(MachineInstr *MI) { |
394 | // Verify that a call site info is at valid state. This assertion should |
395 | // be triggered during the implementation of support for the |
396 | // call site info of a new architecture. If the assertion is triggered, |
397 | // back trace will tell where to insert a call to updateCallSiteInfo(). |
398 | assert((!MI->isCandidateForCallSiteEntry() ||(static_cast <bool> ((!MI->isCandidateForCallSiteEntry () || CallSitesInfo.find(MI) == CallSitesInfo.end()) && "Call site info was not updated!") ? void (0) : __assert_fail ("(!MI->isCandidateForCallSiteEntry() || CallSitesInfo.find(MI) == CallSitesInfo.end()) && \"Call site info was not updated!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 400, __extension__ __PRETTY_FUNCTION__)) |
399 | CallSitesInfo.find(MI) == CallSitesInfo.end()) &&(static_cast <bool> ((!MI->isCandidateForCallSiteEntry () || CallSitesInfo.find(MI) == CallSitesInfo.end()) && "Call site info was not updated!") ? void (0) : __assert_fail ("(!MI->isCandidateForCallSiteEntry() || CallSitesInfo.find(MI) == CallSitesInfo.end()) && \"Call site info was not updated!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 400, __extension__ __PRETTY_FUNCTION__)) |
400 | "Call site info was not updated!")(static_cast <bool> ((!MI->isCandidateForCallSiteEntry () || CallSitesInfo.find(MI) == CallSitesInfo.end()) && "Call site info was not updated!") ? void (0) : __assert_fail ("(!MI->isCandidateForCallSiteEntry() || CallSitesInfo.find(MI) == CallSitesInfo.end()) && \"Call site info was not updated!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 400, __extension__ __PRETTY_FUNCTION__)); |
401 | // Strip it for parts. The operand array and the MI object itself are |
402 | // independently recyclable. |
403 | if (MI->Operands) |
404 | deallocateOperandArray(MI->CapOperands, MI->Operands); |
405 | // Don't call ~MachineInstr() which must be trivial anyway because |
406 | // ~MachineFunction drops whole lists of MachineInstrs wihout calling their |
407 | // destructors. |
408 | InstructionRecycler.Deallocate(Allocator, MI); |
409 | } |
410 | |
411 | /// Allocate a new MachineBasicBlock. Use this instead of |
412 | /// `new MachineBasicBlock'. |
413 | MachineBasicBlock * |
414 | MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { |
415 | return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) |
416 | MachineBasicBlock(*this, bb); |
417 | } |
418 | |
419 | /// Delete the given MachineBasicBlock. |
420 | void |
421 | MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { |
422 | assert(MBB->getParent() == this && "MBB parent mismatch!")(static_cast <bool> (MBB->getParent() == this && "MBB parent mismatch!") ? void (0) : __assert_fail ("MBB->getParent() == this && \"MBB parent mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 422, __extension__ __PRETTY_FUNCTION__)); |
423 | // Clean up any references to MBB in jump tables before deleting it. |
424 | if (JumpTableInfo) |
425 | JumpTableInfo->RemoveMBBFromJumpTables(MBB); |
426 | MBB->~MachineBasicBlock(); |
427 | BasicBlockRecycler.Deallocate(Allocator, MBB); |
428 | } |
429 | |
430 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
431 | MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, |
432 | Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, |
433 | SyncScope::ID SSID, AtomicOrdering Ordering, |
434 | AtomicOrdering FailureOrdering) { |
435 | return new (Allocator) |
436 | MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, |
437 | SSID, Ordering, FailureOrdering); |
438 | } |
439 | |
440 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
441 | MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, |
442 | Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, |
443 | SyncScope::ID SSID, AtomicOrdering Ordering, |
444 | AtomicOrdering FailureOrdering) { |
445 | return new (Allocator) |
446 | MachineMemOperand(PtrInfo, f, MemTy, base_alignment, AAInfo, Ranges, SSID, |
447 | Ordering, FailureOrdering); |
448 | } |
449 | |
450 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
451 | const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, uint64_t Size) { |
452 | return new (Allocator) |
453 | MachineMemOperand(PtrInfo, MMO->getFlags(), Size, MMO->getBaseAlign(), |
454 | AAMDNodes(), nullptr, MMO->getSyncScopeID(), |
455 | MMO->getSuccessOrdering(), MMO->getFailureOrdering()); |
456 | } |
457 | |
458 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
459 | const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, LLT Ty) { |
460 | return new (Allocator) |
461 | MachineMemOperand(PtrInfo, MMO->getFlags(), Ty, MMO->getBaseAlign(), |
462 | AAMDNodes(), nullptr, MMO->getSyncScopeID(), |
463 | MMO->getSuccessOrdering(), MMO->getFailureOrdering()); |
464 | } |
465 | |
466 | MachineMemOperand * |
467 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
468 | int64_t Offset, LLT Ty) { |
469 | const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); |
470 | |
471 | // If there is no pointer value, the offset isn't tracked so we need to adjust |
472 | // the base alignment. |
473 | Align Alignment = PtrInfo.V.isNull() |
474 | ? commonAlignment(MMO->getBaseAlign(), Offset) |
475 | : MMO->getBaseAlign(); |
476 | |
477 | // Do not preserve ranges, since we don't necessarily know what the high bits |
478 | // are anymore. |
479 | return new (Allocator) MachineMemOperand( |
480 | PtrInfo.getWithOffset(Offset), MMO->getFlags(), Ty, Alignment, |
481 | MMO->getAAInfo(), nullptr, MMO->getSyncScopeID(), |
482 | MMO->getSuccessOrdering(), MMO->getFailureOrdering()); |
483 | } |
484 | |
485 | MachineMemOperand * |
486 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
487 | const AAMDNodes &AAInfo) { |
488 | MachinePointerInfo MPI = MMO->getValue() ? |
489 | MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : |
490 | MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); |
491 | |
492 | return new (Allocator) MachineMemOperand( |
493 | MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, |
494 | MMO->getRanges(), MMO->getSyncScopeID(), MMO->getSuccessOrdering(), |
495 | MMO->getFailureOrdering()); |
496 | } |
497 | |
498 | MachineMemOperand * |
499 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
500 | MachineMemOperand::Flags Flags) { |
501 | return new (Allocator) MachineMemOperand( |
502 | MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), |
503 | MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), |
504 | MMO->getSuccessOrdering(), MMO->getFailureOrdering()); |
505 | } |
506 | |
507 | MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( |
508 | ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, |
509 | MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { |
510 | return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, |
511 | PostInstrSymbol, HeapAllocMarker); |
512 | } |
513 | |
514 | const char *MachineFunction::createExternalSymbolName(StringRef Name) { |
515 | char *Dest = Allocator.Allocate<char>(Name.size() + 1); |
516 | llvm::copy(Name, Dest); |
517 | Dest[Name.size()] = 0; |
518 | return Dest; |
519 | } |
520 | |
521 | uint32_t *MachineFunction::allocateRegMask() { |
522 | unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); |
523 | unsigned Size = MachineOperand::getRegMaskSize(NumRegs); |
524 | uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); |
525 | memset(Mask, 0, Size * sizeof(Mask[0])); |
526 | return Mask; |
527 | } |
528 | |
529 | ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { |
530 | int* AllocMask = Allocator.Allocate<int>(Mask.size()); |
531 | copy(Mask, AllocMask); |
532 | return {AllocMask, Mask.size()}; |
533 | } |
534 | |
535 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
536 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineFunction::dump() const { |
537 | print(dbgs()); |
538 | } |
539 | #endif |
540 | |
541 | StringRef MachineFunction::getName() const { |
542 | return getFunction().getName(); |
543 | } |
544 | |
545 | void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { |
546 | OS << "# Machine code for function " << getName() << ": "; |
547 | getProperties().print(OS); |
548 | OS << '\n'; |
549 | |
550 | // Print Frame Information |
551 | FrameInfo->print(*this, OS); |
552 | |
553 | // Print JumpTable Information |
554 | if (JumpTableInfo) |
555 | JumpTableInfo->print(OS); |
556 | |
557 | // Print Constant Pool |
558 | ConstantPool->print(OS); |
559 | |
560 | const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); |
561 | |
562 | if (RegInfo && !RegInfo->livein_empty()) { |
563 | OS << "Function Live Ins: "; |
564 | for (MachineRegisterInfo::livein_iterator |
565 | I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { |
566 | OS << printReg(I->first, TRI); |
567 | if (I->second) |
568 | OS << " in " << printReg(I->second, TRI); |
569 | if (std::next(I) != E) |
570 | OS << ", "; |
571 | } |
572 | OS << '\n'; |
573 | } |
574 | |
575 | ModuleSlotTracker MST(getFunction().getParent()); |
576 | MST.incorporateFunction(getFunction()); |
577 | for (const auto &BB : *this) { |
578 | OS << '\n'; |
579 | // If we print the whole function, print it at its most verbose level. |
580 | BB.print(OS, MST, Indexes, /*IsStandalone=*/true); |
581 | } |
582 | |
583 | OS << "\n# End machine code for function " << getName() << ".\n\n"; |
584 | } |
585 | |
586 | /// True if this function needs frame moves for debug or exceptions. |
587 | bool MachineFunction::needsFrameMoves() const { |
588 | return getMMI().hasDebugInfo() || |
589 | getTarget().Options.ForceDwarfFrameSection || |
590 | F.needsUnwindTableEntry(); |
591 | } |
592 | |
593 | namespace llvm { |
594 | |
595 | template<> |
596 | struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { |
597 | DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
598 | |
599 | static std::string getGraphName(const MachineFunction *F) { |
600 | return ("CFG for '" + F->getName() + "' function").str(); |
601 | } |
602 | |
603 | std::string getNodeLabel(const MachineBasicBlock *Node, |
604 | const MachineFunction *Graph) { |
605 | std::string OutStr; |
606 | { |
607 | raw_string_ostream OSS(OutStr); |
608 | |
609 | if (isSimple()) { |
610 | OSS << printMBBReference(*Node); |
611 | if (const BasicBlock *BB = Node->getBasicBlock()) |
612 | OSS << ": " << BB->getName(); |
613 | } else |
614 | Node->print(OSS); |
615 | } |
616 | |
617 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
618 | |
619 | // Process string output to make it nicer... |
620 | for (unsigned i = 0; i != OutStr.length(); ++i) |
621 | if (OutStr[i] == '\n') { // Left justify |
622 | OutStr[i] = '\\'; |
623 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
624 | } |
625 | return OutStr; |
626 | } |
627 | }; |
628 | |
629 | } // end namespace llvm |
630 | |
631 | void MachineFunction::viewCFG() const |
632 | { |
633 | #ifndef NDEBUG |
634 | ViewGraph(this, "mf" + getName()); |
635 | #else |
636 | errs() << "MachineFunction::viewCFG is only available in debug builds on " |
637 | << "systems with Graphviz or gv!\n"; |
638 | #endif // NDEBUG |
639 | } |
640 | |
641 | void MachineFunction::viewCFGOnly() const |
642 | { |
643 | #ifndef NDEBUG |
644 | ViewGraph(this, "mf" + getName(), true); |
645 | #else |
646 | errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " |
647 | << "systems with Graphviz or gv!\n"; |
648 | #endif // NDEBUG |
649 | } |
650 | |
651 | /// Add the specified physical register as a live-in value and |
652 | /// create a corresponding virtual register for it. |
653 | Register MachineFunction::addLiveIn(MCRegister PReg, |
654 | const TargetRegisterClass *RC) { |
655 | MachineRegisterInfo &MRI = getRegInfo(); |
656 | Register VReg = MRI.getLiveInVirtReg(PReg); |
657 | if (VReg) { |
658 | const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); |
659 | (void)VRegRC; |
660 | // A physical register can be added several times. |
661 | // Between two calls, the register class of the related virtual register |
662 | // may have been constrained to match some operation constraints. |
663 | // In that case, check that the current register class includes the |
664 | // physical register and is a sub class of the specified RC. |
665 | assert((VRegRC == RC || (VRegRC->contains(PReg) &&(static_cast <bool> ((VRegRC == RC || (VRegRC->contains (PReg) && RC->hasSubClassEq(VRegRC))) && "Register class mismatch!" ) ? void (0) : __assert_fail ("(VRegRC == RC || (VRegRC->contains(PReg) && RC->hasSubClassEq(VRegRC))) && \"Register class mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 667, __extension__ __PRETTY_FUNCTION__)) |
666 | RC->hasSubClassEq(VRegRC))) &&(static_cast <bool> ((VRegRC == RC || (VRegRC->contains (PReg) && RC->hasSubClassEq(VRegRC))) && "Register class mismatch!" ) ? void (0) : __assert_fail ("(VRegRC == RC || (VRegRC->contains(PReg) && RC->hasSubClassEq(VRegRC))) && \"Register class mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 667, __extension__ __PRETTY_FUNCTION__)) |
667 | "Register class mismatch!")(static_cast <bool> ((VRegRC == RC || (VRegRC->contains (PReg) && RC->hasSubClassEq(VRegRC))) && "Register class mismatch!" ) ? void (0) : __assert_fail ("(VRegRC == RC || (VRegRC->contains(PReg) && RC->hasSubClassEq(VRegRC))) && \"Register class mismatch!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 667, __extension__ __PRETTY_FUNCTION__)); |
668 | return VReg; |
669 | } |
670 | VReg = MRI.createVirtualRegister(RC); |
671 | MRI.addLiveIn(PReg, VReg); |
672 | return VReg; |
673 | } |
674 | |
675 | /// Return the MCSymbol for the specified non-empty jump table. |
676 | /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a |
677 | /// normal 'L' label is returned. |
678 | MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, |
679 | bool isLinkerPrivate) const { |
680 | const DataLayout &DL = getDataLayout(); |
681 | assert(JumpTableInfo && "No jump tables")(static_cast <bool> (JumpTableInfo && "No jump tables" ) ? void (0) : __assert_fail ("JumpTableInfo && \"No jump tables\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 681, __extension__ __PRETTY_FUNCTION__)); |
682 | assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!")(static_cast <bool> (JTI < JumpTableInfo->getJumpTables ().size() && "Invalid JTI!") ? void (0) : __assert_fail ("JTI < JumpTableInfo->getJumpTables().size() && \"Invalid JTI!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 682, __extension__ __PRETTY_FUNCTION__)); |
683 | |
684 | StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() |
685 | : DL.getPrivateGlobalPrefix(); |
686 | SmallString<60> Name; |
687 | raw_svector_ostream(Name) |
688 | << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; |
689 | return Ctx.getOrCreateSymbol(Name); |
690 | } |
691 | |
692 | /// Return a function-local symbol to represent the PIC base. |
693 | MCSymbol *MachineFunction::getPICBaseSymbol() const { |
694 | const DataLayout &DL = getDataLayout(); |
695 | return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + |
696 | Twine(getFunctionNumber()) + "$pb"); |
697 | } |
698 | |
699 | /// \name Exception Handling |
700 | /// \{ |
701 | |
702 | LandingPadInfo & |
703 | MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { |
704 | unsigned N = LandingPads.size(); |
705 | for (unsigned i = 0; i < N; ++i) { |
706 | LandingPadInfo &LP = LandingPads[i]; |
707 | if (LP.LandingPadBlock == LandingPad) |
708 | return LP; |
709 | } |
710 | |
711 | LandingPads.push_back(LandingPadInfo(LandingPad)); |
712 | return LandingPads[N]; |
713 | } |
714 | |
715 | void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, |
716 | MCSymbol *BeginLabel, MCSymbol *EndLabel) { |
717 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
718 | LP.BeginLabels.push_back(BeginLabel); |
719 | LP.EndLabels.push_back(EndLabel); |
720 | } |
721 | |
722 | MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { |
723 | MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); |
724 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
725 | LP.LandingPadLabel = LandingPadLabel; |
726 | |
727 | const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); |
728 | if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { |
729 | if (const auto *PF = |
730 | dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) |
731 | getMMI().addPersonality(PF); |
732 | |
733 | if (LPI->isCleanup()) |
734 | addCleanup(LandingPad); |
735 | |
736 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% |
737 | // correct, but we need to do it this way because of how the DWARF EH |
738 | // emitter processes the clauses. |
739 | for (unsigned I = LPI->getNumClauses(); I != 0; --I) { |
740 | Value *Val = LPI->getClause(I - 1); |
741 | if (LPI->isCatch(I - 1)) { |
742 | addCatchTypeInfo(LandingPad, |
743 | dyn_cast<GlobalValue>(Val->stripPointerCasts())); |
744 | } else { |
745 | // Add filters in a list. |
746 | auto *CVal = cast<Constant>(Val); |
747 | SmallVector<const GlobalValue *, 4> FilterList; |
748 | for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); |
749 | II != IE; ++II) |
750 | FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); |
751 | |
752 | addFilterTypeInfo(LandingPad, FilterList); |
753 | } |
754 | } |
755 | |
756 | } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { |
757 | for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { |
758 | Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); |
759 | addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); |
760 | } |
761 | |
762 | } else { |
763 | assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!")(static_cast <bool> (isa<CleanupPadInst>(FirstI) && "Invalid landingpad!") ? void (0) : __assert_fail ("isa<CleanupPadInst>(FirstI) && \"Invalid landingpad!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 763, __extension__ __PRETTY_FUNCTION__)); |
764 | } |
765 | |
766 | return LandingPadLabel; |
767 | } |
768 | |
769 | void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, |
770 | ArrayRef<const GlobalValue *> TyInfo) { |
771 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
772 | for (unsigned N = TyInfo.size(); N; --N) |
773 | LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); |
774 | } |
775 | |
776 | void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, |
777 | ArrayRef<const GlobalValue *> TyInfo) { |
778 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
779 | std::vector<unsigned> IdsInFilter(TyInfo.size()); |
780 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
781 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
782 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
783 | } |
784 | |
785 | void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, |
786 | bool TidyIfNoBeginLabels) { |
787 | for (unsigned i = 0; i != LandingPads.size(); ) { |
788 | LandingPadInfo &LandingPad = LandingPads[i]; |
789 | if (LandingPad.LandingPadLabel && |
790 | !LandingPad.LandingPadLabel->isDefined() && |
791 | (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) |
792 | LandingPad.LandingPadLabel = nullptr; |
793 | |
794 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
795 | // "nounwind" case. |
796 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
797 | LandingPads.erase(LandingPads.begin() + i); |
798 | continue; |
799 | } |
800 | |
801 | if (TidyIfNoBeginLabels) { |
802 | for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { |
803 | MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; |
804 | MCSymbol *EndLabel = LandingPad.EndLabels[j]; |
805 | if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && |
806 | (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) |
807 | continue; |
808 | |
809 | LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); |
810 | LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); |
811 | --j; |
812 | --e; |
813 | } |
814 | |
815 | // Remove landing pads with no try-ranges. |
816 | if (LandingPads[i].BeginLabels.empty()) { |
817 | LandingPads.erase(LandingPads.begin() + i); |
818 | continue; |
819 | } |
820 | } |
821 | |
822 | // If there is no landing pad, ensure that the list of typeids is empty. |
823 | // If the only typeid is a cleanup, this is the same as having no typeids. |
824 | if (!LandingPad.LandingPadBlock || |
825 | (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) |
826 | LandingPad.TypeIds.clear(); |
827 | ++i; |
828 | } |
829 | } |
830 | |
831 | void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { |
832 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
833 | LP.TypeIds.push_back(0); |
834 | } |
835 | |
836 | void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, |
837 | const Function *Filter, |
838 | const BlockAddress *RecoverBA) { |
839 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
840 | SEHHandler Handler; |
841 | Handler.FilterOrFinally = Filter; |
842 | Handler.RecoverBA = RecoverBA; |
843 | LP.SEHHandlers.push_back(Handler); |
844 | } |
845 | |
846 | void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, |
847 | const Function *Cleanup) { |
848 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
849 | SEHHandler Handler; |
850 | Handler.FilterOrFinally = Cleanup; |
851 | Handler.RecoverBA = nullptr; |
852 | LP.SEHHandlers.push_back(Handler); |
853 | } |
854 | |
855 | void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, |
856 | ArrayRef<unsigned> Sites) { |
857 | LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); |
858 | } |
859 | |
860 | unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { |
861 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
862 | if (TypeInfos[i] == TI) return i + 1; |
863 | |
864 | TypeInfos.push_back(TI); |
865 | return TypeInfos.size(); |
866 | } |
867 | |
868 | int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { |
869 | // If the new filter coincides with the tail of an existing filter, then |
870 | // re-use the existing filter. Folding filters more than this requires |
871 | // re-ordering filters and/or their elements - probably not worth it. |
872 | for (unsigned i : FilterEnds) { |
873 | unsigned j = TyIds.size(); |
874 | |
875 | while (i && j) |
876 | if (FilterIds[--i] != TyIds[--j]) |
877 | goto try_next; |
878 | |
879 | if (!j) |
880 | // The new filter coincides with range [i, end) of the existing filter. |
881 | return -(1 + i); |
882 | |
883 | try_next:; |
884 | } |
885 | |
886 | // Add the new filter. |
887 | int FilterID = -(1 + FilterIds.size()); |
888 | FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); |
889 | llvm::append_range(FilterIds, TyIds); |
890 | FilterEnds.push_back(FilterIds.size()); |
891 | FilterIds.push_back(0); // terminator |
892 | return FilterID; |
893 | } |
894 | |
895 | MachineFunction::CallSiteInfoMap::iterator |
896 | MachineFunction::getCallSiteInfo(const MachineInstr *MI) { |
897 | assert(MI->isCandidateForCallSiteEntry() &&(static_cast <bool> (MI->isCandidateForCallSiteEntry () && "Call site info refers only to call (MI) candidates" ) ? void (0) : __assert_fail ("MI->isCandidateForCallSiteEntry() && \"Call site info refers only to call (MI) candidates\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 898, __extension__ __PRETTY_FUNCTION__)) |
898 | "Call site info refers only to call (MI) candidates")(static_cast <bool> (MI->isCandidateForCallSiteEntry () && "Call site info refers only to call (MI) candidates" ) ? void (0) : __assert_fail ("MI->isCandidateForCallSiteEntry() && \"Call site info refers only to call (MI) candidates\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 898, __extension__ __PRETTY_FUNCTION__)); |
899 | |
900 | if (!Target.Options.EmitCallSiteInfo) |
901 | return CallSitesInfo.end(); |
902 | return CallSitesInfo.find(MI); |
903 | } |
904 | |
905 | /// Return the call machine instruction or find a call within bundle. |
906 | static const MachineInstr *getCallInstr(const MachineInstr *MI) { |
907 | if (!MI->isBundle()) |
908 | return MI; |
909 | |
910 | for (auto &BMI : make_range(getBundleStart(MI->getIterator()), |
911 | getBundleEnd(MI->getIterator()))) |
912 | if (BMI.isCandidateForCallSiteEntry()) |
913 | return &BMI; |
914 | |
915 | llvm_unreachable("Unexpected bundle without a call site candidate")::llvm::llvm_unreachable_internal("Unexpected bundle without a call site candidate" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 915); |
916 | } |
917 | |
918 | void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { |
919 | assert(MI->shouldUpdateCallSiteInfo() &&(static_cast <bool> (MI->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles" ) ? void (0) : __assert_fail ("MI->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 921, __extension__ __PRETTY_FUNCTION__)) |
920 | "Call site info refers only to call (MI) candidates or "(static_cast <bool> (MI->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles" ) ? void (0) : __assert_fail ("MI->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 921, __extension__ __PRETTY_FUNCTION__)) |
921 | "candidates inside bundles")(static_cast <bool> (MI->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles" ) ? void (0) : __assert_fail ("MI->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 921, __extension__ __PRETTY_FUNCTION__)); |
922 | |
923 | const MachineInstr *CallMI = getCallInstr(MI); |
924 | CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); |
925 | if (CSIt == CallSitesInfo.end()) |
926 | return; |
927 | CallSitesInfo.erase(CSIt); |
928 | } |
929 | |
930 | void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, |
931 | const MachineInstr *New) { |
932 | assert(Old->shouldUpdateCallSiteInfo() &&(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 934, __extension__ __PRETTY_FUNCTION__)) |
933 | "Call site info refers only to call (MI) candidates or "(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 934, __extension__ __PRETTY_FUNCTION__)) |
934 | "candidates inside bundles")(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 934, __extension__ __PRETTY_FUNCTION__)); |
935 | |
936 | if (!New->isCandidateForCallSiteEntry()) |
937 | return eraseCallSiteInfo(Old); |
938 | |
939 | const MachineInstr *OldCallMI = getCallInstr(Old); |
940 | CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); |
941 | if (CSIt == CallSitesInfo.end()) |
942 | return; |
943 | |
944 | CallSiteInfo CSInfo = CSIt->second; |
945 | CallSitesInfo[New] = CSInfo; |
946 | } |
947 | |
948 | void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, |
949 | const MachineInstr *New) { |
950 | assert(Old->shouldUpdateCallSiteInfo() &&(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 952, __extension__ __PRETTY_FUNCTION__)) |
951 | "Call site info refers only to call (MI) candidates or "(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 952, __extension__ __PRETTY_FUNCTION__)) |
952 | "candidates inside bundles")(static_cast <bool> (Old->shouldUpdateCallSiteInfo() && "Call site info refers only to call (MI) candidates or " "candidates inside bundles") ? void (0) : __assert_fail ("Old->shouldUpdateCallSiteInfo() && \"Call site info refers only to call (MI) candidates or \" \"candidates inside bundles\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 952, __extension__ __PRETTY_FUNCTION__)); |
953 | |
954 | if (!New->isCandidateForCallSiteEntry()) |
955 | return eraseCallSiteInfo(Old); |
956 | |
957 | const MachineInstr *OldCallMI = getCallInstr(Old); |
958 | CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); |
959 | if (CSIt == CallSitesInfo.end()) |
960 | return; |
961 | |
962 | CallSiteInfo CSInfo = std::move(CSIt->second); |
963 | CallSitesInfo.erase(CSIt); |
964 | CallSitesInfo[New] = CSInfo; |
965 | } |
966 | |
967 | void MachineFunction::setDebugInstrNumberingCount(unsigned Num) { |
968 | DebugInstrNumberingCount = Num; |
969 | } |
970 | |
971 | void MachineFunction::makeDebugValueSubstitution(DebugInstrOperandPair A, |
972 | DebugInstrOperandPair B, |
973 | unsigned Subreg) { |
974 | // Catch any accidental self-loops. |
975 | assert(A.first != B.first)(static_cast <bool> (A.first != B.first) ? void (0) : __assert_fail ("A.first != B.first", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 975, __extension__ __PRETTY_FUNCTION__)); |
976 | DebugValueSubstitutions.push_back({A, B, Subreg}); |
977 | } |
978 | |
979 | void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old, |
980 | MachineInstr &New, |
981 | unsigned MaxOperand) { |
982 | // If the Old instruction wasn't tracked at all, there is no work to do. |
983 | unsigned OldInstrNum = Old.peekDebugInstrNum(); |
984 | if (!OldInstrNum) |
985 | return; |
986 | |
987 | // Iterate over all operands looking for defs to create substitutions for. |
988 | // Avoid creating new instr numbers unless we create a new substitution. |
989 | // While this has no functional effect, it risks confusing someone reading |
990 | // MIR output. |
991 | // Examine all the operands, or the first N specified by the caller. |
992 | MaxOperand = std::min(MaxOperand, Old.getNumOperands()); |
Value stored to 'MaxOperand' is never read | |
993 | for (unsigned int I = 0; I < Old.getNumOperands(); ++I) { |
994 | const auto &OldMO = Old.getOperand(I); |
995 | auto &NewMO = New.getOperand(I); |
996 | (void)NewMO; |
997 | |
998 | if (!OldMO.isReg() || !OldMO.isDef()) |
999 | continue; |
1000 | assert(NewMO.isDef())(static_cast <bool> (NewMO.isDef()) ? void (0) : __assert_fail ("NewMO.isDef()", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1000, __extension__ __PRETTY_FUNCTION__)); |
1001 | |
1002 | unsigned NewInstrNum = New.getDebugInstrNum(); |
1003 | makeDebugValueSubstitution(std::make_pair(OldInstrNum, I), |
1004 | std::make_pair(NewInstrNum, I)); |
1005 | } |
1006 | } |
1007 | |
1008 | auto MachineFunction::salvageCopySSA(MachineInstr &MI) |
1009 | -> DebugInstrOperandPair { |
1010 | MachineRegisterInfo &MRI = getRegInfo(); |
1011 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
1012 | const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); |
1013 | |
1014 | // Chase the value read by a copy-like instruction back to the instruction |
1015 | // that ultimately _defines_ that value. This may pass: |
1016 | // * Through multiple intermediate copies, including subregister moves / |
1017 | // copies, |
1018 | // * Copies from physical registers that must then be traced back to the |
1019 | // defining instruction, |
1020 | // * Or, physical registers may be live-in to (only) the entry block, which |
1021 | // requires a DBG_PHI to be created. |
1022 | // We can pursue this problem in that order: trace back through copies, |
1023 | // optionally through a physical register, to a defining instruction. We |
1024 | // should never move from physreg to vreg. As we're still in SSA form, no need |
1025 | // to worry about partial definitions of registers. |
1026 | |
1027 | // Helper lambda to interpret a copy-like instruction. Takes instruction, |
1028 | // returns the register read and any subregister identifying which part is |
1029 | // read. |
1030 | auto GetRegAndSubreg = |
1031 | [&](const MachineInstr &Cpy) -> std::pair<Register, unsigned> { |
1032 | Register NewReg, OldReg; |
1033 | unsigned SubReg; |
1034 | if (Cpy.isCopy()) { |
1035 | OldReg = Cpy.getOperand(0).getReg(); |
1036 | NewReg = Cpy.getOperand(1).getReg(); |
1037 | SubReg = Cpy.getOperand(1).getSubReg(); |
1038 | } else if (Cpy.isSubregToReg()) { |
1039 | OldReg = Cpy.getOperand(0).getReg(); |
1040 | NewReg = Cpy.getOperand(2).getReg(); |
1041 | SubReg = Cpy.getOperand(3).getImm(); |
1042 | } else { |
1043 | auto CopyDetails = *TII.isCopyInstr(Cpy); |
1044 | const MachineOperand &Src = *CopyDetails.Source; |
1045 | const MachineOperand &Dest = *CopyDetails.Destination; |
1046 | OldReg = Dest.getReg(); |
1047 | NewReg = Src.getReg(); |
1048 | SubReg = Src.getSubReg(); |
1049 | } |
1050 | |
1051 | return {NewReg, SubReg}; |
1052 | }; |
1053 | |
1054 | // First seek either the defining instruction, or a copy from a physreg. |
1055 | // During search, the current state is the current copy instruction, and which |
1056 | // register we've read. Accumulate qualifying subregisters into SubregsSeen; |
1057 | // deal with those later. |
1058 | auto State = GetRegAndSubreg(MI); |
1059 | auto CurInst = MI.getIterator(); |
1060 | SmallVector<unsigned, 4> SubregsSeen; |
1061 | while (true) { |
1062 | // If we've found a copy from a physreg, first portion of search is over. |
1063 | if (!State.first.isVirtual()) |
1064 | break; |
1065 | |
1066 | // Record any subregister qualifier. |
1067 | if (State.second) |
1068 | SubregsSeen.push_back(State.second); |
1069 | |
1070 | assert(MRI.hasOneDef(State.first))(static_cast <bool> (MRI.hasOneDef(State.first)) ? void (0) : __assert_fail ("MRI.hasOneDef(State.first)", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1070, __extension__ __PRETTY_FUNCTION__)); |
1071 | MachineInstr &Inst = *MRI.def_begin(State.first)->getParent(); |
1072 | CurInst = Inst.getIterator(); |
1073 | |
1074 | // Any non-copy instruction is the defining instruction we're seeking. |
1075 | if (!Inst.isCopyLike() && !TII.isCopyInstr(Inst)) |
1076 | break; |
1077 | State = GetRegAndSubreg(Inst); |
1078 | }; |
1079 | |
1080 | // Helper lambda to apply additional subregister substitutions to a known |
1081 | // instruction/operand pair. Adds new (fake) substitutions so that we can |
1082 | // record the subregister. FIXME: this isn't very space efficient if multiple |
1083 | // values are tracked back through the same copies; cache something later. |
1084 | auto ApplySubregisters = |
1085 | [&](DebugInstrOperandPair P) -> DebugInstrOperandPair { |
1086 | for (unsigned Subreg : reverse(SubregsSeen)) { |
1087 | // Fetch a new instruction number, not attached to an actual instruction. |
1088 | unsigned NewInstrNumber = getNewDebugInstrNum(); |
1089 | // Add a substitution from the "new" number to the known one, with a |
1090 | // qualifying subreg. |
1091 | makeDebugValueSubstitution({NewInstrNumber, 0}, P, Subreg); |
1092 | // Return the new number; to find the underlying value, consumers need to |
1093 | // deal with the qualifying subreg. |
1094 | P = {NewInstrNumber, 0}; |
1095 | } |
1096 | return P; |
1097 | }; |
1098 | |
1099 | // If we managed to find the defining instruction after COPYs, return an |
1100 | // instruction / operand pair after adding subregister qualifiers. |
1101 | if (State.first.isVirtual()) { |
1102 | // Virtual register def -- we can just look up where this happens. |
1103 | MachineInstr *Inst = MRI.def_begin(State.first)->getParent(); |
1104 | for (auto &MO : Inst->operands()) { |
1105 | if (!MO.isReg() || !MO.isDef() || MO.getReg() != State.first) |
1106 | continue; |
1107 | return ApplySubregisters( |
1108 | {Inst->getDebugInstrNum(), Inst->getOperandNo(&MO)}); |
1109 | } |
1110 | |
1111 | llvm_unreachable("Vreg def with no corresponding operand?")::llvm::llvm_unreachable_internal("Vreg def with no corresponding operand?" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1111); |
1112 | } |
1113 | |
1114 | // Our search ended in a copy from a physreg: walk back up the function |
1115 | // looking for whatever defines the physreg. |
1116 | assert(CurInst->isCopyLike() || TII.isCopyInstr(*CurInst))(static_cast <bool> (CurInst->isCopyLike() || TII.isCopyInstr (*CurInst)) ? void (0) : __assert_fail ("CurInst->isCopyLike() || TII.isCopyInstr(*CurInst)" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1116, __extension__ __PRETTY_FUNCTION__)); |
1117 | State = GetRegAndSubreg(*CurInst); |
1118 | Register RegToSeek = State.first; |
1119 | |
1120 | auto RMII = CurInst->getReverseIterator(); |
1121 | auto PrevInstrs = make_range(RMII, CurInst->getParent()->instr_rend()); |
1122 | for (auto &ToExamine : PrevInstrs) { |
1123 | for (auto &MO : ToExamine.operands()) { |
1124 | // Test for operand that defines something aliasing RegToSeek. |
1125 | if (!MO.isReg() || !MO.isDef() || |
1126 | !TRI.regsOverlap(RegToSeek, MO.getReg())) |
1127 | continue; |
1128 | |
1129 | return ApplySubregisters( |
1130 | {ToExamine.getDebugInstrNum(), ToExamine.getOperandNo(&MO)}); |
1131 | } |
1132 | } |
1133 | |
1134 | // We reached the start of the block before finding a defining instruction. |
1135 | // It must be an argument: assert that this is the entry block, and produce |
1136 | // a DBG_PHI. |
1137 | assert(!State.first.isVirtual())(static_cast <bool> (!State.first.isVirtual()) ? void ( 0) : __assert_fail ("!State.first.isVirtual()", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1137, __extension__ __PRETTY_FUNCTION__)); |
1138 | MachineBasicBlock &TargetBB = *CurInst->getParent(); |
1139 | assert(&*TargetBB.getParent()->begin() == &TargetBB)(static_cast <bool> (&*TargetBB.getParent()->begin () == &TargetBB) ? void (0) : __assert_fail ("&*TargetBB.getParent()->begin() == &TargetBB" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1139, __extension__ __PRETTY_FUNCTION__)); |
1140 | |
1141 | // Create DBG_PHI for specified physreg. |
1142 | auto Builder = BuildMI(TargetBB, TargetBB.getFirstNonPHI(), DebugLoc(), |
1143 | TII.get(TargetOpcode::DBG_PHI)); |
1144 | Builder.addReg(State.first, RegState::Debug); |
1145 | unsigned NewNum = getNewDebugInstrNum(); |
1146 | Builder.addImm(NewNum); |
1147 | return ApplySubregisters({NewNum, 0u}); |
1148 | } |
1149 | |
1150 | void MachineFunction::finalizeDebugInstrRefs() { |
1151 | auto *TII = getSubtarget().getInstrInfo(); |
1152 | |
1153 | auto MakeDbgValue = [&](MachineInstr &MI) { |
1154 | const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_VALUE); |
1155 | MI.setDesc(RefII); |
1156 | MI.getOperand(1).ChangeToRegister(0, false); |
1157 | MI.getOperand(0).setIsDebug(); |
1158 | }; |
1159 | |
1160 | if (!getTarget().Options.ValueTrackingVariableLocations) |
1161 | return; |
1162 | |
1163 | for (auto &MBB : *this) { |
1164 | for (auto &MI : MBB) { |
1165 | if (!MI.isDebugRef() || !MI.getOperand(0).isReg()) |
1166 | continue; |
1167 | |
1168 | Register Reg = MI.getOperand(0).getReg(); |
1169 | |
1170 | // Some vregs can be deleted as redundant in the meantime. Mark those |
1171 | // as DBG_VALUE $noreg. |
1172 | if (Reg == 0) { |
1173 | MakeDbgValue(MI); |
1174 | continue; |
1175 | } |
1176 | |
1177 | assert(Reg.isVirtual())(static_cast <bool> (Reg.isVirtual()) ? void (0) : __assert_fail ("Reg.isVirtual()", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1177, __extension__ __PRETTY_FUNCTION__)); |
1178 | MachineInstr &DefMI = *RegInfo->def_instr_begin(Reg); |
1179 | assert(RegInfo->hasOneDef(Reg))(static_cast <bool> (RegInfo->hasOneDef(Reg)) ? void (0) : __assert_fail ("RegInfo->hasOneDef(Reg)", "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1179, __extension__ __PRETTY_FUNCTION__)); |
1180 | |
1181 | // If we've found a copy-like instruction, follow it back to the |
1182 | // instruction that defines the source value, see salvageCopySSA docs |
1183 | // for why this is important. |
1184 | if (DefMI.isCopyLike() || TII->isCopyInstr(DefMI)) { |
1185 | auto Result = salvageCopySSA(DefMI); |
1186 | MI.getOperand(0).ChangeToImmediate(Result.first); |
1187 | MI.getOperand(1).setImm(Result.second); |
1188 | } else { |
1189 | // Otherwise, identify the operand number that the VReg refers to. |
1190 | unsigned OperandIdx = 0; |
1191 | for (const auto &MO : DefMI.operands()) { |
1192 | if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) |
1193 | break; |
1194 | ++OperandIdx; |
1195 | } |
1196 | assert(OperandIdx < DefMI.getNumOperands())(static_cast <bool> (OperandIdx < DefMI.getNumOperands ()) ? void (0) : __assert_fail ("OperandIdx < DefMI.getNumOperands()" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1196, __extension__ __PRETTY_FUNCTION__)); |
1197 | |
1198 | // Morph this instr ref to point at the given instruction and operand. |
1199 | unsigned ID = DefMI.getDebugInstrNum(); |
1200 | MI.getOperand(0).ChangeToImmediate(ID); |
1201 | MI.getOperand(1).setImm(OperandIdx); |
1202 | } |
1203 | } |
1204 | } |
1205 | } |
1206 | |
1207 | /// \} |
1208 | |
1209 | //===----------------------------------------------------------------------===// |
1210 | // MachineJumpTableInfo implementation |
1211 | //===----------------------------------------------------------------------===// |
1212 | |
1213 | /// Return the size of each entry in the jump table. |
1214 | unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { |
1215 | // The size of a jump table entry is 4 bytes unless the entry is just the |
1216 | // address of a block, in which case it is the pointer size. |
1217 | switch (getEntryKind()) { |
1218 | case MachineJumpTableInfo::EK_BlockAddress: |
1219 | return TD.getPointerSize(); |
1220 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
1221 | return 8; |
1222 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
1223 | case MachineJumpTableInfo::EK_LabelDifference32: |
1224 | case MachineJumpTableInfo::EK_Custom32: |
1225 | return 4; |
1226 | case MachineJumpTableInfo::EK_Inline: |
1227 | return 0; |
1228 | } |
1229 | llvm_unreachable("Unknown jump table encoding!")::llvm::llvm_unreachable_internal("Unknown jump table encoding!" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1229); |
1230 | } |
1231 | |
1232 | /// Return the alignment of each entry in the jump table. |
1233 | unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { |
1234 | // The alignment of a jump table entry is the alignment of int32 unless the |
1235 | // entry is just the address of a block, in which case it is the pointer |
1236 | // alignment. |
1237 | switch (getEntryKind()) { |
1238 | case MachineJumpTableInfo::EK_BlockAddress: |
1239 | return TD.getPointerABIAlignment(0).value(); |
1240 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
1241 | return TD.getABIIntegerTypeAlignment(64).value(); |
1242 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
1243 | case MachineJumpTableInfo::EK_LabelDifference32: |
1244 | case MachineJumpTableInfo::EK_Custom32: |
1245 | return TD.getABIIntegerTypeAlignment(32).value(); |
1246 | case MachineJumpTableInfo::EK_Inline: |
1247 | return 1; |
1248 | } |
1249 | llvm_unreachable("Unknown jump table encoding!")::llvm::llvm_unreachable_internal("Unknown jump table encoding!" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1249); |
1250 | } |
1251 | |
1252 | /// Create a new jump table entry in the jump table info. |
1253 | unsigned MachineJumpTableInfo::createJumpTableIndex( |
1254 | const std::vector<MachineBasicBlock*> &DestBBs) { |
1255 | assert(!DestBBs.empty() && "Cannot create an empty jump table!")(static_cast <bool> (!DestBBs.empty() && "Cannot create an empty jump table!" ) ? void (0) : __assert_fail ("!DestBBs.empty() && \"Cannot create an empty jump table!\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1255, __extension__ __PRETTY_FUNCTION__)); |
1256 | JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
1257 | return JumpTables.size()-1; |
1258 | } |
1259 | |
1260 | /// If Old is the target of any jump tables, update the jump tables to branch |
1261 | /// to New instead. |
1262 | bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
1263 | MachineBasicBlock *New) { |
1264 | assert(Old != New && "Not making a change?")(static_cast <bool> (Old != New && "Not making a change?" ) ? void (0) : __assert_fail ("Old != New && \"Not making a change?\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1264, __extension__ __PRETTY_FUNCTION__)); |
1265 | bool MadeChange = false; |
1266 | for (size_t i = 0, e = JumpTables.size(); i != e; ++i) |
1267 | ReplaceMBBInJumpTable(i, Old, New); |
1268 | return MadeChange; |
1269 | } |
1270 | |
1271 | /// If MBB is present in any jump tables, remove it. |
1272 | bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { |
1273 | bool MadeChange = false; |
1274 | for (MachineJumpTableEntry &JTE : JumpTables) { |
1275 | auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); |
1276 | MadeChange |= (removeBeginItr != JTE.MBBs.end()); |
1277 | JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); |
1278 | } |
1279 | return MadeChange; |
1280 | } |
1281 | |
1282 | /// If Old is a target of the jump tables, update the jump table to branch to |
1283 | /// New instead. |
1284 | bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, |
1285 | MachineBasicBlock *Old, |
1286 | MachineBasicBlock *New) { |
1287 | assert(Old != New && "Not making a change?")(static_cast <bool> (Old != New && "Not making a change?" ) ? void (0) : __assert_fail ("Old != New && \"Not making a change?\"" , "/build/llvm-toolchain-snapshot-13~++20210716111140+69a56845317b/llvm/lib/CodeGen/MachineFunction.cpp" , 1287, __extension__ __PRETTY_FUNCTION__)); |
1288 | bool MadeChange = false; |
1289 | MachineJumpTableEntry &JTE = JumpTables[Idx]; |
1290 | for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) |
1291 | if (JTE.MBBs[j] == Old) { |
1292 | JTE.MBBs[j] = New; |
1293 | MadeChange = true; |
1294 | } |
1295 | return MadeChange; |
1296 | } |
1297 | |
1298 | void MachineJumpTableInfo::print(raw_ostream &OS) const { |
1299 | if (JumpTables.empty()) return; |
1300 | |
1301 | OS << "Jump Tables:\n"; |
1302 | |
1303 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { |
1304 | OS << printJumpTableEntryReference(i) << ':'; |
1305 | for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) |
1306 | OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); |
1307 | if (i != e) |
1308 | OS << '\n'; |
1309 | } |
1310 | |
1311 | OS << '\n'; |
1312 | } |
1313 | |
1314 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
1315 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineJumpTableInfo::dump() const { print(dbgs()); } |
1316 | #endif |
1317 | |
1318 | Printable llvm::printJumpTableEntryReference(unsigned Idx) { |
1319 | return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); |
1320 | } |
1321 | |
1322 | //===----------------------------------------------------------------------===// |
1323 | // MachineConstantPool implementation |
1324 | //===----------------------------------------------------------------------===// |
1325 | |
1326 | void MachineConstantPoolValue::anchor() {} |
1327 | |
1328 | unsigned MachineConstantPoolValue::getSizeInBytes(const DataLayout &DL) const { |
1329 | return DL.getTypeAllocSize(Ty); |
1330 | } |
1331 | |
1332 | unsigned MachineConstantPoolEntry::getSizeInBytes(const DataLayout &DL) const { |
1333 | if (isMachineConstantPoolEntry()) |
1334 | return Val.MachineCPVal->getSizeInBytes(DL); |
1335 | return DL.getTypeAllocSize(Val.ConstVal->getType()); |
1336 | } |
1337 | |
1338 | bool MachineConstantPoolEntry::needsRelocation() const { |
1339 | if (isMachineConstantPoolEntry()) |
1340 | return true; |
1341 | return Val.ConstVal->needsDynamicRelocation(); |
1342 | } |
1343 | |
1344 | SectionKind |
1345 | MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { |
1346 | if (needsRelocation()) |
1347 | return SectionKind::getReadOnlyWithRel(); |
1348 | switch (getSizeInBytes(*DL)) { |
1349 | case 4: |
1350 | return SectionKind::getMergeableConst4(); |
1351 | case 8: |
1352 | return SectionKind::getMergeableConst8(); |
1353 | case 16: |
1354 | return SectionKind::getMergeableConst16(); |
1355 | case 32: |
1356 | return SectionKind::getMergeableConst32(); |
1357 | default: |
1358 | return SectionKind::getReadOnly(); |
1359 | } |
1360 | } |
1361 | |
1362 | MachineConstantPool::~MachineConstantPool() { |
1363 | // A constant may be a member of both Constants and MachineCPVsSharingEntries, |
1364 | // so keep track of which we've deleted to avoid double deletions. |
1365 | DenseSet<MachineConstantPoolValue*> Deleted; |
1366 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
1367 | if (Constants[i].isMachineConstantPoolEntry()) { |
1368 | Deleted.insert(Constants[i].Val.MachineCPVal); |
1369 | delete Constants[i].Val.MachineCPVal; |
1370 | } |
1371 | for (MachineConstantPoolValue *CPV : MachineCPVsSharingEntries) { |
1372 | if (Deleted.count(CPV) == 0) |
1373 | delete CPV; |
1374 | } |
1375 | } |
1376 | |
1377 | /// Test whether the given two constants can be allocated the same constant pool |
1378 | /// entry. |
1379 | static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, |
1380 | const DataLayout &DL) { |
1381 | // Handle the trivial case quickly. |
1382 | if (A == B) return true; |
1383 | |
1384 | // If they have the same type but weren't the same constant, quickly |
1385 | // reject them. |
1386 | if (A->getType() == B->getType()) return false; |
1387 | |
1388 | // We can't handle structs or arrays. |
1389 | if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || |
1390 | isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) |
1391 | return false; |
1392 | |
1393 | // For now, only support constants with the same size. |
1394 | uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); |
1395 | if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) |
1396 | return false; |
1397 | |
1398 | Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); |
1399 | |
1400 | // Try constant folding a bitcast of both instructions to an integer. If we |
1401 | // get two identical ConstantInt's, then we are good to share them. We use |
1402 | // the constant folding APIs to do this so that we get the benefit of |
1403 | // DataLayout. |
1404 | if (isa<PointerType>(A->getType())) |
1405 | A = ConstantFoldCastOperand(Instruction::PtrToInt, |
1406 | const_cast<Constant *>(A), IntTy, DL); |
1407 | else if (A->getType() != IntTy) |
1408 | A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), |
1409 | IntTy, DL); |
1410 | if (isa<PointerType>(B->getType())) |
1411 | B = ConstantFoldCastOperand(Instruction::PtrToInt, |
1412 | const_cast<Constant *>(B), IntTy, DL); |
1413 | else if (B->getType() != IntTy) |
1414 | B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), |
1415 | IntTy, DL); |
1416 | |
1417 | return A == B; |
1418 | } |
1419 | |
1420 | /// Create a new entry in the constant pool or return an existing one. |
1421 | /// User must specify the log2 of the minimum required alignment for the object. |
1422 | unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, |
1423 | Align Alignment) { |
1424 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
1425 | |
1426 | // Check to see if we already have this constant. |
1427 | // |
1428 | // FIXME, this could be made much more efficient for large constant pools. |
1429 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
1430 | if (!Constants[i].isMachineConstantPoolEntry() && |
1431 | CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { |
1432 | if (Constants[i].getAlign() < Alignment) |
1433 | Constants[i].Alignment = Alignment; |
1434 | return i; |
1435 | } |
1436 | |
1437 | Constants.push_back(MachineConstantPoolEntry(C, Alignment)); |
1438 | return Constants.size()-1; |
1439 | } |
1440 | |
1441 | unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, |
1442 | Align Alignment) { |
1443 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
1444 | |
1445 | // Check to see if we already have this constant. |
1446 | // |
1447 | // FIXME, this could be made much more efficient for large constant pools. |
1448 | int Idx = V->getExistingMachineCPValue(this, Alignment); |
1449 | if (Idx != -1) { |
1450 | MachineCPVsSharingEntries.insert(V); |
1451 | return (unsigned)Idx; |
1452 | } |
1453 | |
1454 | Constants.push_back(MachineConstantPoolEntry(V, Alignment)); |
1455 | return Constants.size()-1; |
1456 | } |
1457 | |
1458 | void MachineConstantPool::print(raw_ostream &OS) const { |
1459 | if (Constants.empty()) return; |
1460 | |
1461 | OS << "Constant Pool:\n"; |
1462 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
1463 | OS << " cp#" << i << ": "; |
1464 | if (Constants[i].isMachineConstantPoolEntry()) |
1465 | Constants[i].Val.MachineCPVal->print(OS); |
1466 | else |
1467 | Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); |
1468 | OS << ", align=" << Constants[i].getAlign().value(); |
1469 | OS << "\n"; |
1470 | } |
1471 | } |
1472 | |
1473 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
1474 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineConstantPool::dump() const { print(dbgs()); } |
1475 | #endif |