1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/CodeGen/BackendUtil.h" 11 #include "clang/Basic/Diagnostic.h" 12 #include "clang/Basic/LangOptions.h" 13 #include "clang/Basic/TargetOptions.h" 14 #include "clang/Frontend/CodeGenOptions.h" 15 #include "clang/Frontend/FrontendDiagnostic.h" 16 #include "clang/Frontend/Utils.h" 17 #include "llvm/ADT/SmallSet.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include "llvm/ADT/StringSwitch.h" 20 #include "llvm/ADT/Triple.h" 21 #include "llvm/Analysis/TargetLibraryInfo.h" 22 #include "llvm/Analysis/TargetTransformInfo.h" 23 #include "llvm/Bitcode/BitcodeReader.h" 24 #include "llvm/Bitcode/BitcodeWriter.h" 25 #include "llvm/Bitcode/BitcodeWriterPass.h" 26 #include "llvm/CodeGen/RegAllocRegistry.h" 27 #include "llvm/CodeGen/SchedulerRegistry.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/IRPrintingPasses.h" 30 #include "llvm/IR/LegacyPassManager.h" 31 #include "llvm/IR/Module.h" 32 #include "llvm/IR/ModuleSummaryIndex.h" 33 #include "llvm/IR/Verifier.h" 34 #include "llvm/LTO/LTOBackend.h" 35 #include "llvm/MC/SubtargetFeature.h" 36 #include "llvm/Object/ModuleSummaryIndexObjectFile.h" 37 #include "llvm/Passes/PassBuilder.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/MemoryBuffer.h" 40 #include "llvm/Support/PrettyStackTrace.h" 41 #include "llvm/Support/TargetRegistry.h" 42 #include "llvm/Support/Timer.h" 43 #include "llvm/Support/raw_ostream.h" 44 #include "llvm/Target/TargetMachine.h" 45 #include "llvm/Target/TargetOptions.h" 46 #include "llvm/Target/TargetSubtargetInfo.h" 47 #include "llvm/Transforms/Coroutines.h" 48 #include "llvm/Transforms/IPO.h" 49 #include "llvm/Transforms/IPO/AlwaysInliner.h" 50 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 51 #include "llvm/Transforms/Instrumentation.h" 52 #include "llvm/Transforms/ObjCARC.h" 53 #include "llvm/Transforms/Scalar.h" 54 #include "llvm/Transforms/Scalar/GVN.h" 55 #include "llvm/Transforms/Utils/SymbolRewriter.h" 56 #include <memory> 57 using namespace clang; 58 using namespace llvm; 59 60 namespace { 61 62 class EmitAssemblyHelper { 63 DiagnosticsEngine &Diags; 64 const CodeGenOptions &CodeGenOpts; 65 const clang::TargetOptions &TargetOpts; 66 const LangOptions &LangOpts; 67 Module *TheModule; 68 69 Timer CodeGenerationTime; 70 71 std::unique_ptr<raw_pwrite_stream> OS; 72 73 private: 74 TargetIRAnalysis getTargetIRAnalysis() const { 75 if (TM) 76 return TM->getTargetIRAnalysis(); 77 78 return TargetIRAnalysis(); 79 } 80 81 /// Set LLVM command line options passed through -backend-option. 82 void setCommandLineOpts(); 83 84 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM); 85 86 /// Generates the TargetMachine. 87 /// Leaves TM unchanged if it is unable to create the target machine. 88 /// Some of our clang tests specify triples which are not built 89 /// into clang. This is okay because these tests check the generated 90 /// IR, and they require DataLayout which depends on the triple. 91 /// In this case, we allow this method to fail and not report an error. 92 /// When MustCreateTM is used, we print an error if we are unable to load 93 /// the requested target. 94 void CreateTargetMachine(bool MustCreateTM); 95 96 /// Add passes necessary to emit assembly or LLVM IR. 97 /// 98 /// \return True on success. 99 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, 100 raw_pwrite_stream &OS); 101 102 public: 103 EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts, 104 const clang::TargetOptions &TOpts, 105 const LangOptions &LOpts, Module *M) 106 : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts), 107 TheModule(M), CodeGenerationTime("codegen", "Code Generation Time") {} 108 109 ~EmitAssemblyHelper() { 110 if (CodeGenOpts.DisableFree) 111 BuryPointer(std::move(TM)); 112 } 113 114 std::unique_ptr<TargetMachine> TM; 115 116 void EmitAssembly(BackendAction Action, 117 std::unique_ptr<raw_pwrite_stream> OS); 118 119 void EmitAssemblyWithNewPassManager(BackendAction Action, 120 std::unique_ptr<raw_pwrite_stream> OS); 121 }; 122 123 // We need this wrapper to access LangOpts and CGOpts from extension functions 124 // that we add to the PassManagerBuilder. 125 class PassManagerBuilderWrapper : public PassManagerBuilder { 126 public: 127 PassManagerBuilderWrapper(const CodeGenOptions &CGOpts, 128 const LangOptions &LangOpts) 129 : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {} 130 const CodeGenOptions &getCGOpts() const { return CGOpts; } 131 const LangOptions &getLangOpts() const { return LangOpts; } 132 private: 133 const CodeGenOptions &CGOpts; 134 const LangOptions &LangOpts; 135 }; 136 137 } 138 139 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 140 if (Builder.OptLevel > 0) 141 PM.add(createObjCARCAPElimPass()); 142 } 143 144 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 145 if (Builder.OptLevel > 0) 146 PM.add(createObjCARCExpandPass()); 147 } 148 149 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 150 if (Builder.OptLevel > 0) 151 PM.add(createObjCARCOptPass()); 152 } 153 154 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder, 155 legacy::PassManagerBase &PM) { 156 PM.add(createAddDiscriminatorsPass()); 157 } 158 159 static void addBoundsCheckingPass(const PassManagerBuilder &Builder, 160 legacy::PassManagerBase &PM) { 161 PM.add(createBoundsCheckingPass()); 162 } 163 164 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder, 165 legacy::PassManagerBase &PM) { 166 const PassManagerBuilderWrapper &BuilderWrapper = 167 static_cast<const PassManagerBuilderWrapper&>(Builder); 168 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 169 SanitizerCoverageOptions Opts; 170 Opts.CoverageType = 171 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType); 172 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls; 173 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB; 174 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp; 175 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv; 176 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep; 177 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters; 178 Opts.TracePC = CGOpts.SanitizeCoverageTracePC; 179 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard; 180 PM.add(createSanitizerCoverageModulePass(Opts)); 181 } 182 183 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder, 184 legacy::PassManagerBase &PM) { 185 const PassManagerBuilderWrapper &BuilderWrapper = 186 static_cast<const PassManagerBuilderWrapper&>(Builder); 187 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 188 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address); 189 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope; 190 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover, 191 UseAfterScope)); 192 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover)); 193 } 194 195 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder, 196 legacy::PassManagerBase &PM) { 197 PM.add(createAddressSanitizerFunctionPass( 198 /*CompileKernel*/ true, 199 /*Recover*/ true, /*UseAfterScope*/ false)); 200 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true, 201 /*Recover*/true)); 202 } 203 204 static void addMemorySanitizerPass(const PassManagerBuilder &Builder, 205 legacy::PassManagerBase &PM) { 206 const PassManagerBuilderWrapper &BuilderWrapper = 207 static_cast<const PassManagerBuilderWrapper&>(Builder); 208 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 209 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins; 210 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory); 211 PM.add(createMemorySanitizerPass(TrackOrigins, Recover)); 212 213 // MemorySanitizer inserts complex instrumentation that mostly follows 214 // the logic of the original code, but operates on "shadow" values. 215 // It can benefit from re-running some general purpose optimization passes. 216 if (Builder.OptLevel > 0) { 217 PM.add(createEarlyCSEPass()); 218 PM.add(createReassociatePass()); 219 PM.add(createLICMPass()); 220 PM.add(createGVNPass()); 221 PM.add(createInstructionCombiningPass()); 222 PM.add(createDeadStoreEliminationPass()); 223 } 224 } 225 226 static void addThreadSanitizerPass(const PassManagerBuilder &Builder, 227 legacy::PassManagerBase &PM) { 228 PM.add(createThreadSanitizerPass()); 229 } 230 231 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder, 232 legacy::PassManagerBase &PM) { 233 const PassManagerBuilderWrapper &BuilderWrapper = 234 static_cast<const PassManagerBuilderWrapper&>(Builder); 235 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 236 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles)); 237 } 238 239 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder, 240 legacy::PassManagerBase &PM) { 241 const PassManagerBuilderWrapper &BuilderWrapper = 242 static_cast<const PassManagerBuilderWrapper&>(Builder); 243 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 244 EfficiencySanitizerOptions Opts; 245 if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag)) 246 Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag; 247 else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet)) 248 Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet; 249 PM.add(createEfficiencySanitizerPass(Opts)); 250 } 251 252 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple, 253 const CodeGenOptions &CodeGenOpts) { 254 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple); 255 if (!CodeGenOpts.SimplifyLibCalls) 256 TLII->disableAllFunctions(); 257 else { 258 // Disable individual libc/libm calls in TargetLibraryInfo. 259 LibFunc::Func F; 260 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs()) 261 if (TLII->getLibFunc(FuncName, F)) 262 TLII->setUnavailable(F); 263 } 264 265 switch (CodeGenOpts.getVecLib()) { 266 case CodeGenOptions::Accelerate: 267 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate); 268 break; 269 case CodeGenOptions::SVML: 270 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML); 271 break; 272 default: 273 break; 274 } 275 return TLII; 276 } 277 278 static void addSymbolRewriterPass(const CodeGenOptions &Opts, 279 legacy::PassManager *MPM) { 280 llvm::SymbolRewriter::RewriteDescriptorList DL; 281 282 llvm::SymbolRewriter::RewriteMapParser MapParser; 283 for (const auto &MapFile : Opts.RewriteMapFiles) 284 MapParser.parse(MapFile, &DL); 285 286 MPM->add(createRewriteSymbolsPass(DL)); 287 } 288 289 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM, 290 legacy::FunctionPassManager &FPM) { 291 // Handle disabling of all LLVM passes, where we want to preserve the 292 // internal module before any optimization. 293 if (CodeGenOpts.DisableLLVMPasses) 294 return; 295 296 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts); 297 298 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM 299 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling) 300 // are inserted before PMBuilder ones - they'd get the default-constructed 301 // TLI with an unknown target otherwise. 302 Triple TargetTriple(TheModule->getTargetTriple()); 303 std::unique_ptr<TargetLibraryInfoImpl> TLII( 304 createTLII(TargetTriple, CodeGenOpts)); 305 306 // At O0 and O1 we only run the always inliner which is more efficient. At 307 // higher optimization levels we run the normal inliner. 308 if (CodeGenOpts.OptimizationLevel <= 1) { 309 bool InsertLifetimeIntrinsics = CodeGenOpts.OptimizationLevel != 0; 310 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics); 311 } else { 312 PMBuilder.Inliner = createFunctionInliningPass( 313 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize); 314 } 315 316 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel; 317 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; 318 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB; 319 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP; 320 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop; 321 322 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 323 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; 324 PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex; 325 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO; 326 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; 327 328 MPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 329 330 // Add target-specific passes that need to run as early as possible. 331 if (TM) 332 PMBuilder.addExtension( 333 PassManagerBuilder::EP_EarlyAsPossible, 334 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { 335 TM->addEarlyAsPossiblePasses(PM); 336 }); 337 338 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 339 addAddDiscriminatorsPass); 340 341 // In ObjC ARC mode, add the main ARC optimization passes. 342 if (LangOpts.ObjCAutoRefCount) { 343 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 344 addObjCARCExpandPass); 345 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly, 346 addObjCARCAPElimPass); 347 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 348 addObjCARCOptPass); 349 } 350 351 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) { 352 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 353 addBoundsCheckingPass); 354 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 355 addBoundsCheckingPass); 356 } 357 358 if (CodeGenOpts.SanitizeCoverageType || 359 CodeGenOpts.SanitizeCoverageIndirectCalls || 360 CodeGenOpts.SanitizeCoverageTraceCmp) { 361 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 362 addSanitizerCoveragePass); 363 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 364 addSanitizerCoveragePass); 365 } 366 367 if (LangOpts.Sanitize.has(SanitizerKind::Address)) { 368 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 369 addAddressSanitizerPasses); 370 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 371 addAddressSanitizerPasses); 372 } 373 374 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) { 375 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 376 addKernelAddressSanitizerPasses); 377 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 378 addKernelAddressSanitizerPasses); 379 } 380 381 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) { 382 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 383 addMemorySanitizerPass); 384 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 385 addMemorySanitizerPass); 386 } 387 388 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 389 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 390 addThreadSanitizerPass); 391 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 392 addThreadSanitizerPass); 393 } 394 395 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 396 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 397 addDataFlowSanitizerPass); 398 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 399 addDataFlowSanitizerPass); 400 } 401 402 if (LangOpts.CoroutinesTS) 403 addCoroutinePassesToExtensionPoints(PMBuilder); 404 405 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) { 406 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 407 addEfficiencySanitizerPass); 408 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 409 addEfficiencySanitizerPass); 410 } 411 412 // Set up the per-function pass manager. 413 FPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 414 if (CodeGenOpts.VerifyModule) 415 FPM.add(createVerifierPass()); 416 417 // Set up the per-module pass manager. 418 if (!CodeGenOpts.RewriteMapFiles.empty()) 419 addSymbolRewriterPass(CodeGenOpts, &MPM); 420 421 if (!CodeGenOpts.DisableGCov && 422 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) { 423 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 424 // LLVM's -default-gcov-version flag is set to something invalid. 425 GCOVOptions Options; 426 Options.EmitNotes = CodeGenOpts.EmitGcovNotes; 427 Options.EmitData = CodeGenOpts.EmitGcovArcs; 428 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4); 429 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum; 430 Options.NoRedZone = CodeGenOpts.DisableRedZone; 431 Options.FunctionNamesInData = 432 !CodeGenOpts.CoverageNoFunctionNamesInData; 433 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody; 434 MPM.add(createGCOVProfilerPass(Options)); 435 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo) 436 MPM.add(createStripSymbolsPass(true)); 437 } 438 439 if (CodeGenOpts.hasProfileClangInstr()) { 440 InstrProfOptions Options; 441 Options.NoRedZone = CodeGenOpts.DisableRedZone; 442 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 443 MPM.add(createInstrProfilingLegacyPass(Options)); 444 } 445 if (CodeGenOpts.hasProfileIRInstr()) { 446 PMBuilder.EnablePGOInstrGen = true; 447 if (!CodeGenOpts.InstrProfileOutput.empty()) 448 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput; 449 else 450 PMBuilder.PGOInstrGen = "default_%m.profraw"; 451 } 452 if (CodeGenOpts.hasProfileIRUse()) 453 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath; 454 455 if (!CodeGenOpts.SampleProfileFile.empty()) 456 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile; 457 458 PMBuilder.populateFunctionPassManager(FPM); 459 PMBuilder.populateModulePassManager(MPM); 460 } 461 462 void EmitAssemblyHelper::setCommandLineOpts() { 463 SmallVector<const char *, 16> BackendArgs; 464 BackendArgs.push_back("clang"); // Fake program name. 465 if (!CodeGenOpts.DebugPass.empty()) { 466 BackendArgs.push_back("-debug-pass"); 467 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 468 } 469 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 470 BackendArgs.push_back("-limit-float-precision"); 471 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 472 } 473 for (const std::string &BackendOption : CodeGenOpts.BackendOptions) 474 BackendArgs.push_back(BackendOption.c_str()); 475 BackendArgs.push_back(nullptr); 476 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 477 BackendArgs.data()); 478 } 479 480 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 481 // Create the TargetMachine for generating code. 482 std::string Error; 483 std::string Triple = TheModule->getTargetTriple(); 484 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 485 if (!TheTarget) { 486 if (MustCreateTM) 487 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 488 return; 489 } 490 491 unsigned CodeModel = 492 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel) 493 .Case("small", llvm::CodeModel::Small) 494 .Case("kernel", llvm::CodeModel::Kernel) 495 .Case("medium", llvm::CodeModel::Medium) 496 .Case("large", llvm::CodeModel::Large) 497 .Case("default", llvm::CodeModel::Default) 498 .Default(~0u); 499 assert(CodeModel != ~0u && "invalid code model!"); 500 llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel); 501 502 std::string FeaturesStr = 503 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 504 505 // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp. 506 llvm::Optional<llvm::Reloc::Model> RM; 507 RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel) 508 .Case("static", llvm::Reloc::Static) 509 .Case("pic", llvm::Reloc::PIC_) 510 .Case("ropi", llvm::Reloc::ROPI) 511 .Case("rwpi", llvm::Reloc::RWPI) 512 .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI) 513 .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC); 514 assert(RM.hasValue() && "invalid PIC model!"); 515 516 CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 517 switch (CodeGenOpts.OptimizationLevel) { 518 default: break; 519 case 0: OptLevel = CodeGenOpt::None; break; 520 case 3: OptLevel = CodeGenOpt::Aggressive; break; 521 } 522 523 llvm::TargetOptions Options; 524 525 Options.ThreadModel = 526 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel) 527 .Case("posix", llvm::ThreadModel::POSIX) 528 .Case("single", llvm::ThreadModel::Single); 529 530 // Set float ABI type. 531 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 532 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 533 "Invalid Floating Point ABI!"); 534 Options.FloatABIType = 535 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 536 .Case("soft", llvm::FloatABI::Soft) 537 .Case("softfp", llvm::FloatABI::Soft) 538 .Case("hard", llvm::FloatABI::Hard) 539 .Default(llvm::FloatABI::Default); 540 541 // Set FP fusion mode. 542 switch (CodeGenOpts.getFPContractMode()) { 543 case CodeGenOptions::FPC_Off: 544 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict; 545 break; 546 case CodeGenOptions::FPC_On: 547 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 548 break; 549 case CodeGenOptions::FPC_Fast: 550 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 551 break; 552 } 553 554 Options.UseInitArray = CodeGenOpts.UseInitArray; 555 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 556 Options.CompressDebugSections = CodeGenOpts.CompressDebugSections; 557 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations; 558 559 // Set EABI version. 560 Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion) 561 .Case("4", llvm::EABI::EABI4) 562 .Case("5", llvm::EABI::EABI5) 563 .Case("gnu", llvm::EABI::GNU) 564 .Default(llvm::EABI::Default); 565 566 if (LangOpts.SjLjExceptions) 567 Options.ExceptionModel = llvm::ExceptionHandling::SjLj; 568 569 Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD; 570 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 571 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 572 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 573 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 574 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment; 575 Options.FunctionSections = CodeGenOpts.FunctionSections; 576 Options.DataSections = CodeGenOpts.DataSections; 577 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 578 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 579 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 580 581 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 582 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 583 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm; 584 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 585 Options.MCOptions.MCIncrementalLinkerCompatible = 586 CodeGenOpts.IncrementalLinkerCompatible; 587 Options.MCOptions.MCPIECopyRelocations = 588 CodeGenOpts.PIECopyRelocations; 589 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 590 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 591 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments; 592 Options.MCOptions.ABIName = TargetOpts.ABI; 593 594 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, 595 Options, RM, CM, OptLevel)); 596 } 597 598 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, 599 BackendAction Action, 600 raw_pwrite_stream &OS) { 601 // Add LibraryInfo. 602 llvm::Triple TargetTriple(TheModule->getTargetTriple()); 603 std::unique_ptr<TargetLibraryInfoImpl> TLII( 604 createTLII(TargetTriple, CodeGenOpts)); 605 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII)); 606 607 // Normal mode, emit a .s or .o file by running the code generator. Note, 608 // this also adds codegenerator level optimization passes. 609 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; 610 if (Action == Backend_EmitObj) 611 CGFT = TargetMachine::CGFT_ObjectFile; 612 else if (Action == Backend_EmitMCNull) 613 CGFT = TargetMachine::CGFT_Null; 614 else 615 assert(Action == Backend_EmitAssembly && "Invalid action!"); 616 617 // Add ObjC ARC final-cleanup optimizations. This is done as part of the 618 // "codegen" passes so that it isn't run multiple times when there is 619 // inlining happening. 620 if (CodeGenOpts.OptimizationLevel > 0) 621 CodeGenPasses.add(createObjCARCContractPass()); 622 623 if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT, 624 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 625 Diags.Report(diag::err_fe_unable_to_interface_with_target); 626 return false; 627 } 628 629 return true; 630 } 631 632 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, 633 std::unique_ptr<raw_pwrite_stream> OS) { 634 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); 635 636 setCommandLineOpts(); 637 638 bool UsesCodeGen = (Action != Backend_EmitNothing && 639 Action != Backend_EmitBC && 640 Action != Backend_EmitLL); 641 CreateTargetMachine(UsesCodeGen); 642 643 if (UsesCodeGen && !TM) 644 return; 645 if (TM) 646 TheModule->setDataLayout(TM->createDataLayout()); 647 648 legacy::PassManager PerModulePasses; 649 PerModulePasses.add( 650 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 651 652 legacy::FunctionPassManager PerFunctionPasses(TheModule); 653 PerFunctionPasses.add( 654 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 655 656 CreatePasses(PerModulePasses, PerFunctionPasses); 657 658 legacy::PassManager CodeGenPasses; 659 CodeGenPasses.add( 660 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 661 662 switch (Action) { 663 case Backend_EmitNothing: 664 break; 665 666 case Backend_EmitBC: 667 PerModulePasses.add(createBitcodeWriterPass( 668 *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitSummaryIndex, 669 CodeGenOpts.EmitSummaryIndex)); 670 break; 671 672 case Backend_EmitLL: 673 PerModulePasses.add( 674 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 675 break; 676 677 default: 678 if (!AddEmitPasses(CodeGenPasses, Action, *OS)) 679 return; 680 } 681 682 // Before executing passes, print the final values of the LLVM options. 683 cl::PrintOptionValues(); 684 685 // Run passes. For now we do all passes at once, but eventually we 686 // would like to have the option of streaming code generation. 687 688 { 689 PrettyStackTraceString CrashInfo("Per-function optimization"); 690 691 PerFunctionPasses.doInitialization(); 692 for (Function &F : *TheModule) 693 if (!F.isDeclaration()) 694 PerFunctionPasses.run(F); 695 PerFunctionPasses.doFinalization(); 696 } 697 698 { 699 PrettyStackTraceString CrashInfo("Per-module optimization passes"); 700 PerModulePasses.run(*TheModule); 701 } 702 703 { 704 PrettyStackTraceString CrashInfo("Code generation"); 705 CodeGenPasses.run(*TheModule); 706 } 707 } 708 709 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) { 710 switch (Opts.OptimizationLevel) { 711 default: 712 llvm_unreachable("Invalid optimization level!"); 713 714 case 1: 715 return PassBuilder::O1; 716 717 case 2: 718 switch (Opts.OptimizeSize) { 719 default: 720 llvm_unreachable("Invalide optimization level for size!"); 721 722 case 0: 723 return PassBuilder::O2; 724 725 case 1: 726 return PassBuilder::Os; 727 728 case 2: 729 return PassBuilder::Oz; 730 } 731 732 case 3: 733 return PassBuilder::O3; 734 } 735 } 736 737 /// A clean version of `EmitAssembly` that uses the new pass manager. 738 /// 739 /// Not all features are currently supported in this system, but where 740 /// necessary it falls back to the legacy pass manager to at least provide 741 /// basic functionality. 742 /// 743 /// This API is planned to have its functionality finished and then to replace 744 /// `EmitAssembly` at some point in the future when the default switches. 745 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager( 746 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) { 747 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); 748 setCommandLineOpts(); 749 750 // The new pass manager always makes a target machine available to passes 751 // during construction. 752 CreateTargetMachine(/*MustCreateTM*/ true); 753 if (!TM) 754 // This will already be diagnosed, just bail. 755 return; 756 TheModule->setDataLayout(TM->createDataLayout()); 757 758 PassBuilder PB(TM.get()); 759 760 LoopAnalysisManager LAM; 761 FunctionAnalysisManager FAM; 762 CGSCCAnalysisManager CGAM; 763 ModuleAnalysisManager MAM; 764 765 // Register the AA manager first so that our version is the one used. 766 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); 767 768 // Register all the basic analyses with the managers. 769 PB.registerModuleAnalyses(MAM); 770 PB.registerCGSCCAnalyses(CGAM); 771 PB.registerFunctionAnalyses(FAM); 772 PB.registerLoopAnalyses(LAM); 773 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 774 775 ModulePassManager MPM; 776 777 if (!CodeGenOpts.DisableLLVMPasses) { 778 if (CodeGenOpts.OptimizationLevel == 0) { 779 // Build a minimal pipeline based on the semantics required by Clang, 780 // which is just that always inlining occurs. 781 MPM.addPass(AlwaysInlinerPass()); 782 } else { 783 // Otherwise, use the default pass pipeline. We also have to map our 784 // optimization levels into one of the distinct levels used to configure 785 // the pipeline. 786 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts); 787 788 MPM = PB.buildPerModuleDefaultPipeline(Level); 789 } 790 } 791 792 // FIXME: We still use the legacy pass manager to do code generation. We 793 // create that pass manager here and use it as needed below. 794 legacy::PassManager CodeGenPasses; 795 bool NeedCodeGen = false; 796 797 // Append any output we need to the pass manager. 798 switch (Action) { 799 case Backend_EmitNothing: 800 break; 801 802 case Backend_EmitBC: 803 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, 804 CodeGenOpts.EmitSummaryIndex, 805 CodeGenOpts.EmitSummaryIndex)); 806 break; 807 808 case Backend_EmitLL: 809 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 810 break; 811 812 case Backend_EmitAssembly: 813 case Backend_EmitMCNull: 814 case Backend_EmitObj: 815 NeedCodeGen = true; 816 CodeGenPasses.add( 817 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 818 if (!AddEmitPasses(CodeGenPasses, Action, *OS)) 819 // FIXME: Should we handle this error differently? 820 return; 821 break; 822 } 823 824 // Before executing passes, print the final values of the LLVM options. 825 cl::PrintOptionValues(); 826 827 // Now that we have all of the passes ready, run them. 828 { 829 PrettyStackTraceString CrashInfo("Optimizer"); 830 MPM.run(*TheModule, MAM); 831 } 832 833 // Now if needed, run the legacy PM for codegen. 834 if (NeedCodeGen) { 835 PrettyStackTraceString CrashInfo("Code generation"); 836 CodeGenPasses.run(*TheModule); 837 } 838 } 839 840 static void runThinLTOBackend(const CodeGenOptions &CGOpts, Module *M, 841 std::unique_ptr<raw_pwrite_stream> OS) { 842 // If we are performing a ThinLTO importing compile, load the function index 843 // into memory and pass it into thinBackend, which will run the function 844 // importer and invoke LTO passes. 845 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = 846 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile); 847 if (!IndexOrErr) { 848 logAllUnhandledErrors(IndexOrErr.takeError(), errs(), 849 "Error loading index file '" + 850 CGOpts.ThinLTOIndexFile + "': "); 851 return; 852 } 853 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr); 854 855 StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> 856 ModuleToDefinedGVSummaries; 857 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 858 859 // We can simply import the values mentioned in the combined index, since 860 // we should only invoke this using the individual indexes written out 861 // via a WriteIndexesThinBackend. 862 FunctionImporter::ImportMapTy ImportList; 863 for (auto &GlobalList : *CombinedIndex) { 864 auto GUID = GlobalList.first; 865 assert(GlobalList.second.size() == 1 && 866 "Expected individual combined index to have one summary per GUID"); 867 auto &Summary = GlobalList.second[0]; 868 // Skip the summaries for the importing module. These are included to 869 // e.g. record required linkage changes. 870 if (Summary->modulePath() == M->getModuleIdentifier()) 871 continue; 872 // Doesn't matter what value we plug in to the map, just needs an entry 873 // to provoke importing by thinBackend. 874 ImportList[Summary->modulePath()][GUID] = 1; 875 } 876 877 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports; 878 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap; 879 880 for (auto &I : ImportList) { 881 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr = 882 llvm::MemoryBuffer::getFile(I.first()); 883 if (!MBOrErr) { 884 errs() << "Error loading imported file '" << I.first() 885 << "': " << MBOrErr.getError().message() << "\n"; 886 return; 887 } 888 889 Expected<std::vector<BitcodeModule>> BMsOrErr = 890 getBitcodeModuleList(**MBOrErr); 891 if (!BMsOrErr) { 892 handleAllErrors(BMsOrErr.takeError(), [&](ErrorInfoBase &EIB) { 893 errs() << "Error loading imported file '" << I.first() 894 << "': " << EIB.message() << '\n'; 895 }); 896 return; 897 } 898 899 // The bitcode file may contain multiple modules, we want the one with a 900 // summary. 901 bool FoundModule = false; 902 for (BitcodeModule &BM : *BMsOrErr) { 903 Expected<bool> HasSummary = BM.hasSummary(); 904 if (HasSummary && *HasSummary) { 905 ModuleMap.insert({I.first(), BM}); 906 FoundModule = true; 907 break; 908 } 909 } 910 if (!FoundModule) { 911 errs() << "Error loading imported file '" << I.first() 912 << "': Could not find module summary\n"; 913 return; 914 } 915 916 OwnedImports.push_back(std::move(*MBOrErr)); 917 } 918 auto AddStream = [&](size_t Task) { 919 return llvm::make_unique<lto::NativeObjectStream>(std::move(OS)); 920 }; 921 lto::Config Conf; 922 if (Error E = thinBackend( 923 Conf, 0, AddStream, *M, *CombinedIndex, ImportList, 924 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) { 925 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 926 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n'; 927 }); 928 } 929 } 930 931 void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 932 const CodeGenOptions &CGOpts, 933 const clang::TargetOptions &TOpts, 934 const LangOptions &LOpts, const llvm::DataLayout &TDesc, 935 Module *M, BackendAction Action, 936 std::unique_ptr<raw_pwrite_stream> OS) { 937 if (!CGOpts.ThinLTOIndexFile.empty()) { 938 runThinLTOBackend(CGOpts, M, std::move(OS)); 939 return; 940 } 941 942 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); 943 944 if (CGOpts.ExperimentalNewPassManager) 945 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS)); 946 else 947 AsmHelper.EmitAssembly(Action, std::move(OS)); 948 949 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's 950 // DataLayout. 951 if (AsmHelper.TM) { 952 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 953 if (DLDesc != TDesc.getStringRepresentation()) { 954 unsigned DiagID = Diags.getCustomDiagID( 955 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 956 "expected target description '%1'"); 957 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation(); 958 } 959 } 960 } 961 962 static const char* getSectionNameForBitcode(const Triple &T) { 963 switch (T.getObjectFormat()) { 964 case Triple::MachO: 965 return "__LLVM,__bitcode"; 966 case Triple::COFF: 967 case Triple::ELF: 968 case Triple::UnknownObjectFormat: 969 return ".llvmbc"; 970 } 971 llvm_unreachable("Unimplemented ObjectFormatType"); 972 } 973 974 static const char* getSectionNameForCommandline(const Triple &T) { 975 switch (T.getObjectFormat()) { 976 case Triple::MachO: 977 return "__LLVM,__cmdline"; 978 case Triple::COFF: 979 case Triple::ELF: 980 case Triple::UnknownObjectFormat: 981 return ".llvmcmd"; 982 } 983 llvm_unreachable("Unimplemented ObjectFormatType"); 984 } 985 986 // With -fembed-bitcode, save a copy of the llvm IR as data in the 987 // __LLVM,__bitcode section. 988 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, 989 llvm::MemoryBufferRef Buf) { 990 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) 991 return; 992 993 // Save llvm.compiler.used and remote it. 994 SmallVector<Constant*, 2> UsedArray; 995 SmallSet<GlobalValue*, 4> UsedGlobals; 996 Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0); 997 GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true); 998 for (auto *GV : UsedGlobals) { 999 if (GV->getName() != "llvm.embedded.module" && 1000 GV->getName() != "llvm.cmdline") 1001 UsedArray.push_back( 1002 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1003 } 1004 if (Used) 1005 Used->eraseFromParent(); 1006 1007 // Embed the bitcode for the llvm module. 1008 std::string Data; 1009 ArrayRef<uint8_t> ModuleData; 1010 Triple T(M->getTargetTriple()); 1011 // Create a constant that contains the bitcode. 1012 // In case of embedding a marker, ignore the input Buf and use the empty 1013 // ArrayRef. It is also legal to create a bitcode marker even Buf is empty. 1014 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) { 1015 if (!isBitcode((const unsigned char *)Buf.getBufferStart(), 1016 (const unsigned char *)Buf.getBufferEnd())) { 1017 // If the input is LLVM Assembly, bitcode is produced by serializing 1018 // the module. Use-lists order need to be perserved in this case. 1019 llvm::raw_string_ostream OS(Data); 1020 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true); 1021 ModuleData = 1022 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size()); 1023 } else 1024 // If the input is LLVM bitcode, write the input byte stream directly. 1025 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(), 1026 Buf.getBufferSize()); 1027 } 1028 llvm::Constant *ModuleConstant = 1029 llvm::ConstantDataArray::get(M->getContext(), ModuleData); 1030 llvm::GlobalVariable *GV = new llvm::GlobalVariable( 1031 *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, 1032 ModuleConstant); 1033 GV->setSection(getSectionNameForBitcode(T)); 1034 UsedArray.push_back( 1035 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1036 if (llvm::GlobalVariable *Old = 1037 M->getGlobalVariable("llvm.embedded.module", true)) { 1038 assert(Old->hasOneUse() && 1039 "llvm.embedded.module can only be used once in llvm.compiler.used"); 1040 GV->takeName(Old); 1041 Old->eraseFromParent(); 1042 } else { 1043 GV->setName("llvm.embedded.module"); 1044 } 1045 1046 // Skip if only bitcode needs to be embedded. 1047 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) { 1048 // Embed command-line options. 1049 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()), 1050 CGOpts.CmdArgs.size()); 1051 llvm::Constant *CmdConstant = 1052 llvm::ConstantDataArray::get(M->getContext(), CmdData); 1053 GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true, 1054 llvm::GlobalValue::PrivateLinkage, 1055 CmdConstant); 1056 GV->setSection(getSectionNameForCommandline(T)); 1057 UsedArray.push_back( 1058 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1059 if (llvm::GlobalVariable *Old = 1060 M->getGlobalVariable("llvm.cmdline", true)) { 1061 assert(Old->hasOneUse() && 1062 "llvm.cmdline can only be used once in llvm.compiler.used"); 1063 GV->takeName(Old); 1064 Old->eraseFromParent(); 1065 } else { 1066 GV->setName("llvm.cmdline"); 1067 } 1068 } 1069 1070 if (UsedArray.empty()) 1071 return; 1072 1073 // Recreate llvm.compiler.used. 1074 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size()); 1075 auto *NewUsed = new GlobalVariable( 1076 *M, ATy, false, llvm::GlobalValue::AppendingLinkage, 1077 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); 1078 NewUsed->setSection("llvm.metadata"); 1079 } 1080