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));
57 NoIntegratedAssembler("no-integrated-as", cl::Hidden,
58 cl::desc("Disable integrated assembler"));
60 static bool getVerboseAsm() {
62 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
63 case cl::BOU_TRUE: return true;
64 case cl::BOU_FALSE: return false;
66 llvm_unreachable("Invalid verbose asm state");
69 void LLVMTargetMachine::initAsmInfo() {
70 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
72 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
73 // and if the old one gets included then MCAsmInfo will be NULL and
75 // Provide the user with a useful error message about what's wrong.
76 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
77 "Make sure you include the correct TargetSelect.h"
78 "and that InitializeAllTargetMCs() is being invoked!");
80 if (NoIntegratedAssembler)
81 TmpAsmInfo->setUseIntegratedAssembler(false);
86 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
87 StringRef CPU, StringRef FS,
88 TargetOptions Options,
89 Reloc::Model RM, CodeModel::Model CM,
91 : TargetMachine(T, Triple, CPU, FS, Options) {
92 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
95 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
96 PM.add(createBasicTargetTransformInfoPass(this));
99 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
100 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
103 AnalysisID StartAfter,
104 AnalysisID StopAfter) {
105 // Add internal analysis passes from the target machine.
106 TM->addAnalysisPasses(PM);
108 // Targets may override createPassConfig to provide a target-specific sublass.
109 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
110 PassConfig->setStartStopPasses(StartAfter, StopAfter);
112 // Set PassConfig options provided by TargetMachine.
113 PassConfig->setDisableVerify(DisableVerify);
117 PassConfig->addIRPasses();
119 PassConfig->addCodeGenPrepare();
121 PassConfig->addPassesToHandleExceptions();
123 PassConfig->addISelPrepare();
125 // Install a MachineModuleInfo class, which is an immutable pass that holds
126 // all the per-module stuff we're generating, including MCContext.
127 MachineModuleInfo *MMI =
128 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
129 &TM->getTargetLowering()->getObjFileLowering());
132 // Set up a MachineFunction for the rest of CodeGen to work on.
133 PM.add(new MachineFunctionAnalysis(*TM));
135 // Enable FastISel with -fast, but allow that to be overridden.
136 if (EnableFastISelOption == cl::BOU_TRUE ||
137 (TM->getOptLevel() == CodeGenOpt::None &&
138 EnableFastISelOption != cl::BOU_FALSE))
139 TM->setFastISel(true);
141 // Ask the target for an isel.
142 if (PassConfig->addInstSelector())
145 PassConfig->addMachinePasses();
147 PassConfig->setInitialized();
149 return &MMI->getContext();
152 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
153 formatted_raw_ostream &Out,
154 CodeGenFileType FileType,
156 AnalysisID StartAfter,
157 AnalysisID StopAfter) {
158 // Add common CodeGen passes.
159 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
160 StartAfter, StopAfter);
165 // FIXME: The intent is that this should eventually write out a YAML file,
166 // containing the LLVM IR, the machine-level IR (when stopping after a
167 // machine-level pass), and whatever other information is needed to
168 // deserialize the code and resume compilation. For now, just write the
170 PM.add(createPrintModulePass(Out));
174 if (hasMCSaveTempLabels())
175 Context->setAllowTemporaryLabels(false);
177 const MCAsmInfo &MAI = *getMCAsmInfo();
178 const MCRegisterInfo &MRI = *getRegisterInfo();
179 const MCInstrInfo &MII = *getInstrInfo();
180 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
181 OwningPtr<MCStreamer> AsmStreamer;
184 case CGFT_AssemblyFile: {
185 MCInstPrinter *InstPrinter =
186 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
189 // Create a code emitter if asked to show the encoding.
190 MCCodeEmitter *MCE = 0;
192 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
194 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
196 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
199 hasMCUseDwarfDirectory(),
203 AsmStreamer.reset(S);
206 case CGFT_ObjectFile: {
207 // Create the code emitter for the target if it exists. If not, .o file
209 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
211 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
213 if (MCE == 0 || MAB == 0)
216 AsmStreamer.reset(getTarget().createMCObjectStreamer(
217 getTargetTriple(), *Context, *MAB, Out, MCE, STI, hasMCRelaxAll(),
218 hasMCNoExecStack()));
222 // The Null output is intended for use for performance analysis and testing,
224 AsmStreamer.reset(createNullStreamer(*Context));
228 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
229 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
233 // If successful, createAsmPrinter took ownership of AsmStreamer.
241 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
242 /// get machine code emitted. This uses a JITCodeEmitter object to handle
243 /// actually outputting the machine code and resolving things like the address
244 /// of functions. This method should return true if machine code emission is
247 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
249 bool DisableVerify) {
250 // Add common CodeGen passes.
251 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
255 addCodeEmitter(PM, JCE);
257 return false; // success!
260 /// addPassesToEmitMC - Add passes to the specified pass manager to get
261 /// machine code emitted with the MCJIT. This method returns true if machine
262 /// code is not supported. It fills the MCContext Ctx pointer which can be
263 /// used to build custom MCStreamer.
265 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
268 bool DisableVerify) {
269 // Add common CodeGen passes.
270 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
274 if (hasMCSaveTempLabels())
275 Ctx->setAllowTemporaryLabels(false);
277 // Create the code emitter for the target if it exists. If not, .o file
279 const MCRegisterInfo &MRI = *getRegisterInfo();
280 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
281 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
283 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
285 if (MCE == 0 || MAB == 0)
288 OwningPtr<MCStreamer> AsmStreamer;
289 AsmStreamer.reset(getTarget().createMCObjectStreamer(
290 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, hasMCRelaxAll(),
291 hasMCNoExecStack()));
293 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
294 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
298 // If successful, createAsmPrinter took ownership of AsmStreamer.
303 return false; // success!