1 | //===-- CodeGenTargetMachineImpl.cpp --------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file This file implements the CodeGenTargetMachineImpl class. |
10 | /// |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/CodeGen/CodeGenTargetMachineImpl.h" |
14 | #include "llvm/CodeGen/AsmPrinter.h" |
15 | #include "llvm/CodeGen/BasicTTIImpl.h" |
16 | #include "llvm/CodeGen/MachineModuleInfo.h" |
17 | #include "llvm/CodeGen/Passes.h" |
18 | #include "llvm/CodeGen/TargetPassConfig.h" |
19 | #include "llvm/IR/LegacyPassManager.h" |
20 | #include "llvm/MC/MCAsmBackend.h" |
21 | #include "llvm/MC/MCAsmInfo.h" |
22 | #include "llvm/MC/MCCodeEmitter.h" |
23 | #include "llvm/MC/MCContext.h" |
24 | #include "llvm/MC/MCInstPrinter.h" |
25 | #include "llvm/MC/MCInstrInfo.h" |
26 | #include "llvm/MC/MCObjectWriter.h" |
27 | #include "llvm/MC/MCRegisterInfo.h" |
28 | #include "llvm/MC/MCStreamer.h" |
29 | #include "llvm/MC/MCSubtargetInfo.h" |
30 | #include "llvm/MC/TargetRegistry.h" |
31 | #include "llvm/Support/CommandLine.h" |
32 | #include "llvm/Support/FormattedStream.h" |
33 | #include "llvm/Target/TargetMachine.h" |
34 | #include "llvm/Target/TargetOptions.h" |
35 | using namespace llvm; |
36 | |
37 | static cl::opt<bool> |
38 | EnableTrapUnreachable("trap-unreachable" , cl::Hidden, |
39 | cl::desc("Enable generating trap for unreachable" )); |
40 | |
41 | static cl::opt<bool> EnableNoTrapAfterNoreturn( |
42 | "no-trap-after-noreturn" , cl::Hidden, |
43 | cl::desc("Do not emit a trap instruction for 'unreachable' IR instructions " |
44 | "after noreturn calls, even if --trap-unreachable is set." )); |
45 | |
46 | void CodeGenTargetMachineImpl::initAsmInfo() { |
47 | MRI.reset(p: TheTarget.createMCRegInfo(TT: getTargetTriple().str())); |
48 | assert(MRI && "Unable to create reg info" ); |
49 | MII.reset(p: TheTarget.createMCInstrInfo()); |
50 | assert(MII && "Unable to create instruction info" ); |
51 | // FIXME: Having an MCSubtargetInfo on the target machine is a hack due |
52 | // to some backends having subtarget feature dependent module level |
53 | // code generation. This is similar to the hack in the AsmPrinter for |
54 | // module level assembly etc. |
55 | STI.reset(p: TheTarget.createMCSubtargetInfo( |
56 | TheTriple: getTargetTriple().str(), CPU: getTargetCPU(), Features: getTargetFeatureString())); |
57 | assert(STI && "Unable to create subtarget info" ); |
58 | |
59 | MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo( |
60 | MRI: *MRI, TheTriple: getTargetTriple().str(), Options: Options.MCOptions); |
61 | // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, |
62 | // and if the old one gets included then MCAsmInfo will be NULL and |
63 | // we'll crash later. |
64 | // Provide the user with a useful error message about what's wrong. |
65 | assert(TmpAsmInfo && "MCAsmInfo not initialized. " |
66 | "Make sure you include the correct TargetSelect.h" |
67 | "and that InitializeAllTargetMCs() is being invoked!" ); |
68 | |
69 | if (Options.BinutilsVersion.first > 0) |
70 | TmpAsmInfo->setBinutilsVersion(Options.BinutilsVersion); |
71 | |
72 | if (Options.DisableIntegratedAS) { |
73 | TmpAsmInfo->setUseIntegratedAssembler(false); |
74 | // If there is explict option disable integratedAS, we can't use it for |
75 | // inlineasm either. |
76 | TmpAsmInfo->setParseInlineAsmUsingAsmParser(false); |
77 | } |
78 | |
79 | TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); |
80 | |
81 | TmpAsmInfo->setFullRegisterNames(Options.MCOptions.PPCUseFullRegisterNames); |
82 | |
83 | if (Options.ExceptionModel != ExceptionHandling::None) |
84 | TmpAsmInfo->setExceptionsType(Options.ExceptionModel); |
85 | |
86 | AsmInfo.reset(p: TmpAsmInfo); |
87 | } |
88 | |
89 | CodeGenTargetMachineImpl::CodeGenTargetMachineImpl( |
90 | const Target &T, StringRef DataLayoutString, const Triple &TT, |
91 | StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, |
92 | CodeModel::Model CM, CodeGenOptLevel OL) |
93 | : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { |
94 | this->RM = RM; |
95 | this->CMModel = CM; |
96 | this->OptLevel = OL; |
97 | |
98 | if (EnableTrapUnreachable) |
99 | this->Options.TrapUnreachable = true; |
100 | if (EnableNoTrapAfterNoreturn) |
101 | this->Options.NoTrapAfterNoreturn = true; |
102 | } |
103 | |
104 | TargetTransformInfo |
105 | CodeGenTargetMachineImpl::getTargetTransformInfo(const Function &F) const { |
106 | return TargetTransformInfo(std::make_unique<BasicTTIImpl>(args: this, args: F)); |
107 | } |
108 | |
109 | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
110 | static TargetPassConfig * |
111 | addPassesToGenerateCode(CodeGenTargetMachineImpl &TM, PassManagerBase &PM, |
112 | bool DisableVerify, |
113 | MachineModuleInfoWrapperPass &MMIWP) { |
114 | // Targets may override createPassConfig to provide a target-specific |
115 | // subclass. |
116 | TargetPassConfig *PassConfig = TM.createPassConfig(PM); |
117 | // Set PassConfig options provided by TargetMachine. |
118 | PassConfig->setDisableVerify(DisableVerify); |
119 | PM.add(P: PassConfig); |
120 | PM.add(P: &MMIWP); |
121 | |
122 | if (PassConfig->addISelPasses()) |
123 | return nullptr; |
124 | PassConfig->addMachinePasses(); |
125 | PassConfig->setInitialized(); |
126 | return PassConfig; |
127 | } |
128 | |
129 | bool CodeGenTargetMachineImpl::addAsmPrinter(PassManagerBase &PM, |
130 | raw_pwrite_stream &Out, |
131 | raw_pwrite_stream *DwoOut, |
132 | CodeGenFileType FileType, |
133 | MCContext &Context) { |
134 | Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr = |
135 | createMCStreamer(Out, DwoOut, FileType, Ctx&: Context); |
136 | if (!MCStreamerOrErr) { |
137 | Context.reportError(L: SMLoc(), Msg: toString(E: MCStreamerOrErr.takeError())); |
138 | return true; |
139 | } |
140 | |
141 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
142 | FunctionPass *Printer = |
143 | getTarget().createAsmPrinter(TM&: *this, Streamer: std::move(*MCStreamerOrErr)); |
144 | if (!Printer) |
145 | return true; |
146 | |
147 | PM.add(P: Printer); |
148 | return false; |
149 | } |
150 | |
151 | Expected<std::unique_ptr<MCStreamer>> |
152 | CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out, |
153 | raw_pwrite_stream *DwoOut, |
154 | CodeGenFileType FileType, |
155 | MCContext &Context) { |
156 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
157 | const MCAsmInfo &MAI = *getMCAsmInfo(); |
158 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
159 | const MCInstrInfo &MII = *getMCInstrInfo(); |
160 | |
161 | std::unique_ptr<MCStreamer> AsmStreamer; |
162 | |
163 | switch (FileType) { |
164 | case CodeGenFileType::AssemblyFile: { |
165 | std::unique_ptr<MCInstPrinter> InstPrinter(getTarget().createMCInstPrinter( |
166 | T: getTargetTriple(), |
167 | SyntaxVariant: Options.MCOptions.OutputAsmVariant.value_or(u: MAI.getAssemblerDialect()), |
168 | MAI, MII, MRI)); |
169 | for (StringRef Opt : Options.MCOptions.InstPrinterOptions) |
170 | if (!InstPrinter->applyTargetSpecificCLOption(Opt)) |
171 | return createStringError(S: "invalid InstPrinter option '" + Opt + "'" ); |
172 | |
173 | // Create a code emitter if asked to show the encoding. |
174 | std::unique_ptr<MCCodeEmitter> MCE; |
175 | if (Options.MCOptions.ShowMCEncoding) |
176 | MCE.reset(p: getTarget().createMCCodeEmitter(II: MII, Ctx&: Context)); |
177 | |
178 | std::unique_ptr<MCAsmBackend> MAB( |
179 | getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions)); |
180 | auto FOut = std::make_unique<formatted_raw_ostream>(args&: Out); |
181 | MCStreamer *S = getTarget().createAsmStreamer( |
182 | Ctx&: Context, OS: std::move(FOut), IP: std::move(InstPrinter), CE: std::move(MCE), |
183 | TAB: std::move(MAB)); |
184 | AsmStreamer.reset(p: S); |
185 | break; |
186 | } |
187 | case CodeGenFileType::ObjectFile: { |
188 | // Create the code emitter for the target if it exists. If not, .o file |
189 | // emission fails. |
190 | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(II: MII, Ctx&: Context); |
191 | if (!MCE) |
192 | return make_error<StringError>(Args: "createMCCodeEmitter failed" , |
193 | Args: inconvertibleErrorCode()); |
194 | MCAsmBackend *MAB = |
195 | getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions); |
196 | if (!MAB) |
197 | return make_error<StringError>(Args: "createMCAsmBackend failed" , |
198 | Args: inconvertibleErrorCode()); |
199 | |
200 | Triple T(getTargetTriple()); |
201 | AsmStreamer.reset(p: getTarget().createMCObjectStreamer( |
202 | T, Ctx&: Context, TAB: std::unique_ptr<MCAsmBackend>(MAB), |
203 | OW: DwoOut ? MAB->createDwoObjectWriter(OS&: Out, DwoOS&: *DwoOut) |
204 | : MAB->createObjectWriter(OS&: Out), |
205 | Emitter: std::unique_ptr<MCCodeEmitter>(MCE), STI)); |
206 | break; |
207 | } |
208 | case CodeGenFileType::Null: |
209 | // The Null output is intended for use for performance analysis and testing, |
210 | // not real users. |
211 | AsmStreamer.reset(p: getTarget().createNullStreamer(Ctx&: Context)); |
212 | break; |
213 | } |
214 | |
215 | return std::move(AsmStreamer); |
216 | } |
217 | |
218 | bool CodeGenTargetMachineImpl::addPassesToEmitFile( |
219 | PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, |
220 | CodeGenFileType FileType, bool DisableVerify, |
221 | MachineModuleInfoWrapperPass *MMIWP) { |
222 | // Add common CodeGen passes. |
223 | if (!MMIWP) |
224 | MMIWP = new MachineModuleInfoWrapperPass(this); |
225 | TargetPassConfig *PassConfig = |
226 | addPassesToGenerateCode(TM&: *this, PM, DisableVerify, MMIWP&: *MMIWP); |
227 | if (!PassConfig) |
228 | return true; |
229 | |
230 | if (TargetPassConfig::willCompleteCodeGenPipeline()) { |
231 | if (addAsmPrinter(PM, Out, DwoOut, FileType, Context&: MMIWP->getMMI().getContext())) |
232 | return true; |
233 | } else { |
234 | // MIR printing is redundant with -filetype=null. |
235 | if (FileType != CodeGenFileType::Null) |
236 | PM.add(P: createPrintMIRPass(OS&: Out)); |
237 | } |
238 | |
239 | PM.add(P: createFreeMachineFunctionPass()); |
240 | return false; |
241 | } |
242 | |
243 | /// addPassesToEmitMC - Add passes to the specified pass manager to get |
244 | /// machine code emitted with the MCJIT. This method returns true if machine |
245 | /// code is not supported. It fills the MCContext Ctx pointer which can be |
246 | /// used to build custom MCStreamer. |
247 | /// |
248 | bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM, |
249 | MCContext *&Ctx, |
250 | raw_pwrite_stream &Out, |
251 | bool DisableVerify) { |
252 | // Add common CodeGen passes. |
253 | MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); |
254 | TargetPassConfig *PassConfig = |
255 | addPassesToGenerateCode(TM&: *this, PM, DisableVerify, MMIWP&: *MMIWP); |
256 | if (!PassConfig) |
257 | return true; |
258 | assert(TargetPassConfig::willCompleteCodeGenPipeline() && |
259 | "Cannot emit MC with limited codegen pipeline" ); |
260 | |
261 | Ctx = &MMIWP->getMMI().getContext(); |
262 | // libunwind is unable to load compact unwind dynamically, so we must generate |
263 | // DWARF unwind info for the JIT. |
264 | Options.MCOptions.EmitDwarfUnwind = EmitDwarfUnwindType::Always; |
265 | |
266 | // Create the code emitter for the target if it exists. If not, .o file |
267 | // emission fails. |
268 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
269 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
270 | std::unique_ptr<MCCodeEmitter> MCE( |
271 | getTarget().createMCCodeEmitter(II: *getMCInstrInfo(), Ctx&: *Ctx)); |
272 | if (!MCE) |
273 | return true; |
274 | MCAsmBackend *MAB = |
275 | getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions); |
276 | if (!MAB) |
277 | return true; |
278 | |
279 | const Triple &T = getTargetTriple(); |
280 | std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( |
281 | T, Ctx&: *Ctx, TAB: std::unique_ptr<MCAsmBackend>(MAB), OW: MAB->createObjectWriter(OS&: Out), |
282 | Emitter: std::move(MCE), STI)); |
283 | |
284 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
285 | FunctionPass *Printer = |
286 | getTarget().createAsmPrinter(TM&: *this, Streamer: std::move(AsmStreamer)); |
287 | if (!Printer) |
288 | return true; |
289 | |
290 | PM.add(P: Printer); |
291 | PM.add(P: createFreeMachineFunctionPass()); |
292 | |
293 | return false; // success! |
294 | } |
295 | |