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 static cl::opt<cl::boolOrDefault>
47 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
48 cl::init(cl::BOU_UNSET));
50 static bool getVerboseAsm() {
52 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
53 case cl::BOU_TRUE: return true;
54 case cl::BOU_FALSE: return false;
56 llvm_unreachable("Invalid verbose asm state");
59 void LLVMTargetMachine::initAsmInfo() {
60 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
62 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
63 // and if the old one gets included then MCAsmInfo will be NULL and
65 // Provide the user with a useful error message about what's wrong.
66 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
67 "Make sure you include the correct TargetSelect.h"
68 "and that InitializeAllTargetMCs() is being invoked!");
70 if (Options.DisableIntegratedAS)
71 TmpAsmInfo->setUseIntegratedAssembler(false);
73 if (Options.CompressDebugSections)
74 TmpAsmInfo->setCompressDebugSections(true);
79 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
80 StringRef CPU, StringRef FS,
81 TargetOptions Options,
82 Reloc::Model RM, CodeModel::Model CM,
84 : TargetMachine(T, Triple, CPU, FS, Options) {
85 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
88 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
89 PM.add(createBasicTargetTransformInfoPass(this));
92 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
93 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
96 AnalysisID StartAfter,
97 AnalysisID StopAfter) {
98 // Add internal analysis passes from the target machine.
99 TM->addAnalysisPasses(PM);
101 // Targets may override createPassConfig to provide a target-specific sublass.
102 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
103 PassConfig->setStartStopPasses(StartAfter, StopAfter);
105 // Set PassConfig options provided by TargetMachine.
106 PassConfig->setDisableVerify(DisableVerify);
110 PassConfig->addIRPasses();
112 PassConfig->addCodeGenPrepare();
114 PassConfig->addPassesToHandleExceptions();
116 PassConfig->addISelPrepare();
118 // Install a MachineModuleInfo class, which is an immutable pass that holds
119 // all the per-module stuff we're generating, including MCContext.
120 MachineModuleInfo *MMI =
121 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
122 &TM->getTargetLowering()->getObjFileLowering());
125 // Set up a MachineFunction for the rest of CodeGen to work on.
126 PM.add(new MachineFunctionAnalysis(*TM));
128 // Enable FastISel with -fast, but allow that to be overridden.
129 if (EnableFastISelOption == cl::BOU_TRUE ||
130 (TM->getOptLevel() == CodeGenOpt::None &&
131 EnableFastISelOption != cl::BOU_FALSE))
132 TM->setFastISel(true);
134 // Ask the target for an isel.
135 if (PassConfig->addInstSelector())
138 PassConfig->addMachinePasses();
140 PassConfig->setInitialized();
142 return &MMI->getContext();
145 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
146 formatted_raw_ostream &Out,
147 CodeGenFileType FileType,
149 AnalysisID StartAfter,
150 AnalysisID StopAfter) {
151 // Add common CodeGen passes.
152 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
153 StartAfter, StopAfter);
158 // FIXME: The intent is that this should eventually write out a YAML file,
159 // containing the LLVM IR, the machine-level IR (when stopping after a
160 // machine-level pass), and whatever other information is needed to
161 // deserialize the code and resume compilation. For now, just write the
163 PM.add(createPrintModulePass(Out));
167 if (Options.MCOptions.MCSaveTempLabels)
168 Context->setAllowTemporaryLabels(false);
170 const MCAsmInfo &MAI = *getMCAsmInfo();
171 const MCRegisterInfo &MRI = *getRegisterInfo();
172 const MCInstrInfo &MII = *getInstrInfo();
173 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
174 std::unique_ptr<MCStreamer> AsmStreamer;
177 case CGFT_AssemblyFile: {
178 MCInstPrinter *InstPrinter =
179 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
182 // Create a code emitter if asked to show the encoding.
183 MCCodeEmitter *MCE = nullptr;
184 if (Options.MCOptions.ShowMCEncoding)
185 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
187 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
189 MCStreamer *S = getTarget().createAsmStreamer(
190 *Context, Out, getVerboseAsm(), Options.MCOptions.MCUseDwarfDirectory,
191 InstPrinter, MCE, MAB, Options.MCOptions.ShowMCInst);
192 AsmStreamer.reset(S);
195 case CGFT_ObjectFile: {
196 // Create the code emitter for the target if it exists. If not, .o file
198 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
200 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
205 AsmStreamer.reset(getTarget().createMCObjectStreamer(
206 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
207 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
211 // The Null output is intended for use for performance analysis and testing,
213 AsmStreamer.reset(createNullStreamer(*Context));
217 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
218 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
222 // If successful, createAsmPrinter took ownership of AsmStreamer.
223 AsmStreamer.release();
230 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
231 /// get machine code emitted. This uses a JITCodeEmitter object to handle
232 /// actually outputting the machine code and resolving things like the address
233 /// of functions. This method should return true if machine code emission is
236 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
238 bool DisableVerify) {
239 // Add common CodeGen passes.
240 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
245 addCodeEmitter(PM, JCE);
247 return false; // success!
250 /// addPassesToEmitMC - Add passes to the specified pass manager to get
251 /// machine code emitted with the MCJIT. This method returns true if machine
252 /// code is not supported. It fills the MCContext Ctx pointer which can be
253 /// used to build custom MCStreamer.
255 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
258 bool DisableVerify) {
259 // Add common CodeGen passes.
260 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
264 if (Options.MCOptions.MCSaveTempLabels)
265 Ctx->setAllowTemporaryLabels(false);
267 // Create the code emitter for the target if it exists. If not, .o file
269 const MCRegisterInfo &MRI = *getRegisterInfo();
270 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
271 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
273 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
278 std::unique_ptr<MCStreamer> AsmStreamer;
279 AsmStreamer.reset(getTarget().createMCObjectStreamer(
280 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
281 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
283 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
284 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
288 // If successful, createAsmPrinter took ownership of AsmStreamer.
289 AsmStreamer.release();
293 return false; // success!