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