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/Analysis/JumpInstrTableInfo.h" 16 #include "llvm/Analysis/Passes.h" 17 #include "llvm/CodeGen/AsmPrinter.h" 18 #include "llvm/CodeGen/BasicTTIImpl.h" 19 #include "llvm/CodeGen/ForwardControlFlowIntegrity.h" 20 #include "llvm/CodeGen/JumpInstrTables.h" 21 #include "llvm/CodeGen/MachineFunctionAnalysis.h" 22 #include "llvm/CodeGen/MachineModuleInfo.h" 23 #include "llvm/CodeGen/Passes.h" 24 #include "llvm/IR/IRPrintingPasses.h" 25 #include "llvm/IR/Verifier.h" 26 #include "llvm/MC/MCAsmInfo.h" 27 #include "llvm/MC/MCContext.h" 28 #include "llvm/MC/MCInstrInfo.h" 29 #include "llvm/MC/MCStreamer.h" 30 #include "llvm/MC/MCSubtargetInfo.h" 31 #include "llvm/PassManager.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/FormattedStream.h" 35 #include "llvm/Support/TargetRegistry.h" 36 #include "llvm/Target/TargetInstrInfo.h" 37 #include "llvm/Target/TargetLowering.h" 38 #include "llvm/Target/TargetLoweringObjectFile.h" 39 #include "llvm/Target/TargetOptions.h" 40 #include "llvm/Target/TargetRegisterInfo.h" 41 #include "llvm/Target/TargetSubtargetInfo.h" 42 #include "llvm/Transforms/Scalar.h" 43 using namespace llvm; 44 45 // Enable or disable FastISel. Both options are needed, because 46 // FastISel is enabled by default with -fast, and we wish to be 47 // able to enable or disable fast-isel independently from -O0. 48 static cl::opt<cl::boolOrDefault> 49 EnableFastISelOption("fast-isel", cl::Hidden, 50 cl::desc("Enable the \"fast\" instruction selector")); 51 52 void LLVMTargetMachine::initAsmInfo() { 53 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo( 54 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple()); 55 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 56 // and if the old one gets included then MCAsmInfo will be NULL and 57 // we'll crash later. 58 // Provide the user with a useful error message about what's wrong. 59 assert(TmpAsmInfo && "MCAsmInfo not initialized. " 60 "Make sure you include the correct TargetSelect.h" 61 "and that InitializeAllTargetMCs() is being invoked!"); 62 63 if (Options.DisableIntegratedAS) 64 TmpAsmInfo->setUseIntegratedAssembler(false); 65 66 if (Options.CompressDebugSections) 67 TmpAsmInfo->setCompressDebugSections(true); 68 69 AsmInfo = TmpAsmInfo; 70 } 71 72 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple, 73 StringRef CPU, StringRef FS, 74 TargetOptions Options, 75 Reloc::Model RM, CodeModel::Model CM, 76 CodeGenOpt::Level OL) 77 : TargetMachine(T, Triple, CPU, FS, Options) { 78 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL); 79 } 80 81 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { 82 return TargetIRAnalysis([this](Function &F) { 83 return TargetTransformInfo(BasicTTIImpl(this, F)); 84 }); 85 } 86 87 /// addPassesToX helper drives creation and initialization of TargetPassConfig. 88 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM, 89 PassManagerBase &PM, 90 bool DisableVerify, 91 AnalysisID StartAfter, 92 AnalysisID StopAfter) { 93 94 // Add internal analysis passes from the target machine. 95 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); 96 97 // Targets may override createPassConfig to provide a target-specific 98 // subclass. 99 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 100 PassConfig->setStartStopPasses(StartAfter, StopAfter); 101 102 // Set PassConfig options provided by TargetMachine. 103 PassConfig->setDisableVerify(DisableVerify); 104 105 PM.add(PassConfig); 106 107 PassConfig->addIRPasses(); 108 109 PassConfig->addCodeGenPrepare(); 110 111 PassConfig->addPassesToHandleExceptions(); 112 113 PassConfig->addISelPrepare(); 114 115 // Install a MachineModuleInfo class, which is an immutable pass that holds 116 // all the per-module stuff we're generating, including MCContext. 117 MachineModuleInfo *MMI = new MachineModuleInfo( 118 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(), 119 TM->getObjFileLowering()); 120 PM.add(MMI); 121 122 // Set up a MachineFunction for the rest of CodeGen to work on. 123 PM.add(new MachineFunctionAnalysis(*TM)); 124 125 // Enable FastISel with -fast, but allow that to be overridden. 126 if (EnableFastISelOption == cl::BOU_TRUE || 127 (TM->getOptLevel() == CodeGenOpt::None && 128 EnableFastISelOption != cl::BOU_FALSE)) 129 TM->setFastISel(true); 130 131 // Ask the target for an isel. 132 if (PassConfig->addInstSelector()) 133 return nullptr; 134 135 PassConfig->addMachinePasses(); 136 137 PassConfig->setInitialized(); 138 139 return &MMI->getContext(); 140 } 141 142 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 143 formatted_raw_ostream &Out, 144 CodeGenFileType FileType, 145 bool DisableVerify, 146 AnalysisID StartAfter, 147 AnalysisID StopAfter) { 148 // Passes to handle jumptable function annotations. These can't be handled at 149 // JIT time, so we don't add them directly to addPassesToGenerateCode. 150 PM.add(createJumpInstrTableInfoPass( 151 getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound())); 152 PM.add(createJumpInstrTablesPass(Options.JTType)); 153 if (Options.FCFI) 154 PM.add(createForwardControlFlowIntegrityPass( 155 Options.JTType, Options.CFIType, Options.CFIEnforcing, 156 Options.getCFIFuncName())); 157 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 (Options.MCOptions.MCSaveTempLabels) 175 Context->setAllowTemporaryLabels(false); 176 177 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); 178 const MCAsmInfo &MAI = *getMCAsmInfo(); 179 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo(); 180 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo(); 181 std::unique_ptr<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 = nullptr; 191 if (Options.MCOptions.ShowMCEncoding) 192 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context); 193 194 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 195 TargetCPU); 196 MCStreamer *S = getTarget().createAsmStreamer( 197 *Context, Out, Options.MCOptions.AsmVerbose, 198 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 199 Options.MCOptions.ShowMCInst); 200 AsmStreamer.reset(S); 201 break; 202 } 203 case CGFT_ObjectFile: { 204 // Create the code emitter for the target if it exists. If not, .o file 205 // emission fails. 206 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, 207 *Context); 208 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 209 TargetCPU); 210 if (!MCE || !MAB) 211 return true; 212 213 AsmStreamer.reset( 214 getTarget() 215 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE, 216 STI, Options.MCOptions.MCRelaxAll)); 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(getTarget().createNullStreamer(*Context)); 223 break; 224 } 225 226 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 227 FunctionPass *Printer = 228 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 229 if (!Printer) 230 return true; 231 232 PM.add(Printer); 233 234 return false; 235 } 236 237 /// addPassesToEmitMC - Add passes to the specified pass manager to get 238 /// machine code emitted with the MCJIT. This method returns true if machine 239 /// code is not supported. It fills the MCContext Ctx pointer which can be 240 /// used to build custom MCStreamer. 241 /// 242 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, 243 MCContext *&Ctx, 244 raw_ostream &Out, 245 bool DisableVerify) { 246 // Add common CodeGen passes. 247 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr); 248 if (!Ctx) 249 return true; 250 251 if (Options.MCOptions.MCSaveTempLabels) 252 Ctx->setAllowTemporaryLabels(false); 253 254 // Create the code emitter for the target if it exists. If not, .o file 255 // emission fails. 256 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo(); 257 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); 258 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter( 259 *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx); 260 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 261 TargetCPU); 262 if (!MCE || !MAB) 263 return true; 264 265 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 266 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, 267 Options.MCOptions.MCRelaxAll)); 268 269 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 270 FunctionPass *Printer = 271 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 272 if (!Printer) 273 return true; 274 275 PM.add(Printer); 276 277 return false; // success! 278 } 279