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/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/IR/IRPrintingPasses.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetLowering.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Target/TargetSubtargetInfo.h"
36 #include "llvm/Transforms/Scalar.h"
39 // Enable or disable FastISel. Both options are needed, because
40 // FastISel is enabled by default with -fast, and we wish to be
41 // able to enable or disable fast-isel independently from -O0.
42 static cl::opt<cl::boolOrDefault>
43 EnableFastISelOption("fast-isel", cl::Hidden,
44 cl::desc("Enable the \"fast\" instruction selector"));
46 void LLVMTargetMachine::initAsmInfo() {
47 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
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 if (Options.CompressDebugSections)
61 TmpAsmInfo->setCompressDebugSections(true);
66 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
67 StringRef CPU, StringRef FS,
68 TargetOptions Options,
69 Reloc::Model RM, CodeModel::Model CM,
71 : TargetMachine(T, Triple, CPU, FS, Options) {
72 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
75 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
76 PM.add(createBasicTargetTransformInfoPass(this));
79 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
80 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
83 AnalysisID StartAfter,
84 AnalysisID StopAfter) {
85 // Add internal analysis passes from the target machine.
86 TM->addAnalysisPasses(PM);
88 // Targets may override createPassConfig to provide a target-specific
90 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
91 PassConfig->setStartStopPasses(StartAfter, StopAfter);
93 // Set PassConfig options provided by TargetMachine.
94 PassConfig->setDisableVerify(DisableVerify);
98 PassConfig->addIRPasses();
100 PassConfig->addCodeGenPrepare();
102 PassConfig->addPassesToHandleExceptions();
104 PassConfig->addISelPrepare();
106 // Install a MachineModuleInfo class, which is an immutable pass that holds
107 // all the per-module stuff we're generating, including MCContext.
108 MachineModuleInfo *MMI =
109 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
110 &TM->getTargetLowering()->getObjFileLowering());
113 // Set up a MachineFunction for the rest of CodeGen to work on.
114 PM.add(new MachineFunctionAnalysis(*TM));
116 // Enable FastISel with -fast, but allow that to be overridden.
117 if (EnableFastISelOption == cl::BOU_TRUE ||
118 (TM->getOptLevel() == CodeGenOpt::None &&
119 EnableFastISelOption != cl::BOU_FALSE))
120 TM->setFastISel(true);
122 // Ask the target for an isel.
123 if (PassConfig->addInstSelector())
126 PassConfig->addMachinePasses();
128 PassConfig->setInitialized();
130 return &MMI->getContext();
133 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
134 formatted_raw_ostream &Out,
135 CodeGenFileType FileType,
137 AnalysisID StartAfter,
138 AnalysisID StopAfter) {
139 // Add common CodeGen passes.
140 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
141 StartAfter, StopAfter);
146 // FIXME: The intent is that this should eventually write out a YAML file,
147 // containing the LLVM IR, the machine-level IR (when stopping after a
148 // machine-level pass), and whatever other information is needed to
149 // deserialize the code and resume compilation. For now, just write the
151 PM.add(createPrintModulePass(Out));
155 if (Options.MCOptions.MCSaveTempLabels)
156 Context->setAllowTemporaryLabels(false);
158 const MCAsmInfo &MAI = *getMCAsmInfo();
159 const MCRegisterInfo &MRI = *getRegisterInfo();
160 const MCInstrInfo &MII = *getInstrInfo();
161 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
162 std::unique_ptr<MCStreamer> AsmStreamer;
165 case CGFT_AssemblyFile: {
166 MCInstPrinter *InstPrinter =
167 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
170 // Create a code emitter if asked to show the encoding.
171 MCCodeEmitter *MCE = nullptr;
172 if (Options.MCOptions.ShowMCEncoding)
173 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
175 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
177 MCStreamer *S = getTarget().createAsmStreamer(
178 *Context, Out, Options.MCOptions.AsmVerbose,
179 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
180 Options.MCOptions.ShowMCInst);
181 AsmStreamer.reset(S);
184 case CGFT_ObjectFile: {
185 // Create the code emitter for the target if it exists. If not, .o file
187 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
189 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
194 AsmStreamer.reset(getTarget().createMCObjectStreamer(
195 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
196 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
200 // The Null output is intended for use for performance analysis and testing,
202 AsmStreamer.reset(createNullStreamer(*Context));
206 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
207 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
211 // If successful, createAsmPrinter took ownership of AsmStreamer.
212 AsmStreamer.release();
219 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
220 /// get machine code emitted. This uses a JITCodeEmitter object to handle
221 /// actually outputting the machine code and resolving things like the address
222 /// of functions. This method should return true if machine code emission is
225 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
227 bool DisableVerify) {
228 // Add common CodeGen passes.
229 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
234 addCodeEmitter(PM, JCE);
236 return false; // success!
239 /// addPassesToEmitMC - Add passes to the specified pass manager to get
240 /// machine code emitted with the MCJIT. This method returns true if machine
241 /// code is not supported. It fills the MCContext Ctx pointer which can be
242 /// used to build custom MCStreamer.
244 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
247 bool DisableVerify) {
248 // Add common CodeGen passes.
249 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
253 if (Options.MCOptions.MCSaveTempLabels)
254 Ctx->setAllowTemporaryLabels(false);
256 // Create the code emitter for the target if it exists. If not, .o file
258 const MCRegisterInfo &MRI = *getRegisterInfo();
259 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
260 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
262 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
267 std::unique_ptr<MCStreamer> AsmStreamer;
268 AsmStreamer.reset(getTarget().createMCObjectStreamer(
269 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
270 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
272 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
273 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
277 // If successful, createAsmPrinter took ownership of AsmStreamer.
278 AsmStreamer.release();
282 return false; // success!