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/Analysis/JumpInstrTableInfo.h"
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/BasicTTIImpl.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 TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
82 return TargetIRAnalysis([this](Function &F) {
83 return TargetTransformInfo(BasicTTIImpl(this, F));
87 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
88 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
91 AnalysisID StartAfter,
92 AnalysisID StopAfter) {
94 // Add internal analysis passes from the target machine.
95 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
97 // Targets may override createPassConfig to provide a target-specific
99 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
100 PassConfig->setStartStopPasses(StartAfter, StopAfter);
102 // Set PassConfig options provided by TargetMachine.
103 PassConfig->setDisableVerify(DisableVerify);
107 PassConfig->addIRPasses();
109 PassConfig->addCodeGenPrepare();
111 PassConfig->addPassesToHandleExceptions();
113 PassConfig->addISelPrepare();
115 // Install a MachineModuleInfo class, which is an immutable pass that holds
116 // all the per-module stuff we're generating, including MCContext.
117 MachineModuleInfo *MMI = new MachineModuleInfo(
118 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
119 TM->getObjFileLowering());
122 // Set up a MachineFunction for the rest of CodeGen to work on.
123 PM.add(new MachineFunctionAnalysis(*TM));
125 // Enable FastISel with -fast, but allow that to be overridden.
126 if (EnableFastISelOption == cl::BOU_TRUE ||
127 (TM->getOptLevel() == CodeGenOpt::None &&
128 EnableFastISelOption != cl::BOU_FALSE))
129 TM->setFastISel(true);
131 // Ask the target for an isel.
132 if (PassConfig->addInstSelector())
135 PassConfig->addMachinePasses();
137 PassConfig->setInitialized();
139 return &MMI->getContext();
142 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
143 formatted_raw_ostream &Out,
144 CodeGenFileType FileType,
146 AnalysisID StartAfter,
147 AnalysisID StopAfter) {
148 // Passes to handle jumptable function annotations. These can't be handled at
149 // JIT time, so we don't add them directly to addPassesToGenerateCode.
150 PM.add(createJumpInstrTableInfoPass(
151 getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
152 PM.add(createJumpInstrTablesPass(Options.JTType));
154 PM.add(createForwardControlFlowIntegrityPass(
155 Options.JTType, Options.CFIType, Options.CFIEnforcing,
156 Options.getCFIFuncName()));
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 (Options.MCOptions.MCSaveTempLabels)
175 Context->setAllowTemporaryLabels(false);
177 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
178 const MCAsmInfo &MAI = *getMCAsmInfo();
179 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
180 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
181 std::unique_ptr<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 = nullptr;
191 if (Options.MCOptions.ShowMCEncoding)
192 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
194 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
196 MCStreamer *S = getTarget().createAsmStreamer(
197 *Context, Out, Options.MCOptions.AsmVerbose,
198 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
199 Options.MCOptions.ShowMCInst);
200 AsmStreamer.reset(S);
203 case CGFT_ObjectFile: {
204 // Create the code emitter for the target if it exists. If not, .o file
206 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
208 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
215 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
216 STI, Options.MCOptions.MCRelaxAll));
220 // The Null output is intended for use for performance analysis and testing,
222 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
226 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
227 FunctionPass *Printer =
228 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
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(getTarget().createMCObjectStreamer(
266 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
267 Options.MCOptions.MCRelaxAll));
269 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
270 FunctionPass *Printer =
271 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
277 return false; // success!