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/Target/TargetMachine.h"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/BasicTTIImpl.h"
18 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/IR/IRPrintingPasses.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/MC/MCAsmInfo.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/TargetInstrInfo.h"
34 #include "llvm/Target/TargetLowering.h"
35 #include "llvm/Target/TargetLoweringObjectFile.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/Target/TargetRegisterInfo.h"
38 #include "llvm/Target/TargetSubtargetInfo.h"
39 #include "llvm/Transforms/Scalar.h"
42 // Enable or disable FastISel. Both options are needed, because
43 // FastISel is enabled by default with -fast, and we wish to be
44 // able to enable or disable fast-isel independently from -O0.
45 static cl::opt<cl::boolOrDefault>
46 EnableFastISelOption("fast-isel", cl::Hidden,
47 cl::desc("Enable the \"fast\" instruction selector"));
49 void LLVMTargetMachine::initAsmInfo() {
50 MRI = TheTarget.createMCRegInfo(getTargetTriple());
51 MII = TheTarget.createMCInstrInfo();
53 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*MRI, getTargetTriple());
54 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
55 // and if the old one gets included then MCAsmInfo will be NULL and
57 // Provide the user with a useful error message about what's wrong.
58 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
59 "Make sure you include the correct TargetSelect.h"
60 "and that InitializeAllTargetMCs() is being invoked!");
62 if (Options.DisableIntegratedAS)
63 TmpAsmInfo->setUseIntegratedAssembler(false);
65 if (Options.CompressDebugSections)
66 TmpAsmInfo->setCompressDebugSections(true);
71 LLVMTargetMachine::LLVMTargetMachine(const Target &T,
72 StringRef DataLayoutString,
73 StringRef Triple, StringRef CPU,
74 StringRef FS, TargetOptions Options,
75 Reloc::Model RM, CodeModel::Model CM,
77 : TargetMachine(T, DataLayoutString, Triple, CPU, FS, Options) {
78 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
81 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
82 return TargetIRAnalysis([this](Function &F) {
83 return TargetTransformInfo(BasicTTIImpl(this, F));
87 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
88 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
91 AnalysisID StartAfter,
92 AnalysisID StopAfter) {
94 // Add internal analysis passes from the target machine.
95 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
97 // Targets may override createPassConfig to provide a target-specific
99 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
100 PassConfig->setStartStopPasses(StartAfter, StopAfter);
102 // Set PassConfig options provided by TargetMachine.
103 PassConfig->setDisableVerify(DisableVerify);
107 PassConfig->addIRPasses();
109 PassConfig->addCodeGenPrepare();
111 PassConfig->addPassesToHandleExceptions();
113 PassConfig->addISelPrepare();
115 // Install a MachineModuleInfo class, which is an immutable pass that holds
116 // all the per-module stuff we're generating, including MCContext.
117 MachineModuleInfo *MMI = new MachineModuleInfo(
118 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
119 TM->getObjFileLowering());
122 // Set up a MachineFunction for the rest of CodeGen to work on.
123 PM.add(new MachineFunctionAnalysis(*TM));
125 // Enable FastISel with -fast, but allow that to be overridden.
126 if (EnableFastISelOption == cl::BOU_TRUE ||
127 (TM->getOptLevel() == CodeGenOpt::None &&
128 EnableFastISelOption != cl::BOU_FALSE))
129 TM->setFastISel(true);
131 // Ask the target for an isel.
132 if (PassConfig->addInstSelector())
135 PassConfig->addMachinePasses();
137 PassConfig->setInitialized();
139 return &MMI->getContext();
142 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
143 formatted_raw_ostream &Out,
144 CodeGenFileType FileType,
146 AnalysisID StartAfter,
147 AnalysisID StopAfter) {
148 // Add common CodeGen passes.
149 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
150 StartAfter, StopAfter);
155 // FIXME: The intent is that this should eventually write out a YAML file,
156 // containing the LLVM IR, the machine-level IR (when stopping after a
157 // machine-level pass), and whatever other information is needed to
158 // deserialize the code and resume compilation. For now, just write the
160 PM.add(createPrintModulePass(Out));
164 if (Options.MCOptions.MCSaveTempLabels)
165 Context->setAllowTemporaryLabels(false);
167 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
168 const MCAsmInfo &MAI = *getMCAsmInfo();
169 const MCRegisterInfo &MRI = *getMCRegisterInfo();
170 const MCInstrInfo &MII = *getMCInstrInfo();
172 std::unique_ptr<MCStreamer> AsmStreamer;
175 case CGFT_AssemblyFile: {
176 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
177 MAI.getAssemblerDialect(), MAI, MII, MRI, STI);
179 // Create a code emitter if asked to show the encoding.
180 MCCodeEmitter *MCE = nullptr;
181 if (Options.MCOptions.ShowMCEncoding)
182 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
184 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
186 MCStreamer *S = getTarget().createAsmStreamer(
187 *Context, Out, Options.MCOptions.AsmVerbose,
188 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
189 Options.MCOptions.ShowMCInst);
190 AsmStreamer.reset(S);
193 case CGFT_ObjectFile: {
194 // Create the code emitter for the target if it exists. If not, .o file
196 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
197 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
202 Triple T(getTargetTriple());
203 AsmStreamer.reset(getTarget().createMCObjectStreamer(
204 T, *Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll));
208 // The Null output is intended for use for performance analysis and testing,
210 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
214 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
215 FunctionPass *Printer =
216 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
225 /// addPassesToEmitMC - Add passes to the specified pass manager to get
226 /// machine code emitted with the MCJIT. This method returns true if machine
227 /// code is not supported. It fills the MCContext Ctx pointer which can be
228 /// used to build custom MCStreamer.
230 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
233 bool DisableVerify) {
234 // Add common CodeGen passes.
235 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
239 if (Options.MCOptions.MCSaveTempLabels)
240 Ctx->setAllowTemporaryLabels(false);
242 // Create the code emitter for the target if it exists. If not, .o file
244 const MCRegisterInfo &MRI = *getMCRegisterInfo();
245 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
246 *getSubtargetImpl()->getInstrInfo(), MRI, *Ctx);
247 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
252 Triple T(getTargetTriple());
253 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
254 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
255 T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll));
257 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
258 FunctionPass *Printer =
259 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
265 return false; // success!