xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp (revision 6e2a86ed5abfdb75ba9c08ea94ed8dbd41e75c9e)
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/MIPatternMatch.h"
18 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/StackProtector.h"
24 #include "llvm/CodeGen/TargetInstrInfo.h"
25 #include "llvm/CodeGen/TargetLowering.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/Target/TargetMachine.h"
30 
31 #define DEBUG_TYPE "globalisel-utils"
32 
33 using namespace llvm;
34 using namespace MIPatternMatch;
35 
36 Register llvm::constrainRegToClass(MachineRegisterInfo &MRI,
37                                    const TargetInstrInfo &TII,
38                                    const RegisterBankInfo &RBI, Register Reg,
39                                    const TargetRegisterClass &RegClass) {
40   if (!RBI.constrainGenericRegister(Reg, RegClass, MRI))
41     return MRI.createVirtualRegister(&RegClass);
42 
43   return Reg;
44 }
45 
46 Register llvm::constrainOperandRegClass(
47     const MachineFunction &MF, const TargetRegisterInfo &TRI,
48     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
49     const RegisterBankInfo &RBI, MachineInstr &InsertPt,
50     const TargetRegisterClass &RegClass, const MachineOperand &RegMO) {
51   Register Reg = RegMO.getReg();
52   // Assume physical registers are properly constrained.
53   assert(Register::isVirtualRegister(Reg) && "PhysReg not implemented");
54 
55   Register ConstrainedReg = constrainRegToClass(MRI, TII, RBI, Reg, RegClass);
56   // If we created a new virtual register because the class is not compatible
57   // then create a copy between the new and the old register.
58   if (ConstrainedReg != Reg) {
59     MachineBasicBlock::iterator InsertIt(&InsertPt);
60     MachineBasicBlock &MBB = *InsertPt.getParent();
61     if (RegMO.isUse()) {
62       BuildMI(MBB, InsertIt, InsertPt.getDebugLoc(),
63               TII.get(TargetOpcode::COPY), ConstrainedReg)
64           .addReg(Reg);
65     } else {
66       assert(RegMO.isDef() && "Must be a definition");
67       BuildMI(MBB, std::next(InsertIt), InsertPt.getDebugLoc(),
68               TII.get(TargetOpcode::COPY), Reg)
69           .addReg(ConstrainedReg);
70     }
71   } else {
72     if (GISelChangeObserver *Observer = MF.getObserver()) {
73       if (!RegMO.isDef()) {
74         MachineInstr *RegDef = MRI.getVRegDef(Reg);
75         Observer->changedInstr(*RegDef);
76       }
77       Observer->changingAllUsesOfReg(MRI, Reg);
78       Observer->finishedChangingAllUsesOfReg();
79     }
80   }
81   return ConstrainedReg;
82 }
83 
84 Register llvm::constrainOperandRegClass(
85     const MachineFunction &MF, const TargetRegisterInfo &TRI,
86     MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
87     const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
88     const MachineOperand &RegMO, unsigned OpIdx) {
89   Register Reg = RegMO.getReg();
90   // Assume physical registers are properly constrained.
91   assert(Register::isVirtualRegister(Reg) && "PhysReg not implemented");
92 
93   const TargetRegisterClass *RegClass = TII.getRegClass(II, OpIdx, &TRI, MF);
94   // Some of the target independent instructions, like COPY, may not impose any
95   // register class constraints on some of their operands: If it's a use, we can
96   // skip constraining as the instruction defining the register would constrain
97   // it.
98 
99   // We can't constrain unallocatable register classes, because we can't create
100   // virtual registers for these classes, so we need to let targets handled this
101   // case.
102   if (RegClass && !RegClass->isAllocatable())
103     RegClass = TRI.getConstrainedRegClassForOperand(RegMO, MRI);
104 
105   if (!RegClass) {
106     assert((!isTargetSpecificOpcode(II.getOpcode()) || RegMO.isUse()) &&
107            "Register class constraint is required unless either the "
108            "instruction is target independent or the operand is a use");
109     // FIXME: Just bailing out like this here could be not enough, unless we
110     // expect the users of this function to do the right thing for PHIs and
111     // COPY:
112     //   v1 = COPY v0
113     //   v2 = COPY v1
114     // v1 here may end up not being constrained at all. Please notice that to
115     // reproduce the issue we likely need a destination pattern of a selection
116     // rule producing such extra copies, not just an input GMIR with them as
117     // every existing target using selectImpl handles copies before calling it
118     // and they never reach this function.
119     return Reg;
120   }
121   return constrainOperandRegClass(MF, TRI, MRI, TII, RBI, InsertPt, *RegClass,
122                                   RegMO);
123 }
124 
125 bool llvm::constrainSelectedInstRegOperands(MachineInstr &I,
126                                             const TargetInstrInfo &TII,
127                                             const TargetRegisterInfo &TRI,
128                                             const RegisterBankInfo &RBI) {
129   assert(!isPreISelGenericOpcode(I.getOpcode()) &&
130          "A selected instruction is expected");
131   MachineBasicBlock &MBB = *I.getParent();
132   MachineFunction &MF = *MBB.getParent();
133   MachineRegisterInfo &MRI = MF.getRegInfo();
134 
135   for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
136     MachineOperand &MO = I.getOperand(OpI);
137 
138     // There's nothing to be done on non-register operands.
139     if (!MO.isReg())
140       continue;
141 
142     LLVM_DEBUG(dbgs() << "Converting operand: " << MO << '\n');
143     assert(MO.isReg() && "Unsupported non-reg operand");
144 
145     Register Reg = MO.getReg();
146     // Physical registers don't need to be constrained.
147     if (Register::isPhysicalRegister(Reg))
148       continue;
149 
150     // Register operands with a value of 0 (e.g. predicate operands) don't need
151     // to be constrained.
152     if (Reg == 0)
153       continue;
154 
155     // If the operand is a vreg, we should constrain its regclass, and only
156     // insert COPYs if that's impossible.
157     // constrainOperandRegClass does that for us.
158     MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
159                                        MO, OpI));
160 
161     // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
162     // done.
163     if (MO.isUse()) {
164       int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
165       if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
166         I.tieOperands(DefIdx, OpI);
167     }
168   }
169   return true;
170 }
171 
172 bool llvm::canReplaceReg(Register DstReg, Register SrcReg,
173                          MachineRegisterInfo &MRI) {
174   // Give up if either DstReg or SrcReg  is a physical register.
175   if (DstReg.isPhysical() || SrcReg.isPhysical())
176     return false;
177   // Give up if the types don't match.
178   if (MRI.getType(DstReg) != MRI.getType(SrcReg))
179     return false;
180   // Replace if either DstReg has no constraints or the register
181   // constraints match.
182   return !MRI.getRegClassOrRegBank(DstReg) ||
183          MRI.getRegClassOrRegBank(DstReg) == MRI.getRegClassOrRegBank(SrcReg);
184 }
185 
186 bool llvm::isTriviallyDead(const MachineInstr &MI,
187                            const MachineRegisterInfo &MRI) {
188   // FIXME: This logical is mostly duplicated with
189   // DeadMachineInstructionElim::isDead. Why is LOCAL_ESCAPE not considered in
190   // MachineInstr::isLabel?
191 
192   // Don't delete frame allocation labels.
193   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE)
194     return false;
195 
196   // If we can move an instruction, we can remove it.  Otherwise, it has
197   // a side-effect of some sort.
198   bool SawStore = false;
199   if (!MI.isSafeToMove(/*AA=*/nullptr, SawStore) && !MI.isPHI())
200     return false;
201 
202   // Instructions without side-effects are dead iff they only define dead vregs.
203   for (auto &MO : MI.operands()) {
204     if (!MO.isReg() || !MO.isDef())
205       continue;
206 
207     Register Reg = MO.getReg();
208     if (Register::isPhysicalRegister(Reg) || !MRI.use_nodbg_empty(Reg))
209       return false;
210   }
211   return true;
212 }
213 
214 static void reportGISelDiagnostic(DiagnosticSeverity Severity,
215                                   MachineFunction &MF,
216                                   const TargetPassConfig &TPC,
217                                   MachineOptimizationRemarkEmitter &MORE,
218                                   MachineOptimizationRemarkMissed &R) {
219   bool IsFatal = Severity == DS_Error &&
220                  TPC.isGlobalISelAbortEnabled();
221   // Print the function name explicitly if we don't have a debug location (which
222   // makes the diagnostic less useful) or if we're going to emit a raw error.
223   if (!R.getLocation().isValid() || IsFatal)
224     R << (" (in function: " + MF.getName() + ")").str();
225 
226   if (IsFatal)
227     report_fatal_error(R.getMsg());
228   else
229     MORE.emit(R);
230 }
231 
232 void llvm::reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
233                               MachineOptimizationRemarkEmitter &MORE,
234                               MachineOptimizationRemarkMissed &R) {
235   reportGISelDiagnostic(DS_Warning, MF, TPC, MORE, R);
236 }
237 
238 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
239                               MachineOptimizationRemarkEmitter &MORE,
240                               MachineOptimizationRemarkMissed &R) {
241   MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
242   reportGISelDiagnostic(DS_Error, MF, TPC, MORE, R);
243 }
244 
245 void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
246                               MachineOptimizationRemarkEmitter &MORE,
247                               const char *PassName, StringRef Msg,
248                               const MachineInstr &MI) {
249   MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
250                                     MI.getDebugLoc(), MI.getParent());
251   R << Msg;
252   // Printing MI is expensive;  only do it if expensive remarks are enabled.
253   if (TPC.isGlobalISelAbortEnabled() || MORE.allowExtraAnalysis(PassName))
254     R << ": " << ore::MNV("Inst", MI);
255   reportGISelFailure(MF, TPC, MORE, R);
256 }
257 
258 Optional<int64_t> llvm::getConstantVRegVal(Register VReg,
259                                            const MachineRegisterInfo &MRI) {
260   Optional<ValueAndVReg> ValAndVReg =
261       getConstantVRegValWithLookThrough(VReg, MRI, /*LookThroughInstrs*/ false);
262   assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
263          "Value found while looking through instrs");
264   if (!ValAndVReg)
265     return None;
266   return ValAndVReg->Value;
267 }
268 
269 Optional<ValueAndVReg> llvm::getConstantVRegValWithLookThrough(
270     Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs,
271     bool HandleFConstant) {
272   SmallVector<std::pair<unsigned, unsigned>, 4> SeenOpcodes;
273   MachineInstr *MI;
274   auto IsConstantOpcode = [HandleFConstant](unsigned Opcode) {
275     return Opcode == TargetOpcode::G_CONSTANT ||
276            (HandleFConstant && Opcode == TargetOpcode::G_FCONSTANT);
277   };
278   auto GetImmediateValue = [HandleFConstant,
279                             &MRI](const MachineInstr &MI) -> Optional<APInt> {
280     const MachineOperand &CstVal = MI.getOperand(1);
281     if (!CstVal.isImm() && !CstVal.isCImm() &&
282         (!HandleFConstant || !CstVal.isFPImm()))
283       return None;
284     if (!CstVal.isFPImm()) {
285       unsigned BitWidth =
286           MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
287       APInt Val = CstVal.isImm() ? APInt(BitWidth, CstVal.getImm())
288                                  : CstVal.getCImm()->getValue();
289       assert(Val.getBitWidth() == BitWidth &&
290              "Value bitwidth doesn't match definition type");
291       return Val;
292     }
293     return CstVal.getFPImm()->getValueAPF().bitcastToAPInt();
294   };
295   while ((MI = MRI.getVRegDef(VReg)) && !IsConstantOpcode(MI->getOpcode()) &&
296          LookThroughInstrs) {
297     switch (MI->getOpcode()) {
298     case TargetOpcode::G_TRUNC:
299     case TargetOpcode::G_SEXT:
300     case TargetOpcode::G_ZEXT:
301       SeenOpcodes.push_back(std::make_pair(
302           MI->getOpcode(),
303           MRI.getType(MI->getOperand(0).getReg()).getSizeInBits()));
304       VReg = MI->getOperand(1).getReg();
305       break;
306     case TargetOpcode::COPY:
307       VReg = MI->getOperand(1).getReg();
308       if (Register::isPhysicalRegister(VReg))
309         return None;
310       break;
311     case TargetOpcode::G_INTTOPTR:
312       VReg = MI->getOperand(1).getReg();
313       break;
314     default:
315       return None;
316     }
317   }
318   if (!MI || !IsConstantOpcode(MI->getOpcode()))
319     return None;
320 
321   Optional<APInt> MaybeVal = GetImmediateValue(*MI);
322   if (!MaybeVal)
323     return None;
324   APInt &Val = *MaybeVal;
325   while (!SeenOpcodes.empty()) {
326     std::pair<unsigned, unsigned> OpcodeAndSize = SeenOpcodes.pop_back_val();
327     switch (OpcodeAndSize.first) {
328     case TargetOpcode::G_TRUNC:
329       Val = Val.trunc(OpcodeAndSize.second);
330       break;
331     case TargetOpcode::G_SEXT:
332       Val = Val.sext(OpcodeAndSize.second);
333       break;
334     case TargetOpcode::G_ZEXT:
335       Val = Val.zext(OpcodeAndSize.second);
336       break;
337     }
338   }
339 
340   if (Val.getBitWidth() > 64)
341     return None;
342 
343   return ValueAndVReg{Val.getSExtValue(), VReg};
344 }
345 
346 const ConstantFP *
347 llvm::getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI) {
348   MachineInstr *MI = MRI.getVRegDef(VReg);
349   if (TargetOpcode::G_FCONSTANT != MI->getOpcode())
350     return nullptr;
351   return MI->getOperand(1).getFPImm();
352 }
353 
354 namespace {
355 struct DefinitionAndSourceRegister {
356   MachineInstr *MI;
357   Register Reg;
358 };
359 } // namespace
360 
361 static Optional<DefinitionAndSourceRegister>
362 getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI) {
363   Register DefSrcReg = Reg;
364   auto *DefMI = MRI.getVRegDef(Reg);
365   auto DstTy = MRI.getType(DefMI->getOperand(0).getReg());
366   if (!DstTy.isValid())
367     return None;
368   while (DefMI->getOpcode() == TargetOpcode::COPY) {
369     Register SrcReg = DefMI->getOperand(1).getReg();
370     auto SrcTy = MRI.getType(SrcReg);
371     if (!SrcTy.isValid())
372       break;
373     DefMI = MRI.getVRegDef(SrcReg);
374     DefSrcReg = SrcReg;
375   }
376   return DefinitionAndSourceRegister{DefMI, DefSrcReg};
377 }
378 
379 MachineInstr *llvm::getDefIgnoringCopies(Register Reg,
380                                          const MachineRegisterInfo &MRI) {
381   Optional<DefinitionAndSourceRegister> DefSrcReg =
382       getDefSrcRegIgnoringCopies(Reg, MRI);
383   return DefSrcReg ? DefSrcReg->MI : nullptr;
384 }
385 
386 Register llvm::getSrcRegIgnoringCopies(Register Reg,
387                                        const MachineRegisterInfo &MRI) {
388   Optional<DefinitionAndSourceRegister> DefSrcReg =
389       getDefSrcRegIgnoringCopies(Reg, MRI);
390   return DefSrcReg ? DefSrcReg->Reg : Register();
391 }
392 
393 MachineInstr *llvm::getOpcodeDef(unsigned Opcode, Register Reg,
394                                  const MachineRegisterInfo &MRI) {
395   MachineInstr *DefMI = getDefIgnoringCopies(Reg, MRI);
396   return DefMI && DefMI->getOpcode() == Opcode ? DefMI : nullptr;
397 }
398 
399 APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
400   if (Size == 32)
401     return APFloat(float(Val));
402   if (Size == 64)
403     return APFloat(Val);
404   if (Size != 16)
405     llvm_unreachable("Unsupported FPConstant size");
406   bool Ignored;
407   APFloat APF(Val);
408   APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
409   return APF;
410 }
411 
412 Optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode, const Register Op1,
413                                         const Register Op2,
414                                         const MachineRegisterInfo &MRI) {
415   auto MaybeOp2Cst = getConstantVRegVal(Op2, MRI);
416   if (!MaybeOp2Cst)
417     return None;
418 
419   auto MaybeOp1Cst = getConstantVRegVal(Op1, MRI);
420   if (!MaybeOp1Cst)
421     return None;
422 
423   LLT Ty = MRI.getType(Op1);
424   APInt C1(Ty.getSizeInBits(), *MaybeOp1Cst, true);
425   APInt C2(Ty.getSizeInBits(), *MaybeOp2Cst, true);
426   switch (Opcode) {
427   default:
428     break;
429   case TargetOpcode::G_ADD:
430     return C1 + C2;
431   case TargetOpcode::G_AND:
432     return C1 & C2;
433   case TargetOpcode::G_ASHR:
434     return C1.ashr(C2);
435   case TargetOpcode::G_LSHR:
436     return C1.lshr(C2);
437   case TargetOpcode::G_MUL:
438     return C1 * C2;
439   case TargetOpcode::G_OR:
440     return C1 | C2;
441   case TargetOpcode::G_SHL:
442     return C1 << C2;
443   case TargetOpcode::G_SUB:
444     return C1 - C2;
445   case TargetOpcode::G_XOR:
446     return C1 ^ C2;
447   case TargetOpcode::G_UDIV:
448     if (!C2.getBoolValue())
449       break;
450     return C1.udiv(C2);
451   case TargetOpcode::G_SDIV:
452     if (!C2.getBoolValue())
453       break;
454     return C1.sdiv(C2);
455   case TargetOpcode::G_UREM:
456     if (!C2.getBoolValue())
457       break;
458     return C1.urem(C2);
459   case TargetOpcode::G_SREM:
460     if (!C2.getBoolValue())
461       break;
462     return C1.srem(C2);
463   }
464 
465   return None;
466 }
467 
468 bool llvm::isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
469                            bool SNaN) {
470   const MachineInstr *DefMI = MRI.getVRegDef(Val);
471   if (!DefMI)
472     return false;
473 
474   const TargetMachine& TM = DefMI->getMF()->getTarget();
475   if (DefMI->getFlag(MachineInstr::FmNoNans) || TM.Options.NoNaNsFPMath)
476     return true;
477 
478   if (SNaN) {
479     // FP operations quiet. For now, just handle the ones inserted during
480     // legalization.
481     switch (DefMI->getOpcode()) {
482     case TargetOpcode::G_FPEXT:
483     case TargetOpcode::G_FPTRUNC:
484     case TargetOpcode::G_FCANONICALIZE:
485       return true;
486     default:
487       return false;
488     }
489   }
490 
491   return false;
492 }
493 
494 Align llvm::inferAlignFromPtrInfo(MachineFunction &MF,
495                                   const MachinePointerInfo &MPO) {
496   auto PSV = MPO.V.dyn_cast<const PseudoSourceValue *>();
497   if (auto FSPV = dyn_cast_or_null<FixedStackPseudoSourceValue>(PSV)) {
498     MachineFrameInfo &MFI = MF.getFrameInfo();
499     return commonAlignment(MFI.getObjectAlign(FSPV->getFrameIndex()),
500                            MPO.Offset);
501   }
502 
503   return Align(1);
504 }
505 
506 Register llvm::getFunctionLiveInPhysReg(MachineFunction &MF,
507                                         const TargetInstrInfo &TII,
508                                         MCRegister PhysReg,
509                                         const TargetRegisterClass &RC,
510                                         LLT RegTy) {
511   DebugLoc DL; // FIXME: Is no location the right choice?
512   MachineBasicBlock &EntryMBB = MF.front();
513   MachineRegisterInfo &MRI = MF.getRegInfo();
514   Register LiveIn = MRI.getLiveInVirtReg(PhysReg);
515   if (LiveIn) {
516     MachineInstr *Def = MRI.getVRegDef(LiveIn);
517     if (Def) {
518       // FIXME: Should the verifier check this is in the entry block?
519       assert(Def->getParent() == &EntryMBB && "live-in copy not in entry block");
520       return LiveIn;
521     }
522 
523     // It's possible the incoming argument register and copy was added during
524     // lowering, but later deleted due to being/becoming dead. If this happens,
525     // re-insert the copy.
526   } else {
527     // The live in register was not present, so add it.
528     LiveIn = MF.addLiveIn(PhysReg, &RC);
529     if (RegTy.isValid())
530       MRI.setType(LiveIn, RegTy);
531   }
532 
533   BuildMI(EntryMBB, EntryMBB.begin(), DL, TII.get(TargetOpcode::COPY), LiveIn)
534     .addReg(PhysReg);
535   if (!EntryMBB.isLiveIn(PhysReg))
536     EntryMBB.addLiveIn(PhysReg);
537   return LiveIn;
538 }
539 
540 Optional<APInt> llvm::ConstantFoldExtOp(unsigned Opcode, const Register Op1,
541                                         uint64_t Imm,
542                                         const MachineRegisterInfo &MRI) {
543   auto MaybeOp1Cst = getConstantVRegVal(Op1, MRI);
544   if (MaybeOp1Cst) {
545     LLT Ty = MRI.getType(Op1);
546     APInt C1(Ty.getSizeInBits(), *MaybeOp1Cst, true);
547     switch (Opcode) {
548     default:
549       break;
550     case TargetOpcode::G_SEXT_INREG:
551       return C1.trunc(Imm).sext(C1.getBitWidth());
552     }
553   }
554   return None;
555 }
556 
557 void llvm::getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU) {
558   AU.addPreserved<StackProtector>();
559 }
560 
561 static unsigned getLCMSize(unsigned OrigSize, unsigned TargetSize) {
562   unsigned Mul = OrigSize * TargetSize;
563   unsigned GCDSize = greatestCommonDivisor(OrigSize, TargetSize);
564   return Mul / GCDSize;
565 }
566 
567 LLT llvm::getLCMType(LLT OrigTy, LLT TargetTy) {
568   const unsigned OrigSize = OrigTy.getSizeInBits();
569   const unsigned TargetSize = TargetTy.getSizeInBits();
570 
571   if (OrigSize == TargetSize)
572     return OrigTy;
573 
574   if (OrigTy.isVector()) {
575     const LLT OrigElt = OrigTy.getElementType();
576 
577     if (TargetTy.isVector()) {
578       const LLT TargetElt = TargetTy.getElementType();
579 
580       if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
581         int GCDElts = greatestCommonDivisor(OrigTy.getNumElements(),
582                                             TargetTy.getNumElements());
583         // Prefer the original element type.
584         int Mul = OrigTy.getNumElements() * TargetTy.getNumElements();
585         return LLT::vector(Mul / GCDElts, OrigTy.getElementType());
586       }
587     } else {
588       if (OrigElt.getSizeInBits() == TargetSize)
589         return OrigTy;
590     }
591 
592     unsigned LCMSize = getLCMSize(OrigSize, TargetSize);
593     return LLT::vector(LCMSize / OrigElt.getSizeInBits(), OrigElt);
594   }
595 
596   if (TargetTy.isVector()) {
597     unsigned LCMSize = getLCMSize(OrigSize, TargetSize);
598     return LLT::vector(LCMSize / OrigSize, OrigTy);
599   }
600 
601   unsigned LCMSize = getLCMSize(OrigSize, TargetSize);
602 
603   // Preserve pointer types.
604   if (LCMSize == OrigSize)
605     return OrigTy;
606   if (LCMSize == TargetSize)
607     return TargetTy;
608 
609   return LLT::scalar(LCMSize);
610 }
611 
612 LLT llvm::getGCDType(LLT OrigTy, LLT TargetTy) {
613   const unsigned OrigSize = OrigTy.getSizeInBits();
614   const unsigned TargetSize = TargetTy.getSizeInBits();
615 
616   if (OrigSize == TargetSize)
617     return OrigTy;
618 
619   if (OrigTy.isVector()) {
620     LLT OrigElt = OrigTy.getElementType();
621     if (TargetTy.isVector()) {
622       LLT TargetElt = TargetTy.getElementType();
623       if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
624         int GCD = greatestCommonDivisor(OrigTy.getNumElements(),
625                                         TargetTy.getNumElements());
626         return LLT::scalarOrVector(GCD, OrigElt);
627       }
628     } else {
629       // If the source is a vector of pointers, return a pointer element.
630       if (OrigElt.getSizeInBits() == TargetSize)
631         return OrigElt;
632     }
633 
634     unsigned GCD = greatestCommonDivisor(OrigSize, TargetSize);
635     if (GCD == OrigElt.getSizeInBits())
636       return OrigElt;
637 
638     // If we can't produce the original element type, we have to use a smaller
639     // scalar.
640     if (GCD < OrigElt.getSizeInBits())
641       return LLT::scalar(GCD);
642     return LLT::vector(GCD / OrigElt.getSizeInBits(), OrigElt);
643   }
644 
645   if (TargetTy.isVector()) {
646     // Try to preserve the original element type.
647     LLT TargetElt = TargetTy.getElementType();
648     if (TargetElt.getSizeInBits() == OrigSize)
649       return OrigTy;
650   }
651 
652   unsigned GCD = greatestCommonDivisor(OrigSize, TargetSize);
653   return LLT::scalar(GCD);
654 }
655 
656 Optional<int> llvm::getSplatIndex(MachineInstr &MI) {
657   assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
658          "Only G_SHUFFLE_VECTOR can have a splat index!");
659   ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
660   auto FirstDefinedIdx = find_if(Mask, [](int Elt) { return Elt >= 0; });
661 
662   // If all elements are undefined, this shuffle can be considered a splat.
663   // Return 0 for better potential for callers to simplify.
664   if (FirstDefinedIdx == Mask.end())
665     return 0;
666 
667   // Make sure all remaining elements are either undef or the same
668   // as the first non-undef value.
669   int SplatValue = *FirstDefinedIdx;
670   if (any_of(make_range(std::next(FirstDefinedIdx), Mask.end()),
671              [&SplatValue](int Elt) { return Elt >= 0 && Elt != SplatValue; }))
672     return None;
673 
674   return SplatValue;
675 }
676 
677 static bool isBuildVectorOp(unsigned Opcode) {
678   return Opcode == TargetOpcode::G_BUILD_VECTOR ||
679          Opcode == TargetOpcode::G_BUILD_VECTOR_TRUNC;
680 }
681 
682 // TODO: Handle mixed undef elements.
683 static bool isBuildVectorConstantSplat(const MachineInstr &MI,
684                                        const MachineRegisterInfo &MRI,
685                                        int64_t SplatValue) {
686   if (!isBuildVectorOp(MI.getOpcode()))
687     return false;
688 
689   const unsigned NumOps = MI.getNumOperands();
690   for (unsigned I = 1; I != NumOps; ++I) {
691     Register Element = MI.getOperand(I).getReg();
692     int64_t ElementValue;
693     if (!mi_match(Element, MRI, m_ICst(ElementValue)) ||
694         ElementValue != SplatValue)
695       return false;
696   }
697 
698   return true;
699 }
700 
701 Optional<int64_t>
702 llvm::getBuildVectorConstantSplat(const MachineInstr &MI,
703                                   const MachineRegisterInfo &MRI) {
704   if (!isBuildVectorOp(MI.getOpcode()))
705     return None;
706 
707   const unsigned NumOps = MI.getNumOperands();
708   Optional<int64_t> Scalar;
709   for (unsigned I = 1; I != NumOps; ++I) {
710     Register Element = MI.getOperand(I).getReg();
711     int64_t ElementValue;
712     if (!mi_match(Element, MRI, m_ICst(ElementValue)))
713       return None;
714     if (!Scalar)
715       Scalar = ElementValue;
716     else if (*Scalar != ElementValue)
717       return None;
718   }
719 
720   return Scalar;
721 }
722 
723 bool llvm::isBuildVectorAllZeros(const MachineInstr &MI,
724                                  const MachineRegisterInfo &MRI) {
725   return isBuildVectorConstantSplat(MI, MRI, 0);
726 }
727 
728 bool llvm::isBuildVectorAllOnes(const MachineInstr &MI,
729                                 const MachineRegisterInfo &MRI) {
730   return isBuildVectorConstantSplat(MI, MRI, -1);
731 }
732 
733 bool llvm::isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
734                           bool IsFP) {
735   switch (TLI.getBooleanContents(IsVector, IsFP)) {
736   case TargetLowering::UndefinedBooleanContent:
737     return Val & 0x1;
738   case TargetLowering::ZeroOrOneBooleanContent:
739     return Val == 1;
740   case TargetLowering::ZeroOrNegativeOneBooleanContent:
741     return Val == -1;
742   }
743   llvm_unreachable("Invalid boolean contents");
744 }
745 
746 int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
747                              bool IsFP) {
748   switch (TLI.getBooleanContents(IsVector, IsFP)) {
749   case TargetLowering::UndefinedBooleanContent:
750   case TargetLowering::ZeroOrOneBooleanContent:
751     return 1;
752   case TargetLowering::ZeroOrNegativeOneBooleanContent:
753     return -1;
754   }
755   llvm_unreachable("Invalid boolean contents");
756 }
757