xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===-- llvm/CodeGen/GlobalISel/LegalizerHelper.cpp -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file This file implements the LegalizerHelper class to legalize
100b57cec5SDimitry Andric /// individual instructions and the LegalizeMachineIR wrapper pass for the
110b57cec5SDimitry Andric /// primary legalization.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/CallLowering.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
208bcb0991SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
250b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
260b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "legalizer"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric using namespace LegalizeActions;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric /// Try to break down \p OrigTy into \p NarrowTy sized pieces.
340b57cec5SDimitry Andric ///
350b57cec5SDimitry Andric /// Returns the number of \p NarrowTy elements needed to reconstruct \p OrigTy,
360b57cec5SDimitry Andric /// with any leftover piece as type \p LeftoverTy
370b57cec5SDimitry Andric ///
380b57cec5SDimitry Andric /// Returns -1 in the first element of the pair if the breakdown is not
390b57cec5SDimitry Andric /// satisfiable.
400b57cec5SDimitry Andric static std::pair<int, int>
410b57cec5SDimitry Andric getNarrowTypeBreakDown(LLT OrigTy, LLT NarrowTy, LLT &LeftoverTy) {
420b57cec5SDimitry Andric   assert(!LeftoverTy.isValid() && "this is an out argument");
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   unsigned Size = OrigTy.getSizeInBits();
450b57cec5SDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
460b57cec5SDimitry Andric   unsigned NumParts = Size / NarrowSize;
470b57cec5SDimitry Andric   unsigned LeftoverSize = Size - NumParts * NarrowSize;
480b57cec5SDimitry Andric   assert(Size > NarrowSize);
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   if (LeftoverSize == 0)
510b57cec5SDimitry Andric     return {NumParts, 0};
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   if (NarrowTy.isVector()) {
540b57cec5SDimitry Andric     unsigned EltSize = OrigTy.getScalarSizeInBits();
550b57cec5SDimitry Andric     if (LeftoverSize % EltSize != 0)
560b57cec5SDimitry Andric       return {-1, -1};
570b57cec5SDimitry Andric     LeftoverTy = LLT::scalarOrVector(LeftoverSize / EltSize, EltSize);
580b57cec5SDimitry Andric   } else {
590b57cec5SDimitry Andric     LeftoverTy = LLT::scalar(LeftoverSize);
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   int NumLeftover = LeftoverSize / LeftoverTy.getSizeInBits();
630b57cec5SDimitry Andric   return std::make_pair(NumParts, NumLeftover);
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
66*5ffd83dbSDimitry Andric static Type *getFloatTypeForLLT(LLVMContext &Ctx, LLT Ty) {
67*5ffd83dbSDimitry Andric 
68*5ffd83dbSDimitry Andric   if (!Ty.isScalar())
69*5ffd83dbSDimitry Andric     return nullptr;
70*5ffd83dbSDimitry Andric 
71*5ffd83dbSDimitry Andric   switch (Ty.getSizeInBits()) {
72*5ffd83dbSDimitry Andric   case 16:
73*5ffd83dbSDimitry Andric     return Type::getHalfTy(Ctx);
74*5ffd83dbSDimitry Andric   case 32:
75*5ffd83dbSDimitry Andric     return Type::getFloatTy(Ctx);
76*5ffd83dbSDimitry Andric   case 64:
77*5ffd83dbSDimitry Andric     return Type::getDoubleTy(Ctx);
78*5ffd83dbSDimitry Andric   case 128:
79*5ffd83dbSDimitry Andric     return Type::getFP128Ty(Ctx);
80*5ffd83dbSDimitry Andric   default:
81*5ffd83dbSDimitry Andric     return nullptr;
82*5ffd83dbSDimitry Andric   }
83*5ffd83dbSDimitry Andric }
84*5ffd83dbSDimitry Andric 
850b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF,
860b57cec5SDimitry Andric                                  GISelChangeObserver &Observer,
870b57cec5SDimitry Andric                                  MachineIRBuilder &Builder)
88*5ffd83dbSDimitry Andric     : MIRBuilder(Builder), Observer(Observer), MRI(MF.getRegInfo()),
89*5ffd83dbSDimitry Andric       LI(*MF.getSubtarget().getLegalizerInfo()) {
900b57cec5SDimitry Andric   MIRBuilder.setChangeObserver(Observer);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, const LegalizerInfo &LI,
940b57cec5SDimitry Andric                                  GISelChangeObserver &Observer,
950b57cec5SDimitry Andric                                  MachineIRBuilder &B)
96*5ffd83dbSDimitry Andric     : MIRBuilder(B), Observer(Observer), MRI(MF.getRegInfo()), LI(LI) {
970b57cec5SDimitry Andric   MIRBuilder.setChangeObserver(Observer);
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
1000b57cec5SDimitry Andric LegalizerHelper::legalizeInstrStep(MachineInstr &MI) {
101*5ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing: " << MI);
102*5ffd83dbSDimitry Andric 
103*5ffd83dbSDimitry Andric   MIRBuilder.setInstrAndDebugLoc(MI);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_INTRINSIC ||
1060b57cec5SDimitry Andric       MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS)
107*5ffd83dbSDimitry Andric     return LI.legalizeIntrinsic(*this, MI) ? Legalized : UnableToLegalize;
1080b57cec5SDimitry Andric   auto Step = LI.getAction(MI, MRI);
1090b57cec5SDimitry Andric   switch (Step.Action) {
1100b57cec5SDimitry Andric   case Legal:
1110b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Already legal\n");
1120b57cec5SDimitry Andric     return AlreadyLegal;
1130b57cec5SDimitry Andric   case Libcall:
1140b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Convert to libcall\n");
1150b57cec5SDimitry Andric     return libcall(MI);
1160b57cec5SDimitry Andric   case NarrowScalar:
1170b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Narrow scalar\n");
1180b57cec5SDimitry Andric     return narrowScalar(MI, Step.TypeIdx, Step.NewType);
1190b57cec5SDimitry Andric   case WidenScalar:
1200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Widen scalar\n");
1210b57cec5SDimitry Andric     return widenScalar(MI, Step.TypeIdx, Step.NewType);
122*5ffd83dbSDimitry Andric   case Bitcast:
123*5ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << ".. Bitcast type\n");
124*5ffd83dbSDimitry Andric     return bitcast(MI, Step.TypeIdx, Step.NewType);
1250b57cec5SDimitry Andric   case Lower:
1260b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Lower\n");
1270b57cec5SDimitry Andric     return lower(MI, Step.TypeIdx, Step.NewType);
1280b57cec5SDimitry Andric   case FewerElements:
1290b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Reduce number of elements\n");
1300b57cec5SDimitry Andric     return fewerElementsVector(MI, Step.TypeIdx, Step.NewType);
1310b57cec5SDimitry Andric   case MoreElements:
1320b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Increase number of elements\n");
1330b57cec5SDimitry Andric     return moreElementsVector(MI, Step.TypeIdx, Step.NewType);
1340b57cec5SDimitry Andric   case Custom:
1350b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Custom legalization\n");
136*5ffd83dbSDimitry Andric     return LI.legalizeCustom(*this, MI) ? Legalized : UnableToLegalize;
1370b57cec5SDimitry Andric   default:
1380b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Unable to legalize\n");
1390b57cec5SDimitry Andric     return UnableToLegalize;
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric void LegalizerHelper::extractParts(Register Reg, LLT Ty, int NumParts,
1440b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &VRegs) {
1450b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i)
1460b57cec5SDimitry Andric     VRegs.push_back(MRI.createGenericVirtualRegister(Ty));
1470b57cec5SDimitry Andric   MIRBuilder.buildUnmerge(VRegs, Reg);
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric bool LegalizerHelper::extractParts(Register Reg, LLT RegTy,
1510b57cec5SDimitry Andric                                    LLT MainTy, LLT &LeftoverTy,
1520b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &VRegs,
1530b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &LeftoverRegs) {
1540b57cec5SDimitry Andric   assert(!LeftoverTy.isValid() && "this is an out argument");
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   unsigned RegSize = RegTy.getSizeInBits();
1570b57cec5SDimitry Andric   unsigned MainSize = MainTy.getSizeInBits();
1580b57cec5SDimitry Andric   unsigned NumParts = RegSize / MainSize;
1590b57cec5SDimitry Andric   unsigned LeftoverSize = RegSize - NumParts * MainSize;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   // Use an unmerge when possible.
1620b57cec5SDimitry Andric   if (LeftoverSize == 0) {
1630b57cec5SDimitry Andric     for (unsigned I = 0; I < NumParts; ++I)
1640b57cec5SDimitry Andric       VRegs.push_back(MRI.createGenericVirtualRegister(MainTy));
1650b57cec5SDimitry Andric     MIRBuilder.buildUnmerge(VRegs, Reg);
1660b57cec5SDimitry Andric     return true;
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   if (MainTy.isVector()) {
1700b57cec5SDimitry Andric     unsigned EltSize = MainTy.getScalarSizeInBits();
1710b57cec5SDimitry Andric     if (LeftoverSize % EltSize != 0)
1720b57cec5SDimitry Andric       return false;
1730b57cec5SDimitry Andric     LeftoverTy = LLT::scalarOrVector(LeftoverSize / EltSize, EltSize);
1740b57cec5SDimitry Andric   } else {
1750b57cec5SDimitry Andric     LeftoverTy = LLT::scalar(LeftoverSize);
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   // For irregular sizes, extract the individual parts.
1790b57cec5SDimitry Andric   for (unsigned I = 0; I != NumParts; ++I) {
1800b57cec5SDimitry Andric     Register NewReg = MRI.createGenericVirtualRegister(MainTy);
1810b57cec5SDimitry Andric     VRegs.push_back(NewReg);
1820b57cec5SDimitry Andric     MIRBuilder.buildExtract(NewReg, Reg, MainSize * I);
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   for (unsigned Offset = MainSize * NumParts; Offset < RegSize;
1860b57cec5SDimitry Andric        Offset += LeftoverSize) {
1870b57cec5SDimitry Andric     Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy);
1880b57cec5SDimitry Andric     LeftoverRegs.push_back(NewReg);
1890b57cec5SDimitry Andric     MIRBuilder.buildExtract(NewReg, Reg, Offset);
1900b57cec5SDimitry Andric   }
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   return true;
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric void LegalizerHelper::insertParts(Register DstReg,
1960b57cec5SDimitry Andric                                   LLT ResultTy, LLT PartTy,
1970b57cec5SDimitry Andric                                   ArrayRef<Register> PartRegs,
1980b57cec5SDimitry Andric                                   LLT LeftoverTy,
1990b57cec5SDimitry Andric                                   ArrayRef<Register> LeftoverRegs) {
2000b57cec5SDimitry Andric   if (!LeftoverTy.isValid()) {
2010b57cec5SDimitry Andric     assert(LeftoverRegs.empty());
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     if (!ResultTy.isVector()) {
2040b57cec5SDimitry Andric       MIRBuilder.buildMerge(DstReg, PartRegs);
2050b57cec5SDimitry Andric       return;
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric     if (PartTy.isVector())
2090b57cec5SDimitry Andric       MIRBuilder.buildConcatVectors(DstReg, PartRegs);
2100b57cec5SDimitry Andric     else
2110b57cec5SDimitry Andric       MIRBuilder.buildBuildVector(DstReg, PartRegs);
2120b57cec5SDimitry Andric     return;
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   unsigned PartSize = PartTy.getSizeInBits();
2160b57cec5SDimitry Andric   unsigned LeftoverPartSize = LeftoverTy.getSizeInBits();
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   Register CurResultReg = MRI.createGenericVirtualRegister(ResultTy);
2190b57cec5SDimitry Andric   MIRBuilder.buildUndef(CurResultReg);
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   unsigned Offset = 0;
2220b57cec5SDimitry Andric   for (Register PartReg : PartRegs) {
2230b57cec5SDimitry Andric     Register NewResultReg = MRI.createGenericVirtualRegister(ResultTy);
2240b57cec5SDimitry Andric     MIRBuilder.buildInsert(NewResultReg, CurResultReg, PartReg, Offset);
2250b57cec5SDimitry Andric     CurResultReg = NewResultReg;
2260b57cec5SDimitry Andric     Offset += PartSize;
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   for (unsigned I = 0, E = LeftoverRegs.size(); I != E; ++I) {
2300b57cec5SDimitry Andric     // Use the original output register for the final insert to avoid a copy.
2310b57cec5SDimitry Andric     Register NewResultReg = (I + 1 == E) ?
2320b57cec5SDimitry Andric       DstReg : MRI.createGenericVirtualRegister(ResultTy);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     MIRBuilder.buildInsert(NewResultReg, CurResultReg, LeftoverRegs[I], Offset);
2350b57cec5SDimitry Andric     CurResultReg = NewResultReg;
2360b57cec5SDimitry Andric     Offset += LeftoverPartSize;
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
240*5ffd83dbSDimitry Andric /// Return the result registers of G_UNMERGE_VALUES \p MI in \p Regs
241*5ffd83dbSDimitry Andric static void getUnmergeResults(SmallVectorImpl<Register> &Regs,
242*5ffd83dbSDimitry Andric                               const MachineInstr &MI) {
243*5ffd83dbSDimitry Andric   assert(MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES);
244*5ffd83dbSDimitry Andric 
245*5ffd83dbSDimitry Andric   const int NumResults = MI.getNumOperands() - 1;
246*5ffd83dbSDimitry Andric   Regs.resize(NumResults);
247*5ffd83dbSDimitry Andric   for (int I = 0; I != NumResults; ++I)
248*5ffd83dbSDimitry Andric     Regs[I] = MI.getOperand(I).getReg();
249*5ffd83dbSDimitry Andric }
250*5ffd83dbSDimitry Andric 
251*5ffd83dbSDimitry Andric LLT LegalizerHelper::extractGCDType(SmallVectorImpl<Register> &Parts, LLT DstTy,
252*5ffd83dbSDimitry Andric                                     LLT NarrowTy, Register SrcReg) {
253*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
254*5ffd83dbSDimitry Andric 
255*5ffd83dbSDimitry Andric   LLT GCDTy = getGCDType(DstTy, getGCDType(SrcTy, NarrowTy));
256*5ffd83dbSDimitry Andric   if (SrcTy == GCDTy) {
257*5ffd83dbSDimitry Andric     // If the source already evenly divides the result type, we don't need to do
258*5ffd83dbSDimitry Andric     // anything.
259*5ffd83dbSDimitry Andric     Parts.push_back(SrcReg);
260*5ffd83dbSDimitry Andric   } else {
261*5ffd83dbSDimitry Andric     // Need to split into common type sized pieces.
262*5ffd83dbSDimitry Andric     auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg);
263*5ffd83dbSDimitry Andric     getUnmergeResults(Parts, *Unmerge);
264*5ffd83dbSDimitry Andric   }
265*5ffd83dbSDimitry Andric 
266*5ffd83dbSDimitry Andric   return GCDTy;
267*5ffd83dbSDimitry Andric }
268*5ffd83dbSDimitry Andric 
269*5ffd83dbSDimitry Andric LLT LegalizerHelper::buildLCMMergePieces(LLT DstTy, LLT NarrowTy, LLT GCDTy,
270*5ffd83dbSDimitry Andric                                          SmallVectorImpl<Register> &VRegs,
271*5ffd83dbSDimitry Andric                                          unsigned PadStrategy) {
272*5ffd83dbSDimitry Andric   LLT LCMTy = getLCMType(DstTy, NarrowTy);
273*5ffd83dbSDimitry Andric 
274*5ffd83dbSDimitry Andric   int NumParts = LCMTy.getSizeInBits() / NarrowTy.getSizeInBits();
275*5ffd83dbSDimitry Andric   int NumSubParts = NarrowTy.getSizeInBits() / GCDTy.getSizeInBits();
276*5ffd83dbSDimitry Andric   int NumOrigSrc = VRegs.size();
277*5ffd83dbSDimitry Andric 
278*5ffd83dbSDimitry Andric   Register PadReg;
279*5ffd83dbSDimitry Andric 
280*5ffd83dbSDimitry Andric   // Get a value we can use to pad the source value if the sources won't evenly
281*5ffd83dbSDimitry Andric   // cover the result type.
282*5ffd83dbSDimitry Andric   if (NumOrigSrc < NumParts * NumSubParts) {
283*5ffd83dbSDimitry Andric     if (PadStrategy == TargetOpcode::G_ZEXT)
284*5ffd83dbSDimitry Andric       PadReg = MIRBuilder.buildConstant(GCDTy, 0).getReg(0);
285*5ffd83dbSDimitry Andric     else if (PadStrategy == TargetOpcode::G_ANYEXT)
286*5ffd83dbSDimitry Andric       PadReg = MIRBuilder.buildUndef(GCDTy).getReg(0);
287*5ffd83dbSDimitry Andric     else {
288*5ffd83dbSDimitry Andric       assert(PadStrategy == TargetOpcode::G_SEXT);
289*5ffd83dbSDimitry Andric 
290*5ffd83dbSDimitry Andric       // Shift the sign bit of the low register through the high register.
291*5ffd83dbSDimitry Andric       auto ShiftAmt =
292*5ffd83dbSDimitry Andric         MIRBuilder.buildConstant(LLT::scalar(64), GCDTy.getSizeInBits() - 1);
293*5ffd83dbSDimitry Andric       PadReg = MIRBuilder.buildAShr(GCDTy, VRegs.back(), ShiftAmt).getReg(0);
294*5ffd83dbSDimitry Andric     }
295*5ffd83dbSDimitry Andric   }
296*5ffd83dbSDimitry Andric 
297*5ffd83dbSDimitry Andric   // Registers for the final merge to be produced.
298*5ffd83dbSDimitry Andric   SmallVector<Register, 4> Remerge(NumParts);
299*5ffd83dbSDimitry Andric 
300*5ffd83dbSDimitry Andric   // Registers needed for intermediate merges, which will be merged into a
301*5ffd83dbSDimitry Andric   // source for Remerge.
302*5ffd83dbSDimitry Andric   SmallVector<Register, 4> SubMerge(NumSubParts);
303*5ffd83dbSDimitry Andric 
304*5ffd83dbSDimitry Andric   // Once we've fully read off the end of the original source bits, we can reuse
305*5ffd83dbSDimitry Andric   // the same high bits for remaining padding elements.
306*5ffd83dbSDimitry Andric   Register AllPadReg;
307*5ffd83dbSDimitry Andric 
308*5ffd83dbSDimitry Andric   // Build merges to the LCM type to cover the original result type.
309*5ffd83dbSDimitry Andric   for (int I = 0; I != NumParts; ++I) {
310*5ffd83dbSDimitry Andric     bool AllMergePartsArePadding = true;
311*5ffd83dbSDimitry Andric 
312*5ffd83dbSDimitry Andric     // Build the requested merges to the requested type.
313*5ffd83dbSDimitry Andric     for (int J = 0; J != NumSubParts; ++J) {
314*5ffd83dbSDimitry Andric       int Idx = I * NumSubParts + J;
315*5ffd83dbSDimitry Andric       if (Idx >= NumOrigSrc) {
316*5ffd83dbSDimitry Andric         SubMerge[J] = PadReg;
317*5ffd83dbSDimitry Andric         continue;
318*5ffd83dbSDimitry Andric       }
319*5ffd83dbSDimitry Andric 
320*5ffd83dbSDimitry Andric       SubMerge[J] = VRegs[Idx];
321*5ffd83dbSDimitry Andric 
322*5ffd83dbSDimitry Andric       // There are meaningful bits here we can't reuse later.
323*5ffd83dbSDimitry Andric       AllMergePartsArePadding = false;
324*5ffd83dbSDimitry Andric     }
325*5ffd83dbSDimitry Andric 
326*5ffd83dbSDimitry Andric     // If we've filled up a complete piece with padding bits, we can directly
327*5ffd83dbSDimitry Andric     // emit the natural sized constant if applicable, rather than a merge of
328*5ffd83dbSDimitry Andric     // smaller constants.
329*5ffd83dbSDimitry Andric     if (AllMergePartsArePadding && !AllPadReg) {
330*5ffd83dbSDimitry Andric       if (PadStrategy == TargetOpcode::G_ANYEXT)
331*5ffd83dbSDimitry Andric         AllPadReg = MIRBuilder.buildUndef(NarrowTy).getReg(0);
332*5ffd83dbSDimitry Andric       else if (PadStrategy == TargetOpcode::G_ZEXT)
333*5ffd83dbSDimitry Andric         AllPadReg = MIRBuilder.buildConstant(NarrowTy, 0).getReg(0);
334*5ffd83dbSDimitry Andric 
335*5ffd83dbSDimitry Andric       // If this is a sign extension, we can't materialize a trivial constant
336*5ffd83dbSDimitry Andric       // with the right type and have to produce a merge.
337*5ffd83dbSDimitry Andric     }
338*5ffd83dbSDimitry Andric 
339*5ffd83dbSDimitry Andric     if (AllPadReg) {
340*5ffd83dbSDimitry Andric       // Avoid creating additional instructions if we're just adding additional
341*5ffd83dbSDimitry Andric       // copies of padding bits.
342*5ffd83dbSDimitry Andric       Remerge[I] = AllPadReg;
343*5ffd83dbSDimitry Andric       continue;
344*5ffd83dbSDimitry Andric     }
345*5ffd83dbSDimitry Andric 
346*5ffd83dbSDimitry Andric     if (NumSubParts == 1)
347*5ffd83dbSDimitry Andric       Remerge[I] = SubMerge[0];
348*5ffd83dbSDimitry Andric     else
349*5ffd83dbSDimitry Andric       Remerge[I] = MIRBuilder.buildMerge(NarrowTy, SubMerge).getReg(0);
350*5ffd83dbSDimitry Andric 
351*5ffd83dbSDimitry Andric     // In the sign extend padding case, re-use the first all-signbit merge.
352*5ffd83dbSDimitry Andric     if (AllMergePartsArePadding && !AllPadReg)
353*5ffd83dbSDimitry Andric       AllPadReg = Remerge[I];
354*5ffd83dbSDimitry Andric   }
355*5ffd83dbSDimitry Andric 
356*5ffd83dbSDimitry Andric   VRegs = std::move(Remerge);
357*5ffd83dbSDimitry Andric   return LCMTy;
358*5ffd83dbSDimitry Andric }
359*5ffd83dbSDimitry Andric 
360*5ffd83dbSDimitry Andric void LegalizerHelper::buildWidenedRemergeToDst(Register DstReg, LLT LCMTy,
361*5ffd83dbSDimitry Andric                                                ArrayRef<Register> RemergeRegs) {
362*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
363*5ffd83dbSDimitry Andric 
364*5ffd83dbSDimitry Andric   // Create the merge to the widened source, and extract the relevant bits into
365*5ffd83dbSDimitry Andric   // the result.
366*5ffd83dbSDimitry Andric 
367*5ffd83dbSDimitry Andric   if (DstTy == LCMTy) {
368*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(DstReg, RemergeRegs);
369*5ffd83dbSDimitry Andric     return;
370*5ffd83dbSDimitry Andric   }
371*5ffd83dbSDimitry Andric 
372*5ffd83dbSDimitry Andric   auto Remerge = MIRBuilder.buildMerge(LCMTy, RemergeRegs);
373*5ffd83dbSDimitry Andric   if (DstTy.isScalar() && LCMTy.isScalar()) {
374*5ffd83dbSDimitry Andric     MIRBuilder.buildTrunc(DstReg, Remerge);
375*5ffd83dbSDimitry Andric     return;
376*5ffd83dbSDimitry Andric   }
377*5ffd83dbSDimitry Andric 
378*5ffd83dbSDimitry Andric   if (LCMTy.isVector()) {
379*5ffd83dbSDimitry Andric     MIRBuilder.buildExtract(DstReg, Remerge, 0);
380*5ffd83dbSDimitry Andric     return;
381*5ffd83dbSDimitry Andric   }
382*5ffd83dbSDimitry Andric 
383*5ffd83dbSDimitry Andric   llvm_unreachable("unhandled case");
384*5ffd83dbSDimitry Andric }
385*5ffd83dbSDimitry Andric 
3860b57cec5SDimitry Andric static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) {
387*5ffd83dbSDimitry Andric #define RTLIBCASE(LibcallPrefix)                                               \
388*5ffd83dbSDimitry Andric   do {                                                                         \
389*5ffd83dbSDimitry Andric     switch (Size) {                                                            \
390*5ffd83dbSDimitry Andric     case 32:                                                                   \
391*5ffd83dbSDimitry Andric       return RTLIB::LibcallPrefix##32;                                         \
392*5ffd83dbSDimitry Andric     case 64:                                                                   \
393*5ffd83dbSDimitry Andric       return RTLIB::LibcallPrefix##64;                                         \
394*5ffd83dbSDimitry Andric     case 128:                                                                  \
395*5ffd83dbSDimitry Andric       return RTLIB::LibcallPrefix##128;                                        \
396*5ffd83dbSDimitry Andric     default:                                                                   \
397*5ffd83dbSDimitry Andric       llvm_unreachable("unexpected size");                                     \
398*5ffd83dbSDimitry Andric     }                                                                          \
399*5ffd83dbSDimitry Andric   } while (0)
400*5ffd83dbSDimitry Andric 
401*5ffd83dbSDimitry Andric   assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
402*5ffd83dbSDimitry Andric 
4030b57cec5SDimitry Andric   switch (Opcode) {
4040b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
405*5ffd83dbSDimitry Andric     RTLIBCASE(SDIV_I);
4060b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
407*5ffd83dbSDimitry Andric     RTLIBCASE(UDIV_I);
4080b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
409*5ffd83dbSDimitry Andric     RTLIBCASE(SREM_I);
4100b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
411*5ffd83dbSDimitry Andric     RTLIBCASE(UREM_I);
4120b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
413*5ffd83dbSDimitry Andric     RTLIBCASE(CTLZ_I);
4140b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
415*5ffd83dbSDimitry Andric     RTLIBCASE(ADD_F);
4160b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
417*5ffd83dbSDimitry Andric     RTLIBCASE(SUB_F);
4180b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
419*5ffd83dbSDimitry Andric     RTLIBCASE(MUL_F);
4200b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
421*5ffd83dbSDimitry Andric     RTLIBCASE(DIV_F);
4220b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
423*5ffd83dbSDimitry Andric     RTLIBCASE(EXP_F);
4240b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
425*5ffd83dbSDimitry Andric     RTLIBCASE(EXP2_F);
4260b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
427*5ffd83dbSDimitry Andric     RTLIBCASE(REM_F);
4280b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
429*5ffd83dbSDimitry Andric     RTLIBCASE(POW_F);
4300b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
431*5ffd83dbSDimitry Andric     RTLIBCASE(FMA_F);
4320b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
433*5ffd83dbSDimitry Andric     RTLIBCASE(SIN_F);
4340b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
435*5ffd83dbSDimitry Andric     RTLIBCASE(COS_F);
4360b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
437*5ffd83dbSDimitry Andric     RTLIBCASE(LOG10_F);
4380b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
439*5ffd83dbSDimitry Andric     RTLIBCASE(LOG_F);
4400b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
441*5ffd83dbSDimitry Andric     RTLIBCASE(LOG2_F);
4420b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
443*5ffd83dbSDimitry Andric     RTLIBCASE(CEIL_F);
4440b57cec5SDimitry Andric   case TargetOpcode::G_FFLOOR:
445*5ffd83dbSDimitry Andric     RTLIBCASE(FLOOR_F);
446*5ffd83dbSDimitry Andric   case TargetOpcode::G_FMINNUM:
447*5ffd83dbSDimitry Andric     RTLIBCASE(FMIN_F);
448*5ffd83dbSDimitry Andric   case TargetOpcode::G_FMAXNUM:
449*5ffd83dbSDimitry Andric     RTLIBCASE(FMAX_F);
450*5ffd83dbSDimitry Andric   case TargetOpcode::G_FSQRT:
451*5ffd83dbSDimitry Andric     RTLIBCASE(SQRT_F);
452*5ffd83dbSDimitry Andric   case TargetOpcode::G_FRINT:
453*5ffd83dbSDimitry Andric     RTLIBCASE(RINT_F);
454*5ffd83dbSDimitry Andric   case TargetOpcode::G_FNEARBYINT:
455*5ffd83dbSDimitry Andric     RTLIBCASE(NEARBYINT_F);
4560b57cec5SDimitry Andric   }
4570b57cec5SDimitry Andric   llvm_unreachable("Unknown libcall function");
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
4608bcb0991SDimitry Andric /// True if an instruction is in tail position in its caller. Intended for
4618bcb0991SDimitry Andric /// legalizing libcalls as tail calls when possible.
4628bcb0991SDimitry Andric static bool isLibCallInTailPosition(MachineInstr &MI) {
463*5ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
464*5ffd83dbSDimitry Andric   const Function &F = MBB.getParent()->getFunction();
4658bcb0991SDimitry Andric 
4668bcb0991SDimitry Andric   // Conservatively require the attributes of the call to match those of
4678bcb0991SDimitry Andric   // the return. Ignore NoAlias and NonNull because they don't affect the
4688bcb0991SDimitry Andric   // call sequence.
4698bcb0991SDimitry Andric   AttributeList CallerAttrs = F.getAttributes();
4708bcb0991SDimitry Andric   if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex)
4718bcb0991SDimitry Andric           .removeAttribute(Attribute::NoAlias)
4728bcb0991SDimitry Andric           .removeAttribute(Attribute::NonNull)
4738bcb0991SDimitry Andric           .hasAttributes())
4748bcb0991SDimitry Andric     return false;
4758bcb0991SDimitry Andric 
4768bcb0991SDimitry Andric   // It's not safe to eliminate the sign / zero extension of the return value.
4778bcb0991SDimitry Andric   if (CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt) ||
4788bcb0991SDimitry Andric       CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
4798bcb0991SDimitry Andric     return false;
4808bcb0991SDimitry Andric 
4818bcb0991SDimitry Andric   // Only tail call if the following instruction is a standard return.
4828bcb0991SDimitry Andric   auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
483*5ffd83dbSDimitry Andric   auto Next = next_nodbg(MI.getIterator(), MBB.instr_end());
484*5ffd83dbSDimitry Andric   if (Next == MBB.instr_end() || TII.isTailCall(*Next) || !Next->isReturn())
4858bcb0991SDimitry Andric     return false;
4868bcb0991SDimitry Andric 
4878bcb0991SDimitry Andric   return true;
4888bcb0991SDimitry Andric }
4898bcb0991SDimitry Andric 
4900b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
491*5ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, const char *Name,
4920b57cec5SDimitry Andric                     const CallLowering::ArgInfo &Result,
493*5ffd83dbSDimitry Andric                     ArrayRef<CallLowering::ArgInfo> Args,
494*5ffd83dbSDimitry Andric                     const CallingConv::ID CC) {
4950b57cec5SDimitry Andric   auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering();
4960b57cec5SDimitry Andric 
4978bcb0991SDimitry Andric   CallLowering::CallLoweringInfo Info;
498*5ffd83dbSDimitry Andric   Info.CallConv = CC;
4998bcb0991SDimitry Andric   Info.Callee = MachineOperand::CreateES(Name);
5008bcb0991SDimitry Andric   Info.OrigRet = Result;
5018bcb0991SDimitry Andric   std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
5028bcb0991SDimitry Andric   if (!CLI.lowerCall(MIRBuilder, Info))
5030b57cec5SDimitry Andric     return LegalizerHelper::UnableToLegalize;
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   return LegalizerHelper::Legalized;
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric 
508*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
509*5ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall,
510*5ffd83dbSDimitry Andric                     const CallLowering::ArgInfo &Result,
511*5ffd83dbSDimitry Andric                     ArrayRef<CallLowering::ArgInfo> Args) {
512*5ffd83dbSDimitry Andric   auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
513*5ffd83dbSDimitry Andric   const char *Name = TLI.getLibcallName(Libcall);
514*5ffd83dbSDimitry Andric   const CallingConv::ID CC = TLI.getLibcallCallingConv(Libcall);
515*5ffd83dbSDimitry Andric   return createLibcall(MIRBuilder, Name, Result, Args, CC);
516*5ffd83dbSDimitry Andric }
517*5ffd83dbSDimitry Andric 
5180b57cec5SDimitry Andric // Useful for libcalls where all operands have the same type.
5190b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult
5200b57cec5SDimitry Andric simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size,
5210b57cec5SDimitry Andric               Type *OpType) {
5220b57cec5SDimitry Andric   auto Libcall = getRTLibDesc(MI.getOpcode(), Size);
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   SmallVector<CallLowering::ArgInfo, 3> Args;
5250b57cec5SDimitry Andric   for (unsigned i = 1; i < MI.getNumOperands(); i++)
5260b57cec5SDimitry Andric     Args.push_back({MI.getOperand(i).getReg(), OpType});
5270b57cec5SDimitry Andric   return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), OpType},
5280b57cec5SDimitry Andric                        Args);
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5318bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
5328bcb0991SDimitry Andric llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
5338bcb0991SDimitry Andric                        MachineInstr &MI) {
5348bcb0991SDimitry Andric   assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS);
5358bcb0991SDimitry Andric   auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
5368bcb0991SDimitry Andric 
5378bcb0991SDimitry Andric   SmallVector<CallLowering::ArgInfo, 3> Args;
5388bcb0991SDimitry Andric   // Add all the args, except for the last which is an imm denoting 'tail'.
5398bcb0991SDimitry Andric   for (unsigned i = 1; i < MI.getNumOperands() - 1; i++) {
5408bcb0991SDimitry Andric     Register Reg = MI.getOperand(i).getReg();
5418bcb0991SDimitry Andric 
5428bcb0991SDimitry Andric     // Need derive an IR type for call lowering.
5438bcb0991SDimitry Andric     LLT OpLLT = MRI.getType(Reg);
5448bcb0991SDimitry Andric     Type *OpTy = nullptr;
5458bcb0991SDimitry Andric     if (OpLLT.isPointer())
5468bcb0991SDimitry Andric       OpTy = Type::getInt8PtrTy(Ctx, OpLLT.getAddressSpace());
5478bcb0991SDimitry Andric     else
5488bcb0991SDimitry Andric       OpTy = IntegerType::get(Ctx, OpLLT.getSizeInBits());
5498bcb0991SDimitry Andric     Args.push_back({Reg, OpTy});
5508bcb0991SDimitry Andric   }
5518bcb0991SDimitry Andric 
5528bcb0991SDimitry Andric   auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering();
5538bcb0991SDimitry Andric   auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
5548bcb0991SDimitry Andric   Intrinsic::ID ID = MI.getOperand(0).getIntrinsicID();
5558bcb0991SDimitry Andric   RTLIB::Libcall RTLibcall;
5568bcb0991SDimitry Andric   switch (ID) {
5578bcb0991SDimitry Andric   case Intrinsic::memcpy:
5588bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMCPY;
5598bcb0991SDimitry Andric     break;
5608bcb0991SDimitry Andric   case Intrinsic::memset:
5618bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMSET;
5628bcb0991SDimitry Andric     break;
5638bcb0991SDimitry Andric   case Intrinsic::memmove:
5648bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMMOVE;
5658bcb0991SDimitry Andric     break;
5668bcb0991SDimitry Andric   default:
5678bcb0991SDimitry Andric     return LegalizerHelper::UnableToLegalize;
5688bcb0991SDimitry Andric   }
5698bcb0991SDimitry Andric   const char *Name = TLI.getLibcallName(RTLibcall);
5708bcb0991SDimitry Andric 
571*5ffd83dbSDimitry Andric   MIRBuilder.setInstrAndDebugLoc(MI);
5728bcb0991SDimitry Andric 
5738bcb0991SDimitry Andric   CallLowering::CallLoweringInfo Info;
5748bcb0991SDimitry Andric   Info.CallConv = TLI.getLibcallCallingConv(RTLibcall);
5758bcb0991SDimitry Andric   Info.Callee = MachineOperand::CreateES(Name);
5768bcb0991SDimitry Andric   Info.OrigRet = CallLowering::ArgInfo({0}, Type::getVoidTy(Ctx));
5778bcb0991SDimitry Andric   Info.IsTailCall = MI.getOperand(MI.getNumOperands() - 1).getImm() == 1 &&
5788bcb0991SDimitry Andric                     isLibCallInTailPosition(MI);
5798bcb0991SDimitry Andric 
5808bcb0991SDimitry Andric   std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
5818bcb0991SDimitry Andric   if (!CLI.lowerCall(MIRBuilder, Info))
5828bcb0991SDimitry Andric     return LegalizerHelper::UnableToLegalize;
5838bcb0991SDimitry Andric 
5848bcb0991SDimitry Andric   if (Info.LoweredTailCall) {
5858bcb0991SDimitry Andric     assert(Info.IsTailCall && "Lowered tail call when it wasn't a tail call?");
586*5ffd83dbSDimitry Andric     // We must have a return following the call (or debug insts) to get past
5878bcb0991SDimitry Andric     // isLibCallInTailPosition.
588*5ffd83dbSDimitry Andric     do {
589*5ffd83dbSDimitry Andric       MachineInstr *Next = MI.getNextNode();
590*5ffd83dbSDimitry Andric       assert(Next && (Next->isReturn() || Next->isDebugInstr()) &&
591*5ffd83dbSDimitry Andric              "Expected instr following MI to be return or debug inst?");
5928bcb0991SDimitry Andric       // We lowered a tail call, so the call is now the return from the block.
5938bcb0991SDimitry Andric       // Delete the old return.
594*5ffd83dbSDimitry Andric       Next->eraseFromParent();
595*5ffd83dbSDimitry Andric     } while (MI.getNextNode());
5968bcb0991SDimitry Andric   }
5978bcb0991SDimitry Andric 
5988bcb0991SDimitry Andric   return LegalizerHelper::Legalized;
5998bcb0991SDimitry Andric }
6008bcb0991SDimitry Andric 
6010b57cec5SDimitry Andric static RTLIB::Libcall getConvRTLibDesc(unsigned Opcode, Type *ToType,
6020b57cec5SDimitry Andric                                        Type *FromType) {
6030b57cec5SDimitry Andric   auto ToMVT = MVT::getVT(ToType);
6040b57cec5SDimitry Andric   auto FromMVT = MVT::getVT(FromType);
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   switch (Opcode) {
6070b57cec5SDimitry Andric   case TargetOpcode::G_FPEXT:
6080b57cec5SDimitry Andric     return RTLIB::getFPEXT(FromMVT, ToMVT);
6090b57cec5SDimitry Andric   case TargetOpcode::G_FPTRUNC:
6100b57cec5SDimitry Andric     return RTLIB::getFPROUND(FromMVT, ToMVT);
6110b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
6120b57cec5SDimitry Andric     return RTLIB::getFPTOSINT(FromMVT, ToMVT);
6130b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI:
6140b57cec5SDimitry Andric     return RTLIB::getFPTOUINT(FromMVT, ToMVT);
6150b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
6160b57cec5SDimitry Andric     return RTLIB::getSINTTOFP(FromMVT, ToMVT);
6170b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP:
6180b57cec5SDimitry Andric     return RTLIB::getUINTTOFP(FromMVT, ToMVT);
6190b57cec5SDimitry Andric   }
6200b57cec5SDimitry Andric   llvm_unreachable("Unsupported libcall function");
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult
6240b57cec5SDimitry Andric conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType,
6250b57cec5SDimitry Andric                   Type *FromType) {
6260b57cec5SDimitry Andric   RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType);
6270b57cec5SDimitry Andric   return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), ToType},
6280b57cec5SDimitry Andric                        {{MI.getOperand(1).getReg(), FromType}});
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
6320b57cec5SDimitry Andric LegalizerHelper::libcall(MachineInstr &MI) {
6330b57cec5SDimitry Andric   LLT LLTy = MRI.getType(MI.getOperand(0).getReg());
6340b57cec5SDimitry Andric   unsigned Size = LLTy.getSizeInBits();
6350b57cec5SDimitry Andric   auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric   switch (MI.getOpcode()) {
6380b57cec5SDimitry Andric   default:
6390b57cec5SDimitry Andric     return UnableToLegalize;
6400b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
6410b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
6420b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
6430b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
6440b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF: {
6450b57cec5SDimitry Andric     Type *HLTy = IntegerType::get(Ctx, Size);
6460b57cec5SDimitry Andric     auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy);
6470b57cec5SDimitry Andric     if (Status != Legalized)
6480b57cec5SDimitry Andric       return Status;
6490b57cec5SDimitry Andric     break;
6500b57cec5SDimitry Andric   }
6510b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
6520b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
6530b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
6540b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
6550b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
6560b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
6570b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
6580b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
6590b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
6600b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
6610b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
6620b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
6630b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
6640b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
6650b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
666*5ffd83dbSDimitry Andric   case TargetOpcode::G_FFLOOR:
667*5ffd83dbSDimitry Andric   case TargetOpcode::G_FMINNUM:
668*5ffd83dbSDimitry Andric   case TargetOpcode::G_FMAXNUM:
669*5ffd83dbSDimitry Andric   case TargetOpcode::G_FSQRT:
670*5ffd83dbSDimitry Andric   case TargetOpcode::G_FRINT:
671*5ffd83dbSDimitry Andric   case TargetOpcode::G_FNEARBYINT: {
672*5ffd83dbSDimitry Andric     Type *HLTy = getFloatTypeForLLT(Ctx, LLTy);
673*5ffd83dbSDimitry Andric     if (!HLTy || (Size != 32 && Size != 64 && Size != 128)) {
674*5ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "No libcall available for size " << Size << ".\n");
6750b57cec5SDimitry Andric       return UnableToLegalize;
6760b57cec5SDimitry Andric     }
6770b57cec5SDimitry Andric     auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy);
6780b57cec5SDimitry Andric     if (Status != Legalized)
6790b57cec5SDimitry Andric       return Status;
6800b57cec5SDimitry Andric     break;
6810b57cec5SDimitry Andric   }
682*5ffd83dbSDimitry Andric   case TargetOpcode::G_FPEXT:
6830b57cec5SDimitry Andric   case TargetOpcode::G_FPTRUNC: {
684*5ffd83dbSDimitry Andric     Type *FromTy = getFloatTypeForLLT(Ctx,  MRI.getType(MI.getOperand(1).getReg()));
685*5ffd83dbSDimitry Andric     Type *ToTy = getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(0).getReg()));
686*5ffd83dbSDimitry Andric     if (!FromTy || !ToTy)
6870b57cec5SDimitry Andric       return UnableToLegalize;
688*5ffd83dbSDimitry Andric     LegalizeResult Status = conversionLibcall(MI, MIRBuilder, ToTy, FromTy );
6890b57cec5SDimitry Andric     if (Status != Legalized)
6900b57cec5SDimitry Andric       return Status;
6910b57cec5SDimitry Andric     break;
6920b57cec5SDimitry Andric   }
6930b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
6940b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI: {
6950b57cec5SDimitry Andric     // FIXME: Support other types
6960b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
6970b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
6980b57cec5SDimitry Andric     if ((ToSize != 32 && ToSize != 64) || (FromSize != 32 && FromSize != 64))
6990b57cec5SDimitry Andric       return UnableToLegalize;
7000b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
7010b57cec5SDimitry Andric         MI, MIRBuilder,
7020b57cec5SDimitry Andric         ToSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx),
7030b57cec5SDimitry Andric         FromSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx));
7040b57cec5SDimitry Andric     if (Status != Legalized)
7050b57cec5SDimitry Andric       return Status;
7060b57cec5SDimitry Andric     break;
7070b57cec5SDimitry Andric   }
7080b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
7090b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP: {
7100b57cec5SDimitry Andric     // FIXME: Support other types
7110b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
7120b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
7130b57cec5SDimitry Andric     if ((FromSize != 32 && FromSize != 64) || (ToSize != 32 && ToSize != 64))
7140b57cec5SDimitry Andric       return UnableToLegalize;
7150b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
7160b57cec5SDimitry Andric         MI, MIRBuilder,
7170b57cec5SDimitry Andric         ToSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx),
7180b57cec5SDimitry Andric         FromSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx));
7190b57cec5SDimitry Andric     if (Status != Legalized)
7200b57cec5SDimitry Andric       return Status;
7210b57cec5SDimitry Andric     break;
7220b57cec5SDimitry Andric   }
7230b57cec5SDimitry Andric   }
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric   MI.eraseFromParent();
7260b57cec5SDimitry Andric   return Legalized;
7270b57cec5SDimitry Andric }
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
7300b57cec5SDimitry Andric                                                               unsigned TypeIdx,
7310b57cec5SDimitry Andric                                                               LLT NarrowTy) {
7320b57cec5SDimitry Andric   uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
7330b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric   switch (MI.getOpcode()) {
7360b57cec5SDimitry Andric   default:
7370b57cec5SDimitry Andric     return UnableToLegalize;
7380b57cec5SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF: {
739*5ffd83dbSDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
740*5ffd83dbSDimitry Andric     LLT DstTy = MRI.getType(DstReg);
741*5ffd83dbSDimitry Andric 
742*5ffd83dbSDimitry Andric     // If SizeOp0 is not an exact multiple of NarrowSize, emit
743*5ffd83dbSDimitry Andric     // G_ANYEXT(G_IMPLICIT_DEF). Cast result to vector if needed.
744*5ffd83dbSDimitry Andric     // FIXME: Although this would also be legal for the general case, it causes
745*5ffd83dbSDimitry Andric     //  a lot of regressions in the emitted code (superfluous COPYs, artifact
746*5ffd83dbSDimitry Andric     //  combines not being hit). This seems to be a problem related to the
747*5ffd83dbSDimitry Andric     //  artifact combiner.
748*5ffd83dbSDimitry Andric     if (SizeOp0 % NarrowSize != 0) {
749*5ffd83dbSDimitry Andric       LLT ImplicitTy = NarrowTy;
750*5ffd83dbSDimitry Andric       if (DstTy.isVector())
751*5ffd83dbSDimitry Andric         ImplicitTy = LLT::vector(DstTy.getNumElements(), ImplicitTy);
752*5ffd83dbSDimitry Andric 
753*5ffd83dbSDimitry Andric       Register ImplicitReg = MIRBuilder.buildUndef(ImplicitTy).getReg(0);
754*5ffd83dbSDimitry Andric       MIRBuilder.buildAnyExt(DstReg, ImplicitReg);
755*5ffd83dbSDimitry Andric 
756*5ffd83dbSDimitry Andric       MI.eraseFromParent();
757*5ffd83dbSDimitry Andric       return Legalized;
758*5ffd83dbSDimitry Andric     }
759*5ffd83dbSDimitry Andric 
7600b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric     SmallVector<Register, 2> DstRegs;
7630b57cec5SDimitry Andric     for (int i = 0; i < NumParts; ++i)
764*5ffd83dbSDimitry Andric       DstRegs.push_back(MIRBuilder.buildUndef(NarrowTy).getReg(0));
7650b57cec5SDimitry Andric 
766*5ffd83dbSDimitry Andric     if (DstTy.isVector())
7670b57cec5SDimitry Andric       MIRBuilder.buildBuildVector(DstReg, DstRegs);
7680b57cec5SDimitry Andric     else
7690b57cec5SDimitry Andric       MIRBuilder.buildMerge(DstReg, DstRegs);
7700b57cec5SDimitry Andric     MI.eraseFromParent();
7710b57cec5SDimitry Andric     return Legalized;
7720b57cec5SDimitry Andric   }
7730b57cec5SDimitry Andric   case TargetOpcode::G_CONSTANT: {
7740b57cec5SDimitry Andric     LLT Ty = MRI.getType(MI.getOperand(0).getReg());
7750b57cec5SDimitry Andric     const APInt &Val = MI.getOperand(1).getCImm()->getValue();
7760b57cec5SDimitry Andric     unsigned TotalSize = Ty.getSizeInBits();
7770b57cec5SDimitry Andric     unsigned NarrowSize = NarrowTy.getSizeInBits();
7780b57cec5SDimitry Andric     int NumParts = TotalSize / NarrowSize;
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric     SmallVector<Register, 4> PartRegs;
7810b57cec5SDimitry Andric     for (int I = 0; I != NumParts; ++I) {
7820b57cec5SDimitry Andric       unsigned Offset = I * NarrowSize;
7830b57cec5SDimitry Andric       auto K = MIRBuilder.buildConstant(NarrowTy,
7840b57cec5SDimitry Andric                                         Val.lshr(Offset).trunc(NarrowSize));
7850b57cec5SDimitry Andric       PartRegs.push_back(K.getReg(0));
7860b57cec5SDimitry Andric     }
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric     LLT LeftoverTy;
7890b57cec5SDimitry Andric     unsigned LeftoverBits = TotalSize - NumParts * NarrowSize;
7900b57cec5SDimitry Andric     SmallVector<Register, 1> LeftoverRegs;
7910b57cec5SDimitry Andric     if (LeftoverBits != 0) {
7920b57cec5SDimitry Andric       LeftoverTy = LLT::scalar(LeftoverBits);
7930b57cec5SDimitry Andric       auto K = MIRBuilder.buildConstant(
7940b57cec5SDimitry Andric         LeftoverTy,
7950b57cec5SDimitry Andric         Val.lshr(NumParts * NarrowSize).trunc(LeftoverBits));
7960b57cec5SDimitry Andric       LeftoverRegs.push_back(K.getReg(0));
7970b57cec5SDimitry Andric     }
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric     insertParts(MI.getOperand(0).getReg(),
8000b57cec5SDimitry Andric                 Ty, NarrowTy, PartRegs, LeftoverTy, LeftoverRegs);
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric     MI.eraseFromParent();
8030b57cec5SDimitry Andric     return Legalized;
8040b57cec5SDimitry Andric   }
805*5ffd83dbSDimitry Andric   case TargetOpcode::G_SEXT:
806*5ffd83dbSDimitry Andric   case TargetOpcode::G_ZEXT:
807*5ffd83dbSDimitry Andric   case TargetOpcode::G_ANYEXT:
808*5ffd83dbSDimitry Andric     return narrowScalarExt(MI, TypeIdx, NarrowTy);
8098bcb0991SDimitry Andric   case TargetOpcode::G_TRUNC: {
8108bcb0991SDimitry Andric     if (TypeIdx != 1)
8118bcb0991SDimitry Andric       return UnableToLegalize;
8128bcb0991SDimitry Andric 
8138bcb0991SDimitry Andric     uint64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
8148bcb0991SDimitry Andric     if (NarrowTy.getSizeInBits() * 2 != SizeOp1) {
8158bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't narrow trunc to type " << NarrowTy << "\n");
8168bcb0991SDimitry Andric       return UnableToLegalize;
8178bcb0991SDimitry Andric     }
8188bcb0991SDimitry Andric 
819*5ffd83dbSDimitry Andric     auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1));
820*5ffd83dbSDimitry Andric     MIRBuilder.buildCopy(MI.getOperand(0), Unmerge.getReg(0));
8218bcb0991SDimitry Andric     MI.eraseFromParent();
8228bcb0991SDimitry Andric     return Legalized;
8238bcb0991SDimitry Andric   }
8248bcb0991SDimitry Andric 
825*5ffd83dbSDimitry Andric   case TargetOpcode::G_FREEZE:
826*5ffd83dbSDimitry Andric     return reduceOperationWidth(MI, TypeIdx, NarrowTy);
827*5ffd83dbSDimitry Andric 
8280b57cec5SDimitry Andric   case TargetOpcode::G_ADD: {
8290b57cec5SDimitry Andric     // FIXME: add support for when SizeOp0 isn't an exact multiple of
8300b57cec5SDimitry Andric     // NarrowSize.
8310b57cec5SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
8320b57cec5SDimitry Andric       return UnableToLegalize;
8330b57cec5SDimitry Andric     // Expand in terms of carry-setting/consuming G_ADDE instructions.
8340b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
8350b57cec5SDimitry Andric 
8360b57cec5SDimitry Andric     SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
8370b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
8380b57cec5SDimitry Andric     extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
8390b57cec5SDimitry Andric 
8408bcb0991SDimitry Andric     Register CarryIn;
8410b57cec5SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
8420b57cec5SDimitry Andric       Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
8430b57cec5SDimitry Andric       Register CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
8440b57cec5SDimitry Andric 
8458bcb0991SDimitry Andric       if (i == 0)
8468bcb0991SDimitry Andric         MIRBuilder.buildUAddo(DstReg, CarryOut, Src1Regs[i], Src2Regs[i]);
8478bcb0991SDimitry Andric       else {
8480b57cec5SDimitry Andric         MIRBuilder.buildUAdde(DstReg, CarryOut, Src1Regs[i],
8490b57cec5SDimitry Andric                               Src2Regs[i], CarryIn);
8508bcb0991SDimitry Andric       }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric       DstRegs.push_back(DstReg);
8530b57cec5SDimitry Andric       CarryIn = CarryOut;
8540b57cec5SDimitry Andric     }
8550b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
8560b57cec5SDimitry Andric     if(MRI.getType(DstReg).isVector())
8570b57cec5SDimitry Andric       MIRBuilder.buildBuildVector(DstReg, DstRegs);
8580b57cec5SDimitry Andric     else
8590b57cec5SDimitry Andric       MIRBuilder.buildMerge(DstReg, DstRegs);
8600b57cec5SDimitry Andric     MI.eraseFromParent();
8610b57cec5SDimitry Andric     return Legalized;
8620b57cec5SDimitry Andric   }
8630b57cec5SDimitry Andric   case TargetOpcode::G_SUB: {
8640b57cec5SDimitry Andric     // FIXME: add support for when SizeOp0 isn't an exact multiple of
8650b57cec5SDimitry Andric     // NarrowSize.
8660b57cec5SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
8670b57cec5SDimitry Andric       return UnableToLegalize;
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric     SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
8720b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
8730b57cec5SDimitry Andric     extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
8760b57cec5SDimitry Andric     Register BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
8770b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_USUBO, {DstReg, BorrowOut},
8780b57cec5SDimitry Andric                           {Src1Regs[0], Src2Regs[0]});
8790b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
8800b57cec5SDimitry Andric     Register BorrowIn = BorrowOut;
8810b57cec5SDimitry Andric     for (int i = 1; i < NumParts; ++i) {
8820b57cec5SDimitry Andric       DstReg = MRI.createGenericVirtualRegister(NarrowTy);
8830b57cec5SDimitry Andric       BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_USUBE, {DstReg, BorrowOut},
8860b57cec5SDimitry Andric                             {Src1Regs[i], Src2Regs[i], BorrowIn});
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric       DstRegs.push_back(DstReg);
8890b57cec5SDimitry Andric       BorrowIn = BorrowOut;
8900b57cec5SDimitry Andric     }
891*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0), DstRegs);
8920b57cec5SDimitry Andric     MI.eraseFromParent();
8930b57cec5SDimitry Andric     return Legalized;
8940b57cec5SDimitry Andric   }
8950b57cec5SDimitry Andric   case TargetOpcode::G_MUL:
8960b57cec5SDimitry Andric   case TargetOpcode::G_UMULH:
8970b57cec5SDimitry Andric     return narrowScalarMul(MI, NarrowTy);
8980b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
8990b57cec5SDimitry Andric     return narrowScalarExtract(MI, TypeIdx, NarrowTy);
9000b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
9010b57cec5SDimitry Andric     return narrowScalarInsert(MI, TypeIdx, NarrowTy);
9020b57cec5SDimitry Andric   case TargetOpcode::G_LOAD: {
9030b57cec5SDimitry Andric     const auto &MMO = **MI.memoperands_begin();
9040b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
9050b57cec5SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
9060b57cec5SDimitry Andric     if (DstTy.isVector())
9070b57cec5SDimitry Andric       return UnableToLegalize;
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric     if (8 * MMO.getSize() != DstTy.getSizeInBits()) {
9100b57cec5SDimitry Andric       Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
9110b57cec5SDimitry Andric       auto &MMO = **MI.memoperands_begin();
912*5ffd83dbSDimitry Andric       MIRBuilder.buildLoad(TmpReg, MI.getOperand(1), MMO);
9130b57cec5SDimitry Andric       MIRBuilder.buildAnyExt(DstReg, TmpReg);
9140b57cec5SDimitry Andric       MI.eraseFromParent();
9150b57cec5SDimitry Andric       return Legalized;
9160b57cec5SDimitry Andric     }
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy);
9190b57cec5SDimitry Andric   }
9200b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD:
9210b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD: {
9220b57cec5SDimitry Andric     bool ZExt = MI.getOpcode() == TargetOpcode::G_ZEXTLOAD;
9230b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
9240b57cec5SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric     Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
9270b57cec5SDimitry Andric     auto &MMO = **MI.memoperands_begin();
9280b57cec5SDimitry Andric     if (MMO.getSizeInBits() == NarrowSize) {
9290b57cec5SDimitry Andric       MIRBuilder.buildLoad(TmpReg, PtrReg, MMO);
9300b57cec5SDimitry Andric     } else {
931*5ffd83dbSDimitry Andric       MIRBuilder.buildLoadInstr(MI.getOpcode(), TmpReg, PtrReg, MMO);
9320b57cec5SDimitry Andric     }
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric     if (ZExt)
9350b57cec5SDimitry Andric       MIRBuilder.buildZExt(DstReg, TmpReg);
9360b57cec5SDimitry Andric     else
9370b57cec5SDimitry Andric       MIRBuilder.buildSExt(DstReg, TmpReg);
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric     MI.eraseFromParent();
9400b57cec5SDimitry Andric     return Legalized;
9410b57cec5SDimitry Andric   }
9420b57cec5SDimitry Andric   case TargetOpcode::G_STORE: {
9430b57cec5SDimitry Andric     const auto &MMO = **MI.memoperands_begin();
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(0).getReg();
9460b57cec5SDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
9470b57cec5SDimitry Andric     if (SrcTy.isVector())
9480b57cec5SDimitry Andric       return UnableToLegalize;
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
9510b57cec5SDimitry Andric     unsigned HandledSize = NumParts * NarrowTy.getSizeInBits();
9520b57cec5SDimitry Andric     unsigned LeftoverBits = SrcTy.getSizeInBits() - HandledSize;
9530b57cec5SDimitry Andric     if (SrcTy.isVector() && LeftoverBits != 0)
9540b57cec5SDimitry Andric       return UnableToLegalize;
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric     if (8 * MMO.getSize() != SrcTy.getSizeInBits()) {
9570b57cec5SDimitry Andric       Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
9580b57cec5SDimitry Andric       auto &MMO = **MI.memoperands_begin();
9590b57cec5SDimitry Andric       MIRBuilder.buildTrunc(TmpReg, SrcReg);
960*5ffd83dbSDimitry Andric       MIRBuilder.buildStore(TmpReg, MI.getOperand(1), MMO);
9610b57cec5SDimitry Andric       MI.eraseFromParent();
9620b57cec5SDimitry Andric       return Legalized;
9630b57cec5SDimitry Andric     }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, 0, NarrowTy);
9660b57cec5SDimitry Andric   }
9670b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
9680b57cec5SDimitry Andric     return narrowScalarSelect(MI, TypeIdx, NarrowTy);
9690b57cec5SDimitry Andric   case TargetOpcode::G_AND:
9700b57cec5SDimitry Andric   case TargetOpcode::G_OR:
9710b57cec5SDimitry Andric   case TargetOpcode::G_XOR: {
9720b57cec5SDimitry Andric     // Legalize bitwise operation:
9730b57cec5SDimitry Andric     // A = BinOp<Ty> B, C
9740b57cec5SDimitry Andric     // into:
9750b57cec5SDimitry Andric     // B1, ..., BN = G_UNMERGE_VALUES B
9760b57cec5SDimitry Andric     // C1, ..., CN = G_UNMERGE_VALUES C
9770b57cec5SDimitry Andric     // A1 = BinOp<Ty/N> B1, C2
9780b57cec5SDimitry Andric     // ...
9790b57cec5SDimitry Andric     // AN = BinOp<Ty/N> BN, CN
9800b57cec5SDimitry Andric     // A = G_MERGE_VALUES A1, ..., AN
9810b57cec5SDimitry Andric     return narrowScalarBasic(MI, TypeIdx, NarrowTy);
9820b57cec5SDimitry Andric   }
9830b57cec5SDimitry Andric   case TargetOpcode::G_SHL:
9840b57cec5SDimitry Andric   case TargetOpcode::G_LSHR:
9850b57cec5SDimitry Andric   case TargetOpcode::G_ASHR:
9860b57cec5SDimitry Andric     return narrowScalarShift(MI, TypeIdx, NarrowTy);
9870b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
9880b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
9890b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
9900b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
9910b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP:
992*5ffd83dbSDimitry Andric     if (TypeIdx == 1)
993*5ffd83dbSDimitry Andric       switch (MI.getOpcode()) {
994*5ffd83dbSDimitry Andric       case TargetOpcode::G_CTLZ:
995*5ffd83dbSDimitry Andric       case TargetOpcode::G_CTLZ_ZERO_UNDEF:
996*5ffd83dbSDimitry Andric         return narrowScalarCTLZ(MI, TypeIdx, NarrowTy);
997*5ffd83dbSDimitry Andric       case TargetOpcode::G_CTTZ:
998*5ffd83dbSDimitry Andric       case TargetOpcode::G_CTTZ_ZERO_UNDEF:
999*5ffd83dbSDimitry Andric         return narrowScalarCTTZ(MI, TypeIdx, NarrowTy);
1000*5ffd83dbSDimitry Andric       case TargetOpcode::G_CTPOP:
1001*5ffd83dbSDimitry Andric         return narrowScalarCTPOP(MI, TypeIdx, NarrowTy);
1002*5ffd83dbSDimitry Andric       default:
1003*5ffd83dbSDimitry Andric         return UnableToLegalize;
1004*5ffd83dbSDimitry Andric       }
10050b57cec5SDimitry Andric 
10060b57cec5SDimitry Andric     Observer.changingInstr(MI);
10070b57cec5SDimitry Andric     narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT);
10080b57cec5SDimitry Andric     Observer.changedInstr(MI);
10090b57cec5SDimitry Andric     return Legalized;
10100b57cec5SDimitry Andric   case TargetOpcode::G_INTTOPTR:
10110b57cec5SDimitry Andric     if (TypeIdx != 1)
10120b57cec5SDimitry Andric       return UnableToLegalize;
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric     Observer.changingInstr(MI);
10150b57cec5SDimitry Andric     narrowScalarSrc(MI, NarrowTy, 1);
10160b57cec5SDimitry Andric     Observer.changedInstr(MI);
10170b57cec5SDimitry Andric     return Legalized;
10180b57cec5SDimitry Andric   case TargetOpcode::G_PTRTOINT:
10190b57cec5SDimitry Andric     if (TypeIdx != 0)
10200b57cec5SDimitry Andric       return UnableToLegalize;
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric     Observer.changingInstr(MI);
10230b57cec5SDimitry Andric     narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT);
10240b57cec5SDimitry Andric     Observer.changedInstr(MI);
10250b57cec5SDimitry Andric     return Legalized;
10260b57cec5SDimitry Andric   case TargetOpcode::G_PHI: {
10270b57cec5SDimitry Andric     unsigned NumParts = SizeOp0 / NarrowSize;
1028*5ffd83dbSDimitry Andric     SmallVector<Register, 2> DstRegs(NumParts);
1029*5ffd83dbSDimitry Andric     SmallVector<SmallVector<Register, 2>, 2> SrcRegs(MI.getNumOperands() / 2);
10300b57cec5SDimitry Andric     Observer.changingInstr(MI);
10310b57cec5SDimitry Andric     for (unsigned i = 1; i < MI.getNumOperands(); i += 2) {
10320b57cec5SDimitry Andric       MachineBasicBlock &OpMBB = *MI.getOperand(i + 1).getMBB();
10330b57cec5SDimitry Andric       MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
10340b57cec5SDimitry Andric       extractParts(MI.getOperand(i).getReg(), NarrowTy, NumParts,
10350b57cec5SDimitry Andric                    SrcRegs[i / 2]);
10360b57cec5SDimitry Andric     }
10370b57cec5SDimitry Andric     MachineBasicBlock &MBB = *MI.getParent();
10380b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MBB, MI);
10390b57cec5SDimitry Andric     for (unsigned i = 0; i < NumParts; ++i) {
10400b57cec5SDimitry Andric       DstRegs[i] = MRI.createGenericVirtualRegister(NarrowTy);
10410b57cec5SDimitry Andric       MachineInstrBuilder MIB =
10420b57cec5SDimitry Andric           MIRBuilder.buildInstr(TargetOpcode::G_PHI).addDef(DstRegs[i]);
10430b57cec5SDimitry Andric       for (unsigned j = 1; j < MI.getNumOperands(); j += 2)
10440b57cec5SDimitry Andric         MIB.addUse(SrcRegs[j / 2][i]).add(MI.getOperand(j + 1));
10450b57cec5SDimitry Andric     }
10468bcb0991SDimitry Andric     MIRBuilder.setInsertPt(MBB, MBB.getFirstNonPHI());
1047*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0), DstRegs);
10480b57cec5SDimitry Andric     Observer.changedInstr(MI);
10490b57cec5SDimitry Andric     MI.eraseFromParent();
10500b57cec5SDimitry Andric     return Legalized;
10510b57cec5SDimitry Andric   }
10520b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT_VECTOR_ELT:
10530b57cec5SDimitry Andric   case TargetOpcode::G_INSERT_VECTOR_ELT: {
10540b57cec5SDimitry Andric     if (TypeIdx != 2)
10550b57cec5SDimitry Andric       return UnableToLegalize;
10560b57cec5SDimitry Andric 
10570b57cec5SDimitry Andric     int OpIdx = MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT ? 2 : 3;
10580b57cec5SDimitry Andric     Observer.changingInstr(MI);
10590b57cec5SDimitry Andric     narrowScalarSrc(MI, NarrowTy, OpIdx);
10600b57cec5SDimitry Andric     Observer.changedInstr(MI);
10610b57cec5SDimitry Andric     return Legalized;
10620b57cec5SDimitry Andric   }
10630b57cec5SDimitry Andric   case TargetOpcode::G_ICMP: {
10640b57cec5SDimitry Andric     uint64_t SrcSize = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
10650b57cec5SDimitry Andric     if (NarrowSize * 2 != SrcSize)
10660b57cec5SDimitry Andric       return UnableToLegalize;
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric     Observer.changingInstr(MI);
10690b57cec5SDimitry Andric     Register LHSL = MRI.createGenericVirtualRegister(NarrowTy);
10700b57cec5SDimitry Andric     Register LHSH = MRI.createGenericVirtualRegister(NarrowTy);
1071*5ffd83dbSDimitry Andric     MIRBuilder.buildUnmerge({LHSL, LHSH}, MI.getOperand(2));
10720b57cec5SDimitry Andric 
10730b57cec5SDimitry Andric     Register RHSL = MRI.createGenericVirtualRegister(NarrowTy);
10740b57cec5SDimitry Andric     Register RHSH = MRI.createGenericVirtualRegister(NarrowTy);
1075*5ffd83dbSDimitry Andric     MIRBuilder.buildUnmerge({RHSL, RHSH}, MI.getOperand(3));
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric     CmpInst::Predicate Pred =
10780b57cec5SDimitry Andric         static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
10798bcb0991SDimitry Andric     LLT ResTy = MRI.getType(MI.getOperand(0).getReg());
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
10820b57cec5SDimitry Andric       MachineInstrBuilder XorL = MIRBuilder.buildXor(NarrowTy, LHSL, RHSL);
10830b57cec5SDimitry Andric       MachineInstrBuilder XorH = MIRBuilder.buildXor(NarrowTy, LHSH, RHSH);
10840b57cec5SDimitry Andric       MachineInstrBuilder Or = MIRBuilder.buildOr(NarrowTy, XorL, XorH);
10850b57cec5SDimitry Andric       MachineInstrBuilder Zero = MIRBuilder.buildConstant(NarrowTy, 0);
1086*5ffd83dbSDimitry Andric       MIRBuilder.buildICmp(Pred, MI.getOperand(0), Or, Zero);
10870b57cec5SDimitry Andric     } else {
10888bcb0991SDimitry Andric       MachineInstrBuilder CmpH = MIRBuilder.buildICmp(Pred, ResTy, LHSH, RHSH);
10890b57cec5SDimitry Andric       MachineInstrBuilder CmpHEQ =
10908bcb0991SDimitry Andric           MIRBuilder.buildICmp(CmpInst::Predicate::ICMP_EQ, ResTy, LHSH, RHSH);
10910b57cec5SDimitry Andric       MachineInstrBuilder CmpLU = MIRBuilder.buildICmp(
10928bcb0991SDimitry Andric           ICmpInst::getUnsignedPredicate(Pred), ResTy, LHSL, RHSL);
1093*5ffd83dbSDimitry Andric       MIRBuilder.buildSelect(MI.getOperand(0), CmpHEQ, CmpLU, CmpH);
10940b57cec5SDimitry Andric     }
10950b57cec5SDimitry Andric     Observer.changedInstr(MI);
10960b57cec5SDimitry Andric     MI.eraseFromParent();
10970b57cec5SDimitry Andric     return Legalized;
10980b57cec5SDimitry Andric   }
10998bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG: {
11008bcb0991SDimitry Andric     if (TypeIdx != 0)
11018bcb0991SDimitry Andric       return UnableToLegalize;
11028bcb0991SDimitry Andric 
11038bcb0991SDimitry Andric     int64_t SizeInBits = MI.getOperand(2).getImm();
11048bcb0991SDimitry Andric 
11058bcb0991SDimitry Andric     // So long as the new type has more bits than the bits we're extending we
11068bcb0991SDimitry Andric     // don't need to break it apart.
11078bcb0991SDimitry Andric     if (NarrowTy.getScalarSizeInBits() >= SizeInBits) {
11088bcb0991SDimitry Andric       Observer.changingInstr(MI);
11098bcb0991SDimitry Andric       // We don't lose any non-extension bits by truncating the src and
11108bcb0991SDimitry Andric       // sign-extending the dst.
11118bcb0991SDimitry Andric       MachineOperand &MO1 = MI.getOperand(1);
1112*5ffd83dbSDimitry Andric       auto TruncMIB = MIRBuilder.buildTrunc(NarrowTy, MO1);
1113*5ffd83dbSDimitry Andric       MO1.setReg(TruncMIB.getReg(0));
11148bcb0991SDimitry Andric 
11158bcb0991SDimitry Andric       MachineOperand &MO2 = MI.getOperand(0);
11168bcb0991SDimitry Andric       Register DstExt = MRI.createGenericVirtualRegister(NarrowTy);
11178bcb0991SDimitry Andric       MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1118*5ffd83dbSDimitry Andric       MIRBuilder.buildSExt(MO2, DstExt);
11198bcb0991SDimitry Andric       MO2.setReg(DstExt);
11208bcb0991SDimitry Andric       Observer.changedInstr(MI);
11218bcb0991SDimitry Andric       return Legalized;
11228bcb0991SDimitry Andric     }
11238bcb0991SDimitry Andric 
11248bcb0991SDimitry Andric     // Break it apart. Components below the extension point are unmodified. The
11258bcb0991SDimitry Andric     // component containing the extension point becomes a narrower SEXT_INREG.
11268bcb0991SDimitry Andric     // Components above it are ashr'd from the component containing the
11278bcb0991SDimitry Andric     // extension point.
11288bcb0991SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
11298bcb0991SDimitry Andric       return UnableToLegalize;
11308bcb0991SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
11318bcb0991SDimitry Andric 
11328bcb0991SDimitry Andric     // List the registers where the destination will be scattered.
11338bcb0991SDimitry Andric     SmallVector<Register, 2> DstRegs;
11348bcb0991SDimitry Andric     // List the registers where the source will be split.
11358bcb0991SDimitry Andric     SmallVector<Register, 2> SrcRegs;
11368bcb0991SDimitry Andric 
11378bcb0991SDimitry Andric     // Create all the temporary registers.
11388bcb0991SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
11398bcb0991SDimitry Andric       Register SrcReg = MRI.createGenericVirtualRegister(NarrowTy);
11408bcb0991SDimitry Andric 
11418bcb0991SDimitry Andric       SrcRegs.push_back(SrcReg);
11428bcb0991SDimitry Andric     }
11438bcb0991SDimitry Andric 
11448bcb0991SDimitry Andric     // Explode the big arguments into smaller chunks.
1145*5ffd83dbSDimitry Andric     MIRBuilder.buildUnmerge(SrcRegs, MI.getOperand(1));
11468bcb0991SDimitry Andric 
11478bcb0991SDimitry Andric     Register AshrCstReg =
11488bcb0991SDimitry Andric         MIRBuilder.buildConstant(NarrowTy, NarrowTy.getScalarSizeInBits() - 1)
1149*5ffd83dbSDimitry Andric             .getReg(0);
11508bcb0991SDimitry Andric     Register FullExtensionReg = 0;
11518bcb0991SDimitry Andric     Register PartialExtensionReg = 0;
11528bcb0991SDimitry Andric 
11538bcb0991SDimitry Andric     // Do the operation on each small part.
11548bcb0991SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
11558bcb0991SDimitry Andric       if ((i + 1) * NarrowTy.getScalarSizeInBits() < SizeInBits)
11568bcb0991SDimitry Andric         DstRegs.push_back(SrcRegs[i]);
11578bcb0991SDimitry Andric       else if (i * NarrowTy.getScalarSizeInBits() > SizeInBits) {
11588bcb0991SDimitry Andric         assert(PartialExtensionReg &&
11598bcb0991SDimitry Andric                "Expected to visit partial extension before full");
11608bcb0991SDimitry Andric         if (FullExtensionReg) {
11618bcb0991SDimitry Andric           DstRegs.push_back(FullExtensionReg);
11628bcb0991SDimitry Andric           continue;
11638bcb0991SDimitry Andric         }
1164*5ffd83dbSDimitry Andric         DstRegs.push_back(
1165*5ffd83dbSDimitry Andric             MIRBuilder.buildAShr(NarrowTy, PartialExtensionReg, AshrCstReg)
1166*5ffd83dbSDimitry Andric                 .getReg(0));
11678bcb0991SDimitry Andric         FullExtensionReg = DstRegs.back();
11688bcb0991SDimitry Andric       } else {
11698bcb0991SDimitry Andric         DstRegs.push_back(
11708bcb0991SDimitry Andric             MIRBuilder
11718bcb0991SDimitry Andric                 .buildInstr(
11728bcb0991SDimitry Andric                     TargetOpcode::G_SEXT_INREG, {NarrowTy},
11738bcb0991SDimitry Andric                     {SrcRegs[i], SizeInBits % NarrowTy.getScalarSizeInBits()})
1174*5ffd83dbSDimitry Andric                 .getReg(0));
11758bcb0991SDimitry Andric         PartialExtensionReg = DstRegs.back();
11768bcb0991SDimitry Andric       }
11778bcb0991SDimitry Andric     }
11788bcb0991SDimitry Andric 
11798bcb0991SDimitry Andric     // Gather the destination registers into the final destination.
11808bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
11818bcb0991SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
11828bcb0991SDimitry Andric     MI.eraseFromParent();
11838bcb0991SDimitry Andric     return Legalized;
11848bcb0991SDimitry Andric   }
1185480093f4SDimitry Andric   case TargetOpcode::G_BSWAP:
1186480093f4SDimitry Andric   case TargetOpcode::G_BITREVERSE: {
1187480093f4SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
1188480093f4SDimitry Andric       return UnableToLegalize;
1189480093f4SDimitry Andric 
1190480093f4SDimitry Andric     Observer.changingInstr(MI);
1191480093f4SDimitry Andric     SmallVector<Register, 2> SrcRegs, DstRegs;
1192480093f4SDimitry Andric     unsigned NumParts = SizeOp0 / NarrowSize;
1193480093f4SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
1194480093f4SDimitry Andric 
1195480093f4SDimitry Andric     for (unsigned i = 0; i < NumParts; ++i) {
1196480093f4SDimitry Andric       auto DstPart = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy},
1197480093f4SDimitry Andric                                            {SrcRegs[NumParts - 1 - i]});
1198480093f4SDimitry Andric       DstRegs.push_back(DstPart.getReg(0));
1199480093f4SDimitry Andric     }
1200480093f4SDimitry Andric 
1201*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0), DstRegs);
1202480093f4SDimitry Andric 
1203480093f4SDimitry Andric     Observer.changedInstr(MI);
1204480093f4SDimitry Andric     MI.eraseFromParent();
1205480093f4SDimitry Andric     return Legalized;
1206480093f4SDimitry Andric   }
1207*5ffd83dbSDimitry Andric   case TargetOpcode::G_PTRMASK: {
1208*5ffd83dbSDimitry Andric     if (TypeIdx != 1)
1209*5ffd83dbSDimitry Andric       return UnableToLegalize;
1210*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
1211*5ffd83dbSDimitry Andric     narrowScalarSrc(MI, NarrowTy, 2);
1212*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
1213*5ffd83dbSDimitry Andric     return Legalized;
12140b57cec5SDimitry Andric   }
12150b57cec5SDimitry Andric   }
1216*5ffd83dbSDimitry Andric }
1217*5ffd83dbSDimitry Andric 
1218*5ffd83dbSDimitry Andric Register LegalizerHelper::coerceToScalar(Register Val) {
1219*5ffd83dbSDimitry Andric   LLT Ty = MRI.getType(Val);
1220*5ffd83dbSDimitry Andric   if (Ty.isScalar())
1221*5ffd83dbSDimitry Andric     return Val;
1222*5ffd83dbSDimitry Andric 
1223*5ffd83dbSDimitry Andric   const DataLayout &DL = MIRBuilder.getDataLayout();
1224*5ffd83dbSDimitry Andric   LLT NewTy = LLT::scalar(Ty.getSizeInBits());
1225*5ffd83dbSDimitry Andric   if (Ty.isPointer()) {
1226*5ffd83dbSDimitry Andric     if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace()))
1227*5ffd83dbSDimitry Andric       return Register();
1228*5ffd83dbSDimitry Andric     return MIRBuilder.buildPtrToInt(NewTy, Val).getReg(0);
1229*5ffd83dbSDimitry Andric   }
1230*5ffd83dbSDimitry Andric 
1231*5ffd83dbSDimitry Andric   Register NewVal = Val;
1232*5ffd83dbSDimitry Andric 
1233*5ffd83dbSDimitry Andric   assert(Ty.isVector());
1234*5ffd83dbSDimitry Andric   LLT EltTy = Ty.getElementType();
1235*5ffd83dbSDimitry Andric   if (EltTy.isPointer())
1236*5ffd83dbSDimitry Andric     NewVal = MIRBuilder.buildPtrToInt(NewTy, NewVal).getReg(0);
1237*5ffd83dbSDimitry Andric   return MIRBuilder.buildBitcast(NewTy, NewVal).getReg(0);
1238*5ffd83dbSDimitry Andric }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy,
12410b57cec5SDimitry Andric                                      unsigned OpIdx, unsigned ExtOpcode) {
12420b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
1243*5ffd83dbSDimitry Andric   auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO});
1244*5ffd83dbSDimitry Andric   MO.setReg(ExtB.getReg(0));
12450b57cec5SDimitry Andric }
12460b57cec5SDimitry Andric 
12470b57cec5SDimitry Andric void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy,
12480b57cec5SDimitry Andric                                       unsigned OpIdx) {
12490b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
1250*5ffd83dbSDimitry Andric   auto ExtB = MIRBuilder.buildTrunc(NarrowTy, MO);
1251*5ffd83dbSDimitry Andric   MO.setReg(ExtB.getReg(0));
12520b57cec5SDimitry Andric }
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy,
12550b57cec5SDimitry Andric                                      unsigned OpIdx, unsigned TruncOpcode) {
12560b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
12570b57cec5SDimitry Andric   Register DstExt = MRI.createGenericVirtualRegister(WideTy);
12580b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1259*5ffd83dbSDimitry Andric   MIRBuilder.buildInstr(TruncOpcode, {MO}, {DstExt});
12600b57cec5SDimitry Andric   MO.setReg(DstExt);
12610b57cec5SDimitry Andric }
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry Andric void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy,
12640b57cec5SDimitry Andric                                       unsigned OpIdx, unsigned ExtOpcode) {
12650b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
12660b57cec5SDimitry Andric   Register DstTrunc = MRI.createGenericVirtualRegister(NarrowTy);
12670b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1268*5ffd83dbSDimitry Andric   MIRBuilder.buildInstr(ExtOpcode, {MO}, {DstTrunc});
12690b57cec5SDimitry Andric   MO.setReg(DstTrunc);
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorDst(MachineInstr &MI, LLT WideTy,
12730b57cec5SDimitry Andric                                             unsigned OpIdx) {
12740b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
12750b57cec5SDimitry Andric   Register DstExt = MRI.createGenericVirtualRegister(WideTy);
12760b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1277*5ffd83dbSDimitry Andric   MIRBuilder.buildExtract(MO, DstExt, 0);
12780b57cec5SDimitry Andric   MO.setReg(DstExt);
12790b57cec5SDimitry Andric }
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorSrc(MachineInstr &MI, LLT MoreTy,
12820b57cec5SDimitry Andric                                             unsigned OpIdx) {
12830b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric   LLT OldTy = MRI.getType(MO.getReg());
12860b57cec5SDimitry Andric   unsigned OldElts = OldTy.getNumElements();
12870b57cec5SDimitry Andric   unsigned NewElts = MoreTy.getNumElements();
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric   unsigned NumParts = NewElts / OldElts;
12900b57cec5SDimitry Andric 
12910b57cec5SDimitry Andric   // Use concat_vectors if the result is a multiple of the number of elements.
12920b57cec5SDimitry Andric   if (NumParts * OldElts == NewElts) {
12930b57cec5SDimitry Andric     SmallVector<Register, 8> Parts;
12940b57cec5SDimitry Andric     Parts.push_back(MO.getReg());
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric     Register ImpDef = MIRBuilder.buildUndef(OldTy).getReg(0);
12970b57cec5SDimitry Andric     for (unsigned I = 1; I != NumParts; ++I)
12980b57cec5SDimitry Andric       Parts.push_back(ImpDef);
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric     auto Concat = MIRBuilder.buildConcatVectors(MoreTy, Parts);
13010b57cec5SDimitry Andric     MO.setReg(Concat.getReg(0));
13020b57cec5SDimitry Andric     return;
13030b57cec5SDimitry Andric   }
13040b57cec5SDimitry Andric 
13050b57cec5SDimitry Andric   Register MoreReg = MRI.createGenericVirtualRegister(MoreTy);
13060b57cec5SDimitry Andric   Register ImpDef = MIRBuilder.buildUndef(MoreTy).getReg(0);
13070b57cec5SDimitry Andric   MIRBuilder.buildInsert(MoreReg, ImpDef, MO.getReg(), 0);
13080b57cec5SDimitry Andric   MO.setReg(MoreReg);
13090b57cec5SDimitry Andric }
13100b57cec5SDimitry Andric 
1311*5ffd83dbSDimitry Andric void LegalizerHelper::bitcastSrc(MachineInstr &MI, LLT CastTy, unsigned OpIdx) {
1312*5ffd83dbSDimitry Andric   MachineOperand &Op = MI.getOperand(OpIdx);
1313*5ffd83dbSDimitry Andric   Op.setReg(MIRBuilder.buildBitcast(CastTy, Op).getReg(0));
1314*5ffd83dbSDimitry Andric }
1315*5ffd83dbSDimitry Andric 
1316*5ffd83dbSDimitry Andric void LegalizerHelper::bitcastDst(MachineInstr &MI, LLT CastTy, unsigned OpIdx) {
1317*5ffd83dbSDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
1318*5ffd83dbSDimitry Andric   Register CastDst = MRI.createGenericVirtualRegister(CastTy);
1319*5ffd83dbSDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1320*5ffd83dbSDimitry Andric   MIRBuilder.buildBitcast(MO, CastDst);
1321*5ffd83dbSDimitry Andric   MO.setReg(CastDst);
1322*5ffd83dbSDimitry Andric }
1323*5ffd83dbSDimitry Andric 
13240b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
13250b57cec5SDimitry Andric LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx,
13260b57cec5SDimitry Andric                                         LLT WideTy) {
13270b57cec5SDimitry Andric   if (TypeIdx != 1)
13280b57cec5SDimitry Andric     return UnableToLegalize;
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
13310b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
13320b57cec5SDimitry Andric   if (DstTy.isVector())
13330b57cec5SDimitry Andric     return UnableToLegalize;
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric   Register Src1 = MI.getOperand(1).getReg();
13360b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src1);
13370b57cec5SDimitry Andric   const int DstSize = DstTy.getSizeInBits();
13380b57cec5SDimitry Andric   const int SrcSize = SrcTy.getSizeInBits();
13390b57cec5SDimitry Andric   const int WideSize = WideTy.getSizeInBits();
13400b57cec5SDimitry Andric   const int NumMerge = (DstSize + WideSize - 1) / WideSize;
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric   unsigned NumOps = MI.getNumOperands();
13430b57cec5SDimitry Andric   unsigned NumSrc = MI.getNumOperands() - 1;
13440b57cec5SDimitry Andric   unsigned PartSize = DstTy.getSizeInBits() / NumSrc;
13450b57cec5SDimitry Andric 
13460b57cec5SDimitry Andric   if (WideSize >= DstSize) {
13470b57cec5SDimitry Andric     // Directly pack the bits in the target type.
13480b57cec5SDimitry Andric     Register ResultReg = MIRBuilder.buildZExt(WideTy, Src1).getReg(0);
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric     for (unsigned I = 2; I != NumOps; ++I) {
13510b57cec5SDimitry Andric       const unsigned Offset = (I - 1) * PartSize;
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric       Register SrcReg = MI.getOperand(I).getReg();
13540b57cec5SDimitry Andric       assert(MRI.getType(SrcReg) == LLT::scalar(PartSize));
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric       auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg);
13570b57cec5SDimitry Andric 
13588bcb0991SDimitry Andric       Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg :
13590b57cec5SDimitry Andric         MRI.createGenericVirtualRegister(WideTy);
13600b57cec5SDimitry Andric 
13610b57cec5SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset);
13620b57cec5SDimitry Andric       auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt);
13630b57cec5SDimitry Andric       MIRBuilder.buildOr(NextResult, ResultReg, Shl);
13640b57cec5SDimitry Andric       ResultReg = NextResult;
13650b57cec5SDimitry Andric     }
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric     if (WideSize > DstSize)
13680b57cec5SDimitry Andric       MIRBuilder.buildTrunc(DstReg, ResultReg);
13698bcb0991SDimitry Andric     else if (DstTy.isPointer())
13708bcb0991SDimitry Andric       MIRBuilder.buildIntToPtr(DstReg, ResultReg);
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric     MI.eraseFromParent();
13730b57cec5SDimitry Andric     return Legalized;
13740b57cec5SDimitry Andric   }
13750b57cec5SDimitry Andric 
13760b57cec5SDimitry Andric   // Unmerge the original values to the GCD type, and recombine to the next
13770b57cec5SDimitry Andric   // multiple greater than the original type.
13780b57cec5SDimitry Andric   //
13790b57cec5SDimitry Andric   // %3:_(s12) = G_MERGE_VALUES %0:_(s4), %1:_(s4), %2:_(s4) -> s6
13800b57cec5SDimitry Andric   // %4:_(s2), %5:_(s2) = G_UNMERGE_VALUES %0
13810b57cec5SDimitry Andric   // %6:_(s2), %7:_(s2) = G_UNMERGE_VALUES %1
13820b57cec5SDimitry Andric   // %8:_(s2), %9:_(s2) = G_UNMERGE_VALUES %2
13830b57cec5SDimitry Andric   // %10:_(s6) = G_MERGE_VALUES %4, %5, %6
13840b57cec5SDimitry Andric   // %11:_(s6) = G_MERGE_VALUES %7, %8, %9
13850b57cec5SDimitry Andric   // %12:_(s12) = G_MERGE_VALUES %10, %11
13860b57cec5SDimitry Andric   //
13870b57cec5SDimitry Andric   // Padding with undef if necessary:
13880b57cec5SDimitry Andric   //
13890b57cec5SDimitry Andric   // %2:_(s8) = G_MERGE_VALUES %0:_(s4), %1:_(s4) -> s6
13900b57cec5SDimitry Andric   // %3:_(s2), %4:_(s2) = G_UNMERGE_VALUES %0
13910b57cec5SDimitry Andric   // %5:_(s2), %6:_(s2) = G_UNMERGE_VALUES %1
13920b57cec5SDimitry Andric   // %7:_(s2) = G_IMPLICIT_DEF
13930b57cec5SDimitry Andric   // %8:_(s6) = G_MERGE_VALUES %3, %4, %5
13940b57cec5SDimitry Andric   // %9:_(s6) = G_MERGE_VALUES %6, %7, %7
13950b57cec5SDimitry Andric   // %10:_(s12) = G_MERGE_VALUES %8, %9
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   const int GCD = greatestCommonDivisor(SrcSize, WideSize);
13980b57cec5SDimitry Andric   LLT GCDTy = LLT::scalar(GCD);
13990b57cec5SDimitry Andric 
14000b57cec5SDimitry Andric   SmallVector<Register, 8> Parts;
14010b57cec5SDimitry Andric   SmallVector<Register, 8> NewMergeRegs;
14020b57cec5SDimitry Andric   SmallVector<Register, 8> Unmerges;
14030b57cec5SDimitry Andric   LLT WideDstTy = LLT::scalar(NumMerge * WideSize);
14040b57cec5SDimitry Andric 
14050b57cec5SDimitry Andric   // Decompose the original operands if they don't evenly divide.
14060b57cec5SDimitry Andric   for (int I = 1, E = MI.getNumOperands(); I != E; ++I) {
14070b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
14080b57cec5SDimitry Andric     if (GCD == SrcSize) {
14090b57cec5SDimitry Andric       Unmerges.push_back(SrcReg);
14100b57cec5SDimitry Andric     } else {
14110b57cec5SDimitry Andric       auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg);
14120b57cec5SDimitry Andric       for (int J = 0, JE = Unmerge->getNumOperands() - 1; J != JE; ++J)
14130b57cec5SDimitry Andric         Unmerges.push_back(Unmerge.getReg(J));
14140b57cec5SDimitry Andric     }
14150b57cec5SDimitry Andric   }
14160b57cec5SDimitry Andric 
14170b57cec5SDimitry Andric   // Pad with undef to the next size that is a multiple of the requested size.
14180b57cec5SDimitry Andric   if (static_cast<int>(Unmerges.size()) != NumMerge * WideSize) {
14190b57cec5SDimitry Andric     Register UndefReg = MIRBuilder.buildUndef(GCDTy).getReg(0);
14200b57cec5SDimitry Andric     for (int I = Unmerges.size(); I != NumMerge * WideSize; ++I)
14210b57cec5SDimitry Andric       Unmerges.push_back(UndefReg);
14220b57cec5SDimitry Andric   }
14230b57cec5SDimitry Andric 
14240b57cec5SDimitry Andric   const int PartsPerGCD = WideSize / GCD;
14250b57cec5SDimitry Andric 
14260b57cec5SDimitry Andric   // Build merges of each piece.
14270b57cec5SDimitry Andric   ArrayRef<Register> Slicer(Unmerges);
14280b57cec5SDimitry Andric   for (int I = 0; I != NumMerge; ++I, Slicer = Slicer.drop_front(PartsPerGCD)) {
14290b57cec5SDimitry Andric     auto Merge = MIRBuilder.buildMerge(WideTy, Slicer.take_front(PartsPerGCD));
14300b57cec5SDimitry Andric     NewMergeRegs.push_back(Merge.getReg(0));
14310b57cec5SDimitry Andric   }
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   // A truncate may be necessary if the requested type doesn't evenly divide the
14340b57cec5SDimitry Andric   // original result type.
14350b57cec5SDimitry Andric   if (DstTy.getSizeInBits() == WideDstTy.getSizeInBits()) {
14360b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, NewMergeRegs);
14370b57cec5SDimitry Andric   } else {
14380b57cec5SDimitry Andric     auto FinalMerge = MIRBuilder.buildMerge(WideDstTy, NewMergeRegs);
14390b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, FinalMerge.getReg(0));
14400b57cec5SDimitry Andric   }
14410b57cec5SDimitry Andric 
14420b57cec5SDimitry Andric   MI.eraseFromParent();
14430b57cec5SDimitry Andric   return Legalized;
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
14470b57cec5SDimitry Andric LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx,
14480b57cec5SDimitry Andric                                           LLT WideTy) {
14490b57cec5SDimitry Andric   if (TypeIdx != 0)
14500b57cec5SDimitry Andric     return UnableToLegalize;
14510b57cec5SDimitry Andric 
1452*5ffd83dbSDimitry Andric   int NumDst = MI.getNumOperands() - 1;
14530b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(NumDst).getReg();
14540b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
1455*5ffd83dbSDimitry Andric   if (SrcTy.isVector())
14560b57cec5SDimitry Andric     return UnableToLegalize;
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric   Register Dst0Reg = MI.getOperand(0).getReg();
14590b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst0Reg);
14600b57cec5SDimitry Andric   if (!DstTy.isScalar())
14610b57cec5SDimitry Andric     return UnableToLegalize;
14620b57cec5SDimitry Andric 
1463*5ffd83dbSDimitry Andric   if (WideTy.getSizeInBits() >= SrcTy.getSizeInBits()) {
1464*5ffd83dbSDimitry Andric     if (SrcTy.isPointer()) {
1465*5ffd83dbSDimitry Andric       const DataLayout &DL = MIRBuilder.getDataLayout();
1466*5ffd83dbSDimitry Andric       if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) {
1467*5ffd83dbSDimitry Andric         LLVM_DEBUG(
1468*5ffd83dbSDimitry Andric             dbgs() << "Not casting non-integral address space integer\n");
1469*5ffd83dbSDimitry Andric         return UnableToLegalize;
14700b57cec5SDimitry Andric       }
14710b57cec5SDimitry Andric 
1472*5ffd83dbSDimitry Andric       SrcTy = LLT::scalar(SrcTy.getSizeInBits());
1473*5ffd83dbSDimitry Andric       SrcReg = MIRBuilder.buildPtrToInt(SrcTy, SrcReg).getReg(0);
1474*5ffd83dbSDimitry Andric     }
14750b57cec5SDimitry Andric 
1476*5ffd83dbSDimitry Andric     // Widen SrcTy to WideTy. This does not affect the result, but since the
1477*5ffd83dbSDimitry Andric     // user requested this size, it is probably better handled than SrcTy and
1478*5ffd83dbSDimitry Andric     // should reduce the total number of legalization artifacts
1479*5ffd83dbSDimitry Andric     if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) {
1480*5ffd83dbSDimitry Andric       SrcTy = WideTy;
1481*5ffd83dbSDimitry Andric       SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0);
1482*5ffd83dbSDimitry Andric     }
14830b57cec5SDimitry Andric 
1484*5ffd83dbSDimitry Andric     // Theres no unmerge type to target. Directly extract the bits from the
1485*5ffd83dbSDimitry Andric     // source type
1486*5ffd83dbSDimitry Andric     unsigned DstSize = DstTy.getSizeInBits();
14870b57cec5SDimitry Andric 
1488*5ffd83dbSDimitry Andric     MIRBuilder.buildTrunc(Dst0Reg, SrcReg);
1489*5ffd83dbSDimitry Andric     for (int I = 1; I != NumDst; ++I) {
1490*5ffd83dbSDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(SrcTy, DstSize * I);
1491*5ffd83dbSDimitry Andric       auto Shr = MIRBuilder.buildLShr(SrcTy, SrcReg, ShiftAmt);
1492*5ffd83dbSDimitry Andric       MIRBuilder.buildTrunc(MI.getOperand(I), Shr);
1493*5ffd83dbSDimitry Andric     }
1494*5ffd83dbSDimitry Andric 
1495*5ffd83dbSDimitry Andric     MI.eraseFromParent();
1496*5ffd83dbSDimitry Andric     return Legalized;
1497*5ffd83dbSDimitry Andric   }
1498*5ffd83dbSDimitry Andric 
1499*5ffd83dbSDimitry Andric   // Extend the source to a wider type.
1500*5ffd83dbSDimitry Andric   LLT LCMTy = getLCMType(SrcTy, WideTy);
1501*5ffd83dbSDimitry Andric 
1502*5ffd83dbSDimitry Andric   Register WideSrc = SrcReg;
1503*5ffd83dbSDimitry Andric   if (LCMTy.getSizeInBits() != SrcTy.getSizeInBits()) {
1504*5ffd83dbSDimitry Andric     // TODO: If this is an integral address space, cast to integer and anyext.
1505*5ffd83dbSDimitry Andric     if (SrcTy.isPointer()) {
1506*5ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "Widening pointer source types not implemented\n");
1507*5ffd83dbSDimitry Andric       return UnableToLegalize;
1508*5ffd83dbSDimitry Andric     }
1509*5ffd83dbSDimitry Andric 
1510*5ffd83dbSDimitry Andric     WideSrc = MIRBuilder.buildAnyExt(LCMTy, WideSrc).getReg(0);
1511*5ffd83dbSDimitry Andric   }
1512*5ffd83dbSDimitry Andric 
1513*5ffd83dbSDimitry Andric   auto Unmerge = MIRBuilder.buildUnmerge(WideTy, WideSrc);
1514*5ffd83dbSDimitry Andric 
1515*5ffd83dbSDimitry Andric   // Create a sequence of unmerges to the original results. since we may have
1516*5ffd83dbSDimitry Andric   // widened the source, we will need to pad the results with dead defs to cover
1517*5ffd83dbSDimitry Andric   // the source register.
1518*5ffd83dbSDimitry Andric   // e.g. widen s16 to s32:
1519*5ffd83dbSDimitry Andric   // %1:_(s16), %2:_(s16), %3:_(s16) = G_UNMERGE_VALUES %0:_(s48)
1520*5ffd83dbSDimitry Andric   //
1521*5ffd83dbSDimitry Andric   // =>
1522*5ffd83dbSDimitry Andric   //  %4:_(s64) = G_ANYEXT %0:_(s48)
1523*5ffd83dbSDimitry Andric   //  %5:_(s32), %6:_(s32) = G_UNMERGE_VALUES %4 ; Requested unmerge
1524*5ffd83dbSDimitry Andric   //  %1:_(s16), %2:_(s16) = G_UNMERGE_VALUES %5 ; unpack to original regs
1525*5ffd83dbSDimitry Andric   //  %3:_(s16), dead %7 = G_UNMERGE_VALUES %6 ; original reg + extra dead def
1526*5ffd83dbSDimitry Andric 
1527*5ffd83dbSDimitry Andric   const int NumUnmerge = Unmerge->getNumOperands() - 1;
1528*5ffd83dbSDimitry Andric   const int PartsPerUnmerge = WideTy.getSizeInBits() / DstTy.getSizeInBits();
1529*5ffd83dbSDimitry Andric 
1530*5ffd83dbSDimitry Andric   for (int I = 0; I != NumUnmerge; ++I) {
1531*5ffd83dbSDimitry Andric     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES);
1532*5ffd83dbSDimitry Andric 
1533*5ffd83dbSDimitry Andric     for (int J = 0; J != PartsPerUnmerge; ++J) {
1534*5ffd83dbSDimitry Andric       int Idx = I * PartsPerUnmerge + J;
1535*5ffd83dbSDimitry Andric       if (Idx < NumDst)
1536*5ffd83dbSDimitry Andric         MIB.addDef(MI.getOperand(Idx).getReg());
1537*5ffd83dbSDimitry Andric       else {
1538*5ffd83dbSDimitry Andric         // Create dead def for excess components.
1539*5ffd83dbSDimitry Andric         MIB.addDef(MRI.createGenericVirtualRegister(DstTy));
1540*5ffd83dbSDimitry Andric       }
1541*5ffd83dbSDimitry Andric     }
1542*5ffd83dbSDimitry Andric 
1543*5ffd83dbSDimitry Andric     MIB.addUse(Unmerge.getReg(I));
1544*5ffd83dbSDimitry Andric   }
1545*5ffd83dbSDimitry Andric 
1546*5ffd83dbSDimitry Andric   MI.eraseFromParent();
15470b57cec5SDimitry Andric   return Legalized;
15480b57cec5SDimitry Andric }
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
15510b57cec5SDimitry Andric LegalizerHelper::widenScalarExtract(MachineInstr &MI, unsigned TypeIdx,
15520b57cec5SDimitry Andric                                     LLT WideTy) {
15530b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
15540b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
15550b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
15560b57cec5SDimitry Andric 
15570b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
15580b57cec5SDimitry Andric   unsigned Offset = MI.getOperand(2).getImm();
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric   if (TypeIdx == 0) {
15610b57cec5SDimitry Andric     if (SrcTy.isVector() || DstTy.isVector())
15620b57cec5SDimitry Andric       return UnableToLegalize;
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric     SrcOp Src(SrcReg);
15650b57cec5SDimitry Andric     if (SrcTy.isPointer()) {
15660b57cec5SDimitry Andric       // Extracts from pointers can be handled only if they are really just
15670b57cec5SDimitry Andric       // simple integers.
15680b57cec5SDimitry Andric       const DataLayout &DL = MIRBuilder.getDataLayout();
15690b57cec5SDimitry Andric       if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace()))
15700b57cec5SDimitry Andric         return UnableToLegalize;
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric       LLT SrcAsIntTy = LLT::scalar(SrcTy.getSizeInBits());
15730b57cec5SDimitry Andric       Src = MIRBuilder.buildPtrToInt(SrcAsIntTy, Src);
15740b57cec5SDimitry Andric       SrcTy = SrcAsIntTy;
15750b57cec5SDimitry Andric     }
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric     if (DstTy.isPointer())
15780b57cec5SDimitry Andric       return UnableToLegalize;
15790b57cec5SDimitry Andric 
15800b57cec5SDimitry Andric     if (Offset == 0) {
15810b57cec5SDimitry Andric       // Avoid a shift in the degenerate case.
15820b57cec5SDimitry Andric       MIRBuilder.buildTrunc(DstReg,
15830b57cec5SDimitry Andric                             MIRBuilder.buildAnyExtOrTrunc(WideTy, Src));
15840b57cec5SDimitry Andric       MI.eraseFromParent();
15850b57cec5SDimitry Andric       return Legalized;
15860b57cec5SDimitry Andric     }
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric     // Do a shift in the source type.
15890b57cec5SDimitry Andric     LLT ShiftTy = SrcTy;
15900b57cec5SDimitry Andric     if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) {
15910b57cec5SDimitry Andric       Src = MIRBuilder.buildAnyExt(WideTy, Src);
15920b57cec5SDimitry Andric       ShiftTy = WideTy;
15930b57cec5SDimitry Andric     } else if (WideTy.getSizeInBits() > SrcTy.getSizeInBits())
15940b57cec5SDimitry Andric       return UnableToLegalize;
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric     auto LShr = MIRBuilder.buildLShr(
15970b57cec5SDimitry Andric       ShiftTy, Src, MIRBuilder.buildConstant(ShiftTy, Offset));
15980b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, LShr);
15990b57cec5SDimitry Andric     MI.eraseFromParent();
16000b57cec5SDimitry Andric     return Legalized;
16010b57cec5SDimitry Andric   }
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   if (SrcTy.isScalar()) {
16040b57cec5SDimitry Andric     Observer.changingInstr(MI);
16050b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
16060b57cec5SDimitry Andric     Observer.changedInstr(MI);
16070b57cec5SDimitry Andric     return Legalized;
16080b57cec5SDimitry Andric   }
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric   if (!SrcTy.isVector())
16110b57cec5SDimitry Andric     return UnableToLegalize;
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   if (DstTy != SrcTy.getElementType())
16140b57cec5SDimitry Andric     return UnableToLegalize;
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric   if (Offset % SrcTy.getScalarSizeInBits() != 0)
16170b57cec5SDimitry Andric     return UnableToLegalize;
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   Observer.changingInstr(MI);
16200b57cec5SDimitry Andric   widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
16210b57cec5SDimitry Andric 
16220b57cec5SDimitry Andric   MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) *
16230b57cec5SDimitry Andric                           Offset);
16240b57cec5SDimitry Andric   widenScalarDst(MI, WideTy.getScalarType(), 0);
16250b57cec5SDimitry Andric   Observer.changedInstr(MI);
16260b57cec5SDimitry Andric   return Legalized;
16270b57cec5SDimitry Andric }
16280b57cec5SDimitry Andric 
16290b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
16300b57cec5SDimitry Andric LegalizerHelper::widenScalarInsert(MachineInstr &MI, unsigned TypeIdx,
16310b57cec5SDimitry Andric                                    LLT WideTy) {
16320b57cec5SDimitry Andric   if (TypeIdx != 0)
16330b57cec5SDimitry Andric     return UnableToLegalize;
16340b57cec5SDimitry Andric   Observer.changingInstr(MI);
16350b57cec5SDimitry Andric   widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
16360b57cec5SDimitry Andric   widenScalarDst(MI, WideTy);
16370b57cec5SDimitry Andric   Observer.changedInstr(MI);
16380b57cec5SDimitry Andric   return Legalized;
16390b57cec5SDimitry Andric }
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
1642*5ffd83dbSDimitry Andric LegalizerHelper::widenScalarAddSubSat(MachineInstr &MI, unsigned TypeIdx,
1643*5ffd83dbSDimitry Andric                                       LLT WideTy) {
1644*5ffd83dbSDimitry Andric   bool IsSigned = MI.getOpcode() == TargetOpcode::G_SADDSAT ||
1645*5ffd83dbSDimitry Andric                   MI.getOpcode() == TargetOpcode::G_SSUBSAT;
1646*5ffd83dbSDimitry Andric   // We can convert this to:
1647*5ffd83dbSDimitry Andric   //   1. Any extend iN to iM
1648*5ffd83dbSDimitry Andric   //   2. SHL by M-N
1649*5ffd83dbSDimitry Andric   //   3. [US][ADD|SUB]SAT
1650*5ffd83dbSDimitry Andric   //   4. L/ASHR by M-N
1651*5ffd83dbSDimitry Andric   //
1652*5ffd83dbSDimitry Andric   // It may be more efficient to lower this to a min and a max operation in
1653*5ffd83dbSDimitry Andric   // the higher precision arithmetic if the promoted operation isn't legal,
1654*5ffd83dbSDimitry Andric   // but this decision is up to the target's lowering request.
1655*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
16560b57cec5SDimitry Andric 
1657*5ffd83dbSDimitry Andric   unsigned NewBits = WideTy.getScalarSizeInBits();
1658*5ffd83dbSDimitry Andric   unsigned SHLAmount = NewBits - MRI.getType(DstReg).getScalarSizeInBits();
1659*5ffd83dbSDimitry Andric 
1660*5ffd83dbSDimitry Andric   auto LHS = MIRBuilder.buildAnyExt(WideTy, MI.getOperand(1));
1661*5ffd83dbSDimitry Andric   auto RHS = MIRBuilder.buildAnyExt(WideTy, MI.getOperand(2));
1662*5ffd83dbSDimitry Andric   auto ShiftK = MIRBuilder.buildConstant(WideTy, SHLAmount);
1663*5ffd83dbSDimitry Andric   auto ShiftL = MIRBuilder.buildShl(WideTy, LHS, ShiftK);
1664*5ffd83dbSDimitry Andric   auto ShiftR = MIRBuilder.buildShl(WideTy, RHS, ShiftK);
1665*5ffd83dbSDimitry Andric 
1666*5ffd83dbSDimitry Andric   auto WideInst = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy},
1667*5ffd83dbSDimitry Andric                                         {ShiftL, ShiftR}, MI.getFlags());
1668*5ffd83dbSDimitry Andric 
1669*5ffd83dbSDimitry Andric   // Use a shift that will preserve the number of sign bits when the trunc is
1670*5ffd83dbSDimitry Andric   // folded away.
1671*5ffd83dbSDimitry Andric   auto Result = IsSigned ? MIRBuilder.buildAShr(WideTy, WideInst, ShiftK)
1672*5ffd83dbSDimitry Andric                          : MIRBuilder.buildLShr(WideTy, WideInst, ShiftK);
1673*5ffd83dbSDimitry Andric 
1674*5ffd83dbSDimitry Andric   MIRBuilder.buildTrunc(DstReg, Result);
1675*5ffd83dbSDimitry Andric   MI.eraseFromParent();
1676*5ffd83dbSDimitry Andric   return Legalized;
1677*5ffd83dbSDimitry Andric }
1678*5ffd83dbSDimitry Andric 
1679*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
1680*5ffd83dbSDimitry Andric LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
16810b57cec5SDimitry Andric   switch (MI.getOpcode()) {
16820b57cec5SDimitry Andric   default:
16830b57cec5SDimitry Andric     return UnableToLegalize;
16840b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
16850b57cec5SDimitry Andric     return widenScalarExtract(MI, TypeIdx, WideTy);
16860b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
16870b57cec5SDimitry Andric     return widenScalarInsert(MI, TypeIdx, WideTy);
16880b57cec5SDimitry Andric   case TargetOpcode::G_MERGE_VALUES:
16890b57cec5SDimitry Andric     return widenScalarMergeValues(MI, TypeIdx, WideTy);
16900b57cec5SDimitry Andric   case TargetOpcode::G_UNMERGE_VALUES:
16910b57cec5SDimitry Andric     return widenScalarUnmergeValues(MI, TypeIdx, WideTy);
16920b57cec5SDimitry Andric   case TargetOpcode::G_UADDO:
16930b57cec5SDimitry Andric   case TargetOpcode::G_USUBO: {
16940b57cec5SDimitry Andric     if (TypeIdx == 1)
16950b57cec5SDimitry Andric       return UnableToLegalize; // TODO
1696*5ffd83dbSDimitry Andric     auto LHSZext = MIRBuilder.buildZExt(WideTy, MI.getOperand(2));
1697*5ffd83dbSDimitry Andric     auto RHSZext = MIRBuilder.buildZExt(WideTy, MI.getOperand(3));
16980b57cec5SDimitry Andric     unsigned Opcode = MI.getOpcode() == TargetOpcode::G_UADDO
16990b57cec5SDimitry Andric                           ? TargetOpcode::G_ADD
17000b57cec5SDimitry Andric                           : TargetOpcode::G_SUB;
17010b57cec5SDimitry Andric     // Do the arithmetic in the larger type.
17020b57cec5SDimitry Andric     auto NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSZext, RHSZext});
17030b57cec5SDimitry Andric     LLT OrigTy = MRI.getType(MI.getOperand(0).getReg());
1704*5ffd83dbSDimitry Andric     APInt Mask =
1705*5ffd83dbSDimitry Andric         APInt::getLowBitsSet(WideTy.getSizeInBits(), OrigTy.getSizeInBits());
1706*5ffd83dbSDimitry Andric     auto AndOp = MIRBuilder.buildAnd(
1707*5ffd83dbSDimitry Andric         WideTy, NewOp, MIRBuilder.buildConstant(WideTy, Mask));
17080b57cec5SDimitry Andric     // There is no overflow if the AndOp is the same as NewOp.
1709*5ffd83dbSDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1), NewOp, AndOp);
17100b57cec5SDimitry Andric     // Now trunc the NewOp to the original result.
1711*5ffd83dbSDimitry Andric     MIRBuilder.buildTrunc(MI.getOperand(0), NewOp);
17120b57cec5SDimitry Andric     MI.eraseFromParent();
17130b57cec5SDimitry Andric     return Legalized;
17140b57cec5SDimitry Andric   }
1715*5ffd83dbSDimitry Andric   case TargetOpcode::G_SADDSAT:
1716*5ffd83dbSDimitry Andric   case TargetOpcode::G_SSUBSAT:
1717*5ffd83dbSDimitry Andric   case TargetOpcode::G_UADDSAT:
1718*5ffd83dbSDimitry Andric   case TargetOpcode::G_USUBSAT:
1719*5ffd83dbSDimitry Andric     return widenScalarAddSubSat(MI, TypeIdx, WideTy);
17200b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
17210b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
17220b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
17230b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
17240b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP: {
17250b57cec5SDimitry Andric     if (TypeIdx == 0) {
17260b57cec5SDimitry Andric       Observer.changingInstr(MI);
17270b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
17280b57cec5SDimitry Andric       Observer.changedInstr(MI);
17290b57cec5SDimitry Andric       return Legalized;
17300b57cec5SDimitry Andric     }
17310b57cec5SDimitry Andric 
17320b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
17330b57cec5SDimitry Andric 
17340b57cec5SDimitry Andric     // First ZEXT the input.
17350b57cec5SDimitry Andric     auto MIBSrc = MIRBuilder.buildZExt(WideTy, SrcReg);
17360b57cec5SDimitry Andric     LLT CurTy = MRI.getType(SrcReg);
17370b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_CTTZ) {
17380b57cec5SDimitry Andric       // The count is the same in the larger type except if the original
17390b57cec5SDimitry Andric       // value was zero.  This can be handled by setting the bit just off
17400b57cec5SDimitry Andric       // the top of the original type.
17410b57cec5SDimitry Andric       auto TopBit =
17420b57cec5SDimitry Andric           APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits());
17430b57cec5SDimitry Andric       MIBSrc = MIRBuilder.buildOr(
17440b57cec5SDimitry Andric         WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit));
17450b57cec5SDimitry Andric     }
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric     // Perform the operation at the larger size.
17480b57cec5SDimitry Andric     auto MIBNewOp = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, {MIBSrc});
17490b57cec5SDimitry Andric     // This is already the correct result for CTPOP and CTTZs
17500b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_CTLZ ||
17510b57cec5SDimitry Andric         MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) {
17520b57cec5SDimitry Andric       // The correct result is NewOp - (Difference in widety and current ty).
17530b57cec5SDimitry Andric       unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits();
1754*5ffd83dbSDimitry Andric       MIBNewOp = MIRBuilder.buildSub(
1755*5ffd83dbSDimitry Andric           WideTy, MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff));
17560b57cec5SDimitry Andric     }
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric     MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp);
17590b57cec5SDimitry Andric     MI.eraseFromParent();
17600b57cec5SDimitry Andric     return Legalized;
17610b57cec5SDimitry Andric   }
17620b57cec5SDimitry Andric   case TargetOpcode::G_BSWAP: {
17630b57cec5SDimitry Andric     Observer.changingInstr(MI);
17640b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric     Register ShrReg = MRI.createGenericVirtualRegister(WideTy);
17670b57cec5SDimitry Andric     Register DstExt = MRI.createGenericVirtualRegister(WideTy);
17680b57cec5SDimitry Andric     Register ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy);
17690b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric     MI.getOperand(0).setReg(DstExt);
17720b57cec5SDimitry Andric 
17730b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
17740b57cec5SDimitry Andric 
17750b57cec5SDimitry Andric     LLT Ty = MRI.getType(DstReg);
17760b57cec5SDimitry Andric     unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits();
17770b57cec5SDimitry Andric     MIRBuilder.buildConstant(ShiftAmtReg, DiffBits);
1778*5ffd83dbSDimitry Andric     MIRBuilder.buildLShr(ShrReg, DstExt, ShiftAmtReg);
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, ShrReg);
17810b57cec5SDimitry Andric     Observer.changedInstr(MI);
17820b57cec5SDimitry Andric     return Legalized;
17830b57cec5SDimitry Andric   }
17848bcb0991SDimitry Andric   case TargetOpcode::G_BITREVERSE: {
17858bcb0991SDimitry Andric     Observer.changingInstr(MI);
17868bcb0991SDimitry Andric 
17878bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
17888bcb0991SDimitry Andric     LLT Ty = MRI.getType(DstReg);
17898bcb0991SDimitry Andric     unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits();
17908bcb0991SDimitry Andric 
17918bcb0991SDimitry Andric     Register DstExt = MRI.createGenericVirtualRegister(WideTy);
17928bcb0991SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
17938bcb0991SDimitry Andric     MI.getOperand(0).setReg(DstExt);
17948bcb0991SDimitry Andric     MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
17958bcb0991SDimitry Andric 
17968bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(WideTy, DiffBits);
17978bcb0991SDimitry Andric     auto Shift = MIRBuilder.buildLShr(WideTy, DstExt, ShiftAmt);
17988bcb0991SDimitry Andric     MIRBuilder.buildTrunc(DstReg, Shift);
17998bcb0991SDimitry Andric     Observer.changedInstr(MI);
18008bcb0991SDimitry Andric     return Legalized;
18018bcb0991SDimitry Andric   }
1802*5ffd83dbSDimitry Andric   case TargetOpcode::G_FREEZE:
1803*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
1804*5ffd83dbSDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
1805*5ffd83dbSDimitry Andric     widenScalarDst(MI, WideTy);
1806*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
1807*5ffd83dbSDimitry Andric     return Legalized;
1808*5ffd83dbSDimitry Andric 
18090b57cec5SDimitry Andric   case TargetOpcode::G_ADD:
18100b57cec5SDimitry Andric   case TargetOpcode::G_AND:
18110b57cec5SDimitry Andric   case TargetOpcode::G_MUL:
18120b57cec5SDimitry Andric   case TargetOpcode::G_OR:
18130b57cec5SDimitry Andric   case TargetOpcode::G_XOR:
18140b57cec5SDimitry Andric   case TargetOpcode::G_SUB:
18150b57cec5SDimitry Andric     // Perform operation at larger width (any extension is fines here, high bits
18160b57cec5SDimitry Andric     // don't affect the result) and then truncate the result back to the
18170b57cec5SDimitry Andric     // original type.
18180b57cec5SDimitry Andric     Observer.changingInstr(MI);
18190b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
18200b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
18210b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
18220b57cec5SDimitry Andric     Observer.changedInstr(MI);
18230b57cec5SDimitry Andric     return Legalized;
18240b57cec5SDimitry Andric 
18250b57cec5SDimitry Andric   case TargetOpcode::G_SHL:
18260b57cec5SDimitry Andric     Observer.changingInstr(MI);
18270b57cec5SDimitry Andric 
18280b57cec5SDimitry Andric     if (TypeIdx == 0) {
18290b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
18300b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
18310b57cec5SDimitry Andric     } else {
18320b57cec5SDimitry Andric       assert(TypeIdx == 1);
18330b57cec5SDimitry Andric       // The "number of bits to shift" operand must preserve its value as an
18340b57cec5SDimitry Andric       // unsigned integer:
18350b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
18360b57cec5SDimitry Andric     }
18370b57cec5SDimitry Andric 
18380b57cec5SDimitry Andric     Observer.changedInstr(MI);
18390b57cec5SDimitry Andric     return Legalized;
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
18420b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
18430b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
18440b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
18450b57cec5SDimitry Andric     Observer.changingInstr(MI);
18460b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT);
18470b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
18480b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
18490b57cec5SDimitry Andric     Observer.changedInstr(MI);
18500b57cec5SDimitry Andric     return Legalized;
18510b57cec5SDimitry Andric 
18520b57cec5SDimitry Andric   case TargetOpcode::G_ASHR:
18530b57cec5SDimitry Andric   case TargetOpcode::G_LSHR:
18540b57cec5SDimitry Andric     Observer.changingInstr(MI);
18550b57cec5SDimitry Andric 
18560b57cec5SDimitry Andric     if (TypeIdx == 0) {
18570b57cec5SDimitry Andric       unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ?
18580b57cec5SDimitry Andric         TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, CvtOp);
18610b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
18620b57cec5SDimitry Andric     } else {
18630b57cec5SDimitry Andric       assert(TypeIdx == 1);
18640b57cec5SDimitry Andric       // The "number of bits to shift" operand must preserve its value as an
18650b57cec5SDimitry Andric       // unsigned integer:
18660b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
18670b57cec5SDimitry Andric     }
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric     Observer.changedInstr(MI);
18700b57cec5SDimitry Andric     return Legalized;
18710b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
18720b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
18730b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
18740b57cec5SDimitry Andric   case TargetOpcode::G_UMAX:
18750b57cec5SDimitry Andric     Observer.changingInstr(MI);
18760b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
18770b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
18780b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
18790b57cec5SDimitry Andric     Observer.changedInstr(MI);
18800b57cec5SDimitry Andric     return Legalized;
18810b57cec5SDimitry Andric 
18820b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
18830b57cec5SDimitry Andric     Observer.changingInstr(MI);
18840b57cec5SDimitry Andric     if (TypeIdx == 0) {
18850b57cec5SDimitry Andric       // Perform operation at larger width (any extension is fine here, high
18860b57cec5SDimitry Andric       // bits don't affect the result) and then truncate the result back to the
18870b57cec5SDimitry Andric       // original type.
18880b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
18890b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT);
18900b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
18910b57cec5SDimitry Andric     } else {
18920b57cec5SDimitry Andric       bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector();
18930b57cec5SDimitry Andric       // Explicit extension is required here since high bits affect the result.
18940b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false));
18950b57cec5SDimitry Andric     }
18960b57cec5SDimitry Andric     Observer.changedInstr(MI);
18970b57cec5SDimitry Andric     return Legalized;
18980b57cec5SDimitry Andric 
18990b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
19000b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI:
19010b57cec5SDimitry Andric     Observer.changingInstr(MI);
19028bcb0991SDimitry Andric 
19038bcb0991SDimitry Andric     if (TypeIdx == 0)
19040b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
19058bcb0991SDimitry Andric     else
19068bcb0991SDimitry Andric       widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT);
19078bcb0991SDimitry Andric 
19080b57cec5SDimitry Andric     Observer.changedInstr(MI);
19090b57cec5SDimitry Andric     return Legalized;
19100b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
19110b57cec5SDimitry Andric     if (TypeIdx != 1)
19120b57cec5SDimitry Andric       return UnableToLegalize;
19130b57cec5SDimitry Andric     Observer.changingInstr(MI);
19140b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT);
19150b57cec5SDimitry Andric     Observer.changedInstr(MI);
19160b57cec5SDimitry Andric     return Legalized;
19170b57cec5SDimitry Andric 
19180b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP:
19190b57cec5SDimitry Andric     if (TypeIdx != 1)
19200b57cec5SDimitry Andric       return UnableToLegalize;
19210b57cec5SDimitry Andric     Observer.changingInstr(MI);
19220b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
19230b57cec5SDimitry Andric     Observer.changedInstr(MI);
19240b57cec5SDimitry Andric     return Legalized;
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric   case TargetOpcode::G_LOAD:
19270b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD:
19280b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD:
19290b57cec5SDimitry Andric     Observer.changingInstr(MI);
19300b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
19310b57cec5SDimitry Andric     Observer.changedInstr(MI);
19320b57cec5SDimitry Andric     return Legalized;
19330b57cec5SDimitry Andric 
19340b57cec5SDimitry Andric   case TargetOpcode::G_STORE: {
19350b57cec5SDimitry Andric     if (TypeIdx != 0)
19360b57cec5SDimitry Andric       return UnableToLegalize;
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric     LLT Ty = MRI.getType(MI.getOperand(0).getReg());
19390b57cec5SDimitry Andric     if (!isPowerOf2_32(Ty.getSizeInBits()))
19400b57cec5SDimitry Andric       return UnableToLegalize;
19410b57cec5SDimitry Andric 
19420b57cec5SDimitry Andric     Observer.changingInstr(MI);
19430b57cec5SDimitry Andric 
19440b57cec5SDimitry Andric     unsigned ExtType = Ty.getScalarSizeInBits() == 1 ?
19450b57cec5SDimitry Andric       TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT;
19460b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 0, ExtType);
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric     Observer.changedInstr(MI);
19490b57cec5SDimitry Andric     return Legalized;
19500b57cec5SDimitry Andric   }
19510b57cec5SDimitry Andric   case TargetOpcode::G_CONSTANT: {
19520b57cec5SDimitry Andric     MachineOperand &SrcMO = MI.getOperand(1);
19530b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
1954480093f4SDimitry Andric     unsigned ExtOpc = LI.getExtOpcodeForWideningConstant(
1955480093f4SDimitry Andric         MRI.getType(MI.getOperand(0).getReg()));
1956480093f4SDimitry Andric     assert((ExtOpc == TargetOpcode::G_ZEXT || ExtOpc == TargetOpcode::G_SEXT ||
1957480093f4SDimitry Andric             ExtOpc == TargetOpcode::G_ANYEXT) &&
1958480093f4SDimitry Andric            "Illegal Extend");
1959480093f4SDimitry Andric     const APInt &SrcVal = SrcMO.getCImm()->getValue();
1960480093f4SDimitry Andric     const APInt &Val = (ExtOpc == TargetOpcode::G_SEXT)
1961480093f4SDimitry Andric                            ? SrcVal.sext(WideTy.getSizeInBits())
1962480093f4SDimitry Andric                            : SrcVal.zext(WideTy.getSizeInBits());
19630b57cec5SDimitry Andric     Observer.changingInstr(MI);
19640b57cec5SDimitry Andric     SrcMO.setCImm(ConstantInt::get(Ctx, Val));
19650b57cec5SDimitry Andric 
19660b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
19670b57cec5SDimitry Andric     Observer.changedInstr(MI);
19680b57cec5SDimitry Andric     return Legalized;
19690b57cec5SDimitry Andric   }
19700b57cec5SDimitry Andric   case TargetOpcode::G_FCONSTANT: {
19710b57cec5SDimitry Andric     MachineOperand &SrcMO = MI.getOperand(1);
19720b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
19730b57cec5SDimitry Andric     APFloat Val = SrcMO.getFPImm()->getValueAPF();
19740b57cec5SDimitry Andric     bool LosesInfo;
19750b57cec5SDimitry Andric     switch (WideTy.getSizeInBits()) {
19760b57cec5SDimitry Andric     case 32:
19770b57cec5SDimitry Andric       Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
19780b57cec5SDimitry Andric                   &LosesInfo);
19790b57cec5SDimitry Andric       break;
19800b57cec5SDimitry Andric     case 64:
19810b57cec5SDimitry Andric       Val.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
19820b57cec5SDimitry Andric                   &LosesInfo);
19830b57cec5SDimitry Andric       break;
19840b57cec5SDimitry Andric     default:
19850b57cec5SDimitry Andric       return UnableToLegalize;
19860b57cec5SDimitry Andric     }
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric     assert(!LosesInfo && "extend should always be lossless");
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric     Observer.changingInstr(MI);
19910b57cec5SDimitry Andric     SrcMO.setFPImm(ConstantFP::get(Ctx, Val));
19920b57cec5SDimitry Andric 
19930b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC);
19940b57cec5SDimitry Andric     Observer.changedInstr(MI);
19950b57cec5SDimitry Andric     return Legalized;
19960b57cec5SDimitry Andric   }
19970b57cec5SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF: {
19980b57cec5SDimitry Andric     Observer.changingInstr(MI);
19990b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
20000b57cec5SDimitry Andric     Observer.changedInstr(MI);
20010b57cec5SDimitry Andric     return Legalized;
20020b57cec5SDimitry Andric   }
20030b57cec5SDimitry Andric   case TargetOpcode::G_BRCOND:
20040b57cec5SDimitry Andric     Observer.changingInstr(MI);
20050b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 0, MIRBuilder.getBoolExtOp(false, false));
20060b57cec5SDimitry Andric     Observer.changedInstr(MI);
20070b57cec5SDimitry Andric     return Legalized;
20080b57cec5SDimitry Andric 
20090b57cec5SDimitry Andric   case TargetOpcode::G_FCMP:
20100b57cec5SDimitry Andric     Observer.changingInstr(MI);
20110b57cec5SDimitry Andric     if (TypeIdx == 0)
20120b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
20130b57cec5SDimitry Andric     else {
20140b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT);
20150b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT);
20160b57cec5SDimitry Andric     }
20170b57cec5SDimitry Andric     Observer.changedInstr(MI);
20180b57cec5SDimitry Andric     return Legalized;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric   case TargetOpcode::G_ICMP:
20210b57cec5SDimitry Andric     Observer.changingInstr(MI);
20220b57cec5SDimitry Andric     if (TypeIdx == 0)
20230b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
20240b57cec5SDimitry Andric     else {
20250b57cec5SDimitry Andric       unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>(
20260b57cec5SDimitry Andric                                MI.getOperand(1).getPredicate()))
20270b57cec5SDimitry Andric                                ? TargetOpcode::G_SEXT
20280b57cec5SDimitry Andric                                : TargetOpcode::G_ZEXT;
20290b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, ExtOpcode);
20300b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, ExtOpcode);
20310b57cec5SDimitry Andric     }
20320b57cec5SDimitry Andric     Observer.changedInstr(MI);
20330b57cec5SDimitry Andric     return Legalized;
20340b57cec5SDimitry Andric 
2035480093f4SDimitry Andric   case TargetOpcode::G_PTR_ADD:
2036480093f4SDimitry Andric     assert(TypeIdx == 1 && "unable to legalize pointer of G_PTR_ADD");
20370b57cec5SDimitry Andric     Observer.changingInstr(MI);
20380b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
20390b57cec5SDimitry Andric     Observer.changedInstr(MI);
20400b57cec5SDimitry Andric     return Legalized;
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric   case TargetOpcode::G_PHI: {
20430b57cec5SDimitry Andric     assert(TypeIdx == 0 && "Expecting only Idx 0");
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric     Observer.changingInstr(MI);
20460b57cec5SDimitry Andric     for (unsigned I = 1; I < MI.getNumOperands(); I += 2) {
20470b57cec5SDimitry Andric       MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
20480b57cec5SDimitry Andric       MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
20490b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT);
20500b57cec5SDimitry Andric     }
20510b57cec5SDimitry Andric 
20520b57cec5SDimitry Andric     MachineBasicBlock &MBB = *MI.getParent();
20530b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI());
20540b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
20550b57cec5SDimitry Andric     Observer.changedInstr(MI);
20560b57cec5SDimitry Andric     return Legalized;
20570b57cec5SDimitry Andric   }
20580b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
20590b57cec5SDimitry Andric     if (TypeIdx == 0) {
20600b57cec5SDimitry Andric       Register VecReg = MI.getOperand(1).getReg();
20610b57cec5SDimitry Andric       LLT VecTy = MRI.getType(VecReg);
20620b57cec5SDimitry Andric       Observer.changingInstr(MI);
20630b57cec5SDimitry Andric 
20640b57cec5SDimitry Andric       widenScalarSrc(MI, LLT::vector(VecTy.getNumElements(),
20650b57cec5SDimitry Andric                                      WideTy.getSizeInBits()),
20660b57cec5SDimitry Andric                      1, TargetOpcode::G_SEXT);
20670b57cec5SDimitry Andric 
20680b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
20690b57cec5SDimitry Andric       Observer.changedInstr(MI);
20700b57cec5SDimitry Andric       return Legalized;
20710b57cec5SDimitry Andric     }
20720b57cec5SDimitry Andric 
20730b57cec5SDimitry Andric     if (TypeIdx != 2)
20740b57cec5SDimitry Andric       return UnableToLegalize;
20750b57cec5SDimitry Andric     Observer.changingInstr(MI);
2076480093f4SDimitry Andric     // TODO: Probably should be zext
20770b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
20780b57cec5SDimitry Andric     Observer.changedInstr(MI);
20790b57cec5SDimitry Andric     return Legalized;
20800b57cec5SDimitry Andric   }
2081480093f4SDimitry Andric   case TargetOpcode::G_INSERT_VECTOR_ELT: {
2082480093f4SDimitry Andric     if (TypeIdx == 1) {
2083480093f4SDimitry Andric       Observer.changingInstr(MI);
2084480093f4SDimitry Andric 
2085480093f4SDimitry Andric       Register VecReg = MI.getOperand(1).getReg();
2086480093f4SDimitry Andric       LLT VecTy = MRI.getType(VecReg);
2087480093f4SDimitry Andric       LLT WideVecTy = LLT::vector(VecTy.getNumElements(), WideTy);
2088480093f4SDimitry Andric 
2089480093f4SDimitry Andric       widenScalarSrc(MI, WideVecTy, 1, TargetOpcode::G_ANYEXT);
2090480093f4SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
2091480093f4SDimitry Andric       widenScalarDst(MI, WideVecTy, 0);
2092480093f4SDimitry Andric       Observer.changedInstr(MI);
2093480093f4SDimitry Andric       return Legalized;
2094480093f4SDimitry Andric     }
2095480093f4SDimitry Andric 
2096480093f4SDimitry Andric     if (TypeIdx == 2) {
2097480093f4SDimitry Andric       Observer.changingInstr(MI);
2098480093f4SDimitry Andric       // TODO: Probably should be zext
2099480093f4SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT);
2100480093f4SDimitry Andric       Observer.changedInstr(MI);
2101*5ffd83dbSDimitry Andric       return Legalized;
2102480093f4SDimitry Andric     }
2103480093f4SDimitry Andric 
2104*5ffd83dbSDimitry Andric     return UnableToLegalize;
2105480093f4SDimitry Andric   }
21060b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
21070b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
21080b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
21090b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
21108bcb0991SDimitry Andric   case TargetOpcode::G_FMAD:
21110b57cec5SDimitry Andric   case TargetOpcode::G_FNEG:
21120b57cec5SDimitry Andric   case TargetOpcode::G_FABS:
21130b57cec5SDimitry Andric   case TargetOpcode::G_FCANONICALIZE:
21140b57cec5SDimitry Andric   case TargetOpcode::G_FMINNUM:
21150b57cec5SDimitry Andric   case TargetOpcode::G_FMAXNUM:
21160b57cec5SDimitry Andric   case TargetOpcode::G_FMINNUM_IEEE:
21170b57cec5SDimitry Andric   case TargetOpcode::G_FMAXNUM_IEEE:
21180b57cec5SDimitry Andric   case TargetOpcode::G_FMINIMUM:
21190b57cec5SDimitry Andric   case TargetOpcode::G_FMAXIMUM:
21200b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
21210b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
21220b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
21230b57cec5SDimitry Andric   case TargetOpcode::G_FFLOOR:
21240b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
21250b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
21260b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
21270b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
21280b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
21290b57cec5SDimitry Andric   case TargetOpcode::G_FRINT:
21300b57cec5SDimitry Andric   case TargetOpcode::G_FNEARBYINT:
21310b57cec5SDimitry Andric   case TargetOpcode::G_FSQRT:
21320b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
21330b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
21340b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
21350b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC_TRUNC:
21360b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC_ROUND:
21370b57cec5SDimitry Andric     assert(TypeIdx == 0);
21380b57cec5SDimitry Andric     Observer.changingInstr(MI);
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric     for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I)
21410b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT);
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC);
21440b57cec5SDimitry Andric     Observer.changedInstr(MI);
21450b57cec5SDimitry Andric     return Legalized;
21460b57cec5SDimitry Andric   case TargetOpcode::G_INTTOPTR:
21470b57cec5SDimitry Andric     if (TypeIdx != 1)
21480b57cec5SDimitry Andric       return UnableToLegalize;
21490b57cec5SDimitry Andric 
21500b57cec5SDimitry Andric     Observer.changingInstr(MI);
21510b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
21520b57cec5SDimitry Andric     Observer.changedInstr(MI);
21530b57cec5SDimitry Andric     return Legalized;
21540b57cec5SDimitry Andric   case TargetOpcode::G_PTRTOINT:
21550b57cec5SDimitry Andric     if (TypeIdx != 0)
21560b57cec5SDimitry Andric       return UnableToLegalize;
21570b57cec5SDimitry Andric 
21580b57cec5SDimitry Andric     Observer.changingInstr(MI);
21590b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0);
21600b57cec5SDimitry Andric     Observer.changedInstr(MI);
21610b57cec5SDimitry Andric     return Legalized;
21620b57cec5SDimitry Andric   case TargetOpcode::G_BUILD_VECTOR: {
21630b57cec5SDimitry Andric     Observer.changingInstr(MI);
21640b57cec5SDimitry Andric 
21650b57cec5SDimitry Andric     const LLT WideEltTy = TypeIdx == 1 ? WideTy : WideTy.getElementType();
21660b57cec5SDimitry Andric     for (int I = 1, E = MI.getNumOperands(); I != E; ++I)
21670b57cec5SDimitry Andric       widenScalarSrc(MI, WideEltTy, I, TargetOpcode::G_ANYEXT);
21680b57cec5SDimitry Andric 
21690b57cec5SDimitry Andric     // Avoid changing the result vector type if the source element type was
21700b57cec5SDimitry Andric     // requested.
21710b57cec5SDimitry Andric     if (TypeIdx == 1) {
21720b57cec5SDimitry Andric       auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
21730b57cec5SDimitry Andric       MI.setDesc(TII.get(TargetOpcode::G_BUILD_VECTOR_TRUNC));
21740b57cec5SDimitry Andric     } else {
21750b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
21760b57cec5SDimitry Andric     }
21770b57cec5SDimitry Andric 
21780b57cec5SDimitry Andric     Observer.changedInstr(MI);
21790b57cec5SDimitry Andric     return Legalized;
21800b57cec5SDimitry Andric   }
21818bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG:
21828bcb0991SDimitry Andric     if (TypeIdx != 0)
21838bcb0991SDimitry Andric       return UnableToLegalize;
21848bcb0991SDimitry Andric 
21858bcb0991SDimitry Andric     Observer.changingInstr(MI);
21868bcb0991SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
21878bcb0991SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_TRUNC);
21888bcb0991SDimitry Andric     Observer.changedInstr(MI);
21898bcb0991SDimitry Andric     return Legalized;
2190*5ffd83dbSDimitry Andric   case TargetOpcode::G_PTRMASK: {
2191*5ffd83dbSDimitry Andric     if (TypeIdx != 1)
2192*5ffd83dbSDimitry Andric       return UnableToLegalize;
2193*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2194*5ffd83dbSDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
2195*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
2196*5ffd83dbSDimitry Andric     return Legalized;
2197*5ffd83dbSDimitry Andric   }
2198*5ffd83dbSDimitry Andric   }
2199*5ffd83dbSDimitry Andric }
2200*5ffd83dbSDimitry Andric 
2201*5ffd83dbSDimitry Andric static void getUnmergePieces(SmallVectorImpl<Register> &Pieces,
2202*5ffd83dbSDimitry Andric                              MachineIRBuilder &B, Register Src, LLT Ty) {
2203*5ffd83dbSDimitry Andric   auto Unmerge = B.buildUnmerge(Ty, Src);
2204*5ffd83dbSDimitry Andric   for (int I = 0, E = Unmerge->getNumOperands() - 1; I != E; ++I)
2205*5ffd83dbSDimitry Andric     Pieces.push_back(Unmerge.getReg(I));
2206*5ffd83dbSDimitry Andric }
2207*5ffd83dbSDimitry Andric 
2208*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
2209*5ffd83dbSDimitry Andric LegalizerHelper::lowerBitcast(MachineInstr &MI) {
2210*5ffd83dbSDimitry Andric   Register Dst = MI.getOperand(0).getReg();
2211*5ffd83dbSDimitry Andric   Register Src = MI.getOperand(1).getReg();
2212*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(Dst);
2213*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(Src);
2214*5ffd83dbSDimitry Andric 
2215*5ffd83dbSDimitry Andric   if (SrcTy.isVector()) {
2216*5ffd83dbSDimitry Andric     LLT SrcEltTy = SrcTy.getElementType();
2217*5ffd83dbSDimitry Andric     SmallVector<Register, 8> SrcRegs;
2218*5ffd83dbSDimitry Andric 
2219*5ffd83dbSDimitry Andric     if (DstTy.isVector()) {
2220*5ffd83dbSDimitry Andric       int NumDstElt = DstTy.getNumElements();
2221*5ffd83dbSDimitry Andric       int NumSrcElt = SrcTy.getNumElements();
2222*5ffd83dbSDimitry Andric 
2223*5ffd83dbSDimitry Andric       LLT DstEltTy = DstTy.getElementType();
2224*5ffd83dbSDimitry Andric       LLT DstCastTy = DstEltTy; // Intermediate bitcast result type
2225*5ffd83dbSDimitry Andric       LLT SrcPartTy = SrcEltTy; // Original unmerge result type.
2226*5ffd83dbSDimitry Andric 
2227*5ffd83dbSDimitry Andric       // If there's an element size mismatch, insert intermediate casts to match
2228*5ffd83dbSDimitry Andric       // the result element type.
2229*5ffd83dbSDimitry Andric       if (NumSrcElt < NumDstElt) { // Source element type is larger.
2230*5ffd83dbSDimitry Andric         // %1:_(<4 x s8>) = G_BITCAST %0:_(<2 x s16>)
2231*5ffd83dbSDimitry Andric         //
2232*5ffd83dbSDimitry Andric         // =>
2233*5ffd83dbSDimitry Andric         //
2234*5ffd83dbSDimitry Andric         // %2:_(s16), %3:_(s16) = G_UNMERGE_VALUES %0
2235*5ffd83dbSDimitry Andric         // %3:_(<2 x s8>) = G_BITCAST %2
2236*5ffd83dbSDimitry Andric         // %4:_(<2 x s8>) = G_BITCAST %3
2237*5ffd83dbSDimitry Andric         // %1:_(<4 x s16>) = G_CONCAT_VECTORS %3, %4
2238*5ffd83dbSDimitry Andric         DstCastTy = LLT::vector(NumDstElt / NumSrcElt, DstEltTy);
2239*5ffd83dbSDimitry Andric         SrcPartTy = SrcEltTy;
2240*5ffd83dbSDimitry Andric       } else if (NumSrcElt > NumDstElt) { // Source element type is smaller.
2241*5ffd83dbSDimitry Andric         //
2242*5ffd83dbSDimitry Andric         // %1:_(<2 x s16>) = G_BITCAST %0:_(<4 x s8>)
2243*5ffd83dbSDimitry Andric         //
2244*5ffd83dbSDimitry Andric         // =>
2245*5ffd83dbSDimitry Andric         //
2246*5ffd83dbSDimitry Andric         // %2:_(<2 x s8>), %3:_(<2 x s8>) = G_UNMERGE_VALUES %0
2247*5ffd83dbSDimitry Andric         // %3:_(s16) = G_BITCAST %2
2248*5ffd83dbSDimitry Andric         // %4:_(s16) = G_BITCAST %3
2249*5ffd83dbSDimitry Andric         // %1:_(<2 x s16>) = G_BUILD_VECTOR %3, %4
2250*5ffd83dbSDimitry Andric         SrcPartTy = LLT::vector(NumSrcElt / NumDstElt, SrcEltTy);
2251*5ffd83dbSDimitry Andric         DstCastTy = DstEltTy;
2252*5ffd83dbSDimitry Andric       }
2253*5ffd83dbSDimitry Andric 
2254*5ffd83dbSDimitry Andric       getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcPartTy);
2255*5ffd83dbSDimitry Andric       for (Register &SrcReg : SrcRegs)
2256*5ffd83dbSDimitry Andric         SrcReg = MIRBuilder.buildBitcast(DstCastTy, SrcReg).getReg(0);
2257*5ffd83dbSDimitry Andric     } else
2258*5ffd83dbSDimitry Andric       getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcEltTy);
2259*5ffd83dbSDimitry Andric 
2260*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(Dst, SrcRegs);
2261*5ffd83dbSDimitry Andric     MI.eraseFromParent();
2262*5ffd83dbSDimitry Andric     return Legalized;
2263*5ffd83dbSDimitry Andric   }
2264*5ffd83dbSDimitry Andric 
2265*5ffd83dbSDimitry Andric   if (DstTy.isVector()) {
2266*5ffd83dbSDimitry Andric     SmallVector<Register, 8> SrcRegs;
2267*5ffd83dbSDimitry Andric     getUnmergePieces(SrcRegs, MIRBuilder, Src, DstTy.getElementType());
2268*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(Dst, SrcRegs);
2269*5ffd83dbSDimitry Andric     MI.eraseFromParent();
2270*5ffd83dbSDimitry Andric     return Legalized;
2271*5ffd83dbSDimitry Andric   }
2272*5ffd83dbSDimitry Andric 
2273*5ffd83dbSDimitry Andric   return UnableToLegalize;
2274*5ffd83dbSDimitry Andric }
2275*5ffd83dbSDimitry Andric 
2276*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
2277*5ffd83dbSDimitry Andric LegalizerHelper::bitcast(MachineInstr &MI, unsigned TypeIdx, LLT CastTy) {
2278*5ffd83dbSDimitry Andric   switch (MI.getOpcode()) {
2279*5ffd83dbSDimitry Andric   case TargetOpcode::G_LOAD: {
2280*5ffd83dbSDimitry Andric     if (TypeIdx != 0)
2281*5ffd83dbSDimitry Andric       return UnableToLegalize;
2282*5ffd83dbSDimitry Andric 
2283*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2284*5ffd83dbSDimitry Andric     bitcastDst(MI, CastTy, 0);
2285*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
2286*5ffd83dbSDimitry Andric     return Legalized;
2287*5ffd83dbSDimitry Andric   }
2288*5ffd83dbSDimitry Andric   case TargetOpcode::G_STORE: {
2289*5ffd83dbSDimitry Andric     if (TypeIdx != 0)
2290*5ffd83dbSDimitry Andric       return UnableToLegalize;
2291*5ffd83dbSDimitry Andric 
2292*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2293*5ffd83dbSDimitry Andric     bitcastSrc(MI, CastTy, 0);
2294*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
2295*5ffd83dbSDimitry Andric     return Legalized;
2296*5ffd83dbSDimitry Andric   }
2297*5ffd83dbSDimitry Andric   case TargetOpcode::G_SELECT: {
2298*5ffd83dbSDimitry Andric     if (TypeIdx != 0)
2299*5ffd83dbSDimitry Andric       return UnableToLegalize;
2300*5ffd83dbSDimitry Andric 
2301*5ffd83dbSDimitry Andric     if (MRI.getType(MI.getOperand(1).getReg()).isVector()) {
2302*5ffd83dbSDimitry Andric       LLVM_DEBUG(
2303*5ffd83dbSDimitry Andric           dbgs() << "bitcast action not implemented for vector select\n");
2304*5ffd83dbSDimitry Andric       return UnableToLegalize;
2305*5ffd83dbSDimitry Andric     }
2306*5ffd83dbSDimitry Andric 
2307*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2308*5ffd83dbSDimitry Andric     bitcastSrc(MI, CastTy, 2);
2309*5ffd83dbSDimitry Andric     bitcastSrc(MI, CastTy, 3);
2310*5ffd83dbSDimitry Andric     bitcastDst(MI, CastTy, 0);
2311*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
2312*5ffd83dbSDimitry Andric     return Legalized;
2313*5ffd83dbSDimitry Andric   }
2314*5ffd83dbSDimitry Andric   case TargetOpcode::G_AND:
2315*5ffd83dbSDimitry Andric   case TargetOpcode::G_OR:
2316*5ffd83dbSDimitry Andric   case TargetOpcode::G_XOR: {
2317*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2318*5ffd83dbSDimitry Andric     bitcastSrc(MI, CastTy, 1);
2319*5ffd83dbSDimitry Andric     bitcastSrc(MI, CastTy, 2);
2320*5ffd83dbSDimitry Andric     bitcastDst(MI, CastTy, 0);
2321*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
2322*5ffd83dbSDimitry Andric     return Legalized;
2323*5ffd83dbSDimitry Andric   }
2324*5ffd83dbSDimitry Andric   default:
2325*5ffd83dbSDimitry Andric     return UnableToLegalize;
23260b57cec5SDimitry Andric   }
23270b57cec5SDimitry Andric }
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
23300b57cec5SDimitry Andric LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
23310b57cec5SDimitry Andric   using namespace TargetOpcode;
23320b57cec5SDimitry Andric 
23330b57cec5SDimitry Andric   switch(MI.getOpcode()) {
23340b57cec5SDimitry Andric   default:
23350b57cec5SDimitry Andric     return UnableToLegalize;
2336*5ffd83dbSDimitry Andric   case TargetOpcode::G_BITCAST:
2337*5ffd83dbSDimitry Andric     return lowerBitcast(MI);
23380b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
23390b57cec5SDimitry Andric   case TargetOpcode::G_UREM: {
2340*5ffd83dbSDimitry Andric     auto Quot =
2341*5ffd83dbSDimitry Andric         MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV, {Ty},
2342*5ffd83dbSDimitry Andric                               {MI.getOperand(1), MI.getOperand(2)});
23430b57cec5SDimitry Andric 
2344*5ffd83dbSDimitry Andric     auto Prod = MIRBuilder.buildMul(Ty, Quot, MI.getOperand(2));
2345*5ffd83dbSDimitry Andric     MIRBuilder.buildSub(MI.getOperand(0), MI.getOperand(1), Prod);
23460b57cec5SDimitry Andric     MI.eraseFromParent();
23470b57cec5SDimitry Andric     return Legalized;
23480b57cec5SDimitry Andric   }
23498bcb0991SDimitry Andric   case TargetOpcode::G_SADDO:
23508bcb0991SDimitry Andric   case TargetOpcode::G_SSUBO:
23518bcb0991SDimitry Andric     return lowerSADDO_SSUBO(MI);
23520b57cec5SDimitry Andric   case TargetOpcode::G_SMULO:
23530b57cec5SDimitry Andric   case TargetOpcode::G_UMULO: {
23540b57cec5SDimitry Andric     // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the
23550b57cec5SDimitry Andric     // result.
23560b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
23570b57cec5SDimitry Andric     Register Overflow = MI.getOperand(1).getReg();
23580b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
23590b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
23600b57cec5SDimitry Andric 
23610b57cec5SDimitry Andric     unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO
23620b57cec5SDimitry Andric                           ? TargetOpcode::G_SMULH
23630b57cec5SDimitry Andric                           : TargetOpcode::G_UMULH;
23640b57cec5SDimitry Andric 
2365*5ffd83dbSDimitry Andric     Observer.changingInstr(MI);
2366*5ffd83dbSDimitry Andric     const auto &TII = MIRBuilder.getTII();
2367*5ffd83dbSDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_MUL));
2368*5ffd83dbSDimitry Andric     MI.RemoveOperand(1);
2369*5ffd83dbSDimitry Andric     Observer.changedInstr(MI);
23700b57cec5SDimitry Andric 
2371*5ffd83dbSDimitry Andric     MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
2372*5ffd83dbSDimitry Andric 
2373*5ffd83dbSDimitry Andric     auto HiPart = MIRBuilder.buildInstr(Opcode, {Ty}, {LHS, RHS});
2374*5ffd83dbSDimitry Andric     auto Zero = MIRBuilder.buildConstant(Ty, 0);
23750b57cec5SDimitry Andric 
23760b57cec5SDimitry Andric     // For *signed* multiply, overflow is detected by checking:
23770b57cec5SDimitry Andric     // (hi != (lo >> bitwidth-1))
23780b57cec5SDimitry Andric     if (Opcode == TargetOpcode::G_SMULH) {
2379*5ffd83dbSDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(Ty, Ty.getSizeInBits() - 1);
2380*5ffd83dbSDimitry Andric       auto Shifted = MIRBuilder.buildAShr(Ty, Res, ShiftAmt);
23810b57cec5SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted);
23820b57cec5SDimitry Andric     } else {
23830b57cec5SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero);
23840b57cec5SDimitry Andric     }
23850b57cec5SDimitry Andric     return Legalized;
23860b57cec5SDimitry Andric   }
23870b57cec5SDimitry Andric   case TargetOpcode::G_FNEG: {
23880b57cec5SDimitry Andric     // TODO: Handle vector types once we are able to
23890b57cec5SDimitry Andric     // represent them.
23900b57cec5SDimitry Andric     if (Ty.isVector())
23910b57cec5SDimitry Andric       return UnableToLegalize;
23920b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
23930b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
2394*5ffd83dbSDimitry Andric     Type *ZeroTy = getFloatTypeForLLT(Ctx, Ty);
2395*5ffd83dbSDimitry Andric     if (!ZeroTy)
2396*5ffd83dbSDimitry Andric       return UnableToLegalize;
23970b57cec5SDimitry Andric     ConstantFP &ZeroForNegation =
23980b57cec5SDimitry Andric         *cast<ConstantFP>(ConstantFP::getZeroValueForNegation(ZeroTy));
23990b57cec5SDimitry Andric     auto Zero = MIRBuilder.buildFConstant(Ty, ZeroForNegation);
24000b57cec5SDimitry Andric     Register SubByReg = MI.getOperand(1).getReg();
2401*5ffd83dbSDimitry Andric     Register ZeroReg = Zero.getReg(0);
2402*5ffd83dbSDimitry Andric     MIRBuilder.buildFSub(Res, ZeroReg, SubByReg, MI.getFlags());
24030b57cec5SDimitry Andric     MI.eraseFromParent();
24040b57cec5SDimitry Andric     return Legalized;
24050b57cec5SDimitry Andric   }
24060b57cec5SDimitry Andric   case TargetOpcode::G_FSUB: {
24070b57cec5SDimitry Andric     // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)).
24080b57cec5SDimitry Andric     // First, check if G_FNEG is marked as Lower. If so, we may
24090b57cec5SDimitry Andric     // end up with an infinite loop as G_FSUB is used to legalize G_FNEG.
24100b57cec5SDimitry Andric     if (LI.getAction({G_FNEG, {Ty}}).Action == Lower)
24110b57cec5SDimitry Andric       return UnableToLegalize;
24120b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
24130b57cec5SDimitry Andric     Register LHS = MI.getOperand(1).getReg();
24140b57cec5SDimitry Andric     Register RHS = MI.getOperand(2).getReg();
24150b57cec5SDimitry Andric     Register Neg = MRI.createGenericVirtualRegister(Ty);
2416*5ffd83dbSDimitry Andric     MIRBuilder.buildFNeg(Neg, RHS);
2417*5ffd83dbSDimitry Andric     MIRBuilder.buildFAdd(Res, LHS, Neg, MI.getFlags());
24180b57cec5SDimitry Andric     MI.eraseFromParent();
24190b57cec5SDimitry Andric     return Legalized;
24200b57cec5SDimitry Andric   }
24218bcb0991SDimitry Andric   case TargetOpcode::G_FMAD:
24228bcb0991SDimitry Andric     return lowerFMad(MI);
2423*5ffd83dbSDimitry Andric   case TargetOpcode::G_FFLOOR:
2424*5ffd83dbSDimitry Andric     return lowerFFloor(MI);
2425480093f4SDimitry Andric   case TargetOpcode::G_INTRINSIC_ROUND:
2426480093f4SDimitry Andric     return lowerIntrinsicRound(MI);
24270b57cec5SDimitry Andric   case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: {
24280b57cec5SDimitry Andric     Register OldValRes = MI.getOperand(0).getReg();
24290b57cec5SDimitry Andric     Register SuccessRes = MI.getOperand(1).getReg();
24300b57cec5SDimitry Andric     Register Addr = MI.getOperand(2).getReg();
24310b57cec5SDimitry Andric     Register CmpVal = MI.getOperand(3).getReg();
24320b57cec5SDimitry Andric     Register NewVal = MI.getOperand(4).getReg();
24330b57cec5SDimitry Andric     MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal,
24340b57cec5SDimitry Andric                                   **MI.memoperands_begin());
24350b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal);
24360b57cec5SDimitry Andric     MI.eraseFromParent();
24370b57cec5SDimitry Andric     return Legalized;
24380b57cec5SDimitry Andric   }
24390b57cec5SDimitry Andric   case TargetOpcode::G_LOAD:
24400b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD:
24410b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD: {
24420b57cec5SDimitry Andric     // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT
24430b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
24440b57cec5SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
24450b57cec5SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
24460b57cec5SDimitry Andric     auto &MMO = **MI.memoperands_begin();
24470b57cec5SDimitry Andric 
24488bcb0991SDimitry Andric     if (DstTy.getSizeInBits() == MMO.getSizeInBits()) {
24498bcb0991SDimitry Andric       if (MI.getOpcode() == TargetOpcode::G_LOAD) {
24508bcb0991SDimitry Andric         // This load needs splitting into power of 2 sized loads.
24518bcb0991SDimitry Andric         if (DstTy.isVector())
24520b57cec5SDimitry Andric           return UnableToLegalize;
24538bcb0991SDimitry Andric         if (isPowerOf2_32(DstTy.getSizeInBits()))
24548bcb0991SDimitry Andric           return UnableToLegalize; // Don't know what we're being asked to do.
24558bcb0991SDimitry Andric 
24568bcb0991SDimitry Andric         // Our strategy here is to generate anyextending loads for the smaller
24578bcb0991SDimitry Andric         // types up to next power-2 result type, and then combine the two larger
24588bcb0991SDimitry Andric         // result values together, before truncating back down to the non-pow-2
24598bcb0991SDimitry Andric         // type.
24608bcb0991SDimitry Andric         // E.g. v1 = i24 load =>
2461*5ffd83dbSDimitry Andric         // v2 = i32 zextload (2 byte)
24628bcb0991SDimitry Andric         // v3 = i32 load (1 byte)
24638bcb0991SDimitry Andric         // v4 = i32 shl v3, 16
24648bcb0991SDimitry Andric         // v5 = i32 or v4, v2
24658bcb0991SDimitry Andric         // v1 = i24 trunc v5
24668bcb0991SDimitry Andric         // By doing this we generate the correct truncate which should get
24678bcb0991SDimitry Andric         // combined away as an artifact with a matching extend.
24688bcb0991SDimitry Andric         uint64_t LargeSplitSize = PowerOf2Floor(DstTy.getSizeInBits());
24698bcb0991SDimitry Andric         uint64_t SmallSplitSize = DstTy.getSizeInBits() - LargeSplitSize;
24708bcb0991SDimitry Andric 
24718bcb0991SDimitry Andric         MachineFunction &MF = MIRBuilder.getMF();
24728bcb0991SDimitry Andric         MachineMemOperand *LargeMMO =
24738bcb0991SDimitry Andric             MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8);
24748bcb0991SDimitry Andric         MachineMemOperand *SmallMMO = MF.getMachineMemOperand(
24758bcb0991SDimitry Andric             &MMO, LargeSplitSize / 8, SmallSplitSize / 8);
24768bcb0991SDimitry Andric 
24778bcb0991SDimitry Andric         LLT PtrTy = MRI.getType(PtrReg);
24788bcb0991SDimitry Andric         unsigned AnyExtSize = NextPowerOf2(DstTy.getSizeInBits());
24798bcb0991SDimitry Andric         LLT AnyExtTy = LLT::scalar(AnyExtSize);
24808bcb0991SDimitry Andric         Register LargeLdReg = MRI.createGenericVirtualRegister(AnyExtTy);
24818bcb0991SDimitry Andric         Register SmallLdReg = MRI.createGenericVirtualRegister(AnyExtTy);
2482*5ffd83dbSDimitry Andric         auto LargeLoad = MIRBuilder.buildLoadInstr(
2483*5ffd83dbSDimitry Andric             TargetOpcode::G_ZEXTLOAD, LargeLdReg, PtrReg, *LargeMMO);
24848bcb0991SDimitry Andric 
2485*5ffd83dbSDimitry Andric         auto OffsetCst = MIRBuilder.buildConstant(
2486*5ffd83dbSDimitry Andric             LLT::scalar(PtrTy.getSizeInBits()), LargeSplitSize / 8);
2487480093f4SDimitry Andric         Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy);
2488480093f4SDimitry Andric         auto SmallPtr =
2489480093f4SDimitry Andric             MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst.getReg(0));
24908bcb0991SDimitry Andric         auto SmallLoad = MIRBuilder.buildLoad(SmallLdReg, SmallPtr.getReg(0),
24918bcb0991SDimitry Andric                                               *SmallMMO);
24928bcb0991SDimitry Andric 
24938bcb0991SDimitry Andric         auto ShiftAmt = MIRBuilder.buildConstant(AnyExtTy, LargeSplitSize);
24948bcb0991SDimitry Andric         auto Shift = MIRBuilder.buildShl(AnyExtTy, SmallLoad, ShiftAmt);
24958bcb0991SDimitry Andric         auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad);
24968bcb0991SDimitry Andric         MIRBuilder.buildTrunc(DstReg, {Or.getReg(0)});
24978bcb0991SDimitry Andric         MI.eraseFromParent();
24988bcb0991SDimitry Andric         return Legalized;
24998bcb0991SDimitry Andric       }
25000b57cec5SDimitry Andric       MIRBuilder.buildLoad(DstReg, PtrReg, MMO);
25010b57cec5SDimitry Andric       MI.eraseFromParent();
25020b57cec5SDimitry Andric       return Legalized;
25030b57cec5SDimitry Andric     }
25040b57cec5SDimitry Andric 
25050b57cec5SDimitry Andric     if (DstTy.isScalar()) {
25060b57cec5SDimitry Andric       Register TmpReg =
25070b57cec5SDimitry Andric           MRI.createGenericVirtualRegister(LLT::scalar(MMO.getSizeInBits()));
25080b57cec5SDimitry Andric       MIRBuilder.buildLoad(TmpReg, PtrReg, MMO);
25090b57cec5SDimitry Andric       switch (MI.getOpcode()) {
25100b57cec5SDimitry Andric       default:
25110b57cec5SDimitry Andric         llvm_unreachable("Unexpected opcode");
25120b57cec5SDimitry Andric       case TargetOpcode::G_LOAD:
2513480093f4SDimitry Andric         MIRBuilder.buildExtOrTrunc(TargetOpcode::G_ANYEXT, DstReg, TmpReg);
25140b57cec5SDimitry Andric         break;
25150b57cec5SDimitry Andric       case TargetOpcode::G_SEXTLOAD:
25160b57cec5SDimitry Andric         MIRBuilder.buildSExt(DstReg, TmpReg);
25170b57cec5SDimitry Andric         break;
25180b57cec5SDimitry Andric       case TargetOpcode::G_ZEXTLOAD:
25190b57cec5SDimitry Andric         MIRBuilder.buildZExt(DstReg, TmpReg);
25200b57cec5SDimitry Andric         break;
25210b57cec5SDimitry Andric       }
25220b57cec5SDimitry Andric       MI.eraseFromParent();
25230b57cec5SDimitry Andric       return Legalized;
25240b57cec5SDimitry Andric     }
25250b57cec5SDimitry Andric 
25260b57cec5SDimitry Andric     return UnableToLegalize;
25270b57cec5SDimitry Andric   }
25288bcb0991SDimitry Andric   case TargetOpcode::G_STORE: {
25298bcb0991SDimitry Andric     // Lower a non-power of 2 store into multiple pow-2 stores.
25308bcb0991SDimitry Andric     // E.g. split an i24 store into an i16 store + i8 store.
25318bcb0991SDimitry Andric     // We do this by first extending the stored value to the next largest power
25328bcb0991SDimitry Andric     // of 2 type, and then using truncating stores to store the components.
25338bcb0991SDimitry Andric     // By doing this, likewise with G_LOAD, generate an extend that can be
25348bcb0991SDimitry Andric     // artifact-combined away instead of leaving behind extracts.
25358bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(0).getReg();
25368bcb0991SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
25378bcb0991SDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
25388bcb0991SDimitry Andric     MachineMemOperand &MMO = **MI.memoperands_begin();
25398bcb0991SDimitry Andric     if (SrcTy.getSizeInBits() != MMO.getSizeInBits())
25408bcb0991SDimitry Andric       return UnableToLegalize;
25418bcb0991SDimitry Andric     if (SrcTy.isVector())
25428bcb0991SDimitry Andric       return UnableToLegalize;
25438bcb0991SDimitry Andric     if (isPowerOf2_32(SrcTy.getSizeInBits()))
25448bcb0991SDimitry Andric       return UnableToLegalize; // Don't know what we're being asked to do.
25458bcb0991SDimitry Andric 
25468bcb0991SDimitry Andric     // Extend to the next pow-2.
25478bcb0991SDimitry Andric     const LLT ExtendTy = LLT::scalar(NextPowerOf2(SrcTy.getSizeInBits()));
25488bcb0991SDimitry Andric     auto ExtVal = MIRBuilder.buildAnyExt(ExtendTy, SrcReg);
25498bcb0991SDimitry Andric 
25508bcb0991SDimitry Andric     // Obtain the smaller value by shifting away the larger value.
25518bcb0991SDimitry Andric     uint64_t LargeSplitSize = PowerOf2Floor(SrcTy.getSizeInBits());
25528bcb0991SDimitry Andric     uint64_t SmallSplitSize = SrcTy.getSizeInBits() - LargeSplitSize;
25538bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(ExtendTy, LargeSplitSize);
25548bcb0991SDimitry Andric     auto SmallVal = MIRBuilder.buildLShr(ExtendTy, ExtVal, ShiftAmt);
25558bcb0991SDimitry Andric 
2556480093f4SDimitry Andric     // Generate the PtrAdd and truncating stores.
25578bcb0991SDimitry Andric     LLT PtrTy = MRI.getType(PtrReg);
2558*5ffd83dbSDimitry Andric     auto OffsetCst = MIRBuilder.buildConstant(
2559*5ffd83dbSDimitry Andric             LLT::scalar(PtrTy.getSizeInBits()), LargeSplitSize / 8);
2560480093f4SDimitry Andric     Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy);
2561480093f4SDimitry Andric     auto SmallPtr =
2562480093f4SDimitry Andric         MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst.getReg(0));
25638bcb0991SDimitry Andric 
25648bcb0991SDimitry Andric     MachineFunction &MF = MIRBuilder.getMF();
25658bcb0991SDimitry Andric     MachineMemOperand *LargeMMO =
25668bcb0991SDimitry Andric         MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8);
25678bcb0991SDimitry Andric     MachineMemOperand *SmallMMO =
25688bcb0991SDimitry Andric         MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8);
25698bcb0991SDimitry Andric     MIRBuilder.buildStore(ExtVal.getReg(0), PtrReg, *LargeMMO);
25708bcb0991SDimitry Andric     MIRBuilder.buildStore(SmallVal.getReg(0), SmallPtr.getReg(0), *SmallMMO);
25718bcb0991SDimitry Andric     MI.eraseFromParent();
25728bcb0991SDimitry Andric     return Legalized;
25738bcb0991SDimitry Andric   }
25740b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
25750b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
25760b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
25770b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
25780b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP:
25790b57cec5SDimitry Andric     return lowerBitCount(MI, TypeIdx, Ty);
25800b57cec5SDimitry Andric   case G_UADDO: {
25810b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
25820b57cec5SDimitry Andric     Register CarryOut = MI.getOperand(1).getReg();
25830b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
25840b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
25850b57cec5SDimitry Andric 
25860b57cec5SDimitry Andric     MIRBuilder.buildAdd(Res, LHS, RHS);
25870b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS);
25880b57cec5SDimitry Andric 
25890b57cec5SDimitry Andric     MI.eraseFromParent();
25900b57cec5SDimitry Andric     return Legalized;
25910b57cec5SDimitry Andric   }
25920b57cec5SDimitry Andric   case G_UADDE: {
25930b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
25940b57cec5SDimitry Andric     Register CarryOut = MI.getOperand(1).getReg();
25950b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
25960b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
25970b57cec5SDimitry Andric     Register CarryIn = MI.getOperand(4).getReg();
2598*5ffd83dbSDimitry Andric     LLT Ty = MRI.getType(Res);
25990b57cec5SDimitry Andric 
2600*5ffd83dbSDimitry Andric     auto TmpRes = MIRBuilder.buildAdd(Ty, LHS, RHS);
2601*5ffd83dbSDimitry Andric     auto ZExtCarryIn = MIRBuilder.buildZExt(Ty, CarryIn);
26020b57cec5SDimitry Andric     MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn);
26030b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS);
26040b57cec5SDimitry Andric 
26050b57cec5SDimitry Andric     MI.eraseFromParent();
26060b57cec5SDimitry Andric     return Legalized;
26070b57cec5SDimitry Andric   }
26080b57cec5SDimitry Andric   case G_USUBO: {
26090b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
26100b57cec5SDimitry Andric     Register BorrowOut = MI.getOperand(1).getReg();
26110b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
26120b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
26130b57cec5SDimitry Andric 
26140b57cec5SDimitry Andric     MIRBuilder.buildSub(Res, LHS, RHS);
26150b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS);
26160b57cec5SDimitry Andric 
26170b57cec5SDimitry Andric     MI.eraseFromParent();
26180b57cec5SDimitry Andric     return Legalized;
26190b57cec5SDimitry Andric   }
26200b57cec5SDimitry Andric   case G_USUBE: {
26210b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
26220b57cec5SDimitry Andric     Register BorrowOut = MI.getOperand(1).getReg();
26230b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
26240b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
26250b57cec5SDimitry Andric     Register BorrowIn = MI.getOperand(4).getReg();
2626*5ffd83dbSDimitry Andric     const LLT CondTy = MRI.getType(BorrowOut);
2627*5ffd83dbSDimitry Andric     const LLT Ty = MRI.getType(Res);
26280b57cec5SDimitry Andric 
2629*5ffd83dbSDimitry Andric     auto TmpRes = MIRBuilder.buildSub(Ty, LHS, RHS);
2630*5ffd83dbSDimitry Andric     auto ZExtBorrowIn = MIRBuilder.buildZExt(Ty, BorrowIn);
26310b57cec5SDimitry Andric     MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn);
2632*5ffd83dbSDimitry Andric 
2633*5ffd83dbSDimitry Andric     auto LHS_EQ_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, CondTy, LHS, RHS);
2634*5ffd83dbSDimitry Andric     auto LHS_ULT_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CondTy, LHS, RHS);
26350b57cec5SDimitry Andric     MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS);
26360b57cec5SDimitry Andric 
26370b57cec5SDimitry Andric     MI.eraseFromParent();
26380b57cec5SDimitry Andric     return Legalized;
26390b57cec5SDimitry Andric   }
26400b57cec5SDimitry Andric   case G_UITOFP:
26410b57cec5SDimitry Andric     return lowerUITOFP(MI, TypeIdx, Ty);
26420b57cec5SDimitry Andric   case G_SITOFP:
26430b57cec5SDimitry Andric     return lowerSITOFP(MI, TypeIdx, Ty);
26448bcb0991SDimitry Andric   case G_FPTOUI:
26458bcb0991SDimitry Andric     return lowerFPTOUI(MI, TypeIdx, Ty);
2646*5ffd83dbSDimitry Andric   case G_FPTOSI:
2647*5ffd83dbSDimitry Andric     return lowerFPTOSI(MI);
2648*5ffd83dbSDimitry Andric   case G_FPTRUNC:
2649*5ffd83dbSDimitry Andric     return lowerFPTRUNC(MI, TypeIdx, Ty);
26500b57cec5SDimitry Andric   case G_SMIN:
26510b57cec5SDimitry Andric   case G_SMAX:
26520b57cec5SDimitry Andric   case G_UMIN:
26530b57cec5SDimitry Andric   case G_UMAX:
26540b57cec5SDimitry Andric     return lowerMinMax(MI, TypeIdx, Ty);
26550b57cec5SDimitry Andric   case G_FCOPYSIGN:
26560b57cec5SDimitry Andric     return lowerFCopySign(MI, TypeIdx, Ty);
26570b57cec5SDimitry Andric   case G_FMINNUM:
26580b57cec5SDimitry Andric   case G_FMAXNUM:
26590b57cec5SDimitry Andric     return lowerFMinNumMaxNum(MI);
2660*5ffd83dbSDimitry Andric   case G_MERGE_VALUES:
2661*5ffd83dbSDimitry Andric     return lowerMergeValues(MI);
26628bcb0991SDimitry Andric   case G_UNMERGE_VALUES:
26638bcb0991SDimitry Andric     return lowerUnmergeValues(MI);
26648bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG: {
26658bcb0991SDimitry Andric     assert(MI.getOperand(2).isImm() && "Expected immediate");
26668bcb0991SDimitry Andric     int64_t SizeInBits = MI.getOperand(2).getImm();
26678bcb0991SDimitry Andric 
26688bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
26698bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
26708bcb0991SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
26718bcb0991SDimitry Andric     Register TmpRes = MRI.createGenericVirtualRegister(DstTy);
26728bcb0991SDimitry Andric 
26738bcb0991SDimitry Andric     auto MIBSz = MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - SizeInBits);
2674*5ffd83dbSDimitry Andric     MIRBuilder.buildShl(TmpRes, SrcReg, MIBSz->getOperand(0));
2675*5ffd83dbSDimitry Andric     MIRBuilder.buildAShr(DstReg, TmpRes, MIBSz->getOperand(0));
26768bcb0991SDimitry Andric     MI.eraseFromParent();
26778bcb0991SDimitry Andric     return Legalized;
26788bcb0991SDimitry Andric   }
26798bcb0991SDimitry Andric   case G_SHUFFLE_VECTOR:
26808bcb0991SDimitry Andric     return lowerShuffleVector(MI);
26818bcb0991SDimitry Andric   case G_DYN_STACKALLOC:
26828bcb0991SDimitry Andric     return lowerDynStackAlloc(MI);
26838bcb0991SDimitry Andric   case G_EXTRACT:
26848bcb0991SDimitry Andric     return lowerExtract(MI);
26858bcb0991SDimitry Andric   case G_INSERT:
26868bcb0991SDimitry Andric     return lowerInsert(MI);
2687480093f4SDimitry Andric   case G_BSWAP:
2688480093f4SDimitry Andric     return lowerBswap(MI);
2689480093f4SDimitry Andric   case G_BITREVERSE:
2690480093f4SDimitry Andric     return lowerBitreverse(MI);
2691480093f4SDimitry Andric   case G_READ_REGISTER:
2692*5ffd83dbSDimitry Andric   case G_WRITE_REGISTER:
2693*5ffd83dbSDimitry Andric     return lowerReadWriteRegister(MI);
26940b57cec5SDimitry Andric   }
26950b57cec5SDimitry Andric }
26960b57cec5SDimitry Andric 
26970b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorImplicitDef(
26980b57cec5SDimitry Andric     MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy) {
26990b57cec5SDimitry Andric   SmallVector<Register, 2> DstRegs;
27000b57cec5SDimitry Andric 
27010b57cec5SDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
27020b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
27030b57cec5SDimitry Andric   unsigned Size = MRI.getType(DstReg).getSizeInBits();
27040b57cec5SDimitry Andric   int NumParts = Size / NarrowSize;
27050b57cec5SDimitry Andric   // FIXME: Don't know how to handle the situation where the small vectors
27060b57cec5SDimitry Andric   // aren't all the same size yet.
27070b57cec5SDimitry Andric   if (Size % NarrowSize != 0)
27080b57cec5SDimitry Andric     return UnableToLegalize;
27090b57cec5SDimitry Andric 
27100b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
27110b57cec5SDimitry Andric     Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
27120b57cec5SDimitry Andric     MIRBuilder.buildUndef(TmpReg);
27130b57cec5SDimitry Andric     DstRegs.push_back(TmpReg);
27140b57cec5SDimitry Andric   }
27150b57cec5SDimitry Andric 
27160b57cec5SDimitry Andric   if (NarrowTy.isVector())
27170b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
27180b57cec5SDimitry Andric   else
27190b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
27200b57cec5SDimitry Andric 
27210b57cec5SDimitry Andric   MI.eraseFromParent();
27220b57cec5SDimitry Andric   return Legalized;
27230b57cec5SDimitry Andric }
27240b57cec5SDimitry Andric 
27250b57cec5SDimitry Andric // Handle splitting vector operations which need to have the same number of
27260b57cec5SDimitry Andric // elements in each type index, but each type index may have a different element
27270b57cec5SDimitry Andric // type.
27280b57cec5SDimitry Andric //
27290b57cec5SDimitry Andric // e.g.  <4 x s64> = G_SHL <4 x s64>, <4 x s32> ->
27300b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
27310b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
27320b57cec5SDimitry Andric //
27330b57cec5SDimitry Andric // Also handles some irregular breakdown cases, e.g.
27340b57cec5SDimitry Andric // e.g.  <3 x s64> = G_SHL <3 x s64>, <3 x s32> ->
27350b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
27360b57cec5SDimitry Andric //             s64 = G_SHL s64, s32
27370b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
27380b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorMultiEltType(
27390b57cec5SDimitry Andric   MachineInstr &MI, unsigned TypeIdx, LLT NarrowTyArg) {
27400b57cec5SDimitry Andric   if (TypeIdx != 0)
27410b57cec5SDimitry Andric     return UnableToLegalize;
27420b57cec5SDimitry Andric 
27430b57cec5SDimitry Andric   const LLT NarrowTy0 = NarrowTyArg;
27440b57cec5SDimitry Andric   const unsigned NewNumElts =
27450b57cec5SDimitry Andric       NarrowTy0.isVector() ? NarrowTy0.getNumElements() : 1;
27460b57cec5SDimitry Andric 
27470b57cec5SDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
27480b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
27490b57cec5SDimitry Andric   LLT LeftoverTy0;
27500b57cec5SDimitry Andric 
27510b57cec5SDimitry Andric   // All of the operands need to have the same number of elements, so if we can
27520b57cec5SDimitry Andric   // determine a type breakdown for the result type, we can for all of the
27530b57cec5SDimitry Andric   // source types.
27540b57cec5SDimitry Andric   int NumParts = getNarrowTypeBreakDown(DstTy, NarrowTy0, LeftoverTy0).first;
27550b57cec5SDimitry Andric   if (NumParts < 0)
27560b57cec5SDimitry Andric     return UnableToLegalize;
27570b57cec5SDimitry Andric 
27580b57cec5SDimitry Andric   SmallVector<MachineInstrBuilder, 4> NewInsts;
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, LeftoverDstRegs;
27610b57cec5SDimitry Andric   SmallVector<Register, 4> PartRegs, LeftoverRegs;
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) {
27640b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
27650b57cec5SDimitry Andric     LLT SrcTyI = MRI.getType(SrcReg);
27660b57cec5SDimitry Andric     LLT NarrowTyI = LLT::scalarOrVector(NewNumElts, SrcTyI.getScalarType());
27670b57cec5SDimitry Andric     LLT LeftoverTyI;
27680b57cec5SDimitry Andric 
27690b57cec5SDimitry Andric     // Split this operand into the requested typed registers, and any leftover
27700b57cec5SDimitry Andric     // required to reproduce the original type.
27710b57cec5SDimitry Andric     if (!extractParts(SrcReg, SrcTyI, NarrowTyI, LeftoverTyI, PartRegs,
27720b57cec5SDimitry Andric                       LeftoverRegs))
27730b57cec5SDimitry Andric       return UnableToLegalize;
27740b57cec5SDimitry Andric 
27750b57cec5SDimitry Andric     if (I == 1) {
27760b57cec5SDimitry Andric       // For the first operand, create an instruction for each part and setup
27770b57cec5SDimitry Andric       // the result.
27780b57cec5SDimitry Andric       for (Register PartReg : PartRegs) {
27790b57cec5SDimitry Andric         Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy0);
27800b57cec5SDimitry Andric         NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode())
27810b57cec5SDimitry Andric                                .addDef(PartDstReg)
27820b57cec5SDimitry Andric                                .addUse(PartReg));
27830b57cec5SDimitry Andric         DstRegs.push_back(PartDstReg);
27840b57cec5SDimitry Andric       }
27850b57cec5SDimitry Andric 
27860b57cec5SDimitry Andric       for (Register LeftoverReg : LeftoverRegs) {
27870b57cec5SDimitry Andric         Register PartDstReg = MRI.createGenericVirtualRegister(LeftoverTy0);
27880b57cec5SDimitry Andric         NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode())
27890b57cec5SDimitry Andric                                .addDef(PartDstReg)
27900b57cec5SDimitry Andric                                .addUse(LeftoverReg));
27910b57cec5SDimitry Andric         LeftoverDstRegs.push_back(PartDstReg);
27920b57cec5SDimitry Andric       }
27930b57cec5SDimitry Andric     } else {
27940b57cec5SDimitry Andric       assert(NewInsts.size() == PartRegs.size() + LeftoverRegs.size());
27950b57cec5SDimitry Andric 
27960b57cec5SDimitry Andric       // Add the newly created operand splits to the existing instructions. The
27970b57cec5SDimitry Andric       // odd-sized pieces are ordered after the requested NarrowTyArg sized
27980b57cec5SDimitry Andric       // pieces.
27990b57cec5SDimitry Andric       unsigned InstCount = 0;
28000b57cec5SDimitry Andric       for (unsigned J = 0, JE = PartRegs.size(); J != JE; ++J)
28010b57cec5SDimitry Andric         NewInsts[InstCount++].addUse(PartRegs[J]);
28020b57cec5SDimitry Andric       for (unsigned J = 0, JE = LeftoverRegs.size(); J != JE; ++J)
28030b57cec5SDimitry Andric         NewInsts[InstCount++].addUse(LeftoverRegs[J]);
28040b57cec5SDimitry Andric     }
28050b57cec5SDimitry Andric 
28060b57cec5SDimitry Andric     PartRegs.clear();
28070b57cec5SDimitry Andric     LeftoverRegs.clear();
28080b57cec5SDimitry Andric   }
28090b57cec5SDimitry Andric 
28100b57cec5SDimitry Andric   // Insert the newly built operations and rebuild the result register.
28110b57cec5SDimitry Andric   for (auto &MIB : NewInsts)
28120b57cec5SDimitry Andric     MIRBuilder.insertInstr(MIB);
28130b57cec5SDimitry Andric 
28140b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy0, DstRegs, LeftoverTy0, LeftoverDstRegs);
28150b57cec5SDimitry Andric 
28160b57cec5SDimitry Andric   MI.eraseFromParent();
28170b57cec5SDimitry Andric   return Legalized;
28180b57cec5SDimitry Andric }
28190b57cec5SDimitry Andric 
28200b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
28210b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCasts(MachineInstr &MI, unsigned TypeIdx,
28220b57cec5SDimitry Andric                                           LLT NarrowTy) {
28230b57cec5SDimitry Andric   if (TypeIdx != 0)
28240b57cec5SDimitry Andric     return UnableToLegalize;
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
28270b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
28280b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
28290b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
28300b57cec5SDimitry Andric 
28310b57cec5SDimitry Andric   LLT NarrowTy0 = NarrowTy;
28320b57cec5SDimitry Andric   LLT NarrowTy1;
28330b57cec5SDimitry Andric   unsigned NumParts;
28340b57cec5SDimitry Andric 
28350b57cec5SDimitry Andric   if (NarrowTy.isVector()) {
28360b57cec5SDimitry Andric     // Uneven breakdown not handled.
28370b57cec5SDimitry Andric     NumParts = DstTy.getNumElements() / NarrowTy.getNumElements();
28380b57cec5SDimitry Andric     if (NumParts * NarrowTy.getNumElements() != DstTy.getNumElements())
28390b57cec5SDimitry Andric       return UnableToLegalize;
28400b57cec5SDimitry Andric 
28410b57cec5SDimitry Andric     NarrowTy1 = LLT::vector(NumParts, SrcTy.getElementType().getSizeInBits());
28420b57cec5SDimitry Andric   } else {
28430b57cec5SDimitry Andric     NumParts = DstTy.getNumElements();
28440b57cec5SDimitry Andric     NarrowTy1 = SrcTy.getElementType();
28450b57cec5SDimitry Andric   }
28460b57cec5SDimitry Andric 
28470b57cec5SDimitry Andric   SmallVector<Register, 4> SrcRegs, DstRegs;
28480b57cec5SDimitry Andric   extractParts(SrcReg, NarrowTy1, NumParts, SrcRegs);
28490b57cec5SDimitry Andric 
28500b57cec5SDimitry Andric   for (unsigned I = 0; I < NumParts; ++I) {
28510b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
2852*5ffd83dbSDimitry Andric     MachineInstr *NewInst =
2853*5ffd83dbSDimitry Andric         MIRBuilder.buildInstr(MI.getOpcode(), {DstReg}, {SrcRegs[I]});
28540b57cec5SDimitry Andric 
28550b57cec5SDimitry Andric     NewInst->setFlags(MI.getFlags());
28560b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
28570b57cec5SDimitry Andric   }
28580b57cec5SDimitry Andric 
28590b57cec5SDimitry Andric   if (NarrowTy.isVector())
28600b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
28610b57cec5SDimitry Andric   else
28620b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric   MI.eraseFromParent();
28650b57cec5SDimitry Andric   return Legalized;
28660b57cec5SDimitry Andric }
28670b57cec5SDimitry Andric 
28680b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
28690b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCmp(MachineInstr &MI, unsigned TypeIdx,
28700b57cec5SDimitry Andric                                         LLT NarrowTy) {
28710b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
28720b57cec5SDimitry Andric   Register Src0Reg = MI.getOperand(2).getReg();
28730b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
28740b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src0Reg);
28750b57cec5SDimitry Andric 
28760b57cec5SDimitry Andric   unsigned NumParts;
28770b57cec5SDimitry Andric   LLT NarrowTy0, NarrowTy1;
28780b57cec5SDimitry Andric 
28790b57cec5SDimitry Andric   if (TypeIdx == 0) {
28800b57cec5SDimitry Andric     unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1;
28810b57cec5SDimitry Andric     unsigned OldElts = DstTy.getNumElements();
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric     NarrowTy0 = NarrowTy;
28840b57cec5SDimitry Andric     NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : DstTy.getNumElements();
28850b57cec5SDimitry Andric     NarrowTy1 = NarrowTy.isVector() ?
28860b57cec5SDimitry Andric       LLT::vector(NarrowTy.getNumElements(), SrcTy.getScalarSizeInBits()) :
28870b57cec5SDimitry Andric       SrcTy.getElementType();
28880b57cec5SDimitry Andric 
28890b57cec5SDimitry Andric   } else {
28900b57cec5SDimitry Andric     unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1;
28910b57cec5SDimitry Andric     unsigned OldElts = SrcTy.getNumElements();
28920b57cec5SDimitry Andric 
28930b57cec5SDimitry Andric     NumParts = NarrowTy.isVector() ? (OldElts / NewElts) :
28940b57cec5SDimitry Andric       NarrowTy.getNumElements();
28950b57cec5SDimitry Andric     NarrowTy0 = LLT::vector(NarrowTy.getNumElements(),
28960b57cec5SDimitry Andric                             DstTy.getScalarSizeInBits());
28970b57cec5SDimitry Andric     NarrowTy1 = NarrowTy;
28980b57cec5SDimitry Andric   }
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric   // FIXME: Don't know how to handle the situation where the small vectors
29010b57cec5SDimitry Andric   // aren't all the same size yet.
29020b57cec5SDimitry Andric   if (NarrowTy1.isVector() &&
29030b57cec5SDimitry Andric       NarrowTy1.getNumElements() * NumParts != DstTy.getNumElements())
29040b57cec5SDimitry Andric     return UnableToLegalize;
29050b57cec5SDimitry Andric 
29060b57cec5SDimitry Andric   CmpInst::Predicate Pred
29070b57cec5SDimitry Andric     = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
29080b57cec5SDimitry Andric 
29090b57cec5SDimitry Andric   SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
29100b57cec5SDimitry Andric   extractParts(MI.getOperand(2).getReg(), NarrowTy1, NumParts, Src1Regs);
29110b57cec5SDimitry Andric   extractParts(MI.getOperand(3).getReg(), NarrowTy1, NumParts, Src2Regs);
29120b57cec5SDimitry Andric 
29130b57cec5SDimitry Andric   for (unsigned I = 0; I < NumParts; ++I) {
29140b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
29150b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
29160b57cec5SDimitry Andric 
29170b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_ICMP)
29180b57cec5SDimitry Andric       MIRBuilder.buildICmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]);
29190b57cec5SDimitry Andric     else {
29200b57cec5SDimitry Andric       MachineInstr *NewCmp
29210b57cec5SDimitry Andric         = MIRBuilder.buildFCmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]);
29220b57cec5SDimitry Andric       NewCmp->setFlags(MI.getFlags());
29230b57cec5SDimitry Andric     }
29240b57cec5SDimitry Andric   }
29250b57cec5SDimitry Andric 
29260b57cec5SDimitry Andric   if (NarrowTy1.isVector())
29270b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
29280b57cec5SDimitry Andric   else
29290b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
29300b57cec5SDimitry Andric 
29310b57cec5SDimitry Andric   MI.eraseFromParent();
29320b57cec5SDimitry Andric   return Legalized;
29330b57cec5SDimitry Andric }
29340b57cec5SDimitry Andric 
29350b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
29360b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorSelect(MachineInstr &MI, unsigned TypeIdx,
29370b57cec5SDimitry Andric                                            LLT NarrowTy) {
29380b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
29390b57cec5SDimitry Andric   Register CondReg = MI.getOperand(1).getReg();
29400b57cec5SDimitry Andric 
29410b57cec5SDimitry Andric   unsigned NumParts = 0;
29420b57cec5SDimitry Andric   LLT NarrowTy0, NarrowTy1;
29430b57cec5SDimitry Andric 
29440b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
29450b57cec5SDimitry Andric   LLT CondTy = MRI.getType(CondReg);
29460b57cec5SDimitry Andric   unsigned Size = DstTy.getSizeInBits();
29470b57cec5SDimitry Andric 
29480b57cec5SDimitry Andric   assert(TypeIdx == 0 || CondTy.isVector());
29490b57cec5SDimitry Andric 
29500b57cec5SDimitry Andric   if (TypeIdx == 0) {
29510b57cec5SDimitry Andric     NarrowTy0 = NarrowTy;
29520b57cec5SDimitry Andric     NarrowTy1 = CondTy;
29530b57cec5SDimitry Andric 
29540b57cec5SDimitry Andric     unsigned NarrowSize = NarrowTy0.getSizeInBits();
29550b57cec5SDimitry Andric     // FIXME: Don't know how to handle the situation where the small vectors
29560b57cec5SDimitry Andric     // aren't all the same size yet.
29570b57cec5SDimitry Andric     if (Size % NarrowSize != 0)
29580b57cec5SDimitry Andric       return UnableToLegalize;
29590b57cec5SDimitry Andric 
29600b57cec5SDimitry Andric     NumParts = Size / NarrowSize;
29610b57cec5SDimitry Andric 
29620b57cec5SDimitry Andric     // Need to break down the condition type
29630b57cec5SDimitry Andric     if (CondTy.isVector()) {
29640b57cec5SDimitry Andric       if (CondTy.getNumElements() == NumParts)
29650b57cec5SDimitry Andric         NarrowTy1 = CondTy.getElementType();
29660b57cec5SDimitry Andric       else
29670b57cec5SDimitry Andric         NarrowTy1 = LLT::vector(CondTy.getNumElements() / NumParts,
29680b57cec5SDimitry Andric                                 CondTy.getScalarSizeInBits());
29690b57cec5SDimitry Andric     }
29700b57cec5SDimitry Andric   } else {
29710b57cec5SDimitry Andric     NumParts = CondTy.getNumElements();
29720b57cec5SDimitry Andric     if (NarrowTy.isVector()) {
29730b57cec5SDimitry Andric       // TODO: Handle uneven breakdown.
29740b57cec5SDimitry Andric       if (NumParts * NarrowTy.getNumElements() != CondTy.getNumElements())
29750b57cec5SDimitry Andric         return UnableToLegalize;
29760b57cec5SDimitry Andric 
29770b57cec5SDimitry Andric       return UnableToLegalize;
29780b57cec5SDimitry Andric     } else {
29790b57cec5SDimitry Andric       NarrowTy0 = DstTy.getElementType();
29800b57cec5SDimitry Andric       NarrowTy1 = NarrowTy;
29810b57cec5SDimitry Andric     }
29820b57cec5SDimitry Andric   }
29830b57cec5SDimitry Andric 
29840b57cec5SDimitry Andric   SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs;
29850b57cec5SDimitry Andric   if (CondTy.isVector())
29860b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy1, NumParts, Src0Regs);
29870b57cec5SDimitry Andric 
29880b57cec5SDimitry Andric   extractParts(MI.getOperand(2).getReg(), NarrowTy0, NumParts, Src1Regs);
29890b57cec5SDimitry Andric   extractParts(MI.getOperand(3).getReg(), NarrowTy0, NumParts, Src2Regs);
29900b57cec5SDimitry Andric 
29910b57cec5SDimitry Andric   for (unsigned i = 0; i < NumParts; ++i) {
29920b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
29930b57cec5SDimitry Andric     MIRBuilder.buildSelect(DstReg, CondTy.isVector() ? Src0Regs[i] : CondReg,
29940b57cec5SDimitry Andric                            Src1Regs[i], Src2Regs[i]);
29950b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
29960b57cec5SDimitry Andric   }
29970b57cec5SDimitry Andric 
29980b57cec5SDimitry Andric   if (NarrowTy0.isVector())
29990b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
30000b57cec5SDimitry Andric   else
30010b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
30020b57cec5SDimitry Andric 
30030b57cec5SDimitry Andric   MI.eraseFromParent();
30040b57cec5SDimitry Andric   return Legalized;
30050b57cec5SDimitry Andric }
30060b57cec5SDimitry Andric 
30070b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
30080b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx,
30090b57cec5SDimitry Andric                                         LLT NarrowTy) {
30100b57cec5SDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
30110b57cec5SDimitry Andric   LLT PhiTy = MRI.getType(DstReg);
30120b57cec5SDimitry Andric   LLT LeftoverTy;
30130b57cec5SDimitry Andric 
30140b57cec5SDimitry Andric   // All of the operands need to have the same number of elements, so if we can
30150b57cec5SDimitry Andric   // determine a type breakdown for the result type, we can for all of the
30160b57cec5SDimitry Andric   // source types.
30170b57cec5SDimitry Andric   int NumParts, NumLeftover;
30180b57cec5SDimitry Andric   std::tie(NumParts, NumLeftover)
30190b57cec5SDimitry Andric     = getNarrowTypeBreakDown(PhiTy, NarrowTy, LeftoverTy);
30200b57cec5SDimitry Andric   if (NumParts < 0)
30210b57cec5SDimitry Andric     return UnableToLegalize;
30220b57cec5SDimitry Andric 
30230b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, LeftoverDstRegs;
30240b57cec5SDimitry Andric   SmallVector<MachineInstrBuilder, 4> NewInsts;
30250b57cec5SDimitry Andric 
30260b57cec5SDimitry Andric   const int TotalNumParts = NumParts + NumLeftover;
30270b57cec5SDimitry Andric 
30280b57cec5SDimitry Andric   // Insert the new phis in the result block first.
30290b57cec5SDimitry Andric   for (int I = 0; I != TotalNumParts; ++I) {
30300b57cec5SDimitry Andric     LLT Ty = I < NumParts ? NarrowTy : LeftoverTy;
30310b57cec5SDimitry Andric     Register PartDstReg = MRI.createGenericVirtualRegister(Ty);
30320b57cec5SDimitry Andric     NewInsts.push_back(MIRBuilder.buildInstr(TargetOpcode::G_PHI)
30330b57cec5SDimitry Andric                        .addDef(PartDstReg));
30340b57cec5SDimitry Andric     if (I < NumParts)
30350b57cec5SDimitry Andric       DstRegs.push_back(PartDstReg);
30360b57cec5SDimitry Andric     else
30370b57cec5SDimitry Andric       LeftoverDstRegs.push_back(PartDstReg);
30380b57cec5SDimitry Andric   }
30390b57cec5SDimitry Andric 
30400b57cec5SDimitry Andric   MachineBasicBlock *MBB = MI.getParent();
30410b57cec5SDimitry Andric   MIRBuilder.setInsertPt(*MBB, MBB->getFirstNonPHI());
30420b57cec5SDimitry Andric   insertParts(DstReg, PhiTy, NarrowTy, DstRegs, LeftoverTy, LeftoverDstRegs);
30430b57cec5SDimitry Andric 
30440b57cec5SDimitry Andric   SmallVector<Register, 4> PartRegs, LeftoverRegs;
30450b57cec5SDimitry Andric 
30460b57cec5SDimitry Andric   // Insert code to extract the incoming values in each predecessor block.
30470b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
30480b57cec5SDimitry Andric     PartRegs.clear();
30490b57cec5SDimitry Andric     LeftoverRegs.clear();
30500b57cec5SDimitry Andric 
30510b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
30520b57cec5SDimitry Andric     MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
30530b57cec5SDimitry Andric     MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
30540b57cec5SDimitry Andric 
30550b57cec5SDimitry Andric     LLT Unused;
30560b57cec5SDimitry Andric     if (!extractParts(SrcReg, PhiTy, NarrowTy, Unused, PartRegs,
30570b57cec5SDimitry Andric                       LeftoverRegs))
30580b57cec5SDimitry Andric       return UnableToLegalize;
30590b57cec5SDimitry Andric 
30600b57cec5SDimitry Andric     // Add the newly created operand splits to the existing instructions. The
30610b57cec5SDimitry Andric     // odd-sized pieces are ordered after the requested NarrowTyArg sized
30620b57cec5SDimitry Andric     // pieces.
30630b57cec5SDimitry Andric     for (int J = 0; J != TotalNumParts; ++J) {
30640b57cec5SDimitry Andric       MachineInstrBuilder MIB = NewInsts[J];
30650b57cec5SDimitry Andric       MIB.addUse(J < NumParts ? PartRegs[J] : LeftoverRegs[J - NumParts]);
30660b57cec5SDimitry Andric       MIB.addMBB(&OpMBB);
30670b57cec5SDimitry Andric     }
30680b57cec5SDimitry Andric   }
30690b57cec5SDimitry Andric 
30700b57cec5SDimitry Andric   MI.eraseFromParent();
30710b57cec5SDimitry Andric   return Legalized;
30720b57cec5SDimitry Andric }
30730b57cec5SDimitry Andric 
30740b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
30758bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorUnmergeValues(MachineInstr &MI,
30768bcb0991SDimitry Andric                                                   unsigned TypeIdx,
30778bcb0991SDimitry Andric                                                   LLT NarrowTy) {
30788bcb0991SDimitry Andric   if (TypeIdx != 1)
30798bcb0991SDimitry Andric     return UnableToLegalize;
30808bcb0991SDimitry Andric 
30818bcb0991SDimitry Andric   const int NumDst = MI.getNumOperands() - 1;
30828bcb0991SDimitry Andric   const Register SrcReg = MI.getOperand(NumDst).getReg();
30838bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
30848bcb0991SDimitry Andric 
30858bcb0991SDimitry Andric   LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
30868bcb0991SDimitry Andric 
30878bcb0991SDimitry Andric   // TODO: Create sequence of extracts.
30888bcb0991SDimitry Andric   if (DstTy == NarrowTy)
30898bcb0991SDimitry Andric     return UnableToLegalize;
30908bcb0991SDimitry Andric 
30918bcb0991SDimitry Andric   LLT GCDTy = getGCDType(SrcTy, NarrowTy);
30928bcb0991SDimitry Andric   if (DstTy == GCDTy) {
30938bcb0991SDimitry Andric     // This would just be a copy of the same unmerge.
30948bcb0991SDimitry Andric     // TODO: Create extracts, pad with undef and create intermediate merges.
30958bcb0991SDimitry Andric     return UnableToLegalize;
30968bcb0991SDimitry Andric   }
30978bcb0991SDimitry Andric 
30988bcb0991SDimitry Andric   auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg);
30998bcb0991SDimitry Andric   const int NumUnmerge = Unmerge->getNumOperands() - 1;
31008bcb0991SDimitry Andric   const int PartsPerUnmerge = NumDst / NumUnmerge;
31018bcb0991SDimitry Andric 
31028bcb0991SDimitry Andric   for (int I = 0; I != NumUnmerge; ++I) {
31038bcb0991SDimitry Andric     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES);
31048bcb0991SDimitry Andric 
31058bcb0991SDimitry Andric     for (int J = 0; J != PartsPerUnmerge; ++J)
31068bcb0991SDimitry Andric       MIB.addDef(MI.getOperand(I * PartsPerUnmerge + J).getReg());
31078bcb0991SDimitry Andric     MIB.addUse(Unmerge.getReg(I));
31088bcb0991SDimitry Andric   }
31098bcb0991SDimitry Andric 
31108bcb0991SDimitry Andric   MI.eraseFromParent();
31118bcb0991SDimitry Andric   return Legalized;
31128bcb0991SDimitry Andric }
31138bcb0991SDimitry Andric 
31148bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
31158bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorBuildVector(MachineInstr &MI,
31168bcb0991SDimitry Andric                                                 unsigned TypeIdx,
31178bcb0991SDimitry Andric                                                 LLT NarrowTy) {
31188bcb0991SDimitry Andric   assert(TypeIdx == 0 && "not a vector type index");
31198bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
31208bcb0991SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
31218bcb0991SDimitry Andric   LLT SrcTy = DstTy.getElementType();
31228bcb0991SDimitry Andric 
31238bcb0991SDimitry Andric   int DstNumElts = DstTy.getNumElements();
31248bcb0991SDimitry Andric   int NarrowNumElts = NarrowTy.getNumElements();
31258bcb0991SDimitry Andric   int NumConcat = (DstNumElts + NarrowNumElts - 1) / NarrowNumElts;
31268bcb0991SDimitry Andric   LLT WidenedDstTy = LLT::vector(NarrowNumElts * NumConcat, SrcTy);
31278bcb0991SDimitry Andric 
31288bcb0991SDimitry Andric   SmallVector<Register, 8> ConcatOps;
31298bcb0991SDimitry Andric   SmallVector<Register, 8> SubBuildVector;
31308bcb0991SDimitry Andric 
31318bcb0991SDimitry Andric   Register UndefReg;
31328bcb0991SDimitry Andric   if (WidenedDstTy != DstTy)
31338bcb0991SDimitry Andric     UndefReg = MIRBuilder.buildUndef(SrcTy).getReg(0);
31348bcb0991SDimitry Andric 
31358bcb0991SDimitry Andric   // Create a G_CONCAT_VECTORS of NarrowTy pieces, padding with undef as
31368bcb0991SDimitry Andric   // necessary.
31378bcb0991SDimitry Andric   //
31388bcb0991SDimitry Andric   // %3:_(<3 x s16>) = G_BUILD_VECTOR %0, %1, %2
31398bcb0991SDimitry Andric   //   -> <2 x s16>
31408bcb0991SDimitry Andric   //
31418bcb0991SDimitry Andric   // %4:_(s16) = G_IMPLICIT_DEF
31428bcb0991SDimitry Andric   // %5:_(<2 x s16>) = G_BUILD_VECTOR %0, %1
31438bcb0991SDimitry Andric   // %6:_(<2 x s16>) = G_BUILD_VECTOR %2, %4
31448bcb0991SDimitry Andric   // %7:_(<4 x s16>) = G_CONCAT_VECTORS %5, %6
31458bcb0991SDimitry Andric   // %3:_(<3 x s16>) = G_EXTRACT %7, 0
31468bcb0991SDimitry Andric   for (int I = 0; I != NumConcat; ++I) {
31478bcb0991SDimitry Andric     for (int J = 0; J != NarrowNumElts; ++J) {
31488bcb0991SDimitry Andric       int SrcIdx = NarrowNumElts * I + J;
31498bcb0991SDimitry Andric 
31508bcb0991SDimitry Andric       if (SrcIdx < DstNumElts) {
31518bcb0991SDimitry Andric         Register SrcReg = MI.getOperand(SrcIdx + 1).getReg();
31528bcb0991SDimitry Andric         SubBuildVector.push_back(SrcReg);
31538bcb0991SDimitry Andric       } else
31548bcb0991SDimitry Andric         SubBuildVector.push_back(UndefReg);
31558bcb0991SDimitry Andric     }
31568bcb0991SDimitry Andric 
31578bcb0991SDimitry Andric     auto BuildVec = MIRBuilder.buildBuildVector(NarrowTy, SubBuildVector);
31588bcb0991SDimitry Andric     ConcatOps.push_back(BuildVec.getReg(0));
31598bcb0991SDimitry Andric     SubBuildVector.clear();
31608bcb0991SDimitry Andric   }
31618bcb0991SDimitry Andric 
31628bcb0991SDimitry Andric   if (DstTy == WidenedDstTy)
31638bcb0991SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, ConcatOps);
31648bcb0991SDimitry Andric   else {
31658bcb0991SDimitry Andric     auto Concat = MIRBuilder.buildConcatVectors(WidenedDstTy, ConcatOps);
31668bcb0991SDimitry Andric     MIRBuilder.buildExtract(DstReg, Concat, 0);
31678bcb0991SDimitry Andric   }
31688bcb0991SDimitry Andric 
31698bcb0991SDimitry Andric   MI.eraseFromParent();
31708bcb0991SDimitry Andric   return Legalized;
31718bcb0991SDimitry Andric }
31728bcb0991SDimitry Andric 
31738bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
31740b57cec5SDimitry Andric LegalizerHelper::reduceLoadStoreWidth(MachineInstr &MI, unsigned TypeIdx,
31750b57cec5SDimitry Andric                                       LLT NarrowTy) {
31760b57cec5SDimitry Andric   // FIXME: Don't know how to handle secondary types yet.
31770b57cec5SDimitry Andric   if (TypeIdx != 0)
31780b57cec5SDimitry Andric     return UnableToLegalize;
31790b57cec5SDimitry Andric 
31800b57cec5SDimitry Andric   MachineMemOperand *MMO = *MI.memoperands_begin();
31810b57cec5SDimitry Andric 
31820b57cec5SDimitry Andric   // This implementation doesn't work for atomics. Give up instead of doing
31830b57cec5SDimitry Andric   // something invalid.
31840b57cec5SDimitry Andric   if (MMO->getOrdering() != AtomicOrdering::NotAtomic ||
31850b57cec5SDimitry Andric       MMO->getFailureOrdering() != AtomicOrdering::NotAtomic)
31860b57cec5SDimitry Andric     return UnableToLegalize;
31870b57cec5SDimitry Andric 
31880b57cec5SDimitry Andric   bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD;
31890b57cec5SDimitry Andric   Register ValReg = MI.getOperand(0).getReg();
31900b57cec5SDimitry Andric   Register AddrReg = MI.getOperand(1).getReg();
31910b57cec5SDimitry Andric   LLT ValTy = MRI.getType(ValReg);
31920b57cec5SDimitry Andric 
3193*5ffd83dbSDimitry Andric   // FIXME: Do we need a distinct NarrowMemory legalize action?
3194*5ffd83dbSDimitry Andric   if (ValTy.getSizeInBits() != 8 * MMO->getSize()) {
3195*5ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "Can't narrow extload/truncstore\n");
3196*5ffd83dbSDimitry Andric     return UnableToLegalize;
3197*5ffd83dbSDimitry Andric   }
3198*5ffd83dbSDimitry Andric 
31990b57cec5SDimitry Andric   int NumParts = -1;
32000b57cec5SDimitry Andric   int NumLeftover = -1;
32010b57cec5SDimitry Andric   LLT LeftoverTy;
32020b57cec5SDimitry Andric   SmallVector<Register, 8> NarrowRegs, NarrowLeftoverRegs;
32030b57cec5SDimitry Andric   if (IsLoad) {
32040b57cec5SDimitry Andric     std::tie(NumParts, NumLeftover) = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy);
32050b57cec5SDimitry Andric   } else {
32060b57cec5SDimitry Andric     if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs,
32070b57cec5SDimitry Andric                      NarrowLeftoverRegs)) {
32080b57cec5SDimitry Andric       NumParts = NarrowRegs.size();
32090b57cec5SDimitry Andric       NumLeftover = NarrowLeftoverRegs.size();
32100b57cec5SDimitry Andric     }
32110b57cec5SDimitry Andric   }
32120b57cec5SDimitry Andric 
32130b57cec5SDimitry Andric   if (NumParts == -1)
32140b57cec5SDimitry Andric     return UnableToLegalize;
32150b57cec5SDimitry Andric 
32160b57cec5SDimitry Andric   const LLT OffsetTy = LLT::scalar(MRI.getType(AddrReg).getScalarSizeInBits());
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric   unsigned TotalSize = ValTy.getSizeInBits();
32190b57cec5SDimitry Andric 
32200b57cec5SDimitry Andric   // Split the load/store into PartTy sized pieces starting at Offset. If this
32210b57cec5SDimitry Andric   // is a load, return the new registers in ValRegs. For a store, each elements
32220b57cec5SDimitry Andric   // of ValRegs should be PartTy. Returns the next offset that needs to be
32230b57cec5SDimitry Andric   // handled.
32240b57cec5SDimitry Andric   auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<Register> &ValRegs,
32250b57cec5SDimitry Andric                              unsigned Offset) -> unsigned {
32260b57cec5SDimitry Andric     MachineFunction &MF = MIRBuilder.getMF();
32270b57cec5SDimitry Andric     unsigned PartSize = PartTy.getSizeInBits();
32280b57cec5SDimitry Andric     for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize;
32290b57cec5SDimitry Andric          Offset += PartSize, ++Idx) {
32300b57cec5SDimitry Andric       unsigned ByteSize = PartSize / 8;
32310b57cec5SDimitry Andric       unsigned ByteOffset = Offset / 8;
32320b57cec5SDimitry Andric       Register NewAddrReg;
32330b57cec5SDimitry Andric 
3234480093f4SDimitry Andric       MIRBuilder.materializePtrAdd(NewAddrReg, AddrReg, OffsetTy, ByteOffset);
32350b57cec5SDimitry Andric 
32360b57cec5SDimitry Andric       MachineMemOperand *NewMMO =
32370b57cec5SDimitry Andric         MF.getMachineMemOperand(MMO, ByteOffset, ByteSize);
32380b57cec5SDimitry Andric 
32390b57cec5SDimitry Andric       if (IsLoad) {
32400b57cec5SDimitry Andric         Register Dst = MRI.createGenericVirtualRegister(PartTy);
32410b57cec5SDimitry Andric         ValRegs.push_back(Dst);
32420b57cec5SDimitry Andric         MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO);
32430b57cec5SDimitry Andric       } else {
32440b57cec5SDimitry Andric         MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO);
32450b57cec5SDimitry Andric       }
32460b57cec5SDimitry Andric     }
32470b57cec5SDimitry Andric 
32480b57cec5SDimitry Andric     return Offset;
32490b57cec5SDimitry Andric   };
32500b57cec5SDimitry Andric 
32510b57cec5SDimitry Andric   unsigned HandledOffset = splitTypePieces(NarrowTy, NarrowRegs, 0);
32520b57cec5SDimitry Andric 
32530b57cec5SDimitry Andric   // Handle the rest of the register if this isn't an even type breakdown.
32540b57cec5SDimitry Andric   if (LeftoverTy.isValid())
32550b57cec5SDimitry Andric     splitTypePieces(LeftoverTy, NarrowLeftoverRegs, HandledOffset);
32560b57cec5SDimitry Andric 
32570b57cec5SDimitry Andric   if (IsLoad) {
32580b57cec5SDimitry Andric     insertParts(ValReg, ValTy, NarrowTy, NarrowRegs,
32590b57cec5SDimitry Andric                 LeftoverTy, NarrowLeftoverRegs);
32600b57cec5SDimitry Andric   }
32610b57cec5SDimitry Andric 
32620b57cec5SDimitry Andric   MI.eraseFromParent();
32630b57cec5SDimitry Andric   return Legalized;
32640b57cec5SDimitry Andric }
32650b57cec5SDimitry Andric 
32660b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
3267*5ffd83dbSDimitry Andric LegalizerHelper::reduceOperationWidth(MachineInstr &MI, unsigned int TypeIdx,
3268*5ffd83dbSDimitry Andric                                       LLT NarrowTy) {
3269*5ffd83dbSDimitry Andric   assert(TypeIdx == 0 && "only one type index expected");
3270*5ffd83dbSDimitry Andric 
3271*5ffd83dbSDimitry Andric   const unsigned Opc = MI.getOpcode();
3272*5ffd83dbSDimitry Andric   const int NumOps = MI.getNumOperands() - 1;
3273*5ffd83dbSDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
3274*5ffd83dbSDimitry Andric   const unsigned Flags = MI.getFlags();
3275*5ffd83dbSDimitry Andric   const unsigned NarrowSize = NarrowTy.getSizeInBits();
3276*5ffd83dbSDimitry Andric   const LLT NarrowScalarTy = LLT::scalar(NarrowSize);
3277*5ffd83dbSDimitry Andric 
3278*5ffd83dbSDimitry Andric   assert(NumOps <= 3 && "expected instruction with 1 result and 1-3 sources");
3279*5ffd83dbSDimitry Andric 
3280*5ffd83dbSDimitry Andric   // First of all check whether we are narrowing (changing the element type)
3281*5ffd83dbSDimitry Andric   // or reducing the vector elements
3282*5ffd83dbSDimitry Andric   const LLT DstTy = MRI.getType(DstReg);
3283*5ffd83dbSDimitry Andric   const bool IsNarrow = NarrowTy.getScalarType() != DstTy.getScalarType();
3284*5ffd83dbSDimitry Andric 
3285*5ffd83dbSDimitry Andric   SmallVector<Register, 8> ExtractedRegs[3];
3286*5ffd83dbSDimitry Andric   SmallVector<Register, 8> Parts;
3287*5ffd83dbSDimitry Andric 
3288*5ffd83dbSDimitry Andric   unsigned NarrowElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1;
3289*5ffd83dbSDimitry Andric 
3290*5ffd83dbSDimitry Andric   // Break down all the sources into NarrowTy pieces we can operate on. This may
3291*5ffd83dbSDimitry Andric   // involve creating merges to a wider type, padded with undef.
3292*5ffd83dbSDimitry Andric   for (int I = 0; I != NumOps; ++I) {
3293*5ffd83dbSDimitry Andric     Register SrcReg = MI.getOperand(I + 1).getReg();
3294*5ffd83dbSDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
3295*5ffd83dbSDimitry Andric 
3296*5ffd83dbSDimitry Andric     // The type to narrow SrcReg to. For narrowing, this is a smaller scalar.
3297*5ffd83dbSDimitry Andric     // For fewerElements, this is a smaller vector with the same element type.
3298*5ffd83dbSDimitry Andric     LLT OpNarrowTy;
3299*5ffd83dbSDimitry Andric     if (IsNarrow) {
3300*5ffd83dbSDimitry Andric       OpNarrowTy = NarrowScalarTy;
3301*5ffd83dbSDimitry Andric 
3302*5ffd83dbSDimitry Andric       // In case of narrowing, we need to cast vectors to scalars for this to
3303*5ffd83dbSDimitry Andric       // work properly
3304*5ffd83dbSDimitry Andric       // FIXME: Can we do without the bitcast here if we're narrowing?
3305*5ffd83dbSDimitry Andric       if (SrcTy.isVector()) {
3306*5ffd83dbSDimitry Andric         SrcTy = LLT::scalar(SrcTy.getSizeInBits());
3307*5ffd83dbSDimitry Andric         SrcReg = MIRBuilder.buildBitcast(SrcTy, SrcReg).getReg(0);
3308*5ffd83dbSDimitry Andric       }
3309*5ffd83dbSDimitry Andric     } else {
3310*5ffd83dbSDimitry Andric       OpNarrowTy = LLT::scalarOrVector(NarrowElts, SrcTy.getScalarType());
3311*5ffd83dbSDimitry Andric     }
3312*5ffd83dbSDimitry Andric 
3313*5ffd83dbSDimitry Andric     LLT GCDTy = extractGCDType(ExtractedRegs[I], SrcTy, OpNarrowTy, SrcReg);
3314*5ffd83dbSDimitry Andric 
3315*5ffd83dbSDimitry Andric     // Build a sequence of NarrowTy pieces in ExtractedRegs for this operand.
3316*5ffd83dbSDimitry Andric     buildLCMMergePieces(SrcTy, OpNarrowTy, GCDTy, ExtractedRegs[I],
3317*5ffd83dbSDimitry Andric                         TargetOpcode::G_ANYEXT);
3318*5ffd83dbSDimitry Andric   }
3319*5ffd83dbSDimitry Andric 
3320*5ffd83dbSDimitry Andric   SmallVector<Register, 8> ResultRegs;
3321*5ffd83dbSDimitry Andric 
3322*5ffd83dbSDimitry Andric   // Input operands for each sub-instruction.
3323*5ffd83dbSDimitry Andric   SmallVector<SrcOp, 4> InputRegs(NumOps, Register());
3324*5ffd83dbSDimitry Andric 
3325*5ffd83dbSDimitry Andric   int NumParts = ExtractedRegs[0].size();
3326*5ffd83dbSDimitry Andric   const unsigned DstSize = DstTy.getSizeInBits();
3327*5ffd83dbSDimitry Andric   const LLT DstScalarTy = LLT::scalar(DstSize);
3328*5ffd83dbSDimitry Andric 
3329*5ffd83dbSDimitry Andric   // Narrowing needs to use scalar types
3330*5ffd83dbSDimitry Andric   LLT DstLCMTy, NarrowDstTy;
3331*5ffd83dbSDimitry Andric   if (IsNarrow) {
3332*5ffd83dbSDimitry Andric     DstLCMTy = getLCMType(DstScalarTy, NarrowScalarTy);
3333*5ffd83dbSDimitry Andric     NarrowDstTy = NarrowScalarTy;
3334*5ffd83dbSDimitry Andric   } else {
3335*5ffd83dbSDimitry Andric     DstLCMTy = getLCMType(DstTy, NarrowTy);
3336*5ffd83dbSDimitry Andric     NarrowDstTy = NarrowTy;
3337*5ffd83dbSDimitry Andric   }
3338*5ffd83dbSDimitry Andric 
3339*5ffd83dbSDimitry Andric   // We widened the source registers to satisfy merge/unmerge size
3340*5ffd83dbSDimitry Andric   // constraints. We'll have some extra fully undef parts.
3341*5ffd83dbSDimitry Andric   const int NumRealParts = (DstSize + NarrowSize - 1) / NarrowSize;
3342*5ffd83dbSDimitry Andric 
3343*5ffd83dbSDimitry Andric   for (int I = 0; I != NumRealParts; ++I) {
3344*5ffd83dbSDimitry Andric     // Emit this instruction on each of the split pieces.
3345*5ffd83dbSDimitry Andric     for (int J = 0; J != NumOps; ++J)
3346*5ffd83dbSDimitry Andric       InputRegs[J] = ExtractedRegs[J][I];
3347*5ffd83dbSDimitry Andric 
3348*5ffd83dbSDimitry Andric     auto Inst = MIRBuilder.buildInstr(Opc, {NarrowDstTy}, InputRegs, Flags);
3349*5ffd83dbSDimitry Andric     ResultRegs.push_back(Inst.getReg(0));
3350*5ffd83dbSDimitry Andric   }
3351*5ffd83dbSDimitry Andric 
3352*5ffd83dbSDimitry Andric   // Fill out the widened result with undef instead of creating instructions
3353*5ffd83dbSDimitry Andric   // with undef inputs.
3354*5ffd83dbSDimitry Andric   int NumUndefParts = NumParts - NumRealParts;
3355*5ffd83dbSDimitry Andric   if (NumUndefParts != 0)
3356*5ffd83dbSDimitry Andric     ResultRegs.append(NumUndefParts,
3357*5ffd83dbSDimitry Andric                       MIRBuilder.buildUndef(NarrowDstTy).getReg(0));
3358*5ffd83dbSDimitry Andric 
3359*5ffd83dbSDimitry Andric   // Extract the possibly padded result. Use a scratch register if we need to do
3360*5ffd83dbSDimitry Andric   // a final bitcast, otherwise use the original result register.
3361*5ffd83dbSDimitry Andric   Register MergeDstReg;
3362*5ffd83dbSDimitry Andric   if (IsNarrow && DstTy.isVector())
3363*5ffd83dbSDimitry Andric     MergeDstReg = MRI.createGenericVirtualRegister(DstScalarTy);
3364*5ffd83dbSDimitry Andric   else
3365*5ffd83dbSDimitry Andric     MergeDstReg = DstReg;
3366*5ffd83dbSDimitry Andric 
3367*5ffd83dbSDimitry Andric   buildWidenedRemergeToDst(MergeDstReg, DstLCMTy, ResultRegs);
3368*5ffd83dbSDimitry Andric 
3369*5ffd83dbSDimitry Andric   // Recast to vector if we narrowed a vector
3370*5ffd83dbSDimitry Andric   if (IsNarrow && DstTy.isVector())
3371*5ffd83dbSDimitry Andric     MIRBuilder.buildBitcast(DstReg, MergeDstReg);
3372*5ffd83dbSDimitry Andric 
3373*5ffd83dbSDimitry Andric   MI.eraseFromParent();
3374*5ffd83dbSDimitry Andric   return Legalized;
3375*5ffd83dbSDimitry Andric }
3376*5ffd83dbSDimitry Andric 
3377*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
3378*5ffd83dbSDimitry Andric LegalizerHelper::fewerElementsVectorSextInReg(MachineInstr &MI, unsigned TypeIdx,
3379*5ffd83dbSDimitry Andric                                               LLT NarrowTy) {
3380*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
3381*5ffd83dbSDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
3382*5ffd83dbSDimitry Andric   int64_t Imm = MI.getOperand(2).getImm();
3383*5ffd83dbSDimitry Andric 
3384*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
3385*5ffd83dbSDimitry Andric 
3386*5ffd83dbSDimitry Andric   SmallVector<Register, 8> Parts;
3387*5ffd83dbSDimitry Andric   LLT GCDTy = extractGCDType(Parts, DstTy, NarrowTy, SrcReg);
3388*5ffd83dbSDimitry Andric   LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts);
3389*5ffd83dbSDimitry Andric 
3390*5ffd83dbSDimitry Andric   for (Register &R : Parts)
3391*5ffd83dbSDimitry Andric     R = MIRBuilder.buildSExtInReg(NarrowTy, R, Imm).getReg(0);
3392*5ffd83dbSDimitry Andric 
3393*5ffd83dbSDimitry Andric   buildWidenedRemergeToDst(DstReg, LCMTy, Parts);
3394*5ffd83dbSDimitry Andric 
3395*5ffd83dbSDimitry Andric   MI.eraseFromParent();
3396*5ffd83dbSDimitry Andric   return Legalized;
3397*5ffd83dbSDimitry Andric }
3398*5ffd83dbSDimitry Andric 
3399*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
34000b57cec5SDimitry Andric LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx,
34010b57cec5SDimitry Andric                                      LLT NarrowTy) {
34020b57cec5SDimitry Andric   using namespace TargetOpcode;
34030b57cec5SDimitry Andric 
34040b57cec5SDimitry Andric   switch (MI.getOpcode()) {
34050b57cec5SDimitry Andric   case G_IMPLICIT_DEF:
34060b57cec5SDimitry Andric     return fewerElementsVectorImplicitDef(MI, TypeIdx, NarrowTy);
3407*5ffd83dbSDimitry Andric   case G_TRUNC:
34080b57cec5SDimitry Andric   case G_AND:
34090b57cec5SDimitry Andric   case G_OR:
34100b57cec5SDimitry Andric   case G_XOR:
34110b57cec5SDimitry Andric   case G_ADD:
34120b57cec5SDimitry Andric   case G_SUB:
34130b57cec5SDimitry Andric   case G_MUL:
34140b57cec5SDimitry Andric   case G_SMULH:
34150b57cec5SDimitry Andric   case G_UMULH:
34160b57cec5SDimitry Andric   case G_FADD:
34170b57cec5SDimitry Andric   case G_FMUL:
34180b57cec5SDimitry Andric   case G_FSUB:
34190b57cec5SDimitry Andric   case G_FNEG:
34200b57cec5SDimitry Andric   case G_FABS:
34210b57cec5SDimitry Andric   case G_FCANONICALIZE:
34220b57cec5SDimitry Andric   case G_FDIV:
34230b57cec5SDimitry Andric   case G_FREM:
34240b57cec5SDimitry Andric   case G_FMA:
34258bcb0991SDimitry Andric   case G_FMAD:
34260b57cec5SDimitry Andric   case G_FPOW:
34270b57cec5SDimitry Andric   case G_FEXP:
34280b57cec5SDimitry Andric   case G_FEXP2:
34290b57cec5SDimitry Andric   case G_FLOG:
34300b57cec5SDimitry Andric   case G_FLOG2:
34310b57cec5SDimitry Andric   case G_FLOG10:
34320b57cec5SDimitry Andric   case G_FNEARBYINT:
34330b57cec5SDimitry Andric   case G_FCEIL:
34340b57cec5SDimitry Andric   case G_FFLOOR:
34350b57cec5SDimitry Andric   case G_FRINT:
34360b57cec5SDimitry Andric   case G_INTRINSIC_ROUND:
34370b57cec5SDimitry Andric   case G_INTRINSIC_TRUNC:
34380b57cec5SDimitry Andric   case G_FCOS:
34390b57cec5SDimitry Andric   case G_FSIN:
34400b57cec5SDimitry Andric   case G_FSQRT:
34410b57cec5SDimitry Andric   case G_BSWAP:
34428bcb0991SDimitry Andric   case G_BITREVERSE:
34430b57cec5SDimitry Andric   case G_SDIV:
3444480093f4SDimitry Andric   case G_UDIV:
3445480093f4SDimitry Andric   case G_SREM:
3446480093f4SDimitry Andric   case G_UREM:
34470b57cec5SDimitry Andric   case G_SMIN:
34480b57cec5SDimitry Andric   case G_SMAX:
34490b57cec5SDimitry Andric   case G_UMIN:
34500b57cec5SDimitry Andric   case G_UMAX:
34510b57cec5SDimitry Andric   case G_FMINNUM:
34520b57cec5SDimitry Andric   case G_FMAXNUM:
34530b57cec5SDimitry Andric   case G_FMINNUM_IEEE:
34540b57cec5SDimitry Andric   case G_FMAXNUM_IEEE:
34550b57cec5SDimitry Andric   case G_FMINIMUM:
34560b57cec5SDimitry Andric   case G_FMAXIMUM:
3457*5ffd83dbSDimitry Andric   case G_FSHL:
3458*5ffd83dbSDimitry Andric   case G_FSHR:
3459*5ffd83dbSDimitry Andric   case G_FREEZE:
3460*5ffd83dbSDimitry Andric   case G_SADDSAT:
3461*5ffd83dbSDimitry Andric   case G_SSUBSAT:
3462*5ffd83dbSDimitry Andric   case G_UADDSAT:
3463*5ffd83dbSDimitry Andric   case G_USUBSAT:
3464*5ffd83dbSDimitry Andric     return reduceOperationWidth(MI, TypeIdx, NarrowTy);
34650b57cec5SDimitry Andric   case G_SHL:
34660b57cec5SDimitry Andric   case G_LSHR:
34670b57cec5SDimitry Andric   case G_ASHR:
34680b57cec5SDimitry Andric   case G_CTLZ:
34690b57cec5SDimitry Andric   case G_CTLZ_ZERO_UNDEF:
34700b57cec5SDimitry Andric   case G_CTTZ:
34710b57cec5SDimitry Andric   case G_CTTZ_ZERO_UNDEF:
34720b57cec5SDimitry Andric   case G_CTPOP:
34730b57cec5SDimitry Andric   case G_FCOPYSIGN:
34740b57cec5SDimitry Andric     return fewerElementsVectorMultiEltType(MI, TypeIdx, NarrowTy);
34750b57cec5SDimitry Andric   case G_ZEXT:
34760b57cec5SDimitry Andric   case G_SEXT:
34770b57cec5SDimitry Andric   case G_ANYEXT:
34780b57cec5SDimitry Andric   case G_FPEXT:
34790b57cec5SDimitry Andric   case G_FPTRUNC:
34800b57cec5SDimitry Andric   case G_SITOFP:
34810b57cec5SDimitry Andric   case G_UITOFP:
34820b57cec5SDimitry Andric   case G_FPTOSI:
34830b57cec5SDimitry Andric   case G_FPTOUI:
34840b57cec5SDimitry Andric   case G_INTTOPTR:
34850b57cec5SDimitry Andric   case G_PTRTOINT:
34860b57cec5SDimitry Andric   case G_ADDRSPACE_CAST:
34870b57cec5SDimitry Andric     return fewerElementsVectorCasts(MI, TypeIdx, NarrowTy);
34880b57cec5SDimitry Andric   case G_ICMP:
34890b57cec5SDimitry Andric   case G_FCMP:
34900b57cec5SDimitry Andric     return fewerElementsVectorCmp(MI, TypeIdx, NarrowTy);
34910b57cec5SDimitry Andric   case G_SELECT:
34920b57cec5SDimitry Andric     return fewerElementsVectorSelect(MI, TypeIdx, NarrowTy);
34930b57cec5SDimitry Andric   case G_PHI:
34940b57cec5SDimitry Andric     return fewerElementsVectorPhi(MI, TypeIdx, NarrowTy);
34958bcb0991SDimitry Andric   case G_UNMERGE_VALUES:
34968bcb0991SDimitry Andric     return fewerElementsVectorUnmergeValues(MI, TypeIdx, NarrowTy);
34978bcb0991SDimitry Andric   case G_BUILD_VECTOR:
34988bcb0991SDimitry Andric     return fewerElementsVectorBuildVector(MI, TypeIdx, NarrowTy);
34990b57cec5SDimitry Andric   case G_LOAD:
35000b57cec5SDimitry Andric   case G_STORE:
35010b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy);
3502*5ffd83dbSDimitry Andric   case G_SEXT_INREG:
3503*5ffd83dbSDimitry Andric     return fewerElementsVectorSextInReg(MI, TypeIdx, NarrowTy);
35040b57cec5SDimitry Andric   default:
35050b57cec5SDimitry Andric     return UnableToLegalize;
35060b57cec5SDimitry Andric   }
35070b57cec5SDimitry Andric }
35080b57cec5SDimitry Andric 
35090b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
35100b57cec5SDimitry Andric LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt,
35110b57cec5SDimitry Andric                                              const LLT HalfTy, const LLT AmtTy) {
35120b57cec5SDimitry Andric 
35130b57cec5SDimitry Andric   Register InL = MRI.createGenericVirtualRegister(HalfTy);
35140b57cec5SDimitry Andric   Register InH = MRI.createGenericVirtualRegister(HalfTy);
3515*5ffd83dbSDimitry Andric   MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1));
35160b57cec5SDimitry Andric 
35170b57cec5SDimitry Andric   if (Amt.isNullValue()) {
3518*5ffd83dbSDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0), {InL, InH});
35190b57cec5SDimitry Andric     MI.eraseFromParent();
35200b57cec5SDimitry Andric     return Legalized;
35210b57cec5SDimitry Andric   }
35220b57cec5SDimitry Andric 
35230b57cec5SDimitry Andric   LLT NVT = HalfTy;
35240b57cec5SDimitry Andric   unsigned NVTBits = HalfTy.getSizeInBits();
35250b57cec5SDimitry Andric   unsigned VTBits = 2 * NVTBits;
35260b57cec5SDimitry Andric 
35270b57cec5SDimitry Andric   SrcOp Lo(Register(0)), Hi(Register(0));
35280b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_SHL) {
35290b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
35300b57cec5SDimitry Andric       Lo = Hi = MIRBuilder.buildConstant(NVT, 0);
35310b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
35320b57cec5SDimitry Andric       Lo = MIRBuilder.buildConstant(NVT, 0);
35330b57cec5SDimitry Andric       Hi = MIRBuilder.buildShl(NVT, InL,
35340b57cec5SDimitry Andric                                MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
35350b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
35360b57cec5SDimitry Andric       Lo = MIRBuilder.buildConstant(NVT, 0);
35370b57cec5SDimitry Andric       Hi = InL;
35380b57cec5SDimitry Andric     } else {
35390b57cec5SDimitry Andric       Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt));
35400b57cec5SDimitry Andric       auto OrLHS =
35410b57cec5SDimitry Andric           MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt));
35420b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildLShr(
35430b57cec5SDimitry Andric           NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
35440b57cec5SDimitry Andric       Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
35450b57cec5SDimitry Andric     }
35460b57cec5SDimitry Andric   } else if (MI.getOpcode() == TargetOpcode::G_LSHR) {
35470b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
35480b57cec5SDimitry Andric       Lo = Hi = MIRBuilder.buildConstant(NVT, 0);
35490b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
35500b57cec5SDimitry Andric       Lo = MIRBuilder.buildLShr(NVT, InH,
35510b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
35520b57cec5SDimitry Andric       Hi = MIRBuilder.buildConstant(NVT, 0);
35530b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
35540b57cec5SDimitry Andric       Lo = InH;
35550b57cec5SDimitry Andric       Hi = MIRBuilder.buildConstant(NVT, 0);
35560b57cec5SDimitry Andric     } else {
35570b57cec5SDimitry Andric       auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt);
35580b57cec5SDimitry Andric 
35590b57cec5SDimitry Andric       auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst);
35600b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildShl(
35610b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
35620b57cec5SDimitry Andric 
35630b57cec5SDimitry Andric       Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
35640b57cec5SDimitry Andric       Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst);
35650b57cec5SDimitry Andric     }
35660b57cec5SDimitry Andric   } else {
35670b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
35680b57cec5SDimitry Andric       Hi = Lo = MIRBuilder.buildAShr(
35690b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
35700b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
35710b57cec5SDimitry Andric       Lo = MIRBuilder.buildAShr(NVT, InH,
35720b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
35730b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH,
35740b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
35750b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
35760b57cec5SDimitry Andric       Lo = InH;
35770b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH,
35780b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
35790b57cec5SDimitry Andric     } else {
35800b57cec5SDimitry Andric       auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt);
35810b57cec5SDimitry Andric 
35820b57cec5SDimitry Andric       auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst);
35830b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildShl(
35840b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
35850b57cec5SDimitry Andric 
35860b57cec5SDimitry Andric       Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
35870b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst);
35880b57cec5SDimitry Andric     }
35890b57cec5SDimitry Andric   }
35900b57cec5SDimitry Andric 
3591*5ffd83dbSDimitry Andric   MIRBuilder.buildMerge(MI.getOperand(0), {Lo, Hi});
35920b57cec5SDimitry Andric   MI.eraseFromParent();
35930b57cec5SDimitry Andric 
35940b57cec5SDimitry Andric   return Legalized;
35950b57cec5SDimitry Andric }
35960b57cec5SDimitry Andric 
35970b57cec5SDimitry Andric // TODO: Optimize if constant shift amount.
35980b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
35990b57cec5SDimitry Andric LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx,
36000b57cec5SDimitry Andric                                    LLT RequestedTy) {
36010b57cec5SDimitry Andric   if (TypeIdx == 1) {
36020b57cec5SDimitry Andric     Observer.changingInstr(MI);
36030b57cec5SDimitry Andric     narrowScalarSrc(MI, RequestedTy, 2);
36040b57cec5SDimitry Andric     Observer.changedInstr(MI);
36050b57cec5SDimitry Andric     return Legalized;
36060b57cec5SDimitry Andric   }
36070b57cec5SDimitry Andric 
36080b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
36090b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
36100b57cec5SDimitry Andric   if (DstTy.isVector())
36110b57cec5SDimitry Andric     return UnableToLegalize;
36120b57cec5SDimitry Andric 
36130b57cec5SDimitry Andric   Register Amt = MI.getOperand(2).getReg();
36140b57cec5SDimitry Andric   LLT ShiftAmtTy = MRI.getType(Amt);
36150b57cec5SDimitry Andric   const unsigned DstEltSize = DstTy.getScalarSizeInBits();
36160b57cec5SDimitry Andric   if (DstEltSize % 2 != 0)
36170b57cec5SDimitry Andric     return UnableToLegalize;
36180b57cec5SDimitry Andric 
36190b57cec5SDimitry Andric   // Ignore the input type. We can only go to exactly half the size of the
36200b57cec5SDimitry Andric   // input. If that isn't small enough, the resulting pieces will be further
36210b57cec5SDimitry Andric   // legalized.
36220b57cec5SDimitry Andric   const unsigned NewBitSize = DstEltSize / 2;
36230b57cec5SDimitry Andric   const LLT HalfTy = LLT::scalar(NewBitSize);
36240b57cec5SDimitry Andric   const LLT CondTy = LLT::scalar(1);
36250b57cec5SDimitry Andric 
36260b57cec5SDimitry Andric   if (const MachineInstr *KShiftAmt =
36270b57cec5SDimitry Andric           getOpcodeDef(TargetOpcode::G_CONSTANT, Amt, MRI)) {
36280b57cec5SDimitry Andric     return narrowScalarShiftByConstant(
36290b57cec5SDimitry Andric         MI, KShiftAmt->getOperand(1).getCImm()->getValue(), HalfTy, ShiftAmtTy);
36300b57cec5SDimitry Andric   }
36310b57cec5SDimitry Andric 
36320b57cec5SDimitry Andric   // TODO: Expand with known bits.
36330b57cec5SDimitry Andric 
36340b57cec5SDimitry Andric   // Handle the fully general expansion by an unknown amount.
36350b57cec5SDimitry Andric   auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize);
36360b57cec5SDimitry Andric 
36370b57cec5SDimitry Andric   Register InL = MRI.createGenericVirtualRegister(HalfTy);
36380b57cec5SDimitry Andric   Register InH = MRI.createGenericVirtualRegister(HalfTy);
3639*5ffd83dbSDimitry Andric   MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1));
36400b57cec5SDimitry Andric 
36410b57cec5SDimitry Andric   auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits);
36420b57cec5SDimitry Andric   auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt);
36430b57cec5SDimitry Andric 
36440b57cec5SDimitry Andric   auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0);
36450b57cec5SDimitry Andric   auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits);
36460b57cec5SDimitry Andric   auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero);
36470b57cec5SDimitry Andric 
36480b57cec5SDimitry Andric   Register ResultRegs[2];
36490b57cec5SDimitry Andric   switch (MI.getOpcode()) {
36500b57cec5SDimitry Andric   case TargetOpcode::G_SHL: {
36510b57cec5SDimitry Andric     // Short: ShAmt < NewBitSize
36528bcb0991SDimitry Andric     auto LoS = MIRBuilder.buildShl(HalfTy, InL, Amt);
36530b57cec5SDimitry Andric 
36548bcb0991SDimitry Andric     auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, AmtLack);
36558bcb0991SDimitry Andric     auto HiOr = MIRBuilder.buildShl(HalfTy, InH, Amt);
36568bcb0991SDimitry Andric     auto HiS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr);
36570b57cec5SDimitry Andric 
36580b57cec5SDimitry Andric     // Long: ShAmt >= NewBitSize
36590b57cec5SDimitry Andric     auto LoL = MIRBuilder.buildConstant(HalfTy, 0);         // Lo part is zero.
36600b57cec5SDimitry Andric     auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part.
36610b57cec5SDimitry Andric 
36620b57cec5SDimitry Andric     auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL);
36630b57cec5SDimitry Andric     auto Hi = MIRBuilder.buildSelect(
36640b57cec5SDimitry Andric         HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL));
36650b57cec5SDimitry Andric 
36660b57cec5SDimitry Andric     ResultRegs[0] = Lo.getReg(0);
36670b57cec5SDimitry Andric     ResultRegs[1] = Hi.getReg(0);
36680b57cec5SDimitry Andric     break;
36690b57cec5SDimitry Andric   }
36708bcb0991SDimitry Andric   case TargetOpcode::G_LSHR:
36710b57cec5SDimitry Andric   case TargetOpcode::G_ASHR: {
36720b57cec5SDimitry Andric     // Short: ShAmt < NewBitSize
36738bcb0991SDimitry Andric     auto HiS = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, {InH, Amt});
36740b57cec5SDimitry Andric 
36758bcb0991SDimitry Andric     auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, Amt);
36768bcb0991SDimitry Andric     auto HiOr = MIRBuilder.buildShl(HalfTy, InH, AmtLack);
36778bcb0991SDimitry Andric     auto LoS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr);
36780b57cec5SDimitry Andric 
36790b57cec5SDimitry Andric     // Long: ShAmt >= NewBitSize
36808bcb0991SDimitry Andric     MachineInstrBuilder HiL;
36818bcb0991SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_LSHR) {
36828bcb0991SDimitry Andric       HiL = MIRBuilder.buildConstant(HalfTy, 0);            // Hi part is zero.
36838bcb0991SDimitry Andric     } else {
36848bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1);
36858bcb0991SDimitry Andric       HiL = MIRBuilder.buildAShr(HalfTy, InH, ShiftAmt);    // Sign of Hi part.
36868bcb0991SDimitry Andric     }
36878bcb0991SDimitry Andric     auto LoL = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy},
36888bcb0991SDimitry Andric                                      {InH, AmtExcess});     // Lo from Hi part.
36890b57cec5SDimitry Andric 
36900b57cec5SDimitry Andric     auto Lo = MIRBuilder.buildSelect(
36910b57cec5SDimitry Andric         HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL));
36920b57cec5SDimitry Andric 
36930b57cec5SDimitry Andric     auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL);
36940b57cec5SDimitry Andric 
36950b57cec5SDimitry Andric     ResultRegs[0] = Lo.getReg(0);
36960b57cec5SDimitry Andric     ResultRegs[1] = Hi.getReg(0);
36970b57cec5SDimitry Andric     break;
36980b57cec5SDimitry Andric   }
36990b57cec5SDimitry Andric   default:
37000b57cec5SDimitry Andric     llvm_unreachable("not a shift");
37010b57cec5SDimitry Andric   }
37020b57cec5SDimitry Andric 
37030b57cec5SDimitry Andric   MIRBuilder.buildMerge(DstReg, ResultRegs);
37040b57cec5SDimitry Andric   MI.eraseFromParent();
37050b57cec5SDimitry Andric   return Legalized;
37060b57cec5SDimitry Andric }
37070b57cec5SDimitry Andric 
37080b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
37090b57cec5SDimitry Andric LegalizerHelper::moreElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx,
37100b57cec5SDimitry Andric                                        LLT MoreTy) {
37110b57cec5SDimitry Andric   assert(TypeIdx == 0 && "Expecting only Idx 0");
37120b57cec5SDimitry Andric 
37130b57cec5SDimitry Andric   Observer.changingInstr(MI);
37140b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
37150b57cec5SDimitry Andric     MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
37160b57cec5SDimitry Andric     MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
37170b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, I);
37180b57cec5SDimitry Andric   }
37190b57cec5SDimitry Andric 
37200b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
37210b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI());
37220b57cec5SDimitry Andric   moreElementsVectorDst(MI, MoreTy, 0);
37230b57cec5SDimitry Andric   Observer.changedInstr(MI);
37240b57cec5SDimitry Andric   return Legalized;
37250b57cec5SDimitry Andric }
37260b57cec5SDimitry Andric 
37270b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
37280b57cec5SDimitry Andric LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx,
37290b57cec5SDimitry Andric                                     LLT MoreTy) {
37300b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
37310b57cec5SDimitry Andric   switch (Opc) {
37328bcb0991SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF:
37338bcb0991SDimitry Andric   case TargetOpcode::G_LOAD: {
37348bcb0991SDimitry Andric     if (TypeIdx != 0)
37358bcb0991SDimitry Andric       return UnableToLegalize;
37360b57cec5SDimitry Andric     Observer.changingInstr(MI);
37370b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
37380b57cec5SDimitry Andric     Observer.changedInstr(MI);
37390b57cec5SDimitry Andric     return Legalized;
37400b57cec5SDimitry Andric   }
37418bcb0991SDimitry Andric   case TargetOpcode::G_STORE:
37428bcb0991SDimitry Andric     if (TypeIdx != 0)
37438bcb0991SDimitry Andric       return UnableToLegalize;
37448bcb0991SDimitry Andric     Observer.changingInstr(MI);
37458bcb0991SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 0);
37468bcb0991SDimitry Andric     Observer.changedInstr(MI);
37478bcb0991SDimitry Andric     return Legalized;
37480b57cec5SDimitry Andric   case TargetOpcode::G_AND:
37490b57cec5SDimitry Andric   case TargetOpcode::G_OR:
37500b57cec5SDimitry Andric   case TargetOpcode::G_XOR:
37510b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
37520b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
37530b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
3754480093f4SDimitry Andric   case TargetOpcode::G_UMAX:
3755480093f4SDimitry Andric   case TargetOpcode::G_FMINNUM:
3756480093f4SDimitry Andric   case TargetOpcode::G_FMAXNUM:
3757480093f4SDimitry Andric   case TargetOpcode::G_FMINNUM_IEEE:
3758480093f4SDimitry Andric   case TargetOpcode::G_FMAXNUM_IEEE:
3759480093f4SDimitry Andric   case TargetOpcode::G_FMINIMUM:
3760480093f4SDimitry Andric   case TargetOpcode::G_FMAXIMUM: {
37610b57cec5SDimitry Andric     Observer.changingInstr(MI);
37620b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
37630b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 2);
37640b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
37650b57cec5SDimitry Andric     Observer.changedInstr(MI);
37660b57cec5SDimitry Andric     return Legalized;
37670b57cec5SDimitry Andric   }
37680b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
37690b57cec5SDimitry Andric     if (TypeIdx != 1)
37700b57cec5SDimitry Andric       return UnableToLegalize;
37710b57cec5SDimitry Andric     Observer.changingInstr(MI);
37720b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
37730b57cec5SDimitry Andric     Observer.changedInstr(MI);
37740b57cec5SDimitry Andric     return Legalized;
37750b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
3776*5ffd83dbSDimitry Andric   case TargetOpcode::G_FREEZE:
37770b57cec5SDimitry Andric     if (TypeIdx != 0)
37780b57cec5SDimitry Andric       return UnableToLegalize;
37790b57cec5SDimitry Andric     Observer.changingInstr(MI);
37800b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
37810b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
37820b57cec5SDimitry Andric     Observer.changedInstr(MI);
37830b57cec5SDimitry Andric     return Legalized;
37840b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
37850b57cec5SDimitry Andric     if (TypeIdx != 0)
37860b57cec5SDimitry Andric       return UnableToLegalize;
37870b57cec5SDimitry Andric     if (MRI.getType(MI.getOperand(1).getReg()).isVector())
37880b57cec5SDimitry Andric       return UnableToLegalize;
37890b57cec5SDimitry Andric 
37900b57cec5SDimitry Andric     Observer.changingInstr(MI);
37910b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 2);
37920b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 3);
37930b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
37940b57cec5SDimitry Andric     Observer.changedInstr(MI);
37950b57cec5SDimitry Andric     return Legalized;
37968bcb0991SDimitry Andric   case TargetOpcode::G_UNMERGE_VALUES: {
37978bcb0991SDimitry Andric     if (TypeIdx != 1)
37988bcb0991SDimitry Andric       return UnableToLegalize;
37998bcb0991SDimitry Andric 
38008bcb0991SDimitry Andric     LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
38018bcb0991SDimitry Andric     int NumDst = MI.getNumOperands() - 1;
38028bcb0991SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, NumDst);
38038bcb0991SDimitry Andric 
38048bcb0991SDimitry Andric     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES);
38058bcb0991SDimitry Andric     for (int I = 0; I != NumDst; ++I)
38068bcb0991SDimitry Andric       MIB.addDef(MI.getOperand(I).getReg());
38078bcb0991SDimitry Andric 
38088bcb0991SDimitry Andric     int NewNumDst = MoreTy.getSizeInBits() / DstTy.getSizeInBits();
38098bcb0991SDimitry Andric     for (int I = NumDst; I != NewNumDst; ++I)
38108bcb0991SDimitry Andric       MIB.addDef(MRI.createGenericVirtualRegister(DstTy));
38118bcb0991SDimitry Andric 
38128bcb0991SDimitry Andric     MIB.addUse(MI.getOperand(NumDst).getReg());
38138bcb0991SDimitry Andric     MI.eraseFromParent();
38148bcb0991SDimitry Andric     return Legalized;
38158bcb0991SDimitry Andric   }
38160b57cec5SDimitry Andric   case TargetOpcode::G_PHI:
38170b57cec5SDimitry Andric     return moreElementsVectorPhi(MI, TypeIdx, MoreTy);
38180b57cec5SDimitry Andric   default:
38190b57cec5SDimitry Andric     return UnableToLegalize;
38200b57cec5SDimitry Andric   }
38210b57cec5SDimitry Andric }
38220b57cec5SDimitry Andric 
38230b57cec5SDimitry Andric void LegalizerHelper::multiplyRegisters(SmallVectorImpl<Register> &DstRegs,
38240b57cec5SDimitry Andric                                         ArrayRef<Register> Src1Regs,
38250b57cec5SDimitry Andric                                         ArrayRef<Register> Src2Regs,
38260b57cec5SDimitry Andric                                         LLT NarrowTy) {
38270b57cec5SDimitry Andric   MachineIRBuilder &B = MIRBuilder;
38280b57cec5SDimitry Andric   unsigned SrcParts = Src1Regs.size();
38290b57cec5SDimitry Andric   unsigned DstParts = DstRegs.size();
38300b57cec5SDimitry Andric 
38310b57cec5SDimitry Andric   unsigned DstIdx = 0; // Low bits of the result.
38320b57cec5SDimitry Andric   Register FactorSum =
38330b57cec5SDimitry Andric       B.buildMul(NarrowTy, Src1Regs[DstIdx], Src2Regs[DstIdx]).getReg(0);
38340b57cec5SDimitry Andric   DstRegs[DstIdx] = FactorSum;
38350b57cec5SDimitry Andric 
38360b57cec5SDimitry Andric   unsigned CarrySumPrevDstIdx;
38370b57cec5SDimitry Andric   SmallVector<Register, 4> Factors;
38380b57cec5SDimitry Andric 
38390b57cec5SDimitry Andric   for (DstIdx = 1; DstIdx < DstParts; DstIdx++) {
38400b57cec5SDimitry Andric     // Collect low parts of muls for DstIdx.
38410b57cec5SDimitry Andric     for (unsigned i = DstIdx + 1 < SrcParts ? 0 : DstIdx - SrcParts + 1;
38420b57cec5SDimitry Andric          i <= std::min(DstIdx, SrcParts - 1); ++i) {
38430b57cec5SDimitry Andric       MachineInstrBuilder Mul =
38440b57cec5SDimitry Andric           B.buildMul(NarrowTy, Src1Regs[DstIdx - i], Src2Regs[i]);
38450b57cec5SDimitry Andric       Factors.push_back(Mul.getReg(0));
38460b57cec5SDimitry Andric     }
38470b57cec5SDimitry Andric     // Collect high parts of muls from previous DstIdx.
38480b57cec5SDimitry Andric     for (unsigned i = DstIdx < SrcParts ? 0 : DstIdx - SrcParts;
38490b57cec5SDimitry Andric          i <= std::min(DstIdx - 1, SrcParts - 1); ++i) {
38500b57cec5SDimitry Andric       MachineInstrBuilder Umulh =
38510b57cec5SDimitry Andric           B.buildUMulH(NarrowTy, Src1Regs[DstIdx - 1 - i], Src2Regs[i]);
38520b57cec5SDimitry Andric       Factors.push_back(Umulh.getReg(0));
38530b57cec5SDimitry Andric     }
3854480093f4SDimitry Andric     // Add CarrySum from additions calculated for previous DstIdx.
38550b57cec5SDimitry Andric     if (DstIdx != 1) {
38560b57cec5SDimitry Andric       Factors.push_back(CarrySumPrevDstIdx);
38570b57cec5SDimitry Andric     }
38580b57cec5SDimitry Andric 
38590b57cec5SDimitry Andric     Register CarrySum;
38600b57cec5SDimitry Andric     // Add all factors and accumulate all carries into CarrySum.
38610b57cec5SDimitry Andric     if (DstIdx != DstParts - 1) {
38620b57cec5SDimitry Andric       MachineInstrBuilder Uaddo =
38630b57cec5SDimitry Andric           B.buildUAddo(NarrowTy, LLT::scalar(1), Factors[0], Factors[1]);
38640b57cec5SDimitry Andric       FactorSum = Uaddo.getReg(0);
38650b57cec5SDimitry Andric       CarrySum = B.buildZExt(NarrowTy, Uaddo.getReg(1)).getReg(0);
38660b57cec5SDimitry Andric       for (unsigned i = 2; i < Factors.size(); ++i) {
38670b57cec5SDimitry Andric         MachineInstrBuilder Uaddo =
38680b57cec5SDimitry Andric             B.buildUAddo(NarrowTy, LLT::scalar(1), FactorSum, Factors[i]);
38690b57cec5SDimitry Andric         FactorSum = Uaddo.getReg(0);
38700b57cec5SDimitry Andric         MachineInstrBuilder Carry = B.buildZExt(NarrowTy, Uaddo.getReg(1));
38710b57cec5SDimitry Andric         CarrySum = B.buildAdd(NarrowTy, CarrySum, Carry).getReg(0);
38720b57cec5SDimitry Andric       }
38730b57cec5SDimitry Andric     } else {
38740b57cec5SDimitry Andric       // Since value for the next index is not calculated, neither is CarrySum.
38750b57cec5SDimitry Andric       FactorSum = B.buildAdd(NarrowTy, Factors[0], Factors[1]).getReg(0);
38760b57cec5SDimitry Andric       for (unsigned i = 2; i < Factors.size(); ++i)
38770b57cec5SDimitry Andric         FactorSum = B.buildAdd(NarrowTy, FactorSum, Factors[i]).getReg(0);
38780b57cec5SDimitry Andric     }
38790b57cec5SDimitry Andric 
38800b57cec5SDimitry Andric     CarrySumPrevDstIdx = CarrySum;
38810b57cec5SDimitry Andric     DstRegs[DstIdx] = FactorSum;
38820b57cec5SDimitry Andric     Factors.clear();
38830b57cec5SDimitry Andric   }
38840b57cec5SDimitry Andric }
38850b57cec5SDimitry Andric 
38860b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
38870b57cec5SDimitry Andric LegalizerHelper::narrowScalarMul(MachineInstr &MI, LLT NarrowTy) {
38880b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
38890b57cec5SDimitry Andric   Register Src1 = MI.getOperand(1).getReg();
38900b57cec5SDimitry Andric   Register Src2 = MI.getOperand(2).getReg();
38910b57cec5SDimitry Andric 
38920b57cec5SDimitry Andric   LLT Ty = MRI.getType(DstReg);
38930b57cec5SDimitry Andric   if (Ty.isVector())
38940b57cec5SDimitry Andric     return UnableToLegalize;
38950b57cec5SDimitry Andric 
38960b57cec5SDimitry Andric   unsigned SrcSize = MRI.getType(Src1).getSizeInBits();
38970b57cec5SDimitry Andric   unsigned DstSize = Ty.getSizeInBits();
38980b57cec5SDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
38990b57cec5SDimitry Andric   if (DstSize % NarrowSize != 0 || SrcSize % NarrowSize != 0)
39000b57cec5SDimitry Andric     return UnableToLegalize;
39010b57cec5SDimitry Andric 
39020b57cec5SDimitry Andric   unsigned NumDstParts = DstSize / NarrowSize;
39030b57cec5SDimitry Andric   unsigned NumSrcParts = SrcSize / NarrowSize;
39040b57cec5SDimitry Andric   bool IsMulHigh = MI.getOpcode() == TargetOpcode::G_UMULH;
39050b57cec5SDimitry Andric   unsigned DstTmpParts = NumDstParts * (IsMulHigh ? 2 : 1);
39060b57cec5SDimitry Andric 
3907*5ffd83dbSDimitry Andric   SmallVector<Register, 2> Src1Parts, Src2Parts;
3908*5ffd83dbSDimitry Andric   SmallVector<Register, 2> DstTmpRegs(DstTmpParts);
39090b57cec5SDimitry Andric   extractParts(Src1, NarrowTy, NumSrcParts, Src1Parts);
39100b57cec5SDimitry Andric   extractParts(Src2, NarrowTy, NumSrcParts, Src2Parts);
39110b57cec5SDimitry Andric   multiplyRegisters(DstTmpRegs, Src1Parts, Src2Parts, NarrowTy);
39120b57cec5SDimitry Andric 
39130b57cec5SDimitry Andric   // Take only high half of registers if this is high mul.
39140b57cec5SDimitry Andric   ArrayRef<Register> DstRegs(
39150b57cec5SDimitry Andric       IsMulHigh ? &DstTmpRegs[DstTmpParts / 2] : &DstTmpRegs[0], NumDstParts);
39160b57cec5SDimitry Andric   MIRBuilder.buildMerge(DstReg, DstRegs);
39170b57cec5SDimitry Andric   MI.eraseFromParent();
39180b57cec5SDimitry Andric   return Legalized;
39190b57cec5SDimitry Andric }
39200b57cec5SDimitry Andric 
39210b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
39220b57cec5SDimitry Andric LegalizerHelper::narrowScalarExtract(MachineInstr &MI, unsigned TypeIdx,
39230b57cec5SDimitry Andric                                      LLT NarrowTy) {
39240b57cec5SDimitry Andric   if (TypeIdx != 1)
39250b57cec5SDimitry Andric     return UnableToLegalize;
39260b57cec5SDimitry Andric 
39270b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
39280b57cec5SDimitry Andric 
39290b57cec5SDimitry Andric   int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
39300b57cec5SDimitry Andric   // FIXME: add support for when SizeOp1 isn't an exact multiple of
39310b57cec5SDimitry Andric   // NarrowSize.
39320b57cec5SDimitry Andric   if (SizeOp1 % NarrowSize != 0)
39330b57cec5SDimitry Andric     return UnableToLegalize;
39340b57cec5SDimitry Andric   int NumParts = SizeOp1 / NarrowSize;
39350b57cec5SDimitry Andric 
39360b57cec5SDimitry Andric   SmallVector<Register, 2> SrcRegs, DstRegs;
39370b57cec5SDimitry Andric   SmallVector<uint64_t, 2> Indexes;
39380b57cec5SDimitry Andric   extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
39390b57cec5SDimitry Andric 
39400b57cec5SDimitry Andric   Register OpReg = MI.getOperand(0).getReg();
39410b57cec5SDimitry Andric   uint64_t OpStart = MI.getOperand(2).getImm();
39420b57cec5SDimitry Andric   uint64_t OpSize = MRI.getType(OpReg).getSizeInBits();
39430b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
39440b57cec5SDimitry Andric     unsigned SrcStart = i * NarrowSize;
39450b57cec5SDimitry Andric 
39460b57cec5SDimitry Andric     if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) {
39470b57cec5SDimitry Andric       // No part of the extract uses this subregister, ignore it.
39480b57cec5SDimitry Andric       continue;
39490b57cec5SDimitry Andric     } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) {
39500b57cec5SDimitry Andric       // The entire subregister is extracted, forward the value.
39510b57cec5SDimitry Andric       DstRegs.push_back(SrcRegs[i]);
39520b57cec5SDimitry Andric       continue;
39530b57cec5SDimitry Andric     }
39540b57cec5SDimitry Andric 
39550b57cec5SDimitry Andric     // OpSegStart is where this destination segment would start in OpReg if it
39560b57cec5SDimitry Andric     // extended infinitely in both directions.
39570b57cec5SDimitry Andric     int64_t ExtractOffset;
39580b57cec5SDimitry Andric     uint64_t SegSize;
39590b57cec5SDimitry Andric     if (OpStart < SrcStart) {
39600b57cec5SDimitry Andric       ExtractOffset = 0;
39610b57cec5SDimitry Andric       SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart);
39620b57cec5SDimitry Andric     } else {
39630b57cec5SDimitry Andric       ExtractOffset = OpStart - SrcStart;
39640b57cec5SDimitry Andric       SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize);
39650b57cec5SDimitry Andric     }
39660b57cec5SDimitry Andric 
39670b57cec5SDimitry Andric     Register SegReg = SrcRegs[i];
39680b57cec5SDimitry Andric     if (ExtractOffset != 0 || SegSize != NarrowSize) {
39690b57cec5SDimitry Andric       // A genuine extract is needed.
39700b57cec5SDimitry Andric       SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize));
39710b57cec5SDimitry Andric       MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset);
39720b57cec5SDimitry Andric     }
39730b57cec5SDimitry Andric 
39740b57cec5SDimitry Andric     DstRegs.push_back(SegReg);
39750b57cec5SDimitry Andric   }
39760b57cec5SDimitry Andric 
39770b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
39780b57cec5SDimitry Andric   if (MRI.getType(DstReg).isVector())
39790b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
3980*5ffd83dbSDimitry Andric   else if (DstRegs.size() > 1)
39810b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
3982*5ffd83dbSDimitry Andric   else
3983*5ffd83dbSDimitry Andric     MIRBuilder.buildCopy(DstReg, DstRegs[0]);
39840b57cec5SDimitry Andric   MI.eraseFromParent();
39850b57cec5SDimitry Andric   return Legalized;
39860b57cec5SDimitry Andric }
39870b57cec5SDimitry Andric 
39880b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
39890b57cec5SDimitry Andric LegalizerHelper::narrowScalarInsert(MachineInstr &MI, unsigned TypeIdx,
39900b57cec5SDimitry Andric                                     LLT NarrowTy) {
39910b57cec5SDimitry Andric   // FIXME: Don't know how to handle secondary types yet.
39920b57cec5SDimitry Andric   if (TypeIdx != 0)
39930b57cec5SDimitry Andric     return UnableToLegalize;
39940b57cec5SDimitry Andric 
39950b57cec5SDimitry Andric   uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
39960b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
39970b57cec5SDimitry Andric 
39980b57cec5SDimitry Andric   // FIXME: add support for when SizeOp0 isn't an exact multiple of
39990b57cec5SDimitry Andric   // NarrowSize.
40000b57cec5SDimitry Andric   if (SizeOp0 % NarrowSize != 0)
40010b57cec5SDimitry Andric     return UnableToLegalize;
40020b57cec5SDimitry Andric 
40030b57cec5SDimitry Andric   int NumParts = SizeOp0 / NarrowSize;
40040b57cec5SDimitry Andric 
40050b57cec5SDimitry Andric   SmallVector<Register, 2> SrcRegs, DstRegs;
40060b57cec5SDimitry Andric   SmallVector<uint64_t, 2> Indexes;
40070b57cec5SDimitry Andric   extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
40080b57cec5SDimitry Andric 
40090b57cec5SDimitry Andric   Register OpReg = MI.getOperand(2).getReg();
40100b57cec5SDimitry Andric   uint64_t OpStart = MI.getOperand(3).getImm();
40110b57cec5SDimitry Andric   uint64_t OpSize = MRI.getType(OpReg).getSizeInBits();
40120b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
40130b57cec5SDimitry Andric     unsigned DstStart = i * NarrowSize;
40140b57cec5SDimitry Andric 
40150b57cec5SDimitry Andric     if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) {
40160b57cec5SDimitry Andric       // No part of the insert affects this subregister, forward the original.
40170b57cec5SDimitry Andric       DstRegs.push_back(SrcRegs[i]);
40180b57cec5SDimitry Andric       continue;
40190b57cec5SDimitry Andric     } else if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) {
40200b57cec5SDimitry Andric       // The entire subregister is defined by this insert, forward the new
40210b57cec5SDimitry Andric       // value.
40220b57cec5SDimitry Andric       DstRegs.push_back(OpReg);
40230b57cec5SDimitry Andric       continue;
40240b57cec5SDimitry Andric     }
40250b57cec5SDimitry Andric 
40260b57cec5SDimitry Andric     // OpSegStart is where this destination segment would start in OpReg if it
40270b57cec5SDimitry Andric     // extended infinitely in both directions.
40280b57cec5SDimitry Andric     int64_t ExtractOffset, InsertOffset;
40290b57cec5SDimitry Andric     uint64_t SegSize;
40300b57cec5SDimitry Andric     if (OpStart < DstStart) {
40310b57cec5SDimitry Andric       InsertOffset = 0;
40320b57cec5SDimitry Andric       ExtractOffset = DstStart - OpStart;
40330b57cec5SDimitry Andric       SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart);
40340b57cec5SDimitry Andric     } else {
40350b57cec5SDimitry Andric       InsertOffset = OpStart - DstStart;
40360b57cec5SDimitry Andric       ExtractOffset = 0;
40370b57cec5SDimitry Andric       SegSize =
40380b57cec5SDimitry Andric         std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart);
40390b57cec5SDimitry Andric     }
40400b57cec5SDimitry Andric 
40410b57cec5SDimitry Andric     Register SegReg = OpReg;
40420b57cec5SDimitry Andric     if (ExtractOffset != 0 || SegSize != OpSize) {
40430b57cec5SDimitry Andric       // A genuine extract is needed.
40440b57cec5SDimitry Andric       SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize));
40450b57cec5SDimitry Andric       MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset);
40460b57cec5SDimitry Andric     }
40470b57cec5SDimitry Andric 
40480b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
40490b57cec5SDimitry Andric     MIRBuilder.buildInsert(DstReg, SrcRegs[i], SegReg, InsertOffset);
40500b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
40510b57cec5SDimitry Andric   }
40520b57cec5SDimitry Andric 
40530b57cec5SDimitry Andric   assert(DstRegs.size() == (unsigned)NumParts && "not all parts covered");
40540b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
40550b57cec5SDimitry Andric   if(MRI.getType(DstReg).isVector())
40560b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
40570b57cec5SDimitry Andric   else
40580b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
40590b57cec5SDimitry Andric   MI.eraseFromParent();
40600b57cec5SDimitry Andric   return Legalized;
40610b57cec5SDimitry Andric }
40620b57cec5SDimitry Andric 
40630b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
40640b57cec5SDimitry Andric LegalizerHelper::narrowScalarBasic(MachineInstr &MI, unsigned TypeIdx,
40650b57cec5SDimitry Andric                                    LLT NarrowTy) {
40660b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
40670b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
40680b57cec5SDimitry Andric 
40690b57cec5SDimitry Andric   assert(MI.getNumOperands() == 3 && TypeIdx == 0);
40700b57cec5SDimitry Andric 
40710b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, DstLeftoverRegs;
40720b57cec5SDimitry Andric   SmallVector<Register, 4> Src0Regs, Src0LeftoverRegs;
40730b57cec5SDimitry Andric   SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs;
40740b57cec5SDimitry Andric   LLT LeftoverTy;
40750b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(1).getReg(), DstTy, NarrowTy, LeftoverTy,
40760b57cec5SDimitry Andric                     Src0Regs, Src0LeftoverRegs))
40770b57cec5SDimitry Andric     return UnableToLegalize;
40780b57cec5SDimitry Andric 
40790b57cec5SDimitry Andric   LLT Unused;
40800b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, Unused,
40810b57cec5SDimitry Andric                     Src1Regs, Src1LeftoverRegs))
40820b57cec5SDimitry Andric     llvm_unreachable("inconsistent extractParts result");
40830b57cec5SDimitry Andric 
40840b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) {
40850b57cec5SDimitry Andric     auto Inst = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy},
40860b57cec5SDimitry Andric                                         {Src0Regs[I], Src1Regs[I]});
4087*5ffd83dbSDimitry Andric     DstRegs.push_back(Inst.getReg(0));
40880b57cec5SDimitry Andric   }
40890b57cec5SDimitry Andric 
40900b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) {
40910b57cec5SDimitry Andric     auto Inst = MIRBuilder.buildInstr(
40920b57cec5SDimitry Andric       MI.getOpcode(),
40930b57cec5SDimitry Andric       {LeftoverTy}, {Src0LeftoverRegs[I], Src1LeftoverRegs[I]});
4094*5ffd83dbSDimitry Andric     DstLeftoverRegs.push_back(Inst.getReg(0));
40950b57cec5SDimitry Andric   }
40960b57cec5SDimitry Andric 
40970b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy, DstRegs,
40980b57cec5SDimitry Andric               LeftoverTy, DstLeftoverRegs);
40990b57cec5SDimitry Andric 
41000b57cec5SDimitry Andric   MI.eraseFromParent();
41010b57cec5SDimitry Andric   return Legalized;
41020b57cec5SDimitry Andric }
41030b57cec5SDimitry Andric 
41040b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
4105*5ffd83dbSDimitry Andric LegalizerHelper::narrowScalarExt(MachineInstr &MI, unsigned TypeIdx,
4106*5ffd83dbSDimitry Andric                                  LLT NarrowTy) {
4107*5ffd83dbSDimitry Andric   if (TypeIdx != 0)
4108*5ffd83dbSDimitry Andric     return UnableToLegalize;
4109*5ffd83dbSDimitry Andric 
4110*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4111*5ffd83dbSDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
4112*5ffd83dbSDimitry Andric 
4113*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
4114*5ffd83dbSDimitry Andric   if (DstTy.isVector())
4115*5ffd83dbSDimitry Andric     return UnableToLegalize;
4116*5ffd83dbSDimitry Andric 
4117*5ffd83dbSDimitry Andric   SmallVector<Register, 8> Parts;
4118*5ffd83dbSDimitry Andric   LLT GCDTy = extractGCDType(Parts, DstTy, NarrowTy, SrcReg);
4119*5ffd83dbSDimitry Andric   LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts, MI.getOpcode());
4120*5ffd83dbSDimitry Andric   buildWidenedRemergeToDst(DstReg, LCMTy, Parts);
4121*5ffd83dbSDimitry Andric 
4122*5ffd83dbSDimitry Andric   MI.eraseFromParent();
4123*5ffd83dbSDimitry Andric   return Legalized;
4124*5ffd83dbSDimitry Andric }
4125*5ffd83dbSDimitry Andric 
4126*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
41270b57cec5SDimitry Andric LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx,
41280b57cec5SDimitry Andric                                     LLT NarrowTy) {
41290b57cec5SDimitry Andric   if (TypeIdx != 0)
41300b57cec5SDimitry Andric     return UnableToLegalize;
41310b57cec5SDimitry Andric 
41320b57cec5SDimitry Andric   Register CondReg = MI.getOperand(1).getReg();
41330b57cec5SDimitry Andric   LLT CondTy = MRI.getType(CondReg);
41340b57cec5SDimitry Andric   if (CondTy.isVector()) // TODO: Handle vselect
41350b57cec5SDimitry Andric     return UnableToLegalize;
41360b57cec5SDimitry Andric 
41370b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
41380b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
41390b57cec5SDimitry Andric 
41400b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, DstLeftoverRegs;
41410b57cec5SDimitry Andric   SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs;
41420b57cec5SDimitry Andric   SmallVector<Register, 4> Src2Regs, Src2LeftoverRegs;
41430b57cec5SDimitry Andric   LLT LeftoverTy;
41440b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy,
41450b57cec5SDimitry Andric                     Src1Regs, Src1LeftoverRegs))
41460b57cec5SDimitry Andric     return UnableToLegalize;
41470b57cec5SDimitry Andric 
41480b57cec5SDimitry Andric   LLT Unused;
41490b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused,
41500b57cec5SDimitry Andric                     Src2Regs, Src2LeftoverRegs))
41510b57cec5SDimitry Andric     llvm_unreachable("inconsistent extractParts result");
41520b57cec5SDimitry Andric 
41530b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) {
41540b57cec5SDimitry Andric     auto Select = MIRBuilder.buildSelect(NarrowTy,
41550b57cec5SDimitry Andric                                          CondReg, Src1Regs[I], Src2Regs[I]);
4156*5ffd83dbSDimitry Andric     DstRegs.push_back(Select.getReg(0));
41570b57cec5SDimitry Andric   }
41580b57cec5SDimitry Andric 
41590b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) {
41600b57cec5SDimitry Andric     auto Select = MIRBuilder.buildSelect(
41610b57cec5SDimitry Andric       LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]);
4162*5ffd83dbSDimitry Andric     DstLeftoverRegs.push_back(Select.getReg(0));
41630b57cec5SDimitry Andric   }
41640b57cec5SDimitry Andric 
41650b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy, DstRegs,
41660b57cec5SDimitry Andric               LeftoverTy, DstLeftoverRegs);
41670b57cec5SDimitry Andric 
41680b57cec5SDimitry Andric   MI.eraseFromParent();
41690b57cec5SDimitry Andric   return Legalized;
41700b57cec5SDimitry Andric }
41710b57cec5SDimitry Andric 
41720b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
4173*5ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTLZ(MachineInstr &MI, unsigned TypeIdx,
4174*5ffd83dbSDimitry Andric                                   LLT NarrowTy) {
4175*5ffd83dbSDimitry Andric   if (TypeIdx != 1)
4176*5ffd83dbSDimitry Andric     return UnableToLegalize;
4177*5ffd83dbSDimitry Andric 
4178*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4179*5ffd83dbSDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
4180*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
4181*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
4182*5ffd83dbSDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
4183*5ffd83dbSDimitry Andric 
4184*5ffd83dbSDimitry Andric   if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) {
4185*5ffd83dbSDimitry Andric     const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF;
4186*5ffd83dbSDimitry Andric 
4187*5ffd83dbSDimitry Andric     MachineIRBuilder &B = MIRBuilder;
4188*5ffd83dbSDimitry Andric     auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg);
4189*5ffd83dbSDimitry Andric     // ctlz(Hi:Lo) -> Hi == 0 ? (NarrowSize + ctlz(Lo)) : ctlz(Hi)
4190*5ffd83dbSDimitry Andric     auto C_0 = B.buildConstant(NarrowTy, 0);
4191*5ffd83dbSDimitry Andric     auto HiIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1),
4192*5ffd83dbSDimitry Andric                                 UnmergeSrc.getReg(1), C_0);
4193*5ffd83dbSDimitry Andric     auto LoCTLZ = IsUndef ?
4194*5ffd83dbSDimitry Andric       B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0)) :
4195*5ffd83dbSDimitry Andric       B.buildCTLZ(DstTy, UnmergeSrc.getReg(0));
4196*5ffd83dbSDimitry Andric     auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize);
4197*5ffd83dbSDimitry Andric     auto HiIsZeroCTLZ = B.buildAdd(DstTy, LoCTLZ, C_NarrowSize);
4198*5ffd83dbSDimitry Andric     auto HiCTLZ = B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1));
4199*5ffd83dbSDimitry Andric     B.buildSelect(DstReg, HiIsZero, HiIsZeroCTLZ, HiCTLZ);
4200*5ffd83dbSDimitry Andric 
4201*5ffd83dbSDimitry Andric     MI.eraseFromParent();
4202*5ffd83dbSDimitry Andric     return Legalized;
4203*5ffd83dbSDimitry Andric   }
4204*5ffd83dbSDimitry Andric 
4205*5ffd83dbSDimitry Andric   return UnableToLegalize;
4206*5ffd83dbSDimitry Andric }
4207*5ffd83dbSDimitry Andric 
4208*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4209*5ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTTZ(MachineInstr &MI, unsigned TypeIdx,
4210*5ffd83dbSDimitry Andric                                   LLT NarrowTy) {
4211*5ffd83dbSDimitry Andric   if (TypeIdx != 1)
4212*5ffd83dbSDimitry Andric     return UnableToLegalize;
4213*5ffd83dbSDimitry Andric 
4214*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4215*5ffd83dbSDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
4216*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
4217*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
4218*5ffd83dbSDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
4219*5ffd83dbSDimitry Andric 
4220*5ffd83dbSDimitry Andric   if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) {
4221*5ffd83dbSDimitry Andric     const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTTZ_ZERO_UNDEF;
4222*5ffd83dbSDimitry Andric 
4223*5ffd83dbSDimitry Andric     MachineIRBuilder &B = MIRBuilder;
4224*5ffd83dbSDimitry Andric     auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg);
4225*5ffd83dbSDimitry Andric     // cttz(Hi:Lo) -> Lo == 0 ? (cttz(Hi) + NarrowSize) : cttz(Lo)
4226*5ffd83dbSDimitry Andric     auto C_0 = B.buildConstant(NarrowTy, 0);
4227*5ffd83dbSDimitry Andric     auto LoIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1),
4228*5ffd83dbSDimitry Andric                                 UnmergeSrc.getReg(0), C_0);
4229*5ffd83dbSDimitry Andric     auto HiCTTZ = IsUndef ?
4230*5ffd83dbSDimitry Andric       B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1)) :
4231*5ffd83dbSDimitry Andric       B.buildCTTZ(DstTy, UnmergeSrc.getReg(1));
4232*5ffd83dbSDimitry Andric     auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize);
4233*5ffd83dbSDimitry Andric     auto LoIsZeroCTTZ = B.buildAdd(DstTy, HiCTTZ, C_NarrowSize);
4234*5ffd83dbSDimitry Andric     auto LoCTTZ = B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0));
4235*5ffd83dbSDimitry Andric     B.buildSelect(DstReg, LoIsZero, LoIsZeroCTTZ, LoCTTZ);
4236*5ffd83dbSDimitry Andric 
4237*5ffd83dbSDimitry Andric     MI.eraseFromParent();
4238*5ffd83dbSDimitry Andric     return Legalized;
4239*5ffd83dbSDimitry Andric   }
4240*5ffd83dbSDimitry Andric 
4241*5ffd83dbSDimitry Andric   return UnableToLegalize;
4242*5ffd83dbSDimitry Andric }
4243*5ffd83dbSDimitry Andric 
4244*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4245*5ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTPOP(MachineInstr &MI, unsigned TypeIdx,
4246*5ffd83dbSDimitry Andric                                    LLT NarrowTy) {
4247*5ffd83dbSDimitry Andric   if (TypeIdx != 1)
4248*5ffd83dbSDimitry Andric     return UnableToLegalize;
4249*5ffd83dbSDimitry Andric 
4250*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4251*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
4252*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
4253*5ffd83dbSDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
4254*5ffd83dbSDimitry Andric 
4255*5ffd83dbSDimitry Andric   if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) {
4256*5ffd83dbSDimitry Andric     auto UnmergeSrc = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1));
4257*5ffd83dbSDimitry Andric 
4258*5ffd83dbSDimitry Andric     auto LoCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(0));
4259*5ffd83dbSDimitry Andric     auto HiCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(1));
4260*5ffd83dbSDimitry Andric     MIRBuilder.buildAdd(DstReg, HiCTPOP, LoCTPOP);
4261*5ffd83dbSDimitry Andric 
4262*5ffd83dbSDimitry Andric     MI.eraseFromParent();
4263*5ffd83dbSDimitry Andric     return Legalized;
4264*5ffd83dbSDimitry Andric   }
4265*5ffd83dbSDimitry Andric 
4266*5ffd83dbSDimitry Andric   return UnableToLegalize;
4267*5ffd83dbSDimitry Andric }
4268*5ffd83dbSDimitry Andric 
4269*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
42700b57cec5SDimitry Andric LegalizerHelper::lowerBitCount(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
42710b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
42720b57cec5SDimitry Andric   auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
42730b57cec5SDimitry Andric   auto isSupported = [this](const LegalityQuery &Q) {
42740b57cec5SDimitry Andric     auto QAction = LI.getAction(Q).Action;
42750b57cec5SDimitry Andric     return QAction == Legal || QAction == Libcall || QAction == Custom;
42760b57cec5SDimitry Andric   };
42770b57cec5SDimitry Andric   switch (Opc) {
42780b57cec5SDimitry Andric   default:
42790b57cec5SDimitry Andric     return UnableToLegalize;
42800b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF: {
42810b57cec5SDimitry Andric     // This trivially expands to CTLZ.
42820b57cec5SDimitry Andric     Observer.changingInstr(MI);
42830b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTLZ));
42840b57cec5SDimitry Andric     Observer.changedInstr(MI);
42850b57cec5SDimitry Andric     return Legalized;
42860b57cec5SDimitry Andric   }
42870b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ: {
4288*5ffd83dbSDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
42890b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
4290*5ffd83dbSDimitry Andric     LLT DstTy = MRI.getType(DstReg);
4291*5ffd83dbSDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
4292*5ffd83dbSDimitry Andric     unsigned Len = SrcTy.getSizeInBits();
4293*5ffd83dbSDimitry Andric 
4294*5ffd83dbSDimitry Andric     if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {DstTy, SrcTy}})) {
42950b57cec5SDimitry Andric       // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero.
4296*5ffd83dbSDimitry Andric       auto CtlzZU = MIRBuilder.buildCTLZ_ZERO_UNDEF(DstTy, SrcReg);
4297*5ffd83dbSDimitry Andric       auto ZeroSrc = MIRBuilder.buildConstant(SrcTy, 0);
4298*5ffd83dbSDimitry Andric       auto ICmp = MIRBuilder.buildICmp(
4299*5ffd83dbSDimitry Andric           CmpInst::ICMP_EQ, SrcTy.changeElementSize(1), SrcReg, ZeroSrc);
4300*5ffd83dbSDimitry Andric       auto LenConst = MIRBuilder.buildConstant(DstTy, Len);
4301*5ffd83dbSDimitry Andric       MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CtlzZU);
43020b57cec5SDimitry Andric       MI.eraseFromParent();
43030b57cec5SDimitry Andric       return Legalized;
43040b57cec5SDimitry Andric     }
43050b57cec5SDimitry Andric     // for now, we do this:
43060b57cec5SDimitry Andric     // NewLen = NextPowerOf2(Len);
43070b57cec5SDimitry Andric     // x = x | (x >> 1);
43080b57cec5SDimitry Andric     // x = x | (x >> 2);
43090b57cec5SDimitry Andric     // ...
43100b57cec5SDimitry Andric     // x = x | (x >>16);
43110b57cec5SDimitry Andric     // x = x | (x >>32); // for 64-bit input
43120b57cec5SDimitry Andric     // Upto NewLen/2
43130b57cec5SDimitry Andric     // return Len - popcount(x);
43140b57cec5SDimitry Andric     //
43150b57cec5SDimitry Andric     // Ref: "Hacker's Delight" by Henry Warren
43160b57cec5SDimitry Andric     Register Op = SrcReg;
43170b57cec5SDimitry Andric     unsigned NewLen = PowerOf2Ceil(Len);
43180b57cec5SDimitry Andric     for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) {
4319*5ffd83dbSDimitry Andric       auto MIBShiftAmt = MIRBuilder.buildConstant(SrcTy, 1ULL << i);
4320*5ffd83dbSDimitry Andric       auto MIBOp = MIRBuilder.buildOr(
4321*5ffd83dbSDimitry Andric           SrcTy, Op, MIRBuilder.buildLShr(SrcTy, Op, MIBShiftAmt));
4322*5ffd83dbSDimitry Andric       Op = MIBOp.getReg(0);
43230b57cec5SDimitry Andric     }
4324*5ffd83dbSDimitry Andric     auto MIBPop = MIRBuilder.buildCTPOP(DstTy, Op);
4325*5ffd83dbSDimitry Andric     MIRBuilder.buildSub(MI.getOperand(0), MIRBuilder.buildConstant(DstTy, Len),
4326*5ffd83dbSDimitry Andric                         MIBPop);
43270b57cec5SDimitry Andric     MI.eraseFromParent();
43280b57cec5SDimitry Andric     return Legalized;
43290b57cec5SDimitry Andric   }
43300b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF: {
43310b57cec5SDimitry Andric     // This trivially expands to CTTZ.
43320b57cec5SDimitry Andric     Observer.changingInstr(MI);
43330b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTTZ));
43340b57cec5SDimitry Andric     Observer.changedInstr(MI);
43350b57cec5SDimitry Andric     return Legalized;
43360b57cec5SDimitry Andric   }
43370b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ: {
4338*5ffd83dbSDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
43390b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
4340*5ffd83dbSDimitry Andric     LLT DstTy = MRI.getType(DstReg);
4341*5ffd83dbSDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
4342*5ffd83dbSDimitry Andric 
4343*5ffd83dbSDimitry Andric     unsigned Len = SrcTy.getSizeInBits();
4344*5ffd83dbSDimitry Andric     if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {DstTy, SrcTy}})) {
43450b57cec5SDimitry Andric       // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with
43460b57cec5SDimitry Andric       // zero.
4347*5ffd83dbSDimitry Andric       auto CttzZU = MIRBuilder.buildCTTZ_ZERO_UNDEF(DstTy, SrcReg);
4348*5ffd83dbSDimitry Andric       auto Zero = MIRBuilder.buildConstant(SrcTy, 0);
4349*5ffd83dbSDimitry Andric       auto ICmp = MIRBuilder.buildICmp(
4350*5ffd83dbSDimitry Andric           CmpInst::ICMP_EQ, DstTy.changeElementSize(1), SrcReg, Zero);
4351*5ffd83dbSDimitry Andric       auto LenConst = MIRBuilder.buildConstant(DstTy, Len);
4352*5ffd83dbSDimitry Andric       MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CttzZU);
43530b57cec5SDimitry Andric       MI.eraseFromParent();
43540b57cec5SDimitry Andric       return Legalized;
43550b57cec5SDimitry Andric     }
43560b57cec5SDimitry Andric     // for now, we use: { return popcount(~x & (x - 1)); }
43570b57cec5SDimitry Andric     // unless the target has ctlz but not ctpop, in which case we use:
43580b57cec5SDimitry Andric     // { return 32 - nlz(~x & (x-1)); }
43590b57cec5SDimitry Andric     // Ref: "Hacker's Delight" by Henry Warren
43600b57cec5SDimitry Andric     auto MIBCstNeg1 = MIRBuilder.buildConstant(Ty, -1);
4361*5ffd83dbSDimitry Andric     auto MIBNot = MIRBuilder.buildXor(Ty, SrcReg, MIBCstNeg1);
4362*5ffd83dbSDimitry Andric     auto MIBTmp = MIRBuilder.buildAnd(
4363*5ffd83dbSDimitry Andric         Ty, MIBNot, MIRBuilder.buildAdd(Ty, SrcReg, MIBCstNeg1));
43640b57cec5SDimitry Andric     if (!isSupported({TargetOpcode::G_CTPOP, {Ty, Ty}}) &&
43650b57cec5SDimitry Andric         isSupported({TargetOpcode::G_CTLZ, {Ty, Ty}})) {
43660b57cec5SDimitry Andric       auto MIBCstLen = MIRBuilder.buildConstant(Ty, Len);
4367*5ffd83dbSDimitry Andric       MIRBuilder.buildSub(MI.getOperand(0), MIBCstLen,
4368*5ffd83dbSDimitry Andric                           MIRBuilder.buildCTLZ(Ty, MIBTmp));
43690b57cec5SDimitry Andric       MI.eraseFromParent();
43700b57cec5SDimitry Andric       return Legalized;
43710b57cec5SDimitry Andric     }
43720b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTPOP));
4373*5ffd83dbSDimitry Andric     MI.getOperand(1).setReg(MIBTmp.getReg(0));
4374*5ffd83dbSDimitry Andric     return Legalized;
4375*5ffd83dbSDimitry Andric   }
4376*5ffd83dbSDimitry Andric   case TargetOpcode::G_CTPOP: {
4377*5ffd83dbSDimitry Andric     unsigned Size = Ty.getSizeInBits();
4378*5ffd83dbSDimitry Andric     MachineIRBuilder &B = MIRBuilder;
4379*5ffd83dbSDimitry Andric 
4380*5ffd83dbSDimitry Andric     // Count set bits in blocks of 2 bits. Default approach would be
4381*5ffd83dbSDimitry Andric     // B2Count = { val & 0x55555555 } + { (val >> 1) & 0x55555555 }
4382*5ffd83dbSDimitry Andric     // We use following formula instead:
4383*5ffd83dbSDimitry Andric     // B2Count = val - { (val >> 1) & 0x55555555 }
4384*5ffd83dbSDimitry Andric     // since it gives same result in blocks of 2 with one instruction less.
4385*5ffd83dbSDimitry Andric     auto C_1 = B.buildConstant(Ty, 1);
4386*5ffd83dbSDimitry Andric     auto B2Set1LoTo1Hi = B.buildLShr(Ty, MI.getOperand(1).getReg(), C_1);
4387*5ffd83dbSDimitry Andric     APInt B2Mask1HiTo0 = APInt::getSplat(Size, APInt(8, 0x55));
4388*5ffd83dbSDimitry Andric     auto C_B2Mask1HiTo0 = B.buildConstant(Ty, B2Mask1HiTo0);
4389*5ffd83dbSDimitry Andric     auto B2Count1Hi = B.buildAnd(Ty, B2Set1LoTo1Hi, C_B2Mask1HiTo0);
4390*5ffd83dbSDimitry Andric     auto B2Count = B.buildSub(Ty, MI.getOperand(1).getReg(), B2Count1Hi);
4391*5ffd83dbSDimitry Andric 
4392*5ffd83dbSDimitry Andric     // In order to get count in blocks of 4 add values from adjacent block of 2.
4393*5ffd83dbSDimitry Andric     // B4Count = { B2Count & 0x33333333 } + { (B2Count >> 2) & 0x33333333 }
4394*5ffd83dbSDimitry Andric     auto C_2 = B.buildConstant(Ty, 2);
4395*5ffd83dbSDimitry Andric     auto B4Set2LoTo2Hi = B.buildLShr(Ty, B2Count, C_2);
4396*5ffd83dbSDimitry Andric     APInt B4Mask2HiTo0 = APInt::getSplat(Size, APInt(8, 0x33));
4397*5ffd83dbSDimitry Andric     auto C_B4Mask2HiTo0 = B.buildConstant(Ty, B4Mask2HiTo0);
4398*5ffd83dbSDimitry Andric     auto B4HiB2Count = B.buildAnd(Ty, B4Set2LoTo2Hi, C_B4Mask2HiTo0);
4399*5ffd83dbSDimitry Andric     auto B4LoB2Count = B.buildAnd(Ty, B2Count, C_B4Mask2HiTo0);
4400*5ffd83dbSDimitry Andric     auto B4Count = B.buildAdd(Ty, B4HiB2Count, B4LoB2Count);
4401*5ffd83dbSDimitry Andric 
4402*5ffd83dbSDimitry Andric     // For count in blocks of 8 bits we don't have to mask high 4 bits before
4403*5ffd83dbSDimitry Andric     // addition since count value sits in range {0,...,8} and 4 bits are enough
4404*5ffd83dbSDimitry Andric     // to hold such binary values. After addition high 4 bits still hold count
4405*5ffd83dbSDimitry Andric     // of set bits in high 4 bit block, set them to zero and get 8 bit result.
4406*5ffd83dbSDimitry Andric     // B8Count = { B4Count + (B4Count >> 4) } & 0x0F0F0F0F
4407*5ffd83dbSDimitry Andric     auto C_4 = B.buildConstant(Ty, 4);
4408*5ffd83dbSDimitry Andric     auto B8HiB4Count = B.buildLShr(Ty, B4Count, C_4);
4409*5ffd83dbSDimitry Andric     auto B8CountDirty4Hi = B.buildAdd(Ty, B8HiB4Count, B4Count);
4410*5ffd83dbSDimitry Andric     APInt B8Mask4HiTo0 = APInt::getSplat(Size, APInt(8, 0x0F));
4411*5ffd83dbSDimitry Andric     auto C_B8Mask4HiTo0 = B.buildConstant(Ty, B8Mask4HiTo0);
4412*5ffd83dbSDimitry Andric     auto B8Count = B.buildAnd(Ty, B8CountDirty4Hi, C_B8Mask4HiTo0);
4413*5ffd83dbSDimitry Andric 
4414*5ffd83dbSDimitry Andric     assert(Size<=128 && "Scalar size is too large for CTPOP lower algorithm");
4415*5ffd83dbSDimitry Andric     // 8 bits can hold CTPOP result of 128 bit int or smaller. Mul with this
4416*5ffd83dbSDimitry Andric     // bitmask will set 8 msb in ResTmp to sum of all B8Counts in 8 bit blocks.
4417*5ffd83dbSDimitry Andric     auto MulMask = B.buildConstant(Ty, APInt::getSplat(Size, APInt(8, 0x01)));
4418*5ffd83dbSDimitry Andric     auto ResTmp = B.buildMul(Ty, B8Count, MulMask);
4419*5ffd83dbSDimitry Andric 
4420*5ffd83dbSDimitry Andric     // Shift count result from 8 high bits to low bits.
4421*5ffd83dbSDimitry Andric     auto C_SizeM8 = B.buildConstant(Ty, Size - 8);
4422*5ffd83dbSDimitry Andric     B.buildLShr(MI.getOperand(0).getReg(), ResTmp, C_SizeM8);
4423*5ffd83dbSDimitry Andric 
4424*5ffd83dbSDimitry Andric     MI.eraseFromParent();
44250b57cec5SDimitry Andric     return Legalized;
44260b57cec5SDimitry Andric   }
44270b57cec5SDimitry Andric   }
44280b57cec5SDimitry Andric }
44290b57cec5SDimitry Andric 
44300b57cec5SDimitry Andric // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float
44310b57cec5SDimitry Andric // representation.
44320b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
44330b57cec5SDimitry Andric LegalizerHelper::lowerU64ToF32BitOps(MachineInstr &MI) {
44340b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
44350b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
44360b57cec5SDimitry Andric   const LLT S64 = LLT::scalar(64);
44370b57cec5SDimitry Andric   const LLT S32 = LLT::scalar(32);
44380b57cec5SDimitry Andric   const LLT S1 = LLT::scalar(1);
44390b57cec5SDimitry Andric 
44400b57cec5SDimitry Andric   assert(MRI.getType(Src) == S64 && MRI.getType(Dst) == S32);
44410b57cec5SDimitry Andric 
44420b57cec5SDimitry Andric   // unsigned cul2f(ulong u) {
44430b57cec5SDimitry Andric   //   uint lz = clz(u);
44440b57cec5SDimitry Andric   //   uint e = (u != 0) ? 127U + 63U - lz : 0;
44450b57cec5SDimitry Andric   //   u = (u << lz) & 0x7fffffffffffffffUL;
44460b57cec5SDimitry Andric   //   ulong t = u & 0xffffffffffUL;
44470b57cec5SDimitry Andric   //   uint v = (e << 23) | (uint)(u >> 40);
44480b57cec5SDimitry Andric   //   uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
44490b57cec5SDimitry Andric   //   return as_float(v + r);
44500b57cec5SDimitry Andric   // }
44510b57cec5SDimitry Andric 
44520b57cec5SDimitry Andric   auto Zero32 = MIRBuilder.buildConstant(S32, 0);
44530b57cec5SDimitry Andric   auto Zero64 = MIRBuilder.buildConstant(S64, 0);
44540b57cec5SDimitry Andric 
44550b57cec5SDimitry Andric   auto LZ = MIRBuilder.buildCTLZ_ZERO_UNDEF(S32, Src);
44560b57cec5SDimitry Andric 
44570b57cec5SDimitry Andric   auto K = MIRBuilder.buildConstant(S32, 127U + 63U);
44580b57cec5SDimitry Andric   auto Sub = MIRBuilder.buildSub(S32, K, LZ);
44590b57cec5SDimitry Andric 
44600b57cec5SDimitry Andric   auto NotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, Src, Zero64);
44610b57cec5SDimitry Andric   auto E = MIRBuilder.buildSelect(S32, NotZero, Sub, Zero32);
44620b57cec5SDimitry Andric 
44630b57cec5SDimitry Andric   auto Mask0 = MIRBuilder.buildConstant(S64, (-1ULL) >> 1);
44640b57cec5SDimitry Andric   auto ShlLZ = MIRBuilder.buildShl(S64, Src, LZ);
44650b57cec5SDimitry Andric 
44660b57cec5SDimitry Andric   auto U = MIRBuilder.buildAnd(S64, ShlLZ, Mask0);
44670b57cec5SDimitry Andric 
44680b57cec5SDimitry Andric   auto Mask1 = MIRBuilder.buildConstant(S64, 0xffffffffffULL);
44690b57cec5SDimitry Andric   auto T = MIRBuilder.buildAnd(S64, U, Mask1);
44700b57cec5SDimitry Andric 
44710b57cec5SDimitry Andric   auto UShl = MIRBuilder.buildLShr(S64, U, MIRBuilder.buildConstant(S64, 40));
44720b57cec5SDimitry Andric   auto ShlE = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 23));
44730b57cec5SDimitry Andric   auto V = MIRBuilder.buildOr(S32, ShlE, MIRBuilder.buildTrunc(S32, UShl));
44740b57cec5SDimitry Andric 
44750b57cec5SDimitry Andric   auto C = MIRBuilder.buildConstant(S64, 0x8000000000ULL);
44760b57cec5SDimitry Andric   auto RCmp = MIRBuilder.buildICmp(CmpInst::ICMP_UGT, S1, T, C);
44770b57cec5SDimitry Andric   auto TCmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, T, C);
44780b57cec5SDimitry Andric   auto One = MIRBuilder.buildConstant(S32, 1);
44790b57cec5SDimitry Andric 
44800b57cec5SDimitry Andric   auto VTrunc1 = MIRBuilder.buildAnd(S32, V, One);
44810b57cec5SDimitry Andric   auto Select0 = MIRBuilder.buildSelect(S32, TCmp, VTrunc1, Zero32);
44820b57cec5SDimitry Andric   auto R = MIRBuilder.buildSelect(S32, RCmp, One, Select0);
44830b57cec5SDimitry Andric   MIRBuilder.buildAdd(Dst, V, R);
44840b57cec5SDimitry Andric 
4485*5ffd83dbSDimitry Andric   MI.eraseFromParent();
44860b57cec5SDimitry Andric   return Legalized;
44870b57cec5SDimitry Andric }
44880b57cec5SDimitry Andric 
44890b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
44900b57cec5SDimitry Andric LegalizerHelper::lowerUITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
44910b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
44920b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
44930b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst);
44940b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src);
44950b57cec5SDimitry Andric 
4496480093f4SDimitry Andric   if (SrcTy == LLT::scalar(1)) {
4497480093f4SDimitry Andric     auto True = MIRBuilder.buildFConstant(DstTy, 1.0);
4498480093f4SDimitry Andric     auto False = MIRBuilder.buildFConstant(DstTy, 0.0);
4499480093f4SDimitry Andric     MIRBuilder.buildSelect(Dst, Src, True, False);
4500480093f4SDimitry Andric     MI.eraseFromParent();
4501480093f4SDimitry Andric     return Legalized;
4502480093f4SDimitry Andric   }
4503480093f4SDimitry Andric 
45040b57cec5SDimitry Andric   if (SrcTy != LLT::scalar(64))
45050b57cec5SDimitry Andric     return UnableToLegalize;
45060b57cec5SDimitry Andric 
45070b57cec5SDimitry Andric   if (DstTy == LLT::scalar(32)) {
45080b57cec5SDimitry Andric     // TODO: SelectionDAG has several alternative expansions to port which may
45090b57cec5SDimitry Andric     // be more reasonble depending on the available instructions. If a target
45100b57cec5SDimitry Andric     // has sitofp, does not have CTLZ, or can efficiently use f64 as an
45110b57cec5SDimitry Andric     // intermediate type, this is probably worse.
45120b57cec5SDimitry Andric     return lowerU64ToF32BitOps(MI);
45130b57cec5SDimitry Andric   }
45140b57cec5SDimitry Andric 
45150b57cec5SDimitry Andric   return UnableToLegalize;
45160b57cec5SDimitry Andric }
45170b57cec5SDimitry Andric 
45180b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
45190b57cec5SDimitry Andric LegalizerHelper::lowerSITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
45200b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
45210b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
45220b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst);
45230b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src);
45240b57cec5SDimitry Andric 
45250b57cec5SDimitry Andric   const LLT S64 = LLT::scalar(64);
45260b57cec5SDimitry Andric   const LLT S32 = LLT::scalar(32);
45270b57cec5SDimitry Andric   const LLT S1 = LLT::scalar(1);
45280b57cec5SDimitry Andric 
4529480093f4SDimitry Andric   if (SrcTy == S1) {
4530480093f4SDimitry Andric     auto True = MIRBuilder.buildFConstant(DstTy, -1.0);
4531480093f4SDimitry Andric     auto False = MIRBuilder.buildFConstant(DstTy, 0.0);
4532480093f4SDimitry Andric     MIRBuilder.buildSelect(Dst, Src, True, False);
4533480093f4SDimitry Andric     MI.eraseFromParent();
4534480093f4SDimitry Andric     return Legalized;
4535480093f4SDimitry Andric   }
4536480093f4SDimitry Andric 
45370b57cec5SDimitry Andric   if (SrcTy != S64)
45380b57cec5SDimitry Andric     return UnableToLegalize;
45390b57cec5SDimitry Andric 
45400b57cec5SDimitry Andric   if (DstTy == S32) {
45410b57cec5SDimitry Andric     // signed cl2f(long l) {
45420b57cec5SDimitry Andric     //   long s = l >> 63;
45430b57cec5SDimitry Andric     //   float r = cul2f((l + s) ^ s);
45440b57cec5SDimitry Andric     //   return s ? -r : r;
45450b57cec5SDimitry Andric     // }
45460b57cec5SDimitry Andric     Register L = Src;
45470b57cec5SDimitry Andric     auto SignBit = MIRBuilder.buildConstant(S64, 63);
45480b57cec5SDimitry Andric     auto S = MIRBuilder.buildAShr(S64, L, SignBit);
45490b57cec5SDimitry Andric 
45500b57cec5SDimitry Andric     auto LPlusS = MIRBuilder.buildAdd(S64, L, S);
45510b57cec5SDimitry Andric     auto Xor = MIRBuilder.buildXor(S64, LPlusS, S);
45520b57cec5SDimitry Andric     auto R = MIRBuilder.buildUITOFP(S32, Xor);
45530b57cec5SDimitry Andric 
45540b57cec5SDimitry Andric     auto RNeg = MIRBuilder.buildFNeg(S32, R);
45550b57cec5SDimitry Andric     auto SignNotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, S,
45560b57cec5SDimitry Andric                                             MIRBuilder.buildConstant(S64, 0));
45570b57cec5SDimitry Andric     MIRBuilder.buildSelect(Dst, SignNotZero, RNeg, R);
4558*5ffd83dbSDimitry Andric     MI.eraseFromParent();
45590b57cec5SDimitry Andric     return Legalized;
45600b57cec5SDimitry Andric   }
45610b57cec5SDimitry Andric 
45620b57cec5SDimitry Andric   return UnableToLegalize;
45630b57cec5SDimitry Andric }
45640b57cec5SDimitry Andric 
45658bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
45668bcb0991SDimitry Andric LegalizerHelper::lowerFPTOUI(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
45678bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
45688bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
45698bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst);
45708bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(Src);
45718bcb0991SDimitry Andric   const LLT S64 = LLT::scalar(64);
45728bcb0991SDimitry Andric   const LLT S32 = LLT::scalar(32);
45738bcb0991SDimitry Andric 
45748bcb0991SDimitry Andric   if (SrcTy != S64 && SrcTy != S32)
45758bcb0991SDimitry Andric     return UnableToLegalize;
45768bcb0991SDimitry Andric   if (DstTy != S32 && DstTy != S64)
45778bcb0991SDimitry Andric     return UnableToLegalize;
45788bcb0991SDimitry Andric 
45798bcb0991SDimitry Andric   // FPTOSI gives same result as FPTOUI for positive signed integers.
45808bcb0991SDimitry Andric   // FPTOUI needs to deal with fp values that convert to unsigned integers
45818bcb0991SDimitry Andric   // greater or equal to 2^31 for float or 2^63 for double. For brevity 2^Exp.
45828bcb0991SDimitry Andric 
45838bcb0991SDimitry Andric   APInt TwoPExpInt = APInt::getSignMask(DstTy.getSizeInBits());
45848bcb0991SDimitry Andric   APFloat TwoPExpFP(SrcTy.getSizeInBits() == 32 ? APFloat::IEEEsingle()
45858bcb0991SDimitry Andric                                                 : APFloat::IEEEdouble(),
45868bcb0991SDimitry Andric                     APInt::getNullValue(SrcTy.getSizeInBits()));
45878bcb0991SDimitry Andric   TwoPExpFP.convertFromAPInt(TwoPExpInt, false, APFloat::rmNearestTiesToEven);
45888bcb0991SDimitry Andric 
45898bcb0991SDimitry Andric   MachineInstrBuilder FPTOSI = MIRBuilder.buildFPTOSI(DstTy, Src);
45908bcb0991SDimitry Andric 
45918bcb0991SDimitry Andric   MachineInstrBuilder Threshold = MIRBuilder.buildFConstant(SrcTy, TwoPExpFP);
45928bcb0991SDimitry Andric   // For fp Value greater or equal to Threshold(2^Exp), we use FPTOSI on
45938bcb0991SDimitry Andric   // (Value - 2^Exp) and add 2^Exp by setting highest bit in result to 1.
45948bcb0991SDimitry Andric   MachineInstrBuilder FSub = MIRBuilder.buildFSub(SrcTy, Src, Threshold);
45958bcb0991SDimitry Andric   MachineInstrBuilder ResLowBits = MIRBuilder.buildFPTOSI(DstTy, FSub);
45968bcb0991SDimitry Andric   MachineInstrBuilder ResHighBit = MIRBuilder.buildConstant(DstTy, TwoPExpInt);
45978bcb0991SDimitry Andric   MachineInstrBuilder Res = MIRBuilder.buildXor(DstTy, ResLowBits, ResHighBit);
45988bcb0991SDimitry Andric 
4599480093f4SDimitry Andric   const LLT S1 = LLT::scalar(1);
4600480093f4SDimitry Andric 
46018bcb0991SDimitry Andric   MachineInstrBuilder FCMP =
4602480093f4SDimitry Andric       MIRBuilder.buildFCmp(CmpInst::FCMP_ULT, S1, Src, Threshold);
46038bcb0991SDimitry Andric   MIRBuilder.buildSelect(Dst, FCMP, FPTOSI, Res);
46048bcb0991SDimitry Andric 
46058bcb0991SDimitry Andric   MI.eraseFromParent();
46068bcb0991SDimitry Andric   return Legalized;
46078bcb0991SDimitry Andric }
46088bcb0991SDimitry Andric 
4609*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOSI(MachineInstr &MI) {
4610*5ffd83dbSDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4611*5ffd83dbSDimitry Andric   Register Src = MI.getOperand(1).getReg();
4612*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(Dst);
4613*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(Src);
4614*5ffd83dbSDimitry Andric   const LLT S64 = LLT::scalar(64);
4615*5ffd83dbSDimitry Andric   const LLT S32 = LLT::scalar(32);
4616*5ffd83dbSDimitry Andric 
4617*5ffd83dbSDimitry Andric   // FIXME: Only f32 to i64 conversions are supported.
4618*5ffd83dbSDimitry Andric   if (SrcTy.getScalarType() != S32 || DstTy.getScalarType() != S64)
4619*5ffd83dbSDimitry Andric     return UnableToLegalize;
4620*5ffd83dbSDimitry Andric 
4621*5ffd83dbSDimitry Andric   // Expand f32 -> i64 conversion
4622*5ffd83dbSDimitry Andric   // This algorithm comes from compiler-rt's implementation of fixsfdi:
4623*5ffd83dbSDimitry Andric   // https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/builtins/fixsfdi.c
4624*5ffd83dbSDimitry Andric 
4625*5ffd83dbSDimitry Andric   unsigned SrcEltBits = SrcTy.getScalarSizeInBits();
4626*5ffd83dbSDimitry Andric 
4627*5ffd83dbSDimitry Andric   auto ExponentMask = MIRBuilder.buildConstant(SrcTy, 0x7F800000);
4628*5ffd83dbSDimitry Andric   auto ExponentLoBit = MIRBuilder.buildConstant(SrcTy, 23);
4629*5ffd83dbSDimitry Andric 
4630*5ffd83dbSDimitry Andric   auto AndExpMask = MIRBuilder.buildAnd(SrcTy, Src, ExponentMask);
4631*5ffd83dbSDimitry Andric   auto ExponentBits = MIRBuilder.buildLShr(SrcTy, AndExpMask, ExponentLoBit);
4632*5ffd83dbSDimitry Andric 
4633*5ffd83dbSDimitry Andric   auto SignMask = MIRBuilder.buildConstant(SrcTy,
4634*5ffd83dbSDimitry Andric                                            APInt::getSignMask(SrcEltBits));
4635*5ffd83dbSDimitry Andric   auto AndSignMask = MIRBuilder.buildAnd(SrcTy, Src, SignMask);
4636*5ffd83dbSDimitry Andric   auto SignLowBit = MIRBuilder.buildConstant(SrcTy, SrcEltBits - 1);
4637*5ffd83dbSDimitry Andric   auto Sign = MIRBuilder.buildAShr(SrcTy, AndSignMask, SignLowBit);
4638*5ffd83dbSDimitry Andric   Sign = MIRBuilder.buildSExt(DstTy, Sign);
4639*5ffd83dbSDimitry Andric 
4640*5ffd83dbSDimitry Andric   auto MantissaMask = MIRBuilder.buildConstant(SrcTy, 0x007FFFFF);
4641*5ffd83dbSDimitry Andric   auto AndMantissaMask = MIRBuilder.buildAnd(SrcTy, Src, MantissaMask);
4642*5ffd83dbSDimitry Andric   auto K = MIRBuilder.buildConstant(SrcTy, 0x00800000);
4643*5ffd83dbSDimitry Andric 
4644*5ffd83dbSDimitry Andric   auto R = MIRBuilder.buildOr(SrcTy, AndMantissaMask, K);
4645*5ffd83dbSDimitry Andric   R = MIRBuilder.buildZExt(DstTy, R);
4646*5ffd83dbSDimitry Andric 
4647*5ffd83dbSDimitry Andric   auto Bias = MIRBuilder.buildConstant(SrcTy, 127);
4648*5ffd83dbSDimitry Andric   auto Exponent = MIRBuilder.buildSub(SrcTy, ExponentBits, Bias);
4649*5ffd83dbSDimitry Andric   auto SubExponent = MIRBuilder.buildSub(SrcTy, Exponent, ExponentLoBit);
4650*5ffd83dbSDimitry Andric   auto ExponentSub = MIRBuilder.buildSub(SrcTy, ExponentLoBit, Exponent);
4651*5ffd83dbSDimitry Andric 
4652*5ffd83dbSDimitry Andric   auto Shl = MIRBuilder.buildShl(DstTy, R, SubExponent);
4653*5ffd83dbSDimitry Andric   auto Srl = MIRBuilder.buildLShr(DstTy, R, ExponentSub);
4654*5ffd83dbSDimitry Andric 
4655*5ffd83dbSDimitry Andric   const LLT S1 = LLT::scalar(1);
4656*5ffd83dbSDimitry Andric   auto CmpGt = MIRBuilder.buildICmp(CmpInst::ICMP_SGT,
4657*5ffd83dbSDimitry Andric                                     S1, Exponent, ExponentLoBit);
4658*5ffd83dbSDimitry Andric 
4659*5ffd83dbSDimitry Andric   R = MIRBuilder.buildSelect(DstTy, CmpGt, Shl, Srl);
4660*5ffd83dbSDimitry Andric 
4661*5ffd83dbSDimitry Andric   auto XorSign = MIRBuilder.buildXor(DstTy, R, Sign);
4662*5ffd83dbSDimitry Andric   auto Ret = MIRBuilder.buildSub(DstTy, XorSign, Sign);
4663*5ffd83dbSDimitry Andric 
4664*5ffd83dbSDimitry Andric   auto ZeroSrcTy = MIRBuilder.buildConstant(SrcTy, 0);
4665*5ffd83dbSDimitry Andric 
4666*5ffd83dbSDimitry Andric   auto ExponentLt0 = MIRBuilder.buildICmp(CmpInst::ICMP_SLT,
4667*5ffd83dbSDimitry Andric                                           S1, Exponent, ZeroSrcTy);
4668*5ffd83dbSDimitry Andric 
4669*5ffd83dbSDimitry Andric   auto ZeroDstTy = MIRBuilder.buildConstant(DstTy, 0);
4670*5ffd83dbSDimitry Andric   MIRBuilder.buildSelect(Dst, ExponentLt0, ZeroDstTy, Ret);
4671*5ffd83dbSDimitry Andric 
4672*5ffd83dbSDimitry Andric   MI.eraseFromParent();
4673*5ffd83dbSDimitry Andric   return Legalized;
4674*5ffd83dbSDimitry Andric }
4675*5ffd83dbSDimitry Andric 
4676*5ffd83dbSDimitry Andric // f64 -> f16 conversion using round-to-nearest-even rounding mode.
4677*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4678*5ffd83dbSDimitry Andric LegalizerHelper::lowerFPTRUNC_F64_TO_F16(MachineInstr &MI) {
4679*5ffd83dbSDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4680*5ffd83dbSDimitry Andric   Register Src = MI.getOperand(1).getReg();
4681*5ffd83dbSDimitry Andric 
4682*5ffd83dbSDimitry Andric   if (MRI.getType(Src).isVector()) // TODO: Handle vectors directly.
4683*5ffd83dbSDimitry Andric     return UnableToLegalize;
4684*5ffd83dbSDimitry Andric 
4685*5ffd83dbSDimitry Andric   const unsigned ExpMask = 0x7ff;
4686*5ffd83dbSDimitry Andric   const unsigned ExpBiasf64 = 1023;
4687*5ffd83dbSDimitry Andric   const unsigned ExpBiasf16 = 15;
4688*5ffd83dbSDimitry Andric   const LLT S32 = LLT::scalar(32);
4689*5ffd83dbSDimitry Andric   const LLT S1 = LLT::scalar(1);
4690*5ffd83dbSDimitry Andric 
4691*5ffd83dbSDimitry Andric   auto Unmerge = MIRBuilder.buildUnmerge(S32, Src);
4692*5ffd83dbSDimitry Andric   Register U = Unmerge.getReg(0);
4693*5ffd83dbSDimitry Andric   Register UH = Unmerge.getReg(1);
4694*5ffd83dbSDimitry Andric 
4695*5ffd83dbSDimitry Andric   auto E = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 20));
4696*5ffd83dbSDimitry Andric   E = MIRBuilder.buildAnd(S32, E, MIRBuilder.buildConstant(S32, ExpMask));
4697*5ffd83dbSDimitry Andric 
4698*5ffd83dbSDimitry Andric   // Subtract the fp64 exponent bias (1023) to get the real exponent and
4699*5ffd83dbSDimitry Andric   // add the f16 bias (15) to get the biased exponent for the f16 format.
4700*5ffd83dbSDimitry Andric   E = MIRBuilder.buildAdd(
4701*5ffd83dbSDimitry Andric     S32, E, MIRBuilder.buildConstant(S32, -ExpBiasf64 + ExpBiasf16));
4702*5ffd83dbSDimitry Andric 
4703*5ffd83dbSDimitry Andric   auto M = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 8));
4704*5ffd83dbSDimitry Andric   M = MIRBuilder.buildAnd(S32, M, MIRBuilder.buildConstant(S32, 0xffe));
4705*5ffd83dbSDimitry Andric 
4706*5ffd83dbSDimitry Andric   auto MaskedSig = MIRBuilder.buildAnd(S32, UH,
4707*5ffd83dbSDimitry Andric                                        MIRBuilder.buildConstant(S32, 0x1ff));
4708*5ffd83dbSDimitry Andric   MaskedSig = MIRBuilder.buildOr(S32, MaskedSig, U);
4709*5ffd83dbSDimitry Andric 
4710*5ffd83dbSDimitry Andric   auto Zero = MIRBuilder.buildConstant(S32, 0);
4711*5ffd83dbSDimitry Andric   auto SigCmpNE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, MaskedSig, Zero);
4712*5ffd83dbSDimitry Andric   auto Lo40Set = MIRBuilder.buildZExt(S32, SigCmpNE0);
4713*5ffd83dbSDimitry Andric   M = MIRBuilder.buildOr(S32, M, Lo40Set);
4714*5ffd83dbSDimitry Andric 
4715*5ffd83dbSDimitry Andric   // (M != 0 ? 0x0200 : 0) | 0x7c00;
4716*5ffd83dbSDimitry Andric   auto Bits0x200 = MIRBuilder.buildConstant(S32, 0x0200);
4717*5ffd83dbSDimitry Andric   auto CmpM_NE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, M, Zero);
4718*5ffd83dbSDimitry Andric   auto SelectCC = MIRBuilder.buildSelect(S32, CmpM_NE0, Bits0x200, Zero);
4719*5ffd83dbSDimitry Andric 
4720*5ffd83dbSDimitry Andric   auto Bits0x7c00 = MIRBuilder.buildConstant(S32, 0x7c00);
4721*5ffd83dbSDimitry Andric   auto I = MIRBuilder.buildOr(S32, SelectCC, Bits0x7c00);
4722*5ffd83dbSDimitry Andric 
4723*5ffd83dbSDimitry Andric   // N = M | (E << 12);
4724*5ffd83dbSDimitry Andric   auto EShl12 = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 12));
4725*5ffd83dbSDimitry Andric   auto N = MIRBuilder.buildOr(S32, M, EShl12);
4726*5ffd83dbSDimitry Andric 
4727*5ffd83dbSDimitry Andric   // B = clamp(1-E, 0, 13);
4728*5ffd83dbSDimitry Andric   auto One = MIRBuilder.buildConstant(S32, 1);
4729*5ffd83dbSDimitry Andric   auto OneSubExp = MIRBuilder.buildSub(S32, One, E);
4730*5ffd83dbSDimitry Andric   auto B = MIRBuilder.buildSMax(S32, OneSubExp, Zero);
4731*5ffd83dbSDimitry Andric   B = MIRBuilder.buildSMin(S32, B, MIRBuilder.buildConstant(S32, 13));
4732*5ffd83dbSDimitry Andric 
4733*5ffd83dbSDimitry Andric   auto SigSetHigh = MIRBuilder.buildOr(S32, M,
4734*5ffd83dbSDimitry Andric                                        MIRBuilder.buildConstant(S32, 0x1000));
4735*5ffd83dbSDimitry Andric 
4736*5ffd83dbSDimitry Andric   auto D = MIRBuilder.buildLShr(S32, SigSetHigh, B);
4737*5ffd83dbSDimitry Andric   auto D0 = MIRBuilder.buildShl(S32, D, B);
4738*5ffd83dbSDimitry Andric 
4739*5ffd83dbSDimitry Andric   auto D0_NE_SigSetHigh = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1,
4740*5ffd83dbSDimitry Andric                                              D0, SigSetHigh);
4741*5ffd83dbSDimitry Andric   auto D1 = MIRBuilder.buildZExt(S32, D0_NE_SigSetHigh);
4742*5ffd83dbSDimitry Andric   D = MIRBuilder.buildOr(S32, D, D1);
4743*5ffd83dbSDimitry Andric 
4744*5ffd83dbSDimitry Andric   auto CmpELtOne = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, S1, E, One);
4745*5ffd83dbSDimitry Andric   auto V = MIRBuilder.buildSelect(S32, CmpELtOne, D, N);
4746*5ffd83dbSDimitry Andric 
4747*5ffd83dbSDimitry Andric   auto VLow3 = MIRBuilder.buildAnd(S32, V, MIRBuilder.buildConstant(S32, 7));
4748*5ffd83dbSDimitry Andric   V = MIRBuilder.buildLShr(S32, V, MIRBuilder.buildConstant(S32, 2));
4749*5ffd83dbSDimitry Andric 
4750*5ffd83dbSDimitry Andric   auto VLow3Eq3 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, VLow3,
4751*5ffd83dbSDimitry Andric                                        MIRBuilder.buildConstant(S32, 3));
4752*5ffd83dbSDimitry Andric   auto V0 = MIRBuilder.buildZExt(S32, VLow3Eq3);
4753*5ffd83dbSDimitry Andric 
4754*5ffd83dbSDimitry Andric   auto VLow3Gt5 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, S1, VLow3,
4755*5ffd83dbSDimitry Andric                                        MIRBuilder.buildConstant(S32, 5));
4756*5ffd83dbSDimitry Andric   auto V1 = MIRBuilder.buildZExt(S32, VLow3Gt5);
4757*5ffd83dbSDimitry Andric 
4758*5ffd83dbSDimitry Andric   V1 = MIRBuilder.buildOr(S32, V0, V1);
4759*5ffd83dbSDimitry Andric   V = MIRBuilder.buildAdd(S32, V, V1);
4760*5ffd83dbSDimitry Andric 
4761*5ffd83dbSDimitry Andric   auto CmpEGt30 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT,  S1,
4762*5ffd83dbSDimitry Andric                                        E, MIRBuilder.buildConstant(S32, 30));
4763*5ffd83dbSDimitry Andric   V = MIRBuilder.buildSelect(S32, CmpEGt30,
4764*5ffd83dbSDimitry Andric                              MIRBuilder.buildConstant(S32, 0x7c00), V);
4765*5ffd83dbSDimitry Andric 
4766*5ffd83dbSDimitry Andric   auto CmpEGt1039 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1,
4767*5ffd83dbSDimitry Andric                                          E, MIRBuilder.buildConstant(S32, 1039));
4768*5ffd83dbSDimitry Andric   V = MIRBuilder.buildSelect(S32, CmpEGt1039, I, V);
4769*5ffd83dbSDimitry Andric 
4770*5ffd83dbSDimitry Andric   // Extract the sign bit.
4771*5ffd83dbSDimitry Andric   auto Sign = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 16));
4772*5ffd83dbSDimitry Andric   Sign = MIRBuilder.buildAnd(S32, Sign, MIRBuilder.buildConstant(S32, 0x8000));
4773*5ffd83dbSDimitry Andric 
4774*5ffd83dbSDimitry Andric   // Insert the sign bit
4775*5ffd83dbSDimitry Andric   V = MIRBuilder.buildOr(S32, Sign, V);
4776*5ffd83dbSDimitry Andric 
4777*5ffd83dbSDimitry Andric   MIRBuilder.buildTrunc(Dst, V);
4778*5ffd83dbSDimitry Andric   MI.eraseFromParent();
4779*5ffd83dbSDimitry Andric   return Legalized;
4780*5ffd83dbSDimitry Andric }
4781*5ffd83dbSDimitry Andric 
4782*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4783*5ffd83dbSDimitry Andric LegalizerHelper::lowerFPTRUNC(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
4784*5ffd83dbSDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4785*5ffd83dbSDimitry Andric   Register Src = MI.getOperand(1).getReg();
4786*5ffd83dbSDimitry Andric 
4787*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(Dst);
4788*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(Src);
4789*5ffd83dbSDimitry Andric   const LLT S64 = LLT::scalar(64);
4790*5ffd83dbSDimitry Andric   const LLT S16 = LLT::scalar(16);
4791*5ffd83dbSDimitry Andric 
4792*5ffd83dbSDimitry Andric   if (DstTy.getScalarType() == S16 && SrcTy.getScalarType() == S64)
4793*5ffd83dbSDimitry Andric     return lowerFPTRUNC_F64_TO_F16(MI);
4794*5ffd83dbSDimitry Andric 
4795*5ffd83dbSDimitry Andric   return UnableToLegalize;
4796*5ffd83dbSDimitry Andric }
4797*5ffd83dbSDimitry Andric 
47980b57cec5SDimitry Andric static CmpInst::Predicate minMaxToCompare(unsigned Opc) {
47990b57cec5SDimitry Andric   switch (Opc) {
48000b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
48010b57cec5SDimitry Andric     return CmpInst::ICMP_SLT;
48020b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
48030b57cec5SDimitry Andric     return CmpInst::ICMP_SGT;
48040b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
48050b57cec5SDimitry Andric     return CmpInst::ICMP_ULT;
48060b57cec5SDimitry Andric   case TargetOpcode::G_UMAX:
48070b57cec5SDimitry Andric     return CmpInst::ICMP_UGT;
48080b57cec5SDimitry Andric   default:
48090b57cec5SDimitry Andric     llvm_unreachable("not in integer min/max");
48100b57cec5SDimitry Andric   }
48110b57cec5SDimitry Andric }
48120b57cec5SDimitry Andric 
48130b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
48140b57cec5SDimitry Andric LegalizerHelper::lowerMinMax(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
48150b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
48160b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
48170b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
48180b57cec5SDimitry Andric 
48190b57cec5SDimitry Andric   const CmpInst::Predicate Pred = minMaxToCompare(MI.getOpcode());
48200b57cec5SDimitry Andric   LLT CmpType = MRI.getType(Dst).changeElementSize(1);
48210b57cec5SDimitry Andric 
48220b57cec5SDimitry Andric   auto Cmp = MIRBuilder.buildICmp(Pred, CmpType, Src0, Src1);
48230b57cec5SDimitry Andric   MIRBuilder.buildSelect(Dst, Cmp, Src0, Src1);
48240b57cec5SDimitry Andric 
48250b57cec5SDimitry Andric   MI.eraseFromParent();
48260b57cec5SDimitry Andric   return Legalized;
48270b57cec5SDimitry Andric }
48280b57cec5SDimitry Andric 
48290b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
48300b57cec5SDimitry Andric LegalizerHelper::lowerFCopySign(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
48310b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
48320b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
48330b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
48340b57cec5SDimitry Andric 
48350b57cec5SDimitry Andric   const LLT Src0Ty = MRI.getType(Src0);
48360b57cec5SDimitry Andric   const LLT Src1Ty = MRI.getType(Src1);
48370b57cec5SDimitry Andric 
48380b57cec5SDimitry Andric   const int Src0Size = Src0Ty.getScalarSizeInBits();
48390b57cec5SDimitry Andric   const int Src1Size = Src1Ty.getScalarSizeInBits();
48400b57cec5SDimitry Andric 
48410b57cec5SDimitry Andric   auto SignBitMask = MIRBuilder.buildConstant(
48420b57cec5SDimitry Andric     Src0Ty, APInt::getSignMask(Src0Size));
48430b57cec5SDimitry Andric 
48440b57cec5SDimitry Andric   auto NotSignBitMask = MIRBuilder.buildConstant(
48450b57cec5SDimitry Andric     Src0Ty, APInt::getLowBitsSet(Src0Size, Src0Size - 1));
48460b57cec5SDimitry Andric 
48470b57cec5SDimitry Andric   auto And0 = MIRBuilder.buildAnd(Src0Ty, Src0, NotSignBitMask);
48480b57cec5SDimitry Andric   MachineInstr *Or;
48490b57cec5SDimitry Andric 
48500b57cec5SDimitry Andric   if (Src0Ty == Src1Ty) {
4851*5ffd83dbSDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src1Ty, Src1, SignBitMask);
48520b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
48530b57cec5SDimitry Andric   } else if (Src0Size > Src1Size) {
48540b57cec5SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Src0Ty, Src0Size - Src1Size);
48550b57cec5SDimitry Andric     auto Zext = MIRBuilder.buildZExt(Src0Ty, Src1);
48560b57cec5SDimitry Andric     auto Shift = MIRBuilder.buildShl(Src0Ty, Zext, ShiftAmt);
48570b57cec5SDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src0Ty, Shift, SignBitMask);
48580b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
48590b57cec5SDimitry Andric   } else {
48600b57cec5SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Src1Ty, Src1Size - Src0Size);
48610b57cec5SDimitry Andric     auto Shift = MIRBuilder.buildLShr(Src1Ty, Src1, ShiftAmt);
48620b57cec5SDimitry Andric     auto Trunc = MIRBuilder.buildTrunc(Src0Ty, Shift);
48630b57cec5SDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src0Ty, Trunc, SignBitMask);
48640b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
48650b57cec5SDimitry Andric   }
48660b57cec5SDimitry Andric 
48670b57cec5SDimitry Andric   // Be careful about setting nsz/nnan/ninf on every instruction, since the
48680b57cec5SDimitry Andric   // constants are a nan and -0.0, but the final result should preserve
48690b57cec5SDimitry Andric   // everything.
48700b57cec5SDimitry Andric   if (unsigned Flags = MI.getFlags())
48710b57cec5SDimitry Andric     Or->setFlags(Flags);
48720b57cec5SDimitry Andric 
48730b57cec5SDimitry Andric   MI.eraseFromParent();
48740b57cec5SDimitry Andric   return Legalized;
48750b57cec5SDimitry Andric }
48760b57cec5SDimitry Andric 
48770b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
48780b57cec5SDimitry Andric LegalizerHelper::lowerFMinNumMaxNum(MachineInstr &MI) {
48790b57cec5SDimitry Andric   unsigned NewOp = MI.getOpcode() == TargetOpcode::G_FMINNUM ?
48800b57cec5SDimitry Andric     TargetOpcode::G_FMINNUM_IEEE : TargetOpcode::G_FMAXNUM_IEEE;
48810b57cec5SDimitry Andric 
48820b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
48830b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
48840b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
48850b57cec5SDimitry Andric   LLT Ty = MRI.getType(Dst);
48860b57cec5SDimitry Andric 
48870b57cec5SDimitry Andric   if (!MI.getFlag(MachineInstr::FmNoNans)) {
48880b57cec5SDimitry Andric     // Insert canonicalizes if it's possible we need to quiet to get correct
48890b57cec5SDimitry Andric     // sNaN behavior.
48900b57cec5SDimitry Andric 
48910b57cec5SDimitry Andric     // Note this must be done here, and not as an optimization combine in the
48920b57cec5SDimitry Andric     // absence of a dedicate quiet-snan instruction as we're using an
48930b57cec5SDimitry Andric     // omni-purpose G_FCANONICALIZE.
48940b57cec5SDimitry Andric     if (!isKnownNeverSNaN(Src0, MRI))
48950b57cec5SDimitry Andric       Src0 = MIRBuilder.buildFCanonicalize(Ty, Src0, MI.getFlags()).getReg(0);
48960b57cec5SDimitry Andric 
48970b57cec5SDimitry Andric     if (!isKnownNeverSNaN(Src1, MRI))
48980b57cec5SDimitry Andric       Src1 = MIRBuilder.buildFCanonicalize(Ty, Src1, MI.getFlags()).getReg(0);
48990b57cec5SDimitry Andric   }
49000b57cec5SDimitry Andric 
49010b57cec5SDimitry Andric   // If there are no nans, it's safe to simply replace this with the non-IEEE
49020b57cec5SDimitry Andric   // version.
49030b57cec5SDimitry Andric   MIRBuilder.buildInstr(NewOp, {Dst}, {Src0, Src1}, MI.getFlags());
49040b57cec5SDimitry Andric   MI.eraseFromParent();
49050b57cec5SDimitry Andric   return Legalized;
49060b57cec5SDimitry Andric }
49078bcb0991SDimitry Andric 
49088bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFMad(MachineInstr &MI) {
49098bcb0991SDimitry Andric   // Expand G_FMAD a, b, c -> G_FADD (G_FMUL a, b), c
49108bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
49118bcb0991SDimitry Andric   LLT Ty = MRI.getType(DstReg);
49128bcb0991SDimitry Andric   unsigned Flags = MI.getFlags();
49138bcb0991SDimitry Andric 
49148bcb0991SDimitry Andric   auto Mul = MIRBuilder.buildFMul(Ty, MI.getOperand(1), MI.getOperand(2),
49158bcb0991SDimitry Andric                                   Flags);
49168bcb0991SDimitry Andric   MIRBuilder.buildFAdd(DstReg, Mul, MI.getOperand(3), Flags);
49178bcb0991SDimitry Andric   MI.eraseFromParent();
49188bcb0991SDimitry Andric   return Legalized;
49198bcb0991SDimitry Andric }
49208bcb0991SDimitry Andric 
49218bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
4922480093f4SDimitry Andric LegalizerHelper::lowerIntrinsicRound(MachineInstr &MI) {
4923480093f4SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4924*5ffd83dbSDimitry Andric   Register X = MI.getOperand(1).getReg();
4925*5ffd83dbSDimitry Andric   const unsigned Flags = MI.getFlags();
4926*5ffd83dbSDimitry Andric   const LLT Ty = MRI.getType(DstReg);
4927*5ffd83dbSDimitry Andric   const LLT CondTy = Ty.changeElementSize(1);
4928*5ffd83dbSDimitry Andric 
4929*5ffd83dbSDimitry Andric   // round(x) =>
4930*5ffd83dbSDimitry Andric   //  t = trunc(x);
4931*5ffd83dbSDimitry Andric   //  d = fabs(x - t);
4932*5ffd83dbSDimitry Andric   //  o = copysign(1.0f, x);
4933*5ffd83dbSDimitry Andric   //  return t + (d >= 0.5 ? o : 0.0);
4934*5ffd83dbSDimitry Andric 
4935*5ffd83dbSDimitry Andric   auto T = MIRBuilder.buildIntrinsicTrunc(Ty, X, Flags);
4936*5ffd83dbSDimitry Andric 
4937*5ffd83dbSDimitry Andric   auto Diff = MIRBuilder.buildFSub(Ty, X, T, Flags);
4938*5ffd83dbSDimitry Andric   auto AbsDiff = MIRBuilder.buildFAbs(Ty, Diff, Flags);
4939*5ffd83dbSDimitry Andric   auto Zero = MIRBuilder.buildFConstant(Ty, 0.0);
4940*5ffd83dbSDimitry Andric   auto One = MIRBuilder.buildFConstant(Ty, 1.0);
4941*5ffd83dbSDimitry Andric   auto Half = MIRBuilder.buildFConstant(Ty, 0.5);
4942*5ffd83dbSDimitry Andric   auto SignOne = MIRBuilder.buildFCopysign(Ty, One, X);
4943*5ffd83dbSDimitry Andric 
4944*5ffd83dbSDimitry Andric   auto Cmp = MIRBuilder.buildFCmp(CmpInst::FCMP_OGE, CondTy, AbsDiff, Half,
4945*5ffd83dbSDimitry Andric                                   Flags);
4946*5ffd83dbSDimitry Andric   auto Sel = MIRBuilder.buildSelect(Ty, Cmp, SignOne, Zero, Flags);
4947*5ffd83dbSDimitry Andric 
4948*5ffd83dbSDimitry Andric   MIRBuilder.buildFAdd(DstReg, T, Sel, Flags);
4949*5ffd83dbSDimitry Andric 
4950*5ffd83dbSDimitry Andric   MI.eraseFromParent();
4951*5ffd83dbSDimitry Andric   return Legalized;
4952*5ffd83dbSDimitry Andric }
4953*5ffd83dbSDimitry Andric 
4954*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4955*5ffd83dbSDimitry Andric LegalizerHelper::lowerFFloor(MachineInstr &MI) {
4956*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4957480093f4SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
4958480093f4SDimitry Andric   unsigned Flags = MI.getFlags();
4959480093f4SDimitry Andric   LLT Ty = MRI.getType(DstReg);
4960480093f4SDimitry Andric   const LLT CondTy = Ty.changeElementSize(1);
4961480093f4SDimitry Andric 
4962480093f4SDimitry Andric   // result = trunc(src);
4963480093f4SDimitry Andric   // if (src < 0.0 && src != result)
4964480093f4SDimitry Andric   //   result += -1.0.
4965480093f4SDimitry Andric 
4966480093f4SDimitry Andric   auto Trunc = MIRBuilder.buildIntrinsicTrunc(Ty, SrcReg, Flags);
4967*5ffd83dbSDimitry Andric   auto Zero = MIRBuilder.buildFConstant(Ty, 0.0);
4968480093f4SDimitry Andric 
4969480093f4SDimitry Andric   auto Lt0 = MIRBuilder.buildFCmp(CmpInst::FCMP_OLT, CondTy,
4970480093f4SDimitry Andric                                   SrcReg, Zero, Flags);
4971480093f4SDimitry Andric   auto NeTrunc = MIRBuilder.buildFCmp(CmpInst::FCMP_ONE, CondTy,
4972480093f4SDimitry Andric                                       SrcReg, Trunc, Flags);
4973480093f4SDimitry Andric   auto And = MIRBuilder.buildAnd(CondTy, Lt0, NeTrunc);
4974480093f4SDimitry Andric   auto AddVal = MIRBuilder.buildSITOFP(Ty, And);
4975480093f4SDimitry Andric 
4976*5ffd83dbSDimitry Andric   MIRBuilder.buildFAdd(DstReg, Trunc, AddVal, Flags);
4977*5ffd83dbSDimitry Andric   MI.eraseFromParent();
4978*5ffd83dbSDimitry Andric   return Legalized;
4979*5ffd83dbSDimitry Andric }
4980*5ffd83dbSDimitry Andric 
4981*5ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult
4982*5ffd83dbSDimitry Andric LegalizerHelper::lowerMergeValues(MachineInstr &MI) {
4983*5ffd83dbSDimitry Andric   const unsigned NumOps = MI.getNumOperands();
4984*5ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4985*5ffd83dbSDimitry Andric   Register Src0Reg = MI.getOperand(1).getReg();
4986*5ffd83dbSDimitry Andric   LLT DstTy = MRI.getType(DstReg);
4987*5ffd83dbSDimitry Andric   LLT SrcTy = MRI.getType(Src0Reg);
4988*5ffd83dbSDimitry Andric   unsigned PartSize = SrcTy.getSizeInBits();
4989*5ffd83dbSDimitry Andric 
4990*5ffd83dbSDimitry Andric   LLT WideTy = LLT::scalar(DstTy.getSizeInBits());
4991*5ffd83dbSDimitry Andric   Register ResultReg = MIRBuilder.buildZExt(WideTy, Src0Reg).getReg(0);
4992*5ffd83dbSDimitry Andric 
4993*5ffd83dbSDimitry Andric   for (unsigned I = 2; I != NumOps; ++I) {
4994*5ffd83dbSDimitry Andric     const unsigned Offset = (I - 1) * PartSize;
4995*5ffd83dbSDimitry Andric 
4996*5ffd83dbSDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
4997*5ffd83dbSDimitry Andric     auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg);
4998*5ffd83dbSDimitry Andric 
4999*5ffd83dbSDimitry Andric     Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg :
5000*5ffd83dbSDimitry Andric       MRI.createGenericVirtualRegister(WideTy);
5001*5ffd83dbSDimitry Andric 
5002*5ffd83dbSDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset);
5003*5ffd83dbSDimitry Andric     auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt);
5004*5ffd83dbSDimitry Andric     MIRBuilder.buildOr(NextResult, ResultReg, Shl);
5005*5ffd83dbSDimitry Andric     ResultReg = NextResult;
5006*5ffd83dbSDimitry Andric   }
5007*5ffd83dbSDimitry Andric 
5008*5ffd83dbSDimitry Andric   if (DstTy.isPointer()) {
5009*5ffd83dbSDimitry Andric     if (MIRBuilder.getDataLayout().isNonIntegralAddressSpace(
5010*5ffd83dbSDimitry Andric           DstTy.getAddressSpace())) {
5011*5ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "Not casting nonintegral address space\n");
5012*5ffd83dbSDimitry Andric       return UnableToLegalize;
5013*5ffd83dbSDimitry Andric     }
5014*5ffd83dbSDimitry Andric 
5015*5ffd83dbSDimitry Andric     MIRBuilder.buildIntToPtr(DstReg, ResultReg);
5016*5ffd83dbSDimitry Andric   }
5017*5ffd83dbSDimitry Andric 
5018480093f4SDimitry Andric   MI.eraseFromParent();
5019480093f4SDimitry Andric   return Legalized;
5020480093f4SDimitry Andric }
5021480093f4SDimitry Andric 
5022480093f4SDimitry Andric LegalizerHelper::LegalizeResult
50238bcb0991SDimitry Andric LegalizerHelper::lowerUnmergeValues(MachineInstr &MI) {
50248bcb0991SDimitry Andric   const unsigned NumDst = MI.getNumOperands() - 1;
5025*5ffd83dbSDimitry Andric   Register SrcReg = MI.getOperand(NumDst).getReg();
50268bcb0991SDimitry Andric   Register Dst0Reg = MI.getOperand(0).getReg();
50278bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst0Reg);
5028*5ffd83dbSDimitry Andric   if (DstTy.isPointer())
5029*5ffd83dbSDimitry Andric     return UnableToLegalize; // TODO
50308bcb0991SDimitry Andric 
5031*5ffd83dbSDimitry Andric   SrcReg = coerceToScalar(SrcReg);
5032*5ffd83dbSDimitry Andric   if (!SrcReg)
5033*5ffd83dbSDimitry Andric     return UnableToLegalize;
50348bcb0991SDimitry Andric 
50358bcb0991SDimitry Andric   // Expand scalarizing unmerge as bitcast to integer and shift.
5036*5ffd83dbSDimitry Andric   LLT IntTy = MRI.getType(SrcReg);
50378bcb0991SDimitry Andric 
5038*5ffd83dbSDimitry Andric   MIRBuilder.buildTrunc(Dst0Reg, SrcReg);
50398bcb0991SDimitry Andric 
50408bcb0991SDimitry Andric   const unsigned DstSize = DstTy.getSizeInBits();
50418bcb0991SDimitry Andric   unsigned Offset = DstSize;
50428bcb0991SDimitry Andric   for (unsigned I = 1; I != NumDst; ++I, Offset += DstSize) {
50438bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(IntTy, Offset);
5044*5ffd83dbSDimitry Andric     auto Shift = MIRBuilder.buildLShr(IntTy, SrcReg, ShiftAmt);
50458bcb0991SDimitry Andric     MIRBuilder.buildTrunc(MI.getOperand(I), Shift);
50468bcb0991SDimitry Andric   }
50478bcb0991SDimitry Andric 
50488bcb0991SDimitry Andric   MI.eraseFromParent();
50498bcb0991SDimitry Andric   return Legalized;
50508bcb0991SDimitry Andric }
50518bcb0991SDimitry Andric 
50528bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
50538bcb0991SDimitry Andric LegalizerHelper::lowerShuffleVector(MachineInstr &MI) {
50548bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
50558bcb0991SDimitry Andric   Register Src0Reg = MI.getOperand(1).getReg();
50568bcb0991SDimitry Andric   Register Src1Reg = MI.getOperand(2).getReg();
50578bcb0991SDimitry Andric   LLT Src0Ty = MRI.getType(Src0Reg);
50588bcb0991SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
50598bcb0991SDimitry Andric   LLT IdxTy = LLT::scalar(32);
50608bcb0991SDimitry Andric 
5061480093f4SDimitry Andric   ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
50628bcb0991SDimitry Andric 
50638bcb0991SDimitry Andric   if (DstTy.isScalar()) {
50648bcb0991SDimitry Andric     if (Src0Ty.isVector())
50658bcb0991SDimitry Andric       return UnableToLegalize;
50668bcb0991SDimitry Andric 
50678bcb0991SDimitry Andric     // This is just a SELECT.
50688bcb0991SDimitry Andric     assert(Mask.size() == 1 && "Expected a single mask element");
50698bcb0991SDimitry Andric     Register Val;
50708bcb0991SDimitry Andric     if (Mask[0] < 0 || Mask[0] > 1)
50718bcb0991SDimitry Andric       Val = MIRBuilder.buildUndef(DstTy).getReg(0);
50728bcb0991SDimitry Andric     else
50738bcb0991SDimitry Andric       Val = Mask[0] == 0 ? Src0Reg : Src1Reg;
50748bcb0991SDimitry Andric     MIRBuilder.buildCopy(DstReg, Val);
50758bcb0991SDimitry Andric     MI.eraseFromParent();
50768bcb0991SDimitry Andric     return Legalized;
50778bcb0991SDimitry Andric   }
50788bcb0991SDimitry Andric 
50798bcb0991SDimitry Andric   Register Undef;
50808bcb0991SDimitry Andric   SmallVector<Register, 32> BuildVec;
50818bcb0991SDimitry Andric   LLT EltTy = DstTy.getElementType();
50828bcb0991SDimitry Andric 
50838bcb0991SDimitry Andric   for (int Idx : Mask) {
50848bcb0991SDimitry Andric     if (Idx < 0) {
50858bcb0991SDimitry Andric       if (!Undef.isValid())
50868bcb0991SDimitry Andric         Undef = MIRBuilder.buildUndef(EltTy).getReg(0);
50878bcb0991SDimitry Andric       BuildVec.push_back(Undef);
50888bcb0991SDimitry Andric       continue;
50898bcb0991SDimitry Andric     }
50908bcb0991SDimitry Andric 
50918bcb0991SDimitry Andric     if (Src0Ty.isScalar()) {
50928bcb0991SDimitry Andric       BuildVec.push_back(Idx == 0 ? Src0Reg : Src1Reg);
50938bcb0991SDimitry Andric     } else {
50948bcb0991SDimitry Andric       int NumElts = Src0Ty.getNumElements();
50958bcb0991SDimitry Andric       Register SrcVec = Idx < NumElts ? Src0Reg : Src1Reg;
50968bcb0991SDimitry Andric       int ExtractIdx = Idx < NumElts ? Idx : Idx - NumElts;
50978bcb0991SDimitry Andric       auto IdxK = MIRBuilder.buildConstant(IdxTy, ExtractIdx);
50988bcb0991SDimitry Andric       auto Extract = MIRBuilder.buildExtractVectorElement(EltTy, SrcVec, IdxK);
50998bcb0991SDimitry Andric       BuildVec.push_back(Extract.getReg(0));
51008bcb0991SDimitry Andric     }
51018bcb0991SDimitry Andric   }
51028bcb0991SDimitry Andric 
51038bcb0991SDimitry Andric   MIRBuilder.buildBuildVector(DstReg, BuildVec);
51048bcb0991SDimitry Andric   MI.eraseFromParent();
51058bcb0991SDimitry Andric   return Legalized;
51068bcb0991SDimitry Andric }
51078bcb0991SDimitry Andric 
51088bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
51098bcb0991SDimitry Andric LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) {
5110*5ffd83dbSDimitry Andric   const auto &MF = *MI.getMF();
5111*5ffd83dbSDimitry Andric   const auto &TFI = *MF.getSubtarget().getFrameLowering();
5112*5ffd83dbSDimitry Andric   if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
5113*5ffd83dbSDimitry Andric     return UnableToLegalize;
5114*5ffd83dbSDimitry Andric 
51158bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
51168bcb0991SDimitry Andric   Register AllocSize = MI.getOperand(1).getReg();
5117*5ffd83dbSDimitry Andric   Align Alignment = assumeAligned(MI.getOperand(2).getImm());
51188bcb0991SDimitry Andric 
51198bcb0991SDimitry Andric   LLT PtrTy = MRI.getType(Dst);
51208bcb0991SDimitry Andric   LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
51218bcb0991SDimitry Andric 
5122*5ffd83dbSDimitry Andric   const auto &TLI = *MF.getSubtarget().getTargetLowering();
51238bcb0991SDimitry Andric   Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
51248bcb0991SDimitry Andric   auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg);
51258bcb0991SDimitry Andric   SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp);
51268bcb0991SDimitry Andric 
51278bcb0991SDimitry Andric   // Subtract the final alloc from the SP. We use G_PTRTOINT here so we don't
51288bcb0991SDimitry Andric   // have to generate an extra instruction to negate the alloc and then use
5129480093f4SDimitry Andric   // G_PTR_ADD to add the negative offset.
51308bcb0991SDimitry Andric   auto Alloc = MIRBuilder.buildSub(IntPtrTy, SPTmp, AllocSize);
5131*5ffd83dbSDimitry Andric   if (Alignment > Align(1)) {
5132*5ffd83dbSDimitry Andric     APInt AlignMask(IntPtrTy.getSizeInBits(), Alignment.value(), true);
51338bcb0991SDimitry Andric     AlignMask.negate();
51348bcb0991SDimitry Andric     auto AlignCst = MIRBuilder.buildConstant(IntPtrTy, AlignMask);
51358bcb0991SDimitry Andric     Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst);
51368bcb0991SDimitry Andric   }
51378bcb0991SDimitry Andric 
51388bcb0991SDimitry Andric   SPTmp = MIRBuilder.buildCast(PtrTy, Alloc);
51398bcb0991SDimitry Andric   MIRBuilder.buildCopy(SPReg, SPTmp);
51408bcb0991SDimitry Andric   MIRBuilder.buildCopy(Dst, SPTmp);
51418bcb0991SDimitry Andric 
51428bcb0991SDimitry Andric   MI.eraseFromParent();
51438bcb0991SDimitry Andric   return Legalized;
51448bcb0991SDimitry Andric }
51458bcb0991SDimitry Andric 
51468bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
51478bcb0991SDimitry Andric LegalizerHelper::lowerExtract(MachineInstr &MI) {
51488bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
51498bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
51508bcb0991SDimitry Andric   unsigned Offset = MI.getOperand(2).getImm();
51518bcb0991SDimitry Andric 
51528bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst);
51538bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(Src);
51548bcb0991SDimitry Andric 
51558bcb0991SDimitry Andric   if (DstTy.isScalar() &&
51568bcb0991SDimitry Andric       (SrcTy.isScalar() ||
51578bcb0991SDimitry Andric        (SrcTy.isVector() && DstTy == SrcTy.getElementType()))) {
51588bcb0991SDimitry Andric     LLT SrcIntTy = SrcTy;
51598bcb0991SDimitry Andric     if (!SrcTy.isScalar()) {
51608bcb0991SDimitry Andric       SrcIntTy = LLT::scalar(SrcTy.getSizeInBits());
51618bcb0991SDimitry Andric       Src = MIRBuilder.buildBitcast(SrcIntTy, Src).getReg(0);
51628bcb0991SDimitry Andric     }
51638bcb0991SDimitry Andric 
51648bcb0991SDimitry Andric     if (Offset == 0)
51658bcb0991SDimitry Andric       MIRBuilder.buildTrunc(Dst, Src);
51668bcb0991SDimitry Andric     else {
51678bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(SrcIntTy, Offset);
51688bcb0991SDimitry Andric       auto Shr = MIRBuilder.buildLShr(SrcIntTy, Src, ShiftAmt);
51698bcb0991SDimitry Andric       MIRBuilder.buildTrunc(Dst, Shr);
51708bcb0991SDimitry Andric     }
51718bcb0991SDimitry Andric 
51728bcb0991SDimitry Andric     MI.eraseFromParent();
51738bcb0991SDimitry Andric     return Legalized;
51748bcb0991SDimitry Andric   }
51758bcb0991SDimitry Andric 
51768bcb0991SDimitry Andric   return UnableToLegalize;
51778bcb0991SDimitry Andric }
51788bcb0991SDimitry Andric 
51798bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerInsert(MachineInstr &MI) {
51808bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
51818bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
51828bcb0991SDimitry Andric   Register InsertSrc = MI.getOperand(2).getReg();
51838bcb0991SDimitry Andric   uint64_t Offset = MI.getOperand(3).getImm();
51848bcb0991SDimitry Andric 
51858bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Src);
51868bcb0991SDimitry Andric   LLT InsertTy = MRI.getType(InsertSrc);
51878bcb0991SDimitry Andric 
5188*5ffd83dbSDimitry Andric   if (InsertTy.isVector() ||
5189*5ffd83dbSDimitry Andric       (DstTy.isVector() && DstTy.getElementType() != InsertTy))
5190*5ffd83dbSDimitry Andric     return UnableToLegalize;
5191*5ffd83dbSDimitry Andric 
5192*5ffd83dbSDimitry Andric   const DataLayout &DL = MIRBuilder.getDataLayout();
5193*5ffd83dbSDimitry Andric   if ((DstTy.isPointer() &&
5194*5ffd83dbSDimitry Andric        DL.isNonIntegralAddressSpace(DstTy.getAddressSpace())) ||
5195*5ffd83dbSDimitry Andric       (InsertTy.isPointer() &&
5196*5ffd83dbSDimitry Andric        DL.isNonIntegralAddressSpace(InsertTy.getAddressSpace()))) {
5197*5ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "Not casting non-integral address space integer\n");
5198*5ffd83dbSDimitry Andric     return UnableToLegalize;
5199*5ffd83dbSDimitry Andric   }
5200*5ffd83dbSDimitry Andric 
52018bcb0991SDimitry Andric   LLT IntDstTy = DstTy;
5202*5ffd83dbSDimitry Andric 
52038bcb0991SDimitry Andric   if (!DstTy.isScalar()) {
52048bcb0991SDimitry Andric     IntDstTy = LLT::scalar(DstTy.getSizeInBits());
5205*5ffd83dbSDimitry Andric     Src = MIRBuilder.buildCast(IntDstTy, Src).getReg(0);
5206*5ffd83dbSDimitry Andric   }
5207*5ffd83dbSDimitry Andric 
5208*5ffd83dbSDimitry Andric   if (!InsertTy.isScalar()) {
5209*5ffd83dbSDimitry Andric     const LLT IntInsertTy = LLT::scalar(InsertTy.getSizeInBits());
5210*5ffd83dbSDimitry Andric     InsertSrc = MIRBuilder.buildPtrToInt(IntInsertTy, InsertSrc).getReg(0);
52118bcb0991SDimitry Andric   }
52128bcb0991SDimitry Andric 
52138bcb0991SDimitry Andric   Register ExtInsSrc = MIRBuilder.buildZExt(IntDstTy, InsertSrc).getReg(0);
52148bcb0991SDimitry Andric   if (Offset != 0) {
52158bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(IntDstTy, Offset);
52168bcb0991SDimitry Andric     ExtInsSrc = MIRBuilder.buildShl(IntDstTy, ExtInsSrc, ShiftAmt).getReg(0);
52178bcb0991SDimitry Andric   }
52188bcb0991SDimitry Andric 
5219*5ffd83dbSDimitry Andric   APInt MaskVal = APInt::getBitsSetWithWrap(
5220*5ffd83dbSDimitry Andric       DstTy.getSizeInBits(), Offset + InsertTy.getSizeInBits(), Offset);
52218bcb0991SDimitry Andric 
52228bcb0991SDimitry Andric   auto Mask = MIRBuilder.buildConstant(IntDstTy, MaskVal);
52238bcb0991SDimitry Andric   auto MaskedSrc = MIRBuilder.buildAnd(IntDstTy, Src, Mask);
52248bcb0991SDimitry Andric   auto Or = MIRBuilder.buildOr(IntDstTy, MaskedSrc, ExtInsSrc);
52258bcb0991SDimitry Andric 
5226*5ffd83dbSDimitry Andric   MIRBuilder.buildCast(Dst, Or);
52278bcb0991SDimitry Andric   MI.eraseFromParent();
52288bcb0991SDimitry Andric   return Legalized;
52298bcb0991SDimitry Andric }
52308bcb0991SDimitry Andric 
52318bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
52328bcb0991SDimitry Andric LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) {
52338bcb0991SDimitry Andric   Register Dst0 = MI.getOperand(0).getReg();
52348bcb0991SDimitry Andric   Register Dst1 = MI.getOperand(1).getReg();
52358bcb0991SDimitry Andric   Register LHS = MI.getOperand(2).getReg();
52368bcb0991SDimitry Andric   Register RHS = MI.getOperand(3).getReg();
52378bcb0991SDimitry Andric   const bool IsAdd = MI.getOpcode() == TargetOpcode::G_SADDO;
52388bcb0991SDimitry Andric 
52398bcb0991SDimitry Andric   LLT Ty = MRI.getType(Dst0);
52408bcb0991SDimitry Andric   LLT BoolTy = MRI.getType(Dst1);
52418bcb0991SDimitry Andric 
52428bcb0991SDimitry Andric   if (IsAdd)
52438bcb0991SDimitry Andric     MIRBuilder.buildAdd(Dst0, LHS, RHS);
52448bcb0991SDimitry Andric   else
52458bcb0991SDimitry Andric     MIRBuilder.buildSub(Dst0, LHS, RHS);
52468bcb0991SDimitry Andric 
52478bcb0991SDimitry Andric   // TODO: If SADDSAT/SSUBSAT is legal, compare results to detect overflow.
52488bcb0991SDimitry Andric 
52498bcb0991SDimitry Andric   auto Zero = MIRBuilder.buildConstant(Ty, 0);
52508bcb0991SDimitry Andric 
52518bcb0991SDimitry Andric   // For an addition, the result should be less than one of the operands (LHS)
52528bcb0991SDimitry Andric   // if and only if the other operand (RHS) is negative, otherwise there will
52538bcb0991SDimitry Andric   // be overflow.
52548bcb0991SDimitry Andric   // For a subtraction, the result should be less than one of the operands
52558bcb0991SDimitry Andric   // (LHS) if and only if the other operand (RHS) is (non-zero) positive,
52568bcb0991SDimitry Andric   // otherwise there will be overflow.
52578bcb0991SDimitry Andric   auto ResultLowerThanLHS =
52588bcb0991SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Dst0, LHS);
52598bcb0991SDimitry Andric   auto ConditionRHS = MIRBuilder.buildICmp(
52608bcb0991SDimitry Andric       IsAdd ? CmpInst::ICMP_SLT : CmpInst::ICMP_SGT, BoolTy, RHS, Zero);
52618bcb0991SDimitry Andric 
52628bcb0991SDimitry Andric   MIRBuilder.buildXor(Dst1, ConditionRHS, ResultLowerThanLHS);
52638bcb0991SDimitry Andric   MI.eraseFromParent();
52648bcb0991SDimitry Andric   return Legalized;
52658bcb0991SDimitry Andric }
5266480093f4SDimitry Andric 
5267480093f4SDimitry Andric LegalizerHelper::LegalizeResult
5268480093f4SDimitry Andric LegalizerHelper::lowerBswap(MachineInstr &MI) {
5269480093f4SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
5270480093f4SDimitry Andric   Register Src = MI.getOperand(1).getReg();
5271480093f4SDimitry Andric   const LLT Ty = MRI.getType(Src);
5272*5ffd83dbSDimitry Andric   unsigned SizeInBytes = (Ty.getScalarSizeInBits() + 7) / 8;
5273480093f4SDimitry Andric   unsigned BaseShiftAmt = (SizeInBytes - 1) * 8;
5274480093f4SDimitry Andric 
5275480093f4SDimitry Andric   // Swap most and least significant byte, set remaining bytes in Res to zero.
5276480093f4SDimitry Andric   auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt);
5277480093f4SDimitry Andric   auto LSByteShiftedLeft = MIRBuilder.buildShl(Ty, Src, ShiftAmt);
5278480093f4SDimitry Andric   auto MSByteShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt);
5279480093f4SDimitry Andric   auto Res = MIRBuilder.buildOr(Ty, MSByteShiftedRight, LSByteShiftedLeft);
5280480093f4SDimitry Andric 
5281480093f4SDimitry Andric   // Set i-th high/low byte in Res to i-th low/high byte from Src.
5282480093f4SDimitry Andric   for (unsigned i = 1; i < SizeInBytes / 2; ++i) {
5283480093f4SDimitry Andric     // AND with Mask leaves byte i unchanged and sets remaining bytes to 0.
5284480093f4SDimitry Andric     APInt APMask(SizeInBytes * 8, 0xFF << (i * 8));
5285480093f4SDimitry Andric     auto Mask = MIRBuilder.buildConstant(Ty, APMask);
5286480093f4SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt - 16 * i);
5287480093f4SDimitry Andric     // Low byte shifted left to place of high byte: (Src & Mask) << ShiftAmt.
5288480093f4SDimitry Andric     auto LoByte = MIRBuilder.buildAnd(Ty, Src, Mask);
5289480093f4SDimitry Andric     auto LoShiftedLeft = MIRBuilder.buildShl(Ty, LoByte, ShiftAmt);
5290480093f4SDimitry Andric     Res = MIRBuilder.buildOr(Ty, Res, LoShiftedLeft);
5291480093f4SDimitry Andric     // High byte shifted right to place of low byte: (Src >> ShiftAmt) & Mask.
5292480093f4SDimitry Andric     auto SrcShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt);
5293480093f4SDimitry Andric     auto HiShiftedRight = MIRBuilder.buildAnd(Ty, SrcShiftedRight, Mask);
5294480093f4SDimitry Andric     Res = MIRBuilder.buildOr(Ty, Res, HiShiftedRight);
5295480093f4SDimitry Andric   }
5296480093f4SDimitry Andric   Res.getInstr()->getOperand(0).setReg(Dst);
5297480093f4SDimitry Andric 
5298480093f4SDimitry Andric   MI.eraseFromParent();
5299480093f4SDimitry Andric   return Legalized;
5300480093f4SDimitry Andric }
5301480093f4SDimitry Andric 
5302480093f4SDimitry Andric //{ (Src & Mask) >> N } | { (Src << N) & Mask }
5303480093f4SDimitry Andric static MachineInstrBuilder SwapN(unsigned N, DstOp Dst, MachineIRBuilder &B,
5304480093f4SDimitry Andric                                  MachineInstrBuilder Src, APInt Mask) {
5305480093f4SDimitry Andric   const LLT Ty = Dst.getLLTTy(*B.getMRI());
5306480093f4SDimitry Andric   MachineInstrBuilder C_N = B.buildConstant(Ty, N);
5307480093f4SDimitry Andric   MachineInstrBuilder MaskLoNTo0 = B.buildConstant(Ty, Mask);
5308480093f4SDimitry Andric   auto LHS = B.buildLShr(Ty, B.buildAnd(Ty, Src, MaskLoNTo0), C_N);
5309480093f4SDimitry Andric   auto RHS = B.buildAnd(Ty, B.buildShl(Ty, Src, C_N), MaskLoNTo0);
5310480093f4SDimitry Andric   return B.buildOr(Dst, LHS, RHS);
5311480093f4SDimitry Andric }
5312480093f4SDimitry Andric 
5313480093f4SDimitry Andric LegalizerHelper::LegalizeResult
5314480093f4SDimitry Andric LegalizerHelper::lowerBitreverse(MachineInstr &MI) {
5315480093f4SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
5316480093f4SDimitry Andric   Register Src = MI.getOperand(1).getReg();
5317480093f4SDimitry Andric   const LLT Ty = MRI.getType(Src);
5318480093f4SDimitry Andric   unsigned Size = Ty.getSizeInBits();
5319480093f4SDimitry Andric 
5320480093f4SDimitry Andric   MachineInstrBuilder BSWAP =
5321480093f4SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_BSWAP, {Ty}, {Src});
5322480093f4SDimitry Andric 
5323480093f4SDimitry Andric   // swap high and low 4 bits in 8 bit blocks 7654|3210 -> 3210|7654
5324480093f4SDimitry Andric   //    [(val & 0xF0F0F0F0) >> 4] | [(val & 0x0F0F0F0F) << 4]
5325480093f4SDimitry Andric   // -> [(val & 0xF0F0F0F0) >> 4] | [(val << 4) & 0xF0F0F0F0]
5326480093f4SDimitry Andric   MachineInstrBuilder Swap4 =
5327480093f4SDimitry Andric       SwapN(4, Ty, MIRBuilder, BSWAP, APInt::getSplat(Size, APInt(8, 0xF0)));
5328480093f4SDimitry Andric 
5329480093f4SDimitry Andric   // swap high and low 2 bits in 4 bit blocks 32|10 76|54 -> 10|32 54|76
5330480093f4SDimitry Andric   //    [(val & 0xCCCCCCCC) >> 2] & [(val & 0x33333333) << 2]
5331480093f4SDimitry Andric   // -> [(val & 0xCCCCCCCC) >> 2] & [(val << 2) & 0xCCCCCCCC]
5332480093f4SDimitry Andric   MachineInstrBuilder Swap2 =
5333480093f4SDimitry Andric       SwapN(2, Ty, MIRBuilder, Swap4, APInt::getSplat(Size, APInt(8, 0xCC)));
5334480093f4SDimitry Andric 
5335480093f4SDimitry Andric   // swap high and low 1 bit in 2 bit blocks 1|0 3|2 5|4 7|6 -> 0|1 2|3 4|5 6|7
5336480093f4SDimitry Andric   //    [(val & 0xAAAAAAAA) >> 1] & [(val & 0x55555555) << 1]
5337480093f4SDimitry Andric   // -> [(val & 0xAAAAAAAA) >> 1] & [(val << 1) & 0xAAAAAAAA]
5338480093f4SDimitry Andric   SwapN(1, Dst, MIRBuilder, Swap2, APInt::getSplat(Size, APInt(8, 0xAA)));
5339480093f4SDimitry Andric 
5340480093f4SDimitry Andric   MI.eraseFromParent();
5341480093f4SDimitry Andric   return Legalized;
5342480093f4SDimitry Andric }
5343480093f4SDimitry Andric 
5344480093f4SDimitry Andric LegalizerHelper::LegalizeResult
5345*5ffd83dbSDimitry Andric LegalizerHelper::lowerReadWriteRegister(MachineInstr &MI) {
5346480093f4SDimitry Andric   MachineFunction &MF = MIRBuilder.getMF();
5347480093f4SDimitry Andric   const TargetSubtargetInfo &STI = MF.getSubtarget();
5348480093f4SDimitry Andric   const TargetLowering *TLI = STI.getTargetLowering();
5349*5ffd83dbSDimitry Andric 
5350*5ffd83dbSDimitry Andric   bool IsRead = MI.getOpcode() == TargetOpcode::G_READ_REGISTER;
5351*5ffd83dbSDimitry Andric   int NameOpIdx = IsRead ? 1 : 0;
5352*5ffd83dbSDimitry Andric   int ValRegIndex = IsRead ? 0 : 1;
5353*5ffd83dbSDimitry Andric 
5354*5ffd83dbSDimitry Andric   Register ValReg = MI.getOperand(ValRegIndex).getReg();
5355*5ffd83dbSDimitry Andric   const LLT Ty = MRI.getType(ValReg);
5356*5ffd83dbSDimitry Andric   const MDString *RegStr = cast<MDString>(
5357*5ffd83dbSDimitry Andric     cast<MDNode>(MI.getOperand(NameOpIdx).getMetadata())->getOperand(0));
5358*5ffd83dbSDimitry Andric 
5359*5ffd83dbSDimitry Andric   Register PhysReg = TLI->getRegisterByName(RegStr->getString().data(), Ty, MF);
5360*5ffd83dbSDimitry Andric   if (!PhysReg.isValid())
5361480093f4SDimitry Andric     return UnableToLegalize;
5362480093f4SDimitry Andric 
5363*5ffd83dbSDimitry Andric   if (IsRead)
5364*5ffd83dbSDimitry Andric     MIRBuilder.buildCopy(ValReg, PhysReg);
5365*5ffd83dbSDimitry Andric   else
5366*5ffd83dbSDimitry Andric     MIRBuilder.buildCopy(PhysReg, ValReg);
5367*5ffd83dbSDimitry Andric 
5368480093f4SDimitry Andric   MI.eraseFromParent();
5369480093f4SDimitry Andric   return Legalized;
5370480093f4SDimitry Andric }
5371