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