1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// 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 // This file implements the LLVMTargetMachine class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Target/TargetMachine.h" 15 #include "llvm/Analysis/Passes.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/BasicTTIImpl.h" 18 #include "llvm/CodeGen/MachineFunctionAnalysis.h" 19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 #include "llvm/CodeGen/Passes.h" 21 #include "llvm/IR/IRPrintingPasses.h" 22 #include "llvm/IR/LegacyPassManager.h" 23 #include "llvm/IR/Verifier.h" 24 #include "llvm/MC/MCAsmInfo.h" 25 #include "llvm/MC/MCContext.h" 26 #include "llvm/MC/MCInstrInfo.h" 27 #include "llvm/MC/MCStreamer.h" 28 #include "llvm/MC/MCSubtargetInfo.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/FormattedStream.h" 32 #include "llvm/Support/TargetRegistry.h" 33 #include "llvm/Target/TargetLoweringObjectFile.h" 34 #include "llvm/Target/TargetOptions.h" 35 #include "llvm/Transforms/Scalar.h" 36 using namespace llvm; 37 38 // Enable or disable FastISel. Both options are needed, because 39 // FastISel is enabled by default with -fast, and we wish to be 40 // able to enable or disable fast-isel independently from -O0. 41 static cl::opt<cl::boolOrDefault> 42 EnableFastISelOption("fast-isel", cl::Hidden, 43 cl::desc("Enable the \"fast\" instruction selector")); 44 45 void LLVMTargetMachine::initAsmInfo() { 46 MRI = TheTarget.createMCRegInfo(getTargetTriple()); 47 MII = TheTarget.createMCInstrInfo(); 48 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due 49 // to some backends having subtarget feature dependent module level 50 // code generation. This is similar to the hack in the AsmPrinter for 51 // module level assembly etc. 52 STI = TheTarget.createMCSubtargetInfo(getTargetTriple(), getTargetCPU(), 53 getTargetFeatureString()); 54 55 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*MRI, getTargetTriple()); 56 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 57 // and if the old one gets included then MCAsmInfo will be NULL and 58 // we'll crash later. 59 // Provide the user with a useful error message about what's wrong. 60 assert(TmpAsmInfo && "MCAsmInfo not initialized. " 61 "Make sure you include the correct TargetSelect.h" 62 "and that InitializeAllTargetMCs() is being invoked!"); 63 64 if (Options.DisableIntegratedAS) 65 TmpAsmInfo->setUseIntegratedAssembler(false); 66 67 if (Options.CompressDebugSections) 68 TmpAsmInfo->setCompressDebugSections(true); 69 70 AsmInfo = TmpAsmInfo; 71 } 72 73 LLVMTargetMachine::LLVMTargetMachine(const Target &T, 74 StringRef DataLayoutString, 75 StringRef Triple, StringRef CPU, 76 StringRef FS, TargetOptions Options, 77 Reloc::Model RM, CodeModel::Model CM, 78 CodeGenOpt::Level OL) 79 : TargetMachine(T, DataLayoutString, Triple, CPU, FS, Options) { 80 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL); 81 } 82 83 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { 84 return TargetIRAnalysis([this](Function &F) { 85 return TargetTransformInfo(BasicTTIImpl(this, F)); 86 }); 87 } 88 89 /// addPassesToX helper drives creation and initialization of TargetPassConfig. 90 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM, 91 PassManagerBase &PM, 92 bool DisableVerify, 93 AnalysisID StartAfter, 94 AnalysisID StopAfter) { 95 96 // Add internal analysis passes from the target machine. 97 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); 98 99 // Targets may override createPassConfig to provide a target-specific 100 // subclass. 101 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 102 PassConfig->setStartStopPasses(StartAfter, StopAfter); 103 104 // Set PassConfig options provided by TargetMachine. 105 PassConfig->setDisableVerify(DisableVerify); 106 107 PM.add(PassConfig); 108 109 PassConfig->addIRPasses(); 110 111 PassConfig->addCodeGenPrepare(); 112 113 PassConfig->addPassesToHandleExceptions(); 114 115 PassConfig->addISelPrepare(); 116 117 // Install a MachineModuleInfo class, which is an immutable pass that holds 118 // all the per-module stuff we're generating, including MCContext. 119 MachineModuleInfo *MMI = new MachineModuleInfo( 120 *TM->getMCAsmInfo(), *TM->getMCRegisterInfo(), TM->getObjFileLowering()); 121 PM.add(MMI); 122 123 // Set up a MachineFunction for the rest of CodeGen to work on. 124 PM.add(new MachineFunctionAnalysis(*TM)); 125 126 // Enable FastISel with -fast, but allow that to be overridden. 127 if (EnableFastISelOption == cl::BOU_TRUE || 128 (TM->getOptLevel() == CodeGenOpt::None && 129 EnableFastISelOption != cl::BOU_FALSE)) 130 TM->setFastISel(true); 131 132 // Ask the target for an isel. 133 if (PassConfig->addInstSelector()) 134 return nullptr; 135 136 PassConfig->addMachinePasses(); 137 138 PassConfig->setInitialized(); 139 140 return &MMI->getContext(); 141 } 142 143 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 144 formatted_raw_ostream &Out, 145 CodeGenFileType FileType, 146 bool DisableVerify, 147 AnalysisID StartAfter, 148 AnalysisID StopAfter) { 149 // Add common CodeGen passes. 150 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 151 StartAfter, StopAfter); 152 if (!Context) 153 return true; 154 155 if (StopAfter) { 156 // FIXME: The intent is that this should eventually write out a YAML file, 157 // containing the LLVM IR, the machine-level IR (when stopping after a 158 // machine-level pass), and whatever other information is needed to 159 // deserialize the code and resume compilation. For now, just write the 160 // LLVM IR. 161 PM.add(createPrintModulePass(Out)); 162 return false; 163 } 164 165 if (Options.MCOptions.MCSaveTempLabels) 166 Context->setAllowTemporaryLabels(false); 167 168 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 169 const MCAsmInfo &MAI = *getMCAsmInfo(); 170 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 171 const MCInstrInfo &MII = *getMCInstrInfo(); 172 173 std::unique_ptr<MCStreamer> AsmStreamer; 174 175 switch (FileType) { 176 case CGFT_AssemblyFile: { 177 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 178 Triple(getTargetTriple()), MAI.getAssemblerDialect(), MAI, MII, MRI); 179 180 // Create a code emitter if asked to show the encoding. 181 MCCodeEmitter *MCE = nullptr; 182 if (Options.MCOptions.ShowMCEncoding) 183 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context); 184 185 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 186 TargetCPU); 187 MCStreamer *S = getTarget().createAsmStreamer( 188 *Context, Out, Options.MCOptions.AsmVerbose, 189 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 190 Options.MCOptions.ShowMCInst); 191 AsmStreamer.reset(S); 192 break; 193 } 194 case CGFT_ObjectFile: { 195 // Create the code emitter for the target if it exists. If not, .o file 196 // emission fails. 197 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context); 198 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 199 TargetCPU); 200 if (!MCE || !MAB) 201 return true; 202 203 Triple T(getTargetTriple()); 204 AsmStreamer.reset(getTarget().createMCObjectStreamer( 205 T, *Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll, 206 /*DWARFMustBeAtTheEnd*/ true)); 207 break; 208 } 209 case CGFT_Null: 210 // The Null output is intended for use for performance analysis and testing, 211 // not real users. 212 AsmStreamer.reset(getTarget().createNullStreamer(*Context)); 213 break; 214 } 215 216 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 217 FunctionPass *Printer = 218 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 219 if (!Printer) 220 return true; 221 222 PM.add(Printer); 223 224 return false; 225 } 226 227 /// addPassesToEmitMC - Add passes to the specified pass manager to get 228 /// machine code emitted with the MCJIT. This method returns true if machine 229 /// code is not supported. It fills the MCContext Ctx pointer which can be 230 /// used to build custom MCStreamer. 231 /// 232 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, 233 MCContext *&Ctx, 234 raw_ostream &Out, 235 bool DisableVerify) { 236 // Add common CodeGen passes. 237 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr); 238 if (!Ctx) 239 return true; 240 241 if (Options.MCOptions.MCSaveTempLabels) 242 Ctx->setAllowTemporaryLabels(false); 243 244 // Create the code emitter for the target if it exists. If not, .o file 245 // emission fails. 246 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 247 MCCodeEmitter *MCE = 248 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 249 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 250 TargetCPU); 251 if (!MCE || !MAB) 252 return true; 253 254 Triple T(getTargetTriple()); 255 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 256 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 257 T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll, 258 /*DWARFMustBeAtTheEnd*/ true)); 259 260 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 261 FunctionPass *Printer = 262 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 263 if (!Printer) 264 return true; 265 266 PM.add(Printer); 267 268 return false; // success! 269 } 270