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