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/ADT/OwningPtr.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/IRPrintingPasses.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/PassManager.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Target/TargetLowering.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/Target/TargetRegisterInfo.h"
36 #include "llvm/Target/TargetSubtargetInfo.h"
37 #include "llvm/Transforms/Scalar.h"
40 // Enable or disable FastISel. Both options are needed, because
41 // FastISel is enabled by default with -fast, and we wish to be
42 // able to enable or disable fast-isel independently from -O0.
43 static cl::opt<cl::boolOrDefault>
44 EnableFastISelOption("fast-isel", cl::Hidden,
45 cl::desc("Enable the \"fast\" instruction selector"));
47 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
48 cl::desc("Show encoding in .s output"));
49 static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
50 cl::desc("Show instruction structure in .s output"));
52 static cl::opt<cl::boolOrDefault>
53 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
54 cl::init(cl::BOU_UNSET));
56 static bool getVerboseAsm() {
58 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
59 case cl::BOU_TRUE: return true;
60 case cl::BOU_FALSE: return false;
62 llvm_unreachable("Invalid verbose asm state");
65 void LLVMTargetMachine::initAsmInfo() {
66 AsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(), TargetTriple);
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(AsmInfo && "MCAsmInfo not initialized. "
72 "Make sure you include the correct TargetSelect.h"
73 "and that InitializeAllTargetMCs() is being invoked!");
76 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
77 StringRef CPU, StringRef FS,
78 TargetOptions Options,
79 Reloc::Model RM, CodeModel::Model CM,
81 : TargetMachine(T, Triple, CPU, FS, Options) {
82 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
85 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
86 PM.add(createBasicTargetTransformInfoPass(this));
89 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
90 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
93 AnalysisID StartAfter,
94 AnalysisID StopAfter) {
95 // Targets may override createPassConfig to provide a target-specific sublass.
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 =
115 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
116 &TM->getTargetLowering()->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 (hasMCSaveTempLabels())
162 Context->setAllowTemporaryLabels(false);
164 const MCAsmInfo &MAI = *getMCAsmInfo();
165 const MCRegisterInfo &MRI = *getRegisterInfo();
166 const MCInstrInfo &MII = *getInstrInfo();
167 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
168 OwningPtr<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 = 0;
179 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
181 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
183 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
187 hasMCUseDwarfDirectory(),
191 AsmStreamer.reset(S);
194 case CGFT_ObjectFile: {
195 // Create the code emitter for the target if it exists. If not, .o file
197 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
199 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
201 if (MCE == 0 || MAB == 0)
204 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
206 MCE, hasMCRelaxAll(),
207 hasMCNoExecStack()));
208 AsmStreamer.get()->setAutoInitSections(true);
212 // The Null output is intended for use for performance analysis and testing,
214 AsmStreamer.reset(createNullStreamer(*Context));
218 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
219 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
223 // If successful, createAsmPrinter took ownership of AsmStreamer.
231 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
232 /// get machine code emitted. This uses a JITCodeEmitter object to handle
233 /// actually outputting the machine code and resolving things like the address
234 /// of functions. This method should return true if machine code emission is
237 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
239 bool DisableVerify) {
240 // Add common CodeGen passes.
241 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
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, 0, 0);
264 if (hasMCSaveTempLabels())
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(),
275 if (MCE == 0 || MAB == 0)
278 OwningPtr<MCStreamer> AsmStreamer;
279 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
282 hasMCNoExecStack()));
283 AsmStreamer.get()->InitSections();
285 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
286 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
290 // If successful, createAsmPrinter took ownership of AsmStreamer.
295 return false; // success!