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 this->RM = RM; 81 this->CMModel = CM; 82 this->OptLevel = OL; 83 } 84 85 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { 86 return TargetIRAnalysis([this](const Function &F) { 87 return TargetTransformInfo(BasicTTIImpl(this, F)); 88 }); 89 } 90 91 /// addPassesToX helper drives creation and initialization of TargetPassConfig. 92 static MCContext * 93 addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, 94 bool DisableVerify, bool &WillCompleteCodeGenPipeline, 95 raw_pwrite_stream &Out, MachineModuleInfo *MMI) { 96 // Targets may override createPassConfig to provide a target-specific 97 // subclass. 98 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 99 // Set PassConfig options provided by TargetMachine. 100 PassConfig->setDisableVerify(DisableVerify); 101 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline(); 102 PM.add(PassConfig); 103 if (!MMI) 104 MMI = new MachineModuleInfo(TM); 105 PM.add(MMI); 106 107 if (PassConfig->addISelPasses()) 108 return nullptr; 109 PassConfig->addMachinePasses(); 110 PassConfig->setInitialized(); 111 if (!WillCompleteCodeGenPipeline) 112 PM.add(createPrintMIRPass(Out)); 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(PassManagerBase &PM, 189 raw_pwrite_stream &Out, 190 CodeGenFileType FileType, 191 bool DisableVerify, 192 MachineModuleInfo *MMI) { 193 // Add common CodeGen passes. 194 bool WillCompleteCodeGenPipeline = true; 195 MCContext *Context = addPassesToGenerateCode( 196 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); 197 if (!Context) 198 return true; 199 200 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context)) 201 return true; 202 203 PM.add(createFreeMachineFunctionPass()); 204 return false; 205 } 206 207 /// addPassesToEmitMC - Add passes to the specified pass manager to get 208 /// machine code emitted with the MCJIT. This method returns true if machine 209 /// code is not supported. It fills the MCContext Ctx pointer which can be 210 /// used to build custom MCStreamer. 211 /// 212 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 213 raw_pwrite_stream &Out, 214 bool DisableVerify) { 215 // Add common CodeGen passes. 216 bool WillCompleteCodeGenPipeline = true; 217 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 218 WillCompleteCodeGenPipeline, Out, 219 /*MachineModuleInfo*/ nullptr); 220 if (!Ctx) 221 return true; 222 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); 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