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