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.MCOptions.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 assert(TmpAsmInfo->getExceptionHandlingType() ==
84 getTargetTriple().getDefaultExceptionHandling() &&
85 "MCAsmInfo and Triple disagree on default exception handling type");
86
87 if (Options.ExceptionModel != ExceptionHandling::Default)
88 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
89
90 AsmInfo.reset(p: TmpAsmInfo);
91}
92
93CodeGenTargetMachineImpl::CodeGenTargetMachineImpl(
94 const Target &T, StringRef DataLayoutString, const Triple &TT,
95 StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM,
96 CodeModel::Model CM, CodeGenOptLevel OL)
97 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
98 this->RM = RM;
99 this->CMModel = CM;
100 this->OptLevel = OL;
101
102 if (EnableTrapUnreachable)
103 this->Options.TrapUnreachable = true;
104 if (EnableNoTrapAfterNoreturn)
105 this->Options.NoTrapAfterNoreturn = true;
106}
107
108TargetTransformInfo
109CodeGenTargetMachineImpl::getTargetTransformInfo(const Function &F) const {
110 return TargetTransformInfo(std::make_unique<BasicTTIImpl>(args: this, args: F));
111}
112
113/// addPassesToX helper drives creation and initialization of TargetPassConfig.
114static TargetPassConfig *
115addPassesToGenerateCode(CodeGenTargetMachineImpl &TM, PassManagerBase &PM,
116 bool DisableVerify,
117 MachineModuleInfoWrapperPass &MMIWP) {
118 // Targets may override createPassConfig to provide a target-specific
119 // subclass.
120 TargetPassConfig *PassConfig = TM.createPassConfig(PM);
121 // Set PassConfig options provided by TargetMachine.
122 PassConfig->setDisableVerify(DisableVerify);
123 PM.add(P: PassConfig);
124 PM.add(P: &MMIWP);
125
126 const TargetOptions &Options = TM.Options;
127 TargetLibraryInfoImpl TLII(TM.getTargetTriple(), Options.VecLib);
128 PM.add(P: new TargetLibraryInfoWrapperPass(TLII));
129 PM.add(
130 P: new RuntimeLibraryInfoWrapper(Options.MCOptions.ABIName, Options.VecLib));
131
132 invokeGlobalTargetPassConfigCallbacks(TM, PM, PassConfig);
133
134 if (PassConfig->addISelPasses())
135 return nullptr;
136 PassConfig->addMachinePasses();
137 PassConfig->setInitialized();
138 return PassConfig;
139}
140
141bool CodeGenTargetMachineImpl::addAsmPrinter(PassManagerBase &PM,
142 raw_pwrite_stream &Out,
143 raw_pwrite_stream *DwoOut,
144 CodeGenFileType FileType,
145 MCContext &Context) {
146 Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
147 createMCStreamer(Out, DwoOut, FileType, Ctx&: Context);
148 if (!MCStreamerOrErr) {
149 Context.reportError(L: SMLoc(), Msg: toString(E: MCStreamerOrErr.takeError()));
150 return true;
151 }
152
153 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
154 FunctionPass *Printer =
155 getTarget().createAsmPrinter(TM&: *this, Streamer: std::move(*MCStreamerOrErr));
156 if (!Printer)
157 return true;
158
159 PM.add(P: Printer);
160 return false;
161}
162
163Expected<std::unique_ptr<MCStreamer>>
164CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
165 raw_pwrite_stream *DwoOut,
166 CodeGenFileType FileType,
167 MCContext &Context) {
168 const MCSubtargetInfo &STI = getMCSubtargetInfo();
169 const MCAsmInfo &MAI = getMCAsmInfo();
170 const MCRegisterInfo &MRI = getMCRegisterInfo();
171 const MCInstrInfo &MII = *getMCInstrInfo();
172
173 std::unique_ptr<MCStreamer> AsmStreamer;
174
175 switch (FileType) {
176 case CodeGenFileType::AssemblyFile: {
177 std::unique_ptr<MCInstPrinter> InstPrinter(getTarget().createMCInstPrinter(
178 T: getTargetTriple(), SyntaxVariant: MAI.getOutputAssemblerDialect(), MAI, MII, MRI));
179 for (StringRef Opt : Options.MCOptions.InstPrinterOptions)
180 if (!InstPrinter->applyTargetSpecificCLOption(Opt))
181 return createStringError(S: "invalid InstPrinter option '" + Opt + "'");
182
183 // Create a code emitter if asked to show the encoding.
184 std::unique_ptr<MCCodeEmitter> MCE;
185 if (Options.MCOptions.ShowMCEncoding)
186 MCE.reset(p: getTarget().createMCCodeEmitter(II: MII, Ctx&: Context));
187
188 std::unique_ptr<MCAsmBackend> MAB(
189 getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions));
190 auto FOut = std::make_unique<formatted_raw_ostream>(args&: Out);
191 MCStreamer *S = getTarget().createAsmStreamer(
192 Ctx&: Context, OS: std::move(FOut), IP: std::move(InstPrinter), CE: std::move(MCE),
193 TAB: std::move(MAB));
194 AsmStreamer.reset(p: S);
195 break;
196 }
197 case CodeGenFileType::ObjectFile: {
198 // Create the code emitter for the target if it exists. If not, .o file
199 // emission fails.
200 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(II: MII, Ctx&: Context);
201 if (!MCE)
202 return make_error<StringError>(Args: "createMCCodeEmitter failed",
203 Args: inconvertibleErrorCode());
204 MCAsmBackend *MAB =
205 getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions);
206 if (!MAB)
207 return make_error<StringError>(Args: "createMCAsmBackend failed",
208 Args: inconvertibleErrorCode());
209
210 Triple T(getTargetTriple());
211 AsmStreamer.reset(p: getTarget().createMCObjectStreamer(
212 T, Ctx&: Context, TAB: std::unique_ptr<MCAsmBackend>(MAB),
213 OW: DwoOut ? MAB->createDwoObjectWriter(OS&: Out, DwoOS&: *DwoOut)
214 : MAB->createObjectWriter(OS&: Out),
215 Emitter: std::unique_ptr<MCCodeEmitter>(MCE), STI));
216 break;
217 }
218 case CodeGenFileType::Null:
219 // The Null output is intended for use for performance analysis and testing,
220 // not real users.
221 AsmStreamer.reset(p: getTarget().createNullStreamer(Ctx&: Context));
222 break;
223 }
224
225 return std::move(AsmStreamer);
226}
227
228bool CodeGenTargetMachineImpl::addPassesToEmitFile(
229 PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
230 CodeGenFileType FileType, bool DisableVerify,
231 MachineModuleInfoWrapperPass *MMIWP) {
232 // Add common CodeGen passes.
233 if (!MMIWP)
234 MMIWP = new MachineModuleInfoWrapperPass(this);
235 TargetPassConfig *PassConfig =
236 addPassesToGenerateCode(TM&: *this, PM, DisableVerify, MMIWP&: *MMIWP);
237 if (!PassConfig)
238 return true;
239
240 if (TargetPassConfig::willCompleteCodeGenPipeline()) {
241 if (addAsmPrinter(PM, Out, DwoOut, FileType, Context&: MMIWP->getMMI().getContext()))
242 return true;
243 } else {
244 // MIR printing is redundant with -filetype=null.
245 if (FileType != CodeGenFileType::Null)
246 PM.add(P: createPrintMIRPass(OS&: Out));
247 }
248
249 PM.add(P: createFreeMachineFunctionPass());
250 return false;
251}
252
253/// addPassesToEmitMC - Add passes to the specified pass manager to get
254/// machine code emitted with the MCJIT. This method returns true if machine
255/// code is not supported. It fills the MCContext Ctx pointer which can be
256/// used to build custom MCStreamer.
257///
258bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
259 MCContext *&Ctx,
260 raw_pwrite_stream &Out,
261 bool DisableVerify) {
262 // Add common CodeGen passes.
263 MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this);
264 TargetPassConfig *PassConfig =
265 addPassesToGenerateCode(TM&: *this, PM, DisableVerify, MMIWP&: *MMIWP);
266 if (!PassConfig)
267 return true;
268 assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
269 "Cannot emit MC with limited codegen pipeline");
270
271 Ctx = &MMIWP->getMMI().getContext();
272 // libunwind is unable to load compact unwind dynamically, so we must generate
273 // DWARF unwind info for the JIT.
274 Options.MCOptions.EmitDwarfUnwind = EmitDwarfUnwindType::Always;
275
276 // Create the code emitter for the target if it exists. If not, .o file
277 // emission fails.
278 const MCSubtargetInfo &STI = getMCSubtargetInfo();
279 const MCRegisterInfo &MRI = getMCRegisterInfo();
280 std::unique_ptr<MCCodeEmitter> MCE(
281 getTarget().createMCCodeEmitter(II: *getMCInstrInfo(), Ctx&: *Ctx));
282 if (!MCE)
283 return true;
284 MCAsmBackend *MAB =
285 getTarget().createMCAsmBackend(STI, MRI, Options: Options.MCOptions);
286 if (!MAB)
287 return true;
288
289 const Triple &T = getTargetTriple();
290 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
291 T, Ctx&: *Ctx, TAB: std::unique_ptr<MCAsmBackend>(MAB), OW: MAB->createObjectWriter(OS&: Out),
292 Emitter: std::move(MCE), STI));
293
294 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
295 FunctionPass *Printer =
296 getTarget().createAsmPrinter(TM&: *this, Streamer: std::move(AsmStreamer));
297 if (!Printer)
298 return true;
299
300 PM.add(P: Printer);
301 PM.add(P: createFreeMachineFunctionPass());
302
303 return false; // success!
304}
305