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);
81 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
82 StringRef CPU, StringRef FS,
83 TargetOptions Options,
84 Reloc::Model RM, CodeModel::Model CM,
86 : TargetMachine(T, Triple, CPU, FS, Options) {
87 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
90 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
91 PM.add(createBasicTargetTransformInfoPass(this));
94 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
95 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
98 AnalysisID StartAfter,
99 AnalysisID StopAfter) {
100 // Add internal analysis passes from the target machine.
101 TM->addAnalysisPasses(PM);
103 // Targets may override createPassConfig to provide a target-specific sublass.
104 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
105 PassConfig->setStartStopPasses(StartAfter, StopAfter);
107 // Set PassConfig options provided by TargetMachine.
108 PassConfig->setDisableVerify(DisableVerify);
112 PassConfig->addIRPasses();
114 PassConfig->addCodeGenPrepare();
116 PassConfig->addPassesToHandleExceptions();
118 PassConfig->addISelPrepare();
120 // Install a MachineModuleInfo class, which is an immutable pass that holds
121 // all the per-module stuff we're generating, including MCContext.
122 MachineModuleInfo *MMI =
123 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
124 &TM->getTargetLowering()->getObjFileLowering());
127 // Set up a MachineFunction for the rest of CodeGen to work on.
128 PM.add(new MachineFunctionAnalysis(*TM));
130 // Enable FastISel with -fast, but allow that to be overridden.
131 if (EnableFastISelOption == cl::BOU_TRUE ||
132 (TM->getOptLevel() == CodeGenOpt::None &&
133 EnableFastISelOption != cl::BOU_FALSE))
134 TM->setFastISel(true);
136 // Ask the target for an isel.
137 if (PassConfig->addInstSelector())
140 PassConfig->addMachinePasses();
142 PassConfig->setInitialized();
144 return &MMI->getContext();
147 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
148 formatted_raw_ostream &Out,
149 CodeGenFileType FileType,
151 AnalysisID StartAfter,
152 AnalysisID StopAfter) {
153 // Add common CodeGen passes.
154 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
155 StartAfter, StopAfter);
160 // FIXME: The intent is that this should eventually write out a YAML file,
161 // containing the LLVM IR, the machine-level IR (when stopping after a
162 // machine-level pass), and whatever other information is needed to
163 // deserialize the code and resume compilation. For now, just write the
165 PM.add(createPrintModulePass(Out));
169 if (hasMCSaveTempLabels())
170 Context->setAllowTemporaryLabels(false);
172 const MCAsmInfo &MAI = *getMCAsmInfo();
173 const MCRegisterInfo &MRI = *getRegisterInfo();
174 const MCInstrInfo &MII = *getInstrInfo();
175 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
176 std::unique_ptr<MCStreamer> AsmStreamer;
179 case CGFT_AssemblyFile: {
180 MCInstPrinter *InstPrinter =
181 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
184 // Create a code emitter if asked to show the encoding.
185 MCCodeEmitter *MCE = 0;
187 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
189 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
191 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
194 hasMCUseDwarfDirectory(),
198 AsmStreamer.reset(S);
201 case CGFT_ObjectFile: {
202 // Create the code emitter for the target if it exists. If not, .o file
204 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
206 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
208 if (MCE == 0 || MAB == 0)
211 AsmStreamer.reset(getTarget().createMCObjectStreamer(
212 getTargetTriple(), *Context, *MAB, Out, MCE, STI, hasMCRelaxAll(),
213 hasMCNoExecStack()));
217 // The Null output is intended for use for performance analysis and testing,
219 AsmStreamer.reset(createNullStreamer(*Context));
223 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
224 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
228 // If successful, createAsmPrinter took ownership of AsmStreamer.
229 AsmStreamer.release();
236 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
237 /// get machine code emitted. This uses a JITCodeEmitter object to handle
238 /// actually outputting the machine code and resolving things like the address
239 /// of functions. This method should return true if machine code emission is
242 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
244 bool DisableVerify) {
245 // Add common CodeGen passes.
246 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
250 addCodeEmitter(PM, JCE);
252 return false; // success!
255 /// addPassesToEmitMC - Add passes to the specified pass manager to get
256 /// machine code emitted with the MCJIT. This method returns true if machine
257 /// code is not supported. It fills the MCContext Ctx pointer which can be
258 /// used to build custom MCStreamer.
260 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
263 bool DisableVerify) {
264 // Add common CodeGen passes.
265 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
269 if (hasMCSaveTempLabels())
270 Ctx->setAllowTemporaryLabels(false);
272 // Create the code emitter for the target if it exists. If not, .o file
274 const MCRegisterInfo &MRI = *getRegisterInfo();
275 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
276 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
278 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
280 if (MCE == 0 || MAB == 0)
283 std::unique_ptr<MCStreamer> AsmStreamer;
284 AsmStreamer.reset(getTarget().createMCObjectStreamer(
285 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, hasMCRelaxAll(),
286 hasMCNoExecStack()));
288 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
289 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
293 // If successful, createAsmPrinter took ownership of AsmStreamer.
294 AsmStreamer.release();
298 return false; // success!