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