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