LLVM 24.0.0git
IRTranslator.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/IRTranslator.cpp - 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 implements the IRTranslator class.
10//===----------------------------------------------------------------------===//
11
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/ScopeExit.h"
20#include "llvm/Analysis/Loads.h"
55#include "llvm/IR/Analysis.h"
56#include "llvm/IR/BasicBlock.h"
57#include "llvm/IR/CFG.h"
58#include "llvm/IR/Constant.h"
59#include "llvm/IR/Constants.h"
60#include "llvm/IR/DataLayout.h"
63#include "llvm/IR/Function.h"
65#include "llvm/IR/InlineAsm.h"
66#include "llvm/IR/InstrTypes.h"
69#include "llvm/IR/Intrinsics.h"
70#include "llvm/IR/IntrinsicsAMDGPU.h"
71#include "llvm/IR/LLVMContext.h"
72#include "llvm/IR/Metadata.h"
73#include "llvm/IR/Module.h"
75#include "llvm/IR/Statepoint.h"
76#include "llvm/IR/Type.h"
77#include "llvm/IR/User.h"
78#include "llvm/IR/Value.h"
80#include "llvm/MC/MCContext.h"
81#include "llvm/Pass.h"
84#include "llvm/Support/Debug.h"
91#include <algorithm>
92#include <cassert>
93#include <cstdint>
94#include <iterator>
95#include <optional>
96#include <string>
97#include <utility>
98#include <vector>
99
100#define DEBUG_TYPE "ir-translator"
101
102using namespace llvm;
103
104static cl::opt<bool>
105 EnableCSEInIRTranslator("enable-cse-in-ir-translator",
106 cl::desc("Should enable CSE in ir-translator"),
107 cl::init(false));
108
109namespace llvm {
110
112 /// Interface used to lower the everything related to calls.
113 const CallLowering *CLI = nullptr;
114
115 SSPLayoutInfo *SPInfo = nullptr;
116
117 /// This class contains the mapping between the Values to vreg related data.
118 class ValueToVRegInfo {
119 public:
120 ValueToVRegInfo() = default;
121
122 using VRegListT = SmallVector<Register, 1>;
123 using OffsetListT = SmallVector<uint64_t, 1>;
124
125 using const_vreg_iterator =
127 using const_offset_iterator =
129
130 inline const_vreg_iterator vregs_end() const { return ValToVRegs.end(); }
131
132 VRegListT *getVRegs(const Value &V) {
133 auto [It, Inserted] = ValToVRegs.try_emplace(&V);
134 if (!Inserted)
135 return It->second;
136
137 // We placement new using our fast allocator since we never try to free
138 // the vectors until translation is finished.
139 It->second = new (VRegAlloc.Allocate()) VRegListT();
140 return It->second;
141 }
142
143 OffsetListT *getOffsets(const Value &V) {
144 assert(V.getType()->isAggregateType() &&
145 "Offsets are for aggregate values");
146 auto [It, Inserted] = TypeToOffsets.try_emplace(V.getType());
147 if (!Inserted)
148 return It->second;
149
150 It->second = new (OffsetAlloc.Allocate()) OffsetListT();
151 return It->second;
152 }
153
154 const_vreg_iterator findVRegs(const Value &V) const {
155 return ValToVRegs.find(&V);
156 }
157
158 bool contains(const Value &V) const { return ValToVRegs.contains(&V); }
159
160 void reserveVRegs(unsigned NumValues) { ValToVRegs.reserve(NumValues); }
161
162 void reset() {
163 ValToVRegs.clear();
164 TypeToOffsets.clear();
165 VRegAlloc.DestroyAll();
166 OffsetAlloc.DestroyAll();
167 }
168
169 private:
172
173 // We store pointers to vectors here since references may be invalidated
174 // while we hold them if we stored the vectors directly.
177 };
178
179 /// Mapping of the values of the current LLVM IR function to the related
180 /// virtual registers and offsets.
181 ValueToVRegInfo VMap;
182
183 // One BasicBlock can be translated to multiple MachineBasicBlocks. For such
184 // BasicBlocks translated to multiple MachineBasicBlocks, MachinePreds retains
185 // a mapping between the edges arriving at the BasicBlock to the corresponding
186 // created MachineBasicBlocks. Some BasicBlocks that get translated to a
187 // single MachineBasicBlock may also end up in this Map.
188 using CFGEdge = std::pair<const BasicBlock *, const BasicBlock *>;
190
191 // List of stubbed PHI instructions, for values and basic blocks to be filled
192 // in once all MachineBasicBlocks have been created.
194 PendingPHIs;
195
196 // Constant DbgValues that cannot be materialized as DBG_VALUEs
197 // because they are not an immediate or symbol reference.
199 PendingConstDbgValues;
200
201 /// Record of what frame index has been allocated to specified allocas for
202 /// this function.
204
205 SwiftErrorValueTracking SwiftError;
206
207 /// \name Methods for translating form LLVM IR to MachineInstr.
208 /// \see ::translate for general information on the translate methods.
209 /// @{
210
211 /// Translate \p Inst into its corresponding MachineInstr instruction(s).
212 /// Insert the newly translated instruction(s) right where the CurBuilder
213 /// is set.
214 ///
215 /// The general algorithm is:
216 /// 1. Look for a virtual register for each operand or
217 /// create one.
218 /// 2 Update the VMap accordingly.
219 /// 2.alt. For constant arguments, if they are compile time constants,
220 /// produce an immediate in the right operand and do not touch
221 /// ValToReg. Actually we will go with a virtual register for each
222 /// constants because it may be expensive to actually materialize the
223 /// constant. Moreover, if the constant spans on several instructions,
224 /// CSE may not catch them.
225 /// => Update ValToVReg and remember that we saw a constant in Constants.
226 /// We will materialize all the constants in finalize.
227 /// Note: we would need to do something so that we can recognize such operand
228 /// as constants.
229 /// 3. Create the generic instruction.
230 ///
231 /// \return true if the translation succeeded.
232 bool translate(const Instruction &Inst);
233
234 /// Materialize \p C into virtual-register \p Reg. The generic instructions
235 /// performing this materialization will be inserted into the entry block of
236 /// the function.
237 ///
238 /// \return true if the materialization succeeded.
239 bool translate(const Constant &C, Register Reg);
240
241 /// Examine any debug-info attached to the instruction (in the form of
242 /// DbgRecords) and translate it.
243 void translateDbgInfo(const Instruction &Inst, MachineIRBuilder &MIRBuilder);
244
245 /// Translate a debug-info record of a dbg.value into a DBG_* instruction.
246 /// Pass in all the contents of the record, rather than relying on how it's
247 /// stored.
248 void translateDbgValueRecord(Value *V, bool HasArgList,
249 const DILocalVariable *Variable,
251 const DebugLoc &DL,
252 MachineIRBuilder &MIRBuilder);
253
254 /// Translate a debug-info record of a dbg.declare into an indirect DBG_*
255 /// instruction. Pass in all the contents of the record, rather than relying
256 /// on how it's stored.
257 void translateDbgDeclareRecord(Value *Address, bool HasArgList,
258 const DILocalVariable *Variable,
260 const DebugLoc &DL,
261 MachineIRBuilder &MIRBuilder);
262
263 // Translate U as a copy of V.
264 bool translateCopy(const User &U, const Value &V,
265 MachineIRBuilder &MIRBuilder);
266 bool translateCopy(const User &U, Register Src, MachineIRBuilder &MIRBuilder);
267
268 /// Translate an LLVM bitcast into generic IR. Either a COPY or a G_BITCAST is
269 /// emitted.
270 bool translateBitCast(const User &U, MachineIRBuilder &MIRBuilder);
271
272 /// Translate an LLVM load instruction into generic IR.
273 bool translateLoad(const User &U, MachineIRBuilder &MIRBuilder);
274
275 /// Translate an LLVM store instruction into generic IR.
276 bool translateStore(const User &U, MachineIRBuilder &MIRBuilder);
277
278 /// Translate an LLVM string intrinsic (memcpy, memset, ...).
279 bool translateMemFunc(const CallInst &CI, MachineIRBuilder &MIRBuilder,
280 unsigned Opcode);
281
282 /// Translate an LLVM trap intrinsic (trap, debugtrap, ubsantrap).
283 bool translateTrap(const CallInst &U, MachineIRBuilder &MIRBuilder,
284 unsigned Opcode);
285
286 // Translate @llvm.vector.interleave2 and
287 // @llvm.vector.deinterleave2 intrinsics for fixed-width vector
288 // types into vector shuffles.
289 bool translateVectorInterleave2Intrinsic(const CallInst &CI,
290 MachineIRBuilder &MIRBuilder);
291 bool translateVectorDeinterleave2Intrinsic(const CallInst &CI,
292 MachineIRBuilder &MIRBuilder);
293
294 void getStackGuard(Register DstReg, MachineIRBuilder &MIRBuilder);
295
296 bool translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
297 MachineIRBuilder &MIRBuilder);
298 bool translateFixedPointIntrinsic(unsigned Op, const CallInst &CI,
299 MachineIRBuilder &MIRBuilder);
300
301 /// Helper function for translateSimpleIntrinsic.
302 /// \return The generic opcode for \p IntrinsicID if \p IntrinsicID is a
303 /// simple intrinsic (ceil, fabs, etc.). Otherwise, returns
304 /// Intrinsic::not_intrinsic.
305 unsigned getSimpleIntrinsicOpcode(Intrinsic::ID ID);
306
307 /// Translates the intrinsics defined in getSimpleIntrinsicOpcode.
308 /// \return true if the translation succeeded.
309 bool translateSimpleIntrinsic(const CallInst &CI, Intrinsic::ID ID,
310 MachineIRBuilder &MIRBuilder);
311
312 bool translateConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI,
313 MachineIRBuilder &MIRBuilder);
314
315 bool translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
316 MachineIRBuilder &MIRBuilder);
317
318 /// Returns the single livein physical register Arg was lowered to, if
319 /// possible.
320 std::optional<MCRegister> getArgPhysReg(Argument &Arg);
321
322 /// If debug-info targets an Argument and its expression is an EntryValue,
323 /// lower it as either an entry in the MF debug table (dbg.declare), or a
324 /// DBG_VALUE targeting the corresponding livein register for that Argument
325 /// (dbg.value).
326 bool translateIfEntryValueArgument(bool isDeclare, Value *Arg,
327 const DILocalVariable *Var,
328 const DIExpression *Expr,
329 const DebugLoc &DL,
330 MachineIRBuilder &MIRBuilder);
331
332 bool translateInlineAsm(const CallBase &CB, MachineIRBuilder &MIRBuilder);
333
334 /// Common code for translating normal calls or invokes.
335 bool translateCallBase(const CallBase &CB, MachineIRBuilder &MIRBuilder);
336
337 /// Translate call instruction.
338 /// \pre \p U is a call instruction.
339 bool translateCall(const User &U, MachineIRBuilder &MIRBuilder);
340
341 bool translateIntrinsic(
342 const CallBase &CB, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder,
343 ArrayRef<TargetLowering::IntrinsicInfo> TgtMemIntrinsicInfos = {});
344
345 /// Report an intrinsic the subtarget does not support and define its results
346 /// with G_IMPLICIT_DEF. Prevents creating a malformed MIR.
347 bool handleUnsupportedIntrinsic(const CallBase &CB, Intrinsic::ID ID,
348 MachineIRBuilder &MIRBuilder);
349
350 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
351 /// many places it could ultimately go. In the IR, we have a single unwind
352 /// destination, but in the machine CFG, we enumerate all the possible blocks.
353 /// This function skips over imaginary basic blocks that hold catchswitch
354 /// instructions, and finds all the "real" machine
355 /// basic block destinations. As those destinations may not be successors of
356 /// EHPadBB, here we also calculate the edge probability to those
357 /// destinations. The passed-in Prob is the edge probability to EHPadBB.
358 bool findUnwindDestinations(
359 const BasicBlock *EHPadBB, BranchProbability Prob,
360 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
361 &UnwindDests);
362
363 bool translateInvoke(const User &U, MachineIRBuilder &MIRBuilder);
364
365 bool translateCallBr(const User &U, MachineIRBuilder &MIRBuilder);
366
367 bool translateLandingPad(const User &U, MachineIRBuilder &MIRBuilder);
368
369 /// Translate one of LLVM's cast instructions into MachineInstrs, with the
370 /// given generic Opcode.
371 bool translateCast(unsigned Opcode, const User &U,
372 MachineIRBuilder &MIRBuilder);
373
374 /// Translate a phi instruction.
375 bool translatePHI(const User &U, MachineIRBuilder &MIRBuilder);
376
377 /// Translate a comparison (icmp or fcmp) instruction or constant.
378 bool translateCompare(const User &U, MachineIRBuilder &MIRBuilder);
379
380 /// Translate an integer compare instruction (or constant).
381 bool translateICmp(const User &U, MachineIRBuilder &MIRBuilder) {
382 return translateCompare(U, MIRBuilder);
383 }
384
385 /// Translate a floating-point compare instruction (or constant).
386 bool translateFCmp(const User &U, MachineIRBuilder &MIRBuilder) {
387 return translateCompare(U, MIRBuilder);
388 }
389
390 /// Add remaining operands onto phis we've translated. Executed after all
391 /// MachineBasicBlocks for the function have been created.
392 void finishPendingPhis();
393
394 /// Describe any constants in PendingConstDbgValues that are
395 /// materialized as vregs.
396 void finishPendingConstDbgValues();
397
398 /// Translate \p Inst into a unary operation \p Opcode.
399 /// \pre \p U is a unary operation.
400 bool translateUnaryOp(unsigned Opcode, const User &U,
401 MachineIRBuilder &MIRBuilder);
402
403 /// Translate \p Inst into a binary operation \p Opcode.
404 /// \pre \p U is a binary operation.
405 bool translateBinaryOp(unsigned Opcode, const User &U,
406 MachineIRBuilder &MIRBuilder);
407
408 /// If the set of cases should be emitted as a series of branches, return
409 /// true. If we should emit this as a bunch of and/or'd together conditions,
410 /// return false.
411 bool shouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases);
412 /// Helper method for findMergedConditions.
413 /// This function emits a branch and is used at the leaves of an OR or an
414 /// AND operator tree.
415 void emitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
417 MachineBasicBlock *CurBB,
418 MachineBasicBlock *SwitchBB,
419 BranchProbability TProb,
420 BranchProbability FProb, bool InvertCond);
421 /// Used during condbr translation to find trees of conditions that can be
422 /// optimized.
423 void findMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
425 MachineBasicBlock *SwitchBB,
427 BranchProbability FProb, bool InvertCond);
428
429 /// Translate branch (br) instruction.
430 /// \pre \p U is a branch instruction.
431 bool translateUncondBr(const User &U, MachineIRBuilder &MIRBuilder);
432 bool translateCondBr(const User &U, MachineIRBuilder &MIRBuilder);
433
434 // Begin switch lowering functions.
435 bool emitJumpTableHeader(SwitchCG::JumpTable &JT,
437 MachineBasicBlock *HeaderBB);
438 void emitJumpTable(SwitchCG::JumpTable &JT, MachineBasicBlock *MBB);
439
440 void emitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB,
441 MachineIRBuilder &MIB);
442
443 /// Generate for the BitTest header block, which precedes each sequence of
444 /// BitTestCases.
445 void emitBitTestHeader(SwitchCG::BitTestBlock &BTB,
446 MachineBasicBlock *SwitchMBB);
447 /// Generate code to produces one "bit test" for a given BitTestCase \p B.
448 void emitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB,
449 BranchProbability BranchProbToNext, Register Reg,
451
452 void splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
454 MachineBasicBlock *SwitchMBB, MachineIRBuilder &MIB);
455
456 bool lowerJumpTableWorkItem(
458 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
461 MachineBasicBlock *Fallthrough, bool FallthroughUnreachable);
462
463 bool lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I, Value *Cond,
464 MachineBasicBlock *Fallthrough,
465 bool FallthroughUnreachable,
466 BranchProbability UnhandledProbs,
467 MachineBasicBlock *CurMBB,
468 MachineIRBuilder &MIB,
469 MachineBasicBlock *SwitchMBB);
470
471 bool lowerBitTestWorkItem(
473 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
475 BranchProbability DefaultProb, BranchProbability UnhandledProbs,
477 bool FallthroughUnreachable);
478
479 bool lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond,
480 MachineBasicBlock *SwitchMBB,
481 MachineBasicBlock *DefaultMBB,
482 MachineIRBuilder &MIB);
483
484 bool translateSwitch(const User &U, MachineIRBuilder &MIRBuilder);
485 // End switch lowering section.
486
487 bool translateIndirectBr(const User &U, MachineIRBuilder &MIRBuilder);
488
489 bool translateExtractValue(const User &U, MachineIRBuilder &MIRBuilder);
490
491 bool translateInsertValue(const User &U, MachineIRBuilder &MIRBuilder);
492
493 bool translateSelect(const User &U, MachineIRBuilder &MIRBuilder);
494
495 bool translateGetElementPtr(const User &U, MachineIRBuilder &MIRBuilder);
496
497 bool translateAlloca(const User &U, MachineIRBuilder &MIRBuilder);
498
499 /// Translate return (ret) instruction.
500 /// The target needs to implement CallLowering::lowerReturn for
501 /// this to succeed.
502 /// \pre \p U is a return instruction.
503 bool translateRet(const User &U, MachineIRBuilder &MIRBuilder);
504
505 bool translateFNeg(const User &U, MachineIRBuilder &MIRBuilder);
506
507 bool translateAdd(const User &U, MachineIRBuilder &MIRBuilder) {
508 return translateBinaryOp(TargetOpcode::G_ADD, U, MIRBuilder);
509 }
510 bool translateSub(const User &U, MachineIRBuilder &MIRBuilder) {
511 return translateBinaryOp(TargetOpcode::G_SUB, U, MIRBuilder);
512 }
513 bool translateAnd(const User &U, MachineIRBuilder &MIRBuilder) {
514 return translateBinaryOp(TargetOpcode::G_AND, U, MIRBuilder);
515 }
516 bool translateMul(const User &U, MachineIRBuilder &MIRBuilder) {
517 return translateBinaryOp(TargetOpcode::G_MUL, U, MIRBuilder);
518 }
519 bool translateOr(const User &U, MachineIRBuilder &MIRBuilder) {
520 return translateBinaryOp(TargetOpcode::G_OR, U, MIRBuilder);
521 }
522 bool translateXor(const User &U, MachineIRBuilder &MIRBuilder) {
523 return translateBinaryOp(TargetOpcode::G_XOR, U, MIRBuilder);
524 }
525
526 bool translateUDiv(const User &U, MachineIRBuilder &MIRBuilder) {
527 return translateBinaryOp(TargetOpcode::G_UDIV, U, MIRBuilder);
528 }
529 bool translateSDiv(const User &U, MachineIRBuilder &MIRBuilder) {
530 return translateBinaryOp(TargetOpcode::G_SDIV, U, MIRBuilder);
531 }
532 bool translateURem(const User &U, MachineIRBuilder &MIRBuilder) {
533 return translateBinaryOp(TargetOpcode::G_UREM, U, MIRBuilder);
534 }
535 bool translateSRem(const User &U, MachineIRBuilder &MIRBuilder) {
536 return translateBinaryOp(TargetOpcode::G_SREM, U, MIRBuilder);
537 }
538 bool translateIntToPtr(const User &U, MachineIRBuilder &MIRBuilder) {
539 return translateCast(TargetOpcode::G_INTTOPTR, U, MIRBuilder);
540 }
541 bool translatePtrToInt(const User &U, MachineIRBuilder &MIRBuilder) {
542 return translateCast(TargetOpcode::G_PTRTOINT, U, MIRBuilder);
543 }
544 bool translatePtrToAddr(const User &U, MachineIRBuilder &MIRBuilder) {
545 // FIXME: this is not correct for pointers with addr width != pointer width
546 return translatePtrToInt(U, MIRBuilder);
547 }
548 bool translateTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
549 return translateCast(TargetOpcode::G_TRUNC, U, MIRBuilder);
550 }
551 bool translateFPTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
552 return translateCast(TargetOpcode::G_FPTRUNC, U, MIRBuilder);
553 }
554 bool translateFPExt(const User &U, MachineIRBuilder &MIRBuilder) {
555 return translateCast(TargetOpcode::G_FPEXT, U, MIRBuilder);
556 }
557 bool translateFPToUI(const User &U, MachineIRBuilder &MIRBuilder) {
558 return translateCast(TargetOpcode::G_FPTOUI, U, MIRBuilder);
559 }
560 bool translateFPToSI(const User &U, MachineIRBuilder &MIRBuilder) {
561 return translateCast(TargetOpcode::G_FPTOSI, U, MIRBuilder);
562 }
563 bool translateUIToFP(const User &U, MachineIRBuilder &MIRBuilder) {
564 return translateCast(TargetOpcode::G_UITOFP, U, MIRBuilder);
565 }
566 bool translateSIToFP(const User &U, MachineIRBuilder &MIRBuilder) {
567 return translateCast(TargetOpcode::G_SITOFP, U, MIRBuilder);
568 }
569 bool translateUnreachable(const User &U, MachineIRBuilder &MIRBuilder);
570
571 bool translateSExt(const User &U, MachineIRBuilder &MIRBuilder) {
572 return translateCast(TargetOpcode::G_SEXT, U, MIRBuilder);
573 }
574
575 bool translateZExt(const User &U, MachineIRBuilder &MIRBuilder) {
576 return translateCast(TargetOpcode::G_ZEXT, U, MIRBuilder);
577 }
578
579 bool translateShl(const User &U, MachineIRBuilder &MIRBuilder) {
580 return translateBinaryOp(TargetOpcode::G_SHL, U, MIRBuilder);
581 }
582 bool translateLShr(const User &U, MachineIRBuilder &MIRBuilder) {
583 return translateBinaryOp(TargetOpcode::G_LSHR, U, MIRBuilder);
584 }
585 bool translateAShr(const User &U, MachineIRBuilder &MIRBuilder) {
586 return translateBinaryOp(TargetOpcode::G_ASHR, U, MIRBuilder);
587 }
588
589 bool translateFAdd(const User &U, MachineIRBuilder &MIRBuilder) {
590 return translateBinaryOp(TargetOpcode::G_FADD, U, MIRBuilder);
591 }
592 bool translateFSub(const User &U, MachineIRBuilder &MIRBuilder) {
593 return translateBinaryOp(TargetOpcode::G_FSUB, U, MIRBuilder);
594 }
595 bool translateFMul(const User &U, MachineIRBuilder &MIRBuilder) {
596 return translateBinaryOp(TargetOpcode::G_FMUL, U, MIRBuilder);
597 }
598 bool translateFDiv(const User &U, MachineIRBuilder &MIRBuilder) {
599 return translateBinaryOp(TargetOpcode::G_FDIV, U, MIRBuilder);
600 }
601 bool translateFRem(const User &U, MachineIRBuilder &MIRBuilder) {
602 return translateBinaryOp(TargetOpcode::G_FREM, U, MIRBuilder);
603 }
604
605 bool translateVAArg(const User &U, MachineIRBuilder &MIRBuilder);
606
607 bool translateInsertElement(const User &U, MachineIRBuilder &MIRBuilder);
608 bool translateInsertVector(const User &U, MachineIRBuilder &MIRBuilder);
609
610 bool translateExtractElement(const User &U, MachineIRBuilder &MIRBuilder);
611 bool translateExtractVector(const User &U, MachineIRBuilder &MIRBuilder);
612
613 bool translateShuffleVector(const User &U, MachineIRBuilder &MIRBuilder);
614
615 bool translateAtomicCmpXchg(const User &U, MachineIRBuilder &MIRBuilder);
616 bool translateAtomicRMW(const User &U, MachineIRBuilder &MIRBuilder);
617 bool translateFence(const User &U, MachineIRBuilder &MIRBuilder);
618 bool translateFreeze(const User &U, MachineIRBuilder &MIRBuilder);
619
620 bool translateBitExtract(const User &U, MachineIRBuilder &MIRBuilder);
621 bool translateBitInsert(const User &U, MachineIRBuilder &MIRBuilder);
622
623 // Stubs to keep the compiler happy while we implement the rest of the
624 // translation.
625 bool translateResume(const User &U, MachineIRBuilder &MIRBuilder) {
626 return false;
627 }
628 bool translateCleanupRet(const User &U, MachineIRBuilder &MIRBuilder) {
629 return false;
630 }
631 bool translateCatchRet(const User &U, MachineIRBuilder &MIRBuilder) {
632 return false;
633 }
634 bool translateCatchSwitch(const User &U, MachineIRBuilder &MIRBuilder) {
635 return false;
636 }
637 bool translateAddrSpaceCast(const User &U, MachineIRBuilder &MIRBuilder) {
638 return translateCast(TargetOpcode::G_ADDRSPACE_CAST, U, MIRBuilder);
639 }
640 bool translateCleanupPad(const User &U, MachineIRBuilder &MIRBuilder) {
641 return false;
642 }
643 bool translateCatchPad(const User &U, MachineIRBuilder &MIRBuilder) {
644 return false;
645 }
646 bool translateUserOp1(const User &U, MachineIRBuilder &MIRBuilder) {
647 return false;
648 }
649 bool translateUserOp2(const User &U, MachineIRBuilder &MIRBuilder) {
650 return false;
651 }
652
653 bool translateConvergenceControlIntrinsic(const CallInst &CI,
654 Intrinsic::ID ID,
655 MachineIRBuilder &MIRBuilder);
656
657 /// @}
658
659 // Builder for machine instruction a la IRBuilder.
660 // I.e., compared to regular MIBuilder, this one also inserts the instruction
661 // in the current block, it can creates block, etc., basically a kind of
662 // IRBuilder, but for Machine IR.
663 // CSEMIRBuilder CurBuilder;
664 std::unique_ptr<MachineIRBuilder> CurBuilder;
665
666 // Builder set to the entry block (just after ABI lowering instructions). Used
667 // as a convenient location for Constants.
668 // CSEMIRBuilder EntryBuilder;
669 std::unique_ptr<MachineIRBuilder> EntryBuilder;
670
671 // The MachineFunction currently being translated.
672 MachineFunction *MF = nullptr;
673
674 /// MachineRegisterInfo used to create virtual registers.
675 MachineRegisterInfo *MRI = nullptr;
676
677 const DataLayout *DL = nullptr;
678
679 CodeGenOptLevel OptLevel;
680
681 /// Current optimization remark emitter. Used to report failures.
682 std::unique_ptr<OptimizationRemarkEmitter> ORE;
683
684 AAResults *AA = nullptr;
685 AssumptionCache *AC = nullptr;
686 const TargetLibraryInfo *LibInfo = nullptr;
687 const LibcallLoweringInfo *Libcalls = nullptr;
688 const TargetLowering *TLI = nullptr;
689 FunctionLoweringInfo FuncInfo;
690
691 // True when either the Target Machine specifies no optimizations or the
692 // function has the optnone attribute.
693 bool EnableOpts = false;
694
695 /// True when the block contains a tail call. This allows the IRTranslator to
696 /// stop translating such blocks early.
697 bool HasTailCall = false;
698
699 StackProtectorDescriptor SPDescriptor;
700
701 bool mayTranslateUserTypes(const User &U) const;
702
703 /// Switch analysis and optimization.
704 class GISelSwitchLowering : public SwitchCG::SwitchLowering {
705 public:
706 GISelSwitchLowering(IRTranslatorImpl *irt, FunctionLoweringInfo &funcinfo)
707 : SwitchLowering(funcinfo), IRT(irt) {
708 assert(irt && "irt is null!");
709 }
710
711 void addSuccessorWithProb(
714 IRT->addSuccessorWithProb(Src, Dst, Prob);
715 }
716
717 ~GISelSwitchLowering() override = default;
718
719 private:
720 IRTranslatorImpl *IRT;
721 };
722
723 std::unique_ptr<GISelSwitchLowering> SL;
724
725 // * Insert all the code needed to materialize the constants
726 // at the proper place. E.g., Entry block or dominator block
727 // of each constant depending on how fancy we want to be.
728 // * Clear the different maps.
729 void finalizeFunction();
730
731 // Processing steps done per block. E.g. emitting jump tables, stack
732 // protectors etc. Returns true if no errors, false if there was a problem
733 // that caused an abort.
734 bool finalizeBasicBlock(const BasicBlock &BB, MachineBasicBlock &MBB);
735
736 /// Codegen a new tail for a stack protector check ParentMBB which has had its
737 /// tail spliced into a stack protector check success bb.
738 ///
739 /// For a high level explanation of how this fits into the stack protector
740 /// generation see the comment on the declaration of class
741 /// StackProtectorDescriptor.
742 ///
743 /// \return true if there were no problems.
744 bool emitSPDescriptorParent(StackProtectorDescriptor &SPD,
745 MachineBasicBlock *ParentBB);
746
747 /// Codegen the failure basic block for a stack protector check.
748 ///
749 /// A failure stack protector machine basic block consists simply of a call to
750 /// __stack_chk_fail().
751 ///
752 /// For a high level explanation of how this fits into the stack protector
753 /// generation see the comment on the declaration of class
754 /// StackProtectorDescriptor.
755 ///
756 /// \return true if there were no problems.
757 bool emitSPDescriptorFailure(StackProtectorDescriptor &SPD,
758 MachineBasicBlock *FailureBB);
759
760 /// Get the VRegs that represent \p Val.
761 /// Non-aggregate types have just one corresponding VReg and the list can be
762 /// used as a single "unsigned". Aggregates get flattened. If such VRegs do
763 /// not exist, they are created.
764 ArrayRef<Register> getOrCreateVRegs(const Value &Val);
765
766 Register getOrCreateVReg(const Value &Val) {
767 auto Regs = getOrCreateVRegs(Val);
768 if (Regs.empty())
769 return 0;
770 assert(Regs.size() == 1 &&
771 "attempt to get single VReg for aggregate or void");
772 return Regs[0];
773 }
774
775 Register getOrCreateConvergenceTokenVReg(const Value &Token) {
776 assert(Token.getType()->isTokenTy());
777 auto &Regs = *VMap.getVRegs(Token);
778 if (!Regs.empty()) {
779 assert(Regs.size() == 1 &&
780 "Expected a single register for convergence tokens.");
781 return Regs[0];
782 }
783
784 auto Reg = MRI->createGenericVirtualRegister(LLT::token());
785 Regs.push_back(Reg);
786 return Reg;
787 }
788
789 /// Allocate empty vregs for \p Val. For aggregate values, also populate
790 /// their offsets.
791 ValueToVRegInfo::VRegListT &allocateVRegs(const Value &Val);
792
793 /// Get the frame index that represents \p Val.
794 /// If such VReg does not exist, it is created.
795 int getOrCreateFrameIndex(const AllocaInst &AI);
796
797 /// Get the alignment of the given memory operation instruction. This will
798 /// either be the explicitly specified value or the ABI-required alignment for
799 /// the type being accessed (according to the Module's DataLayout).
800 Align getMemOpAlign(const Instruction &I);
801
802 /// Get the MachineBasicBlock that represents \p BB. Specifically, the block
803 /// returned will be the head of the translated block (suitable for branch
804 /// destinations).
805 MachineBasicBlock &getMBB(const BasicBlock &BB);
806
807 /// Record \p NewPred as a Machine predecessor to `Edge.second`, corresponding
808 /// to `Edge.first` at the IR level. This is used when IRTranslation creates
809 /// multiple MachineBasicBlocks for a given IR block and the CFG is no longer
810 /// represented simply by the IR-level CFG.
811 void addMachineCFGPred(CFGEdge Edge, MachineBasicBlock *NewPred);
812
813 /// Returns the Machine IR predecessors for the given IR CFG edge. Usually
814 /// this is just the single MachineBasicBlock corresponding to the predecessor
815 /// in the IR. More complex lowering can result in multiple MachineBasicBlocks
816 /// preceding the original though (e.g. switch instructions).
817 SmallVector<MachineBasicBlock *, 1> getMachinePredBBs(CFGEdge Edge) {
818 auto RemappedEdge = MachinePreds.find(Edge);
819 if (RemappedEdge != MachinePreds.end())
820 return RemappedEdge->second;
821 return SmallVector<MachineBasicBlock *, 4>(1, &getMBB(*Edge.first));
822 }
823
824 /// Return branch probability calculated by BranchProbabilityInfo for IR
825 /// blocks.
826 BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
827 const MachineBasicBlock *Dst) const;
828
829 void addSuccessorWithProb(
832
833public:
835 : OptLevel(OptLevel) {}
836
837 // Algo:
838 // CallLowering = MF.subtarget.getCallLowering()
839 // F = MF.getParent()
840 // MIRBuilder.reset(MF)
841 // getMBB(F.getEntryBB())
842 // CallLowering->translateArguments(MIRBuilder, F, ValToVReg)
843 // for each bb in F
844 // getMBB(bb)
845 // for each inst in bb
846 // if (!translate(MIRBuilder, inst, ValToVReg, ConstantToSequence))
847 // reportFatalUsageError("Don't know how to translate input");
848 // finalize()
850 function_ref<GISelCSEInfo *()> GetCSEInfo,
851 bool ShouldSkipOpts,
852 function_ref<AAResults *()> GetAAResults,
854 function_ref<AssumptionCache *()> GetAC,
855 TargetLibraryInfo *LibraryInfo,
856 const LibcallLoweringInfo *LibcallInfo,
857 SSPLayoutInfo *StackProtectorInfo);
858};
859
860} // namespace llvm
861
863
865 "IRTranslator LLVM IR -> MI", false, false)
872 "IRTranslator LLVM IR -> MI", false, false)
873
877 MF.getProperties().setFailedISel();
878 bool IsGlobalISelAbortEnabled =
879 MF.getTarget().Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
880
881 // Print the function name explicitly if we don't have a debug location (which
882 // makes the diagnostic less useful) or if we're going to emit a raw error.
883 if (!R.getLocation().isValid() || IsGlobalISelAbortEnabled)
884 R << (" (in function: " + MF.getName() + ")").str();
885
886 if (IsGlobalISelAbortEnabled)
887 report_fatal_error(Twine(R.getMsg()));
888 else
889 ORE.emit(R);
890}
891
893 : MachineFunctionPass(ID), OptLevel(OptLevel),
894 Impl(std::make_unique<IRTranslatorImpl>(OptLevel)) {}
895
897
898#ifndef NDEBUG
899namespace {
900/// Verify that every instruction created has the same DILocation as the
901/// instruction being translated.
902class DILocationVerifier : public GISelChangeObserver {
903 const Instruction *CurrInst = nullptr;
904
905public:
906 DILocationVerifier() = default;
907 ~DILocationVerifier() override = default;
908
909 const Instruction *getCurrentInst() const { return CurrInst; }
910 void setCurrentInst(const Instruction *Inst) { CurrInst = Inst; }
911
912 void erasingInstr(MachineInstr &MI) override {}
913 void changingInstr(MachineInstr &MI) override {}
914 void changedInstr(MachineInstr &MI) override {}
915
916 void createdInstr(MachineInstr &MI) override {
917 assert(getCurrentInst() && "Inserted instruction without a current MI");
918
919 // Only print the check message if we're actually checking it.
920#ifndef NDEBUG
921 LLVM_DEBUG(dbgs() << "Checking DILocation from " << *CurrInst
922 << " was copied to " << MI);
923#endif
924 // We allow insts in the entry block to have no debug loc because
925 // they could have originated from constants, and we don't want a jumpy
926 // debug experience.
927 assert((CurrInst->getDebugLoc() == MI.getDebugLoc() ||
928 (MI.getParent()->isEntryBlock() && !MI.getDebugLoc()) ||
929 (MI.isDebugInstr())) &&
930 "Line info was not transferred to all instructions");
931 }
932};
933} // namespace
934#endif // ifndef NDEBUG
935
952
953IRTranslatorImpl::ValueToVRegInfo::VRegListT &
954IRTranslatorImpl::allocateVRegs(const Value &Val) {
955 auto VRegsIt = VMap.findVRegs(Val);
956 if (VRegsIt != VMap.vregs_end())
957 return *VRegsIt->second;
958 auto *Regs = VMap.getVRegs(Val);
959 if (!Val.getType()->isAggregateType()) {
960 Regs->push_back(0);
961 return *Regs;
962 }
963
964 auto *Offsets = VMap.getOffsets(Val);
965 SmallVector<LLT, 4> SplitTys;
966 computeValueLLTs(*DL, *Val.getType(), SplitTys,
967 Offsets->empty() ? Offsets : nullptr);
968 for (unsigned i = 0; i < SplitTys.size(); ++i)
969 Regs->push_back(0);
970 return *Regs;
971}
972
973ArrayRef<Register> IRTranslatorImpl::getOrCreateVRegs(const Value &Val) {
974 auto VRegsIt = VMap.findVRegs(Val);
975 if (VRegsIt != VMap.vregs_end())
976 return *VRegsIt->second;
977
978 if (Val.getType()->isVoidTy())
979 return *VMap.getVRegs(Val);
980
981 // Create entry for this type.
982 auto *VRegs = VMap.getVRegs(Val);
983
984 if (!Val.getType()->isTokenTy())
985 assert(Val.getType()->isSized() &&
986 "Don't know how to create an empty vreg");
987
988 // Fast-path values that lower to a single vreg.
989 if (!Val.getType()->isAggregateType()) {
990 LLT Ty = getLLTForType(*Val.getType(), *DL);
991 VRegs->push_back(MRI->createGenericVirtualRegister(Ty));
992 if (isa<Constant>(Val)) {
993 bool Success = translate(cast<Constant>(Val), VRegs->front());
994 if (!Success) {
995 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
996 MF->getFunction().getSubprogram(),
997 &MF->getFunction().getEntryBlock());
998 R << "unable to translate constant: " << ore::NV("Type", Val.getType());
999 reportTranslationError(*MF, *ORE, R);
1000 }
1001 }
1002 return *VRegs;
1003 }
1004
1005 SmallVector<LLT, 4> SplitTys;
1006 auto *Offsets = VMap.getOffsets(Val);
1007 computeValueLLTs(*DL, *Val.getType(), SplitTys,
1008 Offsets->empty() ? Offsets : nullptr);
1009
1010 if (!isa<Constant>(Val)) {
1011 for (auto Ty : SplitTys)
1012 VRegs->push_back(MRI->createGenericVirtualRegister(Ty));
1013 return *VRegs;
1014 }
1015
1016 // UndefValue, ConstantAggregateZero
1017 auto &C = cast<Constant>(Val);
1018 unsigned Idx = 0;
1019 while (auto Elt = C.getAggregateElement(Idx++)) {
1020 auto EltRegs = getOrCreateVRegs(*Elt);
1021 llvm::append_range(*VRegs, EltRegs);
1022 }
1023
1024 return *VRegs;
1025}
1026
1027int IRTranslatorImpl::getOrCreateFrameIndex(const AllocaInst &AI) {
1028 auto [MapEntry, Inserted] = FrameIndices.try_emplace(&AI);
1029 if (!Inserted)
1030 return MapEntry->second;
1031
1032 TypeSize TySize = AI.getAllocationSize(*DL).value_or(TypeSize::getZero());
1033 uint64_t Size = TySize.getKnownMinValue();
1034
1035 // Always allocate at least one byte.
1036 Size = std::max<uint64_t>(Size, 1u);
1037
1038 int &FI = MapEntry->second;
1039 FI = MF->getFrameInfo().CreateStackObject(Size, AI.getAlign(), false, &AI);
1040
1041 // Scalable vectors and structures that contain scalable vectors may
1042 // need a special StackID to distinguish them from other (fixed size)
1043 // stack objects.
1044 if (TySize.isScalable()) {
1045 auto StackID =
1046 MF->getSubtarget().getFrameLowering()->getStackIDForScalableVectors();
1047 MF->getFrameInfo().setStackID(FI, StackID);
1048 }
1049
1050 return FI;
1051}
1052
1053Align IRTranslatorImpl::getMemOpAlign(const Instruction &I) {
1054 if (const StoreInst *SI = dyn_cast<StoreInst>(&I))
1055 return SI->getAlign();
1056 if (const LoadInst *LI = dyn_cast<LoadInst>(&I))
1057 return LI->getAlign();
1058 if (const AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(&I))
1059 return AI->getAlign();
1060 if (const AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(&I))
1061 return AI->getAlign();
1062
1063 OptimizationRemarkMissed R("gisel-ir-translator", "", &I);
1064 R << "unable to translate memop: " << ore::NV("Opcode", &I);
1065 reportTranslationError(*MF, *ORE, R);
1066 return Align(1);
1067}
1068
1069MachineBasicBlock &IRTranslatorImpl::getMBB(const BasicBlock &BB) {
1070 MachineBasicBlock *MBB = FuncInfo.getMBB(&BB);
1071 assert(MBB && "BasicBlock was not encountered before");
1072 return *MBB;
1073}
1074
1075void IRTranslatorImpl::addMachineCFGPred(CFGEdge Edge,
1076 MachineBasicBlock *NewPred) {
1077 assert(NewPred && "new predecessor must be a real MachineBasicBlock");
1078 MachinePreds[Edge].push_back(NewPred);
1079}
1080
1081bool IRTranslatorImpl::translateBinaryOp(unsigned Opcode, const User &U,
1082 MachineIRBuilder &MIRBuilder) {
1083 if (!mayTranslateUserTypes(U))
1084 return false;
1085
1086 // Get or create a virtual register for each value.
1087 // Unless the value is a Constant => loadimm cst?
1088 // or inline constant each time?
1089 // Creation of a virtual register needs to have a size.
1090 Register Op0 = getOrCreateVReg(*U.getOperand(0));
1091 Register Op1 = getOrCreateVReg(*U.getOperand(1));
1092 Register Res = getOrCreateVReg(U);
1093 uint32_t Flags = 0;
1094 if (isa<Instruction>(U)) {
1095 const Instruction &I = cast<Instruction>(U);
1097 }
1098
1099 MIRBuilder.buildInstr(Opcode, {Res}, {Op0, Op1}, Flags);
1100 return true;
1101}
1102
1103bool IRTranslatorImpl::translateUnaryOp(unsigned Opcode, const User &U,
1104 MachineIRBuilder &MIRBuilder) {
1105 if (!mayTranslateUserTypes(U))
1106 return false;
1107
1108 Register Op0 = getOrCreateVReg(*U.getOperand(0));
1109 Register Res = getOrCreateVReg(U);
1110 uint32_t Flags = 0;
1111 if (isa<Instruction>(U)) {
1112 const Instruction &I = cast<Instruction>(U);
1114 }
1115 MIRBuilder.buildInstr(Opcode, {Res}, {Op0}, Flags);
1116 return true;
1117}
1118
1119bool IRTranslatorImpl::translateFNeg(const User &U,
1120 MachineIRBuilder &MIRBuilder) {
1121 return translateUnaryOp(TargetOpcode::G_FNEG, U, MIRBuilder);
1122}
1123
1124bool IRTranslatorImpl::translateCompare(const User &U,
1125 MachineIRBuilder &MIRBuilder) {
1126 if (!mayTranslateUserTypes(U))
1127 return false;
1128
1129 auto *CI = cast<CmpInst>(&U);
1130 Register Op0 = getOrCreateVReg(*U.getOperand(0));
1131 Register Op1 = getOrCreateVReg(*U.getOperand(1));
1132 Register Res = getOrCreateVReg(U);
1133 CmpInst::Predicate Pred = CI->getPredicate();
1135 if (CmpInst::isIntPredicate(Pred))
1136 MIRBuilder.buildICmp(Pred, Res, Op0, Op1, Flags);
1137 else if (Pred == CmpInst::FCMP_FALSE)
1138 MIRBuilder.buildCopy(
1139 Res, getOrCreateVReg(*Constant::getNullValue(U.getType())));
1140 else if (Pred == CmpInst::FCMP_TRUE)
1141 MIRBuilder.buildCopy(
1142 Res, getOrCreateVReg(*Constant::getAllOnesValue(U.getType())));
1143 else
1144 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1, Flags);
1145
1146 return true;
1147}
1148
1149bool IRTranslatorImpl::translateRet(const User &U,
1150 MachineIRBuilder &MIRBuilder) {
1151 const ReturnInst &RI = cast<ReturnInst>(U);
1152 const Value *Ret = RI.getReturnValue();
1153 if (Ret && DL->getTypeStoreSize(Ret->getType()).isZero())
1154 Ret = nullptr;
1155
1156 ArrayRef<Register> VRegs;
1157 if (Ret)
1158 VRegs = getOrCreateVRegs(*Ret);
1159
1160 Register SwiftErrorVReg = 0;
1161 if (CLI->supportSwiftError() && SwiftError.getFunctionArg()) {
1162 SwiftErrorVReg = SwiftError.getOrCreateVRegUseAt(
1163 &RI, &MIRBuilder.getMBB(), SwiftError.getFunctionArg());
1164 }
1165
1166 // The target may mess up with the insertion point, but
1167 // this is not important as a return is the last instruction
1168 // of the block anyway.
1169 return CLI->lowerReturn(MIRBuilder, Ret, VRegs, FuncInfo, SwiftErrorVReg);
1170}
1171
1172void IRTranslatorImpl::emitBranchForMergedCondition(
1174 MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
1175 BranchProbability TProb, BranchProbability FProb, bool InvertCond) {
1176 // If the leaf of the tree is a comparison, merge the condition into
1177 // the caseblock.
1178 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1179 CmpInst::Predicate Condition;
1180 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
1181 Condition = InvertCond ? IC->getInversePredicate() : IC->getPredicate();
1182 } else {
1183 const FCmpInst *FC = cast<FCmpInst>(Cond);
1184 Condition = InvertCond ? FC->getInversePredicate() : FC->getPredicate();
1185 }
1186
1187 SwitchCG::CaseBlock CB(Condition, false, BOp->getOperand(0),
1188 BOp->getOperand(1), nullptr, TBB, FBB, CurBB,
1189 CurBuilder->getDebugLoc(), TProb, FProb);
1190 SL->SwitchCases.push_back(CB);
1191 return;
1192 }
1193
1194 // Create a CaseBlock record representing this branch.
1196 SwitchCG::CaseBlock CB(
1197 Pred, false, Cond, ConstantInt::getTrue(MF->getFunction().getContext()),
1198 nullptr, TBB, FBB, CurBB, CurBuilder->getDebugLoc(), TProb, FProb);
1199 SL->SwitchCases.push_back(CB);
1200}
1201
1202static bool isValInBlock(const Value *V, const BasicBlock *BB) {
1203 if (const Instruction *I = dyn_cast<Instruction>(V))
1204 return I->getParent() == BB;
1205 return true;
1206}
1207
1208void IRTranslatorImpl::findMergedConditions(
1210 MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
1212 BranchProbability FProb, bool InvertCond) {
1213 using namespace PatternMatch;
1214 assert((Opc == Instruction::And || Opc == Instruction::Or) &&
1215 "Expected Opc to be AND/OR");
1216 // Skip over not part of the tree and remember to invert op and operands at
1217 // next level.
1218 Value *NotCond;
1219 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
1220 isValInBlock(NotCond, CurBB->getBasicBlock())) {
1221 findMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
1222 !InvertCond);
1223 return;
1224 }
1225
1227 const Value *BOpOp0, *BOpOp1;
1228 // Compute the effective opcode for Cond, taking into account whether it needs
1229 // to be inverted, e.g.
1230 // and (not (or A, B)), C
1231 // gets lowered as
1232 // and (and (not A, not B), C)
1234 if (BOp) {
1235 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
1236 ? Instruction::And
1237 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
1238 ? Instruction::Or
1240 if (InvertCond) {
1241 if (BOpc == Instruction::And)
1242 BOpc = Instruction::Or;
1243 else if (BOpc == Instruction::Or)
1244 BOpc = Instruction::And;
1245 }
1246 }
1247
1248 // If this node is not part of the or/and tree, emit it as a branch.
1249 // Note that all nodes in the tree should have same opcode.
1250 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
1251 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
1252 !isValInBlock(BOpOp0, CurBB->getBasicBlock()) ||
1253 !isValInBlock(BOpOp1, CurBB->getBasicBlock())) {
1254 emitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB, TProb, FProb,
1255 InvertCond);
1256 return;
1257 }
1258
1259 // Create TmpBB after CurBB.
1260 MachineFunction::iterator BBI(CurBB);
1261 MachineBasicBlock *TmpBB =
1262 MF->CreateMachineBasicBlock(CurBB->getBasicBlock());
1263 CurBB->getParent()->insert(++BBI, TmpBB);
1264
1265 if (Opc == Instruction::Or) {
1266 // Codegen X | Y as:
1267 // BB1:
1268 // jmp_if_X TBB
1269 // jmp TmpBB
1270 // TmpBB:
1271 // jmp_if_Y TBB
1272 // jmp FBB
1273 //
1274
1275 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
1276 // The requirement is that
1277 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
1278 // = TrueProb for original BB.
1279 // Assuming the original probabilities are A and B, one choice is to set
1280 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
1281 // A/(1+B) and 2B/(1+B). This choice assumes that
1282 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
1283 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
1284 // TmpBB, but the math is more complicated.
1285
1286 auto NewTrueProb = TProb / 2;
1287 auto NewFalseProb = TProb / 2 + FProb;
1288 // Emit the LHS condition.
1289 findMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
1290 NewFalseProb, InvertCond);
1291
1292 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
1293 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
1295 // Emit the RHS condition into TmpBB.
1296 findMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
1297 Probs[1], InvertCond);
1298 } else {
1299 assert(Opc == Instruction::And && "Unknown merge op!");
1300 // Codegen X & Y as:
1301 // BB1:
1302 // jmp_if_X TmpBB
1303 // jmp FBB
1304 // TmpBB:
1305 // jmp_if_Y TBB
1306 // jmp FBB
1307 //
1308 // This requires creation of TmpBB after CurBB.
1309
1310 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
1311 // The requirement is that
1312 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
1313 // = FalseProb for original BB.
1314 // Assuming the original probabilities are A and B, one choice is to set
1315 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
1316 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
1317 // TrueProb for BB1 * FalseProb for TmpBB.
1318
1319 auto NewTrueProb = TProb + FProb / 2;
1320 auto NewFalseProb = FProb / 2;
1321 // Emit the LHS condition.
1322 findMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
1323 NewFalseProb, InvertCond);
1324
1325 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
1326 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
1328 // Emit the RHS condition into TmpBB.
1329 findMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
1330 Probs[1], InvertCond);
1331 }
1332}
1333
1334bool IRTranslatorImpl::shouldEmitAsBranches(
1335 const std::vector<SwitchCG::CaseBlock> &Cases) {
1336 // For multiple cases, it's better to emit as branches.
1337 if (Cases.size() != 2)
1338 return true;
1339
1340 // If this is two comparisons of the same values or'd or and'd together, they
1341 // will get folded into a single comparison, so don't emit two blocks.
1342 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1343 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1344 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1345 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1346 return false;
1347 }
1348
1349 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1350 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1351 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1352 Cases[0].PredInfo.Pred == Cases[1].PredInfo.Pred &&
1353 isa<Constant>(Cases[0].CmpRHS) &&
1354 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1355 if (Cases[0].PredInfo.Pred == CmpInst::ICMP_EQ &&
1356 Cases[0].TrueBB == Cases[1].ThisBB)
1357 return false;
1358 if (Cases[0].PredInfo.Pred == CmpInst::ICMP_NE &&
1359 Cases[0].FalseBB == Cases[1].ThisBB)
1360 return false;
1361 }
1362
1363 return true;
1364}
1365
1366bool IRTranslatorImpl::translateUncondBr(const User &U,
1367 MachineIRBuilder &MIRBuilder) {
1368 const UncondBrInst &BrInst = cast<UncondBrInst>(U);
1369 auto &CurMBB = MIRBuilder.getMBB();
1370 auto *Succ0MBB = &getMBB(*BrInst.getSuccessor(0));
1371
1372 // If the unconditional target is the layout successor, fallthrough.
1373 if (OptLevel == CodeGenOptLevel::None || !CurMBB.isLayoutSuccessor(Succ0MBB))
1374 MIRBuilder.buildBr(*Succ0MBB);
1375
1376 // Link successors.
1377 for (const BasicBlock *Succ : successors(&BrInst))
1378 CurMBB.addSuccessor(&getMBB(*Succ));
1379 return true;
1380}
1381
1382bool IRTranslatorImpl::translateCondBr(const User &U,
1383 MachineIRBuilder &MIRBuilder) {
1384 const CondBrInst &BrInst = cast<CondBrInst>(U);
1385 auto &CurMBB = MIRBuilder.getMBB();
1386 auto *Succ0MBB = &getMBB(*BrInst.getSuccessor(0));
1387
1388 // If this condition is one of the special cases we handle, do special stuff
1389 // now.
1390 const Value *CondVal = BrInst.getCondition();
1391 MachineBasicBlock *Succ1MBB = &getMBB(*BrInst.getSuccessor(1));
1392
1393 // If this is a series of conditions that are or'd or and'd together, emit
1394 // this as a sequence of branches instead of setcc's with and/or operations.
1395 // As long as jumps are not expensive (exceptions for multi-use logic ops,
1396 // unpredictable branches, and vector extracts because those jumps are likely
1397 // expensive for any target), this should improve performance.
1398 // For example, instead of something like:
1399 // cmp A, B
1400 // C = seteq
1401 // cmp D, E
1402 // F = setle
1403 // or C, F
1404 // jnz foo
1405 // Emit:
1406 // cmp A, B
1407 // je foo
1408 // cmp D, E
1409 // jle foo
1410 using namespace PatternMatch;
1411 const Instruction *CondI = dyn_cast<Instruction>(CondVal);
1412 if (!TLI->isJumpExpensive() && CondI && CondI->hasOneUse() &&
1413 !BrInst.hasMetadata(LLVMContext::MD_unpredictable)) {
1415 Value *Vec;
1416 const Value *BOp0, *BOp1;
1417 if (match(CondI, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
1418 Opcode = Instruction::And;
1419 else if (match(CondI, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
1420 Opcode = Instruction::Or;
1421
1422 if (Opcode && !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
1423 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value())))) {
1424 findMergedConditions(CondI, Succ0MBB, Succ1MBB, &CurMBB, &CurMBB, Opcode,
1425 getEdgeProbability(&CurMBB, Succ0MBB),
1426 getEdgeProbability(&CurMBB, Succ1MBB),
1427 /*InvertCond=*/false);
1428 assert(SL->SwitchCases[0].ThisBB == &CurMBB && "Unexpected lowering!");
1429
1430 // Allow some cases to be rejected.
1431 if (shouldEmitAsBranches(SL->SwitchCases)) {
1432 // Emit the branch for this block.
1433 emitSwitchCase(SL->SwitchCases[0], &CurMBB, *CurBuilder);
1434 SL->SwitchCases.erase(SL->SwitchCases.begin());
1435 return true;
1436 }
1437
1438 // Okay, we decided not to do this, remove any inserted MBB's and clear
1439 // SwitchCases.
1440 for (unsigned I = 1, E = SL->SwitchCases.size(); I != E; ++I)
1441 MF->erase(SL->SwitchCases[I].ThisBB);
1442
1443 SL->SwitchCases.clear();
1444 }
1445 }
1446
1447 // Create a CaseBlock record representing this branch.
1448 SwitchCG::CaseBlock CB(CmpInst::ICMP_EQ, false, CondVal,
1449 ConstantInt::getTrue(MF->getFunction().getContext()),
1450 nullptr, Succ0MBB, Succ1MBB, &CurMBB,
1451 CurBuilder->getDebugLoc());
1452
1453 // Use emitSwitchCase to actually insert the fast branch sequence for this
1454 // cond branch.
1455 emitSwitchCase(CB, &CurMBB, *CurBuilder);
1456 return true;
1457}
1458
1459void IRTranslatorImpl::addSuccessorWithProb(MachineBasicBlock *Src,
1460 MachineBasicBlock *Dst,
1461 BranchProbability Prob) {
1462 if (!FuncInfo.BPI) {
1463 Src->addSuccessorWithoutProb(Dst);
1464 return;
1465 }
1466 if (Prob.isUnknown())
1467 Prob = getEdgeProbability(Src, Dst);
1468 Src->addSuccessor(Dst, Prob);
1469}
1470
1472IRTranslatorImpl::getEdgeProbability(const MachineBasicBlock *Src,
1473 const MachineBasicBlock *Dst) const {
1474 const BasicBlock *SrcBB = Src->getBasicBlock();
1475 const BasicBlock *DstBB = Dst->getBasicBlock();
1476 if (!FuncInfo.BPI) {
1477 // If BPI is not available, set the default probability as 1 / N, where N is
1478 // the number of successors.
1479 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
1480 return BranchProbability(1, SuccSize);
1481 }
1482 return FuncInfo.BPI->getEdgeProbability(SrcBB, DstBB);
1483}
1484
1485bool IRTranslatorImpl::translateSwitch(const User &U, MachineIRBuilder &MIB) {
1486 using namespace SwitchCG;
1487 // Extract cases from the switch.
1488 const SwitchInst &SI = cast<SwitchInst>(U);
1489 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1490 CaseClusterVector Clusters;
1491 Clusters.reserve(SI.getNumCases());
1492 for (const auto &I : SI.cases()) {
1493 MachineBasicBlock *Succ = &getMBB(*I.getCaseSuccessor());
1494 assert(Succ && "Could not find successor mbb in mapping");
1495 const ConstantInt *CaseVal = I.getCaseValue();
1496 BranchProbability Prob =
1497 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
1498 : BranchProbability(1, SI.getNumCases() + 1);
1499 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
1500 }
1501
1502 MachineBasicBlock *DefaultMBB = &getMBB(*SI.getDefaultDest());
1503
1504 // Cluster adjacent cases with the same destination. We do this at all
1505 // optimization levels because it's cheap to do and will make codegen faster
1506 // if there are many clusters.
1507 sortAndRangeify(Clusters);
1508
1509 MachineBasicBlock *SwitchMBB = &getMBB(*SI.getParent());
1510
1511 // If there is only the default destination, jump there directly.
1512 if (Clusters.empty()) {
1513 SwitchMBB->addSuccessor(DefaultMBB);
1514 if (DefaultMBB != SwitchMBB->getNextNode())
1515 MIB.buildBr(*DefaultMBB);
1516 return true;
1517 }
1518
1519 SL->findJumpTables(Clusters, &SI, std::nullopt, DefaultMBB, nullptr, nullptr);
1520 SL->findBitTestClusters(Clusters, &SI);
1521
1522 LLVM_DEBUG({
1523 dbgs() << "Case clusters: ";
1524 for (const CaseCluster &C : Clusters) {
1525 if (C.Kind == CC_JumpTable)
1526 dbgs() << "JT:";
1527 if (C.Kind == CC_BitTests)
1528 dbgs() << "BT:";
1529
1530 C.Low->getValue().print(dbgs(), true);
1531 if (C.Low != C.High) {
1532 dbgs() << '-';
1533 C.High->getValue().print(dbgs(), true);
1534 }
1535 dbgs() << ' ';
1536 }
1537 dbgs() << '\n';
1538 });
1539
1540 assert(!Clusters.empty());
1541 SwitchWorkList WorkList;
1542 CaseClusterIt First = Clusters.begin();
1543 CaseClusterIt Last = Clusters.end() - 1;
1544 auto DefaultProb = getEdgeProbability(SwitchMBB, DefaultMBB);
1545 WorkList.push_back({SwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
1546
1547 while (!WorkList.empty()) {
1548 SwitchWorkListItem W = WorkList.pop_back_val();
1549
1550 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
1551 // For optimized builds, lower large range as a balanced binary tree.
1552 if (NumClusters > 3 &&
1553 MF->getTarget().getOptLevel() != CodeGenOptLevel::None &&
1554 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
1555 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB, MIB);
1556 continue;
1557 }
1558
1559 if (!lowerSwitchWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB, MIB))
1560 return false;
1561 }
1562 return true;
1563}
1564
1565void IRTranslatorImpl::splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
1567 Value *Cond, MachineBasicBlock *SwitchMBB,
1568 MachineIRBuilder &MIB) {
1569 using namespace SwitchCG;
1570 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
1571 "Clusters not sorted?");
1572 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
1573
1574 auto [LastLeft, FirstRight, LeftProb, RightProb] =
1575 SL->computeSplitWorkItemInfo(W);
1576
1577 // Use the first element on the right as pivot since we will make less-than
1578 // comparisons against it.
1579 CaseClusterIt PivotCluster = FirstRight;
1580 assert(PivotCluster > W.FirstCluster);
1581 assert(PivotCluster <= W.LastCluster);
1582
1583 CaseClusterIt FirstLeft = W.FirstCluster;
1584 CaseClusterIt LastRight = W.LastCluster;
1585
1586 const ConstantInt *Pivot = PivotCluster->Low;
1587
1588 // New blocks will be inserted immediately after the current one.
1590 ++BBI;
1591
1592 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
1593 // we can branch to its destination directly if it's squeezed exactly in
1594 // between the known lower bound and Pivot - 1.
1595 MachineBasicBlock *LeftMBB;
1596 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
1597 FirstLeft->Low == W.GE &&
1598 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
1599 LeftMBB = FirstLeft->MBB;
1600 } else {
1601 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
1602 FuncInfo.MF->insert(BBI, LeftMBB);
1603 WorkList.push_back(
1604 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
1605 }
1606
1607 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
1608 // single cluster, RHS.Low == Pivot, and we can branch to its destination
1609 // directly if RHS.High equals the current upper bound.
1610 MachineBasicBlock *RightMBB;
1611 if (FirstRight == LastRight && FirstRight->Kind == CC_Range && W.LT &&
1612 (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
1613 RightMBB = FirstRight->MBB;
1614 } else {
1615 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
1616 FuncInfo.MF->insert(BBI, RightMBB);
1617 WorkList.push_back(
1618 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
1619 }
1620
1621 // Create the CaseBlock record that will be used to lower the branch.
1622 CaseBlock CB(ICmpInst::Predicate::ICMP_SLT, false, Cond, Pivot, nullptr,
1623 LeftMBB, RightMBB, W.MBB, MIB.getDebugLoc(), LeftProb,
1624 RightProb);
1625
1626 if (W.MBB == SwitchMBB)
1627 emitSwitchCase(CB, SwitchMBB, MIB);
1628 else
1629 SL->SwitchCases.push_back(CB);
1630}
1631
1632void IRTranslatorImpl::emitJumpTable(SwitchCG::JumpTable &JT,
1634 // Emit the code for the jump table
1635 assert(JT.Reg && "Should lower JT Header first!");
1636 MachineIRBuilder MIB(*MBB->getParent());
1637 MIB.setMBB(*MBB);
1638 MIB.setDebugLoc(CurBuilder->getDebugLoc());
1639
1640 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
1641 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
1642
1643 auto Table = MIB.buildJumpTable(PtrTy, JT.JTI);
1644 MIB.buildBrJT(Table.getReg(0), JT.JTI, JT.Reg);
1645}
1646
1647bool IRTranslatorImpl::emitJumpTableHeader(SwitchCG::JumpTable &JT,
1649 MachineBasicBlock *HeaderBB) {
1650 MachineIRBuilder MIB(*HeaderBB->getParent());
1651 MIB.setMBB(*HeaderBB);
1652 MIB.setDebugLoc(CurBuilder->getDebugLoc());
1653
1654 const Value &SValue = *JTH.SValue;
1655 // Subtract the lowest switch case value from the value being switched on.
1656 const LLT SwitchTy = getLLTForType(*SValue.getType(), *DL);
1657 Register SwitchOpReg = getOrCreateVReg(SValue);
1658 auto FirstCst = MIB.buildConstant(SwitchTy, JTH.First);
1659 auto Sub = MIB.buildSub({SwitchTy}, SwitchOpReg, FirstCst);
1660
1661 // This value may be smaller or larger than the target's pointer type, and
1662 // therefore require extension or truncating.
1663 auto *PtrIRTy = PointerType::getUnqual(SValue.getContext());
1664 const LLT PtrScalarTy = LLT::integer(DL->getTypeSizeInBits(PtrIRTy));
1665 auto Index = MIB.buildZExtOrTrunc(PtrScalarTy, Sub);
1666
1667 JT.Reg = Index.getReg(0);
1668
1669 if (JTH.FallthroughUnreachable) {
1670 if (JT.MBB != HeaderBB->getNextNode())
1671 MIB.buildBr(*JT.MBB);
1672 return true;
1673 }
1674
1675 // Emit the range check for the jump table, and branch to the default block
1676 // for the switch statement if the value being switched on exceeds the
1677 // largest case in the switch.
1678 auto Cst = getOrCreateVReg(
1679 *ConstantInt::get(SValue.getType(), JTH.Last - JTH.First));
1680 auto Cmp = MIB.buildICmp(CmpInst::ICMP_UGT, LLT::integer(1), Sub, Cst);
1681
1682 auto BrCond = MIB.buildBrCond(Cmp.getReg(0), *JT.Default);
1683
1684 // Avoid emitting unnecessary branches to the next block.
1685 if (JT.MBB != HeaderBB->getNextNode())
1686 BrCond = MIB.buildBr(*JT.MBB);
1687 return true;
1688}
1689
1690void IRTranslatorImpl::emitSwitchCase(SwitchCG::CaseBlock &CB,
1691 MachineBasicBlock *SwitchBB,
1692 MachineIRBuilder &MIB) {
1693 Register CondLHS = getOrCreateVReg(*CB.CmpLHS);
1694 Register Cond;
1695 DebugLoc OldDbgLoc = MIB.getDebugLoc();
1696 MIB.setDebugLoc(CB.DbgLoc);
1697 MIB.setMBB(*CB.ThisBB);
1698
1699 if (CB.PredInfo.NoCmp) {
1700 // Branch or fall through to TrueBB.
1701 addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
1702 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
1703 CB.ThisBB);
1705 if (CB.TrueBB != CB.ThisBB->getNextNode())
1706 MIB.buildBr(*CB.TrueBB);
1707 MIB.setDebugLoc(OldDbgLoc);
1708 return;
1709 }
1710
1711 const LLT i1Ty = LLT::integer(1);
1712 // Build the compare.
1713 if (!CB.CmpMHS) {
1714 const auto *CI = dyn_cast<ConstantInt>(CB.CmpRHS);
1715 // For conditional branch lowering, we might try to do something silly like
1716 // emit an G_ICMP to compare an existing G_ICMP i1 result with true. If so,
1717 // just re-use the existing condition vreg.
1718 if (MRI->getType(CondLHS).getSizeInBits() == 1 && CI && CI->isOne() &&
1720 Cond = CondLHS;
1721 } else {
1722 Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
1724 Cond =
1725 MIB.buildFCmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
1726 else
1727 Cond =
1728 MIB.buildICmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
1729 }
1730 } else {
1732 "Can only handle SLE ranges");
1733
1734 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1735 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
1736
1737 Register CmpOpReg = getOrCreateVReg(*CB.CmpMHS);
1738 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1739 Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
1740 Cond =
1741 MIB.buildICmp(CmpInst::ICMP_SLE, i1Ty, CmpOpReg, CondRHS).getReg(0);
1742 } else {
1743 const LLT CmpTy = MRI->getType(CmpOpReg);
1744 auto Sub = MIB.buildSub({CmpTy}, CmpOpReg, CondLHS);
1745 auto Diff = MIB.buildConstant(CmpTy, High - Low);
1746 Cond = MIB.buildICmp(CmpInst::ICMP_ULE, i1Ty, Sub, Diff).getReg(0);
1747 }
1748 }
1749
1750 // Update successor info
1751 addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
1752
1753 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
1754 CB.ThisBB);
1755
1756 // TrueBB and FalseBB are always different unless the incoming IR is
1757 // degenerate. This only happens when running llc on weird IR.
1758 if (CB.TrueBB != CB.FalseBB)
1759 addSuccessorWithProb(CB.ThisBB, CB.FalseBB, CB.FalseProb);
1761
1762 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.FalseBB->getBasicBlock()},
1763 CB.ThisBB);
1764
1765 MIB.buildBrCond(Cond, *CB.TrueBB);
1766 MIB.buildBr(*CB.FalseBB);
1767 MIB.setDebugLoc(OldDbgLoc);
1768}
1769
1770bool IRTranslatorImpl::lowerJumpTableWorkItem(
1772 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
1775 MachineBasicBlock *Fallthrough, bool FallthroughUnreachable) {
1776 using namespace SwitchCG;
1777 MachineFunction *CurMF = SwitchMBB->getParent();
1778 // FIXME: Optimize away range check based on pivot comparisons.
1779 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
1780 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
1781 BranchProbability DefaultProb = W.DefaultProb;
1782
1783 // The jump block hasn't been inserted yet; insert it here.
1784 MachineBasicBlock *JumpMBB = JT->MBB;
1785 CurMF->insert(BBI, JumpMBB);
1786
1787 // Since the jump table block is separate from the switch block, we need
1788 // to keep track of it as a machine predecessor to the default block,
1789 // otherwise we lose the phi edges.
1790 addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
1791 CurMBB);
1792 addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
1793 JumpMBB);
1794
1795 auto JumpProb = I->Prob;
1796 auto FallthroughProb = UnhandledProbs;
1797
1798 // If the default statement is a target of the jump table, we evenly
1799 // distribute the default probability to successors of CurMBB. Also
1800 // update the probability on the edge from JumpMBB to Fallthrough.
1801 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
1802 SE = JumpMBB->succ_end();
1803 SI != SE; ++SI) {
1804 if (*SI == DefaultMBB) {
1805 JumpProb += DefaultProb / 2;
1806 FallthroughProb -= DefaultProb / 2;
1807 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
1808 JumpMBB->normalizeSuccProbs();
1809 } else {
1810 // Also record edges from the jump table block to it's successors.
1811 addMachineCFGPred({SwitchMBB->getBasicBlock(), (*SI)->getBasicBlock()},
1812 JumpMBB);
1813 }
1814 }
1815
1816 if (FallthroughUnreachable)
1817 JTH->FallthroughUnreachable = true;
1818
1819 if (!JTH->FallthroughUnreachable)
1820 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
1821 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
1822 CurMBB->normalizeSuccProbs();
1823
1824 // The jump table header will be inserted in our current block, do the
1825 // range check, and fall through to our fallthrough block.
1826 JTH->HeaderBB = CurMBB;
1827 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
1828
1829 // If we're in the right place, emit the jump table header right now.
1830 if (CurMBB == SwitchMBB) {
1831 if (!emitJumpTableHeader(*JT, *JTH, CurMBB))
1832 return false;
1833 JTH->Emitted = true;
1834 }
1835 return true;
1836}
1837bool IRTranslatorImpl::lowerSwitchRangeWorkItem(
1839 bool FallthroughUnreachable, BranchProbability UnhandledProbs,
1840 MachineBasicBlock *CurMBB, MachineIRBuilder &MIB,
1841 MachineBasicBlock *SwitchMBB) {
1842 using namespace SwitchCG;
1843 const Value *RHS, *LHS, *MHS;
1844 CmpInst::Predicate Pred;
1845 if (I->Low == I->High) {
1846 // Check Cond == I->Low.
1847 Pred = CmpInst::ICMP_EQ;
1848 LHS = Cond;
1849 RHS = I->Low;
1850 MHS = nullptr;
1851 } else {
1852 // Check I->Low <= Cond <= I->High.
1853 Pred = CmpInst::ICMP_SLE;
1854 LHS = I->Low;
1855 MHS = Cond;
1856 RHS = I->High;
1857 }
1858
1859 // If Fallthrough is unreachable, fold away the comparison.
1860 // The false probability is the sum of all unhandled cases.
1861 CaseBlock CB(Pred, FallthroughUnreachable, LHS, RHS, MHS, I->MBB, Fallthrough,
1862 CurMBB, MIB.getDebugLoc(), I->Prob, UnhandledProbs);
1863
1864 emitSwitchCase(CB, SwitchMBB, MIB);
1865 return true;
1866}
1867
1868void IRTranslatorImpl::emitBitTestHeader(SwitchCG::BitTestBlock &B,
1869 MachineBasicBlock *SwitchBB) {
1870 MachineIRBuilder &MIB = *CurBuilder;
1871 MIB.setMBB(*SwitchBB);
1872
1873 // Subtract the minimum value.
1874 Register SwitchOpReg = getOrCreateVReg(*B.SValue);
1875
1876 LLT SwitchOpTy = MRI->getType(SwitchOpReg);
1877 Register MinValReg = MIB.buildConstant(SwitchOpTy, B.First).getReg(0);
1878 auto RangeSub = MIB.buildSub(SwitchOpTy, SwitchOpReg, MinValReg);
1879
1880 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
1881 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
1882
1883 LLT MaskTy = SwitchOpTy;
1884 if (MaskTy.getSizeInBits() > PtrTy.getSizeInBits() ||
1886 MaskTy = LLT::integer(PtrTy.getSizeInBits());
1887 else {
1888 // Ensure that the type will fit the mask value.
1889 for (const SwitchCG::BitTestCase &Case : B.Cases) {
1890 if (!isUIntN(SwitchOpTy.getSizeInBits(), Case.Mask)) {
1891 // Switch table case range are encoded into series of masks.
1892 // Just use pointer type, it's guaranteed to fit.
1893 MaskTy = LLT::integer(PtrTy.getSizeInBits());
1894 break;
1895 }
1896 }
1897 }
1898 Register SubReg = RangeSub.getReg(0);
1899 if (SwitchOpTy != MaskTy)
1900 SubReg = MIB.buildZExtOrTrunc(MaskTy, SubReg).getReg(0);
1901
1902 B.RegVT = getMVTForLLT(MaskTy);
1903 B.Reg = SubReg;
1904
1905 MachineBasicBlock *MBB = B.Cases[0].ThisBB;
1906
1907 if (!B.FallthroughUnreachable)
1908 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
1909 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
1910
1911 SwitchBB->normalizeSuccProbs();
1912
1913 if (!B.FallthroughUnreachable) {
1914 // Conditional branch to the default block.
1915 auto RangeCst = MIB.buildConstant(SwitchOpTy, B.Range);
1916 auto RangeCmp = MIB.buildICmp(CmpInst::Predicate::ICMP_UGT, LLT::integer(1),
1917 RangeSub, RangeCst);
1918 MIB.buildBrCond(RangeCmp, *B.Default);
1919 }
1920
1921 // Avoid emitting unnecessary branches to the next block.
1922 if (MBB != SwitchBB->getNextNode())
1923 MIB.buildBr(*MBB);
1924}
1925
1926void IRTranslatorImpl::emitBitTestCase(SwitchCG::BitTestBlock &BB,
1927 MachineBasicBlock *NextMBB,
1928 BranchProbability BranchProbToNext,
1930 MachineBasicBlock *SwitchBB) {
1931 MachineIRBuilder &MIB = *CurBuilder;
1932 MIB.setMBB(*SwitchBB);
1933
1934 LLT SwitchTy = getLLTForMVT(BB.RegVT);
1935 Register Cmp;
1936 unsigned PopCount = llvm::popcount(B.Mask);
1937 if (PopCount == 1) {
1938 // Testing for a single bit; just compare the shift count with what it
1939 // would need to be to shift a 1 bit in that position.
1940 auto MaskTrailingZeros =
1941 MIB.buildConstant(SwitchTy, llvm::countr_zero(B.Mask));
1943 MaskTrailingZeros)
1944 .getReg(0);
1945 } else if (PopCount == BB.Range) {
1946 // There is only one zero bit in the range, test for it directly.
1947 auto MaskTrailingOnes =
1948 MIB.buildConstant(SwitchTy, llvm::countr_one(B.Mask));
1949 Cmp =
1950 MIB.buildICmp(CmpInst::ICMP_NE, LLT::integer(1), Reg, MaskTrailingOnes)
1951 .getReg(0);
1952 } else {
1953 // Make desired shift.
1954 auto CstOne = MIB.buildConstant(SwitchTy, 1);
1955 auto SwitchVal = MIB.buildShl(SwitchTy, CstOne, Reg);
1956
1957 // Emit bit tests and jumps.
1958 auto CstMask = MIB.buildConstant(SwitchTy, B.Mask);
1959 auto AndOp = MIB.buildAnd(SwitchTy, SwitchVal, CstMask);
1960 auto CstZero = MIB.buildConstant(SwitchTy, 0);
1961 Cmp = MIB.buildICmp(CmpInst::ICMP_NE, LLT::integer(1), AndOp, CstZero)
1962 .getReg(0);
1963 }
1964
1965 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
1966 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
1967 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
1968 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
1969 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
1970 // one as they are relative probabilities (and thus work more like weights),
1971 // and hence we need to normalize them to let the sum of them become one.
1972 SwitchBB->normalizeSuccProbs();
1973
1974 // Record the fact that the IR edge from the header to the bit test target
1975 // will go through our new block. Neeeded for PHIs to have nodes added.
1976 addMachineCFGPred({BB.Parent->getBasicBlock(), B.TargetBB->getBasicBlock()},
1977 SwitchBB);
1978
1979 MIB.buildBrCond(Cmp, *B.TargetBB);
1980
1981 // Avoid emitting unnecessary branches to the next block.
1982 if (NextMBB != SwitchBB->getNextNode())
1983 MIB.buildBr(*NextMBB);
1984}
1985
1986bool IRTranslatorImpl::lowerBitTestWorkItem(
1988 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
1990 BranchProbability DefaultProb, BranchProbability UnhandledProbs,
1992 bool FallthroughUnreachable) {
1993 using namespace SwitchCG;
1994 MachineFunction *CurMF = SwitchMBB->getParent();
1995 // FIXME: Optimize away range check based on pivot comparisons.
1996 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
1997 // The bit test blocks haven't been inserted yet; insert them here.
1998 for (BitTestCase &BTC : BTB->Cases)
1999 CurMF->insert(BBI, BTC.ThisBB);
2000
2001 // Fill in fields of the BitTestBlock.
2002 BTB->Parent = CurMBB;
2003 BTB->Default = Fallthrough;
2004
2005 BTB->DefaultProb = UnhandledProbs;
2006 // If the cases in bit test don't form a contiguous range, we evenly
2007 // distribute the probability on the edge to Fallthrough to two
2008 // successors of CurMBB.
2009 if (!BTB->ContiguousRange) {
2010 BTB->Prob += DefaultProb / 2;
2011 BTB->DefaultProb -= DefaultProb / 2;
2012 }
2013
2014 if (FallthroughUnreachable)
2015 BTB->FallthroughUnreachable = true;
2016
2017 // If we're in the right place, emit the bit test header right now.
2018 if (CurMBB == SwitchMBB) {
2019 emitBitTestHeader(*BTB, SwitchMBB);
2020 BTB->Emitted = true;
2021 }
2022 return true;
2023}
2024
2025bool IRTranslatorImpl::lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W,
2026 Value *Cond,
2027 MachineBasicBlock *SwitchMBB,
2028 MachineBasicBlock *DefaultMBB,
2029 MachineIRBuilder &MIB) {
2030 using namespace SwitchCG;
2031 MachineFunction *CurMF = FuncInfo.MF;
2032 MachineBasicBlock *NextMBB = nullptr;
2034 if (++BBI != FuncInfo.MF->end())
2035 NextMBB = &*BBI;
2036
2037 if (EnableOpts) {
2038 // Here, we order cases by probability so the most likely case will be
2039 // checked first. However, two clusters can have the same probability in
2040 // which case their relative ordering is non-deterministic. So we use Low
2041 // as a tie-breaker as clusters are guaranteed to never overlap.
2042 llvm::sort(W.FirstCluster, W.LastCluster + 1,
2043 [](const CaseCluster &a, const CaseCluster &b) {
2044 return a.Prob != b.Prob
2045 ? a.Prob > b.Prob
2046 : a.Low->getValue().slt(b.Low->getValue());
2047 });
2048
2049 // Rearrange the case blocks so that the last one falls through if possible
2050 // without changing the order of probabilities.
2051 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster;) {
2052 --I;
2053 if (I->Prob > W.LastCluster->Prob)
2054 break;
2055 if (I->Kind == CC_Range && I->MBB == NextMBB) {
2056 std::swap(*I, *W.LastCluster);
2057 break;
2058 }
2059 }
2060 }
2061
2062 // Compute total probability.
2063 BranchProbability DefaultProb = W.DefaultProb;
2064 BranchProbability UnhandledProbs = DefaultProb;
2065 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
2066 UnhandledProbs += I->Prob;
2067
2068 MachineBasicBlock *CurMBB = W.MBB;
2069 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
2070 bool FallthroughUnreachable = false;
2071 MachineBasicBlock *Fallthrough;
2072 if (I == W.LastCluster) {
2073 // For the last cluster, fall through to the default destination.
2074 Fallthrough = DefaultMBB;
2075 FallthroughUnreachable =
2077 } else {
2078 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
2079 CurMF->insert(BBI, Fallthrough);
2080 }
2081 UnhandledProbs -= I->Prob;
2082
2083 switch (I->Kind) {
2084 case CC_BitTests: {
2085 if (!lowerBitTestWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
2086 DefaultProb, UnhandledProbs, I, Fallthrough,
2087 FallthroughUnreachable)) {
2088 LLVM_DEBUG(dbgs() << "Failed to lower bit test for switch");
2089 return false;
2090 }
2091 break;
2092 }
2093
2094 case CC_JumpTable: {
2095 if (!lowerJumpTableWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
2096 UnhandledProbs, I, Fallthrough,
2097 FallthroughUnreachable)) {
2098 LLVM_DEBUG(dbgs() << "Failed to lower jump table");
2099 return false;
2100 }
2101 break;
2102 }
2103 case CC_Range: {
2104 if (!lowerSwitchRangeWorkItem(I, Cond, Fallthrough,
2105 FallthroughUnreachable, UnhandledProbs,
2106 CurMBB, MIB, SwitchMBB)) {
2107 LLVM_DEBUG(dbgs() << "Failed to lower switch range");
2108 return false;
2109 }
2110 break;
2111 }
2112 }
2113 CurMBB = Fallthrough;
2114 }
2115
2116 return true;
2117}
2118
2119bool IRTranslatorImpl::translateIndirectBr(const User &U,
2120 MachineIRBuilder &MIRBuilder) {
2121 const IndirectBrInst &BrInst = cast<IndirectBrInst>(U);
2122
2123 const Register Tgt = getOrCreateVReg(*BrInst.getAddress());
2124 MIRBuilder.buildBrIndirect(Tgt);
2125
2126 // Link successors.
2127 SmallPtrSet<const BasicBlock *, 32> AddedSuccessors;
2128 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
2129 for (const BasicBlock *Succ : successors(&BrInst)) {
2130 // It's legal for indirectbr instructions to have duplicate blocks in the
2131 // destination list. We don't allow this in MIR. Skip anything that's
2132 // already a successor.
2133 if (!AddedSuccessors.insert(Succ).second)
2134 continue;
2135 CurBB.addSuccessor(&getMBB(*Succ));
2136 }
2137
2138 return true;
2139}
2140
2141static bool isSwiftError(const Value *V) {
2142 if (auto Arg = dyn_cast<Argument>(V))
2143 return Arg->hasSwiftErrorAttr();
2144 if (auto AI = dyn_cast<AllocaInst>(V))
2145 return AI->isSwiftError();
2146 return false;
2147}
2148
2149bool IRTranslatorImpl::translateLoad(const User &U,
2150 MachineIRBuilder &MIRBuilder) {
2151 const LoadInst &LI = cast<LoadInst>(U);
2152 TypeSize StoreSize = DL->getTypeStoreSize(LI.getType());
2153 if (StoreSize.isZero())
2154 return true;
2155
2156 ArrayRef<Register> Regs = getOrCreateVRegs(LI);
2157 Register Base = getOrCreateVReg(*LI.getPointerOperand());
2158 AAMDNodes AAInfo = LI.getAAMetadata();
2159
2160 const Value *Ptr = LI.getPointerOperand();
2161
2162 if (CLI->supportSwiftError() && isSwiftError(Ptr)) {
2163 assert(Regs.size() == 1 && "swifterror should be single pointer");
2164 Register VReg =
2165 SwiftError.getOrCreateVRegUseAt(&LI, &MIRBuilder.getMBB(), Ptr);
2166 MIRBuilder.buildCopy(Regs[0], VReg);
2167 return true;
2168 }
2169
2171 TLI->getLoadMemOperandFlags(LI, *DL, AC, LibInfo, OptLevel);
2172 if (AA && !(Flags & MachineMemOperand::MOInvariant)) {
2173 if (AA->pointsToConstantMemory(
2174 MemoryLocation(Ptr, LocationSize::precise(StoreSize), AAInfo))) {
2176 }
2177 }
2178
2179 // Fast-path the common single-register load.
2180 if (Regs.size() == 1) {
2181 auto *MMO = MF->getMachineMemOperand(
2182 MachinePointerInfo(LI.getPointerOperand()), Flags,
2183 MRI->getType(Regs[0]), getMemOpAlign(LI),
2184 MMOMetadata(AAInfo, LI.getMetadata(LLVMContext::MD_range)),
2185 LI.getSyncScopeID(), LI.getOrdering());
2186 MIRBuilder.buildLoad(Regs[0], Base, *MMO);
2187 return true;
2188 }
2189
2190 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(LI);
2191 Type *OffsetIRTy = DL->getIndexType(Ptr->getType());
2192 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
2193 for (unsigned i = 0; i < Regs.size(); ++i) {
2194 Register Addr;
2195 MIRBuilder.materializeObjectPtrOffset(Addr, Base, OffsetTy, Offsets[i]);
2196
2197 MachinePointerInfo Ptr(LI.getPointerOperand(), Offsets[i]);
2198 Align BaseAlign = getMemOpAlign(LI);
2199 auto *MMO =
2200 MF->getMachineMemOperand(Ptr, Flags, MRI->getType(Regs[i]),
2201 commonAlignment(BaseAlign, Offsets[i]), AAInfo,
2202 LI.getSyncScopeID(), LI.getOrdering());
2203 MIRBuilder.buildLoad(Regs[i], Addr, *MMO);
2204 }
2205
2206 return true;
2207}
2208
2209bool IRTranslatorImpl::translateStore(const User &U,
2210 MachineIRBuilder &MIRBuilder) {
2211 const StoreInst &SI = cast<StoreInst>(U);
2212 if (DL->getTypeStoreSize(SI.getValueOperand()->getType()).isZero())
2213 return true;
2214
2215 ArrayRef<Register> Vals = getOrCreateVRegs(*SI.getValueOperand());
2216 Register Base = getOrCreateVReg(*SI.getPointerOperand());
2217
2218 if (CLI->supportSwiftError() && isSwiftError(SI.getPointerOperand())) {
2219 assert(Vals.size() == 1 && "swifterror should be single pointer");
2220
2221 Register VReg = SwiftError.getOrCreateVRegDefAt(&SI, &MIRBuilder.getMBB(),
2222 SI.getPointerOperand());
2223 MIRBuilder.buildCopy(VReg, Vals[0]);
2224 return true;
2225 }
2226
2227 MachineMemOperand::Flags Flags = TLI->getStoreMemOperandFlags(SI, *DL);
2228 // Fast-path the common single-register store.
2229 if (Vals.size() == 1) {
2230 auto *MMO = MF->getMachineMemOperand(
2231 MachinePointerInfo(SI.getPointerOperand()), Flags,
2232 MRI->getType(Vals[0]), getMemOpAlign(SI), SI.getAAMetadata(),
2233 SI.getSyncScopeID(), SI.getOrdering());
2234 MIRBuilder.buildStore(Vals[0], Base, *MMO);
2235 return true;
2236 }
2237
2238 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*SI.getValueOperand());
2239 Type *OffsetIRTy = DL->getIndexType(SI.getPointerOperandType());
2240 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
2241 for (unsigned i = 0; i < Vals.size(); ++i) {
2242 Register Addr;
2243 MIRBuilder.materializeObjectPtrOffset(Addr, Base, OffsetTy, Offsets[i]);
2244
2245 MachinePointerInfo Ptr(SI.getPointerOperand(), Offsets[i]);
2246 Align BaseAlign = getMemOpAlign(SI);
2247 auto *MMO = MF->getMachineMemOperand(Ptr, Flags, MRI->getType(Vals[i]),
2248 commonAlignment(BaseAlign, Offsets[i]),
2249 SI.getAAMetadata(),
2250 SI.getSyncScopeID(), SI.getOrdering());
2251 MIRBuilder.buildStore(Vals[i], Addr, *MMO);
2252 }
2253 return true;
2254}
2255
2257 const Value *Src = U.getOperand(0);
2258 Type *Int32Ty = Type::getInt32Ty(U.getContext());
2259
2260 // getIndexedOffsetInType is designed for GEPs, so the first index is the
2261 // usual array element rather than looking into the actual aggregate.
2263 Indices.push_back(ConstantInt::get(Int32Ty, 0));
2264
2265 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
2266 for (auto Idx : EVI->indices())
2267 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
2268 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
2269 for (auto Idx : IVI->indices())
2270 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
2271 } else {
2272 llvm::append_range(Indices, drop_begin(U.operands()));
2273 }
2274
2275 return static_cast<uint64_t>(
2276 DL.getIndexedOffsetInType(Src->getType(), Indices));
2277}
2278
2279bool IRTranslatorImpl::translateExtractValue(const User &U,
2280 MachineIRBuilder &MIRBuilder) {
2281 const Value *Src = U.getOperand(0);
2283 ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
2284 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*Src);
2285 unsigned Idx = llvm::lower_bound(Offsets, Offset) - Offsets.begin();
2286 auto &DstRegs = allocateVRegs(U);
2287
2288 for (unsigned i = 0; i < DstRegs.size(); ++i)
2289 DstRegs[i] = SrcRegs[Idx++];
2290
2291 return true;
2292}
2293
2294bool IRTranslatorImpl::translateInsertValue(const User &U,
2295 MachineIRBuilder &MIRBuilder) {
2296 const Value *Src = U.getOperand(0);
2298 auto &DstRegs = allocateVRegs(U);
2299 ArrayRef<uint64_t> DstOffsets = *VMap.getOffsets(U);
2300 ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
2301 ArrayRef<Register> InsertedRegs = getOrCreateVRegs(*U.getOperand(1));
2302 auto *InsertedIt = InsertedRegs.begin();
2303
2304 for (unsigned i = 0; i < DstRegs.size(); ++i) {
2305 if (DstOffsets[i] >= Offset && InsertedIt != InsertedRegs.end())
2306 DstRegs[i] = *InsertedIt++;
2307 else
2308 DstRegs[i] = SrcRegs[i];
2309 }
2310
2311 return true;
2312}
2313
2314bool IRTranslatorImpl::translateSelect(const User &U,
2315 MachineIRBuilder &MIRBuilder) {
2316 Register Tst = getOrCreateVReg(*U.getOperand(0));
2317 ArrayRef<Register> ResRegs = getOrCreateVRegs(U);
2318 ArrayRef<Register> Op0Regs = getOrCreateVRegs(*U.getOperand(1));
2319 ArrayRef<Register> Op1Regs = getOrCreateVRegs(*U.getOperand(2));
2320
2321 uint32_t Flags = 0;
2322 if (const SelectInst *SI = dyn_cast<SelectInst>(&U))
2324
2325 for (unsigned i = 0; i < ResRegs.size(); ++i) {
2326 MIRBuilder.buildSelect(ResRegs[i], Tst, Op0Regs[i], Op1Regs[i], Flags);
2327 }
2328
2329 return true;
2330}
2331
2332bool IRTranslatorImpl::translateCopy(const User &U, const Value &V,
2333 MachineIRBuilder &MIRBuilder) {
2334 return translateCopy(U, getOrCreateVReg(V), MIRBuilder);
2335}
2336
2337bool IRTranslatorImpl::translateCopy(const User &U, Register Src,
2338 MachineIRBuilder &MIRBuilder) {
2339 auto &Regs = *VMap.getVRegs(U);
2340 if (Regs.empty()) {
2341 Regs.push_back(Src);
2342 } else {
2343 // If we already assigned a vreg for this instruction, we can't change that.
2344 // Emit a copy to satisfy the users we already emitted.
2345 MIRBuilder.buildCopy(Regs[0], Src);
2346 }
2347 return true;
2348}
2349
2350bool IRTranslatorImpl::translateBitCast(const User &U,
2351 MachineIRBuilder &MIRBuilder) {
2352 Type *SrcTy = U.getOperand(0)->getType();
2353 Type *DstTy = U.getType();
2354
2355 // If we're bitcasting to the source type, we can reuse the source vreg.
2356 if (getLLTForType(*SrcTy, *DL) == getLLTForType(*DstTy, *DL)) {
2357 // If the source is a ConstantInt then it was probably created by
2358 // ConstantHoisting and we should leave it alone.
2359 if (isa<ConstantInt>(U.getOperand(0)))
2360 return translateCast(TargetOpcode::G_CONSTANT_FOLD_BARRIER, U,
2361 MIRBuilder);
2362 return translateCopy(U, *U.getOperand(0), MIRBuilder);
2363 }
2364
2365 // The IR only allows pointer/non-pointer bitcasts with byte types, but
2366 // G_BITCAST can't convert between pointers and other types. Go through an
2367 // integer with the pointer's shape instead: `bitcast <2 x b32> to ptr`
2368 // becomes a G_BITCAST to i64 and a G_INTTOPTR.
2369 if (SrcTy->isPtrOrPtrVectorTy() != DstTy->isPtrOrPtrVectorTy()) {
2370 assert((SrcTy->isByteOrByteVectorTy() || DstTy->isByteOrByteVectorTy()) &&
2371 "only byte types can be bitcast to or from pointers");
2372 Type *PtrIRTy = SrcTy->isPtrOrPtrVectorTy() ? SrcTy : DstTy;
2373 LLT IntTy = getLLTForType(*DL->getIntPtrType(PtrIRTy), *DL);
2374 Register Src = getOrCreateVReg(*U.getOperand(0));
2375 Register Dst = getOrCreateVReg(U);
2376 if (MRI->getType(Src) != IntTy && MRI->getType(Dst) != IntTy)
2377 Src = MIRBuilder.buildCast(IntTy, Src).getReg(0);
2378 MIRBuilder.buildCast(Dst, Src);
2379 return true;
2380 }
2381
2382 return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
2383}
2384
2385bool IRTranslatorImpl::translateCast(unsigned Opcode, const User &U,
2386 MachineIRBuilder &MIRBuilder) {
2387 if (!mayTranslateUserTypes(U))
2388 return false;
2389
2390 uint32_t Flags = 0;
2391 if (const Instruction *I = dyn_cast<Instruction>(&U))
2393
2394 Register Op = getOrCreateVReg(*U.getOperand(0));
2395 Register Res = getOrCreateVReg(U);
2396 MIRBuilder.buildInstr(Opcode, {Res}, {Op}, Flags);
2397 return true;
2398}
2399
2400bool IRTranslatorImpl::translateGetElementPtr(const User &U,
2401 MachineIRBuilder &MIRBuilder) {
2402 Value &Op0 = *U.getOperand(0);
2403 Register BaseReg = getOrCreateVReg(Op0);
2404 Type *PtrIRTy = Op0.getType();
2405 LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
2406 Type *OffsetIRTy = DL->getIndexType(PtrIRTy);
2407 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
2408
2409 uint32_t PtrAddFlags = 0;
2410 // Each PtrAdd generated to implement the GEP inherits its nuw, nusw, inbounds
2411 // flags.
2412 if (const Instruction *I = dyn_cast<Instruction>(&U))
2414
2415 auto PtrAddFlagsWithConst = [&](int64_t Offset) {
2416 // For nusw/inbounds GEP with an offset that is nonnegative when interpreted
2417 // as signed, assume there is no unsigned overflow.
2418 if (Offset >= 0 && (PtrAddFlags & MachineInstr::MIFlag::NoUSWrap))
2419 return PtrAddFlags | MachineInstr::MIFlag::NoUWrap;
2420 return PtrAddFlags;
2421 };
2422
2423 // Normalize Vector GEP - all scalar operands should be converted to the
2424 // splat vector.
2425 unsigned VectorWidth = 0;
2426
2427 // True if we should use a splat vector; using VectorWidth alone is not
2428 // sufficient.
2429 bool WantSplatVector = false;
2430 if (auto *VT = dyn_cast<VectorType>(U.getType())) {
2431 VectorWidth = cast<FixedVectorType>(VT)->getNumElements();
2432 // We don't produce 1 x N vectors; those are treated as scalars.
2433 WantSplatVector = VectorWidth > 1;
2434 }
2435
2436 if (cast<GEPOperator>(U).hasAllZeroIndices())
2437 return translateCopy(U, BaseReg, MIRBuilder);
2438
2439 // We might need to splat the base pointer into a vector if the offsets
2440 // are vectors.
2441 if (WantSplatVector && !PtrTy.isVector()) {
2442 BaseReg = MIRBuilder
2443 .buildSplatBuildVector(LLT::fixed_vector(VectorWidth, PtrTy),
2444 BaseReg)
2445 .getReg(0);
2446 PtrIRTy = FixedVectorType::get(PtrIRTy, VectorWidth);
2447 PtrTy = getLLTForType(*PtrIRTy, *DL);
2448 OffsetIRTy = DL->getIndexType(PtrIRTy);
2449 OffsetTy = getLLTForType(*OffsetIRTy, *DL);
2450 }
2451
2452 int64_t Offset = 0;
2453 for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
2454 GTI != E; ++GTI) {
2455 const Value *Idx = GTI.getOperand();
2456 if (StructType *StTy = GTI.getStructTypeOrNull()) {
2457 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
2458 Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
2459 continue;
2460 } else {
2461 uint64_t ElementSize = GTI.getSequentialElementStride(*DL);
2462
2463 // If this is a scalar constant or a splat vector of constants,
2464 // handle it quickly.
2465 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
2466 if (std::optional<int64_t> Val = CI->getValue().trySExtValue()) {
2467 Offset += ElementSize * *Val;
2468 continue;
2469 }
2470 }
2471
2472 if (Offset != 0) {
2473 auto OffsetMIB = MIRBuilder.buildConstant({OffsetTy}, Offset);
2474 BaseReg = MIRBuilder
2475 .buildPtrAdd(PtrTy, BaseReg, OffsetMIB.getReg(0),
2476 PtrAddFlagsWithConst(Offset))
2477 .getReg(0);
2478 Offset = 0;
2479 }
2480
2481 Register IdxReg = getOrCreateVReg(*Idx);
2482 LLT IdxTy = MRI->getType(IdxReg);
2483 if (IdxTy != OffsetTy) {
2484 if (!IdxTy.isVector() && WantSplatVector) {
2485 IdxReg = MIRBuilder
2487 IdxReg)
2488 .getReg(0);
2489 }
2490
2491 IdxReg = MIRBuilder.buildSExtOrTrunc(OffsetTy, IdxReg).getReg(0);
2492 }
2493
2494 // N = N + Idx * ElementSize;
2495 // Avoid doing it for ElementSize of 1.
2496 Register GepOffsetReg;
2497 if (ElementSize != 1) {
2498 auto ElementSizeMIB = MIRBuilder.buildConstant(
2499 getLLTForType(*OffsetIRTy, *DL), ElementSize);
2500
2501 // The multiplication is NUW if the GEP is NUW and NSW if the GEP is
2502 // NUSW.
2503 uint32_t ScaleFlags = PtrAddFlags & MachineInstr::MIFlag::NoUWrap;
2504 if (PtrAddFlags & MachineInstr::MIFlag::NoUSWrap)
2505 ScaleFlags |= MachineInstr::MIFlag::NoSWrap;
2506
2507 GepOffsetReg =
2508 MIRBuilder.buildMul(OffsetTy, IdxReg, ElementSizeMIB, ScaleFlags)
2509 .getReg(0);
2510 } else {
2511 GepOffsetReg = IdxReg;
2512 }
2513
2514 BaseReg =
2515 MIRBuilder.buildPtrAdd(PtrTy, BaseReg, GepOffsetReg, PtrAddFlags)
2516 .getReg(0);
2517 }
2518 }
2519
2520 if (Offset != 0) {
2521 auto OffsetMIB =
2522 MIRBuilder.buildConstant(OffsetTy, Offset);
2523
2524 MIRBuilder.buildPtrAdd(getOrCreateVReg(U), BaseReg, OffsetMIB.getReg(0),
2525 PtrAddFlagsWithConst(Offset));
2526 return true;
2527 }
2528
2529 return translateCopy(U, BaseReg, MIRBuilder);
2530}
2531
2532bool IRTranslatorImpl::translateMemFunc(const CallInst &CI,
2533 MachineIRBuilder &MIRBuilder,
2534 unsigned Opcode) {
2535 const Value *SrcPtr = CI.getArgOperand(1);
2536 // If the source is undef, then just emit a nop.
2537 if (isa<UndefValue>(SrcPtr))
2538 return true;
2539
2541
2542 unsigned MinPtrSize = UINT_MAX;
2543 for (auto AI = CI.arg_begin(), AE = CI.arg_end(); std::next(AI) != AE; ++AI) {
2544 Register SrcReg = getOrCreateVReg(**AI);
2545 LLT SrcTy = MRI->getType(SrcReg);
2546 if (SrcTy.isPointer())
2547 MinPtrSize = std::min<unsigned>(SrcTy.getSizeInBits(), MinPtrSize);
2548 SrcRegs.push_back(SrcReg);
2549 }
2550
2551 LLT SizeTy = LLT::integer(MinPtrSize);
2552
2553 // The size operand should be the minimum of the pointer sizes.
2554 Register &SizeOpReg = SrcRegs[SrcRegs.size() - 1];
2555 if (MRI->getType(SizeOpReg) != SizeTy)
2556 SizeOpReg = MIRBuilder.buildZExtOrTrunc(SizeTy, SizeOpReg).getReg(0);
2557
2558 auto ICall = MIRBuilder.buildInstr(Opcode);
2559 for (Register SrcReg : SrcRegs)
2560 ICall.addUse(SrcReg);
2561
2562 Align DstAlign;
2563 Align SrcAlign;
2564 unsigned IsVol =
2565 cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1))->getZExtValue();
2566
2567 ConstantInt *CopySize = nullptr;
2568
2569 if (auto *MCI = dyn_cast<MemCpyInst>(&CI)) {
2570 DstAlign = MCI->getDestAlign().valueOrOne();
2571 SrcAlign = MCI->getSourceAlign().valueOrOne();
2572 CopySize = dyn_cast<ConstantInt>(MCI->getArgOperand(2));
2573 } else if (auto *MMI = dyn_cast<MemMoveInst>(&CI)) {
2574 DstAlign = MMI->getDestAlign().valueOrOne();
2575 SrcAlign = MMI->getSourceAlign().valueOrOne();
2576 CopySize = dyn_cast<ConstantInt>(MMI->getArgOperand(2));
2577 } else {
2578 auto *MSI = cast<MemSetInst>(&CI);
2579 DstAlign = MSI->getDestAlign().valueOrOne();
2580 }
2581
2582 if (Opcode != TargetOpcode::G_MEMCPY_INLINE &&
2583 Opcode != TargetOpcode::G_MEMSET_INLINE) {
2584 // We need to propagate the tail call flag from the IR inst as an argument.
2585 // Otherwise, we have to pessimize and assume later that we cannot tail call
2586 // any memory intrinsics.
2587 ICall.addImm(CI.isTailCall() ? 1 : 0);
2588 }
2589
2590 // Create mem operands to store the alignment and volatile info.
2593 if (IsVol) {
2594 LoadFlags |= MachineMemOperand::MOVolatile;
2595 StoreFlags |= MachineMemOperand::MOVolatile;
2596 }
2597
2598 AAMDNodes AAInfo = CI.getAAMetadata();
2599 if (AA && CopySize &&
2600 AA->pointsToConstantMemory(MemoryLocation(
2601 SrcPtr, LocationSize::precise(CopySize->getZExtValue()), AAInfo))) {
2602 LoadFlags |= MachineMemOperand::MOInvariant;
2603
2604 // FIXME: pointsToConstantMemory probably does not imply dereferenceable,
2605 // but the previous usage implied it did. Probably should check
2606 // isDereferenceableAndAlignedPointer.
2608 }
2609
2610 ICall.addMemOperand(
2611 MF->getMachineMemOperand(MachinePointerInfo(CI.getArgOperand(0)),
2612 StoreFlags, 1, DstAlign, AAInfo));
2613 if (Opcode != TargetOpcode::G_MEMSET &&
2614 Opcode != TargetOpcode::G_MEMSET_INLINE)
2615 ICall.addMemOperand(MF->getMachineMemOperand(
2616 MachinePointerInfo(SrcPtr), LoadFlags, 1, SrcAlign, AAInfo));
2617
2618 return true;
2619}
2620
2621bool IRTranslatorImpl::translateTrap(const CallInst &CI,
2622 MachineIRBuilder &MIRBuilder,
2623 unsigned Opcode) {
2624 StringRef TrapFuncName =
2625 CI.getAttributes().getFnAttr("trap-func-name").getValueAsString();
2626 if (TrapFuncName.empty()) {
2627 MachineInstrBuilder MIB;
2628 if (Opcode == TargetOpcode::G_UBSANTRAP) {
2629 uint64_t Code = cast<ConstantInt>(CI.getOperand(0))->getZExtValue();
2630 MIB = MIRBuilder.buildInstr(Opcode, {}, ArrayRef<llvm::SrcOp>{Code});
2631 } else {
2632 MIB = MIRBuilder.buildInstr(Opcode);
2633 }
2634 if (CI.cannotMerge())
2636 return true;
2637 }
2638
2639 CallLowering::CallLoweringInfo Info;
2640 if (Opcode == TargetOpcode::G_UBSANTRAP)
2641 Info.OrigArgs.push_back({getOrCreateVRegs(*CI.getArgOperand(0)),
2642 CI.getArgOperand(0)->getType(), 0});
2643
2644 Info.Callee = MachineOperand::CreateES(TrapFuncName.data());
2645 Info.CB = &CI;
2646 Info.OrigRet = {Register(), Type::getVoidTy(CI.getContext()), 0};
2647 Info.NoMerge = CI.cannotMerge();
2648 return CLI->lowerCall(MIRBuilder, Info);
2649}
2650
2651bool IRTranslatorImpl::translateVectorInterleave2Intrinsic(
2652 const CallInst &CI, MachineIRBuilder &MIRBuilder) {
2653 assert(CI.getIntrinsicID() == Intrinsic::vector_interleave2 &&
2654 "This function can only be called on the interleave2 intrinsic!");
2655 // Canonicalize interleave2 to G_SHUFFLE_VECTOR (similar to SelectionDAG).
2656 Register Op0 = getOrCreateVReg(*CI.getOperand(0));
2657 Register Op1 = getOrCreateVReg(*CI.getOperand(1));
2658 Register Res = getOrCreateVReg(CI);
2659
2660 LLT OpTy = MRI->getType(Op0);
2661 MIRBuilder.buildShuffleVector(Res, Op0, Op1,
2663
2664 return true;
2665}
2666
2667bool IRTranslatorImpl::translateVectorDeinterleave2Intrinsic(
2668 const CallInst &CI, MachineIRBuilder &MIRBuilder) {
2669 assert(CI.getIntrinsicID() == Intrinsic::vector_deinterleave2 &&
2670 "This function can only be called on the deinterleave2 intrinsic!");
2671 // Canonicalize deinterleave2 to shuffles that extract sub-vectors (similar to
2672 // SelectionDAG).
2673 Register Op = getOrCreateVReg(*CI.getOperand(0));
2674 auto Undef = MIRBuilder.buildUndef(MRI->getType(Op));
2675 ArrayRef<Register> Res = getOrCreateVRegs(CI);
2676
2677 LLT ResTy = MRI->getType(Res[0]);
2678 if (ResTy.isScalar()) {
2679 MIRBuilder.buildExtractVectorElementConstant(Res[0], Op, 0);
2680 MIRBuilder.buildExtractVectorElementConstant(Res[1], Op, 1);
2681
2682 return true;
2683 }
2684
2685 assert(ResTy.isVector() && "Expected vector result type");
2686 MIRBuilder.buildShuffleVector(Res[0], Op, Undef,
2687 createStrideMask(0, 2, ResTy.getNumElements()));
2688 MIRBuilder.buildShuffleVector(Res[1], Op, Undef,
2689 createStrideMask(1, 2, ResTy.getNumElements()));
2690
2691 return true;
2692}
2693
2694void IRTranslatorImpl::getStackGuard(Register DstReg,
2695 MachineIRBuilder &MIRBuilder) {
2696 Value *Global =
2697 TLI->getSDagStackGuard(*MF->getFunction().getParent(), *Libcalls);
2698 if (!Global) {
2699 LLVMContext &Ctx = MIRBuilder.getContext();
2700 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
2701 MIRBuilder.buildUndef(DstReg);
2702 return;
2703 }
2704
2705 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2706 MRI->setRegClass(DstReg,
2707 TII.getRegClass(TII.get(TargetOpcode::LOAD_STACK_GUARD), 0));
2708 auto MIB =
2709 MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD, {DstReg}, {});
2710
2711 unsigned AddrSpace = Global->getType()->getPointerAddressSpace();
2712 LLT PtrTy = LLT::pointer(AddrSpace, DL->getPointerSizeInBits(AddrSpace));
2713
2714 MachinePointerInfo MPInfo(Global);
2717 MachineMemOperand *MemRef = MF->getMachineMemOperand(
2718 MPInfo, Flags, PtrTy, DL->getPointerABIAlignment(AddrSpace));
2719 MIB.setMemRefs({MemRef});
2720}
2721
2722bool IRTranslatorImpl::translateOverflowIntrinsic(
2723 const CallInst &CI, unsigned Op, MachineIRBuilder &MIRBuilder) {
2724 ArrayRef<Register> ResRegs = getOrCreateVRegs(CI);
2725 MIRBuilder.buildInstr(
2726 Op, {ResRegs[0], ResRegs[1]},
2727 {getOrCreateVReg(*CI.getOperand(0)), getOrCreateVReg(*CI.getOperand(1))});
2728
2729 return true;
2730}
2731
2732bool IRTranslatorImpl::translateFixedPointIntrinsic(
2733 unsigned Op, const CallInst &CI, MachineIRBuilder &MIRBuilder) {
2734 Register Dst = getOrCreateVReg(CI);
2735 Register Src0 = getOrCreateVReg(*CI.getOperand(0));
2736 Register Src1 = getOrCreateVReg(*CI.getOperand(1));
2737 uint64_t Scale = cast<ConstantInt>(CI.getOperand(2))->getZExtValue();
2738 MIRBuilder.buildInstr(Op, {Dst}, { Src0, Src1, Scale });
2739 return true;
2740}
2741
2742unsigned IRTranslatorImpl::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
2743 switch (ID) {
2744 default:
2745 break;
2746 case Intrinsic::acos:
2747 return TargetOpcode::G_FACOS;
2748 case Intrinsic::asin:
2749 return TargetOpcode::G_FASIN;
2750 case Intrinsic::atan:
2751 return TargetOpcode::G_FATAN;
2752 case Intrinsic::atan2:
2753 return TargetOpcode::G_FATAN2;
2754 case Intrinsic::bswap:
2755 return TargetOpcode::G_BSWAP;
2756 case Intrinsic::bitreverse:
2757 return TargetOpcode::G_BITREVERSE;
2758 case Intrinsic::clmul:
2759 return TargetOpcode::G_CLMUL;
2760 case Intrinsic::fshl:
2761 return TargetOpcode::G_FSHL;
2762 case Intrinsic::fshr:
2763 return TargetOpcode::G_FSHR;
2764 case Intrinsic::ceil:
2765 return TargetOpcode::G_FCEIL;
2766 case Intrinsic::cos:
2767 return TargetOpcode::G_FCOS;
2768 case Intrinsic::cosh:
2769 return TargetOpcode::G_FCOSH;
2770 case Intrinsic::ctpop:
2771 return TargetOpcode::G_CTPOP;
2772 case Intrinsic::smulh:
2773 return TargetOpcode::G_SMULH;
2774 case Intrinsic::umulh:
2775 return TargetOpcode::G_UMULH;
2776 case Intrinsic::exp:
2777 return TargetOpcode::G_FEXP;
2778 case Intrinsic::exp2:
2779 return TargetOpcode::G_FEXP2;
2780 case Intrinsic::exp10:
2781 return TargetOpcode::G_FEXP10;
2782 case Intrinsic::fabs:
2783 return TargetOpcode::G_FABS;
2784 case Intrinsic::copysign:
2785 return TargetOpcode::G_FCOPYSIGN;
2786 case Intrinsic::minnum:
2787 return TargetOpcode::G_FMINNUM;
2788 case Intrinsic::maxnum:
2789 return TargetOpcode::G_FMAXNUM;
2790 case Intrinsic::minimum:
2791 return TargetOpcode::G_FMINIMUM;
2792 case Intrinsic::maximum:
2793 return TargetOpcode::G_FMAXIMUM;
2794 case Intrinsic::minimumnum:
2795 return TargetOpcode::G_FMINIMUMNUM;
2796 case Intrinsic::maximumnum:
2797 return TargetOpcode::G_FMAXIMUMNUM;
2798 case Intrinsic::canonicalize:
2799 return TargetOpcode::G_FCANONICALIZE;
2800 case Intrinsic::floor:
2801 return TargetOpcode::G_FFLOOR;
2802 case Intrinsic::fma:
2803 return TargetOpcode::G_FMA;
2804 case Intrinsic::log:
2805 return TargetOpcode::G_FLOG;
2806 case Intrinsic::log2:
2807 return TargetOpcode::G_FLOG2;
2808 case Intrinsic::log10:
2809 return TargetOpcode::G_FLOG10;
2810 case Intrinsic::ldexp:
2811 return TargetOpcode::G_FLDEXP;
2812 case Intrinsic::nearbyint:
2813 return TargetOpcode::G_FNEARBYINT;
2814 case Intrinsic::pow:
2815 return TargetOpcode::G_FPOW;
2816 case Intrinsic::powi:
2817 return TargetOpcode::G_FPOWI;
2818 case Intrinsic::rint:
2819 return TargetOpcode::G_FRINT;
2820 case Intrinsic::round:
2821 return TargetOpcode::G_INTRINSIC_ROUND;
2822 case Intrinsic::roundeven:
2823 return TargetOpcode::G_INTRINSIC_ROUNDEVEN;
2824 case Intrinsic::sin:
2825 return TargetOpcode::G_FSIN;
2826 case Intrinsic::sinh:
2827 return TargetOpcode::G_FSINH;
2828 case Intrinsic::sqrt:
2829 return TargetOpcode::G_FSQRT;
2830 case Intrinsic::tan:
2831 return TargetOpcode::G_FTAN;
2832 case Intrinsic::tanh:
2833 return TargetOpcode::G_FTANH;
2834 case Intrinsic::trunc:
2835 return TargetOpcode::G_INTRINSIC_TRUNC;
2836 case Intrinsic::readcyclecounter:
2837 return TargetOpcode::G_READCYCLECOUNTER;
2838 case Intrinsic::readsteadycounter:
2839 return TargetOpcode::G_READSTEADYCOUNTER;
2840 case Intrinsic::ptrmask:
2841 return TargetOpcode::G_PTRMASK;
2842 case Intrinsic::lrint:
2843 return TargetOpcode::G_INTRINSIC_LRINT;
2844 case Intrinsic::llrint:
2845 return TargetOpcode::G_INTRINSIC_LLRINT;
2846 // FADD/FMUL require checking the FMF, so are handled elsewhere.
2847 case Intrinsic::vector_reduce_fmin:
2848 return TargetOpcode::G_VECREDUCE_FMIN;
2849 case Intrinsic::vector_reduce_fmax:
2850 return TargetOpcode::G_VECREDUCE_FMAX;
2851 case Intrinsic::vector_reduce_fminimum:
2852 return TargetOpcode::G_VECREDUCE_FMINIMUM;
2853 case Intrinsic::vector_reduce_fmaximum:
2854 return TargetOpcode::G_VECREDUCE_FMAXIMUM;
2855 case Intrinsic::vector_reduce_fminimumnum:
2856 return TargetOpcode::G_VECREDUCE_FMINIMUMNUM;
2857 case Intrinsic::vector_reduce_fmaximumnum:
2858 return TargetOpcode::G_VECREDUCE_FMAXIMUMNUM;
2859 case Intrinsic::vector_reduce_add:
2860 return TargetOpcode::G_VECREDUCE_ADD;
2861 case Intrinsic::vector_reduce_mul:
2862 return TargetOpcode::G_VECREDUCE_MUL;
2863 case Intrinsic::vector_reduce_and:
2864 return TargetOpcode::G_VECREDUCE_AND;
2865 case Intrinsic::vector_reduce_or:
2866 return TargetOpcode::G_VECREDUCE_OR;
2867 case Intrinsic::vector_reduce_xor:
2868 return TargetOpcode::G_VECREDUCE_XOR;
2869 case Intrinsic::vector_reduce_smax:
2870 return TargetOpcode::G_VECREDUCE_SMAX;
2871 case Intrinsic::vector_reduce_smin:
2872 return TargetOpcode::G_VECREDUCE_SMIN;
2873 case Intrinsic::vector_reduce_umax:
2874 return TargetOpcode::G_VECREDUCE_UMAX;
2875 case Intrinsic::vector_reduce_umin:
2876 return TargetOpcode::G_VECREDUCE_UMIN;
2877 case Intrinsic::experimental_vector_compress:
2878 return TargetOpcode::G_VECTOR_COMPRESS;
2879 case Intrinsic::lround:
2880 return TargetOpcode::G_LROUND;
2881 case Intrinsic::llround:
2882 return TargetOpcode::G_LLROUND;
2883 case Intrinsic::get_fpenv:
2884 return TargetOpcode::G_GET_FPENV;
2885 case Intrinsic::get_fpmode:
2886 return TargetOpcode::G_GET_FPMODE;
2887 }
2889}
2890
2891bool IRTranslatorImpl::translateSimpleIntrinsic(const CallInst &CI,
2892 Intrinsic::ID ID,
2893 MachineIRBuilder &MIRBuilder) {
2894
2895 unsigned Op = getSimpleIntrinsicOpcode(ID);
2896
2897 // Is this a simple intrinsic?
2899 return false;
2900
2901 // Yes. Let's translate it.
2903 for (const auto &Arg : CI.args())
2904 VRegs.push_back(getOrCreateVReg(*Arg));
2905
2906 MIRBuilder.buildInstr(Op, {getOrCreateVReg(CI)}, VRegs,
2908 return true;
2909}
2910
2911// TODO: Include ConstainedOps.def when all strict instructions are defined.
2913 switch (ID) {
2914 case Intrinsic::experimental_constrained_fadd:
2915 return TargetOpcode::G_STRICT_FADD;
2916 case Intrinsic::experimental_constrained_fsub:
2917 return TargetOpcode::G_STRICT_FSUB;
2918 case Intrinsic::experimental_constrained_fmul:
2919 return TargetOpcode::G_STRICT_FMUL;
2920 case Intrinsic::experimental_constrained_fdiv:
2921 return TargetOpcode::G_STRICT_FDIV;
2922 case Intrinsic::experimental_constrained_frem:
2923 return TargetOpcode::G_STRICT_FREM;
2924 case Intrinsic::experimental_constrained_fma:
2925 return TargetOpcode::G_STRICT_FMA;
2926 case Intrinsic::experimental_constrained_sqrt:
2927 return TargetOpcode::G_STRICT_FSQRT;
2928 case Intrinsic::experimental_constrained_ldexp:
2929 return TargetOpcode::G_STRICT_FLDEXP;
2930 case Intrinsic::experimental_constrained_fcmp:
2931 return TargetOpcode::G_STRICT_FCMP;
2932 case Intrinsic::experimental_constrained_fcmps:
2933 return TargetOpcode::G_STRICT_FCMPS;
2934 default:
2935 return 0;
2936 }
2937}
2938
2939bool IRTranslatorImpl::translateConstrainedFPIntrinsic(
2940 const ConstrainedFPIntrinsic &FPI, MachineIRBuilder &MIRBuilder) {
2942
2943 unsigned Opcode = getConstrainedOpcode(FPI.getIntrinsicID());
2944 if (!Opcode)
2945 return false;
2946
2950
2951 if (Opcode == TargetOpcode::G_STRICT_FCMP ||
2952 Opcode == TargetOpcode::G_STRICT_FCMPS) {
2953 auto *FPCmp = cast<ConstrainedFPCmpIntrinsic>(&FPI);
2954 Register Operand0 = getOrCreateVReg(*FPCmp->getArgOperand(0));
2955 Register Operand1 = getOrCreateVReg(*FPCmp->getArgOperand(1));
2956 Register Result = getOrCreateVReg(FPI);
2957 MIRBuilder.buildInstr(Opcode, {Result}, {}, Flags)
2958 .addPredicate(FPCmp->getPredicate())
2959 .addUse(Operand0)
2960 .addUse(Operand1);
2961 return true;
2962 }
2963
2965 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
2966 VRegs.push_back(getOrCreateVReg(*FPI.getArgOperand(I)));
2967
2968 MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(FPI)}, VRegs, Flags);
2969 return true;
2970}
2971
2972std::optional<MCRegister> IRTranslatorImpl::getArgPhysReg(Argument &Arg) {
2973 auto VRegs = getOrCreateVRegs(Arg);
2974 if (VRegs.size() != 1)
2975 return std::nullopt;
2976
2977 // Arguments are lowered as a copy of a livein physical register.
2978 auto *VRegDef = MF->getRegInfo().getVRegDef(VRegs[0]);
2979 if (!VRegDef || !VRegDef->isCopy())
2980 return std::nullopt;
2981 return VRegDef->getOperand(1).getReg().asMCReg();
2982}
2983
2984bool IRTranslatorImpl::translateIfEntryValueArgument(
2985 bool isDeclare, Value *Val, const DILocalVariable *Var,
2986 const DIExpression *Expr, const DebugLoc &DL,
2987 MachineIRBuilder &MIRBuilder) {
2988 auto *Arg = dyn_cast<Argument>(Val);
2989 if (!Arg)
2990 return false;
2991
2992 if (!Expr->isEntryValue())
2993 return false;
2994
2995 std::optional<MCRegister> PhysReg = getArgPhysReg(*Arg);
2996 if (!PhysReg) {
2997 LLVM_DEBUG(dbgs() << "Dropping dbg." << (isDeclare ? "declare" : "value")
2998 << ": expression is entry_value but "
2999 << "couldn't find a physical register\n");
3000 LLVM_DEBUG(dbgs() << *Var << "\n");
3001 return true;
3002 }
3003
3004 if (isDeclare) {
3005 // Append an op deref to account for the fact that this is a dbg_declare.
3006 Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
3007 MF->setVariableDbgInfo(Var, Expr, *PhysReg, DL);
3008 } else {
3009 MIRBuilder.buildDirectDbgValue(*PhysReg, Var, Expr);
3010 }
3011
3012 return true;
3013}
3014
3015static unsigned getConvOpcode(Intrinsic::ID ID) {
3016 switch (ID) {
3017 default:
3018 llvm_unreachable("Unexpected intrinsic");
3019 case Intrinsic::experimental_convergence_anchor:
3020 return TargetOpcode::CONVERGENCECTRL_ANCHOR;
3021 case Intrinsic::experimental_convergence_entry:
3022 return TargetOpcode::CONVERGENCECTRL_ENTRY;
3023 case Intrinsic::experimental_convergence_loop:
3024 return TargetOpcode::CONVERGENCECTRL_LOOP;
3025 }
3026}
3027
3028bool IRTranslatorImpl::translateConvergenceControlIntrinsic(
3029 const CallInst &CI, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder) {
3030 MachineInstrBuilder MIB = MIRBuilder.buildInstr(getConvOpcode(ID));
3031 Register OutputReg = getOrCreateConvergenceTokenVReg(CI);
3032 MIB.addDef(OutputReg);
3033
3034 if (ID == Intrinsic::experimental_convergence_loop) {
3036 assert(Bundle && "Expected a convergence control token.");
3037 Register InputReg =
3038 getOrCreateConvergenceTokenVReg(*Bundle->Inputs[0].get());
3039 MIB.addUse(InputReg);
3040 }
3041
3042 return true;
3043}
3044
3045bool IRTranslatorImpl::translateKnownIntrinsic(const CallInst &CI,
3046 Intrinsic::ID ID,
3047 MachineIRBuilder &MIRBuilder) {
3048 if (auto *MI = dyn_cast<AnyMemIntrinsic>(&CI)) {
3049 if (ORE->enabled()) {
3050 if (MemoryOpRemark::canHandle(MI, *LibInfo)) {
3051 MemoryOpRemark R(*ORE, "gisel-ir-translator-memsize", *DL, *LibInfo);
3052 R.visit(MI);
3053 }
3054 }
3055 }
3056
3057 // If this is a simple intrinsic (that is, we just need to add a def of
3058 // a vreg, and uses for each arg operand, then translate it.
3059 if (translateSimpleIntrinsic(CI, ID, MIRBuilder))
3060 return true;
3061
3062 switch (ID) {
3063 default:
3064 break;
3065 case Intrinsic::lifetime_start:
3066 case Intrinsic::lifetime_end: {
3067 // No stack colouring in O0, discard region information.
3068 if (MF->getTarget().getOptLevel() == CodeGenOptLevel::None ||
3069 MF->getFunction().hasOptNone())
3070 return true;
3071
3072 unsigned Op = ID == Intrinsic::lifetime_start ? TargetOpcode::LIFETIME_START
3073 : TargetOpcode::LIFETIME_END;
3074
3075 const AllocaInst *AI = dyn_cast<AllocaInst>(CI.getArgOperand(0));
3076 if (!AI || !AI->isStaticAlloca())
3077 return true;
3078
3079 MIRBuilder.buildInstr(Op).addFrameIndex(getOrCreateFrameIndex(*AI));
3080 return true;
3081 }
3082 case Intrinsic::fake_use: {
3084 for (const auto &Arg : CI.args())
3085 llvm::append_range(VRegs, getOrCreateVRegs(*Arg));
3086 MIRBuilder.buildInstr(TargetOpcode::FAKE_USE, {}, VRegs);
3087 MF->setHasFakeUses(true);
3088 return true;
3089 }
3090 case Intrinsic::vaend:
3091 // No target I know of cares about va_end. Certainly no in-tree target
3092 // does. Simplest intrinsic ever!
3093 return true;
3094 case Intrinsic::vastart: {
3095 Value *Ptr = CI.getArgOperand(0);
3096 unsigned ListSize = TLI->getVaListSizeInBits(*DL) / 8;
3097 Align Alignment = getKnownAlignment(Ptr, *DL);
3098
3099 MIRBuilder.buildInstr(TargetOpcode::G_VASTART, {}, {getOrCreateVReg(*Ptr)})
3100 .addMemOperand(MF->getMachineMemOperand(MachinePointerInfo(Ptr),
3102 ListSize, Alignment));
3103 return true;
3104 }
3105 case Intrinsic::uadd_with_overflow:
3106 return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDO, MIRBuilder);
3107 case Intrinsic::sadd_with_overflow:
3108 return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
3109 case Intrinsic::usub_with_overflow:
3110 return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBO, MIRBuilder);
3111 case Intrinsic::ssub_with_overflow:
3112 return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
3113 case Intrinsic::umul_with_overflow:
3114 return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
3115 case Intrinsic::smul_with_overflow:
3116 return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
3117 case Intrinsic::uadd_sat:
3118 return translateBinaryOp(TargetOpcode::G_UADDSAT, CI, MIRBuilder);
3119 case Intrinsic::sadd_sat:
3120 return translateBinaryOp(TargetOpcode::G_SADDSAT, CI, MIRBuilder);
3121 case Intrinsic::usub_sat:
3122 return translateBinaryOp(TargetOpcode::G_USUBSAT, CI, MIRBuilder);
3123 case Intrinsic::ssub_sat:
3124 return translateBinaryOp(TargetOpcode::G_SSUBSAT, CI, MIRBuilder);
3125 case Intrinsic::ushl_sat:
3126 return translateBinaryOp(TargetOpcode::G_USHLSAT, CI, MIRBuilder);
3127 case Intrinsic::sshl_sat:
3128 return translateBinaryOp(TargetOpcode::G_SSHLSAT, CI, MIRBuilder);
3129 case Intrinsic::umin:
3130 return translateBinaryOp(TargetOpcode::G_UMIN, CI, MIRBuilder);
3131 case Intrinsic::umax:
3132 return translateBinaryOp(TargetOpcode::G_UMAX, CI, MIRBuilder);
3133 case Intrinsic::smin:
3134 return translateBinaryOp(TargetOpcode::G_SMIN, CI, MIRBuilder);
3135 case Intrinsic::smax:
3136 return translateBinaryOp(TargetOpcode::G_SMAX, CI, MIRBuilder);
3137 case Intrinsic::abs:
3138 // TODO: Preserve "int min is poison" arg in GMIR?
3139 return translateUnaryOp(TargetOpcode::G_ABS, CI, MIRBuilder);
3140 case Intrinsic::smul_fix:
3141 return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIX, CI, MIRBuilder);
3142 case Intrinsic::umul_fix:
3143 return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIX, CI, MIRBuilder);
3144 case Intrinsic::smul_fix_sat:
3145 return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIXSAT, CI, MIRBuilder);
3146 case Intrinsic::umul_fix_sat:
3147 return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIXSAT, CI, MIRBuilder);
3148 case Intrinsic::sdiv_fix:
3149 return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIX, CI, MIRBuilder);
3150 case Intrinsic::udiv_fix:
3151 return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIX, CI, MIRBuilder);
3152 case Intrinsic::sdiv_fix_sat:
3153 return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIXSAT, CI, MIRBuilder);
3154 case Intrinsic::udiv_fix_sat:
3155 return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIXSAT, CI, MIRBuilder);
3156 case Intrinsic::fmuladd: {
3157 Register Dst = getOrCreateVReg(CI);
3158 Register Op0 = getOrCreateVReg(*CI.getArgOperand(0));
3159 Register Op1 = getOrCreateVReg(*CI.getArgOperand(1));
3160 Register Op2 = getOrCreateVReg(*CI.getArgOperand(2));
3161 if (TLI->isFMAFasterThanFMulAndFAdd(*MF,
3162 TLI->getValueType(*DL, CI.getType()))) {
3163 // TODO: Revisit this to see if we should move this part of the
3164 // lowering to the combiner.
3165 MIRBuilder.buildFMA(Dst, Op0, Op1, Op2,
3167 } else {
3168 LLT Ty = getLLTForType(*CI.getType(), *DL);
3169 auto FMul = MIRBuilder.buildFMul(
3170 Ty, Op0, Op1, MachineInstr::copyFlagsFromInstruction(CI));
3171 MIRBuilder.buildFAdd(Dst, FMul, Op2,
3173 }
3174 return true;
3175 }
3176 case Intrinsic::frexp: {
3177 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
3178 MIRBuilder.buildFFrexp(VRegs[0], VRegs[1],
3179 getOrCreateVReg(*CI.getArgOperand(0)),
3181 return true;
3182 }
3183 case Intrinsic::modf: {
3184 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
3185 MIRBuilder.buildModf(VRegs[0], VRegs[1],
3186 getOrCreateVReg(*CI.getArgOperand(0)),
3188 return true;
3189 }
3190 case Intrinsic::sincos: {
3191 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
3192 MIRBuilder.buildFSincos(VRegs[0], VRegs[1],
3193 getOrCreateVReg(*CI.getArgOperand(0)),
3195 return true;
3196 }
3197 case Intrinsic::fptosi_sat:
3198 MIRBuilder.buildFPTOSI_SAT(getOrCreateVReg(CI),
3199 getOrCreateVReg(*CI.getArgOperand(0)));
3200 return true;
3201 case Intrinsic::fptoui_sat:
3202 MIRBuilder.buildFPTOUI_SAT(getOrCreateVReg(CI),
3203 getOrCreateVReg(*CI.getArgOperand(0)));
3204 return true;
3205 case Intrinsic::memcpy_inline:
3206 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMCPY_INLINE);
3207 case Intrinsic::memcpy:
3208 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMCPY);
3209 case Intrinsic::memmove:
3210 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMMOVE);
3211 case Intrinsic::memset:
3212 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMSET);
3213 case Intrinsic::memset_inline:
3214 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMSET_INLINE);
3215 case Intrinsic::eh_typeid_for: {
3216 GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
3217 Register Reg = getOrCreateVReg(CI);
3218 unsigned TypeID = MF->getTypeIDFor(GV);
3219 MIRBuilder.buildConstant(Reg, TypeID);
3220 return true;
3221 }
3222 case Intrinsic::objectsize:
3223 llvm_unreachable("llvm.objectsize.* should have been lowered already");
3224
3225 case Intrinsic::is_constant:
3226 llvm_unreachable("llvm.is.constant.* should have been lowered already");
3227
3228 case Intrinsic::stackguard:
3229 getStackGuard(getOrCreateVReg(CI), MIRBuilder);
3230 return true;
3231 case Intrinsic::stackprotector: {
3232 LLT PtrTy = getLLTForType(*CI.getArgOperand(0)->getType(), *DL);
3233 Register GuardVal;
3234 if (TLI->useLoadStackGuardNode(*CI.getModule())) {
3235 GuardVal = MRI->createGenericVirtualRegister(PtrTy);
3236 getStackGuard(GuardVal, MIRBuilder);
3237 } else
3238 GuardVal = getOrCreateVReg(*CI.getArgOperand(0)); // The guard's value.
3239
3240 AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
3241 int FI = getOrCreateFrameIndex(*Slot);
3242 MF->getFrameInfo().setStackProtectorIndex(FI);
3243
3244 MIRBuilder.buildStore(
3245 GuardVal, getOrCreateVReg(*Slot),
3246 *MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
3249 PtrTy, Align(8)));
3250 return true;
3251 }
3252 case Intrinsic::stacksave: {
3253 MIRBuilder.buildInstr(TargetOpcode::G_STACKSAVE, {getOrCreateVReg(CI)}, {});
3254 return true;
3255 }
3256 case Intrinsic::stackrestore: {
3257 MIRBuilder.buildInstr(TargetOpcode::G_STACKRESTORE, {},
3258 {getOrCreateVReg(*CI.getArgOperand(0))});
3259 return true;
3260 }
3261 case Intrinsic::cttz:
3262 case Intrinsic::ctlz: {
3263 ConstantInt *Cst = cast<ConstantInt>(CI.getArgOperand(1));
3264 bool isTrailing = ID == Intrinsic::cttz;
3265 unsigned Opcode = isTrailing ? Cst->isZero()
3266 ? TargetOpcode::G_CTTZ
3267 : TargetOpcode::G_CTTZ_ZERO_POISON
3268 : Cst->isZero() ? TargetOpcode::G_CTLZ
3269 : TargetOpcode::G_CTLZ_ZERO_POISON;
3270 MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(CI)},
3271 {getOrCreateVReg(*CI.getArgOperand(0))});
3272 return true;
3273 }
3274 case Intrinsic::invariant_start: {
3275 MIRBuilder.buildUndef(getOrCreateVReg(CI));
3276 return true;
3277 }
3278 case Intrinsic::invariant_end:
3279 return true;
3280 case Intrinsic::expect:
3281 case Intrinsic::expect_with_probability:
3282 case Intrinsic::annotation:
3283 case Intrinsic::ptr_annotation:
3284 case Intrinsic::launder_invariant_group:
3285 case Intrinsic::threadlocal_address: {
3286 // Drop the intrinsic, but forward the value.
3287 MIRBuilder.buildCopy(getOrCreateVReg(CI),
3288 getOrCreateVReg(*CI.getArgOperand(0)));
3289 return true;
3290 }
3291 case Intrinsic::assume:
3292 case Intrinsic::experimental_noalias_scope_decl:
3293 case Intrinsic::var_annotation:
3294 case Intrinsic::sideeffect:
3295 // Discard annotate attributes, assumptions, and artificial side-effects.
3296 return true;
3297 case Intrinsic::read_volatile_register:
3298 case Intrinsic::read_register: {
3299 Value *Arg = CI.getArgOperand(0);
3300 MIRBuilder
3301 .buildInstr(TargetOpcode::G_READ_REGISTER, {getOrCreateVReg(CI)}, {})
3302 .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()));
3303 return true;
3304 }
3305 case Intrinsic::write_register: {
3306 Value *Arg = CI.getArgOperand(0);
3307 MIRBuilder.buildInstr(TargetOpcode::G_WRITE_REGISTER)
3308 .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()))
3309 .addUse(getOrCreateVReg(*CI.getArgOperand(1)));
3310 return true;
3311 }
3312 case Intrinsic::localescape: {
3313 MachineBasicBlock &EntryMBB = MF->front();
3314 StringRef EscapedName = GlobalValue::dropLLVMManglingEscape(MF->getName());
3315
3316 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
3317 // is the same on all targets.
3318 for (unsigned Idx = 0, E = CI.arg_size(); Idx < E; ++Idx) {
3319 Value *Arg = CI.getArgOperand(Idx)->stripPointerCasts();
3320 if (isa<ConstantPointerNull>(Arg))
3321 continue; // Skip null pointers. They represent a hole in index space.
3322
3323 int FI = getOrCreateFrameIndex(*cast<AllocaInst>(Arg));
3324 MCSymbol *FrameAllocSym =
3325 MF->getContext().getOrCreateFrameAllocSymbol(EscapedName, Idx);
3326
3327 // This should be inserted at the start of the entry block.
3328 auto LocalEscape =
3329 MIRBuilder.buildInstrNoInsert(TargetOpcode::LOCAL_ESCAPE)
3330 .addSym(FrameAllocSym)
3331 .addFrameIndex(FI);
3332
3333 EntryMBB.insert(EntryMBB.begin(), LocalEscape);
3334 }
3335
3336 return true;
3337 }
3338 case Intrinsic::vector_reduce_fadd:
3339 case Intrinsic::vector_reduce_fmul: {
3340 // Need to check for the reassoc flag to decide whether we want a
3341 // sequential reduction opcode or not.
3342 Register Dst = getOrCreateVReg(CI);
3343 Register ScalarSrc = getOrCreateVReg(*CI.getArgOperand(0));
3344 Register VecSrc = getOrCreateVReg(*CI.getArgOperand(1));
3345 unsigned Opc = 0;
3346 if (!CI.hasAllowReassoc()) {
3347 // The sequential ordering case.
3348 Opc = ID == Intrinsic::vector_reduce_fadd
3349 ? TargetOpcode::G_VECREDUCE_SEQ_FADD
3350 : TargetOpcode::G_VECREDUCE_SEQ_FMUL;
3351 if (!MRI->getType(VecSrc).isVector())
3352 Opc = ID == Intrinsic::vector_reduce_fadd ? TargetOpcode::G_FADD
3353 : TargetOpcode::G_FMUL;
3354 MIRBuilder.buildInstr(Opc, {Dst}, {ScalarSrc, VecSrc},
3356 return true;
3357 }
3358 // We split the operation into a separate G_FADD/G_FMUL + the reduce,
3359 // since the associativity doesn't matter.
3360 unsigned ScalarOpc;
3361 if (ID == Intrinsic::vector_reduce_fadd) {
3362 Opc = TargetOpcode::G_VECREDUCE_FADD;
3363 ScalarOpc = TargetOpcode::G_FADD;
3364 } else {
3365 Opc = TargetOpcode::G_VECREDUCE_FMUL;
3366 ScalarOpc = TargetOpcode::G_FMUL;
3367 }
3368 LLT DstTy = MRI->getType(Dst);
3369 auto Rdx = MIRBuilder.buildInstr(
3370 Opc, {DstTy}, {VecSrc}, MachineInstr::copyFlagsFromInstruction(CI));
3371 MIRBuilder.buildInstr(ScalarOpc, {Dst}, {ScalarSrc, Rdx},
3373
3374 return true;
3375 }
3376 case Intrinsic::trap:
3377 return translateTrap(CI, MIRBuilder, TargetOpcode::G_TRAP);
3378 case Intrinsic::debugtrap:
3379 return translateTrap(CI, MIRBuilder, TargetOpcode::G_DEBUGTRAP);
3380 case Intrinsic::ubsantrap:
3381 return translateTrap(CI, MIRBuilder, TargetOpcode::G_UBSANTRAP);
3382 case Intrinsic::allow_runtime_check:
3383 case Intrinsic::allow_ubsan_check:
3384 MIRBuilder.buildCopy(getOrCreateVReg(CI),
3385 getOrCreateVReg(*ConstantInt::getTrue(CI.getType())));
3386 return true;
3387 case Intrinsic::amdgcn_cs_chain:
3388 case Intrinsic::amdgcn_call_whole_wave:
3389 return translateCallBase(CI, MIRBuilder);
3390 case Intrinsic::fptrunc_round: {
3392
3393 // Convert the metadata argument to a constant integer
3394 Metadata *MD = cast<MetadataAsValue>(CI.getArgOperand(1))->getMetadata();
3395 std::optional<RoundingMode> RoundMode =
3396 convertStrToRoundingMode(cast<MDString>(MD)->getString());
3397
3398 // Add the Rounding mode as an integer
3399 MIRBuilder
3400 .buildInstr(TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND,
3401 {getOrCreateVReg(CI)},
3402 {getOrCreateVReg(*CI.getArgOperand(0))}, Flags)
3403 .addImm((int)*RoundMode);
3404
3405 return true;
3406 }
3407 case Intrinsic::is_fpclass: {
3408 Value *FpValue = CI.getOperand(0);
3409 ConstantInt *TestMaskValue = cast<ConstantInt>(CI.getOperand(1));
3410
3411 MIRBuilder
3412 .buildInstr(TargetOpcode::G_IS_FPCLASS, {getOrCreateVReg(CI)},
3413 {getOrCreateVReg(*FpValue)})
3414 .addImm(TestMaskValue->getZExtValue());
3415
3416 return true;
3417 }
3418 case Intrinsic::set_fpenv: {
3419 Value *FPEnv = CI.getOperand(0);
3420 MIRBuilder.buildSetFPEnv(getOrCreateVReg(*FPEnv));
3421 return true;
3422 }
3423 case Intrinsic::reset_fpenv:
3424 MIRBuilder.buildResetFPEnv();
3425 return true;
3426 case Intrinsic::set_fpmode: {
3427 Value *FPState = CI.getOperand(0);
3428 MIRBuilder.buildSetFPMode(getOrCreateVReg(*FPState));
3429 return true;
3430 }
3431 case Intrinsic::reset_fpmode:
3432 MIRBuilder.buildResetFPMode();
3433 return true;
3434 case Intrinsic::get_rounding:
3435 MIRBuilder.buildGetRounding(getOrCreateVReg(CI));
3436 return true;
3437 case Intrinsic::set_rounding:
3438 MIRBuilder.buildSetRounding(getOrCreateVReg(*CI.getOperand(0)));
3439 return true;
3440 case Intrinsic::vscale: {
3441 MIRBuilder.buildVScale(getOrCreateVReg(CI), 1);
3442 return true;
3443 }
3444 case Intrinsic::scmp:
3445 MIRBuilder.buildSCmp(getOrCreateVReg(CI),
3446 getOrCreateVReg(*CI.getOperand(0)),
3447 getOrCreateVReg(*CI.getOperand(1)));
3448 return true;
3449 case Intrinsic::ucmp:
3450 MIRBuilder.buildUCmp(getOrCreateVReg(CI),
3451 getOrCreateVReg(*CI.getOperand(0)),
3452 getOrCreateVReg(*CI.getOperand(1)));
3453 return true;
3454 case Intrinsic::vector_extract:
3455 return translateExtractVector(CI, MIRBuilder);
3456 case Intrinsic::vector_insert:
3457 return translateInsertVector(CI, MIRBuilder);
3458 case Intrinsic::stepvector: {
3459 MIRBuilder.buildStepVector(getOrCreateVReg(CI), 1);
3460 return true;
3461 }
3462 case Intrinsic::prefetch: {
3463 Value *Addr = CI.getOperand(0);
3464 unsigned RW = cast<ConstantInt>(CI.getOperand(1))->getZExtValue();
3465 unsigned Locality = cast<ConstantInt>(CI.getOperand(2))->getZExtValue();
3466 unsigned CacheType = cast<ConstantInt>(CI.getOperand(3))->getZExtValue();
3467
3469 auto &MMO = *MF->getMachineMemOperand(MachinePointerInfo(Addr), Flags,
3470 LLT(), Align());
3471
3472 MIRBuilder.buildPrefetch(getOrCreateVReg(*Addr), RW, Locality, CacheType,
3473 MMO);
3474
3475 return true;
3476 }
3477
3478 case Intrinsic::speculative_load: {
3479 // Only the pointer operand is needed at codegen; the remaining arguments
3480 // carry IR-level semantics only.
3481 const Value *Ptr = CI.getArgOperand(0);
3482 Register Dst = getOrCreateVReg(CI);
3484 Flags |= TLI->getTargetMMOFlags(CI);
3485 if (CI.hasMetadata(LLVMContext::MD_nontemporal))
3487 if (CI.hasMetadata(LLVMContext::MD_invariant_load))
3489 auto *MMO = MF->getMachineMemOperand(
3490 MachinePointerInfo(Ptr), Flags, MRI->getType(Dst),
3491 CI.getParamAlign(0).valueOrOne(), MMOMetadata(CI.getAAMetadata()));
3492 MIRBuilder.buildLoad(Dst, getOrCreateVReg(*Ptr), *MMO);
3493 return true;
3494 }
3495
3496 case Intrinsic::vector_interleave2:
3497 case Intrinsic::vector_deinterleave2: {
3498 // Both intrinsics have at least one operand.
3499 Value *Op0 = CI.getOperand(0);
3500 LLT ResTy = getLLTForType(*Op0->getType(), MIRBuilder.getDataLayout());
3501 if (!ResTy.isFixedVector())
3502 return false;
3503
3504 if (CI.getIntrinsicID() == Intrinsic::vector_interleave2)
3505 return translateVectorInterleave2Intrinsic(CI, MIRBuilder);
3506
3507 return translateVectorDeinterleave2Intrinsic(CI, MIRBuilder);
3508 }
3509
3510#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
3511 case Intrinsic::INTRINSIC:
3512#include "llvm/IR/ConstrainedOps.def"
3513 return translateConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(CI),
3514 MIRBuilder);
3515 case Intrinsic::experimental_convergence_anchor:
3516 case Intrinsic::experimental_convergence_entry:
3517 case Intrinsic::experimental_convergence_loop:
3518 return translateConvergenceControlIntrinsic(CI, ID, MIRBuilder);
3519 case Intrinsic::reloc_none: {
3520 Metadata *MD = cast<MetadataAsValue>(CI.getArgOperand(0))->getMetadata();
3521 StringRef SymbolName = cast<MDString>(MD)->getString();
3522 MIRBuilder.buildInstr(TargetOpcode::RELOC_NONE)
3524 return true;
3525 }
3526 }
3527 return false;
3528}
3529
3530bool IRTranslatorImpl::translateInlineAsm(const CallBase &CB,
3531 MachineIRBuilder &MIRBuilder) {
3532 if (!mayTranslateUserTypes(CB))
3533 return false;
3534
3535 const InlineAsmLowering *ALI = MF->getSubtarget().getInlineAsmLowering();
3536
3537 if (!ALI) {
3538 LLVM_DEBUG(
3539 dbgs() << "Inline asm lowering is not supported for this target yet\n");
3540 return false;
3541 }
3542
3543 return ALI->lowerInlineAsm(
3544 MIRBuilder, CB, [&](const Value &Val) { return getOrCreateVRegs(Val); });
3545}
3546
3547bool IRTranslatorImpl::translateCallBase(const CallBase &CB,
3548 MachineIRBuilder &MIRBuilder) {
3549 ArrayRef<Register> Res = getOrCreateVRegs(CB);
3550
3552 Register SwiftInVReg = 0;
3553 Register SwiftErrorVReg = 0;
3554 for (const auto &Arg : CB.args()) {
3555 if (CLI->supportSwiftError() && isSwiftError(Arg)) {
3556 assert(SwiftInVReg == 0 && "Expected only one swift error argument");
3557 LLT Ty = getLLTForType(*Arg->getType(), *DL);
3558 SwiftInVReg = MRI->createGenericVirtualRegister(Ty);
3559 MIRBuilder.buildCopy(SwiftInVReg, SwiftError.getOrCreateVRegUseAt(
3560 &CB, &MIRBuilder.getMBB(), Arg));
3561 Args.emplace_back(ArrayRef(SwiftInVReg));
3562 SwiftErrorVReg =
3563 SwiftError.getOrCreateVRegDefAt(&CB, &MIRBuilder.getMBB(), Arg);
3564 continue;
3565 }
3566 Args.push_back(getOrCreateVRegs(*Arg));
3567 }
3568
3569 if (auto *CI = dyn_cast<CallInst>(&CB)) {
3570 if (ORE->enabled()) {
3571 if (MemoryOpRemark::canHandle(CI, *LibInfo)) {
3572 MemoryOpRemark R(*ORE, "gisel-ir-translator-memsize", *DL, *LibInfo);
3573 R.visit(CI);
3574 }
3575 }
3576 }
3577
3578 std::optional<CallLowering::PtrAuthInfo> PAI;
3579 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_ptrauth)) {
3580 // Functions should never be ptrauth-called directly.
3581 assert(!CB.getCalledFunction() && "invalid direct ptrauth call");
3582
3583 const Value *Key = Bundle->Inputs[0];
3584 const Value *Discriminator = Bundle->Inputs[1];
3585
3586 // Look through ptrauth constants to try to eliminate the matching bundle
3587 // and turn this into a direct call with no ptrauth.
3588 // CallLowering will use the raw pointer if it doesn't find the PAI.
3589 const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CB.getCalledOperand());
3590 if (!CalleeCPA || !isa<Function>(CalleeCPA->getPointer()) ||
3591 !CalleeCPA->isKnownCompatibleWith(Key, Discriminator, *DL)) {
3592 // If we can't make it direct, package the bundle into PAI.
3593 Register DiscReg = getOrCreateVReg(*Discriminator);
3594 PAI = CallLowering::PtrAuthInfo{cast<ConstantInt>(Key)->getZExtValue(),
3595 DiscReg};
3596 }
3597 }
3598
3599 Register ConvergenceCtrlToken = 0;
3600 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
3601 const auto &Token = *Bundle->Inputs[0].get();
3602 ConvergenceCtrlToken = getOrCreateConvergenceTokenVReg(Token);
3603 }
3604
3605 // We don't set HasCalls on MFI here yet because call lowering may decide to
3606 // optimize into tail calls. Instead, we defer that to selection where a final
3607 // scan is done to check if any instructions are calls.
3608 bool Success = CLI->lowerCall(
3609 MIRBuilder, CB, Res, Args, SwiftErrorVReg, PAI, ConvergenceCtrlToken,
3610 [&]() { return getOrCreateVReg(*CB.getCalledOperand()); });
3611
3612 // Check if we just inserted a tail call.
3613 if (Success) {
3614 assert(!HasTailCall && "Can't tail call return twice from block?");
3615 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
3616 HasTailCall = TII->isTailCall(*std::prev(MIRBuilder.getInsertPt()));
3617 }
3618
3619 return Success;
3620}
3621
3622bool IRTranslatorImpl::translateCall(const User &U,
3623 MachineIRBuilder &MIRBuilder) {
3624 if (!mayTranslateUserTypes(U))
3625 return false;
3626
3627 const CallInst &CI = cast<CallInst>(U);
3628 const Function *F = CI.getCalledFunction();
3629
3630 // FIXME: support Windows dllimport function calls and calls through
3631 // weak symbols.
3632 if (F && (F->hasDLLImportStorageClass() ||
3633 (MF->getTarget().getTargetTriple().isOSWindowsOrUEFI() &&
3634 F->hasExternalWeakLinkage())))
3635 return false;
3636
3637 // FIXME: support control flow guard targets.
3639 return false;
3640
3641 // FIXME: support statepoints and related.
3643 return false;
3644
3645 if (CI.isInlineAsm())
3646 return translateInlineAsm(CI, MIRBuilder);
3647
3648 Intrinsic::ID ID = F ? F->getIntrinsicID() : Intrinsic::not_intrinsic;
3649 if (!F || ID == Intrinsic::not_intrinsic) {
3650 if (translateCallBase(CI, MIRBuilder)) {
3651 diagnoseDontCall(CI);
3652 return true;
3653 }
3654 return false;
3655 }
3656
3657 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
3658
3659 if (!MF->getSubtarget().isIntrinsicSupported(ID))
3660 return handleUnsupportedIntrinsic(CI, ID, MIRBuilder);
3661
3662 if (translateKnownIntrinsic(CI, ID, MIRBuilder))
3663 return true;
3664
3666 TLI->getTgtMemIntrinsic(Infos, CI, *MF, ID);
3667
3668 return translateIntrinsic(CI, ID, MIRBuilder, Infos);
3669}
3670
3671bool IRTranslatorImpl::handleUnsupportedIntrinsic(
3672 const CallBase &CB, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder) {
3673 const Function &F = MF->getFunction();
3674 F.getContext().diagnose(
3675 DiagnosticInfoUnsupportedTargetIntrinsic(F, ID, CB.getDebugLoc()));
3676
3677 if (!CB.getType()->isVoidTy()) {
3678 for (Register Reg : getOrCreateVRegs(CB))
3679 MIRBuilder.buildUndef(Reg);
3680 }
3681
3682 return true;
3683}
3684
3685/// Translate a call or callbr to an intrinsic.
3686bool IRTranslatorImpl::translateIntrinsic(
3687 const CallBase &CB, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder,
3688 ArrayRef<TargetLowering::IntrinsicInfo> TgtMemIntrinsicInfos) {
3689 if (!MF->getSubtarget().isIntrinsicSupported(ID))
3690 return handleUnsupportedIntrinsic(CB, ID, MIRBuilder);
3691
3692 ArrayRef<Register> ResultRegs;
3693 if (!CB.getType()->isVoidTy())
3694 ResultRegs = getOrCreateVRegs(CB);
3695
3696 // Ignore the callsite attributes. Backend code is most likely not expecting
3697 // an intrinsic to sometimes have side effects and sometimes not.
3698 MachineInstrBuilder MIB = MIRBuilder.buildIntrinsic(ID, ResultRegs);
3699 if (isa<FPMathOperator>(CB))
3700 MIB->copyIRFlags(CB);
3701
3702 for (const auto &Arg : enumerate(CB.args())) {
3703 // If this is required to be an immediate, don't materialize it in a
3704 // register.
3705 if (CB.paramHasAttr(Arg.index(), Attribute::ImmArg)) {
3706 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg.value())) {
3707 // imm arguments are more convenient than cimm (and realistically
3708 // probably sufficient), so use them.
3709 assert(CI->getBitWidth() <= 64 &&
3710 "large intrinsic immediates not handled");
3711 MIB.addImm(CI->getSExtValue());
3712 } else {
3713 MIB.addFPImm(cast<ConstantFP>(Arg.value()));
3714 }
3715 } else if (auto *MDVal = dyn_cast<MetadataAsValue>(Arg.value())) {
3716 auto *MD = MDVal->getMetadata();
3717 auto *MDN = dyn_cast<MDNode>(MD);
3718 if (!MDN) {
3719 if (auto *ConstMD = dyn_cast<ConstantAsMetadata>(MD))
3720 MDN = MDNode::get(MF->getFunction().getContext(), ConstMD);
3721 else // This was probably an MDString.
3722 return false;
3723 }
3724 MIB.addMetadata(MDN);
3725 } else {
3726 ArrayRef<Register> VRegs = getOrCreateVRegs(*Arg.value());
3727 if (VRegs.size() > 1)
3728 return false;
3729 MIB.addUse(VRegs[0]);
3730 }
3731 }
3732
3733 // Add MachineMemOperands for each memory access described by the target.
3734 for (const auto &Info : TgtMemIntrinsicInfos) {
3735 Align Alignment = Info.align.value_or(
3736 DL->getABITypeAlign(Info.memVT.getTypeForEVT(CB.getContext())));
3737 LLT MemTy = Info.memVT.isSimple()
3738 ? getLLTForMVT(Info.memVT.getSimpleVT())
3739 : LLT::scalar(Info.memVT.getStoreSizeInBits());
3740
3741 // TODO: We currently just fallback to address space 0 if
3742 // getTgtMemIntrinsic didn't yield anything useful.
3743 MachinePointerInfo MPI;
3744 if (Info.ptrVal) {
3745 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
3746 } else if (Info.fallbackAddressSpace) {
3747 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
3748 }
3749 MIB.addMemOperand(MF->getMachineMemOperand(
3750 MPI, Info.flags, MemTy, Alignment, CB.getAAMetadata(), Info.ssid,
3751 Info.order, Info.failureOrder));
3752 }
3753
3754 if (CB.isConvergent()) {
3755 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
3756 auto *Token = Bundle->Inputs[0].get();
3757 Register TokenReg = getOrCreateVReg(*Token);
3758 MIB.addUse(TokenReg, RegState::Implicit);
3759 }
3760 }
3761
3763 MIB->setDeactivationSymbol(*MF, Bundle->Inputs[0].get());
3764
3765 return true;
3766}
3767
3768bool IRTranslatorImpl::findUnwindDestinations(
3769 const BasicBlock *EHPadBB, BranchProbability Prob,
3770 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
3771 &UnwindDests) {
3773 EHPadBB->getParent()->getFunction().getPersonalityFn());
3774 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
3775 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
3776 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
3777 bool IsWasmD = Personality == EHPersonality::Wasm_D;
3778 bool IsSEH = isAsynchronousEHPersonality(Personality);
3779
3780 if (IsWasmCXX || IsWasmD) {
3781 // Ignore this for now.
3782 return false;
3783 }
3784
3785 while (EHPadBB) {
3787 BasicBlock *NewEHPadBB = nullptr;
3788 if (isa<LandingPadInst>(Pad)) {
3789 // Stop on landingpads. They are not funclets.
3790 UnwindDests.emplace_back(&getMBB(*EHPadBB), Prob);
3791 break;
3792 }
3793 if (isa<CleanupPadInst>(Pad)) {
3794 // Stop on cleanup pads. Cleanups are always funclet entries for all known
3795 // personalities.
3796 UnwindDests.emplace_back(&getMBB(*EHPadBB), Prob);
3797 UnwindDests.back().first->setIsEHScopeEntry();
3798 UnwindDests.back().first->setIsEHFuncletEntry();
3799 break;
3800 }
3801 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
3802 // Add the catchpad handlers to the possible destinations.
3803 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
3804 UnwindDests.emplace_back(&getMBB(*CatchPadBB), Prob);
3805 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
3806 if (IsMSVCCXX || IsCoreCLR)
3807 UnwindDests.back().first->setIsEHFuncletEntry();
3808 if (!IsSEH)
3809 UnwindDests.back().first->setIsEHScopeEntry();
3810 }
3811 NewEHPadBB = CatchSwitch->getUnwindDest();
3812 } else {
3813 continue;
3814 }
3815
3816 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3817 if (BPI && NewEHPadBB)
3818 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
3819 EHPadBB = NewEHPadBB;
3820 }
3821 return true;
3822}
3823
3824bool IRTranslatorImpl::translateInvoke(const User &U,
3825 MachineIRBuilder &MIRBuilder) {
3826 const InvokeInst &I = cast<InvokeInst>(U);
3827 MCContext &Context = MF->getContext();
3828
3829 const BasicBlock *ReturnBB = I.getSuccessor(0);
3830 const BasicBlock *EHPadBB = I.getSuccessor(1);
3831
3832 const Function *Fn = I.getCalledFunction();
3833
3834 // FIXME: support invoking patchpoint and statepoint intrinsics.
3835 if (Fn && Fn->isIntrinsic())
3836 return false;
3837
3838 // FIXME: support whatever these are.
3839 if (I.hasDeoptState())
3840 return false;
3841
3842 // FIXME: support control flow guard targets.
3843 if (I.countOperandBundlesOfType(LLVMContext::OB_cfguardtarget))
3844 return false;
3845
3846 // FIXME: support Windows exception handling.
3847 if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHIIt()))
3848 return false;
3849
3850 // FIXME: support Windows dllimport function calls and calls through
3851 // weak symbols.
3852 if (Fn && (Fn->hasDLLImportStorageClass() ||
3853 (MF->getTarget().getTargetTriple().isOSWindowsOrUEFI() &&
3854 Fn->hasExternalWeakLinkage())))
3855 return false;
3856
3857 bool LowerInlineAsm = I.isInlineAsm();
3858 bool NeedEHLabel = true;
3859
3860 // Emit the actual call, bracketed by EH_LABELs so that the MF knows about
3861 // the region covered by the try.
3862 MCSymbol *BeginSymbol = nullptr;
3863 if (NeedEHLabel) {
3864 MIRBuilder.buildInstr(TargetOpcode::G_INVOKE_REGION_START);
3865 BeginSymbol = Context.createTempSymbol();
3866 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
3867 }
3868
3869 if (LowerInlineAsm) {
3870 if (!translateInlineAsm(I, MIRBuilder))
3871 return false;
3872 } else if (!translateCallBase(I, MIRBuilder))
3873 return false;
3874
3875 MCSymbol *EndSymbol = nullptr;
3876 if (NeedEHLabel) {
3877 EndSymbol = Context.createTempSymbol();
3878 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
3879 }
3880
3882 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3883 MachineBasicBlock *InvokeMBB = &MIRBuilder.getMBB();
3884 BranchProbability EHPadBBProb =
3885 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3887
3888 if (!findUnwindDestinations(EHPadBB, EHPadBBProb, UnwindDests))
3889 return false;
3890
3891 MachineBasicBlock &EHPadMBB = getMBB(*EHPadBB),
3892 &ReturnMBB = getMBB(*ReturnBB);
3893 // Update successor info.
3894 addSuccessorWithProb(InvokeMBB, &ReturnMBB);
3895 for (auto &UnwindDest : UnwindDests) {
3896 UnwindDest.first->setIsEHPad();
3897 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3898 }
3899 InvokeMBB->normalizeSuccProbs();
3900
3901 if (NeedEHLabel) {
3902 assert(BeginSymbol && "Expected a begin symbol!");
3903 assert(EndSymbol && "Expected an end symbol!");
3904 MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
3905 }
3906
3907 MIRBuilder.buildBr(ReturnMBB);
3908 return true;
3909}
3910
3911/// The intrinsics currently supported by callbr are implicit control flow
3912/// intrinsics such as amdgcn.kill.
3913bool IRTranslatorImpl::translateCallBr(const User &U,
3914 MachineIRBuilder &MIRBuilder) {
3915 if (!mayTranslateUserTypes(U))
3916 return false; // see translateCall
3917
3918 const CallBrInst &I = cast<CallBrInst>(U);
3919 MachineBasicBlock *CallBrMBB = &MIRBuilder.getMBB();
3920
3921 Intrinsic::ID IID = I.getIntrinsicID();
3922 if (I.isInlineAsm()) {
3923 // FIXME: inline asm is not yet supported for callbr in GlobalISel. As soon
3924 // as we add support, we need to handle the indirect asm targets, see
3925 // SelectionDAGBuilder::visitCallBr().
3926 return false;
3927 }
3928 if (!translateIntrinsic(I, IID, MIRBuilder))
3929 return false;
3930
3931 // Retrieve successors.
3932 SmallPtrSet<BasicBlock *, 8> Dests = {I.getDefaultDest()};
3933 MachineBasicBlock *Return = &getMBB(*I.getDefaultDest());
3934
3935 // Update successor info.
3936 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3937
3938 // Add indirect targets as successors. For intrinsic callbr, these represent
3939 // implicit control flow (e.g., the "kill" path for amdgcn.kill). We mark them
3940 // with setIsInlineAsmBrIndirectTarget so the machine verifier accepts them as
3941 // valid successors, even though they're not from inline asm.
3942 for (BasicBlock *Dest : I.getIndirectDests()) {
3943 MachineBasicBlock &Target = getMBB(*Dest);
3944 Target.setIsInlineAsmBrIndirectTarget();
3945 Target.setLabelMustBeEmitted();
3946 // Don't add duplicate machine successors.
3947 if (Dests.insert(Dest).second)
3948 addSuccessorWithProb(CallBrMBB, &Target, BranchProbability::getZero());
3949 }
3950
3951 CallBrMBB->normalizeSuccProbs();
3952
3953 // Drop into default successor.
3954 MIRBuilder.buildBr(*Return);
3955
3956 return true;
3957}
3958
3959bool IRTranslatorImpl::translateLandingPad(const User &U,
3960 MachineIRBuilder &MIRBuilder) {
3961 const LandingPadInst &LP = cast<LandingPadInst>(U);
3962
3963 MachineBasicBlock &MBB = MIRBuilder.getMBB();
3964
3965 MBB.setIsEHPad();
3966
3967 // If there aren't registers to copy the values into (e.g., during SjLj
3968 // exceptions), then don't bother.
3969 const Constant *PersonalityFn = MF->getFunction().getPersonalityFn();
3970 if (TLI->getExceptionPointerRegister(FuncInfo.ExceptionModel,
3971 PersonalityFn) == 0 &&
3972 TLI->getExceptionSelectorRegister(FuncInfo.ExceptionModel,
3973 PersonalityFn) == 0)
3974 return true;
3975
3976 // If landingpad's return type is token type, we don't create DAG nodes
3977 // for its exception pointer and selector value. The extraction of exception
3978 // pointer or selector value from token type landingpads is not currently
3979 // supported.
3980 if (LP.getType()->isTokenTy())
3981 return true;
3982
3984 return false;
3985
3986 // Add a label to mark the beginning of the landing pad. Deletion of the
3987 // landing pad can thus be detected via the MachineModuleInfo.
3988 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
3989 .addSym(MF->addLandingPad(&MBB));
3990
3991 // If the unwinder does not preserve all registers, ensure that the
3992 // function marks the clobbered registers as used.
3993 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
3994 if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
3995 MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
3996
3997 LLT Ty = getLLTForType(*LP.getType(), *DL);
3998 Register Undef = MRI->createGenericVirtualRegister(Ty);
3999 MIRBuilder.buildUndef(Undef);
4000
4002 for (Type *Ty : cast<StructType>(LP.getType())->elements())
4003 Tys.push_back(getLLTForType(*Ty, *DL));
4004 assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
4005
4006 // Mark exception register as live in.
4007 Register ExceptionReg =
4008 TLI->getExceptionPointerRegister(FuncInfo.ExceptionModel, PersonalityFn);
4009 if (!ExceptionReg)
4010 return false;
4011
4012 MBB.addLiveIn(ExceptionReg);
4013 ArrayRef<Register> ResRegs = getOrCreateVRegs(LP);
4014 MIRBuilder.buildCopy(ResRegs[0], ExceptionReg);
4015
4016 Register SelectorReg =
4017 TLI->getExceptionSelectorRegister(FuncInfo.ExceptionModel, PersonalityFn);
4018 if (!SelectorReg)
4019 return false;
4020
4021 MBB.addLiveIn(SelectorReg);
4022 Register PtrVReg = MRI->createGenericVirtualRegister(Tys[0]);
4023 MIRBuilder.buildCopy(PtrVReg, SelectorReg);
4024 MIRBuilder.buildCast(ResRegs[1], PtrVReg);
4025
4026 return true;
4027}
4028
4029bool IRTranslatorImpl::translateAlloca(const User &U,
4030 MachineIRBuilder &MIRBuilder) {
4031 auto &AI = cast<AllocaInst>(U);
4032
4033 if (AI.isSwiftError())
4034 return true;
4035
4036 if (AI.isStaticAlloca()) {
4037 Register Res = getOrCreateVReg(AI);
4038 int FI = getOrCreateFrameIndex(AI);
4039 auto MIB = MIRBuilder.buildFrameIndex(Res, FI);
4040 // The frame address may be materialized before the alloca's source line is
4041 // reached. Clear its debug location to avoid reporting that line early.
4042 if (AI.getDebugLoc())
4044 return true;
4045 }
4046
4047 // FIXME: support stack probing for Windows and UEFI.
4048 if (MF->getTarget().getTargetTriple().isOSWindowsOrUEFI())
4049 return false;
4050
4051 // Now we're in the harder dynamic case.
4052 Register NumElts = getOrCreateVReg(*AI.getArraySize());
4053 Type *IntPtrIRTy = DL->getIntPtrType(AI.getType());
4054 LLT IntPtrTy = getLLTForType(*IntPtrIRTy, *DL);
4055 if (MRI->getType(NumElts) != IntPtrTy) {
4056 Register ExtElts = MRI->createGenericVirtualRegister(IntPtrTy);
4057 MIRBuilder.buildZExtOrTrunc(ExtElts, NumElts);
4058 NumElts = ExtElts;
4059 }
4060
4061 TypeSize TySize = AI.getAllocationBaseSize(*DL);
4062
4063 Register AllocSize = MRI->createGenericVirtualRegister(IntPtrTy);
4064 Register TySizeReg;
4065 if (TySize.isScalable()) {
4066 // For scalable types, use vscale * min_value
4067 TySizeReg = MRI->createGenericVirtualRegister(IntPtrTy);
4068 MIRBuilder.buildVScale(TySizeReg, TySize.getKnownMinValue());
4069 } else {
4070 // For fixed types, use a constant
4071 TySizeReg =
4072 getOrCreateVReg(*ConstantInt::get(IntPtrIRTy, TySize.getFixedValue()));
4073 }
4074 MIRBuilder.buildMul(AllocSize, NumElts, TySizeReg);
4075
4076 // Round the size of the allocation up to the stack alignment size
4077 // by add SA-1 to the size. This doesn't overflow because we're computing
4078 // an address inside an alloca.
4079 Align StackAlign = MF->getSubtarget().getFrameLowering()->getStackAlign();
4080 auto SAMinusOne = MIRBuilder.buildConstant(IntPtrTy, StackAlign.value() - 1);
4081 auto AllocAdd = MIRBuilder.buildAdd(IntPtrTy, AllocSize, SAMinusOne,
4083 auto AlignCst =
4084 MIRBuilder.buildConstant(IntPtrTy, ~(uint64_t)(StackAlign.value() - 1));
4085 auto AlignedAlloc = MIRBuilder.buildAnd(IntPtrTy, AllocAdd, AlignCst);
4086
4087 Align Alignment = AI.getAlign();
4088 if (Alignment <= StackAlign)
4089 Alignment = Align(1);
4090 MIRBuilder.buildDynStackAlloc(getOrCreateVReg(AI), AlignedAlloc, Alignment);
4091
4092 MF->getFrameInfo().CreateVariableSizedObject(Alignment, &AI);
4093 assert(MF->getFrameInfo().hasVarSizedObjects());
4094 return true;
4095}
4096
4097bool IRTranslatorImpl::translateVAArg(const User &U,
4098 MachineIRBuilder &MIRBuilder) {
4099 // FIXME: We may need more info about the type. Because of how LLT works,
4100 // we're completely discarding the i64/double distinction here (amongst
4101 // others). Fortunately the ABIs I know of where that matters don't use va_arg
4102 // anyway but that's not guaranteed.
4103 MIRBuilder.buildInstr(TargetOpcode::G_VAARG, {getOrCreateVReg(U)},
4104 {getOrCreateVReg(*U.getOperand(0)),
4105 DL->getABITypeAlign(U.getType()).value()});
4106 return true;
4107}
4108
4109bool IRTranslatorImpl::translateUnreachable(const User &U,
4110 MachineIRBuilder &MIRBuilder) {
4111 auto &UI = cast<UnreachableInst>(U);
4112 if (!UI.shouldLowerToTrap(MF->getTarget().Options.TrapUnreachable,
4113 MF->getTarget().Options.NoTrapAfterNoreturn))
4114 return true;
4115
4116 MIRBuilder.buildTrap();
4117 return true;
4118}
4119
4120bool IRTranslatorImpl::translateInsertElement(const User &U,
4121 MachineIRBuilder &MIRBuilder) {
4122 // If it is a <1 x Ty> vector, use the scalar as it is
4123 // not a legal vector type in LLT.
4124 if (auto *FVT = dyn_cast<FixedVectorType>(U.getType());
4125 FVT && FVT->getNumElements() == 1)
4126 return translateCopy(U, *U.getOperand(1), MIRBuilder);
4127
4128 Register Res = getOrCreateVReg(U);
4129 Register Val = getOrCreateVReg(*U.getOperand(0));
4130 Register Elt = getOrCreateVReg(*U.getOperand(1));
4131 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
4132 Register Idx;
4133 if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(2))) {
4134 if (CI->getBitWidth() != PreferredVecIdxWidth) {
4135 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
4136 auto *NewIdxCI = ConstantInt::get(CI->getContext(), NewIdx);
4137 Idx = getOrCreateVReg(*NewIdxCI);
4138 }
4139 }
4140 if (!Idx)
4141 Idx = getOrCreateVReg(*U.getOperand(2));
4142 if (MRI->getType(Idx).getSizeInBits() != PreferredVecIdxWidth) {
4143 const LLT VecIdxTy =
4144 MRI->getType(Idx).changeElementSize(PreferredVecIdxWidth);
4145 Idx = MIRBuilder.buildZExtOrTrunc(VecIdxTy, Idx).getReg(0);
4146 }
4147 MIRBuilder.buildInsertVectorElement(Res, Val, Elt, Idx);
4148 return true;
4149}
4150
4151bool IRTranslatorImpl::translateInsertVector(const User &U,
4152 MachineIRBuilder &MIRBuilder) {
4153 Register Dst = getOrCreateVReg(U);
4154 Register Vec = getOrCreateVReg(*U.getOperand(0));
4155 Register Elt = getOrCreateVReg(*U.getOperand(1));
4156
4157 ConstantInt *CI = cast<ConstantInt>(U.getOperand(2));
4158 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
4159
4160 // Resize Index to preferred index width.
4161 if (CI->getBitWidth() != PreferredVecIdxWidth) {
4162 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
4163 CI = ConstantInt::get(CI->getContext(), NewIdx);
4164 }
4165
4166 // If it is a <1 x Ty> vector, we have to use other means.
4167 if (auto *ResultType = dyn_cast<FixedVectorType>(U.getOperand(1)->getType());
4168 ResultType && ResultType->getNumElements() == 1) {
4169 if (auto *InputType = dyn_cast<FixedVectorType>(U.getOperand(0)->getType());
4170 InputType && InputType->getNumElements() == 1) {
4171 // We are inserting an illegal fixed vector into an illegal
4172 // fixed vector, use the scalar as it is not a legal vector type
4173 // in LLT.
4174 return translateCopy(U, Vec, MIRBuilder);
4175 }
4176 if (isa<FixedVectorType>(U.getOperand(0)->getType())) {
4177 // We are inserting an illegal fixed vector into a legal fixed
4178 // vector, use the scalar as it is not a legal vector type in
4179 // LLT.
4180 Register Idx = getOrCreateVReg(*CI);
4181 MIRBuilder.buildInsertVectorElement(Dst, Vec, Elt, Idx);
4182 return true;
4183 }
4184 if (isa<ScalableVectorType>(U.getOperand(0)->getType())) {
4185 // We are inserting an illegal fixed vector into a scalable
4186 // vector, use a scalar element insert.
4187 LLT VecIdxTy = LLT::integer(PreferredVecIdxWidth);
4188 Register Idx = getOrCreateVReg(*CI);
4189 auto ScaledIndex = MIRBuilder.buildMul(
4190 VecIdxTy, MIRBuilder.buildVScale(VecIdxTy, 1), Idx);
4191 MIRBuilder.buildInsertVectorElement(Dst, Vec, Elt, ScaledIndex);
4192 return true;
4193 }
4194 }
4195
4196 MIRBuilder.buildInsertSubvector(Dst, Vec, Elt, CI->getZExtValue());
4197 return true;
4198}
4199
4200bool IRTranslatorImpl::translateExtractElement(const User &U,
4201 MachineIRBuilder &MIRBuilder) {
4202 // If it is a <1 x Ty> vector, use the scalar as it is
4203 // not a legal vector type in LLT.
4204 if (const FixedVectorType *FVT =
4205 dyn_cast<FixedVectorType>(U.getOperand(0)->getType()))
4206 if (FVT->getNumElements() == 1)
4207 return translateCopy(U, *U.getOperand(0), MIRBuilder);
4208
4209 Register Res = getOrCreateVReg(U);
4210 Register Val = getOrCreateVReg(*U.getOperand(0));
4211 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
4212 Register Idx;
4213 if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(1))) {
4214 if (CI->getBitWidth() != PreferredVecIdxWidth) {
4215 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
4216 auto *NewIdxCI = ConstantInt::get(CI->getContext(), NewIdx);
4217 Idx = getOrCreateVReg(*NewIdxCI);
4218 }
4219 }
4220 if (!Idx)
4221 Idx = getOrCreateVReg(*U.getOperand(1));
4222 if (MRI->getType(Idx).getSizeInBits() != PreferredVecIdxWidth) {
4223 const LLT VecIdxTy =
4224 MRI->getType(Idx).changeElementSize(PreferredVecIdxWidth);
4225 Idx = MIRBuilder.buildZExtOrTrunc(VecIdxTy, Idx).getReg(0);
4226 }
4227 MIRBuilder.buildExtractVectorElement(Res, Val, Idx);
4228 return true;
4229}
4230
4231bool IRTranslatorImpl::translateExtractVector(const User &U,
4232 MachineIRBuilder &MIRBuilder) {
4233 Register Res = getOrCreateVReg(U);
4234 Register Vec = getOrCreateVReg(*U.getOperand(0));
4235 ConstantInt *CI = cast<ConstantInt>(U.getOperand(1));
4236 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
4237
4238 // Resize Index to preferred index width.
4239 if (CI->getBitWidth() != PreferredVecIdxWidth) {
4240 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
4241 CI = ConstantInt::get(CI->getContext(), NewIdx);
4242 }
4243
4244 // If it is a <1 x Ty> vector, we have to use other means.
4245 if (auto *ResultType = dyn_cast<FixedVectorType>(U.getType());
4246 ResultType && ResultType->getNumElements() == 1) {
4247 if (auto *InputType = dyn_cast<FixedVectorType>(U.getOperand(0)->getType());
4248 InputType && InputType->getNumElements() == 1) {
4249 // We are extracting an illegal fixed vector from an illegal fixed vector,
4250 // use the scalar as it is not a legal vector type in LLT.
4251 return translateCopy(U, Vec, MIRBuilder);
4252 }
4253 if (isa<FixedVectorType>(U.getOperand(0)->getType())) {
4254 // We are extracting an illegal fixed vector from a legal fixed
4255 // vector, use the scalar as it is not a legal vector type in
4256 // LLT.
4257 Register Idx = getOrCreateVReg(*CI);
4258 MIRBuilder.buildExtractVectorElement(Res, Vec, Idx);
4259 return true;
4260 }
4261 if (isa<ScalableVectorType>(U.getOperand(0)->getType())) {
4262 // We are extracting an illegal fixed vector from a scalable
4263 // vector, use a scalar element extract.
4264 LLT VecIdxTy = LLT::integer(PreferredVecIdxWidth);
4265 Register Idx = getOrCreateVReg(*CI);
4266 auto ScaledIndex = MIRBuilder.buildMul(
4267 VecIdxTy, MIRBuilder.buildVScale(VecIdxTy, 1), Idx);
4268 MIRBuilder.buildExtractVectorElement(Res, Vec, ScaledIndex);
4269 return true;
4270 }
4271 }
4272
4273 MIRBuilder.buildExtractSubvector(Res, Vec, CI->getZExtValue());
4274 return true;
4275}
4276
4277bool IRTranslatorImpl::translateShuffleVector(const User &U,
4278 MachineIRBuilder &MIRBuilder) {
4279 // A ShuffleVector that operates on scalable vectors is a splat vector where
4280 // the value of the splat vector is the 0th element of the first operand,
4281 // since the index mask operand is the zeroinitializer (undef and
4282 // poison are treated as zeroinitializer here).
4283 if (U.getOperand(0)->getType()->isScalableTy()) {
4284 Register Val = getOrCreateVReg(*U.getOperand(0));
4285 auto SplatVal = MIRBuilder.buildExtractVectorElementConstant(
4286 MRI->getType(Val).getElementType(), Val, 0);
4287 MIRBuilder.buildSplatVector(getOrCreateVReg(U), SplatVal);
4288 return true;
4289 }
4290
4291 ArrayRef<int> Mask;
4292 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&U))
4293 Mask = SVI->getShuffleMask();
4294 else
4295 Mask = cast<ConstantExpr>(U).getShuffleMask();
4296
4297 // As GISel does not represent <1 x > vectors as a separate type from scalars,
4298 // we transform shuffle_vector with a scalar output to an
4299 // ExtractVectorElement. If the input type is also scalar it becomes a Copy.
4300 unsigned DstElts = cast<FixedVectorType>(U.getType())->getNumElements();
4301 unsigned SrcElts =
4302 cast<FixedVectorType>(U.getOperand(0)->getType())->getNumElements();
4303 if (DstElts == 1) {
4304 unsigned M = Mask[0];
4305 if (SrcElts == 1) {
4306 if (M == 0 || M == 1)
4307 return translateCopy(U, *U.getOperand(M), MIRBuilder);
4308 MIRBuilder.buildUndef(getOrCreateVReg(U));
4309 } else {
4310 Register Dst = getOrCreateVReg(U);
4311 if (M < SrcElts) {
4313 Dst, getOrCreateVReg(*U.getOperand(0)), M);
4314 } else if (M < SrcElts * 2) {
4316 Dst, getOrCreateVReg(*U.getOperand(1)), M - SrcElts);
4317 } else {
4318 MIRBuilder.buildUndef(Dst);
4319 }
4320 }
4321 return true;
4322 }
4323
4324 // A single element src is transformed to a build_vector.
4325 if (SrcElts == 1) {
4328 for (int M : Mask) {
4329 LLT SrcTy = getLLTForType(*U.getOperand(0)->getType(), *DL);
4330 if (M == 0 || M == 1) {
4331 Ops.push_back(getOrCreateVReg(*U.getOperand(M)));
4332 } else {
4333 if (!Undef.isValid()) {
4334 Undef = MRI->createGenericVirtualRegister(SrcTy);
4335 MIRBuilder.buildUndef(Undef);
4336 }
4337 Ops.push_back(Undef);
4338 }
4339 }
4340 MIRBuilder.buildBuildVector(getOrCreateVReg(U), Ops);
4341 return true;
4342 }
4343
4344 ArrayRef<int> MaskAlloc = MF->allocateShuffleMask(Mask);
4345 MIRBuilder
4346 .buildInstr(TargetOpcode::G_SHUFFLE_VECTOR, {getOrCreateVReg(U)},
4347 {getOrCreateVReg(*U.getOperand(0)),
4348 getOrCreateVReg(*U.getOperand(1))})
4349 .addShuffleMask(MaskAlloc);
4350 return true;
4351}
4352
4353bool IRTranslatorImpl::translateBitInsert(const User &U,
4354 MachineIRBuilder &MIRBuilder) {
4355 Register Res = getOrCreateVReg(U);
4356 Register Base = getOrCreateVReg(*U.getOperand(0));
4357 Register Val = getOrCreateVReg(*U.getOperand(1));
4358 Register Offset = getOrCreateVReg(*U.getOperand(2));
4359 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
4360 LLT BaseTy = MRI.getType(Base);
4361 LLT ValTy = MRI.getType(Val);
4362
4363 assert(BaseTy.getSizeInBits() >= ValTy.getSizeInBits() &&
4364 "bitinsert val wider than base should be rejected by verifier");
4365
4366 // If Val is a floating-point type, bitcast it to an integer of the same
4367 // size so buildZExtOrTrunc can safely extend or truncate it.
4368 if (ValTy.isFloat()) {
4369 ValTy = LLT::scalar(ValTy.getSizeInBits());
4370 Val = MIRBuilder.buildBitcast(ValTy, Val).getReg(0);
4371 } else if (ValTy.isPointer()) {
4372 ValTy = LLT::scalar(ValTy.getSizeInBits());
4373 Val = MIRBuilder.buildPtrToInt(ValTy, Val).getReg(0);
4374 }
4375
4376 // Convert Offset to the target's preferred shift amount type.
4377 LLT ShiftAmtTy = TLI->getPreferredShiftAmountTy(BaseTy);
4378 Register LegalOffset =
4379 MIRBuilder.buildZExtOrTrunc(ShiftAmtTy, Offset).getReg(0);
4380
4381 // Truncate or extend Val to BaseTy so only the inserted bit range remains.
4382 Register ExtVal = MIRBuilder.buildZExtOrTrunc(BaseTy, Val).getReg(0);
4383
4384 unsigned BaseBitWidth = BaseTy.getSizeInBits();
4385 unsigned ValBitWidth = ValTy.getSizeInBits();
4386 APInt InsertMask = APInt::getLowBitsSet(BaseBitWidth, ValBitWidth);
4387 Register MaskConst = MIRBuilder.buildConstant(BaseTy, InsertMask).getReg(0);
4388 Register ShiftedMask =
4389 MIRBuilder.buildShl(BaseTy, MaskConst, LegalOffset).getReg(0);
4390 Register ClearMask = MIRBuilder.buildNot(BaseTy, ShiftedMask).getReg(0);
4391 Register ClearedBase = MIRBuilder.buildAnd(BaseTy, Base, ClearMask).getReg(0);
4392 Register ShiftedVal =
4393 MIRBuilder.buildShl(BaseTy, ExtVal, LegalOffset).getReg(0);
4394 MIRBuilder.buildOr(Res, ClearedBase, ShiftedVal);
4395 return true;
4396}
4397
4398bool IRTranslatorImpl::translateBitExtract(const User &U,
4399 MachineIRBuilder &MIRBuilder) {
4400 Register Res = getOrCreateVReg(U);
4401 Register Src = getOrCreateVReg(*U.getOperand(0));
4402 Register Offset = getOrCreateVReg(*U.getOperand(1));
4403 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
4404 LLT SrcTy = MRI.getType(Src);
4405 LLT ResTy = MRI.getType(Res);
4406
4407 assert(ResTy.getSizeInBits() <= SrcTy.getSizeInBits() &&
4408 "bitextract result wider than source should be rejected by verifier");
4409
4410 // Convert Offset to the target's preferred shift amount type.
4411 LLT ShiftAmtTy = TLI->getPreferredShiftAmountTy(SrcTy);
4412 Register LegalOffset =
4413 MIRBuilder.buildZExtOrTrunc(ShiftAmtTy, Offset).getReg(0);
4414
4415 // Shift right by Offset to bring the target field down to bit 0.
4416 Register Shifted = MIRBuilder.buildLShr(SrcTy, Src, LegalOffset).getReg(0);
4417
4418 if (ResTy.isFloat()) {
4419 // Drop into the integer domain to safely handle the size conversion
4420 LLT IntResTy = LLT::scalar(ResTy.getSizeInBits());
4421 Register IntRes = MRI.createGenericVirtualRegister(IntResTy);
4422
4423 if (SrcTy == IntResTy)
4424 MIRBuilder.buildCopy(IntRes, Shifted);
4425 else
4426 MIRBuilder.buildTrunc(IntRes, Shifted);
4427
4428 // Bitcast the raw integer bits back into the requested floating-point
4429 // register
4430 MIRBuilder.buildBitcast(Res, IntRes);
4431 } else if (ResTy.isPointer()) {
4432 // Drop into the integer domain to safely handle the size conversion
4433 LLT IntResTy = LLT::scalar(ResTy.getSizeInBits());
4434 Register IntRes = MRI.createGenericVirtualRegister(IntResTy);
4435
4436 if (SrcTy == IntResTy)
4437 MIRBuilder.buildCopy(IntRes, Shifted);
4438 else
4439 MIRBuilder.buildTrunc(IntRes, Shifted);
4440
4441 MIRBuilder.buildIntToPtr(Res, IntRes);
4442 } else {
4443 // Normal integer path
4444 if (SrcTy == ResTy)
4445 MIRBuilder.buildCopy(Res, Shifted);
4446 else
4447 MIRBuilder.buildTrunc(Res, Shifted);
4448 }
4449
4450 return true;
4451}
4452
4453bool IRTranslatorImpl::translatePHI(const User &U,
4454 MachineIRBuilder &MIRBuilder) {
4455 const PHINode &PI = cast<PHINode>(U);
4456
4457 SmallVector<MachineInstr *, 4> Insts;
4458 for (auto Reg : getOrCreateVRegs(PI)) {
4459 auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_PHI, {Reg}, {});
4460 Insts.push_back(MIB.getInstr());
4461 }
4462
4463 PendingPHIs.emplace_back(&PI, std::move(Insts));
4464 return true;
4465}
4466
4467bool IRTranslatorImpl::translateAtomicCmpXchg(const User &U,
4468 MachineIRBuilder &MIRBuilder) {
4469 const AtomicCmpXchgInst &I = cast<AtomicCmpXchgInst>(U);
4470
4471 auto Flags = TLI->getAtomicMemOperandFlags(I, *DL);
4472
4473 auto Res = getOrCreateVRegs(I);
4474 Register OldValRes = Res[0];
4475 Register SuccessRes = Res[1];
4476 Register Addr = getOrCreateVReg(*I.getPointerOperand());
4477 Register Cmp = getOrCreateVReg(*I.getCompareOperand());
4478 Register NewVal = getOrCreateVReg(*I.getNewValOperand());
4479
4481 OldValRes, SuccessRes, Addr, Cmp, NewVal,
4482 *MF->getMachineMemOperand(
4483 MachinePointerInfo(I.getPointerOperand()), Flags, MRI->getType(Cmp),
4484 getMemOpAlign(I), I.getAAMetadata(), I.getSyncScopeID(),
4485 I.getSuccessOrdering(), I.getFailureOrdering()));
4486 return true;
4487}
4488
4489bool IRTranslatorImpl::translateAtomicRMW(const User &U,
4490 MachineIRBuilder &MIRBuilder) {
4491 if (!mayTranslateUserTypes(U))
4492 return false;
4493
4494 const AtomicRMWInst &I = cast<AtomicRMWInst>(U);
4495 auto Flags = TLI->getAtomicMemOperandFlags(I, *DL);
4496
4497 Register Res = getOrCreateVReg(I);
4498 Register Addr = getOrCreateVReg(*I.getPointerOperand());
4499 Register Val = getOrCreateVReg(*I.getValOperand());
4500
4501 unsigned Opcode = 0;
4502 switch (I.getOperation()) {
4503 default:
4504 return false;
4506 Opcode = TargetOpcode::G_ATOMICRMW_XCHG;
4507 break;
4508 case AtomicRMWInst::Add:
4509 Opcode = TargetOpcode::G_ATOMICRMW_ADD;
4510 break;
4511 case AtomicRMWInst::Sub:
4512 Opcode = TargetOpcode::G_ATOMICRMW_SUB;
4513 break;
4514 case AtomicRMWInst::And:
4515 Opcode = TargetOpcode::G_ATOMICRMW_AND;
4516 break;
4518 Opcode = TargetOpcode::G_ATOMICRMW_NAND;
4519 break;
4520 case AtomicRMWInst::Or:
4521 Opcode = TargetOpcode::G_ATOMICRMW_OR;
4522 break;
4523 case AtomicRMWInst::Xor:
4524 Opcode = TargetOpcode::G_ATOMICRMW_XOR;
4525 break;
4526 case AtomicRMWInst::Max:
4527 Opcode = TargetOpcode::G_ATOMICRMW_MAX;
4528 break;
4529 case AtomicRMWInst::Min:
4530 Opcode = TargetOpcode::G_ATOMICRMW_MIN;
4531 break;
4533 Opcode = TargetOpcode::G_ATOMICRMW_UMAX;
4534 break;
4536 Opcode = TargetOpcode::G_ATOMICRMW_UMIN;
4537 break;
4539 Opcode = TargetOpcode::G_ATOMICRMW_FADD;
4540 break;
4542 Opcode = TargetOpcode::G_ATOMICRMW_FSUB;
4543 break;
4545 Opcode = TargetOpcode::G_ATOMICRMW_FMAX;
4546 break;
4548 Opcode = TargetOpcode::G_ATOMICRMW_FMIN;
4549 break;
4551 Opcode = TargetOpcode::G_ATOMICRMW_FMAXIMUM;
4552 break;
4554 Opcode = TargetOpcode::G_ATOMICRMW_FMINIMUM;
4555 break;
4557 Opcode = TargetOpcode::G_ATOMICRMW_FMAXIMUMNUM;
4558 break;
4560 Opcode = TargetOpcode::G_ATOMICRMW_FMINIMUMNUM;
4561 break;
4563 Opcode = TargetOpcode::G_ATOMICRMW_UINC_WRAP;
4564 break;
4566 Opcode = TargetOpcode::G_ATOMICRMW_UDEC_WRAP;
4567 break;
4569 Opcode = TargetOpcode::G_ATOMICRMW_USUB_COND;
4570 break;
4572 Opcode = TargetOpcode::G_ATOMICRMW_USUB_SAT;
4573 break;
4574 }
4575
4576 MIRBuilder.buildAtomicRMW(
4577 Opcode, Res, Addr, Val,
4578 *MF->getMachineMemOperand(MachinePointerInfo(I.getPointerOperand()),
4579 Flags, MRI->getType(Val), getMemOpAlign(I),
4580 I.getAAMetadata(), I.getSyncScopeID(),
4581 I.getOrdering()));
4582 return true;
4583}
4584
4585bool IRTranslatorImpl::translateFence(const User &U,
4586 MachineIRBuilder &MIRBuilder) {
4587 const FenceInst &Fence = cast<FenceInst>(U);
4588 MIRBuilder.buildFence(static_cast<unsigned>(Fence.getOrdering()),
4589 Fence.getSyncScopeID());
4590 return true;
4591}
4592
4593bool IRTranslatorImpl::translateFreeze(const User &U,
4594 MachineIRBuilder &MIRBuilder) {
4595 const ArrayRef<Register> DstRegs = getOrCreateVRegs(U);
4596 const ArrayRef<Register> SrcRegs = getOrCreateVRegs(*U.getOperand(0));
4597
4598 assert(DstRegs.size() == SrcRegs.size() &&
4599 "Freeze with different source and destination type?");
4600
4601 for (unsigned I = 0; I < DstRegs.size(); ++I) {
4602 MIRBuilder.buildFreeze(DstRegs[I], SrcRegs[I]);
4603 }
4604
4605 return true;
4606}
4607
4608void IRTranslatorImpl::finishPendingConstDbgValues() {
4609 for (auto [MI, C] : PendingConstDbgValues) {
4610 auto VRegsIt = VMap.findVRegs(*C);
4611 if (VRegsIt == VMap.vregs_end() || VRegsIt->second->size() != 1)
4612 continue;
4613 // Constants are materialized in the entry block, so the vreg dominates
4614 // every debug value; where it is not live, the location is dropped later.
4615 MachineIRBuilder Builder(*MI);
4616 Builder.buildDirectDbgValue(VRegsIt->second->front(),
4617 MI->getDebugVariable(),
4618 MI->getDebugExpression());
4619 MI->eraseFromParent();
4620 }
4621 PendingConstDbgValues.clear();
4622}
4623
4624void IRTranslatorImpl::finishPendingPhis() {
4625#ifndef NDEBUG
4626 DILocationVerifier Verifier;
4627 GISelObserverWrapper WrapperObserver(&Verifier);
4628 RAIIMFObsDelInstaller ObsInstall(*MF, WrapperObserver);
4629#endif // ifndef NDEBUG
4630 for (auto &Phi : PendingPHIs) {
4631 const PHINode *PI = Phi.first;
4632 if (PI->getType()->isEmptyTy())
4633 continue;
4634 ArrayRef<MachineInstr *> ComponentPHIs = Phi.second;
4635 MachineBasicBlock *PhiMBB = ComponentPHIs[0]->getParent();
4636 EntryBuilder->setDebugLoc(PI->getDebugLoc());
4637#ifndef NDEBUG
4638 Verifier.setCurrentInst(PI);
4639#endif // ifndef NDEBUG
4640
4641 SmallPtrSet<const MachineBasicBlock *, 16> SeenPreds;
4642 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
4643 auto IRPred = PI->getIncomingBlock(i);
4644 ArrayRef<Register> ValRegs = getOrCreateVRegs(*PI->getIncomingValue(i));
4645 for (auto *Pred : getMachinePredBBs({IRPred, PI->getParent()})) {
4646 if (SeenPreds.count(Pred) || !PhiMBB->isPredecessor(Pred))
4647 continue;
4648 SeenPreds.insert(Pred);
4649 for (unsigned j = 0; j < ValRegs.size(); ++j) {
4650 MachineInstrBuilder MIB(*MF, ComponentPHIs[j]);
4651 MIB.addUse(ValRegs[j]);
4652 MIB.addMBB(Pred);
4653 }
4654 }
4655 }
4656 }
4657}
4658
4659void IRTranslatorImpl::translateDbgValueRecord(Value *V, bool HasArgList,
4660 const DILocalVariable *Variable,
4661 const DIExpression *Expression,
4662 const DebugLoc &DL,
4663 MachineIRBuilder &MIRBuilder) {
4664 assert(Variable->isValidLocationForIntrinsic(DL) &&
4665 "Expected inlined-at fields to agree");
4666 // Act as if we're handling a debug intrinsic.
4667 MIRBuilder.setDebugLoc(DL);
4668
4669 if (!V || HasArgList) {
4670 // DI cannot produce a valid DBG_VALUE, so produce an undef DBG_VALUE to
4671 // terminate any prior location.
4672 MIRBuilder.buildIndirectDbgValue(0, Variable, Expression);
4673 return;
4674 }
4675
4676 if (const auto *CI = dyn_cast<Constant>(V)) {
4677 MachineInstr *MI = MIRBuilder.buildConstDbgValue(*CI, Variable, Expression);
4678 if (MI->getDebugOperand(0).isReg() && !isa<UndefValue>(CI))
4679 PendingConstDbgValues.emplace_back(MI, CI);
4680 return;
4681 }
4682
4683 if (auto *AI = dyn_cast<AllocaInst>(V);
4684 AI && AI->isStaticAlloca() && Expression->startsWithDeref()) {
4685 // If the value is an alloca and the expression starts with a
4686 // dereference, track a stack slot instead of a register, as registers
4687 // may be clobbered.
4688 auto ExprOperands = Expression->getElements();
4689 auto *ExprDerefRemoved =
4690 DIExpression::get(AI->getContext(), ExprOperands.drop_front());
4691 MIRBuilder.buildFIDbgValue(getOrCreateFrameIndex(*AI), Variable,
4692 ExprDerefRemoved);
4693 return;
4694 }
4695 if (translateIfEntryValueArgument(false, V, Variable, Expression, DL,
4696 MIRBuilder))
4697 return;
4698 for (Register Reg : getOrCreateVRegs(*V)) {
4699 // FIXME: This does not handle register-indirect values at offset 0. The
4700 // direct/indirect thing shouldn't really be handled by something as
4701 // implicit as reg+noreg vs reg+imm in the first place, but it seems
4702 // pretty baked in right now.
4703 MIRBuilder.buildDirectDbgValue(Reg, Variable, Expression);
4704 }
4705}
4706
4707void IRTranslatorImpl::translateDbgDeclareRecord(
4708 Value *Address, bool HasArgList, const DILocalVariable *Variable,
4709 const DIExpression *Expression, const DebugLoc &DL,
4710 MachineIRBuilder &MIRBuilder) {
4711 if (!Address || isa<UndefValue>(Address)) {
4712 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *Variable << "\n");
4713 return;
4714 }
4715
4716 assert(Variable->isValidLocationForIntrinsic(DL) &&
4717 "Expected inlined-at fields to agree");
4718 auto AI = dyn_cast<AllocaInst>(Address);
4719 if (AI && AI->isStaticAlloca()) {
4720 // Static allocas are tracked at the MF level, no need for DBG_VALUE
4721 // instructions (in fact, they get ignored if they *do* exist).
4722 MF->setVariableDbgInfo(Variable, Expression,
4723 getOrCreateFrameIndex(*AI), DL);
4724 return;
4725 }
4726
4727 if (translateIfEntryValueArgument(true, Address, Variable,
4728 Expression, DL,
4729 MIRBuilder))
4730 return;
4731
4732 // A dbg.declare describes the address of a source variable, so lower it
4733 // into an indirect DBG_VALUE.
4734 MIRBuilder.setDebugLoc(DL);
4735 MIRBuilder.buildIndirectDbgValue(getOrCreateVReg(*Address), Variable,
4736 Expression);
4737}
4738
4739void IRTranslatorImpl::translateDbgInfo(const Instruction &Inst,
4740 MachineIRBuilder &MIRBuilder) {
4741 for (DbgRecord &DR : Inst.getDbgRecordRange()) {
4742 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
4743 MIRBuilder.setDebugLoc(DLR->getDebugLoc());
4744 assert(DLR->getLabel() && "Missing label");
4745 assert(DLR->getLabel()->isValidLocationForIntrinsic(
4746 MIRBuilder.getDebugLoc()) &&
4747 "Expected inlined-at fields to agree");
4748 MIRBuilder.buildDbgLabel(DLR->getLabel());
4749 continue;
4750 }
4751 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
4752 const DILocalVariable *Variable = DVR.getVariable();
4753 const DIExpression *Expression = DVR.getExpression();
4754 Value *V = DVR.getVariableLocationOp(0);
4755 if (DVR.isDbgDeclare())
4756 translateDbgDeclareRecord(V, DVR.hasArgList(), Variable, Expression,
4757 DVR.getDebugLoc(), MIRBuilder);
4758 else
4759 translateDbgValueRecord(V, DVR.hasArgList(), Variable, Expression,
4760 DVR.getDebugLoc(), MIRBuilder);
4761 }
4762}
4763
4764bool IRTranslatorImpl::translate(const Instruction &Inst) {
4765 CurBuilder->setDebugLoc(Inst.getDebugLoc());
4766 CurBuilder->setPCSections(Inst.getMetadata(LLVMContext::MD_pcsections));
4767 CurBuilder->setMMRAMetadata(Inst.getMetadata(LLVMContext::MD_mmra));
4768
4769 if (TLI->fallBackToDAGISel(Inst))
4770 return false;
4771
4772 switch (Inst.getOpcode()) {
4773#define HANDLE_INST(NUM, OPCODE, CLASS) \
4774 case Instruction::OPCODE: \
4775 return translate##OPCODE(Inst, *CurBuilder.get());
4776#include "llvm/IR/Instruction.def"
4777 default:
4778 return false;
4779 }
4780}
4781
4782bool IRTranslatorImpl::translate(const Constant &C, Register Reg) {
4783 // We only emit constants into the entry block from here. To prevent jumpy
4784 // debug behaviour remove debug line.
4785 if (auto CurrInstDL = CurBuilder->getDL())
4786 EntryBuilder->setDebugLoc(DebugLoc());
4787
4788 if (auto CI = dyn_cast<ConstantInt>(&C)) {
4789 // buildConstant expects a to-be-splatted scalar ConstantInt.
4790 if (isa<VectorType>(CI->getType()))
4791 CI = ConstantInt::get(CI->getContext(), CI->getValue());
4792 EntryBuilder->buildConstant(Reg, *CI);
4793 } else if (auto CB = dyn_cast<ConstantByte>(&C)) {
4794 // Byte constants share G_CONSTANT with integers; the destination Reg's
4795 // LLT (an integer LLT, see getLLTForType) determines vector splatting.
4796 EntryBuilder->buildConstant(Reg, CB->getValue());
4797 } else if (auto CF = dyn_cast<ConstantFP>(&C)) {
4798 // buildFConstant expects a to-be-splatted scalar ConstantFP.
4799 if (isa<VectorType>(CF->getType()))
4800 CF = ConstantFP::get(CF->getContext(), CF->getValue());
4801 EntryBuilder->buildFConstant(Reg, *CF);
4802 } else if (isa<UndefValue>(C))
4803 EntryBuilder->buildUndef(Reg);
4804 else if (isa<ConstantPointerNull>(C))
4805 EntryBuilder->buildConstant(Reg, 0);
4806 else if (auto GV = dyn_cast<GlobalValue>(&C))
4807 EntryBuilder->buildGlobalValue(Reg, GV);
4808 else if (auto CPA = dyn_cast<ConstantPtrAuth>(&C)) {
4809 Register Addr = getOrCreateVReg(*CPA->getPointer());
4810 Register AddrDisc = getOrCreateVReg(*CPA->getAddrDiscriminator());
4811 EntryBuilder->buildConstantPtrAuth(Reg, CPA, Addr, AddrDisc);
4812 } else if (auto CAZ = dyn_cast<ConstantAggregateZero>(&C)) {
4813 Constant &Elt = *CAZ->getElementValue(0u);
4814 if (isa<ScalableVectorType>(CAZ->getType())) {
4815 EntryBuilder->buildSplatVector(Reg, getOrCreateVReg(Elt));
4816 return true;
4817 }
4818 // Return the scalar if it is a <1 x Ty> vector.
4819 unsigned NumElts = CAZ->getElementCount().getFixedValue();
4820 if (NumElts == 1)
4821 return translateCopy(C, Elt, *EntryBuilder);
4822 // All elements are zero so we can just use the first one.
4823 EntryBuilder->buildSplatBuildVector(Reg, getOrCreateVReg(Elt));
4824 } else if (auto CV = dyn_cast<ConstantDataVector>(&C)) {
4825 // Return the scalar if it is a <1 x Ty> vector.
4826 if (CV->getNumElements() == 1)
4827 return translateCopy(C, *CV->getElementAsConstant(0), *EntryBuilder);
4829 for (unsigned i = 0; i < CV->getNumElements(); ++i) {
4830 Constant &Elt = *CV->getElementAsConstant(i);
4831 Ops.push_back(getOrCreateVReg(Elt));
4832 }
4833 EntryBuilder->buildBuildVector(Reg, Ops);
4834 } else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
4835 switch(CE->getOpcode()) {
4836#define HANDLE_INST(NUM, OPCODE, CLASS) \
4837 case Instruction::OPCODE: \
4838 return translate##OPCODE(*CE, *EntryBuilder.get());
4839#include "llvm/IR/Instruction.def"
4840 default:
4841 return false;
4842 }
4843 } else if (auto CV = dyn_cast<ConstantVector>(&C)) {
4844 if (CV->getNumOperands() == 1)
4845 return translateCopy(C, *CV->getOperand(0), *EntryBuilder);
4847 for (unsigned i = 0; i < CV->getNumOperands(); ++i) {
4848 Ops.push_back(getOrCreateVReg(*CV->getOperand(i)));
4849 }
4850 EntryBuilder->buildBuildVector(Reg, Ops);
4851 } else if (auto *BA = dyn_cast<BlockAddress>(&C)) {
4852 EntryBuilder->buildBlockAddress(Reg, BA);
4853 } else
4854 return false;
4855
4856 return true;
4857}
4858
4859bool IRTranslatorImpl::mayTranslateUserTypes(const User &U) const {
4860 const TargetMachine &TM = TLI->getTargetMachine();
4861 if (LLT::getUseExtended())
4862 return true;
4863
4864 // BF16 cannot currently be represented by default LLT. To avoid miscompiles
4865 // we prevent any instructions using them by default in all targets that do
4866 // not explicitly enable it via LLT::setUseExtended(true).
4867 // SPIRV target is exception.
4868 return TM.getTargetTriple().isSPIRV() ||
4869 (!U.getType()->getScalarType()->isBFloatTy() &&
4870 !any_of(U.operands(), [](Value *V) {
4871 return V->getType()->getScalarType()->isBFloatTy();
4872 }));
4873}
4874
4875bool IRTranslatorImpl::finalizeBasicBlock(const BasicBlock &BB,
4877 for (auto &BTB : SL->BitTestCases) {
4878 // Emit header first, if it wasn't already emitted.
4879 if (!BTB.Emitted)
4880 emitBitTestHeader(BTB, BTB.Parent);
4881
4882 BranchProbability UnhandledProb = BTB.Prob;
4883 for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) {
4884 UnhandledProb -= BTB.Cases[j].ExtraProb;
4885 // Set the current basic block to the mbb we wish to insert the code into
4886 MachineBasicBlock *MBB = BTB.Cases[j].ThisBB;
4887 // If all cases cover a contiguous range, it is not necessary to jump to
4888 // the default block after the last bit test fails. This is because the
4889 // range check during bit test header creation has guaranteed that every
4890 // case here doesn't go outside the range. In this case, there is no need
4891 // to perform the last bit test, as it will always be true. Instead, make
4892 // the second-to-last bit-test fall through to the target of the last bit
4893 // test, and delete the last bit test.
4894
4895 MachineBasicBlock *NextMBB;
4896 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
4897 // Second-to-last bit-test with contiguous range: fall through to the
4898 // target of the final bit test.
4899 NextMBB = BTB.Cases[j + 1].TargetBB;
4900 } else if (j + 1 == ej) {
4901 // For the last bit test, fall through to Default.
4902 NextMBB = BTB.Default;
4903 } else {
4904 // Otherwise, fall through to the next bit test.
4905 NextMBB = BTB.Cases[j + 1].ThisBB;
4906 }
4907
4908 emitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j], MBB);
4909
4910 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
4911 // We need to record the replacement phi edge here that normally
4912 // happens in emitBitTestCase before we delete the case, otherwise the
4913 // phi edge will be lost.
4914 addMachineCFGPred({BTB.Parent->getBasicBlock(),
4915 BTB.Cases[ej - 1].TargetBB->getBasicBlock()},
4916 MBB);
4917 // Since we're not going to use the final bit test, remove it.
4918 BTB.Cases.pop_back();
4919 break;
4920 }
4921 }
4922 // This is "default" BB. We have two jumps to it. From "header" BB and from
4923 // last "case" BB, unless the latter was skipped.
4924 CFGEdge HeaderToDefaultEdge = {BTB.Parent->getBasicBlock(),
4925 BTB.Default->getBasicBlock()};
4926 addMachineCFGPred(HeaderToDefaultEdge, BTB.Parent);
4927 if (!BTB.ContiguousRange) {
4928 addMachineCFGPred(HeaderToDefaultEdge, BTB.Cases.back().ThisBB);
4929 }
4930 }
4931 SL->BitTestCases.clear();
4932
4933 for (auto &JTCase : SL->JTCases) {
4934 // Emit header first, if it wasn't already emitted.
4935 if (!JTCase.first.Emitted)
4936 emitJumpTableHeader(JTCase.second, JTCase.first, JTCase.first.HeaderBB);
4937
4938 emitJumpTable(JTCase.second, JTCase.second.MBB);
4939 }
4940 SL->JTCases.clear();
4941
4942 for (auto &SwCase : SL->SwitchCases)
4943 emitSwitchCase(SwCase, &CurBuilder->getMBB(), *CurBuilder);
4944 SL->SwitchCases.clear();
4945
4946 // Check if we need to generate stack-protector guard checks.
4947 if (SPInfo->shouldEmitSDCheck(BB)) {
4948 bool FunctionBasedInstrumentation =
4949 TLI->getSSPStackGuardCheck(*MF->getFunction().getParent(), *Libcalls);
4950 SPDescriptor.initialize(&BB, &MBB, FunctionBasedInstrumentation);
4951 }
4952 // Handle stack protector.
4953 if (SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) {
4954 LLVM_DEBUG(dbgs() << "Unimplemented stack protector case\n");
4955 return false;
4956 } else if (SPDescriptor.shouldEmitStackProtector()) {
4957 MachineBasicBlock *ParentMBB = SPDescriptor.getParentMBB();
4958 MachineBasicBlock *SuccessMBB = SPDescriptor.getSuccessMBB();
4959
4960 // Find the split point to split the parent mbb. At the same time copy all
4961 // physical registers used in the tail of parent mbb into virtual registers
4962 // before the split point and back into physical registers after the split
4963 // point. This prevents us needing to deal with Live-ins and many other
4964 // register allocation issues caused by us splitting the parent mbb. The
4965 // register allocator will clean up said virtual copies later on.
4967 ParentMBB, *MF->getSubtarget().getInstrInfo());
4968
4969 // Splice the terminator of ParentMBB into SuccessMBB.
4970 SuccessMBB->splice(SuccessMBB->end(), ParentMBB, SplitPoint,
4971 ParentMBB->end());
4972
4973 // Add compare/jump on neq/jump to the parent BB.
4974 if (!emitSPDescriptorParent(SPDescriptor, ParentMBB))
4975 return false;
4976
4977 // CodeGen Failure MBB if we have not codegened it yet.
4978 MachineBasicBlock *FailureMBB = SPDescriptor.getFailureMBB();
4979 if (FailureMBB->empty()) {
4980 if (!emitSPDescriptorFailure(SPDescriptor, FailureMBB))
4981 return false;
4982 }
4983
4984 // Clear the Per-BB State.
4985 SPDescriptor.resetPerBBState();
4986 }
4987 return true;
4988}
4989
4990bool IRTranslatorImpl::emitSPDescriptorParent(StackProtectorDescriptor &SPD,
4991 MachineBasicBlock *ParentBB) {
4992 CurBuilder->setInsertPt(*ParentBB, ParentBB->end());
4993 // First create the loads to the guard/stack slot for the comparison.
4994 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
4995 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
4996 LLT PtrMemTy = getLLTForMVT(TLI->getPointerMemTy(*DL));
4997
4998 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
4999 int FI = MFI.getStackProtectorIndex();
5000
5001 Register Guard;
5002 Register StackSlotPtr = CurBuilder->buildFrameIndex(PtrTy, FI).getReg(0);
5003 const Module &M = *ParentBB->getParent()->getFunction().getParent();
5004 Align Align = DL->getPrefTypeAlign(PointerType::getUnqual(M.getContext()));
5005
5006 // Generate code to load the content of the guard slot.
5007 Register GuardVal =
5008 CurBuilder
5009 ->buildLoad(PtrMemTy, StackSlotPtr,
5010 MachinePointerInfo::getFixedStack(*MF, FI), Align,
5012 .getReg(0);
5013
5014 // Retrieve guard check function, nullptr if instrumentation is inlined.
5015 if (const Function *GuardCheckFn = TLI->getSSPStackGuardCheck(M, *Libcalls)) {
5016 // This path is currently untestable on GlobalISel, since the only platform
5017 // that needs this seems to be Windows, and we fall back on that currently.
5018 // The code still lives here in case that changes.
5019 // Silence warning about unused variable until the code below that uses
5020 // 'GuardCheckFn' is enabled.
5021 (void)GuardCheckFn;
5022 return false;
5023#if 0
5024 // The target provides a guard check function to validate the guard value.
5025 // Generate a call to that function with the content of the guard slot as
5026 // argument.
5027 FunctionType *FnTy = GuardCheckFn->getFunctionType();
5028 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
5029 ISD::ArgFlagsTy Flags;
5030 if (GuardCheckFn->hasAttribute(1, Attribute::AttrKind::InReg))
5031 Flags.setInReg();
5032 CallLowering::ArgInfo GuardArgInfo(
5033 {GuardVal, FnTy->getParamType(0), {Flags}});
5034
5035 CallLowering::CallLoweringInfo Info;
5036 Info.OrigArgs.push_back(GuardArgInfo);
5037 Info.CallConv = GuardCheckFn->getCallingConv();
5038 Info.Callee = MachineOperand::CreateGA(GuardCheckFn, 0);
5039 Info.OrigRet = {Register(), FnTy->getReturnType()};
5040 if (!CLI->lowerCall(MIRBuilder, Info)) {
5041 LLVM_DEBUG(dbgs() << "Failed to lower call to stack protector check\n");
5042 return false;
5043 }
5044 return true;
5045#endif
5046 }
5047
5048 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
5049 // Otherwise, emit a volatile load to retrieve the stack guard value.
5050 if (TLI->useLoadStackGuardNode(*ParentBB->getBasicBlock()->getModule())) {
5051 Guard = MRI->createGenericVirtualRegister(PtrMemTy);
5052 getStackGuard(Guard, *CurBuilder);
5053 } else {
5054 // TODO: test using android subtarget when we support @llvm.thread.pointer.
5055 const Value *IRGuard = TLI->getSDagStackGuard(M, *Libcalls);
5056 if (IRGuard) {
5057 Register GuardPtr = getOrCreateVReg(*IRGuard);
5058
5059 Guard = CurBuilder
5060 ->buildLoad(PtrMemTy, GuardPtr,
5061 MachinePointerInfo::getFixedStack(*MF, FI), Align,
5064 .getReg(0);
5065 } else {
5066 LLVMContext &Ctx = CurBuilder->getContext();
5067 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
5068 Guard = CurBuilder->buildUndef(PtrMemTy).getReg(0);
5069 }
5070 }
5071
5072 // Perform the comparison.
5073 auto Cmp =
5074 CurBuilder->buildICmp(CmpInst::ICMP_NE, LLT::integer(1), Guard, GuardVal);
5075 // If the guard/stackslot do not equal, branch to failure MBB.
5076 CurBuilder->buildBrCond(Cmp, *SPD.getFailureMBB());
5077 // Otherwise branch to success MBB.
5078 CurBuilder->buildBr(*SPD.getSuccessMBB());
5079 return true;
5080}
5081
5082bool IRTranslatorImpl::emitSPDescriptorFailure(StackProtectorDescriptor &SPD,
5083 MachineBasicBlock *FailureBB) {
5084 const RTLIB::LibcallImpl LibcallImpl =
5085 Libcalls->getLibcallImpl(RTLIB::STACKPROTECTOR_CHECK_FAIL);
5086 if (LibcallImpl == RTLIB::Unsupported)
5087 return false;
5088
5089 CurBuilder->setInsertPt(*FailureBB, FailureBB->end());
5090
5091 CallLowering::CallLoweringInfo Info;
5092 Info.CallConv = Libcalls->getLibcallImplCallingConv(LibcallImpl);
5093
5094 StringRef LibcallName =
5096 Info.Callee = MachineOperand::CreateES(LibcallName.data());
5097 Info.OrigRet = {Register(), Type::getVoidTy(MF->getFunction().getContext()),
5098 0};
5099 if (!CLI->lowerCall(*CurBuilder, Info)) {
5100 LLVM_DEBUG(dbgs() << "Failed to lower call to stack protector fail\n");
5101 return false;
5102 }
5103
5104 // Emit a trap instruction if we are required to do so.
5105 const TargetOptions &TargetOpts = TLI->getTargetMachine().Options;
5106 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
5107 CurBuilder->buildInstr(TargetOpcode::G_TRAP);
5108
5109 return true;
5110}
5111
5112void IRTranslatorImpl::finalizeFunction() {
5113 // Release the memory used by the different maps we
5114 // needed during the translation.
5115 PendingPHIs.clear();
5116 PendingConstDbgValues.clear();
5117 VMap.reset();
5118 FrameIndices.clear();
5119 MachinePreds.clear();
5120 // MachineIRBuilder::DebugLoc can outlive the DILocation it holds. Clear it
5121 // to avoid accessing free’d memory (in runOnMachineFunction) and to avoid
5122 // destroying it twice (in ~IRTranslator() and ~LLVMContext())
5123 EntryBuilder.reset();
5124 CurBuilder.reset();
5125 FuncInfo.clear();
5126 SPDescriptor.resetPerFunctionState();
5127}
5128
5129/// Returns true if a BasicBlock \p BB within a variadic function contains a
5130/// variadic musttail call.
5131static bool checkForMustTailInVarArgFn(bool IsVarArg, const BasicBlock &BB) {
5132 if (!IsVarArg)
5133 return false;
5134
5135 // Walk the block backwards, because tail calls usually only appear at the end
5136 // of a block.
5137 return llvm::any_of(llvm::reverse(BB), [](const Instruction &I) {
5138 const auto *CI = dyn_cast<CallInst>(&I);
5139 return CI && CI->isMustTailCall();
5140 });
5141}
5142
5144 MachineFunction &CurMF, function_ref<GISelCSEInfo *()> GetCSEInfo,
5145 bool ShouldSkipOpts, function_ref<AAResults *()> GetAAResults,
5147 function_ref<AssumptionCache *()> GetAC, TargetLibraryInfo *LibraryInfo,
5148 const LibcallLoweringInfo *LibcallInfo, SSPLayoutInfo *StackProtectorInfo) {
5149 MF = &CurMF;
5150 const Function &F = MF->getFunction();
5151 ORE = std::make_unique<OptimizationRemarkEmitter>(&F);
5152 CLI = MF->getSubtarget().getCallLowering();
5153 SPInfo = StackProtectorInfo;
5154
5155 if (CLI->fallBackToDAGISel(*MF)) {
5156 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
5157 F.getSubprogram(), &F.getEntryBlock());
5158 R << "unable to lower function: "
5159 << ore::NV("Prototype", F.getFunctionType());
5160
5161 reportTranslationError(*MF, *ORE, R);
5162 return false;
5163 }
5164
5165 // Set the CSEConfig and run the analysis.
5166 GISelCSEInfo *CSEInfo = nullptr;
5167
5168 bool EnableCSE = EnableCSEInIRTranslator.getNumOccurrences()
5170 : true;
5171
5172 const TargetSubtargetInfo &Subtarget = MF->getSubtarget();
5173 TLI = Subtarget.getTargetLowering();
5174
5175 if (EnableCSE) {
5176 EntryBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
5177 CSEInfo = GetCSEInfo();
5178 EntryBuilder->setCSEInfo(CSEInfo);
5179 CurBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
5180 CurBuilder->setCSEInfo(CSEInfo);
5181 } else {
5182 EntryBuilder = std::make_unique<MachineIRBuilder>();
5183 CurBuilder = std::make_unique<MachineIRBuilder>();
5184 }
5185 CLI = Subtarget.getCallLowering();
5186 CurBuilder->setMF(*MF);
5187 EntryBuilder->setMF(*MF);
5188 MRI = &MF->getRegInfo();
5189 DL = &F.getDataLayout();
5190 const TargetMachine &TM = MF->getTarget();
5191 EnableOpts = OptLevel != CodeGenOptLevel::None && !ShouldSkipOpts;
5192 FuncInfo.MF = MF;
5193 FuncInfo.ExceptionModel = F.getParent()->getExceptionModel();
5194 if (EnableOpts) {
5195 AA = GetAAResults();
5196 FuncInfo.BPI = GetBPI();
5197 AC = GetAC();
5198 } else {
5199 AA = nullptr;
5200 FuncInfo.BPI = nullptr;
5201 AC = nullptr;
5202 }
5203 LibInfo = LibraryInfo;
5204 Libcalls = LibcallInfo;
5205
5206 FuncInfo.CanLowerReturn = CLI->checkReturnTypeForCallConv(*MF);
5207
5208 SL = std::make_unique<GISelSwitchLowering>(this, FuncInfo);
5209 SL->init(*TLI, TM, *DL);
5210
5211 assert(PendingPHIs.empty() && "stale PHIs");
5212
5213 // Targets which want to use big endian can enable it using
5214 // enableBigEndian()
5215 if (!DL->isLittleEndian() && !CLI->enableBigEndian()) {
5216 // Currently we don't properly handle big endian code.
5217 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
5218 F.getSubprogram(), &F.getEntryBlock());
5219 R << "unable to translate in big endian mode";
5220 reportTranslationError(*MF, *ORE, R);
5221 return false;
5222 }
5223
5224 // Release the per-function state when we return, whether we succeeded or not.
5225 llvm::scope_exit FinalizeOnReturn([this]() { finalizeFunction(); });
5226
5227 // Setup a separate basic-block for the arguments and constants
5228 MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
5229 MF->push_back(EntryBB);
5230 EntryBuilder->setMBB(*EntryBB);
5231
5232 DebugLoc DbgLoc = F.getEntryBlock().getFirstNonPHIIt()->getDebugLoc();
5233 SwiftError.setFunction(CurMF);
5234 SwiftError.createEntriesInEntryBlock(DbgLoc);
5235
5236 bool IsVarArg = F.isVarArg();
5237 bool HasMustTailInVarArgFn = false;
5238 // Use arguments and instructions to estimate the number of mapped values and
5239 // virtual registers.
5240 unsigned NumValues = F.arg_size();
5241
5242 // Create all blocks, in IR order, to preserve the layout.
5243 FuncInfo.MBBMap.resize(F.getMaxBlockNumber());
5244 for (const BasicBlock &BB: F) {
5245 NumValues += BB.size();
5246 auto *&MBB = FuncInfo.MBBMap[BB.getNumber()];
5247
5248 MBB = MF->CreateMachineBasicBlock(&BB);
5249 MF->push_back(MBB);
5250
5251 // Only mark the block if the BlockAddress actually has users. The
5252 // hasAddressTaken flag may be stale if the BlockAddress was optimized away
5253 // but the constant still exists in the uniquing table.
5254 if (BB.hasAddressTaken()) {
5255 if (BlockAddress *BA = BlockAddress::lookup(&BB))
5256 if (!BA->hasZeroLiveUses())
5257 MBB->setAddressTakenIRBlock(const_cast<BasicBlock *>(&BB));
5258 }
5259
5260 if (!HasMustTailInVarArgFn)
5261 HasMustTailInVarArgFn = checkForMustTailInVarArgFn(IsVarArg, BB);
5262 }
5263
5264 VMap.reserveVRegs(NumValues);
5265 MRI->reserveVirtRegs(NumValues);
5266
5267 MF->getFrameInfo().setHasMustTailInVarArgFunc(HasMustTailInVarArgFn);
5268
5269 // Make our arguments/constants entry block fallthrough to the IR entry block.
5270 EntryBB->addSuccessor(&getMBB(F.front()));
5271
5272 // Lower the actual args into this basic block.
5273 SmallVector<ArrayRef<Register>, 8> VRegArgs;
5274 for (const Argument &Arg: F.args()) {
5275 if (DL->getTypeStoreSize(Arg.getType()).isZero())
5276 continue; // Don't handle zero sized types.
5277 ArrayRef<Register> VRegs = getOrCreateVRegs(Arg);
5278 VRegArgs.push_back(VRegs);
5279
5280 if (CLI->supportSwiftError() && Arg.hasSwiftErrorAttr()) {
5281 assert(VRegs.size() == 1 && "Too many vregs for Swift error");
5282 SwiftError.setCurrentVReg(EntryBB, SwiftError.getFunctionArg(), VRegs[0]);
5283 }
5284 }
5285
5286 if (!CLI->lowerFormalArguments(*EntryBuilder, F, VRegArgs, FuncInfo)) {
5287 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
5288 F.getSubprogram(), &F.getEntryBlock());
5289 R << "unable to lower arguments: "
5290 << ore::NV("Prototype", F.getFunctionType());
5291 reportTranslationError(*MF, *ORE, R);
5292 return false;
5293 }
5294
5295 // Need to visit defs before uses when translating instructions.
5296 GISelObserverWrapper WrapperObserver;
5297 if (EnableCSE && CSEInfo)
5298 WrapperObserver.addObserver(CSEInfo);
5299 {
5301#ifndef NDEBUG
5302 DILocationVerifier Verifier;
5303 WrapperObserver.addObserver(&Verifier);
5304#endif // ifndef NDEBUG
5305 RAIIMFObsDelInstaller ObsInstall(*MF, WrapperObserver);
5306 for (const BasicBlock *BB : RPOT) {
5307 MachineBasicBlock &MBB = getMBB(*BB);
5308 // Set the insertion point of all the following translations to
5309 // the end of this basic block.
5310 CurBuilder->setMBB(MBB);
5311 HasTailCall = false;
5312 for (const Instruction &Inst : *BB) {
5313 // If we translated a tail call in the last step, then we know
5314 // everything after the call is either a return, or something that is
5315 // handled by the call itself. (E.g. a lifetime marker or assume
5316 // intrinsic.) In this case, we should stop translating the block and
5317 // move on.
5318 if (HasTailCall)
5319 break;
5320#ifndef NDEBUG
5321 Verifier.setCurrentInst(&Inst);
5322#endif // ifndef NDEBUG
5323
5324 // Translate any debug-info attached to the instruction.
5325 translateDbgInfo(Inst, *CurBuilder);
5326
5327 if (translate(Inst))
5328 continue;
5329
5330 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
5331 Inst.getDebugLoc(), BB);
5332 R << "unable to translate instruction: " << ore::NV("Opcode", &Inst);
5333
5334 if (ORE->allowExtraAnalysis("gisel-ir-translator")) {
5335 std::string InstStrStorage;
5336 raw_string_ostream InstStr(InstStrStorage);
5337 InstStr << Inst;
5338
5339 R << ": '" << InstStrStorage << "'";
5340 }
5341
5342 reportTranslationError(*MF, *ORE, R);
5343 return false;
5344 }
5345
5346 if (!finalizeBasicBlock(*BB, MBB)) {
5347 OptimizationRemarkMissed R("gisel-ir-translator", "GISelFailure",
5348 BB->getTerminator()->getDebugLoc(), BB);
5349 R << "unable to translate basic block";
5350 reportTranslationError(*MF, *ORE, R);
5351 return false;
5352 }
5353 }
5354#ifndef NDEBUG
5355 WrapperObserver.removeObserver(&Verifier);
5356#endif
5357 }
5358
5359 finishPendingPhis();
5360 finishPendingConstDbgValues();
5361
5362 SwiftError.propagateVRegs();
5363
5364 // Merge the argument lowering and constants block with its single
5365 // successor, the LLVM-IR entry block. We want the basic block to
5366 // be maximal.
5367 assert(EntryBB->succ_size() == 1 &&
5368 "Custom BB used for lowering should have only one successor");
5369 // Get the successor of the current entry block.
5370 MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
5371 assert(NewEntryBB.pred_size() == 1 &&
5372 "LLVM-IR entry block has a predecessor!?");
5373 // Move all the instruction from the current entry block to the
5374 // new entry block.
5375 NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
5376 EntryBB->end());
5377
5378 // Update the live-in information for the new entry block.
5379 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
5380 NewEntryBB.addLiveIn(LiveIn);
5381 NewEntryBB.sortUniqueLiveIns();
5382
5383 // Get rid of the now empty basic block.
5384 EntryBB->removeSuccessor(&NewEntryBB);
5385 MF->remove(EntryBB);
5386 MF->deleteMachineBasicBlock(EntryBB);
5387
5388 assert(&MF->front() == &NewEntryBB &&
5389 "New entry wasn't next in the list of basic block!");
5390
5391 // Initialize stack protector information.
5392 SPInfo->copyToMachineFrameInfo(MF->getFrameInfo());
5393
5394 return false;
5395}
5396
5398 const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
5399 Function &F = MF.getFunction();
5400
5401 bool ShouldSkipOpts = skipFunction(MF.getFunction());
5402 return Impl->runOnMachineFunction(
5403 MF,
5404 [&]() {
5408 return &Wrapper.get(TPC.getCSEConfig());
5409 },
5410 ShouldSkipOpts,
5411 [&]() { return &getAnalysis<AAResultsWrapperPass>().getAAResults(); },
5412 [&]() {
5414 },
5415 [&]() {
5416 return &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
5417 MF.getFunction());
5418 },
5420 &getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
5421 *F.getParent(), Subtarget),
5422 &getAnalysis<StackProtector>().getLayoutInfo());
5423}
5424
5426 : Impl(std::make_unique<IRTranslatorImpl>(OptLevel)) {}
5427
5430
5433 const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
5434 Function &F = MF.getFunction();
5435
5436 bool ShouldSkipOpts = MF.getFunction().hasOptNone() ||
5439 .getManager();
5440 auto &MAMProxy =
5442 const ModuleLibcallLoweringInfo *MLLI =
5443 MAMProxy.getCachedResult<LibcallLoweringModuleAnalysis>(*F.getParent());
5444 if (!MLLI)
5446 "LibcallLoweringModuleAnalysis must be available for IRTranslator");
5447 Impl->runOnMachineFunction(
5448 MF, [&]() { return MFAM.getResult<GISelCSEAnalysis>(MF).get(); },
5449 ShouldSkipOpts, [&]() { return &FAM.getResult<AAManager>(F); },
5450 [&]() { return &FAM.getResult<BranchProbabilityAnalysis>(F); },
5451 [&]() { return &FAM.getResult<AssumptionAnalysis>(F); },
5452 &FAM.getResult<TargetLibraryAnalysis>(F),
5453 &getLibcallLowering(*MLLI, Subtarget),
5454 &FAM.getResult<SSPLayoutAnalysis>(F));
5455
5457}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
uint64_t uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
This file describes how to lower LLVM calls to machine code calls.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
This contains common code to allow clients to notify changes to machine instr.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
static cl::opt< bool > EnableCSEInIRTranslator("enable-cse-in-ir-translator", cl::desc("Should enable CSE in ir-translator"), cl::init(false))
static bool checkForMustTailInVarArgFn(bool IsVarArg, const BasicBlock &BB)
Returns true if a BasicBlock BB within a variadic function contains a variadic musttail call.
static unsigned getConvOpcode(Intrinsic::ID ID)
static uint64_t getOffsetFromIndices(const User &U, const DataLayout &DL)
static unsigned getConstrainedOpcode(Intrinsic::ID ID)
IRTranslator LLVM IR MI
IRTranslator LLVM IR static false void reportTranslationError(MachineFunction &MF, OptimizationRemarkEmitter &ORE, OptimizationRemarkMissed &R)
static bool isValInBlock(const Value *V, const BasicBlock *BB)
static bool isSwiftError(const Value *V)
This file declares the IRTranslator pass.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
This file describes how to lower LLVM inline asm to machine code INLINEASM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
Implement a low-level type suitable for MachineInstr level instruction selection.
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
uint64_t High
OptimizedStructLayoutField Field
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
verify safepoint Safepoint IR Verifier
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file describes how to lower LLVM code to machine code.
Target-Independent Code Generator Pass Configuration Options pass.
Value * RHS
Value * LHS
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1078
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:302
an instruction to allocate memory on the stack
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
LLVM_ABI TypeSize getAllocationBaseSize(const DataLayout &DL) const
Get the size of the allocated type.
PointerType * getType() const
Overload to return most specific pointer type.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
const Value * getArraySize() const
Get the number of elements allocated.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasSwiftErrorAttr() const
Return true if this argument has the swifterror attribute.
Definition Function.cpp:146
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMaximumNum
*p = maximumnum(old, v) maximumnum matches the behavior of llvm.maximumnum.
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ FMinimumNum
*p = minimumnum(old, v) minimumnum matches the behavior of llvm.minimumnum.
@ Nand
*p = ~(old & v)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
unsigned getNumber() const
Definition BasicBlock.h:95
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:666
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
size_t size() const
Definition BasicBlock.h:461
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
The address of a basic block.
Definition Constants.h:1088
static LLVM_ABI BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
Legacy analysis pass which computes BlockFrequencyInfo.
Analysis pass which computes BranchProbabilityInfo.
Legacy analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
static constexpr BranchProbability getOne()
static constexpr BranchProbability getUnknown()
static constexpr BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool isInlineAsm() const
Check if this call is an inline asm statement.
bool cannotMerge() const
Determine if the call cannot be tail merged.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
bool isFPPredicate() const
Definition InstrTypes.h:845
bool isIntPredicate() const
Definition InstrTypes.h:846
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
This is an important base class in LLVM.
Definition Constant.h:44
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static LLVM_ABI DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
LLVM_ABI bool startsWithDeref() const
Return whether the first element a DW_OP_deref.
ArrayRef< uint64_t > getElements() const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
DebugLoc getDebugLoc() const
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
A debug info location.
Definition DebugLoc.h:126
static DebugLoc getDropped()
Definition DebugLoc.h:155
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:679
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:843
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:197
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:696
Constant * getPersonalityFn() const
Get the personality function associated with this function.
const Function & getFunction() const
Definition Function.h:167
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:252
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
The actual analysis pass wrapper.
Definition CSEInfo.h:244
Simple wrapper that does the following.
Definition CSEInfo.h:214
The CSE Analysis object.
Definition CSEInfo.h:72
Abstract class that contains various methods for clients to notify about changes.
Simple wrapper observer that takes several observers, and calls each one for each event.
void removeObserver(GISelChangeObserver *O)
void addObserver(GISelChangeObserver *O)
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasExternalWeakLinkage() const
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
bool isTailCall(const MachineInstr &MI) const override
IRTranslatorImpl(CodeGenOptLevel OptLevel=CodeGenOptLevel::None)
bool runOnMachineFunction(MachineFunction &MF, function_ref< GISelCSEInfo *()> GetCSEInfo, bool ShouldSkipOpts, function_ref< AAResults *()> GetAAResults, function_ref< BranchProbabilityInfo *()> GetBPI, function_ref< AssumptionCache *()> GetAC, TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallInfo, SSPLayoutInfo *StackProtectorInfo)
IRTranslatorLegacy(CodeGenOptLevel OptLevel=CodeGenOptLevel::None)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
~IRTranslatorLegacy() override
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI ~IRTranslatorPass()
LLVM_ABI IRTranslatorPass(CodeGenOptLevel OptLevel)
bool lowerInlineAsm(MachineIRBuilder &MIRBuilder, const CallBase &CB, std::function< ArrayRef< Register >(const Value &Val)> GetOrCreateVRegs) const
Lower the given inline asm call instruction GetOrCreateVRegs is a callback to materialize a register ...
This instruction inserts a struct field of array element value into an aggregate value.
iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const
Return a range over the DbgRecords attached to this instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI bool hasAllowReassoc() const LLVM_READONLY
Determine whether the allow-reassociation flag is set.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
static bool getUseExtended()
constexpr bool isScalar() const
constexpr LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isFloat() const
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
static LLT integer(unsigned SizeInBits)
LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Tracks which library functions to use for a particular subtarget or function.
Value * getPointerOperand()
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
static LocationSize precise(uint64_t Value)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void push_back(MachineInstr *MI)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
LLVM_ABI bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
Helper class to build MachineInstr.
MachineInstrBuilder buildFPTOUI_SAT(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_FPTOUI_SAT Src0.
MachineInstrBuilder buildFMul(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildFreeze(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_FREEZE Src.
MachineInstrBuilder buildBr(MachineBasicBlock &Dest)
Build and insert G_BR Dest.
MachineInstrBuilder buildModf(const DstOp &Fract, const DstOp &Int, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Fract, Int = G_FMODF Src.
LLVMContext & getContext() const
MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ADD Op0, Op1.
MachineInstrBuilder buildUndef(const DstOp &Res)
Build and insert Res = IMPLICIT_DEF.
MachineInstrBuilder buildResetFPMode()
Build and insert G_RESET_FPMODE.
MachineInstrBuilder buildNot(const DstOp &Dst, const SrcOp &Src0)
Build and insert a bitwise not, NegOne = G_CONSTANT -1 Res = G_OR Op0, NegOne.
MachineInstrBuilder buildFPTOSI_SAT(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_FPTOSI_SAT Src0.
MachineInstrBuilder buildUCmp(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1)
Build and insert a Res = G_UCMP Op0, Op1.
MachineInstrBuilder buildJumpTable(const LLT PtrTy, unsigned JTI)
Build and insert Res = G_JUMP_TABLE JTI.
MachineInstrBuilder buildGetRounding(const DstOp &Dst)
Build and insert Dst = G_GET_ROUNDING.
MachineInstrBuilder buildSCmp(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1)
Build and insert a Res = G_SCMP Op0, Op1.
MachineInstrBuilder buildFence(unsigned Ordering, unsigned Scope)
Build and insert G_FENCE Ordering, Scope.
MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildFMA(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, const SrcOp &Src2, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FMA Op0, Op1, Op2.
MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_MUL Op0, Op1.
MachineInstrBuilder buildInsertSubvector(const DstOp &Res, const SrcOp &Src0, const SrcOp &Src1, unsigned Index)
Build and insert Res = G_INSERT_SUBVECTOR Src0, Src1, Idx.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src)
Build and insert an appropriate cast between two registers of equal size.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_SEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildAtomicRMW(unsigned Opcode, const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val, MachineMemOperand &MMO)
Build and insert OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO.
MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_SUB Op0, Op1.
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef< Register > Res, bool HasSideEffects, bool isConvergent)
Build and insert a G_INTRINSIC instruction.
MachineInstrBuilder buildVScale(const DstOp &Res, unsigned MinElts)
Build and insert Res = G_VSCALE MinElts.
MachineInstrBuilder buildSplatBuildVector(const DstOp &Res, const SrcOp &Src)
Build and insert Res = G_BUILD_VECTOR with Src replicated to fill the number of elements.
MachineInstrBuilder buildSetFPMode(const SrcOp &Src)
Build and insert G_SET_FPMODE Src.
MachineInstrBuilder buildIntToPtr(const DstOp &Dst, const SrcOp &Src)
Build and insert a G_INTTOPTR instruction.
MachineInstrBuilder buildIndirectDbgValue(Register Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in me...
MachineInstrBuilder buildBuildVector(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_BUILD_VECTOR Op0, ...
MachineInstrBuilder buildConstDbgValue(const Constant &C, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instructions specifying that Variable is given by C (suitably modified b...
MachineInstrBuilder buildBrCond(const SrcOp &Tst, MachineBasicBlock &Dest)
Build and insert G_BRCOND Tst, Dest.
std::optional< MachineInstrBuilder > materializeObjectPtrOffset(Register &Res, Register Op0, const LLT ValueTy, uint64_t Value)
Materialize and insert an instruction with appropriate flags for addressing some offset of an object,...
MachineInstrBuilder buildSetRounding(const SrcOp &Src)
Build and insert G_SET_ROUNDING.
MachineInstrBuilder buildExtractVectorElement(const DstOp &Res, const SrcOp &Val, const SrcOp &Idx)
Build and insert Res = G_EXTRACT_VECTOR_ELT Val, Idx.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_PTR_ADD Op0, Op1.
MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ZEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res, const SrcOp &Val, const int Idx)
Build and insert Res = G_EXTRACT_VECTOR_ELT Val, Idx.
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineInstrBuilder buildDirectDbgValue(Register Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in Re...
MachineInstrBuilder buildDbgLabel(const MDNode *Label)
Build and insert a DBG_LABEL instructions specifying that Label is given.
MachineInstrBuilder buildBrJT(Register TablePtr, unsigned JTI, Register IndexReg)
Build and insert G_BRJT TablePtr, JTI, IndexReg.
MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size, Align Alignment)
Build and insert Res = G_DYN_STACKALLOC Size, Align.
MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in th...
MachineInstrBuilder buildResetFPEnv()
Build and insert G_RESET_FPENV.
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_TRUNC Op.
void setDebugLoc(const DebugLoc &DL)
Set the debug location to DL for all the next build instructions.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
MachineInstrBuilder buildInsertVectorElement(const DstOp &Res, const SrcOp &Val, const SrcOp &Elt, const SrcOp &Idx)
Build and insert Res = G_INSERT_VECTOR_ELT Val, Elt, Idx.
MachineInstrBuilder buildAtomicCmpXchgWithSuccess(const DstOp &OldValRes, const DstOp &SuccessRes, const SrcOp &Addr, const SrcOp &CmpVal, const SrcOp &NewVal, MachineMemOperand &MMO)
Build and insert OldValRes<def>, SuccessRes<def> = / G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr,...
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_BITCAST Src.
const DebugLoc & getDebugLoc()
Get the current instruction's debug location.
MachineInstrBuilder buildTrap(bool Debug=false)
Build and insert G_TRAP or G_DEBUGTRAP.
MachineInstrBuilder buildFFrexp(const DstOp &Fract, const DstOp &Exp, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Fract, Exp = G_FFREXP Src.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_OR Op0, Op1.
MachineInstrBuilder buildFSincos(const DstOp &Sin, const DstOp &Cos, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Sin, Cos = G_FSINCOS Src.
MachineInstrBuilder buildShuffleVector(const DstOp &Res, const SrcOp &Src1, const SrcOp &Src2, ArrayRef< int > Mask)
Build and insert Res = G_SHUFFLE_VECTOR Src1, Src2, Mask.
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
MachineInstrBuilder buildPrefetch(const SrcOp &Addr, unsigned RW, unsigned Locality, unsigned CacheType, MachineMemOperand &MMO)
Build and insert G_PREFETCH Addr, RW, Locality, CacheType.
MachineInstrBuilder buildExtractSubvector(const DstOp &Res, const SrcOp &Src, unsigned Index)
Build and insert Res = G_EXTRACT_SUBVECTOR Src, Idx0.
const DataLayout & getDataLayout() const
MachineInstrBuilder buildBrIndirect(Register Tgt)
Build and insert G_BRINDIRECT Tgt.
MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val)
Build and insert Res = G_SPLAT_VECTOR Val.
MachineInstrBuilder buildStepVector(const DstOp &Res, unsigned Step)
Build and insert Res = G_STEP_VECTOR Step.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_FCMP PredOp0, Op1.
MachineInstrBuilder buildFAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FADD Op0, Op1.
MachineInstrBuilder buildSetFPEnv(const SrcOp &Src)
Build and insert G_SET_FPENV Src.
MachineInstrBuilder buildPtrToInt(const DstOp &Dst, const SrcOp &Src)
Build and insert a G_PTRTOINT instruction.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addFPImm(const ConstantFP *Val) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
LLVM_ABI void copyIRFlags(const Instruction &I)
Copy all flags to MachineInst MIFlags.
static LLVM_ABI uint32_t copyFlagsFromInstruction(const Instruction &I)
LLVM_ABI void setDeactivationSymbol(MachineFunction &MF, Value *DS)
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateES(const char *SymName, unsigned TargetFlags=0)
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
Records a mapping from an opaque lowering context to its LibcallLoweringInfo.
The optimization diagnostic interface.
Diagnostic information for missed-optimization remarks.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Class to install both of the above.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
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:398
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
SwitchLowering(FunctionLoweringInfo &funcinfo)
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Primary interface to the complete machine description for the target machine.
const Triple & getTargetTriple() const
const Target & getTarget() const
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
Target-Independent Code Generator Pass Configuration Options.
virtual std::unique_ptr< CSEConfigBase > getCSEConfig() const
Returns the CSEConfig object to use for the current optimization level.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const CallLowering * getCallLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isSPIRV() const
Tests whether the target is SPIR-V (32/64-bit/Logical).
Definition Triple.h:979
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getZero()
Definition TypeSize.h:354
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:170
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:272
bool isByteOrByteVectorTy() const
Return true if this is a byte type or a vector of byte types.
Definition Type.h:243
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:314
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:280
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:231
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
BasicBlock * getSuccessor(unsigned i=0) const
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
constexpr bool isZero() const
Definition TypeSize.h:154
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
A raw_ostream that writes to an std::string.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
auto m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
LLVM_ABI void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
initializer< Ty > init(const Ty &Val)
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< CodeNode * > Code
Definition RDFGraph.h:388
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
@ Offset
Definition DWP.cpp:577
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Undef
Value of the register doesn't matter.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
LLVM_ABI bool isExceptionPointerAndSelectorType(Type *Ty)
Return true if landingpad result type Ty is a struct of an exception pointer (pointer or integer) and...
Definition Analysis.cpp:201
LLVM_ABI MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI MachineBasicBlock::iterator findSplitPointForStackProtector(MachineBasicBlock *BB, const TargetInstrInfo &TII)
Find the split point at which to splice the end of BB into its success stack protector check machine ...
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:149
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI const LibcallLoweringInfo & getLibcallLowering(const ModuleLibcallLoweringInfo &ModuleInfo, const TargetSubtargetInfo &Subtarget)
Resolve the LibcallLoweringInfo for Subtarget from the module-level ModuleInfo, applying the subtarge...
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
generic_gep_type_iterator<> gep_type_iterator
auto succ_size(const MachineBasicBlock *BB)
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Success
The lock was released successfully.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Global
Append to llvm.global_dtors.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1137
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2068
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ FMul
Product of floats.
@ Sub
Subtraction of integers.
IntPtrTy
Definition InstrProf.h:82
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI void computeValueLLTs(const DataLayout &DL, Type &Ty, SmallVectorImpl< LLT > &ValueLLTs, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
computeValueLLTs - Given an LLVM IR type, compute a sequence of LLTs that represent all the individua...
Definition Analysis.cpp:155
LLVM_ABI GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:183
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:206
Align getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to infer an alignment for the specified pointer.
Definition Local.h:240
LLVM_ABI LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
Pair of physical register and lane mask.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:135
static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
Register Reg
The virtual register containing the index of the jump table entry to jump to.
MachineBasicBlock * Default
The MBB of the default bb, which is a successor of the range check MBB.
unsigned JTI
The JumpTableIndex for this jump table in the function.
MachineBasicBlock * MBB
The MBB into which to emit the code for the indirect jump.