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/Analysis/Passes.h" 15 #include "llvm/CodeGen/AsmPrinter.h" 16 #include "llvm/CodeGen/BasicTTIImpl.h" 17 #include "llvm/CodeGen/MachineModuleInfo.h" 18 #include "llvm/CodeGen/Passes.h" 19 #include "llvm/CodeGen/TargetLoweringObjectFile.h" 20 #include "llvm/CodeGen/TargetPassConfig.h" 21 #include "llvm/IR/LegacyPassManager.h" 22 #include "llvm/MC/MCAsmBackend.h" 23 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/MC/MCCodeEmitter.h" 25 #include "llvm/MC/MCContext.h" 26 #include "llvm/MC/MCInstrInfo.h" 27 #include "llvm/MC/MCStreamer.h" 28 #include "llvm/MC/MCSubtargetInfo.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/FormattedStream.h" 32 #include "llvm/Support/TargetRegistry.h" 33 #include "llvm/Target/TargetMachine.h" 34 #include "llvm/Target/TargetOptions.h" 35 using namespace llvm; 36 37 void LLVMTargetMachine::initAsmInfo() { 38 MRI = TheTarget.createMCRegInfo(getTargetTriple().str()); 39 MII = TheTarget.createMCInstrInfo(); 40 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due 41 // to some backends having subtarget feature dependent module level 42 // code generation. This is similar to the hack in the AsmPrinter for 43 // module level assembly etc. 44 STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(), 45 getTargetFeatureString()); 46 47 MCAsmInfo *TmpAsmInfo = 48 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str()); 49 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 50 // and if the old one gets included then MCAsmInfo will be NULL and 51 // we'll crash later. 52 // Provide the user with a useful error message about what's wrong. 53 assert(TmpAsmInfo && "MCAsmInfo not initialized. " 54 "Make sure you include the correct TargetSelect.h" 55 "and that InitializeAllTargetMCs() is being invoked!"); 56 57 if (Options.DisableIntegratedAS) 58 TmpAsmInfo->setUseIntegratedAssembler(false); 59 60 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); 61 62 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); 63 64 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); 65 66 if (Options.ExceptionModel != ExceptionHandling::None) 67 TmpAsmInfo->setExceptionsType(Options.ExceptionModel); 68 69 AsmInfo = TmpAsmInfo; 70 } 71 72 LLVMTargetMachine::LLVMTargetMachine(const Target &T, 73 StringRef DataLayoutString, 74 const Triple &TT, StringRef CPU, 75 StringRef FS, const TargetOptions &Options, 76 Reloc::Model RM, CodeModel::Model CM, 77 CodeGenOpt::Level OL) 78 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { 79 this->RM = RM; 80 this->CMModel = CM; 81 this->OptLevel = OL; 82 } 83 84 TargetTransformInfo 85 LLVMTargetMachine::getTargetTransformInfo(const Function &F) { 86 return TargetTransformInfo(BasicTTIImpl(this, F)); 87 } 88 89 /// addPassesToX helper drives creation and initialization of TargetPassConfig. 90 static MCContext * 91 addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, 92 bool DisableVerify, bool &WillCompleteCodeGenPipeline, 93 raw_pwrite_stream &Out, MachineModuleInfo *MMI) { 94 // Targets may override createPassConfig to provide a target-specific 95 // subclass. 96 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 97 // Set PassConfig options provided by TargetMachine. 98 PassConfig->setDisableVerify(DisableVerify); 99 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline(); 100 PM.add(PassConfig); 101 if (!MMI) 102 MMI = new MachineModuleInfo(TM); 103 PM.add(MMI); 104 105 if (PassConfig->addISelPasses()) 106 return nullptr; 107 PassConfig->addMachinePasses(); 108 PassConfig->setInitialized(); 109 if (!WillCompleteCodeGenPipeline) 110 PM.add(createPrintMIRPass(Out)); 111 112 return &MMI->getContext(); 113 } 114 115 bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 116 raw_pwrite_stream &Out, CodeGenFileType FileType, 117 MCContext &Context) { 118 if (Options.MCOptions.MCSaveTempLabels) 119 Context.setAllowTemporaryLabels(false); 120 121 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 122 const MCAsmInfo &MAI = *getMCAsmInfo(); 123 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 124 const MCInstrInfo &MII = *getMCInstrInfo(); 125 126 std::unique_ptr<MCStreamer> AsmStreamer; 127 128 switch (FileType) { 129 case CGFT_AssemblyFile: { 130 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 131 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 132 133 // Create a code emitter if asked to show the encoding. 134 MCCodeEmitter *MCE = nullptr; 135 if (Options.MCOptions.ShowMCEncoding) 136 MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 137 138 MCAsmBackend *MAB = 139 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 140 Options.MCOptions); 141 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); 142 MCStreamer *S = getTarget().createAsmStreamer( 143 Context, std::move(FOut), Options.MCOptions.AsmVerbose, 144 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 145 Options.MCOptions.ShowMCInst); 146 AsmStreamer.reset(S); 147 break; 148 } 149 case CGFT_ObjectFile: { 150 // Create the code emitter for the target if it exists. If not, .o file 151 // emission fails. 152 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 153 MCAsmBackend *MAB = 154 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 155 Options.MCOptions); 156 if (!MCE || !MAB) 157 return true; 158 159 // Don't waste memory on names of temp labels. 160 Context.setUseNamesOnTempLabels(false); 161 162 Triple T(getTargetTriple().str()); 163 AsmStreamer.reset(getTarget().createMCObjectStreamer( 164 T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out, 165 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 166 Options.MCOptions.MCIncrementalLinkerCompatible, 167 /*DWARFMustBeAtTheEnd*/ true)); 168 break; 169 } 170 case CGFT_Null: 171 // The Null output is intended for use for performance analysis and testing, 172 // not real users. 173 AsmStreamer.reset(getTarget().createNullStreamer(Context)); 174 break; 175 } 176 177 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 178 FunctionPass *Printer = 179 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 180 if (!Printer) 181 return true; 182 183 PM.add(Printer); 184 return false; 185 } 186 187 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 188 raw_pwrite_stream &Out, 189 CodeGenFileType FileType, 190 bool DisableVerify, 191 MachineModuleInfo *MMI) { 192 // Add common CodeGen passes. 193 bool WillCompleteCodeGenPipeline = true; 194 MCContext *Context = addPassesToGenerateCode( 195 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); 196 if (!Context) 197 return true; 198 199 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context)) 200 return true; 201 202 PM.add(createFreeMachineFunctionPass()); 203 return false; 204 } 205 206 /// addPassesToEmitMC - Add passes to the specified pass manager to get 207 /// machine code emitted with the MCJIT. This method returns true if machine 208 /// code is not supported. It fills the MCContext Ctx pointer which can be 209 /// used to build custom MCStreamer. 210 /// 211 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 212 raw_pwrite_stream &Out, 213 bool DisableVerify) { 214 // Add common CodeGen passes. 215 bool WillCompleteCodeGenPipeline = true; 216 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 217 WillCompleteCodeGenPipeline, Out, 218 /*MachineModuleInfo*/ nullptr); 219 if (!Ctx) 220 return true; 221 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); 222 223 if (Options.MCOptions.MCSaveTempLabels) 224 Ctx->setAllowTemporaryLabels(false); 225 226 // Create the code emitter for the target if it exists. If not, .o file 227 // emission fails. 228 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 229 MCCodeEmitter *MCE = 230 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 231 MCAsmBackend *MAB = 232 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 233 Options.MCOptions); 234 if (!MCE || !MAB) 235 return true; 236 237 const Triple &T = getTargetTriple(); 238 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 239 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 240 T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out, 241 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 242 Options.MCOptions.MCIncrementalLinkerCompatible, 243 /*DWARFMustBeAtTheEnd*/ true)); 244 245 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 246 FunctionPass *Printer = 247 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 248 if (!Printer) 249 return true; 250 251 PM.add(Printer); 252 PM.add(createFreeMachineFunctionPass()); 253 254 return false; // success! 255 } 256