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 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); 64 65 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); 66 67 if (Options.ExceptionModel != ExceptionHandling::None) 68 TmpAsmInfo->setExceptionsType(Options.ExceptionModel); 69 70 AsmInfo = TmpAsmInfo; 71 } 72 73 LLVMTargetMachine::LLVMTargetMachine(const Target &T, 74 StringRef DataLayoutString, 75 const Triple &TT, StringRef CPU, 76 StringRef FS, const TargetOptions &Options, 77 Reloc::Model RM, CodeModel::Model CM, 78 CodeGenOpt::Level OL) 79 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { 80 T.adjustCodeGenOpts(TT, RM, CM); 81 this->RM = RM; 82 this->CMModel = CM; 83 this->OptLevel = OL; 84 } 85 86 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { 87 return TargetIRAnalysis([this](const Function &F) { 88 return TargetTransformInfo(BasicTTIImpl(this, F)); 89 }); 90 } 91 92 /// addPassesToX helper drives creation and initialization of TargetPassConfig. 93 static MCContext * 94 addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, 95 bool DisableVerify, AnalysisID StartBefore, 96 AnalysisID StartAfter, AnalysisID StopBefore, 97 AnalysisID StopAfter) { 98 // Targets may override createPassConfig to provide a target-specific 99 // subclass. 100 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 101 PassConfig->setStartStopPasses(StartBefore, StartAfter, StopBefore, 102 StopAfter); 103 // Set PassConfig options provided by TargetMachine. 104 PassConfig->setDisableVerify(DisableVerify); 105 PM.add(PassConfig); 106 MachineModuleInfo *MMI = new MachineModuleInfo(TM); 107 PM.add(MMI); 108 109 if (PassConfig->addISelPasses()) 110 return nullptr; 111 PassConfig->addMachinePasses(); 112 PassConfig->setInitialized(); 113 114 return &MMI->getContext(); 115 } 116 117 bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 118 raw_pwrite_stream &Out, CodeGenFileType FileType, 119 MCContext &Context) { 120 if (Options.MCOptions.MCSaveTempLabels) 121 Context.setAllowTemporaryLabels(false); 122 123 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 124 const MCAsmInfo &MAI = *getMCAsmInfo(); 125 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 126 const MCInstrInfo &MII = *getMCInstrInfo(); 127 128 std::unique_ptr<MCStreamer> AsmStreamer; 129 130 switch (FileType) { 131 case CGFT_AssemblyFile: { 132 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 133 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 134 135 // Create a code emitter if asked to show the encoding. 136 MCCodeEmitter *MCE = nullptr; 137 if (Options.MCOptions.ShowMCEncoding) 138 MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 139 140 MCAsmBackend *MAB = 141 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 142 Options.MCOptions); 143 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); 144 MCStreamer *S = getTarget().createAsmStreamer( 145 Context, std::move(FOut), Options.MCOptions.AsmVerbose, 146 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 147 Options.MCOptions.ShowMCInst); 148 AsmStreamer.reset(S); 149 break; 150 } 151 case CGFT_ObjectFile: { 152 // Create the code emitter for the target if it exists. If not, .o file 153 // emission fails. 154 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 155 MCAsmBackend *MAB = 156 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 157 Options.MCOptions); 158 if (!MCE || !MAB) 159 return true; 160 161 // Don't waste memory on names of temp labels. 162 Context.setUseNamesOnTempLabels(false); 163 164 Triple T(getTargetTriple().str()); 165 AsmStreamer.reset(getTarget().createMCObjectStreamer( 166 T, Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll, 167 Options.MCOptions.MCIncrementalLinkerCompatible, 168 /*DWARFMustBeAtTheEnd*/ true)); 169 break; 170 } 171 case CGFT_Null: 172 // The Null output is intended for use for performance analysis and testing, 173 // not real users. 174 AsmStreamer.reset(getTarget().createNullStreamer(Context)); 175 break; 176 } 177 178 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 179 FunctionPass *Printer = 180 getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 181 if (!Printer) 182 return true; 183 184 PM.add(Printer); 185 return false; 186 } 187 188 bool LLVMTargetMachine::addPassesToEmitFile( 189 PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType, 190 bool DisableVerify, AnalysisID StartBefore, AnalysisID StartAfter, 191 AnalysisID StopBefore, AnalysisID StopAfter) { 192 // Add common CodeGen passes. 193 MCContext *Context = 194 addPassesToGenerateCode(this, PM, DisableVerify, StartBefore, StartAfter, 195 StopBefore, StopAfter); 196 if (!Context) 197 return true; 198 199 if (StopBefore || StopAfter) { 200 PM.add(createPrintMIRPass(Out)); 201 } else { 202 if (addAsmPrinter(PM, Out, FileType, *Context)) 203 return true; 204 } 205 206 PM.add(createFreeMachineFunctionPass()); 207 return false; 208 } 209 210 /// addPassesToEmitMC - Add passes to the specified pass manager to get 211 /// machine code emitted with the MCJIT. This method returns true if machine 212 /// code is not supported. It fills the MCContext Ctx pointer which can be 213 /// used to build custom MCStreamer. 214 /// 215 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 216 raw_pwrite_stream &Out, 217 bool DisableVerify) { 218 // Add common CodeGen passes. 219 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr, 220 nullptr, nullptr); 221 if (!Ctx) 222 return true; 223 224 if (Options.MCOptions.MCSaveTempLabels) 225 Ctx->setAllowTemporaryLabels(false); 226 227 // Create the code emitter for the target if it exists. If not, .o file 228 // emission fails. 229 const MCRegisterInfo &MRI = *getMCRegisterInfo(); 230 MCCodeEmitter *MCE = 231 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 232 MCAsmBackend *MAB = 233 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 234 Options.MCOptions); 235 if (!MCE || !MAB) 236 return true; 237 238 const Triple &T = getTargetTriple(); 239 const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 240 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 241 T, *Ctx, *MAB, Out, 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