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 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
51 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
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
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!");
60 if (Options.DisableIntegratedAS)
61 TmpAsmInfo->setUseIntegratedAssembler(false);
63 if (Options.CompressDebugSections)
64 TmpAsmInfo->setCompressDebugSections(true);
69 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
70 StringRef CPU, StringRef FS,
71 TargetOptions Options,
72 Reloc::Model RM, CodeModel::Model CM,
74 : TargetMachine(T, Triple, CPU, FS, Options) {
75 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
78 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
79 return TargetIRAnalysis([this](Function &F) {
80 return TargetTransformInfo(BasicTTIImpl(this, F));
84 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
85 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
88 AnalysisID StartAfter,
89 AnalysisID StopAfter) {
91 // Add internal analysis passes from the target machine.
92 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
94 // Targets may override createPassConfig to provide a target-specific
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
97 PassConfig->setStartStopPasses(StartAfter, StopAfter);
99 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify);
104 PassConfig->addIRPasses();
106 PassConfig->addCodeGenPrepare();
108 PassConfig->addPassesToHandleExceptions();
110 PassConfig->addISelPrepare();
112 // Install a MachineModuleInfo class, which is an immutable pass that holds
113 // all the per-module stuff we're generating, including MCContext.
114 MachineModuleInfo *MMI = new MachineModuleInfo(
115 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
116 TM->getObjFileLowering());
119 // Set up a MachineFunction for the rest of CodeGen to work on.
120 PM.add(new MachineFunctionAnalysis(*TM));
122 // Enable FastISel with -fast, but allow that to be overridden.
123 if (EnableFastISelOption == cl::BOU_TRUE ||
124 (TM->getOptLevel() == CodeGenOpt::None &&
125 EnableFastISelOption != cl::BOU_FALSE))
126 TM->setFastISel(true);
128 // Ask the target for an isel.
129 if (PassConfig->addInstSelector())
132 PassConfig->addMachinePasses();
134 PassConfig->setInitialized();
136 return &MMI->getContext();
139 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
140 formatted_raw_ostream &Out,
141 CodeGenFileType FileType,
143 AnalysisID StartAfter,
144 AnalysisID StopAfter) {
145 // Add common CodeGen passes.
146 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
147 StartAfter, StopAfter);
152 // FIXME: The intent is that this should eventually write out a YAML file,
153 // containing the LLVM IR, the machine-level IR (when stopping after a
154 // machine-level pass), and whatever other information is needed to
155 // deserialize the code and resume compilation. For now, just write the
157 PM.add(createPrintModulePass(Out));
161 if (Options.MCOptions.MCSaveTempLabels)
162 Context->setAllowTemporaryLabels(false);
164 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
165 const MCAsmInfo &MAI = *getMCAsmInfo();
166 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
167 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
168 std::unique_ptr<MCStreamer> AsmStreamer;
171 case CGFT_AssemblyFile: {
172 MCInstPrinter *InstPrinter =
173 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
176 // Create a code emitter if asked to show the encoding.
177 MCCodeEmitter *MCE = nullptr;
178 if (Options.MCOptions.ShowMCEncoding)
179 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
181 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
183 MCStreamer *S = getTarget().createAsmStreamer(
184 *Context, Out, Options.MCOptions.AsmVerbose,
185 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
186 Options.MCOptions.ShowMCInst);
187 AsmStreamer.reset(S);
190 case CGFT_ObjectFile: {
191 // Create the code emitter for the target if it exists. If not, .o file
193 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
194 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
201 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
202 STI, Options.MCOptions.MCRelaxAll));
206 // The Null output is intended for use for performance analysis and testing,
208 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
212 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
213 FunctionPass *Printer =
214 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
223 /// addPassesToEmitMC - Add passes to the specified pass manager to get
224 /// machine code emitted with the MCJIT. This method returns true if machine
225 /// code is not supported. It fills the MCContext Ctx pointer which can be
226 /// used to build custom MCStreamer.
228 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
231 bool DisableVerify) {
232 // Add common CodeGen passes.
233 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
237 if (Options.MCOptions.MCSaveTempLabels)
238 Ctx->setAllowTemporaryLabels(false);
240 // Create the code emitter for the target if it exists. If not, .o file
242 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
243 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
244 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
245 *getSubtargetImpl()->getInstrInfo(), MRI, *Ctx);
246 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
251 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
252 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
253 Options.MCOptions.MCRelaxAll));
255 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
256 FunctionPass *Printer =
257 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
263 return false; // success!