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,
70 StringRef DataLayoutString,
71 StringRef Triple, StringRef CPU,
72 StringRef FS, TargetOptions Options,
73 Reloc::Model RM, CodeModel::Model CM,
75 : TargetMachine(T, DataLayoutString, Triple, CPU, FS, Options) {
76 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
79 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
80 return TargetIRAnalysis([this](Function &F) {
81 return TargetTransformInfo(BasicTTIImpl(this, F));
85 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
86 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
89 AnalysisID StartAfter,
90 AnalysisID StopAfter) {
92 // Add internal analysis passes from the target machine.
93 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
95 // Targets may override createPassConfig to provide a target-specific
97 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
98 PassConfig->setStartStopPasses(StartAfter, StopAfter);
100 // Set PassConfig options provided by TargetMachine.
101 PassConfig->setDisableVerify(DisableVerify);
105 PassConfig->addIRPasses();
107 PassConfig->addCodeGenPrepare();
109 PassConfig->addPassesToHandleExceptions();
111 PassConfig->addISelPrepare();
113 // Install a MachineModuleInfo class, which is an immutable pass that holds
114 // all the per-module stuff we're generating, including MCContext.
115 MachineModuleInfo *MMI = new MachineModuleInfo(
116 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
117 TM->getObjFileLowering());
120 // Set up a MachineFunction for the rest of CodeGen to work on.
121 PM.add(new MachineFunctionAnalysis(*TM));
123 // Enable FastISel with -fast, but allow that to be overridden.
124 if (EnableFastISelOption == cl::BOU_TRUE ||
125 (TM->getOptLevel() == CodeGenOpt::None &&
126 EnableFastISelOption != cl::BOU_FALSE))
127 TM->setFastISel(true);
129 // Ask the target for an isel.
130 if (PassConfig->addInstSelector())
133 PassConfig->addMachinePasses();
135 PassConfig->setInitialized();
137 return &MMI->getContext();
140 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
141 formatted_raw_ostream &Out,
142 CodeGenFileType FileType,
144 AnalysisID StartAfter,
145 AnalysisID StopAfter) {
146 // Add common CodeGen passes.
147 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
148 StartAfter, StopAfter);
153 // FIXME: The intent is that this should eventually write out a YAML file,
154 // containing the LLVM IR, the machine-level IR (when stopping after a
155 // machine-level pass), and whatever other information is needed to
156 // deserialize the code and resume compilation. For now, just write the
158 PM.add(createPrintModulePass(Out));
162 if (Options.MCOptions.MCSaveTempLabels)
163 Context->setAllowTemporaryLabels(false);
165 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
166 const MCAsmInfo &MAI = *getMCAsmInfo();
167 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
168 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
169 std::unique_ptr<MCStreamer> AsmStreamer;
172 case CGFT_AssemblyFile: {
173 MCInstPrinter *InstPrinter =
174 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
177 // Create a code emitter if asked to show the encoding.
178 MCCodeEmitter *MCE = nullptr;
179 if (Options.MCOptions.ShowMCEncoding)
180 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
182 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
184 MCStreamer *S = getTarget().createAsmStreamer(
185 *Context, Out, Options.MCOptions.AsmVerbose,
186 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
187 Options.MCOptions.ShowMCInst);
188 AsmStreamer.reset(S);
191 case CGFT_ObjectFile: {
192 // Create the code emitter for the target if it exists. If not, .o file
194 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
195 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
202 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
203 STI, Options.MCOptions.MCRelaxAll));
207 // The Null output is intended for use for performance analysis and testing,
209 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
213 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
214 FunctionPass *Printer =
215 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
224 /// addPassesToEmitMC - Add passes to the specified pass manager to get
225 /// machine code emitted with the MCJIT. This method returns true if machine
226 /// code is not supported. It fills the MCContext Ctx pointer which can be
227 /// used to build custom MCStreamer.
229 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
232 bool DisableVerify) {
233 // Add common CodeGen passes.
234 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
238 if (Options.MCOptions.MCSaveTempLabels)
239 Ctx->setAllowTemporaryLabels(false);
241 // Create the code emitter for the target if it exists. If not, .o file
243 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
244 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
245 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
246 *getSubtargetImpl()->getInstrInfo(), MRI, *Ctx);
247 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
252 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
253 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
254 Options.MCOptions.MCRelaxAll));
256 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
257 FunctionPass *Printer =
258 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
264 return false; // success!