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