xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp (revision f0bae562dc3b30e55e5f92e698adf291e024d9a5)
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 
12 #include "llvm/CodeGen/GlobalISel/Utils.h"
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/CodeGen/CodeGenCommonISel.h"
17 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
18 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
19 #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
20 #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h"
21 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
22 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/MachineSizeOpts.h"
28 #include "llvm/CodeGen/RegisterBankInfo.h"
29 #include "llvm/CodeGen/StackProtector.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/CodeGen/TargetLowering.h"
32 #include "llvm/CodeGen/TargetOpcodes.h"
33 #include "llvm/CodeGen/TargetPassConfig.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Transforms/Utils/SizeOpts.h"
38 #include <numeric>
39 #include <optional>
40 
41 #define DEBUG_TYPE "globalisel-utils"
42 
43 using namespace llvm;
44 using namespace MIPatternMatch;
45 
46 Register llvm::constrainRegToClass(MachineRegisterInfo &MRI,
47                                    const TargetInstrInfo &TII,
48                                    const RegisterBankInfo &RBI, Register Reg,
49                                    const TargetRegisterClass &RegClass) {
50   if (!RBI.constrainGenericRegister(Reg, RegClass, MRI))
51     return MRI.createVirtualRegister(&RegClass);
52 
53   return Reg;
54 }
55 
56 Register llvm::constrainOperandRegClass(
57     const MachineFunction &MF, const TargetRegisterInfo &TRI,
58     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
59     const RegisterBankInfo &RBI, MachineInstr &InsertPt,
60     const TargetRegisterClass &RegClass, MachineOperand &RegMO) {
61   Register Reg = RegMO.getReg();
62   // Assume physical registers are properly constrained.
63   assert(Reg.isVirtual() && "PhysReg not implemented");
64 
65   // Save the old register class to check whether
66   // the change notifications will be required.
67   // TODO: A better approach would be to pass
68   // the observers to constrainRegToClass().
69   auto *OldRegClass = MRI.getRegClassOrNull(Reg);
70   Register ConstrainedReg = constrainRegToClass(MRI, TII, RBI, Reg, RegClass);
71   // If we created a new virtual register because the class is not compatible
72   // then create a copy between the new and the old register.
73   if (ConstrainedReg != Reg) {
74     MachineBasicBlock::iterator InsertIt(&InsertPt);
75     MachineBasicBlock &MBB = *InsertPt.getParent();
76     // FIXME: The copy needs to have the classes constrained for its operands.
77     // Use operand's regbank to get the class for old register (Reg).
78     if (RegMO.isUse()) {
79       BuildMI(MBB, InsertIt, InsertPt.getDebugLoc(),
80               TII.get(TargetOpcode::COPY), ConstrainedReg)
81           .addReg(Reg);
82     } else {
83       assert(RegMO.isDef() && "Must be a definition");
84       BuildMI(MBB, std::next(InsertIt), InsertPt.getDebugLoc(),
85               TII.get(TargetOpcode::COPY), Reg)
86           .addReg(ConstrainedReg);
87     }
88     if (GISelChangeObserver *Observer = MF.getObserver()) {
89       Observer->changingInstr(*RegMO.getParent());
90     }
91     RegMO.setReg(ConstrainedReg);
92     if (GISelChangeObserver *Observer = MF.getObserver()) {
93       Observer->changedInstr(*RegMO.getParent());
94     }
95   } else if (OldRegClass != MRI.getRegClassOrNull(Reg)) {
96     if (GISelChangeObserver *Observer = MF.getObserver()) {
97       if (!RegMO.isDef()) {
98         MachineInstr *RegDef = MRI.getVRegDef(Reg);
99         Observer->changedInstr(*RegDef);
100       }
101       Observer->changingAllUsesOfReg(MRI, Reg);
102       Observer->finishedChangingAllUsesOfReg();
103     }
104   }
105   return ConstrainedReg;
106 }
107 
108 Register llvm::constrainOperandRegClass(
109     const MachineFunction &MF, const TargetRegisterInfo &TRI,
110     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
111     const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
112     MachineOperand &RegMO, unsigned OpIdx) {
113   Register Reg = RegMO.getReg();
114   // Assume physical registers are properly constrained.
115   assert(Reg.isVirtual() && "PhysReg not implemented");
116 
117   const TargetRegisterClass *OpRC = TII.getRegClass(II, OpIdx, &TRI, MF);
118   // Some of the target independent instructions, like COPY, may not impose any
119   // register class constraints on some of their operands: If it's a use, we can
120   // skip constraining as the instruction defining the register would constrain
121   // it.
122 
123   if (OpRC) {
124     // Obtain the RC from incoming regbank if it is a proper sub-class. Operands
125     // can have multiple regbanks for a superclass that combine different
126     // register types (E.g., AMDGPU's VGPR and AGPR). The regbank ambiguity
127     // resolved by targets during regbankselect should not be overridden.
128     if (const auto *SubRC = TRI.getCommonSubClass(
129             OpRC, TRI.getConstrainedRegClassForOperand(RegMO, MRI)))
130       OpRC = SubRC;
131 
132     OpRC = TRI.getAllocatableClass(OpRC);
133   }
134 
135   if (!OpRC) {
136     assert((!isTargetSpecificOpcode(II.getOpcode()) || RegMO.isUse()) &&
137            "Register class constraint is required unless either the "
138            "instruction is target independent or the operand is a use");
139     // FIXME: Just bailing out like this here could be not enough, unless we
140     // expect the users of this function to do the right thing for PHIs and
141     // COPY:
142     //   v1 = COPY v0
143     //   v2 = COPY v1
144     // v1 here may end up not being constrained at all. Please notice that to
145     // reproduce the issue we likely need a destination pattern of a selection
146     // rule producing such extra copies, not just an input GMIR with them as
147     // every existing target using selectImpl handles copies before calling it
148     // and they never reach this function.
149     return Reg;
150   }
151   return constrainOperandRegClass(MF, TRI, MRI, TII, RBI, InsertPt, *OpRC,
152                                   RegMO);
153 }
154 
155 bool llvm::constrainSelectedInstRegOperands(MachineInstr &I,
156                                             const TargetInstrInfo &TII,
157                                             const TargetRegisterInfo &TRI,
158                                             const RegisterBankInfo &RBI) {
159   assert(!isPreISelGenericOpcode(I.getOpcode()) &&
160          "A selected instruction is expected");
161   MachineBasicBlock &MBB = *I.getParent();
162   MachineFunction &MF = *MBB.getParent();
163   MachineRegisterInfo &MRI = MF.getRegInfo();
164 
165   for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
166     MachineOperand &MO = I.getOperand(OpI);
167 
168     // There's nothing to be done on non-register operands.
169     if (!MO.isReg())
170       continue;
171 
172     LLVM_DEBUG(dbgs() << "Converting operand: " << MO << '\n');
173     assert(MO.isReg() && "Unsupported non-reg operand");
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   return true;
199 }
200 
201 bool llvm::canReplaceReg(Register DstReg, Register SrcReg,
202                          MachineRegisterInfo &MRI) {
203   // Give up if either DstReg or SrcReg  is a physical register.
204   if (DstReg.isPhysical() || SrcReg.isPhysical())
205     return false;
206   // Give up if the types don't match.
207   if (MRI.getType(DstReg) != MRI.getType(SrcReg))
208     return false;
209   // Replace if either DstReg has no constraints or the register
210   // constraints match.
211   const auto &DstRBC = MRI.getRegClassOrRegBank(DstReg);
212   if (!DstRBC || DstRBC == MRI.getRegClassOrRegBank(SrcReg))
213     return true;
214 
215   // Otherwise match if the Src is already a regclass that is covered by the Dst
216   // RegBank.
217   return DstRBC.is<const RegisterBank *>() && MRI.getRegClassOrNull(SrcReg) &&
218          DstRBC.get<const RegisterBank *>()->covers(
219              *MRI.getRegClassOrNull(SrcReg));
220 }
221 
222 bool llvm::isTriviallyDead(const MachineInstr &MI,
223                            const MachineRegisterInfo &MRI) {
224   // Instructions without side-effects are dead iff they only define dead regs.
225   // This function is hot and this loop returns early in the common case,
226   // so only perform additional checks before this if absolutely necessary.
227   for (const auto &MO : MI.all_defs()) {
228     Register Reg = MO.getReg();
229     if (Reg.isPhysical() || !MRI.use_nodbg_empty(Reg))
230       return false;
231   }
232   return MI.wouldBeTriviallyDead();
233 }
234 
235 static void reportGISelDiagnostic(DiagnosticSeverity Severity,
236                                   MachineFunction &MF,
237                                   const TargetPassConfig &TPC,
238                                   MachineOptimizationRemarkEmitter &MORE,
239                                   MachineOptimizationRemarkMissed &R) {
240   bool IsFatal = Severity == DS_Error &&
241                  TPC.isGlobalISelAbortEnabled();
242   // Print the function name explicitly if we don't have a debug location (which
243   // makes the diagnostic less useful) or if we're going to emit a raw error.
244   if (!R.getLocation().isValid() || IsFatal)
245     R << (" (in function: " + MF.getName() + ")").str();
246 
247   if (IsFatal)
248     report_fatal_error(Twine(R.getMsg()));
249   else
250     MORE.emit(R);
251 }
252 
253 void llvm::reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
254                               MachineOptimizationRemarkEmitter &MORE,
255                               MachineOptimizationRemarkMissed &R) {
256   reportGISelDiagnostic(DS_Warning, MF, TPC, MORE, R);
257 }
258 
259 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
260                               MachineOptimizationRemarkEmitter &MORE,
261                               MachineOptimizationRemarkMissed &R) {
262   MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
263   reportGISelDiagnostic(DS_Error, MF, TPC, MORE, R);
264 }
265 
266 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
267                               MachineOptimizationRemarkEmitter &MORE,
268                               const char *PassName, StringRef Msg,
269                               const MachineInstr &MI) {
270   MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
271                                     MI.getDebugLoc(), MI.getParent());
272   R << Msg;
273   // Printing MI is expensive;  only do it if expensive remarks are enabled.
274   if (TPC.isGlobalISelAbortEnabled() || MORE.allowExtraAnalysis(PassName))
275     R << ": " << ore::MNV("Inst", MI);
276   reportGISelFailure(MF, TPC, MORE, R);
277 }
278 
279 std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
280                                                const MachineRegisterInfo &MRI) {
281   std::optional<ValueAndVReg> ValAndVReg = getIConstantVRegValWithLookThrough(
282       VReg, MRI, /*LookThroughInstrs*/ false);
283   assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
284          "Value found while looking through instrs");
285   if (!ValAndVReg)
286     return std::nullopt;
287   return ValAndVReg->Value;
288 }
289 
290 const APInt &llvm::getIConstantFromReg(Register Reg,
291                                        const MachineRegisterInfo &MRI) {
292   MachineInstr *Const = MRI.getVRegDef(Reg);
293   assert((Const && Const->getOpcode() == TargetOpcode::G_CONSTANT) &&
294          "expected a G_CONSTANT on Reg");
295   return Const->getOperand(1).getCImm()->getValue();
296 }
297 
298 std::optional<int64_t>
299 llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI) {
300   std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
301   if (Val && Val->getBitWidth() <= 64)
302     return Val->getSExtValue();
303   return std::nullopt;
304 }
305 
306 namespace {
307 
308 // This function is used in many places, and as such, it has some
309 // micro-optimizations to try and make it as fast as it can be.
310 //
311 // - We use template arguments to avoid an indirect call caused by passing a
312 // function_ref/std::function
313 // - GetAPCstValue does not return std::optional<APInt> as that's expensive.
314 // Instead it returns true/false and places the result in a pre-constructed
315 // APInt.
316 //
317 // Please change this function carefully and benchmark your changes.
318 template <bool (*IsConstantOpcode)(const MachineInstr *),
319           bool (*GetAPCstValue)(const MachineInstr *MI, APInt &)>
320 std::optional<ValueAndVReg>
321 getConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI,
322                                   bool LookThroughInstrs = true,
323                                   bool LookThroughAnyExt = false) {
324   SmallVector<std::pair<unsigned, unsigned>, 4> SeenOpcodes;
325   MachineInstr *MI;
326 
327   while ((MI = MRI.getVRegDef(VReg)) && !IsConstantOpcode(MI) &&
328          LookThroughInstrs) {
329     switch (MI->getOpcode()) {
330     case TargetOpcode::G_ANYEXT:
331       if (!LookThroughAnyExt)
332         return std::nullopt;
333       [[fallthrough]];
334     case TargetOpcode::G_TRUNC:
335     case TargetOpcode::G_SEXT:
336     case TargetOpcode::G_ZEXT:
337       SeenOpcodes.push_back(std::make_pair(
338           MI->getOpcode(),
339           MRI.getType(MI->getOperand(0).getReg()).getSizeInBits()));
340       VReg = MI->getOperand(1).getReg();
341       break;
342     case TargetOpcode::COPY:
343       VReg = MI->getOperand(1).getReg();
344       if (VReg.isPhysical())
345         return std::nullopt;
346       break;
347     case TargetOpcode::G_INTTOPTR:
348       VReg = MI->getOperand(1).getReg();
349       break;
350     default:
351       return std::nullopt;
352     }
353   }
354   if (!MI || !IsConstantOpcode(MI))
355     return std::nullopt;
356 
357   APInt Val;
358   if (!GetAPCstValue(MI, Val))
359     return std::nullopt;
360   for (auto &Pair : reverse(SeenOpcodes)) {
361     switch (Pair.first) {
362     case TargetOpcode::G_TRUNC:
363       Val = Val.trunc(Pair.second);
364       break;
365     case TargetOpcode::G_ANYEXT:
366     case TargetOpcode::G_SEXT:
367       Val = Val.sext(Pair.second);
368       break;
369     case TargetOpcode::G_ZEXT:
370       Val = Val.zext(Pair.second);
371       break;
372     }
373   }
374 
375   return ValueAndVReg{std::move(Val), VReg};
376 }
377 
378 bool isIConstant(const MachineInstr *MI) {
379   if (!MI)
380     return false;
381   return MI->getOpcode() == TargetOpcode::G_CONSTANT;
382 }
383 
384 bool isFConstant(const MachineInstr *MI) {
385   if (!MI)
386     return false;
387   return MI->getOpcode() == TargetOpcode::G_FCONSTANT;
388 }
389 
390 bool isAnyConstant(const MachineInstr *MI) {
391   if (!MI)
392     return false;
393   unsigned Opc = MI->getOpcode();
394   return Opc == TargetOpcode::G_CONSTANT || Opc == TargetOpcode::G_FCONSTANT;
395 }
396 
397 bool getCImmAsAPInt(const MachineInstr *MI, APInt &Result) {
398   const MachineOperand &CstVal = MI->getOperand(1);
399   if (!CstVal.isCImm())
400     return false;
401   Result = CstVal.getCImm()->getValue();
402   return true;
403 }
404 
405 bool getCImmOrFPImmAsAPInt(const MachineInstr *MI, APInt &Result) {
406   const MachineOperand &CstVal = MI->getOperand(1);
407   if (CstVal.isCImm())
408     Result = CstVal.getCImm()->getValue();
409   else if (CstVal.isFPImm())
410     Result = CstVal.getFPImm()->getValueAPF().bitcastToAPInt();
411   else
412     return false;
413   return true;
414 }
415 
416 } // end anonymous namespace
417 
418 std::optional<ValueAndVReg> llvm::getIConstantVRegValWithLookThrough(
419     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
420   return getConstantVRegValWithLookThrough<isIConstant, getCImmAsAPInt>(
421       VReg, MRI, LookThroughInstrs);
422 }
423 
424 std::optional<ValueAndVReg> llvm::getAnyConstantVRegValWithLookThrough(
425     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs,
426     bool LookThroughAnyExt) {
427   return getConstantVRegValWithLookThrough<isAnyConstant,
428                                            getCImmOrFPImmAsAPInt>(
429       VReg, MRI, LookThroughInstrs, LookThroughAnyExt);
430 }
431 
432 std::optional<FPValueAndVReg> llvm::getFConstantVRegValWithLookThrough(
433     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
434   auto Reg =
435       getConstantVRegValWithLookThrough<isFConstant, getCImmOrFPImmAsAPInt>(
436           VReg, MRI, LookThroughInstrs);
437   if (!Reg)
438     return std::nullopt;
439   return FPValueAndVReg{getConstantFPVRegVal(Reg->VReg, MRI)->getValueAPF(),
440                         Reg->VReg};
441 }
442 
443 const ConstantFP *
444 llvm::getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI) {
445   MachineInstr *MI = MRI.getVRegDef(VReg);
446   if (TargetOpcode::G_FCONSTANT != MI->getOpcode())
447     return nullptr;
448   return MI->getOperand(1).getFPImm();
449 }
450 
451 std::optional<DefinitionAndSourceRegister>
452 llvm::getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI) {
453   Register DefSrcReg = Reg;
454   auto *DefMI = MRI.getVRegDef(Reg);
455   auto DstTy = MRI.getType(DefMI->getOperand(0).getReg());
456   if (!DstTy.isValid())
457     return std::nullopt;
458   unsigned Opc = DefMI->getOpcode();
459   while (Opc == TargetOpcode::COPY || isPreISelGenericOptimizationHint(Opc)) {
460     Register SrcReg = DefMI->getOperand(1).getReg();
461     auto SrcTy = MRI.getType(SrcReg);
462     if (!SrcTy.isValid())
463       break;
464     DefMI = MRI.getVRegDef(SrcReg);
465     DefSrcReg = SrcReg;
466     Opc = DefMI->getOpcode();
467   }
468   return DefinitionAndSourceRegister{DefMI, DefSrcReg};
469 }
470 
471 MachineInstr *llvm::getDefIgnoringCopies(Register Reg,
472                                          const MachineRegisterInfo &MRI) {
473   std::optional<DefinitionAndSourceRegister> DefSrcReg =
474       getDefSrcRegIgnoringCopies(Reg, MRI);
475   return DefSrcReg ? DefSrcReg->MI : nullptr;
476 }
477 
478 Register llvm::getSrcRegIgnoringCopies(Register Reg,
479                                        const MachineRegisterInfo &MRI) {
480   std::optional<DefinitionAndSourceRegister> DefSrcReg =
481       getDefSrcRegIgnoringCopies(Reg, MRI);
482   return DefSrcReg ? DefSrcReg->Reg : Register();
483 }
484 
485 void llvm::extractParts(Register Reg, LLT Ty, int NumParts,
486                         SmallVectorImpl<Register> &VRegs,
487                         MachineIRBuilder &MIRBuilder,
488                         MachineRegisterInfo &MRI) {
489   for (int i = 0; i < NumParts; ++i)
490     VRegs.push_back(MRI.createGenericVirtualRegister(Ty));
491   MIRBuilder.buildUnmerge(VRegs, Reg);
492 }
493 
494 bool llvm::extractParts(Register Reg, LLT RegTy, LLT MainTy, LLT &LeftoverTy,
495                         SmallVectorImpl<Register> &VRegs,
496                         SmallVectorImpl<Register> &LeftoverRegs,
497                         MachineIRBuilder &MIRBuilder,
498                         MachineRegisterInfo &MRI) {
499   assert(!LeftoverTy.isValid() && "this is an out argument");
500 
501   unsigned RegSize = RegTy.getSizeInBits();
502   unsigned MainSize = MainTy.getSizeInBits();
503   unsigned NumParts = RegSize / MainSize;
504   unsigned LeftoverSize = RegSize - NumParts * MainSize;
505 
506   // Use an unmerge when possible.
507   if (LeftoverSize == 0) {
508     for (unsigned I = 0; I < NumParts; ++I)
509       VRegs.push_back(MRI.createGenericVirtualRegister(MainTy));
510     MIRBuilder.buildUnmerge(VRegs, Reg);
511     return true;
512   }
513 
514   // Try to use unmerge for irregular vector split where possible
515   // For example when splitting a <6 x i32> into <4 x i32> with <2 x i32>
516   // leftover, it becomes:
517   //  <2 x i32> %2, <2 x i32>%3, <2 x i32> %4 = G_UNMERGE_VALUE <6 x i32> %1
518   //  <4 x i32> %5 = G_CONCAT_VECTOR <2 x i32> %2, <2 x i32> %3
519   if (RegTy.isVector() && MainTy.isVector()) {
520     unsigned RegNumElts = RegTy.getNumElements();
521     unsigned MainNumElts = MainTy.getNumElements();
522     unsigned LeftoverNumElts = RegNumElts % MainNumElts;
523     // If can unmerge to LeftoverTy, do it
524     if (MainNumElts % LeftoverNumElts == 0 &&
525         RegNumElts % LeftoverNumElts == 0 &&
526         RegTy.getScalarSizeInBits() == MainTy.getScalarSizeInBits() &&
527         LeftoverNumElts > 1) {
528       LeftoverTy =
529           LLT::fixed_vector(LeftoverNumElts, RegTy.getScalarSizeInBits());
530 
531       // Unmerge the SrcReg to LeftoverTy vectors
532       SmallVector<Register, 4> UnmergeValues;
533       extractParts(Reg, LeftoverTy, RegNumElts / LeftoverNumElts, UnmergeValues,
534                    MIRBuilder, MRI);
535 
536       // Find how many LeftoverTy makes one MainTy
537       unsigned LeftoverPerMain = MainNumElts / LeftoverNumElts;
538       unsigned NumOfLeftoverVal =
539           ((RegNumElts % MainNumElts) / LeftoverNumElts);
540 
541       // Create as many MainTy as possible using unmerged value
542       SmallVector<Register, 4> MergeValues;
543       for (unsigned I = 0; I < UnmergeValues.size() - NumOfLeftoverVal; I++) {
544         MergeValues.push_back(UnmergeValues[I]);
545         if (MergeValues.size() == LeftoverPerMain) {
546           VRegs.push_back(
547               MIRBuilder.buildMergeLikeInstr(MainTy, MergeValues).getReg(0));
548           MergeValues.clear();
549         }
550       }
551       // Populate LeftoverRegs with the leftovers
552       for (unsigned I = UnmergeValues.size() - NumOfLeftoverVal;
553            I < UnmergeValues.size(); I++) {
554         LeftoverRegs.push_back(UnmergeValues[I]);
555       }
556       return true;
557     }
558   }
559   // Perform irregular split. Leftover is last element of RegPieces.
560   if (MainTy.isVector()) {
561     SmallVector<Register, 8> RegPieces;
562     extractVectorParts(Reg, MainTy.getNumElements(), RegPieces, MIRBuilder,
563                        MRI);
564     for (unsigned i = 0; i < RegPieces.size() - 1; ++i)
565       VRegs.push_back(RegPieces[i]);
566     LeftoverRegs.push_back(RegPieces[RegPieces.size() - 1]);
567     LeftoverTy = MRI.getType(LeftoverRegs[0]);
568     return true;
569   }
570 
571   LeftoverTy = LLT::scalar(LeftoverSize);
572   // For irregular sizes, extract the individual parts.
573   for (unsigned I = 0; I != NumParts; ++I) {
574     Register NewReg = MRI.createGenericVirtualRegister(MainTy);
575     VRegs.push_back(NewReg);
576     MIRBuilder.buildExtract(NewReg, Reg, MainSize * I);
577   }
578 
579   for (unsigned Offset = MainSize * NumParts; Offset < RegSize;
580        Offset += LeftoverSize) {
581     Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy);
582     LeftoverRegs.push_back(NewReg);
583     MIRBuilder.buildExtract(NewReg, Reg, Offset);
584   }
585 
586   return true;
587 }
588 
589 void llvm::extractVectorParts(Register Reg, unsigned NumElts,
590                               SmallVectorImpl<Register> &VRegs,
591                               MachineIRBuilder &MIRBuilder,
592                               MachineRegisterInfo &MRI) {
593   LLT RegTy = MRI.getType(Reg);
594   assert(RegTy.isVector() && "Expected a vector type");
595 
596   LLT EltTy = RegTy.getElementType();
597   LLT NarrowTy = (NumElts == 1) ? EltTy : LLT::fixed_vector(NumElts, EltTy);
598   unsigned RegNumElts = RegTy.getNumElements();
599   unsigned LeftoverNumElts = RegNumElts % NumElts;
600   unsigned NumNarrowTyPieces = RegNumElts / NumElts;
601 
602   // Perfect split without leftover
603   if (LeftoverNumElts == 0)
604     return extractParts(Reg, NarrowTy, NumNarrowTyPieces, VRegs, MIRBuilder,
605                         MRI);
606 
607   // Irregular split. Provide direct access to all elements for artifact
608   // combiner using unmerge to elements. Then build vectors with NumElts
609   // elements. Remaining element(s) will be (used to build vector) Leftover.
610   SmallVector<Register, 8> Elts;
611   extractParts(Reg, EltTy, RegNumElts, Elts, MIRBuilder, MRI);
612 
613   unsigned Offset = 0;
614   // Requested sub-vectors of NarrowTy.
615   for (unsigned i = 0; i < NumNarrowTyPieces; ++i, Offset += NumElts) {
616     ArrayRef<Register> Pieces(&Elts[Offset], NumElts);
617     VRegs.push_back(MIRBuilder.buildMergeLikeInstr(NarrowTy, Pieces).getReg(0));
618   }
619 
620   // Leftover element(s).
621   if (LeftoverNumElts == 1) {
622     VRegs.push_back(Elts[Offset]);
623   } else {
624     LLT LeftoverTy = LLT::fixed_vector(LeftoverNumElts, EltTy);
625     ArrayRef<Register> Pieces(&Elts[Offset], LeftoverNumElts);
626     VRegs.push_back(
627         MIRBuilder.buildMergeLikeInstr(LeftoverTy, Pieces).getReg(0));
628   }
629 }
630 
631 MachineInstr *llvm::getOpcodeDef(unsigned Opcode, Register Reg,
632                                  const MachineRegisterInfo &MRI) {
633   MachineInstr *DefMI = getDefIgnoringCopies(Reg, MRI);
634   return DefMI && DefMI->getOpcode() == Opcode ? DefMI : nullptr;
635 }
636 
637 APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
638   if (Size == 32)
639     return APFloat(float(Val));
640   if (Size == 64)
641     return APFloat(Val);
642   if (Size != 16)
643     llvm_unreachable("Unsupported FPConstant size");
644   bool Ignored;
645   APFloat APF(Val);
646   APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
647   return APF;
648 }
649 
650 std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
651                                              const Register Op1,
652                                              const Register Op2,
653                                              const MachineRegisterInfo &MRI) {
654   auto MaybeOp2Cst = getAnyConstantVRegValWithLookThrough(Op2, MRI, false);
655   if (!MaybeOp2Cst)
656     return std::nullopt;
657 
658   auto MaybeOp1Cst = getAnyConstantVRegValWithLookThrough(Op1, MRI, false);
659   if (!MaybeOp1Cst)
660     return std::nullopt;
661 
662   const APInt &C1 = MaybeOp1Cst->Value;
663   const APInt &C2 = MaybeOp2Cst->Value;
664   switch (Opcode) {
665   default:
666     break;
667   case TargetOpcode::G_ADD:
668     return C1 + C2;
669   case TargetOpcode::G_PTR_ADD:
670     // Types can be of different width here.
671     // Result needs to be the same width as C1, so trunc or sext C2.
672     return C1 + C2.sextOrTrunc(C1.getBitWidth());
673   case TargetOpcode::G_AND:
674     return C1 & C2;
675   case TargetOpcode::G_ASHR:
676     return C1.ashr(C2);
677   case TargetOpcode::G_LSHR:
678     return C1.lshr(C2);
679   case TargetOpcode::G_MUL:
680     return C1 * C2;
681   case TargetOpcode::G_OR:
682     return C1 | C2;
683   case TargetOpcode::G_SHL:
684     return C1 << C2;
685   case TargetOpcode::G_SUB:
686     return C1 - C2;
687   case TargetOpcode::G_XOR:
688     return C1 ^ C2;
689   case TargetOpcode::G_UDIV:
690     if (!C2.getBoolValue())
691       break;
692     return C1.udiv(C2);
693   case TargetOpcode::G_SDIV:
694     if (!C2.getBoolValue())
695       break;
696     return C1.sdiv(C2);
697   case TargetOpcode::G_UREM:
698     if (!C2.getBoolValue())
699       break;
700     return C1.urem(C2);
701   case TargetOpcode::G_SREM:
702     if (!C2.getBoolValue())
703       break;
704     return C1.srem(C2);
705   case TargetOpcode::G_SMIN:
706     return APIntOps::smin(C1, C2);
707   case TargetOpcode::G_SMAX:
708     return APIntOps::smax(C1, C2);
709   case TargetOpcode::G_UMIN:
710     return APIntOps::umin(C1, C2);
711   case TargetOpcode::G_UMAX:
712     return APIntOps::umax(C1, C2);
713   }
714 
715   return std::nullopt;
716 }
717 
718 std::optional<APFloat>
719 llvm::ConstantFoldFPBinOp(unsigned Opcode, const Register Op1,
720                           const Register Op2, const MachineRegisterInfo &MRI) {
721   const ConstantFP *Op2Cst = getConstantFPVRegVal(Op2, MRI);
722   if (!Op2Cst)
723     return std::nullopt;
724 
725   const ConstantFP *Op1Cst = getConstantFPVRegVal(Op1, MRI);
726   if (!Op1Cst)
727     return std::nullopt;
728 
729   APFloat C1 = Op1Cst->getValueAPF();
730   const APFloat &C2 = Op2Cst->getValueAPF();
731   switch (Opcode) {
732   case TargetOpcode::G_FADD:
733     C1.add(C2, APFloat::rmNearestTiesToEven);
734     return C1;
735   case TargetOpcode::G_FSUB:
736     C1.subtract(C2, APFloat::rmNearestTiesToEven);
737     return C1;
738   case TargetOpcode::G_FMUL:
739     C1.multiply(C2, APFloat::rmNearestTiesToEven);
740     return C1;
741   case TargetOpcode::G_FDIV:
742     C1.divide(C2, APFloat::rmNearestTiesToEven);
743     return C1;
744   case TargetOpcode::G_FREM:
745     C1.mod(C2);
746     return C1;
747   case TargetOpcode::G_FCOPYSIGN:
748     C1.copySign(C2);
749     return C1;
750   case TargetOpcode::G_FMINNUM:
751     return minnum(C1, C2);
752   case TargetOpcode::G_FMAXNUM:
753     return maxnum(C1, C2);
754   case TargetOpcode::G_FMINIMUM:
755     return minimum(C1, C2);
756   case TargetOpcode::G_FMAXIMUM:
757     return maximum(C1, C2);
758   case TargetOpcode::G_FMINNUM_IEEE:
759   case TargetOpcode::G_FMAXNUM_IEEE:
760     // FIXME: These operations were unfortunately named. fminnum/fmaxnum do not
761     // follow the IEEE behavior for signaling nans and follow libm's fmin/fmax,
762     // and currently there isn't a nice wrapper in APFloat for the version with
763     // correct snan handling.
764     break;
765   default:
766     break;
767   }
768 
769   return std::nullopt;
770 }
771 
772 SmallVector<APInt>
773 llvm::ConstantFoldVectorBinop(unsigned Opcode, const Register Op1,
774                               const Register Op2,
775                               const MachineRegisterInfo &MRI) {
776   auto *SrcVec2 = getOpcodeDef<GBuildVector>(Op2, MRI);
777   if (!SrcVec2)
778     return SmallVector<APInt>();
779 
780   auto *SrcVec1 = getOpcodeDef<GBuildVector>(Op1, MRI);
781   if (!SrcVec1)
782     return SmallVector<APInt>();
783 
784   SmallVector<APInt> FoldedElements;
785   for (unsigned Idx = 0, E = SrcVec1->getNumSources(); Idx < E; ++Idx) {
786     auto MaybeCst = ConstantFoldBinOp(Opcode, SrcVec1->getSourceReg(Idx),
787                                       SrcVec2->getSourceReg(Idx), MRI);
788     if (!MaybeCst)
789       return SmallVector<APInt>();
790     FoldedElements.push_back(*MaybeCst);
791   }
792   return FoldedElements;
793 }
794 
795 bool llvm::isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
796                            bool SNaN) {
797   const MachineInstr *DefMI = MRI.getVRegDef(Val);
798   if (!DefMI)
799     return false;
800 
801   const TargetMachine& TM = DefMI->getMF()->getTarget();
802   if (DefMI->getFlag(MachineInstr::FmNoNans) || TM.Options.NoNaNsFPMath)
803     return true;
804 
805   // If the value is a constant, we can obviously see if it is a NaN or not.
806   if (const ConstantFP *FPVal = getConstantFPVRegVal(Val, MRI)) {
807     return !FPVal->getValueAPF().isNaN() ||
808            (SNaN && !FPVal->getValueAPF().isSignaling());
809   }
810 
811   if (DefMI->getOpcode() == TargetOpcode::G_BUILD_VECTOR) {
812     for (const auto &Op : DefMI->uses())
813       if (!isKnownNeverNaN(Op.getReg(), MRI, SNaN))
814         return false;
815     return true;
816   }
817 
818   switch (DefMI->getOpcode()) {
819   default:
820     break;
821   case TargetOpcode::G_FADD:
822   case TargetOpcode::G_FSUB:
823   case TargetOpcode::G_FMUL:
824   case TargetOpcode::G_FDIV:
825   case TargetOpcode::G_FREM:
826   case TargetOpcode::G_FSIN:
827   case TargetOpcode::G_FCOS:
828   case TargetOpcode::G_FTAN:
829   case TargetOpcode::G_FACOS:
830   case TargetOpcode::G_FASIN:
831   case TargetOpcode::G_FATAN:
832   case TargetOpcode::G_FATAN2:
833   case TargetOpcode::G_FCOSH:
834   case TargetOpcode::G_FSINH:
835   case TargetOpcode::G_FTANH:
836   case TargetOpcode::G_FMA:
837   case TargetOpcode::G_FMAD:
838     if (SNaN)
839       return true;
840 
841     // TODO: Need isKnownNeverInfinity
842     return false;
843   case TargetOpcode::G_FMINNUM_IEEE:
844   case TargetOpcode::G_FMAXNUM_IEEE: {
845     if (SNaN)
846       return true;
847     // This can return a NaN if either operand is an sNaN, or if both operands
848     // are NaN.
849     return (isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI) &&
850             isKnownNeverSNaN(DefMI->getOperand(2).getReg(), MRI)) ||
851            (isKnownNeverSNaN(DefMI->getOperand(1).getReg(), MRI) &&
852             isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI));
853   }
854   case TargetOpcode::G_FMINNUM:
855   case TargetOpcode::G_FMAXNUM: {
856     // Only one needs to be known not-nan, since it will be returned if the
857     // other ends up being one.
858     return isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI, SNaN) ||
859            isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI, SNaN);
860   }
861   }
862 
863   if (SNaN) {
864     // FP operations quiet. For now, just handle the ones inserted during
865     // legalization.
866     switch (DefMI->getOpcode()) {
867     case TargetOpcode::G_FPEXT:
868     case TargetOpcode::G_FPTRUNC:
869     case TargetOpcode::G_FCANONICALIZE:
870       return true;
871     default:
872       return false;
873     }
874   }
875 
876   return false;
877 }
878 
879 Align llvm::inferAlignFromPtrInfo(MachineFunction &MF,
880                                   const MachinePointerInfo &MPO) {
881   auto PSV = dyn_cast_if_present<const PseudoSourceValue *>(MPO.V);
882   if (auto FSPV = dyn_cast_or_null<FixedStackPseudoSourceValue>(PSV)) {
883     MachineFrameInfo &MFI = MF.getFrameInfo();
884     return commonAlignment(MFI.getObjectAlign(FSPV->getFrameIndex()),
885                            MPO.Offset);
886   }
887 
888   if (const Value *V = dyn_cast_if_present<const Value *>(MPO.V)) {
889     const Module *M = MF.getFunction().getParent();
890     return V->getPointerAlignment(M->getDataLayout());
891   }
892 
893   return Align(1);
894 }
895 
896 Register llvm::getFunctionLiveInPhysReg(MachineFunction &MF,
897                                         const TargetInstrInfo &TII,
898                                         MCRegister PhysReg,
899                                         const TargetRegisterClass &RC,
900                                         const DebugLoc &DL, LLT RegTy) {
901   MachineBasicBlock &EntryMBB = MF.front();
902   MachineRegisterInfo &MRI = MF.getRegInfo();
903   Register LiveIn = MRI.getLiveInVirtReg(PhysReg);
904   if (LiveIn) {
905     MachineInstr *Def = MRI.getVRegDef(LiveIn);
906     if (Def) {
907       // FIXME: Should the verifier check this is in the entry block?
908       assert(Def->getParent() == &EntryMBB && "live-in copy not in entry block");
909       return LiveIn;
910     }
911 
912     // It's possible the incoming argument register and copy was added during
913     // lowering, but later deleted due to being/becoming dead. If this happens,
914     // re-insert the copy.
915   } else {
916     // The live in register was not present, so add it.
917     LiveIn = MF.addLiveIn(PhysReg, &RC);
918     if (RegTy.isValid())
919       MRI.setType(LiveIn, RegTy);
920   }
921 
922   BuildMI(EntryMBB, EntryMBB.begin(), DL, TII.get(TargetOpcode::COPY), LiveIn)
923     .addReg(PhysReg);
924   if (!EntryMBB.isLiveIn(PhysReg))
925     EntryMBB.addLiveIn(PhysReg);
926   return LiveIn;
927 }
928 
929 std::optional<APInt> llvm::ConstantFoldExtOp(unsigned Opcode,
930                                              const Register Op1, uint64_t Imm,
931                                              const MachineRegisterInfo &MRI) {
932   auto MaybeOp1Cst = getIConstantVRegVal(Op1, MRI);
933   if (MaybeOp1Cst) {
934     switch (Opcode) {
935     default:
936       break;
937     case TargetOpcode::G_SEXT_INREG: {
938       LLT Ty = MRI.getType(Op1);
939       return MaybeOp1Cst->trunc(Imm).sext(Ty.getScalarSizeInBits());
940     }
941     }
942   }
943   return std::nullopt;
944 }
945 
946 std::optional<APInt> llvm::ConstantFoldCastOp(unsigned Opcode, LLT DstTy,
947                                               const Register Op0,
948                                               const MachineRegisterInfo &MRI) {
949   std::optional<APInt> Val = getIConstantVRegVal(Op0, MRI);
950   if (!Val)
951     return Val;
952 
953   const unsigned DstSize = DstTy.getScalarSizeInBits();
954 
955   switch (Opcode) {
956   case TargetOpcode::G_SEXT:
957     return Val->sext(DstSize);
958   case TargetOpcode::G_ZEXT:
959   case TargetOpcode::G_ANYEXT:
960     // TODO: DAG considers target preference when constant folding any_extend.
961     return Val->zext(DstSize);
962   default:
963     break;
964   }
965 
966   llvm_unreachable("unexpected cast opcode to constant fold");
967 }
968 
969 std::optional<APFloat>
970 llvm::ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
971                              const MachineRegisterInfo &MRI) {
972   assert(Opcode == TargetOpcode::G_SITOFP || Opcode == TargetOpcode::G_UITOFP);
973   if (auto MaybeSrcVal = getIConstantVRegVal(Src, MRI)) {
974     APFloat DstVal(getFltSemanticForLLT(DstTy));
975     DstVal.convertFromAPInt(*MaybeSrcVal, Opcode == TargetOpcode::G_SITOFP,
976                             APFloat::rmNearestTiesToEven);
977     return DstVal;
978   }
979   return std::nullopt;
980 }
981 
982 std::optional<SmallVector<unsigned>>
983 llvm::ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
984                              std::function<unsigned(APInt)> CB) {
985   LLT Ty = MRI.getType(Src);
986   SmallVector<unsigned> FoldedCTLZs;
987   auto tryFoldScalar = [&](Register R) -> std::optional<unsigned> {
988     auto MaybeCst = getIConstantVRegVal(R, MRI);
989     if (!MaybeCst)
990       return std::nullopt;
991     return CB(*MaybeCst);
992   };
993   if (Ty.isVector()) {
994     // Try to constant fold each element.
995     auto *BV = getOpcodeDef<GBuildVector>(Src, MRI);
996     if (!BV)
997       return std::nullopt;
998     for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
999       if (auto MaybeFold = tryFoldScalar(BV->getSourceReg(SrcIdx))) {
1000         FoldedCTLZs.emplace_back(*MaybeFold);
1001         continue;
1002       }
1003       return std::nullopt;
1004     }
1005     return FoldedCTLZs;
1006   }
1007   if (auto MaybeCst = tryFoldScalar(Src)) {
1008     FoldedCTLZs.emplace_back(*MaybeCst);
1009     return FoldedCTLZs;
1010   }
1011   return std::nullopt;
1012 }
1013 
1014 std::optional<SmallVector<APInt>>
1015 llvm::ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2,
1016                        const MachineRegisterInfo &MRI) {
1017   LLT Ty = MRI.getType(Op1);
1018   if (Ty != MRI.getType(Op2))
1019     return std::nullopt;
1020 
1021   auto TryFoldScalar = [&MRI, Pred](Register LHS,
1022                                     Register RHS) -> std::optional<APInt> {
1023     auto LHSCst = getIConstantVRegVal(LHS, MRI);
1024     auto RHSCst = getIConstantVRegVal(RHS, MRI);
1025     if (!LHSCst || !RHSCst)
1026       return std::nullopt;
1027 
1028     switch (Pred) {
1029     case CmpInst::Predicate::ICMP_EQ:
1030       return APInt(/*numBits=*/1, LHSCst->eq(*RHSCst));
1031     case CmpInst::Predicate::ICMP_NE:
1032       return APInt(/*numBits=*/1, LHSCst->ne(*RHSCst));
1033     case CmpInst::Predicate::ICMP_UGT:
1034       return APInt(/*numBits=*/1, LHSCst->ugt(*RHSCst));
1035     case CmpInst::Predicate::ICMP_UGE:
1036       return APInt(/*numBits=*/1, LHSCst->uge(*RHSCst));
1037     case CmpInst::Predicate::ICMP_ULT:
1038       return APInt(/*numBits=*/1, LHSCst->ult(*RHSCst));
1039     case CmpInst::Predicate::ICMP_ULE:
1040       return APInt(/*numBits=*/1, LHSCst->ule(*RHSCst));
1041     case CmpInst::Predicate::ICMP_SGT:
1042       return APInt(/*numBits=*/1, LHSCst->sgt(*RHSCst));
1043     case CmpInst::Predicate::ICMP_SGE:
1044       return APInt(/*numBits=*/1, LHSCst->sge(*RHSCst));
1045     case CmpInst::Predicate::ICMP_SLT:
1046       return APInt(/*numBits=*/1, LHSCst->slt(*RHSCst));
1047     case CmpInst::Predicate::ICMP_SLE:
1048       return APInt(/*numBits=*/1, LHSCst->sle(*RHSCst));
1049     default:
1050       return std::nullopt;
1051     }
1052   };
1053 
1054   SmallVector<APInt> FoldedICmps;
1055 
1056   if (Ty.isVector()) {
1057     // Try to constant fold each element.
1058     auto *BV1 = getOpcodeDef<GBuildVector>(Op1, MRI);
1059     auto *BV2 = getOpcodeDef<GBuildVector>(Op2, MRI);
1060     if (!BV1 || !BV2)
1061       return std::nullopt;
1062     assert(BV1->getNumSources() == BV2->getNumSources() && "Invalid vectors");
1063     for (unsigned I = 0; I < BV1->getNumSources(); ++I) {
1064       if (auto MaybeFold =
1065               TryFoldScalar(BV1->getSourceReg(I), BV2->getSourceReg(I))) {
1066         FoldedICmps.emplace_back(*MaybeFold);
1067         continue;
1068       }
1069       return std::nullopt;
1070     }
1071     return FoldedICmps;
1072   }
1073 
1074   if (auto MaybeCst = TryFoldScalar(Op1, Op2)) {
1075     FoldedICmps.emplace_back(*MaybeCst);
1076     return FoldedICmps;
1077   }
1078 
1079   return std::nullopt;
1080 }
1081 
1082 bool llvm::isKnownToBeAPowerOfTwo(Register Reg, const MachineRegisterInfo &MRI,
1083                                   GISelKnownBits *KB) {
1084   std::optional<DefinitionAndSourceRegister> DefSrcReg =
1085       getDefSrcRegIgnoringCopies(Reg, MRI);
1086   if (!DefSrcReg)
1087     return false;
1088 
1089   const MachineInstr &MI = *DefSrcReg->MI;
1090   const LLT Ty = MRI.getType(Reg);
1091 
1092   switch (MI.getOpcode()) {
1093   case TargetOpcode::G_CONSTANT: {
1094     unsigned BitWidth = Ty.getScalarSizeInBits();
1095     const ConstantInt *CI = MI.getOperand(1).getCImm();
1096     return CI->getValue().zextOrTrunc(BitWidth).isPowerOf2();
1097   }
1098   case TargetOpcode::G_SHL: {
1099     // A left-shift of a constant one will have exactly one bit set because
1100     // shifting the bit off the end is undefined.
1101 
1102     // TODO: Constant splat
1103     if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
1104       if (*ConstLHS == 1)
1105         return true;
1106     }
1107 
1108     break;
1109   }
1110   case TargetOpcode::G_LSHR: {
1111     if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
1112       if (ConstLHS->isSignMask())
1113         return true;
1114     }
1115 
1116     break;
1117   }
1118   case TargetOpcode::G_BUILD_VECTOR: {
1119     // TODO: Probably should have a recursion depth guard since you could have
1120     // bitcasted vector elements.
1121     for (const MachineOperand &MO : llvm::drop_begin(MI.operands()))
1122       if (!isKnownToBeAPowerOfTwo(MO.getReg(), MRI, KB))
1123         return false;
1124 
1125     return true;
1126   }
1127   case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1128     // Only handle constants since we would need to know if number of leading
1129     // zeros is greater than the truncation amount.
1130     const unsigned BitWidth = Ty.getScalarSizeInBits();
1131     for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) {
1132       auto Const = getIConstantVRegVal(MO.getReg(), MRI);
1133       if (!Const || !Const->zextOrTrunc(BitWidth).isPowerOf2())
1134         return false;
1135     }
1136 
1137     return true;
1138   }
1139   default:
1140     break;
1141   }
1142 
1143   if (!KB)
1144     return false;
1145 
1146   // More could be done here, though the above checks are enough
1147   // to handle some common cases.
1148 
1149   // Fall back to computeKnownBits to catch other known cases.
1150   KnownBits Known = KB->getKnownBits(Reg);
1151   return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
1152 }
1153 
1154 void llvm::getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU) {
1155   AU.addPreserved<StackProtector>();
1156 }
1157 
1158 LLT llvm::getLCMType(LLT OrigTy, LLT TargetTy) {
1159   if (OrigTy.getSizeInBits() == TargetTy.getSizeInBits())
1160     return OrigTy;
1161 
1162   if (OrigTy.isVector() && TargetTy.isVector()) {
1163     LLT OrigElt = OrigTy.getElementType();
1164     LLT TargetElt = TargetTy.getElementType();
1165 
1166     // TODO: The docstring for this function says the intention is to use this
1167     // function to build MERGE/UNMERGE instructions. It won't be the case that
1168     // we generate a MERGE/UNMERGE between fixed and scalable vector types. We
1169     // could implement getLCMType between the two in the future if there was a
1170     // need, but it is not worth it now as this function should not be used in
1171     // that way.
1172     assert(((OrigTy.isScalableVector() && !TargetTy.isFixedVector()) ||
1173             (OrigTy.isFixedVector() && !TargetTy.isScalableVector())) &&
1174            "getLCMType not implemented between fixed and scalable vectors.");
1175 
1176     if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
1177       int GCDMinElts = std::gcd(OrigTy.getElementCount().getKnownMinValue(),
1178                                 TargetTy.getElementCount().getKnownMinValue());
1179       // Prefer the original element type.
1180       ElementCount Mul = OrigTy.getElementCount().multiplyCoefficientBy(
1181           TargetTy.getElementCount().getKnownMinValue());
1182       return LLT::vector(Mul.divideCoefficientBy(GCDMinElts),
1183                          OrigTy.getElementType());
1184     }
1185     unsigned LCM = std::lcm(OrigTy.getSizeInBits().getKnownMinValue(),
1186                             TargetTy.getSizeInBits().getKnownMinValue());
1187     return LLT::vector(
1188         ElementCount::get(LCM / OrigElt.getSizeInBits(), OrigTy.isScalable()),
1189         OrigElt);
1190   }
1191 
1192   // One type is scalar, one type is vector
1193   if (OrigTy.isVector() || TargetTy.isVector()) {
1194     LLT VecTy = OrigTy.isVector() ? OrigTy : TargetTy;
1195     LLT ScalarTy = OrigTy.isVector() ? TargetTy : OrigTy;
1196     LLT EltTy = VecTy.getElementType();
1197     LLT OrigEltTy = OrigTy.isVector() ? OrigTy.getElementType() : OrigTy;
1198 
1199     // Prefer scalar type from OrigTy.
1200     if (EltTy.getSizeInBits() == ScalarTy.getSizeInBits())
1201       return LLT::vector(VecTy.getElementCount(), OrigEltTy);
1202 
1203     // Different size scalars. Create vector with the same total size.
1204     // LCM will take fixed/scalable from VecTy.
1205     unsigned LCM = std::lcm(EltTy.getSizeInBits().getFixedValue() *
1206                                 VecTy.getElementCount().getKnownMinValue(),
1207                             ScalarTy.getSizeInBits().getFixedValue());
1208     // Prefer type from OrigTy
1209     return LLT::vector(ElementCount::get(LCM / OrigEltTy.getSizeInBits(),
1210                                          VecTy.getElementCount().isScalable()),
1211                        OrigEltTy);
1212   }
1213 
1214   // At this point, both types are scalars of different size
1215   unsigned LCM = std::lcm(OrigTy.getSizeInBits().getFixedValue(),
1216                           TargetTy.getSizeInBits().getFixedValue());
1217   // Preserve pointer types.
1218   if (LCM == OrigTy.getSizeInBits())
1219     return OrigTy;
1220   if (LCM == TargetTy.getSizeInBits())
1221     return TargetTy;
1222   return LLT::scalar(LCM);
1223 }
1224 
1225 LLT llvm::getCoverTy(LLT OrigTy, LLT TargetTy) {
1226 
1227   if ((OrigTy.isScalableVector() && TargetTy.isFixedVector()) ||
1228       (OrigTy.isFixedVector() && TargetTy.isScalableVector()))
1229     llvm_unreachable(
1230         "getCoverTy not implemented between fixed and scalable vectors.");
1231 
1232   if (!OrigTy.isVector() || !TargetTy.isVector() || OrigTy == TargetTy ||
1233       (OrigTy.getScalarSizeInBits() != TargetTy.getScalarSizeInBits()))
1234     return getLCMType(OrigTy, TargetTy);
1235 
1236   unsigned OrigTyNumElts = OrigTy.getElementCount().getKnownMinValue();
1237   unsigned TargetTyNumElts = TargetTy.getElementCount().getKnownMinValue();
1238   if (OrigTyNumElts % TargetTyNumElts == 0)
1239     return OrigTy;
1240 
1241   unsigned NumElts = alignTo(OrigTyNumElts, TargetTyNumElts);
1242   return LLT::scalarOrVector(ElementCount::getFixed(NumElts),
1243                              OrigTy.getElementType());
1244 }
1245 
1246 LLT llvm::getGCDType(LLT OrigTy, LLT TargetTy) {
1247   if (OrigTy.getSizeInBits() == TargetTy.getSizeInBits())
1248     return OrigTy;
1249 
1250   if (OrigTy.isVector() && TargetTy.isVector()) {
1251     LLT OrigElt = OrigTy.getElementType();
1252 
1253     // TODO: The docstring for this function says the intention is to use this
1254     // function to build MERGE/UNMERGE instructions. It won't be the case that
1255     // we generate a MERGE/UNMERGE between fixed and scalable vector types. We
1256     // could implement getGCDType between the two in the future if there was a
1257     // need, but it is not worth it now as this function should not be used in
1258     // that way.
1259     assert(((OrigTy.isScalableVector() && !TargetTy.isFixedVector()) ||
1260             (OrigTy.isFixedVector() && !TargetTy.isScalableVector())) &&
1261            "getGCDType not implemented between fixed and scalable vectors.");
1262 
1263     unsigned GCD = std::gcd(OrigTy.getSizeInBits().getKnownMinValue(),
1264                             TargetTy.getSizeInBits().getKnownMinValue());
1265     if (GCD == OrigElt.getSizeInBits())
1266       return LLT::scalarOrVector(ElementCount::get(1, OrigTy.isScalable()),
1267                                  OrigElt);
1268 
1269     // Cannot produce original element type, but both have vscale in common.
1270     if (GCD < OrigElt.getSizeInBits())
1271       return LLT::scalarOrVector(ElementCount::get(1, OrigTy.isScalable()),
1272                                  GCD);
1273 
1274     return LLT::vector(
1275         ElementCount::get(GCD / OrigElt.getSizeInBits().getFixedValue(),
1276                           OrigTy.isScalable()),
1277         OrigElt);
1278   }
1279 
1280   // If one type is vector and the element size matches the scalar size, then
1281   // the gcd is the scalar type.
1282   if (OrigTy.isVector() &&
1283       OrigTy.getElementType().getSizeInBits() == TargetTy.getSizeInBits())
1284     return OrigTy.getElementType();
1285   if (TargetTy.isVector() &&
1286       TargetTy.getElementType().getSizeInBits() == OrigTy.getSizeInBits())
1287     return OrigTy;
1288 
1289   // At this point, both types are either scalars of different type or one is a
1290   // vector and one is a scalar. If both types are scalars, the GCD type is the
1291   // GCD between the two scalar sizes. If one is vector and one is scalar, then
1292   // the GCD type is the GCD between the scalar and the vector element size.
1293   LLT OrigScalar = OrigTy.getScalarType();
1294   LLT TargetScalar = TargetTy.getScalarType();
1295   unsigned GCD = std::gcd(OrigScalar.getSizeInBits().getFixedValue(),
1296                           TargetScalar.getSizeInBits().getFixedValue());
1297   return LLT::scalar(GCD);
1298 }
1299 
1300 std::optional<int> llvm::getSplatIndex(MachineInstr &MI) {
1301   assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
1302          "Only G_SHUFFLE_VECTOR can have a splat index!");
1303   ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
1304   auto FirstDefinedIdx = find_if(Mask, [](int Elt) { return Elt >= 0; });
1305 
1306   // If all elements are undefined, this shuffle can be considered a splat.
1307   // Return 0 for better potential for callers to simplify.
1308   if (FirstDefinedIdx == Mask.end())
1309     return 0;
1310 
1311   // Make sure all remaining elements are either undef or the same
1312   // as the first non-undef value.
1313   int SplatValue = *FirstDefinedIdx;
1314   if (any_of(make_range(std::next(FirstDefinedIdx), Mask.end()),
1315              [&SplatValue](int Elt) { return Elt >= 0 && Elt != SplatValue; }))
1316     return std::nullopt;
1317 
1318   return SplatValue;
1319 }
1320 
1321 static bool isBuildVectorOp(unsigned Opcode) {
1322   return Opcode == TargetOpcode::G_BUILD_VECTOR ||
1323          Opcode == TargetOpcode::G_BUILD_VECTOR_TRUNC;
1324 }
1325 
1326 namespace {
1327 
1328 std::optional<ValueAndVReg> getAnyConstantSplat(Register VReg,
1329                                                 const MachineRegisterInfo &MRI,
1330                                                 bool AllowUndef) {
1331   MachineInstr *MI = getDefIgnoringCopies(VReg, MRI);
1332   if (!MI)
1333     return std::nullopt;
1334 
1335   bool isConcatVectorsOp = MI->getOpcode() == TargetOpcode::G_CONCAT_VECTORS;
1336   if (!isBuildVectorOp(MI->getOpcode()) && !isConcatVectorsOp)
1337     return std::nullopt;
1338 
1339   std::optional<ValueAndVReg> SplatValAndReg;
1340   for (MachineOperand &Op : MI->uses()) {
1341     Register Element = Op.getReg();
1342     // If we have a G_CONCAT_VECTOR, we recursively look into the
1343     // vectors that we're concatenating to see if they're splats.
1344     auto ElementValAndReg =
1345         isConcatVectorsOp
1346             ? getAnyConstantSplat(Element, MRI, AllowUndef)
1347             : getAnyConstantVRegValWithLookThrough(Element, MRI, true, true);
1348 
1349     // If AllowUndef, treat undef as value that will result in a constant splat.
1350     if (!ElementValAndReg) {
1351       if (AllowUndef && isa<GImplicitDef>(MRI.getVRegDef(Element)))
1352         continue;
1353       return std::nullopt;
1354     }
1355 
1356     // Record splat value
1357     if (!SplatValAndReg)
1358       SplatValAndReg = ElementValAndReg;
1359 
1360     // Different constant than the one already recorded, not a constant splat.
1361     if (SplatValAndReg->Value != ElementValAndReg->Value)
1362       return std::nullopt;
1363   }
1364 
1365   return SplatValAndReg;
1366 }
1367 
1368 } // end anonymous namespace
1369 
1370 bool llvm::isBuildVectorConstantSplat(const Register Reg,
1371                                       const MachineRegisterInfo &MRI,
1372                                       int64_t SplatValue, bool AllowUndef) {
1373   if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef))
1374     return mi_match(SplatValAndReg->VReg, MRI, m_SpecificICst(SplatValue));
1375   return false;
1376 }
1377 
1378 bool llvm::isBuildVectorConstantSplat(const MachineInstr &MI,
1379                                       const MachineRegisterInfo &MRI,
1380                                       int64_t SplatValue, bool AllowUndef) {
1381   return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
1382                                     AllowUndef);
1383 }
1384 
1385 std::optional<APInt>
1386 llvm::getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI) {
1387   if (auto SplatValAndReg =
1388           getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false)) {
1389     if (std::optional<ValueAndVReg> ValAndVReg =
1390         getIConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI))
1391       return ValAndVReg->Value;
1392   }
1393 
1394   return std::nullopt;
1395 }
1396 
1397 std::optional<APInt>
1398 llvm::getIConstantSplatVal(const MachineInstr &MI,
1399                            const MachineRegisterInfo &MRI) {
1400   return getIConstantSplatVal(MI.getOperand(0).getReg(), MRI);
1401 }
1402 
1403 std::optional<int64_t>
1404 llvm::getIConstantSplatSExtVal(const Register Reg,
1405                                const MachineRegisterInfo &MRI) {
1406   if (auto SplatValAndReg =
1407           getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false))
1408     return getIConstantVRegSExtVal(SplatValAndReg->VReg, MRI);
1409   return std::nullopt;
1410 }
1411 
1412 std::optional<int64_t>
1413 llvm::getIConstantSplatSExtVal(const MachineInstr &MI,
1414                                const MachineRegisterInfo &MRI) {
1415   return getIConstantSplatSExtVal(MI.getOperand(0).getReg(), MRI);
1416 }
1417 
1418 std::optional<FPValueAndVReg>
1419 llvm::getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
1420                         bool AllowUndef) {
1421   if (auto SplatValAndReg = getAnyConstantSplat(VReg, MRI, AllowUndef))
1422     return getFConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
1423   return std::nullopt;
1424 }
1425 
1426 bool llvm::isBuildVectorAllZeros(const MachineInstr &MI,
1427                                  const MachineRegisterInfo &MRI,
1428                                  bool AllowUndef) {
1429   return isBuildVectorConstantSplat(MI, MRI, 0, AllowUndef);
1430 }
1431 
1432 bool llvm::isBuildVectorAllOnes(const MachineInstr &MI,
1433                                 const MachineRegisterInfo &MRI,
1434                                 bool AllowUndef) {
1435   return isBuildVectorConstantSplat(MI, MRI, -1, AllowUndef);
1436 }
1437 
1438 std::optional<RegOrConstant>
1439 llvm::getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI) {
1440   unsigned Opc = MI.getOpcode();
1441   if (!isBuildVectorOp(Opc))
1442     return std::nullopt;
1443   if (auto Splat = getIConstantSplatSExtVal(MI, MRI))
1444     return RegOrConstant(*Splat);
1445   auto Reg = MI.getOperand(1).getReg();
1446   if (any_of(drop_begin(MI.operands(), 2),
1447              [&Reg](const MachineOperand &Op) { return Op.getReg() != Reg; }))
1448     return std::nullopt;
1449   return RegOrConstant(Reg);
1450 }
1451 
1452 static bool isConstantScalar(const MachineInstr &MI,
1453                              const MachineRegisterInfo &MRI,
1454                              bool AllowFP = true,
1455                              bool AllowOpaqueConstants = true) {
1456   switch (MI.getOpcode()) {
1457   case TargetOpcode::G_CONSTANT:
1458   case TargetOpcode::G_IMPLICIT_DEF:
1459     return true;
1460   case TargetOpcode::G_FCONSTANT:
1461     return AllowFP;
1462   case TargetOpcode::G_GLOBAL_VALUE:
1463   case TargetOpcode::G_FRAME_INDEX:
1464   case TargetOpcode::G_BLOCK_ADDR:
1465   case TargetOpcode::G_JUMP_TABLE:
1466     return AllowOpaqueConstants;
1467   default:
1468     return false;
1469   }
1470 }
1471 
1472 bool llvm::isConstantOrConstantVector(MachineInstr &MI,
1473                                       const MachineRegisterInfo &MRI) {
1474   Register Def = MI.getOperand(0).getReg();
1475   if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1476     return true;
1477   GBuildVector *BV = dyn_cast<GBuildVector>(&MI);
1478   if (!BV)
1479     return false;
1480   for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
1481     if (getIConstantVRegValWithLookThrough(BV->getSourceReg(SrcIdx), MRI) ||
1482         getOpcodeDef<GImplicitDef>(BV->getSourceReg(SrcIdx), MRI))
1483       continue;
1484     return false;
1485   }
1486   return true;
1487 }
1488 
1489 bool llvm::isConstantOrConstantVector(const MachineInstr &MI,
1490                                       const MachineRegisterInfo &MRI,
1491                                       bool AllowFP, bool AllowOpaqueConstants) {
1492   if (isConstantScalar(MI, MRI, AllowFP, AllowOpaqueConstants))
1493     return true;
1494 
1495   if (!isBuildVectorOp(MI.getOpcode()))
1496     return false;
1497 
1498   const unsigned NumOps = MI.getNumOperands();
1499   for (unsigned I = 1; I != NumOps; ++I) {
1500     const MachineInstr *ElementDef = MRI.getVRegDef(MI.getOperand(I).getReg());
1501     if (!isConstantScalar(*ElementDef, MRI, AllowFP, AllowOpaqueConstants))
1502       return false;
1503   }
1504 
1505   return true;
1506 }
1507 
1508 std::optional<APInt>
1509 llvm::isConstantOrConstantSplatVector(MachineInstr &MI,
1510                                       const MachineRegisterInfo &MRI) {
1511   Register Def = MI.getOperand(0).getReg();
1512   if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1513     return C->Value;
1514   auto MaybeCst = getIConstantSplatSExtVal(MI, MRI);
1515   if (!MaybeCst)
1516     return std::nullopt;
1517   const unsigned ScalarSize = MRI.getType(Def).getScalarSizeInBits();
1518   return APInt(ScalarSize, *MaybeCst, true);
1519 }
1520 
1521 bool llvm::isNullOrNullSplat(const MachineInstr &MI,
1522                              const MachineRegisterInfo &MRI, bool AllowUndefs) {
1523   switch (MI.getOpcode()) {
1524   case TargetOpcode::G_IMPLICIT_DEF:
1525     return AllowUndefs;
1526   case TargetOpcode::G_CONSTANT:
1527     return MI.getOperand(1).getCImm()->isNullValue();
1528   case TargetOpcode::G_FCONSTANT: {
1529     const ConstantFP *FPImm = MI.getOperand(1).getFPImm();
1530     return FPImm->isZero() && !FPImm->isNegative();
1531   }
1532   default:
1533     if (!AllowUndefs) // TODO: isBuildVectorAllZeros assumes undef is OK already
1534       return false;
1535     return isBuildVectorAllZeros(MI, MRI);
1536   }
1537 }
1538 
1539 bool llvm::isAllOnesOrAllOnesSplat(const MachineInstr &MI,
1540                                    const MachineRegisterInfo &MRI,
1541                                    bool AllowUndefs) {
1542   switch (MI.getOpcode()) {
1543   case TargetOpcode::G_IMPLICIT_DEF:
1544     return AllowUndefs;
1545   case TargetOpcode::G_CONSTANT:
1546     return MI.getOperand(1).getCImm()->isAllOnesValue();
1547   default:
1548     if (!AllowUndefs) // TODO: isBuildVectorAllOnes assumes undef is OK already
1549       return false;
1550     return isBuildVectorAllOnes(MI, MRI);
1551   }
1552 }
1553 
1554 bool llvm::matchUnaryPredicate(
1555     const MachineRegisterInfo &MRI, Register Reg,
1556     std::function<bool(const Constant *ConstVal)> Match, bool AllowUndefs) {
1557 
1558   const MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
1559   if (AllowUndefs && Def->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
1560     return Match(nullptr);
1561 
1562   // TODO: Also handle fconstant
1563   if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
1564     return Match(Def->getOperand(1).getCImm());
1565 
1566   if (Def->getOpcode() != TargetOpcode::G_BUILD_VECTOR)
1567     return false;
1568 
1569   for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
1570     Register SrcElt = Def->getOperand(I).getReg();
1571     const MachineInstr *SrcDef = getDefIgnoringCopies(SrcElt, MRI);
1572     if (AllowUndefs && SrcDef->getOpcode() == TargetOpcode::G_IMPLICIT_DEF) {
1573       if (!Match(nullptr))
1574         return false;
1575       continue;
1576     }
1577 
1578     if (SrcDef->getOpcode() != TargetOpcode::G_CONSTANT ||
1579         !Match(SrcDef->getOperand(1).getCImm()))
1580       return false;
1581   }
1582 
1583   return true;
1584 }
1585 
1586 bool llvm::isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
1587                           bool IsFP) {
1588   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1589   case TargetLowering::UndefinedBooleanContent:
1590     return Val & 0x1;
1591   case TargetLowering::ZeroOrOneBooleanContent:
1592     return Val == 1;
1593   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1594     return Val == -1;
1595   }
1596   llvm_unreachable("Invalid boolean contents");
1597 }
1598 
1599 bool llvm::isConstFalseVal(const TargetLowering &TLI, int64_t Val,
1600                            bool IsVector, bool IsFP) {
1601   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1602   case TargetLowering::UndefinedBooleanContent:
1603     return ~Val & 0x1;
1604   case TargetLowering::ZeroOrOneBooleanContent:
1605   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1606     return Val == 0;
1607   }
1608   llvm_unreachable("Invalid boolean contents");
1609 }
1610 
1611 int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
1612                              bool IsFP) {
1613   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1614   case TargetLowering::UndefinedBooleanContent:
1615   case TargetLowering::ZeroOrOneBooleanContent:
1616     return 1;
1617   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1618     return -1;
1619   }
1620   llvm_unreachable("Invalid boolean contents");
1621 }
1622 
1623 void llvm::saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI,
1624                             LostDebugLocObserver *LocObserver,
1625                             SmallInstListTy &DeadInstChain) {
1626   for (MachineOperand &Op : MI.uses()) {
1627     if (Op.isReg() && Op.getReg().isVirtual())
1628       DeadInstChain.insert(MRI.getVRegDef(Op.getReg()));
1629   }
1630   LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
1631   DeadInstChain.remove(&MI);
1632   MI.eraseFromParent();
1633   if (LocObserver)
1634     LocObserver->checkpoint(false);
1635 }
1636 
1637 void llvm::eraseInstrs(ArrayRef<MachineInstr *> DeadInstrs,
1638                        MachineRegisterInfo &MRI,
1639                        LostDebugLocObserver *LocObserver) {
1640   SmallInstListTy DeadInstChain;
1641   for (MachineInstr *MI : DeadInstrs)
1642     saveUsesAndErase(*MI, MRI, LocObserver, DeadInstChain);
1643 
1644   while (!DeadInstChain.empty()) {
1645     MachineInstr *Inst = DeadInstChain.pop_back_val();
1646     if (!isTriviallyDead(*Inst, MRI))
1647       continue;
1648     saveUsesAndErase(*Inst, MRI, LocObserver, DeadInstChain);
1649   }
1650 }
1651 
1652 void llvm::eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI,
1653                       LostDebugLocObserver *LocObserver) {
1654   return eraseInstrs({&MI}, MRI, LocObserver);
1655 }
1656 
1657 void llvm::salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI) {
1658   for (auto &Def : MI.defs()) {
1659     assert(Def.isReg() && "Must be a reg");
1660 
1661     SmallVector<MachineOperand *, 16> DbgUsers;
1662     for (auto &MOUse : MRI.use_operands(Def.getReg())) {
1663       MachineInstr *DbgValue = MOUse.getParent();
1664       // Ignore partially formed DBG_VALUEs.
1665       if (DbgValue->isNonListDebugValue() && DbgValue->getNumOperands() == 4) {
1666         DbgUsers.push_back(&MOUse);
1667       }
1668     }
1669 
1670     if (!DbgUsers.empty()) {
1671       salvageDebugInfoForDbgValue(MRI, MI, DbgUsers);
1672     }
1673   }
1674 }
1675 
1676 bool llvm::isPreISelGenericFloatingPointOpcode(unsigned Opc) {
1677   switch (Opc) {
1678   case TargetOpcode::G_FABS:
1679   case TargetOpcode::G_FADD:
1680   case TargetOpcode::G_FCANONICALIZE:
1681   case TargetOpcode::G_FCEIL:
1682   case TargetOpcode::G_FCONSTANT:
1683   case TargetOpcode::G_FCOPYSIGN:
1684   case TargetOpcode::G_FCOS:
1685   case TargetOpcode::G_FDIV:
1686   case TargetOpcode::G_FEXP2:
1687   case TargetOpcode::G_FEXP:
1688   case TargetOpcode::G_FFLOOR:
1689   case TargetOpcode::G_FLOG10:
1690   case TargetOpcode::G_FLOG2:
1691   case TargetOpcode::G_FLOG:
1692   case TargetOpcode::G_FMA:
1693   case TargetOpcode::G_FMAD:
1694   case TargetOpcode::G_FMAXIMUM:
1695   case TargetOpcode::G_FMAXNUM:
1696   case TargetOpcode::G_FMAXNUM_IEEE:
1697   case TargetOpcode::G_FMINIMUM:
1698   case TargetOpcode::G_FMINNUM:
1699   case TargetOpcode::G_FMINNUM_IEEE:
1700   case TargetOpcode::G_FMUL:
1701   case TargetOpcode::G_FNEARBYINT:
1702   case TargetOpcode::G_FNEG:
1703   case TargetOpcode::G_FPEXT:
1704   case TargetOpcode::G_FPOW:
1705   case TargetOpcode::G_FPTRUNC:
1706   case TargetOpcode::G_FREM:
1707   case TargetOpcode::G_FRINT:
1708   case TargetOpcode::G_FSIN:
1709   case TargetOpcode::G_FTAN:
1710   case TargetOpcode::G_FACOS:
1711   case TargetOpcode::G_FASIN:
1712   case TargetOpcode::G_FATAN:
1713   case TargetOpcode::G_FATAN2:
1714   case TargetOpcode::G_FCOSH:
1715   case TargetOpcode::G_FSINH:
1716   case TargetOpcode::G_FTANH:
1717   case TargetOpcode::G_FSQRT:
1718   case TargetOpcode::G_FSUB:
1719   case TargetOpcode::G_INTRINSIC_ROUND:
1720   case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1721   case TargetOpcode::G_INTRINSIC_TRUNC:
1722     return true;
1723   default:
1724     return false;
1725   }
1726 }
1727 
1728 /// Shifts return poison if shiftwidth is larger than the bitwidth.
1729 static bool shiftAmountKnownInRange(Register ShiftAmount,
1730                                     const MachineRegisterInfo &MRI) {
1731   LLT Ty = MRI.getType(ShiftAmount);
1732 
1733   if (Ty.isScalableVector())
1734     return false; // Can't tell, just return false to be safe
1735 
1736   if (Ty.isScalar()) {
1737     std::optional<ValueAndVReg> Val =
1738         getIConstantVRegValWithLookThrough(ShiftAmount, MRI);
1739     if (!Val)
1740       return false;
1741     return Val->Value.ult(Ty.getScalarSizeInBits());
1742   }
1743 
1744   GBuildVector *BV = getOpcodeDef<GBuildVector>(ShiftAmount, MRI);
1745   if (!BV)
1746     return false;
1747 
1748   unsigned Sources = BV->getNumSources();
1749   for (unsigned I = 0; I < Sources; ++I) {
1750     std::optional<ValueAndVReg> Val =
1751         getIConstantVRegValWithLookThrough(BV->getSourceReg(I), MRI);
1752     if (!Val)
1753       return false;
1754     if (!Val->Value.ult(Ty.getScalarSizeInBits()))
1755       return false;
1756   }
1757 
1758   return true;
1759 }
1760 
1761 namespace {
1762 enum class UndefPoisonKind {
1763   PoisonOnly = (1 << 0),
1764   UndefOnly = (1 << 1),
1765   UndefOrPoison = PoisonOnly | UndefOnly,
1766 };
1767 }
1768 
1769 static bool includesPoison(UndefPoisonKind Kind) {
1770   return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
1771 }
1772 
1773 static bool includesUndef(UndefPoisonKind Kind) {
1774   return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
1775 }
1776 
1777 static bool canCreateUndefOrPoison(Register Reg, const MachineRegisterInfo &MRI,
1778                                    bool ConsiderFlagsAndMetadata,
1779                                    UndefPoisonKind Kind) {
1780   MachineInstr *RegDef = MRI.getVRegDef(Reg);
1781 
1782   if (ConsiderFlagsAndMetadata && includesPoison(Kind))
1783     if (auto *GMI = dyn_cast<GenericMachineInstr>(RegDef))
1784       if (GMI->hasPoisonGeneratingFlags())
1785         return true;
1786 
1787   // Check whether opcode is a poison/undef-generating operation.
1788   switch (RegDef->getOpcode()) {
1789   case TargetOpcode::G_BUILD_VECTOR:
1790   case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
1791     return false;
1792   case TargetOpcode::G_SHL:
1793   case TargetOpcode::G_ASHR:
1794   case TargetOpcode::G_LSHR:
1795     return includesPoison(Kind) &&
1796            !shiftAmountKnownInRange(RegDef->getOperand(2).getReg(), MRI);
1797   case TargetOpcode::G_FPTOSI:
1798   case TargetOpcode::G_FPTOUI:
1799     // fptosi/ui yields poison if the resulting value does not fit in the
1800     // destination type.
1801     return true;
1802   case TargetOpcode::G_CTLZ:
1803   case TargetOpcode::G_CTTZ:
1804   case TargetOpcode::G_ABS:
1805   case TargetOpcode::G_CTPOP:
1806   case TargetOpcode::G_BSWAP:
1807   case TargetOpcode::G_BITREVERSE:
1808   case TargetOpcode::G_FSHL:
1809   case TargetOpcode::G_FSHR:
1810   case TargetOpcode::G_SMAX:
1811   case TargetOpcode::G_SMIN:
1812   case TargetOpcode::G_UMAX:
1813   case TargetOpcode::G_UMIN:
1814   case TargetOpcode::G_PTRMASK:
1815   case TargetOpcode::G_SADDO:
1816   case TargetOpcode::G_SSUBO:
1817   case TargetOpcode::G_UADDO:
1818   case TargetOpcode::G_USUBO:
1819   case TargetOpcode::G_SMULO:
1820   case TargetOpcode::G_UMULO:
1821   case TargetOpcode::G_SADDSAT:
1822   case TargetOpcode::G_UADDSAT:
1823   case TargetOpcode::G_SSUBSAT:
1824   case TargetOpcode::G_USUBSAT:
1825     return false;
1826   case TargetOpcode::G_SSHLSAT:
1827   case TargetOpcode::G_USHLSAT:
1828     return includesPoison(Kind) &&
1829            !shiftAmountKnownInRange(RegDef->getOperand(2).getReg(), MRI);
1830   case TargetOpcode::G_INSERT_VECTOR_ELT: {
1831     GInsertVectorElement *Insert = cast<GInsertVectorElement>(RegDef);
1832     if (includesPoison(Kind)) {
1833       std::optional<ValueAndVReg> Index =
1834           getIConstantVRegValWithLookThrough(Insert->getIndexReg(), MRI);
1835       if (!Index)
1836         return true;
1837       LLT VecTy = MRI.getType(Insert->getVectorReg());
1838       return Index->Value.uge(VecTy.getElementCount().getKnownMinValue());
1839     }
1840     return false;
1841   }
1842   case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1843     GExtractVectorElement *Extract = cast<GExtractVectorElement>(RegDef);
1844     if (includesPoison(Kind)) {
1845       std::optional<ValueAndVReg> Index =
1846           getIConstantVRegValWithLookThrough(Extract->getIndexReg(), MRI);
1847       if (!Index)
1848         return true;
1849       LLT VecTy = MRI.getType(Extract->getVectorReg());
1850       return Index->Value.uge(VecTy.getElementCount().getKnownMinValue());
1851     }
1852     return false;
1853   }
1854   case TargetOpcode::G_SHUFFLE_VECTOR: {
1855     GShuffleVector *Shuffle = cast<GShuffleVector>(RegDef);
1856     ArrayRef<int> Mask = Shuffle->getMask();
1857     return includesPoison(Kind) && is_contained(Mask, -1);
1858   }
1859   case TargetOpcode::G_FNEG:
1860   case TargetOpcode::G_PHI:
1861   case TargetOpcode::G_SELECT:
1862   case TargetOpcode::G_UREM:
1863   case TargetOpcode::G_SREM:
1864   case TargetOpcode::G_FREEZE:
1865   case TargetOpcode::G_ICMP:
1866   case TargetOpcode::G_FCMP:
1867   case TargetOpcode::G_FADD:
1868   case TargetOpcode::G_FSUB:
1869   case TargetOpcode::G_FMUL:
1870   case TargetOpcode::G_FDIV:
1871   case TargetOpcode::G_FREM:
1872   case TargetOpcode::G_PTR_ADD:
1873     return false;
1874   default:
1875     return !isa<GCastOp>(RegDef) && !isa<GBinOp>(RegDef);
1876   }
1877 }
1878 
1879 static bool isGuaranteedNotToBeUndefOrPoison(Register Reg,
1880                                              const MachineRegisterInfo &MRI,
1881                                              unsigned Depth,
1882                                              UndefPoisonKind Kind) {
1883   if (Depth >= MaxAnalysisRecursionDepth)
1884     return false;
1885 
1886   MachineInstr *RegDef = MRI.getVRegDef(Reg);
1887 
1888   switch (RegDef->getOpcode()) {
1889   case TargetOpcode::G_FREEZE:
1890     return true;
1891   case TargetOpcode::G_IMPLICIT_DEF:
1892     return !includesUndef(Kind);
1893   case TargetOpcode::G_CONSTANT:
1894   case TargetOpcode::G_FCONSTANT:
1895     return true;
1896   case TargetOpcode::G_BUILD_VECTOR: {
1897     GBuildVector *BV = cast<GBuildVector>(RegDef);
1898     unsigned NumSources = BV->getNumSources();
1899     for (unsigned I = 0; I < NumSources; ++I)
1900       if (!::isGuaranteedNotToBeUndefOrPoison(BV->getSourceReg(I), MRI,
1901                                               Depth + 1, Kind))
1902         return false;
1903     return true;
1904   }
1905   case TargetOpcode::G_PHI: {
1906     GPhi *Phi = cast<GPhi>(RegDef);
1907     unsigned NumIncoming = Phi->getNumIncomingValues();
1908     for (unsigned I = 0; I < NumIncoming; ++I)
1909       if (!::isGuaranteedNotToBeUndefOrPoison(Phi->getIncomingValue(I), MRI,
1910                                               Depth + 1, Kind))
1911         return false;
1912     return true;
1913   }
1914   default: {
1915     auto MOCheck = [&](const MachineOperand &MO) {
1916       if (!MO.isReg())
1917         return true;
1918       return ::isGuaranteedNotToBeUndefOrPoison(MO.getReg(), MRI, Depth + 1,
1919                                                 Kind);
1920     };
1921     return !::canCreateUndefOrPoison(Reg, MRI,
1922                                      /*ConsiderFlagsAndMetadata=*/true, Kind) &&
1923            all_of(RegDef->uses(), MOCheck);
1924   }
1925   }
1926 }
1927 
1928 bool llvm::canCreateUndefOrPoison(Register Reg, const MachineRegisterInfo &MRI,
1929                                   bool ConsiderFlagsAndMetadata) {
1930   return ::canCreateUndefOrPoison(Reg, MRI, ConsiderFlagsAndMetadata,
1931                                   UndefPoisonKind::UndefOrPoison);
1932 }
1933 
1934 bool canCreatePoison(Register Reg, const MachineRegisterInfo &MRI,
1935                      bool ConsiderFlagsAndMetadata = true) {
1936   return ::canCreateUndefOrPoison(Reg, MRI, ConsiderFlagsAndMetadata,
1937                                   UndefPoisonKind::PoisonOnly);
1938 }
1939 
1940 bool llvm::isGuaranteedNotToBeUndefOrPoison(Register Reg,
1941                                             const MachineRegisterInfo &MRI,
1942                                             unsigned Depth) {
1943   return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1944                                             UndefPoisonKind::UndefOrPoison);
1945 }
1946 
1947 bool llvm::isGuaranteedNotToBePoison(Register Reg,
1948                                      const MachineRegisterInfo &MRI,
1949                                      unsigned Depth) {
1950   return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1951                                             UndefPoisonKind::PoisonOnly);
1952 }
1953 
1954 bool llvm::isGuaranteedNotToBeUndef(Register Reg,
1955                                     const MachineRegisterInfo &MRI,
1956                                     unsigned Depth) {
1957   return ::isGuaranteedNotToBeUndefOrPoison(Reg, MRI, Depth,
1958                                             UndefPoisonKind::UndefOnly);
1959 }
1960 
1961 Type *llvm::getTypeForLLT(LLT Ty, LLVMContext &C) {
1962   if (Ty.isVector())
1963     return VectorType::get(IntegerType::get(C, Ty.getScalarSizeInBits()),
1964                            Ty.getElementCount());
1965   return IntegerType::get(C, Ty.getSizeInBits());
1966 }
1967 
1968 APInt llvm::GIConstant::getScalarValue() const {
1969   assert(Kind == GIConstantKind::Scalar && "Expected scalar constant");
1970 
1971   return Value;
1972 }
1973 
1974 std::optional<GIConstant>
1975 llvm::GIConstant::getConstant(Register Const, const MachineRegisterInfo &MRI) {
1976   MachineInstr *Constant = getDefIgnoringCopies(Const, MRI);
1977 
1978   if (GSplatVector *Splat = dyn_cast<GSplatVector>(Constant)) {
1979     std::optional<ValueAndVReg> MayBeConstant =
1980         getIConstantVRegValWithLookThrough(Splat->getScalarReg(), MRI);
1981     if (!MayBeConstant)
1982       return std::nullopt;
1983     return GIConstant(MayBeConstant->Value, GIConstantKind::ScalableVector);
1984   }
1985 
1986   if (GBuildVector *Build = dyn_cast<GBuildVector>(Constant)) {
1987     SmallVector<APInt> Values;
1988     unsigned NumSources = Build->getNumSources();
1989     for (unsigned I = 0; I < NumSources; ++I) {
1990       Register SrcReg = Build->getSourceReg(I);
1991       std::optional<ValueAndVReg> MayBeConstant =
1992           getIConstantVRegValWithLookThrough(SrcReg, MRI);
1993       if (!MayBeConstant)
1994         return std::nullopt;
1995       Values.push_back(MayBeConstant->Value);
1996     }
1997     return GIConstant(Values);
1998   }
1999 
2000   std::optional<ValueAndVReg> MayBeConstant =
2001       getIConstantVRegValWithLookThrough(Const, MRI);
2002   if (!MayBeConstant)
2003     return std::nullopt;
2004 
2005   return GIConstant(MayBeConstant->Value, GIConstantKind::Scalar);
2006 }
2007 
2008 APFloat llvm::GFConstant::getScalarValue() const {
2009   assert(Kind == GFConstantKind::Scalar && "Expected scalar constant");
2010 
2011   return Values[0];
2012 }
2013 
2014 std::optional<GFConstant>
2015 llvm::GFConstant::getConstant(Register Const, const MachineRegisterInfo &MRI) {
2016   MachineInstr *Constant = getDefIgnoringCopies(Const, MRI);
2017 
2018   if (GSplatVector *Splat = dyn_cast<GSplatVector>(Constant)) {
2019     std::optional<FPValueAndVReg> MayBeConstant =
2020         getFConstantVRegValWithLookThrough(Splat->getScalarReg(), MRI);
2021     if (!MayBeConstant)
2022       return std::nullopt;
2023     return GFConstant(MayBeConstant->Value, GFConstantKind::ScalableVector);
2024   }
2025 
2026   if (GBuildVector *Build = dyn_cast<GBuildVector>(Constant)) {
2027     SmallVector<APFloat> Values;
2028     unsigned NumSources = Build->getNumSources();
2029     for (unsigned I = 0; I < NumSources; ++I) {
2030       Register SrcReg = Build->getSourceReg(I);
2031       std::optional<FPValueAndVReg> MayBeConstant =
2032           getFConstantVRegValWithLookThrough(SrcReg, MRI);
2033       if (!MayBeConstant)
2034         return std::nullopt;
2035       Values.push_back(MayBeConstant->Value);
2036     }
2037     return GFConstant(Values);
2038   }
2039 
2040   std::optional<FPValueAndVReg> MayBeConstant =
2041       getFConstantVRegValWithLookThrough(Const, MRI);
2042   if (!MayBeConstant)
2043     return std::nullopt;
2044 
2045   return GFConstant(MayBeConstant->Value, GFConstantKind::Scalar);
2046 }
2047