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