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