xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp (revision 6eb0b0a0452a4c53ceb1156afd17764fbff51111)
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/CodeGen/CodeGenCommonISel.h"
16 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
17 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
18 #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
19 #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h"
20 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/MachineSizeOpts.h"
26 #include "llvm/CodeGen/RegisterBankInfo.h"
27 #include "llvm/CodeGen/StackProtector.h"
28 #include "llvm/CodeGen/TargetInstrInfo.h"
29 #include "llvm/CodeGen/TargetLowering.h"
30 #include "llvm/CodeGen/TargetPassConfig.h"
31 #include "llvm/CodeGen/TargetRegisterInfo.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Transforms/Utils/SizeOpts.h"
35 #include <numeric>
36 #include <optional>
37 
38 #define DEBUG_TYPE "globalisel-utils"
39 
40 using namespace llvm;
41 using namespace MIPatternMatch;
42 
43 Register llvm::constrainRegToClass(MachineRegisterInfo &MRI,
44                                    const TargetInstrInfo &TII,
45                                    const RegisterBankInfo &RBI, Register Reg,
46                                    const TargetRegisterClass &RegClass) {
47   if (!RBI.constrainGenericRegister(Reg, RegClass, MRI))
48     return MRI.createVirtualRegister(&RegClass);
49 
50   return Reg;
51 }
52 
53 Register llvm::constrainOperandRegClass(
54     const MachineFunction &MF, const TargetRegisterInfo &TRI,
55     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
56     const RegisterBankInfo &RBI, MachineInstr &InsertPt,
57     const TargetRegisterClass &RegClass, MachineOperand &RegMO) {
58   Register Reg = RegMO.getReg();
59   // Assume physical registers are properly constrained.
60   assert(Register::isVirtualRegister(Reg) && "PhysReg not implemented");
61 
62   // Save the old register class to check whether
63   // the change notifications will be required.
64   // TODO: A better approach would be to pass
65   // the observers to constrainRegToClass().
66   auto *OldRegClass = MRI.getRegClassOrNull(Reg);
67   Register ConstrainedReg = constrainRegToClass(MRI, TII, RBI, Reg, RegClass);
68   // If we created a new virtual register because the class is not compatible
69   // then create a copy between the new and the old register.
70   if (ConstrainedReg != Reg) {
71     MachineBasicBlock::iterator InsertIt(&InsertPt);
72     MachineBasicBlock &MBB = *InsertPt.getParent();
73     // FIXME: The copy needs to have the classes constrained for its operands.
74     // Use operand's regbank to get the class for old register (Reg).
75     if (RegMO.isUse()) {
76       BuildMI(MBB, InsertIt, InsertPt.getDebugLoc(),
77               TII.get(TargetOpcode::COPY), ConstrainedReg)
78           .addReg(Reg);
79     } else {
80       assert(RegMO.isDef() && "Must be a definition");
81       BuildMI(MBB, std::next(InsertIt), InsertPt.getDebugLoc(),
82               TII.get(TargetOpcode::COPY), Reg)
83           .addReg(ConstrainedReg);
84     }
85     if (GISelChangeObserver *Observer = MF.getObserver()) {
86       Observer->changingInstr(*RegMO.getParent());
87     }
88     RegMO.setReg(ConstrainedReg);
89     if (GISelChangeObserver *Observer = MF.getObserver()) {
90       Observer->changedInstr(*RegMO.getParent());
91     }
92   } else if (OldRegClass != MRI.getRegClassOrNull(Reg)) {
93     if (GISelChangeObserver *Observer = MF.getObserver()) {
94       if (!RegMO.isDef()) {
95         MachineInstr *RegDef = MRI.getVRegDef(Reg);
96         Observer->changedInstr(*RegDef);
97       }
98       Observer->changingAllUsesOfReg(MRI, Reg);
99       Observer->finishedChangingAllUsesOfReg();
100     }
101   }
102   return ConstrainedReg;
103 }
104 
105 Register llvm::constrainOperandRegClass(
106     const MachineFunction &MF, const TargetRegisterInfo &TRI,
107     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
108     const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
109     MachineOperand &RegMO, unsigned OpIdx) {
110   Register Reg = RegMO.getReg();
111   // Assume physical registers are properly constrained.
112   assert(Register::isVirtualRegister(Reg) && "PhysReg not implemented");
113 
114   const TargetRegisterClass *OpRC = TII.getRegClass(II, OpIdx, &TRI, MF);
115   // Some of the target independent instructions, like COPY, may not impose any
116   // register class constraints on some of their operands: If it's a use, we can
117   // skip constraining as the instruction defining the register would constrain
118   // it.
119 
120   if (OpRC) {
121     // Obtain the RC from incoming regbank if it is a proper sub-class. Operands
122     // can have multiple regbanks for a superclass that combine different
123     // register types (E.g., AMDGPU's VGPR and AGPR). The regbank ambiguity
124     // resolved by targets during regbankselect should not be overridden.
125     if (const auto *SubRC = TRI.getCommonSubClass(
126             OpRC, TRI.getConstrainedRegClassForOperand(RegMO, MRI)))
127       OpRC = SubRC;
128 
129     OpRC = TRI.getAllocatableClass(OpRC);
130   }
131 
132   if (!OpRC) {
133     assert((!isTargetSpecificOpcode(II.getOpcode()) || RegMO.isUse()) &&
134            "Register class constraint is required unless either the "
135            "instruction is target independent or the operand is a use");
136     // FIXME: Just bailing out like this here could be not enough, unless we
137     // expect the users of this function to do the right thing for PHIs and
138     // COPY:
139     //   v1 = COPY v0
140     //   v2 = COPY v1
141     // v1 here may end up not being constrained at all. Please notice that to
142     // reproduce the issue we likely need a destination pattern of a selection
143     // rule producing such extra copies, not just an input GMIR with them as
144     // every existing target using selectImpl handles copies before calling it
145     // and they never reach this function.
146     return Reg;
147   }
148   return constrainOperandRegClass(MF, TRI, MRI, TII, RBI, InsertPt, *OpRC,
149                                   RegMO);
150 }
151 
152 bool llvm::constrainSelectedInstRegOperands(MachineInstr &I,
153                                             const TargetInstrInfo &TII,
154                                             const TargetRegisterInfo &TRI,
155                                             const RegisterBankInfo &RBI) {
156   assert(!isPreISelGenericOpcode(I.getOpcode()) &&
157          "A selected instruction is expected");
158   MachineBasicBlock &MBB = *I.getParent();
159   MachineFunction &MF = *MBB.getParent();
160   MachineRegisterInfo &MRI = MF.getRegInfo();
161 
162   for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
163     MachineOperand &MO = I.getOperand(OpI);
164 
165     // There's nothing to be done on non-register operands.
166     if (!MO.isReg())
167       continue;
168 
169     LLVM_DEBUG(dbgs() << "Converting operand: " << MO << '\n');
170     assert(MO.isReg() && "Unsupported non-reg operand");
171 
172     Register Reg = MO.getReg();
173     // Physical registers don't need to be constrained.
174     if (Register::isPhysicalRegister(Reg))
175       continue;
176 
177     // Register operands with a value of 0 (e.g. predicate operands) don't need
178     // to be constrained.
179     if (Reg == 0)
180       continue;
181 
182     // If the operand is a vreg, we should constrain its regclass, and only
183     // insert COPYs if that's impossible.
184     // constrainOperandRegClass does that for us.
185     constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), MO, OpI);
186 
187     // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
188     // done.
189     if (MO.isUse()) {
190       int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
191       if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
192         I.tieOperands(DefIdx, OpI);
193     }
194   }
195   return true;
196 }
197 
198 bool llvm::canReplaceReg(Register DstReg, Register SrcReg,
199                          MachineRegisterInfo &MRI) {
200   // Give up if either DstReg or SrcReg  is a physical register.
201   if (DstReg.isPhysical() || SrcReg.isPhysical())
202     return false;
203   // Give up if the types don't match.
204   if (MRI.getType(DstReg) != MRI.getType(SrcReg))
205     return false;
206   // Replace if either DstReg has no constraints or the register
207   // constraints match.
208   return !MRI.getRegClassOrRegBank(DstReg) ||
209          MRI.getRegClassOrRegBank(DstReg) == MRI.getRegClassOrRegBank(SrcReg);
210 }
211 
212 bool llvm::isTriviallyDead(const MachineInstr &MI,
213                            const MachineRegisterInfo &MRI) {
214   // FIXME: This logical is mostly duplicated with
215   // DeadMachineInstructionElim::isDead. Why is LOCAL_ESCAPE not considered in
216   // MachineInstr::isLabel?
217 
218   // Don't delete frame allocation labels.
219   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE)
220     return false;
221   // LIFETIME markers should be preserved even if they seem dead.
222   if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
223       MI.getOpcode() == TargetOpcode::LIFETIME_END)
224     return false;
225 
226   // If we can move an instruction, we can remove it.  Otherwise, it has
227   // a side-effect of some sort.
228   bool SawStore = false;
229   if (!MI.isSafeToMove(/*AA=*/nullptr, SawStore) && !MI.isPHI())
230     return false;
231 
232   // Instructions without side-effects are dead iff they only define dead vregs.
233   for (const auto &MO : MI.operands()) {
234     if (!MO.isReg() || !MO.isDef())
235       continue;
236 
237     Register Reg = MO.getReg();
238     if (Register::isPhysicalRegister(Reg) || !MRI.use_nodbg_empty(Reg))
239       return false;
240   }
241   return true;
242 }
243 
244 static void reportGISelDiagnostic(DiagnosticSeverity Severity,
245                                   MachineFunction &MF,
246                                   const TargetPassConfig &TPC,
247                                   MachineOptimizationRemarkEmitter &MORE,
248                                   MachineOptimizationRemarkMissed &R) {
249   bool IsFatal = Severity == DS_Error &&
250                  TPC.isGlobalISelAbortEnabled();
251   // Print the function name explicitly if we don't have a debug location (which
252   // makes the diagnostic less useful) or if we're going to emit a raw error.
253   if (!R.getLocation().isValid() || IsFatal)
254     R << (" (in function: " + MF.getName() + ")").str();
255 
256   if (IsFatal)
257     report_fatal_error(Twine(R.getMsg()));
258   else
259     MORE.emit(R);
260 }
261 
262 void llvm::reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
263                               MachineOptimizationRemarkEmitter &MORE,
264                               MachineOptimizationRemarkMissed &R) {
265   reportGISelDiagnostic(DS_Warning, MF, TPC, MORE, R);
266 }
267 
268 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
269                               MachineOptimizationRemarkEmitter &MORE,
270                               MachineOptimizationRemarkMissed &R) {
271   MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
272   reportGISelDiagnostic(DS_Error, MF, TPC, MORE, R);
273 }
274 
275 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
276                               MachineOptimizationRemarkEmitter &MORE,
277                               const char *PassName, StringRef Msg,
278                               const MachineInstr &MI) {
279   MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
280                                     MI.getDebugLoc(), MI.getParent());
281   R << Msg;
282   // Printing MI is expensive;  only do it if expensive remarks are enabled.
283   if (TPC.isGlobalISelAbortEnabled() || MORE.allowExtraAnalysis(PassName))
284     R << ": " << ore::MNV("Inst", MI);
285   reportGISelFailure(MF, TPC, MORE, R);
286 }
287 
288 std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
289                                                const MachineRegisterInfo &MRI) {
290   std::optional<ValueAndVReg> ValAndVReg = getIConstantVRegValWithLookThrough(
291       VReg, MRI, /*LookThroughInstrs*/ false);
292   assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
293          "Value found while looking through instrs");
294   if (!ValAndVReg)
295     return std::nullopt;
296   return ValAndVReg->Value;
297 }
298 
299 std::optional<int64_t>
300 llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI) {
301   std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
302   if (Val && Val->getBitWidth() <= 64)
303     return Val->getSExtValue();
304   return std::nullopt;
305 }
306 
307 namespace {
308 
309 typedef std::function<bool(const MachineInstr *)> IsOpcodeFn;
310 typedef std::function<std::optional<APInt>(const MachineInstr *MI)> GetAPCstFn;
311 
312 std::optional<ValueAndVReg> getConstantVRegValWithLookThrough(
313     Register VReg, const MachineRegisterInfo &MRI, IsOpcodeFn IsConstantOpcode,
314     GetAPCstFn getAPCstValue, bool LookThroughInstrs = true,
315     bool LookThroughAnyExt = false) {
316   SmallVector<std::pair<unsigned, unsigned>, 4> SeenOpcodes;
317   MachineInstr *MI;
318 
319   while ((MI = MRI.getVRegDef(VReg)) && !IsConstantOpcode(MI) &&
320          LookThroughInstrs) {
321     switch (MI->getOpcode()) {
322     case TargetOpcode::G_ANYEXT:
323       if (!LookThroughAnyExt)
324         return std::nullopt;
325       [[fallthrough]];
326     case TargetOpcode::G_TRUNC:
327     case TargetOpcode::G_SEXT:
328     case TargetOpcode::G_ZEXT:
329       SeenOpcodes.push_back(std::make_pair(
330           MI->getOpcode(),
331           MRI.getType(MI->getOperand(0).getReg()).getSizeInBits()));
332       VReg = MI->getOperand(1).getReg();
333       break;
334     case TargetOpcode::COPY:
335       VReg = MI->getOperand(1).getReg();
336       if (Register::isPhysicalRegister(VReg))
337         return std::nullopt;
338       break;
339     case TargetOpcode::G_INTTOPTR:
340       VReg = MI->getOperand(1).getReg();
341       break;
342     default:
343       return std::nullopt;
344     }
345   }
346   if (!MI || !IsConstantOpcode(MI))
347     return std::nullopt;
348 
349   std::optional<APInt> MaybeVal = getAPCstValue(MI);
350   if (!MaybeVal)
351     return std::nullopt;
352   APInt &Val = *MaybeVal;
353   while (!SeenOpcodes.empty()) {
354     std::pair<unsigned, unsigned> OpcodeAndSize = SeenOpcodes.pop_back_val();
355     switch (OpcodeAndSize.first) {
356     case TargetOpcode::G_TRUNC:
357       Val = Val.trunc(OpcodeAndSize.second);
358       break;
359     case TargetOpcode::G_ANYEXT:
360     case TargetOpcode::G_SEXT:
361       Val = Val.sext(OpcodeAndSize.second);
362       break;
363     case TargetOpcode::G_ZEXT:
364       Val = Val.zext(OpcodeAndSize.second);
365       break;
366     }
367   }
368 
369   return ValueAndVReg{Val, VReg};
370 }
371 
372 bool isIConstant(const MachineInstr *MI) {
373   if (!MI)
374     return false;
375   return MI->getOpcode() == TargetOpcode::G_CONSTANT;
376 }
377 
378 bool isFConstant(const MachineInstr *MI) {
379   if (!MI)
380     return false;
381   return MI->getOpcode() == TargetOpcode::G_FCONSTANT;
382 }
383 
384 bool isAnyConstant(const MachineInstr *MI) {
385   if (!MI)
386     return false;
387   unsigned Opc = MI->getOpcode();
388   return Opc == TargetOpcode::G_CONSTANT || Opc == TargetOpcode::G_FCONSTANT;
389 }
390 
391 std::optional<APInt> getCImmAsAPInt(const MachineInstr *MI) {
392   const MachineOperand &CstVal = MI->getOperand(1);
393   if (CstVal.isCImm())
394     return CstVal.getCImm()->getValue();
395   return std::nullopt;
396 }
397 
398 std::optional<APInt> getCImmOrFPImmAsAPInt(const MachineInstr *MI) {
399   const MachineOperand &CstVal = MI->getOperand(1);
400   if (CstVal.isCImm())
401     return CstVal.getCImm()->getValue();
402   if (CstVal.isFPImm())
403     return CstVal.getFPImm()->getValueAPF().bitcastToAPInt();
404   return std::nullopt;
405 }
406 
407 } // end anonymous namespace
408 
409 std::optional<ValueAndVReg> llvm::getIConstantVRegValWithLookThrough(
410     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
411   return getConstantVRegValWithLookThrough(VReg, MRI, isIConstant,
412                                            getCImmAsAPInt, LookThroughInstrs);
413 }
414 
415 std::optional<ValueAndVReg> llvm::getAnyConstantVRegValWithLookThrough(
416     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs,
417     bool LookThroughAnyExt) {
418   return getConstantVRegValWithLookThrough(
419       VReg, MRI, isAnyConstant, getCImmOrFPImmAsAPInt, LookThroughInstrs,
420       LookThroughAnyExt);
421 }
422 
423 std::optional<FPValueAndVReg> llvm::getFConstantVRegValWithLookThrough(
424     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
425   auto Reg = getConstantVRegValWithLookThrough(
426       VReg, MRI, isFConstant, getCImmOrFPImmAsAPInt, LookThroughInstrs);
427   if (!Reg)
428     return std::nullopt;
429   return FPValueAndVReg{getConstantFPVRegVal(Reg->VReg, MRI)->getValueAPF(),
430                         Reg->VReg};
431 }
432 
433 const ConstantFP *
434 llvm::getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI) {
435   MachineInstr *MI = MRI.getVRegDef(VReg);
436   if (TargetOpcode::G_FCONSTANT != MI->getOpcode())
437     return nullptr;
438   return MI->getOperand(1).getFPImm();
439 }
440 
441 std::optional<DefinitionAndSourceRegister>
442 llvm::getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI) {
443   Register DefSrcReg = Reg;
444   auto *DefMI = MRI.getVRegDef(Reg);
445   auto DstTy = MRI.getType(DefMI->getOperand(0).getReg());
446   if (!DstTy.isValid())
447     return std::nullopt;
448   unsigned Opc = DefMI->getOpcode();
449   while (Opc == TargetOpcode::COPY || isPreISelGenericOptimizationHint(Opc)) {
450     Register SrcReg = DefMI->getOperand(1).getReg();
451     auto SrcTy = MRI.getType(SrcReg);
452     if (!SrcTy.isValid())
453       break;
454     DefMI = MRI.getVRegDef(SrcReg);
455     DefSrcReg = SrcReg;
456     Opc = DefMI->getOpcode();
457   }
458   return DefinitionAndSourceRegister{DefMI, DefSrcReg};
459 }
460 
461 MachineInstr *llvm::getDefIgnoringCopies(Register Reg,
462                                          const MachineRegisterInfo &MRI) {
463   std::optional<DefinitionAndSourceRegister> DefSrcReg =
464       getDefSrcRegIgnoringCopies(Reg, MRI);
465   return DefSrcReg ? DefSrcReg->MI : nullptr;
466 }
467 
468 Register llvm::getSrcRegIgnoringCopies(Register Reg,
469                                        const MachineRegisterInfo &MRI) {
470   std::optional<DefinitionAndSourceRegister> DefSrcReg =
471       getDefSrcRegIgnoringCopies(Reg, MRI);
472   return DefSrcReg ? DefSrcReg->Reg : Register();
473 }
474 
475 MachineInstr *llvm::getOpcodeDef(unsigned Opcode, Register Reg,
476                                  const MachineRegisterInfo &MRI) {
477   MachineInstr *DefMI = getDefIgnoringCopies(Reg, MRI);
478   return DefMI && DefMI->getOpcode() == Opcode ? DefMI : nullptr;
479 }
480 
481 APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
482   if (Size == 32)
483     return APFloat(float(Val));
484   if (Size == 64)
485     return APFloat(Val);
486   if (Size != 16)
487     llvm_unreachable("Unsupported FPConstant size");
488   bool Ignored;
489   APFloat APF(Val);
490   APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
491   return APF;
492 }
493 
494 std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
495                                              const Register Op1,
496                                              const Register Op2,
497                                              const MachineRegisterInfo &MRI) {
498   auto MaybeOp2Cst = getAnyConstantVRegValWithLookThrough(Op2, MRI, false);
499   if (!MaybeOp2Cst)
500     return std::nullopt;
501 
502   auto MaybeOp1Cst = getAnyConstantVRegValWithLookThrough(Op1, MRI, false);
503   if (!MaybeOp1Cst)
504     return std::nullopt;
505 
506   const APInt &C1 = MaybeOp1Cst->Value;
507   const APInt &C2 = MaybeOp2Cst->Value;
508   switch (Opcode) {
509   default:
510     break;
511   case TargetOpcode::G_ADD:
512   case TargetOpcode::G_PTR_ADD:
513     return C1 + C2;
514   case TargetOpcode::G_AND:
515     return C1 & C2;
516   case TargetOpcode::G_ASHR:
517     return C1.ashr(C2);
518   case TargetOpcode::G_LSHR:
519     return C1.lshr(C2);
520   case TargetOpcode::G_MUL:
521     return C1 * C2;
522   case TargetOpcode::G_OR:
523     return C1 | C2;
524   case TargetOpcode::G_SHL:
525     return C1 << C2;
526   case TargetOpcode::G_SUB:
527     return C1 - C2;
528   case TargetOpcode::G_XOR:
529     return C1 ^ C2;
530   case TargetOpcode::G_UDIV:
531     if (!C2.getBoolValue())
532       break;
533     return C1.udiv(C2);
534   case TargetOpcode::G_SDIV:
535     if (!C2.getBoolValue())
536       break;
537     return C1.sdiv(C2);
538   case TargetOpcode::G_UREM:
539     if (!C2.getBoolValue())
540       break;
541     return C1.urem(C2);
542   case TargetOpcode::G_SREM:
543     if (!C2.getBoolValue())
544       break;
545     return C1.srem(C2);
546   case TargetOpcode::G_SMIN:
547     return APIntOps::smin(C1, C2);
548   case TargetOpcode::G_SMAX:
549     return APIntOps::smax(C1, C2);
550   case TargetOpcode::G_UMIN:
551     return APIntOps::umin(C1, C2);
552   case TargetOpcode::G_UMAX:
553     return APIntOps::umax(C1, C2);
554   }
555 
556   return std::nullopt;
557 }
558 
559 std::optional<APFloat>
560 llvm::ConstantFoldFPBinOp(unsigned Opcode, const Register Op1,
561                           const Register Op2, const MachineRegisterInfo &MRI) {
562   const ConstantFP *Op2Cst = getConstantFPVRegVal(Op2, MRI);
563   if (!Op2Cst)
564     return std::nullopt;
565 
566   const ConstantFP *Op1Cst = getConstantFPVRegVal(Op1, MRI);
567   if (!Op1Cst)
568     return std::nullopt;
569 
570   APFloat C1 = Op1Cst->getValueAPF();
571   const APFloat &C2 = Op2Cst->getValueAPF();
572   switch (Opcode) {
573   case TargetOpcode::G_FADD:
574     C1.add(C2, APFloat::rmNearestTiesToEven);
575     return C1;
576   case TargetOpcode::G_FSUB:
577     C1.subtract(C2, APFloat::rmNearestTiesToEven);
578     return C1;
579   case TargetOpcode::G_FMUL:
580     C1.multiply(C2, APFloat::rmNearestTiesToEven);
581     return C1;
582   case TargetOpcode::G_FDIV:
583     C1.divide(C2, APFloat::rmNearestTiesToEven);
584     return C1;
585   case TargetOpcode::G_FREM:
586     C1.mod(C2);
587     return C1;
588   case TargetOpcode::G_FCOPYSIGN:
589     C1.copySign(C2);
590     return C1;
591   case TargetOpcode::G_FMINNUM:
592     return minnum(C1, C2);
593   case TargetOpcode::G_FMAXNUM:
594     return maxnum(C1, C2);
595   case TargetOpcode::G_FMINIMUM:
596     return minimum(C1, C2);
597   case TargetOpcode::G_FMAXIMUM:
598     return maximum(C1, C2);
599   case TargetOpcode::G_FMINNUM_IEEE:
600   case TargetOpcode::G_FMAXNUM_IEEE:
601     // FIXME: These operations were unfortunately named. fminnum/fmaxnum do not
602     // follow the IEEE behavior for signaling nans and follow libm's fmin/fmax,
603     // and currently there isn't a nice wrapper in APFloat for the version with
604     // correct snan handling.
605     break;
606   default:
607     break;
608   }
609 
610   return std::nullopt;
611 }
612 
613 SmallVector<APInt>
614 llvm::ConstantFoldVectorBinop(unsigned Opcode, const Register Op1,
615                               const Register Op2,
616                               const MachineRegisterInfo &MRI) {
617   auto *SrcVec2 = getOpcodeDef<GBuildVector>(Op2, MRI);
618   if (!SrcVec2)
619     return SmallVector<APInt>();
620 
621   auto *SrcVec1 = getOpcodeDef<GBuildVector>(Op1, MRI);
622   if (!SrcVec1)
623     return SmallVector<APInt>();
624 
625   SmallVector<APInt> FoldedElements;
626   for (unsigned Idx = 0, E = SrcVec1->getNumSources(); Idx < E; ++Idx) {
627     auto MaybeCst = ConstantFoldBinOp(Opcode, SrcVec1->getSourceReg(Idx),
628                                       SrcVec2->getSourceReg(Idx), MRI);
629     if (!MaybeCst)
630       return SmallVector<APInt>();
631     FoldedElements.push_back(*MaybeCst);
632   }
633   return FoldedElements;
634 }
635 
636 bool llvm::isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
637                            bool SNaN) {
638   const MachineInstr *DefMI = MRI.getVRegDef(Val);
639   if (!DefMI)
640     return false;
641 
642   const TargetMachine& TM = DefMI->getMF()->getTarget();
643   if (DefMI->getFlag(MachineInstr::FmNoNans) || TM.Options.NoNaNsFPMath)
644     return true;
645 
646   // If the value is a constant, we can obviously see if it is a NaN or not.
647   if (const ConstantFP *FPVal = getConstantFPVRegVal(Val, MRI)) {
648     return !FPVal->getValueAPF().isNaN() ||
649            (SNaN && !FPVal->getValueAPF().isSignaling());
650   }
651 
652   if (DefMI->getOpcode() == TargetOpcode::G_BUILD_VECTOR) {
653     for (const auto &Op : DefMI->uses())
654       if (!isKnownNeverNaN(Op.getReg(), MRI, SNaN))
655         return false;
656     return true;
657   }
658 
659   switch (DefMI->getOpcode()) {
660   default:
661     break;
662   case TargetOpcode::G_FADD:
663   case TargetOpcode::G_FSUB:
664   case TargetOpcode::G_FMUL:
665   case TargetOpcode::G_FDIV:
666   case TargetOpcode::G_FREM:
667   case TargetOpcode::G_FSIN:
668   case TargetOpcode::G_FCOS:
669   case TargetOpcode::G_FMA:
670   case TargetOpcode::G_FMAD:
671     if (SNaN)
672       return true;
673 
674     // TODO: Need isKnownNeverInfinity
675     return false;
676   case TargetOpcode::G_FMINNUM_IEEE:
677   case TargetOpcode::G_FMAXNUM_IEEE: {
678     if (SNaN)
679       return true;
680     // This can return a NaN if either operand is an sNaN, or if both operands
681     // are NaN.
682     return (isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI) &&
683             isKnownNeverSNaN(DefMI->getOperand(2).getReg(), MRI)) ||
684            (isKnownNeverSNaN(DefMI->getOperand(1).getReg(), MRI) &&
685             isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI));
686   }
687   case TargetOpcode::G_FMINNUM:
688   case TargetOpcode::G_FMAXNUM: {
689     // Only one needs to be known not-nan, since it will be returned if the
690     // other ends up being one.
691     return isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI, SNaN) ||
692            isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI, SNaN);
693   }
694   }
695 
696   if (SNaN) {
697     // FP operations quiet. For now, just handle the ones inserted during
698     // legalization.
699     switch (DefMI->getOpcode()) {
700     case TargetOpcode::G_FPEXT:
701     case TargetOpcode::G_FPTRUNC:
702     case TargetOpcode::G_FCANONICALIZE:
703       return true;
704     default:
705       return false;
706     }
707   }
708 
709   return false;
710 }
711 
712 Align llvm::inferAlignFromPtrInfo(MachineFunction &MF,
713                                   const MachinePointerInfo &MPO) {
714   auto PSV = MPO.V.dyn_cast<const PseudoSourceValue *>();
715   if (auto FSPV = dyn_cast_or_null<FixedStackPseudoSourceValue>(PSV)) {
716     MachineFrameInfo &MFI = MF.getFrameInfo();
717     return commonAlignment(MFI.getObjectAlign(FSPV->getFrameIndex()),
718                            MPO.Offset);
719   }
720 
721   if (const Value *V = MPO.V.dyn_cast<const Value *>()) {
722     const Module *M = MF.getFunction().getParent();
723     return V->getPointerAlignment(M->getDataLayout());
724   }
725 
726   return Align(1);
727 }
728 
729 Register llvm::getFunctionLiveInPhysReg(MachineFunction &MF,
730                                         const TargetInstrInfo &TII,
731                                         MCRegister PhysReg,
732                                         const TargetRegisterClass &RC,
733                                         const DebugLoc &DL, LLT RegTy) {
734   MachineBasicBlock &EntryMBB = MF.front();
735   MachineRegisterInfo &MRI = MF.getRegInfo();
736   Register LiveIn = MRI.getLiveInVirtReg(PhysReg);
737   if (LiveIn) {
738     MachineInstr *Def = MRI.getVRegDef(LiveIn);
739     if (Def) {
740       // FIXME: Should the verifier check this is in the entry block?
741       assert(Def->getParent() == &EntryMBB && "live-in copy not in entry block");
742       return LiveIn;
743     }
744 
745     // It's possible the incoming argument register and copy was added during
746     // lowering, but later deleted due to being/becoming dead. If this happens,
747     // re-insert the copy.
748   } else {
749     // The live in register was not present, so add it.
750     LiveIn = MF.addLiveIn(PhysReg, &RC);
751     if (RegTy.isValid())
752       MRI.setType(LiveIn, RegTy);
753   }
754 
755   BuildMI(EntryMBB, EntryMBB.begin(), DL, TII.get(TargetOpcode::COPY), LiveIn)
756     .addReg(PhysReg);
757   if (!EntryMBB.isLiveIn(PhysReg))
758     EntryMBB.addLiveIn(PhysReg);
759   return LiveIn;
760 }
761 
762 std::optional<APInt> llvm::ConstantFoldExtOp(unsigned Opcode,
763                                              const Register Op1, uint64_t Imm,
764                                              const MachineRegisterInfo &MRI) {
765   auto MaybeOp1Cst = getIConstantVRegVal(Op1, MRI);
766   if (MaybeOp1Cst) {
767     switch (Opcode) {
768     default:
769       break;
770     case TargetOpcode::G_SEXT_INREG: {
771       LLT Ty = MRI.getType(Op1);
772       return MaybeOp1Cst->trunc(Imm).sext(Ty.getScalarSizeInBits());
773     }
774     }
775   }
776   return std::nullopt;
777 }
778 
779 std::optional<APFloat>
780 llvm::ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
781                              const MachineRegisterInfo &MRI) {
782   assert(Opcode == TargetOpcode::G_SITOFP || Opcode == TargetOpcode::G_UITOFP);
783   if (auto MaybeSrcVal = getIConstantVRegVal(Src, MRI)) {
784     APFloat DstVal(getFltSemanticForLLT(DstTy));
785     DstVal.convertFromAPInt(*MaybeSrcVal, Opcode == TargetOpcode::G_SITOFP,
786                             APFloat::rmNearestTiesToEven);
787     return DstVal;
788   }
789   return std::nullopt;
790 }
791 
792 std::optional<SmallVector<unsigned>>
793 llvm::ConstantFoldCTLZ(Register Src, const MachineRegisterInfo &MRI) {
794   LLT Ty = MRI.getType(Src);
795   SmallVector<unsigned> FoldedCTLZs;
796   auto tryFoldScalar = [&](Register R) -> std::optional<unsigned> {
797     auto MaybeCst = getIConstantVRegVal(R, MRI);
798     if (!MaybeCst)
799       return std::nullopt;
800     return MaybeCst->countLeadingZeros();
801   };
802   if (Ty.isVector()) {
803     // Try to constant fold each element.
804     auto *BV = getOpcodeDef<GBuildVector>(Src, MRI);
805     if (!BV)
806       return std::nullopt;
807     for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
808       if (auto MaybeFold = tryFoldScalar(BV->getSourceReg(SrcIdx))) {
809         FoldedCTLZs.emplace_back(*MaybeFold);
810         continue;
811       }
812       return std::nullopt;
813     }
814     return FoldedCTLZs;
815   }
816   if (auto MaybeCst = tryFoldScalar(Src)) {
817     FoldedCTLZs.emplace_back(*MaybeCst);
818     return FoldedCTLZs;
819   }
820   return std::nullopt;
821 }
822 
823 bool llvm::isKnownToBeAPowerOfTwo(Register Reg, const MachineRegisterInfo &MRI,
824                                   GISelKnownBits *KB) {
825   std::optional<DefinitionAndSourceRegister> DefSrcReg =
826       getDefSrcRegIgnoringCopies(Reg, MRI);
827   if (!DefSrcReg)
828     return false;
829 
830   const MachineInstr &MI = *DefSrcReg->MI;
831   const LLT Ty = MRI.getType(Reg);
832 
833   switch (MI.getOpcode()) {
834   case TargetOpcode::G_CONSTANT: {
835     unsigned BitWidth = Ty.getScalarSizeInBits();
836     const ConstantInt *CI = MI.getOperand(1).getCImm();
837     return CI->getValue().zextOrTrunc(BitWidth).isPowerOf2();
838   }
839   case TargetOpcode::G_SHL: {
840     // A left-shift of a constant one will have exactly one bit set because
841     // shifting the bit off the end is undefined.
842 
843     // TODO: Constant splat
844     if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
845       if (*ConstLHS == 1)
846         return true;
847     }
848 
849     break;
850   }
851   case TargetOpcode::G_LSHR: {
852     if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
853       if (ConstLHS->isSignMask())
854         return true;
855     }
856 
857     break;
858   }
859   case TargetOpcode::G_BUILD_VECTOR: {
860     // TODO: Probably should have a recursion depth guard since you could have
861     // bitcasted vector elements.
862     for (const MachineOperand &MO : llvm::drop_begin(MI.operands()))
863       if (!isKnownToBeAPowerOfTwo(MO.getReg(), MRI, KB))
864         return false;
865 
866     return true;
867   }
868   case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
869     // Only handle constants since we would need to know if number of leading
870     // zeros is greater than the truncation amount.
871     const unsigned BitWidth = Ty.getScalarSizeInBits();
872     for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) {
873       auto Const = getIConstantVRegVal(MO.getReg(), MRI);
874       if (!Const || !Const->zextOrTrunc(BitWidth).isPowerOf2())
875         return false;
876     }
877 
878     return true;
879   }
880   default:
881     break;
882   }
883 
884   if (!KB)
885     return false;
886 
887   // More could be done here, though the above checks are enough
888   // to handle some common cases.
889 
890   // Fall back to computeKnownBits to catch other known cases.
891   KnownBits Known = KB->getKnownBits(Reg);
892   return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
893 }
894 
895 void llvm::getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU) {
896   AU.addPreserved<StackProtector>();
897 }
898 
899 LLT llvm::getLCMType(LLT OrigTy, LLT TargetTy) {
900   const unsigned OrigSize = OrigTy.getSizeInBits();
901   const unsigned TargetSize = TargetTy.getSizeInBits();
902 
903   if (OrigSize == TargetSize)
904     return OrigTy;
905 
906   if (OrigTy.isVector()) {
907     const LLT OrigElt = OrigTy.getElementType();
908 
909     if (TargetTy.isVector()) {
910       const LLT TargetElt = TargetTy.getElementType();
911 
912       if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
913         int GCDElts =
914             std::gcd(OrigTy.getNumElements(), TargetTy.getNumElements());
915         // Prefer the original element type.
916         ElementCount Mul = OrigTy.getElementCount() * TargetTy.getNumElements();
917         return LLT::vector(Mul.divideCoefficientBy(GCDElts),
918                            OrigTy.getElementType());
919       }
920     } else {
921       if (OrigElt.getSizeInBits() == TargetSize)
922         return OrigTy;
923     }
924 
925     unsigned LCMSize = std::lcm(OrigSize, TargetSize);
926     return LLT::fixed_vector(LCMSize / OrigElt.getSizeInBits(), OrigElt);
927   }
928 
929   if (TargetTy.isVector()) {
930     unsigned LCMSize = std::lcm(OrigSize, TargetSize);
931     return LLT::fixed_vector(LCMSize / OrigSize, OrigTy);
932   }
933 
934   unsigned LCMSize = std::lcm(OrigSize, TargetSize);
935 
936   // Preserve pointer types.
937   if (LCMSize == OrigSize)
938     return OrigTy;
939   if (LCMSize == TargetSize)
940     return TargetTy;
941 
942   return LLT::scalar(LCMSize);
943 }
944 
945 LLT llvm::getCoverTy(LLT OrigTy, LLT TargetTy) {
946   if (!OrigTy.isVector() || !TargetTy.isVector() || OrigTy == TargetTy ||
947       (OrigTy.getScalarSizeInBits() != TargetTy.getScalarSizeInBits()))
948     return getLCMType(OrigTy, TargetTy);
949 
950   unsigned OrigTyNumElts = OrigTy.getNumElements();
951   unsigned TargetTyNumElts = TargetTy.getNumElements();
952   if (OrigTyNumElts % TargetTyNumElts == 0)
953     return OrigTy;
954 
955   unsigned NumElts = alignTo(OrigTyNumElts, TargetTyNumElts);
956   return LLT::scalarOrVector(ElementCount::getFixed(NumElts),
957                              OrigTy.getElementType());
958 }
959 
960 LLT llvm::getGCDType(LLT OrigTy, LLT TargetTy) {
961   const unsigned OrigSize = OrigTy.getSizeInBits();
962   const unsigned TargetSize = TargetTy.getSizeInBits();
963 
964   if (OrigSize == TargetSize)
965     return OrigTy;
966 
967   if (OrigTy.isVector()) {
968     LLT OrigElt = OrigTy.getElementType();
969     if (TargetTy.isVector()) {
970       LLT TargetElt = TargetTy.getElementType();
971       if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
972         int GCD = std::gcd(OrigTy.getNumElements(), TargetTy.getNumElements());
973         return LLT::scalarOrVector(ElementCount::getFixed(GCD), OrigElt);
974       }
975     } else {
976       // If the source is a vector of pointers, return a pointer element.
977       if (OrigElt.getSizeInBits() == TargetSize)
978         return OrigElt;
979     }
980 
981     unsigned GCD = std::gcd(OrigSize, TargetSize);
982     if (GCD == OrigElt.getSizeInBits())
983       return OrigElt;
984 
985     // If we can't produce the original element type, we have to use a smaller
986     // scalar.
987     if (GCD < OrigElt.getSizeInBits())
988       return LLT::scalar(GCD);
989     return LLT::fixed_vector(GCD / OrigElt.getSizeInBits(), OrigElt);
990   }
991 
992   if (TargetTy.isVector()) {
993     // Try to preserve the original element type.
994     LLT TargetElt = TargetTy.getElementType();
995     if (TargetElt.getSizeInBits() == OrigSize)
996       return OrigTy;
997   }
998 
999   unsigned GCD = std::gcd(OrigSize, TargetSize);
1000   return LLT::scalar(GCD);
1001 }
1002 
1003 std::optional<int> llvm::getSplatIndex(MachineInstr &MI) {
1004   assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
1005          "Only G_SHUFFLE_VECTOR can have a splat index!");
1006   ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
1007   auto FirstDefinedIdx = find_if(Mask, [](int Elt) { return Elt >= 0; });
1008 
1009   // If all elements are undefined, this shuffle can be considered a splat.
1010   // Return 0 for better potential for callers to simplify.
1011   if (FirstDefinedIdx == Mask.end())
1012     return 0;
1013 
1014   // Make sure all remaining elements are either undef or the same
1015   // as the first non-undef value.
1016   int SplatValue = *FirstDefinedIdx;
1017   if (any_of(make_range(std::next(FirstDefinedIdx), Mask.end()),
1018              [&SplatValue](int Elt) { return Elt >= 0 && Elt != SplatValue; }))
1019     return std::nullopt;
1020 
1021   return SplatValue;
1022 }
1023 
1024 static bool isBuildVectorOp(unsigned Opcode) {
1025   return Opcode == TargetOpcode::G_BUILD_VECTOR ||
1026          Opcode == TargetOpcode::G_BUILD_VECTOR_TRUNC;
1027 }
1028 
1029 namespace {
1030 
1031 std::optional<ValueAndVReg> getAnyConstantSplat(Register VReg,
1032                                                 const MachineRegisterInfo &MRI,
1033                                                 bool AllowUndef) {
1034   MachineInstr *MI = getDefIgnoringCopies(VReg, MRI);
1035   if (!MI)
1036     return std::nullopt;
1037 
1038   if (!isBuildVectorOp(MI->getOpcode()))
1039     return std::nullopt;
1040 
1041   std::optional<ValueAndVReg> SplatValAndReg;
1042   for (MachineOperand &Op : MI->uses()) {
1043     Register Element = Op.getReg();
1044     auto ElementValAndReg =
1045         getAnyConstantVRegValWithLookThrough(Element, MRI, true, true);
1046 
1047     // If AllowUndef, treat undef as value that will result in a constant splat.
1048     if (!ElementValAndReg) {
1049       if (AllowUndef && isa<GImplicitDef>(MRI.getVRegDef(Element)))
1050         continue;
1051       return std::nullopt;
1052     }
1053 
1054     // Record splat value
1055     if (!SplatValAndReg)
1056       SplatValAndReg = ElementValAndReg;
1057 
1058     // Different constant then the one already recorded, not a constant splat.
1059     if (SplatValAndReg->Value != ElementValAndReg->Value)
1060       return std::nullopt;
1061   }
1062 
1063   return SplatValAndReg;
1064 }
1065 
1066 } // end anonymous namespace
1067 
1068 bool llvm::isBuildVectorConstantSplat(const Register Reg,
1069                                       const MachineRegisterInfo &MRI,
1070                                       int64_t SplatValue, bool AllowUndef) {
1071   if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef))
1072     return mi_match(SplatValAndReg->VReg, MRI, m_SpecificICst(SplatValue));
1073   return false;
1074 }
1075 
1076 bool llvm::isBuildVectorConstantSplat(const MachineInstr &MI,
1077                                       const MachineRegisterInfo &MRI,
1078                                       int64_t SplatValue, bool AllowUndef) {
1079   return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
1080                                     AllowUndef);
1081 }
1082 
1083 std::optional<APInt>
1084 llvm::getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI) {
1085   if (auto SplatValAndReg =
1086           getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false)) {
1087     std::optional<ValueAndVReg> ValAndVReg =
1088         getIConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
1089     return ValAndVReg->Value;
1090   }
1091 
1092   return std::nullopt;
1093 }
1094 
1095 std::optional<APInt>
1096 llvm::getIConstantSplatVal(const MachineInstr &MI,
1097                            const MachineRegisterInfo &MRI) {
1098   return getIConstantSplatVal(MI.getOperand(0).getReg(), MRI);
1099 }
1100 
1101 std::optional<int64_t>
1102 llvm::getIConstantSplatSExtVal(const Register Reg,
1103                                const MachineRegisterInfo &MRI) {
1104   if (auto SplatValAndReg =
1105           getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false))
1106     return getIConstantVRegSExtVal(SplatValAndReg->VReg, MRI);
1107   return std::nullopt;
1108 }
1109 
1110 std::optional<int64_t>
1111 llvm::getIConstantSplatSExtVal(const MachineInstr &MI,
1112                                const MachineRegisterInfo &MRI) {
1113   return getIConstantSplatSExtVal(MI.getOperand(0).getReg(), MRI);
1114 }
1115 
1116 std::optional<FPValueAndVReg>
1117 llvm::getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
1118                         bool AllowUndef) {
1119   if (auto SplatValAndReg = getAnyConstantSplat(VReg, MRI, AllowUndef))
1120     return getFConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
1121   return std::nullopt;
1122 }
1123 
1124 bool llvm::isBuildVectorAllZeros(const MachineInstr &MI,
1125                                  const MachineRegisterInfo &MRI,
1126                                  bool AllowUndef) {
1127   return isBuildVectorConstantSplat(MI, MRI, 0, AllowUndef);
1128 }
1129 
1130 bool llvm::isBuildVectorAllOnes(const MachineInstr &MI,
1131                                 const MachineRegisterInfo &MRI,
1132                                 bool AllowUndef) {
1133   return isBuildVectorConstantSplat(MI, MRI, -1, AllowUndef);
1134 }
1135 
1136 std::optional<RegOrConstant>
1137 llvm::getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI) {
1138   unsigned Opc = MI.getOpcode();
1139   if (!isBuildVectorOp(Opc))
1140     return std::nullopt;
1141   if (auto Splat = getIConstantSplatSExtVal(MI, MRI))
1142     return RegOrConstant(*Splat);
1143   auto Reg = MI.getOperand(1).getReg();
1144   if (any_of(make_range(MI.operands_begin() + 2, MI.operands_end()),
1145              [&Reg](const MachineOperand &Op) { return Op.getReg() != Reg; }))
1146     return std::nullopt;
1147   return RegOrConstant(Reg);
1148 }
1149 
1150 static bool isConstantScalar(const MachineInstr &MI,
1151                              const MachineRegisterInfo &MRI,
1152                              bool AllowFP = true,
1153                              bool AllowOpaqueConstants = true) {
1154   switch (MI.getOpcode()) {
1155   case TargetOpcode::G_CONSTANT:
1156   case TargetOpcode::G_IMPLICIT_DEF:
1157     return true;
1158   case TargetOpcode::G_FCONSTANT:
1159     return AllowFP;
1160   case TargetOpcode::G_GLOBAL_VALUE:
1161   case TargetOpcode::G_FRAME_INDEX:
1162   case TargetOpcode::G_BLOCK_ADDR:
1163   case TargetOpcode::G_JUMP_TABLE:
1164     return AllowOpaqueConstants;
1165   default:
1166     return false;
1167   }
1168 }
1169 
1170 bool llvm::isConstantOrConstantVector(MachineInstr &MI,
1171                                       const MachineRegisterInfo &MRI) {
1172   Register Def = MI.getOperand(0).getReg();
1173   if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1174     return true;
1175   GBuildVector *BV = dyn_cast<GBuildVector>(&MI);
1176   if (!BV)
1177     return false;
1178   for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
1179     if (getIConstantVRegValWithLookThrough(BV->getSourceReg(SrcIdx), MRI) ||
1180         getOpcodeDef<GImplicitDef>(BV->getSourceReg(SrcIdx), MRI))
1181       continue;
1182     return false;
1183   }
1184   return true;
1185 }
1186 
1187 bool llvm::isConstantOrConstantVector(const MachineInstr &MI,
1188                                       const MachineRegisterInfo &MRI,
1189                                       bool AllowFP, bool AllowOpaqueConstants) {
1190   if (isConstantScalar(MI, MRI, AllowFP, AllowOpaqueConstants))
1191     return true;
1192 
1193   if (!isBuildVectorOp(MI.getOpcode()))
1194     return false;
1195 
1196   const unsigned NumOps = MI.getNumOperands();
1197   for (unsigned I = 1; I != NumOps; ++I) {
1198     const MachineInstr *ElementDef = MRI.getVRegDef(MI.getOperand(I).getReg());
1199     if (!isConstantScalar(*ElementDef, MRI, AllowFP, AllowOpaqueConstants))
1200       return false;
1201   }
1202 
1203   return true;
1204 }
1205 
1206 std::optional<APInt>
1207 llvm::isConstantOrConstantSplatVector(MachineInstr &MI,
1208                                       const MachineRegisterInfo &MRI) {
1209   Register Def = MI.getOperand(0).getReg();
1210   if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
1211     return C->Value;
1212   auto MaybeCst = getIConstantSplatSExtVal(MI, MRI);
1213   if (!MaybeCst)
1214     return std::nullopt;
1215   const unsigned ScalarSize = MRI.getType(Def).getScalarSizeInBits();
1216   return APInt(ScalarSize, *MaybeCst, true);
1217 }
1218 
1219 bool llvm::isNullOrNullSplat(const MachineInstr &MI,
1220                              const MachineRegisterInfo &MRI, bool AllowUndefs) {
1221   switch (MI.getOpcode()) {
1222   case TargetOpcode::G_IMPLICIT_DEF:
1223     return AllowUndefs;
1224   case TargetOpcode::G_CONSTANT:
1225     return MI.getOperand(1).getCImm()->isNullValue();
1226   case TargetOpcode::G_FCONSTANT: {
1227     const ConstantFP *FPImm = MI.getOperand(1).getFPImm();
1228     return FPImm->isZero() && !FPImm->isNegative();
1229   }
1230   default:
1231     if (!AllowUndefs) // TODO: isBuildVectorAllZeros assumes undef is OK already
1232       return false;
1233     return isBuildVectorAllZeros(MI, MRI);
1234   }
1235 }
1236 
1237 bool llvm::isAllOnesOrAllOnesSplat(const MachineInstr &MI,
1238                                    const MachineRegisterInfo &MRI,
1239                                    bool AllowUndefs) {
1240   switch (MI.getOpcode()) {
1241   case TargetOpcode::G_IMPLICIT_DEF:
1242     return AllowUndefs;
1243   case TargetOpcode::G_CONSTANT:
1244     return MI.getOperand(1).getCImm()->isAllOnesValue();
1245   default:
1246     if (!AllowUndefs) // TODO: isBuildVectorAllOnes assumes undef is OK already
1247       return false;
1248     return isBuildVectorAllOnes(MI, MRI);
1249   }
1250 }
1251 
1252 bool llvm::matchUnaryPredicate(
1253     const MachineRegisterInfo &MRI, Register Reg,
1254     std::function<bool(const Constant *ConstVal)> Match, bool AllowUndefs) {
1255 
1256   const MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
1257   if (AllowUndefs && Def->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
1258     return Match(nullptr);
1259 
1260   // TODO: Also handle fconstant
1261   if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
1262     return Match(Def->getOperand(1).getCImm());
1263 
1264   if (Def->getOpcode() != TargetOpcode::G_BUILD_VECTOR)
1265     return false;
1266 
1267   for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
1268     Register SrcElt = Def->getOperand(I).getReg();
1269     const MachineInstr *SrcDef = getDefIgnoringCopies(SrcElt, MRI);
1270     if (AllowUndefs && SrcDef->getOpcode() == TargetOpcode::G_IMPLICIT_DEF) {
1271       if (!Match(nullptr))
1272         return false;
1273       continue;
1274     }
1275 
1276     if (SrcDef->getOpcode() != TargetOpcode::G_CONSTANT ||
1277         !Match(SrcDef->getOperand(1).getCImm()))
1278       return false;
1279   }
1280 
1281   return true;
1282 }
1283 
1284 bool llvm::isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
1285                           bool IsFP) {
1286   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1287   case TargetLowering::UndefinedBooleanContent:
1288     return Val & 0x1;
1289   case TargetLowering::ZeroOrOneBooleanContent:
1290     return Val == 1;
1291   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1292     return Val == -1;
1293   }
1294   llvm_unreachable("Invalid boolean contents");
1295 }
1296 
1297 bool llvm::isConstFalseVal(const TargetLowering &TLI, int64_t Val,
1298                            bool IsVector, bool IsFP) {
1299   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1300   case TargetLowering::UndefinedBooleanContent:
1301     return ~Val & 0x1;
1302   case TargetLowering::ZeroOrOneBooleanContent:
1303   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1304     return Val == 0;
1305   }
1306   llvm_unreachable("Invalid boolean contents");
1307 }
1308 
1309 int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
1310                              bool IsFP) {
1311   switch (TLI.getBooleanContents(IsVector, IsFP)) {
1312   case TargetLowering::UndefinedBooleanContent:
1313   case TargetLowering::ZeroOrOneBooleanContent:
1314     return 1;
1315   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1316     return -1;
1317   }
1318   llvm_unreachable("Invalid boolean contents");
1319 }
1320 
1321 bool llvm::shouldOptForSize(const MachineBasicBlock &MBB,
1322                             ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
1323   const auto &F = MBB.getParent()->getFunction();
1324   return F.hasOptSize() || F.hasMinSize() ||
1325          llvm::shouldOptimizeForSize(MBB.getBasicBlock(), PSI, BFI);
1326 }
1327 
1328 void llvm::saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI,
1329                             LostDebugLocObserver *LocObserver,
1330                             SmallInstListTy &DeadInstChain) {
1331   for (MachineOperand &Op : MI.uses()) {
1332     if (Op.isReg() && Op.getReg().isVirtual())
1333       DeadInstChain.insert(MRI.getVRegDef(Op.getReg()));
1334   }
1335   LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
1336   DeadInstChain.remove(&MI);
1337   MI.eraseFromParent();
1338   if (LocObserver)
1339     LocObserver->checkpoint(false);
1340 }
1341 
1342 void llvm::eraseInstrs(ArrayRef<MachineInstr *> DeadInstrs,
1343                        MachineRegisterInfo &MRI,
1344                        LostDebugLocObserver *LocObserver) {
1345   SmallInstListTy DeadInstChain;
1346   for (MachineInstr *MI : DeadInstrs)
1347     saveUsesAndErase(*MI, MRI, LocObserver, DeadInstChain);
1348 
1349   while (!DeadInstChain.empty()) {
1350     MachineInstr *Inst = DeadInstChain.pop_back_val();
1351     if (!isTriviallyDead(*Inst, MRI))
1352       continue;
1353     saveUsesAndErase(*Inst, MRI, LocObserver, DeadInstChain);
1354   }
1355 }
1356 
1357 void llvm::eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI,
1358                       LostDebugLocObserver *LocObserver) {
1359   return eraseInstrs({&MI}, MRI, LocObserver);
1360 }
1361 
1362 void llvm::salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI) {
1363   for (auto &Def : MI.defs()) {
1364     assert(Def.isReg() && "Must be a reg");
1365 
1366     SmallVector<MachineOperand *, 16> DbgUsers;
1367     for (auto &MOUse : MRI.use_operands(Def.getReg())) {
1368       MachineInstr *DbgValue = MOUse.getParent();
1369       // Ignore partially formed DBG_VALUEs.
1370       if (DbgValue->isNonListDebugValue() && DbgValue->getNumOperands() == 4) {
1371         DbgUsers.push_back(&MOUse);
1372       }
1373     }
1374 
1375     if (!DbgUsers.empty()) {
1376       salvageDebugInfoForDbgValue(MRI, MI, DbgUsers);
1377     }
1378   }
1379 }
1380