LLVM 23.0.0git
Utils.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/Utils.cpp -------------------------*- 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 This file implements the utility functions used by the GlobalISel
9/// pipeline.
10//===----------------------------------------------------------------------===//
11
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/APInt.h"
35#include "llvm/IR/Constants.h"
39#include <numeric>
40#include <optional>
41
42#define DEBUG_TYPE "globalisel-utils"
43
44using namespace llvm;
45using namespace MIPatternMatch;
46
48 const TargetInstrInfo &TII,
49 const RegisterBankInfo &RBI, Register Reg,
50 const TargetRegisterClass &RegClass) {
51 if (!RBI.constrainGenericRegister(Reg, RegClass, MRI))
52 return MRI.createVirtualRegister(&RegClass);
53
54 return Reg;
55}
56
58 const MachineFunction &MF, const TargetRegisterInfo &TRI,
60 const RegisterBankInfo &RBI, MachineInstr &InsertPt,
61 const TargetRegisterClass &RegClass, MachineOperand &RegMO) {
62 Register Reg = RegMO.getReg();
63 // Assume physical registers are properly constrained.
64 assert(Reg.isVirtual() && "PhysReg not implemented");
65
66 // Save the old register class to check whether
67 // the change notifications will be required.
68 // TODO: A better approach would be to pass
69 // the observers to constrainRegToClass().
70 auto *OldRegClass = MRI.getRegClassOrNull(Reg);
71 Register ConstrainedReg = constrainRegToClass(MRI, TII, RBI, Reg, RegClass);
72 // If we created a new virtual register because the class is not compatible
73 // then create a copy between the new and the old register.
74 if (ConstrainedReg != Reg) {
75 MachineBasicBlock::iterator InsertIt(&InsertPt);
76 MachineBasicBlock &MBB = *InsertPt.getParent();
77 // FIXME: The copy needs to have the classes constrained for its operands.
78 // Use operand's regbank to get the class for old register (Reg).
79 if (RegMO.isUse()) {
80 BuildMI(MBB, InsertIt, InsertPt.getDebugLoc(),
81 TII.get(TargetOpcode::COPY), ConstrainedReg)
82 .addReg(Reg);
83 } else {
84 assert(RegMO.isDef() && "Must be a definition");
85 BuildMI(MBB, std::next(InsertIt), InsertPt.getDebugLoc(),
86 TII.get(TargetOpcode::COPY), Reg)
87 .addReg(ConstrainedReg);
88 }
89 if (GISelChangeObserver *Observer = MF.getObserver()) {
90 Observer->changingInstr(*RegMO.getParent());
91 }
92 RegMO.setReg(ConstrainedReg);
93 if (GISelChangeObserver *Observer = MF.getObserver()) {
94 Observer->changedInstr(*RegMO.getParent());
95 }
96 } else if (OldRegClass != MRI.getRegClassOrNull(Reg)) {
97 if (GISelChangeObserver *Observer = MF.getObserver()) {
98 if (!RegMO.isDef()) {
99 MachineInstr *RegDef = MRI.getVRegDef(Reg);
100 Observer->changedInstr(*RegDef);
101 }
102 Observer->changingAllUsesOfReg(MRI, Reg);
103 Observer->finishedChangingAllUsesOfReg();
104 }
105 }
106 return ConstrainedReg;
107}
108
110 const MachineFunction &MF, const TargetRegisterInfo &TRI,
112 const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
113 MachineOperand &RegMO, unsigned OpIdx) {
114 Register Reg = RegMO.getReg();
115 // Assume physical registers are properly constrained.
116 assert(Reg.isVirtual() && "PhysReg not implemented");
117
118 const TargetRegisterClass *OpRC = TII.getRegClass(II, OpIdx);
119 // Some of the target independent instructions, like COPY, may not impose any
120 // register class constraints on some of their operands: If it's a use, we can
121 // skip constraining as the instruction defining the register would constrain
122 // it.
123
124 if (OpRC) {
125 // Obtain the RC from incoming regbank if it is a proper sub-class. Operands
126 // can have multiple regbanks for a superclass that combine different
127 // register types (E.g., AMDGPU's VGPR and AGPR). The regbank ambiguity
128 // resolved by targets during regbankselect should not be overridden.
129 if (const auto *SubRC = TRI.getCommonSubClass(
130 OpRC, TRI.getConstrainedRegClassForOperand(RegMO, MRI)))
131 OpRC = SubRC;
132
133 OpRC = TRI.getAllocatableClass(OpRC);
134 }
135
136 if (!OpRC) {
137 assert((!isTargetSpecificOpcode(II.getOpcode()) || RegMO.isUse()) &&
138 "Register class constraint is required unless either the "
139 "instruction is target independent or the operand is a use");
140 // FIXME: Just bailing out like this here could be not enough, unless we
141 // expect the users of this function to do the right thing for PHIs and
142 // COPY:
143 // v1 = COPY v0
144 // v2 = COPY v1
145 // v1 here may end up not being constrained at all. Please notice that to
146 // reproduce the issue we likely need a destination pattern of a selection
147 // rule producing such extra copies, not just an input GMIR with them as
148 // every existing target using selectImpl handles copies before calling it
149 // and they never reach this function.
150 return Reg;
151 }
152 return constrainOperandRegClass(MF, TRI, MRI, TII, RBI, InsertPt, *OpRC,
153 RegMO);
154}
155
157 const TargetInstrInfo &TII,
158 const TargetRegisterInfo &TRI,
159 const RegisterBankInfo &RBI) {
160 assert(!isPreISelGenericOpcode(I.getOpcode()) &&
161 "A selected instruction is expected");
162 MachineBasicBlock &MBB = *I.getParent();
163 MachineFunction &MF = *MBB.getParent();
165
166 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
167 MachineOperand &MO = I.getOperand(OpI);
168
169 // There's nothing to be done on non-register operands.
170 if (!MO.isReg())
171 continue;
172
173 LLVM_DEBUG(dbgs() << "Converting operand: " << MO << '\n');
174
175 Register Reg = MO.getReg();
176 // Physical registers don't need to be constrained.
177 if (Reg.isPhysical())
178 continue;
179
180 // Register operands with a value of 0 (e.g. predicate operands) don't need
181 // to be constrained.
182 if (Reg == 0)
183 continue;
184
185 // If the operand is a vreg, we should constrain its regclass, and only
186 // insert COPYs if that's impossible.
187 // constrainOperandRegClass does that for us.
188 constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), MO, OpI);
189
190 // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
191 // done.
192 if (MO.isUse()) {
193 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
194 if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
195 I.tieOperands(DefIdx, OpI);
196 }
197 }
198}
199
201 MachineRegisterInfo &MRI) {
202 // Give up if either DstReg or SrcReg is a physical register.
203 if (DstReg.isPhysical() || SrcReg.isPhysical())
204 return false;
205 // Give up if the types don't match.
206 if (MRI.getType(DstReg) != MRI.getType(SrcReg))
207 return false;
208 // Replace if either DstReg has no constraints or the register
209 // constraints match.
210 const auto &DstRBC = MRI.getRegClassOrRegBank(DstReg);
211 if (!DstRBC || DstRBC == MRI.getRegClassOrRegBank(SrcReg))
212 return true;
213
214 // Otherwise match if the Src is already a regclass that is covered by the Dst
215 // RegBank.
216 return isa<const RegisterBank *>(DstRBC) && MRI.getRegClassOrNull(SrcReg) &&
217 cast<const RegisterBank *>(DstRBC)->covers(
218 *MRI.getRegClassOrNull(SrcReg));
219}
220
222 const MachineRegisterInfo &MRI) {
223 // Instructions without side-effects are dead iff they only define dead regs.
224 // This function is hot and this loop returns early in the common case,
225 // so only perform additional checks before this if absolutely necessary.
226 for (const auto &MO : MI.all_defs()) {
227 Register Reg = MO.getReg();
228 if (Reg.isPhysical() || !MRI.use_nodbg_empty(Reg))
229 return false;
230 }
231 return MI.wouldBeTriviallyDead();
232}
233
235 MachineFunction &MF,
238 bool IsGlobalISelAbortEnabled =
240 bool IsFatal = Severity == DS_Error && IsGlobalISelAbortEnabled;
241 // Print the function name explicitly if we don't have a debug location (which
242 // makes the diagnostic less useful) or if we're going to emit a raw error.
243 if (!R.getLocation().isValid() || IsFatal)
244 R << (" (in function: " + MF.getName() + ")").str();
245
246 if (IsFatal)
247 reportFatalUsageError(Twine(R.getMsg()));
248 else
249 MORE.emit(R);
250}
251
257
264
267 const char *PassName, StringRef Msg,
268 const MachineInstr &MI) {
269 MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
270 MI.getDebugLoc(), MI.getParent());
271 R << Msg;
272 // Printing MI is expensive; only do it if expensive remarks are enabled.
274 MORE.allowExtraAnalysis(PassName))
275 R << ": " << ore::MNV("Inst", MI);
276 reportGISelFailure(MF, MORE, R);
277}
278
279unsigned llvm::getInverseGMinMaxOpcode(unsigned MinMaxOpc) {
280 switch (MinMaxOpc) {
281 case TargetOpcode::G_SMIN:
282 return TargetOpcode::G_SMAX;
283 case TargetOpcode::G_SMAX:
284 return TargetOpcode::G_SMIN;
285 case TargetOpcode::G_UMIN:
286 return TargetOpcode::G_UMAX;
287 case TargetOpcode::G_UMAX:
288 return TargetOpcode::G_UMIN;
289 default:
290 llvm_unreachable("unrecognized opcode");
291 }
292}
293
294std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
295 const MachineRegisterInfo &MRI) {
296 std::optional<ValueAndVReg> ValAndVReg = getIConstantVRegValWithLookThrough(
297 VReg, MRI, /*LookThroughInstrs*/ false);
298 assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
299 "Value found while looking through instrs");
300 if (!ValAndVReg)
301 return std::nullopt;
302 return ValAndVReg->Value;
303}
304
306 const MachineRegisterInfo &MRI) {
307 MachineInstr *Const = MRI.getVRegDef(Reg);
308 assert((Const && Const->getOpcode() == TargetOpcode::G_CONSTANT) &&
309 "expected a G_CONSTANT on Reg");
310 return Const->getOperand(1).getCImm()->getValue();
311}
312
313std::optional<int64_t>
315 std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
316 if (Val && Val->getBitWidth() <= 64)
317 return Val->getSExtValue();
318 return std::nullopt;
319}
320
321namespace {
322
323// This function is used in many places, and as such, it has some
324// micro-optimizations to try and make it as fast as it can be.
325//
326// - We use template arguments to avoid an indirect call caused by passing a
327// function_ref/std::function
328// - GetAPCstValue does not return std::optional<APInt> as that's expensive.
329// Instead it returns true/false and places the result in a pre-constructed
330// APInt.
331//
332// Please change this function carefully and benchmark your changes.
333template <bool (*IsConstantOpcode)(const MachineInstr *),
334 bool (*GetAPCstValue)(const MachineInstr *MI, APInt &)>
335std::optional<ValueAndVReg>
336getConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI,
337 bool LookThroughInstrs = true,
338 bool LookThroughAnyExt = false) {
341
342 while ((MI = MRI.getVRegDef(VReg)) && !IsConstantOpcode(MI) &&
343 LookThroughInstrs) {
344 switch (MI->getOpcode()) {
345 case TargetOpcode::G_ANYEXT:
346 if (!LookThroughAnyExt)
347 return std::nullopt;
348 [[fallthrough]];
349 case TargetOpcode::G_TRUNC:
350 case TargetOpcode::G_SEXT:
351 case TargetOpcode::G_ZEXT:
352 SeenOpcodes.push_back(std::make_pair(
353 MI->getOpcode(),
354 MRI.getType(MI->getOperand(0).getReg()).getSizeInBits()));
355 VReg = MI->getOperand(1).getReg();
356 break;
357 case TargetOpcode::COPY:
358 VReg = MI->getOperand(1).getReg();
359 if (VReg.isPhysical())
360 return std::nullopt;
361 break;
362 case TargetOpcode::G_INTTOPTR:
363 VReg = MI->getOperand(1).getReg();
364 break;
365 default:
366 return std::nullopt;
367 }
368 }
369 if (!MI || !IsConstantOpcode(MI))
370 return std::nullopt;
371
372 APInt Val;
373 if (!GetAPCstValue(MI, Val))
374 return std::nullopt;
375 for (auto &Pair : reverse(SeenOpcodes)) {
376 switch (Pair.first) {
377 case TargetOpcode::G_TRUNC:
378 Val = Val.trunc(Pair.second);
379 break;
380 case TargetOpcode::G_ANYEXT:
381 case TargetOpcode::G_SEXT:
382 Val = Val.sext(Pair.second);
383 break;
384 case TargetOpcode::G_ZEXT:
385 Val = Val.zext(Pair.second);
386 break;
387 }
388 }
389
390 return ValueAndVReg{std::move(Val), VReg};
391}
392
393bool isIConstant(const MachineInstr *MI) {
394 if (!MI)
395 return false;
396 return MI->getOpcode() == TargetOpcode::G_CONSTANT;
397}
398
399bool isFConstant(const MachineInstr *MI) {
400 if (!MI)
401 return false;
402 return MI->getOpcode() == TargetOpcode::G_FCONSTANT;
403}
404
405bool isAnyConstant(const MachineInstr *MI) {
406 if (!MI)
407 return false;
408 unsigned Opc = MI->getOpcode();
409 return Opc == TargetOpcode::G_CONSTANT || Opc == TargetOpcode::G_FCONSTANT;
410}
411
412bool getCImmAsAPInt(const MachineInstr *MI, APInt &Result) {
413 const MachineOperand &CstVal = MI->getOperand(1);
414 if (!CstVal.isCImm())
415 return false;
416 Result = CstVal.getCImm()->getValue();
417 return true;
418}
419
420bool getCImmOrFPImmAsAPInt(const MachineInstr *MI, APInt &Result) {
421 const MachineOperand &CstVal = MI->getOperand(1);
422 if (CstVal.isCImm())
423 Result = CstVal.getCImm()->getValue();
424 else if (CstVal.isFPImm())
426 else
427 return false;
428 return true;
429}
430
431} // end anonymous namespace
432
434 Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
435 return getConstantVRegValWithLookThrough<isIConstant, getCImmAsAPInt>(
436 VReg, MRI, LookThroughInstrs);
437}
438
440 Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs,
441 bool LookThroughAnyExt) {
442 return getConstantVRegValWithLookThrough<isAnyConstant,
443 getCImmOrFPImmAsAPInt>(
444 VReg, MRI, LookThroughInstrs, LookThroughAnyExt);
445}
446
447std::optional<FPValueAndVReg> llvm::getFConstantVRegValWithLookThrough(
448 Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
449 auto Reg =
450 getConstantVRegValWithLookThrough<isFConstant, getCImmOrFPImmAsAPInt>(
451 VReg, MRI, LookThroughInstrs);
452 if (!Reg)
453 return std::nullopt;
454
455 APFloat FloatVal(getFltSemanticForLLT(LLT::scalar(Reg->Value.getBitWidth())),
456 Reg->Value);
457 return FPValueAndVReg{FloatVal, Reg->VReg};
458}
459
460const ConstantFP *
462 MachineInstr *MI = MRI.getVRegDef(VReg);
463 if (TargetOpcode::G_FCONSTANT != MI->getOpcode())
464 return nullptr;
465 return MI->getOperand(1).getFPImm();
466}
467
468std::optional<DefinitionAndSourceRegister>
470 Register DefSrcReg = Reg;
471 // This assumes that the code is in SSA form, so there should only be one
472 // definition.
473 auto DefIt = MRI.def_begin(Reg);
474 if (DefIt == MRI.def_end())
475 return {};
476 MachineOperand &DefOpnd = *DefIt;
477 MachineInstr *DefMI = DefOpnd.getParent();
478 auto DstTy = MRI.getType(DefOpnd.getReg());
479 if (!DstTy.isValid())
480 return std::nullopt;
481 unsigned Opc = DefMI->getOpcode();
482 while (Opc == TargetOpcode::COPY || isPreISelGenericOptimizationHint(Opc)) {
483 Register SrcReg = DefMI->getOperand(1).getReg();
484 auto SrcTy = MRI.getType(SrcReg);
485 if (!SrcTy.isValid())
486 break;
487 DefMI = MRI.getVRegDef(SrcReg);
488 DefSrcReg = SrcReg;
489 Opc = DefMI->getOpcode();
490 }
491 return DefinitionAndSourceRegister{DefMI, DefSrcReg};
492}
493
495 const MachineRegisterInfo &MRI) {
496 std::optional<DefinitionAndSourceRegister> DefSrcReg =
498 return DefSrcReg ? DefSrcReg->MI : nullptr;
499}
500
502 const MachineRegisterInfo &MRI) {
503 std::optional<DefinitionAndSourceRegister> DefSrcReg =
505 return DefSrcReg ? DefSrcReg->Reg : Register();
506}
507
508void llvm::extractParts(Register Reg, LLT Ty, int NumParts,
510 MachineIRBuilder &MIRBuilder,
511 MachineRegisterInfo &MRI) {
512 for (int i = 0; i < NumParts; ++i)
514 MIRBuilder.buildUnmerge(VRegs, Reg);
515}
516
517bool llvm::extractParts(Register Reg, LLT RegTy, LLT MainTy, LLT &LeftoverTy,
519 SmallVectorImpl<Register> &LeftoverRegs,
520 MachineIRBuilder &MIRBuilder,
521 MachineRegisterInfo &MRI) {
522 assert(!LeftoverTy.isValid() && "this is an out argument");
523
524 unsigned RegSize = RegTy.getSizeInBits();
525 unsigned MainSize = MainTy.getSizeInBits();
526 unsigned NumParts = RegSize / MainSize;
527 unsigned LeftoverSize = RegSize - NumParts * MainSize;
528
529 // Use an unmerge when possible.
530 if (LeftoverSize == 0) {
531 for (unsigned I = 0; I < NumParts; ++I)
532 VRegs.push_back(MRI.createGenericVirtualRegister(MainTy));
533 MIRBuilder.buildUnmerge(VRegs, Reg);
534 return true;
535 }
536
537 // Try to use unmerge for irregular vector split where possible
538 // For example when splitting a <6 x i32> into <4 x i32> with <2 x i32>
539 // leftover, it becomes:
540 // <2 x i32> %2, <2 x i32>%3, <2 x i32> %4 = G_UNMERGE_VALUE <6 x i32> %1
541 // <4 x i32> %5 = G_CONCAT_VECTOR <2 x i32> %2, <2 x i32> %3
542 if (RegTy.isVector() && MainTy.isVector()) {
543 unsigned RegNumElts = RegTy.getNumElements();
544 unsigned MainNumElts = MainTy.getNumElements();
545 unsigned LeftoverNumElts = RegNumElts % MainNumElts;
546 // If can unmerge to LeftoverTy, do it
547 if (MainNumElts % LeftoverNumElts == 0 &&
548 RegNumElts % LeftoverNumElts == 0 &&
549 RegTy.getScalarSizeInBits() == MainTy.getScalarSizeInBits() &&
550 LeftoverNumElts > 1) {
551 LeftoverTy = LLT::fixed_vector(LeftoverNumElts, RegTy.getElementType());
552
553 // Unmerge the SrcReg to LeftoverTy vectors
554 SmallVector<Register, 4> UnmergeValues;
555 extractParts(Reg, LeftoverTy, RegNumElts / LeftoverNumElts, UnmergeValues,
556 MIRBuilder, MRI);
557
558 // Find how many LeftoverTy makes one MainTy
559 unsigned LeftoverPerMain = MainNumElts / LeftoverNumElts;
560 unsigned NumOfLeftoverVal =
561 ((RegNumElts % MainNumElts) / LeftoverNumElts);
562
563 // Create as many MainTy as possible using unmerged value
564 SmallVector<Register, 4> MergeValues;
565 for (unsigned I = 0; I < UnmergeValues.size() - NumOfLeftoverVal; I++) {
566 MergeValues.push_back(UnmergeValues[I]);
567 if (MergeValues.size() == LeftoverPerMain) {
568 VRegs.push_back(
569 MIRBuilder.buildMergeLikeInstr(MainTy, MergeValues).getReg(0));
570 MergeValues.clear();
571 }
572 }
573 // Populate LeftoverRegs with the leftovers
574 for (unsigned I = UnmergeValues.size() - NumOfLeftoverVal;
575 I < UnmergeValues.size(); I++) {
576 LeftoverRegs.push_back(UnmergeValues[I]);
577 }
578 return true;
579 }
580 }
581 // Perform irregular split. Leftover is last element of RegPieces.
582 if (MainTy.isVector()) {
583 SmallVector<Register, 8> RegPieces;
584 extractVectorParts(Reg, MainTy.getNumElements(), RegPieces, MIRBuilder,
585 MRI);
586 for (unsigned i = 0; i < RegPieces.size() - 1; ++i)
587 VRegs.push_back(RegPieces[i]);
588 LeftoverRegs.push_back(RegPieces[RegPieces.size() - 1]);
589 LeftoverTy = MRI.getType(LeftoverRegs[0]);
590 return true;
591 }
592
593 LeftoverTy = LLT::integer(LeftoverSize);
594 // For irregular sizes, extract the individual parts.
595 for (unsigned I = 0; I != NumParts; ++I) {
596 Register NewReg = MRI.createGenericVirtualRegister(MainTy);
597 VRegs.push_back(NewReg);
598 MIRBuilder.buildExtract(NewReg, Reg, MainSize * I);
599 }
600
601 for (unsigned Offset = MainSize * NumParts; Offset < RegSize;
602 Offset += LeftoverSize) {
603 Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy);
604 LeftoverRegs.push_back(NewReg);
605 MIRBuilder.buildExtract(NewReg, Reg, Offset);
606 }
607
608 return true;
609}
610
611void llvm::extractVectorParts(Register Reg, unsigned NumElts,
613 MachineIRBuilder &MIRBuilder,
614 MachineRegisterInfo &MRI) {
615 LLT RegTy = MRI.getType(Reg);
616 assert(RegTy.isVector() && "Expected a vector type");
617
618 LLT EltTy = RegTy.getElementType();
619 LLT NarrowTy = (NumElts == 1) ? EltTy : LLT::fixed_vector(NumElts, EltTy);
620 unsigned RegNumElts = RegTy.getNumElements();
621 unsigned LeftoverNumElts = RegNumElts % NumElts;
622 unsigned NumNarrowTyPieces = RegNumElts / NumElts;
623
624 // Perfect split without leftover
625 if (LeftoverNumElts == 0)
626 return extractParts(Reg, NarrowTy, NumNarrowTyPieces, VRegs, MIRBuilder,
627 MRI);
628
629 // Irregular split. Provide direct access to all elements for artifact
630 // combiner using unmerge to elements. Then build vectors with NumElts
631 // elements. Remaining element(s) will be (used to build vector) Leftover.
633 extractParts(Reg, EltTy, RegNumElts, Elts, MIRBuilder, MRI);
634
635 unsigned Offset = 0;
636 // Requested sub-vectors of NarrowTy.
637 for (unsigned i = 0; i < NumNarrowTyPieces; ++i, Offset += NumElts) {
638 ArrayRef<Register> Pieces(&Elts[Offset], NumElts);
639 VRegs.push_back(MIRBuilder.buildMergeLikeInstr(NarrowTy, Pieces).getReg(0));
640 }
641
642 // Leftover element(s).
643 if (LeftoverNumElts == 1) {
644 VRegs.push_back(Elts[Offset]);
645 } else {
646 LLT LeftoverTy = LLT::fixed_vector(LeftoverNumElts, EltTy);
647 ArrayRef<Register> Pieces(&Elts[Offset], LeftoverNumElts);
648 VRegs.push_back(
649 MIRBuilder.buildMergeLikeInstr(LeftoverTy, Pieces).getReg(0));
650 }
651}
652
654 const MachineRegisterInfo &MRI) {
656 return DefMI && DefMI->getOpcode() == Opcode ? DefMI : nullptr;
657}
658
659APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
660 if (Size == 32)
661 return APFloat(float(Val));
662 if (Size == 64)
663 return APFloat(Val);
664 if (Size != 16)
665 llvm_unreachable("Unsupported FPConstant size");
666 bool Ignored;
667 APFloat APF(Val);
669 return APF;
670}
671
672std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
673 const Register Op1,
674 const Register Op2,
675 const MachineRegisterInfo &MRI) {
676 auto MaybeOp2Cst = getAnyConstantVRegValWithLookThrough(Op2, MRI, false);
677 if (!MaybeOp2Cst)
678 return std::nullopt;
679
680 auto MaybeOp1Cst = getAnyConstantVRegValWithLookThrough(Op1, MRI, false);
681 if (!MaybeOp1Cst)
682 return std::nullopt;
683
684 const APInt &C1 = MaybeOp1Cst->Value;
685 const APInt &C2 = MaybeOp2Cst->Value;
686 switch (Opcode) {
687 default:
688 break;
689 case TargetOpcode::G_ADD:
690 return C1 + C2;
691 case TargetOpcode::G_PTR_ADD:
692 // Types can be of different width here.
693 // Result needs to be the same width as C1, so trunc or sext C2.
694 return C1 + C2.sextOrTrunc(C1.getBitWidth());
695 case TargetOpcode::G_AND:
696 return C1 & C2;
697 case TargetOpcode::G_ASHR:
698 return C1.ashr(C2);
699 case TargetOpcode::G_LSHR:
700 return C1.lshr(C2);
701 case TargetOpcode::G_MUL:
702 return C1 * C2;
703 case TargetOpcode::G_OR:
704 return C1 | C2;
705 case TargetOpcode::G_SHL:
706 return C1 << C2;
707 case TargetOpcode::G_SUB:
708 return C1 - C2;
709 case TargetOpcode::G_XOR:
710 return C1 ^ C2;
711 case TargetOpcode::G_UDIV:
712 if (!C2.getBoolValue())
713 break;
714 return C1.udiv(C2);
715 case TargetOpcode::G_SDIV:
716 if (!C2.getBoolValue())
717 break;
718 return C1.sdiv(C2);
719 case TargetOpcode::G_UREM:
720 if (!C2.getBoolValue())
721 break;
722 return C1.urem(C2);
723 case TargetOpcode::G_SREM:
724 if (!C2.getBoolValue())
725 break;
726 return C1.srem(C2);
727 case TargetOpcode::G_SMIN:
728 return APIntOps::smin(C1, C2);
729 case TargetOpcode::G_SMAX:
730 return APIntOps::smax(C1, C2);
731 case TargetOpcode::G_UMIN:
732 return APIntOps::umin(C1, C2);
733 case TargetOpcode::G_UMAX:
734 return APIntOps::umax(C1, C2);
735 }
736
737 return std::nullopt;
738}
739
740std::optional<APFloat>
741llvm::ConstantFoldFPBinOp(unsigned Opcode, const Register Op1,
742 const Register Op2, const MachineRegisterInfo &MRI) {
743 const ConstantFP *Op2Cst = getConstantFPVRegVal(Op2, MRI);
744 if (!Op2Cst)
745 return std::nullopt;
746
747 const ConstantFP *Op1Cst = getConstantFPVRegVal(Op1, MRI);
748 if (!Op1Cst)
749 return std::nullopt;
750
751 APFloat C1 = Op1Cst->getValueAPF();
752 const APFloat &C2 = Op2Cst->getValueAPF();
753 switch (Opcode) {
754 case TargetOpcode::G_FADD:
756 return C1;
757 case TargetOpcode::G_FSUB:
759 return C1;
760 case TargetOpcode::G_FMUL:
762 return C1;
763 case TargetOpcode::G_FDIV:
765 return C1;
766 case TargetOpcode::G_FREM:
767 C1.mod(C2);
768 return C1;
769 case TargetOpcode::G_FCOPYSIGN:
770 C1.copySign(C2);
771 return C1;
772 case TargetOpcode::G_FMINNUM:
773 return minnum(C1, C2);
774 case TargetOpcode::G_FMAXNUM:
775 return maxnum(C1, C2);
776 case TargetOpcode::G_FMINIMUM:
777 return minimum(C1, C2);
778 case TargetOpcode::G_FMAXIMUM:
779 return maximum(C1, C2);
780 case TargetOpcode::G_FMINIMUMNUM:
781 return minimumnum(C1, C2);
782 case TargetOpcode::G_FMAXIMUMNUM:
783 return maximumnum(C1, C2);
784 case TargetOpcode::G_FMINNUM_IEEE:
785 case TargetOpcode::G_FMAXNUM_IEEE:
786 // FIXME: These operations were unfortunately named. fminnum/fmaxnum do not
787 // follow the IEEE behavior for signaling nans and follow libm's fmin/fmax,
788 // and currently there isn't a nice wrapper in APFloat for the version with
789 // correct snan handling.
790 break;
791 default:
792 break;
793 }
794
795 return std::nullopt;
796}
797
799 const MachineRegisterInfo &MRI) {
800 if (auto *BV = getOpcodeDef<GBuildVector>(Reg, MRI))
801 return BV;
802
803 auto *Bitcast = getOpcodeDef(TargetOpcode::G_BITCAST, Reg, MRI);
804 if (!Bitcast)
805 return nullptr;
806
807 auto [Dst, DstTy, Src, SrcTy] = Bitcast->getFirst2RegLLTs();
808 if (!SrcTy.isVector() || !DstTy.isVector())
809 return nullptr;
810 if (SrcTy.getElementCount() != DstTy.getElementCount())
811 return nullptr;
812 if (SrcTy.getScalarSizeInBits() != DstTy.getScalarSizeInBits())
813 return nullptr;
814
815 return getOpcodeDef<GBuildVector>(Src, MRI);
816}
817
819llvm::ConstantFoldVectorBinop(unsigned Opcode, const Register Op1,
820 const Register Op2,
821 const MachineRegisterInfo &MRI) {
822 auto *SrcVec2 = getBuildVectorLikeDef(Op2, MRI);
823 if (!SrcVec2)
824 return SmallVector<APInt>();
825
826 auto *SrcVec1 = getBuildVectorLikeDef(Op1, MRI);
827 if (!SrcVec1)
828 return SmallVector<APInt>();
829
830 SmallVector<APInt> FoldedElements;
831 for (unsigned Idx = 0, E = SrcVec1->getNumSources(); Idx < E; ++Idx) {
832 auto MaybeCst = ConstantFoldBinOp(Opcode, SrcVec1->getSourceReg(Idx),
833 SrcVec2->getSourceReg(Idx), MRI);
834 if (!MaybeCst)
835 return SmallVector<APInt>();
836 FoldedElements.push_back(*MaybeCst);
837 }
838 return FoldedElements;
839}
840
842 const MachinePointerInfo &MPO) {
845 MachineFrameInfo &MFI = MF.getFrameInfo();
846 return commonAlignment(MFI.getObjectAlign(FSPV->getFrameIndex()),
847 MPO.Offset);
848 }
849
850 if (const Value *V = dyn_cast_if_present<const Value *>(MPO.V)) {
851 const Module *M = MF.getFunction().getParent();
852 return V->getPointerAlignment(M->getDataLayout());
853 }
854
855 return Align(1);
856}
857
859 const TargetInstrInfo &TII,
860 MCRegister PhysReg,
861 const TargetRegisterClass &RC,
862 const DebugLoc &DL, LLT RegTy) {
863 MachineBasicBlock &EntryMBB = MF.front();
865 Register LiveIn = MRI.getLiveInVirtReg(PhysReg);
866 if (LiveIn) {
867 MachineInstr *Def = MRI.getVRegDef(LiveIn);
868 if (Def) {
869 // FIXME: Should the verifier check this is in the entry block?
870 assert(Def->getParent() == &EntryMBB && "live-in copy not in entry block");
871 return LiveIn;
872 }
873
874 // It's possible the incoming argument register and copy was added during
875 // lowering, but later deleted due to being/becoming dead. If this happens,
876 // re-insert the copy.
877 } else {
878 // The live in register was not present, so add it.
879 LiveIn = MF.addLiveIn(PhysReg, &RC);
880 if (RegTy.isValid())
881 MRI.setType(LiveIn, RegTy);
882 }
883
884 BuildMI(EntryMBB, EntryMBB.begin(), DL, TII.get(TargetOpcode::COPY), LiveIn)
885 .addReg(PhysReg);
886 if (!EntryMBB.isLiveIn(PhysReg))
887 EntryMBB.addLiveIn(PhysReg);
888 return LiveIn;
889}
890
891std::optional<APInt> llvm::ConstantFoldExtOp(unsigned Opcode,
892 const Register Op1, uint64_t Imm,
893 const MachineRegisterInfo &MRI) {
894 auto MaybeOp1Cst = getIConstantVRegVal(Op1, MRI);
895 if (MaybeOp1Cst) {
896 switch (Opcode) {
897 default:
898 break;
899 case TargetOpcode::G_SEXT_INREG: {
900 LLT Ty = MRI.getType(Op1);
901 return MaybeOp1Cst->trunc(Imm).sext(Ty.getScalarSizeInBits());
902 }
903 }
904 }
905 return std::nullopt;
906}
907
908std::optional<APInt> llvm::ConstantFoldCastOp(unsigned Opcode, LLT DstTy,
909 const Register Op0,
910 const MachineRegisterInfo &MRI) {
911 std::optional<APInt> Val = getIConstantVRegVal(Op0, MRI);
912 if (!Val)
913 return Val;
914
915 const unsigned DstSize = DstTy.getScalarSizeInBits();
916
917 switch (Opcode) {
918 case TargetOpcode::G_SEXT:
919 return Val->sext(DstSize);
920 case TargetOpcode::G_ZEXT:
921 case TargetOpcode::G_ANYEXT:
922 // TODO: DAG considers target preference when constant folding any_extend.
923 return Val->zext(DstSize);
924 default:
925 break;
926 }
927
928 llvm_unreachable("unexpected cast opcode to constant fold");
929}
930
931std::optional<APFloat>
932llvm::ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
933 const MachineRegisterInfo &MRI) {
934 assert(Opcode == TargetOpcode::G_SITOFP || Opcode == TargetOpcode::G_UITOFP);
935 if (auto MaybeSrcVal = getIConstantVRegVal(Src, MRI)) {
936 APFloat DstVal(getFltSemanticForLLT(DstTy));
937 DstVal.convertFromAPInt(*MaybeSrcVal, Opcode == TargetOpcode::G_SITOFP,
939 return DstVal;
940 }
941 return std::nullopt;
942}
943
945llvm::ConstantFoldUnaryIntOp(unsigned Opcode, LLT DstTy, Register Src,
946 const MachineRegisterInfo &MRI) {
947 unsigned EltBits = DstTy.getScalarSizeInBits();
948 auto Fold = [Opcode, EltBits](const APInt &V) -> APInt {
949 switch (Opcode) {
950 case TargetOpcode::G_CTLZ:
951 case TargetOpcode::G_CTLZ_ZERO_POISON:
952 return APInt(EltBits, V.countl_zero());
953 case TargetOpcode::G_CTTZ:
954 case TargetOpcode::G_CTTZ_ZERO_POISON:
955 return APInt(EltBits, V.countr_zero());
956 case TargetOpcode::G_CTPOP:
957 return APInt(EltBits, V.popcount());
958 case TargetOpcode::G_ABS:
959 return V.abs();
960 case TargetOpcode::G_BSWAP:
961 return V.byteSwap();
962 case TargetOpcode::G_BITREVERSE:
963 return V.reverseBits();
964 }
965 llvm_unreachable("unexpected opcode in ConstantFoldUnaryIntOp");
966 };
967
968 auto tryFoldScalar = [&](Register R) -> std::optional<APInt> {
969 if (auto MaybeCst = getIConstantVRegVal(R, MRI))
970 return Fold(*MaybeCst);
971 return std::nullopt;
972 };
973 if (MRI.getType(Src).isVector()) {
974 auto *BV = getOpcodeDef<GBuildVector>(Src, MRI);
975 if (!BV)
976 return {};
977 SmallVector<APInt> Folded;
978 for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
979 if (auto MaybeFold = tryFoldScalar(BV->getSourceReg(SrcIdx))) {
980 Folded.emplace_back(std::move(*MaybeFold));
981 continue;
982 }
983 return {};
984 }
985 return Folded;
986 }
987 if (auto MaybeCst = tryFoldScalar(Src))
988 return {std::move(*MaybeCst)};
989 return {};
990}
991
992std::optional<SmallVector<APInt>>
993llvm::ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2,
994 unsigned DstScalarSizeInBits, unsigned ExtOp,
995 const MachineRegisterInfo &MRI) {
996 assert(ExtOp == TargetOpcode::G_SEXT || ExtOp == TargetOpcode::G_ZEXT ||
997 ExtOp == TargetOpcode::G_ANYEXT);
998
999 const LLT Ty = MRI.getType(Op1);
1000
1001 auto GetICmpResultCst = [&](bool IsTrue) {
1002 if (IsTrue)
1003 return ExtOp == TargetOpcode::G_SEXT
1004 ? APInt::getAllOnes(DstScalarSizeInBits)
1005 : APInt::getOneBitSet(DstScalarSizeInBits, 0);
1006 return APInt::getZero(DstScalarSizeInBits);
1007 };
1008
1009 auto TryFoldScalar = [&](Register LHS, Register RHS) -> std::optional<APInt> {
1010 auto RHSCst = getIConstantVRegVal(RHS, MRI);
1011 if (!RHSCst)
1012 return std::nullopt;
1013 auto LHSCst = getIConstantVRegVal(LHS, MRI);
1014 if (!LHSCst)
1015 return std::nullopt;
1016
1017 switch (Pred) {
1019 return GetICmpResultCst(LHSCst->eq(*RHSCst));
1021 return GetICmpResultCst(LHSCst->ne(*RHSCst));
1023 return GetICmpResultCst(LHSCst->ugt(*RHSCst));
1025 return GetICmpResultCst(LHSCst->uge(*RHSCst));
1027 return GetICmpResultCst(LHSCst->ult(*RHSCst));
1029 return GetICmpResultCst(LHSCst->ule(*RHSCst));
1031 return GetICmpResultCst(LHSCst->sgt(*RHSCst));
1033 return GetICmpResultCst(LHSCst->sge(*RHSCst));
1035 return GetICmpResultCst(LHSCst->slt(*RHSCst));
1037 return GetICmpResultCst(LHSCst->sle(*RHSCst));
1038 default:
1039 return std::nullopt;
1040 }
1041 };
1042
1043 SmallVector<APInt> FoldedICmps;
1044
1045 if (Ty.isVector()) {
1046 // Try to constant fold each element.
1047 auto *BV1 = getOpcodeDef<GBuildVector>(Op1, MRI);
1048 auto *BV2 = getOpcodeDef<GBuildVector>(Op2, MRI);
1049 if (!BV1 || !BV2)
1050 return std::nullopt;
1051 assert(BV1->getNumSources() == BV2->getNumSources() && "Invalid vectors");
1052 for (unsigned I = 0; I < BV1->getNumSources(); ++I) {
1053 if (auto MaybeFold =
1054 TryFoldScalar(BV1->getSourceReg(I), BV2->getSourceReg(I))) {
1055 FoldedICmps.emplace_back(*MaybeFold);
1056 continue;
1057 }
1058 return std::nullopt;
1059 }
1060 return FoldedICmps;
1061 }
1062
1063 if (auto MaybeCst = TryFoldScalar(Op1, Op2)) {
1064 FoldedICmps.emplace_back(*MaybeCst);
1065 return FoldedICmps;
1066 }
1067
1068 return std::nullopt;
1069}
1070
1072 GISelValueTracking *VT) {
1073 std::optional<DefinitionAndSourceRegister> DefSrcReg =
1075 if (!DefSrcReg)
1076 return false;
1077
1078 const MachineInstr &MI = *DefSrcReg->MI;
1079 const LLT Ty = MRI.getType(Reg);
1080
1081 switch (MI.getOpcode()) {
1082 case TargetOpcode::G_CONSTANT: {
1083 unsigned BitWidth = Ty.getScalarSizeInBits();
1084 const ConstantInt *CI = MI.getOperand(1).getCImm();
1085 return CI->getValue().zextOrTrunc(BitWidth).isPowerOf2();
1086 }
1087 case TargetOpcode::G_SHL: {
1088 // A left-shift of a constant one will have exactly one bit set because
1089 // shifting the bit off the end is undefined.
1090
1091 // TODO: Constant splat
1092 if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
1093 if (*ConstLHS == 1)
1094 return true;
1095 }
1096
1097 break;
1098 }
1099 case TargetOpcode::G_LSHR: {
1100 if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
1101 if (ConstLHS->isSignMask())
1102 return true;
1103 }
1104
1105 break;
1106 }
1107 case TargetOpcode::G_BUILD_VECTOR: {
1108 // TODO: Probably should have a recursion depth guard since you could have
1109 // bitcasted vector elements.
1110 for (const MachineOperand &MO : llvm::drop_begin(MI.operands()))
1111 if (!isKnownToBeAPowerOfTwo(MO.getReg(), MRI, VT))
1112 return false;
1113
1114 return true;
1115 }
1116 case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1117 // Only handle constants since we would need to know if number of leading
1118 // zeros is greater than the truncation amount.
1119 const unsigned BitWidth = Ty.getScalarSizeInBits();
1120 for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) {
1121 auto Const = getIConstantVRegVal(MO.getReg(), MRI);
1122 if (!Const || !Const->zextOrTrunc(BitWidth).isPowerOf2())
1123 return false;
1124 }
1125
1126 return true;
1127 }
1128 default:
1129 break;
1130 }
1131
1132 if (!VT)
1133 return false;
1134
1135 // More could be done here, though the above checks are enough
1136 // to handle some common cases.
1137
1138 // Fall back to computeKnownBits to catch other known cases.
1139 KnownBits Known = VT->getKnownBits(Reg);
1140 return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
1141}
1142
1146
1147LLT llvm::getLCMType(LLT OrigTy, LLT TargetTy) {
1148 if (OrigTy.getSizeInBits() == TargetTy.getSizeInBits())
1149 return OrigTy;
1150
1151 if (OrigTy.isVector() && TargetTy.isVector()) {
1152 LLT OrigElt = OrigTy.getElementType();
1153 LLT TargetElt = TargetTy.getElementType();
1154
1155 // TODO: The docstring for this function says the intention is to use this
1156 // function to build MERGE/UNMERGE instructions. It won't be the case that
1157 // we generate a MERGE/UNMERGE between fixed and scalable vector types. We
1158 // could implement getLCMType between the two in the future if there was a
1159 // need, but it is not worth it now as this function should not be used in
1160 // that way.
1161 assert(((OrigTy.isScalableVector() && !TargetTy.isFixedVector()) ||
1162 (OrigTy.isFixedVector() && !TargetTy.isScalableVector())) &&
1163 "getLCMType not implemented between fixed and scalable vectors.");
1164
1165 if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
1166 int GCDMinElts = std::gcd(OrigTy.getElementCount().getKnownMinValue(),
1167 TargetTy.getElementCount().getKnownMinValue());
1168 // Prefer the original element type.
1170 TargetTy.getElementCount().getKnownMinValue());
1171 return LLT::vector(Mul.divideCoefficientBy(GCDMinElts),
1172 OrigTy.getElementType());
1173 }
1174 unsigned LCM = std::lcm(OrigTy.getSizeInBits().getKnownMinValue(),
1175 TargetTy.getSizeInBits().getKnownMinValue());
1176 return LLT::vector(
1177 ElementCount::get(LCM / OrigElt.getSizeInBits(), OrigTy.isScalable()),
1178 OrigElt);
1179 }
1180
1181 // One type is scalar, one type is vector
1182 if (OrigTy.isVector() || TargetTy.isVector()) {
1183 LLT VecTy = OrigTy.isVector() ? OrigTy : TargetTy;
1184 LLT ScalarTy = OrigTy.isVector() ? TargetTy : OrigTy;
1185 LLT EltTy = VecTy.getElementType();
1186 LLT OrigEltTy = OrigTy.isVector() ? OrigTy.getElementType() : OrigTy;
1187
1188 // Prefer scalar type from OrigTy.
1189 if (EltTy.getSizeInBits() == ScalarTy.getSizeInBits())
1190 return LLT::vector(VecTy.getElementCount(), OrigEltTy);
1191
1192 // Different size scalars. Create vector with the same total size.
1193 // LCM will take fixed/scalable from VecTy.
1194 unsigned LCM = std::lcm(EltTy.getSizeInBits().getFixedValue() *
1196 ScalarTy.getSizeInBits().getFixedValue());
1197 // Prefer type from OrigTy
1198 return LLT::vector(ElementCount::get(LCM / OrigEltTy.getSizeInBits(),
1199 VecTy.getElementCount().isScalable()),
1200 OrigEltTy);
1201 }
1202
1203 // At this point, both types are scalars of different size
1204 unsigned LCM = std::lcm(OrigTy.getSizeInBits().getFixedValue(),
1205 TargetTy.getSizeInBits().getFixedValue());
1206 // Preserve pointer types.
1207 if (LCM == OrigTy.getSizeInBits())
1208 return OrigTy;
1209 if (LCM == TargetTy.getSizeInBits())
1210 return TargetTy;
1211 return LLT::scalar(LCM);
1212}
1213
1214LLT llvm::getCoverTy(LLT OrigTy, LLT TargetTy) {
1215
1216 if ((OrigTy.isScalableVector() && TargetTy.isFixedVector()) ||
1217 (OrigTy.isFixedVector() && TargetTy.isScalableVector()))
1219 "getCoverTy not implemented between fixed and scalable vectors.");
1220
1221 if (!OrigTy.isVector() || !TargetTy.isVector() || OrigTy == TargetTy ||
1222 (OrigTy.getScalarSizeInBits() != TargetTy.getScalarSizeInBits()))
1223 return getLCMType(OrigTy, TargetTy);
1224
1225 unsigned OrigTyNumElts = OrigTy.getElementCount().getKnownMinValue();
1226 unsigned TargetTyNumElts = TargetTy.getElementCount().getKnownMinValue();
1227 if (OrigTyNumElts % TargetTyNumElts == 0)
1228 return OrigTy;
1229
1230 unsigned NumElts = alignTo(OrigTyNumElts, TargetTyNumElts);
1232 OrigTy.getElementType());
1233}
1234
1235LLT llvm::getGCDType(LLT OrigTy, LLT TargetTy) {
1236 if (OrigTy.getSizeInBits() == TargetTy.getSizeInBits())
1237 return OrigTy;
1238
1239 if (OrigTy.isVector() && TargetTy.isVector()) {
1240 LLT OrigElt = OrigTy.getElementType();
1241
1242 // TODO: The docstring for this function says the intention is to use this
1243 // function to build MERGE/UNMERGE instructions. It won't be the case that
1244 // we generate a MERGE/UNMERGE between fixed and scalable vector types. We
1245 // could implement getGCDType between the two in the future if there was a
1246 // need, but it is not worth it now as this function should not be used in
1247 // that way.
1248 assert(((OrigTy.isScalableVector() && !TargetTy.isFixedVector()) ||
1249 (OrigTy.isFixedVector() && !TargetTy.isScalableVector())) &&
1250 "getGCDType not implemented between fixed and scalable vectors.");
1251
1252 unsigned GCD = std::gcd(OrigTy.getSizeInBits().getKnownMinValue(),
1253 TargetTy.getSizeInBits().getKnownMinValue());
1254 if (GCD == OrigElt.getSizeInBits())
1256 OrigElt);
1257
1258 // Cannot produce original element type, but both have vscale in common.
1259 if (GCD < OrigElt.getSizeInBits())
1261 GCD);
1262
1263 return LLT::vector(
1265 OrigTy.isScalable()),
1266 OrigElt);
1267 }
1268
1269 // If one type is vector and the element size matches the scalar size, then
1270 // the gcd is the scalar type.
1271 if (OrigTy.isVector() &&
1272 OrigTy.getElementType().getSizeInBits() == TargetTy.getSizeInBits())
1273 return OrigTy.getElementType();
1274 if (TargetTy.isVector() &&
1275 TargetTy.getElementType().getSizeInBits() == OrigTy.getSizeInBits())
1276 return OrigTy;
1277
1278 // At this point, both types are either scalars of different type or one is a
1279 // vector and one is a scalar. If both types are scalars, the GCD type is the
1280 // GCD between the two scalar sizes. If one is vector and one is scalar, then
1281 // the GCD type is the GCD between the scalar and the vector element size.
1282 LLT OrigScalar = OrigTy.getScalarType();
1283 LLT TargetScalar = TargetTy.getScalarType();
1284 unsigned GCD = std::gcd(OrigScalar.getSizeInBits().getFixedValue(),
1285 TargetScalar.getSizeInBits().getFixedValue());
1286 return LLT::integer(GCD);
1287}
1288
1290 assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
1291 "Only G_SHUFFLE_VECTOR can have a splat index!");
1292 ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
1293 auto FirstDefinedIdx = find_if(Mask, [](int Elt) { return Elt >= 0; });
1294
1295 // If all elements are undefined, this shuffle can be considered a splat.
1296 // Return 0 for better potential for callers to simplify.
1297 if (FirstDefinedIdx == Mask.end())
1298 return 0;
1299
1300 // Make sure all remaining elements are either undef or the same
1301 // as the first non-undef value.
1302 int SplatValue = *FirstDefinedIdx;
1303 if (any_of(make_range(std::next(FirstDefinedIdx), Mask.end()),
1304 [&SplatValue](int Elt) { return Elt >= 0 && Elt != SplatValue; }))
1305 return std::nullopt;
1306
1307 return SplatValue;
1308}
1309
1310static bool isBuildVectorOp(unsigned Opcode) {
1311 return Opcode == TargetOpcode::G_BUILD_VECTOR ||
1312 Opcode == TargetOpcode::G_BUILD_VECTOR_TRUNC;
1313}
1314
1315namespace {
1316
1317std::optional<ValueAndVReg> getAnyConstantSplat(Register VReg,
1318 const MachineRegisterInfo &MRI,
1319 bool AllowUndef) {
1320 MachineInstr *MI = getDefIgnoringCopies(VReg, MRI);
1321 if (!MI)
1322 return std::nullopt;
1323
1324 bool isConcatVectorsOp = MI->getOpcode() == TargetOpcode::G_CONCAT_VECTORS;
1325 if (!isBuildVectorOp(MI->getOpcode()) && !isConcatVectorsOp)
1326 return std::nullopt;
1327
1328 std::optional<ValueAndVReg> SplatValAndReg;
1329 for (MachineOperand &Op : MI->uses()) {
1330 Register Element = Op.getReg();
1331 // If we have a G_CONCAT_VECTOR, we recursively look into the
1332 // vectors that we're concatenating to see if they're splats.
1333 auto ElementValAndReg =
1334 isConcatVectorsOp
1335 ? getAnyConstantSplat(Element, MRI, AllowUndef)
1337
1338 // If AllowUndef, treat undef as value that will result in a constant splat.
1339 if (!ElementValAndReg) {
1340 if (AllowUndef && isa<GImplicitDef>(MRI.getVRegDef(Element)))
1341 continue;
1342 return std::nullopt;
1343 }
1344
1345 // Record splat value
1346 if (!SplatValAndReg)
1347 SplatValAndReg = ElementValAndReg;
1348
1349 // Different constant than the one already recorded, not a constant splat.
1350 if (SplatValAndReg->Value != ElementValAndReg->Value)
1351 return std::nullopt;
1352 }
1353
1354 return SplatValAndReg;
1355}
1356
1357} // end anonymous namespace
1358
1360 const MachineRegisterInfo &MRI,
1361 int64_t SplatValue, bool AllowUndef) {
1362 if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef))
1363 return SplatValAndReg->Value.getSExtValue() == SplatValue;
1364
1365 return false;
1366}
1367
1369 const MachineRegisterInfo &MRI,
1370 const APInt &SplatValue,
1371 bool AllowUndef) {
1372 if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef)) {
1373 if (SplatValAndReg->Value.getBitWidth() < SplatValue.getBitWidth())
1374 return APInt::isSameValue(
1375 SplatValAndReg->Value.sext(SplatValue.getBitWidth()), SplatValue);
1376 return APInt::isSameValue(
1377 SplatValAndReg->Value,
1378 SplatValue.sext(SplatValAndReg->Value.getBitWidth()));
1379 }
1380
1381 return false;
1382}
1383
1385 const MachineRegisterInfo &MRI,
1386 int64_t SplatValue, bool AllowUndef) {
1387 return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
1388 AllowUndef);
1389}
1390
1392 const MachineRegisterInfo &MRI,
1393 const APInt &SplatValue,
1394 bool AllowUndef) {
1395 return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
1396 AllowUndef);
1397}
1398
1399std::optional<APInt>
1401 if (auto SplatValAndReg =
1402 getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false)) {
1403 if (std::optional<ValueAndVReg> ValAndVReg =
1404 getIConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI))
1405 return ValAndVReg->Value;
1406 }
1407
1408 return std::nullopt;
1409}
1410
1411std::optional<APInt>
1413 const MachineRegisterInfo &MRI) {
1414 return getIConstantSplatVal(MI.getOperand(0).getReg(), MRI);
1415}
1416
1417std::optional<int64_t>
1419 const MachineRegisterInfo &MRI) {
1420 if (auto SplatValAndReg =
1421 getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false))
1422 return getIConstantVRegSExtVal(SplatValAndReg->VReg, MRI);
1423 return std::nullopt;
1424}
1425
1426std::optional<int64_t>
1428 const MachineRegisterInfo &MRI) {
1429 return getIConstantSplatSExtVal(MI.getOperand(0).getReg(), MRI);
1430}
1431
1432std::optional<FPValueAndVReg>
1434 bool AllowUndef) {
1435 if (auto SplatValAndReg = getAnyConstantSplat(VReg, MRI, AllowUndef))
1436 return getFConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
1437 return std::nullopt;
1438}
1439
1441 const MachineRegisterInfo &MRI,
1442 bool AllowUndef) {
1443 return isBuildVectorConstantSplat(MI, MRI, 0, AllowUndef);
1444}
1445
1447 const MachineRegisterInfo &MRI,
1448 bool AllowUndef) {
1449 return isBuildVectorConstantSplat(MI, MRI, -1, AllowUndef);
1450}
1451
1452std::optional<RegOrConstant>
1454 unsigned Opc = MI.getOpcode();
1455 if (!isBuildVectorOp(Opc))
1456 return std::nullopt;
1457 if (auto Splat = getIConstantSplatSExtVal(MI, MRI))
1458 return RegOrConstant(*Splat);
1459 auto Reg = MI.getOperand(1).getReg();
1460 if (any_of(drop_begin(MI.operands(), 2),
1461 [&Reg](const MachineOperand &Op) { return Op.getReg() != Reg; }))
1462 return std::nullopt;
1463 return RegOrConstant(Reg);
1464}
1465
1467 const MachineRegisterInfo &MRI,
1468 bool AllowFP = true,
1469 bool AllowOpaqueConstants = true) {
1470 switch (MI.getOpcode()) {
1471 case TargetOpcode::G_CONSTANT:
1472 case TargetOpcode::G_IMPLICIT_DEF:
1473 return true;
1474 case TargetOpcode::G_FCONSTANT:
1475 return AllowFP;
1476 case TargetOpcode::G_GLOBAL_VALUE:
1477 case TargetOpcode::G_FRAME_INDEX:
1478 case TargetOpcode::G_BLOCK_ADDR:
1479 case TargetOpcode::G_JUMP_TABLE:
1480 return AllowOpaqueConstants;
1481 default:
1482 return false;
1483 }
1484}
1485
1487 const MachineRegisterInfo &MRI) {
1488 Register Def = MI.getOperand(0).getReg();
1489 if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1490 return true;
1492 if (!BV)
1493 return false;
1494 for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
1495 if (getIConstantVRegValWithLookThrough(BV->getSourceReg(SrcIdx), MRI) ||
1496 getOpcodeDef<GImplicitDef>(BV->getSourceReg(SrcIdx), MRI))
1497 continue;
1498 return false;
1499 }
1500 return true;
1501}
1502
1504 const MachineRegisterInfo &MRI,
1505 bool AllowFP, bool AllowOpaqueConstants) {
1506 if (isConstantScalar(MI, MRI, AllowFP, AllowOpaqueConstants))
1507 return true;
1508
1509 if (!isBuildVectorOp(MI.getOpcode()))
1510 return false;
1511
1512 const unsigned NumOps = MI.getNumOperands();
1513 for (unsigned I = 1; I != NumOps; ++I) {
1514 const MachineInstr *ElementDef = MRI.getVRegDef(MI.getOperand(I).getReg());
1515 if (!isConstantScalar(*ElementDef, MRI, AllowFP, AllowOpaqueConstants))
1516 return false;
1517 }
1518
1519 return true;
1520}
1521
1522std::optional<APInt>
1524 const MachineRegisterInfo &MRI) {
1525 Register Def = MI.getOperand(0).getReg();
1526 if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1527 return C->Value;
1528 auto MaybeCst = getIConstantSplatSExtVal(MI, MRI);
1529 if (!MaybeCst)
1530 return std::nullopt;
1531 const unsigned ScalarSize = MRI.getType(Def).getScalarSizeInBits();
1532 return APInt(ScalarSize, *MaybeCst, true);
1533}
1534
1535std::optional<APFloat>
1537 const MachineRegisterInfo &MRI) {
1538 Register Def = MI.getOperand(0).getReg();
1539 if (auto FpConst = getFConstantVRegValWithLookThrough(Def, MRI))
1540 return FpConst->Value;
1541 auto MaybeCstFP = getFConstantSplat(Def, MRI, /*allowUndef=*/false);
1542 if (!MaybeCstFP)
1543 return std::nullopt;
1544 return MaybeCstFP->Value;
1545}
1546
1548 const MachineRegisterInfo &MRI, bool AllowUndefs) {
1549 switch (MI.getOpcode()) {
1550 case TargetOpcode::G_IMPLICIT_DEF:
1551 return AllowUndefs;
1552 case TargetOpcode::G_CONSTANT:
1553 return MI.getOperand(1).getCImm()->isNullValue();
1554 case TargetOpcode::G_FCONSTANT: {
1555 const ConstantFP *FPImm = MI.getOperand(1).getFPImm();
1556 return FPImm->isZero() && !FPImm->isNegative();
1557 }
1558 default:
1559 if (!AllowUndefs) // TODO: isBuildVectorAllZeros assumes undef is OK already
1560 return false;
1561 return isBuildVectorAllZeros(MI, MRI);
1562 }
1563}
1564
1566 const MachineRegisterInfo &MRI,
1567 bool AllowUndefs) {
1568 switch (MI.getOpcode()) {
1569 case TargetOpcode::G_IMPLICIT_DEF:
1570 return AllowUndefs;
1571 case TargetOpcode::G_CONSTANT:
1572 return MI.getOperand(1).getCImm()->isAllOnesValue();
1573 default:
1574 if (!AllowUndefs) // TODO: isBuildVectorAllOnes assumes undef is OK already
1575 return false;
1576 return isBuildVectorAllOnes(MI, MRI);
1577 }
1578}
1579
1581 const MachineRegisterInfo &MRI, Register Reg,
1582 std::function<bool(const Constant *ConstVal)> Match, bool AllowUndefs) {
1583
1584 const MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
1585 if (AllowUndefs && Def->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
1586 return Match(nullptr);
1587
1588 // TODO: Also handle fconstant
1589 if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
1590 return Match(Def->getOperand(1).getCImm());
1591
1592 if (Def->getOpcode() != TargetOpcode::G_BUILD_VECTOR)
1593 return false;
1594
1595 for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
1596 Register SrcElt = Def->getOperand(I).getReg();
1597 const MachineInstr *SrcDef = getDefIgnoringCopies(SrcElt, MRI);
1598 if (AllowUndefs && SrcDef->getOpcode() == TargetOpcode::G_IMPLICIT_DEF) {
1599 if (!Match(nullptr))
1600 return false;
1601 continue;
1602 }
1603
1604 if (SrcDef->getOpcode() != TargetOpcode::G_CONSTANT ||
1605 !Match(SrcDef->getOperand(1).getCImm()))
1606 return false;
1607 }
1608
1609 return true;
1610}
1611
1612bool llvm::isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
1613 bool IsFP) {
1614 switch (TLI.getBooleanContents(IsVector, IsFP)) {
1616 return Val & 0x1;
1618 return Val == 1;
1620 return Val == -1;
1621 }
1622 llvm_unreachable("Invalid boolean contents");
1623}
1624
1625bool llvm::isConstFalseVal(const TargetLowering &TLI, int64_t Val,
1626 bool IsVector, bool IsFP) {
1627 switch (TLI.getBooleanContents(IsVector, IsFP)) {
1629 return ~Val & 0x1;
1632 return Val == 0;
1633 }
1634 llvm_unreachable("Invalid boolean contents");
1635}
1636
1637int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
1638 bool IsFP) {
1639 switch (TLI.getBooleanContents(IsVector, IsFP)) {
1642 return 1;
1644 return -1;
1645 }
1646 llvm_unreachable("Invalid boolean contents");
1647}
1648
1650 LostDebugLocObserver *LocObserver,
1651 SmallInstListTy &DeadInstChain) {
1652 for (MachineOperand &Op : MI.uses()) {
1653 if (Op.isReg() && Op.getReg().isVirtual())
1654 DeadInstChain.insert(MRI.getVRegDef(Op.getReg()));
1655 }
1656 LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
1657 DeadInstChain.remove(&MI);
1658 MI.eraseFromParent();
1659 if (LocObserver)
1660 LocObserver->checkpoint(false);
1661}
1662
1665 LostDebugLocObserver *LocObserver) {
1666 SmallInstListTy DeadInstChain;
1667 for (MachineInstr *MI : DeadInstrs)
1668 saveUsesAndErase(*MI, MRI, LocObserver, DeadInstChain);
1669
1670 while (!DeadInstChain.empty()) {
1671 MachineInstr *Inst = DeadInstChain.pop_back_val();
1672 if (!isTriviallyDead(*Inst, MRI))
1673 continue;
1674 saveUsesAndErase(*Inst, MRI, LocObserver, DeadInstChain);
1675 }
1676}
1677
1679 LostDebugLocObserver *LocObserver) {
1680 return eraseInstrs({&MI}, MRI, LocObserver);
1681}
1682
1684 for (auto &Def : MI.defs()) {
1685 assert(Def.isReg() && "Must be a reg");
1686
1688 for (auto &MOUse : MRI.use_operands(Def.getReg())) {
1689 MachineInstr *DbgValue = MOUse.getParent();
1690 // Ignore partially formed DBG_VALUEs.
1691 if (DbgValue->isNonListDebugValue() && DbgValue->getNumOperands() == 4) {
1692 DbgUsers.push_back(&MOUse);
1693 }
1694 }
1695
1696 if (!DbgUsers.empty()) {
1697 salvageDebugInfoForDbgValue(MRI, MI, DbgUsers);
1698 }
1699 }
1700}
1701
1703 switch (Opc) {
1704 case TargetOpcode::G_FABS:
1705 case TargetOpcode::G_FADD:
1706 case TargetOpcode::G_FCANONICALIZE:
1707 case TargetOpcode::G_FCEIL:
1708 case TargetOpcode::G_FCONSTANT:
1709 case TargetOpcode::G_FCOPYSIGN:
1710 case TargetOpcode::G_FCOS:
1711 case TargetOpcode::G_FDIV:
1712 case TargetOpcode::G_FEXP2:
1713 case TargetOpcode::G_FEXP:
1714 case TargetOpcode::G_FFLOOR:
1715 case TargetOpcode::G_FLOG10:
1716 case TargetOpcode::G_FLOG2:
1717 case TargetOpcode::G_FLOG:
1718 case TargetOpcode::G_FMA:
1719 case TargetOpcode::G_FMAD:
1720 case TargetOpcode::G_FMAXIMUM:
1721 case TargetOpcode::G_FMAXIMUMNUM:
1722 case TargetOpcode::G_FMAXNUM:
1723 case TargetOpcode::G_FMAXNUM_IEEE:
1724 case TargetOpcode::G_FMINIMUM:
1725 case TargetOpcode::G_FMINIMUMNUM:
1726 case TargetOpcode::G_FMINNUM:
1727 case TargetOpcode::G_FMINNUM_IEEE:
1728 case TargetOpcode::G_FMUL:
1729 case TargetOpcode::G_FNEARBYINT:
1730 case TargetOpcode::G_FNEG:
1731 case TargetOpcode::G_FPEXT:
1732 case TargetOpcode::G_FPEXTLOAD:
1733 case TargetOpcode::G_FPOW:
1734 case TargetOpcode::G_FPTRUNC:
1735 case TargetOpcode::G_FPTRUNCSTORE:
1736 case TargetOpcode::G_FREM:
1737 case TargetOpcode::G_FRINT:
1738 case TargetOpcode::G_FSIN:
1739 case TargetOpcode::G_FTAN:
1740 case TargetOpcode::G_FACOS:
1741 case TargetOpcode::G_FASIN:
1742 case TargetOpcode::G_FATAN:
1743 case TargetOpcode::G_FATAN2:
1744 case TargetOpcode::G_FCOSH:
1745 case TargetOpcode::G_FSINH:
1746 case TargetOpcode::G_FTANH:
1747 case TargetOpcode::G_FSQRT:
1748 case TargetOpcode::G_FSUB:
1749 case TargetOpcode::G_INTRINSIC_ROUND:
1750 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1751 case TargetOpcode::G_INTRINSIC_TRUNC:
1752 return true;
1753 default:
1754 return false;
1755 }
1756}
1757
1758/// Shifts return poison if shiftwidth is larger than the bitwidth.
1759static bool shiftAmountKnownInRange(Register ShiftAmount,
1760 const MachineRegisterInfo &MRI) {
1761 LLT Ty = MRI.getType(ShiftAmount);
1762
1763 if (Ty.isScalableVector())
1764 return false; // Can't tell, just return false to be safe
1765
1766 if (Ty.isScalar()) {
1767 std::optional<ValueAndVReg> Val =
1768 getIConstantVRegValWithLookThrough(ShiftAmount, MRI);
1769 if (!Val)
1770 return false;
1771 return Val->Value.ult(Ty.getScalarSizeInBits());
1772 }
1773
1774 GBuildVector *BV = getOpcodeDef<GBuildVector>(ShiftAmount, MRI);
1775 if (!BV)
1776 return false;
1777
1778 unsigned Sources = BV->getNumSources();
1779 for (unsigned I = 0; I < Sources; ++I) {
1780 std::optional<ValueAndVReg> Val =
1782 if (!Val)
1783 return false;
1784 if (!Val->Value.ult(Ty.getScalarSizeInBits()))
1785 return false;
1786 }
1787
1788 return true;
1789}
1790
1792 bool ConsiderFlagsAndMetadata,
1793 UndefPoisonKind Kind) {
1794 MachineInstr *RegDef = MRI.getVRegDef(Reg);
1795
1796 if (ConsiderFlagsAndMetadata && includesPoison(Kind))
1797 if (auto *GMI = dyn_cast<GenericMachineInstr>(RegDef))
1798 if (GMI->hasPoisonGeneratingFlags())
1799 return true;
1800
1801 // Check whether opcode is a poison/undef-generating operation.
1802 switch (RegDef->getOpcode()) {
1803 case TargetOpcode::G_BUILD_VECTOR:
1804 case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
1805 return false;
1806 case TargetOpcode::G_SHL:
1807 case TargetOpcode::G_ASHR:
1808 case TargetOpcode::G_LSHR:
1809 return includesPoison(Kind) &&
1810 !shiftAmountKnownInRange(RegDef->getOperand(2).getReg(), MRI);
1811 case TargetOpcode::G_FPTOSI:
1812 case TargetOpcode::G_FPTOUI:
1813 // fptosi/ui yields poison if the resulting value does not fit in the
1814 // destination type.
1815 return true;
1816 case TargetOpcode::G_CTLZ:
1817 case TargetOpcode::G_CTTZ:
1818 case TargetOpcode::G_CTLS:
1819 case TargetOpcode::G_ABS:
1820 case TargetOpcode::G_CTPOP:
1821 case TargetOpcode::G_BSWAP:
1822 case TargetOpcode::G_BITREVERSE:
1823 case TargetOpcode::G_FSHL:
1824 case TargetOpcode::G_FSHR:
1825 case TargetOpcode::G_SMAX:
1826 case TargetOpcode::G_SMIN:
1827 case TargetOpcode::G_SCMP:
1828 case TargetOpcode::G_UMAX:
1829 case TargetOpcode::G_UMIN:
1830 case TargetOpcode::G_UCMP:
1831 case TargetOpcode::G_PTRMASK:
1832 case TargetOpcode::G_SADDO:
1833 case TargetOpcode::G_SSUBO:
1834 case TargetOpcode::G_UADDO:
1835 case TargetOpcode::G_USUBO:
1836 case TargetOpcode::G_SMULO:
1837 case TargetOpcode::G_UMULO:
1838 case TargetOpcode::G_SADDSAT:
1839 case TargetOpcode::G_UADDSAT:
1840 case TargetOpcode::G_SSUBSAT:
1841 case TargetOpcode::G_USUBSAT:
1842 case TargetOpcode::G_SBFX:
1843 case TargetOpcode::G_UBFX:
1844 return false;
1845 case TargetOpcode::G_SSHLSAT:
1846 case TargetOpcode::G_USHLSAT:
1847 return includesPoison(Kind) &&
1848 !shiftAmountKnownInRange(RegDef->getOperand(2).getReg(), MRI);
1849 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1851 if (includesPoison(Kind)) {
1852 std::optional<ValueAndVReg> Index =
1853 getIConstantVRegValWithLookThrough(Insert->getIndexReg(), MRI);
1854 if (!Index)
1855 return true;
1856 LLT VecTy = MRI.getType(Insert->getVectorReg());
1857 return Index->Value.uge(VecTy.getElementCount().getKnownMinValue());
1858 }
1859 return false;
1860 }
1861 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1863 if (includesPoison(Kind)) {
1864 std::optional<ValueAndVReg> Index =
1866 if (!Index)
1867 return true;
1868 LLT VecTy = MRI.getType(Extract->getVectorReg());
1869 return Index->Value.uge(VecTy.getElementCount().getKnownMinValue());
1870 }
1871 return false;
1872 }
1873 case TargetOpcode::G_SHUFFLE_VECTOR: {
1874 GShuffleVector *Shuffle = cast<GShuffleVector>(RegDef);
1875 ArrayRef<int> Mask = Shuffle->getMask();
1876 return includesPoison(Kind) && is_contained(Mask, -1);
1877 }
1878 case TargetOpcode::G_FNEG:
1879 case TargetOpcode::G_PHI:
1880 case TargetOpcode::G_SELECT:
1881 case TargetOpcode::G_UREM:
1882 case TargetOpcode::G_SREM:
1883 case TargetOpcode::G_FREEZE:
1884 case TargetOpcode::G_ICMP:
1885 case TargetOpcode::G_FCMP:
1886 case TargetOpcode::G_FADD:
1887 case TargetOpcode::G_FSUB:
1888 case TargetOpcode::G_FMUL:
1889 case TargetOpcode::G_FDIV:
1890 case TargetOpcode::G_FREM:
1891 case TargetOpcode::G_PTR_ADD:
1892 return false;
1893 default:
1894 return !isa<GCastOp>(RegDef) && !isa<GBinOp>(RegDef);
1895 }
1896}
1897
1899 const MachineRegisterInfo &MRI,
1900 unsigned Depth,
1901 UndefPoisonKind Kind) {
1903 return false;
1904
1905 MachineInstr *RegDef = MRI.getVRegDef(Reg);
1906
1907 switch (RegDef->getOpcode()) {
1908 case TargetOpcode::G_FREEZE:
1909 return true;
1910 case TargetOpcode::G_IMPLICIT_DEF:
1911 return !includesUndef(Kind);
1912 case TargetOpcode::G_CONSTANT:
1913 case TargetOpcode::G_FCONSTANT:
1914 return true;
1915 case TargetOpcode::G_BUILD_VECTOR: {
1916 GBuildVector *BV = cast<GBuildVector>(RegDef);
1917 unsigned NumSources = BV->getNumSources();
1918 for (unsigned I = 0; I < NumSources; ++I)
1920 Depth + 1, Kind))
1921 return false;
1922 return true;
1923 }
1924 case TargetOpcode::G_PHI: {
1925 GPhi *Phi = cast<GPhi>(RegDef);
1926 unsigned NumIncoming = Phi->getNumIncomingValues();
1927 for (unsigned I = 0; I < NumIncoming; ++I)
1928 if (!::isGuaranteedNotToBeUndefOrPoison(Phi->getIncomingValue(I), MRI,
1929 Depth + 1, Kind))
1930 return false;
1931 return true;
1932 }
1933 default: {
1934 auto MOCheck = [&](const MachineOperand &MO) {
1935 if (!MO.isReg())
1936 return true;
1937 return ::isGuaranteedNotToBeUndefOrPoison(MO.getReg(), MRI, Depth + 1,
1938 Kind);
1939 };
1940 return !::canCreateUndefOrPoison(Reg, MRI,
1941 /*ConsiderFlagsAndMetadata=*/true, Kind) &&
1942 all_of(RegDef->uses(), MOCheck);
1943 }
1944 }
1945}
1946
1948 bool ConsiderFlagsAndMetadata) {
1949 return ::canCreateUndefOrPoison(Reg, MRI, ConsiderFlagsAndMetadata,
1951}
1952
1954 bool ConsiderFlagsAndMetadata = true) {
1955 return ::canCreateUndefOrPoison(Reg, MRI, ConsiderFlagsAndMetadata,
1957}
1958
1960 const MachineRegisterInfo &MRI,
1961 unsigned Depth) {
1962 return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1964}
1965
1967 const MachineRegisterInfo &MRI,
1968 unsigned Depth) {
1969 return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1971}
1972
1974 const MachineRegisterInfo &MRI,
1975 unsigned Depth) {
1976 return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1978}
1979
1981 if (Ty.isVector())
1982 return VectorType::get(IntegerType::get(C, Ty.getScalarSizeInBits()),
1983 Ty.getElementCount());
1984 return IntegerType::get(C, Ty.getSizeInBits());
1985}
1986
1988 switch (MI.getOpcode()) {
1989 default:
1990 return false;
1991 case TargetOpcode::G_ASSERT_ALIGN:
1992 case TargetOpcode::G_ASSERT_SEXT:
1993 case TargetOpcode::G_ASSERT_ZEXT:
1994 return true;
1995 }
1996}
1997
1999 assert(Kind == GIConstantKind::Scalar && "Expected scalar constant");
2000
2001 return Value;
2002}
2003
2004std::optional<GIConstant>
2007
2009 std::optional<ValueAndVReg> MayBeConstant =
2010 getIConstantVRegValWithLookThrough(Splat->getScalarReg(), MRI);
2011 if (!MayBeConstant)
2012 return std::nullopt;
2013 return GIConstant(MayBeConstant->Value, GIConstantKind::ScalableVector);
2014 }
2015
2017 SmallVector<APInt> Values;
2018 unsigned NumSources = Build->getNumSources();
2019 for (unsigned I = 0; I < NumSources; ++I) {
2020 Register SrcReg = Build->getSourceReg(I);
2021 std::optional<ValueAndVReg> MayBeConstant =
2023 if (!MayBeConstant)
2024 return std::nullopt;
2025 Values.push_back(MayBeConstant->Value);
2026 }
2027 return GIConstant(Values);
2028 }
2029
2030 std::optional<ValueAndVReg> MayBeConstant =
2032 if (!MayBeConstant)
2033 return std::nullopt;
2034
2035 return GIConstant(MayBeConstant->Value, GIConstantKind::Scalar);
2036}
2037
2039 assert(Kind == GFConstantKind::Scalar && "Expected scalar constant");
2040
2041 return Values[0];
2042}
2043
2044std::optional<GFConstant>
2047
2049 std::optional<FPValueAndVReg> MayBeConstant =
2050 getFConstantVRegValWithLookThrough(Splat->getScalarReg(), MRI);
2051 if (!MayBeConstant)
2052 return std::nullopt;
2053 return GFConstant(MayBeConstant->Value, GFConstantKind::ScalableVector);
2054 }
2055
2057 SmallVector<APFloat> Values;
2058 unsigned NumSources = Build->getNumSources();
2059 for (unsigned I = 0; I < NumSources; ++I) {
2060 Register SrcReg = Build->getSourceReg(I);
2061 std::optional<FPValueAndVReg> MayBeConstant =
2063 if (!MayBeConstant)
2064 return std::nullopt;
2065 Values.push_back(MayBeConstant->Value);
2066 }
2067 return GFConstant(Values);
2068 }
2069
2070 std::optional<FPValueAndVReg> MayBeConstant =
2072 if (!MayBeConstant)
2073 return std::nullopt;
2074
2075 return GFConstant(MayBeConstant->Value, GFConstantKind::Scalar);
2076}
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void reportGISelDiagnostic(DiagnosticSeverity Severity, MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Definition Utils.cpp:234
static bool shiftAmountKnownInRange(Register ShiftAmount, const MachineRegisterInfo &MRI)
Shifts return poison if shiftwidth is larger than the bitwidth.
Definition Utils.cpp:1759
static bool isBuildVectorOp(unsigned Opcode)
Definition Utils.cpp:1310
static bool isConstantScalar(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowFP=true, bool AllowOpaqueConstants=true)
Definition Utils.cpp:1466
static GBuildVector * getBuildVectorLikeDef(Register Reg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:798
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This contains common code to allow clients to notify changes to machine instr.
Provides analysis for querying information about KnownBits during GISel passes.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Tracks DebugLocs between checkpoints and verifies that they are transferred.
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineIRBuilder class.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#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.
This file contains the UndefPoisonKind enum and helper functions.
static const char PassName[]
Class recording the (high level) value of a variable.
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1267
void copySign(const APFloat &RHS)
Definition APFloat.h:1361
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5912
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1249
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1240
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1406
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1258
APInt bitcastToAPInt() const
Definition APFloat.h:1430
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1285
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1616
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1055
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1076
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:968
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1709
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1511
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1687
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1084
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1788
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1028
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const APFloat & getValueAPF() const
Definition Constants.h:463
bool isNegative() const
Return true if the sign bit is set.
Definition Constants.h:473
bool isZero() const
Return true if the value is positive or negative zero.
Definition Constants.h:467
This is the shared class of boolean and integer constants.
Definition Constants.h:87
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:43
A debug info location.
Definition DebugLoc.h:123
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
Represents a G_BUILD_VECTOR.
Represents an extract vector element.
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2045
GFConstant(ArrayRef< APFloat > Values)
Definition Utils.h:688
LLVM_ABI APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2038
LLVM_ABI APInt getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:1998
static LLVM_ABI std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2005
GIConstant(ArrayRef< APInt > Values)
Definition Utils.h:647
Abstract class that contains various methods for clients to notify about changes.
KnownBits getKnownBits(Register R)
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn't already in it.
MachineInstr * pop_back_val()
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
Represents an insert vector element.
Register getSourceReg(unsigned I) const
Returns the I'th source register.
unsigned getNumSources() const
Returns the number of source registers.
Represents a G_PHI.
Represents a G_SHUFFLE_VECTOR.
ArrayRef< int > getMask() const
Represents a splat vector.
Module * getParent()
Get the module that this global value is contained inside of...
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:354
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
LLT getScalarType() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr bool isScalable() const
Returns true if the LLT is a scalable vector.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr ElementCount getElementCount() 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 LLT integer(unsigned SizeInBits)
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
static constexpr LLT scalarOrVector(ElementCount EC, LLT ScalarTy)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
void checkpoint(bool CheckDebugLocs=true)
Call this to indicate that it's a good point to assess whether locations have been lost.
Describe properties that are true of each instruction in the target description file.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
GISelChangeObserver * getObserver() const
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Helper class to build MachineInstr.
MachineInstrBuilder buildUnmerge(ArrayRef< LLT > Res, const SrcOp &Op)
Build and insert Res0, ... = G_UNMERGE_VALUES Op.
MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index)
Build and insert Res0, ... = G_EXTRACT Src, Idx0.
MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_MERGE_VALUES Op0, ... or Res = G_BUILD_VECTOR Op0, ... or Res = G_CONCAT_VEC...
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
mop_range uses()
Returns all operands which may be register uses.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
const ConstantInt * getCImm() const
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
Diagnostic information for missed-optimization remarks.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
const RegClassOrRegBank & getRegClassOrRegBank(Register Reg) const
Return the register bank or register class of Reg.
def_iterator def_begin(Register RegNo) const
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
LLVM_ABI Register getLiveInVirtReg(MCRegister PReg) const
getLiveInVirtReg - If PReg is a live-in physical register, return the corresponding live-in virtual r...
const TargetRegisterClass * getRegClassOrNull(Register Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet.
static def_iterator def_end()
iterator_range< use_iterator > use_operands(Register Reg) const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Represents a value which can be a Register or a constant.
Definition Utils.h:392
Holds all the information related to register banks.
static const TargetRegisterClass * constrainGenericRegister(Register Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
TargetOptions Options
GlobalISelAbortMode GlobalISelAbort
EnableGlobalISelAbort - Control abort behaviour when global instruction selection fails to lower/sele...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr LeafTy multiplyCoefficientBy(ScalarTy RHS) const
Definition TypeSize.h:256
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2277
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2282
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition APInt.h:2287
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2292
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
DiagnosticInfoMIROptimization::MachineArgument MNV
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Register getFunctionLiveInPhysReg(MachineFunction &MF, const TargetInstrInfo &TII, MCRegister PhysReg, const TargetRegisterClass &RC, const DebugLoc &DL, LLT RegTy=LLT())
Return a virtual register corresponding to the incoming argument register PhysReg.
Definition Utils.cpp:858
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
LLVM_ABI std::optional< SmallVector< APInt > > ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2, unsigned DstScalarSizeInBits, unsigned ExtOp, const MachineRegisterInfo &MRI)
Definition Utils.cpp:993
@ Offset
Definition DWP.cpp:557
LLVM_ABI bool isBuildVectorAllZeros(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndef=false)
Return true if the specified instruction is a G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all of the...
Definition Utils.cpp:1440
LLVM_ABI Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
Definition Utils.cpp:1980
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
LLVM_ABI Register constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const TargetRegisterClass &RegClass, MachineOperand &RegMO)
Constrain the Register operand OpIdx, so that it is now constrained to the TargetRegisterClass passed...
Definition Utils.cpp:57
LLVM_ABI MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
Definition Utils.cpp:653
LLVM_ABI const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:461
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:294
LLVM_ABI std::optional< APFloat > ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Definition Utils.cpp:932
LLVM_ABI std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1400
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1565
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
LLVM_ABI std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition Utils.cpp:741
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition Utils.cpp:1683
LLVM_ABI void constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Definition Utils.cpp:156
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI std::optional< APInt > ConstantFoldExtOp(unsigned Opcode, const Register Op1, uint64_t Imm, const MachineRegisterInfo &MRI)
Definition Utils.cpp:891
LLVM_ABI std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1453
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1740
GISelWorkList< 4 > SmallInstListTy
Definition Utils.h:567
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp:1523
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1547
LLVM_ABI MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
Definition Utils.cpp:494
LLVM_ABI bool matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg, std::function< bool(const Constant *ConstVal)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant G_B...
Definition Utils.cpp:1580
bool isPreISelGenericOptimizationHint(unsigned Opcode)
LLVM_ABI void reportGISelWarning(MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel warning as a missed optimization remark to the LLVMContext's diagnostic stream.
Definition Utils.cpp:252
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI bool isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Returns true if given the TargetLowering's boolean contents information, the value Val contains a tru...
Definition Utils.cpp:1612
LLVM_ABI LLVM_READNONE LLT getLCMType(LLT OrigTy, LLT TargetTy)
Return the least common multiple type of OrigTy and TargetTy, by changing the number of vector elemen...
Definition Utils.cpp:1147
LLVM_ABI std::optional< int64_t > getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
Definition Utils.cpp:314
LLVM_ABI std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition Utils.cpp:672
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1745
LLVM_ABI const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:305
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1695
LLVM_ABI bool isConstantOrConstantVector(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowFP=true, bool AllowOpaqueConstants=true)
Return true if the specified instruction is known to be a constant, or a vector of constants.
Definition Utils.cpp:1503
constexpr unsigned MaxAnalysisRecursionDepth
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
Definition Utils.cpp:200
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1726
LLVM_ABI void saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver, SmallInstListTy &DeadInstChain)
Definition Utils.cpp:1649
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void reportGISelFailure(MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream.
Definition Utils.cpp:258
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
LLVM_ABI std::optional< ValueAndVReg > getAnyConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true, bool LookThroughAnyExt=false)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT or G_FCONST...
Definition Utils.cpp:439
LLVM_ABI bool isBuildVectorAllOnes(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndef=false)
Return true if the specified instruction is a G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all of the...
Definition Utils.cpp:1446
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI SmallVector< APInt > ConstantFoldVectorBinop(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Tries to constant fold a vector binop with sources Op1 and Op2.
Definition Utils.cpp:819
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
LLVM_ABI std::optional< FPValueAndVReg > getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI, bool AllowUndef=true)
Returns a floating point scalar constant of a build vector splat if it exists.
Definition Utils.cpp:1433
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
LLVM_ABI std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
Definition Utils.cpp:908
LLVM_ABI void extractParts(Register Reg, LLT Ty, int NumParts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Helper function to split a wide generic register into bitwise blocks with the given Type (which impli...
Definition Utils.cpp:508
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1143
LLVM_ABI LLVM_READNONE LLT getCoverTy(LLT OrigTy, LLT TargetTy)
Return smallest type that covers both OrigTy and TargetTy and is multiple of TargetTy.
Definition Utils.cpp:1214
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1676
LLVM_ABI unsigned getInverseGMinMaxOpcode(unsigned MinMaxOpc)
Returns the inverse opcode of MinMaxOpc, which is a generic min/max opcode like G_SMIN.
Definition Utils.cpp:279
@ Mul
Product of integers.
bool isTargetSpecificOpcode(unsigned Opcode)
Check whether the given Opcode is a target-specific opcode.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
LLVM_ABI std::optional< FPValueAndVReg > getFConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_FCONSTANT returns it...
Definition Utils.cpp:447
LLVM_ABI bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Definition Utils.cpp:1625
LLVM_ABI std::optional< APFloat > isConstantOrConstantSplatVectorFP(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a float constant integer or a splat vector of float constant integers.
Definition Utils.cpp:1536
constexpr unsigned BitWidth
LLVM_ABI APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
Definition Utils.cpp:659
LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg, const MachineRegisterInfo &MRI, int64_t SplatValue, bool AllowUndef)
Return true if the specified register is defined by G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all ...
Definition Utils.cpp:1359
LLVM_ABI void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition Utils.cpp:1678
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
LLVM_ABI Register constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, Register Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
Definition Utils.cpp:47
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
Definition Utils.cpp:1637
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< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:433
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1771
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
LLVM_ABI bool isPreISelGenericFloatingPointOpcode(unsigned Opc)
Returns whether opcode Opc is a pre-isel generic floating-point opcode, having only floating-point op...
Definition Utils.cpp:1702
LLVM_ABI std::optional< DefinitionAndSourceRegister > getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, and underlying value Register folding away any copies.
Definition Utils.cpp:469
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI SmallVector< APInt > ConstantFoldUnaryIntOp(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Tries to constant fold a unary integer operation (G_CTLZ, G_CTTZ, G_CTPOP and their _ZERO_POISON vari...
Definition Utils.cpp:945
LLVM_ABI void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition Utils.cpp:1663
void salvageDebugInfoForDbgValue(const MachineRegisterInfo &MRI, MachineInstr &MI, ArrayRef< MachineOperand * > DbgUsers)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI Register getSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the source register for Reg, folding away any trivial copies.
Definition Utils.cpp:501
LLVM_ABI LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy)
Return a type where the total size is the greatest common divisor of OrigTy and TargetTy.
Definition Utils.cpp:1235
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1713
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1753
LLVM_ABI std::optional< int64_t > getIConstantSplatSExtVal(const Register Reg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1418
LLVM_ABI bool isAssertMI(const MachineInstr &MI)
Returns true if the instruction MI is one of the assert instructions.
Definition Utils.cpp:1987
LLVM_ABI void extractVectorParts(Register Reg, unsigned NumElts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Version which handles irregular sub-vector splits.
Definition Utils.cpp:611
LLVM_ABI int getSplatIndex(ArrayRef< int > Mask)
If all non-negative Mask elements are the same value, return that value.
LLVM_ABI bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn't have oth...
Definition Utils.cpp:221
LLVM_ABI Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition Utils.cpp:841
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
#define MORE()
Definition regcomp.c:246
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Simple struct used to hold a Register value and the instruction which defines it.
Definition Utils.h:229
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition KnownBits.h:300
This class contains a discriminated union of information about pointers in memory operands,...
int64_t Offset
Offset - This is an offset from the base Value*.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
Simple struct used to hold a constant integer value and a virtual register.
Definition Utils.h:188