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