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<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
47 cl::desc("Show encoding in .s output"));
48 static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
49 cl::desc("Show instruction structure in .s output"));
51 static cl::opt<cl::boolOrDefault>
52 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
53 cl::init(cl::BOU_UNSET));
55 static bool getVerboseAsm() {
57 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
58 case cl::BOU_TRUE: return true;
59 case cl::BOU_FALSE: return false;
61 llvm_unreachable("Invalid verbose asm state");
64 void LLVMTargetMachine::initAsmInfo() {
65 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
67 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
68 // and if the old one gets included then MCAsmInfo will be NULL and
70 // Provide the user with a useful error message about what's wrong.
71 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
72 "Make sure you include the correct TargetSelect.h"
73 "and that InitializeAllTargetMCs() is being invoked!");
75 if (Options.DisableIntegratedAS)
76 TmpAsmInfo->setUseIntegratedAssembler(false);
78 if (Options.CompressDebugSections)
79 TmpAsmInfo->setCompressDebugSections(true);
84 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
85 StringRef CPU, StringRef FS,
86 TargetOptions Options,
87 Reloc::Model RM, CodeModel::Model CM,
89 : TargetMachine(T, Triple, CPU, FS, Options) {
90 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
93 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
94 PM.add(createBasicTargetTransformInfoPass(this));
97 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
98 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
101 AnalysisID StartAfter,
102 AnalysisID StopAfter) {
103 // Add internal analysis passes from the target machine.
104 TM->addAnalysisPasses(PM);
106 // Targets may override createPassConfig to provide a target-specific sublass.
107 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
108 PassConfig->setStartStopPasses(StartAfter, StopAfter);
110 // Set PassConfig options provided by TargetMachine.
111 PassConfig->setDisableVerify(DisableVerify);
115 PassConfig->addIRPasses();
117 PassConfig->addCodeGenPrepare();
119 PassConfig->addPassesToHandleExceptions();
121 PassConfig->addISelPrepare();
123 // Install a MachineModuleInfo class, which is an immutable pass that holds
124 // all the per-module stuff we're generating, including MCContext.
125 MachineModuleInfo *MMI =
126 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
127 &TM->getTargetLowering()->getObjFileLowering());
130 // Set up a MachineFunction for the rest of CodeGen to work on.
131 PM.add(new MachineFunctionAnalysis(*TM));
133 // Enable FastISel with -fast, but allow that to be overridden.
134 if (EnableFastISelOption == cl::BOU_TRUE ||
135 (TM->getOptLevel() == CodeGenOpt::None &&
136 EnableFastISelOption != cl::BOU_FALSE))
137 TM->setFastISel(true);
139 // Ask the target for an isel.
140 if (PassConfig->addInstSelector())
143 PassConfig->addMachinePasses();
145 PassConfig->setInitialized();
147 return &MMI->getContext();
150 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
151 formatted_raw_ostream &Out,
152 CodeGenFileType FileType,
154 AnalysisID StartAfter,
155 AnalysisID StopAfter) {
156 // Add common CodeGen passes.
157 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
158 StartAfter, StopAfter);
163 // FIXME: The intent is that this should eventually write out a YAML file,
164 // containing the LLVM IR, the machine-level IR (when stopping after a
165 // machine-level pass), and whatever other information is needed to
166 // deserialize the code and resume compilation. For now, just write the
168 PM.add(createPrintModulePass(Out));
172 if (hasMCSaveTempLabels())
173 Context->setAllowTemporaryLabels(false);
175 const MCAsmInfo &MAI = *getMCAsmInfo();
176 const MCRegisterInfo &MRI = *getRegisterInfo();
177 const MCInstrInfo &MII = *getInstrInfo();
178 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
179 std::unique_ptr<MCStreamer> AsmStreamer;
182 case CGFT_AssemblyFile: {
183 MCInstPrinter *InstPrinter =
184 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
187 // Create a code emitter if asked to show the encoding.
188 MCCodeEmitter *MCE = nullptr;
190 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
192 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
194 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
197 hasMCUseDwarfDirectory(),
201 AsmStreamer.reset(S);
204 case CGFT_ObjectFile: {
205 // Create the code emitter for the target if it exists. If not, .o file
207 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
209 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
214 AsmStreamer.reset(getTarget().createMCObjectStreamer(
215 getTargetTriple(), *Context, *MAB, Out, MCE, STI, hasMCRelaxAll(),
216 hasMCNoExecStack()));
220 // The Null output is intended for use for performance analysis and testing,
222 AsmStreamer.reset(createNullStreamer(*Context));
226 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
227 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
231 // If successful, createAsmPrinter took ownership of AsmStreamer.
232 AsmStreamer.release();
239 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
240 /// get machine code emitted. This uses a JITCodeEmitter object to handle
241 /// actually outputting the machine code and resolving things like the address
242 /// of functions. This method should return true if machine code emission is
245 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
247 bool DisableVerify) {
248 // Add common CodeGen passes.
249 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
254 addCodeEmitter(PM, JCE);
256 return false; // success!
259 /// addPassesToEmitMC - Add passes to the specified pass manager to get
260 /// machine code emitted with the MCJIT. This method returns true if machine
261 /// code is not supported. It fills the MCContext Ctx pointer which can be
262 /// used to build custom MCStreamer.
264 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
267 bool DisableVerify) {
268 // Add common CodeGen passes.
269 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
273 if (hasMCSaveTempLabels())
274 Ctx->setAllowTemporaryLabels(false);
276 // Create the code emitter for the target if it exists. If not, .o file
278 const MCRegisterInfo &MRI = *getRegisterInfo();
279 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
280 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
282 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
287 std::unique_ptr<MCStreamer> AsmStreamer;
288 AsmStreamer.reset(getTarget().createMCObjectStreamer(
289 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, hasMCRelaxAll(),
290 hasMCNoExecStack()));
292 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
293 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
297 // If successful, createAsmPrinter took ownership of AsmStreamer.
298 AsmStreamer.release();
302 return false; // success!