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