1f4a2713aSLionel Sambuc //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the LLVMTargetMachine class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
15*0a6a1f1dSLionel Sambuc #include "llvm/Analysis/JumpInstrTableInfo.h"
16*0a6a1f1dSLionel Sambuc #include "llvm/Analysis/Passes.h"
17f4a2713aSLionel Sambuc #include "llvm/CodeGen/AsmPrinter.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/ForwardControlFlowIntegrity.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/JumpInstrTables.h"
20f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunctionAnalysis.h"
21f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineModuleInfo.h"
22f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/IR/IRPrintingPasses.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/IR/Verifier.h"
25f4a2713aSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
26f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
27f4a2713aSLionel Sambuc #include "llvm/MC/MCInstrInfo.h"
28f4a2713aSLionel Sambuc #include "llvm/MC/MCStreamer.h"
29f4a2713aSLionel Sambuc #include "llvm/MC/MCSubtargetInfo.h"
30f4a2713aSLionel Sambuc #include "llvm/PassManager.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
32f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/FormattedStream.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/TargetRegistry.h"
35f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
36f4a2713aSLionel Sambuc #include "llvm/Target/TargetLowering.h"
37f4a2713aSLionel Sambuc #include "llvm/Target/TargetLoweringObjectFile.h"
38f4a2713aSLionel Sambuc #include "llvm/Target/TargetOptions.h"
39f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
40f4a2713aSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
41f4a2713aSLionel Sambuc #include "llvm/Transforms/Scalar.h"
42f4a2713aSLionel Sambuc using namespace llvm;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc // Enable or disable FastISel. Both options are needed, because
45f4a2713aSLionel Sambuc // FastISel is enabled by default with -fast, and we wish to be
46f4a2713aSLionel Sambuc // able to enable or disable fast-isel independently from -O0.
47f4a2713aSLionel Sambuc static cl::opt<cl::boolOrDefault>
48f4a2713aSLionel Sambuc EnableFastISelOption("fast-isel", cl::Hidden,
49f4a2713aSLionel Sambuc   cl::desc("Enable the \"fast\" instruction selector"));
50f4a2713aSLionel Sambuc 
initAsmInfo()51f4a2713aSLionel Sambuc void LLVMTargetMachine::initAsmInfo() {
52*0a6a1f1dSLionel Sambuc   MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
53*0a6a1f1dSLionel Sambuc       *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
54f4a2713aSLionel Sambuc   // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
55f4a2713aSLionel Sambuc   // and if the old one gets included then MCAsmInfo will be NULL and
56f4a2713aSLionel Sambuc   // we'll crash later.
57f4a2713aSLionel Sambuc   // Provide the user with a useful error message about what's wrong.
58*0a6a1f1dSLionel Sambuc   assert(TmpAsmInfo && "MCAsmInfo not initialized. "
59f4a2713aSLionel Sambuc          "Make sure you include the correct TargetSelect.h"
60f4a2713aSLionel Sambuc          "and that InitializeAllTargetMCs() is being invoked!");
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc   if (Options.DisableIntegratedAS)
63*0a6a1f1dSLionel Sambuc     TmpAsmInfo->setUseIntegratedAssembler(false);
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc   if (Options.CompressDebugSections)
66*0a6a1f1dSLionel Sambuc     TmpAsmInfo->setCompressDebugSections(true);
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc   AsmInfo = TmpAsmInfo;
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc 
LLVMTargetMachine(const Target & T,StringRef Triple,StringRef CPU,StringRef FS,TargetOptions Options,Reloc::Model RM,CodeModel::Model CM,CodeGenOpt::Level OL)71f4a2713aSLionel Sambuc LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
72f4a2713aSLionel Sambuc                                      StringRef CPU, StringRef FS,
73f4a2713aSLionel Sambuc                                      TargetOptions Options,
74f4a2713aSLionel Sambuc                                      Reloc::Model RM, CodeModel::Model CM,
75f4a2713aSLionel Sambuc                                      CodeGenOpt::Level OL)
76f4a2713aSLionel Sambuc   : TargetMachine(T, Triple, CPU, FS, Options) {
77f4a2713aSLionel Sambuc   CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
addAnalysisPasses(PassManagerBase & PM)80f4a2713aSLionel Sambuc void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
81f4a2713aSLionel Sambuc   PM.add(createBasicTargetTransformInfoPass(this));
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc /// addPassesToX helper drives creation and initialization of TargetPassConfig.
addPassesToGenerateCode(LLVMTargetMachine * TM,PassManagerBase & PM,bool DisableVerify,AnalysisID StartAfter,AnalysisID StopAfter)85f4a2713aSLionel Sambuc static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
86f4a2713aSLionel Sambuc                                           PassManagerBase &PM,
87f4a2713aSLionel Sambuc                                           bool DisableVerify,
88f4a2713aSLionel Sambuc                                           AnalysisID StartAfter,
89f4a2713aSLionel Sambuc                                           AnalysisID StopAfter) {
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc   // Add internal analysis passes from the target machine.
92*0a6a1f1dSLionel Sambuc   TM->addAnalysisPasses(PM);
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc   // Targets may override createPassConfig to provide a target-specific
95*0a6a1f1dSLionel Sambuc   // subclass.
96f4a2713aSLionel Sambuc   TargetPassConfig *PassConfig = TM->createPassConfig(PM);
97f4a2713aSLionel Sambuc   PassConfig->setStartStopPasses(StartAfter, StopAfter);
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   // Set PassConfig options provided by TargetMachine.
100f4a2713aSLionel Sambuc   PassConfig->setDisableVerify(DisableVerify);
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc   PM.add(PassConfig);
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   PassConfig->addIRPasses();
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   PassConfig->addCodeGenPrepare();
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc   PassConfig->addPassesToHandleExceptions();
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc   PassConfig->addISelPrepare();
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   // Install a MachineModuleInfo class, which is an immutable pass that holds
113f4a2713aSLionel Sambuc   // all the per-module stuff we're generating, including MCContext.
114*0a6a1f1dSLionel Sambuc   MachineModuleInfo *MMI = new MachineModuleInfo(
115*0a6a1f1dSLionel Sambuc       *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
116*0a6a1f1dSLionel Sambuc       &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
117f4a2713aSLionel Sambuc   PM.add(MMI);
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc   // Set up a MachineFunction for the rest of CodeGen to work on.
120f4a2713aSLionel Sambuc   PM.add(new MachineFunctionAnalysis(*TM));
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   // Enable FastISel with -fast, but allow that to be overridden.
123f4a2713aSLionel Sambuc   if (EnableFastISelOption == cl::BOU_TRUE ||
124f4a2713aSLionel Sambuc       (TM->getOptLevel() == CodeGenOpt::None &&
125f4a2713aSLionel Sambuc        EnableFastISelOption != cl::BOU_FALSE))
126f4a2713aSLionel Sambuc     TM->setFastISel(true);
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc   // Ask the target for an isel.
129f4a2713aSLionel Sambuc   if (PassConfig->addInstSelector())
130*0a6a1f1dSLionel Sambuc     return nullptr;
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   PassConfig->addMachinePasses();
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   PassConfig->setInitialized();
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc   return &MMI->getContext();
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc 
addPassesToEmitFile(PassManagerBase & PM,formatted_raw_ostream & Out,CodeGenFileType FileType,bool DisableVerify,AnalysisID StartAfter,AnalysisID StopAfter)139f4a2713aSLionel Sambuc bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
140f4a2713aSLionel Sambuc                                             formatted_raw_ostream &Out,
141f4a2713aSLionel Sambuc                                             CodeGenFileType FileType,
142f4a2713aSLionel Sambuc                                             bool DisableVerify,
143f4a2713aSLionel Sambuc                                             AnalysisID StartAfter,
144f4a2713aSLionel Sambuc                                             AnalysisID StopAfter) {
145*0a6a1f1dSLionel Sambuc   // Passes to handle jumptable function annotations. These can't be handled at
146*0a6a1f1dSLionel Sambuc   // JIT time, so we don't add them directly to addPassesToGenerateCode.
147*0a6a1f1dSLionel Sambuc   PM.add(createJumpInstrTableInfoPass(
148*0a6a1f1dSLionel Sambuc       getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
149*0a6a1f1dSLionel Sambuc   PM.add(createJumpInstrTablesPass(Options.JTType));
150*0a6a1f1dSLionel Sambuc   if (Options.FCFI)
151*0a6a1f1dSLionel Sambuc     PM.add(createForwardControlFlowIntegrityPass(
152*0a6a1f1dSLionel Sambuc         Options.JTType, Options.CFIType, Options.CFIEnforcing,
153*0a6a1f1dSLionel Sambuc         Options.getCFIFuncName()));
154*0a6a1f1dSLionel Sambuc 
155f4a2713aSLionel Sambuc   // Add common CodeGen passes.
156f4a2713aSLionel Sambuc   MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
157f4a2713aSLionel Sambuc                                                StartAfter, StopAfter);
158f4a2713aSLionel Sambuc   if (!Context)
159f4a2713aSLionel Sambuc     return true;
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc   if (StopAfter) {
162f4a2713aSLionel Sambuc     // FIXME: The intent is that this should eventually write out a YAML file,
163f4a2713aSLionel Sambuc     // containing the LLVM IR, the machine-level IR (when stopping after a
164f4a2713aSLionel Sambuc     // machine-level pass), and whatever other information is needed to
165f4a2713aSLionel Sambuc     // deserialize the code and resume compilation.  For now, just write the
166f4a2713aSLionel Sambuc     // LLVM IR.
167*0a6a1f1dSLionel Sambuc     PM.add(createPrintModulePass(Out));
168f4a2713aSLionel Sambuc     return false;
169f4a2713aSLionel Sambuc   }
170f4a2713aSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc   if (Options.MCOptions.MCSaveTempLabels)
172f4a2713aSLionel Sambuc     Context->setAllowTemporaryLabels(false);
173f4a2713aSLionel Sambuc 
174f4a2713aSLionel Sambuc   const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
175*0a6a1f1dSLionel Sambuc   const MCAsmInfo &MAI = *getMCAsmInfo();
176*0a6a1f1dSLionel Sambuc   const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
177*0a6a1f1dSLionel Sambuc   const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
178*0a6a1f1dSLionel Sambuc   std::unique_ptr<MCStreamer> AsmStreamer;
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc   switch (FileType) {
181f4a2713aSLionel Sambuc   case CGFT_AssemblyFile: {
182f4a2713aSLionel Sambuc     MCInstPrinter *InstPrinter =
183f4a2713aSLionel Sambuc       getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
184f4a2713aSLionel Sambuc                                       MII, MRI, STI);
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc     // Create a code emitter if asked to show the encoding.
187*0a6a1f1dSLionel Sambuc     MCCodeEmitter *MCE = nullptr;
188*0a6a1f1dSLionel Sambuc     if (Options.MCOptions.ShowMCEncoding)
189f4a2713aSLionel Sambuc       MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc     MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
192f4a2713aSLionel Sambuc                                                        TargetCPU);
193*0a6a1f1dSLionel Sambuc     MCStreamer *S = getTarget().createAsmStreamer(
194*0a6a1f1dSLionel Sambuc         *Context, Out, Options.MCOptions.AsmVerbose,
195*0a6a1f1dSLionel Sambuc         Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
196*0a6a1f1dSLionel Sambuc         Options.MCOptions.ShowMCInst);
197f4a2713aSLionel Sambuc     AsmStreamer.reset(S);
198f4a2713aSLionel Sambuc     break;
199f4a2713aSLionel Sambuc   }
200f4a2713aSLionel Sambuc   case CGFT_ObjectFile: {
201f4a2713aSLionel Sambuc     // Create the code emitter for the target if it exists.  If not, .o file
202f4a2713aSLionel Sambuc     // emission fails.
203f4a2713aSLionel Sambuc     MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
204f4a2713aSLionel Sambuc                                                          *Context);
205f4a2713aSLionel Sambuc     MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
206f4a2713aSLionel Sambuc                                                        TargetCPU);
207*0a6a1f1dSLionel Sambuc     if (!MCE || !MAB)
208f4a2713aSLionel Sambuc       return true;
209f4a2713aSLionel Sambuc 
210*0a6a1f1dSLionel Sambuc     AsmStreamer.reset(
211*0a6a1f1dSLionel Sambuc         getTarget()
212*0a6a1f1dSLionel Sambuc             .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
213*0a6a1f1dSLionel Sambuc                                     STI, Options.MCOptions.MCRelaxAll));
214f4a2713aSLionel Sambuc     break;
215f4a2713aSLionel Sambuc   }
216f4a2713aSLionel Sambuc   case CGFT_Null:
217f4a2713aSLionel Sambuc     // The Null output is intended for use for performance analysis and testing,
218f4a2713aSLionel Sambuc     // not real users.
219*0a6a1f1dSLionel Sambuc     AsmStreamer.reset(getTarget().createNullStreamer(*Context));
220f4a2713aSLionel Sambuc     break;
221f4a2713aSLionel Sambuc   }
222f4a2713aSLionel Sambuc 
223f4a2713aSLionel Sambuc   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
224f4a2713aSLionel Sambuc   FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
225*0a6a1f1dSLionel Sambuc   if (!Printer)
226f4a2713aSLionel Sambuc     return true;
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc   // If successful, createAsmPrinter took ownership of AsmStreamer.
229*0a6a1f1dSLionel Sambuc   AsmStreamer.release();
230f4a2713aSLionel Sambuc 
231f4a2713aSLionel Sambuc   PM.add(Printer);
232f4a2713aSLionel Sambuc 
233f4a2713aSLionel Sambuc   return false;
234f4a2713aSLionel Sambuc }
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc /// addPassesToEmitMC - Add passes to the specified pass manager to get
237f4a2713aSLionel Sambuc /// machine code emitted with the MCJIT. This method returns true if machine
238f4a2713aSLionel Sambuc /// code is not supported. It fills the MCContext Ctx pointer which can be
239f4a2713aSLionel Sambuc /// used to build custom MCStreamer.
240f4a2713aSLionel Sambuc ///
addPassesToEmitMC(PassManagerBase & PM,MCContext * & Ctx,raw_ostream & Out,bool DisableVerify)241f4a2713aSLionel Sambuc bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
242f4a2713aSLionel Sambuc                                           MCContext *&Ctx,
243f4a2713aSLionel Sambuc                                           raw_ostream &Out,
244f4a2713aSLionel Sambuc                                           bool DisableVerify) {
245f4a2713aSLionel Sambuc   // Add common CodeGen passes.
246*0a6a1f1dSLionel Sambuc   Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
247f4a2713aSLionel Sambuc   if (!Ctx)
248f4a2713aSLionel Sambuc     return true;
249f4a2713aSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc   if (Options.MCOptions.MCSaveTempLabels)
251f4a2713aSLionel Sambuc     Ctx->setAllowTemporaryLabels(false);
252f4a2713aSLionel Sambuc 
253f4a2713aSLionel Sambuc   // Create the code emitter for the target if it exists.  If not, .o file
254f4a2713aSLionel Sambuc   // emission fails.
255*0a6a1f1dSLionel Sambuc   const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
256f4a2713aSLionel Sambuc   const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
257*0a6a1f1dSLionel Sambuc   MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
258*0a6a1f1dSLionel Sambuc       *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
259f4a2713aSLionel Sambuc   MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
260f4a2713aSLionel Sambuc                                                      TargetCPU);
261*0a6a1f1dSLionel Sambuc   if (!MCE || !MAB)
262f4a2713aSLionel Sambuc     return true;
263f4a2713aSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc   std::unique_ptr<MCStreamer> AsmStreamer;
265*0a6a1f1dSLionel Sambuc   AsmStreamer.reset(getTarget()
266*0a6a1f1dSLionel Sambuc                         .createMCObjectStreamer(getTargetTriple(), *Ctx, *MAB,
267*0a6a1f1dSLionel Sambuc                                                 Out, MCE, STI,
268*0a6a1f1dSLionel Sambuc                                                 Options.MCOptions.MCRelaxAll));
269f4a2713aSLionel Sambuc 
270f4a2713aSLionel Sambuc   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
271f4a2713aSLionel Sambuc   FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
272*0a6a1f1dSLionel Sambuc   if (!Printer)
273f4a2713aSLionel Sambuc     return true;
274f4a2713aSLionel Sambuc 
275f4a2713aSLionel Sambuc   // If successful, createAsmPrinter took ownership of AsmStreamer.
276*0a6a1f1dSLionel Sambuc   AsmStreamer.release();
277f4a2713aSLionel Sambuc 
278f4a2713aSLionel Sambuc   PM.add(Printer);
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc   return false; // success!
281f4a2713aSLionel Sambuc }
282