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"
16 #include "llvm/Analysis/JumpInstrTableInfo.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/ForwardControlFlowIntegrity.h"
20 #include "llvm/CodeGen/JumpInstrTables.h"
21 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/IR/IRPrintingPasses.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/PassManager.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetLowering.h"
38 #include "llvm/Target/TargetLoweringObjectFile.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/Target/TargetRegisterInfo.h"
41 #include "llvm/Target/TargetSubtargetInfo.h"
42 #include "llvm/Transforms/Scalar.h"
45 // Enable or disable FastISel. Both options are needed, because
46 // FastISel is enabled by default with -fast, and we wish to be
47 // able to enable or disable fast-isel independently from -O0.
48 static cl::opt<cl::boolOrDefault>
49 EnableFastISelOption("fast-isel", cl::Hidden,
50 cl::desc("Enable the \"fast\" instruction selector"));
52 void LLVMTargetMachine::initAsmInfo() {
53 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
54 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
55 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
56 // and if the old one gets included then MCAsmInfo will be NULL and
58 // Provide the user with a useful error message about what's wrong.
59 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
60 "Make sure you include the correct TargetSelect.h"
61 "and that InitializeAllTargetMCs() is being invoked!");
63 if (Options.DisableIntegratedAS)
64 TmpAsmInfo->setUseIntegratedAssembler(false);
66 if (Options.CompressDebugSections)
67 TmpAsmInfo->setCompressDebugSections(true);
72 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
73 StringRef CPU, StringRef FS,
74 TargetOptions Options,
75 Reloc::Model RM, CodeModel::Model CM,
77 : TargetMachine(T, Triple, CPU, FS, Options) {
78 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
81 void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
82 PM.add(createBasicTargetTransformInfoPass(this));
85 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
86 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
89 AnalysisID StartAfter,
90 AnalysisID StopAfter) {
92 // Add internal analysis passes from the target machine.
93 TM->addAnalysisPasses(PM);
95 // Targets may override createPassConfig to provide a target-specific
97 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
98 PassConfig->setStartStopPasses(StartAfter, StopAfter);
100 // Set PassConfig options provided by TargetMachine.
101 PassConfig->setDisableVerify(DisableVerify);
105 PassConfig->addIRPasses();
107 PassConfig->addCodeGenPrepare();
109 PassConfig->addPassesToHandleExceptions();
111 PassConfig->addISelPrepare();
113 // Install a MachineModuleInfo class, which is an immutable pass that holds
114 // all the per-module stuff we're generating, including MCContext.
115 MachineModuleInfo *MMI = new MachineModuleInfo(
116 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
117 &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
120 // Set up a MachineFunction for the rest of CodeGen to work on.
121 PM.add(new MachineFunctionAnalysis(*TM));
123 // Enable FastISel with -fast, but allow that to be overridden.
124 if (EnableFastISelOption == cl::BOU_TRUE ||
125 (TM->getOptLevel() == CodeGenOpt::None &&
126 EnableFastISelOption != cl::BOU_FALSE))
127 TM->setFastISel(true);
129 // Ask the target for an isel.
130 if (PassConfig->addInstSelector())
133 PassConfig->addMachinePasses();
135 PassConfig->setInitialized();
137 return &MMI->getContext();
140 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
141 formatted_raw_ostream &Out,
142 CodeGenFileType FileType,
144 AnalysisID StartAfter,
145 AnalysisID StopAfter) {
146 // Passes to handle jumptable function annotations. These can't be handled at
147 // JIT time, so we don't add them directly to addPassesToGenerateCode.
148 PM.add(createJumpInstrTableInfoPass(
149 getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
150 PM.add(createJumpInstrTablesPass(Options.JTType));
152 PM.add(createForwardControlFlowIntegrityPass(
153 Options.JTType, Options.CFIType, Options.CFIEnforcing,
154 Options.getCFIFuncName()));
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 (Options.MCOptions.MCSaveTempLabels)
173 Context->setAllowTemporaryLabels(false);
175 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
176 const MCAsmInfo &MAI = *getMCAsmInfo();
177 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
178 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
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;
189 if (Options.MCOptions.ShowMCEncoding)
190 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
192 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
194 MCStreamer *S = getTarget().createAsmStreamer(
195 *Context, Out, Options.MCOptions.AsmVerbose,
196 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
197 Options.MCOptions.ShowMCInst);
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(),
213 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
214 STI, Options.MCOptions.MCRelaxAll));
218 // The Null output is intended for use for performance analysis and testing,
220 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
224 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
225 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
229 // If successful, createAsmPrinter took ownership of AsmStreamer.
230 AsmStreamer.release();
237 /// addPassesToEmitMC - Add passes to the specified pass manager to get
238 /// machine code emitted with the MCJIT. This method returns true if machine
239 /// code is not supported. It fills the MCContext Ctx pointer which can be
240 /// used to build custom MCStreamer.
242 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
245 bool DisableVerify) {
246 // Add common CodeGen passes.
247 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
251 if (Options.MCOptions.MCSaveTempLabels)
252 Ctx->setAllowTemporaryLabels(false);
254 // Create the code emitter for the target if it exists. If not, .o file
256 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
257 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
258 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
259 *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
260 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
265 std::unique_ptr<MCStreamer> AsmStreamer;
266 AsmStreamer.reset(getTarget()
267 .createMCObjectStreamer(getTargetTriple(), *Ctx, *MAB,
269 Options.MCOptions.MCRelaxAll));
271 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
272 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
276 // If successful, createAsmPrinter took ownership of AsmStreamer.
277 AsmStreamer.release();
281 return false; // success!