xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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 
660b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF,
670b57cec5SDimitry Andric                                  GISelChangeObserver &Observer,
680b57cec5SDimitry Andric                                  MachineIRBuilder &Builder)
690b57cec5SDimitry Andric     : MIRBuilder(Builder), MRI(MF.getRegInfo()),
700b57cec5SDimitry Andric       LI(*MF.getSubtarget().getLegalizerInfo()), Observer(Observer) {
710b57cec5SDimitry Andric   MIRBuilder.setMF(MF);
720b57cec5SDimitry Andric   MIRBuilder.setChangeObserver(Observer);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, const LegalizerInfo &LI,
760b57cec5SDimitry Andric                                  GISelChangeObserver &Observer,
770b57cec5SDimitry Andric                                  MachineIRBuilder &B)
780b57cec5SDimitry Andric     : MIRBuilder(B), MRI(MF.getRegInfo()), LI(LI), Observer(Observer) {
790b57cec5SDimitry Andric   MIRBuilder.setMF(MF);
800b57cec5SDimitry Andric   MIRBuilder.setChangeObserver(Observer);
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
830b57cec5SDimitry Andric LegalizerHelper::legalizeInstrStep(MachineInstr &MI) {
840b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing: "; MI.print(dbgs()));
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_INTRINSIC ||
870b57cec5SDimitry Andric       MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS)
880b57cec5SDimitry Andric     return LI.legalizeIntrinsic(MI, MRI, MIRBuilder) ? Legalized
890b57cec5SDimitry Andric                                                      : UnableToLegalize;
900b57cec5SDimitry Andric   auto Step = LI.getAction(MI, MRI);
910b57cec5SDimitry Andric   switch (Step.Action) {
920b57cec5SDimitry Andric   case Legal:
930b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Already legal\n");
940b57cec5SDimitry Andric     return AlreadyLegal;
950b57cec5SDimitry Andric   case Libcall:
960b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Convert to libcall\n");
970b57cec5SDimitry Andric     return libcall(MI);
980b57cec5SDimitry Andric   case NarrowScalar:
990b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Narrow scalar\n");
1000b57cec5SDimitry Andric     return narrowScalar(MI, Step.TypeIdx, Step.NewType);
1010b57cec5SDimitry Andric   case WidenScalar:
1020b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Widen scalar\n");
1030b57cec5SDimitry Andric     return widenScalar(MI, Step.TypeIdx, Step.NewType);
1040b57cec5SDimitry Andric   case Lower:
1050b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Lower\n");
1060b57cec5SDimitry Andric     return lower(MI, Step.TypeIdx, Step.NewType);
1070b57cec5SDimitry Andric   case FewerElements:
1080b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Reduce number of elements\n");
1090b57cec5SDimitry Andric     return fewerElementsVector(MI, Step.TypeIdx, Step.NewType);
1100b57cec5SDimitry Andric   case MoreElements:
1110b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Increase number of elements\n");
1120b57cec5SDimitry Andric     return moreElementsVector(MI, Step.TypeIdx, Step.NewType);
1130b57cec5SDimitry Andric   case Custom:
1140b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Custom legalization\n");
1150b57cec5SDimitry Andric     return LI.legalizeCustom(MI, MRI, MIRBuilder, Observer) ? Legalized
1160b57cec5SDimitry Andric                                                             : UnableToLegalize;
1170b57cec5SDimitry Andric   default:
1180b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. Unable to legalize\n");
1190b57cec5SDimitry Andric     return UnableToLegalize;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric void LegalizerHelper::extractParts(Register Reg, LLT Ty, int NumParts,
1240b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &VRegs) {
1250b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i)
1260b57cec5SDimitry Andric     VRegs.push_back(MRI.createGenericVirtualRegister(Ty));
1270b57cec5SDimitry Andric   MIRBuilder.buildUnmerge(VRegs, Reg);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric bool LegalizerHelper::extractParts(Register Reg, LLT RegTy,
1310b57cec5SDimitry Andric                                    LLT MainTy, LLT &LeftoverTy,
1320b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &VRegs,
1330b57cec5SDimitry Andric                                    SmallVectorImpl<Register> &LeftoverRegs) {
1340b57cec5SDimitry Andric   assert(!LeftoverTy.isValid() && "this is an out argument");
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   unsigned RegSize = RegTy.getSizeInBits();
1370b57cec5SDimitry Andric   unsigned MainSize = MainTy.getSizeInBits();
1380b57cec5SDimitry Andric   unsigned NumParts = RegSize / MainSize;
1390b57cec5SDimitry Andric   unsigned LeftoverSize = RegSize - NumParts * MainSize;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // Use an unmerge when possible.
1420b57cec5SDimitry Andric   if (LeftoverSize == 0) {
1430b57cec5SDimitry Andric     for (unsigned I = 0; I < NumParts; ++I)
1440b57cec5SDimitry Andric       VRegs.push_back(MRI.createGenericVirtualRegister(MainTy));
1450b57cec5SDimitry Andric     MIRBuilder.buildUnmerge(VRegs, Reg);
1460b57cec5SDimitry Andric     return true;
1470b57cec5SDimitry Andric   }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   if (MainTy.isVector()) {
1500b57cec5SDimitry Andric     unsigned EltSize = MainTy.getScalarSizeInBits();
1510b57cec5SDimitry Andric     if (LeftoverSize % EltSize != 0)
1520b57cec5SDimitry Andric       return false;
1530b57cec5SDimitry Andric     LeftoverTy = LLT::scalarOrVector(LeftoverSize / EltSize, EltSize);
1540b57cec5SDimitry Andric   } else {
1550b57cec5SDimitry Andric     LeftoverTy = LLT::scalar(LeftoverSize);
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // For irregular sizes, extract the individual parts.
1590b57cec5SDimitry Andric   for (unsigned I = 0; I != NumParts; ++I) {
1600b57cec5SDimitry Andric     Register NewReg = MRI.createGenericVirtualRegister(MainTy);
1610b57cec5SDimitry Andric     VRegs.push_back(NewReg);
1620b57cec5SDimitry Andric     MIRBuilder.buildExtract(NewReg, Reg, MainSize * I);
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   for (unsigned Offset = MainSize * NumParts; Offset < RegSize;
1660b57cec5SDimitry Andric        Offset += LeftoverSize) {
1670b57cec5SDimitry Andric     Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy);
1680b57cec5SDimitry Andric     LeftoverRegs.push_back(NewReg);
1690b57cec5SDimitry Andric     MIRBuilder.buildExtract(NewReg, Reg, Offset);
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   return true;
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
1758bcb0991SDimitry Andric static LLT getGCDType(LLT OrigTy, LLT TargetTy) {
1768bcb0991SDimitry Andric   if (OrigTy.isVector() && TargetTy.isVector()) {
1778bcb0991SDimitry Andric     assert(OrigTy.getElementType() == TargetTy.getElementType());
1788bcb0991SDimitry Andric     int GCD = greatestCommonDivisor(OrigTy.getNumElements(),
1798bcb0991SDimitry Andric                                     TargetTy.getNumElements());
1808bcb0991SDimitry Andric     return LLT::scalarOrVector(GCD, OrigTy.getElementType());
1818bcb0991SDimitry Andric   }
1828bcb0991SDimitry Andric 
1838bcb0991SDimitry Andric   if (OrigTy.isVector() && !TargetTy.isVector()) {
1848bcb0991SDimitry Andric     assert(OrigTy.getElementType() == TargetTy);
1858bcb0991SDimitry Andric     return TargetTy;
1868bcb0991SDimitry Andric   }
1878bcb0991SDimitry Andric 
1888bcb0991SDimitry Andric   assert(!OrigTy.isVector() && !TargetTy.isVector());
1898bcb0991SDimitry Andric 
1908bcb0991SDimitry Andric   int GCD = greatestCommonDivisor(OrigTy.getSizeInBits(),
1918bcb0991SDimitry Andric                                   TargetTy.getSizeInBits());
1928bcb0991SDimitry Andric   return LLT::scalar(GCD);
1938bcb0991SDimitry Andric }
1948bcb0991SDimitry 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 
2400b57cec5SDimitry Andric static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) {
2410b57cec5SDimitry Andric   switch (Opcode) {
2420b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
2438bcb0991SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
2448bcb0991SDimitry Andric     switch (Size) {
2458bcb0991SDimitry Andric     case 32:
2468bcb0991SDimitry Andric       return RTLIB::SDIV_I32;
2478bcb0991SDimitry Andric     case 64:
2488bcb0991SDimitry Andric       return RTLIB::SDIV_I64;
2498bcb0991SDimitry Andric     case 128:
2508bcb0991SDimitry Andric       return RTLIB::SDIV_I128;
2518bcb0991SDimitry Andric     default:
2528bcb0991SDimitry Andric       llvm_unreachable("unexpected size");
2538bcb0991SDimitry Andric     }
2540b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
2558bcb0991SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
2568bcb0991SDimitry Andric     switch (Size) {
2578bcb0991SDimitry Andric     case 32:
2588bcb0991SDimitry Andric       return RTLIB::UDIV_I32;
2598bcb0991SDimitry Andric     case 64:
2608bcb0991SDimitry Andric       return RTLIB::UDIV_I64;
2618bcb0991SDimitry Andric     case 128:
2628bcb0991SDimitry Andric       return RTLIB::UDIV_I128;
2638bcb0991SDimitry Andric     default:
2648bcb0991SDimitry Andric       llvm_unreachable("unexpected size");
2658bcb0991SDimitry Andric     }
2660b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
2670b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2680b57cec5SDimitry Andric     return Size == 64 ? RTLIB::SREM_I64 : RTLIB::SREM_I32;
2690b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
2700b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2710b57cec5SDimitry Andric     return Size == 64 ? RTLIB::UREM_I64 : RTLIB::UREM_I32;
2720b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
2730b57cec5SDimitry Andric     assert(Size == 32 && "Unsupported size");
2740b57cec5SDimitry Andric     return RTLIB::CTLZ_I32;
2750b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
2760b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2770b57cec5SDimitry Andric     return Size == 64 ? RTLIB::ADD_F64 : RTLIB::ADD_F32;
2780b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
2790b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2800b57cec5SDimitry Andric     return Size == 64 ? RTLIB::SUB_F64 : RTLIB::SUB_F32;
2810b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
2820b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2830b57cec5SDimitry Andric     return Size == 64 ? RTLIB::MUL_F64 : RTLIB::MUL_F32;
2840b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
2850b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2860b57cec5SDimitry Andric     return Size == 64 ? RTLIB::DIV_F64 : RTLIB::DIV_F32;
2870b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
2880b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2890b57cec5SDimitry Andric     return Size == 64 ? RTLIB::EXP_F64 : RTLIB::EXP_F32;
2900b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
2910b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2920b57cec5SDimitry Andric     return Size == 64 ? RTLIB::EXP2_F64 : RTLIB::EXP2_F32;
2930b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
2940b57cec5SDimitry Andric     return Size == 64 ? RTLIB::REM_F64 : RTLIB::REM_F32;
2950b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
2960b57cec5SDimitry Andric     return Size == 64 ? RTLIB::POW_F64 : RTLIB::POW_F32;
2970b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
2980b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
2990b57cec5SDimitry Andric     return Size == 64 ? RTLIB::FMA_F64 : RTLIB::FMA_F32;
3000b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
3010b57cec5SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
3020b57cec5SDimitry Andric     return Size == 128 ? RTLIB::SIN_F128
3030b57cec5SDimitry Andric                        : Size == 64 ? RTLIB::SIN_F64 : RTLIB::SIN_F32;
3040b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
3050b57cec5SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
3060b57cec5SDimitry Andric     return Size == 128 ? RTLIB::COS_F128
3070b57cec5SDimitry Andric                        : Size == 64 ? RTLIB::COS_F64 : RTLIB::COS_F32;
3080b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
3090b57cec5SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
3100b57cec5SDimitry Andric     return Size == 128 ? RTLIB::LOG10_F128
3110b57cec5SDimitry Andric                        : Size == 64 ? RTLIB::LOG10_F64 : RTLIB::LOG10_F32;
3120b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
3130b57cec5SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
3140b57cec5SDimitry Andric     return Size == 128 ? RTLIB::LOG_F128
3150b57cec5SDimitry Andric                        : Size == 64 ? RTLIB::LOG_F64 : RTLIB::LOG_F32;
3160b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
3170b57cec5SDimitry Andric     assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size");
3180b57cec5SDimitry Andric     return Size == 128 ? RTLIB::LOG2_F128
3190b57cec5SDimitry Andric                        : Size == 64 ? RTLIB::LOG2_F64 : RTLIB::LOG2_F32;
3200b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
3210b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
3220b57cec5SDimitry Andric     return Size == 64 ? RTLIB::CEIL_F64 : RTLIB::CEIL_F32;
3230b57cec5SDimitry Andric   case TargetOpcode::G_FFLOOR:
3240b57cec5SDimitry Andric     assert((Size == 32 || Size == 64) && "Unsupported size");
3250b57cec5SDimitry Andric     return Size == 64 ? RTLIB::FLOOR_F64 : RTLIB::FLOOR_F32;
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric   llvm_unreachable("Unknown libcall function");
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3308bcb0991SDimitry Andric /// True if an instruction is in tail position in its caller. Intended for
3318bcb0991SDimitry Andric /// legalizing libcalls as tail calls when possible.
3328bcb0991SDimitry Andric static bool isLibCallInTailPosition(MachineInstr &MI) {
3338bcb0991SDimitry Andric   const Function &F = MI.getParent()->getParent()->getFunction();
3348bcb0991SDimitry Andric 
3358bcb0991SDimitry Andric   // Conservatively require the attributes of the call to match those of
3368bcb0991SDimitry Andric   // the return. Ignore NoAlias and NonNull because they don't affect the
3378bcb0991SDimitry Andric   // call sequence.
3388bcb0991SDimitry Andric   AttributeList CallerAttrs = F.getAttributes();
3398bcb0991SDimitry Andric   if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex)
3408bcb0991SDimitry Andric           .removeAttribute(Attribute::NoAlias)
3418bcb0991SDimitry Andric           .removeAttribute(Attribute::NonNull)
3428bcb0991SDimitry Andric           .hasAttributes())
3438bcb0991SDimitry Andric     return false;
3448bcb0991SDimitry Andric 
3458bcb0991SDimitry Andric   // It's not safe to eliminate the sign / zero extension of the return value.
3468bcb0991SDimitry Andric   if (CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt) ||
3478bcb0991SDimitry Andric       CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
3488bcb0991SDimitry Andric     return false;
3498bcb0991SDimitry Andric 
3508bcb0991SDimitry Andric   // Only tail call if the following instruction is a standard return.
3518bcb0991SDimitry Andric   auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
3528bcb0991SDimitry Andric   MachineInstr *Next = MI.getNextNode();
3538bcb0991SDimitry Andric   if (!Next || TII.isTailCall(*Next) || !Next->isReturn())
3548bcb0991SDimitry Andric     return false;
3558bcb0991SDimitry Andric 
3568bcb0991SDimitry Andric   return true;
3578bcb0991SDimitry Andric }
3588bcb0991SDimitry Andric 
3590b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
3600b57cec5SDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall,
3610b57cec5SDimitry Andric                     const CallLowering::ArgInfo &Result,
3620b57cec5SDimitry Andric                     ArrayRef<CallLowering::ArgInfo> Args) {
3630b57cec5SDimitry Andric   auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering();
3640b57cec5SDimitry Andric   auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
3650b57cec5SDimitry Andric   const char *Name = TLI.getLibcallName(Libcall);
3660b57cec5SDimitry Andric 
3678bcb0991SDimitry Andric   CallLowering::CallLoweringInfo Info;
3688bcb0991SDimitry Andric   Info.CallConv = TLI.getLibcallCallingConv(Libcall);
3698bcb0991SDimitry Andric   Info.Callee = MachineOperand::CreateES(Name);
3708bcb0991SDimitry Andric   Info.OrigRet = Result;
3718bcb0991SDimitry Andric   std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
3728bcb0991SDimitry Andric   if (!CLI.lowerCall(MIRBuilder, Info))
3730b57cec5SDimitry Andric     return LegalizerHelper::UnableToLegalize;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   return LegalizerHelper::Legalized;
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric // Useful for libcalls where all operands have the same type.
3790b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult
3800b57cec5SDimitry Andric simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size,
3810b57cec5SDimitry Andric               Type *OpType) {
3820b57cec5SDimitry Andric   auto Libcall = getRTLibDesc(MI.getOpcode(), Size);
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   SmallVector<CallLowering::ArgInfo, 3> Args;
3850b57cec5SDimitry Andric   for (unsigned i = 1; i < MI.getNumOperands(); i++)
3860b57cec5SDimitry Andric     Args.push_back({MI.getOperand(i).getReg(), OpType});
3870b57cec5SDimitry Andric   return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), OpType},
3880b57cec5SDimitry Andric                        Args);
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric 
3918bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
3928bcb0991SDimitry Andric llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
3938bcb0991SDimitry Andric                        MachineInstr &MI) {
3948bcb0991SDimitry Andric   assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS);
3958bcb0991SDimitry Andric   auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
3968bcb0991SDimitry Andric 
3978bcb0991SDimitry Andric   SmallVector<CallLowering::ArgInfo, 3> Args;
3988bcb0991SDimitry Andric   // Add all the args, except for the last which is an imm denoting 'tail'.
3998bcb0991SDimitry Andric   for (unsigned i = 1; i < MI.getNumOperands() - 1; i++) {
4008bcb0991SDimitry Andric     Register Reg = MI.getOperand(i).getReg();
4018bcb0991SDimitry Andric 
4028bcb0991SDimitry Andric     // Need derive an IR type for call lowering.
4038bcb0991SDimitry Andric     LLT OpLLT = MRI.getType(Reg);
4048bcb0991SDimitry Andric     Type *OpTy = nullptr;
4058bcb0991SDimitry Andric     if (OpLLT.isPointer())
4068bcb0991SDimitry Andric       OpTy = Type::getInt8PtrTy(Ctx, OpLLT.getAddressSpace());
4078bcb0991SDimitry Andric     else
4088bcb0991SDimitry Andric       OpTy = IntegerType::get(Ctx, OpLLT.getSizeInBits());
4098bcb0991SDimitry Andric     Args.push_back({Reg, OpTy});
4108bcb0991SDimitry Andric   }
4118bcb0991SDimitry Andric 
4128bcb0991SDimitry Andric   auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering();
4138bcb0991SDimitry Andric   auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
4148bcb0991SDimitry Andric   Intrinsic::ID ID = MI.getOperand(0).getIntrinsicID();
4158bcb0991SDimitry Andric   RTLIB::Libcall RTLibcall;
4168bcb0991SDimitry Andric   switch (ID) {
4178bcb0991SDimitry Andric   case Intrinsic::memcpy:
4188bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMCPY;
4198bcb0991SDimitry Andric     break;
4208bcb0991SDimitry Andric   case Intrinsic::memset:
4218bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMSET;
4228bcb0991SDimitry Andric     break;
4238bcb0991SDimitry Andric   case Intrinsic::memmove:
4248bcb0991SDimitry Andric     RTLibcall = RTLIB::MEMMOVE;
4258bcb0991SDimitry Andric     break;
4268bcb0991SDimitry Andric   default:
4278bcb0991SDimitry Andric     return LegalizerHelper::UnableToLegalize;
4288bcb0991SDimitry Andric   }
4298bcb0991SDimitry Andric   const char *Name = TLI.getLibcallName(RTLibcall);
4308bcb0991SDimitry Andric 
4318bcb0991SDimitry Andric   MIRBuilder.setInstr(MI);
4328bcb0991SDimitry Andric 
4338bcb0991SDimitry Andric   CallLowering::CallLoweringInfo Info;
4348bcb0991SDimitry Andric   Info.CallConv = TLI.getLibcallCallingConv(RTLibcall);
4358bcb0991SDimitry Andric   Info.Callee = MachineOperand::CreateES(Name);
4368bcb0991SDimitry Andric   Info.OrigRet = CallLowering::ArgInfo({0}, Type::getVoidTy(Ctx));
4378bcb0991SDimitry Andric   Info.IsTailCall = MI.getOperand(MI.getNumOperands() - 1).getImm() == 1 &&
4388bcb0991SDimitry Andric                     isLibCallInTailPosition(MI);
4398bcb0991SDimitry Andric 
4408bcb0991SDimitry Andric   std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
4418bcb0991SDimitry Andric   if (!CLI.lowerCall(MIRBuilder, Info))
4428bcb0991SDimitry Andric     return LegalizerHelper::UnableToLegalize;
4438bcb0991SDimitry Andric 
4448bcb0991SDimitry Andric   if (Info.LoweredTailCall) {
4458bcb0991SDimitry Andric     assert(Info.IsTailCall && "Lowered tail call when it wasn't a tail call?");
4468bcb0991SDimitry Andric     // We must have a return following the call to get past
4478bcb0991SDimitry Andric     // isLibCallInTailPosition.
4488bcb0991SDimitry Andric     assert(MI.getNextNode() && MI.getNextNode()->isReturn() &&
4498bcb0991SDimitry Andric            "Expected instr following MI to be a return?");
4508bcb0991SDimitry Andric 
4518bcb0991SDimitry Andric     // We lowered a tail call, so the call is now the return from the block.
4528bcb0991SDimitry Andric     // Delete the old return.
4538bcb0991SDimitry Andric     MI.getNextNode()->eraseFromParent();
4548bcb0991SDimitry Andric   }
4558bcb0991SDimitry Andric 
4568bcb0991SDimitry Andric   return LegalizerHelper::Legalized;
4578bcb0991SDimitry Andric }
4588bcb0991SDimitry Andric 
4590b57cec5SDimitry Andric static RTLIB::Libcall getConvRTLibDesc(unsigned Opcode, Type *ToType,
4600b57cec5SDimitry Andric                                        Type *FromType) {
4610b57cec5SDimitry Andric   auto ToMVT = MVT::getVT(ToType);
4620b57cec5SDimitry Andric   auto FromMVT = MVT::getVT(FromType);
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   switch (Opcode) {
4650b57cec5SDimitry Andric   case TargetOpcode::G_FPEXT:
4660b57cec5SDimitry Andric     return RTLIB::getFPEXT(FromMVT, ToMVT);
4670b57cec5SDimitry Andric   case TargetOpcode::G_FPTRUNC:
4680b57cec5SDimitry Andric     return RTLIB::getFPROUND(FromMVT, ToMVT);
4690b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
4700b57cec5SDimitry Andric     return RTLIB::getFPTOSINT(FromMVT, ToMVT);
4710b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI:
4720b57cec5SDimitry Andric     return RTLIB::getFPTOUINT(FromMVT, ToMVT);
4730b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
4740b57cec5SDimitry Andric     return RTLIB::getSINTTOFP(FromMVT, ToMVT);
4750b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP:
4760b57cec5SDimitry Andric     return RTLIB::getUINTTOFP(FromMVT, ToMVT);
4770b57cec5SDimitry Andric   }
4780b57cec5SDimitry Andric   llvm_unreachable("Unsupported libcall function");
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult
4820b57cec5SDimitry Andric conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType,
4830b57cec5SDimitry Andric                   Type *FromType) {
4840b57cec5SDimitry Andric   RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType);
4850b57cec5SDimitry Andric   return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), ToType},
4860b57cec5SDimitry Andric                        {{MI.getOperand(1).getReg(), FromType}});
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
4900b57cec5SDimitry Andric LegalizerHelper::libcall(MachineInstr &MI) {
4910b57cec5SDimitry Andric   LLT LLTy = MRI.getType(MI.getOperand(0).getReg());
4920b57cec5SDimitry Andric   unsigned Size = LLTy.getSizeInBits();
4930b57cec5SDimitry Andric   auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   switch (MI.getOpcode()) {
4980b57cec5SDimitry Andric   default:
4990b57cec5SDimitry Andric     return UnableToLegalize;
5000b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
5010b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
5020b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
5030b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
5040b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF: {
5050b57cec5SDimitry Andric     Type *HLTy = IntegerType::get(Ctx, Size);
5060b57cec5SDimitry Andric     auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy);
5070b57cec5SDimitry Andric     if (Status != Legalized)
5080b57cec5SDimitry Andric       return Status;
5090b57cec5SDimitry Andric     break;
5100b57cec5SDimitry Andric   }
5110b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
5120b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
5130b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
5140b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
5150b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
5160b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
5170b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
5180b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
5190b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
5200b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
5210b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
5220b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
5230b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
5240b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
5250b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
5260b57cec5SDimitry Andric   case TargetOpcode::G_FFLOOR: {
5270b57cec5SDimitry Andric     if (Size > 64) {
5280b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Size " << Size << " too large to legalize.\n");
5290b57cec5SDimitry Andric       return UnableToLegalize;
5300b57cec5SDimitry Andric     }
5310b57cec5SDimitry Andric     Type *HLTy = Size == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx);
5320b57cec5SDimitry Andric     auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy);
5330b57cec5SDimitry Andric     if (Status != Legalized)
5340b57cec5SDimitry Andric       return Status;
5350b57cec5SDimitry Andric     break;
5360b57cec5SDimitry Andric   }
5370b57cec5SDimitry Andric   case TargetOpcode::G_FPEXT: {
5380b57cec5SDimitry Andric     // FIXME: Support other floating point types (half, fp128 etc)
5390b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
5400b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
5410b57cec5SDimitry Andric     if (ToSize != 64 || FromSize != 32)
5420b57cec5SDimitry Andric       return UnableToLegalize;
5430b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
5440b57cec5SDimitry Andric         MI, MIRBuilder, Type::getDoubleTy(Ctx), Type::getFloatTy(Ctx));
5450b57cec5SDimitry Andric     if (Status != Legalized)
5460b57cec5SDimitry Andric       return Status;
5470b57cec5SDimitry Andric     break;
5480b57cec5SDimitry Andric   }
5490b57cec5SDimitry Andric   case TargetOpcode::G_FPTRUNC: {
5500b57cec5SDimitry Andric     // FIXME: Support other floating point types (half, fp128 etc)
5510b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
5520b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
5530b57cec5SDimitry Andric     if (ToSize != 32 || FromSize != 64)
5540b57cec5SDimitry Andric       return UnableToLegalize;
5550b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
5560b57cec5SDimitry Andric         MI, MIRBuilder, Type::getFloatTy(Ctx), Type::getDoubleTy(Ctx));
5570b57cec5SDimitry Andric     if (Status != Legalized)
5580b57cec5SDimitry Andric       return Status;
5590b57cec5SDimitry Andric     break;
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
5620b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI: {
5630b57cec5SDimitry Andric     // FIXME: Support other types
5640b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
5650b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
5660b57cec5SDimitry Andric     if ((ToSize != 32 && ToSize != 64) || (FromSize != 32 && FromSize != 64))
5670b57cec5SDimitry Andric       return UnableToLegalize;
5680b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
5690b57cec5SDimitry Andric         MI, MIRBuilder,
5700b57cec5SDimitry Andric         ToSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx),
5710b57cec5SDimitry Andric         FromSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx));
5720b57cec5SDimitry Andric     if (Status != Legalized)
5730b57cec5SDimitry Andric       return Status;
5740b57cec5SDimitry Andric     break;
5750b57cec5SDimitry Andric   }
5760b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
5770b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP: {
5780b57cec5SDimitry Andric     // FIXME: Support other types
5790b57cec5SDimitry Andric     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
5800b57cec5SDimitry Andric     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
5810b57cec5SDimitry Andric     if ((FromSize != 32 && FromSize != 64) || (ToSize != 32 && ToSize != 64))
5820b57cec5SDimitry Andric       return UnableToLegalize;
5830b57cec5SDimitry Andric     LegalizeResult Status = conversionLibcall(
5840b57cec5SDimitry Andric         MI, MIRBuilder,
5850b57cec5SDimitry Andric         ToSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx),
5860b57cec5SDimitry Andric         FromSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx));
5870b57cec5SDimitry Andric     if (Status != Legalized)
5880b57cec5SDimitry Andric       return Status;
5890b57cec5SDimitry Andric     break;
5900b57cec5SDimitry Andric   }
5910b57cec5SDimitry Andric   }
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric   MI.eraseFromParent();
5940b57cec5SDimitry Andric   return Legalized;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
5980b57cec5SDimitry Andric                                                               unsigned TypeIdx,
5990b57cec5SDimitry Andric                                                               LLT NarrowTy) {
6000b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
6030b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   switch (MI.getOpcode()) {
6060b57cec5SDimitry Andric   default:
6070b57cec5SDimitry Andric     return UnableToLegalize;
6080b57cec5SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF: {
6090b57cec5SDimitry Andric     // FIXME: add support for when SizeOp0 isn't an exact multiple of
6100b57cec5SDimitry Andric     // NarrowSize.
6110b57cec5SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
6120b57cec5SDimitry Andric       return UnableToLegalize;
6130b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric     SmallVector<Register, 2> DstRegs;
6160b57cec5SDimitry Andric     for (int i = 0; i < NumParts; ++i)
6170b57cec5SDimitry Andric       DstRegs.push_back(
6180b57cec5SDimitry Andric           MIRBuilder.buildUndef(NarrowTy)->getOperand(0).getReg());
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
6210b57cec5SDimitry Andric     if(MRI.getType(DstReg).isVector())
6220b57cec5SDimitry Andric       MIRBuilder.buildBuildVector(DstReg, DstRegs);
6230b57cec5SDimitry Andric     else
6240b57cec5SDimitry Andric       MIRBuilder.buildMerge(DstReg, DstRegs);
6250b57cec5SDimitry Andric     MI.eraseFromParent();
6260b57cec5SDimitry Andric     return Legalized;
6270b57cec5SDimitry Andric   }
6280b57cec5SDimitry Andric   case TargetOpcode::G_CONSTANT: {
6290b57cec5SDimitry Andric     LLT Ty = MRI.getType(MI.getOperand(0).getReg());
6300b57cec5SDimitry Andric     const APInt &Val = MI.getOperand(1).getCImm()->getValue();
6310b57cec5SDimitry Andric     unsigned TotalSize = Ty.getSizeInBits();
6320b57cec5SDimitry Andric     unsigned NarrowSize = NarrowTy.getSizeInBits();
6330b57cec5SDimitry Andric     int NumParts = TotalSize / NarrowSize;
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric     SmallVector<Register, 4> PartRegs;
6360b57cec5SDimitry Andric     for (int I = 0; I != NumParts; ++I) {
6370b57cec5SDimitry Andric       unsigned Offset = I * NarrowSize;
6380b57cec5SDimitry Andric       auto K = MIRBuilder.buildConstant(NarrowTy,
6390b57cec5SDimitry Andric                                         Val.lshr(Offset).trunc(NarrowSize));
6400b57cec5SDimitry Andric       PartRegs.push_back(K.getReg(0));
6410b57cec5SDimitry Andric     }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric     LLT LeftoverTy;
6440b57cec5SDimitry Andric     unsigned LeftoverBits = TotalSize - NumParts * NarrowSize;
6450b57cec5SDimitry Andric     SmallVector<Register, 1> LeftoverRegs;
6460b57cec5SDimitry Andric     if (LeftoverBits != 0) {
6470b57cec5SDimitry Andric       LeftoverTy = LLT::scalar(LeftoverBits);
6480b57cec5SDimitry Andric       auto K = MIRBuilder.buildConstant(
6490b57cec5SDimitry Andric         LeftoverTy,
6500b57cec5SDimitry Andric         Val.lshr(NumParts * NarrowSize).trunc(LeftoverBits));
6510b57cec5SDimitry Andric       LeftoverRegs.push_back(K.getReg(0));
6520b57cec5SDimitry Andric     }
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric     insertParts(MI.getOperand(0).getReg(),
6550b57cec5SDimitry Andric                 Ty, NarrowTy, PartRegs, LeftoverTy, LeftoverRegs);
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric     MI.eraseFromParent();
6580b57cec5SDimitry Andric     return Legalized;
6590b57cec5SDimitry Andric   }
6608bcb0991SDimitry Andric   case TargetOpcode::G_SEXT: {
6618bcb0991SDimitry Andric     if (TypeIdx != 0)
6628bcb0991SDimitry Andric       return UnableToLegalize;
6638bcb0991SDimitry Andric 
6648bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
6658bcb0991SDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
6668bcb0991SDimitry Andric 
6678bcb0991SDimitry Andric     // FIXME: support the general case where the requested NarrowTy may not be
6688bcb0991SDimitry Andric     // the same as the source type. E.g. s128 = sext(s32)
6698bcb0991SDimitry Andric     if ((SrcTy.getSizeInBits() != SizeOp0 / 2) ||
6708bcb0991SDimitry Andric         SrcTy.getSizeInBits() != NarrowTy.getSizeInBits()) {
6718bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't narrow sext to type " << NarrowTy << "\n");
6728bcb0991SDimitry Andric       return UnableToLegalize;
6738bcb0991SDimitry Andric     }
6748bcb0991SDimitry Andric 
6758bcb0991SDimitry Andric     // Shift the sign bit of the low register through the high register.
6768bcb0991SDimitry Andric     auto ShiftAmt =
6778bcb0991SDimitry Andric         MIRBuilder.buildConstant(LLT::scalar(64), NarrowTy.getSizeInBits() - 1);
6788bcb0991SDimitry Andric     auto Shift = MIRBuilder.buildAShr(NarrowTy, SrcReg, ShiftAmt);
6798bcb0991SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {SrcReg, Shift.getReg(0)});
6808bcb0991SDimitry Andric     MI.eraseFromParent();
6818bcb0991SDimitry Andric     return Legalized;
6828bcb0991SDimitry Andric   }
6838bcb0991SDimitry Andric   case TargetOpcode::G_ZEXT: {
6848bcb0991SDimitry Andric     if (TypeIdx != 0)
6858bcb0991SDimitry Andric       return UnableToLegalize;
6868bcb0991SDimitry Andric 
6878bcb0991SDimitry Andric     LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
6888bcb0991SDimitry Andric     uint64_t SizeOp1 = SrcTy.getSizeInBits();
6898bcb0991SDimitry Andric     if (SizeOp0 % SizeOp1 != 0)
6908bcb0991SDimitry Andric       return UnableToLegalize;
6918bcb0991SDimitry Andric 
6928bcb0991SDimitry Andric     // Generate a merge where the bottom bits are taken from the source, and
6938bcb0991SDimitry Andric     // zero everything else.
6948bcb0991SDimitry Andric     Register ZeroReg = MIRBuilder.buildConstant(SrcTy, 0).getReg(0);
6958bcb0991SDimitry Andric     unsigned NumParts = SizeOp0 / SizeOp1;
6968bcb0991SDimitry Andric     SmallVector<Register, 4> Srcs = {MI.getOperand(1).getReg()};
6978bcb0991SDimitry Andric     for (unsigned Part = 1; Part < NumParts; ++Part)
6988bcb0991SDimitry Andric       Srcs.push_back(ZeroReg);
6998bcb0991SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), Srcs);
7008bcb0991SDimitry Andric     MI.eraseFromParent();
7018bcb0991SDimitry Andric     return Legalized;
7028bcb0991SDimitry Andric   }
7038bcb0991SDimitry Andric   case TargetOpcode::G_TRUNC: {
7048bcb0991SDimitry Andric     if (TypeIdx != 1)
7058bcb0991SDimitry Andric       return UnableToLegalize;
7068bcb0991SDimitry Andric 
7078bcb0991SDimitry Andric     uint64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
7088bcb0991SDimitry Andric     if (NarrowTy.getSizeInBits() * 2 != SizeOp1) {
7098bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't narrow trunc to type " << NarrowTy << "\n");
7108bcb0991SDimitry Andric       return UnableToLegalize;
7118bcb0991SDimitry Andric     }
7128bcb0991SDimitry Andric 
7138bcb0991SDimitry Andric     auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1).getReg());
7148bcb0991SDimitry Andric     MIRBuilder.buildCopy(MI.getOperand(0).getReg(), Unmerge.getReg(0));
7158bcb0991SDimitry Andric     MI.eraseFromParent();
7168bcb0991SDimitry Andric     return Legalized;
7178bcb0991SDimitry Andric   }
7188bcb0991SDimitry Andric 
7190b57cec5SDimitry Andric   case TargetOpcode::G_ADD: {
7200b57cec5SDimitry Andric     // FIXME: add support for when SizeOp0 isn't an exact multiple of
7210b57cec5SDimitry Andric     // NarrowSize.
7220b57cec5SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
7230b57cec5SDimitry Andric       return UnableToLegalize;
7240b57cec5SDimitry Andric     // Expand in terms of carry-setting/consuming G_ADDE instructions.
7250b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric     SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
7280b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
7290b57cec5SDimitry Andric     extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
7300b57cec5SDimitry Andric 
7318bcb0991SDimitry Andric     Register CarryIn;
7320b57cec5SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
7330b57cec5SDimitry Andric       Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
7340b57cec5SDimitry Andric       Register CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
7350b57cec5SDimitry Andric 
7368bcb0991SDimitry Andric       if (i == 0)
7378bcb0991SDimitry Andric         MIRBuilder.buildUAddo(DstReg, CarryOut, Src1Regs[i], Src2Regs[i]);
7388bcb0991SDimitry Andric       else {
7390b57cec5SDimitry Andric         MIRBuilder.buildUAdde(DstReg, CarryOut, Src1Regs[i],
7400b57cec5SDimitry Andric                               Src2Regs[i], CarryIn);
7418bcb0991SDimitry Andric       }
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric       DstRegs.push_back(DstReg);
7440b57cec5SDimitry Andric       CarryIn = CarryOut;
7450b57cec5SDimitry Andric     }
7460b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
7470b57cec5SDimitry Andric     if(MRI.getType(DstReg).isVector())
7480b57cec5SDimitry Andric       MIRBuilder.buildBuildVector(DstReg, DstRegs);
7490b57cec5SDimitry Andric     else
7500b57cec5SDimitry Andric       MIRBuilder.buildMerge(DstReg, DstRegs);
7510b57cec5SDimitry Andric     MI.eraseFromParent();
7520b57cec5SDimitry Andric     return Legalized;
7530b57cec5SDimitry Andric   }
7540b57cec5SDimitry Andric   case TargetOpcode::G_SUB: {
7550b57cec5SDimitry Andric     // FIXME: add support for when SizeOp0 isn't an exact multiple of
7560b57cec5SDimitry Andric     // NarrowSize.
7570b57cec5SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
7580b57cec5SDimitry Andric       return UnableToLegalize;
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric     SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
7630b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
7640b57cec5SDimitry Andric     extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
7670b57cec5SDimitry Andric     Register BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
7680b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_USUBO, {DstReg, BorrowOut},
7690b57cec5SDimitry Andric                           {Src1Regs[0], Src2Regs[0]});
7700b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
7710b57cec5SDimitry Andric     Register BorrowIn = BorrowOut;
7720b57cec5SDimitry Andric     for (int i = 1; i < NumParts; ++i) {
7730b57cec5SDimitry Andric       DstReg = MRI.createGenericVirtualRegister(NarrowTy);
7740b57cec5SDimitry Andric       BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_USUBE, {DstReg, BorrowOut},
7770b57cec5SDimitry Andric                             {Src1Regs[i], Src2Regs[i], BorrowIn});
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric       DstRegs.push_back(DstReg);
7800b57cec5SDimitry Andric       BorrowIn = BorrowOut;
7810b57cec5SDimitry Andric     }
7820b57cec5SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), DstRegs);
7830b57cec5SDimitry Andric     MI.eraseFromParent();
7840b57cec5SDimitry Andric     return Legalized;
7850b57cec5SDimitry Andric   }
7860b57cec5SDimitry Andric   case TargetOpcode::G_MUL:
7870b57cec5SDimitry Andric   case TargetOpcode::G_UMULH:
7880b57cec5SDimitry Andric     return narrowScalarMul(MI, NarrowTy);
7890b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
7900b57cec5SDimitry Andric     return narrowScalarExtract(MI, TypeIdx, NarrowTy);
7910b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
7920b57cec5SDimitry Andric     return narrowScalarInsert(MI, TypeIdx, NarrowTy);
7930b57cec5SDimitry Andric   case TargetOpcode::G_LOAD: {
7940b57cec5SDimitry Andric     const auto &MMO = **MI.memoperands_begin();
7950b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
7960b57cec5SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
7970b57cec5SDimitry Andric     if (DstTy.isVector())
7980b57cec5SDimitry Andric       return UnableToLegalize;
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric     if (8 * MMO.getSize() != DstTy.getSizeInBits()) {
8010b57cec5SDimitry Andric       Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
8020b57cec5SDimitry Andric       auto &MMO = **MI.memoperands_begin();
8030b57cec5SDimitry Andric       MIRBuilder.buildLoad(TmpReg, MI.getOperand(1).getReg(), MMO);
8040b57cec5SDimitry Andric       MIRBuilder.buildAnyExt(DstReg, TmpReg);
8050b57cec5SDimitry Andric       MI.eraseFromParent();
8060b57cec5SDimitry Andric       return Legalized;
8070b57cec5SDimitry Andric     }
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy);
8100b57cec5SDimitry Andric   }
8110b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD:
8120b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD: {
8130b57cec5SDimitry Andric     bool ZExt = MI.getOpcode() == TargetOpcode::G_ZEXTLOAD;
8140b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
8150b57cec5SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric     Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
8180b57cec5SDimitry Andric     auto &MMO = **MI.memoperands_begin();
8190b57cec5SDimitry Andric     if (MMO.getSizeInBits() == NarrowSize) {
8200b57cec5SDimitry Andric       MIRBuilder.buildLoad(TmpReg, PtrReg, MMO);
8210b57cec5SDimitry Andric     } else {
8220b57cec5SDimitry Andric       unsigned ExtLoad = ZExt ? TargetOpcode::G_ZEXTLOAD
8230b57cec5SDimitry Andric         : TargetOpcode::G_SEXTLOAD;
8240b57cec5SDimitry Andric       MIRBuilder.buildInstr(ExtLoad)
8250b57cec5SDimitry Andric         .addDef(TmpReg)
8260b57cec5SDimitry Andric         .addUse(PtrReg)
8270b57cec5SDimitry Andric         .addMemOperand(&MMO);
8280b57cec5SDimitry Andric     }
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric     if (ZExt)
8310b57cec5SDimitry Andric       MIRBuilder.buildZExt(DstReg, TmpReg);
8320b57cec5SDimitry Andric     else
8330b57cec5SDimitry Andric       MIRBuilder.buildSExt(DstReg, TmpReg);
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric     MI.eraseFromParent();
8360b57cec5SDimitry Andric     return Legalized;
8370b57cec5SDimitry Andric   }
8380b57cec5SDimitry Andric   case TargetOpcode::G_STORE: {
8390b57cec5SDimitry Andric     const auto &MMO = **MI.memoperands_begin();
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(0).getReg();
8420b57cec5SDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
8430b57cec5SDimitry Andric     if (SrcTy.isVector())
8440b57cec5SDimitry Andric       return UnableToLegalize;
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
8470b57cec5SDimitry Andric     unsigned HandledSize = NumParts * NarrowTy.getSizeInBits();
8480b57cec5SDimitry Andric     unsigned LeftoverBits = SrcTy.getSizeInBits() - HandledSize;
8490b57cec5SDimitry Andric     if (SrcTy.isVector() && LeftoverBits != 0)
8500b57cec5SDimitry Andric       return UnableToLegalize;
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric     if (8 * MMO.getSize() != SrcTy.getSizeInBits()) {
8530b57cec5SDimitry Andric       Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
8540b57cec5SDimitry Andric       auto &MMO = **MI.memoperands_begin();
8550b57cec5SDimitry Andric       MIRBuilder.buildTrunc(TmpReg, SrcReg);
8560b57cec5SDimitry Andric       MIRBuilder.buildStore(TmpReg, MI.getOperand(1).getReg(), MMO);
8570b57cec5SDimitry Andric       MI.eraseFromParent();
8580b57cec5SDimitry Andric       return Legalized;
8590b57cec5SDimitry Andric     }
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, 0, NarrowTy);
8620b57cec5SDimitry Andric   }
8630b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
8640b57cec5SDimitry Andric     return narrowScalarSelect(MI, TypeIdx, NarrowTy);
8650b57cec5SDimitry Andric   case TargetOpcode::G_AND:
8660b57cec5SDimitry Andric   case TargetOpcode::G_OR:
8670b57cec5SDimitry Andric   case TargetOpcode::G_XOR: {
8680b57cec5SDimitry Andric     // Legalize bitwise operation:
8690b57cec5SDimitry Andric     // A = BinOp<Ty> B, C
8700b57cec5SDimitry Andric     // into:
8710b57cec5SDimitry Andric     // B1, ..., BN = G_UNMERGE_VALUES B
8720b57cec5SDimitry Andric     // C1, ..., CN = G_UNMERGE_VALUES C
8730b57cec5SDimitry Andric     // A1 = BinOp<Ty/N> B1, C2
8740b57cec5SDimitry Andric     // ...
8750b57cec5SDimitry Andric     // AN = BinOp<Ty/N> BN, CN
8760b57cec5SDimitry Andric     // A = G_MERGE_VALUES A1, ..., AN
8770b57cec5SDimitry Andric     return narrowScalarBasic(MI, TypeIdx, NarrowTy);
8780b57cec5SDimitry Andric   }
8790b57cec5SDimitry Andric   case TargetOpcode::G_SHL:
8800b57cec5SDimitry Andric   case TargetOpcode::G_LSHR:
8810b57cec5SDimitry Andric   case TargetOpcode::G_ASHR:
8820b57cec5SDimitry Andric     return narrowScalarShift(MI, TypeIdx, NarrowTy);
8830b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
8840b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
8850b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
8860b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
8870b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP:
8880b57cec5SDimitry Andric     if (TypeIdx != 0)
8890b57cec5SDimitry Andric       return UnableToLegalize; // TODO
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric     Observer.changingInstr(MI);
8920b57cec5SDimitry Andric     narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT);
8930b57cec5SDimitry Andric     Observer.changedInstr(MI);
8940b57cec5SDimitry Andric     return Legalized;
8950b57cec5SDimitry Andric   case TargetOpcode::G_INTTOPTR:
8960b57cec5SDimitry Andric     if (TypeIdx != 1)
8970b57cec5SDimitry Andric       return UnableToLegalize;
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric     Observer.changingInstr(MI);
9000b57cec5SDimitry Andric     narrowScalarSrc(MI, NarrowTy, 1);
9010b57cec5SDimitry Andric     Observer.changedInstr(MI);
9020b57cec5SDimitry Andric     return Legalized;
9030b57cec5SDimitry Andric   case TargetOpcode::G_PTRTOINT:
9040b57cec5SDimitry Andric     if (TypeIdx != 0)
9050b57cec5SDimitry Andric       return UnableToLegalize;
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric     Observer.changingInstr(MI);
9080b57cec5SDimitry Andric     narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT);
9090b57cec5SDimitry Andric     Observer.changedInstr(MI);
9100b57cec5SDimitry Andric     return Legalized;
9110b57cec5SDimitry Andric   case TargetOpcode::G_PHI: {
9120b57cec5SDimitry Andric     unsigned NumParts = SizeOp0 / NarrowSize;
9130b57cec5SDimitry Andric     SmallVector<Register, 2> DstRegs;
9140b57cec5SDimitry Andric     SmallVector<SmallVector<Register, 2>, 2> SrcRegs;
9150b57cec5SDimitry Andric     DstRegs.resize(NumParts);
9160b57cec5SDimitry Andric     SrcRegs.resize(MI.getNumOperands() / 2);
9170b57cec5SDimitry Andric     Observer.changingInstr(MI);
9180b57cec5SDimitry Andric     for (unsigned i = 1; i < MI.getNumOperands(); i += 2) {
9190b57cec5SDimitry Andric       MachineBasicBlock &OpMBB = *MI.getOperand(i + 1).getMBB();
9200b57cec5SDimitry Andric       MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
9210b57cec5SDimitry Andric       extractParts(MI.getOperand(i).getReg(), NarrowTy, NumParts,
9220b57cec5SDimitry Andric                    SrcRegs[i / 2]);
9230b57cec5SDimitry Andric     }
9240b57cec5SDimitry Andric     MachineBasicBlock &MBB = *MI.getParent();
9250b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MBB, MI);
9260b57cec5SDimitry Andric     for (unsigned i = 0; i < NumParts; ++i) {
9270b57cec5SDimitry Andric       DstRegs[i] = MRI.createGenericVirtualRegister(NarrowTy);
9280b57cec5SDimitry Andric       MachineInstrBuilder MIB =
9290b57cec5SDimitry Andric           MIRBuilder.buildInstr(TargetOpcode::G_PHI).addDef(DstRegs[i]);
9300b57cec5SDimitry Andric       for (unsigned j = 1; j < MI.getNumOperands(); j += 2)
9310b57cec5SDimitry Andric         MIB.addUse(SrcRegs[j / 2][i]).add(MI.getOperand(j + 1));
9320b57cec5SDimitry Andric     }
9338bcb0991SDimitry Andric     MIRBuilder.setInsertPt(MBB, MBB.getFirstNonPHI());
9340b57cec5SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), DstRegs);
9350b57cec5SDimitry Andric     Observer.changedInstr(MI);
9360b57cec5SDimitry Andric     MI.eraseFromParent();
9370b57cec5SDimitry Andric     return Legalized;
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT_VECTOR_ELT:
9400b57cec5SDimitry Andric   case TargetOpcode::G_INSERT_VECTOR_ELT: {
9410b57cec5SDimitry Andric     if (TypeIdx != 2)
9420b57cec5SDimitry Andric       return UnableToLegalize;
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric     int OpIdx = MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT ? 2 : 3;
9450b57cec5SDimitry Andric     Observer.changingInstr(MI);
9460b57cec5SDimitry Andric     narrowScalarSrc(MI, NarrowTy, OpIdx);
9470b57cec5SDimitry Andric     Observer.changedInstr(MI);
9480b57cec5SDimitry Andric     return Legalized;
9490b57cec5SDimitry Andric   }
9500b57cec5SDimitry Andric   case TargetOpcode::G_ICMP: {
9510b57cec5SDimitry Andric     uint64_t SrcSize = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
9520b57cec5SDimitry Andric     if (NarrowSize * 2 != SrcSize)
9530b57cec5SDimitry Andric       return UnableToLegalize;
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric     Observer.changingInstr(MI);
9560b57cec5SDimitry Andric     Register LHSL = MRI.createGenericVirtualRegister(NarrowTy);
9570b57cec5SDimitry Andric     Register LHSH = MRI.createGenericVirtualRegister(NarrowTy);
9580b57cec5SDimitry Andric     MIRBuilder.buildUnmerge({LHSL, LHSH}, MI.getOperand(2).getReg());
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric     Register RHSL = MRI.createGenericVirtualRegister(NarrowTy);
9610b57cec5SDimitry Andric     Register RHSH = MRI.createGenericVirtualRegister(NarrowTy);
9620b57cec5SDimitry Andric     MIRBuilder.buildUnmerge({RHSL, RHSH}, MI.getOperand(3).getReg());
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric     CmpInst::Predicate Pred =
9650b57cec5SDimitry Andric         static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
9668bcb0991SDimitry Andric     LLT ResTy = MRI.getType(MI.getOperand(0).getReg());
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
9690b57cec5SDimitry Andric       MachineInstrBuilder XorL = MIRBuilder.buildXor(NarrowTy, LHSL, RHSL);
9700b57cec5SDimitry Andric       MachineInstrBuilder XorH = MIRBuilder.buildXor(NarrowTy, LHSH, RHSH);
9710b57cec5SDimitry Andric       MachineInstrBuilder Or = MIRBuilder.buildOr(NarrowTy, XorL, XorH);
9720b57cec5SDimitry Andric       MachineInstrBuilder Zero = MIRBuilder.buildConstant(NarrowTy, 0);
9730b57cec5SDimitry Andric       MIRBuilder.buildICmp(Pred, MI.getOperand(0).getReg(), Or, Zero);
9740b57cec5SDimitry Andric     } else {
9758bcb0991SDimitry Andric       MachineInstrBuilder CmpH = MIRBuilder.buildICmp(Pred, ResTy, LHSH, RHSH);
9760b57cec5SDimitry Andric       MachineInstrBuilder CmpHEQ =
9778bcb0991SDimitry Andric           MIRBuilder.buildICmp(CmpInst::Predicate::ICMP_EQ, ResTy, LHSH, RHSH);
9780b57cec5SDimitry Andric       MachineInstrBuilder CmpLU = MIRBuilder.buildICmp(
9798bcb0991SDimitry Andric           ICmpInst::getUnsignedPredicate(Pred), ResTy, LHSL, RHSL);
9800b57cec5SDimitry Andric       MIRBuilder.buildSelect(MI.getOperand(0).getReg(), CmpHEQ, CmpLU, CmpH);
9810b57cec5SDimitry Andric     }
9820b57cec5SDimitry Andric     Observer.changedInstr(MI);
9830b57cec5SDimitry Andric     MI.eraseFromParent();
9840b57cec5SDimitry Andric     return Legalized;
9850b57cec5SDimitry Andric   }
9868bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG: {
9878bcb0991SDimitry Andric     if (TypeIdx != 0)
9888bcb0991SDimitry Andric       return UnableToLegalize;
9898bcb0991SDimitry Andric 
9908bcb0991SDimitry Andric     if (!MI.getOperand(2).isImm())
9918bcb0991SDimitry Andric       return UnableToLegalize;
9928bcb0991SDimitry Andric     int64_t SizeInBits = MI.getOperand(2).getImm();
9938bcb0991SDimitry Andric 
9948bcb0991SDimitry Andric     // So long as the new type has more bits than the bits we're extending we
9958bcb0991SDimitry Andric     // don't need to break it apart.
9968bcb0991SDimitry Andric     if (NarrowTy.getScalarSizeInBits() >= SizeInBits) {
9978bcb0991SDimitry Andric       Observer.changingInstr(MI);
9988bcb0991SDimitry Andric       // We don't lose any non-extension bits by truncating the src and
9998bcb0991SDimitry Andric       // sign-extending the dst.
10008bcb0991SDimitry Andric       MachineOperand &MO1 = MI.getOperand(1);
10018bcb0991SDimitry Andric       auto TruncMIB = MIRBuilder.buildTrunc(NarrowTy, MO1.getReg());
10028bcb0991SDimitry Andric       MO1.setReg(TruncMIB->getOperand(0).getReg());
10038bcb0991SDimitry Andric 
10048bcb0991SDimitry Andric       MachineOperand &MO2 = MI.getOperand(0);
10058bcb0991SDimitry Andric       Register DstExt = MRI.createGenericVirtualRegister(NarrowTy);
10068bcb0991SDimitry Andric       MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
10078bcb0991SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_SEXT, {MO2.getReg()}, {DstExt});
10088bcb0991SDimitry Andric       MO2.setReg(DstExt);
10098bcb0991SDimitry Andric       Observer.changedInstr(MI);
10108bcb0991SDimitry Andric       return Legalized;
10118bcb0991SDimitry Andric     }
10128bcb0991SDimitry Andric 
10138bcb0991SDimitry Andric     // Break it apart. Components below the extension point are unmodified. The
10148bcb0991SDimitry Andric     // component containing the extension point becomes a narrower SEXT_INREG.
10158bcb0991SDimitry Andric     // Components above it are ashr'd from the component containing the
10168bcb0991SDimitry Andric     // extension point.
10178bcb0991SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
10188bcb0991SDimitry Andric       return UnableToLegalize;
10198bcb0991SDimitry Andric     int NumParts = SizeOp0 / NarrowSize;
10208bcb0991SDimitry Andric 
10218bcb0991SDimitry Andric     // List the registers where the destination will be scattered.
10228bcb0991SDimitry Andric     SmallVector<Register, 2> DstRegs;
10238bcb0991SDimitry Andric     // List the registers where the source will be split.
10248bcb0991SDimitry Andric     SmallVector<Register, 2> SrcRegs;
10258bcb0991SDimitry Andric 
10268bcb0991SDimitry Andric     // Create all the temporary registers.
10278bcb0991SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
10288bcb0991SDimitry Andric       Register SrcReg = MRI.createGenericVirtualRegister(NarrowTy);
10298bcb0991SDimitry Andric 
10308bcb0991SDimitry Andric       SrcRegs.push_back(SrcReg);
10318bcb0991SDimitry Andric     }
10328bcb0991SDimitry Andric 
10338bcb0991SDimitry Andric     // Explode the big arguments into smaller chunks.
10348bcb0991SDimitry Andric     MIRBuilder.buildUnmerge(SrcRegs, MI.getOperand(1).getReg());
10358bcb0991SDimitry Andric 
10368bcb0991SDimitry Andric     Register AshrCstReg =
10378bcb0991SDimitry Andric         MIRBuilder.buildConstant(NarrowTy, NarrowTy.getScalarSizeInBits() - 1)
10388bcb0991SDimitry Andric             ->getOperand(0)
10398bcb0991SDimitry Andric             .getReg();
10408bcb0991SDimitry Andric     Register FullExtensionReg = 0;
10418bcb0991SDimitry Andric     Register PartialExtensionReg = 0;
10428bcb0991SDimitry Andric 
10438bcb0991SDimitry Andric     // Do the operation on each small part.
10448bcb0991SDimitry Andric     for (int i = 0; i < NumParts; ++i) {
10458bcb0991SDimitry Andric       if ((i + 1) * NarrowTy.getScalarSizeInBits() < SizeInBits)
10468bcb0991SDimitry Andric         DstRegs.push_back(SrcRegs[i]);
10478bcb0991SDimitry Andric       else if (i * NarrowTy.getScalarSizeInBits() > SizeInBits) {
10488bcb0991SDimitry Andric         assert(PartialExtensionReg &&
10498bcb0991SDimitry Andric                "Expected to visit partial extension before full");
10508bcb0991SDimitry Andric         if (FullExtensionReg) {
10518bcb0991SDimitry Andric           DstRegs.push_back(FullExtensionReg);
10528bcb0991SDimitry Andric           continue;
10538bcb0991SDimitry Andric         }
10548bcb0991SDimitry Andric         DstRegs.push_back(MIRBuilder
10558bcb0991SDimitry Andric                               .buildInstr(TargetOpcode::G_ASHR, {NarrowTy},
10568bcb0991SDimitry Andric                                           {PartialExtensionReg, AshrCstReg})
10578bcb0991SDimitry Andric                               ->getOperand(0)
10588bcb0991SDimitry Andric                               .getReg());
10598bcb0991SDimitry Andric         FullExtensionReg = DstRegs.back();
10608bcb0991SDimitry Andric       } else {
10618bcb0991SDimitry Andric         DstRegs.push_back(
10628bcb0991SDimitry Andric             MIRBuilder
10638bcb0991SDimitry Andric                 .buildInstr(
10648bcb0991SDimitry Andric                     TargetOpcode::G_SEXT_INREG, {NarrowTy},
10658bcb0991SDimitry Andric                     {SrcRegs[i], SizeInBits % NarrowTy.getScalarSizeInBits()})
10668bcb0991SDimitry Andric                 ->getOperand(0)
10678bcb0991SDimitry Andric                 .getReg());
10688bcb0991SDimitry Andric         PartialExtensionReg = DstRegs.back();
10698bcb0991SDimitry Andric       }
10708bcb0991SDimitry Andric     }
10718bcb0991SDimitry Andric 
10728bcb0991SDimitry Andric     // Gather the destination registers into the final destination.
10738bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
10748bcb0991SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
10758bcb0991SDimitry Andric     MI.eraseFromParent();
10768bcb0991SDimitry Andric     return Legalized;
10778bcb0991SDimitry Andric   }
1078*480093f4SDimitry Andric   case TargetOpcode::G_BSWAP:
1079*480093f4SDimitry Andric   case TargetOpcode::G_BITREVERSE: {
1080*480093f4SDimitry Andric     if (SizeOp0 % NarrowSize != 0)
1081*480093f4SDimitry Andric       return UnableToLegalize;
1082*480093f4SDimitry Andric 
1083*480093f4SDimitry Andric     Observer.changingInstr(MI);
1084*480093f4SDimitry Andric     SmallVector<Register, 2> SrcRegs, DstRegs;
1085*480093f4SDimitry Andric     unsigned NumParts = SizeOp0 / NarrowSize;
1086*480093f4SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
1087*480093f4SDimitry Andric 
1088*480093f4SDimitry Andric     for (unsigned i = 0; i < NumParts; ++i) {
1089*480093f4SDimitry Andric       auto DstPart = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy},
1090*480093f4SDimitry Andric                                            {SrcRegs[NumParts - 1 - i]});
1091*480093f4SDimitry Andric       DstRegs.push_back(DstPart.getReg(0));
1092*480093f4SDimitry Andric     }
1093*480093f4SDimitry Andric 
1094*480093f4SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), DstRegs);
1095*480093f4SDimitry Andric 
1096*480093f4SDimitry Andric     Observer.changedInstr(MI);
1097*480093f4SDimitry Andric     MI.eraseFromParent();
1098*480093f4SDimitry Andric     return Legalized;
1099*480093f4SDimitry Andric   }
11000b57cec5SDimitry Andric   }
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy,
11040b57cec5SDimitry Andric                                      unsigned OpIdx, unsigned ExtOpcode) {
11050b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11060b57cec5SDimitry Andric   auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO.getReg()});
11070b57cec5SDimitry Andric   MO.setReg(ExtB->getOperand(0).getReg());
11080b57cec5SDimitry Andric }
11090b57cec5SDimitry Andric 
11100b57cec5SDimitry Andric void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy,
11110b57cec5SDimitry Andric                                       unsigned OpIdx) {
11120b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11130b57cec5SDimitry Andric   auto ExtB = MIRBuilder.buildInstr(TargetOpcode::G_TRUNC, {NarrowTy},
11140b57cec5SDimitry Andric                                     {MO.getReg()});
11150b57cec5SDimitry Andric   MO.setReg(ExtB->getOperand(0).getReg());
11160b57cec5SDimitry Andric }
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy,
11190b57cec5SDimitry Andric                                      unsigned OpIdx, unsigned TruncOpcode) {
11200b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11210b57cec5SDimitry Andric   Register DstExt = MRI.createGenericVirtualRegister(WideTy);
11220b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
11230b57cec5SDimitry Andric   MIRBuilder.buildInstr(TruncOpcode, {MO.getReg()}, {DstExt});
11240b57cec5SDimitry Andric   MO.setReg(DstExt);
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy,
11280b57cec5SDimitry Andric                                       unsigned OpIdx, unsigned ExtOpcode) {
11290b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11300b57cec5SDimitry Andric   Register DstTrunc = MRI.createGenericVirtualRegister(NarrowTy);
11310b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
11320b57cec5SDimitry Andric   MIRBuilder.buildInstr(ExtOpcode, {MO.getReg()}, {DstTrunc});
11330b57cec5SDimitry Andric   MO.setReg(DstTrunc);
11340b57cec5SDimitry Andric }
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorDst(MachineInstr &MI, LLT WideTy,
11370b57cec5SDimitry Andric                                             unsigned OpIdx) {
11380b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11390b57cec5SDimitry Andric   Register DstExt = MRI.createGenericVirtualRegister(WideTy);
11400b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
11410b57cec5SDimitry Andric   MIRBuilder.buildExtract(MO.getReg(), DstExt, 0);
11420b57cec5SDimitry Andric   MO.setReg(DstExt);
11430b57cec5SDimitry Andric }
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorSrc(MachineInstr &MI, LLT MoreTy,
11460b57cec5SDimitry Andric                                             unsigned OpIdx) {
11470b57cec5SDimitry Andric   MachineOperand &MO = MI.getOperand(OpIdx);
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric   LLT OldTy = MRI.getType(MO.getReg());
11500b57cec5SDimitry Andric   unsigned OldElts = OldTy.getNumElements();
11510b57cec5SDimitry Andric   unsigned NewElts = MoreTy.getNumElements();
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric   unsigned NumParts = NewElts / OldElts;
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   // Use concat_vectors if the result is a multiple of the number of elements.
11560b57cec5SDimitry Andric   if (NumParts * OldElts == NewElts) {
11570b57cec5SDimitry Andric     SmallVector<Register, 8> Parts;
11580b57cec5SDimitry Andric     Parts.push_back(MO.getReg());
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric     Register ImpDef = MIRBuilder.buildUndef(OldTy).getReg(0);
11610b57cec5SDimitry Andric     for (unsigned I = 1; I != NumParts; ++I)
11620b57cec5SDimitry Andric       Parts.push_back(ImpDef);
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric     auto Concat = MIRBuilder.buildConcatVectors(MoreTy, Parts);
11650b57cec5SDimitry Andric     MO.setReg(Concat.getReg(0));
11660b57cec5SDimitry Andric     return;
11670b57cec5SDimitry Andric   }
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric   Register MoreReg = MRI.createGenericVirtualRegister(MoreTy);
11700b57cec5SDimitry Andric   Register ImpDef = MIRBuilder.buildUndef(MoreTy).getReg(0);
11710b57cec5SDimitry Andric   MIRBuilder.buildInsert(MoreReg, ImpDef, MO.getReg(), 0);
11720b57cec5SDimitry Andric   MO.setReg(MoreReg);
11730b57cec5SDimitry Andric }
11740b57cec5SDimitry Andric 
11750b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
11760b57cec5SDimitry Andric LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx,
11770b57cec5SDimitry Andric                                         LLT WideTy) {
11780b57cec5SDimitry Andric   if (TypeIdx != 1)
11790b57cec5SDimitry Andric     return UnableToLegalize;
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
11820b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
11830b57cec5SDimitry Andric   if (DstTy.isVector())
11840b57cec5SDimitry Andric     return UnableToLegalize;
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric   Register Src1 = MI.getOperand(1).getReg();
11870b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src1);
11880b57cec5SDimitry Andric   const int DstSize = DstTy.getSizeInBits();
11890b57cec5SDimitry Andric   const int SrcSize = SrcTy.getSizeInBits();
11900b57cec5SDimitry Andric   const int WideSize = WideTy.getSizeInBits();
11910b57cec5SDimitry Andric   const int NumMerge = (DstSize + WideSize - 1) / WideSize;
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric   unsigned NumOps = MI.getNumOperands();
11940b57cec5SDimitry Andric   unsigned NumSrc = MI.getNumOperands() - 1;
11950b57cec5SDimitry Andric   unsigned PartSize = DstTy.getSizeInBits() / NumSrc;
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric   if (WideSize >= DstSize) {
11980b57cec5SDimitry Andric     // Directly pack the bits in the target type.
11990b57cec5SDimitry Andric     Register ResultReg = MIRBuilder.buildZExt(WideTy, Src1).getReg(0);
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric     for (unsigned I = 2; I != NumOps; ++I) {
12020b57cec5SDimitry Andric       const unsigned Offset = (I - 1) * PartSize;
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric       Register SrcReg = MI.getOperand(I).getReg();
12050b57cec5SDimitry Andric       assert(MRI.getType(SrcReg) == LLT::scalar(PartSize));
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric       auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg);
12080b57cec5SDimitry Andric 
12098bcb0991SDimitry Andric       Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg :
12100b57cec5SDimitry Andric         MRI.createGenericVirtualRegister(WideTy);
12110b57cec5SDimitry Andric 
12120b57cec5SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset);
12130b57cec5SDimitry Andric       auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt);
12140b57cec5SDimitry Andric       MIRBuilder.buildOr(NextResult, ResultReg, Shl);
12150b57cec5SDimitry Andric       ResultReg = NextResult;
12160b57cec5SDimitry Andric     }
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric     if (WideSize > DstSize)
12190b57cec5SDimitry Andric       MIRBuilder.buildTrunc(DstReg, ResultReg);
12208bcb0991SDimitry Andric     else if (DstTy.isPointer())
12218bcb0991SDimitry Andric       MIRBuilder.buildIntToPtr(DstReg, ResultReg);
12220b57cec5SDimitry Andric 
12230b57cec5SDimitry Andric     MI.eraseFromParent();
12240b57cec5SDimitry Andric     return Legalized;
12250b57cec5SDimitry Andric   }
12260b57cec5SDimitry Andric 
12270b57cec5SDimitry Andric   // Unmerge the original values to the GCD type, and recombine to the next
12280b57cec5SDimitry Andric   // multiple greater than the original type.
12290b57cec5SDimitry Andric   //
12300b57cec5SDimitry Andric   // %3:_(s12) = G_MERGE_VALUES %0:_(s4), %1:_(s4), %2:_(s4) -> s6
12310b57cec5SDimitry Andric   // %4:_(s2), %5:_(s2) = G_UNMERGE_VALUES %0
12320b57cec5SDimitry Andric   // %6:_(s2), %7:_(s2) = G_UNMERGE_VALUES %1
12330b57cec5SDimitry Andric   // %8:_(s2), %9:_(s2) = G_UNMERGE_VALUES %2
12340b57cec5SDimitry Andric   // %10:_(s6) = G_MERGE_VALUES %4, %5, %6
12350b57cec5SDimitry Andric   // %11:_(s6) = G_MERGE_VALUES %7, %8, %9
12360b57cec5SDimitry Andric   // %12:_(s12) = G_MERGE_VALUES %10, %11
12370b57cec5SDimitry Andric   //
12380b57cec5SDimitry Andric   // Padding with undef if necessary:
12390b57cec5SDimitry Andric   //
12400b57cec5SDimitry Andric   // %2:_(s8) = G_MERGE_VALUES %0:_(s4), %1:_(s4) -> s6
12410b57cec5SDimitry Andric   // %3:_(s2), %4:_(s2) = G_UNMERGE_VALUES %0
12420b57cec5SDimitry Andric   // %5:_(s2), %6:_(s2) = G_UNMERGE_VALUES %1
12430b57cec5SDimitry Andric   // %7:_(s2) = G_IMPLICIT_DEF
12440b57cec5SDimitry Andric   // %8:_(s6) = G_MERGE_VALUES %3, %4, %5
12450b57cec5SDimitry Andric   // %9:_(s6) = G_MERGE_VALUES %6, %7, %7
12460b57cec5SDimitry Andric   // %10:_(s12) = G_MERGE_VALUES %8, %9
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric   const int GCD = greatestCommonDivisor(SrcSize, WideSize);
12490b57cec5SDimitry Andric   LLT GCDTy = LLT::scalar(GCD);
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric   SmallVector<Register, 8> Parts;
12520b57cec5SDimitry Andric   SmallVector<Register, 8> NewMergeRegs;
12530b57cec5SDimitry Andric   SmallVector<Register, 8> Unmerges;
12540b57cec5SDimitry Andric   LLT WideDstTy = LLT::scalar(NumMerge * WideSize);
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric   // Decompose the original operands if they don't evenly divide.
12570b57cec5SDimitry Andric   for (int I = 1, E = MI.getNumOperands(); I != E; ++I) {
12580b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
12590b57cec5SDimitry Andric     if (GCD == SrcSize) {
12600b57cec5SDimitry Andric       Unmerges.push_back(SrcReg);
12610b57cec5SDimitry Andric     } else {
12620b57cec5SDimitry Andric       auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg);
12630b57cec5SDimitry Andric       for (int J = 0, JE = Unmerge->getNumOperands() - 1; J != JE; ++J)
12640b57cec5SDimitry Andric         Unmerges.push_back(Unmerge.getReg(J));
12650b57cec5SDimitry Andric     }
12660b57cec5SDimitry Andric   }
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric   // Pad with undef to the next size that is a multiple of the requested size.
12690b57cec5SDimitry Andric   if (static_cast<int>(Unmerges.size()) != NumMerge * WideSize) {
12700b57cec5SDimitry Andric     Register UndefReg = MIRBuilder.buildUndef(GCDTy).getReg(0);
12710b57cec5SDimitry Andric     for (int I = Unmerges.size(); I != NumMerge * WideSize; ++I)
12720b57cec5SDimitry Andric       Unmerges.push_back(UndefReg);
12730b57cec5SDimitry Andric   }
12740b57cec5SDimitry Andric 
12750b57cec5SDimitry Andric   const int PartsPerGCD = WideSize / GCD;
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   // Build merges of each piece.
12780b57cec5SDimitry Andric   ArrayRef<Register> Slicer(Unmerges);
12790b57cec5SDimitry Andric   for (int I = 0; I != NumMerge; ++I, Slicer = Slicer.drop_front(PartsPerGCD)) {
12800b57cec5SDimitry Andric     auto Merge = MIRBuilder.buildMerge(WideTy, Slicer.take_front(PartsPerGCD));
12810b57cec5SDimitry Andric     NewMergeRegs.push_back(Merge.getReg(0));
12820b57cec5SDimitry Andric   }
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric   // A truncate may be necessary if the requested type doesn't evenly divide the
12850b57cec5SDimitry Andric   // original result type.
12860b57cec5SDimitry Andric   if (DstTy.getSizeInBits() == WideDstTy.getSizeInBits()) {
12870b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, NewMergeRegs);
12880b57cec5SDimitry Andric   } else {
12890b57cec5SDimitry Andric     auto FinalMerge = MIRBuilder.buildMerge(WideDstTy, NewMergeRegs);
12900b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, FinalMerge.getReg(0));
12910b57cec5SDimitry Andric   }
12920b57cec5SDimitry Andric 
12930b57cec5SDimitry Andric   MI.eraseFromParent();
12940b57cec5SDimitry Andric   return Legalized;
12950b57cec5SDimitry Andric }
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
12980b57cec5SDimitry Andric LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx,
12990b57cec5SDimitry Andric                                           LLT WideTy) {
13000b57cec5SDimitry Andric   if (TypeIdx != 0)
13010b57cec5SDimitry Andric     return UnableToLegalize;
13020b57cec5SDimitry Andric 
13030b57cec5SDimitry Andric   unsigned NumDst = MI.getNumOperands() - 1;
13040b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(NumDst).getReg();
13050b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
13060b57cec5SDimitry Andric   if (!SrcTy.isScalar())
13070b57cec5SDimitry Andric     return UnableToLegalize;
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   Register Dst0Reg = MI.getOperand(0).getReg();
13100b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst0Reg);
13110b57cec5SDimitry Andric   if (!DstTy.isScalar())
13120b57cec5SDimitry Andric     return UnableToLegalize;
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric   unsigned NewSrcSize = NumDst * WideTy.getSizeInBits();
13150b57cec5SDimitry Andric   LLT NewSrcTy = LLT::scalar(NewSrcSize);
13160b57cec5SDimitry Andric   unsigned SizeDiff = WideTy.getSizeInBits() - DstTy.getSizeInBits();
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric   auto WideSrc = MIRBuilder.buildZExt(NewSrcTy, SrcReg);
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric   for (unsigned I = 1; I != NumDst; ++I) {
13210b57cec5SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(NewSrcTy, SizeDiff * I);
13220b57cec5SDimitry Andric     auto Shl = MIRBuilder.buildShl(NewSrcTy, WideSrc, ShiftAmt);
13230b57cec5SDimitry Andric     WideSrc = MIRBuilder.buildOr(NewSrcTy, WideSrc, Shl);
13240b57cec5SDimitry Andric   }
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric   Observer.changingInstr(MI);
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric   MI.getOperand(NumDst).setReg(WideSrc->getOperand(0).getReg());
13290b57cec5SDimitry Andric   for (unsigned I = 0; I != NumDst; ++I)
13300b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, I);
13310b57cec5SDimitry Andric 
13320b57cec5SDimitry Andric   Observer.changedInstr(MI);
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric   return Legalized;
13350b57cec5SDimitry Andric }
13360b57cec5SDimitry Andric 
13370b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
13380b57cec5SDimitry Andric LegalizerHelper::widenScalarExtract(MachineInstr &MI, unsigned TypeIdx,
13390b57cec5SDimitry Andric                                     LLT WideTy) {
13400b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
13410b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
13420b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
13450b57cec5SDimitry Andric   unsigned Offset = MI.getOperand(2).getImm();
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric   if (TypeIdx == 0) {
13480b57cec5SDimitry Andric     if (SrcTy.isVector() || DstTy.isVector())
13490b57cec5SDimitry Andric       return UnableToLegalize;
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric     SrcOp Src(SrcReg);
13520b57cec5SDimitry Andric     if (SrcTy.isPointer()) {
13530b57cec5SDimitry Andric       // Extracts from pointers can be handled only if they are really just
13540b57cec5SDimitry Andric       // simple integers.
13550b57cec5SDimitry Andric       const DataLayout &DL = MIRBuilder.getDataLayout();
13560b57cec5SDimitry Andric       if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace()))
13570b57cec5SDimitry Andric         return UnableToLegalize;
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric       LLT SrcAsIntTy = LLT::scalar(SrcTy.getSizeInBits());
13600b57cec5SDimitry Andric       Src = MIRBuilder.buildPtrToInt(SrcAsIntTy, Src);
13610b57cec5SDimitry Andric       SrcTy = SrcAsIntTy;
13620b57cec5SDimitry Andric     }
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric     if (DstTy.isPointer())
13650b57cec5SDimitry Andric       return UnableToLegalize;
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric     if (Offset == 0) {
13680b57cec5SDimitry Andric       // Avoid a shift in the degenerate case.
13690b57cec5SDimitry Andric       MIRBuilder.buildTrunc(DstReg,
13700b57cec5SDimitry Andric                             MIRBuilder.buildAnyExtOrTrunc(WideTy, Src));
13710b57cec5SDimitry Andric       MI.eraseFromParent();
13720b57cec5SDimitry Andric       return Legalized;
13730b57cec5SDimitry Andric     }
13740b57cec5SDimitry Andric 
13750b57cec5SDimitry Andric     // Do a shift in the source type.
13760b57cec5SDimitry Andric     LLT ShiftTy = SrcTy;
13770b57cec5SDimitry Andric     if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) {
13780b57cec5SDimitry Andric       Src = MIRBuilder.buildAnyExt(WideTy, Src);
13790b57cec5SDimitry Andric       ShiftTy = WideTy;
13800b57cec5SDimitry Andric     } else if (WideTy.getSizeInBits() > SrcTy.getSizeInBits())
13810b57cec5SDimitry Andric       return UnableToLegalize;
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric     auto LShr = MIRBuilder.buildLShr(
13840b57cec5SDimitry Andric       ShiftTy, Src, MIRBuilder.buildConstant(ShiftTy, Offset));
13850b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, LShr);
13860b57cec5SDimitry Andric     MI.eraseFromParent();
13870b57cec5SDimitry Andric     return Legalized;
13880b57cec5SDimitry Andric   }
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric   if (SrcTy.isScalar()) {
13910b57cec5SDimitry Andric     Observer.changingInstr(MI);
13920b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
13930b57cec5SDimitry Andric     Observer.changedInstr(MI);
13940b57cec5SDimitry Andric     return Legalized;
13950b57cec5SDimitry Andric   }
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   if (!SrcTy.isVector())
13980b57cec5SDimitry Andric     return UnableToLegalize;
13990b57cec5SDimitry Andric 
14000b57cec5SDimitry Andric   if (DstTy != SrcTy.getElementType())
14010b57cec5SDimitry Andric     return UnableToLegalize;
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric   if (Offset % SrcTy.getScalarSizeInBits() != 0)
14040b57cec5SDimitry Andric     return UnableToLegalize;
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric   Observer.changingInstr(MI);
14070b57cec5SDimitry Andric   widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
14080b57cec5SDimitry Andric 
14090b57cec5SDimitry Andric   MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) *
14100b57cec5SDimitry Andric                           Offset);
14110b57cec5SDimitry Andric   widenScalarDst(MI, WideTy.getScalarType(), 0);
14120b57cec5SDimitry Andric   Observer.changedInstr(MI);
14130b57cec5SDimitry Andric   return Legalized;
14140b57cec5SDimitry Andric }
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
14170b57cec5SDimitry Andric LegalizerHelper::widenScalarInsert(MachineInstr &MI, unsigned TypeIdx,
14180b57cec5SDimitry Andric                                    LLT WideTy) {
14190b57cec5SDimitry Andric   if (TypeIdx != 0)
14200b57cec5SDimitry Andric     return UnableToLegalize;
14210b57cec5SDimitry Andric   Observer.changingInstr(MI);
14220b57cec5SDimitry Andric   widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
14230b57cec5SDimitry Andric   widenScalarDst(MI, WideTy);
14240b57cec5SDimitry Andric   Observer.changedInstr(MI);
14250b57cec5SDimitry Andric   return Legalized;
14260b57cec5SDimitry Andric }
14270b57cec5SDimitry Andric 
14280b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
14290b57cec5SDimitry Andric LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
14300b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric   switch (MI.getOpcode()) {
14330b57cec5SDimitry Andric   default:
14340b57cec5SDimitry Andric     return UnableToLegalize;
14350b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
14360b57cec5SDimitry Andric     return widenScalarExtract(MI, TypeIdx, WideTy);
14370b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
14380b57cec5SDimitry Andric     return widenScalarInsert(MI, TypeIdx, WideTy);
14390b57cec5SDimitry Andric   case TargetOpcode::G_MERGE_VALUES:
14400b57cec5SDimitry Andric     return widenScalarMergeValues(MI, TypeIdx, WideTy);
14410b57cec5SDimitry Andric   case TargetOpcode::G_UNMERGE_VALUES:
14420b57cec5SDimitry Andric     return widenScalarUnmergeValues(MI, TypeIdx, WideTy);
14430b57cec5SDimitry Andric   case TargetOpcode::G_UADDO:
14440b57cec5SDimitry Andric   case TargetOpcode::G_USUBO: {
14450b57cec5SDimitry Andric     if (TypeIdx == 1)
14460b57cec5SDimitry Andric       return UnableToLegalize; // TODO
14470b57cec5SDimitry Andric     auto LHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy},
14480b57cec5SDimitry Andric                                          {MI.getOperand(2).getReg()});
14490b57cec5SDimitry Andric     auto RHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy},
14500b57cec5SDimitry Andric                                          {MI.getOperand(3).getReg()});
14510b57cec5SDimitry Andric     unsigned Opcode = MI.getOpcode() == TargetOpcode::G_UADDO
14520b57cec5SDimitry Andric                           ? TargetOpcode::G_ADD
14530b57cec5SDimitry Andric                           : TargetOpcode::G_SUB;
14540b57cec5SDimitry Andric     // Do the arithmetic in the larger type.
14550b57cec5SDimitry Andric     auto NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSZext, RHSZext});
14560b57cec5SDimitry Andric     LLT OrigTy = MRI.getType(MI.getOperand(0).getReg());
14570b57cec5SDimitry Andric     APInt Mask = APInt::getAllOnesValue(OrigTy.getSizeInBits());
14580b57cec5SDimitry Andric     auto AndOp = MIRBuilder.buildInstr(
14590b57cec5SDimitry Andric         TargetOpcode::G_AND, {WideTy},
14600b57cec5SDimitry Andric         {NewOp, MIRBuilder.buildConstant(WideTy, Mask.getZExtValue())});
14610b57cec5SDimitry Andric     // There is no overflow if the AndOp is the same as NewOp.
14620b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1).getReg(), NewOp,
14630b57cec5SDimitry Andric                          AndOp);
14640b57cec5SDimitry Andric     // Now trunc the NewOp to the original result.
14650b57cec5SDimitry Andric     MIRBuilder.buildTrunc(MI.getOperand(0).getReg(), NewOp);
14660b57cec5SDimitry Andric     MI.eraseFromParent();
14670b57cec5SDimitry Andric     return Legalized;
14680b57cec5SDimitry Andric   }
14690b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
14700b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
14710b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
14720b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
14730b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP: {
14740b57cec5SDimitry Andric     if (TypeIdx == 0) {
14750b57cec5SDimitry Andric       Observer.changingInstr(MI);
14760b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
14770b57cec5SDimitry Andric       Observer.changedInstr(MI);
14780b57cec5SDimitry Andric       return Legalized;
14790b57cec5SDimitry Andric     }
14800b57cec5SDimitry Andric 
14810b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric     // First ZEXT the input.
14840b57cec5SDimitry Andric     auto MIBSrc = MIRBuilder.buildZExt(WideTy, SrcReg);
14850b57cec5SDimitry Andric     LLT CurTy = MRI.getType(SrcReg);
14860b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_CTTZ) {
14870b57cec5SDimitry Andric       // The count is the same in the larger type except if the original
14880b57cec5SDimitry Andric       // value was zero.  This can be handled by setting the bit just off
14890b57cec5SDimitry Andric       // the top of the original type.
14900b57cec5SDimitry Andric       auto TopBit =
14910b57cec5SDimitry Andric           APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits());
14920b57cec5SDimitry Andric       MIBSrc = MIRBuilder.buildOr(
14930b57cec5SDimitry Andric         WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit));
14940b57cec5SDimitry Andric     }
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric     // Perform the operation at the larger size.
14970b57cec5SDimitry Andric     auto MIBNewOp = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, {MIBSrc});
14980b57cec5SDimitry Andric     // This is already the correct result for CTPOP and CTTZs
14990b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_CTLZ ||
15000b57cec5SDimitry Andric         MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) {
15010b57cec5SDimitry Andric       // The correct result is NewOp - (Difference in widety and current ty).
15020b57cec5SDimitry Andric       unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits();
15030b57cec5SDimitry Andric       MIBNewOp = MIRBuilder.buildInstr(
15040b57cec5SDimitry Andric           TargetOpcode::G_SUB, {WideTy},
15050b57cec5SDimitry Andric           {MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff)});
15060b57cec5SDimitry Andric     }
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric     MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp);
15090b57cec5SDimitry Andric     MI.eraseFromParent();
15100b57cec5SDimitry Andric     return Legalized;
15110b57cec5SDimitry Andric   }
15120b57cec5SDimitry Andric   case TargetOpcode::G_BSWAP: {
15130b57cec5SDimitry Andric     Observer.changingInstr(MI);
15140b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric     Register ShrReg = MRI.createGenericVirtualRegister(WideTy);
15170b57cec5SDimitry Andric     Register DstExt = MRI.createGenericVirtualRegister(WideTy);
15180b57cec5SDimitry Andric     Register ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy);
15190b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
15200b57cec5SDimitry Andric 
15210b57cec5SDimitry Andric     MI.getOperand(0).setReg(DstExt);
15220b57cec5SDimitry Andric 
15230b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
15240b57cec5SDimitry Andric 
15250b57cec5SDimitry Andric     LLT Ty = MRI.getType(DstReg);
15260b57cec5SDimitry Andric     unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits();
15270b57cec5SDimitry Andric     MIRBuilder.buildConstant(ShiftAmtReg, DiffBits);
15280b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_LSHR)
15290b57cec5SDimitry Andric       .addDef(ShrReg)
15300b57cec5SDimitry Andric       .addUse(DstExt)
15310b57cec5SDimitry Andric       .addUse(ShiftAmtReg);
15320b57cec5SDimitry Andric 
15330b57cec5SDimitry Andric     MIRBuilder.buildTrunc(DstReg, ShrReg);
15340b57cec5SDimitry Andric     Observer.changedInstr(MI);
15350b57cec5SDimitry Andric     return Legalized;
15360b57cec5SDimitry Andric   }
15378bcb0991SDimitry Andric   case TargetOpcode::G_BITREVERSE: {
15388bcb0991SDimitry Andric     Observer.changingInstr(MI);
15398bcb0991SDimitry Andric 
15408bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
15418bcb0991SDimitry Andric     LLT Ty = MRI.getType(DstReg);
15428bcb0991SDimitry Andric     unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits();
15438bcb0991SDimitry Andric 
15448bcb0991SDimitry Andric     Register DstExt = MRI.createGenericVirtualRegister(WideTy);
15458bcb0991SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
15468bcb0991SDimitry Andric     MI.getOperand(0).setReg(DstExt);
15478bcb0991SDimitry Andric     MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
15488bcb0991SDimitry Andric 
15498bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(WideTy, DiffBits);
15508bcb0991SDimitry Andric     auto Shift = MIRBuilder.buildLShr(WideTy, DstExt, ShiftAmt);
15518bcb0991SDimitry Andric     MIRBuilder.buildTrunc(DstReg, Shift);
15528bcb0991SDimitry Andric     Observer.changedInstr(MI);
15538bcb0991SDimitry Andric     return Legalized;
15548bcb0991SDimitry Andric   }
15550b57cec5SDimitry Andric   case TargetOpcode::G_ADD:
15560b57cec5SDimitry Andric   case TargetOpcode::G_AND:
15570b57cec5SDimitry Andric   case TargetOpcode::G_MUL:
15580b57cec5SDimitry Andric   case TargetOpcode::G_OR:
15590b57cec5SDimitry Andric   case TargetOpcode::G_XOR:
15600b57cec5SDimitry Andric   case TargetOpcode::G_SUB:
15610b57cec5SDimitry Andric     // Perform operation at larger width (any extension is fines here, high bits
15620b57cec5SDimitry Andric     // don't affect the result) and then truncate the result back to the
15630b57cec5SDimitry Andric     // original type.
15640b57cec5SDimitry Andric     Observer.changingInstr(MI);
15650b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
15660b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
15670b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
15680b57cec5SDimitry Andric     Observer.changedInstr(MI);
15690b57cec5SDimitry Andric     return Legalized;
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric   case TargetOpcode::G_SHL:
15720b57cec5SDimitry Andric     Observer.changingInstr(MI);
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric     if (TypeIdx == 0) {
15750b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
15760b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
15770b57cec5SDimitry Andric     } else {
15780b57cec5SDimitry Andric       assert(TypeIdx == 1);
15790b57cec5SDimitry Andric       // The "number of bits to shift" operand must preserve its value as an
15800b57cec5SDimitry Andric       // unsigned integer:
15810b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
15820b57cec5SDimitry Andric     }
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric     Observer.changedInstr(MI);
15850b57cec5SDimitry Andric     return Legalized;
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric   case TargetOpcode::G_SDIV:
15880b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
15890b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
15900b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
15910b57cec5SDimitry Andric     Observer.changingInstr(MI);
15920b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT);
15930b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
15940b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
15950b57cec5SDimitry Andric     Observer.changedInstr(MI);
15960b57cec5SDimitry Andric     return Legalized;
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric   case TargetOpcode::G_ASHR:
15990b57cec5SDimitry Andric   case TargetOpcode::G_LSHR:
16000b57cec5SDimitry Andric     Observer.changingInstr(MI);
16010b57cec5SDimitry Andric 
16020b57cec5SDimitry Andric     if (TypeIdx == 0) {
16030b57cec5SDimitry Andric       unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ?
16040b57cec5SDimitry Andric         TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, CvtOp);
16070b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
16080b57cec5SDimitry Andric     } else {
16090b57cec5SDimitry Andric       assert(TypeIdx == 1);
16100b57cec5SDimitry Andric       // The "number of bits to shift" operand must preserve its value as an
16110b57cec5SDimitry Andric       // unsigned integer:
16120b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
16130b57cec5SDimitry Andric     }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric     Observer.changedInstr(MI);
16160b57cec5SDimitry Andric     return Legalized;
16170b57cec5SDimitry Andric   case TargetOpcode::G_UDIV:
16180b57cec5SDimitry Andric   case TargetOpcode::G_UREM:
16190b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
16200b57cec5SDimitry Andric   case TargetOpcode::G_UMAX:
16210b57cec5SDimitry Andric     Observer.changingInstr(MI);
16220b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
16230b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT);
16240b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
16250b57cec5SDimitry Andric     Observer.changedInstr(MI);
16260b57cec5SDimitry Andric     return Legalized;
16270b57cec5SDimitry Andric 
16280b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
16290b57cec5SDimitry Andric     Observer.changingInstr(MI);
16300b57cec5SDimitry Andric     if (TypeIdx == 0) {
16310b57cec5SDimitry Andric       // Perform operation at larger width (any extension is fine here, high
16320b57cec5SDimitry Andric       // bits don't affect the result) and then truncate the result back to the
16330b57cec5SDimitry Andric       // original type.
16340b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
16350b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT);
16360b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
16370b57cec5SDimitry Andric     } else {
16380b57cec5SDimitry Andric       bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector();
16390b57cec5SDimitry Andric       // Explicit extension is required here since high bits affect the result.
16400b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false));
16410b57cec5SDimitry Andric     }
16420b57cec5SDimitry Andric     Observer.changedInstr(MI);
16430b57cec5SDimitry Andric     return Legalized;
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   case TargetOpcode::G_FPTOSI:
16460b57cec5SDimitry Andric   case TargetOpcode::G_FPTOUI:
16470b57cec5SDimitry Andric     Observer.changingInstr(MI);
16488bcb0991SDimitry Andric 
16498bcb0991SDimitry Andric     if (TypeIdx == 0)
16500b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
16518bcb0991SDimitry Andric     else
16528bcb0991SDimitry Andric       widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT);
16538bcb0991SDimitry Andric 
16540b57cec5SDimitry Andric     Observer.changedInstr(MI);
16550b57cec5SDimitry Andric     return Legalized;
16560b57cec5SDimitry Andric   case TargetOpcode::G_SITOFP:
16570b57cec5SDimitry Andric     if (TypeIdx != 1)
16580b57cec5SDimitry Andric       return UnableToLegalize;
16590b57cec5SDimitry Andric     Observer.changingInstr(MI);
16600b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT);
16610b57cec5SDimitry Andric     Observer.changedInstr(MI);
16620b57cec5SDimitry Andric     return Legalized;
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric   case TargetOpcode::G_UITOFP:
16650b57cec5SDimitry Andric     if (TypeIdx != 1)
16660b57cec5SDimitry Andric       return UnableToLegalize;
16670b57cec5SDimitry Andric     Observer.changingInstr(MI);
16680b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
16690b57cec5SDimitry Andric     Observer.changedInstr(MI);
16700b57cec5SDimitry Andric     return Legalized;
16710b57cec5SDimitry Andric 
16720b57cec5SDimitry Andric   case TargetOpcode::G_LOAD:
16730b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD:
16740b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD:
16750b57cec5SDimitry Andric     Observer.changingInstr(MI);
16760b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
16770b57cec5SDimitry Andric     Observer.changedInstr(MI);
16780b57cec5SDimitry Andric     return Legalized;
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric   case TargetOpcode::G_STORE: {
16810b57cec5SDimitry Andric     if (TypeIdx != 0)
16820b57cec5SDimitry Andric       return UnableToLegalize;
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric     LLT Ty = MRI.getType(MI.getOperand(0).getReg());
16850b57cec5SDimitry Andric     if (!isPowerOf2_32(Ty.getSizeInBits()))
16860b57cec5SDimitry Andric       return UnableToLegalize;
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric     Observer.changingInstr(MI);
16890b57cec5SDimitry Andric 
16900b57cec5SDimitry Andric     unsigned ExtType = Ty.getScalarSizeInBits() == 1 ?
16910b57cec5SDimitry Andric       TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT;
16920b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 0, ExtType);
16930b57cec5SDimitry Andric 
16940b57cec5SDimitry Andric     Observer.changedInstr(MI);
16950b57cec5SDimitry Andric     return Legalized;
16960b57cec5SDimitry Andric   }
16970b57cec5SDimitry Andric   case TargetOpcode::G_CONSTANT: {
16980b57cec5SDimitry Andric     MachineOperand &SrcMO = MI.getOperand(1);
16990b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
1700*480093f4SDimitry Andric     unsigned ExtOpc = LI.getExtOpcodeForWideningConstant(
1701*480093f4SDimitry Andric         MRI.getType(MI.getOperand(0).getReg()));
1702*480093f4SDimitry Andric     assert((ExtOpc == TargetOpcode::G_ZEXT || ExtOpc == TargetOpcode::G_SEXT ||
1703*480093f4SDimitry Andric             ExtOpc == TargetOpcode::G_ANYEXT) &&
1704*480093f4SDimitry Andric            "Illegal Extend");
1705*480093f4SDimitry Andric     const APInt &SrcVal = SrcMO.getCImm()->getValue();
1706*480093f4SDimitry Andric     const APInt &Val = (ExtOpc == TargetOpcode::G_SEXT)
1707*480093f4SDimitry Andric                            ? SrcVal.sext(WideTy.getSizeInBits())
1708*480093f4SDimitry Andric                            : SrcVal.zext(WideTy.getSizeInBits());
17090b57cec5SDimitry Andric     Observer.changingInstr(MI);
17100b57cec5SDimitry Andric     SrcMO.setCImm(ConstantInt::get(Ctx, Val));
17110b57cec5SDimitry Andric 
17120b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
17130b57cec5SDimitry Andric     Observer.changedInstr(MI);
17140b57cec5SDimitry Andric     return Legalized;
17150b57cec5SDimitry Andric   }
17160b57cec5SDimitry Andric   case TargetOpcode::G_FCONSTANT: {
17170b57cec5SDimitry Andric     MachineOperand &SrcMO = MI.getOperand(1);
17180b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
17190b57cec5SDimitry Andric     APFloat Val = SrcMO.getFPImm()->getValueAPF();
17200b57cec5SDimitry Andric     bool LosesInfo;
17210b57cec5SDimitry Andric     switch (WideTy.getSizeInBits()) {
17220b57cec5SDimitry Andric     case 32:
17230b57cec5SDimitry Andric       Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
17240b57cec5SDimitry Andric                   &LosesInfo);
17250b57cec5SDimitry Andric       break;
17260b57cec5SDimitry Andric     case 64:
17270b57cec5SDimitry Andric       Val.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
17280b57cec5SDimitry Andric                   &LosesInfo);
17290b57cec5SDimitry Andric       break;
17300b57cec5SDimitry Andric     default:
17310b57cec5SDimitry Andric       return UnableToLegalize;
17320b57cec5SDimitry Andric     }
17330b57cec5SDimitry Andric 
17340b57cec5SDimitry Andric     assert(!LosesInfo && "extend should always be lossless");
17350b57cec5SDimitry Andric 
17360b57cec5SDimitry Andric     Observer.changingInstr(MI);
17370b57cec5SDimitry Andric     SrcMO.setFPImm(ConstantFP::get(Ctx, Val));
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC);
17400b57cec5SDimitry Andric     Observer.changedInstr(MI);
17410b57cec5SDimitry Andric     return Legalized;
17420b57cec5SDimitry Andric   }
17430b57cec5SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF: {
17440b57cec5SDimitry Andric     Observer.changingInstr(MI);
17450b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
17460b57cec5SDimitry Andric     Observer.changedInstr(MI);
17470b57cec5SDimitry Andric     return Legalized;
17480b57cec5SDimitry Andric   }
17490b57cec5SDimitry Andric   case TargetOpcode::G_BRCOND:
17500b57cec5SDimitry Andric     Observer.changingInstr(MI);
17510b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 0, MIRBuilder.getBoolExtOp(false, false));
17520b57cec5SDimitry Andric     Observer.changedInstr(MI);
17530b57cec5SDimitry Andric     return Legalized;
17540b57cec5SDimitry Andric 
17550b57cec5SDimitry Andric   case TargetOpcode::G_FCMP:
17560b57cec5SDimitry Andric     Observer.changingInstr(MI);
17570b57cec5SDimitry Andric     if (TypeIdx == 0)
17580b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
17590b57cec5SDimitry Andric     else {
17600b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT);
17610b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT);
17620b57cec5SDimitry Andric     }
17630b57cec5SDimitry Andric     Observer.changedInstr(MI);
17640b57cec5SDimitry Andric     return Legalized;
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric   case TargetOpcode::G_ICMP:
17670b57cec5SDimitry Andric     Observer.changingInstr(MI);
17680b57cec5SDimitry Andric     if (TypeIdx == 0)
17690b57cec5SDimitry Andric       widenScalarDst(MI, WideTy);
17700b57cec5SDimitry Andric     else {
17710b57cec5SDimitry Andric       unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>(
17720b57cec5SDimitry Andric                                MI.getOperand(1).getPredicate()))
17730b57cec5SDimitry Andric                                ? TargetOpcode::G_SEXT
17740b57cec5SDimitry Andric                                : TargetOpcode::G_ZEXT;
17750b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 2, ExtOpcode);
17760b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, 3, ExtOpcode);
17770b57cec5SDimitry Andric     }
17780b57cec5SDimitry Andric     Observer.changedInstr(MI);
17790b57cec5SDimitry Andric     return Legalized;
17800b57cec5SDimitry Andric 
1781*480093f4SDimitry Andric   case TargetOpcode::G_PTR_ADD:
1782*480093f4SDimitry Andric     assert(TypeIdx == 1 && "unable to legalize pointer of G_PTR_ADD");
17830b57cec5SDimitry Andric     Observer.changingInstr(MI);
17840b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
17850b57cec5SDimitry Andric     Observer.changedInstr(MI);
17860b57cec5SDimitry Andric     return Legalized;
17870b57cec5SDimitry Andric 
17880b57cec5SDimitry Andric   case TargetOpcode::G_PHI: {
17890b57cec5SDimitry Andric     assert(TypeIdx == 0 && "Expecting only Idx 0");
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric     Observer.changingInstr(MI);
17920b57cec5SDimitry Andric     for (unsigned I = 1; I < MI.getNumOperands(); I += 2) {
17930b57cec5SDimitry Andric       MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
17940b57cec5SDimitry Andric       MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
17950b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT);
17960b57cec5SDimitry Andric     }
17970b57cec5SDimitry Andric 
17980b57cec5SDimitry Andric     MachineBasicBlock &MBB = *MI.getParent();
17990b57cec5SDimitry Andric     MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI());
18000b57cec5SDimitry Andric     widenScalarDst(MI, WideTy);
18010b57cec5SDimitry Andric     Observer.changedInstr(MI);
18020b57cec5SDimitry Andric     return Legalized;
18030b57cec5SDimitry Andric   }
18040b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
18050b57cec5SDimitry Andric     if (TypeIdx == 0) {
18060b57cec5SDimitry Andric       Register VecReg = MI.getOperand(1).getReg();
18070b57cec5SDimitry Andric       LLT VecTy = MRI.getType(VecReg);
18080b57cec5SDimitry Andric       Observer.changingInstr(MI);
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric       widenScalarSrc(MI, LLT::vector(VecTy.getNumElements(),
18110b57cec5SDimitry Andric                                      WideTy.getSizeInBits()),
18120b57cec5SDimitry Andric                      1, TargetOpcode::G_SEXT);
18130b57cec5SDimitry Andric 
18140b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
18150b57cec5SDimitry Andric       Observer.changedInstr(MI);
18160b57cec5SDimitry Andric       return Legalized;
18170b57cec5SDimitry Andric     }
18180b57cec5SDimitry Andric 
18190b57cec5SDimitry Andric     if (TypeIdx != 2)
18200b57cec5SDimitry Andric       return UnableToLegalize;
18210b57cec5SDimitry Andric     Observer.changingInstr(MI);
1822*480093f4SDimitry Andric     // TODO: Probably should be zext
18230b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT);
18240b57cec5SDimitry Andric     Observer.changedInstr(MI);
18250b57cec5SDimitry Andric     return Legalized;
18260b57cec5SDimitry Andric   }
1827*480093f4SDimitry Andric   case TargetOpcode::G_INSERT_VECTOR_ELT: {
1828*480093f4SDimitry Andric     if (TypeIdx == 1) {
1829*480093f4SDimitry Andric       Observer.changingInstr(MI);
1830*480093f4SDimitry Andric 
1831*480093f4SDimitry Andric       Register VecReg = MI.getOperand(1).getReg();
1832*480093f4SDimitry Andric       LLT VecTy = MRI.getType(VecReg);
1833*480093f4SDimitry Andric       LLT WideVecTy = LLT::vector(VecTy.getNumElements(), WideTy);
1834*480093f4SDimitry Andric 
1835*480093f4SDimitry Andric       widenScalarSrc(MI, WideVecTy, 1, TargetOpcode::G_ANYEXT);
1836*480093f4SDimitry Andric       widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT);
1837*480093f4SDimitry Andric       widenScalarDst(MI, WideVecTy, 0);
1838*480093f4SDimitry Andric       Observer.changedInstr(MI);
1839*480093f4SDimitry Andric       return Legalized;
1840*480093f4SDimitry Andric     }
1841*480093f4SDimitry Andric 
1842*480093f4SDimitry Andric     if (TypeIdx == 2) {
1843*480093f4SDimitry Andric       Observer.changingInstr(MI);
1844*480093f4SDimitry Andric       // TODO: Probably should be zext
1845*480093f4SDimitry Andric       widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT);
1846*480093f4SDimitry Andric       Observer.changedInstr(MI);
1847*480093f4SDimitry Andric     }
1848*480093f4SDimitry Andric 
1849*480093f4SDimitry Andric     return Legalized;
1850*480093f4SDimitry Andric   }
18510b57cec5SDimitry Andric   case TargetOpcode::G_FADD:
18520b57cec5SDimitry Andric   case TargetOpcode::G_FMUL:
18530b57cec5SDimitry Andric   case TargetOpcode::G_FSUB:
18540b57cec5SDimitry Andric   case TargetOpcode::G_FMA:
18558bcb0991SDimitry Andric   case TargetOpcode::G_FMAD:
18560b57cec5SDimitry Andric   case TargetOpcode::G_FNEG:
18570b57cec5SDimitry Andric   case TargetOpcode::G_FABS:
18580b57cec5SDimitry Andric   case TargetOpcode::G_FCANONICALIZE:
18590b57cec5SDimitry Andric   case TargetOpcode::G_FMINNUM:
18600b57cec5SDimitry Andric   case TargetOpcode::G_FMAXNUM:
18610b57cec5SDimitry Andric   case TargetOpcode::G_FMINNUM_IEEE:
18620b57cec5SDimitry Andric   case TargetOpcode::G_FMAXNUM_IEEE:
18630b57cec5SDimitry Andric   case TargetOpcode::G_FMINIMUM:
18640b57cec5SDimitry Andric   case TargetOpcode::G_FMAXIMUM:
18650b57cec5SDimitry Andric   case TargetOpcode::G_FDIV:
18660b57cec5SDimitry Andric   case TargetOpcode::G_FREM:
18670b57cec5SDimitry Andric   case TargetOpcode::G_FCEIL:
18680b57cec5SDimitry Andric   case TargetOpcode::G_FFLOOR:
18690b57cec5SDimitry Andric   case TargetOpcode::G_FCOS:
18700b57cec5SDimitry Andric   case TargetOpcode::G_FSIN:
18710b57cec5SDimitry Andric   case TargetOpcode::G_FLOG10:
18720b57cec5SDimitry Andric   case TargetOpcode::G_FLOG:
18730b57cec5SDimitry Andric   case TargetOpcode::G_FLOG2:
18740b57cec5SDimitry Andric   case TargetOpcode::G_FRINT:
18750b57cec5SDimitry Andric   case TargetOpcode::G_FNEARBYINT:
18760b57cec5SDimitry Andric   case TargetOpcode::G_FSQRT:
18770b57cec5SDimitry Andric   case TargetOpcode::G_FEXP:
18780b57cec5SDimitry Andric   case TargetOpcode::G_FEXP2:
18790b57cec5SDimitry Andric   case TargetOpcode::G_FPOW:
18800b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC_TRUNC:
18810b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC_ROUND:
18820b57cec5SDimitry Andric     assert(TypeIdx == 0);
18830b57cec5SDimitry Andric     Observer.changingInstr(MI);
18840b57cec5SDimitry Andric 
18850b57cec5SDimitry Andric     for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I)
18860b57cec5SDimitry Andric       widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT);
18870b57cec5SDimitry Andric 
18880b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC);
18890b57cec5SDimitry Andric     Observer.changedInstr(MI);
18900b57cec5SDimitry Andric     return Legalized;
18910b57cec5SDimitry Andric   case TargetOpcode::G_INTTOPTR:
18920b57cec5SDimitry Andric     if (TypeIdx != 1)
18930b57cec5SDimitry Andric       return UnableToLegalize;
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric     Observer.changingInstr(MI);
18960b57cec5SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT);
18970b57cec5SDimitry Andric     Observer.changedInstr(MI);
18980b57cec5SDimitry Andric     return Legalized;
18990b57cec5SDimitry Andric   case TargetOpcode::G_PTRTOINT:
19000b57cec5SDimitry Andric     if (TypeIdx != 0)
19010b57cec5SDimitry Andric       return UnableToLegalize;
19020b57cec5SDimitry Andric 
19030b57cec5SDimitry Andric     Observer.changingInstr(MI);
19040b57cec5SDimitry Andric     widenScalarDst(MI, WideTy, 0);
19050b57cec5SDimitry Andric     Observer.changedInstr(MI);
19060b57cec5SDimitry Andric     return Legalized;
19070b57cec5SDimitry Andric   case TargetOpcode::G_BUILD_VECTOR: {
19080b57cec5SDimitry Andric     Observer.changingInstr(MI);
19090b57cec5SDimitry Andric 
19100b57cec5SDimitry Andric     const LLT WideEltTy = TypeIdx == 1 ? WideTy : WideTy.getElementType();
19110b57cec5SDimitry Andric     for (int I = 1, E = MI.getNumOperands(); I != E; ++I)
19120b57cec5SDimitry Andric       widenScalarSrc(MI, WideEltTy, I, TargetOpcode::G_ANYEXT);
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric     // Avoid changing the result vector type if the source element type was
19150b57cec5SDimitry Andric     // requested.
19160b57cec5SDimitry Andric     if (TypeIdx == 1) {
19170b57cec5SDimitry Andric       auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
19180b57cec5SDimitry Andric       MI.setDesc(TII.get(TargetOpcode::G_BUILD_VECTOR_TRUNC));
19190b57cec5SDimitry Andric     } else {
19200b57cec5SDimitry Andric       widenScalarDst(MI, WideTy, 0);
19210b57cec5SDimitry Andric     }
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric     Observer.changedInstr(MI);
19240b57cec5SDimitry Andric     return Legalized;
19250b57cec5SDimitry Andric   }
19268bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG:
19278bcb0991SDimitry Andric     if (TypeIdx != 0)
19288bcb0991SDimitry Andric       return UnableToLegalize;
19298bcb0991SDimitry Andric 
19308bcb0991SDimitry Andric     Observer.changingInstr(MI);
19318bcb0991SDimitry Andric     widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
19328bcb0991SDimitry Andric     widenScalarDst(MI, WideTy, 0, TargetOpcode::G_TRUNC);
19338bcb0991SDimitry Andric     Observer.changedInstr(MI);
19348bcb0991SDimitry Andric     return Legalized;
19350b57cec5SDimitry Andric   }
19360b57cec5SDimitry Andric }
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
19390b57cec5SDimitry Andric LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
19400b57cec5SDimitry Andric   using namespace TargetOpcode;
19410b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric   switch(MI.getOpcode()) {
19440b57cec5SDimitry Andric   default:
19450b57cec5SDimitry Andric     return UnableToLegalize;
19460b57cec5SDimitry Andric   case TargetOpcode::G_SREM:
19470b57cec5SDimitry Andric   case TargetOpcode::G_UREM: {
19480b57cec5SDimitry Andric     Register QuotReg = MRI.createGenericVirtualRegister(Ty);
19490b57cec5SDimitry Andric     MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV)
19500b57cec5SDimitry Andric         .addDef(QuotReg)
19510b57cec5SDimitry Andric         .addUse(MI.getOperand(1).getReg())
19520b57cec5SDimitry Andric         .addUse(MI.getOperand(2).getReg());
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric     Register ProdReg = MRI.createGenericVirtualRegister(Ty);
19550b57cec5SDimitry Andric     MIRBuilder.buildMul(ProdReg, QuotReg, MI.getOperand(2).getReg());
19560b57cec5SDimitry Andric     MIRBuilder.buildSub(MI.getOperand(0).getReg(), MI.getOperand(1).getReg(),
19570b57cec5SDimitry Andric                         ProdReg);
19580b57cec5SDimitry Andric     MI.eraseFromParent();
19590b57cec5SDimitry Andric     return Legalized;
19600b57cec5SDimitry Andric   }
19618bcb0991SDimitry Andric   case TargetOpcode::G_SADDO:
19628bcb0991SDimitry Andric   case TargetOpcode::G_SSUBO:
19638bcb0991SDimitry Andric     return lowerSADDO_SSUBO(MI);
19640b57cec5SDimitry Andric   case TargetOpcode::G_SMULO:
19650b57cec5SDimitry Andric   case TargetOpcode::G_UMULO: {
19660b57cec5SDimitry Andric     // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the
19670b57cec5SDimitry Andric     // result.
19680b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
19690b57cec5SDimitry Andric     Register Overflow = MI.getOperand(1).getReg();
19700b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
19710b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric     MIRBuilder.buildMul(Res, LHS, RHS);
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric     unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO
19760b57cec5SDimitry Andric                           ? TargetOpcode::G_SMULH
19770b57cec5SDimitry Andric                           : TargetOpcode::G_UMULH;
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric     Register HiPart = MRI.createGenericVirtualRegister(Ty);
19800b57cec5SDimitry Andric     MIRBuilder.buildInstr(Opcode)
19810b57cec5SDimitry Andric       .addDef(HiPart)
19820b57cec5SDimitry Andric       .addUse(LHS)
19830b57cec5SDimitry Andric       .addUse(RHS);
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric     Register Zero = MRI.createGenericVirtualRegister(Ty);
19860b57cec5SDimitry Andric     MIRBuilder.buildConstant(Zero, 0);
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric     // For *signed* multiply, overflow is detected by checking:
19890b57cec5SDimitry Andric     // (hi != (lo >> bitwidth-1))
19900b57cec5SDimitry Andric     if (Opcode == TargetOpcode::G_SMULH) {
19910b57cec5SDimitry Andric       Register Shifted = MRI.createGenericVirtualRegister(Ty);
19920b57cec5SDimitry Andric       Register ShiftAmt = MRI.createGenericVirtualRegister(Ty);
19930b57cec5SDimitry Andric       MIRBuilder.buildConstant(ShiftAmt, Ty.getSizeInBits() - 1);
19940b57cec5SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_ASHR)
19950b57cec5SDimitry Andric         .addDef(Shifted)
19960b57cec5SDimitry Andric         .addUse(Res)
19970b57cec5SDimitry Andric         .addUse(ShiftAmt);
19980b57cec5SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted);
19990b57cec5SDimitry Andric     } else {
20000b57cec5SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero);
20010b57cec5SDimitry Andric     }
20020b57cec5SDimitry Andric     MI.eraseFromParent();
20030b57cec5SDimitry Andric     return Legalized;
20040b57cec5SDimitry Andric   }
20050b57cec5SDimitry Andric   case TargetOpcode::G_FNEG: {
20060b57cec5SDimitry Andric     // TODO: Handle vector types once we are able to
20070b57cec5SDimitry Andric     // represent them.
20080b57cec5SDimitry Andric     if (Ty.isVector())
20090b57cec5SDimitry Andric       return UnableToLegalize;
20100b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
20110b57cec5SDimitry Andric     Type *ZeroTy;
20120b57cec5SDimitry Andric     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
20130b57cec5SDimitry Andric     switch (Ty.getSizeInBits()) {
20140b57cec5SDimitry Andric     case 16:
20150b57cec5SDimitry Andric       ZeroTy = Type::getHalfTy(Ctx);
20160b57cec5SDimitry Andric       break;
20170b57cec5SDimitry Andric     case 32:
20180b57cec5SDimitry Andric       ZeroTy = Type::getFloatTy(Ctx);
20190b57cec5SDimitry Andric       break;
20200b57cec5SDimitry Andric     case 64:
20210b57cec5SDimitry Andric       ZeroTy = Type::getDoubleTy(Ctx);
20220b57cec5SDimitry Andric       break;
20230b57cec5SDimitry Andric     case 128:
20240b57cec5SDimitry Andric       ZeroTy = Type::getFP128Ty(Ctx);
20250b57cec5SDimitry Andric       break;
20260b57cec5SDimitry Andric     default:
20270b57cec5SDimitry Andric       llvm_unreachable("unexpected floating-point type");
20280b57cec5SDimitry Andric     }
20290b57cec5SDimitry Andric     ConstantFP &ZeroForNegation =
20300b57cec5SDimitry Andric         *cast<ConstantFP>(ConstantFP::getZeroValueForNegation(ZeroTy));
20310b57cec5SDimitry Andric     auto Zero = MIRBuilder.buildFConstant(Ty, ZeroForNegation);
20320b57cec5SDimitry Andric     Register SubByReg = MI.getOperand(1).getReg();
20330b57cec5SDimitry Andric     Register ZeroReg = Zero->getOperand(0).getReg();
20340b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_FSUB, {Res}, {ZeroReg, SubByReg},
20350b57cec5SDimitry Andric                           MI.getFlags());
20360b57cec5SDimitry Andric     MI.eraseFromParent();
20370b57cec5SDimitry Andric     return Legalized;
20380b57cec5SDimitry Andric   }
20390b57cec5SDimitry Andric   case TargetOpcode::G_FSUB: {
20400b57cec5SDimitry Andric     // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)).
20410b57cec5SDimitry Andric     // First, check if G_FNEG is marked as Lower. If so, we may
20420b57cec5SDimitry Andric     // end up with an infinite loop as G_FSUB is used to legalize G_FNEG.
20430b57cec5SDimitry Andric     if (LI.getAction({G_FNEG, {Ty}}).Action == Lower)
20440b57cec5SDimitry Andric       return UnableToLegalize;
20450b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
20460b57cec5SDimitry Andric     Register LHS = MI.getOperand(1).getReg();
20470b57cec5SDimitry Andric     Register RHS = MI.getOperand(2).getReg();
20480b57cec5SDimitry Andric     Register Neg = MRI.createGenericVirtualRegister(Ty);
20490b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_FNEG).addDef(Neg).addUse(RHS);
20500b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_FADD, {Res}, {LHS, Neg}, MI.getFlags());
20510b57cec5SDimitry Andric     MI.eraseFromParent();
20520b57cec5SDimitry Andric     return Legalized;
20530b57cec5SDimitry Andric   }
20548bcb0991SDimitry Andric   case TargetOpcode::G_FMAD:
20558bcb0991SDimitry Andric     return lowerFMad(MI);
2056*480093f4SDimitry Andric   case TargetOpcode::G_INTRINSIC_ROUND:
2057*480093f4SDimitry Andric     return lowerIntrinsicRound(MI);
20580b57cec5SDimitry Andric   case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: {
20590b57cec5SDimitry Andric     Register OldValRes = MI.getOperand(0).getReg();
20600b57cec5SDimitry Andric     Register SuccessRes = MI.getOperand(1).getReg();
20610b57cec5SDimitry Andric     Register Addr = MI.getOperand(2).getReg();
20620b57cec5SDimitry Andric     Register CmpVal = MI.getOperand(3).getReg();
20630b57cec5SDimitry Andric     Register NewVal = MI.getOperand(4).getReg();
20640b57cec5SDimitry Andric     MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal,
20650b57cec5SDimitry Andric                                   **MI.memoperands_begin());
20660b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal);
20670b57cec5SDimitry Andric     MI.eraseFromParent();
20680b57cec5SDimitry Andric     return Legalized;
20690b57cec5SDimitry Andric   }
20700b57cec5SDimitry Andric   case TargetOpcode::G_LOAD:
20710b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD:
20720b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD: {
20730b57cec5SDimitry Andric     // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT
20740b57cec5SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
20750b57cec5SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
20760b57cec5SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
20770b57cec5SDimitry Andric     auto &MMO = **MI.memoperands_begin();
20780b57cec5SDimitry Andric 
20798bcb0991SDimitry Andric     if (DstTy.getSizeInBits() == MMO.getSizeInBits()) {
20808bcb0991SDimitry Andric       if (MI.getOpcode() == TargetOpcode::G_LOAD) {
20818bcb0991SDimitry Andric         // This load needs splitting into power of 2 sized loads.
20828bcb0991SDimitry Andric         if (DstTy.isVector())
20830b57cec5SDimitry Andric           return UnableToLegalize;
20848bcb0991SDimitry Andric         if (isPowerOf2_32(DstTy.getSizeInBits()))
20858bcb0991SDimitry Andric           return UnableToLegalize; // Don't know what we're being asked to do.
20868bcb0991SDimitry Andric 
20878bcb0991SDimitry Andric         // Our strategy here is to generate anyextending loads for the smaller
20888bcb0991SDimitry Andric         // types up to next power-2 result type, and then combine the two larger
20898bcb0991SDimitry Andric         // result values together, before truncating back down to the non-pow-2
20908bcb0991SDimitry Andric         // type.
20918bcb0991SDimitry Andric         // E.g. v1 = i24 load =>
20928bcb0991SDimitry Andric         // v2 = i32 load (2 byte)
20938bcb0991SDimitry Andric         // v3 = i32 load (1 byte)
20948bcb0991SDimitry Andric         // v4 = i32 shl v3, 16
20958bcb0991SDimitry Andric         // v5 = i32 or v4, v2
20968bcb0991SDimitry Andric         // v1 = i24 trunc v5
20978bcb0991SDimitry Andric         // By doing this we generate the correct truncate which should get
20988bcb0991SDimitry Andric         // combined away as an artifact with a matching extend.
20998bcb0991SDimitry Andric         uint64_t LargeSplitSize = PowerOf2Floor(DstTy.getSizeInBits());
21008bcb0991SDimitry Andric         uint64_t SmallSplitSize = DstTy.getSizeInBits() - LargeSplitSize;
21018bcb0991SDimitry Andric 
21028bcb0991SDimitry Andric         MachineFunction &MF = MIRBuilder.getMF();
21038bcb0991SDimitry Andric         MachineMemOperand *LargeMMO =
21048bcb0991SDimitry Andric             MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8);
21058bcb0991SDimitry Andric         MachineMemOperand *SmallMMO = MF.getMachineMemOperand(
21068bcb0991SDimitry Andric             &MMO, LargeSplitSize / 8, SmallSplitSize / 8);
21078bcb0991SDimitry Andric 
21088bcb0991SDimitry Andric         LLT PtrTy = MRI.getType(PtrReg);
21098bcb0991SDimitry Andric         unsigned AnyExtSize = NextPowerOf2(DstTy.getSizeInBits());
21108bcb0991SDimitry Andric         LLT AnyExtTy = LLT::scalar(AnyExtSize);
21118bcb0991SDimitry Andric         Register LargeLdReg = MRI.createGenericVirtualRegister(AnyExtTy);
21128bcb0991SDimitry Andric         Register SmallLdReg = MRI.createGenericVirtualRegister(AnyExtTy);
21138bcb0991SDimitry Andric         auto LargeLoad =
21148bcb0991SDimitry Andric             MIRBuilder.buildLoad(LargeLdReg, PtrReg, *LargeMMO);
21158bcb0991SDimitry Andric 
21168bcb0991SDimitry Andric         auto OffsetCst =
21178bcb0991SDimitry Andric             MIRBuilder.buildConstant(LLT::scalar(64), LargeSplitSize / 8);
2118*480093f4SDimitry Andric         Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy);
2119*480093f4SDimitry Andric         auto SmallPtr =
2120*480093f4SDimitry Andric             MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst.getReg(0));
21218bcb0991SDimitry Andric         auto SmallLoad = MIRBuilder.buildLoad(SmallLdReg, SmallPtr.getReg(0),
21228bcb0991SDimitry Andric                                               *SmallMMO);
21238bcb0991SDimitry Andric 
21248bcb0991SDimitry Andric         auto ShiftAmt = MIRBuilder.buildConstant(AnyExtTy, LargeSplitSize);
21258bcb0991SDimitry Andric         auto Shift = MIRBuilder.buildShl(AnyExtTy, SmallLoad, ShiftAmt);
21268bcb0991SDimitry Andric         auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad);
21278bcb0991SDimitry Andric         MIRBuilder.buildTrunc(DstReg, {Or.getReg(0)});
21288bcb0991SDimitry Andric         MI.eraseFromParent();
21298bcb0991SDimitry Andric         return Legalized;
21308bcb0991SDimitry Andric       }
21310b57cec5SDimitry Andric       MIRBuilder.buildLoad(DstReg, PtrReg, MMO);
21320b57cec5SDimitry Andric       MI.eraseFromParent();
21330b57cec5SDimitry Andric       return Legalized;
21340b57cec5SDimitry Andric     }
21350b57cec5SDimitry Andric 
21360b57cec5SDimitry Andric     if (DstTy.isScalar()) {
21370b57cec5SDimitry Andric       Register TmpReg =
21380b57cec5SDimitry Andric           MRI.createGenericVirtualRegister(LLT::scalar(MMO.getSizeInBits()));
21390b57cec5SDimitry Andric       MIRBuilder.buildLoad(TmpReg, PtrReg, MMO);
21400b57cec5SDimitry Andric       switch (MI.getOpcode()) {
21410b57cec5SDimitry Andric       default:
21420b57cec5SDimitry Andric         llvm_unreachable("Unexpected opcode");
21430b57cec5SDimitry Andric       case TargetOpcode::G_LOAD:
2144*480093f4SDimitry Andric         MIRBuilder.buildExtOrTrunc(TargetOpcode::G_ANYEXT, DstReg, TmpReg);
21450b57cec5SDimitry Andric         break;
21460b57cec5SDimitry Andric       case TargetOpcode::G_SEXTLOAD:
21470b57cec5SDimitry Andric         MIRBuilder.buildSExt(DstReg, TmpReg);
21480b57cec5SDimitry Andric         break;
21490b57cec5SDimitry Andric       case TargetOpcode::G_ZEXTLOAD:
21500b57cec5SDimitry Andric         MIRBuilder.buildZExt(DstReg, TmpReg);
21510b57cec5SDimitry Andric         break;
21520b57cec5SDimitry Andric       }
21530b57cec5SDimitry Andric       MI.eraseFromParent();
21540b57cec5SDimitry Andric       return Legalized;
21550b57cec5SDimitry Andric     }
21560b57cec5SDimitry Andric 
21570b57cec5SDimitry Andric     return UnableToLegalize;
21580b57cec5SDimitry Andric   }
21598bcb0991SDimitry Andric   case TargetOpcode::G_STORE: {
21608bcb0991SDimitry Andric     // Lower a non-power of 2 store into multiple pow-2 stores.
21618bcb0991SDimitry Andric     // E.g. split an i24 store into an i16 store + i8 store.
21628bcb0991SDimitry Andric     // We do this by first extending the stored value to the next largest power
21638bcb0991SDimitry Andric     // of 2 type, and then using truncating stores to store the components.
21648bcb0991SDimitry Andric     // By doing this, likewise with G_LOAD, generate an extend that can be
21658bcb0991SDimitry Andric     // artifact-combined away instead of leaving behind extracts.
21668bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(0).getReg();
21678bcb0991SDimitry Andric     Register PtrReg = MI.getOperand(1).getReg();
21688bcb0991SDimitry Andric     LLT SrcTy = MRI.getType(SrcReg);
21698bcb0991SDimitry Andric     MachineMemOperand &MMO = **MI.memoperands_begin();
21708bcb0991SDimitry Andric     if (SrcTy.getSizeInBits() != MMO.getSizeInBits())
21718bcb0991SDimitry Andric       return UnableToLegalize;
21728bcb0991SDimitry Andric     if (SrcTy.isVector())
21738bcb0991SDimitry Andric       return UnableToLegalize;
21748bcb0991SDimitry Andric     if (isPowerOf2_32(SrcTy.getSizeInBits()))
21758bcb0991SDimitry Andric       return UnableToLegalize; // Don't know what we're being asked to do.
21768bcb0991SDimitry Andric 
21778bcb0991SDimitry Andric     // Extend to the next pow-2.
21788bcb0991SDimitry Andric     const LLT ExtendTy = LLT::scalar(NextPowerOf2(SrcTy.getSizeInBits()));
21798bcb0991SDimitry Andric     auto ExtVal = MIRBuilder.buildAnyExt(ExtendTy, SrcReg);
21808bcb0991SDimitry Andric 
21818bcb0991SDimitry Andric     // Obtain the smaller value by shifting away the larger value.
21828bcb0991SDimitry Andric     uint64_t LargeSplitSize = PowerOf2Floor(SrcTy.getSizeInBits());
21838bcb0991SDimitry Andric     uint64_t SmallSplitSize = SrcTy.getSizeInBits() - LargeSplitSize;
21848bcb0991SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(ExtendTy, LargeSplitSize);
21858bcb0991SDimitry Andric     auto SmallVal = MIRBuilder.buildLShr(ExtendTy, ExtVal, ShiftAmt);
21868bcb0991SDimitry Andric 
2187*480093f4SDimitry Andric     // Generate the PtrAdd and truncating stores.
21888bcb0991SDimitry Andric     LLT PtrTy = MRI.getType(PtrReg);
21898bcb0991SDimitry Andric     auto OffsetCst =
21908bcb0991SDimitry Andric         MIRBuilder.buildConstant(LLT::scalar(64), LargeSplitSize / 8);
2191*480093f4SDimitry Andric     Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy);
2192*480093f4SDimitry Andric     auto SmallPtr =
2193*480093f4SDimitry Andric         MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst.getReg(0));
21948bcb0991SDimitry Andric 
21958bcb0991SDimitry Andric     MachineFunction &MF = MIRBuilder.getMF();
21968bcb0991SDimitry Andric     MachineMemOperand *LargeMMO =
21978bcb0991SDimitry Andric         MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8);
21988bcb0991SDimitry Andric     MachineMemOperand *SmallMMO =
21998bcb0991SDimitry Andric         MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8);
22008bcb0991SDimitry Andric     MIRBuilder.buildStore(ExtVal.getReg(0), PtrReg, *LargeMMO);
22018bcb0991SDimitry Andric     MIRBuilder.buildStore(SmallVal.getReg(0), SmallPtr.getReg(0), *SmallMMO);
22028bcb0991SDimitry Andric     MI.eraseFromParent();
22038bcb0991SDimitry Andric     return Legalized;
22048bcb0991SDimitry Andric   }
22050b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF:
22060b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF:
22070b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ:
22080b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ:
22090b57cec5SDimitry Andric   case TargetOpcode::G_CTPOP:
22100b57cec5SDimitry Andric     return lowerBitCount(MI, TypeIdx, Ty);
22110b57cec5SDimitry Andric   case G_UADDO: {
22120b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
22130b57cec5SDimitry Andric     Register CarryOut = MI.getOperand(1).getReg();
22140b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
22150b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
22160b57cec5SDimitry Andric 
22170b57cec5SDimitry Andric     MIRBuilder.buildAdd(Res, LHS, RHS);
22180b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS);
22190b57cec5SDimitry Andric 
22200b57cec5SDimitry Andric     MI.eraseFromParent();
22210b57cec5SDimitry Andric     return Legalized;
22220b57cec5SDimitry Andric   }
22230b57cec5SDimitry Andric   case G_UADDE: {
22240b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
22250b57cec5SDimitry Andric     Register CarryOut = MI.getOperand(1).getReg();
22260b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
22270b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
22280b57cec5SDimitry Andric     Register CarryIn = MI.getOperand(4).getReg();
22290b57cec5SDimitry Andric 
22300b57cec5SDimitry Andric     Register TmpRes = MRI.createGenericVirtualRegister(Ty);
22310b57cec5SDimitry Andric     Register ZExtCarryIn = MRI.createGenericVirtualRegister(Ty);
22320b57cec5SDimitry Andric 
22330b57cec5SDimitry Andric     MIRBuilder.buildAdd(TmpRes, LHS, RHS);
22340b57cec5SDimitry Andric     MIRBuilder.buildZExt(ZExtCarryIn, CarryIn);
22350b57cec5SDimitry Andric     MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn);
22360b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS);
22370b57cec5SDimitry Andric 
22380b57cec5SDimitry Andric     MI.eraseFromParent();
22390b57cec5SDimitry Andric     return Legalized;
22400b57cec5SDimitry Andric   }
22410b57cec5SDimitry Andric   case G_USUBO: {
22420b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
22430b57cec5SDimitry Andric     Register BorrowOut = MI.getOperand(1).getReg();
22440b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
22450b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
22460b57cec5SDimitry Andric 
22470b57cec5SDimitry Andric     MIRBuilder.buildSub(Res, LHS, RHS);
22480b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS);
22490b57cec5SDimitry Andric 
22500b57cec5SDimitry Andric     MI.eraseFromParent();
22510b57cec5SDimitry Andric     return Legalized;
22520b57cec5SDimitry Andric   }
22530b57cec5SDimitry Andric   case G_USUBE: {
22540b57cec5SDimitry Andric     Register Res = MI.getOperand(0).getReg();
22550b57cec5SDimitry Andric     Register BorrowOut = MI.getOperand(1).getReg();
22560b57cec5SDimitry Andric     Register LHS = MI.getOperand(2).getReg();
22570b57cec5SDimitry Andric     Register RHS = MI.getOperand(3).getReg();
22580b57cec5SDimitry Andric     Register BorrowIn = MI.getOperand(4).getReg();
22590b57cec5SDimitry Andric 
22600b57cec5SDimitry Andric     Register TmpRes = MRI.createGenericVirtualRegister(Ty);
22610b57cec5SDimitry Andric     Register ZExtBorrowIn = MRI.createGenericVirtualRegister(Ty);
22620b57cec5SDimitry Andric     Register LHS_EQ_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1));
22630b57cec5SDimitry Andric     Register LHS_ULT_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1));
22640b57cec5SDimitry Andric 
22650b57cec5SDimitry Andric     MIRBuilder.buildSub(TmpRes, LHS, RHS);
22660b57cec5SDimitry Andric     MIRBuilder.buildZExt(ZExtBorrowIn, BorrowIn);
22670b57cec5SDimitry Andric     MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn);
22680b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LHS_EQ_RHS, LHS, RHS);
22690b57cec5SDimitry Andric     MIRBuilder.buildICmp(CmpInst::ICMP_ULT, LHS_ULT_RHS, LHS, RHS);
22700b57cec5SDimitry Andric     MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS);
22710b57cec5SDimitry Andric 
22720b57cec5SDimitry Andric     MI.eraseFromParent();
22730b57cec5SDimitry Andric     return Legalized;
22740b57cec5SDimitry Andric   }
22750b57cec5SDimitry Andric   case G_UITOFP:
22760b57cec5SDimitry Andric     return lowerUITOFP(MI, TypeIdx, Ty);
22770b57cec5SDimitry Andric   case G_SITOFP:
22780b57cec5SDimitry Andric     return lowerSITOFP(MI, TypeIdx, Ty);
22798bcb0991SDimitry Andric   case G_FPTOUI:
22808bcb0991SDimitry Andric     return lowerFPTOUI(MI, TypeIdx, Ty);
22810b57cec5SDimitry Andric   case G_SMIN:
22820b57cec5SDimitry Andric   case G_SMAX:
22830b57cec5SDimitry Andric   case G_UMIN:
22840b57cec5SDimitry Andric   case G_UMAX:
22850b57cec5SDimitry Andric     return lowerMinMax(MI, TypeIdx, Ty);
22860b57cec5SDimitry Andric   case G_FCOPYSIGN:
22870b57cec5SDimitry Andric     return lowerFCopySign(MI, TypeIdx, Ty);
22880b57cec5SDimitry Andric   case G_FMINNUM:
22890b57cec5SDimitry Andric   case G_FMAXNUM:
22900b57cec5SDimitry Andric     return lowerFMinNumMaxNum(MI);
22918bcb0991SDimitry Andric   case G_UNMERGE_VALUES:
22928bcb0991SDimitry Andric     return lowerUnmergeValues(MI);
22938bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG: {
22948bcb0991SDimitry Andric     assert(MI.getOperand(2).isImm() && "Expected immediate");
22958bcb0991SDimitry Andric     int64_t SizeInBits = MI.getOperand(2).getImm();
22968bcb0991SDimitry Andric 
22978bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
22988bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
22998bcb0991SDimitry Andric     LLT DstTy = MRI.getType(DstReg);
23008bcb0991SDimitry Andric     Register TmpRes = MRI.createGenericVirtualRegister(DstTy);
23018bcb0991SDimitry Andric 
23028bcb0991SDimitry Andric     auto MIBSz = MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - SizeInBits);
23038bcb0991SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_SHL, {TmpRes}, {SrcReg, MIBSz->getOperand(0).getReg()});
23048bcb0991SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_ASHR, {DstReg}, {TmpRes, MIBSz->getOperand(0).getReg()});
23058bcb0991SDimitry Andric     MI.eraseFromParent();
23068bcb0991SDimitry Andric     return Legalized;
23078bcb0991SDimitry Andric   }
23088bcb0991SDimitry Andric   case G_SHUFFLE_VECTOR:
23098bcb0991SDimitry Andric     return lowerShuffleVector(MI);
23108bcb0991SDimitry Andric   case G_DYN_STACKALLOC:
23118bcb0991SDimitry Andric     return lowerDynStackAlloc(MI);
23128bcb0991SDimitry Andric   case G_EXTRACT:
23138bcb0991SDimitry Andric     return lowerExtract(MI);
23148bcb0991SDimitry Andric   case G_INSERT:
23158bcb0991SDimitry Andric     return lowerInsert(MI);
2316*480093f4SDimitry Andric   case G_BSWAP:
2317*480093f4SDimitry Andric     return lowerBswap(MI);
2318*480093f4SDimitry Andric   case G_BITREVERSE:
2319*480093f4SDimitry Andric     return lowerBitreverse(MI);
2320*480093f4SDimitry Andric   case G_READ_REGISTER:
2321*480093f4SDimitry Andric     return lowerReadRegister(MI);
23220b57cec5SDimitry Andric   }
23230b57cec5SDimitry Andric }
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorImplicitDef(
23260b57cec5SDimitry Andric     MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy) {
23270b57cec5SDimitry Andric   SmallVector<Register, 2> DstRegs;
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
23300b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
23310b57cec5SDimitry Andric   unsigned Size = MRI.getType(DstReg).getSizeInBits();
23320b57cec5SDimitry Andric   int NumParts = Size / NarrowSize;
23330b57cec5SDimitry Andric   // FIXME: Don't know how to handle the situation where the small vectors
23340b57cec5SDimitry Andric   // aren't all the same size yet.
23350b57cec5SDimitry Andric   if (Size % NarrowSize != 0)
23360b57cec5SDimitry Andric     return UnableToLegalize;
23370b57cec5SDimitry Andric 
23380b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
23390b57cec5SDimitry Andric     Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy);
23400b57cec5SDimitry Andric     MIRBuilder.buildUndef(TmpReg);
23410b57cec5SDimitry Andric     DstRegs.push_back(TmpReg);
23420b57cec5SDimitry Andric   }
23430b57cec5SDimitry Andric 
23440b57cec5SDimitry Andric   if (NarrowTy.isVector())
23450b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
23460b57cec5SDimitry Andric   else
23470b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
23480b57cec5SDimitry Andric 
23490b57cec5SDimitry Andric   MI.eraseFromParent();
23500b57cec5SDimitry Andric   return Legalized;
23510b57cec5SDimitry Andric }
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
23540b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorBasic(MachineInstr &MI, unsigned TypeIdx,
23550b57cec5SDimitry Andric                                           LLT NarrowTy) {
23560b57cec5SDimitry Andric   const unsigned Opc = MI.getOpcode();
23570b57cec5SDimitry Andric   const unsigned NumOps = MI.getNumOperands() - 1;
23580b57cec5SDimitry Andric   const unsigned NarrowSize = NarrowTy.getSizeInBits();
23590b57cec5SDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
23600b57cec5SDimitry Andric   const unsigned Flags = MI.getFlags();
23610b57cec5SDimitry Andric   const LLT DstTy = MRI.getType(DstReg);
23620b57cec5SDimitry Andric   const unsigned Size = DstTy.getSizeInBits();
23630b57cec5SDimitry Andric   const int NumParts = Size / NarrowSize;
23640b57cec5SDimitry Andric   const LLT EltTy = DstTy.getElementType();
23650b57cec5SDimitry Andric   const unsigned EltSize = EltTy.getSizeInBits();
23660b57cec5SDimitry Andric   const unsigned BitsForNumParts = NarrowSize * NumParts;
23670b57cec5SDimitry Andric 
23680b57cec5SDimitry Andric   // Check if we have any leftovers. If we do, then only handle the case where
23690b57cec5SDimitry Andric   // the leftover is one element.
23700b57cec5SDimitry Andric   if (BitsForNumParts != Size && BitsForNumParts + EltSize != Size)
23710b57cec5SDimitry Andric     return UnableToLegalize;
23720b57cec5SDimitry Andric 
23730b57cec5SDimitry Andric   if (BitsForNumParts != Size) {
23740b57cec5SDimitry Andric     Register AccumDstReg = MRI.createGenericVirtualRegister(DstTy);
23750b57cec5SDimitry Andric     MIRBuilder.buildUndef(AccumDstReg);
23760b57cec5SDimitry Andric 
23770b57cec5SDimitry Andric     // Handle the pieces which evenly divide into the requested type with
23780b57cec5SDimitry Andric     // extract/op/insert sequence.
23790b57cec5SDimitry Andric     for (unsigned Offset = 0; Offset < BitsForNumParts; Offset += NarrowSize) {
23800b57cec5SDimitry Andric       SmallVector<SrcOp, 4> SrcOps;
23810b57cec5SDimitry Andric       for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) {
23820b57cec5SDimitry Andric         Register PartOpReg = MRI.createGenericVirtualRegister(NarrowTy);
23830b57cec5SDimitry Andric         MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(), Offset);
23840b57cec5SDimitry Andric         SrcOps.push_back(PartOpReg);
23850b57cec5SDimitry Andric       }
23860b57cec5SDimitry Andric 
23870b57cec5SDimitry Andric       Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy);
23880b57cec5SDimitry Andric       MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags);
23890b57cec5SDimitry Andric 
23900b57cec5SDimitry Andric       Register PartInsertReg = MRI.createGenericVirtualRegister(DstTy);
23910b57cec5SDimitry Andric       MIRBuilder.buildInsert(PartInsertReg, AccumDstReg, PartDstReg, Offset);
23920b57cec5SDimitry Andric       AccumDstReg = PartInsertReg;
23930b57cec5SDimitry Andric     }
23940b57cec5SDimitry Andric 
23950b57cec5SDimitry Andric     // Handle the remaining element sized leftover piece.
23960b57cec5SDimitry Andric     SmallVector<SrcOp, 4> SrcOps;
23970b57cec5SDimitry Andric     for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) {
23980b57cec5SDimitry Andric       Register PartOpReg = MRI.createGenericVirtualRegister(EltTy);
23990b57cec5SDimitry Andric       MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(),
24000b57cec5SDimitry Andric                               BitsForNumParts);
24010b57cec5SDimitry Andric       SrcOps.push_back(PartOpReg);
24020b57cec5SDimitry Andric     }
24030b57cec5SDimitry Andric 
24040b57cec5SDimitry Andric     Register PartDstReg = MRI.createGenericVirtualRegister(EltTy);
24050b57cec5SDimitry Andric     MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags);
24060b57cec5SDimitry Andric     MIRBuilder.buildInsert(DstReg, AccumDstReg, PartDstReg, BitsForNumParts);
24070b57cec5SDimitry Andric     MI.eraseFromParent();
24080b57cec5SDimitry Andric 
24090b57cec5SDimitry Andric     return Legalized;
24100b57cec5SDimitry Andric   }
24110b57cec5SDimitry Andric 
24120b57cec5SDimitry Andric   SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs;
24130b57cec5SDimitry Andric 
24140b57cec5SDimitry Andric   extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src0Regs);
24150b57cec5SDimitry Andric 
24160b57cec5SDimitry Andric   if (NumOps >= 2)
24170b57cec5SDimitry Andric     extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src1Regs);
24180b57cec5SDimitry Andric 
24190b57cec5SDimitry Andric   if (NumOps >= 3)
24200b57cec5SDimitry Andric     extractParts(MI.getOperand(3).getReg(), NarrowTy, NumParts, Src2Regs);
24210b57cec5SDimitry Andric 
24220b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
24230b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
24240b57cec5SDimitry Andric 
24250b57cec5SDimitry Andric     if (NumOps == 1)
24260b57cec5SDimitry Andric       MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i]}, Flags);
24270b57cec5SDimitry Andric     else if (NumOps == 2) {
24280b57cec5SDimitry Andric       MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i], Src1Regs[i]}, Flags);
24290b57cec5SDimitry Andric     } else if (NumOps == 3) {
24300b57cec5SDimitry Andric       MIRBuilder.buildInstr(Opc, {DstReg},
24310b57cec5SDimitry Andric                             {Src0Regs[i], Src1Regs[i], Src2Regs[i]}, Flags);
24320b57cec5SDimitry Andric     }
24330b57cec5SDimitry Andric 
24340b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
24350b57cec5SDimitry Andric   }
24360b57cec5SDimitry Andric 
24370b57cec5SDimitry Andric   if (NarrowTy.isVector())
24380b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
24390b57cec5SDimitry Andric   else
24400b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
24410b57cec5SDimitry Andric 
24420b57cec5SDimitry Andric   MI.eraseFromParent();
24430b57cec5SDimitry Andric   return Legalized;
24440b57cec5SDimitry Andric }
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric // Handle splitting vector operations which need to have the same number of
24470b57cec5SDimitry Andric // elements in each type index, but each type index may have a different element
24480b57cec5SDimitry Andric // type.
24490b57cec5SDimitry Andric //
24500b57cec5SDimitry Andric // e.g.  <4 x s64> = G_SHL <4 x s64>, <4 x s32> ->
24510b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
24520b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
24530b57cec5SDimitry Andric //
24540b57cec5SDimitry Andric // Also handles some irregular breakdown cases, e.g.
24550b57cec5SDimitry Andric // e.g.  <3 x s64> = G_SHL <3 x s64>, <3 x s32> ->
24560b57cec5SDimitry Andric //       <2 x s64> = G_SHL <2 x s64>, <2 x s32>
24570b57cec5SDimitry Andric //             s64 = G_SHL s64, s32
24580b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
24590b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorMultiEltType(
24600b57cec5SDimitry Andric   MachineInstr &MI, unsigned TypeIdx, LLT NarrowTyArg) {
24610b57cec5SDimitry Andric   if (TypeIdx != 0)
24620b57cec5SDimitry Andric     return UnableToLegalize;
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric   const LLT NarrowTy0 = NarrowTyArg;
24650b57cec5SDimitry Andric   const unsigned NewNumElts =
24660b57cec5SDimitry Andric       NarrowTy0.isVector() ? NarrowTy0.getNumElements() : 1;
24670b57cec5SDimitry Andric 
24680b57cec5SDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
24690b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
24700b57cec5SDimitry Andric   LLT LeftoverTy0;
24710b57cec5SDimitry Andric 
24720b57cec5SDimitry Andric   // All of the operands need to have the same number of elements, so if we can
24730b57cec5SDimitry Andric   // determine a type breakdown for the result type, we can for all of the
24740b57cec5SDimitry Andric   // source types.
24750b57cec5SDimitry Andric   int NumParts = getNarrowTypeBreakDown(DstTy, NarrowTy0, LeftoverTy0).first;
24760b57cec5SDimitry Andric   if (NumParts < 0)
24770b57cec5SDimitry Andric     return UnableToLegalize;
24780b57cec5SDimitry Andric 
24790b57cec5SDimitry Andric   SmallVector<MachineInstrBuilder, 4> NewInsts;
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, LeftoverDstRegs;
24820b57cec5SDimitry Andric   SmallVector<Register, 4> PartRegs, LeftoverRegs;
24830b57cec5SDimitry Andric 
24840b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) {
24850b57cec5SDimitry Andric     LLT LeftoverTy;
24860b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
24870b57cec5SDimitry Andric     LLT SrcTyI = MRI.getType(SrcReg);
24880b57cec5SDimitry Andric     LLT NarrowTyI = LLT::scalarOrVector(NewNumElts, SrcTyI.getScalarType());
24890b57cec5SDimitry Andric     LLT LeftoverTyI;
24900b57cec5SDimitry Andric 
24910b57cec5SDimitry Andric     // Split this operand into the requested typed registers, and any leftover
24920b57cec5SDimitry Andric     // required to reproduce the original type.
24930b57cec5SDimitry Andric     if (!extractParts(SrcReg, SrcTyI, NarrowTyI, LeftoverTyI, PartRegs,
24940b57cec5SDimitry Andric                       LeftoverRegs))
24950b57cec5SDimitry Andric       return UnableToLegalize;
24960b57cec5SDimitry Andric 
24970b57cec5SDimitry Andric     if (I == 1) {
24980b57cec5SDimitry Andric       // For the first operand, create an instruction for each part and setup
24990b57cec5SDimitry Andric       // the result.
25000b57cec5SDimitry Andric       for (Register PartReg : PartRegs) {
25010b57cec5SDimitry Andric         Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy0);
25020b57cec5SDimitry Andric         NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode())
25030b57cec5SDimitry Andric                                .addDef(PartDstReg)
25040b57cec5SDimitry Andric                                .addUse(PartReg));
25050b57cec5SDimitry Andric         DstRegs.push_back(PartDstReg);
25060b57cec5SDimitry Andric       }
25070b57cec5SDimitry Andric 
25080b57cec5SDimitry Andric       for (Register LeftoverReg : LeftoverRegs) {
25090b57cec5SDimitry Andric         Register PartDstReg = MRI.createGenericVirtualRegister(LeftoverTy0);
25100b57cec5SDimitry Andric         NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode())
25110b57cec5SDimitry Andric                                .addDef(PartDstReg)
25120b57cec5SDimitry Andric                                .addUse(LeftoverReg));
25130b57cec5SDimitry Andric         LeftoverDstRegs.push_back(PartDstReg);
25140b57cec5SDimitry Andric       }
25150b57cec5SDimitry Andric     } else {
25160b57cec5SDimitry Andric       assert(NewInsts.size() == PartRegs.size() + LeftoverRegs.size());
25170b57cec5SDimitry Andric 
25180b57cec5SDimitry Andric       // Add the newly created operand splits to the existing instructions. The
25190b57cec5SDimitry Andric       // odd-sized pieces are ordered after the requested NarrowTyArg sized
25200b57cec5SDimitry Andric       // pieces.
25210b57cec5SDimitry Andric       unsigned InstCount = 0;
25220b57cec5SDimitry Andric       for (unsigned J = 0, JE = PartRegs.size(); J != JE; ++J)
25230b57cec5SDimitry Andric         NewInsts[InstCount++].addUse(PartRegs[J]);
25240b57cec5SDimitry Andric       for (unsigned J = 0, JE = LeftoverRegs.size(); J != JE; ++J)
25250b57cec5SDimitry Andric         NewInsts[InstCount++].addUse(LeftoverRegs[J]);
25260b57cec5SDimitry Andric     }
25270b57cec5SDimitry Andric 
25280b57cec5SDimitry Andric     PartRegs.clear();
25290b57cec5SDimitry Andric     LeftoverRegs.clear();
25300b57cec5SDimitry Andric   }
25310b57cec5SDimitry Andric 
25320b57cec5SDimitry Andric   // Insert the newly built operations and rebuild the result register.
25330b57cec5SDimitry Andric   for (auto &MIB : NewInsts)
25340b57cec5SDimitry Andric     MIRBuilder.insertInstr(MIB);
25350b57cec5SDimitry Andric 
25360b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy0, DstRegs, LeftoverTy0, LeftoverDstRegs);
25370b57cec5SDimitry Andric 
25380b57cec5SDimitry Andric   MI.eraseFromParent();
25390b57cec5SDimitry Andric   return Legalized;
25400b57cec5SDimitry Andric }
25410b57cec5SDimitry Andric 
25420b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
25430b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCasts(MachineInstr &MI, unsigned TypeIdx,
25440b57cec5SDimitry Andric                                           LLT NarrowTy) {
25450b57cec5SDimitry Andric   if (TypeIdx != 0)
25460b57cec5SDimitry Andric     return UnableToLegalize;
25470b57cec5SDimitry Andric 
25480b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
25490b57cec5SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
25500b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
25510b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
25520b57cec5SDimitry Andric 
25530b57cec5SDimitry Andric   LLT NarrowTy0 = NarrowTy;
25540b57cec5SDimitry Andric   LLT NarrowTy1;
25550b57cec5SDimitry Andric   unsigned NumParts;
25560b57cec5SDimitry Andric 
25570b57cec5SDimitry Andric   if (NarrowTy.isVector()) {
25580b57cec5SDimitry Andric     // Uneven breakdown not handled.
25590b57cec5SDimitry Andric     NumParts = DstTy.getNumElements() / NarrowTy.getNumElements();
25600b57cec5SDimitry Andric     if (NumParts * NarrowTy.getNumElements() != DstTy.getNumElements())
25610b57cec5SDimitry Andric       return UnableToLegalize;
25620b57cec5SDimitry Andric 
25630b57cec5SDimitry Andric     NarrowTy1 = LLT::vector(NumParts, SrcTy.getElementType().getSizeInBits());
25640b57cec5SDimitry Andric   } else {
25650b57cec5SDimitry Andric     NumParts = DstTy.getNumElements();
25660b57cec5SDimitry Andric     NarrowTy1 = SrcTy.getElementType();
25670b57cec5SDimitry Andric   }
25680b57cec5SDimitry Andric 
25690b57cec5SDimitry Andric   SmallVector<Register, 4> SrcRegs, DstRegs;
25700b57cec5SDimitry Andric   extractParts(SrcReg, NarrowTy1, NumParts, SrcRegs);
25710b57cec5SDimitry Andric 
25720b57cec5SDimitry Andric   for (unsigned I = 0; I < NumParts; ++I) {
25730b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
25740b57cec5SDimitry Andric     MachineInstr *NewInst = MIRBuilder.buildInstr(MI.getOpcode())
25750b57cec5SDimitry Andric       .addDef(DstReg)
25760b57cec5SDimitry Andric       .addUse(SrcRegs[I]);
25770b57cec5SDimitry Andric 
25780b57cec5SDimitry Andric     NewInst->setFlags(MI.getFlags());
25790b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
25800b57cec5SDimitry Andric   }
25810b57cec5SDimitry Andric 
25820b57cec5SDimitry Andric   if (NarrowTy.isVector())
25830b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
25840b57cec5SDimitry Andric   else
25850b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
25860b57cec5SDimitry Andric 
25870b57cec5SDimitry Andric   MI.eraseFromParent();
25880b57cec5SDimitry Andric   return Legalized;
25890b57cec5SDimitry Andric }
25900b57cec5SDimitry Andric 
25910b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
25920b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCmp(MachineInstr &MI, unsigned TypeIdx,
25930b57cec5SDimitry Andric                                         LLT NarrowTy) {
25940b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
25950b57cec5SDimitry Andric   Register Src0Reg = MI.getOperand(2).getReg();
25960b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
25970b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src0Reg);
25980b57cec5SDimitry Andric 
25990b57cec5SDimitry Andric   unsigned NumParts;
26000b57cec5SDimitry Andric   LLT NarrowTy0, NarrowTy1;
26010b57cec5SDimitry Andric 
26020b57cec5SDimitry Andric   if (TypeIdx == 0) {
26030b57cec5SDimitry Andric     unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1;
26040b57cec5SDimitry Andric     unsigned OldElts = DstTy.getNumElements();
26050b57cec5SDimitry Andric 
26060b57cec5SDimitry Andric     NarrowTy0 = NarrowTy;
26070b57cec5SDimitry Andric     NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : DstTy.getNumElements();
26080b57cec5SDimitry Andric     NarrowTy1 = NarrowTy.isVector() ?
26090b57cec5SDimitry Andric       LLT::vector(NarrowTy.getNumElements(), SrcTy.getScalarSizeInBits()) :
26100b57cec5SDimitry Andric       SrcTy.getElementType();
26110b57cec5SDimitry Andric 
26120b57cec5SDimitry Andric   } else {
26130b57cec5SDimitry Andric     unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1;
26140b57cec5SDimitry Andric     unsigned OldElts = SrcTy.getNumElements();
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric     NumParts = NarrowTy.isVector() ? (OldElts / NewElts) :
26170b57cec5SDimitry Andric       NarrowTy.getNumElements();
26180b57cec5SDimitry Andric     NarrowTy0 = LLT::vector(NarrowTy.getNumElements(),
26190b57cec5SDimitry Andric                             DstTy.getScalarSizeInBits());
26200b57cec5SDimitry Andric     NarrowTy1 = NarrowTy;
26210b57cec5SDimitry Andric   }
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric   // FIXME: Don't know how to handle the situation where the small vectors
26240b57cec5SDimitry Andric   // aren't all the same size yet.
26250b57cec5SDimitry Andric   if (NarrowTy1.isVector() &&
26260b57cec5SDimitry Andric       NarrowTy1.getNumElements() * NumParts != DstTy.getNumElements())
26270b57cec5SDimitry Andric     return UnableToLegalize;
26280b57cec5SDimitry Andric 
26290b57cec5SDimitry Andric   CmpInst::Predicate Pred
26300b57cec5SDimitry Andric     = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
26310b57cec5SDimitry Andric 
26320b57cec5SDimitry Andric   SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
26330b57cec5SDimitry Andric   extractParts(MI.getOperand(2).getReg(), NarrowTy1, NumParts, Src1Regs);
26340b57cec5SDimitry Andric   extractParts(MI.getOperand(3).getReg(), NarrowTy1, NumParts, Src2Regs);
26350b57cec5SDimitry Andric 
26360b57cec5SDimitry Andric   for (unsigned I = 0; I < NumParts; ++I) {
26370b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
26380b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
26390b57cec5SDimitry Andric 
26400b57cec5SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_ICMP)
26410b57cec5SDimitry Andric       MIRBuilder.buildICmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]);
26420b57cec5SDimitry Andric     else {
26430b57cec5SDimitry Andric       MachineInstr *NewCmp
26440b57cec5SDimitry Andric         = MIRBuilder.buildFCmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]);
26450b57cec5SDimitry Andric       NewCmp->setFlags(MI.getFlags());
26460b57cec5SDimitry Andric     }
26470b57cec5SDimitry Andric   }
26480b57cec5SDimitry Andric 
26490b57cec5SDimitry Andric   if (NarrowTy1.isVector())
26500b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
26510b57cec5SDimitry Andric   else
26520b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
26530b57cec5SDimitry Andric 
26540b57cec5SDimitry Andric   MI.eraseFromParent();
26550b57cec5SDimitry Andric   return Legalized;
26560b57cec5SDimitry Andric }
26570b57cec5SDimitry Andric 
26580b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
26590b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorSelect(MachineInstr &MI, unsigned TypeIdx,
26600b57cec5SDimitry Andric                                            LLT NarrowTy) {
26610b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
26620b57cec5SDimitry Andric   Register CondReg = MI.getOperand(1).getReg();
26630b57cec5SDimitry Andric 
26640b57cec5SDimitry Andric   unsigned NumParts = 0;
26650b57cec5SDimitry Andric   LLT NarrowTy0, NarrowTy1;
26660b57cec5SDimitry Andric 
26670b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
26680b57cec5SDimitry Andric   LLT CondTy = MRI.getType(CondReg);
26690b57cec5SDimitry Andric   unsigned Size = DstTy.getSizeInBits();
26700b57cec5SDimitry Andric 
26710b57cec5SDimitry Andric   assert(TypeIdx == 0 || CondTy.isVector());
26720b57cec5SDimitry Andric 
26730b57cec5SDimitry Andric   if (TypeIdx == 0) {
26740b57cec5SDimitry Andric     NarrowTy0 = NarrowTy;
26750b57cec5SDimitry Andric     NarrowTy1 = CondTy;
26760b57cec5SDimitry Andric 
26770b57cec5SDimitry Andric     unsigned NarrowSize = NarrowTy0.getSizeInBits();
26780b57cec5SDimitry Andric     // FIXME: Don't know how to handle the situation where the small vectors
26790b57cec5SDimitry Andric     // aren't all the same size yet.
26800b57cec5SDimitry Andric     if (Size % NarrowSize != 0)
26810b57cec5SDimitry Andric       return UnableToLegalize;
26820b57cec5SDimitry Andric 
26830b57cec5SDimitry Andric     NumParts = Size / NarrowSize;
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric     // Need to break down the condition type
26860b57cec5SDimitry Andric     if (CondTy.isVector()) {
26870b57cec5SDimitry Andric       if (CondTy.getNumElements() == NumParts)
26880b57cec5SDimitry Andric         NarrowTy1 = CondTy.getElementType();
26890b57cec5SDimitry Andric       else
26900b57cec5SDimitry Andric         NarrowTy1 = LLT::vector(CondTy.getNumElements() / NumParts,
26910b57cec5SDimitry Andric                                 CondTy.getScalarSizeInBits());
26920b57cec5SDimitry Andric     }
26930b57cec5SDimitry Andric   } else {
26940b57cec5SDimitry Andric     NumParts = CondTy.getNumElements();
26950b57cec5SDimitry Andric     if (NarrowTy.isVector()) {
26960b57cec5SDimitry Andric       // TODO: Handle uneven breakdown.
26970b57cec5SDimitry Andric       if (NumParts * NarrowTy.getNumElements() != CondTy.getNumElements())
26980b57cec5SDimitry Andric         return UnableToLegalize;
26990b57cec5SDimitry Andric 
27000b57cec5SDimitry Andric       return UnableToLegalize;
27010b57cec5SDimitry Andric     } else {
27020b57cec5SDimitry Andric       NarrowTy0 = DstTy.getElementType();
27030b57cec5SDimitry Andric       NarrowTy1 = NarrowTy;
27040b57cec5SDimitry Andric     }
27050b57cec5SDimitry Andric   }
27060b57cec5SDimitry Andric 
27070b57cec5SDimitry Andric   SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs;
27080b57cec5SDimitry Andric   if (CondTy.isVector())
27090b57cec5SDimitry Andric     extractParts(MI.getOperand(1).getReg(), NarrowTy1, NumParts, Src0Regs);
27100b57cec5SDimitry Andric 
27110b57cec5SDimitry Andric   extractParts(MI.getOperand(2).getReg(), NarrowTy0, NumParts, Src1Regs);
27120b57cec5SDimitry Andric   extractParts(MI.getOperand(3).getReg(), NarrowTy0, NumParts, Src2Regs);
27130b57cec5SDimitry Andric 
27140b57cec5SDimitry Andric   for (unsigned i = 0; i < NumParts; ++i) {
27150b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0);
27160b57cec5SDimitry Andric     MIRBuilder.buildSelect(DstReg, CondTy.isVector() ? Src0Regs[i] : CondReg,
27170b57cec5SDimitry Andric                            Src1Regs[i], Src2Regs[i]);
27180b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
27190b57cec5SDimitry Andric   }
27200b57cec5SDimitry Andric 
27210b57cec5SDimitry Andric   if (NarrowTy0.isVector())
27220b57cec5SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, DstRegs);
27230b57cec5SDimitry Andric   else
27240b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
27250b57cec5SDimitry Andric 
27260b57cec5SDimitry Andric   MI.eraseFromParent();
27270b57cec5SDimitry Andric   return Legalized;
27280b57cec5SDimitry Andric }
27290b57cec5SDimitry Andric 
27300b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
27310b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx,
27320b57cec5SDimitry Andric                                         LLT NarrowTy) {
27330b57cec5SDimitry Andric   const Register DstReg = MI.getOperand(0).getReg();
27340b57cec5SDimitry Andric   LLT PhiTy = MRI.getType(DstReg);
27350b57cec5SDimitry Andric   LLT LeftoverTy;
27360b57cec5SDimitry Andric 
27370b57cec5SDimitry Andric   // All of the operands need to have the same number of elements, so if we can
27380b57cec5SDimitry Andric   // determine a type breakdown for the result type, we can for all of the
27390b57cec5SDimitry Andric   // source types.
27400b57cec5SDimitry Andric   int NumParts, NumLeftover;
27410b57cec5SDimitry Andric   std::tie(NumParts, NumLeftover)
27420b57cec5SDimitry Andric     = getNarrowTypeBreakDown(PhiTy, NarrowTy, LeftoverTy);
27430b57cec5SDimitry Andric   if (NumParts < 0)
27440b57cec5SDimitry Andric     return UnableToLegalize;
27450b57cec5SDimitry Andric 
27460b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, LeftoverDstRegs;
27470b57cec5SDimitry Andric   SmallVector<MachineInstrBuilder, 4> NewInsts;
27480b57cec5SDimitry Andric 
27490b57cec5SDimitry Andric   const int TotalNumParts = NumParts + NumLeftover;
27500b57cec5SDimitry Andric 
27510b57cec5SDimitry Andric   // Insert the new phis in the result block first.
27520b57cec5SDimitry Andric   for (int I = 0; I != TotalNumParts; ++I) {
27530b57cec5SDimitry Andric     LLT Ty = I < NumParts ? NarrowTy : LeftoverTy;
27540b57cec5SDimitry Andric     Register PartDstReg = MRI.createGenericVirtualRegister(Ty);
27550b57cec5SDimitry Andric     NewInsts.push_back(MIRBuilder.buildInstr(TargetOpcode::G_PHI)
27560b57cec5SDimitry Andric                        .addDef(PartDstReg));
27570b57cec5SDimitry Andric     if (I < NumParts)
27580b57cec5SDimitry Andric       DstRegs.push_back(PartDstReg);
27590b57cec5SDimitry Andric     else
27600b57cec5SDimitry Andric       LeftoverDstRegs.push_back(PartDstReg);
27610b57cec5SDimitry Andric   }
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric   MachineBasicBlock *MBB = MI.getParent();
27640b57cec5SDimitry Andric   MIRBuilder.setInsertPt(*MBB, MBB->getFirstNonPHI());
27650b57cec5SDimitry Andric   insertParts(DstReg, PhiTy, NarrowTy, DstRegs, LeftoverTy, LeftoverDstRegs);
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric   SmallVector<Register, 4> PartRegs, LeftoverRegs;
27680b57cec5SDimitry Andric 
27690b57cec5SDimitry Andric   // Insert code to extract the incoming values in each predecessor block.
27700b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
27710b57cec5SDimitry Andric     PartRegs.clear();
27720b57cec5SDimitry Andric     LeftoverRegs.clear();
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(I).getReg();
27750b57cec5SDimitry Andric     MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
27760b57cec5SDimitry Andric     MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
27770b57cec5SDimitry Andric 
27780b57cec5SDimitry Andric     LLT Unused;
27790b57cec5SDimitry Andric     if (!extractParts(SrcReg, PhiTy, NarrowTy, Unused, PartRegs,
27800b57cec5SDimitry Andric                       LeftoverRegs))
27810b57cec5SDimitry Andric       return UnableToLegalize;
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric     // Add the newly created operand splits to the existing instructions. The
27840b57cec5SDimitry Andric     // odd-sized pieces are ordered after the requested NarrowTyArg sized
27850b57cec5SDimitry Andric     // pieces.
27860b57cec5SDimitry Andric     for (int J = 0; J != TotalNumParts; ++J) {
27870b57cec5SDimitry Andric       MachineInstrBuilder MIB = NewInsts[J];
27880b57cec5SDimitry Andric       MIB.addUse(J < NumParts ? PartRegs[J] : LeftoverRegs[J - NumParts]);
27890b57cec5SDimitry Andric       MIB.addMBB(&OpMBB);
27900b57cec5SDimitry Andric     }
27910b57cec5SDimitry Andric   }
27920b57cec5SDimitry Andric 
27930b57cec5SDimitry Andric   MI.eraseFromParent();
27940b57cec5SDimitry Andric   return Legalized;
27950b57cec5SDimitry Andric }
27960b57cec5SDimitry Andric 
27970b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
27988bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorUnmergeValues(MachineInstr &MI,
27998bcb0991SDimitry Andric                                                   unsigned TypeIdx,
28008bcb0991SDimitry Andric                                                   LLT NarrowTy) {
28018bcb0991SDimitry Andric   if (TypeIdx != 1)
28028bcb0991SDimitry Andric     return UnableToLegalize;
28038bcb0991SDimitry Andric 
28048bcb0991SDimitry Andric   const int NumDst = MI.getNumOperands() - 1;
28058bcb0991SDimitry Andric   const Register SrcReg = MI.getOperand(NumDst).getReg();
28068bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
28078bcb0991SDimitry Andric 
28088bcb0991SDimitry Andric   LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
28098bcb0991SDimitry Andric 
28108bcb0991SDimitry Andric   // TODO: Create sequence of extracts.
28118bcb0991SDimitry Andric   if (DstTy == NarrowTy)
28128bcb0991SDimitry Andric     return UnableToLegalize;
28138bcb0991SDimitry Andric 
28148bcb0991SDimitry Andric   LLT GCDTy = getGCDType(SrcTy, NarrowTy);
28158bcb0991SDimitry Andric   if (DstTy == GCDTy) {
28168bcb0991SDimitry Andric     // This would just be a copy of the same unmerge.
28178bcb0991SDimitry Andric     // TODO: Create extracts, pad with undef and create intermediate merges.
28188bcb0991SDimitry Andric     return UnableToLegalize;
28198bcb0991SDimitry Andric   }
28208bcb0991SDimitry Andric 
28218bcb0991SDimitry Andric   auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg);
28228bcb0991SDimitry Andric   const int NumUnmerge = Unmerge->getNumOperands() - 1;
28238bcb0991SDimitry Andric   const int PartsPerUnmerge = NumDst / NumUnmerge;
28248bcb0991SDimitry Andric 
28258bcb0991SDimitry Andric   for (int I = 0; I != NumUnmerge; ++I) {
28268bcb0991SDimitry Andric     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES);
28278bcb0991SDimitry Andric 
28288bcb0991SDimitry Andric     for (int J = 0; J != PartsPerUnmerge; ++J)
28298bcb0991SDimitry Andric       MIB.addDef(MI.getOperand(I * PartsPerUnmerge + J).getReg());
28308bcb0991SDimitry Andric     MIB.addUse(Unmerge.getReg(I));
28318bcb0991SDimitry Andric   }
28328bcb0991SDimitry Andric 
28338bcb0991SDimitry Andric   MI.eraseFromParent();
28348bcb0991SDimitry Andric   return Legalized;
28358bcb0991SDimitry Andric }
28368bcb0991SDimitry Andric 
28378bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
28388bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorBuildVector(MachineInstr &MI,
28398bcb0991SDimitry Andric                                                 unsigned TypeIdx,
28408bcb0991SDimitry Andric                                                 LLT NarrowTy) {
28418bcb0991SDimitry Andric   assert(TypeIdx == 0 && "not a vector type index");
28428bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
28438bcb0991SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
28448bcb0991SDimitry Andric   LLT SrcTy = DstTy.getElementType();
28458bcb0991SDimitry Andric 
28468bcb0991SDimitry Andric   int DstNumElts = DstTy.getNumElements();
28478bcb0991SDimitry Andric   int NarrowNumElts = NarrowTy.getNumElements();
28488bcb0991SDimitry Andric   int NumConcat = (DstNumElts + NarrowNumElts - 1) / NarrowNumElts;
28498bcb0991SDimitry Andric   LLT WidenedDstTy = LLT::vector(NarrowNumElts * NumConcat, SrcTy);
28508bcb0991SDimitry Andric 
28518bcb0991SDimitry Andric   SmallVector<Register, 8> ConcatOps;
28528bcb0991SDimitry Andric   SmallVector<Register, 8> SubBuildVector;
28538bcb0991SDimitry Andric 
28548bcb0991SDimitry Andric   Register UndefReg;
28558bcb0991SDimitry Andric   if (WidenedDstTy != DstTy)
28568bcb0991SDimitry Andric     UndefReg = MIRBuilder.buildUndef(SrcTy).getReg(0);
28578bcb0991SDimitry Andric 
28588bcb0991SDimitry Andric   // Create a G_CONCAT_VECTORS of NarrowTy pieces, padding with undef as
28598bcb0991SDimitry Andric   // necessary.
28608bcb0991SDimitry Andric   //
28618bcb0991SDimitry Andric   // %3:_(<3 x s16>) = G_BUILD_VECTOR %0, %1, %2
28628bcb0991SDimitry Andric   //   -> <2 x s16>
28638bcb0991SDimitry Andric   //
28648bcb0991SDimitry Andric   // %4:_(s16) = G_IMPLICIT_DEF
28658bcb0991SDimitry Andric   // %5:_(<2 x s16>) = G_BUILD_VECTOR %0, %1
28668bcb0991SDimitry Andric   // %6:_(<2 x s16>) = G_BUILD_VECTOR %2, %4
28678bcb0991SDimitry Andric   // %7:_(<4 x s16>) = G_CONCAT_VECTORS %5, %6
28688bcb0991SDimitry Andric   // %3:_(<3 x s16>) = G_EXTRACT %7, 0
28698bcb0991SDimitry Andric   for (int I = 0; I != NumConcat; ++I) {
28708bcb0991SDimitry Andric     for (int J = 0; J != NarrowNumElts; ++J) {
28718bcb0991SDimitry Andric       int SrcIdx = NarrowNumElts * I + J;
28728bcb0991SDimitry Andric 
28738bcb0991SDimitry Andric       if (SrcIdx < DstNumElts) {
28748bcb0991SDimitry Andric         Register SrcReg = MI.getOperand(SrcIdx + 1).getReg();
28758bcb0991SDimitry Andric         SubBuildVector.push_back(SrcReg);
28768bcb0991SDimitry Andric       } else
28778bcb0991SDimitry Andric         SubBuildVector.push_back(UndefReg);
28788bcb0991SDimitry Andric     }
28798bcb0991SDimitry Andric 
28808bcb0991SDimitry Andric     auto BuildVec = MIRBuilder.buildBuildVector(NarrowTy, SubBuildVector);
28818bcb0991SDimitry Andric     ConcatOps.push_back(BuildVec.getReg(0));
28828bcb0991SDimitry Andric     SubBuildVector.clear();
28838bcb0991SDimitry Andric   }
28848bcb0991SDimitry Andric 
28858bcb0991SDimitry Andric   if (DstTy == WidenedDstTy)
28868bcb0991SDimitry Andric     MIRBuilder.buildConcatVectors(DstReg, ConcatOps);
28878bcb0991SDimitry Andric   else {
28888bcb0991SDimitry Andric     auto Concat = MIRBuilder.buildConcatVectors(WidenedDstTy, ConcatOps);
28898bcb0991SDimitry Andric     MIRBuilder.buildExtract(DstReg, Concat, 0);
28908bcb0991SDimitry Andric   }
28918bcb0991SDimitry Andric 
28928bcb0991SDimitry Andric   MI.eraseFromParent();
28938bcb0991SDimitry Andric   return Legalized;
28948bcb0991SDimitry Andric }
28958bcb0991SDimitry Andric 
28968bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
28970b57cec5SDimitry Andric LegalizerHelper::reduceLoadStoreWidth(MachineInstr &MI, unsigned TypeIdx,
28980b57cec5SDimitry Andric                                       LLT NarrowTy) {
28990b57cec5SDimitry Andric   // FIXME: Don't know how to handle secondary types yet.
29000b57cec5SDimitry Andric   if (TypeIdx != 0)
29010b57cec5SDimitry Andric     return UnableToLegalize;
29020b57cec5SDimitry Andric 
29030b57cec5SDimitry Andric   MachineMemOperand *MMO = *MI.memoperands_begin();
29040b57cec5SDimitry Andric 
29050b57cec5SDimitry Andric   // This implementation doesn't work for atomics. Give up instead of doing
29060b57cec5SDimitry Andric   // something invalid.
29070b57cec5SDimitry Andric   if (MMO->getOrdering() != AtomicOrdering::NotAtomic ||
29080b57cec5SDimitry Andric       MMO->getFailureOrdering() != AtomicOrdering::NotAtomic)
29090b57cec5SDimitry Andric     return UnableToLegalize;
29100b57cec5SDimitry Andric 
29110b57cec5SDimitry Andric   bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD;
29120b57cec5SDimitry Andric   Register ValReg = MI.getOperand(0).getReg();
29130b57cec5SDimitry Andric   Register AddrReg = MI.getOperand(1).getReg();
29140b57cec5SDimitry Andric   LLT ValTy = MRI.getType(ValReg);
29150b57cec5SDimitry Andric 
29160b57cec5SDimitry Andric   int NumParts = -1;
29170b57cec5SDimitry Andric   int NumLeftover = -1;
29180b57cec5SDimitry Andric   LLT LeftoverTy;
29190b57cec5SDimitry Andric   SmallVector<Register, 8> NarrowRegs, NarrowLeftoverRegs;
29200b57cec5SDimitry Andric   if (IsLoad) {
29210b57cec5SDimitry Andric     std::tie(NumParts, NumLeftover) = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy);
29220b57cec5SDimitry Andric   } else {
29230b57cec5SDimitry Andric     if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs,
29240b57cec5SDimitry Andric                      NarrowLeftoverRegs)) {
29250b57cec5SDimitry Andric       NumParts = NarrowRegs.size();
29260b57cec5SDimitry Andric       NumLeftover = NarrowLeftoverRegs.size();
29270b57cec5SDimitry Andric     }
29280b57cec5SDimitry Andric   }
29290b57cec5SDimitry Andric 
29300b57cec5SDimitry Andric   if (NumParts == -1)
29310b57cec5SDimitry Andric     return UnableToLegalize;
29320b57cec5SDimitry Andric 
29330b57cec5SDimitry Andric   const LLT OffsetTy = LLT::scalar(MRI.getType(AddrReg).getScalarSizeInBits());
29340b57cec5SDimitry Andric 
29350b57cec5SDimitry Andric   unsigned TotalSize = ValTy.getSizeInBits();
29360b57cec5SDimitry Andric 
29370b57cec5SDimitry Andric   // Split the load/store into PartTy sized pieces starting at Offset. If this
29380b57cec5SDimitry Andric   // is a load, return the new registers in ValRegs. For a store, each elements
29390b57cec5SDimitry Andric   // of ValRegs should be PartTy. Returns the next offset that needs to be
29400b57cec5SDimitry Andric   // handled.
29410b57cec5SDimitry Andric   auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<Register> &ValRegs,
29420b57cec5SDimitry Andric                              unsigned Offset) -> unsigned {
29430b57cec5SDimitry Andric     MachineFunction &MF = MIRBuilder.getMF();
29440b57cec5SDimitry Andric     unsigned PartSize = PartTy.getSizeInBits();
29450b57cec5SDimitry Andric     for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize;
29460b57cec5SDimitry Andric          Offset += PartSize, ++Idx) {
29470b57cec5SDimitry Andric       unsigned ByteSize = PartSize / 8;
29480b57cec5SDimitry Andric       unsigned ByteOffset = Offset / 8;
29490b57cec5SDimitry Andric       Register NewAddrReg;
29500b57cec5SDimitry Andric 
2951*480093f4SDimitry Andric       MIRBuilder.materializePtrAdd(NewAddrReg, AddrReg, OffsetTy, ByteOffset);
29520b57cec5SDimitry Andric 
29530b57cec5SDimitry Andric       MachineMemOperand *NewMMO =
29540b57cec5SDimitry Andric         MF.getMachineMemOperand(MMO, ByteOffset, ByteSize);
29550b57cec5SDimitry Andric 
29560b57cec5SDimitry Andric       if (IsLoad) {
29570b57cec5SDimitry Andric         Register Dst = MRI.createGenericVirtualRegister(PartTy);
29580b57cec5SDimitry Andric         ValRegs.push_back(Dst);
29590b57cec5SDimitry Andric         MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO);
29600b57cec5SDimitry Andric       } else {
29610b57cec5SDimitry Andric         MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO);
29620b57cec5SDimitry Andric       }
29630b57cec5SDimitry Andric     }
29640b57cec5SDimitry Andric 
29650b57cec5SDimitry Andric     return Offset;
29660b57cec5SDimitry Andric   };
29670b57cec5SDimitry Andric 
29680b57cec5SDimitry Andric   unsigned HandledOffset = splitTypePieces(NarrowTy, NarrowRegs, 0);
29690b57cec5SDimitry Andric 
29700b57cec5SDimitry Andric   // Handle the rest of the register if this isn't an even type breakdown.
29710b57cec5SDimitry Andric   if (LeftoverTy.isValid())
29720b57cec5SDimitry Andric     splitTypePieces(LeftoverTy, NarrowLeftoverRegs, HandledOffset);
29730b57cec5SDimitry Andric 
29740b57cec5SDimitry Andric   if (IsLoad) {
29750b57cec5SDimitry Andric     insertParts(ValReg, ValTy, NarrowTy, NarrowRegs,
29760b57cec5SDimitry Andric                 LeftoverTy, NarrowLeftoverRegs);
29770b57cec5SDimitry Andric   }
29780b57cec5SDimitry Andric 
29790b57cec5SDimitry Andric   MI.eraseFromParent();
29800b57cec5SDimitry Andric   return Legalized;
29810b57cec5SDimitry Andric }
29820b57cec5SDimitry Andric 
29830b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
29840b57cec5SDimitry Andric LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx,
29850b57cec5SDimitry Andric                                      LLT NarrowTy) {
29860b57cec5SDimitry Andric   using namespace TargetOpcode;
29870b57cec5SDimitry Andric 
29880b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
29890b57cec5SDimitry Andric   switch (MI.getOpcode()) {
29900b57cec5SDimitry Andric   case G_IMPLICIT_DEF:
29910b57cec5SDimitry Andric     return fewerElementsVectorImplicitDef(MI, TypeIdx, NarrowTy);
29920b57cec5SDimitry Andric   case G_AND:
29930b57cec5SDimitry Andric   case G_OR:
29940b57cec5SDimitry Andric   case G_XOR:
29950b57cec5SDimitry Andric   case G_ADD:
29960b57cec5SDimitry Andric   case G_SUB:
29970b57cec5SDimitry Andric   case G_MUL:
29980b57cec5SDimitry Andric   case G_SMULH:
29990b57cec5SDimitry Andric   case G_UMULH:
30000b57cec5SDimitry Andric   case G_FADD:
30010b57cec5SDimitry Andric   case G_FMUL:
30020b57cec5SDimitry Andric   case G_FSUB:
30030b57cec5SDimitry Andric   case G_FNEG:
30040b57cec5SDimitry Andric   case G_FABS:
30050b57cec5SDimitry Andric   case G_FCANONICALIZE:
30060b57cec5SDimitry Andric   case G_FDIV:
30070b57cec5SDimitry Andric   case G_FREM:
30080b57cec5SDimitry Andric   case G_FMA:
30098bcb0991SDimitry Andric   case G_FMAD:
30100b57cec5SDimitry Andric   case G_FPOW:
30110b57cec5SDimitry Andric   case G_FEXP:
30120b57cec5SDimitry Andric   case G_FEXP2:
30130b57cec5SDimitry Andric   case G_FLOG:
30140b57cec5SDimitry Andric   case G_FLOG2:
30150b57cec5SDimitry Andric   case G_FLOG10:
30160b57cec5SDimitry Andric   case G_FNEARBYINT:
30170b57cec5SDimitry Andric   case G_FCEIL:
30180b57cec5SDimitry Andric   case G_FFLOOR:
30190b57cec5SDimitry Andric   case G_FRINT:
30200b57cec5SDimitry Andric   case G_INTRINSIC_ROUND:
30210b57cec5SDimitry Andric   case G_INTRINSIC_TRUNC:
30220b57cec5SDimitry Andric   case G_FCOS:
30230b57cec5SDimitry Andric   case G_FSIN:
30240b57cec5SDimitry Andric   case G_FSQRT:
30250b57cec5SDimitry Andric   case G_BSWAP:
30268bcb0991SDimitry Andric   case G_BITREVERSE:
30270b57cec5SDimitry Andric   case G_SDIV:
3028*480093f4SDimitry Andric   case G_UDIV:
3029*480093f4SDimitry Andric   case G_SREM:
3030*480093f4SDimitry Andric   case G_UREM:
30310b57cec5SDimitry Andric   case G_SMIN:
30320b57cec5SDimitry Andric   case G_SMAX:
30330b57cec5SDimitry Andric   case G_UMIN:
30340b57cec5SDimitry Andric   case G_UMAX:
30350b57cec5SDimitry Andric   case G_FMINNUM:
30360b57cec5SDimitry Andric   case G_FMAXNUM:
30370b57cec5SDimitry Andric   case G_FMINNUM_IEEE:
30380b57cec5SDimitry Andric   case G_FMAXNUM_IEEE:
30390b57cec5SDimitry Andric   case G_FMINIMUM:
30400b57cec5SDimitry Andric   case G_FMAXIMUM:
30410b57cec5SDimitry Andric     return fewerElementsVectorBasic(MI, TypeIdx, NarrowTy);
30420b57cec5SDimitry Andric   case G_SHL:
30430b57cec5SDimitry Andric   case G_LSHR:
30440b57cec5SDimitry Andric   case G_ASHR:
30450b57cec5SDimitry Andric   case G_CTLZ:
30460b57cec5SDimitry Andric   case G_CTLZ_ZERO_UNDEF:
30470b57cec5SDimitry Andric   case G_CTTZ:
30480b57cec5SDimitry Andric   case G_CTTZ_ZERO_UNDEF:
30490b57cec5SDimitry Andric   case G_CTPOP:
30500b57cec5SDimitry Andric   case G_FCOPYSIGN:
30510b57cec5SDimitry Andric     return fewerElementsVectorMultiEltType(MI, TypeIdx, NarrowTy);
30520b57cec5SDimitry Andric   case G_ZEXT:
30530b57cec5SDimitry Andric   case G_SEXT:
30540b57cec5SDimitry Andric   case G_ANYEXT:
30550b57cec5SDimitry Andric   case G_FPEXT:
30560b57cec5SDimitry Andric   case G_FPTRUNC:
30570b57cec5SDimitry Andric   case G_SITOFP:
30580b57cec5SDimitry Andric   case G_UITOFP:
30590b57cec5SDimitry Andric   case G_FPTOSI:
30600b57cec5SDimitry Andric   case G_FPTOUI:
30610b57cec5SDimitry Andric   case G_INTTOPTR:
30620b57cec5SDimitry Andric   case G_PTRTOINT:
30630b57cec5SDimitry Andric   case G_ADDRSPACE_CAST:
30640b57cec5SDimitry Andric     return fewerElementsVectorCasts(MI, TypeIdx, NarrowTy);
30650b57cec5SDimitry Andric   case G_ICMP:
30660b57cec5SDimitry Andric   case G_FCMP:
30670b57cec5SDimitry Andric     return fewerElementsVectorCmp(MI, TypeIdx, NarrowTy);
30680b57cec5SDimitry Andric   case G_SELECT:
30690b57cec5SDimitry Andric     return fewerElementsVectorSelect(MI, TypeIdx, NarrowTy);
30700b57cec5SDimitry Andric   case G_PHI:
30710b57cec5SDimitry Andric     return fewerElementsVectorPhi(MI, TypeIdx, NarrowTy);
30728bcb0991SDimitry Andric   case G_UNMERGE_VALUES:
30738bcb0991SDimitry Andric     return fewerElementsVectorUnmergeValues(MI, TypeIdx, NarrowTy);
30748bcb0991SDimitry Andric   case G_BUILD_VECTOR:
30758bcb0991SDimitry Andric     return fewerElementsVectorBuildVector(MI, TypeIdx, NarrowTy);
30760b57cec5SDimitry Andric   case G_LOAD:
30770b57cec5SDimitry Andric   case G_STORE:
30780b57cec5SDimitry Andric     return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy);
30790b57cec5SDimitry Andric   default:
30800b57cec5SDimitry Andric     return UnableToLegalize;
30810b57cec5SDimitry Andric   }
30820b57cec5SDimitry Andric }
30830b57cec5SDimitry Andric 
30840b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
30850b57cec5SDimitry Andric LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt,
30860b57cec5SDimitry Andric                                              const LLT HalfTy, const LLT AmtTy) {
30870b57cec5SDimitry Andric 
30880b57cec5SDimitry Andric   Register InL = MRI.createGenericVirtualRegister(HalfTy);
30890b57cec5SDimitry Andric   Register InH = MRI.createGenericVirtualRegister(HalfTy);
30900b57cec5SDimitry Andric   MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg());
30910b57cec5SDimitry Andric 
30920b57cec5SDimitry Andric   if (Amt.isNullValue()) {
30930b57cec5SDimitry Andric     MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {InL, InH});
30940b57cec5SDimitry Andric     MI.eraseFromParent();
30950b57cec5SDimitry Andric     return Legalized;
30960b57cec5SDimitry Andric   }
30970b57cec5SDimitry Andric 
30980b57cec5SDimitry Andric   LLT NVT = HalfTy;
30990b57cec5SDimitry Andric   unsigned NVTBits = HalfTy.getSizeInBits();
31000b57cec5SDimitry Andric   unsigned VTBits = 2 * NVTBits;
31010b57cec5SDimitry Andric 
31020b57cec5SDimitry Andric   SrcOp Lo(Register(0)), Hi(Register(0));
31030b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_SHL) {
31040b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
31050b57cec5SDimitry Andric       Lo = Hi = MIRBuilder.buildConstant(NVT, 0);
31060b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
31070b57cec5SDimitry Andric       Lo = MIRBuilder.buildConstant(NVT, 0);
31080b57cec5SDimitry Andric       Hi = MIRBuilder.buildShl(NVT, InL,
31090b57cec5SDimitry Andric                                MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
31100b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
31110b57cec5SDimitry Andric       Lo = MIRBuilder.buildConstant(NVT, 0);
31120b57cec5SDimitry Andric       Hi = InL;
31130b57cec5SDimitry Andric     } else {
31140b57cec5SDimitry Andric       Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt));
31150b57cec5SDimitry Andric       auto OrLHS =
31160b57cec5SDimitry Andric           MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt));
31170b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildLShr(
31180b57cec5SDimitry Andric           NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
31190b57cec5SDimitry Andric       Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
31200b57cec5SDimitry Andric     }
31210b57cec5SDimitry Andric   } else if (MI.getOpcode() == TargetOpcode::G_LSHR) {
31220b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
31230b57cec5SDimitry Andric       Lo = Hi = MIRBuilder.buildConstant(NVT, 0);
31240b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
31250b57cec5SDimitry Andric       Lo = MIRBuilder.buildLShr(NVT, InH,
31260b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
31270b57cec5SDimitry Andric       Hi = MIRBuilder.buildConstant(NVT, 0);
31280b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
31290b57cec5SDimitry Andric       Lo = InH;
31300b57cec5SDimitry Andric       Hi = MIRBuilder.buildConstant(NVT, 0);
31310b57cec5SDimitry Andric     } else {
31320b57cec5SDimitry Andric       auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt);
31330b57cec5SDimitry Andric 
31340b57cec5SDimitry Andric       auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst);
31350b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildShl(
31360b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
31370b57cec5SDimitry Andric 
31380b57cec5SDimitry Andric       Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
31390b57cec5SDimitry Andric       Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst);
31400b57cec5SDimitry Andric     }
31410b57cec5SDimitry Andric   } else {
31420b57cec5SDimitry Andric     if (Amt.ugt(VTBits)) {
31430b57cec5SDimitry Andric       Hi = Lo = MIRBuilder.buildAShr(
31440b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
31450b57cec5SDimitry Andric     } else if (Amt.ugt(NVTBits)) {
31460b57cec5SDimitry Andric       Lo = MIRBuilder.buildAShr(NVT, InH,
31470b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, Amt - NVTBits));
31480b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH,
31490b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
31500b57cec5SDimitry Andric     } else if (Amt == NVTBits) {
31510b57cec5SDimitry Andric       Lo = InH;
31520b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH,
31530b57cec5SDimitry Andric                                 MIRBuilder.buildConstant(AmtTy, NVTBits - 1));
31540b57cec5SDimitry Andric     } else {
31550b57cec5SDimitry Andric       auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt);
31560b57cec5SDimitry Andric 
31570b57cec5SDimitry Andric       auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst);
31580b57cec5SDimitry Andric       auto OrRHS = MIRBuilder.buildShl(
31590b57cec5SDimitry Andric           NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits));
31600b57cec5SDimitry Andric 
31610b57cec5SDimitry Andric       Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS);
31620b57cec5SDimitry Andric       Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst);
31630b57cec5SDimitry Andric     }
31640b57cec5SDimitry Andric   }
31650b57cec5SDimitry Andric 
31660b57cec5SDimitry Andric   MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {Lo.getReg(), Hi.getReg()});
31670b57cec5SDimitry Andric   MI.eraseFromParent();
31680b57cec5SDimitry Andric 
31690b57cec5SDimitry Andric   return Legalized;
31700b57cec5SDimitry Andric }
31710b57cec5SDimitry Andric 
31720b57cec5SDimitry Andric // TODO: Optimize if constant shift amount.
31730b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
31740b57cec5SDimitry Andric LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx,
31750b57cec5SDimitry Andric                                    LLT RequestedTy) {
31760b57cec5SDimitry Andric   if (TypeIdx == 1) {
31770b57cec5SDimitry Andric     Observer.changingInstr(MI);
31780b57cec5SDimitry Andric     narrowScalarSrc(MI, RequestedTy, 2);
31790b57cec5SDimitry Andric     Observer.changedInstr(MI);
31800b57cec5SDimitry Andric     return Legalized;
31810b57cec5SDimitry Andric   }
31820b57cec5SDimitry Andric 
31830b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
31840b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
31850b57cec5SDimitry Andric   if (DstTy.isVector())
31860b57cec5SDimitry Andric     return UnableToLegalize;
31870b57cec5SDimitry Andric 
31880b57cec5SDimitry Andric   Register Amt = MI.getOperand(2).getReg();
31890b57cec5SDimitry Andric   LLT ShiftAmtTy = MRI.getType(Amt);
31900b57cec5SDimitry Andric   const unsigned DstEltSize = DstTy.getScalarSizeInBits();
31910b57cec5SDimitry Andric   if (DstEltSize % 2 != 0)
31920b57cec5SDimitry Andric     return UnableToLegalize;
31930b57cec5SDimitry Andric 
31940b57cec5SDimitry Andric   // Ignore the input type. We can only go to exactly half the size of the
31950b57cec5SDimitry Andric   // input. If that isn't small enough, the resulting pieces will be further
31960b57cec5SDimitry Andric   // legalized.
31970b57cec5SDimitry Andric   const unsigned NewBitSize = DstEltSize / 2;
31980b57cec5SDimitry Andric   const LLT HalfTy = LLT::scalar(NewBitSize);
31990b57cec5SDimitry Andric   const LLT CondTy = LLT::scalar(1);
32000b57cec5SDimitry Andric 
32010b57cec5SDimitry Andric   if (const MachineInstr *KShiftAmt =
32020b57cec5SDimitry Andric           getOpcodeDef(TargetOpcode::G_CONSTANT, Amt, MRI)) {
32030b57cec5SDimitry Andric     return narrowScalarShiftByConstant(
32040b57cec5SDimitry Andric         MI, KShiftAmt->getOperand(1).getCImm()->getValue(), HalfTy, ShiftAmtTy);
32050b57cec5SDimitry Andric   }
32060b57cec5SDimitry Andric 
32070b57cec5SDimitry Andric   // TODO: Expand with known bits.
32080b57cec5SDimitry Andric 
32090b57cec5SDimitry Andric   // Handle the fully general expansion by an unknown amount.
32100b57cec5SDimitry Andric   auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize);
32110b57cec5SDimitry Andric 
32120b57cec5SDimitry Andric   Register InL = MRI.createGenericVirtualRegister(HalfTy);
32130b57cec5SDimitry Andric   Register InH = MRI.createGenericVirtualRegister(HalfTy);
32140b57cec5SDimitry Andric   MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg());
32150b57cec5SDimitry Andric 
32160b57cec5SDimitry Andric   auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits);
32170b57cec5SDimitry Andric   auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt);
32180b57cec5SDimitry Andric 
32190b57cec5SDimitry Andric   auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0);
32200b57cec5SDimitry Andric   auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits);
32210b57cec5SDimitry Andric   auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero);
32220b57cec5SDimitry Andric 
32230b57cec5SDimitry Andric   Register ResultRegs[2];
32240b57cec5SDimitry Andric   switch (MI.getOpcode()) {
32250b57cec5SDimitry Andric   case TargetOpcode::G_SHL: {
32260b57cec5SDimitry Andric     // Short: ShAmt < NewBitSize
32278bcb0991SDimitry Andric     auto LoS = MIRBuilder.buildShl(HalfTy, InL, Amt);
32280b57cec5SDimitry Andric 
32298bcb0991SDimitry Andric     auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, AmtLack);
32308bcb0991SDimitry Andric     auto HiOr = MIRBuilder.buildShl(HalfTy, InH, Amt);
32318bcb0991SDimitry Andric     auto HiS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr);
32320b57cec5SDimitry Andric 
32330b57cec5SDimitry Andric     // Long: ShAmt >= NewBitSize
32340b57cec5SDimitry Andric     auto LoL = MIRBuilder.buildConstant(HalfTy, 0);         // Lo part is zero.
32350b57cec5SDimitry Andric     auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part.
32360b57cec5SDimitry Andric 
32370b57cec5SDimitry Andric     auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL);
32380b57cec5SDimitry Andric     auto Hi = MIRBuilder.buildSelect(
32390b57cec5SDimitry Andric         HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL));
32400b57cec5SDimitry Andric 
32410b57cec5SDimitry Andric     ResultRegs[0] = Lo.getReg(0);
32420b57cec5SDimitry Andric     ResultRegs[1] = Hi.getReg(0);
32430b57cec5SDimitry Andric     break;
32440b57cec5SDimitry Andric   }
32458bcb0991SDimitry Andric   case TargetOpcode::G_LSHR:
32460b57cec5SDimitry Andric   case TargetOpcode::G_ASHR: {
32470b57cec5SDimitry Andric     // Short: ShAmt < NewBitSize
32488bcb0991SDimitry Andric     auto HiS = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, {InH, Amt});
32490b57cec5SDimitry Andric 
32508bcb0991SDimitry Andric     auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, Amt);
32518bcb0991SDimitry Andric     auto HiOr = MIRBuilder.buildShl(HalfTy, InH, AmtLack);
32528bcb0991SDimitry Andric     auto LoS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr);
32530b57cec5SDimitry Andric 
32540b57cec5SDimitry Andric     // Long: ShAmt >= NewBitSize
32558bcb0991SDimitry Andric     MachineInstrBuilder HiL;
32568bcb0991SDimitry Andric     if (MI.getOpcode() == TargetOpcode::G_LSHR) {
32578bcb0991SDimitry Andric       HiL = MIRBuilder.buildConstant(HalfTy, 0);            // Hi part is zero.
32588bcb0991SDimitry Andric     } else {
32598bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1);
32608bcb0991SDimitry Andric       HiL = MIRBuilder.buildAShr(HalfTy, InH, ShiftAmt);    // Sign of Hi part.
32618bcb0991SDimitry Andric     }
32628bcb0991SDimitry Andric     auto LoL = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy},
32638bcb0991SDimitry Andric                                      {InH, AmtExcess});     // Lo from Hi part.
32640b57cec5SDimitry Andric 
32650b57cec5SDimitry Andric     auto Lo = MIRBuilder.buildSelect(
32660b57cec5SDimitry Andric         HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL));
32670b57cec5SDimitry Andric 
32680b57cec5SDimitry Andric     auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL);
32690b57cec5SDimitry Andric 
32700b57cec5SDimitry Andric     ResultRegs[0] = Lo.getReg(0);
32710b57cec5SDimitry Andric     ResultRegs[1] = Hi.getReg(0);
32720b57cec5SDimitry Andric     break;
32730b57cec5SDimitry Andric   }
32740b57cec5SDimitry Andric   default:
32750b57cec5SDimitry Andric     llvm_unreachable("not a shift");
32760b57cec5SDimitry Andric   }
32770b57cec5SDimitry Andric 
32780b57cec5SDimitry Andric   MIRBuilder.buildMerge(DstReg, ResultRegs);
32790b57cec5SDimitry Andric   MI.eraseFromParent();
32800b57cec5SDimitry Andric   return Legalized;
32810b57cec5SDimitry Andric }
32820b57cec5SDimitry Andric 
32830b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
32840b57cec5SDimitry Andric LegalizerHelper::moreElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx,
32850b57cec5SDimitry Andric                                        LLT MoreTy) {
32860b57cec5SDimitry Andric   assert(TypeIdx == 0 && "Expecting only Idx 0");
32870b57cec5SDimitry Andric 
32880b57cec5SDimitry Andric   Observer.changingInstr(MI);
32890b57cec5SDimitry Andric   for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
32900b57cec5SDimitry Andric     MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB();
32910b57cec5SDimitry Andric     MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator());
32920b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, I);
32930b57cec5SDimitry Andric   }
32940b57cec5SDimitry Andric 
32950b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
32960b57cec5SDimitry Andric   MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI());
32970b57cec5SDimitry Andric   moreElementsVectorDst(MI, MoreTy, 0);
32980b57cec5SDimitry Andric   Observer.changedInstr(MI);
32990b57cec5SDimitry Andric   return Legalized;
33000b57cec5SDimitry Andric }
33010b57cec5SDimitry Andric 
33020b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
33030b57cec5SDimitry Andric LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx,
33040b57cec5SDimitry Andric                                     LLT MoreTy) {
33050b57cec5SDimitry Andric   MIRBuilder.setInstr(MI);
33060b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
33070b57cec5SDimitry Andric   switch (Opc) {
33088bcb0991SDimitry Andric   case TargetOpcode::G_IMPLICIT_DEF:
33098bcb0991SDimitry Andric   case TargetOpcode::G_LOAD: {
33108bcb0991SDimitry Andric     if (TypeIdx != 0)
33118bcb0991SDimitry Andric       return UnableToLegalize;
33120b57cec5SDimitry Andric     Observer.changingInstr(MI);
33130b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
33140b57cec5SDimitry Andric     Observer.changedInstr(MI);
33150b57cec5SDimitry Andric     return Legalized;
33160b57cec5SDimitry Andric   }
33178bcb0991SDimitry Andric   case TargetOpcode::G_STORE:
33188bcb0991SDimitry Andric     if (TypeIdx != 0)
33198bcb0991SDimitry Andric       return UnableToLegalize;
33208bcb0991SDimitry Andric     Observer.changingInstr(MI);
33218bcb0991SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 0);
33228bcb0991SDimitry Andric     Observer.changedInstr(MI);
33238bcb0991SDimitry Andric     return Legalized;
33240b57cec5SDimitry Andric   case TargetOpcode::G_AND:
33250b57cec5SDimitry Andric   case TargetOpcode::G_OR:
33260b57cec5SDimitry Andric   case TargetOpcode::G_XOR:
33270b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
33280b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
33290b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
3330*480093f4SDimitry Andric   case TargetOpcode::G_UMAX:
3331*480093f4SDimitry Andric   case TargetOpcode::G_FMINNUM:
3332*480093f4SDimitry Andric   case TargetOpcode::G_FMAXNUM:
3333*480093f4SDimitry Andric   case TargetOpcode::G_FMINNUM_IEEE:
3334*480093f4SDimitry Andric   case TargetOpcode::G_FMAXNUM_IEEE:
3335*480093f4SDimitry Andric   case TargetOpcode::G_FMINIMUM:
3336*480093f4SDimitry Andric   case TargetOpcode::G_FMAXIMUM: {
33370b57cec5SDimitry Andric     Observer.changingInstr(MI);
33380b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
33390b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 2);
33400b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
33410b57cec5SDimitry Andric     Observer.changedInstr(MI);
33420b57cec5SDimitry Andric     return Legalized;
33430b57cec5SDimitry Andric   }
33440b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT:
33450b57cec5SDimitry Andric     if (TypeIdx != 1)
33460b57cec5SDimitry Andric       return UnableToLegalize;
33470b57cec5SDimitry Andric     Observer.changingInstr(MI);
33480b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
33490b57cec5SDimitry Andric     Observer.changedInstr(MI);
33500b57cec5SDimitry Andric     return Legalized;
33510b57cec5SDimitry Andric   case TargetOpcode::G_INSERT:
33520b57cec5SDimitry Andric     if (TypeIdx != 0)
33530b57cec5SDimitry Andric       return UnableToLegalize;
33540b57cec5SDimitry Andric     Observer.changingInstr(MI);
33550b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 1);
33560b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
33570b57cec5SDimitry Andric     Observer.changedInstr(MI);
33580b57cec5SDimitry Andric     return Legalized;
33590b57cec5SDimitry Andric   case TargetOpcode::G_SELECT:
33600b57cec5SDimitry Andric     if (TypeIdx != 0)
33610b57cec5SDimitry Andric       return UnableToLegalize;
33620b57cec5SDimitry Andric     if (MRI.getType(MI.getOperand(1).getReg()).isVector())
33630b57cec5SDimitry Andric       return UnableToLegalize;
33640b57cec5SDimitry Andric 
33650b57cec5SDimitry Andric     Observer.changingInstr(MI);
33660b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 2);
33670b57cec5SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, 3);
33680b57cec5SDimitry Andric     moreElementsVectorDst(MI, MoreTy, 0);
33690b57cec5SDimitry Andric     Observer.changedInstr(MI);
33700b57cec5SDimitry Andric     return Legalized;
33718bcb0991SDimitry Andric   case TargetOpcode::G_UNMERGE_VALUES: {
33728bcb0991SDimitry Andric     if (TypeIdx != 1)
33738bcb0991SDimitry Andric       return UnableToLegalize;
33748bcb0991SDimitry Andric 
33758bcb0991SDimitry Andric     LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
33768bcb0991SDimitry Andric     int NumDst = MI.getNumOperands() - 1;
33778bcb0991SDimitry Andric     moreElementsVectorSrc(MI, MoreTy, NumDst);
33788bcb0991SDimitry Andric 
33798bcb0991SDimitry Andric     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES);
33808bcb0991SDimitry Andric     for (int I = 0; I != NumDst; ++I)
33818bcb0991SDimitry Andric       MIB.addDef(MI.getOperand(I).getReg());
33828bcb0991SDimitry Andric 
33838bcb0991SDimitry Andric     int NewNumDst = MoreTy.getSizeInBits() / DstTy.getSizeInBits();
33848bcb0991SDimitry Andric     for (int I = NumDst; I != NewNumDst; ++I)
33858bcb0991SDimitry Andric       MIB.addDef(MRI.createGenericVirtualRegister(DstTy));
33868bcb0991SDimitry Andric 
33878bcb0991SDimitry Andric     MIB.addUse(MI.getOperand(NumDst).getReg());
33888bcb0991SDimitry Andric     MI.eraseFromParent();
33898bcb0991SDimitry Andric     return Legalized;
33908bcb0991SDimitry Andric   }
33910b57cec5SDimitry Andric   case TargetOpcode::G_PHI:
33920b57cec5SDimitry Andric     return moreElementsVectorPhi(MI, TypeIdx, MoreTy);
33930b57cec5SDimitry Andric   default:
33940b57cec5SDimitry Andric     return UnableToLegalize;
33950b57cec5SDimitry Andric   }
33960b57cec5SDimitry Andric }
33970b57cec5SDimitry Andric 
33980b57cec5SDimitry Andric void LegalizerHelper::multiplyRegisters(SmallVectorImpl<Register> &DstRegs,
33990b57cec5SDimitry Andric                                         ArrayRef<Register> Src1Regs,
34000b57cec5SDimitry Andric                                         ArrayRef<Register> Src2Regs,
34010b57cec5SDimitry Andric                                         LLT NarrowTy) {
34020b57cec5SDimitry Andric   MachineIRBuilder &B = MIRBuilder;
34030b57cec5SDimitry Andric   unsigned SrcParts = Src1Regs.size();
34040b57cec5SDimitry Andric   unsigned DstParts = DstRegs.size();
34050b57cec5SDimitry Andric 
34060b57cec5SDimitry Andric   unsigned DstIdx = 0; // Low bits of the result.
34070b57cec5SDimitry Andric   Register FactorSum =
34080b57cec5SDimitry Andric       B.buildMul(NarrowTy, Src1Regs[DstIdx], Src2Regs[DstIdx]).getReg(0);
34090b57cec5SDimitry Andric   DstRegs[DstIdx] = FactorSum;
34100b57cec5SDimitry Andric 
34110b57cec5SDimitry Andric   unsigned CarrySumPrevDstIdx;
34120b57cec5SDimitry Andric   SmallVector<Register, 4> Factors;
34130b57cec5SDimitry Andric 
34140b57cec5SDimitry Andric   for (DstIdx = 1; DstIdx < DstParts; DstIdx++) {
34150b57cec5SDimitry Andric     // Collect low parts of muls for DstIdx.
34160b57cec5SDimitry Andric     for (unsigned i = DstIdx + 1 < SrcParts ? 0 : DstIdx - SrcParts + 1;
34170b57cec5SDimitry Andric          i <= std::min(DstIdx, SrcParts - 1); ++i) {
34180b57cec5SDimitry Andric       MachineInstrBuilder Mul =
34190b57cec5SDimitry Andric           B.buildMul(NarrowTy, Src1Regs[DstIdx - i], Src2Regs[i]);
34200b57cec5SDimitry Andric       Factors.push_back(Mul.getReg(0));
34210b57cec5SDimitry Andric     }
34220b57cec5SDimitry Andric     // Collect high parts of muls from previous DstIdx.
34230b57cec5SDimitry Andric     for (unsigned i = DstIdx < SrcParts ? 0 : DstIdx - SrcParts;
34240b57cec5SDimitry Andric          i <= std::min(DstIdx - 1, SrcParts - 1); ++i) {
34250b57cec5SDimitry Andric       MachineInstrBuilder Umulh =
34260b57cec5SDimitry Andric           B.buildUMulH(NarrowTy, Src1Regs[DstIdx - 1 - i], Src2Regs[i]);
34270b57cec5SDimitry Andric       Factors.push_back(Umulh.getReg(0));
34280b57cec5SDimitry Andric     }
3429*480093f4SDimitry Andric     // Add CarrySum from additions calculated for previous DstIdx.
34300b57cec5SDimitry Andric     if (DstIdx != 1) {
34310b57cec5SDimitry Andric       Factors.push_back(CarrySumPrevDstIdx);
34320b57cec5SDimitry Andric     }
34330b57cec5SDimitry Andric 
34340b57cec5SDimitry Andric     Register CarrySum;
34350b57cec5SDimitry Andric     // Add all factors and accumulate all carries into CarrySum.
34360b57cec5SDimitry Andric     if (DstIdx != DstParts - 1) {
34370b57cec5SDimitry Andric       MachineInstrBuilder Uaddo =
34380b57cec5SDimitry Andric           B.buildUAddo(NarrowTy, LLT::scalar(1), Factors[0], Factors[1]);
34390b57cec5SDimitry Andric       FactorSum = Uaddo.getReg(0);
34400b57cec5SDimitry Andric       CarrySum = B.buildZExt(NarrowTy, Uaddo.getReg(1)).getReg(0);
34410b57cec5SDimitry Andric       for (unsigned i = 2; i < Factors.size(); ++i) {
34420b57cec5SDimitry Andric         MachineInstrBuilder Uaddo =
34430b57cec5SDimitry Andric             B.buildUAddo(NarrowTy, LLT::scalar(1), FactorSum, Factors[i]);
34440b57cec5SDimitry Andric         FactorSum = Uaddo.getReg(0);
34450b57cec5SDimitry Andric         MachineInstrBuilder Carry = B.buildZExt(NarrowTy, Uaddo.getReg(1));
34460b57cec5SDimitry Andric         CarrySum = B.buildAdd(NarrowTy, CarrySum, Carry).getReg(0);
34470b57cec5SDimitry Andric       }
34480b57cec5SDimitry Andric     } else {
34490b57cec5SDimitry Andric       // Since value for the next index is not calculated, neither is CarrySum.
34500b57cec5SDimitry Andric       FactorSum = B.buildAdd(NarrowTy, Factors[0], Factors[1]).getReg(0);
34510b57cec5SDimitry Andric       for (unsigned i = 2; i < Factors.size(); ++i)
34520b57cec5SDimitry Andric         FactorSum = B.buildAdd(NarrowTy, FactorSum, Factors[i]).getReg(0);
34530b57cec5SDimitry Andric     }
34540b57cec5SDimitry Andric 
34550b57cec5SDimitry Andric     CarrySumPrevDstIdx = CarrySum;
34560b57cec5SDimitry Andric     DstRegs[DstIdx] = FactorSum;
34570b57cec5SDimitry Andric     Factors.clear();
34580b57cec5SDimitry Andric   }
34590b57cec5SDimitry Andric }
34600b57cec5SDimitry Andric 
34610b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
34620b57cec5SDimitry Andric LegalizerHelper::narrowScalarMul(MachineInstr &MI, LLT NarrowTy) {
34630b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
34640b57cec5SDimitry Andric   Register Src1 = MI.getOperand(1).getReg();
34650b57cec5SDimitry Andric   Register Src2 = MI.getOperand(2).getReg();
34660b57cec5SDimitry Andric 
34670b57cec5SDimitry Andric   LLT Ty = MRI.getType(DstReg);
34680b57cec5SDimitry Andric   if (Ty.isVector())
34690b57cec5SDimitry Andric     return UnableToLegalize;
34700b57cec5SDimitry Andric 
34710b57cec5SDimitry Andric   unsigned SrcSize = MRI.getType(Src1).getSizeInBits();
34720b57cec5SDimitry Andric   unsigned DstSize = Ty.getSizeInBits();
34730b57cec5SDimitry Andric   unsigned NarrowSize = NarrowTy.getSizeInBits();
34740b57cec5SDimitry Andric   if (DstSize % NarrowSize != 0 || SrcSize % NarrowSize != 0)
34750b57cec5SDimitry Andric     return UnableToLegalize;
34760b57cec5SDimitry Andric 
34770b57cec5SDimitry Andric   unsigned NumDstParts = DstSize / NarrowSize;
34780b57cec5SDimitry Andric   unsigned NumSrcParts = SrcSize / NarrowSize;
34790b57cec5SDimitry Andric   bool IsMulHigh = MI.getOpcode() == TargetOpcode::G_UMULH;
34800b57cec5SDimitry Andric   unsigned DstTmpParts = NumDstParts * (IsMulHigh ? 2 : 1);
34810b57cec5SDimitry Andric 
34820b57cec5SDimitry Andric   SmallVector<Register, 2> Src1Parts, Src2Parts, DstTmpRegs;
34830b57cec5SDimitry Andric   extractParts(Src1, NarrowTy, NumSrcParts, Src1Parts);
34840b57cec5SDimitry Andric   extractParts(Src2, NarrowTy, NumSrcParts, Src2Parts);
34850b57cec5SDimitry Andric   DstTmpRegs.resize(DstTmpParts);
34860b57cec5SDimitry Andric   multiplyRegisters(DstTmpRegs, Src1Parts, Src2Parts, NarrowTy);
34870b57cec5SDimitry Andric 
34880b57cec5SDimitry Andric   // Take only high half of registers if this is high mul.
34890b57cec5SDimitry Andric   ArrayRef<Register> DstRegs(
34900b57cec5SDimitry Andric       IsMulHigh ? &DstTmpRegs[DstTmpParts / 2] : &DstTmpRegs[0], NumDstParts);
34910b57cec5SDimitry Andric   MIRBuilder.buildMerge(DstReg, DstRegs);
34920b57cec5SDimitry Andric   MI.eraseFromParent();
34930b57cec5SDimitry Andric   return Legalized;
34940b57cec5SDimitry Andric }
34950b57cec5SDimitry Andric 
34960b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
34970b57cec5SDimitry Andric LegalizerHelper::narrowScalarExtract(MachineInstr &MI, unsigned TypeIdx,
34980b57cec5SDimitry Andric                                      LLT NarrowTy) {
34990b57cec5SDimitry Andric   if (TypeIdx != 1)
35000b57cec5SDimitry Andric     return UnableToLegalize;
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
35030b57cec5SDimitry Andric 
35040b57cec5SDimitry Andric   int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
35050b57cec5SDimitry Andric   // FIXME: add support for when SizeOp1 isn't an exact multiple of
35060b57cec5SDimitry Andric   // NarrowSize.
35070b57cec5SDimitry Andric   if (SizeOp1 % NarrowSize != 0)
35080b57cec5SDimitry Andric     return UnableToLegalize;
35090b57cec5SDimitry Andric   int NumParts = SizeOp1 / NarrowSize;
35100b57cec5SDimitry Andric 
35110b57cec5SDimitry Andric   SmallVector<Register, 2> SrcRegs, DstRegs;
35120b57cec5SDimitry Andric   SmallVector<uint64_t, 2> Indexes;
35130b57cec5SDimitry Andric   extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
35140b57cec5SDimitry Andric 
35150b57cec5SDimitry Andric   Register OpReg = MI.getOperand(0).getReg();
35160b57cec5SDimitry Andric   uint64_t OpStart = MI.getOperand(2).getImm();
35170b57cec5SDimitry Andric   uint64_t OpSize = MRI.getType(OpReg).getSizeInBits();
35180b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
35190b57cec5SDimitry Andric     unsigned SrcStart = i * NarrowSize;
35200b57cec5SDimitry Andric 
35210b57cec5SDimitry Andric     if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) {
35220b57cec5SDimitry Andric       // No part of the extract uses this subregister, ignore it.
35230b57cec5SDimitry Andric       continue;
35240b57cec5SDimitry Andric     } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) {
35250b57cec5SDimitry Andric       // The entire subregister is extracted, forward the value.
35260b57cec5SDimitry Andric       DstRegs.push_back(SrcRegs[i]);
35270b57cec5SDimitry Andric       continue;
35280b57cec5SDimitry Andric     }
35290b57cec5SDimitry Andric 
35300b57cec5SDimitry Andric     // OpSegStart is where this destination segment would start in OpReg if it
35310b57cec5SDimitry Andric     // extended infinitely in both directions.
35320b57cec5SDimitry Andric     int64_t ExtractOffset;
35330b57cec5SDimitry Andric     uint64_t SegSize;
35340b57cec5SDimitry Andric     if (OpStart < SrcStart) {
35350b57cec5SDimitry Andric       ExtractOffset = 0;
35360b57cec5SDimitry Andric       SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart);
35370b57cec5SDimitry Andric     } else {
35380b57cec5SDimitry Andric       ExtractOffset = OpStart - SrcStart;
35390b57cec5SDimitry Andric       SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize);
35400b57cec5SDimitry Andric     }
35410b57cec5SDimitry Andric 
35420b57cec5SDimitry Andric     Register SegReg = SrcRegs[i];
35430b57cec5SDimitry Andric     if (ExtractOffset != 0 || SegSize != NarrowSize) {
35440b57cec5SDimitry Andric       // A genuine extract is needed.
35450b57cec5SDimitry Andric       SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize));
35460b57cec5SDimitry Andric       MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset);
35470b57cec5SDimitry Andric     }
35480b57cec5SDimitry Andric 
35490b57cec5SDimitry Andric     DstRegs.push_back(SegReg);
35500b57cec5SDimitry Andric   }
35510b57cec5SDimitry Andric 
35520b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
35530b57cec5SDimitry Andric   if(MRI.getType(DstReg).isVector())
35540b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
35550b57cec5SDimitry Andric   else
35560b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
35570b57cec5SDimitry Andric   MI.eraseFromParent();
35580b57cec5SDimitry Andric   return Legalized;
35590b57cec5SDimitry Andric }
35600b57cec5SDimitry Andric 
35610b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
35620b57cec5SDimitry Andric LegalizerHelper::narrowScalarInsert(MachineInstr &MI, unsigned TypeIdx,
35630b57cec5SDimitry Andric                                     LLT NarrowTy) {
35640b57cec5SDimitry Andric   // FIXME: Don't know how to handle secondary types yet.
35650b57cec5SDimitry Andric   if (TypeIdx != 0)
35660b57cec5SDimitry Andric     return UnableToLegalize;
35670b57cec5SDimitry Andric 
35680b57cec5SDimitry Andric   uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
35690b57cec5SDimitry Andric   uint64_t NarrowSize = NarrowTy.getSizeInBits();
35700b57cec5SDimitry Andric 
35710b57cec5SDimitry Andric   // FIXME: add support for when SizeOp0 isn't an exact multiple of
35720b57cec5SDimitry Andric   // NarrowSize.
35730b57cec5SDimitry Andric   if (SizeOp0 % NarrowSize != 0)
35740b57cec5SDimitry Andric     return UnableToLegalize;
35750b57cec5SDimitry Andric 
35760b57cec5SDimitry Andric   int NumParts = SizeOp0 / NarrowSize;
35770b57cec5SDimitry Andric 
35780b57cec5SDimitry Andric   SmallVector<Register, 2> SrcRegs, DstRegs;
35790b57cec5SDimitry Andric   SmallVector<uint64_t, 2> Indexes;
35800b57cec5SDimitry Andric   extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs);
35810b57cec5SDimitry Andric 
35820b57cec5SDimitry Andric   Register OpReg = MI.getOperand(2).getReg();
35830b57cec5SDimitry Andric   uint64_t OpStart = MI.getOperand(3).getImm();
35840b57cec5SDimitry Andric   uint64_t OpSize = MRI.getType(OpReg).getSizeInBits();
35850b57cec5SDimitry Andric   for (int i = 0; i < NumParts; ++i) {
35860b57cec5SDimitry Andric     unsigned DstStart = i * NarrowSize;
35870b57cec5SDimitry Andric 
35880b57cec5SDimitry Andric     if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) {
35890b57cec5SDimitry Andric       // No part of the insert affects this subregister, forward the original.
35900b57cec5SDimitry Andric       DstRegs.push_back(SrcRegs[i]);
35910b57cec5SDimitry Andric       continue;
35920b57cec5SDimitry Andric     } else if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) {
35930b57cec5SDimitry Andric       // The entire subregister is defined by this insert, forward the new
35940b57cec5SDimitry Andric       // value.
35950b57cec5SDimitry Andric       DstRegs.push_back(OpReg);
35960b57cec5SDimitry Andric       continue;
35970b57cec5SDimitry Andric     }
35980b57cec5SDimitry Andric 
35990b57cec5SDimitry Andric     // OpSegStart is where this destination segment would start in OpReg if it
36000b57cec5SDimitry Andric     // extended infinitely in both directions.
36010b57cec5SDimitry Andric     int64_t ExtractOffset, InsertOffset;
36020b57cec5SDimitry Andric     uint64_t SegSize;
36030b57cec5SDimitry Andric     if (OpStart < DstStart) {
36040b57cec5SDimitry Andric       InsertOffset = 0;
36050b57cec5SDimitry Andric       ExtractOffset = DstStart - OpStart;
36060b57cec5SDimitry Andric       SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart);
36070b57cec5SDimitry Andric     } else {
36080b57cec5SDimitry Andric       InsertOffset = OpStart - DstStart;
36090b57cec5SDimitry Andric       ExtractOffset = 0;
36100b57cec5SDimitry Andric       SegSize =
36110b57cec5SDimitry Andric         std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart);
36120b57cec5SDimitry Andric     }
36130b57cec5SDimitry Andric 
36140b57cec5SDimitry Andric     Register SegReg = OpReg;
36150b57cec5SDimitry Andric     if (ExtractOffset != 0 || SegSize != OpSize) {
36160b57cec5SDimitry Andric       // A genuine extract is needed.
36170b57cec5SDimitry Andric       SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize));
36180b57cec5SDimitry Andric       MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset);
36190b57cec5SDimitry Andric     }
36200b57cec5SDimitry Andric 
36210b57cec5SDimitry Andric     Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
36220b57cec5SDimitry Andric     MIRBuilder.buildInsert(DstReg, SrcRegs[i], SegReg, InsertOffset);
36230b57cec5SDimitry Andric     DstRegs.push_back(DstReg);
36240b57cec5SDimitry Andric   }
36250b57cec5SDimitry Andric 
36260b57cec5SDimitry Andric   assert(DstRegs.size() == (unsigned)NumParts && "not all parts covered");
36270b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
36280b57cec5SDimitry Andric   if(MRI.getType(DstReg).isVector())
36290b57cec5SDimitry Andric     MIRBuilder.buildBuildVector(DstReg, DstRegs);
36300b57cec5SDimitry Andric   else
36310b57cec5SDimitry Andric     MIRBuilder.buildMerge(DstReg, DstRegs);
36320b57cec5SDimitry Andric   MI.eraseFromParent();
36330b57cec5SDimitry Andric   return Legalized;
36340b57cec5SDimitry Andric }
36350b57cec5SDimitry Andric 
36360b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
36370b57cec5SDimitry Andric LegalizerHelper::narrowScalarBasic(MachineInstr &MI, unsigned TypeIdx,
36380b57cec5SDimitry Andric                                    LLT NarrowTy) {
36390b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
36400b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
36410b57cec5SDimitry Andric 
36420b57cec5SDimitry Andric   assert(MI.getNumOperands() == 3 && TypeIdx == 0);
36430b57cec5SDimitry Andric 
36440b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, DstLeftoverRegs;
36450b57cec5SDimitry Andric   SmallVector<Register, 4> Src0Regs, Src0LeftoverRegs;
36460b57cec5SDimitry Andric   SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs;
36470b57cec5SDimitry Andric   LLT LeftoverTy;
36480b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(1).getReg(), DstTy, NarrowTy, LeftoverTy,
36490b57cec5SDimitry Andric                     Src0Regs, Src0LeftoverRegs))
36500b57cec5SDimitry Andric     return UnableToLegalize;
36510b57cec5SDimitry Andric 
36520b57cec5SDimitry Andric   LLT Unused;
36530b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, Unused,
36540b57cec5SDimitry Andric                     Src1Regs, Src1LeftoverRegs))
36550b57cec5SDimitry Andric     llvm_unreachable("inconsistent extractParts result");
36560b57cec5SDimitry Andric 
36570b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) {
36580b57cec5SDimitry Andric     auto Inst = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy},
36590b57cec5SDimitry Andric                                         {Src0Regs[I], Src1Regs[I]});
36600b57cec5SDimitry Andric     DstRegs.push_back(Inst->getOperand(0).getReg());
36610b57cec5SDimitry Andric   }
36620b57cec5SDimitry Andric 
36630b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) {
36640b57cec5SDimitry Andric     auto Inst = MIRBuilder.buildInstr(
36650b57cec5SDimitry Andric       MI.getOpcode(),
36660b57cec5SDimitry Andric       {LeftoverTy}, {Src0LeftoverRegs[I], Src1LeftoverRegs[I]});
36670b57cec5SDimitry Andric     DstLeftoverRegs.push_back(Inst->getOperand(0).getReg());
36680b57cec5SDimitry Andric   }
36690b57cec5SDimitry Andric 
36700b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy, DstRegs,
36710b57cec5SDimitry Andric               LeftoverTy, DstLeftoverRegs);
36720b57cec5SDimitry Andric 
36730b57cec5SDimitry Andric   MI.eraseFromParent();
36740b57cec5SDimitry Andric   return Legalized;
36750b57cec5SDimitry Andric }
36760b57cec5SDimitry Andric 
36770b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
36780b57cec5SDimitry Andric LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx,
36790b57cec5SDimitry Andric                                     LLT NarrowTy) {
36800b57cec5SDimitry Andric   if (TypeIdx != 0)
36810b57cec5SDimitry Andric     return UnableToLegalize;
36820b57cec5SDimitry Andric 
36830b57cec5SDimitry Andric   Register CondReg = MI.getOperand(1).getReg();
36840b57cec5SDimitry Andric   LLT CondTy = MRI.getType(CondReg);
36850b57cec5SDimitry Andric   if (CondTy.isVector()) // TODO: Handle vselect
36860b57cec5SDimitry Andric     return UnableToLegalize;
36870b57cec5SDimitry Andric 
36880b57cec5SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
36890b57cec5SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
36900b57cec5SDimitry Andric 
36910b57cec5SDimitry Andric   SmallVector<Register, 4> DstRegs, DstLeftoverRegs;
36920b57cec5SDimitry Andric   SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs;
36930b57cec5SDimitry Andric   SmallVector<Register, 4> Src2Regs, Src2LeftoverRegs;
36940b57cec5SDimitry Andric   LLT LeftoverTy;
36950b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy,
36960b57cec5SDimitry Andric                     Src1Regs, Src1LeftoverRegs))
36970b57cec5SDimitry Andric     return UnableToLegalize;
36980b57cec5SDimitry Andric 
36990b57cec5SDimitry Andric   LLT Unused;
37000b57cec5SDimitry Andric   if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused,
37010b57cec5SDimitry Andric                     Src2Regs, Src2LeftoverRegs))
37020b57cec5SDimitry Andric     llvm_unreachable("inconsistent extractParts result");
37030b57cec5SDimitry Andric 
37040b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) {
37050b57cec5SDimitry Andric     auto Select = MIRBuilder.buildSelect(NarrowTy,
37060b57cec5SDimitry Andric                                          CondReg, Src1Regs[I], Src2Regs[I]);
37070b57cec5SDimitry Andric     DstRegs.push_back(Select->getOperand(0).getReg());
37080b57cec5SDimitry Andric   }
37090b57cec5SDimitry Andric 
37100b57cec5SDimitry Andric   for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) {
37110b57cec5SDimitry Andric     auto Select = MIRBuilder.buildSelect(
37120b57cec5SDimitry Andric       LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]);
37130b57cec5SDimitry Andric     DstLeftoverRegs.push_back(Select->getOperand(0).getReg());
37140b57cec5SDimitry Andric   }
37150b57cec5SDimitry Andric 
37160b57cec5SDimitry Andric   insertParts(DstReg, DstTy, NarrowTy, DstRegs,
37170b57cec5SDimitry Andric               LeftoverTy, DstLeftoverRegs);
37180b57cec5SDimitry Andric 
37190b57cec5SDimitry Andric   MI.eraseFromParent();
37200b57cec5SDimitry Andric   return Legalized;
37210b57cec5SDimitry Andric }
37220b57cec5SDimitry Andric 
37230b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
37240b57cec5SDimitry Andric LegalizerHelper::lowerBitCount(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
37250b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
37260b57cec5SDimitry Andric   auto &TII = *MI.getMF()->getSubtarget().getInstrInfo();
37270b57cec5SDimitry Andric   auto isSupported = [this](const LegalityQuery &Q) {
37280b57cec5SDimitry Andric     auto QAction = LI.getAction(Q).Action;
37290b57cec5SDimitry Andric     return QAction == Legal || QAction == Libcall || QAction == Custom;
37300b57cec5SDimitry Andric   };
37310b57cec5SDimitry Andric   switch (Opc) {
37320b57cec5SDimitry Andric   default:
37330b57cec5SDimitry Andric     return UnableToLegalize;
37340b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ_ZERO_UNDEF: {
37350b57cec5SDimitry Andric     // This trivially expands to CTLZ.
37360b57cec5SDimitry Andric     Observer.changingInstr(MI);
37370b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTLZ));
37380b57cec5SDimitry Andric     Observer.changedInstr(MI);
37390b57cec5SDimitry Andric     return Legalized;
37400b57cec5SDimitry Andric   }
37410b57cec5SDimitry Andric   case TargetOpcode::G_CTLZ: {
37420b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
37430b57cec5SDimitry Andric     unsigned Len = Ty.getSizeInBits();
37440b57cec5SDimitry Andric     if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {Ty, Ty}})) {
37450b57cec5SDimitry Andric       // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero.
37460b57cec5SDimitry Andric       auto MIBCtlzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF,
37470b57cec5SDimitry Andric                                              {Ty}, {SrcReg});
37480b57cec5SDimitry Andric       auto MIBZero = MIRBuilder.buildConstant(Ty, 0);
37490b57cec5SDimitry Andric       auto MIBLen = MIRBuilder.buildConstant(Ty, Len);
37500b57cec5SDimitry Andric       auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1),
37510b57cec5SDimitry Andric                                           SrcReg, MIBZero);
37520b57cec5SDimitry Andric       MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen,
37530b57cec5SDimitry Andric                              MIBCtlzZU);
37540b57cec5SDimitry Andric       MI.eraseFromParent();
37550b57cec5SDimitry Andric       return Legalized;
37560b57cec5SDimitry Andric     }
37570b57cec5SDimitry Andric     // for now, we do this:
37580b57cec5SDimitry Andric     // NewLen = NextPowerOf2(Len);
37590b57cec5SDimitry Andric     // x = x | (x >> 1);
37600b57cec5SDimitry Andric     // x = x | (x >> 2);
37610b57cec5SDimitry Andric     // ...
37620b57cec5SDimitry Andric     // x = x | (x >>16);
37630b57cec5SDimitry Andric     // x = x | (x >>32); // for 64-bit input
37640b57cec5SDimitry Andric     // Upto NewLen/2
37650b57cec5SDimitry Andric     // return Len - popcount(x);
37660b57cec5SDimitry Andric     //
37670b57cec5SDimitry Andric     // Ref: "Hacker's Delight" by Henry Warren
37680b57cec5SDimitry Andric     Register Op = SrcReg;
37690b57cec5SDimitry Andric     unsigned NewLen = PowerOf2Ceil(Len);
37700b57cec5SDimitry Andric     for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) {
37710b57cec5SDimitry Andric       auto MIBShiftAmt = MIRBuilder.buildConstant(Ty, 1ULL << i);
37720b57cec5SDimitry Andric       auto MIBOp = MIRBuilder.buildInstr(
37730b57cec5SDimitry Andric           TargetOpcode::G_OR, {Ty},
37740b57cec5SDimitry Andric           {Op, MIRBuilder.buildInstr(TargetOpcode::G_LSHR, {Ty},
37750b57cec5SDimitry Andric                                      {Op, MIBShiftAmt})});
37760b57cec5SDimitry Andric       Op = MIBOp->getOperand(0).getReg();
37770b57cec5SDimitry Andric     }
37780b57cec5SDimitry Andric     auto MIBPop = MIRBuilder.buildInstr(TargetOpcode::G_CTPOP, {Ty}, {Op});
37790b57cec5SDimitry Andric     MIRBuilder.buildInstr(TargetOpcode::G_SUB, {MI.getOperand(0).getReg()},
37800b57cec5SDimitry Andric                           {MIRBuilder.buildConstant(Ty, Len), MIBPop});
37810b57cec5SDimitry Andric     MI.eraseFromParent();
37820b57cec5SDimitry Andric     return Legalized;
37830b57cec5SDimitry Andric   }
37840b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ_ZERO_UNDEF: {
37850b57cec5SDimitry Andric     // This trivially expands to CTTZ.
37860b57cec5SDimitry Andric     Observer.changingInstr(MI);
37870b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTTZ));
37880b57cec5SDimitry Andric     Observer.changedInstr(MI);
37890b57cec5SDimitry Andric     return Legalized;
37900b57cec5SDimitry Andric   }
37910b57cec5SDimitry Andric   case TargetOpcode::G_CTTZ: {
37920b57cec5SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
37930b57cec5SDimitry Andric     unsigned Len = Ty.getSizeInBits();
37940b57cec5SDimitry Andric     if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {Ty, Ty}})) {
37950b57cec5SDimitry Andric       // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with
37960b57cec5SDimitry Andric       // zero.
37970b57cec5SDimitry Andric       auto MIBCttzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF,
37980b57cec5SDimitry Andric                                              {Ty}, {SrcReg});
37990b57cec5SDimitry Andric       auto MIBZero = MIRBuilder.buildConstant(Ty, 0);
38000b57cec5SDimitry Andric       auto MIBLen = MIRBuilder.buildConstant(Ty, Len);
38010b57cec5SDimitry Andric       auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1),
38020b57cec5SDimitry Andric                                           SrcReg, MIBZero);
38030b57cec5SDimitry Andric       MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen,
38040b57cec5SDimitry Andric                              MIBCttzZU);
38050b57cec5SDimitry Andric       MI.eraseFromParent();
38060b57cec5SDimitry Andric       return Legalized;
38070b57cec5SDimitry Andric     }
38080b57cec5SDimitry Andric     // for now, we use: { return popcount(~x & (x - 1)); }
38090b57cec5SDimitry Andric     // unless the target has ctlz but not ctpop, in which case we use:
38100b57cec5SDimitry Andric     // { return 32 - nlz(~x & (x-1)); }
38110b57cec5SDimitry Andric     // Ref: "Hacker's Delight" by Henry Warren
38120b57cec5SDimitry Andric     auto MIBCstNeg1 = MIRBuilder.buildConstant(Ty, -1);
38130b57cec5SDimitry Andric     auto MIBNot =
38140b57cec5SDimitry Andric         MIRBuilder.buildInstr(TargetOpcode::G_XOR, {Ty}, {SrcReg, MIBCstNeg1});
38150b57cec5SDimitry Andric     auto MIBTmp = MIRBuilder.buildInstr(
38160b57cec5SDimitry Andric         TargetOpcode::G_AND, {Ty},
38170b57cec5SDimitry Andric         {MIBNot, MIRBuilder.buildInstr(TargetOpcode::G_ADD, {Ty},
38180b57cec5SDimitry Andric                                        {SrcReg, MIBCstNeg1})});
38190b57cec5SDimitry Andric     if (!isSupported({TargetOpcode::G_CTPOP, {Ty, Ty}}) &&
38200b57cec5SDimitry Andric         isSupported({TargetOpcode::G_CTLZ, {Ty, Ty}})) {
38210b57cec5SDimitry Andric       auto MIBCstLen = MIRBuilder.buildConstant(Ty, Len);
38220b57cec5SDimitry Andric       MIRBuilder.buildInstr(
38230b57cec5SDimitry Andric           TargetOpcode::G_SUB, {MI.getOperand(0).getReg()},
38240b57cec5SDimitry Andric           {MIBCstLen,
38250b57cec5SDimitry Andric            MIRBuilder.buildInstr(TargetOpcode::G_CTLZ, {Ty}, {MIBTmp})});
38260b57cec5SDimitry Andric       MI.eraseFromParent();
38270b57cec5SDimitry Andric       return Legalized;
38280b57cec5SDimitry Andric     }
38290b57cec5SDimitry Andric     MI.setDesc(TII.get(TargetOpcode::G_CTPOP));
38300b57cec5SDimitry Andric     MI.getOperand(1).setReg(MIBTmp->getOperand(0).getReg());
38310b57cec5SDimitry Andric     return Legalized;
38320b57cec5SDimitry Andric   }
38330b57cec5SDimitry Andric   }
38340b57cec5SDimitry Andric }
38350b57cec5SDimitry Andric 
38360b57cec5SDimitry Andric // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float
38370b57cec5SDimitry Andric // representation.
38380b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
38390b57cec5SDimitry Andric LegalizerHelper::lowerU64ToF32BitOps(MachineInstr &MI) {
38400b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
38410b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
38420b57cec5SDimitry Andric   const LLT S64 = LLT::scalar(64);
38430b57cec5SDimitry Andric   const LLT S32 = LLT::scalar(32);
38440b57cec5SDimitry Andric   const LLT S1 = LLT::scalar(1);
38450b57cec5SDimitry Andric 
38460b57cec5SDimitry Andric   assert(MRI.getType(Src) == S64 && MRI.getType(Dst) == S32);
38470b57cec5SDimitry Andric 
38480b57cec5SDimitry Andric   // unsigned cul2f(ulong u) {
38490b57cec5SDimitry Andric   //   uint lz = clz(u);
38500b57cec5SDimitry Andric   //   uint e = (u != 0) ? 127U + 63U - lz : 0;
38510b57cec5SDimitry Andric   //   u = (u << lz) & 0x7fffffffffffffffUL;
38520b57cec5SDimitry Andric   //   ulong t = u & 0xffffffffffUL;
38530b57cec5SDimitry Andric   //   uint v = (e << 23) | (uint)(u >> 40);
38540b57cec5SDimitry Andric   //   uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
38550b57cec5SDimitry Andric   //   return as_float(v + r);
38560b57cec5SDimitry Andric   // }
38570b57cec5SDimitry Andric 
38580b57cec5SDimitry Andric   auto Zero32 = MIRBuilder.buildConstant(S32, 0);
38590b57cec5SDimitry Andric   auto Zero64 = MIRBuilder.buildConstant(S64, 0);
38600b57cec5SDimitry Andric 
38610b57cec5SDimitry Andric   auto LZ = MIRBuilder.buildCTLZ_ZERO_UNDEF(S32, Src);
38620b57cec5SDimitry Andric 
38630b57cec5SDimitry Andric   auto K = MIRBuilder.buildConstant(S32, 127U + 63U);
38640b57cec5SDimitry Andric   auto Sub = MIRBuilder.buildSub(S32, K, LZ);
38650b57cec5SDimitry Andric 
38660b57cec5SDimitry Andric   auto NotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, Src, Zero64);
38670b57cec5SDimitry Andric   auto E = MIRBuilder.buildSelect(S32, NotZero, Sub, Zero32);
38680b57cec5SDimitry Andric 
38690b57cec5SDimitry Andric   auto Mask0 = MIRBuilder.buildConstant(S64, (-1ULL) >> 1);
38700b57cec5SDimitry Andric   auto ShlLZ = MIRBuilder.buildShl(S64, Src, LZ);
38710b57cec5SDimitry Andric 
38720b57cec5SDimitry Andric   auto U = MIRBuilder.buildAnd(S64, ShlLZ, Mask0);
38730b57cec5SDimitry Andric 
38740b57cec5SDimitry Andric   auto Mask1 = MIRBuilder.buildConstant(S64, 0xffffffffffULL);
38750b57cec5SDimitry Andric   auto T = MIRBuilder.buildAnd(S64, U, Mask1);
38760b57cec5SDimitry Andric 
38770b57cec5SDimitry Andric   auto UShl = MIRBuilder.buildLShr(S64, U, MIRBuilder.buildConstant(S64, 40));
38780b57cec5SDimitry Andric   auto ShlE = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 23));
38790b57cec5SDimitry Andric   auto V = MIRBuilder.buildOr(S32, ShlE, MIRBuilder.buildTrunc(S32, UShl));
38800b57cec5SDimitry Andric 
38810b57cec5SDimitry Andric   auto C = MIRBuilder.buildConstant(S64, 0x8000000000ULL);
38820b57cec5SDimitry Andric   auto RCmp = MIRBuilder.buildICmp(CmpInst::ICMP_UGT, S1, T, C);
38830b57cec5SDimitry Andric   auto TCmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, T, C);
38840b57cec5SDimitry Andric   auto One = MIRBuilder.buildConstant(S32, 1);
38850b57cec5SDimitry Andric 
38860b57cec5SDimitry Andric   auto VTrunc1 = MIRBuilder.buildAnd(S32, V, One);
38870b57cec5SDimitry Andric   auto Select0 = MIRBuilder.buildSelect(S32, TCmp, VTrunc1, Zero32);
38880b57cec5SDimitry Andric   auto R = MIRBuilder.buildSelect(S32, RCmp, One, Select0);
38890b57cec5SDimitry Andric   MIRBuilder.buildAdd(Dst, V, R);
38900b57cec5SDimitry Andric 
38910b57cec5SDimitry Andric   return Legalized;
38920b57cec5SDimitry Andric }
38930b57cec5SDimitry Andric 
38940b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
38950b57cec5SDimitry Andric LegalizerHelper::lowerUITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
38960b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
38970b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
38980b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst);
38990b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src);
39000b57cec5SDimitry Andric 
3901*480093f4SDimitry Andric   if (SrcTy == LLT::scalar(1)) {
3902*480093f4SDimitry Andric     auto True = MIRBuilder.buildFConstant(DstTy, 1.0);
3903*480093f4SDimitry Andric     auto False = MIRBuilder.buildFConstant(DstTy, 0.0);
3904*480093f4SDimitry Andric     MIRBuilder.buildSelect(Dst, Src, True, False);
3905*480093f4SDimitry Andric     MI.eraseFromParent();
3906*480093f4SDimitry Andric     return Legalized;
3907*480093f4SDimitry Andric   }
3908*480093f4SDimitry Andric 
39090b57cec5SDimitry Andric   if (SrcTy != LLT::scalar(64))
39100b57cec5SDimitry Andric     return UnableToLegalize;
39110b57cec5SDimitry Andric 
39120b57cec5SDimitry Andric   if (DstTy == LLT::scalar(32)) {
39130b57cec5SDimitry Andric     // TODO: SelectionDAG has several alternative expansions to port which may
39140b57cec5SDimitry Andric     // be more reasonble depending on the available instructions. If a target
39150b57cec5SDimitry Andric     // has sitofp, does not have CTLZ, or can efficiently use f64 as an
39160b57cec5SDimitry Andric     // intermediate type, this is probably worse.
39170b57cec5SDimitry Andric     return lowerU64ToF32BitOps(MI);
39180b57cec5SDimitry Andric   }
39190b57cec5SDimitry Andric 
39200b57cec5SDimitry Andric   return UnableToLegalize;
39210b57cec5SDimitry Andric }
39220b57cec5SDimitry Andric 
39230b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
39240b57cec5SDimitry Andric LegalizerHelper::lowerSITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
39250b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
39260b57cec5SDimitry Andric   Register Src = MI.getOperand(1).getReg();
39270b57cec5SDimitry Andric   LLT DstTy = MRI.getType(Dst);
39280b57cec5SDimitry Andric   LLT SrcTy = MRI.getType(Src);
39290b57cec5SDimitry Andric 
39300b57cec5SDimitry Andric   const LLT S64 = LLT::scalar(64);
39310b57cec5SDimitry Andric   const LLT S32 = LLT::scalar(32);
39320b57cec5SDimitry Andric   const LLT S1 = LLT::scalar(1);
39330b57cec5SDimitry Andric 
3934*480093f4SDimitry Andric   if (SrcTy == S1) {
3935*480093f4SDimitry Andric     auto True = MIRBuilder.buildFConstant(DstTy, -1.0);
3936*480093f4SDimitry Andric     auto False = MIRBuilder.buildFConstant(DstTy, 0.0);
3937*480093f4SDimitry Andric     MIRBuilder.buildSelect(Dst, Src, True, False);
3938*480093f4SDimitry Andric     MI.eraseFromParent();
3939*480093f4SDimitry Andric     return Legalized;
3940*480093f4SDimitry Andric   }
3941*480093f4SDimitry Andric 
39420b57cec5SDimitry Andric   if (SrcTy != S64)
39430b57cec5SDimitry Andric     return UnableToLegalize;
39440b57cec5SDimitry Andric 
39450b57cec5SDimitry Andric   if (DstTy == S32) {
39460b57cec5SDimitry Andric     // signed cl2f(long l) {
39470b57cec5SDimitry Andric     //   long s = l >> 63;
39480b57cec5SDimitry Andric     //   float r = cul2f((l + s) ^ s);
39490b57cec5SDimitry Andric     //   return s ? -r : r;
39500b57cec5SDimitry Andric     // }
39510b57cec5SDimitry Andric     Register L = Src;
39520b57cec5SDimitry Andric     auto SignBit = MIRBuilder.buildConstant(S64, 63);
39530b57cec5SDimitry Andric     auto S = MIRBuilder.buildAShr(S64, L, SignBit);
39540b57cec5SDimitry Andric 
39550b57cec5SDimitry Andric     auto LPlusS = MIRBuilder.buildAdd(S64, L, S);
39560b57cec5SDimitry Andric     auto Xor = MIRBuilder.buildXor(S64, LPlusS, S);
39570b57cec5SDimitry Andric     auto R = MIRBuilder.buildUITOFP(S32, Xor);
39580b57cec5SDimitry Andric 
39590b57cec5SDimitry Andric     auto RNeg = MIRBuilder.buildFNeg(S32, R);
39600b57cec5SDimitry Andric     auto SignNotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, S,
39610b57cec5SDimitry Andric                                             MIRBuilder.buildConstant(S64, 0));
39620b57cec5SDimitry Andric     MIRBuilder.buildSelect(Dst, SignNotZero, RNeg, R);
39630b57cec5SDimitry Andric     return Legalized;
39640b57cec5SDimitry Andric   }
39650b57cec5SDimitry Andric 
39660b57cec5SDimitry Andric   return UnableToLegalize;
39670b57cec5SDimitry Andric }
39680b57cec5SDimitry Andric 
39698bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
39708bcb0991SDimitry Andric LegalizerHelper::lowerFPTOUI(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
39718bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
39728bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
39738bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst);
39748bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(Src);
39758bcb0991SDimitry Andric   const LLT S64 = LLT::scalar(64);
39768bcb0991SDimitry Andric   const LLT S32 = LLT::scalar(32);
39778bcb0991SDimitry Andric 
39788bcb0991SDimitry Andric   if (SrcTy != S64 && SrcTy != S32)
39798bcb0991SDimitry Andric     return UnableToLegalize;
39808bcb0991SDimitry Andric   if (DstTy != S32 && DstTy != S64)
39818bcb0991SDimitry Andric     return UnableToLegalize;
39828bcb0991SDimitry Andric 
39838bcb0991SDimitry Andric   // FPTOSI gives same result as FPTOUI for positive signed integers.
39848bcb0991SDimitry Andric   // FPTOUI needs to deal with fp values that convert to unsigned integers
39858bcb0991SDimitry Andric   // greater or equal to 2^31 for float or 2^63 for double. For brevity 2^Exp.
39868bcb0991SDimitry Andric 
39878bcb0991SDimitry Andric   APInt TwoPExpInt = APInt::getSignMask(DstTy.getSizeInBits());
39888bcb0991SDimitry Andric   APFloat TwoPExpFP(SrcTy.getSizeInBits() == 32 ? APFloat::IEEEsingle()
39898bcb0991SDimitry Andric                                                 : APFloat::IEEEdouble(),
39908bcb0991SDimitry Andric                     APInt::getNullValue(SrcTy.getSizeInBits()));
39918bcb0991SDimitry Andric   TwoPExpFP.convertFromAPInt(TwoPExpInt, false, APFloat::rmNearestTiesToEven);
39928bcb0991SDimitry Andric 
39938bcb0991SDimitry Andric   MachineInstrBuilder FPTOSI = MIRBuilder.buildFPTOSI(DstTy, Src);
39948bcb0991SDimitry Andric 
39958bcb0991SDimitry Andric   MachineInstrBuilder Threshold = MIRBuilder.buildFConstant(SrcTy, TwoPExpFP);
39968bcb0991SDimitry Andric   // For fp Value greater or equal to Threshold(2^Exp), we use FPTOSI on
39978bcb0991SDimitry Andric   // (Value - 2^Exp) and add 2^Exp by setting highest bit in result to 1.
39988bcb0991SDimitry Andric   MachineInstrBuilder FSub = MIRBuilder.buildFSub(SrcTy, Src, Threshold);
39998bcb0991SDimitry Andric   MachineInstrBuilder ResLowBits = MIRBuilder.buildFPTOSI(DstTy, FSub);
40008bcb0991SDimitry Andric   MachineInstrBuilder ResHighBit = MIRBuilder.buildConstant(DstTy, TwoPExpInt);
40018bcb0991SDimitry Andric   MachineInstrBuilder Res = MIRBuilder.buildXor(DstTy, ResLowBits, ResHighBit);
40028bcb0991SDimitry Andric 
4003*480093f4SDimitry Andric   const LLT S1 = LLT::scalar(1);
4004*480093f4SDimitry Andric 
40058bcb0991SDimitry Andric   MachineInstrBuilder FCMP =
4006*480093f4SDimitry Andric       MIRBuilder.buildFCmp(CmpInst::FCMP_ULT, S1, Src, Threshold);
40078bcb0991SDimitry Andric   MIRBuilder.buildSelect(Dst, FCMP, FPTOSI, Res);
40088bcb0991SDimitry Andric 
40098bcb0991SDimitry Andric   MI.eraseFromParent();
40108bcb0991SDimitry Andric   return Legalized;
40118bcb0991SDimitry Andric }
40128bcb0991SDimitry Andric 
40130b57cec5SDimitry Andric static CmpInst::Predicate minMaxToCompare(unsigned Opc) {
40140b57cec5SDimitry Andric   switch (Opc) {
40150b57cec5SDimitry Andric   case TargetOpcode::G_SMIN:
40160b57cec5SDimitry Andric     return CmpInst::ICMP_SLT;
40170b57cec5SDimitry Andric   case TargetOpcode::G_SMAX:
40180b57cec5SDimitry Andric     return CmpInst::ICMP_SGT;
40190b57cec5SDimitry Andric   case TargetOpcode::G_UMIN:
40200b57cec5SDimitry Andric     return CmpInst::ICMP_ULT;
40210b57cec5SDimitry Andric   case TargetOpcode::G_UMAX:
40220b57cec5SDimitry Andric     return CmpInst::ICMP_UGT;
40230b57cec5SDimitry Andric   default:
40240b57cec5SDimitry Andric     llvm_unreachable("not in integer min/max");
40250b57cec5SDimitry Andric   }
40260b57cec5SDimitry Andric }
40270b57cec5SDimitry Andric 
40280b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
40290b57cec5SDimitry Andric LegalizerHelper::lowerMinMax(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
40300b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
40310b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
40320b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
40330b57cec5SDimitry Andric 
40340b57cec5SDimitry Andric   const CmpInst::Predicate Pred = minMaxToCompare(MI.getOpcode());
40350b57cec5SDimitry Andric   LLT CmpType = MRI.getType(Dst).changeElementSize(1);
40360b57cec5SDimitry Andric 
40370b57cec5SDimitry Andric   auto Cmp = MIRBuilder.buildICmp(Pred, CmpType, Src0, Src1);
40380b57cec5SDimitry Andric   MIRBuilder.buildSelect(Dst, Cmp, Src0, Src1);
40390b57cec5SDimitry Andric 
40400b57cec5SDimitry Andric   MI.eraseFromParent();
40410b57cec5SDimitry Andric   return Legalized;
40420b57cec5SDimitry Andric }
40430b57cec5SDimitry Andric 
40440b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
40450b57cec5SDimitry Andric LegalizerHelper::lowerFCopySign(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
40460b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
40470b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
40480b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
40490b57cec5SDimitry Andric 
40500b57cec5SDimitry Andric   const LLT Src0Ty = MRI.getType(Src0);
40510b57cec5SDimitry Andric   const LLT Src1Ty = MRI.getType(Src1);
40520b57cec5SDimitry Andric 
40530b57cec5SDimitry Andric   const int Src0Size = Src0Ty.getScalarSizeInBits();
40540b57cec5SDimitry Andric   const int Src1Size = Src1Ty.getScalarSizeInBits();
40550b57cec5SDimitry Andric 
40560b57cec5SDimitry Andric   auto SignBitMask = MIRBuilder.buildConstant(
40570b57cec5SDimitry Andric     Src0Ty, APInt::getSignMask(Src0Size));
40580b57cec5SDimitry Andric 
40590b57cec5SDimitry Andric   auto NotSignBitMask = MIRBuilder.buildConstant(
40600b57cec5SDimitry Andric     Src0Ty, APInt::getLowBitsSet(Src0Size, Src0Size - 1));
40610b57cec5SDimitry Andric 
40620b57cec5SDimitry Andric   auto And0 = MIRBuilder.buildAnd(Src0Ty, Src0, NotSignBitMask);
40630b57cec5SDimitry Andric   MachineInstr *Or;
40640b57cec5SDimitry Andric 
40650b57cec5SDimitry Andric   if (Src0Ty == Src1Ty) {
40660b57cec5SDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src1Ty, Src0, SignBitMask);
40670b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
40680b57cec5SDimitry Andric   } else if (Src0Size > Src1Size) {
40690b57cec5SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Src0Ty, Src0Size - Src1Size);
40700b57cec5SDimitry Andric     auto Zext = MIRBuilder.buildZExt(Src0Ty, Src1);
40710b57cec5SDimitry Andric     auto Shift = MIRBuilder.buildShl(Src0Ty, Zext, ShiftAmt);
40720b57cec5SDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src0Ty, Shift, SignBitMask);
40730b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
40740b57cec5SDimitry Andric   } else {
40750b57cec5SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Src1Ty, Src1Size - Src0Size);
40760b57cec5SDimitry Andric     auto Shift = MIRBuilder.buildLShr(Src1Ty, Src1, ShiftAmt);
40770b57cec5SDimitry Andric     auto Trunc = MIRBuilder.buildTrunc(Src0Ty, Shift);
40780b57cec5SDimitry Andric     auto And1 = MIRBuilder.buildAnd(Src0Ty, Trunc, SignBitMask);
40790b57cec5SDimitry Andric     Or = MIRBuilder.buildOr(Dst, And0, And1);
40800b57cec5SDimitry Andric   }
40810b57cec5SDimitry Andric 
40820b57cec5SDimitry Andric   // Be careful about setting nsz/nnan/ninf on every instruction, since the
40830b57cec5SDimitry Andric   // constants are a nan and -0.0, but the final result should preserve
40840b57cec5SDimitry Andric   // everything.
40850b57cec5SDimitry Andric   if (unsigned Flags = MI.getFlags())
40860b57cec5SDimitry Andric     Or->setFlags(Flags);
40870b57cec5SDimitry Andric 
40880b57cec5SDimitry Andric   MI.eraseFromParent();
40890b57cec5SDimitry Andric   return Legalized;
40900b57cec5SDimitry Andric }
40910b57cec5SDimitry Andric 
40920b57cec5SDimitry Andric LegalizerHelper::LegalizeResult
40930b57cec5SDimitry Andric LegalizerHelper::lowerFMinNumMaxNum(MachineInstr &MI) {
40940b57cec5SDimitry Andric   unsigned NewOp = MI.getOpcode() == TargetOpcode::G_FMINNUM ?
40950b57cec5SDimitry Andric     TargetOpcode::G_FMINNUM_IEEE : TargetOpcode::G_FMAXNUM_IEEE;
40960b57cec5SDimitry Andric 
40970b57cec5SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
40980b57cec5SDimitry Andric   Register Src0 = MI.getOperand(1).getReg();
40990b57cec5SDimitry Andric   Register Src1 = MI.getOperand(2).getReg();
41000b57cec5SDimitry Andric   LLT Ty = MRI.getType(Dst);
41010b57cec5SDimitry Andric 
41020b57cec5SDimitry Andric   if (!MI.getFlag(MachineInstr::FmNoNans)) {
41030b57cec5SDimitry Andric     // Insert canonicalizes if it's possible we need to quiet to get correct
41040b57cec5SDimitry Andric     // sNaN behavior.
41050b57cec5SDimitry Andric 
41060b57cec5SDimitry Andric     // Note this must be done here, and not as an optimization combine in the
41070b57cec5SDimitry Andric     // absence of a dedicate quiet-snan instruction as we're using an
41080b57cec5SDimitry Andric     // omni-purpose G_FCANONICALIZE.
41090b57cec5SDimitry Andric     if (!isKnownNeverSNaN(Src0, MRI))
41100b57cec5SDimitry Andric       Src0 = MIRBuilder.buildFCanonicalize(Ty, Src0, MI.getFlags()).getReg(0);
41110b57cec5SDimitry Andric 
41120b57cec5SDimitry Andric     if (!isKnownNeverSNaN(Src1, MRI))
41130b57cec5SDimitry Andric       Src1 = MIRBuilder.buildFCanonicalize(Ty, Src1, MI.getFlags()).getReg(0);
41140b57cec5SDimitry Andric   }
41150b57cec5SDimitry Andric 
41160b57cec5SDimitry Andric   // If there are no nans, it's safe to simply replace this with the non-IEEE
41170b57cec5SDimitry Andric   // version.
41180b57cec5SDimitry Andric   MIRBuilder.buildInstr(NewOp, {Dst}, {Src0, Src1}, MI.getFlags());
41190b57cec5SDimitry Andric   MI.eraseFromParent();
41200b57cec5SDimitry Andric   return Legalized;
41210b57cec5SDimitry Andric }
41228bcb0991SDimitry Andric 
41238bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFMad(MachineInstr &MI) {
41248bcb0991SDimitry Andric   // Expand G_FMAD a, b, c -> G_FADD (G_FMUL a, b), c
41258bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
41268bcb0991SDimitry Andric   LLT Ty = MRI.getType(DstReg);
41278bcb0991SDimitry Andric   unsigned Flags = MI.getFlags();
41288bcb0991SDimitry Andric 
41298bcb0991SDimitry Andric   auto Mul = MIRBuilder.buildFMul(Ty, MI.getOperand(1), MI.getOperand(2),
41308bcb0991SDimitry Andric                                   Flags);
41318bcb0991SDimitry Andric   MIRBuilder.buildFAdd(DstReg, Mul, MI.getOperand(3), Flags);
41328bcb0991SDimitry Andric   MI.eraseFromParent();
41338bcb0991SDimitry Andric   return Legalized;
41348bcb0991SDimitry Andric }
41358bcb0991SDimitry Andric 
41368bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
4137*480093f4SDimitry Andric LegalizerHelper::lowerIntrinsicRound(MachineInstr &MI) {
4138*480093f4SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
4139*480093f4SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
4140*480093f4SDimitry Andric   unsigned Flags = MI.getFlags();
4141*480093f4SDimitry Andric   LLT Ty = MRI.getType(DstReg);
4142*480093f4SDimitry Andric   const LLT CondTy = Ty.changeElementSize(1);
4143*480093f4SDimitry Andric 
4144*480093f4SDimitry Andric   // result = trunc(src);
4145*480093f4SDimitry Andric   // if (src < 0.0 && src != result)
4146*480093f4SDimitry Andric   //   result += -1.0.
4147*480093f4SDimitry Andric 
4148*480093f4SDimitry Andric   auto Zero = MIRBuilder.buildFConstant(Ty, 0.0);
4149*480093f4SDimitry Andric   auto Trunc = MIRBuilder.buildIntrinsicTrunc(Ty, SrcReg, Flags);
4150*480093f4SDimitry Andric 
4151*480093f4SDimitry Andric   auto Lt0 = MIRBuilder.buildFCmp(CmpInst::FCMP_OLT, CondTy,
4152*480093f4SDimitry Andric                                   SrcReg, Zero, Flags);
4153*480093f4SDimitry Andric   auto NeTrunc = MIRBuilder.buildFCmp(CmpInst::FCMP_ONE, CondTy,
4154*480093f4SDimitry Andric                                       SrcReg, Trunc, Flags);
4155*480093f4SDimitry Andric   auto And = MIRBuilder.buildAnd(CondTy, Lt0, NeTrunc);
4156*480093f4SDimitry Andric   auto AddVal = MIRBuilder.buildSITOFP(Ty, And);
4157*480093f4SDimitry Andric 
4158*480093f4SDimitry Andric   MIRBuilder.buildFAdd(DstReg, Trunc, AddVal);
4159*480093f4SDimitry Andric   MI.eraseFromParent();
4160*480093f4SDimitry Andric   return Legalized;
4161*480093f4SDimitry Andric }
4162*480093f4SDimitry Andric 
4163*480093f4SDimitry Andric LegalizerHelper::LegalizeResult
41648bcb0991SDimitry Andric LegalizerHelper::lowerUnmergeValues(MachineInstr &MI) {
41658bcb0991SDimitry Andric   const unsigned NumDst = MI.getNumOperands() - 1;
41668bcb0991SDimitry Andric   const Register SrcReg = MI.getOperand(NumDst).getReg();
41678bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(SrcReg);
41688bcb0991SDimitry Andric 
41698bcb0991SDimitry Andric   Register Dst0Reg = MI.getOperand(0).getReg();
41708bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst0Reg);
41718bcb0991SDimitry Andric 
41728bcb0991SDimitry Andric 
41738bcb0991SDimitry Andric   // Expand scalarizing unmerge as bitcast to integer and shift.
41748bcb0991SDimitry Andric   if (!DstTy.isVector() && SrcTy.isVector() &&
41758bcb0991SDimitry Andric       SrcTy.getElementType() == DstTy) {
41768bcb0991SDimitry Andric     LLT IntTy = LLT::scalar(SrcTy.getSizeInBits());
41778bcb0991SDimitry Andric     Register Cast = MIRBuilder.buildBitcast(IntTy, SrcReg).getReg(0);
41788bcb0991SDimitry Andric 
41798bcb0991SDimitry Andric     MIRBuilder.buildTrunc(Dst0Reg, Cast);
41808bcb0991SDimitry Andric 
41818bcb0991SDimitry Andric     const unsigned DstSize = DstTy.getSizeInBits();
41828bcb0991SDimitry Andric     unsigned Offset = DstSize;
41838bcb0991SDimitry Andric     for (unsigned I = 1; I != NumDst; ++I, Offset += DstSize) {
41848bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(IntTy, Offset);
41858bcb0991SDimitry Andric       auto Shift = MIRBuilder.buildLShr(IntTy, Cast, ShiftAmt);
41868bcb0991SDimitry Andric       MIRBuilder.buildTrunc(MI.getOperand(I), Shift);
41878bcb0991SDimitry Andric     }
41888bcb0991SDimitry Andric 
41898bcb0991SDimitry Andric     MI.eraseFromParent();
41908bcb0991SDimitry Andric     return Legalized;
41918bcb0991SDimitry Andric   }
41928bcb0991SDimitry Andric 
41938bcb0991SDimitry Andric   return UnableToLegalize;
41948bcb0991SDimitry Andric }
41958bcb0991SDimitry Andric 
41968bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
41978bcb0991SDimitry Andric LegalizerHelper::lowerShuffleVector(MachineInstr &MI) {
41988bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
41998bcb0991SDimitry Andric   Register Src0Reg = MI.getOperand(1).getReg();
42008bcb0991SDimitry Andric   Register Src1Reg = MI.getOperand(2).getReg();
42018bcb0991SDimitry Andric   LLT Src0Ty = MRI.getType(Src0Reg);
42028bcb0991SDimitry Andric   LLT DstTy = MRI.getType(DstReg);
42038bcb0991SDimitry Andric   LLT IdxTy = LLT::scalar(32);
42048bcb0991SDimitry Andric 
4205*480093f4SDimitry Andric   ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
42068bcb0991SDimitry Andric 
42078bcb0991SDimitry Andric   if (DstTy.isScalar()) {
42088bcb0991SDimitry Andric     if (Src0Ty.isVector())
42098bcb0991SDimitry Andric       return UnableToLegalize;
42108bcb0991SDimitry Andric 
42118bcb0991SDimitry Andric     // This is just a SELECT.
42128bcb0991SDimitry Andric     assert(Mask.size() == 1 && "Expected a single mask element");
42138bcb0991SDimitry Andric     Register Val;
42148bcb0991SDimitry Andric     if (Mask[0] < 0 || Mask[0] > 1)
42158bcb0991SDimitry Andric       Val = MIRBuilder.buildUndef(DstTy).getReg(0);
42168bcb0991SDimitry Andric     else
42178bcb0991SDimitry Andric       Val = Mask[0] == 0 ? Src0Reg : Src1Reg;
42188bcb0991SDimitry Andric     MIRBuilder.buildCopy(DstReg, Val);
42198bcb0991SDimitry Andric     MI.eraseFromParent();
42208bcb0991SDimitry Andric     return Legalized;
42218bcb0991SDimitry Andric   }
42228bcb0991SDimitry Andric 
42238bcb0991SDimitry Andric   Register Undef;
42248bcb0991SDimitry Andric   SmallVector<Register, 32> BuildVec;
42258bcb0991SDimitry Andric   LLT EltTy = DstTy.getElementType();
42268bcb0991SDimitry Andric 
42278bcb0991SDimitry Andric   for (int Idx : Mask) {
42288bcb0991SDimitry Andric     if (Idx < 0) {
42298bcb0991SDimitry Andric       if (!Undef.isValid())
42308bcb0991SDimitry Andric         Undef = MIRBuilder.buildUndef(EltTy).getReg(0);
42318bcb0991SDimitry Andric       BuildVec.push_back(Undef);
42328bcb0991SDimitry Andric       continue;
42338bcb0991SDimitry Andric     }
42348bcb0991SDimitry Andric 
42358bcb0991SDimitry Andric     if (Src0Ty.isScalar()) {
42368bcb0991SDimitry Andric       BuildVec.push_back(Idx == 0 ? Src0Reg : Src1Reg);
42378bcb0991SDimitry Andric     } else {
42388bcb0991SDimitry Andric       int NumElts = Src0Ty.getNumElements();
42398bcb0991SDimitry Andric       Register SrcVec = Idx < NumElts ? Src0Reg : Src1Reg;
42408bcb0991SDimitry Andric       int ExtractIdx = Idx < NumElts ? Idx : Idx - NumElts;
42418bcb0991SDimitry Andric       auto IdxK = MIRBuilder.buildConstant(IdxTy, ExtractIdx);
42428bcb0991SDimitry Andric       auto Extract = MIRBuilder.buildExtractVectorElement(EltTy, SrcVec, IdxK);
42438bcb0991SDimitry Andric       BuildVec.push_back(Extract.getReg(0));
42448bcb0991SDimitry Andric     }
42458bcb0991SDimitry Andric   }
42468bcb0991SDimitry Andric 
42478bcb0991SDimitry Andric   MIRBuilder.buildBuildVector(DstReg, BuildVec);
42488bcb0991SDimitry Andric   MI.eraseFromParent();
42498bcb0991SDimitry Andric   return Legalized;
42508bcb0991SDimitry Andric }
42518bcb0991SDimitry Andric 
42528bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
42538bcb0991SDimitry Andric LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) {
42548bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
42558bcb0991SDimitry Andric   Register AllocSize = MI.getOperand(1).getReg();
42568bcb0991SDimitry Andric   unsigned Align = MI.getOperand(2).getImm();
42578bcb0991SDimitry Andric 
42588bcb0991SDimitry Andric   const auto &MF = *MI.getMF();
42598bcb0991SDimitry Andric   const auto &TLI = *MF.getSubtarget().getTargetLowering();
42608bcb0991SDimitry Andric 
42618bcb0991SDimitry Andric   LLT PtrTy = MRI.getType(Dst);
42628bcb0991SDimitry Andric   LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
42638bcb0991SDimitry Andric 
42648bcb0991SDimitry Andric   Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
42658bcb0991SDimitry Andric   auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg);
42668bcb0991SDimitry Andric   SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp);
42678bcb0991SDimitry Andric 
42688bcb0991SDimitry Andric   // Subtract the final alloc from the SP. We use G_PTRTOINT here so we don't
42698bcb0991SDimitry Andric   // have to generate an extra instruction to negate the alloc and then use
4270*480093f4SDimitry Andric   // G_PTR_ADD to add the negative offset.
42718bcb0991SDimitry Andric   auto Alloc = MIRBuilder.buildSub(IntPtrTy, SPTmp, AllocSize);
42728bcb0991SDimitry Andric   if (Align) {
42738bcb0991SDimitry Andric     APInt AlignMask(IntPtrTy.getSizeInBits(), Align, true);
42748bcb0991SDimitry Andric     AlignMask.negate();
42758bcb0991SDimitry Andric     auto AlignCst = MIRBuilder.buildConstant(IntPtrTy, AlignMask);
42768bcb0991SDimitry Andric     Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst);
42778bcb0991SDimitry Andric   }
42788bcb0991SDimitry Andric 
42798bcb0991SDimitry Andric   SPTmp = MIRBuilder.buildCast(PtrTy, Alloc);
42808bcb0991SDimitry Andric   MIRBuilder.buildCopy(SPReg, SPTmp);
42818bcb0991SDimitry Andric   MIRBuilder.buildCopy(Dst, SPTmp);
42828bcb0991SDimitry Andric 
42838bcb0991SDimitry Andric   MI.eraseFromParent();
42848bcb0991SDimitry Andric   return Legalized;
42858bcb0991SDimitry Andric }
42868bcb0991SDimitry Andric 
42878bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
42888bcb0991SDimitry Andric LegalizerHelper::lowerExtract(MachineInstr &MI) {
42898bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
42908bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
42918bcb0991SDimitry Andric   unsigned Offset = MI.getOperand(2).getImm();
42928bcb0991SDimitry Andric 
42938bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Dst);
42948bcb0991SDimitry Andric   LLT SrcTy = MRI.getType(Src);
42958bcb0991SDimitry Andric 
42968bcb0991SDimitry Andric   if (DstTy.isScalar() &&
42978bcb0991SDimitry Andric       (SrcTy.isScalar() ||
42988bcb0991SDimitry Andric        (SrcTy.isVector() && DstTy == SrcTy.getElementType()))) {
42998bcb0991SDimitry Andric     LLT SrcIntTy = SrcTy;
43008bcb0991SDimitry Andric     if (!SrcTy.isScalar()) {
43018bcb0991SDimitry Andric       SrcIntTy = LLT::scalar(SrcTy.getSizeInBits());
43028bcb0991SDimitry Andric       Src = MIRBuilder.buildBitcast(SrcIntTy, Src).getReg(0);
43038bcb0991SDimitry Andric     }
43048bcb0991SDimitry Andric 
43058bcb0991SDimitry Andric     if (Offset == 0)
43068bcb0991SDimitry Andric       MIRBuilder.buildTrunc(Dst, Src);
43078bcb0991SDimitry Andric     else {
43088bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(SrcIntTy, Offset);
43098bcb0991SDimitry Andric       auto Shr = MIRBuilder.buildLShr(SrcIntTy, Src, ShiftAmt);
43108bcb0991SDimitry Andric       MIRBuilder.buildTrunc(Dst, Shr);
43118bcb0991SDimitry Andric     }
43128bcb0991SDimitry Andric 
43138bcb0991SDimitry Andric     MI.eraseFromParent();
43148bcb0991SDimitry Andric     return Legalized;
43158bcb0991SDimitry Andric   }
43168bcb0991SDimitry Andric 
43178bcb0991SDimitry Andric   return UnableToLegalize;
43188bcb0991SDimitry Andric }
43198bcb0991SDimitry Andric 
43208bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerInsert(MachineInstr &MI) {
43218bcb0991SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
43228bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
43238bcb0991SDimitry Andric   Register InsertSrc = MI.getOperand(2).getReg();
43248bcb0991SDimitry Andric   uint64_t Offset = MI.getOperand(3).getImm();
43258bcb0991SDimitry Andric 
43268bcb0991SDimitry Andric   LLT DstTy = MRI.getType(Src);
43278bcb0991SDimitry Andric   LLT InsertTy = MRI.getType(InsertSrc);
43288bcb0991SDimitry Andric 
43298bcb0991SDimitry Andric   if (InsertTy.isScalar() &&
43308bcb0991SDimitry Andric       (DstTy.isScalar() ||
43318bcb0991SDimitry Andric        (DstTy.isVector() && DstTy.getElementType() == InsertTy))) {
43328bcb0991SDimitry Andric     LLT IntDstTy = DstTy;
43338bcb0991SDimitry Andric     if (!DstTy.isScalar()) {
43348bcb0991SDimitry Andric       IntDstTy = LLT::scalar(DstTy.getSizeInBits());
43358bcb0991SDimitry Andric       Src = MIRBuilder.buildBitcast(IntDstTy, Src).getReg(0);
43368bcb0991SDimitry Andric     }
43378bcb0991SDimitry Andric 
43388bcb0991SDimitry Andric     Register ExtInsSrc = MIRBuilder.buildZExt(IntDstTy, InsertSrc).getReg(0);
43398bcb0991SDimitry Andric     if (Offset != 0) {
43408bcb0991SDimitry Andric       auto ShiftAmt = MIRBuilder.buildConstant(IntDstTy, Offset);
43418bcb0991SDimitry Andric       ExtInsSrc = MIRBuilder.buildShl(IntDstTy, ExtInsSrc, ShiftAmt).getReg(0);
43428bcb0991SDimitry Andric     }
43438bcb0991SDimitry Andric 
43448bcb0991SDimitry Andric     APInt MaskVal = ~APInt::getBitsSet(DstTy.getSizeInBits(), Offset,
43458bcb0991SDimitry Andric                                        InsertTy.getSizeInBits());
43468bcb0991SDimitry Andric 
43478bcb0991SDimitry Andric     auto Mask = MIRBuilder.buildConstant(IntDstTy, MaskVal);
43488bcb0991SDimitry Andric     auto MaskedSrc = MIRBuilder.buildAnd(IntDstTy, Src, Mask);
43498bcb0991SDimitry Andric     auto Or = MIRBuilder.buildOr(IntDstTy, MaskedSrc, ExtInsSrc);
43508bcb0991SDimitry Andric 
43518bcb0991SDimitry Andric     MIRBuilder.buildBitcast(Dst, Or);
43528bcb0991SDimitry Andric     MI.eraseFromParent();
43538bcb0991SDimitry Andric     return Legalized;
43548bcb0991SDimitry Andric   }
43558bcb0991SDimitry Andric 
43568bcb0991SDimitry Andric   return UnableToLegalize;
43578bcb0991SDimitry Andric }
43588bcb0991SDimitry Andric 
43598bcb0991SDimitry Andric LegalizerHelper::LegalizeResult
43608bcb0991SDimitry Andric LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) {
43618bcb0991SDimitry Andric   Register Dst0 = MI.getOperand(0).getReg();
43628bcb0991SDimitry Andric   Register Dst1 = MI.getOperand(1).getReg();
43638bcb0991SDimitry Andric   Register LHS = MI.getOperand(2).getReg();
43648bcb0991SDimitry Andric   Register RHS = MI.getOperand(3).getReg();
43658bcb0991SDimitry Andric   const bool IsAdd = MI.getOpcode() == TargetOpcode::G_SADDO;
43668bcb0991SDimitry Andric 
43678bcb0991SDimitry Andric   LLT Ty = MRI.getType(Dst0);
43688bcb0991SDimitry Andric   LLT BoolTy = MRI.getType(Dst1);
43698bcb0991SDimitry Andric 
43708bcb0991SDimitry Andric   if (IsAdd)
43718bcb0991SDimitry Andric     MIRBuilder.buildAdd(Dst0, LHS, RHS);
43728bcb0991SDimitry Andric   else
43738bcb0991SDimitry Andric     MIRBuilder.buildSub(Dst0, LHS, RHS);
43748bcb0991SDimitry Andric 
43758bcb0991SDimitry Andric   // TODO: If SADDSAT/SSUBSAT is legal, compare results to detect overflow.
43768bcb0991SDimitry Andric 
43778bcb0991SDimitry Andric   auto Zero = MIRBuilder.buildConstant(Ty, 0);
43788bcb0991SDimitry Andric 
43798bcb0991SDimitry Andric   // For an addition, the result should be less than one of the operands (LHS)
43808bcb0991SDimitry Andric   // if and only if the other operand (RHS) is negative, otherwise there will
43818bcb0991SDimitry Andric   // be overflow.
43828bcb0991SDimitry Andric   // For a subtraction, the result should be less than one of the operands
43838bcb0991SDimitry Andric   // (LHS) if and only if the other operand (RHS) is (non-zero) positive,
43848bcb0991SDimitry Andric   // otherwise there will be overflow.
43858bcb0991SDimitry Andric   auto ResultLowerThanLHS =
43868bcb0991SDimitry Andric       MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Dst0, LHS);
43878bcb0991SDimitry Andric   auto ConditionRHS = MIRBuilder.buildICmp(
43888bcb0991SDimitry Andric       IsAdd ? CmpInst::ICMP_SLT : CmpInst::ICMP_SGT, BoolTy, RHS, Zero);
43898bcb0991SDimitry Andric 
43908bcb0991SDimitry Andric   MIRBuilder.buildXor(Dst1, ConditionRHS, ResultLowerThanLHS);
43918bcb0991SDimitry Andric   MI.eraseFromParent();
43928bcb0991SDimitry Andric   return Legalized;
43938bcb0991SDimitry Andric }
4394*480093f4SDimitry Andric 
4395*480093f4SDimitry Andric LegalizerHelper::LegalizeResult
4396*480093f4SDimitry Andric LegalizerHelper::lowerBswap(MachineInstr &MI) {
4397*480093f4SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4398*480093f4SDimitry Andric   Register Src = MI.getOperand(1).getReg();
4399*480093f4SDimitry Andric   const LLT Ty = MRI.getType(Src);
4400*480093f4SDimitry Andric   unsigned SizeInBytes = Ty.getSizeInBytes();
4401*480093f4SDimitry Andric   unsigned BaseShiftAmt = (SizeInBytes - 1) * 8;
4402*480093f4SDimitry Andric 
4403*480093f4SDimitry Andric   // Swap most and least significant byte, set remaining bytes in Res to zero.
4404*480093f4SDimitry Andric   auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt);
4405*480093f4SDimitry Andric   auto LSByteShiftedLeft = MIRBuilder.buildShl(Ty, Src, ShiftAmt);
4406*480093f4SDimitry Andric   auto MSByteShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt);
4407*480093f4SDimitry Andric   auto Res = MIRBuilder.buildOr(Ty, MSByteShiftedRight, LSByteShiftedLeft);
4408*480093f4SDimitry Andric 
4409*480093f4SDimitry Andric   // Set i-th high/low byte in Res to i-th low/high byte from Src.
4410*480093f4SDimitry Andric   for (unsigned i = 1; i < SizeInBytes / 2; ++i) {
4411*480093f4SDimitry Andric     // AND with Mask leaves byte i unchanged and sets remaining bytes to 0.
4412*480093f4SDimitry Andric     APInt APMask(SizeInBytes * 8, 0xFF << (i * 8));
4413*480093f4SDimitry Andric     auto Mask = MIRBuilder.buildConstant(Ty, APMask);
4414*480093f4SDimitry Andric     auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt - 16 * i);
4415*480093f4SDimitry Andric     // Low byte shifted left to place of high byte: (Src & Mask) << ShiftAmt.
4416*480093f4SDimitry Andric     auto LoByte = MIRBuilder.buildAnd(Ty, Src, Mask);
4417*480093f4SDimitry Andric     auto LoShiftedLeft = MIRBuilder.buildShl(Ty, LoByte, ShiftAmt);
4418*480093f4SDimitry Andric     Res = MIRBuilder.buildOr(Ty, Res, LoShiftedLeft);
4419*480093f4SDimitry Andric     // High byte shifted right to place of low byte: (Src >> ShiftAmt) & Mask.
4420*480093f4SDimitry Andric     auto SrcShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt);
4421*480093f4SDimitry Andric     auto HiShiftedRight = MIRBuilder.buildAnd(Ty, SrcShiftedRight, Mask);
4422*480093f4SDimitry Andric     Res = MIRBuilder.buildOr(Ty, Res, HiShiftedRight);
4423*480093f4SDimitry Andric   }
4424*480093f4SDimitry Andric   Res.getInstr()->getOperand(0).setReg(Dst);
4425*480093f4SDimitry Andric 
4426*480093f4SDimitry Andric   MI.eraseFromParent();
4427*480093f4SDimitry Andric   return Legalized;
4428*480093f4SDimitry Andric }
4429*480093f4SDimitry Andric 
4430*480093f4SDimitry Andric //{ (Src & Mask) >> N } | { (Src << N) & Mask }
4431*480093f4SDimitry Andric static MachineInstrBuilder SwapN(unsigned N, DstOp Dst, MachineIRBuilder &B,
4432*480093f4SDimitry Andric                                  MachineInstrBuilder Src, APInt Mask) {
4433*480093f4SDimitry Andric   const LLT Ty = Dst.getLLTTy(*B.getMRI());
4434*480093f4SDimitry Andric   MachineInstrBuilder C_N = B.buildConstant(Ty, N);
4435*480093f4SDimitry Andric   MachineInstrBuilder MaskLoNTo0 = B.buildConstant(Ty, Mask);
4436*480093f4SDimitry Andric   auto LHS = B.buildLShr(Ty, B.buildAnd(Ty, Src, MaskLoNTo0), C_N);
4437*480093f4SDimitry Andric   auto RHS = B.buildAnd(Ty, B.buildShl(Ty, Src, C_N), MaskLoNTo0);
4438*480093f4SDimitry Andric   return B.buildOr(Dst, LHS, RHS);
4439*480093f4SDimitry Andric }
4440*480093f4SDimitry Andric 
4441*480093f4SDimitry Andric LegalizerHelper::LegalizeResult
4442*480093f4SDimitry Andric LegalizerHelper::lowerBitreverse(MachineInstr &MI) {
4443*480093f4SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4444*480093f4SDimitry Andric   Register Src = MI.getOperand(1).getReg();
4445*480093f4SDimitry Andric   const LLT Ty = MRI.getType(Src);
4446*480093f4SDimitry Andric   unsigned Size = Ty.getSizeInBits();
4447*480093f4SDimitry Andric 
4448*480093f4SDimitry Andric   MachineInstrBuilder BSWAP =
4449*480093f4SDimitry Andric       MIRBuilder.buildInstr(TargetOpcode::G_BSWAP, {Ty}, {Src});
4450*480093f4SDimitry Andric 
4451*480093f4SDimitry Andric   // swap high and low 4 bits in 8 bit blocks 7654|3210 -> 3210|7654
4452*480093f4SDimitry Andric   //    [(val & 0xF0F0F0F0) >> 4] | [(val & 0x0F0F0F0F) << 4]
4453*480093f4SDimitry Andric   // -> [(val & 0xF0F0F0F0) >> 4] | [(val << 4) & 0xF0F0F0F0]
4454*480093f4SDimitry Andric   MachineInstrBuilder Swap4 =
4455*480093f4SDimitry Andric       SwapN(4, Ty, MIRBuilder, BSWAP, APInt::getSplat(Size, APInt(8, 0xF0)));
4456*480093f4SDimitry Andric 
4457*480093f4SDimitry Andric   // swap high and low 2 bits in 4 bit blocks 32|10 76|54 -> 10|32 54|76
4458*480093f4SDimitry Andric   //    [(val & 0xCCCCCCCC) >> 2] & [(val & 0x33333333) << 2]
4459*480093f4SDimitry Andric   // -> [(val & 0xCCCCCCCC) >> 2] & [(val << 2) & 0xCCCCCCCC]
4460*480093f4SDimitry Andric   MachineInstrBuilder Swap2 =
4461*480093f4SDimitry Andric       SwapN(2, Ty, MIRBuilder, Swap4, APInt::getSplat(Size, APInt(8, 0xCC)));
4462*480093f4SDimitry Andric 
4463*480093f4SDimitry 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
4464*480093f4SDimitry Andric   //    [(val & 0xAAAAAAAA) >> 1] & [(val & 0x55555555) << 1]
4465*480093f4SDimitry Andric   // -> [(val & 0xAAAAAAAA) >> 1] & [(val << 1) & 0xAAAAAAAA]
4466*480093f4SDimitry Andric   SwapN(1, Dst, MIRBuilder, Swap2, APInt::getSplat(Size, APInt(8, 0xAA)));
4467*480093f4SDimitry Andric 
4468*480093f4SDimitry Andric   MI.eraseFromParent();
4469*480093f4SDimitry Andric   return Legalized;
4470*480093f4SDimitry Andric }
4471*480093f4SDimitry Andric 
4472*480093f4SDimitry Andric LegalizerHelper::LegalizeResult
4473*480093f4SDimitry Andric LegalizerHelper::lowerReadRegister(MachineInstr &MI) {
4474*480093f4SDimitry Andric   Register Dst = MI.getOperand(0).getReg();
4475*480093f4SDimitry Andric   const LLT Ty = MRI.getType(Dst);
4476*480093f4SDimitry Andric   const MDString *RegStr = cast<MDString>(
4477*480093f4SDimitry Andric     cast<MDNode>(MI.getOperand(1).getMetadata())->getOperand(0));
4478*480093f4SDimitry Andric 
4479*480093f4SDimitry Andric   MachineFunction &MF = MIRBuilder.getMF();
4480*480093f4SDimitry Andric   const TargetSubtargetInfo &STI = MF.getSubtarget();
4481*480093f4SDimitry Andric   const TargetLowering *TLI = STI.getTargetLowering();
4482*480093f4SDimitry Andric   Register Reg = TLI->getRegisterByName(RegStr->getString().data(), Ty, MF);
4483*480093f4SDimitry Andric   if (!Reg.isValid())
4484*480093f4SDimitry Andric     return UnableToLegalize;
4485*480093f4SDimitry Andric 
4486*480093f4SDimitry Andric   MIRBuilder.buildCopy(Dst, Reg);
4487*480093f4SDimitry Andric   MI.eraseFromParent();
4488*480093f4SDimitry Andric   return Legalized;
4489*480093f4SDimitry Andric }
4490