1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVMTargetMachine class.
12 //===----------------------------------------------------------------------===//
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"
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());
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
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!");
57 if (Options.DisableIntegratedAS)
58 TmpAsmInfo->setUseIntegratedAssembler(false);
60 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
62 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
64 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
66 if (Options.ExceptionModel != ExceptionHandling::None)
67 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
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,
78 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
85 LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
86 return TargetTransformInfo(BasicTTIImpl(this, F));
89 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
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
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
97 // Set PassConfig options provided by TargetMachine.
98 PassConfig->setDisableVerify(DisableVerify);
99 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
102 MMI = new MachineModuleInfo(TM);
105 if (PassConfig->addISelPasses())
107 PassConfig->addMachinePasses();
108 PassConfig->setInitialized();
109 if (!WillCompleteCodeGenPipeline)
110 PM.add(createPrintMIRPass(Out));
112 return &MMI->getContext();
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);
121 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
122 const MCAsmInfo &MAI = *getMCAsmInfo();
123 const MCRegisterInfo &MRI = *getMCRegisterInfo();
124 const MCInstrInfo &MII = *getMCInstrInfo();
126 std::unique_ptr<MCStreamer> AsmStreamer;
129 case CGFT_AssemblyFile: {
130 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
131 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
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);
139 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
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);
149 case CGFT_ObjectFile: {
150 // Create the code emitter for the target if it exists. If not, .o file
152 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
154 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
159 // Don't waste memory on names of temp labels.
160 Context.setUseNamesOnTempLabels(false);
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));
171 // The Null output is intended for use for performance analysis and testing,
173 AsmStreamer.reset(getTarget().createNullStreamer(Context));
177 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
178 FunctionPass *Printer =
179 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
187 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
188 raw_pwrite_stream &Out,
189 CodeGenFileType FileType,
191 MachineModuleInfo *MMI) {
192 // Add common CodeGen passes.
193 bool WillCompleteCodeGenPipeline = true;
194 MCContext *Context = addPassesToGenerateCode(
195 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
199 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
202 PM.add(createFreeMachineFunctionPass());
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.
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);
221 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
223 if (Options.MCOptions.MCSaveTempLabels)
224 Ctx->setAllowTemporaryLabels(false);
226 // Create the code emitter for the target if it exists. If not, .o file
228 const MCRegisterInfo &MRI = *getMCRegisterInfo();
230 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
232 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
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));
245 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
246 FunctionPass *Printer =
247 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
252 PM.add(createFreeMachineFunctionPass());
254 return false; // success!