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