File: | llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp |
Warning: | line 279, column 5 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- AsmPrinter.cpp - Common AsmPrinter code ----------------------------===// | |||
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 implements the AsmPrinter class. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "llvm/CodeGen/AsmPrinter.h" | |||
14 | #include "CodeViewDebug.h" | |||
15 | #include "DwarfDebug.h" | |||
16 | #include "DwarfException.h" | |||
17 | #include "PseudoProbePrinter.h" | |||
18 | #include "WasmException.h" | |||
19 | #include "WinCFGuard.h" | |||
20 | #include "WinException.h" | |||
21 | #include "llvm/ADT/APFloat.h" | |||
22 | #include "llvm/ADT/APInt.h" | |||
23 | #include "llvm/ADT/DenseMap.h" | |||
24 | #include "llvm/ADT/STLExtras.h" | |||
25 | #include "llvm/ADT/SmallPtrSet.h" | |||
26 | #include "llvm/ADT/SmallString.h" | |||
27 | #include "llvm/ADT/SmallVector.h" | |||
28 | #include "llvm/ADT/Statistic.h" | |||
29 | #include "llvm/ADT/StringRef.h" | |||
30 | #include "llvm/ADT/Triple.h" | |||
31 | #include "llvm/ADT/Twine.h" | |||
32 | #include "llvm/Analysis/ConstantFolding.h" | |||
33 | #include "llvm/Analysis/EHPersonalities.h" | |||
34 | #include "llvm/Analysis/MemoryLocation.h" | |||
35 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | |||
36 | #include "llvm/BinaryFormat/COFF.h" | |||
37 | #include "llvm/BinaryFormat/Dwarf.h" | |||
38 | #include "llvm/BinaryFormat/ELF.h" | |||
39 | #include "llvm/CodeGen/GCMetadata.h" | |||
40 | #include "llvm/CodeGen/GCMetadataPrinter.h" | |||
41 | #include "llvm/CodeGen/GCStrategy.h" | |||
42 | #include "llvm/CodeGen/MachineBasicBlock.h" | |||
43 | #include "llvm/CodeGen/MachineConstantPool.h" | |||
44 | #include "llvm/CodeGen/MachineDominators.h" | |||
45 | #include "llvm/CodeGen/MachineFrameInfo.h" | |||
46 | #include "llvm/CodeGen/MachineFunction.h" | |||
47 | #include "llvm/CodeGen/MachineFunctionPass.h" | |||
48 | #include "llvm/CodeGen/MachineInstr.h" | |||
49 | #include "llvm/CodeGen/MachineInstrBundle.h" | |||
50 | #include "llvm/CodeGen/MachineJumpTableInfo.h" | |||
51 | #include "llvm/CodeGen/MachineLoopInfo.h" | |||
52 | #include "llvm/CodeGen/MachineMemOperand.h" | |||
53 | #include "llvm/CodeGen/MachineModuleInfo.h" | |||
54 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" | |||
55 | #include "llvm/CodeGen/MachineOperand.h" | |||
56 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" | |||
57 | #include "llvm/CodeGen/StackMaps.h" | |||
58 | #include "llvm/CodeGen/TargetFrameLowering.h" | |||
59 | #include "llvm/CodeGen/TargetInstrInfo.h" | |||
60 | #include "llvm/CodeGen/TargetLowering.h" | |||
61 | #include "llvm/CodeGen/TargetOpcodes.h" | |||
62 | #include "llvm/CodeGen/TargetRegisterInfo.h" | |||
63 | #include "llvm/IR/BasicBlock.h" | |||
64 | #include "llvm/IR/Comdat.h" | |||
65 | #include "llvm/IR/Constant.h" | |||
66 | #include "llvm/IR/Constants.h" | |||
67 | #include "llvm/IR/DataLayout.h" | |||
68 | #include "llvm/IR/DebugInfoMetadata.h" | |||
69 | #include "llvm/IR/DerivedTypes.h" | |||
70 | #include "llvm/IR/Function.h" | |||
71 | #include "llvm/IR/GlobalAlias.h" | |||
72 | #include "llvm/IR/GlobalIFunc.h" | |||
73 | #include "llvm/IR/GlobalIndirectSymbol.h" | |||
74 | #include "llvm/IR/GlobalObject.h" | |||
75 | #include "llvm/IR/GlobalValue.h" | |||
76 | #include "llvm/IR/GlobalVariable.h" | |||
77 | #include "llvm/IR/Instruction.h" | |||
78 | #include "llvm/IR/Mangler.h" | |||
79 | #include "llvm/IR/Metadata.h" | |||
80 | #include "llvm/IR/Module.h" | |||
81 | #include "llvm/IR/Operator.h" | |||
82 | #include "llvm/IR/PseudoProbe.h" | |||
83 | #include "llvm/IR/Type.h" | |||
84 | #include "llvm/IR/Value.h" | |||
85 | #include "llvm/MC/MCAsmInfo.h" | |||
86 | #include "llvm/MC/MCContext.h" | |||
87 | #include "llvm/MC/MCDirectives.h" | |||
88 | #include "llvm/MC/MCDwarf.h" | |||
89 | #include "llvm/MC/MCExpr.h" | |||
90 | #include "llvm/MC/MCInst.h" | |||
91 | #include "llvm/MC/MCSection.h" | |||
92 | #include "llvm/MC/MCSectionCOFF.h" | |||
93 | #include "llvm/MC/MCSectionELF.h" | |||
94 | #include "llvm/MC/MCSectionMachO.h" | |||
95 | #include "llvm/MC/MCSectionXCOFF.h" | |||
96 | #include "llvm/MC/MCStreamer.h" | |||
97 | #include "llvm/MC/MCSubtargetInfo.h" | |||
98 | #include "llvm/MC/MCSymbol.h" | |||
99 | #include "llvm/MC/MCSymbolELF.h" | |||
100 | #include "llvm/MC/MCSymbolXCOFF.h" | |||
101 | #include "llvm/MC/MCTargetOptions.h" | |||
102 | #include "llvm/MC/MCValue.h" | |||
103 | #include "llvm/MC/SectionKind.h" | |||
104 | #include "llvm/Pass.h" | |||
105 | #include "llvm/Remarks/Remark.h" | |||
106 | #include "llvm/Remarks/RemarkFormat.h" | |||
107 | #include "llvm/Remarks/RemarkStreamer.h" | |||
108 | #include "llvm/Remarks/RemarkStringTable.h" | |||
109 | #include "llvm/Support/Casting.h" | |||
110 | #include "llvm/Support/CommandLine.h" | |||
111 | #include "llvm/Support/Compiler.h" | |||
112 | #include "llvm/Support/ErrorHandling.h" | |||
113 | #include "llvm/Support/Format.h" | |||
114 | #include "llvm/Support/MathExtras.h" | |||
115 | #include "llvm/Support/Path.h" | |||
116 | #include "llvm/Support/TargetRegistry.h" | |||
117 | #include "llvm/Support/Timer.h" | |||
118 | #include "llvm/Support/raw_ostream.h" | |||
119 | #include "llvm/Target/TargetLoweringObjectFile.h" | |||
120 | #include "llvm/Target/TargetMachine.h" | |||
121 | #include "llvm/Target/TargetOptions.h" | |||
122 | #include <algorithm> | |||
123 | #include <cassert> | |||
124 | #include <cinttypes> | |||
125 | #include <cstdint> | |||
126 | #include <iterator> | |||
127 | #include <limits> | |||
128 | #include <memory> | |||
129 | #include <string> | |||
130 | #include <utility> | |||
131 | #include <vector> | |||
132 | ||||
133 | using namespace llvm; | |||
134 | ||||
135 | #define DEBUG_TYPE"asm-printer" "asm-printer" | |||
136 | ||||
137 | // FIXME: this option currently only applies to DWARF, and not CodeView, tables | |||
138 | static cl::opt<bool> | |||
139 | DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, | |||
140 | cl::desc("Disable debug info printing")); | |||
141 | ||||
142 | const char DWARFGroupName[] = "dwarf"; | |||
143 | const char DWARFGroupDescription[] = "DWARF Emission"; | |||
144 | const char DbgTimerName[] = "emit"; | |||
145 | const char DbgTimerDescription[] = "Debug Info Emission"; | |||
146 | const char EHTimerName[] = "write_exception"; | |||
147 | const char EHTimerDescription[] = "DWARF Exception Writer"; | |||
148 | const char CFGuardName[] = "Control Flow Guard"; | |||
149 | const char CFGuardDescription[] = "Control Flow Guard"; | |||
150 | const char CodeViewLineTablesGroupName[] = "linetables"; | |||
151 | const char CodeViewLineTablesGroupDescription[] = "CodeView Line Tables"; | |||
152 | const char PPTimerName[] = "emit"; | |||
153 | const char PPTimerDescription[] = "Pseudo Probe Emission"; | |||
154 | const char PPGroupName[] = "pseudo probe"; | |||
155 | const char PPGroupDescription[] = "Pseudo Probe Emission"; | |||
156 | ||||
157 | STATISTIC(EmittedInsts, "Number of machine instrs printed")static llvm::Statistic EmittedInsts = {"asm-printer", "EmittedInsts" , "Number of machine instrs printed"}; | |||
158 | ||||
159 | char AsmPrinter::ID = 0; | |||
160 | ||||
161 | using gcp_map_type = DenseMap<GCStrategy *, std::unique_ptr<GCMetadataPrinter>>; | |||
162 | ||||
163 | static gcp_map_type &getGCMap(void *&P) { | |||
164 | if (!P) | |||
165 | P = new gcp_map_type(); | |||
166 | return *(gcp_map_type*)P; | |||
167 | } | |||
168 | ||||
169 | /// getGVAlignment - Return the alignment to use for the specified global | |||
170 | /// value. This rounds up to the preferred alignment if possible and legal. | |||
171 | Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL, | |||
172 | Align InAlign) { | |||
173 | Align Alignment; | |||
174 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) | |||
175 | Alignment = DL.getPreferredAlign(GVar); | |||
176 | ||||
177 | // If InAlign is specified, round it to it. | |||
178 | if (InAlign > Alignment) | |||
179 | Alignment = InAlign; | |||
180 | ||||
181 | // If the GV has a specified alignment, take it into account. | |||
182 | const MaybeAlign GVAlign(GV->getAlignment()); | |||
183 | if (!GVAlign) | |||
184 | return Alignment; | |||
185 | ||||
186 | assert(GVAlign && "GVAlign must be set")((GVAlign && "GVAlign must be set") ? static_cast< void> (0) : __assert_fail ("GVAlign && \"GVAlign must be set\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 186, __PRETTY_FUNCTION__)); | |||
187 | ||||
188 | // If the GVAlign is larger than NumBits, or if we are required to obey | |||
189 | // NumBits because the GV has an assigned section, obey it. | |||
190 | if (*GVAlign > Alignment || GV->hasSection()) | |||
191 | Alignment = *GVAlign; | |||
192 | return Alignment; | |||
193 | } | |||
194 | ||||
195 | AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer) | |||
196 | : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()), | |||
197 | OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)) { | |||
198 | VerboseAsm = OutStreamer->isVerboseAsm(); | |||
199 | } | |||
200 | ||||
201 | AsmPrinter::~AsmPrinter() { | |||
202 | assert(!DD && Handlers.size() == NumUserHandlers &&((!DD && Handlers.size() == NumUserHandlers && "Debug/EH info didn't get finalized") ? static_cast<void> (0) : __assert_fail ("!DD && Handlers.size() == NumUserHandlers && \"Debug/EH info didn't get finalized\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 203, __PRETTY_FUNCTION__)) | |||
203 | "Debug/EH info didn't get finalized")((!DD && Handlers.size() == NumUserHandlers && "Debug/EH info didn't get finalized") ? static_cast<void> (0) : __assert_fail ("!DD && Handlers.size() == NumUserHandlers && \"Debug/EH info didn't get finalized\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 203, __PRETTY_FUNCTION__)); | |||
204 | ||||
205 | if (GCMetadataPrinters) { | |||
206 | gcp_map_type &GCMap = getGCMap(GCMetadataPrinters); | |||
207 | ||||
208 | delete &GCMap; | |||
209 | GCMetadataPrinters = nullptr; | |||
210 | } | |||
211 | } | |||
212 | ||||
213 | bool AsmPrinter::isPositionIndependent() const { | |||
214 | return TM.isPositionIndependent(); | |||
215 | } | |||
216 | ||||
217 | /// getFunctionNumber - Return a unique ID for the current function. | |||
218 | unsigned AsmPrinter::getFunctionNumber() const { | |||
219 | return MF->getFunctionNumber(); | |||
220 | } | |||
221 | ||||
222 | const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { | |||
223 | return *TM.getObjFileLowering(); | |||
224 | } | |||
225 | ||||
226 | const DataLayout &AsmPrinter::getDataLayout() const { | |||
227 | return MMI->getModule()->getDataLayout(); | |||
228 | } | |||
229 | ||||
230 | // Do not use the cached DataLayout because some client use it without a Module | |||
231 | // (dsymutil, llvm-dwarfdump). | |||
232 | unsigned AsmPrinter::getPointerSize() const { | |||
233 | return TM.getPointerSize(0); // FIXME: Default address space | |||
234 | } | |||
235 | ||||
236 | const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const { | |||
237 | assert(MF && "getSubtargetInfo requires a valid MachineFunction!")((MF && "getSubtargetInfo requires a valid MachineFunction!" ) ? static_cast<void> (0) : __assert_fail ("MF && \"getSubtargetInfo requires a valid MachineFunction!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 237, __PRETTY_FUNCTION__)); | |||
238 | return MF->getSubtarget<MCSubtargetInfo>(); | |||
239 | } | |||
240 | ||||
241 | void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) { | |||
242 | S.emitInstruction(Inst, getSubtargetInfo()); | |||
243 | } | |||
244 | ||||
245 | void AsmPrinter::emitInitialRawDwarfLocDirective(const MachineFunction &MF) { | |||
246 | if (DD) { | |||
247 | assert(OutStreamer->hasRawTextSupport() &&((OutStreamer->hasRawTextSupport() && "Expected assembly output mode." ) ? static_cast<void> (0) : __assert_fail ("OutStreamer->hasRawTextSupport() && \"Expected assembly output mode.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 248, __PRETTY_FUNCTION__)) | |||
248 | "Expected assembly output mode.")((OutStreamer->hasRawTextSupport() && "Expected assembly output mode." ) ? static_cast<void> (0) : __assert_fail ("OutStreamer->hasRawTextSupport() && \"Expected assembly output mode.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 248, __PRETTY_FUNCTION__)); | |||
249 | (void)DD->emitInitialLocDirective(MF, /*CUID=*/0); | |||
250 | } | |||
251 | } | |||
252 | ||||
253 | /// getCurrentSection() - Return the current section we are emitting to. | |||
254 | const MCSection *AsmPrinter::getCurrentSection() const { | |||
255 | return OutStreamer->getCurrentSectionOnly(); | |||
256 | } | |||
257 | ||||
258 | void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { | |||
259 | AU.setPreservesAll(); | |||
260 | MachineFunctionPass::getAnalysisUsage(AU); | |||
261 | AU.addRequired<MachineOptimizationRemarkEmitterPass>(); | |||
262 | AU.addRequired<GCModuleInfo>(); | |||
263 | } | |||
264 | ||||
265 | bool AsmPrinter::doInitialization(Module &M) { | |||
266 | auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>(); | |||
267 | MMI = MMIWP
| |||
| ||||
268 | ||||
269 | // Initialize TargetLoweringObjectFile. | |||
270 | const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) | |||
271 | .Initialize(OutContext, TM); | |||
272 | ||||
273 | const_cast<TargetLoweringObjectFile &>(getObjFileLowering()) | |||
274 | .getModuleMetadata(M); | |||
275 | ||||
276 | OutStreamer->InitSections(false); | |||
277 | ||||
278 | if (DisableDebugInfoPrinting) | |||
279 | MMI->setDebugInfoAvailability(false); | |||
| ||||
280 | ||||
281 | // Emit the version-min deployment target directive if needed. | |||
282 | // | |||
283 | // FIXME: If we end up with a collection of these sorts of Darwin-specific | |||
284 | // or ELF-specific things, it may make sense to have a platform helper class | |||
285 | // that will work with the target helper class. For now keep it here, as the | |||
286 | // alternative is duplicated code in each of the target asm printers that | |||
287 | // use the directive, where it would need the same conditionalization | |||
288 | // anyway. | |||
289 | const Triple &Target = TM.getTargetTriple(); | |||
290 | OutStreamer->emitVersionForTarget(Target, M.getSDKVersion()); | |||
291 | ||||
292 | // Allow the target to emit any magic that it wants at the start of the file. | |||
293 | emitStartOfAsmFile(M); | |||
294 | ||||
295 | // Very minimal debug info. It is ignored if we emit actual debug info. If we | |||
296 | // don't, this at least helps the user find where a global came from. | |||
297 | if (MAI->hasSingleParameterDotFile()) { | |||
298 | // .file "foo.c" | |||
299 | OutStreamer->emitFileDirective( | |||
300 | llvm::sys::path::filename(M.getSourceFileName())); | |||
301 | } | |||
302 | ||||
303 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); | |||
304 | assert(MI && "AsmPrinter didn't require GCModuleInfo?")((MI && "AsmPrinter didn't require GCModuleInfo?") ? static_cast <void> (0) : __assert_fail ("MI && \"AsmPrinter didn't require GCModuleInfo?\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 304, __PRETTY_FUNCTION__)); | |||
305 | for (auto &I : *MI) | |||
306 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) | |||
307 | MP->beginAssembly(M, *MI, *this); | |||
308 | ||||
309 | // Emit module-level inline asm if it exists. | |||
310 | if (!M.getModuleInlineAsm().empty()) { | |||
311 | // We're at the module level. Construct MCSubtarget from the default CPU | |||
312 | // and target triple. | |||
313 | std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo( | |||
314 | TM.getTargetTriple().str(), TM.getTargetCPU(), | |||
315 | TM.getTargetFeatureString())); | |||
316 | assert(STI && "Unable to create subtarget info")((STI && "Unable to create subtarget info") ? static_cast <void> (0) : __assert_fail ("STI && \"Unable to create subtarget info\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 316, __PRETTY_FUNCTION__)); | |||
317 | OutStreamer->AddComment("Start of file scope inline assembly"); | |||
318 | OutStreamer->AddBlankLine(); | |||
319 | emitInlineAsm(M.getModuleInlineAsm() + "\n", | |||
320 | OutContext.getSubtargetCopy(*STI), TM.Options.MCOptions); | |||
321 | OutStreamer->AddComment("End of file scope inline assembly"); | |||
322 | OutStreamer->AddBlankLine(); | |||
323 | } | |||
324 | ||||
325 | if (MAI->doesSupportDebugInformation()) { | |||
326 | bool EmitCodeView = M.getCodeViewFlag(); | |||
327 | if (EmitCodeView && TM.getTargetTriple().isOSWindows()) { | |||
328 | Handlers.emplace_back(std::make_unique<CodeViewDebug>(this), | |||
329 | DbgTimerName, DbgTimerDescription, | |||
330 | CodeViewLineTablesGroupName, | |||
331 | CodeViewLineTablesGroupDescription); | |||
332 | } | |||
333 | if (!EmitCodeView || M.getDwarfVersion()) { | |||
334 | if (!DisableDebugInfoPrinting) { | |||
335 | DD = new DwarfDebug(this); | |||
336 | Handlers.emplace_back(std::unique_ptr<DwarfDebug>(DD), DbgTimerName, | |||
337 | DbgTimerDescription, DWARFGroupName, | |||
338 | DWARFGroupDescription); | |||
339 | } | |||
340 | } | |||
341 | } | |||
342 | ||||
343 | if (M.getNamedMetadata(PseudoProbeDescMetadataName)) { | |||
344 | PP = new PseudoProbeHandler(this, &M); | |||
345 | Handlers.emplace_back(std::unique_ptr<PseudoProbeHandler>(PP), PPTimerName, | |||
346 | PPTimerDescription, PPGroupName, PPGroupDescription); | |||
347 | } | |||
348 | ||||
349 | switch (MAI->getExceptionHandlingType()) { | |||
350 | case ExceptionHandling::SjLj: | |||
351 | case ExceptionHandling::DwarfCFI: | |||
352 | case ExceptionHandling::ARM: | |||
353 | isCFIMoveForDebugging = true; | |||
354 | if (MAI->getExceptionHandlingType() != ExceptionHandling::DwarfCFI) | |||
355 | break; | |||
356 | for (auto &F: M.getFunctionList()) { | |||
357 | // If the module contains any function with unwind data, | |||
358 | // .eh_frame has to be emitted. | |||
359 | // Ignore functions that won't get emitted. | |||
360 | if (!F.isDeclarationForLinker() && F.needsUnwindTableEntry()) { | |||
361 | isCFIMoveForDebugging = false; | |||
362 | break; | |||
363 | } | |||
364 | } | |||
365 | break; | |||
366 | default: | |||
367 | isCFIMoveForDebugging = false; | |||
368 | break; | |||
369 | } | |||
370 | ||||
371 | EHStreamer *ES = nullptr; | |||
372 | switch (MAI->getExceptionHandlingType()) { | |||
373 | case ExceptionHandling::None: | |||
374 | break; | |||
375 | case ExceptionHandling::SjLj: | |||
376 | case ExceptionHandling::DwarfCFI: | |||
377 | ES = new DwarfCFIException(this); | |||
378 | break; | |||
379 | case ExceptionHandling::ARM: | |||
380 | ES = new ARMException(this); | |||
381 | break; | |||
382 | case ExceptionHandling::WinEH: | |||
383 | switch (MAI->getWinEHEncodingType()) { | |||
384 | default: llvm_unreachable("unsupported unwinding information encoding")::llvm::llvm_unreachable_internal("unsupported unwinding information encoding" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 384); | |||
385 | case WinEH::EncodingType::Invalid: | |||
386 | break; | |||
387 | case WinEH::EncodingType::X86: | |||
388 | case WinEH::EncodingType::Itanium: | |||
389 | ES = new WinException(this); | |||
390 | break; | |||
391 | } | |||
392 | break; | |||
393 | case ExceptionHandling::Wasm: | |||
394 | ES = new WasmException(this); | |||
395 | break; | |||
396 | case ExceptionHandling::AIX: | |||
397 | ES = new AIXException(this); | |||
398 | break; | |||
399 | } | |||
400 | if (ES) | |||
401 | Handlers.emplace_back(std::unique_ptr<EHStreamer>(ES), EHTimerName, | |||
402 | EHTimerDescription, DWARFGroupName, | |||
403 | DWARFGroupDescription); | |||
404 | ||||
405 | // Emit tables for any value of cfguard flag (i.e. cfguard=1 or cfguard=2). | |||
406 | if (mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard"))) | |||
407 | Handlers.emplace_back(std::make_unique<WinCFGuard>(this), CFGuardName, | |||
408 | CFGuardDescription, DWARFGroupName, | |||
409 | DWARFGroupDescription); | |||
410 | ||||
411 | for (const HandlerInfo &HI : Handlers) { | |||
412 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
413 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
414 | HI.Handler->beginModule(&M); | |||
415 | } | |||
416 | ||||
417 | return false; | |||
418 | } | |||
419 | ||||
420 | static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) { | |||
421 | if (!MAI.hasWeakDefCanBeHiddenDirective()) | |||
422 | return false; | |||
423 | ||||
424 | return GV->canBeOmittedFromSymbolTable(); | |||
425 | } | |||
426 | ||||
427 | void AsmPrinter::emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const { | |||
428 | GlobalValue::LinkageTypes Linkage = GV->getLinkage(); | |||
429 | switch (Linkage) { | |||
430 | case GlobalValue::CommonLinkage: | |||
431 | case GlobalValue::LinkOnceAnyLinkage: | |||
432 | case GlobalValue::LinkOnceODRLinkage: | |||
433 | case GlobalValue::WeakAnyLinkage: | |||
434 | case GlobalValue::WeakODRLinkage: | |||
435 | if (MAI->hasWeakDefDirective()) { | |||
436 | // .globl _foo | |||
437 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global); | |||
438 | ||||
439 | if (!canBeHidden(GV, *MAI)) | |||
440 | // .weak_definition _foo | |||
441 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_WeakDefinition); | |||
442 | else | |||
443 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate); | |||
444 | } else if (MAI->avoidWeakIfComdat() && GV->hasComdat()) { | |||
445 | // .globl _foo | |||
446 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global); | |||
447 | //NOTE: linkonce is handled by the section the symbol was assigned to. | |||
448 | } else { | |||
449 | // .weak _foo | |||
450 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_Weak); | |||
451 | } | |||
452 | return; | |||
453 | case GlobalValue::ExternalLinkage: | |||
454 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global); | |||
455 | return; | |||
456 | case GlobalValue::PrivateLinkage: | |||
457 | case GlobalValue::InternalLinkage: | |||
458 | return; | |||
459 | case GlobalValue::ExternalWeakLinkage: | |||
460 | case GlobalValue::AvailableExternallyLinkage: | |||
461 | case GlobalValue::AppendingLinkage: | |||
462 | llvm_unreachable("Should never emit this")::llvm::llvm_unreachable_internal("Should never emit this", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 462); | |||
463 | } | |||
464 | llvm_unreachable("Unknown linkage type!")::llvm::llvm_unreachable_internal("Unknown linkage type!", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 464); | |||
465 | } | |||
466 | ||||
467 | void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name, | |||
468 | const GlobalValue *GV) const { | |||
469 | TM.getNameWithPrefix(Name, GV, getObjFileLowering().getMangler()); | |||
470 | } | |||
471 | ||||
472 | MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const { | |||
473 | return TM.getSymbol(GV); | |||
474 | } | |||
475 | ||||
476 | MCSymbol *AsmPrinter::getSymbolPreferLocal(const GlobalValue &GV) const { | |||
477 | // On ELF, use .Lfoo$local if GV is a non-interposable GlobalObject with an | |||
478 | // exact definion (intersection of GlobalValue::hasExactDefinition() and | |||
479 | // !isInterposable()). These linkages include: external, appending, internal, | |||
480 | // private. It may be profitable to use a local alias for external. The | |||
481 | // assembler would otherwise be conservative and assume a global default | |||
482 | // visibility symbol can be interposable, even if the code generator already | |||
483 | // assumed it. | |||
484 | if (TM.getTargetTriple().isOSBinFormatELF() && GV.canBenefitFromLocalAlias()) { | |||
485 | const Module &M = *GV.getParent(); | |||
486 | if (TM.getRelocationModel() != Reloc::Static && | |||
487 | M.getPIELevel() == PIELevel::Default && GV.isDSOLocal()) | |||
488 | return getSymbolWithGlobalValueBase(&GV, "$local"); | |||
489 | } | |||
490 | return TM.getSymbol(&GV); | |||
491 | } | |||
492 | ||||
493 | /// EmitGlobalVariable - Emit the specified global variable to the .s file. | |||
494 | void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { | |||
495 | bool IsEmuTLSVar = TM.useEmulatedTLS() && GV->isThreadLocal(); | |||
496 | assert(!(IsEmuTLSVar && GV->hasCommonLinkage()) &&((!(IsEmuTLSVar && GV->hasCommonLinkage()) && "No emulated TLS variables in the common section") ? static_cast <void> (0) : __assert_fail ("!(IsEmuTLSVar && GV->hasCommonLinkage()) && \"No emulated TLS variables in the common section\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 497, __PRETTY_FUNCTION__)) | |||
497 | "No emulated TLS variables in the common section")((!(IsEmuTLSVar && GV->hasCommonLinkage()) && "No emulated TLS variables in the common section") ? static_cast <void> (0) : __assert_fail ("!(IsEmuTLSVar && GV->hasCommonLinkage()) && \"No emulated TLS variables in the common section\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 497, __PRETTY_FUNCTION__)); | |||
498 | ||||
499 | // Never emit TLS variable xyz in emulated TLS model. | |||
500 | // The initialization value is in __emutls_t.xyz instead of xyz. | |||
501 | if (IsEmuTLSVar) | |||
502 | return; | |||
503 | ||||
504 | if (GV->hasInitializer()) { | |||
505 | // Check to see if this is a special global used by LLVM, if so, emit it. | |||
506 | if (emitSpecialLLVMGlobal(GV)) | |||
507 | return; | |||
508 | ||||
509 | // Skip the emission of global equivalents. The symbol can be emitted later | |||
510 | // on by emitGlobalGOTEquivs in case it turns out to be needed. | |||
511 | if (GlobalGOTEquivs.count(getSymbol(GV))) | |||
512 | return; | |||
513 | ||||
514 | if (isVerbose()) { | |||
515 | // When printing the control variable __emutls_v.*, | |||
516 | // we don't need to print the original TLS variable name. | |||
517 | GV->printAsOperand(OutStreamer->GetCommentOS(), | |||
518 | /*PrintType=*/false, GV->getParent()); | |||
519 | OutStreamer->GetCommentOS() << '\n'; | |||
520 | } | |||
521 | } | |||
522 | ||||
523 | MCSymbol *GVSym = getSymbol(GV); | |||
524 | MCSymbol *EmittedSym = GVSym; | |||
525 | ||||
526 | // getOrCreateEmuTLSControlSym only creates the symbol with name and default | |||
527 | // attributes. | |||
528 | // GV's or GVSym's attributes will be used for the EmittedSym. | |||
529 | emitVisibility(EmittedSym, GV->getVisibility(), !GV->isDeclaration()); | |||
530 | ||||
531 | if (!GV->hasInitializer()) // External globals require no extra code. | |||
532 | return; | |||
533 | ||||
534 | GVSym->redefineIfPossible(); | |||
535 | if (GVSym->isDefined() || GVSym->isVariable()) | |||
536 | OutContext.reportError(SMLoc(), "symbol '" + Twine(GVSym->getName()) + | |||
537 | "' is already defined"); | |||
538 | ||||
539 | if (MAI->hasDotTypeDotSizeDirective()) | |||
540 | OutStreamer->emitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject); | |||
541 | ||||
542 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); | |||
543 | ||||
544 | const DataLayout &DL = GV->getParent()->getDataLayout(); | |||
545 | uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); | |||
546 | ||||
547 | // If the alignment is specified, we *must* obey it. Overaligning a global | |||
548 | // with a specified alignment is a prompt way to break globals emitted to | |||
549 | // sections and expected to be contiguous (e.g. ObjC metadata). | |||
550 | const Align Alignment = getGVAlignment(GV, DL); | |||
551 | ||||
552 | for (const HandlerInfo &HI : Handlers) { | |||
553 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, | |||
554 | HI.TimerGroupName, HI.TimerGroupDescription, | |||
555 | TimePassesIsEnabled); | |||
556 | HI.Handler->setSymbolSize(GVSym, Size); | |||
557 | } | |||
558 | ||||
559 | // Handle common symbols | |||
560 | if (GVKind.isCommon()) { | |||
561 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. | |||
562 | // .comm _foo, 42, 4 | |||
563 | const bool SupportsAlignment = | |||
564 | getObjFileLowering().getCommDirectiveSupportsAlignment(); | |||
565 | OutStreamer->emitCommonSymbol(GVSym, Size, | |||
566 | SupportsAlignment ? Alignment.value() : 0); | |||
567 | return; | |||
568 | } | |||
569 | ||||
570 | // Determine to which section this global should be emitted. | |||
571 | MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM); | |||
572 | ||||
573 | // If we have a bss global going to a section that supports the | |||
574 | // zerofill directive, do so here. | |||
575 | if (GVKind.isBSS() && MAI->hasMachoZeroFillDirective() && | |||
576 | TheSection->isVirtualSection()) { | |||
577 | if (Size == 0) | |||
578 | Size = 1; // zerofill of 0 bytes is undefined. | |||
579 | emitLinkage(GV, GVSym); | |||
580 | // .zerofill __DATA, __bss, _foo, 400, 5 | |||
581 | OutStreamer->emitZerofill(TheSection, GVSym, Size, Alignment.value()); | |||
582 | return; | |||
583 | } | |||
584 | ||||
585 | // If this is a BSS local symbol and we are emitting in the BSS | |||
586 | // section use .lcomm/.comm directive. | |||
587 | if (GVKind.isBSSLocal() && | |||
588 | getObjFileLowering().getBSSSection() == TheSection) { | |||
589 | if (Size == 0) | |||
590 | Size = 1; // .comm Foo, 0 is undefined, avoid it. | |||
591 | ||||
592 | // Use .lcomm only if it supports user-specified alignment. | |||
593 | // Otherwise, while it would still be correct to use .lcomm in some | |||
594 | // cases (e.g. when Align == 1), the external assembler might enfore | |||
595 | // some -unknown- default alignment behavior, which could cause | |||
596 | // spurious differences between external and integrated assembler. | |||
597 | // Prefer to simply fall back to .local / .comm in this case. | |||
598 | if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) { | |||
599 | // .lcomm _foo, 42 | |||
600 | OutStreamer->emitLocalCommonSymbol(GVSym, Size, Alignment.value()); | |||
601 | return; | |||
602 | } | |||
603 | ||||
604 | // .local _foo | |||
605 | OutStreamer->emitSymbolAttribute(GVSym, MCSA_Local); | |||
606 | // .comm _foo, 42, 4 | |||
607 | const bool SupportsAlignment = | |||
608 | getObjFileLowering().getCommDirectiveSupportsAlignment(); | |||
609 | OutStreamer->emitCommonSymbol(GVSym, Size, | |||
610 | SupportsAlignment ? Alignment.value() : 0); | |||
611 | return; | |||
612 | } | |||
613 | ||||
614 | // Handle thread local data for mach-o which requires us to output an | |||
615 | // additional structure of data and mangle the original symbol so that we | |||
616 | // can reference it later. | |||
617 | // | |||
618 | // TODO: This should become an "emit thread local global" method on TLOF. | |||
619 | // All of this macho specific stuff should be sunk down into TLOFMachO and | |||
620 | // stuff like "TLSExtraDataSection" should no longer be part of the parent | |||
621 | // TLOF class. This will also make it more obvious that stuff like | |||
622 | // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho | |||
623 | // specific code. | |||
624 | if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) { | |||
625 | // Emit the .tbss symbol | |||
626 | MCSymbol *MangSym = | |||
627 | OutContext.getOrCreateSymbol(GVSym->getName() + Twine("$tlv$init")); | |||
628 | ||||
629 | if (GVKind.isThreadBSS()) { | |||
630 | TheSection = getObjFileLowering().getTLSBSSSection(); | |||
631 | OutStreamer->emitTBSSSymbol(TheSection, MangSym, Size, Alignment.value()); | |||
632 | } else if (GVKind.isThreadData()) { | |||
633 | OutStreamer->SwitchSection(TheSection); | |||
634 | ||||
635 | emitAlignment(Alignment, GV); | |||
636 | OutStreamer->emitLabel(MangSym); | |||
637 | ||||
638 | emitGlobalConstant(GV->getParent()->getDataLayout(), | |||
639 | GV->getInitializer()); | |||
640 | } | |||
641 | ||||
642 | OutStreamer->AddBlankLine(); | |||
643 | ||||
644 | // Emit the variable struct for the runtime. | |||
645 | MCSection *TLVSect = getObjFileLowering().getTLSExtraDataSection(); | |||
646 | ||||
647 | OutStreamer->SwitchSection(TLVSect); | |||
648 | // Emit the linkage here. | |||
649 | emitLinkage(GV, GVSym); | |||
650 | OutStreamer->emitLabel(GVSym); | |||
651 | ||||
652 | // Three pointers in size: | |||
653 | // - __tlv_bootstrap - used to make sure support exists | |||
654 | // - spare pointer, used when mapped by the runtime | |||
655 | // - pointer to mangled symbol above with initializer | |||
656 | unsigned PtrSize = DL.getPointerTypeSize(GV->getType()); | |||
657 | OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"), | |||
658 | PtrSize); | |||
659 | OutStreamer->emitIntValue(0, PtrSize); | |||
660 | OutStreamer->emitSymbolValue(MangSym, PtrSize); | |||
661 | ||||
662 | OutStreamer->AddBlankLine(); | |||
663 | return; | |||
664 | } | |||
665 | ||||
666 | MCSymbol *EmittedInitSym = GVSym; | |||
667 | ||||
668 | OutStreamer->SwitchSection(TheSection); | |||
669 | ||||
670 | emitLinkage(GV, EmittedInitSym); | |||
671 | emitAlignment(Alignment, GV); | |||
672 | ||||
673 | OutStreamer->emitLabel(EmittedInitSym); | |||
674 | MCSymbol *LocalAlias = getSymbolPreferLocal(*GV); | |||
675 | if (LocalAlias != EmittedInitSym) | |||
676 | OutStreamer->emitLabel(LocalAlias); | |||
677 | ||||
678 | emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer()); | |||
679 | ||||
680 | if (MAI->hasDotTypeDotSizeDirective()) | |||
681 | // .size foo, 42 | |||
682 | OutStreamer->emitELFSize(EmittedInitSym, | |||
683 | MCConstantExpr::create(Size, OutContext)); | |||
684 | ||||
685 | OutStreamer->AddBlankLine(); | |||
686 | } | |||
687 | ||||
688 | /// Emit the directive and value for debug thread local expression | |||
689 | /// | |||
690 | /// \p Value - The value to emit. | |||
691 | /// \p Size - The size of the integer (in bytes) to emit. | |||
692 | void AsmPrinter::emitDebugValue(const MCExpr *Value, unsigned Size) const { | |||
693 | OutStreamer->emitValue(Value, Size); | |||
694 | } | |||
695 | ||||
696 | void AsmPrinter::emitFunctionHeaderComment() {} | |||
697 | ||||
698 | /// EmitFunctionHeader - This method emits the header for the current | |||
699 | /// function. | |||
700 | void AsmPrinter::emitFunctionHeader() { | |||
701 | const Function &F = MF->getFunction(); | |||
702 | ||||
703 | if (isVerbose()) | |||
704 | OutStreamer->GetCommentOS() | |||
705 | << "-- Begin function " | |||
706 | << GlobalValue::dropLLVMManglingEscape(F.getName()) << '\n'; | |||
707 | ||||
708 | // Print out constants referenced by the function | |||
709 | emitConstantPool(); | |||
710 | ||||
711 | // Print the 'header' of function. | |||
712 | MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM)); | |||
713 | OutStreamer->SwitchSection(MF->getSection()); | |||
714 | ||||
715 | if (!MAI->hasVisibilityOnlyWithLinkage()) | |||
716 | emitVisibility(CurrentFnSym, F.getVisibility()); | |||
717 | ||||
718 | if (MAI->needsFunctionDescriptors()) | |||
719 | emitLinkage(&F, CurrentFnDescSym); | |||
720 | ||||
721 | emitLinkage(&F, CurrentFnSym); | |||
722 | if (MAI->hasFunctionAlignment()) | |||
723 | emitAlignment(MF->getAlignment(), &F); | |||
724 | ||||
725 | if (MAI->hasDotTypeDotSizeDirective()) | |||
726 | OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction); | |||
727 | ||||
728 | if (F.hasFnAttribute(Attribute::Cold)) | |||
729 | OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_Cold); | |||
730 | ||||
731 | if (isVerbose()) { | |||
732 | F.printAsOperand(OutStreamer->GetCommentOS(), | |||
733 | /*PrintType=*/false, F.getParent()); | |||
734 | emitFunctionHeaderComment(); | |||
735 | OutStreamer->GetCommentOS() << '\n'; | |||
736 | } | |||
737 | ||||
738 | // Emit the prefix data. | |||
739 | if (F.hasPrefixData()) { | |||
740 | if (MAI->hasSubsectionsViaSymbols()) { | |||
741 | // Preserving prefix data on platforms which use subsections-via-symbols | |||
742 | // is a bit tricky. Here we introduce a symbol for the prefix data | |||
743 | // and use the .alt_entry attribute to mark the function's real entry point | |||
744 | // as an alternative entry point to the prefix-data symbol. | |||
745 | MCSymbol *PrefixSym = OutContext.createLinkerPrivateTempSymbol(); | |||
746 | OutStreamer->emitLabel(PrefixSym); | |||
747 | ||||
748 | emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData()); | |||
749 | ||||
750 | // Emit an .alt_entry directive for the actual function symbol. | |||
751 | OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_AltEntry); | |||
752 | } else { | |||
753 | emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData()); | |||
754 | } | |||
755 | } | |||
756 | ||||
757 | // Emit M NOPs for -fpatchable-function-entry=N,M where M>0. We arbitrarily | |||
758 | // place prefix data before NOPs. | |||
759 | unsigned PatchableFunctionPrefix = 0; | |||
760 | unsigned PatchableFunctionEntry = 0; | |||
761 | (void)F.getFnAttribute("patchable-function-prefix") | |||
762 | .getValueAsString() | |||
763 | .getAsInteger(10, PatchableFunctionPrefix); | |||
764 | (void)F.getFnAttribute("patchable-function-entry") | |||
765 | .getValueAsString() | |||
766 | .getAsInteger(10, PatchableFunctionEntry); | |||
767 | if (PatchableFunctionPrefix) { | |||
768 | CurrentPatchableFunctionEntrySym = | |||
769 | OutContext.createLinkerPrivateTempSymbol(); | |||
770 | OutStreamer->emitLabel(CurrentPatchableFunctionEntrySym); | |||
771 | emitNops(PatchableFunctionPrefix); | |||
772 | } else if (PatchableFunctionEntry) { | |||
773 | // May be reassigned when emitting the body, to reference the label after | |||
774 | // the initial BTI (AArch64) or endbr32/endbr64 (x86). | |||
775 | CurrentPatchableFunctionEntrySym = CurrentFnBegin; | |||
776 | } | |||
777 | ||||
778 | // Emit the function descriptor. This is a virtual function to allow targets | |||
779 | // to emit their specific function descriptor. Right now it is only used by | |||
780 | // the AIX target. The PowerPC 64-bit V1 ELF target also uses function | |||
781 | // descriptors and should be converted to use this hook as well. | |||
782 | if (MAI->needsFunctionDescriptors()) | |||
783 | emitFunctionDescriptor(); | |||
784 | ||||
785 | // Emit the CurrentFnSym. This is a virtual function to allow targets to do | |||
786 | // their wild and crazy things as required. | |||
787 | emitFunctionEntryLabel(); | |||
788 | ||||
789 | if (CurrentFnBegin) { | |||
790 | if (MAI->useAssignmentForEHBegin()) { | |||
791 | MCSymbol *CurPos = OutContext.createTempSymbol(); | |||
792 | OutStreamer->emitLabel(CurPos); | |||
793 | OutStreamer->emitAssignment(CurrentFnBegin, | |||
794 | MCSymbolRefExpr::create(CurPos, OutContext)); | |||
795 | } else { | |||
796 | OutStreamer->emitLabel(CurrentFnBegin); | |||
797 | } | |||
798 | } | |||
799 | ||||
800 | // Emit pre-function debug and/or EH information. | |||
801 | for (const HandlerInfo &HI : Handlers) { | |||
802 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
803 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
804 | HI.Handler->beginFunction(MF); | |||
805 | } | |||
806 | ||||
807 | // Emit the prologue data. | |||
808 | if (F.hasPrologueData()) | |||
809 | emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrologueData()); | |||
810 | } | |||
811 | ||||
812 | /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the | |||
813 | /// function. This can be overridden by targets as required to do custom stuff. | |||
814 | void AsmPrinter::emitFunctionEntryLabel() { | |||
815 | CurrentFnSym->redefineIfPossible(); | |||
816 | ||||
817 | // The function label could have already been emitted if two symbols end up | |||
818 | // conflicting due to asm renaming. Detect this and emit an error. | |||
819 | if (CurrentFnSym->isVariable()) | |||
820 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + | |||
821 | "' is a protected alias"); | |||
822 | if (CurrentFnSym->isDefined()) | |||
823 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + | |||
824 | "' label emitted multiple times to assembly file"); | |||
825 | ||||
826 | OutStreamer->emitLabel(CurrentFnSym); | |||
827 | ||||
828 | if (TM.getTargetTriple().isOSBinFormatELF()) { | |||
829 | MCSymbol *Sym = getSymbolPreferLocal(MF->getFunction()); | |||
830 | if (Sym != CurrentFnSym) | |||
831 | OutStreamer->emitLabel(Sym); | |||
832 | } | |||
833 | } | |||
834 | ||||
835 | /// emitComments - Pretty-print comments for instructions. | |||
836 | static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) { | |||
837 | const MachineFunction *MF = MI.getMF(); | |||
838 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); | |||
839 | ||||
840 | // Check for spills and reloads | |||
841 | ||||
842 | // We assume a single instruction only has a spill or reload, not | |||
843 | // both. | |||
844 | Optional<unsigned> Size; | |||
845 | if ((Size = MI.getRestoreSize(TII))) { | |||
846 | CommentOS << *Size << "-byte Reload\n"; | |||
847 | } else if ((Size = MI.getFoldedRestoreSize(TII))) { | |||
848 | if (*Size) { | |||
849 | if (*Size == unsigned(MemoryLocation::UnknownSize)) | |||
850 | CommentOS << "Unknown-size Folded Reload\n"; | |||
851 | else | |||
852 | CommentOS << *Size << "-byte Folded Reload\n"; | |||
853 | } | |||
854 | } else if ((Size = MI.getSpillSize(TII))) { | |||
855 | CommentOS << *Size << "-byte Spill\n"; | |||
856 | } else if ((Size = MI.getFoldedSpillSize(TII))) { | |||
857 | if (*Size) { | |||
858 | if (*Size == unsigned(MemoryLocation::UnknownSize)) | |||
859 | CommentOS << "Unknown-size Folded Spill\n"; | |||
860 | else | |||
861 | CommentOS << *Size << "-byte Folded Spill\n"; | |||
862 | } | |||
863 | } | |||
864 | ||||
865 | // Check for spill-induced copies | |||
866 | if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse)) | |||
867 | CommentOS << " Reload Reuse\n"; | |||
868 | } | |||
869 | ||||
870 | /// emitImplicitDef - This method emits the specified machine instruction | |||
871 | /// that is an implicit def. | |||
872 | void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const { | |||
873 | Register RegNo = MI->getOperand(0).getReg(); | |||
874 | ||||
875 | SmallString<128> Str; | |||
876 | raw_svector_ostream OS(Str); | |||
877 | OS << "implicit-def: " | |||
878 | << printReg(RegNo, MF->getSubtarget().getRegisterInfo()); | |||
879 | ||||
880 | OutStreamer->AddComment(OS.str()); | |||
881 | OutStreamer->AddBlankLine(); | |||
882 | } | |||
883 | ||||
884 | static void emitKill(const MachineInstr *MI, AsmPrinter &AP) { | |||
885 | std::string Str; | |||
886 | raw_string_ostream OS(Str); | |||
887 | OS << "kill:"; | |||
888 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | |||
889 | const MachineOperand &Op = MI->getOperand(i); | |||
890 | assert(Op.isReg() && "KILL instruction must have only register operands")((Op.isReg() && "KILL instruction must have only register operands" ) ? static_cast<void> (0) : __assert_fail ("Op.isReg() && \"KILL instruction must have only register operands\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 890, __PRETTY_FUNCTION__)); | |||
891 | OS << ' ' << (Op.isDef() ? "def " : "killed ") | |||
892 | << printReg(Op.getReg(), AP.MF->getSubtarget().getRegisterInfo()); | |||
893 | } | |||
894 | AP.OutStreamer->AddComment(OS.str()); | |||
895 | AP.OutStreamer->AddBlankLine(); | |||
896 | } | |||
897 | ||||
898 | /// emitDebugValueComment - This method handles the target-independent form | |||
899 | /// of DBG_VALUE, returning true if it was able to do so. A false return | |||
900 | /// means the target will need to handle MI in EmitInstruction. | |||
901 | static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) { | |||
902 | // This code handles only the 4-operand target-independent form. | |||
903 | if (MI->getNumOperands() != 4) | |||
904 | return false; | |||
905 | ||||
906 | SmallString<128> Str; | |||
907 | raw_svector_ostream OS(Str); | |||
908 | OS << "DEBUG_VALUE: "; | |||
909 | ||||
910 | const DILocalVariable *V = MI->getDebugVariable(); | |||
911 | if (auto *SP = dyn_cast<DISubprogram>(V->getScope())) { | |||
912 | StringRef Name = SP->getName(); | |||
913 | if (!Name.empty()) | |||
914 | OS << Name << ":"; | |||
915 | } | |||
916 | OS << V->getName(); | |||
917 | OS << " <- "; | |||
918 | ||||
919 | // The second operand is only an offset if it's an immediate. | |||
920 | bool MemLoc = MI->isIndirectDebugValue(); | |||
921 | auto Offset = StackOffset::getFixed(MemLoc ? MI->getOperand(1).getImm() : 0); | |||
922 | const DIExpression *Expr = MI->getDebugExpression(); | |||
923 | if (Expr->getNumElements()) { | |||
924 | OS << '['; | |||
925 | bool NeedSep = false; | |||
926 | for (auto Op : Expr->expr_ops()) { | |||
927 | if (NeedSep) | |||
928 | OS << ", "; | |||
929 | else | |||
930 | NeedSep = true; | |||
931 | OS << dwarf::OperationEncodingString(Op.getOp()); | |||
932 | for (unsigned I = 0; I < Op.getNumArgs(); ++I) | |||
933 | OS << ' ' << Op.getArg(I); | |||
934 | } | |||
935 | OS << "] "; | |||
936 | } | |||
937 | ||||
938 | // Register or immediate value. Register 0 means undef. | |||
939 | if (MI->getDebugOperand(0).isFPImm()) { | |||
940 | APFloat APF = APFloat(MI->getDebugOperand(0).getFPImm()->getValueAPF()); | |||
941 | if (MI->getDebugOperand(0).getFPImm()->getType()->isFloatTy()) { | |||
942 | OS << (double)APF.convertToFloat(); | |||
943 | } else if (MI->getDebugOperand(0).getFPImm()->getType()->isDoubleTy()) { | |||
944 | OS << APF.convertToDouble(); | |||
945 | } else { | |||
946 | // There is no good way to print long double. Convert a copy to | |||
947 | // double. Ah well, it's only a comment. | |||
948 | bool ignored; | |||
949 | APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, | |||
950 | &ignored); | |||
951 | OS << "(long double) " << APF.convertToDouble(); | |||
952 | } | |||
953 | } else if (MI->getDebugOperand(0).isImm()) { | |||
954 | OS << MI->getDebugOperand(0).getImm(); | |||
955 | } else if (MI->getDebugOperand(0).isCImm()) { | |||
956 | MI->getDebugOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/); | |||
957 | } else if (MI->getDebugOperand(0).isTargetIndex()) { | |||
958 | auto Op = MI->getDebugOperand(0); | |||
959 | OS << "!target-index(" << Op.getIndex() << "," << Op.getOffset() << ")"; | |||
960 | // NOTE: Want this comment at start of line, don't emit with AddComment. | |||
961 | AP.OutStreamer->emitRawComment(OS.str()); | |||
962 | return true; | |||
963 | } else { | |||
964 | Register Reg; | |||
965 | if (MI->getDebugOperand(0).isReg()) { | |||
966 | Reg = MI->getDebugOperand(0).getReg(); | |||
967 | } else { | |||
968 | assert(MI->getDebugOperand(0).isFI() && "Unknown operand type")((MI->getDebugOperand(0).isFI() && "Unknown operand type" ) ? static_cast<void> (0) : __assert_fail ("MI->getDebugOperand(0).isFI() && \"Unknown operand type\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 968, __PRETTY_FUNCTION__)); | |||
969 | const TargetFrameLowering *TFI = AP.MF->getSubtarget().getFrameLowering(); | |||
970 | Offset += TFI->getFrameIndexReference( | |||
971 | *AP.MF, MI->getDebugOperand(0).getIndex(), Reg); | |||
972 | MemLoc = true; | |||
973 | } | |||
974 | if (Reg == 0) { | |||
975 | // Suppress offset, it is not meaningful here. | |||
976 | OS << "undef"; | |||
977 | // NOTE: Want this comment at start of line, don't emit with AddComment. | |||
978 | AP.OutStreamer->emitRawComment(OS.str()); | |||
979 | return true; | |||
980 | } | |||
981 | if (MemLoc) | |||
982 | OS << '['; | |||
983 | OS << printReg(Reg, AP.MF->getSubtarget().getRegisterInfo()); | |||
984 | } | |||
985 | ||||
986 | if (MemLoc) | |||
987 | OS << '+' << Offset.getFixed() << ']'; | |||
988 | ||||
989 | // NOTE: Want this comment at start of line, don't emit with AddComment. | |||
990 | AP.OutStreamer->emitRawComment(OS.str()); | |||
991 | return true; | |||
992 | } | |||
993 | ||||
994 | /// This method handles the target-independent form of DBG_LABEL, returning | |||
995 | /// true if it was able to do so. A false return means the target will need | |||
996 | /// to handle MI in EmitInstruction. | |||
997 | static bool emitDebugLabelComment(const MachineInstr *MI, AsmPrinter &AP) { | |||
998 | if (MI->getNumOperands() != 1) | |||
999 | return false; | |||
1000 | ||||
1001 | SmallString<128> Str; | |||
1002 | raw_svector_ostream OS(Str); | |||
1003 | OS << "DEBUG_LABEL: "; | |||
1004 | ||||
1005 | const DILabel *V = MI->getDebugLabel(); | |||
1006 | if (auto *SP = dyn_cast<DISubprogram>( | |||
1007 | V->getScope()->getNonLexicalBlockFileScope())) { | |||
1008 | StringRef Name = SP->getName(); | |||
1009 | if (!Name.empty()) | |||
1010 | OS << Name << ":"; | |||
1011 | } | |||
1012 | OS << V->getName(); | |||
1013 | ||||
1014 | // NOTE: Want this comment at start of line, don't emit with AddComment. | |||
1015 | AP.OutStreamer->emitRawComment(OS.str()); | |||
1016 | return true; | |||
1017 | } | |||
1018 | ||||
1019 | AsmPrinter::CFIMoveType AsmPrinter::needsCFIMoves() const { | |||
1020 | if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI && | |||
1021 | MF->getFunction().needsUnwindTableEntry()) | |||
1022 | return CFI_M_EH; | |||
1023 | ||||
1024 | if (MMI->hasDebugInfo() || MF->getTarget().Options.ForceDwarfFrameSection) | |||
1025 | return CFI_M_Debug; | |||
1026 | ||||
1027 | return CFI_M_None; | |||
1028 | } | |||
1029 | ||||
1030 | bool AsmPrinter::needsSEHMoves() { | |||
1031 | return MAI->usesWindowsCFI() && MF->getFunction().needsUnwindTableEntry(); | |||
1032 | } | |||
1033 | ||||
1034 | void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) { | |||
1035 | ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType(); | |||
1036 | if (ExceptionHandlingType != ExceptionHandling::DwarfCFI && | |||
1037 | ExceptionHandlingType != ExceptionHandling::ARM) | |||
1038 | return; | |||
1039 | ||||
1040 | if (needsCFIMoves() == CFI_M_None) | |||
1041 | return; | |||
1042 | ||||
1043 | // If there is no "real" instruction following this CFI instruction, skip | |||
1044 | // emitting it; it would be beyond the end of the function's FDE range. | |||
1045 | auto *MBB = MI.getParent(); | |||
1046 | auto I = std::next(MI.getIterator()); | |||
1047 | while (I != MBB->end() && I->isTransient()) | |||
1048 | ++I; | |||
1049 | if (I == MBB->instr_end() && | |||
1050 | MBB->getReverseIterator() == MBB->getParent()->rbegin()) | |||
1051 | return; | |||
1052 | ||||
1053 | const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions(); | |||
1054 | unsigned CFIIndex = MI.getOperand(0).getCFIIndex(); | |||
1055 | const MCCFIInstruction &CFI = Instrs[CFIIndex]; | |||
1056 | emitCFIInstruction(CFI); | |||
1057 | } | |||
1058 | ||||
1059 | void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) { | |||
1060 | // The operands are the MCSymbol and the frame offset of the allocation. | |||
1061 | MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol(); | |||
1062 | int FrameOffset = MI.getOperand(1).getImm(); | |||
1063 | ||||
1064 | // Emit a symbol assignment. | |||
1065 | OutStreamer->emitAssignment(FrameAllocSym, | |||
1066 | MCConstantExpr::create(FrameOffset, OutContext)); | |||
1067 | } | |||
1068 | ||||
1069 | /// Returns the BB metadata to be emitted in the .llvm_bb_addr_map section for a | |||
1070 | /// given basic block. This can be used to capture more precise profile | |||
1071 | /// information. We use the last 3 bits (LSBs) to ecnode the following | |||
1072 | /// information: | |||
1073 | /// * (1): set if return block (ret or tail call). | |||
1074 | /// * (2): set if ends with a tail call. | |||
1075 | /// * (3): set if exception handling (EH) landing pad. | |||
1076 | /// The remaining bits are zero. | |||
1077 | static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) { | |||
1078 | const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo(); | |||
1079 | return ((unsigned)MBB.isReturnBlock()) | | |||
1080 | ((!MBB.empty() && TII->isTailCall(MBB.back())) << 1) | | |||
1081 | (MBB.isEHPad() << 2); | |||
1082 | } | |||
1083 | ||||
1084 | void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) { | |||
1085 | MCSection *BBAddrMapSection = | |||
1086 | getObjFileLowering().getBBAddrMapSection(*MF.getSection()); | |||
1087 | assert(BBAddrMapSection && ".llvm_bb_addr_map section is not initialized.")((BBAddrMapSection && ".llvm_bb_addr_map section is not initialized." ) ? static_cast<void> (0) : __assert_fail ("BBAddrMapSection && \".llvm_bb_addr_map section is not initialized.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1087, __PRETTY_FUNCTION__)); | |||
1088 | ||||
1089 | const MCSymbol *FunctionSymbol = getFunctionBegin(); | |||
1090 | ||||
1091 | OutStreamer->PushSection(); | |||
1092 | OutStreamer->SwitchSection(BBAddrMapSection); | |||
1093 | OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize()); | |||
1094 | // Emit the total number of basic blocks in this function. | |||
1095 | OutStreamer->emitULEB128IntValue(MF.size()); | |||
1096 | // Emit BB Information for each basic block in the funciton. | |||
1097 | for (const MachineBasicBlock &MBB : MF) { | |||
1098 | const MCSymbol *MBBSymbol = | |||
1099 | MBB.isEntryBlock() ? FunctionSymbol : MBB.getSymbol(); | |||
1100 | // Emit the basic block offset. | |||
1101 | emitLabelDifferenceAsULEB128(MBBSymbol, FunctionSymbol); | |||
1102 | // Emit the basic block size. When BBs have alignments, their size cannot | |||
1103 | // always be computed from their offsets. | |||
1104 | emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol); | |||
1105 | OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB)); | |||
1106 | } | |||
1107 | OutStreamer->PopSection(); | |||
1108 | } | |||
1109 | ||||
1110 | void AsmPrinter::emitPseudoProbe(const MachineInstr &MI) { | |||
1111 | auto GUID = MI.getOperand(0).getImm(); | |||
1112 | auto Index = MI.getOperand(1).getImm(); | |||
1113 | auto Type = MI.getOperand(2).getImm(); | |||
1114 | auto Attr = MI.getOperand(3).getImm(); | |||
1115 | DILocation *DebugLoc = MI.getDebugLoc(); | |||
1116 | PP->emitPseudoProbe(GUID, Index, Type, Attr, DebugLoc); | |||
1117 | } | |||
1118 | ||||
1119 | void AsmPrinter::emitStackSizeSection(const MachineFunction &MF) { | |||
1120 | if (!MF.getTarget().Options.EmitStackSizeSection) | |||
1121 | return; | |||
1122 | ||||
1123 | MCSection *StackSizeSection = | |||
1124 | getObjFileLowering().getStackSizesSection(*getCurrentSection()); | |||
1125 | if (!StackSizeSection) | |||
1126 | return; | |||
1127 | ||||
1128 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); | |||
1129 | // Don't emit functions with dynamic stack allocations. | |||
1130 | if (FrameInfo.hasVarSizedObjects()) | |||
1131 | return; | |||
1132 | ||||
1133 | OutStreamer->PushSection(); | |||
1134 | OutStreamer->SwitchSection(StackSizeSection); | |||
1135 | ||||
1136 | const MCSymbol *FunctionSymbol = getFunctionBegin(); | |||
1137 | uint64_t StackSize = FrameInfo.getStackSize(); | |||
1138 | OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize()); | |||
1139 | OutStreamer->emitULEB128IntValue(StackSize); | |||
1140 | ||||
1141 | OutStreamer->PopSection(); | |||
1142 | } | |||
1143 | ||||
1144 | static bool needFuncLabelsForEHOrDebugInfo(const MachineFunction &MF) { | |||
1145 | MachineModuleInfo &MMI = MF.getMMI(); | |||
1146 | if (!MF.getLandingPads().empty() || MF.hasEHFunclets() || MMI.hasDebugInfo()) | |||
1147 | return true; | |||
1148 | ||||
1149 | // We might emit an EH table that uses function begin and end labels even if | |||
1150 | // we don't have any landingpads. | |||
1151 | if (!MF.getFunction().hasPersonalityFn()) | |||
1152 | return false; | |||
1153 | return !isNoOpWithoutInvoke( | |||
1154 | classifyEHPersonality(MF.getFunction().getPersonalityFn())); | |||
1155 | } | |||
1156 | ||||
1157 | /// EmitFunctionBody - This method emits the body and trailer for a | |||
1158 | /// function. | |||
1159 | void AsmPrinter::emitFunctionBody() { | |||
1160 | emitFunctionHeader(); | |||
1161 | ||||
1162 | // Emit target-specific gunk before the function body. | |||
1163 | emitFunctionBodyStart(); | |||
1164 | ||||
1165 | if (isVerbose()) { | |||
1166 | // Get MachineDominatorTree or compute it on the fly if it's unavailable | |||
1167 | MDT = getAnalysisIfAvailable<MachineDominatorTree>(); | |||
1168 | if (!MDT) { | |||
1169 | OwnedMDT = std::make_unique<MachineDominatorTree>(); | |||
1170 | OwnedMDT->getBase().recalculate(*MF); | |||
1171 | MDT = OwnedMDT.get(); | |||
1172 | } | |||
1173 | ||||
1174 | // Get MachineLoopInfo or compute it on the fly if it's unavailable | |||
1175 | MLI = getAnalysisIfAvailable<MachineLoopInfo>(); | |||
1176 | if (!MLI) { | |||
1177 | OwnedMLI = std::make_unique<MachineLoopInfo>(); | |||
1178 | OwnedMLI->getBase().analyze(MDT->getBase()); | |||
1179 | MLI = OwnedMLI.get(); | |||
1180 | } | |||
1181 | } | |||
1182 | ||||
1183 | // Print out code for the function. | |||
1184 | bool HasAnyRealCode = false; | |||
1185 | int NumInstsInFunction = 0; | |||
1186 | ||||
1187 | bool CanDoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE"asm-printer"); | |||
1188 | for (auto &MBB : *MF) { | |||
1189 | // Print a label for the basic block. | |||
1190 | emitBasicBlockStart(MBB); | |||
1191 | DenseMap<StringRef, unsigned> MnemonicCounts; | |||
1192 | for (auto &MI : MBB) { | |||
1193 | // Print the assembly for the instruction. | |||
1194 | if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() && | |||
1195 | !MI.isDebugInstr()) { | |||
1196 | HasAnyRealCode = true; | |||
1197 | ++NumInstsInFunction; | |||
1198 | } | |||
1199 | ||||
1200 | // If there is a pre-instruction symbol, emit a label for it here. | |||
1201 | if (MCSymbol *S = MI.getPreInstrSymbol()) | |||
1202 | OutStreamer->emitLabel(S); | |||
1203 | ||||
1204 | for (const HandlerInfo &HI : Handlers) { | |||
1205 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
1206 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
1207 | HI.Handler->beginInstruction(&MI); | |||
1208 | } | |||
1209 | ||||
1210 | if (isVerbose()) | |||
1211 | emitComments(MI, OutStreamer->GetCommentOS()); | |||
1212 | ||||
1213 | switch (MI.getOpcode()) { | |||
1214 | case TargetOpcode::CFI_INSTRUCTION: | |||
1215 | emitCFIInstruction(MI); | |||
1216 | break; | |||
1217 | case TargetOpcode::LOCAL_ESCAPE: | |||
1218 | emitFrameAlloc(MI); | |||
1219 | break; | |||
1220 | case TargetOpcode::ANNOTATION_LABEL: | |||
1221 | case TargetOpcode::EH_LABEL: | |||
1222 | case TargetOpcode::GC_LABEL: | |||
1223 | OutStreamer->emitLabel(MI.getOperand(0).getMCSymbol()); | |||
1224 | break; | |||
1225 | case TargetOpcode::INLINEASM: | |||
1226 | case TargetOpcode::INLINEASM_BR: | |||
1227 | emitInlineAsm(&MI); | |||
1228 | break; | |||
1229 | case TargetOpcode::DBG_VALUE: | |||
1230 | if (isVerbose()) { | |||
1231 | if (!emitDebugValueComment(&MI, *this)) | |||
1232 | emitInstruction(&MI); | |||
1233 | } | |||
1234 | break; | |||
1235 | case TargetOpcode::DBG_INSTR_REF: | |||
1236 | // This instruction reference will have been resolved to a machine | |||
1237 | // location, and a nearby DBG_VALUE created. We can safely ignore | |||
1238 | // the instruction reference. | |||
1239 | break; | |||
1240 | case TargetOpcode::DBG_LABEL: | |||
1241 | if (isVerbose()) { | |||
1242 | if (!emitDebugLabelComment(&MI, *this)) | |||
1243 | emitInstruction(&MI); | |||
1244 | } | |||
1245 | break; | |||
1246 | case TargetOpcode::IMPLICIT_DEF: | |||
1247 | if (isVerbose()) emitImplicitDef(&MI); | |||
1248 | break; | |||
1249 | case TargetOpcode::KILL: | |||
1250 | if (isVerbose()) emitKill(&MI, *this); | |||
1251 | break; | |||
1252 | case TargetOpcode::PSEUDO_PROBE: | |||
1253 | emitPseudoProbe(MI); | |||
1254 | break; | |||
1255 | default: | |||
1256 | emitInstruction(&MI); | |||
1257 | if (CanDoExtraAnalysis) { | |||
1258 | MCInst MCI; | |||
1259 | MCI.setOpcode(MI.getOpcode()); | |||
1260 | auto Name = OutStreamer->getMnemonic(MCI); | |||
1261 | auto I = MnemonicCounts.insert({Name, 0u}); | |||
1262 | I.first->second++; | |||
1263 | } | |||
1264 | break; | |||
1265 | } | |||
1266 | ||||
1267 | // If there is a post-instruction symbol, emit a label for it here. | |||
1268 | if (MCSymbol *S = MI.getPostInstrSymbol()) | |||
1269 | OutStreamer->emitLabel(S); | |||
1270 | ||||
1271 | for (const HandlerInfo &HI : Handlers) { | |||
1272 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
1273 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
1274 | HI.Handler->endInstruction(); | |||
1275 | } | |||
1276 | } | |||
1277 | ||||
1278 | // We must emit temporary symbol for the end of this basic block, if either | |||
1279 | // we have BBLabels enabled or if this basic blocks marks the end of a | |||
1280 | // section (except the section containing the entry basic block as the end | |||
1281 | // symbol for that section is CurrentFnEnd). | |||
1282 | if (MF->hasBBLabels() || | |||
1283 | (MAI->hasDotTypeDotSizeDirective() && MBB.isEndSection() && | |||
1284 | !MBB.sameSection(&MF->front()))) | |||
1285 | OutStreamer->emitLabel(MBB.getEndSymbol()); | |||
1286 | ||||
1287 | if (MBB.isEndSection()) { | |||
1288 | // The size directive for the section containing the entry block is | |||
1289 | // handled separately by the function section. | |||
1290 | if (!MBB.sameSection(&MF->front())) { | |||
1291 | if (MAI->hasDotTypeDotSizeDirective()) { | |||
1292 | // Emit the size directive for the basic block section. | |||
1293 | const MCExpr *SizeExp = MCBinaryExpr::createSub( | |||
1294 | MCSymbolRefExpr::create(MBB.getEndSymbol(), OutContext), | |||
1295 | MCSymbolRefExpr::create(CurrentSectionBeginSym, OutContext), | |||
1296 | OutContext); | |||
1297 | OutStreamer->emitELFSize(CurrentSectionBeginSym, SizeExp); | |||
1298 | } | |||
1299 | MBBSectionRanges[MBB.getSectionIDNum()] = | |||
1300 | MBBSectionRange{CurrentSectionBeginSym, MBB.getEndSymbol()}; | |||
1301 | } | |||
1302 | } | |||
1303 | emitBasicBlockEnd(MBB); | |||
1304 | ||||
1305 | if (CanDoExtraAnalysis) { | |||
1306 | // Skip empty blocks. | |||
1307 | if (MBB.empty()) | |||
1308 | continue; | |||
1309 | ||||
1310 | MachineOptimizationRemarkAnalysis R(DEBUG_TYPE"asm-printer", "InstructionMix", | |||
1311 | MBB.begin()->getDebugLoc(), &MBB); | |||
1312 | ||||
1313 | // Generate instruction mix remark. First, sort counts in descending order | |||
1314 | // by count and name. | |||
1315 | SmallVector<std::pair<StringRef, unsigned>, 128> MnemonicVec; | |||
1316 | for (auto &KV : MnemonicCounts) | |||
1317 | MnemonicVec.emplace_back(KV.first, KV.second); | |||
1318 | ||||
1319 | sort(MnemonicVec, [](const std::pair<StringRef, unsigned> &A, | |||
1320 | const std::pair<StringRef, unsigned> &B) { | |||
1321 | if (A.second > B.second) | |||
1322 | return true; | |||
1323 | if (A.second == B.second) | |||
1324 | return StringRef(A.first) < StringRef(B.first); | |||
1325 | return false; | |||
1326 | }); | |||
1327 | R << "BasicBlock: " << ore::NV("BasicBlock", MBB.getName()) << "\n"; | |||
1328 | for (auto &KV : MnemonicVec) { | |||
1329 | auto Name = (Twine("INST_") + KV.first.trim()).str(); | |||
1330 | R << KV.first << ": " << ore::NV(Name, KV.second) << "\n"; | |||
1331 | } | |||
1332 | ORE->emit(R); | |||
1333 | } | |||
1334 | } | |||
1335 | ||||
1336 | EmittedInsts += NumInstsInFunction; | |||
1337 | MachineOptimizationRemarkAnalysis R(DEBUG_TYPE"asm-printer", "InstructionCount", | |||
1338 | MF->getFunction().getSubprogram(), | |||
1339 | &MF->front()); | |||
1340 | R << ore::NV("NumInstructions", NumInstsInFunction) | |||
1341 | << " instructions in function"; | |||
1342 | ORE->emit(R); | |||
1343 | ||||
1344 | // If the function is empty and the object file uses .subsections_via_symbols, | |||
1345 | // then we need to emit *something* to the function body to prevent the | |||
1346 | // labels from collapsing together. Just emit a noop. | |||
1347 | // Similarly, don't emit empty functions on Windows either. It can lead to | |||
1348 | // duplicate entries (two functions with the same RVA) in the Guard CF Table | |||
1349 | // after linking, causing the kernel not to load the binary: | |||
1350 | // https://developercommunity.visualstudio.com/content/problem/45366/vc-linker-creates-invalid-dll-with-clang-cl.html | |||
1351 | // FIXME: Hide this behind some API in e.g. MCAsmInfo or MCTargetStreamer. | |||
1352 | const Triple &TT = TM.getTargetTriple(); | |||
1353 | if (!HasAnyRealCode && (MAI->hasSubsectionsViaSymbols() || | |||
1354 | (TT.isOSWindows() && TT.isOSBinFormatCOFF()))) { | |||
1355 | MCInst Noop; | |||
1356 | MF->getSubtarget().getInstrInfo()->getNoop(Noop); | |||
1357 | ||||
1358 | // Targets can opt-out of emitting the noop here by leaving the opcode | |||
1359 | // unspecified. | |||
1360 | if (Noop.getOpcode()) { | |||
1361 | OutStreamer->AddComment("avoids zero-length function"); | |||
1362 | emitNops(1); | |||
1363 | } | |||
1364 | } | |||
1365 | ||||
1366 | // Switch to the original section in case basic block sections was used. | |||
1367 | OutStreamer->SwitchSection(MF->getSection()); | |||
1368 | ||||
1369 | const Function &F = MF->getFunction(); | |||
1370 | for (const auto &BB : F) { | |||
1371 | if (!BB.hasAddressTaken()) | |||
1372 | continue; | |||
1373 | MCSymbol *Sym = GetBlockAddressSymbol(&BB); | |||
1374 | if (Sym->isDefined()) | |||
1375 | continue; | |||
1376 | OutStreamer->AddComment("Address of block that was removed by CodeGen"); | |||
1377 | OutStreamer->emitLabel(Sym); | |||
1378 | } | |||
1379 | ||||
1380 | // Emit target-specific gunk after the function body. | |||
1381 | emitFunctionBodyEnd(); | |||
1382 | ||||
1383 | if (needFuncLabelsForEHOrDebugInfo(*MF) || | |||
1384 | MAI->hasDotTypeDotSizeDirective()) { | |||
1385 | // Create a symbol for the end of function. | |||
1386 | CurrentFnEnd = createTempSymbol("func_end"); | |||
1387 | OutStreamer->emitLabel(CurrentFnEnd); | |||
1388 | } | |||
1389 | ||||
1390 | // If the target wants a .size directive for the size of the function, emit | |||
1391 | // it. | |||
1392 | if (MAI->hasDotTypeDotSizeDirective()) { | |||
1393 | // We can get the size as difference between the function label and the | |||
1394 | // temp label. | |||
1395 | const MCExpr *SizeExp = MCBinaryExpr::createSub( | |||
1396 | MCSymbolRefExpr::create(CurrentFnEnd, OutContext), | |||
1397 | MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext); | |||
1398 | OutStreamer->emitELFSize(CurrentFnSym, SizeExp); | |||
1399 | } | |||
1400 | ||||
1401 | for (const HandlerInfo &HI : Handlers) { | |||
1402 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
1403 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
1404 | HI.Handler->markFunctionEnd(); | |||
1405 | } | |||
1406 | ||||
1407 | MBBSectionRanges[MF->front().getSectionIDNum()] = | |||
1408 | MBBSectionRange{CurrentFnBegin, CurrentFnEnd}; | |||
1409 | ||||
1410 | // Print out jump tables referenced by the function. | |||
1411 | emitJumpTableInfo(); | |||
1412 | ||||
1413 | // Emit post-function debug and/or EH information. | |||
1414 | for (const HandlerInfo &HI : Handlers) { | |||
1415 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
1416 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
1417 | HI.Handler->endFunction(MF); | |||
1418 | } | |||
1419 | ||||
1420 | // Emit section containing BB address offsets and their metadata, when | |||
1421 | // BB labels are requested for this function. | |||
1422 | if (MF->hasBBLabels()) | |||
1423 | emitBBAddrMapSection(*MF); | |||
1424 | ||||
1425 | // Emit section containing stack size metadata. | |||
1426 | emitStackSizeSection(*MF); | |||
1427 | ||||
1428 | emitPatchableFunctionEntries(); | |||
1429 | ||||
1430 | if (isVerbose()) | |||
1431 | OutStreamer->GetCommentOS() << "-- End function\n"; | |||
1432 | ||||
1433 | OutStreamer->AddBlankLine(); | |||
1434 | } | |||
1435 | ||||
1436 | /// Compute the number of Global Variables that uses a Constant. | |||
1437 | static unsigned getNumGlobalVariableUses(const Constant *C) { | |||
1438 | if (!C) | |||
1439 | return 0; | |||
1440 | ||||
1441 | if (isa<GlobalVariable>(C)) | |||
1442 | return 1; | |||
1443 | ||||
1444 | unsigned NumUses = 0; | |||
1445 | for (auto *CU : C->users()) | |||
1446 | NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU)); | |||
1447 | ||||
1448 | return NumUses; | |||
1449 | } | |||
1450 | ||||
1451 | /// Only consider global GOT equivalents if at least one user is a | |||
1452 | /// cstexpr inside an initializer of another global variables. Also, don't | |||
1453 | /// handle cstexpr inside instructions. During global variable emission, | |||
1454 | /// candidates are skipped and are emitted later in case at least one cstexpr | |||
1455 | /// isn't replaced by a PC relative GOT entry access. | |||
1456 | static bool isGOTEquivalentCandidate(const GlobalVariable *GV, | |||
1457 | unsigned &NumGOTEquivUsers) { | |||
1458 | // Global GOT equivalents are unnamed private globals with a constant | |||
1459 | // pointer initializer to another global symbol. They must point to a | |||
1460 | // GlobalVariable or Function, i.e., as GlobalValue. | |||
1461 | if (!GV->hasGlobalUnnamedAddr() || !GV->hasInitializer() || | |||
1462 | !GV->isConstant() || !GV->isDiscardableIfUnused() || | |||
1463 | !isa<GlobalValue>(GV->getOperand(0))) | |||
1464 | return false; | |||
1465 | ||||
1466 | // To be a got equivalent, at least one of its users need to be a constant | |||
1467 | // expression used by another global variable. | |||
1468 | for (auto *U : GV->users()) | |||
1469 | NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U)); | |||
1470 | ||||
1471 | return NumGOTEquivUsers > 0; | |||
1472 | } | |||
1473 | ||||
1474 | /// Unnamed constant global variables solely contaning a pointer to | |||
1475 | /// another globals variable is equivalent to a GOT table entry; it contains the | |||
1476 | /// the address of another symbol. Optimize it and replace accesses to these | |||
1477 | /// "GOT equivalents" by using the GOT entry for the final global instead. | |||
1478 | /// Compute GOT equivalent candidates among all global variables to avoid | |||
1479 | /// emitting them if possible later on, after it use is replaced by a GOT entry | |||
1480 | /// access. | |||
1481 | void AsmPrinter::computeGlobalGOTEquivs(Module &M) { | |||
1482 | if (!getObjFileLowering().supportIndirectSymViaGOTPCRel()) | |||
1483 | return; | |||
1484 | ||||
1485 | for (const auto &G : M.globals()) { | |||
1486 | unsigned NumGOTEquivUsers = 0; | |||
1487 | if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers)) | |||
1488 | continue; | |||
1489 | ||||
1490 | const MCSymbol *GOTEquivSym = getSymbol(&G); | |||
1491 | GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers); | |||
1492 | } | |||
1493 | } | |||
1494 | ||||
1495 | /// Constant expressions using GOT equivalent globals may not be eligible | |||
1496 | /// for PC relative GOT entry conversion, in such cases we need to emit such | |||
1497 | /// globals we previously omitted in EmitGlobalVariable. | |||
1498 | void AsmPrinter::emitGlobalGOTEquivs() { | |||
1499 | if (!getObjFileLowering().supportIndirectSymViaGOTPCRel()) | |||
1500 | return; | |||
1501 | ||||
1502 | SmallVector<const GlobalVariable *, 8> FailedCandidates; | |||
1503 | for (auto &I : GlobalGOTEquivs) { | |||
1504 | const GlobalVariable *GV = I.second.first; | |||
1505 | unsigned Cnt = I.second.second; | |||
1506 | if (Cnt) | |||
1507 | FailedCandidates.push_back(GV); | |||
1508 | } | |||
1509 | GlobalGOTEquivs.clear(); | |||
1510 | ||||
1511 | for (auto *GV : FailedCandidates) | |||
1512 | emitGlobalVariable(GV); | |||
1513 | } | |||
1514 | ||||
1515 | void AsmPrinter::emitGlobalIndirectSymbol(Module &M, | |||
1516 | const GlobalIndirectSymbol& GIS) { | |||
1517 | MCSymbol *Name = getSymbol(&GIS); | |||
1518 | bool IsFunction = GIS.getValueType()->isFunctionTy(); | |||
1519 | // Treat bitcasts of functions as functions also. This is important at least | |||
1520 | // on WebAssembly where object and function addresses can't alias each other. | |||
1521 | if (!IsFunction) | |||
1522 | if (auto *CE = dyn_cast<ConstantExpr>(GIS.getIndirectSymbol())) | |||
1523 | if (CE->getOpcode() == Instruction::BitCast) | |||
1524 | IsFunction = | |||
1525 | CE->getOperand(0)->getType()->getPointerElementType()->isFunctionTy(); | |||
1526 | ||||
1527 | // AIX's assembly directive `.set` is not usable for aliasing purpose, | |||
1528 | // so AIX has to use the extra-label-at-definition strategy. At this | |||
1529 | // point, all the extra label is emitted, we just have to emit linkage for | |||
1530 | // those labels. | |||
1531 | if (TM.getTargetTriple().isOSBinFormatXCOFF()) { | |||
1532 | assert(!isa<GlobalIFunc>(GIS) && "IFunc is not supported on AIX.")((!isa<GlobalIFunc>(GIS) && "IFunc is not supported on AIX." ) ? static_cast<void> (0) : __assert_fail ("!isa<GlobalIFunc>(GIS) && \"IFunc is not supported on AIX.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1532, __PRETTY_FUNCTION__)); | |||
1533 | assert(MAI->hasVisibilityOnlyWithLinkage() &&((MAI->hasVisibilityOnlyWithLinkage() && "Visibility should be handled with emitLinkage() on AIX." ) ? static_cast<void> (0) : __assert_fail ("MAI->hasVisibilityOnlyWithLinkage() && \"Visibility should be handled with emitLinkage() on AIX.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1534, __PRETTY_FUNCTION__)) | |||
1534 | "Visibility should be handled with emitLinkage() on AIX.")((MAI->hasVisibilityOnlyWithLinkage() && "Visibility should be handled with emitLinkage() on AIX." ) ? static_cast<void> (0) : __assert_fail ("MAI->hasVisibilityOnlyWithLinkage() && \"Visibility should be handled with emitLinkage() on AIX.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1534, __PRETTY_FUNCTION__)); | |||
1535 | emitLinkage(&GIS, Name); | |||
1536 | // If it's a function, also emit linkage for aliases of function entry | |||
1537 | // point. | |||
1538 | if (IsFunction) | |||
1539 | emitLinkage(&GIS, | |||
1540 | getObjFileLowering().getFunctionEntryPointSymbol(&GIS, TM)); | |||
1541 | return; | |||
1542 | } | |||
1543 | ||||
1544 | if (GIS.hasExternalLinkage() || !MAI->getWeakRefDirective()) | |||
1545 | OutStreamer->emitSymbolAttribute(Name, MCSA_Global); | |||
1546 | else if (GIS.hasWeakLinkage() || GIS.hasLinkOnceLinkage()) | |||
1547 | OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference); | |||
1548 | else | |||
1549 | assert(GIS.hasLocalLinkage() && "Invalid alias or ifunc linkage")((GIS.hasLocalLinkage() && "Invalid alias or ifunc linkage" ) ? static_cast<void> (0) : __assert_fail ("GIS.hasLocalLinkage() && \"Invalid alias or ifunc linkage\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1549, __PRETTY_FUNCTION__)); | |||
1550 | ||||
1551 | // Set the symbol type to function if the alias has a function type. | |||
1552 | // This affects codegen when the aliasee is not a function. | |||
1553 | if (IsFunction) | |||
1554 | OutStreamer->emitSymbolAttribute(Name, isa<GlobalIFunc>(GIS) | |||
1555 | ? MCSA_ELF_TypeIndFunction | |||
1556 | : MCSA_ELF_TypeFunction); | |||
1557 | ||||
1558 | emitVisibility(Name, GIS.getVisibility()); | |||
1559 | ||||
1560 | const MCExpr *Expr = lowerConstant(GIS.getIndirectSymbol()); | |||
1561 | ||||
1562 | if (isa<GlobalAlias>(&GIS) && MAI->hasAltEntry() && isa<MCBinaryExpr>(Expr)) | |||
1563 | OutStreamer->emitSymbolAttribute(Name, MCSA_AltEntry); | |||
1564 | ||||
1565 | // Emit the directives as assignments aka .set: | |||
1566 | OutStreamer->emitAssignment(Name, Expr); | |||
1567 | MCSymbol *LocalAlias = getSymbolPreferLocal(GIS); | |||
1568 | if (LocalAlias != Name) | |||
1569 | OutStreamer->emitAssignment(LocalAlias, Expr); | |||
1570 | ||||
1571 | if (auto *GA = dyn_cast<GlobalAlias>(&GIS)) { | |||
1572 | // If the aliasee does not correspond to a symbol in the output, i.e. the | |||
1573 | // alias is not of an object or the aliased object is private, then set the | |||
1574 | // size of the alias symbol from the type of the alias. We don't do this in | |||
1575 | // other situations as the alias and aliasee having differing types but same | |||
1576 | // size may be intentional. | |||
1577 | const GlobalObject *BaseObject = GA->getBaseObject(); | |||
1578 | if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized() && | |||
1579 | (!BaseObject || BaseObject->hasPrivateLinkage())) { | |||
1580 | const DataLayout &DL = M.getDataLayout(); | |||
1581 | uint64_t Size = DL.getTypeAllocSize(GA->getValueType()); | |||
1582 | OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext)); | |||
1583 | } | |||
1584 | } | |||
1585 | } | |||
1586 | ||||
1587 | void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) { | |||
1588 | if (!RS.needsSection()) | |||
1589 | return; | |||
1590 | ||||
1591 | remarks::RemarkSerializer &RemarkSerializer = RS.getSerializer(); | |||
1592 | ||||
1593 | Optional<SmallString<128>> Filename; | |||
1594 | if (Optional<StringRef> FilenameRef = RS.getFilename()) { | |||
1595 | Filename = *FilenameRef; | |||
1596 | sys::fs::make_absolute(*Filename); | |||
1597 | assert(!Filename->empty() && "The filename can't be empty.")((!Filename->empty() && "The filename can't be empty." ) ? static_cast<void> (0) : __assert_fail ("!Filename->empty() && \"The filename can't be empty.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1597, __PRETTY_FUNCTION__)); | |||
1598 | } | |||
1599 | ||||
1600 | std::string Buf; | |||
1601 | raw_string_ostream OS(Buf); | |||
1602 | std::unique_ptr<remarks::MetaSerializer> MetaSerializer = | |||
1603 | Filename ? RemarkSerializer.metaSerializer(OS, StringRef(*Filename)) | |||
1604 | : RemarkSerializer.metaSerializer(OS); | |||
1605 | MetaSerializer->emit(); | |||
1606 | ||||
1607 | // Switch to the remarks section. | |||
1608 | MCSection *RemarksSection = | |||
1609 | OutContext.getObjectFileInfo()->getRemarksSection(); | |||
1610 | OutStreamer->SwitchSection(RemarksSection); | |||
1611 | ||||
1612 | OutStreamer->emitBinaryData(OS.str()); | |||
1613 | } | |||
1614 | ||||
1615 | bool AsmPrinter::doFinalization(Module &M) { | |||
1616 | // Set the MachineFunction to nullptr so that we can catch attempted | |||
1617 | // accesses to MF specific features at the module level and so that | |||
1618 | // we can conditionalize accesses based on whether or not it is nullptr. | |||
1619 | MF = nullptr; | |||
1620 | ||||
1621 | // Gather all GOT equivalent globals in the module. We really need two | |||
1622 | // passes over the globals: one to compute and another to avoid its emission | |||
1623 | // in EmitGlobalVariable, otherwise we would not be able to handle cases | |||
1624 | // where the got equivalent shows up before its use. | |||
1625 | computeGlobalGOTEquivs(M); | |||
1626 | ||||
1627 | // Emit global variables. | |||
1628 | for (const auto &G : M.globals()) | |||
1629 | emitGlobalVariable(&G); | |||
1630 | ||||
1631 | // Emit remaining GOT equivalent globals. | |||
1632 | emitGlobalGOTEquivs(); | |||
1633 | ||||
1634 | const TargetLoweringObjectFile &TLOF = getObjFileLowering(); | |||
1635 | ||||
1636 | // Emit linkage(XCOFF) and visibility info for declarations | |||
1637 | for (const Function &F : M) { | |||
1638 | if (!F.isDeclarationForLinker()) | |||
1639 | continue; | |||
1640 | ||||
1641 | MCSymbol *Name = getSymbol(&F); | |||
1642 | // Function getSymbol gives us the function descriptor symbol for XCOFF. | |||
1643 | ||||
1644 | if (!TM.getTargetTriple().isOSBinFormatXCOFF()) { | |||
1645 | GlobalValue::VisibilityTypes V = F.getVisibility(); | |||
1646 | if (V == GlobalValue::DefaultVisibility) | |||
1647 | continue; | |||
1648 | ||||
1649 | emitVisibility(Name, V, false); | |||
1650 | continue; | |||
1651 | } | |||
1652 | ||||
1653 | if (F.isIntrinsic()) | |||
1654 | continue; | |||
1655 | ||||
1656 | // Handle the XCOFF case. | |||
1657 | // Variable `Name` is the function descriptor symbol (see above). Get the | |||
1658 | // function entry point symbol. | |||
1659 | MCSymbol *FnEntryPointSym = TLOF.getFunctionEntryPointSymbol(&F, TM); | |||
1660 | // Emit linkage for the function entry point. | |||
1661 | emitLinkage(&F, FnEntryPointSym); | |||
1662 | ||||
1663 | // Emit linkage for the function descriptor. | |||
1664 | emitLinkage(&F, Name); | |||
1665 | } | |||
1666 | ||||
1667 | // Emit the remarks section contents. | |||
1668 | // FIXME: Figure out when is the safest time to emit this section. It should | |||
1669 | // not come after debug info. | |||
1670 | if (remarks::RemarkStreamer *RS = M.getContext().getMainRemarkStreamer()) | |||
1671 | emitRemarksSection(*RS); | |||
1672 | ||||
1673 | TLOF.emitModuleMetadata(*OutStreamer, M); | |||
1674 | ||||
1675 | if (TM.getTargetTriple().isOSBinFormatELF()) { | |||
1676 | MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>(); | |||
1677 | ||||
1678 | // Output stubs for external and common global variables. | |||
1679 | MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList(); | |||
1680 | if (!Stubs.empty()) { | |||
1681 | OutStreamer->SwitchSection(TLOF.getDataSection()); | |||
1682 | const DataLayout &DL = M.getDataLayout(); | |||
1683 | ||||
1684 | emitAlignment(Align(DL.getPointerSize())); | |||
1685 | for (const auto &Stub : Stubs) { | |||
1686 | OutStreamer->emitLabel(Stub.first); | |||
1687 | OutStreamer->emitSymbolValue(Stub.second.getPointer(), | |||
1688 | DL.getPointerSize()); | |||
1689 | } | |||
1690 | } | |||
1691 | } | |||
1692 | ||||
1693 | if (TM.getTargetTriple().isOSBinFormatCOFF()) { | |||
1694 | MachineModuleInfoCOFF &MMICOFF = | |||
1695 | MMI->getObjFileInfo<MachineModuleInfoCOFF>(); | |||
1696 | ||||
1697 | // Output stubs for external and common global variables. | |||
1698 | MachineModuleInfoCOFF::SymbolListTy Stubs = MMICOFF.GetGVStubList(); | |||
1699 | if (!Stubs.empty()) { | |||
1700 | const DataLayout &DL = M.getDataLayout(); | |||
1701 | ||||
1702 | for (const auto &Stub : Stubs) { | |||
1703 | SmallString<256> SectionName = StringRef(".rdata$"); | |||
1704 | SectionName += Stub.first->getName(); | |||
1705 | OutStreamer->SwitchSection(OutContext.getCOFFSection( | |||
1706 | SectionName, | |||
1707 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | | |||
1708 | COFF::IMAGE_SCN_LNK_COMDAT, | |||
1709 | SectionKind::getReadOnly(), Stub.first->getName(), | |||
1710 | COFF::IMAGE_COMDAT_SELECT_ANY)); | |||
1711 | emitAlignment(Align(DL.getPointerSize())); | |||
1712 | OutStreamer->emitSymbolAttribute(Stub.first, MCSA_Global); | |||
1713 | OutStreamer->emitLabel(Stub.first); | |||
1714 | OutStreamer->emitSymbolValue(Stub.second.getPointer(), | |||
1715 | DL.getPointerSize()); | |||
1716 | } | |||
1717 | } | |||
1718 | } | |||
1719 | ||||
1720 | // Finalize debug and EH information. | |||
1721 | for (const HandlerInfo &HI : Handlers) { | |||
1722 | NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName, | |||
1723 | HI.TimerGroupDescription, TimePassesIsEnabled); | |||
1724 | HI.Handler->endModule(); | |||
1725 | } | |||
1726 | ||||
1727 | // This deletes all the ephemeral handlers that AsmPrinter added, while | |||
1728 | // keeping all the user-added handlers alive until the AsmPrinter is | |||
1729 | // destroyed. | |||
1730 | Handlers.erase(Handlers.begin() + NumUserHandlers, Handlers.end()); | |||
1731 | DD = nullptr; | |||
1732 | ||||
1733 | // If the target wants to know about weak references, print them all. | |||
1734 | if (MAI->getWeakRefDirective()) { | |||
1735 | // FIXME: This is not lazy, it would be nice to only print weak references | |||
1736 | // to stuff that is actually used. Note that doing so would require targets | |||
1737 | // to notice uses in operands (due to constant exprs etc). This should | |||
1738 | // happen with the MC stuff eventually. | |||
1739 | ||||
1740 | // Print out module-level global objects here. | |||
1741 | for (const auto &GO : M.global_objects()) { | |||
1742 | if (!GO.hasExternalWeakLinkage()) | |||
1743 | continue; | |||
1744 | OutStreamer->emitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference); | |||
1745 | } | |||
1746 | } | |||
1747 | ||||
1748 | // Print aliases in topological order, that is, for each alias a = b, | |||
1749 | // b must be printed before a. | |||
1750 | // This is because on some targets (e.g. PowerPC) linker expects aliases in | |||
1751 | // such an order to generate correct TOC information. | |||
1752 | SmallVector<const GlobalAlias *, 16> AliasStack; | |||
1753 | SmallPtrSet<const GlobalAlias *, 16> AliasVisited; | |||
1754 | for (const auto &Alias : M.aliases()) { | |||
1755 | for (const GlobalAlias *Cur = &Alias; Cur; | |||
1756 | Cur = dyn_cast<GlobalAlias>(Cur->getAliasee())) { | |||
1757 | if (!AliasVisited.insert(Cur).second) | |||
1758 | break; | |||
1759 | AliasStack.push_back(Cur); | |||
1760 | } | |||
1761 | for (const GlobalAlias *AncestorAlias : llvm::reverse(AliasStack)) | |||
1762 | emitGlobalIndirectSymbol(M, *AncestorAlias); | |||
1763 | AliasStack.clear(); | |||
1764 | } | |||
1765 | for (const auto &IFunc : M.ifuncs()) | |||
1766 | emitGlobalIndirectSymbol(M, IFunc); | |||
1767 | ||||
1768 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); | |||
1769 | assert(MI && "AsmPrinter didn't require GCModuleInfo?")((MI && "AsmPrinter didn't require GCModuleInfo?") ? static_cast <void> (0) : __assert_fail ("MI && \"AsmPrinter didn't require GCModuleInfo?\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1769, __PRETTY_FUNCTION__)); | |||
1770 | for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) | |||
1771 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(**--I)) | |||
1772 | MP->finishAssembly(M, *MI, *this); | |||
1773 | ||||
1774 | // Emit llvm.ident metadata in an '.ident' directive. | |||
1775 | emitModuleIdents(M); | |||
1776 | ||||
1777 | // Emit bytes for llvm.commandline metadata. | |||
1778 | emitModuleCommandLines(M); | |||
1779 | ||||
1780 | // Emit __morestack address if needed for indirect calls. | |||
1781 | if (MMI->usesMorestackAddr()) { | |||
1782 | Align Alignment(1); | |||
1783 | MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant( | |||
1784 | getDataLayout(), SectionKind::getReadOnly(), | |||
1785 | /*C=*/nullptr, Alignment); | |||
1786 | OutStreamer->SwitchSection(ReadOnlySection); | |||
1787 | ||||
1788 | MCSymbol *AddrSymbol = | |||
1789 | OutContext.getOrCreateSymbol(StringRef("__morestack_addr")); | |||
1790 | OutStreamer->emitLabel(AddrSymbol); | |||
1791 | ||||
1792 | unsigned PtrSize = MAI->getCodePointerSize(); | |||
1793 | OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("__morestack"), | |||
1794 | PtrSize); | |||
1795 | } | |||
1796 | ||||
1797 | // Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if | |||
1798 | // split-stack is used. | |||
1799 | if (TM.getTargetTriple().isOSBinFormatELF() && MMI->hasSplitStack()) { | |||
1800 | OutStreamer->SwitchSection( | |||
1801 | OutContext.getELFSection(".note.GNU-split-stack", ELF::SHT_PROGBITS, 0)); | |||
1802 | if (MMI->hasNosplitStack()) | |||
1803 | OutStreamer->SwitchSection( | |||
1804 | OutContext.getELFSection(".note.GNU-no-split-stack", ELF::SHT_PROGBITS, 0)); | |||
1805 | } | |||
1806 | ||||
1807 | // If we don't have any trampolines, then we don't require stack memory | |||
1808 | // to be executable. Some targets have a directive to declare this. | |||
1809 | Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); | |||
1810 | if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) | |||
1811 | if (MCSection *S = MAI->getNonexecutableStackSection(OutContext)) | |||
1812 | OutStreamer->SwitchSection(S); | |||
1813 | ||||
1814 | if (TM.Options.EmitAddrsig) { | |||
1815 | // Emit address-significance attributes for all globals. | |||
1816 | OutStreamer->emitAddrsig(); | |||
1817 | for (const GlobalValue &GV : M.global_values()) | |||
1818 | if (!GV.use_empty() && !GV.isThreadLocal() && | |||
1819 | !GV.hasDLLImportStorageClass() && !GV.getName().startswith("llvm.") && | |||
1820 | !GV.hasAtLeastLocalUnnamedAddr()) | |||
1821 | OutStreamer->emitAddrsigSym(getSymbol(&GV)); | |||
1822 | } | |||
1823 | ||||
1824 | // Emit symbol partition specifications (ELF only). | |||
1825 | if (TM.getTargetTriple().isOSBinFormatELF()) { | |||
1826 | unsigned UniqueID = 0; | |||
1827 | for (const GlobalValue &GV : M.global_values()) { | |||
1828 | if (!GV.hasPartition() || GV.isDeclarationForLinker() || | |||
1829 | GV.getVisibility() != GlobalValue::DefaultVisibility) | |||
1830 | continue; | |||
1831 | ||||
1832 | OutStreamer->SwitchSection( | |||
1833 | OutContext.getELFSection(".llvm_sympart", ELF::SHT_LLVM_SYMPART, 0, 0, | |||
1834 | "", ++UniqueID, nullptr)); | |||
1835 | OutStreamer->emitBytes(GV.getPartition()); | |||
1836 | OutStreamer->emitZeros(1); | |||
1837 | OutStreamer->emitValue( | |||
1838 | MCSymbolRefExpr::create(getSymbol(&GV), OutContext), | |||
1839 | MAI->getCodePointerSize()); | |||
1840 | } | |||
1841 | } | |||
1842 | ||||
1843 | // Allow the target to emit any magic that it wants at the end of the file, | |||
1844 | // after everything else has gone out. | |||
1845 | emitEndOfAsmFile(M); | |||
1846 | ||||
1847 | MMI = nullptr; | |||
1848 | ||||
1849 | OutStreamer->Finish(); | |||
1850 | OutStreamer->reset(); | |||
1851 | OwnedMLI.reset(); | |||
1852 | OwnedMDT.reset(); | |||
1853 | ||||
1854 | return false; | |||
1855 | } | |||
1856 | ||||
1857 | MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) { | |||
1858 | auto Res = MBBSectionExceptionSyms.try_emplace(MBB.getSectionIDNum()); | |||
1859 | if (Res.second) | |||
1860 | Res.first->second = createTempSymbol("exception"); | |||
1861 | return Res.first->second; | |||
1862 | } | |||
1863 | ||||
1864 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { | |||
1865 | this->MF = &MF; | |||
1866 | const Function &F = MF.getFunction(); | |||
1867 | ||||
1868 | // Get the function symbol. | |||
1869 | if (!MAI->needsFunctionDescriptors()) { | |||
1870 | CurrentFnSym = getSymbol(&MF.getFunction()); | |||
1871 | } else { | |||
1872 | assert(TM.getTargetTriple().isOSAIX() &&((TM.getTargetTriple().isOSAIX() && "Only AIX uses the function descriptor hooks." ) ? static_cast<void> (0) : __assert_fail ("TM.getTargetTriple().isOSAIX() && \"Only AIX uses the function descriptor hooks.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1873, __PRETTY_FUNCTION__)) | |||
1873 | "Only AIX uses the function descriptor hooks.")((TM.getTargetTriple().isOSAIX() && "Only AIX uses the function descriptor hooks." ) ? static_cast<void> (0) : __assert_fail ("TM.getTargetTriple().isOSAIX() && \"Only AIX uses the function descriptor hooks.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1873, __PRETTY_FUNCTION__)); | |||
1874 | // AIX is unique here in that the name of the symbol emitted for the | |||
1875 | // function body does not have the same name as the source function's | |||
1876 | // C-linkage name. | |||
1877 | assert(CurrentFnDescSym && "The function descriptor symbol needs to be"((CurrentFnDescSym && "The function descriptor symbol needs to be" " initalized first.") ? static_cast<void> (0) : __assert_fail ("CurrentFnDescSym && \"The function descriptor symbol needs to be\" \" initalized first.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1878, __PRETTY_FUNCTION__)) | |||
1878 | " initalized first.")((CurrentFnDescSym && "The function descriptor symbol needs to be" " initalized first.") ? static_cast<void> (0) : __assert_fail ("CurrentFnDescSym && \"The function descriptor symbol needs to be\" \" initalized first.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 1878, __PRETTY_FUNCTION__)); | |||
1879 | ||||
1880 | // Get the function entry point symbol. | |||
1881 | CurrentFnSym = getObjFileLowering().getFunctionEntryPointSymbol(&F, TM); | |||
1882 | } | |||
1883 | ||||
1884 | CurrentFnSymForSize = CurrentFnSym; | |||
1885 | CurrentFnBegin = nullptr; | |||
1886 | CurrentSectionBeginSym = nullptr; | |||
1887 | MBBSectionRanges.clear(); | |||
1888 | MBBSectionExceptionSyms.clear(); | |||
1889 | bool NeedsLocalForSize = MAI->needsLocalForSize(); | |||
1890 | if (F.hasFnAttribute("patchable-function-entry") || | |||
1891 | F.hasFnAttribute("function-instrument") || | |||
1892 | F.hasFnAttribute("xray-instruction-threshold") || | |||
1893 | needFuncLabelsForEHOrDebugInfo(MF) || NeedsLocalForSize || | |||
1894 | MF.getTarget().Options.EmitStackSizeSection || MF.hasBBLabels()) { | |||
1895 | CurrentFnBegin = createTempSymbol("func_begin"); | |||
1896 | if (NeedsLocalForSize) | |||
1897 | CurrentFnSymForSize = CurrentFnBegin; | |||
1898 | } | |||
1899 | ||||
1900 | ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE(); | |||
1901 | } | |||
1902 | ||||
1903 | namespace { | |||
1904 | ||||
1905 | // Keep track the alignment, constpool entries per Section. | |||
1906 | struct SectionCPs { | |||
1907 | MCSection *S; | |||
1908 | Align Alignment; | |||
1909 | SmallVector<unsigned, 4> CPEs; | |||
1910 | ||||
1911 | SectionCPs(MCSection *s, Align a) : S(s), Alignment(a) {} | |||
1912 | }; | |||
1913 | ||||
1914 | } // end anonymous namespace | |||
1915 | ||||
1916 | /// EmitConstantPool - Print to the current output stream assembly | |||
1917 | /// representations of the constants in the constant pool MCP. This is | |||
1918 | /// used to print out constants which have been "spilled to memory" by | |||
1919 | /// the code generator. | |||
1920 | void AsmPrinter::emitConstantPool() { | |||
1921 | const MachineConstantPool *MCP = MF->getConstantPool(); | |||
1922 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); | |||
1923 | if (CP.empty()) return; | |||
1924 | ||||
1925 | // Calculate sections for constant pool entries. We collect entries to go into | |||
1926 | // the same section together to reduce amount of section switch statements. | |||
1927 | SmallVector<SectionCPs, 4> CPSections; | |||
1928 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { | |||
1929 | const MachineConstantPoolEntry &CPE = CP[i]; | |||
1930 | Align Alignment = CPE.getAlign(); | |||
1931 | ||||
1932 | SectionKind Kind = CPE.getSectionKind(&getDataLayout()); | |||
1933 | ||||
1934 | const Constant *C = nullptr; | |||
1935 | if (!CPE.isMachineConstantPoolEntry()) | |||
1936 | C = CPE.Val.ConstVal; | |||
1937 | ||||
1938 | MCSection *S = getObjFileLowering().getSectionForConstant( | |||
1939 | getDataLayout(), Kind, C, Alignment); | |||
1940 | ||||
1941 | // The number of sections are small, just do a linear search from the | |||
1942 | // last section to the first. | |||
1943 | bool Found = false; | |||
1944 | unsigned SecIdx = CPSections.size(); | |||
1945 | while (SecIdx != 0) { | |||
1946 | if (CPSections[--SecIdx].S == S) { | |||
1947 | Found = true; | |||
1948 | break; | |||
1949 | } | |||
1950 | } | |||
1951 | if (!Found) { | |||
1952 | SecIdx = CPSections.size(); | |||
1953 | CPSections.push_back(SectionCPs(S, Alignment)); | |||
1954 | } | |||
1955 | ||||
1956 | if (Alignment > CPSections[SecIdx].Alignment) | |||
1957 | CPSections[SecIdx].Alignment = Alignment; | |||
1958 | CPSections[SecIdx].CPEs.push_back(i); | |||
1959 | } | |||
1960 | ||||
1961 | // Now print stuff into the calculated sections. | |||
1962 | const MCSection *CurSection = nullptr; | |||
1963 | unsigned Offset = 0; | |||
1964 | for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { | |||
1965 | for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { | |||
1966 | unsigned CPI = CPSections[i].CPEs[j]; | |||
1967 | MCSymbol *Sym = GetCPISymbol(CPI); | |||
1968 | if (!Sym->isUndefined()) | |||
1969 | continue; | |||
1970 | ||||
1971 | if (CurSection != CPSections[i].S) { | |||
1972 | OutStreamer->SwitchSection(CPSections[i].S); | |||
1973 | emitAlignment(Align(CPSections[i].Alignment)); | |||
1974 | CurSection = CPSections[i].S; | |||
1975 | Offset = 0; | |||
1976 | } | |||
1977 | ||||
1978 | MachineConstantPoolEntry CPE = CP[CPI]; | |||
1979 | ||||
1980 | // Emit inter-object padding for alignment. | |||
1981 | unsigned NewOffset = alignTo(Offset, CPE.getAlign()); | |||
1982 | OutStreamer->emitZeros(NewOffset - Offset); | |||
1983 | ||||
1984 | Offset = NewOffset + CPE.getSizeInBytes(getDataLayout()); | |||
1985 | ||||
1986 | OutStreamer->emitLabel(Sym); | |||
1987 | if (CPE.isMachineConstantPoolEntry()) | |||
1988 | emitMachineConstantPoolValue(CPE.Val.MachineCPVal); | |||
1989 | else | |||
1990 | emitGlobalConstant(getDataLayout(), CPE.Val.ConstVal); | |||
1991 | } | |||
1992 | } | |||
1993 | } | |||
1994 | ||||
1995 | // Print assembly representations of the jump tables used by the current | |||
1996 | // function. | |||
1997 | void AsmPrinter::emitJumpTableInfo() { | |||
1998 | const DataLayout &DL = MF->getDataLayout(); | |||
1999 | const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); | |||
2000 | if (!MJTI) return; | |||
2001 | if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return; | |||
2002 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); | |||
2003 | if (JT.empty()) return; | |||
2004 | ||||
2005 | // Pick the directive to use to print the jump table entries, and switch to | |||
2006 | // the appropriate section. | |||
2007 | const Function &F = MF->getFunction(); | |||
2008 | const TargetLoweringObjectFile &TLOF = getObjFileLowering(); | |||
2009 | bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection( | |||
2010 | MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32, | |||
2011 | F); | |||
2012 | if (JTInDiffSection) { | |||
2013 | // Drop it in the readonly section. | |||
2014 | MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(F, TM); | |||
2015 | OutStreamer->SwitchSection(ReadOnlySection); | |||
2016 | } | |||
2017 | ||||
2018 | emitAlignment(Align(MJTI->getEntryAlignment(DL))); | |||
2019 | ||||
2020 | // Jump tables in code sections are marked with a data_region directive | |||
2021 | // where that's supported. | |||
2022 | if (!JTInDiffSection) | |||
2023 | OutStreamer->emitDataRegion(MCDR_DataRegionJT32); | |||
2024 | ||||
2025 | for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) { | |||
2026 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; | |||
2027 | ||||
2028 | // If this jump table was deleted, ignore it. | |||
2029 | if (JTBBs.empty()) continue; | |||
2030 | ||||
2031 | // For the EK_LabelDifference32 entry, if using .set avoids a relocation, | |||
2032 | /// emit a .set directive for each unique entry. | |||
2033 | if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 && | |||
2034 | MAI->doesSetDirectiveSuppressReloc()) { | |||
2035 | SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets; | |||
2036 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); | |||
2037 | const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext); | |||
2038 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { | |||
2039 | const MachineBasicBlock *MBB = JTBBs[ii]; | |||
2040 | if (!EmittedSets.insert(MBB).second) | |||
2041 | continue; | |||
2042 | ||||
2043 | // .set LJTSet, LBB32-base | |||
2044 | const MCExpr *LHS = | |||
2045 | MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); | |||
2046 | OutStreamer->emitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()), | |||
2047 | MCBinaryExpr::createSub(LHS, Base, | |||
2048 | OutContext)); | |||
2049 | } | |||
2050 | } | |||
2051 | ||||
2052 | // On some targets (e.g. Darwin) we want to emit two consecutive labels | |||
2053 | // before each jump table. The first label is never referenced, but tells | |||
2054 | // the assembler and linker the extents of the jump table object. The | |||
2055 | // second label is actually referenced by the code. | |||
2056 | if (JTInDiffSection && DL.hasLinkerPrivateGlobalPrefix()) | |||
2057 | // FIXME: This doesn't have to have any specific name, just any randomly | |||
2058 | // named and numbered local label started with 'l' would work. Simplify | |||
2059 | // GetJTISymbol. | |||
2060 | OutStreamer->emitLabel(GetJTISymbol(JTI, true)); | |||
2061 | ||||
2062 | MCSymbol* JTISymbol = GetJTISymbol(JTI); | |||
2063 | OutStreamer->emitLabel(JTISymbol); | |||
2064 | ||||
2065 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) | |||
2066 | emitJumpTableEntry(MJTI, JTBBs[ii], JTI); | |||
2067 | } | |||
2068 | if (!JTInDiffSection) | |||
2069 | OutStreamer->emitDataRegion(MCDR_DataRegionEnd); | |||
2070 | } | |||
2071 | ||||
2072 | /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the | |||
2073 | /// current stream. | |||
2074 | void AsmPrinter::emitJumpTableEntry(const MachineJumpTableInfo *MJTI, | |||
2075 | const MachineBasicBlock *MBB, | |||
2076 | unsigned UID) const { | |||
2077 | assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block")((MBB && MBB->getNumber() >= 0 && "Invalid basic block" ) ? static_cast<void> (0) : __assert_fail ("MBB && MBB->getNumber() >= 0 && \"Invalid basic block\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2077, __PRETTY_FUNCTION__)); | |||
2078 | const MCExpr *Value = nullptr; | |||
2079 | switch (MJTI->getEntryKind()) { | |||
2080 | case MachineJumpTableInfo::EK_Inline: | |||
2081 | llvm_unreachable("Cannot emit EK_Inline jump table entry")::llvm::llvm_unreachable_internal("Cannot emit EK_Inline jump table entry" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2081); | |||
2082 | case MachineJumpTableInfo::EK_Custom32: | |||
2083 | Value = MF->getSubtarget().getTargetLowering()->LowerCustomJumpTableEntry( | |||
2084 | MJTI, MBB, UID, OutContext); | |||
2085 | break; | |||
2086 | case MachineJumpTableInfo::EK_BlockAddress: | |||
2087 | // EK_BlockAddress - Each entry is a plain address of block, e.g.: | |||
2088 | // .word LBB123 | |||
2089 | Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); | |||
2090 | break; | |||
2091 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: { | |||
2092 | // EK_GPRel32BlockAddress - Each entry is an address of block, encoded | |||
2093 | // with a relocation as gp-relative, e.g.: | |||
2094 | // .gprel32 LBB123 | |||
2095 | MCSymbol *MBBSym = MBB->getSymbol(); | |||
2096 | OutStreamer->emitGPRel32Value(MCSymbolRefExpr::create(MBBSym, OutContext)); | |||
2097 | return; | |||
2098 | } | |||
2099 | ||||
2100 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: { | |||
2101 | // EK_GPRel64BlockAddress - Each entry is an address of block, encoded | |||
2102 | // with a relocation as gp-relative, e.g.: | |||
2103 | // .gpdword LBB123 | |||
2104 | MCSymbol *MBBSym = MBB->getSymbol(); | |||
2105 | OutStreamer->emitGPRel64Value(MCSymbolRefExpr::create(MBBSym, OutContext)); | |||
2106 | return; | |||
2107 | } | |||
2108 | ||||
2109 | case MachineJumpTableInfo::EK_LabelDifference32: { | |||
2110 | // Each entry is the address of the block minus the address of the jump | |||
2111 | // table. This is used for PIC jump tables where gprel32 is not supported. | |||
2112 | // e.g.: | |||
2113 | // .word LBB123 - LJTI1_2 | |||
2114 | // If the .set directive avoids relocations, this is emitted as: | |||
2115 | // .set L4_5_set_123, LBB123 - LJTI1_2 | |||
2116 | // .word L4_5_set_123 | |||
2117 | if (MAI->doesSetDirectiveSuppressReloc()) { | |||
2118 | Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()), | |||
2119 | OutContext); | |||
2120 | break; | |||
2121 | } | |||
2122 | Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); | |||
2123 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); | |||
2124 | const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext); | |||
2125 | Value = MCBinaryExpr::createSub(Value, Base, OutContext); | |||
2126 | break; | |||
2127 | } | |||
2128 | } | |||
2129 | ||||
2130 | assert(Value && "Unknown entry kind!")((Value && "Unknown entry kind!") ? static_cast<void > (0) : __assert_fail ("Value && \"Unknown entry kind!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2130, __PRETTY_FUNCTION__)); | |||
2131 | ||||
2132 | unsigned EntrySize = MJTI->getEntrySize(getDataLayout()); | |||
2133 | OutStreamer->emitValue(Value, EntrySize); | |||
2134 | } | |||
2135 | ||||
2136 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a | |||
2137 | /// special global used by LLVM. If so, emit it and return true, otherwise | |||
2138 | /// do nothing and return false. | |||
2139 | bool AsmPrinter::emitSpecialLLVMGlobal(const GlobalVariable *GV) { | |||
2140 | if (GV->getName() == "llvm.used") { | |||
2141 | if (MAI->hasNoDeadStrip()) // No need to emit this at all. | |||
2142 | emitLLVMUsedList(cast<ConstantArray>(GV->getInitializer())); | |||
2143 | return true; | |||
2144 | } | |||
2145 | ||||
2146 | // Ignore debug and non-emitted data. This handles llvm.compiler.used. | |||
2147 | if (GV->getSection() == "llvm.metadata" || | |||
2148 | GV->hasAvailableExternallyLinkage()) | |||
2149 | return true; | |||
2150 | ||||
2151 | if (!GV->hasAppendingLinkage()) return false; | |||
2152 | ||||
2153 | assert(GV->hasInitializer() && "Not a special LLVM global!")((GV->hasInitializer() && "Not a special LLVM global!" ) ? static_cast<void> (0) : __assert_fail ("GV->hasInitializer() && \"Not a special LLVM global!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2153, __PRETTY_FUNCTION__)); | |||
2154 | ||||
2155 | if (GV->getName() == "llvm.global_ctors") { | |||
2156 | emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(), | |||
2157 | /* isCtor */ true); | |||
2158 | ||||
2159 | return true; | |||
2160 | } | |||
2161 | ||||
2162 | if (GV->getName() == "llvm.global_dtors") { | |||
2163 | emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(), | |||
2164 | /* isCtor */ false); | |||
2165 | ||||
2166 | return true; | |||
2167 | } | |||
2168 | ||||
2169 | report_fatal_error("unknown special variable"); | |||
2170 | } | |||
2171 | ||||
2172 | /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each | |||
2173 | /// global in the specified llvm.used list. | |||
2174 | void AsmPrinter::emitLLVMUsedList(const ConstantArray *InitList) { | |||
2175 | // Should be an array of 'i8*'. | |||
2176 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { | |||
2177 | const GlobalValue *GV = | |||
2178 | dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); | |||
2179 | if (GV) | |||
2180 | OutStreamer->emitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip); | |||
2181 | } | |||
2182 | } | |||
2183 | ||||
2184 | void AsmPrinter::preprocessXXStructorList(const DataLayout &DL, | |||
2185 | const Constant *List, | |||
2186 | SmallVector<Structor, 8> &Structors) { | |||
2187 | // Should be an array of '{ i32, void ()*, i8* }' structs. The first value is | |||
2188 | // the init priority. | |||
2189 | if (!isa<ConstantArray>(List)) | |||
2190 | return; | |||
2191 | ||||
2192 | // Gather the structors in a form that's convenient for sorting by priority. | |||
2193 | for (Value *O : cast<ConstantArray>(List)->operands()) { | |||
2194 | auto *CS = cast<ConstantStruct>(O); | |||
2195 | if (CS->getOperand(1)->isNullValue()) | |||
2196 | break; // Found a null terminator, skip the rest. | |||
2197 | ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0)); | |||
2198 | if (!Priority) | |||
2199 | continue; // Malformed. | |||
2200 | Structors.push_back(Structor()); | |||
2201 | Structor &S = Structors.back(); | |||
2202 | S.Priority = Priority->getLimitedValue(65535); | |||
2203 | S.Func = CS->getOperand(1); | |||
2204 | if (!CS->getOperand(2)->isNullValue()) { | |||
2205 | if (TM.getTargetTriple().isOSAIX()) | |||
2206 | llvm::report_fatal_error( | |||
2207 | "associated data of XXStructor list is not yet supported on AIX"); | |||
2208 | S.ComdatKey = | |||
2209 | dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts()); | |||
2210 | } | |||
2211 | } | |||
2212 | ||||
2213 | // Emit the function pointers in the target-specific order | |||
2214 | llvm::stable_sort(Structors, [](const Structor &L, const Structor &R) { | |||
2215 | return L.Priority < R.Priority; | |||
2216 | }); | |||
2217 | } | |||
2218 | ||||
2219 | /// EmitXXStructorList - Emit the ctor or dtor list taking into account the init | |||
2220 | /// priority. | |||
2221 | void AsmPrinter::emitXXStructorList(const DataLayout &DL, const Constant *List, | |||
2222 | bool IsCtor) { | |||
2223 | SmallVector<Structor, 8> Structors; | |||
2224 | preprocessXXStructorList(DL, List, Structors); | |||
2225 | if (Structors.empty()) | |||
2226 | return; | |||
2227 | ||||
2228 | const Align Align = DL.getPointerPrefAlignment(); | |||
2229 | for (Structor &S : Structors) { | |||
2230 | const TargetLoweringObjectFile &Obj = getObjFileLowering(); | |||
2231 | const MCSymbol *KeySym = nullptr; | |||
2232 | if (GlobalValue *GV = S.ComdatKey) { | |||
2233 | if (GV->isDeclarationForLinker()) | |||
2234 | // If the associated variable is not defined in this module | |||
2235 | // (it might be available_externally, or have been an | |||
2236 | // available_externally definition that was dropped by the | |||
2237 | // EliminateAvailableExternally pass), some other TU | |||
2238 | // will provide its dynamic initializer. | |||
2239 | continue; | |||
2240 | ||||
2241 | KeySym = getSymbol(GV); | |||
2242 | } | |||
2243 | ||||
2244 | MCSection *OutputSection = | |||
2245 | (IsCtor ? Obj.getStaticCtorSection(S.Priority, KeySym) | |||
2246 | : Obj.getStaticDtorSection(S.Priority, KeySym)); | |||
2247 | OutStreamer->SwitchSection(OutputSection); | |||
2248 | if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection()) | |||
2249 | emitAlignment(Align); | |||
2250 | emitXXStructor(DL, S.Func); | |||
2251 | } | |||
2252 | } | |||
2253 | ||||
2254 | void AsmPrinter::emitModuleIdents(Module &M) { | |||
2255 | if (!MAI->hasIdentDirective()) | |||
2256 | return; | |||
2257 | ||||
2258 | if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) { | |||
2259 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { | |||
2260 | const MDNode *N = NMD->getOperand(i); | |||
2261 | assert(N->getNumOperands() == 1 &&((N->getNumOperands() == 1 && "llvm.ident metadata entry can have only one operand" ) ? static_cast<void> (0) : __assert_fail ("N->getNumOperands() == 1 && \"llvm.ident metadata entry can have only one operand\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2262, __PRETTY_FUNCTION__)) | |||
2262 | "llvm.ident metadata entry can have only one operand")((N->getNumOperands() == 1 && "llvm.ident metadata entry can have only one operand" ) ? static_cast<void> (0) : __assert_fail ("N->getNumOperands() == 1 && \"llvm.ident metadata entry can have only one operand\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2262, __PRETTY_FUNCTION__)); | |||
2263 | const MDString *S = cast<MDString>(N->getOperand(0)); | |||
2264 | OutStreamer->emitIdent(S->getString()); | |||
2265 | } | |||
2266 | } | |||
2267 | } | |||
2268 | ||||
2269 | void AsmPrinter::emitModuleCommandLines(Module &M) { | |||
2270 | MCSection *CommandLine = getObjFileLowering().getSectionForCommandLines(); | |||
2271 | if (!CommandLine) | |||
2272 | return; | |||
2273 | ||||
2274 | const NamedMDNode *NMD = M.getNamedMetadata("llvm.commandline"); | |||
2275 | if (!NMD || !NMD->getNumOperands()) | |||
2276 | return; | |||
2277 | ||||
2278 | OutStreamer->PushSection(); | |||
2279 | OutStreamer->SwitchSection(CommandLine); | |||
2280 | OutStreamer->emitZeros(1); | |||
2281 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { | |||
2282 | const MDNode *N = NMD->getOperand(i); | |||
2283 | assert(N->getNumOperands() == 1 &&((N->getNumOperands() == 1 && "llvm.commandline metadata entry can have only one operand" ) ? static_cast<void> (0) : __assert_fail ("N->getNumOperands() == 1 && \"llvm.commandline metadata entry can have only one operand\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2284, __PRETTY_FUNCTION__)) | |||
2284 | "llvm.commandline metadata entry can have only one operand")((N->getNumOperands() == 1 && "llvm.commandline metadata entry can have only one operand" ) ? static_cast<void> (0) : __assert_fail ("N->getNumOperands() == 1 && \"llvm.commandline metadata entry can have only one operand\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2284, __PRETTY_FUNCTION__)); | |||
2285 | const MDString *S = cast<MDString>(N->getOperand(0)); | |||
2286 | OutStreamer->emitBytes(S->getString()); | |||
2287 | OutStreamer->emitZeros(1); | |||
2288 | } | |||
2289 | OutStreamer->PopSection(); | |||
2290 | } | |||
2291 | ||||
2292 | //===--------------------------------------------------------------------===// | |||
2293 | // Emission and print routines | |||
2294 | // | |||
2295 | ||||
2296 | /// Emit a byte directive and value. | |||
2297 | /// | |||
2298 | void AsmPrinter::emitInt8(int Value) const { OutStreamer->emitInt8(Value); } | |||
2299 | ||||
2300 | /// Emit a short directive and value. | |||
2301 | void AsmPrinter::emitInt16(int Value) const { OutStreamer->emitInt16(Value); } | |||
2302 | ||||
2303 | /// Emit a long directive and value. | |||
2304 | void AsmPrinter::emitInt32(int Value) const { OutStreamer->emitInt32(Value); } | |||
2305 | ||||
2306 | /// Emit a long long directive and value. | |||
2307 | void AsmPrinter::emitInt64(uint64_t Value) const { | |||
2308 | OutStreamer->emitInt64(Value); | |||
2309 | } | |||
2310 | ||||
2311 | /// Emit something like ".long Hi-Lo" where the size in bytes of the directive | |||
2312 | /// is specified by Size and Hi/Lo specify the labels. This implicitly uses | |||
2313 | /// .set if it avoids relocations. | |||
2314 | void AsmPrinter::emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, | |||
2315 | unsigned Size) const { | |||
2316 | OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, Size); | |||
2317 | } | |||
2318 | ||||
2319 | /// EmitLabelPlusOffset - Emit something like ".long Label+Offset" | |||
2320 | /// where the size in bytes of the directive is specified by Size and Label | |||
2321 | /// specifies the label. This implicitly uses .set if it is available. | |||
2322 | void AsmPrinter::emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, | |||
2323 | unsigned Size, | |||
2324 | bool IsSectionRelative) const { | |||
2325 | if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) { | |||
2326 | OutStreamer->EmitCOFFSecRel32(Label, Offset); | |||
2327 | if (Size > 4) | |||
2328 | OutStreamer->emitZeros(Size - 4); | |||
2329 | return; | |||
2330 | } | |||
2331 | ||||
2332 | // Emit Label+Offset (or just Label if Offset is zero) | |||
2333 | const MCExpr *Expr = MCSymbolRefExpr::create(Label, OutContext); | |||
2334 | if (Offset) | |||
2335 | Expr = MCBinaryExpr::createAdd( | |||
2336 | Expr, MCConstantExpr::create(Offset, OutContext), OutContext); | |||
2337 | ||||
2338 | OutStreamer->emitValue(Expr, Size); | |||
2339 | } | |||
2340 | ||||
2341 | //===----------------------------------------------------------------------===// | |||
2342 | ||||
2343 | // EmitAlignment - Emit an alignment directive to the specified power of | |||
2344 | // two boundary. If a global value is specified, and if that global has | |||
2345 | // an explicit alignment requested, it will override the alignment request | |||
2346 | // if required for correctness. | |||
2347 | void AsmPrinter::emitAlignment(Align Alignment, const GlobalObject *GV) const { | |||
2348 | if (GV) | |||
2349 | Alignment = getGVAlignment(GV, GV->getParent()->getDataLayout(), Alignment); | |||
2350 | ||||
2351 | if (Alignment == Align(1)) | |||
2352 | return; // 1-byte aligned: no need to emit alignment. | |||
2353 | ||||
2354 | if (getCurrentSection()->getKind().isText()) | |||
2355 | OutStreamer->emitCodeAlignment(Alignment.value()); | |||
2356 | else | |||
2357 | OutStreamer->emitValueToAlignment(Alignment.value()); | |||
2358 | } | |||
2359 | ||||
2360 | //===----------------------------------------------------------------------===// | |||
2361 | // Constant emission. | |||
2362 | //===----------------------------------------------------------------------===// | |||
2363 | ||||
2364 | const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) { | |||
2365 | MCContext &Ctx = OutContext; | |||
2366 | ||||
2367 | if (CV->isNullValue() || isa<UndefValue>(CV)) | |||
2368 | return MCConstantExpr::create(0, Ctx); | |||
2369 | ||||
2370 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) | |||
2371 | return MCConstantExpr::create(CI->getZExtValue(), Ctx); | |||
2372 | ||||
2373 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) | |||
2374 | return MCSymbolRefExpr::create(getSymbol(GV), Ctx); | |||
2375 | ||||
2376 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) | |||
2377 | return MCSymbolRefExpr::create(GetBlockAddressSymbol(BA), Ctx); | |||
2378 | ||||
2379 | if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) | |||
2380 | return getObjFileLowering().lowerDSOLocalEquivalent(Equiv, TM); | |||
2381 | ||||
2382 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); | |||
2383 | if (!CE) { | |||
2384 | llvm_unreachable("Unknown constant value to lower!")::llvm::llvm_unreachable_internal("Unknown constant value to lower!" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2384); | |||
2385 | } | |||
2386 | ||||
2387 | switch (CE->getOpcode()) { | |||
2388 | case Instruction::AddrSpaceCast: { | |||
2389 | const Constant *Op = CE->getOperand(0); | |||
2390 | unsigned DstAS = CE->getType()->getPointerAddressSpace(); | |||
2391 | unsigned SrcAS = Op->getType()->getPointerAddressSpace(); | |||
2392 | if (TM.isNoopAddrSpaceCast(SrcAS, DstAS)) | |||
2393 | return lowerConstant(Op); | |||
2394 | ||||
2395 | // Fallthrough to error. | |||
2396 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
2397 | } | |||
2398 | default: { | |||
2399 | // If the code isn't optimized, there may be outstanding folding | |||
2400 | // opportunities. Attempt to fold the expression using DataLayout as a | |||
2401 | // last resort before giving up. | |||
2402 | Constant *C = ConstantFoldConstant(CE, getDataLayout()); | |||
2403 | if (C != CE) | |||
2404 | return lowerConstant(C); | |||
2405 | ||||
2406 | // Otherwise report the problem to the user. | |||
2407 | std::string S; | |||
2408 | raw_string_ostream OS(S); | |||
2409 | OS << "Unsupported expression in static initializer: "; | |||
2410 | CE->printAsOperand(OS, /*PrintType=*/false, | |||
2411 | !MF ? nullptr : MF->getFunction().getParent()); | |||
2412 | report_fatal_error(OS.str()); | |||
2413 | } | |||
2414 | case Instruction::GetElementPtr: { | |||
2415 | // Generate a symbolic expression for the byte address | |||
2416 | APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0); | |||
2417 | cast<GEPOperator>(CE)->accumulateConstantOffset(getDataLayout(), OffsetAI); | |||
2418 | ||||
2419 | const MCExpr *Base = lowerConstant(CE->getOperand(0)); | |||
2420 | if (!OffsetAI) | |||
2421 | return Base; | |||
2422 | ||||
2423 | int64_t Offset = OffsetAI.getSExtValue(); | |||
2424 | return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx), | |||
2425 | Ctx); | |||
2426 | } | |||
2427 | ||||
2428 | case Instruction::Trunc: | |||
2429 | // We emit the value and depend on the assembler to truncate the generated | |||
2430 | // expression properly. This is important for differences between | |||
2431 | // blockaddress labels. Since the two labels are in the same function, it | |||
2432 | // is reasonable to treat their delta as a 32-bit value. | |||
2433 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
2434 | case Instruction::BitCast: | |||
2435 | return lowerConstant(CE->getOperand(0)); | |||
2436 | ||||
2437 | case Instruction::IntToPtr: { | |||
2438 | const DataLayout &DL = getDataLayout(); | |||
2439 | ||||
2440 | // Handle casts to pointers by changing them into casts to the appropriate | |||
2441 | // integer type. This promotes constant folding and simplifies this code. | |||
2442 | Constant *Op = CE->getOperand(0); | |||
2443 | Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()), | |||
2444 | false/*ZExt*/); | |||
2445 | return lowerConstant(Op); | |||
2446 | } | |||
2447 | ||||
2448 | case Instruction::PtrToInt: { | |||
2449 | const DataLayout &DL = getDataLayout(); | |||
2450 | ||||
2451 | // Support only foldable casts to/from pointers that can be eliminated by | |||
2452 | // changing the pointer to the appropriately sized integer type. | |||
2453 | Constant *Op = CE->getOperand(0); | |||
2454 | Type *Ty = CE->getType(); | |||
2455 | ||||
2456 | const MCExpr *OpExpr = lowerConstant(Op); | |||
2457 | ||||
2458 | // We can emit the pointer value into this slot if the slot is an | |||
2459 | // integer slot equal to the size of the pointer. | |||
2460 | // | |||
2461 | // If the pointer is larger than the resultant integer, then | |||
2462 | // as with Trunc just depend on the assembler to truncate it. | |||
2463 | if (DL.getTypeAllocSize(Ty).getFixedSize() <= | |||
2464 | DL.getTypeAllocSize(Op->getType()).getFixedSize()) | |||
2465 | return OpExpr; | |||
2466 | ||||
2467 | // Otherwise the pointer is smaller than the resultant integer, mask off | |||
2468 | // the high bits so we are sure to get a proper truncation if the input is | |||
2469 | // a constant expr. | |||
2470 | unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType()); | |||
2471 | const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx); | |||
2472 | return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx); | |||
2473 | } | |||
2474 | ||||
2475 | case Instruction::Sub: { | |||
2476 | GlobalValue *LHSGV; | |||
2477 | APInt LHSOffset; | |||
2478 | DSOLocalEquivalent *DSOEquiv; | |||
2479 | if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHSGV, LHSOffset, | |||
2480 | getDataLayout(), &DSOEquiv)) { | |||
2481 | GlobalValue *RHSGV; | |||
2482 | APInt RHSOffset; | |||
2483 | if (IsConstantOffsetFromGlobal(CE->getOperand(1), RHSGV, RHSOffset, | |||
2484 | getDataLayout())) { | |||
2485 | const MCExpr *RelocExpr = | |||
2486 | getObjFileLowering().lowerRelativeReference(LHSGV, RHSGV, TM); | |||
2487 | if (!RelocExpr) { | |||
2488 | const MCExpr *LHSExpr = | |||
2489 | MCSymbolRefExpr::create(getSymbol(LHSGV), Ctx); | |||
2490 | if (DSOEquiv && | |||
2491 | getObjFileLowering().supportDSOLocalEquivalentLowering()) | |||
2492 | LHSExpr = | |||
2493 | getObjFileLowering().lowerDSOLocalEquivalent(DSOEquiv, TM); | |||
2494 | RelocExpr = MCBinaryExpr::createSub( | |||
2495 | LHSExpr, MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx), Ctx); | |||
2496 | } | |||
2497 | int64_t Addend = (LHSOffset - RHSOffset).getSExtValue(); | |||
2498 | if (Addend != 0) | |||
2499 | RelocExpr = MCBinaryExpr::createAdd( | |||
2500 | RelocExpr, MCConstantExpr::create(Addend, Ctx), Ctx); | |||
2501 | return RelocExpr; | |||
2502 | } | |||
2503 | } | |||
2504 | } | |||
2505 | // else fallthrough | |||
2506 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
2507 | ||||
2508 | // The MC library also has a right-shift operator, but it isn't consistently | |||
2509 | // signed or unsigned between different targets. | |||
2510 | case Instruction::Add: | |||
2511 | case Instruction::Mul: | |||
2512 | case Instruction::SDiv: | |||
2513 | case Instruction::SRem: | |||
2514 | case Instruction::Shl: | |||
2515 | case Instruction::And: | |||
2516 | case Instruction::Or: | |||
2517 | case Instruction::Xor: { | |||
2518 | const MCExpr *LHS = lowerConstant(CE->getOperand(0)); | |||
2519 | const MCExpr *RHS = lowerConstant(CE->getOperand(1)); | |||
2520 | switch (CE->getOpcode()) { | |||
2521 | default: llvm_unreachable("Unknown binary operator constant cast expr")::llvm::llvm_unreachable_internal("Unknown binary operator constant cast expr" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2521); | |||
2522 | case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx); | |||
2523 | case Instruction::Sub: return MCBinaryExpr::createSub(LHS, RHS, Ctx); | |||
2524 | case Instruction::Mul: return MCBinaryExpr::createMul(LHS, RHS, Ctx); | |||
2525 | case Instruction::SDiv: return MCBinaryExpr::createDiv(LHS, RHS, Ctx); | |||
2526 | case Instruction::SRem: return MCBinaryExpr::createMod(LHS, RHS, Ctx); | |||
2527 | case Instruction::Shl: return MCBinaryExpr::createShl(LHS, RHS, Ctx); | |||
2528 | case Instruction::And: return MCBinaryExpr::createAnd(LHS, RHS, Ctx); | |||
2529 | case Instruction::Or: return MCBinaryExpr::createOr (LHS, RHS, Ctx); | |||
2530 | case Instruction::Xor: return MCBinaryExpr::createXor(LHS, RHS, Ctx); | |||
2531 | } | |||
2532 | } | |||
2533 | } | |||
2534 | } | |||
2535 | ||||
2536 | static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *C, | |||
2537 | AsmPrinter &AP, | |||
2538 | const Constant *BaseCV = nullptr, | |||
2539 | uint64_t Offset = 0); | |||
2540 | ||||
2541 | static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP); | |||
2542 | static void emitGlobalConstantFP(APFloat APF, Type *ET, AsmPrinter &AP); | |||
2543 | ||||
2544 | /// isRepeatedByteSequence - Determine whether the given value is | |||
2545 | /// composed of a repeated sequence of identical bytes and return the | |||
2546 | /// byte value. If it is not a repeated sequence, return -1. | |||
2547 | static int isRepeatedByteSequence(const ConstantDataSequential *V) { | |||
2548 | StringRef Data = V->getRawDataValues(); | |||
2549 | assert(!Data.empty() && "Empty aggregates should be CAZ node")((!Data.empty() && "Empty aggregates should be CAZ node" ) ? static_cast<void> (0) : __assert_fail ("!Data.empty() && \"Empty aggregates should be CAZ node\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2549, __PRETTY_FUNCTION__)); | |||
2550 | char C = Data[0]; | |||
2551 | for (unsigned i = 1, e = Data.size(); i != e; ++i) | |||
2552 | if (Data[i] != C) return -1; | |||
2553 | return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1. | |||
2554 | } | |||
2555 | ||||
2556 | /// isRepeatedByteSequence - Determine whether the given value is | |||
2557 | /// composed of a repeated sequence of identical bytes and return the | |||
2558 | /// byte value. If it is not a repeated sequence, return -1. | |||
2559 | static int isRepeatedByteSequence(const Value *V, const DataLayout &DL) { | |||
2560 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | |||
2561 | uint64_t Size = DL.getTypeAllocSizeInBits(V->getType()); | |||
2562 | assert(Size % 8 == 0)((Size % 8 == 0) ? static_cast<void> (0) : __assert_fail ("Size % 8 == 0", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2562, __PRETTY_FUNCTION__)); | |||
2563 | ||||
2564 | // Extend the element to take zero padding into account. | |||
2565 | APInt Value = CI->getValue().zextOrSelf(Size); | |||
2566 | if (!Value.isSplat(8)) | |||
2567 | return -1; | |||
2568 | ||||
2569 | return Value.zextOrTrunc(8).getZExtValue(); | |||
2570 | } | |||
2571 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) { | |||
2572 | // Make sure all array elements are sequences of the same repeated | |||
2573 | // byte. | |||
2574 | assert(CA->getNumOperands() != 0 && "Should be a CAZ")((CA->getNumOperands() != 0 && "Should be a CAZ") ? static_cast<void> (0) : __assert_fail ("CA->getNumOperands() != 0 && \"Should be a CAZ\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2574, __PRETTY_FUNCTION__)); | |||
2575 | Constant *Op0 = CA->getOperand(0); | |||
2576 | int Byte = isRepeatedByteSequence(Op0, DL); | |||
2577 | if (Byte == -1) | |||
2578 | return -1; | |||
2579 | ||||
2580 | // All array elements must be equal. | |||
2581 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) | |||
2582 | if (CA->getOperand(i) != Op0) | |||
2583 | return -1; | |||
2584 | return Byte; | |||
2585 | } | |||
2586 | ||||
2587 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) | |||
2588 | return isRepeatedByteSequence(CDS); | |||
2589 | ||||
2590 | return -1; | |||
2591 | } | |||
2592 | ||||
2593 | static void emitGlobalConstantDataSequential(const DataLayout &DL, | |||
2594 | const ConstantDataSequential *CDS, | |||
2595 | AsmPrinter &AP) { | |||
2596 | // See if we can aggregate this into a .fill, if so, emit it as such. | |||
2597 | int Value = isRepeatedByteSequence(CDS, DL); | |||
2598 | if (Value != -1) { | |||
2599 | uint64_t Bytes = DL.getTypeAllocSize(CDS->getType()); | |||
2600 | // Don't emit a 1-byte object as a .fill. | |||
2601 | if (Bytes > 1) | |||
2602 | return AP.OutStreamer->emitFill(Bytes, Value); | |||
2603 | } | |||
2604 | ||||
2605 | // If this can be emitted with .ascii/.asciz, emit it as such. | |||
2606 | if (CDS->isString()) | |||
2607 | return AP.OutStreamer->emitBytes(CDS->getAsString()); | |||
2608 | ||||
2609 | // Otherwise, emit the values in successive locations. | |||
2610 | unsigned ElementByteSize = CDS->getElementByteSize(); | |||
2611 | if (isa<IntegerType>(CDS->getElementType())) { | |||
2612 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { | |||
2613 | if (AP.isVerbose()) | |||
2614 | AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64"l" "x" "\n", | |||
2615 | CDS->getElementAsInteger(i)); | |||
2616 | AP.OutStreamer->emitIntValue(CDS->getElementAsInteger(i), | |||
2617 | ElementByteSize); | |||
2618 | } | |||
2619 | } else { | |||
2620 | Type *ET = CDS->getElementType(); | |||
2621 | for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) | |||
2622 | emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP); | |||
2623 | } | |||
2624 | ||||
2625 | unsigned Size = DL.getTypeAllocSize(CDS->getType()); | |||
2626 | unsigned EmittedSize = | |||
2627 | DL.getTypeAllocSize(CDS->getElementType()) * CDS->getNumElements(); | |||
2628 | assert(EmittedSize <= Size && "Size cannot be less than EmittedSize!")((EmittedSize <= Size && "Size cannot be less than EmittedSize!" ) ? static_cast<void> (0) : __assert_fail ("EmittedSize <= Size && \"Size cannot be less than EmittedSize!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2628, __PRETTY_FUNCTION__)); | |||
2629 | if (unsigned Padding = Size - EmittedSize) | |||
2630 | AP.OutStreamer->emitZeros(Padding); | |||
2631 | } | |||
2632 | ||||
2633 | static void emitGlobalConstantArray(const DataLayout &DL, | |||
2634 | const ConstantArray *CA, AsmPrinter &AP, | |||
2635 | const Constant *BaseCV, uint64_t Offset) { | |||
2636 | // See if we can aggregate some values. Make sure it can be | |||
2637 | // represented as a series of bytes of the constant value. | |||
2638 | int Value = isRepeatedByteSequence(CA, DL); | |||
2639 | ||||
2640 | if (Value != -1) { | |||
2641 | uint64_t Bytes = DL.getTypeAllocSize(CA->getType()); | |||
2642 | AP.OutStreamer->emitFill(Bytes, Value); | |||
2643 | } | |||
2644 | else { | |||
2645 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { | |||
2646 | emitGlobalConstantImpl(DL, CA->getOperand(i), AP, BaseCV, Offset); | |||
2647 | Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType()); | |||
2648 | } | |||
2649 | } | |||
2650 | } | |||
2651 | ||||
2652 | static void emitGlobalConstantVector(const DataLayout &DL, | |||
2653 | const ConstantVector *CV, AsmPrinter &AP) { | |||
2654 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) | |||
2655 | emitGlobalConstantImpl(DL, CV->getOperand(i), AP); | |||
2656 | ||||
2657 | unsigned Size = DL.getTypeAllocSize(CV->getType()); | |||
2658 | unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) * | |||
2659 | CV->getType()->getNumElements(); | |||
2660 | if (unsigned Padding = Size - EmittedSize) | |||
2661 | AP.OutStreamer->emitZeros(Padding); | |||
2662 | } | |||
2663 | ||||
2664 | static void emitGlobalConstantStruct(const DataLayout &DL, | |||
2665 | const ConstantStruct *CS, AsmPrinter &AP, | |||
2666 | const Constant *BaseCV, uint64_t Offset) { | |||
2667 | // Print the fields in successive locations. Pad to align if needed! | |||
2668 | unsigned Size = DL.getTypeAllocSize(CS->getType()); | |||
2669 | const StructLayout *Layout = DL.getStructLayout(CS->getType()); | |||
2670 | uint64_t SizeSoFar = 0; | |||
2671 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { | |||
2672 | const Constant *Field = CS->getOperand(i); | |||
2673 | ||||
2674 | // Print the actual field value. | |||
2675 | emitGlobalConstantImpl(DL, Field, AP, BaseCV, Offset + SizeSoFar); | |||
2676 | ||||
2677 | // Check if padding is needed and insert one or more 0s. | |||
2678 | uint64_t FieldSize = DL.getTypeAllocSize(Field->getType()); | |||
2679 | uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1)) | |||
2680 | - Layout->getElementOffset(i)) - FieldSize; | |||
2681 | SizeSoFar += FieldSize + PadSize; | |||
2682 | ||||
2683 | // Insert padding - this may include padding to increase the size of the | |||
2684 | // current field up to the ABI size (if the struct is not packed) as well | |||
2685 | // as padding to ensure that the next field starts at the right offset. | |||
2686 | AP.OutStreamer->emitZeros(PadSize); | |||
2687 | } | |||
2688 | assert(SizeSoFar == Layout->getSizeInBytes() &&((SizeSoFar == Layout->getSizeInBytes() && "Layout of constant struct may be incorrect!" ) ? static_cast<void> (0) : __assert_fail ("SizeSoFar == Layout->getSizeInBytes() && \"Layout of constant struct may be incorrect!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2689, __PRETTY_FUNCTION__)) | |||
2689 | "Layout of constant struct may be incorrect!")((SizeSoFar == Layout->getSizeInBytes() && "Layout of constant struct may be incorrect!" ) ? static_cast<void> (0) : __assert_fail ("SizeSoFar == Layout->getSizeInBytes() && \"Layout of constant struct may be incorrect!\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2689, __PRETTY_FUNCTION__)); | |||
2690 | } | |||
2691 | ||||
2692 | static void emitGlobalConstantFP(APFloat APF, Type *ET, AsmPrinter &AP) { | |||
2693 | assert(ET && "Unknown float type")((ET && "Unknown float type") ? static_cast<void> (0) : __assert_fail ("ET && \"Unknown float type\"", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2693, __PRETTY_FUNCTION__)); | |||
2694 | APInt API = APF.bitcastToAPInt(); | |||
2695 | ||||
2696 | // First print a comment with what we think the original floating-point value | |||
2697 | // should have been. | |||
2698 | if (AP.isVerbose()) { | |||
2699 | SmallString<8> StrVal; | |||
2700 | APF.toString(StrVal); | |||
2701 | ET->print(AP.OutStreamer->GetCommentOS()); | |||
2702 | AP.OutStreamer->GetCommentOS() << ' ' << StrVal << '\n'; | |||
2703 | } | |||
2704 | ||||
2705 | // Now iterate through the APInt chunks, emitting them in endian-correct | |||
2706 | // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit | |||
2707 | // floats). | |||
2708 | unsigned NumBytes = API.getBitWidth() / 8; | |||
2709 | unsigned TrailingBytes = NumBytes % sizeof(uint64_t); | |||
2710 | const uint64_t *p = API.getRawData(); | |||
2711 | ||||
2712 | // PPC's long double has odd notions of endianness compared to how LLVM | |||
2713 | // handles it: p[0] goes first for *big* endian on PPC. | |||
2714 | if (AP.getDataLayout().isBigEndian() && !ET->isPPC_FP128Ty()) { | |||
2715 | int Chunk = API.getNumWords() - 1; | |||
2716 | ||||
2717 | if (TrailingBytes) | |||
2718 | AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk--], TrailingBytes); | |||
2719 | ||||
2720 | for (; Chunk >= 0; --Chunk) | |||
2721 | AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], sizeof(uint64_t)); | |||
2722 | } else { | |||
2723 | unsigned Chunk; | |||
2724 | for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk) | |||
2725 | AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], sizeof(uint64_t)); | |||
2726 | ||||
2727 | if (TrailingBytes) | |||
2728 | AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], TrailingBytes); | |||
2729 | } | |||
2730 | ||||
2731 | // Emit the tail padding for the long double. | |||
2732 | const DataLayout &DL = AP.getDataLayout(); | |||
2733 | AP.OutStreamer->emitZeros(DL.getTypeAllocSize(ET) - DL.getTypeStoreSize(ET)); | |||
2734 | } | |||
2735 | ||||
2736 | static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) { | |||
2737 | emitGlobalConstantFP(CFP->getValueAPF(), CFP->getType(), AP); | |||
2738 | } | |||
2739 | ||||
2740 | static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) { | |||
2741 | const DataLayout &DL = AP.getDataLayout(); | |||
2742 | unsigned BitWidth = CI->getBitWidth(); | |||
2743 | ||||
2744 | // Copy the value as we may massage the layout for constants whose bit width | |||
2745 | // is not a multiple of 64-bits. | |||
2746 | APInt Realigned(CI->getValue()); | |||
2747 | uint64_t ExtraBits = 0; | |||
2748 | unsigned ExtraBitsSize = BitWidth & 63; | |||
2749 | ||||
2750 | if (ExtraBitsSize) { | |||
2751 | // The bit width of the data is not a multiple of 64-bits. | |||
2752 | // The extra bits are expected to be at the end of the chunk of the memory. | |||
2753 | // Little endian: | |||
2754 | // * Nothing to be done, just record the extra bits to emit. | |||
2755 | // Big endian: | |||
2756 | // * Record the extra bits to emit. | |||
2757 | // * Realign the raw data to emit the chunks of 64-bits. | |||
2758 | if (DL.isBigEndian()) { | |||
2759 | // Basically the structure of the raw data is a chunk of 64-bits cells: | |||
2760 | // 0 1 BitWidth / 64 | |||
2761 | // [chunk1][chunk2] ... [chunkN]. | |||
2762 | // The most significant chunk is chunkN and it should be emitted first. | |||
2763 | // However, due to the alignment issue chunkN contains useless bits. | |||
2764 | // Realign the chunks so that they contain only useful information: | |||
2765 | // ExtraBits 0 1 (BitWidth / 64) - 1 | |||
2766 | // chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN] | |||
2767 | ExtraBitsSize = alignTo(ExtraBitsSize, 8); | |||
2768 | ExtraBits = Realigned.getRawData()[0] & | |||
2769 | (((uint64_t)-1) >> (64 - ExtraBitsSize)); | |||
2770 | Realigned.lshrInPlace(ExtraBitsSize); | |||
2771 | } else | |||
2772 | ExtraBits = Realigned.getRawData()[BitWidth / 64]; | |||
2773 | } | |||
2774 | ||||
2775 | // We don't expect assemblers to support integer data directives | |||
2776 | // for more than 64 bits, so we emit the data in at most 64-bit | |||
2777 | // quantities at a time. | |||
2778 | const uint64_t *RawData = Realigned.getRawData(); | |||
2779 | for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { | |||
2780 | uint64_t Val = DL.isBigEndian() ? RawData[e - i - 1] : RawData[i]; | |||
2781 | AP.OutStreamer->emitIntValue(Val, 8); | |||
2782 | } | |||
2783 | ||||
2784 | if (ExtraBitsSize) { | |||
2785 | // Emit the extra bits after the 64-bits chunks. | |||
2786 | ||||
2787 | // Emit a directive that fills the expected size. | |||
2788 | uint64_t Size = AP.getDataLayout().getTypeStoreSize(CI->getType()); | |||
2789 | Size -= (BitWidth / 64) * 8; | |||
2790 | assert(Size && Size * 8 >= ExtraBitsSize &&((Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && "Directive too small for extra bits.") ? static_cast <void> (0) : __assert_fail ("Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && \"Directive too small for extra bits.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2792, __PRETTY_FUNCTION__)) | |||
2791 | (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))((Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && "Directive too small for extra bits.") ? static_cast <void> (0) : __assert_fail ("Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && \"Directive too small for extra bits.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2792, __PRETTY_FUNCTION__)) | |||
2792 | == ExtraBits && "Directive too small for extra bits.")((Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && "Directive too small for extra bits.") ? static_cast <void> (0) : __assert_fail ("Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) == ExtraBits && \"Directive too small for extra bits.\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2792, __PRETTY_FUNCTION__)); | |||
2793 | AP.OutStreamer->emitIntValue(ExtraBits, Size); | |||
2794 | } | |||
2795 | } | |||
2796 | ||||
2797 | /// Transform a not absolute MCExpr containing a reference to a GOT | |||
2798 | /// equivalent global, by a target specific GOT pc relative access to the | |||
2799 | /// final symbol. | |||
2800 | static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME, | |||
2801 | const Constant *BaseCst, | |||
2802 | uint64_t Offset) { | |||
2803 | // The global @foo below illustrates a global that uses a got equivalent. | |||
2804 | // | |||
2805 | // @bar = global i32 42 | |||
2806 | // @gotequiv = private unnamed_addr constant i32* @bar | |||
2807 | // @foo = i32 trunc (i64 sub (i64 ptrtoint (i32** @gotequiv to i64), | |||
2808 | // i64 ptrtoint (i32* @foo to i64)) | |||
2809 | // to i32) | |||
2810 | // | |||
2811 | // The cstexpr in @foo is converted into the MCExpr `ME`, where we actually | |||
2812 | // check whether @foo is suitable to use a GOTPCREL. `ME` is usually in the | |||
2813 | // form: | |||
2814 | // | |||
2815 | // foo = cstexpr, where | |||
2816 | // cstexpr := <gotequiv> - "." + <cst> | |||
2817 | // cstexpr := <gotequiv> - (<foo> - <offset from @foo base>) + <cst> | |||
2818 | // | |||
2819 | // After canonicalization by evaluateAsRelocatable `ME` turns into: | |||
2820 | // | |||
2821 | // cstexpr := <gotequiv> - <foo> + gotpcrelcst, where | |||
2822 | // gotpcrelcst := <offset from @foo base> + <cst> | |||
2823 | MCValue MV; | |||
2824 | if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute()) | |||
2825 | return; | |||
2826 | const MCSymbolRefExpr *SymA = MV.getSymA(); | |||
2827 | if (!SymA) | |||
2828 | return; | |||
2829 | ||||
2830 | // Check that GOT equivalent symbol is cached. | |||
2831 | const MCSymbol *GOTEquivSym = &SymA->getSymbol(); | |||
2832 | if (!AP.GlobalGOTEquivs.count(GOTEquivSym)) | |||
2833 | return; | |||
2834 | ||||
2835 | const GlobalValue *BaseGV = dyn_cast_or_null<GlobalValue>(BaseCst); | |||
2836 | if (!BaseGV) | |||
2837 | return; | |||
2838 | ||||
2839 | // Check for a valid base symbol | |||
2840 | const MCSymbol *BaseSym = AP.getSymbol(BaseGV); | |||
2841 | const MCSymbolRefExpr *SymB = MV.getSymB(); | |||
2842 | ||||
2843 | if (!SymB || BaseSym != &SymB->getSymbol()) | |||
2844 | return; | |||
2845 | ||||
2846 | // Make sure to match: | |||
2847 | // | |||
2848 | // gotpcrelcst := <offset from @foo base> + <cst> | |||
2849 | // | |||
2850 | // If gotpcrelcst is positive it means that we can safely fold the pc rel | |||
2851 | // displacement into the GOTPCREL. We can also can have an extra offset <cst> | |||
2852 | // if the target knows how to encode it. | |||
2853 | int64_t GOTPCRelCst = Offset + MV.getConstant(); | |||
2854 | if (GOTPCRelCst < 0) | |||
2855 | return; | |||
2856 | if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0) | |||
2857 | return; | |||
2858 | ||||
2859 | // Emit the GOT PC relative to replace the got equivalent global, i.e.: | |||
2860 | // | |||
2861 | // bar: | |||
2862 | // .long 42 | |||
2863 | // gotequiv: | |||
2864 | // .quad bar | |||
2865 | // foo: | |||
2866 | // .long gotequiv - "." + <cst> | |||
2867 | // | |||
2868 | // is replaced by the target specific equivalent to: | |||
2869 | // | |||
2870 | // bar: | |||
2871 | // .long 42 | |||
2872 | // foo: | |||
2873 | // .long bar@GOTPCREL+<gotpcrelcst> | |||
2874 | AsmPrinter::GOTEquivUsePair Result = AP.GlobalGOTEquivs[GOTEquivSym]; | |||
2875 | const GlobalVariable *GV = Result.first; | |||
2876 | int NumUses = (int)Result.second; | |||
2877 | const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0)); | |||
2878 | const MCSymbol *FinalSym = AP.getSymbol(FinalGV); | |||
2879 | *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel( | |||
2880 | FinalGV, FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer); | |||
2881 | ||||
2882 | // Update GOT equivalent usage information | |||
2883 | --NumUses; | |||
2884 | if (NumUses >= 0) | |||
2885 | AP.GlobalGOTEquivs[GOTEquivSym] = std::make_pair(GV, NumUses); | |||
2886 | } | |||
2887 | ||||
2888 | static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV, | |||
2889 | AsmPrinter &AP, const Constant *BaseCV, | |||
2890 | uint64_t Offset) { | |||
2891 | uint64_t Size = DL.getTypeAllocSize(CV->getType()); | |||
2892 | ||||
2893 | // Globals with sub-elements such as combinations of arrays and structs | |||
2894 | // are handled recursively by emitGlobalConstantImpl. Keep track of the | |||
2895 | // constant symbol base and the current position with BaseCV and Offset. | |||
2896 | if (!BaseCV && CV->hasOneUse()) | |||
2897 | BaseCV = dyn_cast<Constant>(CV->user_back()); | |||
2898 | ||||
2899 | if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) | |||
2900 | return AP.OutStreamer->emitZeros(Size); | |||
2901 | ||||
2902 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { | |||
2903 | const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType()); | |||
2904 | ||||
2905 | if (StoreSize <= 8) { | |||
2906 | if (AP.isVerbose()) | |||
2907 | AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64"l" "x" "\n", | |||
2908 | CI->getZExtValue()); | |||
2909 | AP.OutStreamer->emitIntValue(CI->getZExtValue(), StoreSize); | |||
2910 | } else { | |||
2911 | emitGlobalConstantLargeInt(CI, AP); | |||
2912 | } | |||
2913 | ||||
2914 | // Emit tail padding if needed | |||
2915 | if (Size != StoreSize) | |||
2916 | AP.OutStreamer->emitZeros(Size - StoreSize); | |||
2917 | ||||
2918 | return; | |||
2919 | } | |||
2920 | ||||
2921 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) | |||
2922 | return emitGlobalConstantFP(CFP, AP); | |||
2923 | ||||
2924 | if (isa<ConstantPointerNull>(CV)) { | |||
2925 | AP.OutStreamer->emitIntValue(0, Size); | |||
2926 | return; | |||
2927 | } | |||
2928 | ||||
2929 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV)) | |||
2930 | return emitGlobalConstantDataSequential(DL, CDS, AP); | |||
2931 | ||||
2932 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) | |||
2933 | return emitGlobalConstantArray(DL, CVA, AP, BaseCV, Offset); | |||
2934 | ||||
2935 | if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) | |||
2936 | return emitGlobalConstantStruct(DL, CVS, AP, BaseCV, Offset); | |||
2937 | ||||
2938 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { | |||
2939 | // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of | |||
2940 | // vectors). | |||
2941 | if (CE->getOpcode() == Instruction::BitCast) | |||
2942 | return emitGlobalConstantImpl(DL, CE->getOperand(0), AP); | |||
2943 | ||||
2944 | if (Size > 8) { | |||
2945 | // If the constant expression's size is greater than 64-bits, then we have | |||
2946 | // to emit the value in chunks. Try to constant fold the value and emit it | |||
2947 | // that way. | |||
2948 | Constant *New = ConstantFoldConstant(CE, DL); | |||
2949 | if (New != CE) | |||
2950 | return emitGlobalConstantImpl(DL, New, AP); | |||
2951 | } | |||
2952 | } | |||
2953 | ||||
2954 | if (const ConstantVector *V = dyn_cast<ConstantVector>(CV)) | |||
2955 | return emitGlobalConstantVector(DL, V, AP); | |||
2956 | ||||
2957 | // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it | |||
2958 | // thread the streamer with EmitValue. | |||
2959 | const MCExpr *ME = AP.lowerConstant(CV); | |||
2960 | ||||
2961 | // Since lowerConstant already folded and got rid of all IR pointer and | |||
2962 | // integer casts, detect GOT equivalent accesses by looking into the MCExpr | |||
2963 | // directly. | |||
2964 | if (AP.getObjFileLowering().supportIndirectSymViaGOTPCRel()) | |||
2965 | handleIndirectSymViaGOTPCRel(AP, &ME, BaseCV, Offset); | |||
2966 | ||||
2967 | AP.OutStreamer->emitValue(ME, Size); | |||
2968 | } | |||
2969 | ||||
2970 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. | |||
2971 | void AsmPrinter::emitGlobalConstant(const DataLayout &DL, const Constant *CV) { | |||
2972 | uint64_t Size = DL.getTypeAllocSize(CV->getType()); | |||
2973 | if (Size) | |||
2974 | emitGlobalConstantImpl(DL, CV, *this); | |||
2975 | else if (MAI->hasSubsectionsViaSymbols()) { | |||
2976 | // If the global has zero size, emit a single byte so that two labels don't | |||
2977 | // look like they are at the same location. | |||
2978 | OutStreamer->emitIntValue(0, 1); | |||
2979 | } | |||
2980 | } | |||
2981 | ||||
2982 | void AsmPrinter::emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { | |||
2983 | // Target doesn't support this yet! | |||
2984 | llvm_unreachable("Target does not support EmitMachineConstantPoolValue")::llvm::llvm_unreachable_internal("Target does not support EmitMachineConstantPoolValue" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 2984); | |||
2985 | } | |||
2986 | ||||
2987 | void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const { | |||
2988 | if (Offset > 0) | |||
2989 | OS << '+' << Offset; | |||
2990 | else if (Offset < 0) | |||
2991 | OS << Offset; | |||
2992 | } | |||
2993 | ||||
2994 | void AsmPrinter::emitNops(unsigned N) { | |||
2995 | MCInst Nop; | |||
2996 | MF->getSubtarget().getInstrInfo()->getNoop(Nop); | |||
2997 | for (; N; --N) | |||
2998 | EmitToStreamer(*OutStreamer, Nop); | |||
2999 | } | |||
3000 | ||||
3001 | //===----------------------------------------------------------------------===// | |||
3002 | // Symbol Lowering Routines. | |||
3003 | //===----------------------------------------------------------------------===// | |||
3004 | ||||
3005 | MCSymbol *AsmPrinter::createTempSymbol(const Twine &Name) const { | |||
3006 | return OutContext.createTempSymbol(Name, true); | |||
3007 | } | |||
3008 | ||||
3009 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const { | |||
3010 | return MMI->getAddrLabelSymbol(BA->getBasicBlock()); | |||
3011 | } | |||
3012 | ||||
3013 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const { | |||
3014 | return MMI->getAddrLabelSymbol(BB); | |||
3015 | } | |||
3016 | ||||
3017 | /// GetCPISymbol - Return the symbol for the specified constant pool entry. | |||
3018 | MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const { | |||
3019 | if (getSubtargetInfo().getTargetTriple().isWindowsMSVCEnvironment()) { | |||
3020 | const MachineConstantPoolEntry &CPE = | |||
3021 | MF->getConstantPool()->getConstants()[CPID]; | |||
3022 | if (!CPE.isMachineConstantPoolEntry()) { | |||
3023 | const DataLayout &DL = MF->getDataLayout(); | |||
3024 | SectionKind Kind = CPE.getSectionKind(&DL); | |||
3025 | const Constant *C = CPE.Val.ConstVal; | |||
3026 | Align Alignment = CPE.Alignment; | |||
3027 | if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>( | |||
3028 | getObjFileLowering().getSectionForConstant(DL, Kind, C, | |||
3029 | Alignment))) { | |||
3030 | if (MCSymbol *Sym = S->getCOMDATSymbol()) { | |||
3031 | if (Sym->isUndefined()) | |||
3032 | OutStreamer->emitSymbolAttribute(Sym, MCSA_Global); | |||
3033 | return Sym; | |||
3034 | } | |||
3035 | } | |||
3036 | } | |||
3037 | } | |||
3038 | ||||
3039 | const DataLayout &DL = getDataLayout(); | |||
3040 | return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + | |||
3041 | "CPI" + Twine(getFunctionNumber()) + "_" + | |||
3042 | Twine(CPID)); | |||
3043 | } | |||
3044 | ||||
3045 | /// GetJTISymbol - Return the symbol for the specified jump table entry. | |||
3046 | MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const { | |||
3047 | return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate); | |||
3048 | } | |||
3049 | ||||
3050 | /// GetJTSetSymbol - Return the symbol for the specified jump table .set | |||
3051 | /// FIXME: privatize to AsmPrinter. | |||
3052 | MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const { | |||
3053 | const DataLayout &DL = getDataLayout(); | |||
3054 | return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + | |||
3055 | Twine(getFunctionNumber()) + "_" + | |||
3056 | Twine(UID) + "_set_" + Twine(MBBID)); | |||
3057 | } | |||
3058 | ||||
3059 | MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV, | |||
3060 | StringRef Suffix) const { | |||
3061 | return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, TM); | |||
3062 | } | |||
3063 | ||||
3064 | /// Return the MCSymbol for the specified ExternalSymbol. | |||
3065 | MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const { | |||
3066 | SmallString<60> NameStr; | |||
3067 | Mangler::getNameWithPrefix(NameStr, Sym, getDataLayout()); | |||
3068 | return OutContext.getOrCreateSymbol(NameStr); | |||
3069 | } | |||
3070 | ||||
3071 | /// PrintParentLoopComment - Print comments about parent loops of this one. | |||
3072 | static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop, | |||
3073 | unsigned FunctionNumber) { | |||
3074 | if (!Loop) return; | |||
3075 | PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber); | |||
3076 | OS.indent(Loop->getLoopDepth()*2) | |||
3077 | << "Parent Loop BB" << FunctionNumber << "_" | |||
3078 | << Loop->getHeader()->getNumber() | |||
3079 | << " Depth=" << Loop->getLoopDepth() << '\n'; | |||
3080 | } | |||
3081 | ||||
3082 | /// PrintChildLoopComment - Print comments about child loops within | |||
3083 | /// the loop for this basic block, with nesting. | |||
3084 | static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop, | |||
3085 | unsigned FunctionNumber) { | |||
3086 | // Add child loop information | |||
3087 | for (const MachineLoop *CL : *Loop) { | |||
3088 | OS.indent(CL->getLoopDepth()*2) | |||
3089 | << "Child Loop BB" << FunctionNumber << "_" | |||
3090 | << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth() | |||
3091 | << '\n'; | |||
3092 | PrintChildLoopComment(OS, CL, FunctionNumber); | |||
3093 | } | |||
3094 | } | |||
3095 | ||||
3096 | /// emitBasicBlockLoopComments - Pretty-print comments for basic blocks. | |||
3097 | static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB, | |||
3098 | const MachineLoopInfo *LI, | |||
3099 | const AsmPrinter &AP) { | |||
3100 | // Add loop depth information | |||
3101 | const MachineLoop *Loop = LI->getLoopFor(&MBB); | |||
3102 | if (!Loop) return; | |||
3103 | ||||
3104 | MachineBasicBlock *Header = Loop->getHeader(); | |||
3105 | assert(Header && "No header for loop")((Header && "No header for loop") ? static_cast<void > (0) : __assert_fail ("Header && \"No header for loop\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 3105, __PRETTY_FUNCTION__)); | |||
3106 | ||||
3107 | // If this block is not a loop header, just print out what is the loop header | |||
3108 | // and return. | |||
3109 | if (Header != &MBB) { | |||
3110 | AP.OutStreamer->AddComment(" in Loop: Header=BB" + | |||
3111 | Twine(AP.getFunctionNumber())+"_" + | |||
3112 | Twine(Loop->getHeader()->getNumber())+ | |||
3113 | " Depth="+Twine(Loop->getLoopDepth())); | |||
3114 | return; | |||
3115 | } | |||
3116 | ||||
3117 | // Otherwise, it is a loop header. Print out information about child and | |||
3118 | // parent loops. | |||
3119 | raw_ostream &OS = AP.OutStreamer->GetCommentOS(); | |||
3120 | ||||
3121 | PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber()); | |||
3122 | ||||
3123 | OS << "=>"; | |||
3124 | OS.indent(Loop->getLoopDepth()*2-2); | |||
3125 | ||||
3126 | OS << "This "; | |||
3127 | if (Loop->isInnermost()) | |||
3128 | OS << "Inner "; | |||
3129 | OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n'; | |||
3130 | ||||
3131 | PrintChildLoopComment(OS, Loop, AP.getFunctionNumber()); | |||
3132 | } | |||
3133 | ||||
3134 | /// emitBasicBlockStart - This method prints the label for the specified | |||
3135 | /// MachineBasicBlock, an alignment (if present) and a comment describing | |||
3136 | /// it if appropriate. | |||
3137 | void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) { | |||
3138 | // End the previous funclet and start a new one. | |||
3139 | if (MBB.isEHFuncletEntry()) { | |||
3140 | for (const HandlerInfo &HI : Handlers) { | |||
3141 | HI.Handler->endFunclet(); | |||
3142 | HI.Handler->beginFunclet(MBB); | |||
3143 | } | |||
3144 | } | |||
3145 | ||||
3146 | // Emit an alignment directive for this block, if needed. | |||
3147 | const Align Alignment = MBB.getAlignment(); | |||
3148 | if (Alignment != Align(1)) | |||
3149 | emitAlignment(Alignment); | |||
3150 | ||||
3151 | // Switch to a new section if this basic block must begin a section. The | |||
3152 | // entry block is always placed in the function section and is handled | |||
3153 | // separately. | |||
3154 | if (MBB.isBeginSection() && !MBB.isEntryBlock()) { | |||
3155 | OutStreamer->SwitchSection( | |||
3156 | getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(), | |||
3157 | MBB, TM)); | |||
3158 | CurrentSectionBeginSym = MBB.getSymbol(); | |||
3159 | } | |||
3160 | ||||
3161 | // If the block has its address taken, emit any labels that were used to | |||
3162 | // reference the block. It is possible that there is more than one label | |||
3163 | // here, because multiple LLVM BB's may have been RAUW'd to this block after | |||
3164 | // the references were generated. | |||
3165 | if (MBB.hasAddressTaken()) { | |||
3166 | const BasicBlock *BB = MBB.getBasicBlock(); | |||
3167 | if (isVerbose()) | |||
3168 | OutStreamer->AddComment("Block address taken"); | |||
3169 | ||||
3170 | // MBBs can have their address taken as part of CodeGen without having | |||
3171 | // their corresponding BB's address taken in IR | |||
3172 | if (BB->hasAddressTaken()) | |||
3173 | for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB)) | |||
3174 | OutStreamer->emitLabel(Sym); | |||
3175 | } | |||
3176 | ||||
3177 | // Print some verbose block comments. | |||
3178 | if (isVerbose()) { | |||
3179 | if (const BasicBlock *BB = MBB.getBasicBlock()) { | |||
3180 | if (BB->hasName()) { | |||
3181 | BB->printAsOperand(OutStreamer->GetCommentOS(), | |||
3182 | /*PrintType=*/false, BB->getModule()); | |||
3183 | OutStreamer->GetCommentOS() << '\n'; | |||
3184 | } | |||
3185 | } | |||
3186 | ||||
3187 | assert(MLI != nullptr && "MachineLoopInfo should has been computed")((MLI != nullptr && "MachineLoopInfo should has been computed" ) ? static_cast<void> (0) : __assert_fail ("MLI != nullptr && \"MachineLoopInfo should has been computed\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 3187, __PRETTY_FUNCTION__)); | |||
3188 | emitBasicBlockLoopComments(MBB, MLI, *this); | |||
3189 | } | |||
3190 | ||||
3191 | // Print the main label for the block. | |||
3192 | if (shouldEmitLabelForBasicBlock(MBB)) { | |||
3193 | if (isVerbose() && MBB.hasLabelMustBeEmitted()) | |||
3194 | OutStreamer->AddComment("Label of block must be emitted"); | |||
3195 | OutStreamer->emitLabel(MBB.getSymbol()); | |||
3196 | } else { | |||
3197 | if (isVerbose()) { | |||
3198 | // NOTE: Want this comment at start of line, don't emit with AddComment. | |||
3199 | OutStreamer->emitRawComment(" %bb." + Twine(MBB.getNumber()) + ":", | |||
3200 | false); | |||
3201 | } | |||
3202 | } | |||
3203 | ||||
3204 | // With BB sections, each basic block must handle CFI information on its own | |||
3205 | // if it begins a section (Entry block is handled separately by | |||
3206 | // AsmPrinterHandler::beginFunction). | |||
3207 | if (MBB.isBeginSection() && !MBB.isEntryBlock()) | |||
3208 | for (const HandlerInfo &HI : Handlers) | |||
3209 | HI.Handler->beginBasicBlock(MBB); | |||
3210 | } | |||
3211 | ||||
3212 | void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) { | |||
3213 | // Check if CFI information needs to be updated for this MBB with basic block | |||
3214 | // sections. | |||
3215 | if (MBB.isEndSection()) | |||
3216 | for (const HandlerInfo &HI : Handlers) | |||
3217 | HI.Handler->endBasicBlock(MBB); | |||
3218 | } | |||
3219 | ||||
3220 | void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility, | |||
3221 | bool IsDefinition) const { | |||
3222 | MCSymbolAttr Attr = MCSA_Invalid; | |||
3223 | ||||
3224 | switch (Visibility) { | |||
3225 | default: break; | |||
3226 | case GlobalValue::HiddenVisibility: | |||
3227 | if (IsDefinition) | |||
3228 | Attr = MAI->getHiddenVisibilityAttr(); | |||
3229 | else | |||
3230 | Attr = MAI->getHiddenDeclarationVisibilityAttr(); | |||
3231 | break; | |||
3232 | case GlobalValue::ProtectedVisibility: | |||
3233 | Attr = MAI->getProtectedVisibilityAttr(); | |||
3234 | break; | |||
3235 | } | |||
3236 | ||||
3237 | if (Attr != MCSA_Invalid) | |||
3238 | OutStreamer->emitSymbolAttribute(Sym, Attr); | |||
3239 | } | |||
3240 | ||||
3241 | bool AsmPrinter::shouldEmitLabelForBasicBlock( | |||
3242 | const MachineBasicBlock &MBB) const { | |||
3243 | // With `-fbasic-block-sections=`, a label is needed for every non-entry block | |||
3244 | // in the labels mode (option `=labels`) and every section beginning in the | |||
3245 | // sections mode (`=all` and `=list=`). | |||
3246 | if ((MF->hasBBLabels() || MBB.isBeginSection()) && !MBB.isEntryBlock()) | |||
3247 | return true; | |||
3248 | // A label is needed for any block with at least one predecessor (when that | |||
3249 | // predecessor is not the fallthrough predecessor, or if it is an EH funclet | |||
3250 | // entry, or if a label is forced). | |||
3251 | return !MBB.pred_empty() && | |||
3252 | (!isBlockOnlyReachableByFallthrough(&MBB) || MBB.isEHFuncletEntry() || | |||
3253 | MBB.hasLabelMustBeEmitted()); | |||
3254 | } | |||
3255 | ||||
3256 | /// isBlockOnlyReachableByFallthough - Return true if the basic block has | |||
3257 | /// exactly one predecessor and the control transfer mechanism between | |||
3258 | /// the predecessor and this block is a fall-through. | |||
3259 | bool AsmPrinter:: | |||
3260 | isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const { | |||
3261 | // If this is a landing pad, it isn't a fall through. If it has no preds, | |||
3262 | // then nothing falls through to it. | |||
3263 | if (MBB->isEHPad() || MBB->pred_empty()) | |||
3264 | return false; | |||
3265 | ||||
3266 | // If there isn't exactly one predecessor, it can't be a fall through. | |||
3267 | if (MBB->pred_size() > 1) | |||
3268 | return false; | |||
3269 | ||||
3270 | // The predecessor has to be immediately before this block. | |||
3271 | MachineBasicBlock *Pred = *MBB->pred_begin(); | |||
3272 | if (!Pred->isLayoutSuccessor(MBB)) | |||
3273 | return false; | |||
3274 | ||||
3275 | // If the block is completely empty, then it definitely does fall through. | |||
3276 | if (Pred->empty()) | |||
3277 | return true; | |||
3278 | ||||
3279 | // Check the terminators in the previous blocks | |||
3280 | for (const auto &MI : Pred->terminators()) { | |||
3281 | // If it is not a simple branch, we are in a table somewhere. | |||
3282 | if (!MI.isBranch() || MI.isIndirectBranch()) | |||
3283 | return false; | |||
3284 | ||||
3285 | // If we are the operands of one of the branches, this is not a fall | |||
3286 | // through. Note that targets with delay slots will usually bundle | |||
3287 | // terminators with the delay slot instruction. | |||
3288 | for (ConstMIBundleOperands OP(MI); OP.isValid(); ++OP) { | |||
3289 | if (OP->isJTI()) | |||
3290 | return false; | |||
3291 | if (OP->isMBB() && OP->getMBB() == MBB) | |||
3292 | return false; | |||
3293 | } | |||
3294 | } | |||
3295 | ||||
3296 | return true; | |||
3297 | } | |||
3298 | ||||
3299 | GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) { | |||
3300 | if (!S.usesMetadata()) | |||
3301 | return nullptr; | |||
3302 | ||||
3303 | gcp_map_type &GCMap = getGCMap(GCMetadataPrinters); | |||
3304 | gcp_map_type::iterator GCPI = GCMap.find(&S); | |||
3305 | if (GCPI != GCMap.end()) | |||
3306 | return GCPI->second.get(); | |||
3307 | ||||
3308 | auto Name = S.getName(); | |||
3309 | ||||
3310 | for (const GCMetadataPrinterRegistry::entry &GCMetaPrinter : | |||
3311 | GCMetadataPrinterRegistry::entries()) | |||
3312 | if (Name == GCMetaPrinter.getName()) { | |||
3313 | std::unique_ptr<GCMetadataPrinter> GMP = GCMetaPrinter.instantiate(); | |||
3314 | GMP->S = &S; | |||
3315 | auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP))); | |||
3316 | return IterBool.first->second.get(); | |||
3317 | } | |||
3318 | ||||
3319 | report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name)); | |||
3320 | } | |||
3321 | ||||
3322 | void AsmPrinter::emitStackMaps(StackMaps &SM) { | |||
3323 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); | |||
3324 | assert(MI && "AsmPrinter didn't require GCModuleInfo?")((MI && "AsmPrinter didn't require GCModuleInfo?") ? static_cast <void> (0) : __assert_fail ("MI && \"AsmPrinter didn't require GCModuleInfo?\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 3324, __PRETTY_FUNCTION__)); | |||
3325 | bool NeedsDefault = false; | |||
3326 | if (MI->begin() == MI->end()) | |||
3327 | // No GC strategy, use the default format. | |||
3328 | NeedsDefault = true; | |||
3329 | else | |||
3330 | for (auto &I : *MI) { | |||
3331 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) | |||
3332 | if (MP->emitStackMaps(SM, *this)) | |||
3333 | continue; | |||
3334 | // The strategy doesn't have printer or doesn't emit custom stack maps. | |||
3335 | // Use the default format. | |||
3336 | NeedsDefault = true; | |||
3337 | } | |||
3338 | ||||
3339 | if (NeedsDefault) | |||
3340 | SM.serializeToStackMapSection(); | |||
3341 | } | |||
3342 | ||||
3343 | /// Pin vtable to this file. | |||
3344 | AsmPrinterHandler::~AsmPrinterHandler() = default; | |||
3345 | ||||
3346 | void AsmPrinterHandler::markFunctionEnd() {} | |||
3347 | ||||
3348 | // In the binary's "xray_instr_map" section, an array of these function entries | |||
3349 | // describes each instrumentation point. When XRay patches your code, the index | |||
3350 | // into this table will be given to your handler as a patch point identifier. | |||
3351 | void AsmPrinter::XRayFunctionEntry::emit(int Bytes, MCStreamer *Out) const { | |||
3352 | auto Kind8 = static_cast<uint8_t>(Kind); | |||
3353 | Out->emitBinaryData(StringRef(reinterpret_cast<const char *>(&Kind8), 1)); | |||
3354 | Out->emitBinaryData( | |||
3355 | StringRef(reinterpret_cast<const char *>(&AlwaysInstrument), 1)); | |||
3356 | Out->emitBinaryData(StringRef(reinterpret_cast<const char *>(&Version), 1)); | |||
3357 | auto Padding = (4 * Bytes) - ((2 * Bytes) + 3); | |||
3358 | assert(Padding >= 0 && "Instrumentation map entry > 4 * Word Size")((Padding >= 0 && "Instrumentation map entry > 4 * Word Size" ) ? static_cast<void> (0) : __assert_fail ("Padding >= 0 && \"Instrumentation map entry > 4 * Word Size\"" , "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 3358, __PRETTY_FUNCTION__)); | |||
3359 | Out->emitZeros(Padding); | |||
3360 | } | |||
3361 | ||||
3362 | void AsmPrinter::emitXRayTable() { | |||
3363 | if (Sleds.empty()) | |||
3364 | return; | |||
3365 | ||||
3366 | auto PrevSection = OutStreamer->getCurrentSectionOnly(); | |||
3367 | const Function &F = MF->getFunction(); | |||
3368 | MCSection *InstMap = nullptr; | |||
3369 | MCSection *FnSledIndex = nullptr; | |||
3370 | const Triple &TT = TM.getTargetTriple(); | |||
3371 | // Use PC-relative addresses on all targets. | |||
3372 | if (TT.isOSBinFormatELF()) { | |||
3373 | auto LinkedToSym = cast<MCSymbolELF>(CurrentFnSym); | |||
3374 | auto Flags = ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER; | |||
3375 | StringRef GroupName; | |||
3376 | if (F.hasComdat()) { | |||
3377 | Flags |= ELF::SHF_GROUP; | |||
3378 | GroupName = F.getComdat()->getName(); | |||
3379 | } | |||
3380 | InstMap = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS, | |||
3381 | Flags, 0, GroupName, | |||
3382 | MCSection::NonUniqueID, LinkedToSym); | |||
3383 | ||||
3384 | if (!TM.Options.XRayOmitFunctionIndex) | |||
3385 | FnSledIndex = OutContext.getELFSection( | |||
3386 | "xray_fn_idx", ELF::SHT_PROGBITS, Flags | ELF::SHF_WRITE, 0, | |||
3387 | GroupName, MCSection::NonUniqueID, LinkedToSym); | |||
3388 | } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) { | |||
3389 | InstMap = OutContext.getMachOSection("__DATA", "xray_instr_map", 0, | |||
3390 | SectionKind::getReadOnlyWithRel()); | |||
3391 | if (!TM.Options.XRayOmitFunctionIndex) | |||
3392 | FnSledIndex = OutContext.getMachOSection( | |||
3393 | "__DATA", "xray_fn_idx", 0, SectionKind::getReadOnlyWithRel()); | |||
3394 | } else { | |||
3395 | llvm_unreachable("Unsupported target")::llvm::llvm_unreachable_internal("Unsupported target", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp" , 3395); | |||
3396 | } | |||
3397 | ||||
3398 | auto WordSizeBytes = MAI->getCodePointerSize(); | |||
3399 | ||||
3400 | // Now we switch to the instrumentation map section. Because this is done | |||
3401 | // per-function, we are able to create an index entry that will represent the | |||
3402 | // range of sleds associated with a function. | |||
3403 | auto &Ctx = OutContext; | |||
3404 | MCSymbol *SledsStart = OutContext.createTempSymbol("xray_sleds_start", true); | |||
3405 | OutStreamer->SwitchSection(InstMap); | |||
3406 | OutStreamer->emitLabel(SledsStart); | |||
3407 | for (const auto &Sled : Sleds) { | |||
3408 | MCSymbol *Dot = Ctx.createTempSymbol(); | |||
3409 | OutStreamer->emitLabel(Dot); | |||
3410 | OutStreamer->emitValueImpl( | |||
3411 | MCBinaryExpr::createSub(MCSymbolRefExpr::create(Sled.Sled, Ctx), | |||
3412 | MCSymbolRefExpr::create(Dot, Ctx), Ctx), | |||
3413 | WordSizeBytes); | |||
3414 | OutStreamer->emitValueImpl( | |||
3415 | MCBinaryExpr::createSub( | |||
3416 | MCSymbolRefExpr::create(CurrentFnBegin, Ctx), | |||
3417 | MCBinaryExpr::createAdd(MCSymbolRefExpr::create(Dot, Ctx), | |||
3418 | MCConstantExpr::create(WordSizeBytes, Ctx), | |||
3419 | Ctx), | |||
3420 | Ctx), | |||
3421 | WordSizeBytes); | |||
3422 | Sled.emit(WordSizeBytes, OutStreamer.get()); | |||
3423 | } | |||
3424 | MCSymbol *SledsEnd = OutContext.createTempSymbol("xray_sleds_end", true); | |||
3425 | OutStreamer->emitLabel(SledsEnd); | |||
3426 | ||||
3427 | // We then emit a single entry in the index per function. We use the symbols | |||
3428 | // that bound the instrumentation map as the range for a specific function. | |||
3429 | // Each entry here will be 2 * word size aligned, as we're writing down two | |||
3430 | // pointers. This should work for both 32-bit and 64-bit platforms. | |||
3431 | if (FnSledIndex) { | |||
3432 | OutStreamer->SwitchSection(FnSledIndex); | |||
3433 | OutStreamer->emitCodeAlignment(2 * WordSizeBytes); | |||
3434 | OutStreamer->emitSymbolValue(SledsStart, WordSizeBytes, false); | |||
3435 | OutStreamer->emitSymbolValue(SledsEnd, WordSizeBytes, false); | |||
3436 | OutStreamer->SwitchSection(PrevSection); | |||
3437 | } | |||
3438 | Sleds.clear(); | |||
3439 | } | |||
3440 | ||||
3441 | void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI, | |||
3442 | SledKind Kind, uint8_t Version) { | |||
3443 | const Function &F = MI.getMF()->getFunction(); | |||
3444 | auto Attr = F.getFnAttribute("function-instrument"); | |||
3445 | bool LogArgs = F.hasFnAttribute("xray-log-args"); | |||
3446 | bool AlwaysInstrument = | |||
3447 | Attr.isStringAttribute() && Attr.getValueAsString() == "xray-always"; | |||
3448 | if (Kind == SledKind::FUNCTION_ENTER && LogArgs) | |||
3449 | Kind = SledKind::LOG_ARGS_ENTER; | |||
3450 | Sleds.emplace_back(XRayFunctionEntry{Sled, CurrentFnSym, Kind, | |||
3451 | AlwaysInstrument, &F, Version}); | |||
3452 | } | |||
3453 | ||||
3454 | void AsmPrinter::emitPatchableFunctionEntries() { | |||
3455 | const Function &F = MF->getFunction(); | |||
3456 | unsigned PatchableFunctionPrefix = 0, PatchableFunctionEntry = 0; | |||
3457 | (void)F.getFnAttribute("patchable-function-prefix") | |||
3458 | .getValueAsString() | |||
3459 | .getAsInteger(10, PatchableFunctionPrefix); | |||
3460 | (void)F.getFnAttribute("patchable-function-entry") | |||
3461 | .getValueAsString() | |||
3462 | .getAsInteger(10, PatchableFunctionEntry); | |||
3463 | if (!PatchableFunctionPrefix && !PatchableFunctionEntry) | |||
3464 | return; | |||
3465 | const unsigned PointerSize = getPointerSize(); | |||
3466 | if (TM.getTargetTriple().isOSBinFormatELF()) { | |||
3467 | auto Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC; | |||
3468 | const MCSymbolELF *LinkedToSym = nullptr; | |||
3469 | StringRef GroupName; | |||
3470 | ||||
3471 | // GNU as < 2.35 did not support section flag 'o'. Use SHF_LINK_ORDER only | |||
3472 | // if we are using the integrated assembler. | |||
3473 | if (MAI->useIntegratedAssembler()) { | |||
3474 | Flags |= ELF::SHF_LINK_ORDER; | |||
3475 | if (F.hasComdat()) { | |||
3476 | Flags |= ELF::SHF_GROUP; | |||
3477 | GroupName = F.getComdat()->getName(); | |||
3478 | } | |||
3479 | LinkedToSym = cast<MCSymbolELF>(CurrentFnSym); | |||
3480 | } | |||
3481 | OutStreamer->SwitchSection(OutContext.getELFSection( | |||
3482 | "__patchable_function_entries", ELF::SHT_PROGBITS, Flags, 0, GroupName, | |||
3483 | MCSection::NonUniqueID, LinkedToSym)); | |||
3484 | emitAlignment(Align(PointerSize)); | |||
3485 | OutStreamer->emitSymbolValue(CurrentPatchableFunctionEntrySym, PointerSize); | |||
3486 | } | |||
3487 | } | |||
3488 | ||||
3489 | uint16_t AsmPrinter::getDwarfVersion() const { | |||
3490 | return OutStreamer->getContext().getDwarfVersion(); | |||
3491 | } | |||
3492 | ||||
3493 | void AsmPrinter::setDwarfVersion(uint16_t Version) { | |||
3494 | OutStreamer->getContext().setDwarfVersion(Version); | |||
3495 | } | |||
3496 | ||||
3497 | bool AsmPrinter::isDwarf64() const { | |||
3498 | return OutStreamer->getContext().getDwarfFormat() == dwarf::DWARF64; | |||
3499 | } | |||
3500 | ||||
3501 | unsigned int AsmPrinter::getDwarfOffsetByteSize() const { | |||
3502 | return dwarf::getDwarfOffsetByteSize( | |||
3503 | OutStreamer->getContext().getDwarfFormat()); | |||
3504 | } | |||
3505 | ||||
3506 | unsigned int AsmPrinter::getUnitLengthFieldByteSize() const { | |||
3507 | return dwarf::getUnitLengthFieldByteSize( | |||
3508 | OutStreamer->getContext().getDwarfFormat()); | |||
3509 | } |