LLVM 24.0.0git
IRTranslator.h
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/IRTranslator.h - IRTranslator ----*- C++ -*-===//
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/// \file
9/// This file declares the IRTranslator pass.
10/// This pass is responsible for translating LLVM IR into MachineInstr.
11/// It uses target hooks to lower the ABI but aside from that, the pass
12/// generated code is generic. This is the default translator used for
13/// GlobalISel.
14///
15/// \todo Replace the comments with actual doxygen comments.
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H
19#define LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H
20
21#include "llvm/ADT/DenseMap.h"
31#include <memory>
32#include <utility>
33
34namespace llvm {
35
36class AllocaInst;
37class AssumptionCache;
38class BasicBlock;
39class CallInst;
40class CallLowering;
41class Constant;
43class DataLayout;
44class DbgDeclareInst;
45class DbgValueInst;
46class Instruction;
48class MachineFunction;
49class MachineInstr;
52class PHINode;
55class User;
56class Value;
57
58// Technically the pass should run on an hypothetical MachineModule,
59// since it should translate Global into some sort of MachineGlobal.
60// The MachineGlobal should ultimately just be a transfer of ownership of
61// the interesting bits that are relevant to represent a global value.
62// That being said, we could investigate what would it cost to just duplicate
63// the information from the LLVM IR.
64// The idea is that ultimately we would be able to free up the memory used
65// by the LLVM IR as soon as the translation is over.
67public:
68 static char ID;
69
70private:
71 /// Interface used to lower the everything related to calls.
72 const CallLowering *CLI = nullptr;
73
74 /// This class contains the mapping between the Values to vreg related data.
75 class ValueToVRegInfo {
76 public:
77 ValueToVRegInfo() = default;
78
79 using VRegListT = SmallVector<Register, 1>;
80 using OffsetListT = SmallVector<uint64_t, 1>;
81
82 using const_vreg_iterator =
84 using const_offset_iterator =
86
87 inline const_vreg_iterator vregs_end() const { return ValToVRegs.end(); }
88
89 VRegListT *getVRegs(const Value &V) {
90 auto It = ValToVRegs.find(&V);
91 if (It != ValToVRegs.end())
92 return It->second;
93
94 return insertVRegs(V);
95 }
96
97 OffsetListT *getOffsets(const Value &V) {
98 auto It = TypeToOffsets.find(V.getType());
99 if (It != TypeToOffsets.end())
100 return It->second;
101
102 return insertOffsets(V);
103 }
104
105 const_vreg_iterator findVRegs(const Value &V) const {
106 return ValToVRegs.find(&V);
107 }
108
109 bool contains(const Value &V) const { return ValToVRegs.contains(&V); }
110
111 void reset() {
112 ValToVRegs.clear();
113 TypeToOffsets.clear();
114 VRegAlloc.DestroyAll();
115 OffsetAlloc.DestroyAll();
116 }
117
118 private:
119 VRegListT *insertVRegs(const Value &V) {
120 assert(!ValToVRegs.contains(&V) && "Value already exists");
121
122 // We placement new using our fast allocator since we never try to free
123 // the vectors until translation is finished.
124 auto *VRegList = new (VRegAlloc.Allocate()) VRegListT();
125 ValToVRegs[&V] = VRegList;
126 return VRegList;
127 }
128
129 OffsetListT *insertOffsets(const Value &V) {
130 assert(!TypeToOffsets.contains(V.getType()) && "Type already exists");
131
132 auto *OffsetList = new (OffsetAlloc.Allocate()) OffsetListT();
133 TypeToOffsets[V.getType()] = OffsetList;
134 return OffsetList;
135 }
138
139 // We store pointers to vectors here since references may be invalidated
140 // while we hold them if we stored the vectors directly.
143 };
144
145 /// Mapping of the values of the current LLVM IR function to the related
146 /// virtual registers and offsets.
147 ValueToVRegInfo VMap;
148
149 // One BasicBlock can be translated to multiple MachineBasicBlocks. For such
150 // BasicBlocks translated to multiple MachineBasicBlocks, MachinePreds retains
151 // a mapping between the edges arriving at the BasicBlock to the corresponding
152 // created MachineBasicBlocks. Some BasicBlocks that get translated to a
153 // single MachineBasicBlock may also end up in this Map.
154 using CFGEdge = std::pair<const BasicBlock *, const BasicBlock *>;
156
157 // List of stubbed PHI instructions, for values and basic blocks to be filled
158 // in once all MachineBasicBlocks have been created.
160 PendingPHIs;
161
162 /// Record of what frame index has been allocated to specified allocas for
163 /// this function.
165
166 SwiftErrorValueTracking SwiftError;
167
168 /// \name Methods for translating form LLVM IR to MachineInstr.
169 /// \see ::translate for general information on the translate methods.
170 /// @{
171
172 /// Translate \p Inst into its corresponding MachineInstr instruction(s).
173 /// Insert the newly translated instruction(s) right where the CurBuilder
174 /// is set.
175 ///
176 /// The general algorithm is:
177 /// 1. Look for a virtual register for each operand or
178 /// create one.
179 /// 2 Update the VMap accordingly.
180 /// 2.alt. For constant arguments, if they are compile time constants,
181 /// produce an immediate in the right operand and do not touch
182 /// ValToReg. Actually we will go with a virtual register for each
183 /// constants because it may be expensive to actually materialize the
184 /// constant. Moreover, if the constant spans on several instructions,
185 /// CSE may not catch them.
186 /// => Update ValToVReg and remember that we saw a constant in Constants.
187 /// We will materialize all the constants in finalize.
188 /// Note: we would need to do something so that we can recognize such operand
189 /// as constants.
190 /// 3. Create the generic instruction.
191 ///
192 /// \return true if the translation succeeded.
193 bool translate(const Instruction &Inst);
194
195 /// Materialize \p C into virtual-register \p Reg. The generic instructions
196 /// performing this materialization will be inserted into the entry block of
197 /// the function.
198 ///
199 /// \return true if the materialization succeeded.
200 bool translate(const Constant &C, Register Reg);
201
202 /// Examine any debug-info attached to the instruction (in the form of
203 /// DbgRecords) and translate it.
204 void translateDbgInfo(const Instruction &Inst,
205 MachineIRBuilder &MIRBuilder);
206
207 /// Translate a debug-info record of a dbg.value into a DBG_* instruction.
208 /// Pass in all the contents of the record, rather than relying on how it's
209 /// stored.
210 void translateDbgValueRecord(Value *V, bool HasArgList,
211 const DILocalVariable *Variable,
212 const DIExpression *Expression, const DebugLoc &DL,
213 MachineIRBuilder &MIRBuilder);
214
215 /// Translate a debug-info record of a dbg.declare into an indirect DBG_*
216 /// instruction. Pass in all the contents of the record, rather than relying
217 /// on how it's stored.
218 void translateDbgDeclareRecord(Value *Address, bool HasArgList,
219 const DILocalVariable *Variable,
220 const DIExpression *Expression, const DebugLoc &DL,
221 MachineIRBuilder &MIRBuilder);
222
223 // Translate U as a copy of V.
224 bool translateCopy(const User &U, const Value &V,
225 MachineIRBuilder &MIRBuilder);
226
227 /// Translate an LLVM bitcast into generic IR. Either a COPY or a G_BITCAST is
228 /// emitted.
229 bool translateBitCast(const User &U, MachineIRBuilder &MIRBuilder);
230
231 /// Translate an LLVM load instruction into generic IR.
232 bool translateLoad(const User &U, MachineIRBuilder &MIRBuilder);
233
234 /// Translate an LLVM store instruction into generic IR.
235 bool translateStore(const User &U, MachineIRBuilder &MIRBuilder);
236
237 /// Translate an LLVM string intrinsic (memcpy, memset, ...).
238 bool translateMemFunc(const CallInst &CI, MachineIRBuilder &MIRBuilder,
239 unsigned Opcode);
240
241 /// Translate an LLVM trap intrinsic (trap, debugtrap, ubsantrap).
242 bool translateTrap(const CallInst &U, MachineIRBuilder &MIRBuilder,
243 unsigned Opcode);
244
245 // Translate @llvm.vector.interleave2 and
246 // @llvm.vector.deinterleave2 intrinsics for fixed-width vector
247 // types into vector shuffles.
248 bool translateVectorInterleave2Intrinsic(const CallInst &CI,
249 MachineIRBuilder &MIRBuilder);
250 bool translateVectorDeinterleave2Intrinsic(const CallInst &CI,
251 MachineIRBuilder &MIRBuilder);
252
253 void getStackGuard(Register DstReg, MachineIRBuilder &MIRBuilder);
254
255 bool translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
256 MachineIRBuilder &MIRBuilder);
257 bool translateFixedPointIntrinsic(unsigned Op, const CallInst &CI,
258 MachineIRBuilder &MIRBuilder);
259
260 /// Helper function for translateSimpleIntrinsic.
261 /// \return The generic opcode for \p IntrinsicID if \p IntrinsicID is a
262 /// simple intrinsic (ceil, fabs, etc.). Otherwise, returns
263 /// Intrinsic::not_intrinsic.
264 unsigned getSimpleIntrinsicOpcode(Intrinsic::ID ID);
265
266 /// Translates the intrinsics defined in getSimpleIntrinsicOpcode.
267 /// \return true if the translation succeeded.
268 bool translateSimpleIntrinsic(const CallInst &CI, Intrinsic::ID ID,
269 MachineIRBuilder &MIRBuilder);
270
271 bool translateConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI,
272 MachineIRBuilder &MIRBuilder);
273
274 bool translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
275 MachineIRBuilder &MIRBuilder);
276
277 /// Returns the single livein physical register Arg was lowered to, if
278 /// possible.
279 std::optional<MCRegister> getArgPhysReg(Argument &Arg);
280
281 /// If debug-info targets an Argument and its expression is an EntryValue,
282 /// lower it as either an entry in the MF debug table (dbg.declare), or a
283 /// DBG_VALUE targeting the corresponding livein register for that Argument
284 /// (dbg.value).
285 bool translateIfEntryValueArgument(bool isDeclare, Value *Arg,
286 const DILocalVariable *Var,
287 const DIExpression *Expr,
288 const DebugLoc &DL,
289 MachineIRBuilder &MIRBuilder);
290
291 bool translateInlineAsm(const CallBase &CB, MachineIRBuilder &MIRBuilder);
292
293 /// Common code for translating normal calls or invokes.
294 bool translateCallBase(const CallBase &CB, MachineIRBuilder &MIRBuilder);
295
296 /// Translate call instruction.
297 /// \pre \p U is a call instruction.
298 bool translateCall(const User &U, MachineIRBuilder &MIRBuilder);
299
300 bool translateIntrinsic(
301 const CallBase &CB, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder,
302 ArrayRef<TargetLowering::IntrinsicInfo> TgtMemIntrinsicInfos = {});
303
304 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
305 /// many places it could ultimately go. In the IR, we have a single unwind
306 /// destination, but in the machine CFG, we enumerate all the possible blocks.
307 /// This function skips over imaginary basic blocks that hold catchswitch
308 /// instructions, and finds all the "real" machine
309 /// basic block destinations. As those destinations may not be successors of
310 /// EHPadBB, here we also calculate the edge probability to those
311 /// destinations. The passed-in Prob is the edge probability to EHPadBB.
313 const BasicBlock *EHPadBB, BranchProbability Prob,
314 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
315 &UnwindDests);
316
317 bool translateInvoke(const User &U, MachineIRBuilder &MIRBuilder);
318
319 bool translateCallBr(const User &U, MachineIRBuilder &MIRBuilder);
320
321 bool translateLandingPad(const User &U, MachineIRBuilder &MIRBuilder);
322
323 /// Translate one of LLVM's cast instructions into MachineInstrs, with the
324 /// given generic Opcode.
325 bool translateCast(unsigned Opcode, const User &U,
326 MachineIRBuilder &MIRBuilder);
327
328 /// Translate a phi instruction.
329 bool translatePHI(const User &U, MachineIRBuilder &MIRBuilder);
330
331 /// Translate a comparison (icmp or fcmp) instruction or constant.
332 bool translateCompare(const User &U, MachineIRBuilder &MIRBuilder);
333
334 /// Translate an integer compare instruction (or constant).
335 bool translateICmp(const User &U, MachineIRBuilder &MIRBuilder) {
336 return translateCompare(U, MIRBuilder);
337 }
338
339 /// Translate a floating-point compare instruction (or constant).
340 bool translateFCmp(const User &U, MachineIRBuilder &MIRBuilder) {
341 return translateCompare(U, MIRBuilder);
342 }
343
344 /// Add remaining operands onto phis we've translated. Executed after all
345 /// MachineBasicBlocks for the function have been created.
346 void finishPendingPhis();
347
348 /// Translate \p Inst into a unary operation \p Opcode.
349 /// \pre \p U is a unary operation.
350 bool translateUnaryOp(unsigned Opcode, const User &U,
351 MachineIRBuilder &MIRBuilder);
352
353 /// Translate \p Inst into a binary operation \p Opcode.
354 /// \pre \p U is a binary operation.
355 bool translateBinaryOp(unsigned Opcode, const User &U,
356 MachineIRBuilder &MIRBuilder);
357
358 /// If the set of cases should be emitted as a series of branches, return
359 /// true. If we should emit this as a bunch of and/or'd together conditions,
360 /// return false.
361 bool shouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases);
362 /// Helper method for findMergedConditions.
363 /// This function emits a branch and is used at the leaves of an OR or an
364 /// AND operator tree.
365 void emitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
366 MachineBasicBlock *FBB,
367 MachineBasicBlock *CurBB,
368 MachineBasicBlock *SwitchBB,
369 BranchProbability TProb,
370 BranchProbability FProb, bool InvertCond);
371 /// Used during condbr translation to find trees of conditions that can be
372 /// optimized.
373 void findMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
374 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
375 MachineBasicBlock *SwitchBB,
376 Instruction::BinaryOps Opc, BranchProbability TProb,
377 BranchProbability FProb, bool InvertCond);
378
379 /// Translate branch (br) instruction.
380 /// \pre \p U is a branch instruction.
381 bool translateUncondBr(const User &U, MachineIRBuilder &MIRBuilder);
382 bool translateCondBr(const User &U, MachineIRBuilder &MIRBuilder);
383
384 // Begin switch lowering functions.
385 bool emitJumpTableHeader(SwitchCG::JumpTable &JT,
386 SwitchCG::JumpTableHeader &JTH,
387 MachineBasicBlock *HeaderBB);
388 void emitJumpTable(SwitchCG::JumpTable &JT, MachineBasicBlock *MBB);
389
390 void emitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB,
391 MachineIRBuilder &MIB);
392
393 /// Generate for the BitTest header block, which precedes each sequence of
394 /// BitTestCases.
395 void emitBitTestHeader(SwitchCG::BitTestBlock &BTB,
396 MachineBasicBlock *SwitchMBB);
397 /// Generate code to produces one "bit test" for a given BitTestCase \p B.
398 void emitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB,
399 BranchProbability BranchProbToNext, Register Reg,
400 SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB);
401
402 void splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
403 const SwitchCG::SwitchWorkListItem &W, Value *Cond,
404 MachineBasicBlock *SwitchMBB, MachineIRBuilder &MIB);
405
406 bool lowerJumpTableWorkItem(
407 SwitchCG::SwitchWorkListItem W, MachineBasicBlock *SwitchMBB,
408 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
409 MachineIRBuilder &MIB, MachineFunction::iterator BBI,
410 BranchProbability UnhandledProbs, SwitchCG::CaseClusterIt I,
411 MachineBasicBlock *Fallthrough, bool FallthroughUnreachable);
412
413 bool lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I, Value *Cond,
414 MachineBasicBlock *Fallthrough,
415 bool FallthroughUnreachable,
416 BranchProbability UnhandledProbs,
417 MachineBasicBlock *CurMBB,
418 MachineIRBuilder &MIB,
419 MachineBasicBlock *SwitchMBB);
420
421 bool lowerBitTestWorkItem(
422 SwitchCG::SwitchWorkListItem W, MachineBasicBlock *SwitchMBB,
423 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
424 MachineIRBuilder &MIB, MachineFunction::iterator BBI,
425 BranchProbability DefaultProb, BranchProbability UnhandledProbs,
426 SwitchCG::CaseClusterIt I, MachineBasicBlock *Fallthrough,
427 bool FallthroughUnreachable);
428
429 bool lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond,
430 MachineBasicBlock *SwitchMBB,
431 MachineBasicBlock *DefaultMBB,
432 MachineIRBuilder &MIB);
433
434 bool translateSwitch(const User &U, MachineIRBuilder &MIRBuilder);
435 // End switch lowering section.
436
437 bool translateIndirectBr(const User &U, MachineIRBuilder &MIRBuilder);
438
439 bool translateExtractValue(const User &U, MachineIRBuilder &MIRBuilder);
440
441 bool translateInsertValue(const User &U, MachineIRBuilder &MIRBuilder);
442
443 bool translateSelect(const User &U, MachineIRBuilder &MIRBuilder);
444
445 bool translateGetElementPtr(const User &U, MachineIRBuilder &MIRBuilder);
446
447 bool translateAlloca(const User &U, MachineIRBuilder &MIRBuilder);
448
449 /// Translate return (ret) instruction.
450 /// The target needs to implement CallLowering::lowerReturn for
451 /// this to succeed.
452 /// \pre \p U is a return instruction.
453 bool translateRet(const User &U, MachineIRBuilder &MIRBuilder);
454
455 bool translateFNeg(const User &U, MachineIRBuilder &MIRBuilder);
456
457 bool translateAdd(const User &U, MachineIRBuilder &MIRBuilder) {
458 return translateBinaryOp(TargetOpcode::G_ADD, U, MIRBuilder);
459 }
460 bool translateSub(const User &U, MachineIRBuilder &MIRBuilder) {
461 return translateBinaryOp(TargetOpcode::G_SUB, U, MIRBuilder);
462 }
463 bool translateAnd(const User &U, MachineIRBuilder &MIRBuilder) {
464 return translateBinaryOp(TargetOpcode::G_AND, U, MIRBuilder);
465 }
466 bool translateMul(const User &U, MachineIRBuilder &MIRBuilder) {
467 return translateBinaryOp(TargetOpcode::G_MUL, U, MIRBuilder);
468 }
469 bool translateOr(const User &U, MachineIRBuilder &MIRBuilder) {
470 return translateBinaryOp(TargetOpcode::G_OR, U, MIRBuilder);
471 }
472 bool translateXor(const User &U, MachineIRBuilder &MIRBuilder) {
473 return translateBinaryOp(TargetOpcode::G_XOR, U, MIRBuilder);
474 }
475
476 bool translateUDiv(const User &U, MachineIRBuilder &MIRBuilder) {
477 return translateBinaryOp(TargetOpcode::G_UDIV, U, MIRBuilder);
478 }
479 bool translateSDiv(const User &U, MachineIRBuilder &MIRBuilder) {
480 return translateBinaryOp(TargetOpcode::G_SDIV, U, MIRBuilder);
481 }
482 bool translateURem(const User &U, MachineIRBuilder &MIRBuilder) {
483 return translateBinaryOp(TargetOpcode::G_UREM, U, MIRBuilder);
484 }
485 bool translateSRem(const User &U, MachineIRBuilder &MIRBuilder) {
486 return translateBinaryOp(TargetOpcode::G_SREM, U, MIRBuilder);
487 }
488 bool translateIntToPtr(const User &U, MachineIRBuilder &MIRBuilder) {
489 return translateCast(TargetOpcode::G_INTTOPTR, U, MIRBuilder);
490 }
491 bool translatePtrToInt(const User &U, MachineIRBuilder &MIRBuilder) {
492 return translateCast(TargetOpcode::G_PTRTOINT, U, MIRBuilder);
493 }
494 bool translatePtrToAddr(const User &U, MachineIRBuilder &MIRBuilder) {
495 // FIXME: this is not correct for pointers with addr width != pointer width
496 return translatePtrToInt(U, MIRBuilder);
497 }
498 bool translateTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
499 return translateCast(TargetOpcode::G_TRUNC, U, MIRBuilder);
500 }
501 bool translateFPTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
502 return translateCast(TargetOpcode::G_FPTRUNC, U, MIRBuilder);
503 }
504 bool translateFPExt(const User &U, MachineIRBuilder &MIRBuilder) {
505 return translateCast(TargetOpcode::G_FPEXT, U, MIRBuilder);
506 }
507 bool translateFPToUI(const User &U, MachineIRBuilder &MIRBuilder) {
508 return translateCast(TargetOpcode::G_FPTOUI, U, MIRBuilder);
509 }
510 bool translateFPToSI(const User &U, MachineIRBuilder &MIRBuilder) {
511 return translateCast(TargetOpcode::G_FPTOSI, U, MIRBuilder);
512 }
513 bool translateUIToFP(const User &U, MachineIRBuilder &MIRBuilder) {
514 return translateCast(TargetOpcode::G_UITOFP, U, MIRBuilder);
515 }
516 bool translateSIToFP(const User &U, MachineIRBuilder &MIRBuilder) {
517 return translateCast(TargetOpcode::G_SITOFP, U, MIRBuilder);
518 }
519 bool translateUnreachable(const User &U, MachineIRBuilder &MIRBuilder);
520
521 bool translateSExt(const User &U, MachineIRBuilder &MIRBuilder) {
522 return translateCast(TargetOpcode::G_SEXT, U, MIRBuilder);
523 }
524
525 bool translateZExt(const User &U, MachineIRBuilder &MIRBuilder) {
526 return translateCast(TargetOpcode::G_ZEXT, U, MIRBuilder);
527 }
528
529 bool translateShl(const User &U, MachineIRBuilder &MIRBuilder) {
530 return translateBinaryOp(TargetOpcode::G_SHL, U, MIRBuilder);
531 }
532 bool translateLShr(const User &U, MachineIRBuilder &MIRBuilder) {
533 return translateBinaryOp(TargetOpcode::G_LSHR, U, MIRBuilder);
534 }
535 bool translateAShr(const User &U, MachineIRBuilder &MIRBuilder) {
536 return translateBinaryOp(TargetOpcode::G_ASHR, U, MIRBuilder);
537 }
538
539 bool translateFAdd(const User &U, MachineIRBuilder &MIRBuilder) {
540 return translateBinaryOp(TargetOpcode::G_FADD, U, MIRBuilder);
541 }
542 bool translateFSub(const User &U, MachineIRBuilder &MIRBuilder) {
543 return translateBinaryOp(TargetOpcode::G_FSUB, U, MIRBuilder);
544 }
545 bool translateFMul(const User &U, MachineIRBuilder &MIRBuilder) {
546 return translateBinaryOp(TargetOpcode::G_FMUL, U, MIRBuilder);
547 }
548 bool translateFDiv(const User &U, MachineIRBuilder &MIRBuilder) {
549 return translateBinaryOp(TargetOpcode::G_FDIV, U, MIRBuilder);
550 }
551 bool translateFRem(const User &U, MachineIRBuilder &MIRBuilder) {
552 return translateBinaryOp(TargetOpcode::G_FREM, U, MIRBuilder);
553 }
554
555 bool translateVAArg(const User &U, MachineIRBuilder &MIRBuilder);
556
557 bool translateInsertElement(const User &U, MachineIRBuilder &MIRBuilder);
558 bool translateInsertVector(const User &U, MachineIRBuilder &MIRBuilder);
559
560 bool translateExtractElement(const User &U, MachineIRBuilder &MIRBuilder);
561 bool translateExtractVector(const User &U, MachineIRBuilder &MIRBuilder);
562
563 bool translateShuffleVector(const User &U, MachineIRBuilder &MIRBuilder);
564
565 bool translateAtomicCmpXchg(const User &U, MachineIRBuilder &MIRBuilder);
566 bool translateAtomicRMW(const User &U, MachineIRBuilder &MIRBuilder);
567 bool translateFence(const User &U, MachineIRBuilder &MIRBuilder);
568 bool translateFreeze(const User &U, MachineIRBuilder &MIRBuilder);
569
570 // Stubs to keep the compiler happy while we implement the rest of the
571 // translation.
572 bool translateResume(const User &U, MachineIRBuilder &MIRBuilder) {
573 return false;
574 }
575 bool translateCleanupRet(const User &U, MachineIRBuilder &MIRBuilder) {
576 return false;
577 }
578 bool translateCatchRet(const User &U, MachineIRBuilder &MIRBuilder) {
579 return false;
580 }
581 bool translateCatchSwitch(const User &U, MachineIRBuilder &MIRBuilder) {
582 return false;
583 }
584 bool translateAddrSpaceCast(const User &U, MachineIRBuilder &MIRBuilder) {
585 return translateCast(TargetOpcode::G_ADDRSPACE_CAST, U, MIRBuilder);
586 }
587 bool translateCleanupPad(const User &U, MachineIRBuilder &MIRBuilder) {
588 return false;
589 }
590 bool translateCatchPad(const User &U, MachineIRBuilder &MIRBuilder) {
591 return false;
592 }
593 bool translateUserOp1(const User &U, MachineIRBuilder &MIRBuilder) {
594 return false;
595 }
596 bool translateUserOp2(const User &U, MachineIRBuilder &MIRBuilder) {
597 return false;
598 }
599
600 bool translateConvergenceControlIntrinsic(const CallInst &CI,
601 Intrinsic::ID ID,
602 MachineIRBuilder &MIRBuilder);
603
604 /// @}
605
606 // Builder for machine instruction a la IRBuilder.
607 // I.e., compared to regular MIBuilder, this one also inserts the instruction
608 // in the current block, it can creates block, etc., basically a kind of
609 // IRBuilder, but for Machine IR.
610 // CSEMIRBuilder CurBuilder;
611 std::unique_ptr<MachineIRBuilder> CurBuilder;
612
613 // Builder set to the entry block (just after ABI lowering instructions). Used
614 // as a convenient location for Constants.
615 // CSEMIRBuilder EntryBuilder;
616 std::unique_ptr<MachineIRBuilder> EntryBuilder;
617
618 // The MachineFunction currently being translated.
619 MachineFunction *MF = nullptr;
620
621 /// MachineRegisterInfo used to create virtual registers.
622 MachineRegisterInfo *MRI = nullptr;
623
624 const DataLayout *DL = nullptr;
625
626 /// Current target configuration. Controls how the pass handles errors.
627 const TargetPassConfig *TPC = nullptr;
628
629 CodeGenOptLevel OptLevel;
630
631 /// Current optimization remark emitter. Used to report failures.
632 std::unique_ptr<OptimizationRemarkEmitter> ORE;
633
634 AAResults *AA = nullptr;
635 AssumptionCache *AC = nullptr;
636 const TargetLibraryInfo *LibInfo = nullptr;
637 const LibcallLoweringInfo *Libcalls = nullptr;
638 const TargetLowering *TLI = nullptr;
639 FunctionLoweringInfo FuncInfo;
640
641 // True when either the Target Machine specifies no optimizations or the
642 // function has the optnone attribute.
643 bool EnableOpts = false;
644
645 /// True when the block contains a tail call. This allows the IRTranslator to
646 /// stop translating such blocks early.
647 bool HasTailCall = false;
648
649 StackProtectorDescriptor SPDescriptor;
650
651 bool mayTranslateUserTypes(const User &U) const;
652
653 /// Switch analysis and optimization.
654 class GISelSwitchLowering : public SwitchCG::SwitchLowering {
655 public:
656 GISelSwitchLowering(IRTranslator *irt, FunctionLoweringInfo &funcinfo)
657 : SwitchLowering(funcinfo), IRT(irt) {
658 assert(irt && "irt is null!");
659 }
660
661 void addSuccessorWithProb(
662 MachineBasicBlock *Src, MachineBasicBlock *Dst,
663 BranchProbability Prob = BranchProbability::getUnknown()) override {
664 IRT->addSuccessorWithProb(Src, Dst, Prob);
665 }
666
667 ~GISelSwitchLowering() override = default;
668
669 private:
670 IRTranslator *IRT;
671 };
672
673 std::unique_ptr<GISelSwitchLowering> SL;
674
675 // * Insert all the code needed to materialize the constants
676 // at the proper place. E.g., Entry block or dominator block
677 // of each constant depending on how fancy we want to be.
678 // * Clear the different maps.
679 void finalizeFunction();
680
681 // Processing steps done per block. E.g. emitting jump tables, stack
682 // protectors etc. Returns true if no errors, false if there was a problem
683 // that caused an abort.
684 bool finalizeBasicBlock(const BasicBlock &BB, MachineBasicBlock &MBB);
685
686 /// Codegen a new tail for a stack protector check ParentMBB which has had its
687 /// tail spliced into a stack protector check success bb.
688 ///
689 /// For a high level explanation of how this fits into the stack protector
690 /// generation see the comment on the declaration of class
691 /// StackProtectorDescriptor.
692 ///
693 /// \return true if there were no problems.
694 bool emitSPDescriptorParent(StackProtectorDescriptor &SPD,
695 MachineBasicBlock *ParentBB);
696
697 /// Codegen the failure basic block for a stack protector check.
698 ///
699 /// A failure stack protector machine basic block consists simply of a call to
700 /// __stack_chk_fail().
701 ///
702 /// For a high level explanation of how this fits into the stack protector
703 /// generation see the comment on the declaration of class
704 /// StackProtectorDescriptor.
705 ///
706 /// \return true if there were no problems.
707 bool emitSPDescriptorFailure(StackProtectorDescriptor &SPD,
708 MachineBasicBlock *FailureBB);
709
710 /// Get the VRegs that represent \p Val.
711 /// Non-aggregate types have just one corresponding VReg and the list can be
712 /// used as a single "unsigned". Aggregates get flattened. If such VRegs do
713 /// not exist, they are created.
714 ArrayRef<Register> getOrCreateVRegs(const Value &Val);
715
716 Register getOrCreateVReg(const Value &Val) {
717 auto Regs = getOrCreateVRegs(Val);
718 if (Regs.empty())
719 return 0;
720 assert(Regs.size() == 1 &&
721 "attempt to get single VReg for aggregate or void");
722 return Regs[0];
723 }
724
725 Register getOrCreateConvergenceTokenVReg(const Value &Token) {
726 assert(Token.getType()->isTokenTy());
727 auto &Regs = *VMap.getVRegs(Token);
728 if (!Regs.empty()) {
729 assert(Regs.size() == 1 &&
730 "Expected a single register for convergence tokens.");
731 return Regs[0];
732 }
733
734 auto Reg = MRI->createGenericVirtualRegister(LLT::token());
735 Regs.push_back(Reg);
736 auto &Offsets = *VMap.getOffsets(Token);
737 if (Offsets.empty())
738 Offsets.push_back(0);
739 return Reg;
740 }
741
742 /// Allocate some vregs and offsets in the VMap. Then populate just the
743 /// offsets while leaving the vregs empty.
744 ValueToVRegInfo::VRegListT &allocateVRegs(const Value &Val);
745
746 /// Get the frame index that represents \p Val.
747 /// If such VReg does not exist, it is created.
748 int getOrCreateFrameIndex(const AllocaInst &AI);
749
750 /// Get the alignment of the given memory operation instruction. This will
751 /// either be the explicitly specified value or the ABI-required alignment for
752 /// the type being accessed (according to the Module's DataLayout).
753 Align getMemOpAlign(const Instruction &I);
754
755 /// Get the MachineBasicBlock that represents \p BB. Specifically, the block
756 /// returned will be the head of the translated block (suitable for branch
757 /// destinations).
758 MachineBasicBlock &getMBB(const BasicBlock &BB);
759
760 /// Record \p NewPred as a Machine predecessor to `Edge.second`, corresponding
761 /// to `Edge.first` at the IR level. This is used when IRTranslation creates
762 /// multiple MachineBasicBlocks for a given IR block and the CFG is no longer
763 /// represented simply by the IR-level CFG.
764 void addMachineCFGPred(CFGEdge Edge, MachineBasicBlock *NewPred);
765
766 /// Returns the Machine IR predecessors for the given IR CFG edge. Usually
767 /// this is just the single MachineBasicBlock corresponding to the predecessor
768 /// in the IR. More complex lowering can result in multiple MachineBasicBlocks
769 /// preceding the original though (e.g. switch instructions).
770 SmallVector<MachineBasicBlock *, 1> getMachinePredBBs(CFGEdge Edge) {
771 auto RemappedEdge = MachinePreds.find(Edge);
772 if (RemappedEdge != MachinePreds.end())
773 return RemappedEdge->second;
774 return SmallVector<MachineBasicBlock *, 4>(1, &getMBB(*Edge.first));
775 }
776
777 /// Return branch probability calculated by BranchProbabilityInfo for IR
778 /// blocks.
779 BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
780 const MachineBasicBlock *Dst) const;
781
782 void addSuccessorWithProb(
783 MachineBasicBlock *Src, MachineBasicBlock *Dst,
784 BranchProbability Prob = BranchProbability::getUnknown());
785
786public:
787 IRTranslator(CodeGenOptLevel OptLevel = CodeGenOptLevel::None);
788
789 StringRef getPassName() const override { return "IRTranslator"; }
790
791 void getAnalysisUsage(AnalysisUsage &AU) const override;
792
793 // Algo:
794 // CallLowering = MF.subtarget.getCallLowering()
795 // F = MF.getParent()
796 // MIRBuilder.reset(MF)
797 // getMBB(F.getEntryBB())
798 // CallLowering->translateArguments(MIRBuilder, F, ValToVReg)
799 // for each bb in F
800 // getMBB(bb)
801 // for each inst in bb
802 // if (!translate(MIRBuilder, inst, ValToVReg, ConstantToSequence))
803 // reportFatalUsageError("Don't know how to translate input");
804 // finalize()
805 bool runOnMachineFunction(MachineFunction &MF) override;
806};
807
808} // end namespace llvm
809
810#endif // LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
std::pair< BasicBlock *, BasicBlock * > Edge
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
This file defines the SmallVector class.
static Value * getStackGuard(const TargetLoweringBase &TLI, const LibcallLoweringInfo &Libcalls, Module *M, IRBuilder<> &B, bool *SupportsSelectionDAGSP=nullptr)
Create a stack guard loading and populate whether SelectionDAG SSP is supported.
an instruction to allocate memory on the stack
Represent the analysis usage information of a pass.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
DWARF expression.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
This represents the llvm.dbg.declare instruction.
This represents the llvm.dbg.value instruction.
A debug info location.
Definition DebugLoc.h:126
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:134
iterator end()
Definition DenseMap.h:141
Class representing an expression and its matching format.
IRTranslator(CodeGenOptLevel OptLevel=CodeGenOptLevel::None)
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
The optimization diagnostic interface.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
Definition Allocator.h:397
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Provides information about what library functions are available for the current target.
Target-Independent Code Generator Pass Configuration Options.
LLVM Value Representation.
Definition Value.h:75
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Offsets
Offsets in bytes from the start of the input buffer.
This is an optimization pass for GlobalISel generic memory operations.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:149
DWARFExpression::Operation Op