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