1*0b57cec5SDimitry Andric //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This is the llc code generator driver. It provides a convenient 10*0b57cec5SDimitry Andric // command-line interface for generating native assembly-language code 11*0b57cec5SDimitry Andric // or C code, given LLVM bitcode. 12*0b57cec5SDimitry Andric // 13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 17*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 18*0b57cec5SDimitry Andric #include "llvm/CodeGen/CommandFlags.inc" 19*0b57cec5SDimitry Andric #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/LinkAllCodegenComponents.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MIRParser/MIRParser.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 26*0b57cec5SDimitry Andric #include "llvm/IR/AutoUpgrade.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h" 33*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 34*0b57cec5SDimitry Andric #include "llvm/IR/RemarkStreamer.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 36*0b57cec5SDimitry Andric #include "llvm/IRReader/IRReader.h" 37*0b57cec5SDimitry Andric #include "llvm/MC/SubtargetFeature.h" 38*0b57cec5SDimitry Andric #include "llvm/Pass.h" 39*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 40*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 41*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 42*0b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h" 43*0b57cec5SDimitry Andric #include "llvm/Support/Host.h" 44*0b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h" 45*0b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h" 46*0b57cec5SDimitry Andric #include "llvm/Support/PluginLoader.h" 47*0b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h" 48*0b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 49*0b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h" 50*0b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h" 51*0b57cec5SDimitry Andric #include "llvm/Support/WithColor.h" 52*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 53*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h" 54*0b57cec5SDimitry Andric #include <memory> 55*0b57cec5SDimitry Andric using namespace llvm; 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric // General options for llc. Other pass-specific options are specified 58*0b57cec5SDimitry Andric // within the corresponding llc passes, and target-specific options 59*0b57cec5SDimitry Andric // and back-end code generation options are specified with the target machine. 60*0b57cec5SDimitry Andric // 61*0b57cec5SDimitry Andric static cl::opt<std::string> 62*0b57cec5SDimitry Andric InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric static cl::opt<std::string> 65*0b57cec5SDimitry Andric InputLanguage("x", cl::desc("Input language ('ir' or 'mir')")); 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric static cl::opt<std::string> 68*0b57cec5SDimitry Andric OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric static cl::opt<std::string> 71*0b57cec5SDimitry Andric SplitDwarfOutputFile("split-dwarf-output", 72*0b57cec5SDimitry Andric cl::desc(".dwo output filename"), 73*0b57cec5SDimitry Andric cl::value_desc("filename")); 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric static cl::opt<unsigned> 76*0b57cec5SDimitry Andric TimeCompilations("time-compilations", cl::Hidden, cl::init(1u), 77*0b57cec5SDimitry Andric cl::value_desc("N"), 78*0b57cec5SDimitry Andric cl::desc("Repeat compilation N times for timing")); 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric static cl::opt<bool> 81*0b57cec5SDimitry Andric NoIntegratedAssembler("no-integrated-as", cl::Hidden, 82*0b57cec5SDimitry Andric cl::desc("Disable integrated assembler")); 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric static cl::opt<bool> 85*0b57cec5SDimitry Andric PreserveComments("preserve-as-comments", cl::Hidden, 86*0b57cec5SDimitry Andric cl::desc("Preserve Comments in outputted assembly"), 87*0b57cec5SDimitry Andric cl::init(true)); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric // Determine optimization level. 90*0b57cec5SDimitry Andric static cl::opt<char> 91*0b57cec5SDimitry Andric OptLevel("O", 92*0b57cec5SDimitry Andric cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " 93*0b57cec5SDimitry Andric "(default = '-O2')"), 94*0b57cec5SDimitry Andric cl::Prefix, 95*0b57cec5SDimitry Andric cl::ZeroOrMore, 96*0b57cec5SDimitry Andric cl::init(' ')); 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric static cl::opt<std::string> 99*0b57cec5SDimitry Andric TargetTriple("mtriple", cl::desc("Override target triple for module")); 100*0b57cec5SDimitry Andric 101*0b57cec5SDimitry Andric static cl::opt<std::string> SplitDwarfFile( 102*0b57cec5SDimitry Andric "split-dwarf-file", 103*0b57cec5SDimitry Andric cl::desc( 104*0b57cec5SDimitry Andric "Specify the name of the .dwo file to encode in the DWARF output")); 105*0b57cec5SDimitry Andric 106*0b57cec5SDimitry Andric static cl::opt<bool> NoVerify("disable-verify", cl::Hidden, 107*0b57cec5SDimitry Andric cl::desc("Do not verify input module")); 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls", 110*0b57cec5SDimitry Andric cl::desc("Disable simplify-libcalls")); 111*0b57cec5SDimitry Andric 112*0b57cec5SDimitry Andric static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden, 113*0b57cec5SDimitry Andric cl::desc("Show encoding in .s output")); 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric static cl::opt<bool> EnableDwarfDirectory( 116*0b57cec5SDimitry Andric "enable-dwarf-directory", cl::Hidden, 117*0b57cec5SDimitry Andric cl::desc("Use .file directives with an explicit directory.")); 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric static cl::opt<bool> AsmVerbose("asm-verbose", 120*0b57cec5SDimitry Andric cl::desc("Add comments to directives."), 121*0b57cec5SDimitry Andric cl::init(true)); 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric static cl::opt<bool> 124*0b57cec5SDimitry Andric CompileTwice("compile-twice", cl::Hidden, 125*0b57cec5SDimitry Andric cl::desc("Run everything twice, re-using the same pass " 126*0b57cec5SDimitry Andric "manager and verify the result is the same."), 127*0b57cec5SDimitry Andric cl::init(false)); 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric static cl::opt<bool> DiscardValueNames( 130*0b57cec5SDimitry Andric "discard-value-names", 131*0b57cec5SDimitry Andric cl::desc("Discard names from Value (other than GlobalValue)."), 132*0b57cec5SDimitry Andric cl::init(false), cl::Hidden); 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric static cl::list<std::string> IncludeDirs("I", cl::desc("include search path")); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric static cl::opt<bool> RemarksWithHotness( 137*0b57cec5SDimitry Andric "pass-remarks-with-hotness", 138*0b57cec5SDimitry Andric cl::desc("With PGO, include profile count in optimization remarks"), 139*0b57cec5SDimitry Andric cl::Hidden); 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric static cl::opt<unsigned> 142*0b57cec5SDimitry Andric RemarksHotnessThreshold("pass-remarks-hotness-threshold", 143*0b57cec5SDimitry Andric cl::desc("Minimum profile count required for " 144*0b57cec5SDimitry Andric "an optimization remark to be output"), 145*0b57cec5SDimitry Andric cl::Hidden); 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric static cl::opt<std::string> 148*0b57cec5SDimitry Andric RemarksFilename("pass-remarks-output", 149*0b57cec5SDimitry Andric cl::desc("Output filename for pass remarks"), 150*0b57cec5SDimitry Andric cl::value_desc("filename")); 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric static cl::opt<std::string> 153*0b57cec5SDimitry Andric RemarksPasses("pass-remarks-filter", 154*0b57cec5SDimitry Andric cl::desc("Only record optimization remarks from passes whose " 155*0b57cec5SDimitry Andric "names match the given regular expression"), 156*0b57cec5SDimitry Andric cl::value_desc("regex")); 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric static cl::opt<std::string> RemarksFormat( 159*0b57cec5SDimitry Andric "pass-remarks-format", 160*0b57cec5SDimitry Andric cl::desc("The format used for serializing remarks (default: YAML)"), 161*0b57cec5SDimitry Andric cl::value_desc("format"), cl::init("yaml")); 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric namespace { 164*0b57cec5SDimitry Andric static ManagedStatic<std::vector<std::string>> RunPassNames; 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric struct RunPassOption { 167*0b57cec5SDimitry Andric void operator=(const std::string &Val) const { 168*0b57cec5SDimitry Andric if (Val.empty()) 169*0b57cec5SDimitry Andric return; 170*0b57cec5SDimitry Andric SmallVector<StringRef, 8> PassNames; 171*0b57cec5SDimitry Andric StringRef(Val).split(PassNames, ',', -1, false); 172*0b57cec5SDimitry Andric for (auto PassName : PassNames) 173*0b57cec5SDimitry Andric RunPassNames->push_back(PassName); 174*0b57cec5SDimitry Andric } 175*0b57cec5SDimitry Andric }; 176*0b57cec5SDimitry Andric } 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric static RunPassOption RunPassOpt; 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass( 181*0b57cec5SDimitry Andric "run-pass", 182*0b57cec5SDimitry Andric cl::desc("Run compiler only for specified passes (comma separated list)"), 183*0b57cec5SDimitry Andric cl::value_desc("pass-name"), cl::ZeroOrMore, cl::location(RunPassOpt)); 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric static int compileModule(char **, LLVMContext &); 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName, 188*0b57cec5SDimitry Andric Triple::OSType OS, 189*0b57cec5SDimitry Andric const char *ProgName) { 190*0b57cec5SDimitry Andric // If we don't yet have an output filename, make one. 191*0b57cec5SDimitry Andric if (OutputFilename.empty()) { 192*0b57cec5SDimitry Andric if (InputFilename == "-") 193*0b57cec5SDimitry Andric OutputFilename = "-"; 194*0b57cec5SDimitry Andric else { 195*0b57cec5SDimitry Andric // If InputFilename ends in .bc or .ll, remove it. 196*0b57cec5SDimitry Andric StringRef IFN = InputFilename; 197*0b57cec5SDimitry Andric if (IFN.endswith(".bc") || IFN.endswith(".ll")) 198*0b57cec5SDimitry Andric OutputFilename = IFN.drop_back(3); 199*0b57cec5SDimitry Andric else if (IFN.endswith(".mir")) 200*0b57cec5SDimitry Andric OutputFilename = IFN.drop_back(4); 201*0b57cec5SDimitry Andric else 202*0b57cec5SDimitry Andric OutputFilename = IFN; 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric switch (FileType) { 205*0b57cec5SDimitry Andric case TargetMachine::CGFT_AssemblyFile: 206*0b57cec5SDimitry Andric if (TargetName[0] == 'c') { 207*0b57cec5SDimitry Andric if (TargetName[1] == 0) 208*0b57cec5SDimitry Andric OutputFilename += ".cbe.c"; 209*0b57cec5SDimitry Andric else if (TargetName[1] == 'p' && TargetName[2] == 'p') 210*0b57cec5SDimitry Andric OutputFilename += ".cpp"; 211*0b57cec5SDimitry Andric else 212*0b57cec5SDimitry Andric OutputFilename += ".s"; 213*0b57cec5SDimitry Andric } else 214*0b57cec5SDimitry Andric OutputFilename += ".s"; 215*0b57cec5SDimitry Andric break; 216*0b57cec5SDimitry Andric case TargetMachine::CGFT_ObjectFile: 217*0b57cec5SDimitry Andric if (OS == Triple::Win32) 218*0b57cec5SDimitry Andric OutputFilename += ".obj"; 219*0b57cec5SDimitry Andric else 220*0b57cec5SDimitry Andric OutputFilename += ".o"; 221*0b57cec5SDimitry Andric break; 222*0b57cec5SDimitry Andric case TargetMachine::CGFT_Null: 223*0b57cec5SDimitry Andric OutputFilename += ".null"; 224*0b57cec5SDimitry Andric break; 225*0b57cec5SDimitry Andric } 226*0b57cec5SDimitry Andric } 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric // Decide if we need "binary" output. 230*0b57cec5SDimitry Andric bool Binary = false; 231*0b57cec5SDimitry Andric switch (FileType) { 232*0b57cec5SDimitry Andric case TargetMachine::CGFT_AssemblyFile: 233*0b57cec5SDimitry Andric break; 234*0b57cec5SDimitry Andric case TargetMachine::CGFT_ObjectFile: 235*0b57cec5SDimitry Andric case TargetMachine::CGFT_Null: 236*0b57cec5SDimitry Andric Binary = true; 237*0b57cec5SDimitry Andric break; 238*0b57cec5SDimitry Andric } 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric // Open the file. 241*0b57cec5SDimitry Andric std::error_code EC; 242*0b57cec5SDimitry Andric sys::fs::OpenFlags OpenFlags = sys::fs::F_None; 243*0b57cec5SDimitry Andric if (!Binary) 244*0b57cec5SDimitry Andric OpenFlags |= sys::fs::F_Text; 245*0b57cec5SDimitry Andric auto FDOut = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags); 246*0b57cec5SDimitry Andric if (EC) { 247*0b57cec5SDimitry Andric WithColor::error() << EC.message() << '\n'; 248*0b57cec5SDimitry Andric return nullptr; 249*0b57cec5SDimitry Andric } 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric return FDOut; 252*0b57cec5SDimitry Andric } 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric struct LLCDiagnosticHandler : public DiagnosticHandler { 255*0b57cec5SDimitry Andric bool *HasError; 256*0b57cec5SDimitry Andric LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {} 257*0b57cec5SDimitry Andric bool handleDiagnostics(const DiagnosticInfo &DI) override { 258*0b57cec5SDimitry Andric if (DI.getSeverity() == DS_Error) 259*0b57cec5SDimitry Andric *HasError = true; 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 262*0b57cec5SDimitry Andric if (!Remark->isEnabled()) 263*0b57cec5SDimitry Andric return true; 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric DiagnosticPrinterRawOStream DP(errs()); 266*0b57cec5SDimitry Andric errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 267*0b57cec5SDimitry Andric DI.print(DP); 268*0b57cec5SDimitry Andric errs() << "\n"; 269*0b57cec5SDimitry Andric return true; 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric }; 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric static void InlineAsmDiagHandler(const SMDiagnostic &SMD, void *Context, 274*0b57cec5SDimitry Andric unsigned LocCookie) { 275*0b57cec5SDimitry Andric bool *HasError = static_cast<bool *>(Context); 276*0b57cec5SDimitry Andric if (SMD.getKind() == SourceMgr::DK_Error) 277*0b57cec5SDimitry Andric *HasError = true; 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric SMD.print(nullptr, errs()); 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric // For testing purposes, we print the LocCookie here. 282*0b57cec5SDimitry Andric if (LocCookie) 283*0b57cec5SDimitry Andric WithColor::note() << "!srcloc = " << LocCookie << "\n"; 284*0b57cec5SDimitry Andric } 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric // main - Entry point for the llc compiler. 287*0b57cec5SDimitry Andric // 288*0b57cec5SDimitry Andric int main(int argc, char **argv) { 289*0b57cec5SDimitry Andric InitLLVM X(argc, argv); 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric // Enable debug stream buffering. 292*0b57cec5SDimitry Andric EnableDebugBuffering = true; 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric LLVMContext Context; 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric // Initialize targets first, so that --version shows registered targets. 297*0b57cec5SDimitry Andric InitializeAllTargets(); 298*0b57cec5SDimitry Andric InitializeAllTargetMCs(); 299*0b57cec5SDimitry Andric InitializeAllAsmPrinters(); 300*0b57cec5SDimitry Andric InitializeAllAsmParsers(); 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric // Initialize codegen and IR passes used by llc so that the -print-after, 303*0b57cec5SDimitry Andric // -print-before, and -stop-after options work. 304*0b57cec5SDimitry Andric PassRegistry *Registry = PassRegistry::getPassRegistry(); 305*0b57cec5SDimitry Andric initializeCore(*Registry); 306*0b57cec5SDimitry Andric initializeCodeGen(*Registry); 307*0b57cec5SDimitry Andric initializeLoopStrengthReducePass(*Registry); 308*0b57cec5SDimitry Andric initializeLowerIntrinsicsPass(*Registry); 309*0b57cec5SDimitry Andric initializeEntryExitInstrumenterPass(*Registry); 310*0b57cec5SDimitry Andric initializePostInlineEntryExitInstrumenterPass(*Registry); 311*0b57cec5SDimitry Andric initializeUnreachableBlockElimLegacyPassPass(*Registry); 312*0b57cec5SDimitry Andric initializeConstantHoistingLegacyPassPass(*Registry); 313*0b57cec5SDimitry Andric initializeScalarOpts(*Registry); 314*0b57cec5SDimitry Andric initializeVectorization(*Registry); 315*0b57cec5SDimitry Andric initializeScalarizeMaskedMemIntrinPass(*Registry); 316*0b57cec5SDimitry Andric initializeExpandReductionsPass(*Registry); 317*0b57cec5SDimitry Andric initializeHardwareLoopsPass(*Registry); 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric // Initialize debugging passes. 320*0b57cec5SDimitry Andric initializeScavengerTestPass(*Registry); 321*0b57cec5SDimitry Andric 322*0b57cec5SDimitry Andric // Register the target printer for --version. 323*0b57cec5SDimitry Andric cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andric Context.setDiscardValueNames(DiscardValueNames); 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric // Set a diagnostic handler that doesn't exit on the first error 330*0b57cec5SDimitry Andric bool HasError = false; 331*0b57cec5SDimitry Andric Context.setDiagnosticHandler( 332*0b57cec5SDimitry Andric llvm::make_unique<LLCDiagnosticHandler>(&HasError)); 333*0b57cec5SDimitry Andric Context.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, &HasError); 334*0b57cec5SDimitry Andric 335*0b57cec5SDimitry Andric Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr = 336*0b57cec5SDimitry Andric setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses, 337*0b57cec5SDimitry Andric RemarksFormat, RemarksWithHotness, 338*0b57cec5SDimitry Andric RemarksHotnessThreshold); 339*0b57cec5SDimitry Andric if (Error E = RemarksFileOrErr.takeError()) { 340*0b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << toString(std::move(E)) << '\n'; 341*0b57cec5SDimitry Andric return 1; 342*0b57cec5SDimitry Andric } 343*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr); 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric if (InputLanguage != "" && InputLanguage != "ir" && 346*0b57cec5SDimitry Andric InputLanguage != "mir") { 347*0b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) 348*0b57cec5SDimitry Andric << "input language must be '', 'IR' or 'MIR'\n"; 349*0b57cec5SDimitry Andric return 1; 350*0b57cec5SDimitry Andric } 351*0b57cec5SDimitry Andric 352*0b57cec5SDimitry Andric // Compile the module TimeCompilations times to give better compile time 353*0b57cec5SDimitry Andric // metrics. 354*0b57cec5SDimitry Andric for (unsigned I = TimeCompilations; I; --I) 355*0b57cec5SDimitry Andric if (int RetVal = compileModule(argv, Context)) 356*0b57cec5SDimitry Andric return RetVal; 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric if (RemarksFile) 359*0b57cec5SDimitry Andric RemarksFile->keep(); 360*0b57cec5SDimitry Andric return 0; 361*0b57cec5SDimitry Andric } 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric static bool addPass(PassManagerBase &PM, const char *argv0, 364*0b57cec5SDimitry Andric StringRef PassName, TargetPassConfig &TPC) { 365*0b57cec5SDimitry Andric if (PassName == "none") 366*0b57cec5SDimitry Andric return false; 367*0b57cec5SDimitry Andric 368*0b57cec5SDimitry Andric const PassRegistry *PR = PassRegistry::getPassRegistry(); 369*0b57cec5SDimitry Andric const PassInfo *PI = PR->getPassInfo(PassName); 370*0b57cec5SDimitry Andric if (!PI) { 371*0b57cec5SDimitry Andric WithColor::error(errs(), argv0) 372*0b57cec5SDimitry Andric << "run-pass " << PassName << " is not registered.\n"; 373*0b57cec5SDimitry Andric return true; 374*0b57cec5SDimitry Andric } 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric Pass *P; 377*0b57cec5SDimitry Andric if (PI->getNormalCtor()) 378*0b57cec5SDimitry Andric P = PI->getNormalCtor()(); 379*0b57cec5SDimitry Andric else { 380*0b57cec5SDimitry Andric WithColor::error(errs(), argv0) 381*0b57cec5SDimitry Andric << "cannot create pass: " << PI->getPassName() << "\n"; 382*0b57cec5SDimitry Andric return true; 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric std::string Banner = std::string("After ") + std::string(P->getPassName()); 385*0b57cec5SDimitry Andric PM.add(P); 386*0b57cec5SDimitry Andric TPC.printAndVerify(Banner); 387*0b57cec5SDimitry Andric 388*0b57cec5SDimitry Andric return false; 389*0b57cec5SDimitry Andric } 390*0b57cec5SDimitry Andric 391*0b57cec5SDimitry Andric static int compileModule(char **argv, LLVMContext &Context) { 392*0b57cec5SDimitry Andric // Load the module to be compiled... 393*0b57cec5SDimitry Andric SMDiagnostic Err; 394*0b57cec5SDimitry Andric std::unique_ptr<Module> M; 395*0b57cec5SDimitry Andric std::unique_ptr<MIRParser> MIR; 396*0b57cec5SDimitry Andric Triple TheTriple; 397*0b57cec5SDimitry Andric 398*0b57cec5SDimitry Andric bool SkipModule = MCPU == "help" || 399*0b57cec5SDimitry Andric (!MAttrs.empty() && MAttrs.front() == "help"); 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric // If user just wants to list available options, skip module loading 402*0b57cec5SDimitry Andric if (!SkipModule) { 403*0b57cec5SDimitry Andric if (InputLanguage == "mir" || 404*0b57cec5SDimitry Andric (InputLanguage == "" && StringRef(InputFilename).endswith(".mir"))) { 405*0b57cec5SDimitry Andric MIR = createMIRParserFromFile(InputFilename, Err, Context); 406*0b57cec5SDimitry Andric if (MIR) 407*0b57cec5SDimitry Andric M = MIR->parseIRModule(); 408*0b57cec5SDimitry Andric } else 409*0b57cec5SDimitry Andric M = parseIRFile(InputFilename, Err, Context, false); 410*0b57cec5SDimitry Andric if (!M) { 411*0b57cec5SDimitry Andric Err.print(argv[0], WithColor::error(errs(), argv[0])); 412*0b57cec5SDimitry Andric return 1; 413*0b57cec5SDimitry Andric } 414*0b57cec5SDimitry Andric 415*0b57cec5SDimitry Andric // If we are supposed to override the target triple, do so now. 416*0b57cec5SDimitry Andric if (!TargetTriple.empty()) 417*0b57cec5SDimitry Andric M->setTargetTriple(Triple::normalize(TargetTriple)); 418*0b57cec5SDimitry Andric TheTriple = Triple(M->getTargetTriple()); 419*0b57cec5SDimitry Andric } else { 420*0b57cec5SDimitry Andric TheTriple = Triple(Triple::normalize(TargetTriple)); 421*0b57cec5SDimitry Andric } 422*0b57cec5SDimitry Andric 423*0b57cec5SDimitry Andric if (TheTriple.getTriple().empty()) 424*0b57cec5SDimitry Andric TheTriple.setTriple(sys::getDefaultTargetTriple()); 425*0b57cec5SDimitry Andric 426*0b57cec5SDimitry Andric // Get the target specific parser. 427*0b57cec5SDimitry Andric std::string Error; 428*0b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, 429*0b57cec5SDimitry Andric Error); 430*0b57cec5SDimitry Andric if (!TheTarget) { 431*0b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << Error; 432*0b57cec5SDimitry Andric return 1; 433*0b57cec5SDimitry Andric } 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr(); 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric CodeGenOpt::Level OLvl = CodeGenOpt::Default; 438*0b57cec5SDimitry Andric switch (OptLevel) { 439*0b57cec5SDimitry Andric default: 440*0b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << "invalid optimization level.\n"; 441*0b57cec5SDimitry Andric return 1; 442*0b57cec5SDimitry Andric case ' ': break; 443*0b57cec5SDimitry Andric case '0': OLvl = CodeGenOpt::None; break; 444*0b57cec5SDimitry Andric case '1': OLvl = CodeGenOpt::Less; break; 445*0b57cec5SDimitry Andric case '2': OLvl = CodeGenOpt::Default; break; 446*0b57cec5SDimitry Andric case '3': OLvl = CodeGenOpt::Aggressive; break; 447*0b57cec5SDimitry Andric } 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); 450*0b57cec5SDimitry Andric Options.DisableIntegratedAS = NoIntegratedAssembler; 451*0b57cec5SDimitry Andric Options.MCOptions.ShowMCEncoding = ShowMCEncoding; 452*0b57cec5SDimitry Andric Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory; 453*0b57cec5SDimitry Andric Options.MCOptions.AsmVerbose = AsmVerbose; 454*0b57cec5SDimitry Andric Options.MCOptions.PreserveAsmComments = PreserveComments; 455*0b57cec5SDimitry Andric Options.MCOptions.IASSearchPaths = IncludeDirs; 456*0b57cec5SDimitry Andric Options.MCOptions.SplitDwarfFile = SplitDwarfFile; 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine( 459*0b57cec5SDimitry Andric TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(), 460*0b57cec5SDimitry Andric getCodeModel(), OLvl)); 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric assert(Target && "Could not allocate target machine!"); 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric // If we don't have a module then just exit now. We do this down 465*0b57cec5SDimitry Andric // here since the CPU/Feature help is underneath the target machine 466*0b57cec5SDimitry Andric // creation. 467*0b57cec5SDimitry Andric if (SkipModule) 468*0b57cec5SDimitry Andric return 0; 469*0b57cec5SDimitry Andric 470*0b57cec5SDimitry Andric assert(M && "Should have exited if we didn't have a module!"); 471*0b57cec5SDimitry Andric if (FloatABIForCalls != FloatABI::Default) 472*0b57cec5SDimitry Andric Options.FloatABIType = FloatABIForCalls; 473*0b57cec5SDimitry Andric 474*0b57cec5SDimitry Andric // Figure out where we are going to send the output. 475*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> Out = 476*0b57cec5SDimitry Andric GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]); 477*0b57cec5SDimitry Andric if (!Out) return 1; 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> DwoOut; 480*0b57cec5SDimitry Andric if (!SplitDwarfOutputFile.empty()) { 481*0b57cec5SDimitry Andric std::error_code EC; 482*0b57cec5SDimitry Andric DwoOut = llvm::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC, 483*0b57cec5SDimitry Andric sys::fs::F_None); 484*0b57cec5SDimitry Andric if (EC) { 485*0b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << EC.message() << '\n'; 486*0b57cec5SDimitry Andric return 1; 487*0b57cec5SDimitry Andric } 488*0b57cec5SDimitry Andric } 489*0b57cec5SDimitry Andric 490*0b57cec5SDimitry Andric // Build up all of the passes that we want to do to the module. 491*0b57cec5SDimitry Andric legacy::PassManager PM; 492*0b57cec5SDimitry Andric 493*0b57cec5SDimitry Andric // Add an appropriate TargetLibraryInfo pass for the module's triple. 494*0b57cec5SDimitry Andric TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple())); 495*0b57cec5SDimitry Andric 496*0b57cec5SDimitry Andric // The -disable-simplify-libcalls flag actually disables all builtin optzns. 497*0b57cec5SDimitry Andric if (DisableSimplifyLibCalls) 498*0b57cec5SDimitry Andric TLII.disableAllFunctions(); 499*0b57cec5SDimitry Andric PM.add(new TargetLibraryInfoWrapperPass(TLII)); 500*0b57cec5SDimitry Andric 501*0b57cec5SDimitry Andric // Add the target data from the target machine, if it exists, or the module. 502*0b57cec5SDimitry Andric M->setDataLayout(Target->createDataLayout()); 503*0b57cec5SDimitry Andric 504*0b57cec5SDimitry Andric // This needs to be done after setting datalayout since it calls verifier 505*0b57cec5SDimitry Andric // to check debug info whereas verifier relies on correct datalayout. 506*0b57cec5SDimitry Andric UpgradeDebugInfo(*M); 507*0b57cec5SDimitry Andric 508*0b57cec5SDimitry Andric // Verify module immediately to catch problems before doInitialization() is 509*0b57cec5SDimitry Andric // called on any passes. 510*0b57cec5SDimitry Andric if (!NoVerify && verifyModule(*M, &errs())) { 511*0b57cec5SDimitry Andric std::string Prefix = 512*0b57cec5SDimitry Andric (Twine(argv[0]) + Twine(": ") + Twine(InputFilename)).str(); 513*0b57cec5SDimitry Andric WithColor::error(errs(), Prefix) << "input module is broken!\n"; 514*0b57cec5SDimitry Andric return 1; 515*0b57cec5SDimitry Andric } 516*0b57cec5SDimitry Andric 517*0b57cec5SDimitry Andric // Override function attributes based on CPUStr, FeaturesStr, and command line 518*0b57cec5SDimitry Andric // flags. 519*0b57cec5SDimitry Andric setFunctionAttributes(CPUStr, FeaturesStr, *M); 520*0b57cec5SDimitry Andric 521*0b57cec5SDimitry Andric if (RelaxAll.getNumOccurrences() > 0 && 522*0b57cec5SDimitry Andric FileType != TargetMachine::CGFT_ObjectFile) 523*0b57cec5SDimitry Andric WithColor::warning(errs(), argv[0]) 524*0b57cec5SDimitry Andric << ": warning: ignoring -mc-relax-all because filetype != obj"; 525*0b57cec5SDimitry Andric 526*0b57cec5SDimitry Andric { 527*0b57cec5SDimitry Andric raw_pwrite_stream *OS = &Out->os(); 528*0b57cec5SDimitry Andric 529*0b57cec5SDimitry Andric // Manually do the buffering rather than using buffer_ostream, 530*0b57cec5SDimitry Andric // so we can memcmp the contents in CompileTwice mode 531*0b57cec5SDimitry Andric SmallVector<char, 0> Buffer; 532*0b57cec5SDimitry Andric std::unique_ptr<raw_svector_ostream> BOS; 533*0b57cec5SDimitry Andric if ((FileType != TargetMachine::CGFT_AssemblyFile && 534*0b57cec5SDimitry Andric !Out->os().supportsSeeking()) || 535*0b57cec5SDimitry Andric CompileTwice) { 536*0b57cec5SDimitry Andric BOS = make_unique<raw_svector_ostream>(Buffer); 537*0b57cec5SDimitry Andric OS = BOS.get(); 538*0b57cec5SDimitry Andric } 539*0b57cec5SDimitry Andric 540*0b57cec5SDimitry Andric const char *argv0 = argv[0]; 541*0b57cec5SDimitry Andric LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine&>(*Target); 542*0b57cec5SDimitry Andric MachineModuleInfo *MMI = new MachineModuleInfo(&LLVMTM); 543*0b57cec5SDimitry Andric 544*0b57cec5SDimitry Andric // Construct a custom pass pipeline that starts after instruction 545*0b57cec5SDimitry Andric // selection. 546*0b57cec5SDimitry Andric if (!RunPassNames->empty()) { 547*0b57cec5SDimitry Andric if (!MIR) { 548*0b57cec5SDimitry Andric WithColor::warning(errs(), argv[0]) 549*0b57cec5SDimitry Andric << "run-pass is for .mir file only.\n"; 550*0b57cec5SDimitry Andric return 1; 551*0b57cec5SDimitry Andric } 552*0b57cec5SDimitry Andric TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM); 553*0b57cec5SDimitry Andric if (TPC.hasLimitedCodeGenPipeline()) { 554*0b57cec5SDimitry Andric WithColor::warning(errs(), argv[0]) 555*0b57cec5SDimitry Andric << "run-pass cannot be used with " 556*0b57cec5SDimitry Andric << TPC.getLimitedCodeGenPipelineReason(" and ") << ".\n"; 557*0b57cec5SDimitry Andric return 1; 558*0b57cec5SDimitry Andric } 559*0b57cec5SDimitry Andric 560*0b57cec5SDimitry Andric TPC.setDisableVerify(NoVerify); 561*0b57cec5SDimitry Andric PM.add(&TPC); 562*0b57cec5SDimitry Andric PM.add(MMI); 563*0b57cec5SDimitry Andric TPC.printAndVerify(""); 564*0b57cec5SDimitry Andric for (const std::string &RunPassName : *RunPassNames) { 565*0b57cec5SDimitry Andric if (addPass(PM, argv0, RunPassName, TPC)) 566*0b57cec5SDimitry Andric return 1; 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric TPC.setInitialized(); 569*0b57cec5SDimitry Andric PM.add(createPrintMIRPass(*OS)); 570*0b57cec5SDimitry Andric PM.add(createFreeMachineFunctionPass()); 571*0b57cec5SDimitry Andric } else if (Target->addPassesToEmitFile(PM, *OS, 572*0b57cec5SDimitry Andric DwoOut ? &DwoOut->os() : nullptr, 573*0b57cec5SDimitry Andric FileType, NoVerify, MMI)) { 574*0b57cec5SDimitry Andric WithColor::warning(errs(), argv[0]) 575*0b57cec5SDimitry Andric << "target does not support generation of this" 576*0b57cec5SDimitry Andric << " file type!\n"; 577*0b57cec5SDimitry Andric return 1; 578*0b57cec5SDimitry Andric } 579*0b57cec5SDimitry Andric 580*0b57cec5SDimitry Andric if (MIR) { 581*0b57cec5SDimitry Andric assert(MMI && "Forgot to create MMI?"); 582*0b57cec5SDimitry Andric if (MIR->parseMachineFunctions(*M, *MMI)) 583*0b57cec5SDimitry Andric return 1; 584*0b57cec5SDimitry Andric } 585*0b57cec5SDimitry Andric 586*0b57cec5SDimitry Andric // Before executing passes, print the final values of the LLVM options. 587*0b57cec5SDimitry Andric cl::PrintOptionValues(); 588*0b57cec5SDimitry Andric 589*0b57cec5SDimitry Andric // If requested, run the pass manager over the same module again, 590*0b57cec5SDimitry Andric // to catch any bugs due to persistent state in the passes. Note that 591*0b57cec5SDimitry Andric // opt has the same functionality, so it may be worth abstracting this out 592*0b57cec5SDimitry Andric // in the future. 593*0b57cec5SDimitry Andric SmallVector<char, 0> CompileTwiceBuffer; 594*0b57cec5SDimitry Andric if (CompileTwice) { 595*0b57cec5SDimitry Andric std::unique_ptr<Module> M2(llvm::CloneModule(*M)); 596*0b57cec5SDimitry Andric PM.run(*M2); 597*0b57cec5SDimitry Andric CompileTwiceBuffer = Buffer; 598*0b57cec5SDimitry Andric Buffer.clear(); 599*0b57cec5SDimitry Andric } 600*0b57cec5SDimitry Andric 601*0b57cec5SDimitry Andric PM.run(*M); 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric auto HasError = 604*0b57cec5SDimitry Andric ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError; 605*0b57cec5SDimitry Andric if (*HasError) 606*0b57cec5SDimitry Andric return 1; 607*0b57cec5SDimitry Andric 608*0b57cec5SDimitry Andric // Compare the two outputs and make sure they're the same 609*0b57cec5SDimitry Andric if (CompileTwice) { 610*0b57cec5SDimitry Andric if (Buffer.size() != CompileTwiceBuffer.size() || 611*0b57cec5SDimitry Andric (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) != 612*0b57cec5SDimitry Andric 0)) { 613*0b57cec5SDimitry Andric errs() 614*0b57cec5SDimitry Andric << "Running the pass manager twice changed the output.\n" 615*0b57cec5SDimitry Andric "Writing the result of the second run to the specified output\n" 616*0b57cec5SDimitry Andric "To generate the one-run comparison binary, just run without\n" 617*0b57cec5SDimitry Andric "the compile-twice option\n"; 618*0b57cec5SDimitry Andric Out->os() << Buffer; 619*0b57cec5SDimitry Andric Out->keep(); 620*0b57cec5SDimitry Andric return 1; 621*0b57cec5SDimitry Andric } 622*0b57cec5SDimitry Andric } 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric if (BOS) { 625*0b57cec5SDimitry Andric Out->os() << Buffer; 626*0b57cec5SDimitry Andric } 627*0b57cec5SDimitry Andric } 628*0b57cec5SDimitry Andric 629*0b57cec5SDimitry Andric // Declare success. 630*0b57cec5SDimitry Andric Out->keep(); 631*0b57cec5SDimitry Andric if (DwoOut) 632*0b57cec5SDimitry Andric DwoOut->keep(); 633*0b57cec5SDimitry Andric 634*0b57cec5SDimitry Andric return 0; 635*0b57cec5SDimitry Andric } 636