blob: 4c6e21ab315afafee440eb27e0ab5488145a052e [file] [log] [blame]
Matthias Braunbb8507e2017-10-12 22:57:28 +00001//===-- 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/Analysis/Passes.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/BasicTTIImpl.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetLoweringObjectFile.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000020#include "llvm/CodeGen/TargetPassConfig.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000021#include "llvm/IR/LegacyPassManager.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000022#include "llvm/MC/MCAsmBackend.h"
23#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCCodeEmitter.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"
Matthias Braunbb8507e2017-10-12 22:57:28 +000033#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetOptions.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000035using namespace llvm;
36
37void LLVMTargetMachine::initAsmInfo() {
38 MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
39 MII = TheTarget.createMCInstrInfo();
40 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
41 // to some backends having subtarget feature dependent module level
42 // code generation. This is similar to the hack in the AsmPrinter for
43 // module level assembly etc.
44 STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
45 getTargetFeatureString());
46
47 MCAsmInfo *TmpAsmInfo =
48 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
49 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
50 // and if the old one gets included then MCAsmInfo will be NULL and
51 // we'll crash later.
52 // Provide the user with a useful error message about what's wrong.
53 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
54 "Make sure you include the correct TargetSelect.h"
55 "and that InitializeAllTargetMCs() is being invoked!");
56
57 if (Options.DisableIntegratedAS)
58 TmpAsmInfo->setUseIntegratedAssembler(false);
59
60 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
61
62 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
63
64 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
65
66 if (Options.ExceptionModel != ExceptionHandling::None)
67 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
68
69 AsmInfo = TmpAsmInfo;
70}
71
72LLVMTargetMachine::LLVMTargetMachine(const Target &T,
73 StringRef DataLayoutString,
74 const Triple &TT, StringRef CPU,
75 StringRef FS, const TargetOptions &Options,
76 Reloc::Model RM, CodeModel::Model CM,
77 CodeGenOpt::Level OL)
78 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
79 this->RM = RM;
80 this->CMModel = CM;
81 this->OptLevel = OL;
82}
83
Sanjoy Das26d11ca2017-12-22 18:21:59 +000084TargetTransformInfo
85LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
86 return TargetTransformInfo(BasicTTIImpl(this, F));
Matthias Braunbb8507e2017-10-12 22:57:28 +000087}
88
89/// addPassesToX helper drives creation and initialization of TargetPassConfig.
90static MCContext *
91addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
92 bool DisableVerify, bool &WillCompleteCodeGenPipeline,
93 raw_pwrite_stream &Out, MachineModuleInfo *MMI) {
94 // Targets may override createPassConfig to provide a target-specific
95 // subclass.
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
97 // Set PassConfig options provided by TargetMachine.
98 PassConfig->setDisableVerify(DisableVerify);
99 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
100 PM.add(PassConfig);
101 if (!MMI)
102 MMI = new MachineModuleInfo(TM);
103 PM.add(MMI);
104
105 if (PassConfig->addISelPasses())
106 return nullptr;
107 PassConfig->addMachinePasses();
108 PassConfig->setInitialized();
109 if (!WillCompleteCodeGenPipeline)
110 PM.add(createPrintMIRPass(Out));
111
112 return &MMI->getContext();
113}
114
115bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
116 raw_pwrite_stream &Out, CodeGenFileType FileType,
117 MCContext &Context) {
118 if (Options.MCOptions.MCSaveTempLabels)
119 Context.setAllowTemporaryLabels(false);
120
121 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
122 const MCAsmInfo &MAI = *getMCAsmInfo();
123 const MCRegisterInfo &MRI = *getMCRegisterInfo();
124 const MCInstrInfo &MII = *getMCInstrInfo();
125
126 std::unique_ptr<MCStreamer> AsmStreamer;
127
128 switch (FileType) {
129 case CGFT_AssemblyFile: {
130 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
131 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
132
133 // Create a code emitter if asked to show the encoding.
134 MCCodeEmitter *MCE = nullptr;
135 if (Options.MCOptions.ShowMCEncoding)
136 MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
137
138 MCAsmBackend *MAB =
Alex Bradburyb22f7512018-01-03 08:53:05 +0000139 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000140 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
141 MCStreamer *S = getTarget().createAsmStreamer(
142 Context, std::move(FOut), Options.MCOptions.AsmVerbose,
143 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
144 Options.MCOptions.ShowMCInst);
145 AsmStreamer.reset(S);
146 break;
147 }
148 case CGFT_ObjectFile: {
149 // Create the code emitter for the target if it exists. If not, .o file
150 // emission fails.
151 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
152 MCAsmBackend *MAB =
Alex Bradburyb22f7512018-01-03 08:53:05 +0000153 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000154 if (!MCE || !MAB)
155 return true;
156
157 // Don't waste memory on names of temp labels.
158 Context.setUseNamesOnTempLabels(false);
159
160 Triple T(getTargetTriple().str());
161 AsmStreamer.reset(getTarget().createMCObjectStreamer(
162 T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out,
163 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
164 Options.MCOptions.MCIncrementalLinkerCompatible,
165 /*DWARFMustBeAtTheEnd*/ true));
166 break;
167 }
168 case CGFT_Null:
169 // The Null output is intended for use for performance analysis and testing,
170 // not real users.
171 AsmStreamer.reset(getTarget().createNullStreamer(Context));
172 break;
173 }
174
175 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
176 FunctionPass *Printer =
177 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
178 if (!Printer)
179 return true;
180
181 PM.add(Printer);
182 return false;
183}
184
185bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
186 raw_pwrite_stream &Out,
187 CodeGenFileType FileType,
188 bool DisableVerify,
189 MachineModuleInfo *MMI) {
190 // Add common CodeGen passes.
191 bool WillCompleteCodeGenPipeline = true;
192 MCContext *Context = addPassesToGenerateCode(
193 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
194 if (!Context)
195 return true;
196
197 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
198 return true;
199
200 PM.add(createFreeMachineFunctionPass());
201 return false;
202}
203
204/// addPassesToEmitMC - Add passes to the specified pass manager to get
205/// machine code emitted with the MCJIT. This method returns true if machine
206/// code is not supported. It fills the MCContext Ctx pointer which can be
207/// used to build custom MCStreamer.
208///
209bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
210 raw_pwrite_stream &Out,
211 bool DisableVerify) {
212 // Add common CodeGen passes.
213 bool WillCompleteCodeGenPipeline = true;
214 Ctx = addPassesToGenerateCode(this, PM, DisableVerify,
215 WillCompleteCodeGenPipeline, Out,
216 /*MachineModuleInfo*/ nullptr);
217 if (!Ctx)
218 return true;
219 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
220
221 if (Options.MCOptions.MCSaveTempLabels)
222 Ctx->setAllowTemporaryLabels(false);
223
224 // Create the code emitter for the target if it exists. If not, .o file
225 // emission fails.
Alex Bradburyb22f7512018-01-03 08:53:05 +0000226 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
Matthias Braunbb8507e2017-10-12 22:57:28 +0000227 const MCRegisterInfo &MRI = *getMCRegisterInfo();
228 MCCodeEmitter *MCE =
229 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
230 MCAsmBackend *MAB =
Alex Bradburyb22f7512018-01-03 08:53:05 +0000231 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000232 if (!MCE || !MAB)
233 return true;
234
235 const Triple &T = getTargetTriple();
Matthias Braunbb8507e2017-10-12 22:57:28 +0000236 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
237 T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out,
238 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
239 Options.MCOptions.MCIncrementalLinkerCompatible,
240 /*DWARFMustBeAtTheEnd*/ true));
241
242 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
243 FunctionPass *Printer =
244 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
245 if (!Printer)
246 return true;
247
248 PM.add(Printer);
249 PM.add(createFreeMachineFunctionPass());
250
251 return false; // success!
252}