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