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" 20*8bcb0991SDimitry 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 175*8bcb0991SDimitry Andric static LLT getGCDType(LLT OrigTy, LLT TargetTy) { 176*8bcb0991SDimitry Andric if (OrigTy.isVector() && TargetTy.isVector()) { 177*8bcb0991SDimitry Andric assert(OrigTy.getElementType() == TargetTy.getElementType()); 178*8bcb0991SDimitry Andric int GCD = greatestCommonDivisor(OrigTy.getNumElements(), 179*8bcb0991SDimitry Andric TargetTy.getNumElements()); 180*8bcb0991SDimitry Andric return LLT::scalarOrVector(GCD, OrigTy.getElementType()); 181*8bcb0991SDimitry Andric } 182*8bcb0991SDimitry Andric 183*8bcb0991SDimitry Andric if (OrigTy.isVector() && !TargetTy.isVector()) { 184*8bcb0991SDimitry Andric assert(OrigTy.getElementType() == TargetTy); 185*8bcb0991SDimitry Andric return TargetTy; 186*8bcb0991SDimitry Andric } 187*8bcb0991SDimitry Andric 188*8bcb0991SDimitry Andric assert(!OrigTy.isVector() && !TargetTy.isVector()); 189*8bcb0991SDimitry Andric 190*8bcb0991SDimitry Andric int GCD = greatestCommonDivisor(OrigTy.getSizeInBits(), 191*8bcb0991SDimitry Andric TargetTy.getSizeInBits()); 192*8bcb0991SDimitry Andric return LLT::scalar(GCD); 193*8bcb0991SDimitry Andric } 194*8bcb0991SDimitry 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: 243*8bcb0991SDimitry Andric assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); 244*8bcb0991SDimitry Andric switch (Size) { 245*8bcb0991SDimitry Andric case 32: 246*8bcb0991SDimitry Andric return RTLIB::SDIV_I32; 247*8bcb0991SDimitry Andric case 64: 248*8bcb0991SDimitry Andric return RTLIB::SDIV_I64; 249*8bcb0991SDimitry Andric case 128: 250*8bcb0991SDimitry Andric return RTLIB::SDIV_I128; 251*8bcb0991SDimitry Andric default: 252*8bcb0991SDimitry Andric llvm_unreachable("unexpected size"); 253*8bcb0991SDimitry Andric } 2540b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 255*8bcb0991SDimitry Andric assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); 256*8bcb0991SDimitry Andric switch (Size) { 257*8bcb0991SDimitry Andric case 32: 258*8bcb0991SDimitry Andric return RTLIB::UDIV_I32; 259*8bcb0991SDimitry Andric case 64: 260*8bcb0991SDimitry Andric return RTLIB::UDIV_I64; 261*8bcb0991SDimitry Andric case 128: 262*8bcb0991SDimitry Andric return RTLIB::UDIV_I128; 263*8bcb0991SDimitry Andric default: 264*8bcb0991SDimitry Andric llvm_unreachable("unexpected size"); 265*8bcb0991SDimitry 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 330*8bcb0991SDimitry Andric /// True if an instruction is in tail position in its caller. Intended for 331*8bcb0991SDimitry Andric /// legalizing libcalls as tail calls when possible. 332*8bcb0991SDimitry Andric static bool isLibCallInTailPosition(MachineInstr &MI) { 333*8bcb0991SDimitry Andric const Function &F = MI.getParent()->getParent()->getFunction(); 334*8bcb0991SDimitry Andric 335*8bcb0991SDimitry Andric // Conservatively require the attributes of the call to match those of 336*8bcb0991SDimitry Andric // the return. Ignore NoAlias and NonNull because they don't affect the 337*8bcb0991SDimitry Andric // call sequence. 338*8bcb0991SDimitry Andric AttributeList CallerAttrs = F.getAttributes(); 339*8bcb0991SDimitry Andric if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex) 340*8bcb0991SDimitry Andric .removeAttribute(Attribute::NoAlias) 341*8bcb0991SDimitry Andric .removeAttribute(Attribute::NonNull) 342*8bcb0991SDimitry Andric .hasAttributes()) 343*8bcb0991SDimitry Andric return false; 344*8bcb0991SDimitry Andric 345*8bcb0991SDimitry Andric // It's not safe to eliminate the sign / zero extension of the return value. 346*8bcb0991SDimitry Andric if (CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt) || 347*8bcb0991SDimitry Andric CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt)) 348*8bcb0991SDimitry Andric return false; 349*8bcb0991SDimitry Andric 350*8bcb0991SDimitry Andric // Only tail call if the following instruction is a standard return. 351*8bcb0991SDimitry Andric auto &TII = *MI.getMF()->getSubtarget().getInstrInfo(); 352*8bcb0991SDimitry Andric MachineInstr *Next = MI.getNextNode(); 353*8bcb0991SDimitry Andric if (!Next || TII.isTailCall(*Next) || !Next->isReturn()) 354*8bcb0991SDimitry Andric return false; 355*8bcb0991SDimitry Andric 356*8bcb0991SDimitry Andric return true; 357*8bcb0991SDimitry Andric } 358*8bcb0991SDimitry 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 367*8bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 368*8bcb0991SDimitry Andric Info.CallConv = TLI.getLibcallCallingConv(Libcall); 369*8bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 370*8bcb0991SDimitry Andric Info.OrigRet = Result; 371*8bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 372*8bcb0991SDimitry 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 391*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 392*8bcb0991SDimitry Andric llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 393*8bcb0991SDimitry Andric MachineInstr &MI) { 394*8bcb0991SDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS); 395*8bcb0991SDimitry Andric auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); 396*8bcb0991SDimitry Andric 397*8bcb0991SDimitry Andric SmallVector<CallLowering::ArgInfo, 3> Args; 398*8bcb0991SDimitry Andric // Add all the args, except for the last which is an imm denoting 'tail'. 399*8bcb0991SDimitry Andric for (unsigned i = 1; i < MI.getNumOperands() - 1; i++) { 400*8bcb0991SDimitry Andric Register Reg = MI.getOperand(i).getReg(); 401*8bcb0991SDimitry Andric 402*8bcb0991SDimitry Andric // Need derive an IR type for call lowering. 403*8bcb0991SDimitry Andric LLT OpLLT = MRI.getType(Reg); 404*8bcb0991SDimitry Andric Type *OpTy = nullptr; 405*8bcb0991SDimitry Andric if (OpLLT.isPointer()) 406*8bcb0991SDimitry Andric OpTy = Type::getInt8PtrTy(Ctx, OpLLT.getAddressSpace()); 407*8bcb0991SDimitry Andric else 408*8bcb0991SDimitry Andric OpTy = IntegerType::get(Ctx, OpLLT.getSizeInBits()); 409*8bcb0991SDimitry Andric Args.push_back({Reg, OpTy}); 410*8bcb0991SDimitry Andric } 411*8bcb0991SDimitry Andric 412*8bcb0991SDimitry Andric auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); 413*8bcb0991SDimitry Andric auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); 414*8bcb0991SDimitry Andric Intrinsic::ID ID = MI.getOperand(0).getIntrinsicID(); 415*8bcb0991SDimitry Andric RTLIB::Libcall RTLibcall; 416*8bcb0991SDimitry Andric switch (ID) { 417*8bcb0991SDimitry Andric case Intrinsic::memcpy: 418*8bcb0991SDimitry Andric RTLibcall = RTLIB::MEMCPY; 419*8bcb0991SDimitry Andric break; 420*8bcb0991SDimitry Andric case Intrinsic::memset: 421*8bcb0991SDimitry Andric RTLibcall = RTLIB::MEMSET; 422*8bcb0991SDimitry Andric break; 423*8bcb0991SDimitry Andric case Intrinsic::memmove: 424*8bcb0991SDimitry Andric RTLibcall = RTLIB::MEMMOVE; 425*8bcb0991SDimitry Andric break; 426*8bcb0991SDimitry Andric default: 427*8bcb0991SDimitry Andric return LegalizerHelper::UnableToLegalize; 428*8bcb0991SDimitry Andric } 429*8bcb0991SDimitry Andric const char *Name = TLI.getLibcallName(RTLibcall); 430*8bcb0991SDimitry Andric 431*8bcb0991SDimitry Andric MIRBuilder.setInstr(MI); 432*8bcb0991SDimitry Andric 433*8bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 434*8bcb0991SDimitry Andric Info.CallConv = TLI.getLibcallCallingConv(RTLibcall); 435*8bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 436*8bcb0991SDimitry Andric Info.OrigRet = CallLowering::ArgInfo({0}, Type::getVoidTy(Ctx)); 437*8bcb0991SDimitry Andric Info.IsTailCall = MI.getOperand(MI.getNumOperands() - 1).getImm() == 1 && 438*8bcb0991SDimitry Andric isLibCallInTailPosition(MI); 439*8bcb0991SDimitry Andric 440*8bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 441*8bcb0991SDimitry Andric if (!CLI.lowerCall(MIRBuilder, Info)) 442*8bcb0991SDimitry Andric return LegalizerHelper::UnableToLegalize; 443*8bcb0991SDimitry Andric 444*8bcb0991SDimitry Andric if (Info.LoweredTailCall) { 445*8bcb0991SDimitry Andric assert(Info.IsTailCall && "Lowered tail call when it wasn't a tail call?"); 446*8bcb0991SDimitry Andric // We must have a return following the call to get past 447*8bcb0991SDimitry Andric // isLibCallInTailPosition. 448*8bcb0991SDimitry Andric assert(MI.getNextNode() && MI.getNextNode()->isReturn() && 449*8bcb0991SDimitry Andric "Expected instr following MI to be a return?"); 450*8bcb0991SDimitry Andric 451*8bcb0991SDimitry Andric // We lowered a tail call, so the call is now the return from the block. 452*8bcb0991SDimitry Andric // Delete the old return. 453*8bcb0991SDimitry Andric MI.getNextNode()->eraseFromParent(); 454*8bcb0991SDimitry Andric } 455*8bcb0991SDimitry Andric 456*8bcb0991SDimitry Andric return LegalizerHelper::Legalized; 457*8bcb0991SDimitry Andric } 458*8bcb0991SDimitry 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 } 660*8bcb0991SDimitry Andric case TargetOpcode::G_SEXT: { 661*8bcb0991SDimitry Andric if (TypeIdx != 0) 662*8bcb0991SDimitry Andric return UnableToLegalize; 663*8bcb0991SDimitry Andric 664*8bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 665*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 666*8bcb0991SDimitry Andric 667*8bcb0991SDimitry Andric // FIXME: support the general case where the requested NarrowTy may not be 668*8bcb0991SDimitry Andric // the same as the source type. E.g. s128 = sext(s32) 669*8bcb0991SDimitry Andric if ((SrcTy.getSizeInBits() != SizeOp0 / 2) || 670*8bcb0991SDimitry Andric SrcTy.getSizeInBits() != NarrowTy.getSizeInBits()) { 671*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow sext to type " << NarrowTy << "\n"); 672*8bcb0991SDimitry Andric return UnableToLegalize; 673*8bcb0991SDimitry Andric } 674*8bcb0991SDimitry Andric 675*8bcb0991SDimitry Andric // Shift the sign bit of the low register through the high register. 676*8bcb0991SDimitry Andric auto ShiftAmt = 677*8bcb0991SDimitry Andric MIRBuilder.buildConstant(LLT::scalar(64), NarrowTy.getSizeInBits() - 1); 678*8bcb0991SDimitry Andric auto Shift = MIRBuilder.buildAShr(NarrowTy, SrcReg, ShiftAmt); 679*8bcb0991SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {SrcReg, Shift.getReg(0)}); 680*8bcb0991SDimitry Andric MI.eraseFromParent(); 681*8bcb0991SDimitry Andric return Legalized; 682*8bcb0991SDimitry Andric } 683*8bcb0991SDimitry Andric case TargetOpcode::G_ZEXT: { 684*8bcb0991SDimitry Andric if (TypeIdx != 0) 685*8bcb0991SDimitry Andric return UnableToLegalize; 686*8bcb0991SDimitry Andric 687*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(MI.getOperand(1).getReg()); 688*8bcb0991SDimitry Andric uint64_t SizeOp1 = SrcTy.getSizeInBits(); 689*8bcb0991SDimitry Andric if (SizeOp0 % SizeOp1 != 0) 690*8bcb0991SDimitry Andric return UnableToLegalize; 691*8bcb0991SDimitry Andric 692*8bcb0991SDimitry Andric // Generate a merge where the bottom bits are taken from the source, and 693*8bcb0991SDimitry Andric // zero everything else. 694*8bcb0991SDimitry Andric Register ZeroReg = MIRBuilder.buildConstant(SrcTy, 0).getReg(0); 695*8bcb0991SDimitry Andric unsigned NumParts = SizeOp0 / SizeOp1; 696*8bcb0991SDimitry Andric SmallVector<Register, 4> Srcs = {MI.getOperand(1).getReg()}; 697*8bcb0991SDimitry Andric for (unsigned Part = 1; Part < NumParts; ++Part) 698*8bcb0991SDimitry Andric Srcs.push_back(ZeroReg); 699*8bcb0991SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0).getReg(), Srcs); 700*8bcb0991SDimitry Andric MI.eraseFromParent(); 701*8bcb0991SDimitry Andric return Legalized; 702*8bcb0991SDimitry Andric } 703*8bcb0991SDimitry Andric case TargetOpcode::G_TRUNC: { 704*8bcb0991SDimitry Andric if (TypeIdx != 1) 705*8bcb0991SDimitry Andric return UnableToLegalize; 706*8bcb0991SDimitry Andric 707*8bcb0991SDimitry Andric uint64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 708*8bcb0991SDimitry Andric if (NarrowTy.getSizeInBits() * 2 != SizeOp1) { 709*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow trunc to type " << NarrowTy << "\n"); 710*8bcb0991SDimitry Andric return UnableToLegalize; 711*8bcb0991SDimitry Andric } 712*8bcb0991SDimitry Andric 713*8bcb0991SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1).getReg()); 714*8bcb0991SDimitry Andric MIRBuilder.buildCopy(MI.getOperand(0).getReg(), Unmerge.getReg(0)); 715*8bcb0991SDimitry Andric MI.eraseFromParent(); 716*8bcb0991SDimitry Andric return Legalized; 717*8bcb0991SDimitry Andric } 718*8bcb0991SDimitry 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 731*8bcb0991SDimitry 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 736*8bcb0991SDimitry Andric if (i == 0) 737*8bcb0991SDimitry Andric MIRBuilder.buildUAddo(DstReg, CarryOut, Src1Regs[i], Src2Regs[i]); 738*8bcb0991SDimitry Andric else { 7390b57cec5SDimitry Andric MIRBuilder.buildUAdde(DstReg, CarryOut, Src1Regs[i], 7400b57cec5SDimitry Andric Src2Regs[i], CarryIn); 741*8bcb0991SDimitry 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 } 933*8bcb0991SDimitry 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()); 966*8bcb0991SDimitry 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 { 975*8bcb0991SDimitry Andric MachineInstrBuilder CmpH = MIRBuilder.buildICmp(Pred, ResTy, LHSH, RHSH); 9760b57cec5SDimitry Andric MachineInstrBuilder CmpHEQ = 977*8bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::Predicate::ICMP_EQ, ResTy, LHSH, RHSH); 9780b57cec5SDimitry Andric MachineInstrBuilder CmpLU = MIRBuilder.buildICmp( 979*8bcb0991SDimitry 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 } 986*8bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 987*8bcb0991SDimitry Andric if (TypeIdx != 0) 988*8bcb0991SDimitry Andric return UnableToLegalize; 989*8bcb0991SDimitry Andric 990*8bcb0991SDimitry Andric if (!MI.getOperand(2).isImm()) 991*8bcb0991SDimitry Andric return UnableToLegalize; 992*8bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 993*8bcb0991SDimitry Andric 994*8bcb0991SDimitry Andric // So long as the new type has more bits than the bits we're extending we 995*8bcb0991SDimitry Andric // don't need to break it apart. 996*8bcb0991SDimitry Andric if (NarrowTy.getScalarSizeInBits() >= SizeInBits) { 997*8bcb0991SDimitry Andric Observer.changingInstr(MI); 998*8bcb0991SDimitry Andric // We don't lose any non-extension bits by truncating the src and 999*8bcb0991SDimitry Andric // sign-extending the dst. 1000*8bcb0991SDimitry Andric MachineOperand &MO1 = MI.getOperand(1); 1001*8bcb0991SDimitry Andric auto TruncMIB = MIRBuilder.buildTrunc(NarrowTy, MO1.getReg()); 1002*8bcb0991SDimitry Andric MO1.setReg(TruncMIB->getOperand(0).getReg()); 1003*8bcb0991SDimitry Andric 1004*8bcb0991SDimitry Andric MachineOperand &MO2 = MI.getOperand(0); 1005*8bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(NarrowTy); 1006*8bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 1007*8bcb0991SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_SEXT, {MO2.getReg()}, {DstExt}); 1008*8bcb0991SDimitry Andric MO2.setReg(DstExt); 1009*8bcb0991SDimitry Andric Observer.changedInstr(MI); 1010*8bcb0991SDimitry Andric return Legalized; 1011*8bcb0991SDimitry Andric } 1012*8bcb0991SDimitry Andric 1013*8bcb0991SDimitry Andric // Break it apart. Components below the extension point are unmodified. The 1014*8bcb0991SDimitry Andric // component containing the extension point becomes a narrower SEXT_INREG. 1015*8bcb0991SDimitry Andric // Components above it are ashr'd from the component containing the 1016*8bcb0991SDimitry Andric // extension point. 1017*8bcb0991SDimitry Andric if (SizeOp0 % NarrowSize != 0) 1018*8bcb0991SDimitry Andric return UnableToLegalize; 1019*8bcb0991SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 1020*8bcb0991SDimitry Andric 1021*8bcb0991SDimitry Andric // List the registers where the destination will be scattered. 1022*8bcb0991SDimitry Andric SmallVector<Register, 2> DstRegs; 1023*8bcb0991SDimitry Andric // List the registers where the source will be split. 1024*8bcb0991SDimitry Andric SmallVector<Register, 2> SrcRegs; 1025*8bcb0991SDimitry Andric 1026*8bcb0991SDimitry Andric // Create all the temporary registers. 1027*8bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 1028*8bcb0991SDimitry Andric Register SrcReg = MRI.createGenericVirtualRegister(NarrowTy); 1029*8bcb0991SDimitry Andric 1030*8bcb0991SDimitry Andric SrcRegs.push_back(SrcReg); 1031*8bcb0991SDimitry Andric } 1032*8bcb0991SDimitry Andric 1033*8bcb0991SDimitry Andric // Explode the big arguments into smaller chunks. 1034*8bcb0991SDimitry Andric MIRBuilder.buildUnmerge(SrcRegs, MI.getOperand(1).getReg()); 1035*8bcb0991SDimitry Andric 1036*8bcb0991SDimitry Andric Register AshrCstReg = 1037*8bcb0991SDimitry Andric MIRBuilder.buildConstant(NarrowTy, NarrowTy.getScalarSizeInBits() - 1) 1038*8bcb0991SDimitry Andric ->getOperand(0) 1039*8bcb0991SDimitry Andric .getReg(); 1040*8bcb0991SDimitry Andric Register FullExtensionReg = 0; 1041*8bcb0991SDimitry Andric Register PartialExtensionReg = 0; 1042*8bcb0991SDimitry Andric 1043*8bcb0991SDimitry Andric // Do the operation on each small part. 1044*8bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 1045*8bcb0991SDimitry Andric if ((i + 1) * NarrowTy.getScalarSizeInBits() < SizeInBits) 1046*8bcb0991SDimitry Andric DstRegs.push_back(SrcRegs[i]); 1047*8bcb0991SDimitry Andric else if (i * NarrowTy.getScalarSizeInBits() > SizeInBits) { 1048*8bcb0991SDimitry Andric assert(PartialExtensionReg && 1049*8bcb0991SDimitry Andric "Expected to visit partial extension before full"); 1050*8bcb0991SDimitry Andric if (FullExtensionReg) { 1051*8bcb0991SDimitry Andric DstRegs.push_back(FullExtensionReg); 1052*8bcb0991SDimitry Andric continue; 1053*8bcb0991SDimitry Andric } 1054*8bcb0991SDimitry Andric DstRegs.push_back(MIRBuilder 1055*8bcb0991SDimitry Andric .buildInstr(TargetOpcode::G_ASHR, {NarrowTy}, 1056*8bcb0991SDimitry Andric {PartialExtensionReg, AshrCstReg}) 1057*8bcb0991SDimitry Andric ->getOperand(0) 1058*8bcb0991SDimitry Andric .getReg()); 1059*8bcb0991SDimitry Andric FullExtensionReg = DstRegs.back(); 1060*8bcb0991SDimitry Andric } else { 1061*8bcb0991SDimitry Andric DstRegs.push_back( 1062*8bcb0991SDimitry Andric MIRBuilder 1063*8bcb0991SDimitry Andric .buildInstr( 1064*8bcb0991SDimitry Andric TargetOpcode::G_SEXT_INREG, {NarrowTy}, 1065*8bcb0991SDimitry Andric {SrcRegs[i], SizeInBits % NarrowTy.getScalarSizeInBits()}) 1066*8bcb0991SDimitry Andric ->getOperand(0) 1067*8bcb0991SDimitry Andric .getReg()); 1068*8bcb0991SDimitry Andric PartialExtensionReg = DstRegs.back(); 1069*8bcb0991SDimitry Andric } 1070*8bcb0991SDimitry Andric } 1071*8bcb0991SDimitry Andric 1072*8bcb0991SDimitry Andric // Gather the destination registers into the final destination. 1073*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 1074*8bcb0991SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 1075*8bcb0991SDimitry Andric MI.eraseFromParent(); 1076*8bcb0991SDimitry Andric return Legalized; 1077*8bcb0991SDimitry Andric } 10780b57cec5SDimitry Andric } 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy, 10820b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 10830b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 10840b57cec5SDimitry Andric auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO.getReg()}); 10850b57cec5SDimitry Andric MO.setReg(ExtB->getOperand(0).getReg()); 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy, 10890b57cec5SDimitry Andric unsigned OpIdx) { 10900b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 10910b57cec5SDimitry Andric auto ExtB = MIRBuilder.buildInstr(TargetOpcode::G_TRUNC, {NarrowTy}, 10920b57cec5SDimitry Andric {MO.getReg()}); 10930b57cec5SDimitry Andric MO.setReg(ExtB->getOperand(0).getReg()); 10940b57cec5SDimitry Andric } 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy, 10970b57cec5SDimitry Andric unsigned OpIdx, unsigned TruncOpcode) { 10980b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 10990b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 11000b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 11010b57cec5SDimitry Andric MIRBuilder.buildInstr(TruncOpcode, {MO.getReg()}, {DstExt}); 11020b57cec5SDimitry Andric MO.setReg(DstExt); 11030b57cec5SDimitry Andric } 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy, 11060b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 11070b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 11080b57cec5SDimitry Andric Register DstTrunc = MRI.createGenericVirtualRegister(NarrowTy); 11090b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 11100b57cec5SDimitry Andric MIRBuilder.buildInstr(ExtOpcode, {MO.getReg()}, {DstTrunc}); 11110b57cec5SDimitry Andric MO.setReg(DstTrunc); 11120b57cec5SDimitry Andric } 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorDst(MachineInstr &MI, LLT WideTy, 11150b57cec5SDimitry Andric unsigned OpIdx) { 11160b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 11170b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 11180b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 11190b57cec5SDimitry Andric MIRBuilder.buildExtract(MO.getReg(), DstExt, 0); 11200b57cec5SDimitry Andric MO.setReg(DstExt); 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorSrc(MachineInstr &MI, LLT MoreTy, 11240b57cec5SDimitry Andric unsigned OpIdx) { 11250b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric LLT OldTy = MRI.getType(MO.getReg()); 11280b57cec5SDimitry Andric unsigned OldElts = OldTy.getNumElements(); 11290b57cec5SDimitry Andric unsigned NewElts = MoreTy.getNumElements(); 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric unsigned NumParts = NewElts / OldElts; 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric // Use concat_vectors if the result is a multiple of the number of elements. 11340b57cec5SDimitry Andric if (NumParts * OldElts == NewElts) { 11350b57cec5SDimitry Andric SmallVector<Register, 8> Parts; 11360b57cec5SDimitry Andric Parts.push_back(MO.getReg()); 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric Register ImpDef = MIRBuilder.buildUndef(OldTy).getReg(0); 11390b57cec5SDimitry Andric for (unsigned I = 1; I != NumParts; ++I) 11400b57cec5SDimitry Andric Parts.push_back(ImpDef); 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric auto Concat = MIRBuilder.buildConcatVectors(MoreTy, Parts); 11430b57cec5SDimitry Andric MO.setReg(Concat.getReg(0)); 11440b57cec5SDimitry Andric return; 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric Register MoreReg = MRI.createGenericVirtualRegister(MoreTy); 11480b57cec5SDimitry Andric Register ImpDef = MIRBuilder.buildUndef(MoreTy).getReg(0); 11490b57cec5SDimitry Andric MIRBuilder.buildInsert(MoreReg, ImpDef, MO.getReg(), 0); 11500b57cec5SDimitry Andric MO.setReg(MoreReg); 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 11540b57cec5SDimitry Andric LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx, 11550b57cec5SDimitry Andric LLT WideTy) { 11560b57cec5SDimitry Andric if (TypeIdx != 1) 11570b57cec5SDimitry Andric return UnableToLegalize; 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 11600b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 11610b57cec5SDimitry Andric if (DstTy.isVector()) 11620b57cec5SDimitry Andric return UnableToLegalize; 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 11650b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src1); 11660b57cec5SDimitry Andric const int DstSize = DstTy.getSizeInBits(); 11670b57cec5SDimitry Andric const int SrcSize = SrcTy.getSizeInBits(); 11680b57cec5SDimitry Andric const int WideSize = WideTy.getSizeInBits(); 11690b57cec5SDimitry Andric const int NumMerge = (DstSize + WideSize - 1) / WideSize; 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric unsigned NumOps = MI.getNumOperands(); 11720b57cec5SDimitry Andric unsigned NumSrc = MI.getNumOperands() - 1; 11730b57cec5SDimitry Andric unsigned PartSize = DstTy.getSizeInBits() / NumSrc; 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric if (WideSize >= DstSize) { 11760b57cec5SDimitry Andric // Directly pack the bits in the target type. 11770b57cec5SDimitry Andric Register ResultReg = MIRBuilder.buildZExt(WideTy, Src1).getReg(0); 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 11800b57cec5SDimitry Andric const unsigned Offset = (I - 1) * PartSize; 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 11830b57cec5SDimitry Andric assert(MRI.getType(SrcReg) == LLT::scalar(PartSize)); 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg); 11860b57cec5SDimitry Andric 1187*8bcb0991SDimitry Andric Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg : 11880b57cec5SDimitry Andric MRI.createGenericVirtualRegister(WideTy); 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset); 11910b57cec5SDimitry Andric auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt); 11920b57cec5SDimitry Andric MIRBuilder.buildOr(NextResult, ResultReg, Shl); 11930b57cec5SDimitry Andric ResultReg = NextResult; 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric if (WideSize > DstSize) 11970b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ResultReg); 1198*8bcb0991SDimitry Andric else if (DstTy.isPointer()) 1199*8bcb0991SDimitry Andric MIRBuilder.buildIntToPtr(DstReg, ResultReg); 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric MI.eraseFromParent(); 12020b57cec5SDimitry Andric return Legalized; 12030b57cec5SDimitry Andric } 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric // Unmerge the original values to the GCD type, and recombine to the next 12060b57cec5SDimitry Andric // multiple greater than the original type. 12070b57cec5SDimitry Andric // 12080b57cec5SDimitry Andric // %3:_(s12) = G_MERGE_VALUES %0:_(s4), %1:_(s4), %2:_(s4) -> s6 12090b57cec5SDimitry Andric // %4:_(s2), %5:_(s2) = G_UNMERGE_VALUES %0 12100b57cec5SDimitry Andric // %6:_(s2), %7:_(s2) = G_UNMERGE_VALUES %1 12110b57cec5SDimitry Andric // %8:_(s2), %9:_(s2) = G_UNMERGE_VALUES %2 12120b57cec5SDimitry Andric // %10:_(s6) = G_MERGE_VALUES %4, %5, %6 12130b57cec5SDimitry Andric // %11:_(s6) = G_MERGE_VALUES %7, %8, %9 12140b57cec5SDimitry Andric // %12:_(s12) = G_MERGE_VALUES %10, %11 12150b57cec5SDimitry Andric // 12160b57cec5SDimitry Andric // Padding with undef if necessary: 12170b57cec5SDimitry Andric // 12180b57cec5SDimitry Andric // %2:_(s8) = G_MERGE_VALUES %0:_(s4), %1:_(s4) -> s6 12190b57cec5SDimitry Andric // %3:_(s2), %4:_(s2) = G_UNMERGE_VALUES %0 12200b57cec5SDimitry Andric // %5:_(s2), %6:_(s2) = G_UNMERGE_VALUES %1 12210b57cec5SDimitry Andric // %7:_(s2) = G_IMPLICIT_DEF 12220b57cec5SDimitry Andric // %8:_(s6) = G_MERGE_VALUES %3, %4, %5 12230b57cec5SDimitry Andric // %9:_(s6) = G_MERGE_VALUES %6, %7, %7 12240b57cec5SDimitry Andric // %10:_(s12) = G_MERGE_VALUES %8, %9 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric const int GCD = greatestCommonDivisor(SrcSize, WideSize); 12270b57cec5SDimitry Andric LLT GCDTy = LLT::scalar(GCD); 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric SmallVector<Register, 8> Parts; 12300b57cec5SDimitry Andric SmallVector<Register, 8> NewMergeRegs; 12310b57cec5SDimitry Andric SmallVector<Register, 8> Unmerges; 12320b57cec5SDimitry Andric LLT WideDstTy = LLT::scalar(NumMerge * WideSize); 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric // Decompose the original operands if they don't evenly divide. 12350b57cec5SDimitry Andric for (int I = 1, E = MI.getNumOperands(); I != E; ++I) { 12360b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 12370b57cec5SDimitry Andric if (GCD == SrcSize) { 12380b57cec5SDimitry Andric Unmerges.push_back(SrcReg); 12390b57cec5SDimitry Andric } else { 12400b57cec5SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 12410b57cec5SDimitry Andric for (int J = 0, JE = Unmerge->getNumOperands() - 1; J != JE; ++J) 12420b57cec5SDimitry Andric Unmerges.push_back(Unmerge.getReg(J)); 12430b57cec5SDimitry Andric } 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric // Pad with undef to the next size that is a multiple of the requested size. 12470b57cec5SDimitry Andric if (static_cast<int>(Unmerges.size()) != NumMerge * WideSize) { 12480b57cec5SDimitry Andric Register UndefReg = MIRBuilder.buildUndef(GCDTy).getReg(0); 12490b57cec5SDimitry Andric for (int I = Unmerges.size(); I != NumMerge * WideSize; ++I) 12500b57cec5SDimitry Andric Unmerges.push_back(UndefReg); 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric const int PartsPerGCD = WideSize / GCD; 12540b57cec5SDimitry Andric 12550b57cec5SDimitry Andric // Build merges of each piece. 12560b57cec5SDimitry Andric ArrayRef<Register> Slicer(Unmerges); 12570b57cec5SDimitry Andric for (int I = 0; I != NumMerge; ++I, Slicer = Slicer.drop_front(PartsPerGCD)) { 12580b57cec5SDimitry Andric auto Merge = MIRBuilder.buildMerge(WideTy, Slicer.take_front(PartsPerGCD)); 12590b57cec5SDimitry Andric NewMergeRegs.push_back(Merge.getReg(0)); 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric // A truncate may be necessary if the requested type doesn't evenly divide the 12630b57cec5SDimitry Andric // original result type. 12640b57cec5SDimitry Andric if (DstTy.getSizeInBits() == WideDstTy.getSizeInBits()) { 12650b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, NewMergeRegs); 12660b57cec5SDimitry Andric } else { 12670b57cec5SDimitry Andric auto FinalMerge = MIRBuilder.buildMerge(WideDstTy, NewMergeRegs); 12680b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, FinalMerge.getReg(0)); 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric MI.eraseFromParent(); 12720b57cec5SDimitry Andric return Legalized; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 12760b57cec5SDimitry Andric LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx, 12770b57cec5SDimitry Andric LLT WideTy) { 12780b57cec5SDimitry Andric if (TypeIdx != 0) 12790b57cec5SDimitry Andric return UnableToLegalize; 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric unsigned NumDst = MI.getNumOperands() - 1; 12820b57cec5SDimitry Andric Register SrcReg = MI.getOperand(NumDst).getReg(); 12830b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 12840b57cec5SDimitry Andric if (!SrcTy.isScalar()) 12850b57cec5SDimitry Andric return UnableToLegalize; 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 12880b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 12890b57cec5SDimitry Andric if (!DstTy.isScalar()) 12900b57cec5SDimitry Andric return UnableToLegalize; 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric unsigned NewSrcSize = NumDst * WideTy.getSizeInBits(); 12930b57cec5SDimitry Andric LLT NewSrcTy = LLT::scalar(NewSrcSize); 12940b57cec5SDimitry Andric unsigned SizeDiff = WideTy.getSizeInBits() - DstTy.getSizeInBits(); 12950b57cec5SDimitry Andric 12960b57cec5SDimitry Andric auto WideSrc = MIRBuilder.buildZExt(NewSrcTy, SrcReg); 12970b57cec5SDimitry Andric 12980b57cec5SDimitry Andric for (unsigned I = 1; I != NumDst; ++I) { 12990b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(NewSrcTy, SizeDiff * I); 13000b57cec5SDimitry Andric auto Shl = MIRBuilder.buildShl(NewSrcTy, WideSrc, ShiftAmt); 13010b57cec5SDimitry Andric WideSrc = MIRBuilder.buildOr(NewSrcTy, WideSrc, Shl); 13020b57cec5SDimitry Andric } 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric Observer.changingInstr(MI); 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric MI.getOperand(NumDst).setReg(WideSrc->getOperand(0).getReg()); 13070b57cec5SDimitry Andric for (unsigned I = 0; I != NumDst; ++I) 13080b57cec5SDimitry Andric widenScalarDst(MI, WideTy, I); 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric Observer.changedInstr(MI); 13110b57cec5SDimitry Andric 13120b57cec5SDimitry Andric return Legalized; 13130b57cec5SDimitry Andric } 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 13160b57cec5SDimitry Andric LegalizerHelper::widenScalarExtract(MachineInstr &MI, unsigned TypeIdx, 13170b57cec5SDimitry Andric LLT WideTy) { 13180b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 13190b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 13200b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 13230b57cec5SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 13240b57cec5SDimitry Andric 13250b57cec5SDimitry Andric if (TypeIdx == 0) { 13260b57cec5SDimitry Andric if (SrcTy.isVector() || DstTy.isVector()) 13270b57cec5SDimitry Andric return UnableToLegalize; 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric SrcOp Src(SrcReg); 13300b57cec5SDimitry Andric if (SrcTy.isPointer()) { 13310b57cec5SDimitry Andric // Extracts from pointers can be handled only if they are really just 13320b57cec5SDimitry Andric // simple integers. 13330b57cec5SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 13340b57cec5SDimitry Andric if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) 13350b57cec5SDimitry Andric return UnableToLegalize; 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric LLT SrcAsIntTy = LLT::scalar(SrcTy.getSizeInBits()); 13380b57cec5SDimitry Andric Src = MIRBuilder.buildPtrToInt(SrcAsIntTy, Src); 13390b57cec5SDimitry Andric SrcTy = SrcAsIntTy; 13400b57cec5SDimitry Andric } 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric if (DstTy.isPointer()) 13430b57cec5SDimitry Andric return UnableToLegalize; 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andric if (Offset == 0) { 13460b57cec5SDimitry Andric // Avoid a shift in the degenerate case. 13470b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, 13480b57cec5SDimitry Andric MIRBuilder.buildAnyExtOrTrunc(WideTy, Src)); 13490b57cec5SDimitry Andric MI.eraseFromParent(); 13500b57cec5SDimitry Andric return Legalized; 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric // Do a shift in the source type. 13540b57cec5SDimitry Andric LLT ShiftTy = SrcTy; 13550b57cec5SDimitry Andric if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) { 13560b57cec5SDimitry Andric Src = MIRBuilder.buildAnyExt(WideTy, Src); 13570b57cec5SDimitry Andric ShiftTy = WideTy; 13580b57cec5SDimitry Andric } else if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) 13590b57cec5SDimitry Andric return UnableToLegalize; 13600b57cec5SDimitry Andric 13610b57cec5SDimitry Andric auto LShr = MIRBuilder.buildLShr( 13620b57cec5SDimitry Andric ShiftTy, Src, MIRBuilder.buildConstant(ShiftTy, Offset)); 13630b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, LShr); 13640b57cec5SDimitry Andric MI.eraseFromParent(); 13650b57cec5SDimitry Andric return Legalized; 13660b57cec5SDimitry Andric } 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric if (SrcTy.isScalar()) { 13690b57cec5SDimitry Andric Observer.changingInstr(MI); 13700b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 13710b57cec5SDimitry Andric Observer.changedInstr(MI); 13720b57cec5SDimitry Andric return Legalized; 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric if (!SrcTy.isVector()) 13760b57cec5SDimitry Andric return UnableToLegalize; 13770b57cec5SDimitry Andric 13780b57cec5SDimitry Andric if (DstTy != SrcTy.getElementType()) 13790b57cec5SDimitry Andric return UnableToLegalize; 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric if (Offset % SrcTy.getScalarSizeInBits() != 0) 13820b57cec5SDimitry Andric return UnableToLegalize; 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric Observer.changingInstr(MI); 13850b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 13860b57cec5SDimitry Andric 13870b57cec5SDimitry Andric MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) * 13880b57cec5SDimitry Andric Offset); 13890b57cec5SDimitry Andric widenScalarDst(MI, WideTy.getScalarType(), 0); 13900b57cec5SDimitry Andric Observer.changedInstr(MI); 13910b57cec5SDimitry Andric return Legalized; 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 13950b57cec5SDimitry Andric LegalizerHelper::widenScalarInsert(MachineInstr &MI, unsigned TypeIdx, 13960b57cec5SDimitry Andric LLT WideTy) { 13970b57cec5SDimitry Andric if (TypeIdx != 0) 13980b57cec5SDimitry Andric return UnableToLegalize; 13990b57cec5SDimitry Andric Observer.changingInstr(MI); 14000b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 14010b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 14020b57cec5SDimitry Andric Observer.changedInstr(MI); 14030b57cec5SDimitry Andric return Legalized; 14040b57cec5SDimitry Andric } 14050b57cec5SDimitry Andric 14060b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 14070b57cec5SDimitry Andric LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) { 14080b57cec5SDimitry Andric MIRBuilder.setInstr(MI); 14090b57cec5SDimitry Andric 14100b57cec5SDimitry Andric switch (MI.getOpcode()) { 14110b57cec5SDimitry Andric default: 14120b57cec5SDimitry Andric return UnableToLegalize; 14130b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 14140b57cec5SDimitry Andric return widenScalarExtract(MI, TypeIdx, WideTy); 14150b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 14160b57cec5SDimitry Andric return widenScalarInsert(MI, TypeIdx, WideTy); 14170b57cec5SDimitry Andric case TargetOpcode::G_MERGE_VALUES: 14180b57cec5SDimitry Andric return widenScalarMergeValues(MI, TypeIdx, WideTy); 14190b57cec5SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: 14200b57cec5SDimitry Andric return widenScalarUnmergeValues(MI, TypeIdx, WideTy); 14210b57cec5SDimitry Andric case TargetOpcode::G_UADDO: 14220b57cec5SDimitry Andric case TargetOpcode::G_USUBO: { 14230b57cec5SDimitry Andric if (TypeIdx == 1) 14240b57cec5SDimitry Andric return UnableToLegalize; // TODO 14250b57cec5SDimitry Andric auto LHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy}, 14260b57cec5SDimitry Andric {MI.getOperand(2).getReg()}); 14270b57cec5SDimitry Andric auto RHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy}, 14280b57cec5SDimitry Andric {MI.getOperand(3).getReg()}); 14290b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode() == TargetOpcode::G_UADDO 14300b57cec5SDimitry Andric ? TargetOpcode::G_ADD 14310b57cec5SDimitry Andric : TargetOpcode::G_SUB; 14320b57cec5SDimitry Andric // Do the arithmetic in the larger type. 14330b57cec5SDimitry Andric auto NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSZext, RHSZext}); 14340b57cec5SDimitry Andric LLT OrigTy = MRI.getType(MI.getOperand(0).getReg()); 14350b57cec5SDimitry Andric APInt Mask = APInt::getAllOnesValue(OrigTy.getSizeInBits()); 14360b57cec5SDimitry Andric auto AndOp = MIRBuilder.buildInstr( 14370b57cec5SDimitry Andric TargetOpcode::G_AND, {WideTy}, 14380b57cec5SDimitry Andric {NewOp, MIRBuilder.buildConstant(WideTy, Mask.getZExtValue())}); 14390b57cec5SDimitry Andric // There is no overflow if the AndOp is the same as NewOp. 14400b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1).getReg(), NewOp, 14410b57cec5SDimitry Andric AndOp); 14420b57cec5SDimitry Andric // Now trunc the NewOp to the original result. 14430b57cec5SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(0).getReg(), NewOp); 14440b57cec5SDimitry Andric MI.eraseFromParent(); 14450b57cec5SDimitry Andric return Legalized; 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 14480b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 14490b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 14500b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 14510b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: { 14520b57cec5SDimitry Andric if (TypeIdx == 0) { 14530b57cec5SDimitry Andric Observer.changingInstr(MI); 14540b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 14550b57cec5SDimitry Andric Observer.changedInstr(MI); 14560b57cec5SDimitry Andric return Legalized; 14570b57cec5SDimitry Andric } 14580b57cec5SDimitry Andric 14590b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 14600b57cec5SDimitry Andric 14610b57cec5SDimitry Andric // First ZEXT the input. 14620b57cec5SDimitry Andric auto MIBSrc = MIRBuilder.buildZExt(WideTy, SrcReg); 14630b57cec5SDimitry Andric LLT CurTy = MRI.getType(SrcReg); 14640b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_CTTZ) { 14650b57cec5SDimitry Andric // The count is the same in the larger type except if the original 14660b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 14670b57cec5SDimitry Andric // the top of the original type. 14680b57cec5SDimitry Andric auto TopBit = 14690b57cec5SDimitry Andric APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits()); 14700b57cec5SDimitry Andric MIBSrc = MIRBuilder.buildOr( 14710b57cec5SDimitry Andric WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit)); 14720b57cec5SDimitry Andric } 14730b57cec5SDimitry Andric 14740b57cec5SDimitry Andric // Perform the operation at the larger size. 14750b57cec5SDimitry Andric auto MIBNewOp = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, {MIBSrc}); 14760b57cec5SDimitry Andric // This is already the correct result for CTPOP and CTTZs 14770b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_CTLZ || 14780b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) { 14790b57cec5SDimitry Andric // The correct result is NewOp - (Difference in widety and current ty). 14800b57cec5SDimitry Andric unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits(); 14810b57cec5SDimitry Andric MIBNewOp = MIRBuilder.buildInstr( 14820b57cec5SDimitry Andric TargetOpcode::G_SUB, {WideTy}, 14830b57cec5SDimitry Andric {MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff)}); 14840b57cec5SDimitry Andric } 14850b57cec5SDimitry Andric 14860b57cec5SDimitry Andric MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp); 14870b57cec5SDimitry Andric MI.eraseFromParent(); 14880b57cec5SDimitry Andric return Legalized; 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric case TargetOpcode::G_BSWAP: { 14910b57cec5SDimitry Andric Observer.changingInstr(MI); 14920b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric Register ShrReg = MRI.createGenericVirtualRegister(WideTy); 14950b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 14960b57cec5SDimitry Andric Register ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy); 14970b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 14980b57cec5SDimitry Andric 14990b57cec5SDimitry Andric MI.getOperand(0).setReg(DstExt); 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 15020b57cec5SDimitry Andric 15030b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 15040b57cec5SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 15050b57cec5SDimitry Andric MIRBuilder.buildConstant(ShiftAmtReg, DiffBits); 15060b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_LSHR) 15070b57cec5SDimitry Andric .addDef(ShrReg) 15080b57cec5SDimitry Andric .addUse(DstExt) 15090b57cec5SDimitry Andric .addUse(ShiftAmtReg); 15100b57cec5SDimitry Andric 15110b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ShrReg); 15120b57cec5SDimitry Andric Observer.changedInstr(MI); 15130b57cec5SDimitry Andric return Legalized; 15140b57cec5SDimitry Andric } 1515*8bcb0991SDimitry Andric case TargetOpcode::G_BITREVERSE: { 1516*8bcb0991SDimitry Andric Observer.changingInstr(MI); 1517*8bcb0991SDimitry Andric 1518*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 1519*8bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 1520*8bcb0991SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 1521*8bcb0991SDimitry Andric 1522*8bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 1523*8bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 1524*8bcb0991SDimitry Andric MI.getOperand(0).setReg(DstExt); 1525*8bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 1526*8bcb0991SDimitry Andric 1527*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, DiffBits); 1528*8bcb0991SDimitry Andric auto Shift = MIRBuilder.buildLShr(WideTy, DstExt, ShiftAmt); 1529*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(DstReg, Shift); 1530*8bcb0991SDimitry Andric Observer.changedInstr(MI); 1531*8bcb0991SDimitry Andric return Legalized; 1532*8bcb0991SDimitry Andric } 15330b57cec5SDimitry Andric case TargetOpcode::G_ADD: 15340b57cec5SDimitry Andric case TargetOpcode::G_AND: 15350b57cec5SDimitry Andric case TargetOpcode::G_MUL: 15360b57cec5SDimitry Andric case TargetOpcode::G_OR: 15370b57cec5SDimitry Andric case TargetOpcode::G_XOR: 15380b57cec5SDimitry Andric case TargetOpcode::G_SUB: 15390b57cec5SDimitry Andric // Perform operation at larger width (any extension is fines here, high bits 15400b57cec5SDimitry Andric // don't affect the result) and then truncate the result back to the 15410b57cec5SDimitry Andric // original type. 15420b57cec5SDimitry Andric Observer.changingInstr(MI); 15430b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 15440b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 15450b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 15460b57cec5SDimitry Andric Observer.changedInstr(MI); 15470b57cec5SDimitry Andric return Legalized; 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andric case TargetOpcode::G_SHL: 15500b57cec5SDimitry Andric Observer.changingInstr(MI); 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric if (TypeIdx == 0) { 15530b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 15540b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 15550b57cec5SDimitry Andric } else { 15560b57cec5SDimitry Andric assert(TypeIdx == 1); 15570b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 15580b57cec5SDimitry Andric // unsigned integer: 15590b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 15600b57cec5SDimitry Andric } 15610b57cec5SDimitry Andric 15620b57cec5SDimitry Andric Observer.changedInstr(MI); 15630b57cec5SDimitry Andric return Legalized; 15640b57cec5SDimitry Andric 15650b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 15660b57cec5SDimitry Andric case TargetOpcode::G_SREM: 15670b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 15680b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 15690b57cec5SDimitry Andric Observer.changingInstr(MI); 15700b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 15710b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 15720b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 15730b57cec5SDimitry Andric Observer.changedInstr(MI); 15740b57cec5SDimitry Andric return Legalized; 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 15770b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 15780b57cec5SDimitry Andric Observer.changingInstr(MI); 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andric if (TypeIdx == 0) { 15810b57cec5SDimitry Andric unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ? 15820b57cec5SDimitry Andric TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, CvtOp); 15850b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 15860b57cec5SDimitry Andric } else { 15870b57cec5SDimitry Andric assert(TypeIdx == 1); 15880b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 15890b57cec5SDimitry Andric // unsigned integer: 15900b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 15910b57cec5SDimitry Andric } 15920b57cec5SDimitry Andric 15930b57cec5SDimitry Andric Observer.changedInstr(MI); 15940b57cec5SDimitry Andric return Legalized; 15950b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 15960b57cec5SDimitry Andric case TargetOpcode::G_UREM: 15970b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 15980b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 15990b57cec5SDimitry Andric Observer.changingInstr(MI); 16000b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 16010b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 16020b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 16030b57cec5SDimitry Andric Observer.changedInstr(MI); 16040b57cec5SDimitry Andric return Legalized; 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 16070b57cec5SDimitry Andric Observer.changingInstr(MI); 16080b57cec5SDimitry Andric if (TypeIdx == 0) { 16090b57cec5SDimitry Andric // Perform operation at larger width (any extension is fine here, high 16100b57cec5SDimitry Andric // bits don't affect the result) and then truncate the result back to the 16110b57cec5SDimitry Andric // original type. 16120b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 16130b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 16140b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 16150b57cec5SDimitry Andric } else { 16160b57cec5SDimitry Andric bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector(); 16170b57cec5SDimitry Andric // Explicit extension is required here since high bits affect the result. 16180b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false)); 16190b57cec5SDimitry Andric } 16200b57cec5SDimitry Andric Observer.changedInstr(MI); 16210b57cec5SDimitry Andric return Legalized; 16220b57cec5SDimitry Andric 16230b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 16240b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: 16250b57cec5SDimitry Andric Observer.changingInstr(MI); 1626*8bcb0991SDimitry Andric 1627*8bcb0991SDimitry Andric if (TypeIdx == 0) 16280b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 1629*8bcb0991SDimitry Andric else 1630*8bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT); 1631*8bcb0991SDimitry Andric 16320b57cec5SDimitry Andric Observer.changedInstr(MI); 16330b57cec5SDimitry Andric return Legalized; 16340b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 16350b57cec5SDimitry Andric if (TypeIdx != 1) 16360b57cec5SDimitry Andric return UnableToLegalize; 16370b57cec5SDimitry Andric Observer.changingInstr(MI); 16380b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 16390b57cec5SDimitry Andric Observer.changedInstr(MI); 16400b57cec5SDimitry Andric return Legalized; 16410b57cec5SDimitry Andric 16420b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: 16430b57cec5SDimitry Andric if (TypeIdx != 1) 16440b57cec5SDimitry Andric return UnableToLegalize; 16450b57cec5SDimitry Andric Observer.changingInstr(MI); 16460b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 16470b57cec5SDimitry Andric Observer.changedInstr(MI); 16480b57cec5SDimitry Andric return Legalized; 16490b57cec5SDimitry Andric 16500b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 16510b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: 16520b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 16530b57cec5SDimitry Andric Observer.changingInstr(MI); 16540b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 16550b57cec5SDimitry Andric Observer.changedInstr(MI); 16560b57cec5SDimitry Andric return Legalized; 16570b57cec5SDimitry Andric 16580b57cec5SDimitry Andric case TargetOpcode::G_STORE: { 16590b57cec5SDimitry Andric if (TypeIdx != 0) 16600b57cec5SDimitry Andric return UnableToLegalize; 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 16630b57cec5SDimitry Andric if (!isPowerOf2_32(Ty.getSizeInBits())) 16640b57cec5SDimitry Andric return UnableToLegalize; 16650b57cec5SDimitry Andric 16660b57cec5SDimitry Andric Observer.changingInstr(MI); 16670b57cec5SDimitry Andric 16680b57cec5SDimitry Andric unsigned ExtType = Ty.getScalarSizeInBits() == 1 ? 16690b57cec5SDimitry Andric TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT; 16700b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, ExtType); 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric Observer.changedInstr(MI); 16730b57cec5SDimitry Andric return Legalized; 16740b57cec5SDimitry Andric } 16750b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: { 16760b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 16770b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 16780b57cec5SDimitry Andric const APInt &Val = SrcMO.getCImm()->getValue().sext(WideTy.getSizeInBits()); 16790b57cec5SDimitry Andric Observer.changingInstr(MI); 16800b57cec5SDimitry Andric SrcMO.setCImm(ConstantInt::get(Ctx, Val)); 16810b57cec5SDimitry Andric 16820b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 16830b57cec5SDimitry Andric Observer.changedInstr(MI); 16840b57cec5SDimitry Andric return Legalized; 16850b57cec5SDimitry Andric } 16860b57cec5SDimitry Andric case TargetOpcode::G_FCONSTANT: { 16870b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 16880b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 16890b57cec5SDimitry Andric APFloat Val = SrcMO.getFPImm()->getValueAPF(); 16900b57cec5SDimitry Andric bool LosesInfo; 16910b57cec5SDimitry Andric switch (WideTy.getSizeInBits()) { 16920b57cec5SDimitry Andric case 32: 16930b57cec5SDimitry Andric Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 16940b57cec5SDimitry Andric &LosesInfo); 16950b57cec5SDimitry Andric break; 16960b57cec5SDimitry Andric case 64: 16970b57cec5SDimitry Andric Val.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, 16980b57cec5SDimitry Andric &LosesInfo); 16990b57cec5SDimitry Andric break; 17000b57cec5SDimitry Andric default: 17010b57cec5SDimitry Andric return UnableToLegalize; 17020b57cec5SDimitry Andric } 17030b57cec5SDimitry Andric 17040b57cec5SDimitry Andric assert(!LosesInfo && "extend should always be lossless"); 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric Observer.changingInstr(MI); 17070b57cec5SDimitry Andric SrcMO.setFPImm(ConstantFP::get(Ctx, Val)); 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 17100b57cec5SDimitry Andric Observer.changedInstr(MI); 17110b57cec5SDimitry Andric return Legalized; 17120b57cec5SDimitry Andric } 17130b57cec5SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: { 17140b57cec5SDimitry Andric Observer.changingInstr(MI); 17150b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 17160b57cec5SDimitry Andric Observer.changedInstr(MI); 17170b57cec5SDimitry Andric return Legalized; 17180b57cec5SDimitry Andric } 17190b57cec5SDimitry Andric case TargetOpcode::G_BRCOND: 17200b57cec5SDimitry Andric Observer.changingInstr(MI); 17210b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, MIRBuilder.getBoolExtOp(false, false)); 17220b57cec5SDimitry Andric Observer.changedInstr(MI); 17230b57cec5SDimitry Andric return Legalized; 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric case TargetOpcode::G_FCMP: 17260b57cec5SDimitry Andric Observer.changingInstr(MI); 17270b57cec5SDimitry Andric if (TypeIdx == 0) 17280b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 17290b57cec5SDimitry Andric else { 17300b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT); 17310b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT); 17320b57cec5SDimitry Andric } 17330b57cec5SDimitry Andric Observer.changedInstr(MI); 17340b57cec5SDimitry Andric return Legalized; 17350b57cec5SDimitry Andric 17360b57cec5SDimitry Andric case TargetOpcode::G_ICMP: 17370b57cec5SDimitry Andric Observer.changingInstr(MI); 17380b57cec5SDimitry Andric if (TypeIdx == 0) 17390b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 17400b57cec5SDimitry Andric else { 17410b57cec5SDimitry Andric unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>( 17420b57cec5SDimitry Andric MI.getOperand(1).getPredicate())) 17430b57cec5SDimitry Andric ? TargetOpcode::G_SEXT 17440b57cec5SDimitry Andric : TargetOpcode::G_ZEXT; 17450b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, ExtOpcode); 17460b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, ExtOpcode); 17470b57cec5SDimitry Andric } 17480b57cec5SDimitry Andric Observer.changedInstr(MI); 17490b57cec5SDimitry Andric return Legalized; 17500b57cec5SDimitry Andric 17510b57cec5SDimitry Andric case TargetOpcode::G_GEP: 17520b57cec5SDimitry Andric assert(TypeIdx == 1 && "unable to legalize pointer of GEP"); 17530b57cec5SDimitry Andric Observer.changingInstr(MI); 17540b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 17550b57cec5SDimitry Andric Observer.changedInstr(MI); 17560b57cec5SDimitry Andric return Legalized; 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 17590b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric Observer.changingInstr(MI); 17620b57cec5SDimitry Andric for (unsigned I = 1; I < MI.getNumOperands(); I += 2) { 17630b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 17640b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 17650b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT); 17660b57cec5SDimitry Andric } 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 17690b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 17700b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 17710b57cec5SDimitry Andric Observer.changedInstr(MI); 17720b57cec5SDimitry Andric return Legalized; 17730b57cec5SDimitry Andric } 17740b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: { 17750b57cec5SDimitry Andric if (TypeIdx == 0) { 17760b57cec5SDimitry Andric Register VecReg = MI.getOperand(1).getReg(); 17770b57cec5SDimitry Andric LLT VecTy = MRI.getType(VecReg); 17780b57cec5SDimitry Andric Observer.changingInstr(MI); 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric widenScalarSrc(MI, LLT::vector(VecTy.getNumElements(), 17810b57cec5SDimitry Andric WideTy.getSizeInBits()), 17820b57cec5SDimitry Andric 1, TargetOpcode::G_SEXT); 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 17850b57cec5SDimitry Andric Observer.changedInstr(MI); 17860b57cec5SDimitry Andric return Legalized; 17870b57cec5SDimitry Andric } 17880b57cec5SDimitry Andric 17890b57cec5SDimitry Andric if (TypeIdx != 2) 17900b57cec5SDimitry Andric return UnableToLegalize; 17910b57cec5SDimitry Andric Observer.changingInstr(MI); 17920b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 17930b57cec5SDimitry Andric Observer.changedInstr(MI); 17940b57cec5SDimitry Andric return Legalized; 17950b57cec5SDimitry Andric } 17960b57cec5SDimitry Andric case TargetOpcode::G_FADD: 17970b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 17980b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 17990b57cec5SDimitry Andric case TargetOpcode::G_FMA: 1800*8bcb0991SDimitry Andric case TargetOpcode::G_FMAD: 18010b57cec5SDimitry Andric case TargetOpcode::G_FNEG: 18020b57cec5SDimitry Andric case TargetOpcode::G_FABS: 18030b57cec5SDimitry Andric case TargetOpcode::G_FCANONICALIZE: 18040b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM: 18050b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM: 18060b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM_IEEE: 18070b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM_IEEE: 18080b57cec5SDimitry Andric case TargetOpcode::G_FMINIMUM: 18090b57cec5SDimitry Andric case TargetOpcode::G_FMAXIMUM: 18100b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 18110b57cec5SDimitry Andric case TargetOpcode::G_FREM: 18120b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 18130b57cec5SDimitry Andric case TargetOpcode::G_FFLOOR: 18140b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 18150b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 18160b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 18170b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 18180b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 18190b57cec5SDimitry Andric case TargetOpcode::G_FRINT: 18200b57cec5SDimitry Andric case TargetOpcode::G_FNEARBYINT: 18210b57cec5SDimitry Andric case TargetOpcode::G_FSQRT: 18220b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 18230b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 18240b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 18250b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_TRUNC: 18260b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUND: 18270b57cec5SDimitry Andric assert(TypeIdx == 0); 18280b57cec5SDimitry Andric Observer.changingInstr(MI); 18290b57cec5SDimitry Andric 18300b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) 18310b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT); 18320b57cec5SDimitry Andric 18330b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 18340b57cec5SDimitry Andric Observer.changedInstr(MI); 18350b57cec5SDimitry Andric return Legalized; 18360b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 18370b57cec5SDimitry Andric if (TypeIdx != 1) 18380b57cec5SDimitry Andric return UnableToLegalize; 18390b57cec5SDimitry Andric 18400b57cec5SDimitry Andric Observer.changingInstr(MI); 18410b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 18420b57cec5SDimitry Andric Observer.changedInstr(MI); 18430b57cec5SDimitry Andric return Legalized; 18440b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 18450b57cec5SDimitry Andric if (TypeIdx != 0) 18460b57cec5SDimitry Andric return UnableToLegalize; 18470b57cec5SDimitry Andric 18480b57cec5SDimitry Andric Observer.changingInstr(MI); 18490b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 18500b57cec5SDimitry Andric Observer.changedInstr(MI); 18510b57cec5SDimitry Andric return Legalized; 18520b57cec5SDimitry Andric case TargetOpcode::G_BUILD_VECTOR: { 18530b57cec5SDimitry Andric Observer.changingInstr(MI); 18540b57cec5SDimitry Andric 18550b57cec5SDimitry Andric const LLT WideEltTy = TypeIdx == 1 ? WideTy : WideTy.getElementType(); 18560b57cec5SDimitry Andric for (int I = 1, E = MI.getNumOperands(); I != E; ++I) 18570b57cec5SDimitry Andric widenScalarSrc(MI, WideEltTy, I, TargetOpcode::G_ANYEXT); 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric // Avoid changing the result vector type if the source element type was 18600b57cec5SDimitry Andric // requested. 18610b57cec5SDimitry Andric if (TypeIdx == 1) { 18620b57cec5SDimitry Andric auto &TII = *MI.getMF()->getSubtarget().getInstrInfo(); 18630b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_BUILD_VECTOR_TRUNC)); 18640b57cec5SDimitry Andric } else { 18650b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 18660b57cec5SDimitry Andric } 18670b57cec5SDimitry Andric 18680b57cec5SDimitry Andric Observer.changedInstr(MI); 18690b57cec5SDimitry Andric return Legalized; 18700b57cec5SDimitry Andric } 1871*8bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: 1872*8bcb0991SDimitry Andric if (TypeIdx != 0) 1873*8bcb0991SDimitry Andric return UnableToLegalize; 1874*8bcb0991SDimitry Andric 1875*8bcb0991SDimitry Andric Observer.changingInstr(MI); 1876*8bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 1877*8bcb0991SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_TRUNC); 1878*8bcb0991SDimitry Andric Observer.changedInstr(MI); 1879*8bcb0991SDimitry Andric return Legalized; 18800b57cec5SDimitry Andric } 18810b57cec5SDimitry Andric } 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 18840b57cec5SDimitry Andric LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 18850b57cec5SDimitry Andric using namespace TargetOpcode; 18860b57cec5SDimitry Andric MIRBuilder.setInstr(MI); 18870b57cec5SDimitry Andric 18880b57cec5SDimitry Andric switch(MI.getOpcode()) { 18890b57cec5SDimitry Andric default: 18900b57cec5SDimitry Andric return UnableToLegalize; 18910b57cec5SDimitry Andric case TargetOpcode::G_SREM: 18920b57cec5SDimitry Andric case TargetOpcode::G_UREM: { 18930b57cec5SDimitry Andric Register QuotReg = MRI.createGenericVirtualRegister(Ty); 18940b57cec5SDimitry Andric MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV) 18950b57cec5SDimitry Andric .addDef(QuotReg) 18960b57cec5SDimitry Andric .addUse(MI.getOperand(1).getReg()) 18970b57cec5SDimitry Andric .addUse(MI.getOperand(2).getReg()); 18980b57cec5SDimitry Andric 18990b57cec5SDimitry Andric Register ProdReg = MRI.createGenericVirtualRegister(Ty); 19000b57cec5SDimitry Andric MIRBuilder.buildMul(ProdReg, QuotReg, MI.getOperand(2).getReg()); 19010b57cec5SDimitry Andric MIRBuilder.buildSub(MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), 19020b57cec5SDimitry Andric ProdReg); 19030b57cec5SDimitry Andric MI.eraseFromParent(); 19040b57cec5SDimitry Andric return Legalized; 19050b57cec5SDimitry Andric } 1906*8bcb0991SDimitry Andric case TargetOpcode::G_SADDO: 1907*8bcb0991SDimitry Andric case TargetOpcode::G_SSUBO: 1908*8bcb0991SDimitry Andric return lowerSADDO_SSUBO(MI); 19090b57cec5SDimitry Andric case TargetOpcode::G_SMULO: 19100b57cec5SDimitry Andric case TargetOpcode::G_UMULO: { 19110b57cec5SDimitry Andric // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the 19120b57cec5SDimitry Andric // result. 19130b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 19140b57cec5SDimitry Andric Register Overflow = MI.getOperand(1).getReg(); 19150b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 19160b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 19170b57cec5SDimitry Andric 19180b57cec5SDimitry Andric MIRBuilder.buildMul(Res, LHS, RHS); 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO 19210b57cec5SDimitry Andric ? TargetOpcode::G_SMULH 19220b57cec5SDimitry Andric : TargetOpcode::G_UMULH; 19230b57cec5SDimitry Andric 19240b57cec5SDimitry Andric Register HiPart = MRI.createGenericVirtualRegister(Ty); 19250b57cec5SDimitry Andric MIRBuilder.buildInstr(Opcode) 19260b57cec5SDimitry Andric .addDef(HiPart) 19270b57cec5SDimitry Andric .addUse(LHS) 19280b57cec5SDimitry Andric .addUse(RHS); 19290b57cec5SDimitry Andric 19300b57cec5SDimitry Andric Register Zero = MRI.createGenericVirtualRegister(Ty); 19310b57cec5SDimitry Andric MIRBuilder.buildConstant(Zero, 0); 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric // For *signed* multiply, overflow is detected by checking: 19340b57cec5SDimitry Andric // (hi != (lo >> bitwidth-1)) 19350b57cec5SDimitry Andric if (Opcode == TargetOpcode::G_SMULH) { 19360b57cec5SDimitry Andric Register Shifted = MRI.createGenericVirtualRegister(Ty); 19370b57cec5SDimitry Andric Register ShiftAmt = MRI.createGenericVirtualRegister(Ty); 19380b57cec5SDimitry Andric MIRBuilder.buildConstant(ShiftAmt, Ty.getSizeInBits() - 1); 19390b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_ASHR) 19400b57cec5SDimitry Andric .addDef(Shifted) 19410b57cec5SDimitry Andric .addUse(Res) 19420b57cec5SDimitry Andric .addUse(ShiftAmt); 19430b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted); 19440b57cec5SDimitry Andric } else { 19450b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero); 19460b57cec5SDimitry Andric } 19470b57cec5SDimitry Andric MI.eraseFromParent(); 19480b57cec5SDimitry Andric return Legalized; 19490b57cec5SDimitry Andric } 19500b57cec5SDimitry Andric case TargetOpcode::G_FNEG: { 19510b57cec5SDimitry Andric // TODO: Handle vector types once we are able to 19520b57cec5SDimitry Andric // represent them. 19530b57cec5SDimitry Andric if (Ty.isVector()) 19540b57cec5SDimitry Andric return UnableToLegalize; 19550b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 19560b57cec5SDimitry Andric Type *ZeroTy; 19570b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 19580b57cec5SDimitry Andric switch (Ty.getSizeInBits()) { 19590b57cec5SDimitry Andric case 16: 19600b57cec5SDimitry Andric ZeroTy = Type::getHalfTy(Ctx); 19610b57cec5SDimitry Andric break; 19620b57cec5SDimitry Andric case 32: 19630b57cec5SDimitry Andric ZeroTy = Type::getFloatTy(Ctx); 19640b57cec5SDimitry Andric break; 19650b57cec5SDimitry Andric case 64: 19660b57cec5SDimitry Andric ZeroTy = Type::getDoubleTy(Ctx); 19670b57cec5SDimitry Andric break; 19680b57cec5SDimitry Andric case 128: 19690b57cec5SDimitry Andric ZeroTy = Type::getFP128Ty(Ctx); 19700b57cec5SDimitry Andric break; 19710b57cec5SDimitry Andric default: 19720b57cec5SDimitry Andric llvm_unreachable("unexpected floating-point type"); 19730b57cec5SDimitry Andric } 19740b57cec5SDimitry Andric ConstantFP &ZeroForNegation = 19750b57cec5SDimitry Andric *cast<ConstantFP>(ConstantFP::getZeroValueForNegation(ZeroTy)); 19760b57cec5SDimitry Andric auto Zero = MIRBuilder.buildFConstant(Ty, ZeroForNegation); 19770b57cec5SDimitry Andric Register SubByReg = MI.getOperand(1).getReg(); 19780b57cec5SDimitry Andric Register ZeroReg = Zero->getOperand(0).getReg(); 19790b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_FSUB, {Res}, {ZeroReg, SubByReg}, 19800b57cec5SDimitry Andric MI.getFlags()); 19810b57cec5SDimitry Andric MI.eraseFromParent(); 19820b57cec5SDimitry Andric return Legalized; 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric case TargetOpcode::G_FSUB: { 19850b57cec5SDimitry Andric // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)). 19860b57cec5SDimitry Andric // First, check if G_FNEG is marked as Lower. If so, we may 19870b57cec5SDimitry Andric // end up with an infinite loop as G_FSUB is used to legalize G_FNEG. 19880b57cec5SDimitry Andric if (LI.getAction({G_FNEG, {Ty}}).Action == Lower) 19890b57cec5SDimitry Andric return UnableToLegalize; 19900b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 19910b57cec5SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 19920b57cec5SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 19930b57cec5SDimitry Andric Register Neg = MRI.createGenericVirtualRegister(Ty); 19940b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_FNEG).addDef(Neg).addUse(RHS); 19950b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_FADD, {Res}, {LHS, Neg}, MI.getFlags()); 19960b57cec5SDimitry Andric MI.eraseFromParent(); 19970b57cec5SDimitry Andric return Legalized; 19980b57cec5SDimitry Andric } 1999*8bcb0991SDimitry Andric case TargetOpcode::G_FMAD: 2000*8bcb0991SDimitry Andric return lowerFMad(MI); 20010b57cec5SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: { 20020b57cec5SDimitry Andric Register OldValRes = MI.getOperand(0).getReg(); 20030b57cec5SDimitry Andric Register SuccessRes = MI.getOperand(1).getReg(); 20040b57cec5SDimitry Andric Register Addr = MI.getOperand(2).getReg(); 20050b57cec5SDimitry Andric Register CmpVal = MI.getOperand(3).getReg(); 20060b57cec5SDimitry Andric Register NewVal = MI.getOperand(4).getReg(); 20070b57cec5SDimitry Andric MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal, 20080b57cec5SDimitry Andric **MI.memoperands_begin()); 20090b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal); 20100b57cec5SDimitry Andric MI.eraseFromParent(); 20110b57cec5SDimitry Andric return Legalized; 20120b57cec5SDimitry Andric } 20130b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 20140b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: 20150b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: { 20160b57cec5SDimitry Andric // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT 20170b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20180b57cec5SDimitry Andric Register PtrReg = MI.getOperand(1).getReg(); 20190b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 20200b57cec5SDimitry Andric auto &MMO = **MI.memoperands_begin(); 20210b57cec5SDimitry Andric 2022*8bcb0991SDimitry Andric if (DstTy.getSizeInBits() == MMO.getSizeInBits()) { 2023*8bcb0991SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_LOAD) { 2024*8bcb0991SDimitry Andric // This load needs splitting into power of 2 sized loads. 2025*8bcb0991SDimitry Andric if (DstTy.isVector()) 20260b57cec5SDimitry Andric return UnableToLegalize; 2027*8bcb0991SDimitry Andric if (isPowerOf2_32(DstTy.getSizeInBits())) 2028*8bcb0991SDimitry Andric return UnableToLegalize; // Don't know what we're being asked to do. 2029*8bcb0991SDimitry Andric 2030*8bcb0991SDimitry Andric // Our strategy here is to generate anyextending loads for the smaller 2031*8bcb0991SDimitry Andric // types up to next power-2 result type, and then combine the two larger 2032*8bcb0991SDimitry Andric // result values together, before truncating back down to the non-pow-2 2033*8bcb0991SDimitry Andric // type. 2034*8bcb0991SDimitry Andric // E.g. v1 = i24 load => 2035*8bcb0991SDimitry Andric // v2 = i32 load (2 byte) 2036*8bcb0991SDimitry Andric // v3 = i32 load (1 byte) 2037*8bcb0991SDimitry Andric // v4 = i32 shl v3, 16 2038*8bcb0991SDimitry Andric // v5 = i32 or v4, v2 2039*8bcb0991SDimitry Andric // v1 = i24 trunc v5 2040*8bcb0991SDimitry Andric // By doing this we generate the correct truncate which should get 2041*8bcb0991SDimitry Andric // combined away as an artifact with a matching extend. 2042*8bcb0991SDimitry Andric uint64_t LargeSplitSize = PowerOf2Floor(DstTy.getSizeInBits()); 2043*8bcb0991SDimitry Andric uint64_t SmallSplitSize = DstTy.getSizeInBits() - LargeSplitSize; 2044*8bcb0991SDimitry Andric 2045*8bcb0991SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 2046*8bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 2047*8bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 2048*8bcb0991SDimitry Andric MachineMemOperand *SmallMMO = MF.getMachineMemOperand( 2049*8bcb0991SDimitry Andric &MMO, LargeSplitSize / 8, SmallSplitSize / 8); 2050*8bcb0991SDimitry Andric 2051*8bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 2052*8bcb0991SDimitry Andric unsigned AnyExtSize = NextPowerOf2(DstTy.getSizeInBits()); 2053*8bcb0991SDimitry Andric LLT AnyExtTy = LLT::scalar(AnyExtSize); 2054*8bcb0991SDimitry Andric Register LargeLdReg = MRI.createGenericVirtualRegister(AnyExtTy); 2055*8bcb0991SDimitry Andric Register SmallLdReg = MRI.createGenericVirtualRegister(AnyExtTy); 2056*8bcb0991SDimitry Andric auto LargeLoad = 2057*8bcb0991SDimitry Andric MIRBuilder.buildLoad(LargeLdReg, PtrReg, *LargeMMO); 2058*8bcb0991SDimitry Andric 2059*8bcb0991SDimitry Andric auto OffsetCst = 2060*8bcb0991SDimitry Andric MIRBuilder.buildConstant(LLT::scalar(64), LargeSplitSize / 8); 2061*8bcb0991SDimitry Andric Register GEPReg = MRI.createGenericVirtualRegister(PtrTy); 2062*8bcb0991SDimitry Andric auto SmallPtr = MIRBuilder.buildGEP(GEPReg, PtrReg, OffsetCst.getReg(0)); 2063*8bcb0991SDimitry Andric auto SmallLoad = MIRBuilder.buildLoad(SmallLdReg, SmallPtr.getReg(0), 2064*8bcb0991SDimitry Andric *SmallMMO); 2065*8bcb0991SDimitry Andric 2066*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(AnyExtTy, LargeSplitSize); 2067*8bcb0991SDimitry Andric auto Shift = MIRBuilder.buildShl(AnyExtTy, SmallLoad, ShiftAmt); 2068*8bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad); 2069*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(DstReg, {Or.getReg(0)}); 2070*8bcb0991SDimitry Andric MI.eraseFromParent(); 2071*8bcb0991SDimitry Andric return Legalized; 2072*8bcb0991SDimitry Andric } 20730b57cec5SDimitry Andric MIRBuilder.buildLoad(DstReg, PtrReg, MMO); 20740b57cec5SDimitry Andric MI.eraseFromParent(); 20750b57cec5SDimitry Andric return Legalized; 20760b57cec5SDimitry Andric } 20770b57cec5SDimitry Andric 20780b57cec5SDimitry Andric if (DstTy.isScalar()) { 20790b57cec5SDimitry Andric Register TmpReg = 20800b57cec5SDimitry Andric MRI.createGenericVirtualRegister(LLT::scalar(MMO.getSizeInBits())); 20810b57cec5SDimitry Andric MIRBuilder.buildLoad(TmpReg, PtrReg, MMO); 20820b57cec5SDimitry Andric switch (MI.getOpcode()) { 20830b57cec5SDimitry Andric default: 20840b57cec5SDimitry Andric llvm_unreachable("Unexpected opcode"); 20850b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 20860b57cec5SDimitry Andric MIRBuilder.buildAnyExt(DstReg, TmpReg); 20870b57cec5SDimitry Andric break; 20880b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: 20890b57cec5SDimitry Andric MIRBuilder.buildSExt(DstReg, TmpReg); 20900b57cec5SDimitry Andric break; 20910b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 20920b57cec5SDimitry Andric MIRBuilder.buildZExt(DstReg, TmpReg); 20930b57cec5SDimitry Andric break; 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric MI.eraseFromParent(); 20960b57cec5SDimitry Andric return Legalized; 20970b57cec5SDimitry Andric } 20980b57cec5SDimitry Andric 20990b57cec5SDimitry Andric return UnableToLegalize; 21000b57cec5SDimitry Andric } 2101*8bcb0991SDimitry Andric case TargetOpcode::G_STORE: { 2102*8bcb0991SDimitry Andric // Lower a non-power of 2 store into multiple pow-2 stores. 2103*8bcb0991SDimitry Andric // E.g. split an i24 store into an i16 store + i8 store. 2104*8bcb0991SDimitry Andric // We do this by first extending the stored value to the next largest power 2105*8bcb0991SDimitry Andric // of 2 type, and then using truncating stores to store the components. 2106*8bcb0991SDimitry Andric // By doing this, likewise with G_LOAD, generate an extend that can be 2107*8bcb0991SDimitry Andric // artifact-combined away instead of leaving behind extracts. 2108*8bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 2109*8bcb0991SDimitry Andric Register PtrReg = MI.getOperand(1).getReg(); 2110*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 2111*8bcb0991SDimitry Andric MachineMemOperand &MMO = **MI.memoperands_begin(); 2112*8bcb0991SDimitry Andric if (SrcTy.getSizeInBits() != MMO.getSizeInBits()) 2113*8bcb0991SDimitry Andric return UnableToLegalize; 2114*8bcb0991SDimitry Andric if (SrcTy.isVector()) 2115*8bcb0991SDimitry Andric return UnableToLegalize; 2116*8bcb0991SDimitry Andric if (isPowerOf2_32(SrcTy.getSizeInBits())) 2117*8bcb0991SDimitry Andric return UnableToLegalize; // Don't know what we're being asked to do. 2118*8bcb0991SDimitry Andric 2119*8bcb0991SDimitry Andric // Extend to the next pow-2. 2120*8bcb0991SDimitry Andric const LLT ExtendTy = LLT::scalar(NextPowerOf2(SrcTy.getSizeInBits())); 2121*8bcb0991SDimitry Andric auto ExtVal = MIRBuilder.buildAnyExt(ExtendTy, SrcReg); 2122*8bcb0991SDimitry Andric 2123*8bcb0991SDimitry Andric // Obtain the smaller value by shifting away the larger value. 2124*8bcb0991SDimitry Andric uint64_t LargeSplitSize = PowerOf2Floor(SrcTy.getSizeInBits()); 2125*8bcb0991SDimitry Andric uint64_t SmallSplitSize = SrcTy.getSizeInBits() - LargeSplitSize; 2126*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(ExtendTy, LargeSplitSize); 2127*8bcb0991SDimitry Andric auto SmallVal = MIRBuilder.buildLShr(ExtendTy, ExtVal, ShiftAmt); 2128*8bcb0991SDimitry Andric 2129*8bcb0991SDimitry Andric // Generate the GEP and truncating stores. 2130*8bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 2131*8bcb0991SDimitry Andric auto OffsetCst = 2132*8bcb0991SDimitry Andric MIRBuilder.buildConstant(LLT::scalar(64), LargeSplitSize / 8); 2133*8bcb0991SDimitry Andric Register GEPReg = MRI.createGenericVirtualRegister(PtrTy); 2134*8bcb0991SDimitry Andric auto SmallPtr = MIRBuilder.buildGEP(GEPReg, PtrReg, OffsetCst.getReg(0)); 2135*8bcb0991SDimitry Andric 2136*8bcb0991SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 2137*8bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 2138*8bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 2139*8bcb0991SDimitry Andric MachineMemOperand *SmallMMO = 2140*8bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8); 2141*8bcb0991SDimitry Andric MIRBuilder.buildStore(ExtVal.getReg(0), PtrReg, *LargeMMO); 2142*8bcb0991SDimitry Andric MIRBuilder.buildStore(SmallVal.getReg(0), SmallPtr.getReg(0), *SmallMMO); 2143*8bcb0991SDimitry Andric MI.eraseFromParent(); 2144*8bcb0991SDimitry Andric return Legalized; 2145*8bcb0991SDimitry Andric } 21460b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 21470b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 21480b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 21490b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 21500b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: 21510b57cec5SDimitry Andric return lowerBitCount(MI, TypeIdx, Ty); 21520b57cec5SDimitry Andric case G_UADDO: { 21530b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 21540b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 21550b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 21560b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 21570b57cec5SDimitry Andric 21580b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, LHS, RHS); 21590b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS); 21600b57cec5SDimitry Andric 21610b57cec5SDimitry Andric MI.eraseFromParent(); 21620b57cec5SDimitry Andric return Legalized; 21630b57cec5SDimitry Andric } 21640b57cec5SDimitry Andric case G_UADDE: { 21650b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 21660b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 21670b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 21680b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 21690b57cec5SDimitry Andric Register CarryIn = MI.getOperand(4).getReg(); 21700b57cec5SDimitry Andric 21710b57cec5SDimitry Andric Register TmpRes = MRI.createGenericVirtualRegister(Ty); 21720b57cec5SDimitry Andric Register ZExtCarryIn = MRI.createGenericVirtualRegister(Ty); 21730b57cec5SDimitry Andric 21740b57cec5SDimitry Andric MIRBuilder.buildAdd(TmpRes, LHS, RHS); 21750b57cec5SDimitry Andric MIRBuilder.buildZExt(ZExtCarryIn, CarryIn); 21760b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn); 21770b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS); 21780b57cec5SDimitry Andric 21790b57cec5SDimitry Andric MI.eraseFromParent(); 21800b57cec5SDimitry Andric return Legalized; 21810b57cec5SDimitry Andric } 21820b57cec5SDimitry Andric case G_USUBO: { 21830b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 21840b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 21850b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 21860b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 21870b57cec5SDimitry Andric 21880b57cec5SDimitry Andric MIRBuilder.buildSub(Res, LHS, RHS); 21890b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS); 21900b57cec5SDimitry Andric 21910b57cec5SDimitry Andric MI.eraseFromParent(); 21920b57cec5SDimitry Andric return Legalized; 21930b57cec5SDimitry Andric } 21940b57cec5SDimitry Andric case G_USUBE: { 21950b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 21960b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 21970b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 21980b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 21990b57cec5SDimitry Andric Register BorrowIn = MI.getOperand(4).getReg(); 22000b57cec5SDimitry Andric 22010b57cec5SDimitry Andric Register TmpRes = MRI.createGenericVirtualRegister(Ty); 22020b57cec5SDimitry Andric Register ZExtBorrowIn = MRI.createGenericVirtualRegister(Ty); 22030b57cec5SDimitry Andric Register LHS_EQ_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1)); 22040b57cec5SDimitry Andric Register LHS_ULT_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1)); 22050b57cec5SDimitry Andric 22060b57cec5SDimitry Andric MIRBuilder.buildSub(TmpRes, LHS, RHS); 22070b57cec5SDimitry Andric MIRBuilder.buildZExt(ZExtBorrowIn, BorrowIn); 22080b57cec5SDimitry Andric MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn); 22090b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LHS_EQ_RHS, LHS, RHS); 22100b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, LHS_ULT_RHS, LHS, RHS); 22110b57cec5SDimitry Andric MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS); 22120b57cec5SDimitry Andric 22130b57cec5SDimitry Andric MI.eraseFromParent(); 22140b57cec5SDimitry Andric return Legalized; 22150b57cec5SDimitry Andric } 22160b57cec5SDimitry Andric case G_UITOFP: 22170b57cec5SDimitry Andric return lowerUITOFP(MI, TypeIdx, Ty); 22180b57cec5SDimitry Andric case G_SITOFP: 22190b57cec5SDimitry Andric return lowerSITOFP(MI, TypeIdx, Ty); 2220*8bcb0991SDimitry Andric case G_FPTOUI: 2221*8bcb0991SDimitry Andric return lowerFPTOUI(MI, TypeIdx, Ty); 22220b57cec5SDimitry Andric case G_SMIN: 22230b57cec5SDimitry Andric case G_SMAX: 22240b57cec5SDimitry Andric case G_UMIN: 22250b57cec5SDimitry Andric case G_UMAX: 22260b57cec5SDimitry Andric return lowerMinMax(MI, TypeIdx, Ty); 22270b57cec5SDimitry Andric case G_FCOPYSIGN: 22280b57cec5SDimitry Andric return lowerFCopySign(MI, TypeIdx, Ty); 22290b57cec5SDimitry Andric case G_FMINNUM: 22300b57cec5SDimitry Andric case G_FMAXNUM: 22310b57cec5SDimitry Andric return lowerFMinNumMaxNum(MI); 2232*8bcb0991SDimitry Andric case G_UNMERGE_VALUES: 2233*8bcb0991SDimitry Andric return lowerUnmergeValues(MI); 2234*8bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 2235*8bcb0991SDimitry Andric assert(MI.getOperand(2).isImm() && "Expected immediate"); 2236*8bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 2237*8bcb0991SDimitry Andric 2238*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 2239*8bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 2240*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 2241*8bcb0991SDimitry Andric Register TmpRes = MRI.createGenericVirtualRegister(DstTy); 2242*8bcb0991SDimitry Andric 2243*8bcb0991SDimitry Andric auto MIBSz = MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - SizeInBits); 2244*8bcb0991SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_SHL, {TmpRes}, {SrcReg, MIBSz->getOperand(0).getReg()}); 2245*8bcb0991SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_ASHR, {DstReg}, {TmpRes, MIBSz->getOperand(0).getReg()}); 2246*8bcb0991SDimitry Andric MI.eraseFromParent(); 2247*8bcb0991SDimitry Andric return Legalized; 2248*8bcb0991SDimitry Andric } 2249*8bcb0991SDimitry Andric case G_SHUFFLE_VECTOR: 2250*8bcb0991SDimitry Andric return lowerShuffleVector(MI); 2251*8bcb0991SDimitry Andric case G_DYN_STACKALLOC: 2252*8bcb0991SDimitry Andric return lowerDynStackAlloc(MI); 2253*8bcb0991SDimitry Andric case G_EXTRACT: 2254*8bcb0991SDimitry Andric return lowerExtract(MI); 2255*8bcb0991SDimitry Andric case G_INSERT: 2256*8bcb0991SDimitry Andric return lowerInsert(MI); 22570b57cec5SDimitry Andric } 22580b57cec5SDimitry Andric } 22590b57cec5SDimitry Andric 22600b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorImplicitDef( 22610b57cec5SDimitry Andric MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy) { 22620b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs; 22630b57cec5SDimitry Andric 22640b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 22650b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 22660b57cec5SDimitry Andric unsigned Size = MRI.getType(DstReg).getSizeInBits(); 22670b57cec5SDimitry Andric int NumParts = Size / NarrowSize; 22680b57cec5SDimitry Andric // FIXME: Don't know how to handle the situation where the small vectors 22690b57cec5SDimitry Andric // aren't all the same size yet. 22700b57cec5SDimitry Andric if (Size % NarrowSize != 0) 22710b57cec5SDimitry Andric return UnableToLegalize; 22720b57cec5SDimitry Andric 22730b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 22740b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 22750b57cec5SDimitry Andric MIRBuilder.buildUndef(TmpReg); 22760b57cec5SDimitry Andric DstRegs.push_back(TmpReg); 22770b57cec5SDimitry Andric } 22780b57cec5SDimitry Andric 22790b57cec5SDimitry Andric if (NarrowTy.isVector()) 22800b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 22810b57cec5SDimitry Andric else 22820b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 22830b57cec5SDimitry Andric 22840b57cec5SDimitry Andric MI.eraseFromParent(); 22850b57cec5SDimitry Andric return Legalized; 22860b57cec5SDimitry Andric } 22870b57cec5SDimitry Andric 22880b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 22890b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorBasic(MachineInstr &MI, unsigned TypeIdx, 22900b57cec5SDimitry Andric LLT NarrowTy) { 22910b57cec5SDimitry Andric const unsigned Opc = MI.getOpcode(); 22920b57cec5SDimitry Andric const unsigned NumOps = MI.getNumOperands() - 1; 22930b57cec5SDimitry Andric const unsigned NarrowSize = NarrowTy.getSizeInBits(); 22940b57cec5SDimitry Andric const Register DstReg = MI.getOperand(0).getReg(); 22950b57cec5SDimitry Andric const unsigned Flags = MI.getFlags(); 22960b57cec5SDimitry Andric const LLT DstTy = MRI.getType(DstReg); 22970b57cec5SDimitry Andric const unsigned Size = DstTy.getSizeInBits(); 22980b57cec5SDimitry Andric const int NumParts = Size / NarrowSize; 22990b57cec5SDimitry Andric const LLT EltTy = DstTy.getElementType(); 23000b57cec5SDimitry Andric const unsigned EltSize = EltTy.getSizeInBits(); 23010b57cec5SDimitry Andric const unsigned BitsForNumParts = NarrowSize * NumParts; 23020b57cec5SDimitry Andric 23030b57cec5SDimitry Andric // Check if we have any leftovers. If we do, then only handle the case where 23040b57cec5SDimitry Andric // the leftover is one element. 23050b57cec5SDimitry Andric if (BitsForNumParts != Size && BitsForNumParts + EltSize != Size) 23060b57cec5SDimitry Andric return UnableToLegalize; 23070b57cec5SDimitry Andric 23080b57cec5SDimitry Andric if (BitsForNumParts != Size) { 23090b57cec5SDimitry Andric Register AccumDstReg = MRI.createGenericVirtualRegister(DstTy); 23100b57cec5SDimitry Andric MIRBuilder.buildUndef(AccumDstReg); 23110b57cec5SDimitry Andric 23120b57cec5SDimitry Andric // Handle the pieces which evenly divide into the requested type with 23130b57cec5SDimitry Andric // extract/op/insert sequence. 23140b57cec5SDimitry Andric for (unsigned Offset = 0; Offset < BitsForNumParts; Offset += NarrowSize) { 23150b57cec5SDimitry Andric SmallVector<SrcOp, 4> SrcOps; 23160b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { 23170b57cec5SDimitry Andric Register PartOpReg = MRI.createGenericVirtualRegister(NarrowTy); 23180b57cec5SDimitry Andric MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(), Offset); 23190b57cec5SDimitry Andric SrcOps.push_back(PartOpReg); 23200b57cec5SDimitry Andric } 23210b57cec5SDimitry Andric 23220b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy); 23230b57cec5SDimitry Andric MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags); 23240b57cec5SDimitry Andric 23250b57cec5SDimitry Andric Register PartInsertReg = MRI.createGenericVirtualRegister(DstTy); 23260b57cec5SDimitry Andric MIRBuilder.buildInsert(PartInsertReg, AccumDstReg, PartDstReg, Offset); 23270b57cec5SDimitry Andric AccumDstReg = PartInsertReg; 23280b57cec5SDimitry Andric } 23290b57cec5SDimitry Andric 23300b57cec5SDimitry Andric // Handle the remaining element sized leftover piece. 23310b57cec5SDimitry Andric SmallVector<SrcOp, 4> SrcOps; 23320b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { 23330b57cec5SDimitry Andric Register PartOpReg = MRI.createGenericVirtualRegister(EltTy); 23340b57cec5SDimitry Andric MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(), 23350b57cec5SDimitry Andric BitsForNumParts); 23360b57cec5SDimitry Andric SrcOps.push_back(PartOpReg); 23370b57cec5SDimitry Andric } 23380b57cec5SDimitry Andric 23390b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(EltTy); 23400b57cec5SDimitry Andric MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags); 23410b57cec5SDimitry Andric MIRBuilder.buildInsert(DstReg, AccumDstReg, PartDstReg, BitsForNumParts); 23420b57cec5SDimitry Andric MI.eraseFromParent(); 23430b57cec5SDimitry Andric 23440b57cec5SDimitry Andric return Legalized; 23450b57cec5SDimitry Andric } 23460b57cec5SDimitry Andric 23470b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs; 23480b57cec5SDimitry Andric 23490b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src0Regs); 23500b57cec5SDimitry Andric 23510b57cec5SDimitry Andric if (NumOps >= 2) 23520b57cec5SDimitry Andric extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src1Regs); 23530b57cec5SDimitry Andric 23540b57cec5SDimitry Andric if (NumOps >= 3) 23550b57cec5SDimitry Andric extractParts(MI.getOperand(3).getReg(), NarrowTy, NumParts, Src2Regs); 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 23580b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy); 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric if (NumOps == 1) 23610b57cec5SDimitry Andric MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i]}, Flags); 23620b57cec5SDimitry Andric else if (NumOps == 2) { 23630b57cec5SDimitry Andric MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i], Src1Regs[i]}, Flags); 23640b57cec5SDimitry Andric } else if (NumOps == 3) { 23650b57cec5SDimitry Andric MIRBuilder.buildInstr(Opc, {DstReg}, 23660b57cec5SDimitry Andric {Src0Regs[i], Src1Regs[i], Src2Regs[i]}, Flags); 23670b57cec5SDimitry Andric } 23680b57cec5SDimitry Andric 23690b57cec5SDimitry Andric DstRegs.push_back(DstReg); 23700b57cec5SDimitry Andric } 23710b57cec5SDimitry Andric 23720b57cec5SDimitry Andric if (NarrowTy.isVector()) 23730b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 23740b57cec5SDimitry Andric else 23750b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric MI.eraseFromParent(); 23780b57cec5SDimitry Andric return Legalized; 23790b57cec5SDimitry Andric } 23800b57cec5SDimitry Andric 23810b57cec5SDimitry Andric // Handle splitting vector operations which need to have the same number of 23820b57cec5SDimitry Andric // elements in each type index, but each type index may have a different element 23830b57cec5SDimitry Andric // type. 23840b57cec5SDimitry Andric // 23850b57cec5SDimitry Andric // e.g. <4 x s64> = G_SHL <4 x s64>, <4 x s32> -> 23860b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 23870b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 23880b57cec5SDimitry Andric // 23890b57cec5SDimitry Andric // Also handles some irregular breakdown cases, e.g. 23900b57cec5SDimitry Andric // e.g. <3 x s64> = G_SHL <3 x s64>, <3 x s32> -> 23910b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 23920b57cec5SDimitry Andric // s64 = G_SHL s64, s32 23930b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 23940b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorMultiEltType( 23950b57cec5SDimitry Andric MachineInstr &MI, unsigned TypeIdx, LLT NarrowTyArg) { 23960b57cec5SDimitry Andric if (TypeIdx != 0) 23970b57cec5SDimitry Andric return UnableToLegalize; 23980b57cec5SDimitry Andric 23990b57cec5SDimitry Andric const LLT NarrowTy0 = NarrowTyArg; 24000b57cec5SDimitry Andric const unsigned NewNumElts = 24010b57cec5SDimitry Andric NarrowTy0.isVector() ? NarrowTy0.getNumElements() : 1; 24020b57cec5SDimitry Andric 24030b57cec5SDimitry Andric const Register DstReg = MI.getOperand(0).getReg(); 24040b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 24050b57cec5SDimitry Andric LLT LeftoverTy0; 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric // All of the operands need to have the same number of elements, so if we can 24080b57cec5SDimitry Andric // determine a type breakdown for the result type, we can for all of the 24090b57cec5SDimitry Andric // source types. 24100b57cec5SDimitry Andric int NumParts = getNarrowTypeBreakDown(DstTy, NarrowTy0, LeftoverTy0).first; 24110b57cec5SDimitry Andric if (NumParts < 0) 24120b57cec5SDimitry Andric return UnableToLegalize; 24130b57cec5SDimitry Andric 24140b57cec5SDimitry Andric SmallVector<MachineInstrBuilder, 4> NewInsts; 24150b57cec5SDimitry Andric 24160b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, LeftoverDstRegs; 24170b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs, LeftoverRegs; 24180b57cec5SDimitry Andric 24190b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { 24200b57cec5SDimitry Andric LLT LeftoverTy; 24210b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 24220b57cec5SDimitry Andric LLT SrcTyI = MRI.getType(SrcReg); 24230b57cec5SDimitry Andric LLT NarrowTyI = LLT::scalarOrVector(NewNumElts, SrcTyI.getScalarType()); 24240b57cec5SDimitry Andric LLT LeftoverTyI; 24250b57cec5SDimitry Andric 24260b57cec5SDimitry Andric // Split this operand into the requested typed registers, and any leftover 24270b57cec5SDimitry Andric // required to reproduce the original type. 24280b57cec5SDimitry Andric if (!extractParts(SrcReg, SrcTyI, NarrowTyI, LeftoverTyI, PartRegs, 24290b57cec5SDimitry Andric LeftoverRegs)) 24300b57cec5SDimitry Andric return UnableToLegalize; 24310b57cec5SDimitry Andric 24320b57cec5SDimitry Andric if (I == 1) { 24330b57cec5SDimitry Andric // For the first operand, create an instruction for each part and setup 24340b57cec5SDimitry Andric // the result. 24350b57cec5SDimitry Andric for (Register PartReg : PartRegs) { 24360b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy0); 24370b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) 24380b57cec5SDimitry Andric .addDef(PartDstReg) 24390b57cec5SDimitry Andric .addUse(PartReg)); 24400b57cec5SDimitry Andric DstRegs.push_back(PartDstReg); 24410b57cec5SDimitry Andric } 24420b57cec5SDimitry Andric 24430b57cec5SDimitry Andric for (Register LeftoverReg : LeftoverRegs) { 24440b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(LeftoverTy0); 24450b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) 24460b57cec5SDimitry Andric .addDef(PartDstReg) 24470b57cec5SDimitry Andric .addUse(LeftoverReg)); 24480b57cec5SDimitry Andric LeftoverDstRegs.push_back(PartDstReg); 24490b57cec5SDimitry Andric } 24500b57cec5SDimitry Andric } else { 24510b57cec5SDimitry Andric assert(NewInsts.size() == PartRegs.size() + LeftoverRegs.size()); 24520b57cec5SDimitry Andric 24530b57cec5SDimitry Andric // Add the newly created operand splits to the existing instructions. The 24540b57cec5SDimitry Andric // odd-sized pieces are ordered after the requested NarrowTyArg sized 24550b57cec5SDimitry Andric // pieces. 24560b57cec5SDimitry Andric unsigned InstCount = 0; 24570b57cec5SDimitry Andric for (unsigned J = 0, JE = PartRegs.size(); J != JE; ++J) 24580b57cec5SDimitry Andric NewInsts[InstCount++].addUse(PartRegs[J]); 24590b57cec5SDimitry Andric for (unsigned J = 0, JE = LeftoverRegs.size(); J != JE; ++J) 24600b57cec5SDimitry Andric NewInsts[InstCount++].addUse(LeftoverRegs[J]); 24610b57cec5SDimitry Andric } 24620b57cec5SDimitry Andric 24630b57cec5SDimitry Andric PartRegs.clear(); 24640b57cec5SDimitry Andric LeftoverRegs.clear(); 24650b57cec5SDimitry Andric } 24660b57cec5SDimitry Andric 24670b57cec5SDimitry Andric // Insert the newly built operations and rebuild the result register. 24680b57cec5SDimitry Andric for (auto &MIB : NewInsts) 24690b57cec5SDimitry Andric MIRBuilder.insertInstr(MIB); 24700b57cec5SDimitry Andric 24710b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy0, DstRegs, LeftoverTy0, LeftoverDstRegs); 24720b57cec5SDimitry Andric 24730b57cec5SDimitry Andric MI.eraseFromParent(); 24740b57cec5SDimitry Andric return Legalized; 24750b57cec5SDimitry Andric } 24760b57cec5SDimitry Andric 24770b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 24780b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCasts(MachineInstr &MI, unsigned TypeIdx, 24790b57cec5SDimitry Andric LLT NarrowTy) { 24800b57cec5SDimitry Andric if (TypeIdx != 0) 24810b57cec5SDimitry Andric return UnableToLegalize; 24820b57cec5SDimitry Andric 24830b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 24840b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 24850b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 24860b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 24870b57cec5SDimitry Andric 24880b57cec5SDimitry Andric LLT NarrowTy0 = NarrowTy; 24890b57cec5SDimitry Andric LLT NarrowTy1; 24900b57cec5SDimitry Andric unsigned NumParts; 24910b57cec5SDimitry Andric 24920b57cec5SDimitry Andric if (NarrowTy.isVector()) { 24930b57cec5SDimitry Andric // Uneven breakdown not handled. 24940b57cec5SDimitry Andric NumParts = DstTy.getNumElements() / NarrowTy.getNumElements(); 24950b57cec5SDimitry Andric if (NumParts * NarrowTy.getNumElements() != DstTy.getNumElements()) 24960b57cec5SDimitry Andric return UnableToLegalize; 24970b57cec5SDimitry Andric 24980b57cec5SDimitry Andric NarrowTy1 = LLT::vector(NumParts, SrcTy.getElementType().getSizeInBits()); 24990b57cec5SDimitry Andric } else { 25000b57cec5SDimitry Andric NumParts = DstTy.getNumElements(); 25010b57cec5SDimitry Andric NarrowTy1 = SrcTy.getElementType(); 25020b57cec5SDimitry Andric } 25030b57cec5SDimitry Andric 25040b57cec5SDimitry Andric SmallVector<Register, 4> SrcRegs, DstRegs; 25050b57cec5SDimitry Andric extractParts(SrcReg, NarrowTy1, NumParts, SrcRegs); 25060b57cec5SDimitry Andric 25070b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) { 25080b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 25090b57cec5SDimitry Andric MachineInstr *NewInst = MIRBuilder.buildInstr(MI.getOpcode()) 25100b57cec5SDimitry Andric .addDef(DstReg) 25110b57cec5SDimitry Andric .addUse(SrcRegs[I]); 25120b57cec5SDimitry Andric 25130b57cec5SDimitry Andric NewInst->setFlags(MI.getFlags()); 25140b57cec5SDimitry Andric DstRegs.push_back(DstReg); 25150b57cec5SDimitry Andric } 25160b57cec5SDimitry Andric 25170b57cec5SDimitry Andric if (NarrowTy.isVector()) 25180b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 25190b57cec5SDimitry Andric else 25200b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 25210b57cec5SDimitry Andric 25220b57cec5SDimitry Andric MI.eraseFromParent(); 25230b57cec5SDimitry Andric return Legalized; 25240b57cec5SDimitry Andric } 25250b57cec5SDimitry Andric 25260b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 25270b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCmp(MachineInstr &MI, unsigned TypeIdx, 25280b57cec5SDimitry Andric LLT NarrowTy) { 25290b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 25300b57cec5SDimitry Andric Register Src0Reg = MI.getOperand(2).getReg(); 25310b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 25320b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src0Reg); 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric unsigned NumParts; 25350b57cec5SDimitry Andric LLT NarrowTy0, NarrowTy1; 25360b57cec5SDimitry Andric 25370b57cec5SDimitry Andric if (TypeIdx == 0) { 25380b57cec5SDimitry Andric unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; 25390b57cec5SDimitry Andric unsigned OldElts = DstTy.getNumElements(); 25400b57cec5SDimitry Andric 25410b57cec5SDimitry Andric NarrowTy0 = NarrowTy; 25420b57cec5SDimitry Andric NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : DstTy.getNumElements(); 25430b57cec5SDimitry Andric NarrowTy1 = NarrowTy.isVector() ? 25440b57cec5SDimitry Andric LLT::vector(NarrowTy.getNumElements(), SrcTy.getScalarSizeInBits()) : 25450b57cec5SDimitry Andric SrcTy.getElementType(); 25460b57cec5SDimitry Andric 25470b57cec5SDimitry Andric } else { 25480b57cec5SDimitry Andric unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; 25490b57cec5SDimitry Andric unsigned OldElts = SrcTy.getNumElements(); 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : 25520b57cec5SDimitry Andric NarrowTy.getNumElements(); 25530b57cec5SDimitry Andric NarrowTy0 = LLT::vector(NarrowTy.getNumElements(), 25540b57cec5SDimitry Andric DstTy.getScalarSizeInBits()); 25550b57cec5SDimitry Andric NarrowTy1 = NarrowTy; 25560b57cec5SDimitry Andric } 25570b57cec5SDimitry Andric 25580b57cec5SDimitry Andric // FIXME: Don't know how to handle the situation where the small vectors 25590b57cec5SDimitry Andric // aren't all the same size yet. 25600b57cec5SDimitry Andric if (NarrowTy1.isVector() && 25610b57cec5SDimitry Andric NarrowTy1.getNumElements() * NumParts != DstTy.getNumElements()) 25620b57cec5SDimitry Andric return UnableToLegalize; 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric CmpInst::Predicate Pred 25650b57cec5SDimitry Andric = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate()); 25660b57cec5SDimitry Andric 25670b57cec5SDimitry Andric SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs; 25680b57cec5SDimitry Andric extractParts(MI.getOperand(2).getReg(), NarrowTy1, NumParts, Src1Regs); 25690b57cec5SDimitry Andric extractParts(MI.getOperand(3).getReg(), NarrowTy1, NumParts, Src2Regs); 25700b57cec5SDimitry Andric 25710b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) { 25720b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 25730b57cec5SDimitry Andric DstRegs.push_back(DstReg); 25740b57cec5SDimitry Andric 25750b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_ICMP) 25760b57cec5SDimitry Andric MIRBuilder.buildICmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); 25770b57cec5SDimitry Andric else { 25780b57cec5SDimitry Andric MachineInstr *NewCmp 25790b57cec5SDimitry Andric = MIRBuilder.buildFCmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); 25800b57cec5SDimitry Andric NewCmp->setFlags(MI.getFlags()); 25810b57cec5SDimitry Andric } 25820b57cec5SDimitry Andric } 25830b57cec5SDimitry Andric 25840b57cec5SDimitry Andric if (NarrowTy1.isVector()) 25850b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 25860b57cec5SDimitry Andric else 25870b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 25880b57cec5SDimitry Andric 25890b57cec5SDimitry Andric MI.eraseFromParent(); 25900b57cec5SDimitry Andric return Legalized; 25910b57cec5SDimitry Andric } 25920b57cec5SDimitry Andric 25930b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 25940b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorSelect(MachineInstr &MI, unsigned TypeIdx, 25950b57cec5SDimitry Andric LLT NarrowTy) { 25960b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 25970b57cec5SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 25980b57cec5SDimitry Andric 25990b57cec5SDimitry Andric unsigned NumParts = 0; 26000b57cec5SDimitry Andric LLT NarrowTy0, NarrowTy1; 26010b57cec5SDimitry Andric 26020b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 26030b57cec5SDimitry Andric LLT CondTy = MRI.getType(CondReg); 26040b57cec5SDimitry Andric unsigned Size = DstTy.getSizeInBits(); 26050b57cec5SDimitry Andric 26060b57cec5SDimitry Andric assert(TypeIdx == 0 || CondTy.isVector()); 26070b57cec5SDimitry Andric 26080b57cec5SDimitry Andric if (TypeIdx == 0) { 26090b57cec5SDimitry Andric NarrowTy0 = NarrowTy; 26100b57cec5SDimitry Andric NarrowTy1 = CondTy; 26110b57cec5SDimitry Andric 26120b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy0.getSizeInBits(); 26130b57cec5SDimitry Andric // FIXME: Don't know how to handle the situation where the small vectors 26140b57cec5SDimitry Andric // aren't all the same size yet. 26150b57cec5SDimitry Andric if (Size % NarrowSize != 0) 26160b57cec5SDimitry Andric return UnableToLegalize; 26170b57cec5SDimitry Andric 26180b57cec5SDimitry Andric NumParts = Size / NarrowSize; 26190b57cec5SDimitry Andric 26200b57cec5SDimitry Andric // Need to break down the condition type 26210b57cec5SDimitry Andric if (CondTy.isVector()) { 26220b57cec5SDimitry Andric if (CondTy.getNumElements() == NumParts) 26230b57cec5SDimitry Andric NarrowTy1 = CondTy.getElementType(); 26240b57cec5SDimitry Andric else 26250b57cec5SDimitry Andric NarrowTy1 = LLT::vector(CondTy.getNumElements() / NumParts, 26260b57cec5SDimitry Andric CondTy.getScalarSizeInBits()); 26270b57cec5SDimitry Andric } 26280b57cec5SDimitry Andric } else { 26290b57cec5SDimitry Andric NumParts = CondTy.getNumElements(); 26300b57cec5SDimitry Andric if (NarrowTy.isVector()) { 26310b57cec5SDimitry Andric // TODO: Handle uneven breakdown. 26320b57cec5SDimitry Andric if (NumParts * NarrowTy.getNumElements() != CondTy.getNumElements()) 26330b57cec5SDimitry Andric return UnableToLegalize; 26340b57cec5SDimitry Andric 26350b57cec5SDimitry Andric return UnableToLegalize; 26360b57cec5SDimitry Andric } else { 26370b57cec5SDimitry Andric NarrowTy0 = DstTy.getElementType(); 26380b57cec5SDimitry Andric NarrowTy1 = NarrowTy; 26390b57cec5SDimitry Andric } 26400b57cec5SDimitry Andric } 26410b57cec5SDimitry Andric 26420b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs; 26430b57cec5SDimitry Andric if (CondTy.isVector()) 26440b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy1, NumParts, Src0Regs); 26450b57cec5SDimitry Andric 26460b57cec5SDimitry Andric extractParts(MI.getOperand(2).getReg(), NarrowTy0, NumParts, Src1Regs); 26470b57cec5SDimitry Andric extractParts(MI.getOperand(3).getReg(), NarrowTy0, NumParts, Src2Regs); 26480b57cec5SDimitry Andric 26490b57cec5SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 26500b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 26510b57cec5SDimitry Andric MIRBuilder.buildSelect(DstReg, CondTy.isVector() ? Src0Regs[i] : CondReg, 26520b57cec5SDimitry Andric Src1Regs[i], Src2Regs[i]); 26530b57cec5SDimitry Andric DstRegs.push_back(DstReg); 26540b57cec5SDimitry Andric } 26550b57cec5SDimitry Andric 26560b57cec5SDimitry Andric if (NarrowTy0.isVector()) 26570b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 26580b57cec5SDimitry Andric else 26590b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 26600b57cec5SDimitry Andric 26610b57cec5SDimitry Andric MI.eraseFromParent(); 26620b57cec5SDimitry Andric return Legalized; 26630b57cec5SDimitry Andric } 26640b57cec5SDimitry Andric 26650b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 26660b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx, 26670b57cec5SDimitry Andric LLT NarrowTy) { 26680b57cec5SDimitry Andric const Register DstReg = MI.getOperand(0).getReg(); 26690b57cec5SDimitry Andric LLT PhiTy = MRI.getType(DstReg); 26700b57cec5SDimitry Andric LLT LeftoverTy; 26710b57cec5SDimitry Andric 26720b57cec5SDimitry Andric // All of the operands need to have the same number of elements, so if we can 26730b57cec5SDimitry Andric // determine a type breakdown for the result type, we can for all of the 26740b57cec5SDimitry Andric // source types. 26750b57cec5SDimitry Andric int NumParts, NumLeftover; 26760b57cec5SDimitry Andric std::tie(NumParts, NumLeftover) 26770b57cec5SDimitry Andric = getNarrowTypeBreakDown(PhiTy, NarrowTy, LeftoverTy); 26780b57cec5SDimitry Andric if (NumParts < 0) 26790b57cec5SDimitry Andric return UnableToLegalize; 26800b57cec5SDimitry Andric 26810b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, LeftoverDstRegs; 26820b57cec5SDimitry Andric SmallVector<MachineInstrBuilder, 4> NewInsts; 26830b57cec5SDimitry Andric 26840b57cec5SDimitry Andric const int TotalNumParts = NumParts + NumLeftover; 26850b57cec5SDimitry Andric 26860b57cec5SDimitry Andric // Insert the new phis in the result block first. 26870b57cec5SDimitry Andric for (int I = 0; I != TotalNumParts; ++I) { 26880b57cec5SDimitry Andric LLT Ty = I < NumParts ? NarrowTy : LeftoverTy; 26890b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(Ty); 26900b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstr(TargetOpcode::G_PHI) 26910b57cec5SDimitry Andric .addDef(PartDstReg)); 26920b57cec5SDimitry Andric if (I < NumParts) 26930b57cec5SDimitry Andric DstRegs.push_back(PartDstReg); 26940b57cec5SDimitry Andric else 26950b57cec5SDimitry Andric LeftoverDstRegs.push_back(PartDstReg); 26960b57cec5SDimitry Andric } 26970b57cec5SDimitry Andric 26980b57cec5SDimitry Andric MachineBasicBlock *MBB = MI.getParent(); 26990b57cec5SDimitry Andric MIRBuilder.setInsertPt(*MBB, MBB->getFirstNonPHI()); 27000b57cec5SDimitry Andric insertParts(DstReg, PhiTy, NarrowTy, DstRegs, LeftoverTy, LeftoverDstRegs); 27010b57cec5SDimitry Andric 27020b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs, LeftoverRegs; 27030b57cec5SDimitry Andric 27040b57cec5SDimitry Andric // Insert code to extract the incoming values in each predecessor block. 27050b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 27060b57cec5SDimitry Andric PartRegs.clear(); 27070b57cec5SDimitry Andric LeftoverRegs.clear(); 27080b57cec5SDimitry Andric 27090b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 27100b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 27110b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 27120b57cec5SDimitry Andric 27130b57cec5SDimitry Andric LLT Unused; 27140b57cec5SDimitry Andric if (!extractParts(SrcReg, PhiTy, NarrowTy, Unused, PartRegs, 27150b57cec5SDimitry Andric LeftoverRegs)) 27160b57cec5SDimitry Andric return UnableToLegalize; 27170b57cec5SDimitry Andric 27180b57cec5SDimitry Andric // Add the newly created operand splits to the existing instructions. The 27190b57cec5SDimitry Andric // odd-sized pieces are ordered after the requested NarrowTyArg sized 27200b57cec5SDimitry Andric // pieces. 27210b57cec5SDimitry Andric for (int J = 0; J != TotalNumParts; ++J) { 27220b57cec5SDimitry Andric MachineInstrBuilder MIB = NewInsts[J]; 27230b57cec5SDimitry Andric MIB.addUse(J < NumParts ? PartRegs[J] : LeftoverRegs[J - NumParts]); 27240b57cec5SDimitry Andric MIB.addMBB(&OpMBB); 27250b57cec5SDimitry Andric } 27260b57cec5SDimitry Andric } 27270b57cec5SDimitry Andric 27280b57cec5SDimitry Andric MI.eraseFromParent(); 27290b57cec5SDimitry Andric return Legalized; 27300b57cec5SDimitry Andric } 27310b57cec5SDimitry Andric 27320b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 2733*8bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorUnmergeValues(MachineInstr &MI, 2734*8bcb0991SDimitry Andric unsigned TypeIdx, 2735*8bcb0991SDimitry Andric LLT NarrowTy) { 2736*8bcb0991SDimitry Andric if (TypeIdx != 1) 2737*8bcb0991SDimitry Andric return UnableToLegalize; 2738*8bcb0991SDimitry Andric 2739*8bcb0991SDimitry Andric const int NumDst = MI.getNumOperands() - 1; 2740*8bcb0991SDimitry Andric const Register SrcReg = MI.getOperand(NumDst).getReg(); 2741*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 2742*8bcb0991SDimitry Andric 2743*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 2744*8bcb0991SDimitry Andric 2745*8bcb0991SDimitry Andric // TODO: Create sequence of extracts. 2746*8bcb0991SDimitry Andric if (DstTy == NarrowTy) 2747*8bcb0991SDimitry Andric return UnableToLegalize; 2748*8bcb0991SDimitry Andric 2749*8bcb0991SDimitry Andric LLT GCDTy = getGCDType(SrcTy, NarrowTy); 2750*8bcb0991SDimitry Andric if (DstTy == GCDTy) { 2751*8bcb0991SDimitry Andric // This would just be a copy of the same unmerge. 2752*8bcb0991SDimitry Andric // TODO: Create extracts, pad with undef and create intermediate merges. 2753*8bcb0991SDimitry Andric return UnableToLegalize; 2754*8bcb0991SDimitry Andric } 2755*8bcb0991SDimitry Andric 2756*8bcb0991SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 2757*8bcb0991SDimitry Andric const int NumUnmerge = Unmerge->getNumOperands() - 1; 2758*8bcb0991SDimitry Andric const int PartsPerUnmerge = NumDst / NumUnmerge; 2759*8bcb0991SDimitry Andric 2760*8bcb0991SDimitry Andric for (int I = 0; I != NumUnmerge; ++I) { 2761*8bcb0991SDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 2762*8bcb0991SDimitry Andric 2763*8bcb0991SDimitry Andric for (int J = 0; J != PartsPerUnmerge; ++J) 2764*8bcb0991SDimitry Andric MIB.addDef(MI.getOperand(I * PartsPerUnmerge + J).getReg()); 2765*8bcb0991SDimitry Andric MIB.addUse(Unmerge.getReg(I)); 2766*8bcb0991SDimitry Andric } 2767*8bcb0991SDimitry Andric 2768*8bcb0991SDimitry Andric MI.eraseFromParent(); 2769*8bcb0991SDimitry Andric return Legalized; 2770*8bcb0991SDimitry Andric } 2771*8bcb0991SDimitry Andric 2772*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 2773*8bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorBuildVector(MachineInstr &MI, 2774*8bcb0991SDimitry Andric unsigned TypeIdx, 2775*8bcb0991SDimitry Andric LLT NarrowTy) { 2776*8bcb0991SDimitry Andric assert(TypeIdx == 0 && "not a vector type index"); 2777*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 2778*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 2779*8bcb0991SDimitry Andric LLT SrcTy = DstTy.getElementType(); 2780*8bcb0991SDimitry Andric 2781*8bcb0991SDimitry Andric int DstNumElts = DstTy.getNumElements(); 2782*8bcb0991SDimitry Andric int NarrowNumElts = NarrowTy.getNumElements(); 2783*8bcb0991SDimitry Andric int NumConcat = (DstNumElts + NarrowNumElts - 1) / NarrowNumElts; 2784*8bcb0991SDimitry Andric LLT WidenedDstTy = LLT::vector(NarrowNumElts * NumConcat, SrcTy); 2785*8bcb0991SDimitry Andric 2786*8bcb0991SDimitry Andric SmallVector<Register, 8> ConcatOps; 2787*8bcb0991SDimitry Andric SmallVector<Register, 8> SubBuildVector; 2788*8bcb0991SDimitry Andric 2789*8bcb0991SDimitry Andric Register UndefReg; 2790*8bcb0991SDimitry Andric if (WidenedDstTy != DstTy) 2791*8bcb0991SDimitry Andric UndefReg = MIRBuilder.buildUndef(SrcTy).getReg(0); 2792*8bcb0991SDimitry Andric 2793*8bcb0991SDimitry Andric // Create a G_CONCAT_VECTORS of NarrowTy pieces, padding with undef as 2794*8bcb0991SDimitry Andric // necessary. 2795*8bcb0991SDimitry Andric // 2796*8bcb0991SDimitry Andric // %3:_(<3 x s16>) = G_BUILD_VECTOR %0, %1, %2 2797*8bcb0991SDimitry Andric // -> <2 x s16> 2798*8bcb0991SDimitry Andric // 2799*8bcb0991SDimitry Andric // %4:_(s16) = G_IMPLICIT_DEF 2800*8bcb0991SDimitry Andric // %5:_(<2 x s16>) = G_BUILD_VECTOR %0, %1 2801*8bcb0991SDimitry Andric // %6:_(<2 x s16>) = G_BUILD_VECTOR %2, %4 2802*8bcb0991SDimitry Andric // %7:_(<4 x s16>) = G_CONCAT_VECTORS %5, %6 2803*8bcb0991SDimitry Andric // %3:_(<3 x s16>) = G_EXTRACT %7, 0 2804*8bcb0991SDimitry Andric for (int I = 0; I != NumConcat; ++I) { 2805*8bcb0991SDimitry Andric for (int J = 0; J != NarrowNumElts; ++J) { 2806*8bcb0991SDimitry Andric int SrcIdx = NarrowNumElts * I + J; 2807*8bcb0991SDimitry Andric 2808*8bcb0991SDimitry Andric if (SrcIdx < DstNumElts) { 2809*8bcb0991SDimitry Andric Register SrcReg = MI.getOperand(SrcIdx + 1).getReg(); 2810*8bcb0991SDimitry Andric SubBuildVector.push_back(SrcReg); 2811*8bcb0991SDimitry Andric } else 2812*8bcb0991SDimitry Andric SubBuildVector.push_back(UndefReg); 2813*8bcb0991SDimitry Andric } 2814*8bcb0991SDimitry Andric 2815*8bcb0991SDimitry Andric auto BuildVec = MIRBuilder.buildBuildVector(NarrowTy, SubBuildVector); 2816*8bcb0991SDimitry Andric ConcatOps.push_back(BuildVec.getReg(0)); 2817*8bcb0991SDimitry Andric SubBuildVector.clear(); 2818*8bcb0991SDimitry Andric } 2819*8bcb0991SDimitry Andric 2820*8bcb0991SDimitry Andric if (DstTy == WidenedDstTy) 2821*8bcb0991SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, ConcatOps); 2822*8bcb0991SDimitry Andric else { 2823*8bcb0991SDimitry Andric auto Concat = MIRBuilder.buildConcatVectors(WidenedDstTy, ConcatOps); 2824*8bcb0991SDimitry Andric MIRBuilder.buildExtract(DstReg, Concat, 0); 2825*8bcb0991SDimitry Andric } 2826*8bcb0991SDimitry Andric 2827*8bcb0991SDimitry Andric MI.eraseFromParent(); 2828*8bcb0991SDimitry Andric return Legalized; 2829*8bcb0991SDimitry Andric } 2830*8bcb0991SDimitry Andric 2831*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 28320b57cec5SDimitry Andric LegalizerHelper::reduceLoadStoreWidth(MachineInstr &MI, unsigned TypeIdx, 28330b57cec5SDimitry Andric LLT NarrowTy) { 28340b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 28350b57cec5SDimitry Andric if (TypeIdx != 0) 28360b57cec5SDimitry Andric return UnableToLegalize; 28370b57cec5SDimitry Andric 28380b57cec5SDimitry Andric MachineMemOperand *MMO = *MI.memoperands_begin(); 28390b57cec5SDimitry Andric 28400b57cec5SDimitry Andric // This implementation doesn't work for atomics. Give up instead of doing 28410b57cec5SDimitry Andric // something invalid. 28420b57cec5SDimitry Andric if (MMO->getOrdering() != AtomicOrdering::NotAtomic || 28430b57cec5SDimitry Andric MMO->getFailureOrdering() != AtomicOrdering::NotAtomic) 28440b57cec5SDimitry Andric return UnableToLegalize; 28450b57cec5SDimitry Andric 28460b57cec5SDimitry Andric bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD; 28470b57cec5SDimitry Andric Register ValReg = MI.getOperand(0).getReg(); 28480b57cec5SDimitry Andric Register AddrReg = MI.getOperand(1).getReg(); 28490b57cec5SDimitry Andric LLT ValTy = MRI.getType(ValReg); 28500b57cec5SDimitry Andric 28510b57cec5SDimitry Andric int NumParts = -1; 28520b57cec5SDimitry Andric int NumLeftover = -1; 28530b57cec5SDimitry Andric LLT LeftoverTy; 28540b57cec5SDimitry Andric SmallVector<Register, 8> NarrowRegs, NarrowLeftoverRegs; 28550b57cec5SDimitry Andric if (IsLoad) { 28560b57cec5SDimitry Andric std::tie(NumParts, NumLeftover) = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy); 28570b57cec5SDimitry Andric } else { 28580b57cec5SDimitry Andric if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs, 28590b57cec5SDimitry Andric NarrowLeftoverRegs)) { 28600b57cec5SDimitry Andric NumParts = NarrowRegs.size(); 28610b57cec5SDimitry Andric NumLeftover = NarrowLeftoverRegs.size(); 28620b57cec5SDimitry Andric } 28630b57cec5SDimitry Andric } 28640b57cec5SDimitry Andric 28650b57cec5SDimitry Andric if (NumParts == -1) 28660b57cec5SDimitry Andric return UnableToLegalize; 28670b57cec5SDimitry Andric 28680b57cec5SDimitry Andric const LLT OffsetTy = LLT::scalar(MRI.getType(AddrReg).getScalarSizeInBits()); 28690b57cec5SDimitry Andric 28700b57cec5SDimitry Andric unsigned TotalSize = ValTy.getSizeInBits(); 28710b57cec5SDimitry Andric 28720b57cec5SDimitry Andric // Split the load/store into PartTy sized pieces starting at Offset. If this 28730b57cec5SDimitry Andric // is a load, return the new registers in ValRegs. For a store, each elements 28740b57cec5SDimitry Andric // of ValRegs should be PartTy. Returns the next offset that needs to be 28750b57cec5SDimitry Andric // handled. 28760b57cec5SDimitry Andric auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<Register> &ValRegs, 28770b57cec5SDimitry Andric unsigned Offset) -> unsigned { 28780b57cec5SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 28790b57cec5SDimitry Andric unsigned PartSize = PartTy.getSizeInBits(); 28800b57cec5SDimitry Andric for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize; 28810b57cec5SDimitry Andric Offset += PartSize, ++Idx) { 28820b57cec5SDimitry Andric unsigned ByteSize = PartSize / 8; 28830b57cec5SDimitry Andric unsigned ByteOffset = Offset / 8; 28840b57cec5SDimitry Andric Register NewAddrReg; 28850b57cec5SDimitry Andric 28860b57cec5SDimitry Andric MIRBuilder.materializeGEP(NewAddrReg, AddrReg, OffsetTy, ByteOffset); 28870b57cec5SDimitry Andric 28880b57cec5SDimitry Andric MachineMemOperand *NewMMO = 28890b57cec5SDimitry Andric MF.getMachineMemOperand(MMO, ByteOffset, ByteSize); 28900b57cec5SDimitry Andric 28910b57cec5SDimitry Andric if (IsLoad) { 28920b57cec5SDimitry Andric Register Dst = MRI.createGenericVirtualRegister(PartTy); 28930b57cec5SDimitry Andric ValRegs.push_back(Dst); 28940b57cec5SDimitry Andric MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO); 28950b57cec5SDimitry Andric } else { 28960b57cec5SDimitry Andric MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO); 28970b57cec5SDimitry Andric } 28980b57cec5SDimitry Andric } 28990b57cec5SDimitry Andric 29000b57cec5SDimitry Andric return Offset; 29010b57cec5SDimitry Andric }; 29020b57cec5SDimitry Andric 29030b57cec5SDimitry Andric unsigned HandledOffset = splitTypePieces(NarrowTy, NarrowRegs, 0); 29040b57cec5SDimitry Andric 29050b57cec5SDimitry Andric // Handle the rest of the register if this isn't an even type breakdown. 29060b57cec5SDimitry Andric if (LeftoverTy.isValid()) 29070b57cec5SDimitry Andric splitTypePieces(LeftoverTy, NarrowLeftoverRegs, HandledOffset); 29080b57cec5SDimitry Andric 29090b57cec5SDimitry Andric if (IsLoad) { 29100b57cec5SDimitry Andric insertParts(ValReg, ValTy, NarrowTy, NarrowRegs, 29110b57cec5SDimitry Andric LeftoverTy, NarrowLeftoverRegs); 29120b57cec5SDimitry Andric } 29130b57cec5SDimitry Andric 29140b57cec5SDimitry Andric MI.eraseFromParent(); 29150b57cec5SDimitry Andric return Legalized; 29160b57cec5SDimitry Andric } 29170b57cec5SDimitry Andric 29180b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 29190b57cec5SDimitry Andric LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx, 29200b57cec5SDimitry Andric LLT NarrowTy) { 29210b57cec5SDimitry Andric using namespace TargetOpcode; 29220b57cec5SDimitry Andric 29230b57cec5SDimitry Andric MIRBuilder.setInstr(MI); 29240b57cec5SDimitry Andric switch (MI.getOpcode()) { 29250b57cec5SDimitry Andric case G_IMPLICIT_DEF: 29260b57cec5SDimitry Andric return fewerElementsVectorImplicitDef(MI, TypeIdx, NarrowTy); 29270b57cec5SDimitry Andric case G_AND: 29280b57cec5SDimitry Andric case G_OR: 29290b57cec5SDimitry Andric case G_XOR: 29300b57cec5SDimitry Andric case G_ADD: 29310b57cec5SDimitry Andric case G_SUB: 29320b57cec5SDimitry Andric case G_MUL: 29330b57cec5SDimitry Andric case G_SMULH: 29340b57cec5SDimitry Andric case G_UMULH: 29350b57cec5SDimitry Andric case G_FADD: 29360b57cec5SDimitry Andric case G_FMUL: 29370b57cec5SDimitry Andric case G_FSUB: 29380b57cec5SDimitry Andric case G_FNEG: 29390b57cec5SDimitry Andric case G_FABS: 29400b57cec5SDimitry Andric case G_FCANONICALIZE: 29410b57cec5SDimitry Andric case G_FDIV: 29420b57cec5SDimitry Andric case G_FREM: 29430b57cec5SDimitry Andric case G_FMA: 2944*8bcb0991SDimitry Andric case G_FMAD: 29450b57cec5SDimitry Andric case G_FPOW: 29460b57cec5SDimitry Andric case G_FEXP: 29470b57cec5SDimitry Andric case G_FEXP2: 29480b57cec5SDimitry Andric case G_FLOG: 29490b57cec5SDimitry Andric case G_FLOG2: 29500b57cec5SDimitry Andric case G_FLOG10: 29510b57cec5SDimitry Andric case G_FNEARBYINT: 29520b57cec5SDimitry Andric case G_FCEIL: 29530b57cec5SDimitry Andric case G_FFLOOR: 29540b57cec5SDimitry Andric case G_FRINT: 29550b57cec5SDimitry Andric case G_INTRINSIC_ROUND: 29560b57cec5SDimitry Andric case G_INTRINSIC_TRUNC: 29570b57cec5SDimitry Andric case G_FCOS: 29580b57cec5SDimitry Andric case G_FSIN: 29590b57cec5SDimitry Andric case G_FSQRT: 29600b57cec5SDimitry Andric case G_BSWAP: 2961*8bcb0991SDimitry Andric case G_BITREVERSE: 29620b57cec5SDimitry Andric case G_SDIV: 29630b57cec5SDimitry Andric case G_SMIN: 29640b57cec5SDimitry Andric case G_SMAX: 29650b57cec5SDimitry Andric case G_UMIN: 29660b57cec5SDimitry Andric case G_UMAX: 29670b57cec5SDimitry Andric case G_FMINNUM: 29680b57cec5SDimitry Andric case G_FMAXNUM: 29690b57cec5SDimitry Andric case G_FMINNUM_IEEE: 29700b57cec5SDimitry Andric case G_FMAXNUM_IEEE: 29710b57cec5SDimitry Andric case G_FMINIMUM: 29720b57cec5SDimitry Andric case G_FMAXIMUM: 29730b57cec5SDimitry Andric return fewerElementsVectorBasic(MI, TypeIdx, NarrowTy); 29740b57cec5SDimitry Andric case G_SHL: 29750b57cec5SDimitry Andric case G_LSHR: 29760b57cec5SDimitry Andric case G_ASHR: 29770b57cec5SDimitry Andric case G_CTLZ: 29780b57cec5SDimitry Andric case G_CTLZ_ZERO_UNDEF: 29790b57cec5SDimitry Andric case G_CTTZ: 29800b57cec5SDimitry Andric case G_CTTZ_ZERO_UNDEF: 29810b57cec5SDimitry Andric case G_CTPOP: 29820b57cec5SDimitry Andric case G_FCOPYSIGN: 29830b57cec5SDimitry Andric return fewerElementsVectorMultiEltType(MI, TypeIdx, NarrowTy); 29840b57cec5SDimitry Andric case G_ZEXT: 29850b57cec5SDimitry Andric case G_SEXT: 29860b57cec5SDimitry Andric case G_ANYEXT: 29870b57cec5SDimitry Andric case G_FPEXT: 29880b57cec5SDimitry Andric case G_FPTRUNC: 29890b57cec5SDimitry Andric case G_SITOFP: 29900b57cec5SDimitry Andric case G_UITOFP: 29910b57cec5SDimitry Andric case G_FPTOSI: 29920b57cec5SDimitry Andric case G_FPTOUI: 29930b57cec5SDimitry Andric case G_INTTOPTR: 29940b57cec5SDimitry Andric case G_PTRTOINT: 29950b57cec5SDimitry Andric case G_ADDRSPACE_CAST: 29960b57cec5SDimitry Andric return fewerElementsVectorCasts(MI, TypeIdx, NarrowTy); 29970b57cec5SDimitry Andric case G_ICMP: 29980b57cec5SDimitry Andric case G_FCMP: 29990b57cec5SDimitry Andric return fewerElementsVectorCmp(MI, TypeIdx, NarrowTy); 30000b57cec5SDimitry Andric case G_SELECT: 30010b57cec5SDimitry Andric return fewerElementsVectorSelect(MI, TypeIdx, NarrowTy); 30020b57cec5SDimitry Andric case G_PHI: 30030b57cec5SDimitry Andric return fewerElementsVectorPhi(MI, TypeIdx, NarrowTy); 3004*8bcb0991SDimitry Andric case G_UNMERGE_VALUES: 3005*8bcb0991SDimitry Andric return fewerElementsVectorUnmergeValues(MI, TypeIdx, NarrowTy); 3006*8bcb0991SDimitry Andric case G_BUILD_VECTOR: 3007*8bcb0991SDimitry Andric return fewerElementsVectorBuildVector(MI, TypeIdx, NarrowTy); 30080b57cec5SDimitry Andric case G_LOAD: 30090b57cec5SDimitry Andric case G_STORE: 30100b57cec5SDimitry Andric return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy); 30110b57cec5SDimitry Andric default: 30120b57cec5SDimitry Andric return UnableToLegalize; 30130b57cec5SDimitry Andric } 30140b57cec5SDimitry Andric } 30150b57cec5SDimitry Andric 30160b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 30170b57cec5SDimitry Andric LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt, 30180b57cec5SDimitry Andric const LLT HalfTy, const LLT AmtTy) { 30190b57cec5SDimitry Andric 30200b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 30210b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 30220b57cec5SDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg()); 30230b57cec5SDimitry Andric 30240b57cec5SDimitry Andric if (Amt.isNullValue()) { 30250b57cec5SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {InL, InH}); 30260b57cec5SDimitry Andric MI.eraseFromParent(); 30270b57cec5SDimitry Andric return Legalized; 30280b57cec5SDimitry Andric } 30290b57cec5SDimitry Andric 30300b57cec5SDimitry Andric LLT NVT = HalfTy; 30310b57cec5SDimitry Andric unsigned NVTBits = HalfTy.getSizeInBits(); 30320b57cec5SDimitry Andric unsigned VTBits = 2 * NVTBits; 30330b57cec5SDimitry Andric 30340b57cec5SDimitry Andric SrcOp Lo(Register(0)), Hi(Register(0)); 30350b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_SHL) { 30360b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 30370b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 30380b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 30390b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 30400b57cec5SDimitry Andric Hi = MIRBuilder.buildShl(NVT, InL, 30410b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 30420b57cec5SDimitry Andric } else if (Amt == NVTBits) { 30430b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 30440b57cec5SDimitry Andric Hi = InL; 30450b57cec5SDimitry Andric } else { 30460b57cec5SDimitry Andric Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt)); 30470b57cec5SDimitry Andric auto OrLHS = 30480b57cec5SDimitry Andric MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt)); 30490b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildLShr( 30500b57cec5SDimitry Andric NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 30510b57cec5SDimitry Andric Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 30520b57cec5SDimitry Andric } 30530b57cec5SDimitry Andric } else if (MI.getOpcode() == TargetOpcode::G_LSHR) { 30540b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 30550b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 30560b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 30570b57cec5SDimitry Andric Lo = MIRBuilder.buildLShr(NVT, InH, 30580b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 30590b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 30600b57cec5SDimitry Andric } else if (Amt == NVTBits) { 30610b57cec5SDimitry Andric Lo = InH; 30620b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 30630b57cec5SDimitry Andric } else { 30640b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 30650b57cec5SDimitry Andric 30660b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 30670b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 30680b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 30690b57cec5SDimitry Andric 30700b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 30710b57cec5SDimitry Andric Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst); 30720b57cec5SDimitry Andric } 30730b57cec5SDimitry Andric } else { 30740b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 30750b57cec5SDimitry Andric Hi = Lo = MIRBuilder.buildAShr( 30760b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 30770b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 30780b57cec5SDimitry Andric Lo = MIRBuilder.buildAShr(NVT, InH, 30790b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 30800b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 30810b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 30820b57cec5SDimitry Andric } else if (Amt == NVTBits) { 30830b57cec5SDimitry Andric Lo = InH; 30840b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 30850b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 30860b57cec5SDimitry Andric } else { 30870b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 30880b57cec5SDimitry Andric 30890b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 30900b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 30910b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 30920b57cec5SDimitry Andric 30930b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 30940b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst); 30950b57cec5SDimitry Andric } 30960b57cec5SDimitry Andric } 30970b57cec5SDimitry Andric 30980b57cec5SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {Lo.getReg(), Hi.getReg()}); 30990b57cec5SDimitry Andric MI.eraseFromParent(); 31000b57cec5SDimitry Andric 31010b57cec5SDimitry Andric return Legalized; 31020b57cec5SDimitry Andric } 31030b57cec5SDimitry Andric 31040b57cec5SDimitry Andric // TODO: Optimize if constant shift amount. 31050b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 31060b57cec5SDimitry Andric LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx, 31070b57cec5SDimitry Andric LLT RequestedTy) { 31080b57cec5SDimitry Andric if (TypeIdx == 1) { 31090b57cec5SDimitry Andric Observer.changingInstr(MI); 31100b57cec5SDimitry Andric narrowScalarSrc(MI, RequestedTy, 2); 31110b57cec5SDimitry Andric Observer.changedInstr(MI); 31120b57cec5SDimitry Andric return Legalized; 31130b57cec5SDimitry Andric } 31140b57cec5SDimitry Andric 31150b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 31160b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 31170b57cec5SDimitry Andric if (DstTy.isVector()) 31180b57cec5SDimitry Andric return UnableToLegalize; 31190b57cec5SDimitry Andric 31200b57cec5SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 31210b57cec5SDimitry Andric LLT ShiftAmtTy = MRI.getType(Amt); 31220b57cec5SDimitry Andric const unsigned DstEltSize = DstTy.getScalarSizeInBits(); 31230b57cec5SDimitry Andric if (DstEltSize % 2 != 0) 31240b57cec5SDimitry Andric return UnableToLegalize; 31250b57cec5SDimitry Andric 31260b57cec5SDimitry Andric // Ignore the input type. We can only go to exactly half the size of the 31270b57cec5SDimitry Andric // input. If that isn't small enough, the resulting pieces will be further 31280b57cec5SDimitry Andric // legalized. 31290b57cec5SDimitry Andric const unsigned NewBitSize = DstEltSize / 2; 31300b57cec5SDimitry Andric const LLT HalfTy = LLT::scalar(NewBitSize); 31310b57cec5SDimitry Andric const LLT CondTy = LLT::scalar(1); 31320b57cec5SDimitry Andric 31330b57cec5SDimitry Andric if (const MachineInstr *KShiftAmt = 31340b57cec5SDimitry Andric getOpcodeDef(TargetOpcode::G_CONSTANT, Amt, MRI)) { 31350b57cec5SDimitry Andric return narrowScalarShiftByConstant( 31360b57cec5SDimitry Andric MI, KShiftAmt->getOperand(1).getCImm()->getValue(), HalfTy, ShiftAmtTy); 31370b57cec5SDimitry Andric } 31380b57cec5SDimitry Andric 31390b57cec5SDimitry Andric // TODO: Expand with known bits. 31400b57cec5SDimitry Andric 31410b57cec5SDimitry Andric // Handle the fully general expansion by an unknown amount. 31420b57cec5SDimitry Andric auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize); 31430b57cec5SDimitry Andric 31440b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 31450b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 31460b57cec5SDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg()); 31470b57cec5SDimitry Andric 31480b57cec5SDimitry Andric auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits); 31490b57cec5SDimitry Andric auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt); 31500b57cec5SDimitry Andric 31510b57cec5SDimitry Andric auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0); 31520b57cec5SDimitry Andric auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits); 31530b57cec5SDimitry Andric auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero); 31540b57cec5SDimitry Andric 31550b57cec5SDimitry Andric Register ResultRegs[2]; 31560b57cec5SDimitry Andric switch (MI.getOpcode()) { 31570b57cec5SDimitry Andric case TargetOpcode::G_SHL: { 31580b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 3159*8bcb0991SDimitry Andric auto LoS = MIRBuilder.buildShl(HalfTy, InL, Amt); 31600b57cec5SDimitry Andric 3161*8bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, AmtLack); 3162*8bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, Amt); 3163*8bcb0991SDimitry Andric auto HiS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 31640b57cec5SDimitry Andric 31650b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 31660b57cec5SDimitry Andric auto LoL = MIRBuilder.buildConstant(HalfTy, 0); // Lo part is zero. 31670b57cec5SDimitry Andric auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part. 31680b57cec5SDimitry Andric 31690b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL); 31700b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect( 31710b57cec5SDimitry Andric HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL)); 31720b57cec5SDimitry Andric 31730b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 31740b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 31750b57cec5SDimitry Andric break; 31760b57cec5SDimitry Andric } 3177*8bcb0991SDimitry Andric case TargetOpcode::G_LSHR: 31780b57cec5SDimitry Andric case TargetOpcode::G_ASHR: { 31790b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 3180*8bcb0991SDimitry Andric auto HiS = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, {InH, Amt}); 31810b57cec5SDimitry Andric 3182*8bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, Amt); 3183*8bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, AmtLack); 3184*8bcb0991SDimitry Andric auto LoS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 31850b57cec5SDimitry Andric 31860b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 3187*8bcb0991SDimitry Andric MachineInstrBuilder HiL; 3188*8bcb0991SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_LSHR) { 3189*8bcb0991SDimitry Andric HiL = MIRBuilder.buildConstant(HalfTy, 0); // Hi part is zero. 3190*8bcb0991SDimitry Andric } else { 3191*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1); 3192*8bcb0991SDimitry Andric HiL = MIRBuilder.buildAShr(HalfTy, InH, ShiftAmt); // Sign of Hi part. 3193*8bcb0991SDimitry Andric } 3194*8bcb0991SDimitry Andric auto LoL = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, 3195*8bcb0991SDimitry Andric {InH, AmtExcess}); // Lo from Hi part. 31960b57cec5SDimitry Andric 31970b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect( 31980b57cec5SDimitry Andric HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL)); 31990b57cec5SDimitry Andric 32000b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL); 32010b57cec5SDimitry Andric 32020b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 32030b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 32040b57cec5SDimitry Andric break; 32050b57cec5SDimitry Andric } 32060b57cec5SDimitry Andric default: 32070b57cec5SDimitry Andric llvm_unreachable("not a shift"); 32080b57cec5SDimitry Andric } 32090b57cec5SDimitry Andric 32100b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, ResultRegs); 32110b57cec5SDimitry Andric MI.eraseFromParent(); 32120b57cec5SDimitry Andric return Legalized; 32130b57cec5SDimitry Andric } 32140b57cec5SDimitry Andric 32150b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 32160b57cec5SDimitry Andric LegalizerHelper::moreElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx, 32170b57cec5SDimitry Andric LLT MoreTy) { 32180b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 32190b57cec5SDimitry Andric 32200b57cec5SDimitry Andric Observer.changingInstr(MI); 32210b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 32220b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 32230b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 32240b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, I); 32250b57cec5SDimitry Andric } 32260b57cec5SDimitry Andric 32270b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 32280b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 32290b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 32300b57cec5SDimitry Andric Observer.changedInstr(MI); 32310b57cec5SDimitry Andric return Legalized; 32320b57cec5SDimitry Andric } 32330b57cec5SDimitry Andric 32340b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 32350b57cec5SDimitry Andric LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx, 32360b57cec5SDimitry Andric LLT MoreTy) { 32370b57cec5SDimitry Andric MIRBuilder.setInstr(MI); 32380b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 32390b57cec5SDimitry Andric switch (Opc) { 3240*8bcb0991SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: 3241*8bcb0991SDimitry Andric case TargetOpcode::G_LOAD: { 3242*8bcb0991SDimitry Andric if (TypeIdx != 0) 3243*8bcb0991SDimitry Andric return UnableToLegalize; 32440b57cec5SDimitry Andric Observer.changingInstr(MI); 32450b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 32460b57cec5SDimitry Andric Observer.changedInstr(MI); 32470b57cec5SDimitry Andric return Legalized; 32480b57cec5SDimitry Andric } 3249*8bcb0991SDimitry Andric case TargetOpcode::G_STORE: 3250*8bcb0991SDimitry Andric if (TypeIdx != 0) 3251*8bcb0991SDimitry Andric return UnableToLegalize; 3252*8bcb0991SDimitry Andric Observer.changingInstr(MI); 3253*8bcb0991SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 0); 3254*8bcb0991SDimitry Andric Observer.changedInstr(MI); 3255*8bcb0991SDimitry Andric return Legalized; 32560b57cec5SDimitry Andric case TargetOpcode::G_AND: 32570b57cec5SDimitry Andric case TargetOpcode::G_OR: 32580b57cec5SDimitry Andric case TargetOpcode::G_XOR: 32590b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 32600b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 32610b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 32620b57cec5SDimitry Andric case TargetOpcode::G_UMAX: { 32630b57cec5SDimitry Andric Observer.changingInstr(MI); 32640b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 32650b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 32660b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 32670b57cec5SDimitry Andric Observer.changedInstr(MI); 32680b57cec5SDimitry Andric return Legalized; 32690b57cec5SDimitry Andric } 32700b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 32710b57cec5SDimitry Andric if (TypeIdx != 1) 32720b57cec5SDimitry Andric return UnableToLegalize; 32730b57cec5SDimitry Andric Observer.changingInstr(MI); 32740b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 32750b57cec5SDimitry Andric Observer.changedInstr(MI); 32760b57cec5SDimitry Andric return Legalized; 32770b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 32780b57cec5SDimitry Andric if (TypeIdx != 0) 32790b57cec5SDimitry Andric return UnableToLegalize; 32800b57cec5SDimitry Andric Observer.changingInstr(MI); 32810b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 32820b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 32830b57cec5SDimitry Andric Observer.changedInstr(MI); 32840b57cec5SDimitry Andric return Legalized; 32850b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 32860b57cec5SDimitry Andric if (TypeIdx != 0) 32870b57cec5SDimitry Andric return UnableToLegalize; 32880b57cec5SDimitry Andric if (MRI.getType(MI.getOperand(1).getReg()).isVector()) 32890b57cec5SDimitry Andric return UnableToLegalize; 32900b57cec5SDimitry Andric 32910b57cec5SDimitry Andric Observer.changingInstr(MI); 32920b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 32930b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 3); 32940b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 32950b57cec5SDimitry Andric Observer.changedInstr(MI); 32960b57cec5SDimitry Andric return Legalized; 3297*8bcb0991SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: { 3298*8bcb0991SDimitry Andric if (TypeIdx != 1) 3299*8bcb0991SDimitry Andric return UnableToLegalize; 3300*8bcb0991SDimitry Andric 3301*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 3302*8bcb0991SDimitry Andric int NumDst = MI.getNumOperands() - 1; 3303*8bcb0991SDimitry Andric moreElementsVectorSrc(MI, MoreTy, NumDst); 3304*8bcb0991SDimitry Andric 3305*8bcb0991SDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 3306*8bcb0991SDimitry Andric for (int I = 0; I != NumDst; ++I) 3307*8bcb0991SDimitry Andric MIB.addDef(MI.getOperand(I).getReg()); 3308*8bcb0991SDimitry Andric 3309*8bcb0991SDimitry Andric int NewNumDst = MoreTy.getSizeInBits() / DstTy.getSizeInBits(); 3310*8bcb0991SDimitry Andric for (int I = NumDst; I != NewNumDst; ++I) 3311*8bcb0991SDimitry Andric MIB.addDef(MRI.createGenericVirtualRegister(DstTy)); 3312*8bcb0991SDimitry Andric 3313*8bcb0991SDimitry Andric MIB.addUse(MI.getOperand(NumDst).getReg()); 3314*8bcb0991SDimitry Andric MI.eraseFromParent(); 3315*8bcb0991SDimitry Andric return Legalized; 3316*8bcb0991SDimitry Andric } 33170b57cec5SDimitry Andric case TargetOpcode::G_PHI: 33180b57cec5SDimitry Andric return moreElementsVectorPhi(MI, TypeIdx, MoreTy); 33190b57cec5SDimitry Andric default: 33200b57cec5SDimitry Andric return UnableToLegalize; 33210b57cec5SDimitry Andric } 33220b57cec5SDimitry Andric } 33230b57cec5SDimitry Andric 33240b57cec5SDimitry Andric void LegalizerHelper::multiplyRegisters(SmallVectorImpl<Register> &DstRegs, 33250b57cec5SDimitry Andric ArrayRef<Register> Src1Regs, 33260b57cec5SDimitry Andric ArrayRef<Register> Src2Regs, 33270b57cec5SDimitry Andric LLT NarrowTy) { 33280b57cec5SDimitry Andric MachineIRBuilder &B = MIRBuilder; 33290b57cec5SDimitry Andric unsigned SrcParts = Src1Regs.size(); 33300b57cec5SDimitry Andric unsigned DstParts = DstRegs.size(); 33310b57cec5SDimitry Andric 33320b57cec5SDimitry Andric unsigned DstIdx = 0; // Low bits of the result. 33330b57cec5SDimitry Andric Register FactorSum = 33340b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx], Src2Regs[DstIdx]).getReg(0); 33350b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 33360b57cec5SDimitry Andric 33370b57cec5SDimitry Andric unsigned CarrySumPrevDstIdx; 33380b57cec5SDimitry Andric SmallVector<Register, 4> Factors; 33390b57cec5SDimitry Andric 33400b57cec5SDimitry Andric for (DstIdx = 1; DstIdx < DstParts; DstIdx++) { 33410b57cec5SDimitry Andric // Collect low parts of muls for DstIdx. 33420b57cec5SDimitry Andric for (unsigned i = DstIdx + 1 < SrcParts ? 0 : DstIdx - SrcParts + 1; 33430b57cec5SDimitry Andric i <= std::min(DstIdx, SrcParts - 1); ++i) { 33440b57cec5SDimitry Andric MachineInstrBuilder Mul = 33450b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx - i], Src2Regs[i]); 33460b57cec5SDimitry Andric Factors.push_back(Mul.getReg(0)); 33470b57cec5SDimitry Andric } 33480b57cec5SDimitry Andric // Collect high parts of muls from previous DstIdx. 33490b57cec5SDimitry Andric for (unsigned i = DstIdx < SrcParts ? 0 : DstIdx - SrcParts; 33500b57cec5SDimitry Andric i <= std::min(DstIdx - 1, SrcParts - 1); ++i) { 33510b57cec5SDimitry Andric MachineInstrBuilder Umulh = 33520b57cec5SDimitry Andric B.buildUMulH(NarrowTy, Src1Regs[DstIdx - 1 - i], Src2Regs[i]); 33530b57cec5SDimitry Andric Factors.push_back(Umulh.getReg(0)); 33540b57cec5SDimitry Andric } 33550b57cec5SDimitry Andric // Add CarrySum from additons calculated for previous DstIdx. 33560b57cec5SDimitry Andric if (DstIdx != 1) { 33570b57cec5SDimitry Andric Factors.push_back(CarrySumPrevDstIdx); 33580b57cec5SDimitry Andric } 33590b57cec5SDimitry Andric 33600b57cec5SDimitry Andric Register CarrySum; 33610b57cec5SDimitry Andric // Add all factors and accumulate all carries into CarrySum. 33620b57cec5SDimitry Andric if (DstIdx != DstParts - 1) { 33630b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 33640b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), Factors[0], Factors[1]); 33650b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 33660b57cec5SDimitry Andric CarrySum = B.buildZExt(NarrowTy, Uaddo.getReg(1)).getReg(0); 33670b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) { 33680b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 33690b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), FactorSum, Factors[i]); 33700b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 33710b57cec5SDimitry Andric MachineInstrBuilder Carry = B.buildZExt(NarrowTy, Uaddo.getReg(1)); 33720b57cec5SDimitry Andric CarrySum = B.buildAdd(NarrowTy, CarrySum, Carry).getReg(0); 33730b57cec5SDimitry Andric } 33740b57cec5SDimitry Andric } else { 33750b57cec5SDimitry Andric // Since value for the next index is not calculated, neither is CarrySum. 33760b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, Factors[0], Factors[1]).getReg(0); 33770b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) 33780b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, FactorSum, Factors[i]).getReg(0); 33790b57cec5SDimitry Andric } 33800b57cec5SDimitry Andric 33810b57cec5SDimitry Andric CarrySumPrevDstIdx = CarrySum; 33820b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 33830b57cec5SDimitry Andric Factors.clear(); 33840b57cec5SDimitry Andric } 33850b57cec5SDimitry Andric } 33860b57cec5SDimitry Andric 33870b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 33880b57cec5SDimitry Andric LegalizerHelper::narrowScalarMul(MachineInstr &MI, LLT NarrowTy) { 33890b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 33900b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 33910b57cec5SDimitry Andric Register Src2 = MI.getOperand(2).getReg(); 33920b57cec5SDimitry Andric 33930b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 33940b57cec5SDimitry Andric if (Ty.isVector()) 33950b57cec5SDimitry Andric return UnableToLegalize; 33960b57cec5SDimitry Andric 33970b57cec5SDimitry Andric unsigned SrcSize = MRI.getType(Src1).getSizeInBits(); 33980b57cec5SDimitry Andric unsigned DstSize = Ty.getSizeInBits(); 33990b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 34000b57cec5SDimitry Andric if (DstSize % NarrowSize != 0 || SrcSize % NarrowSize != 0) 34010b57cec5SDimitry Andric return UnableToLegalize; 34020b57cec5SDimitry Andric 34030b57cec5SDimitry Andric unsigned NumDstParts = DstSize / NarrowSize; 34040b57cec5SDimitry Andric unsigned NumSrcParts = SrcSize / NarrowSize; 34050b57cec5SDimitry Andric bool IsMulHigh = MI.getOpcode() == TargetOpcode::G_UMULH; 34060b57cec5SDimitry Andric unsigned DstTmpParts = NumDstParts * (IsMulHigh ? 2 : 1); 34070b57cec5SDimitry Andric 34080b57cec5SDimitry Andric SmallVector<Register, 2> Src1Parts, Src2Parts, DstTmpRegs; 34090b57cec5SDimitry Andric extractParts(Src1, NarrowTy, NumSrcParts, Src1Parts); 34100b57cec5SDimitry Andric extractParts(Src2, NarrowTy, NumSrcParts, Src2Parts); 34110b57cec5SDimitry Andric DstTmpRegs.resize(DstTmpParts); 34120b57cec5SDimitry Andric multiplyRegisters(DstTmpRegs, Src1Parts, Src2Parts, NarrowTy); 34130b57cec5SDimitry Andric 34140b57cec5SDimitry Andric // Take only high half of registers if this is high mul. 34150b57cec5SDimitry Andric ArrayRef<Register> DstRegs( 34160b57cec5SDimitry Andric IsMulHigh ? &DstTmpRegs[DstTmpParts / 2] : &DstTmpRegs[0], NumDstParts); 34170b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 34180b57cec5SDimitry Andric MI.eraseFromParent(); 34190b57cec5SDimitry Andric return Legalized; 34200b57cec5SDimitry Andric } 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 34230b57cec5SDimitry Andric LegalizerHelper::narrowScalarExtract(MachineInstr &MI, unsigned TypeIdx, 34240b57cec5SDimitry Andric LLT NarrowTy) { 34250b57cec5SDimitry Andric if (TypeIdx != 1) 34260b57cec5SDimitry Andric return UnableToLegalize; 34270b57cec5SDimitry Andric 34280b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 34290b57cec5SDimitry Andric 34300b57cec5SDimitry Andric int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 34310b57cec5SDimitry Andric // FIXME: add support for when SizeOp1 isn't an exact multiple of 34320b57cec5SDimitry Andric // NarrowSize. 34330b57cec5SDimitry Andric if (SizeOp1 % NarrowSize != 0) 34340b57cec5SDimitry Andric return UnableToLegalize; 34350b57cec5SDimitry Andric int NumParts = SizeOp1 / NarrowSize; 34360b57cec5SDimitry Andric 34370b57cec5SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 34380b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 34390b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 34400b57cec5SDimitry Andric 34410b57cec5SDimitry Andric Register OpReg = MI.getOperand(0).getReg(); 34420b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(2).getImm(); 34430b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 34440b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 34450b57cec5SDimitry Andric unsigned SrcStart = i * NarrowSize; 34460b57cec5SDimitry Andric 34470b57cec5SDimitry Andric if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) { 34480b57cec5SDimitry Andric // No part of the extract uses this subregister, ignore it. 34490b57cec5SDimitry Andric continue; 34500b57cec5SDimitry Andric } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 34510b57cec5SDimitry Andric // The entire subregister is extracted, forward the value. 34520b57cec5SDimitry Andric DstRegs.push_back(SrcRegs[i]); 34530b57cec5SDimitry Andric continue; 34540b57cec5SDimitry Andric } 34550b57cec5SDimitry Andric 34560b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 34570b57cec5SDimitry Andric // extended infinitely in both directions. 34580b57cec5SDimitry Andric int64_t ExtractOffset; 34590b57cec5SDimitry Andric uint64_t SegSize; 34600b57cec5SDimitry Andric if (OpStart < SrcStart) { 34610b57cec5SDimitry Andric ExtractOffset = 0; 34620b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart); 34630b57cec5SDimitry Andric } else { 34640b57cec5SDimitry Andric ExtractOffset = OpStart - SrcStart; 34650b57cec5SDimitry Andric SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize); 34660b57cec5SDimitry Andric } 34670b57cec5SDimitry Andric 34680b57cec5SDimitry Andric Register SegReg = SrcRegs[i]; 34690b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != NarrowSize) { 34700b57cec5SDimitry Andric // A genuine extract is needed. 34710b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 34720b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset); 34730b57cec5SDimitry Andric } 34740b57cec5SDimitry Andric 34750b57cec5SDimitry Andric DstRegs.push_back(SegReg); 34760b57cec5SDimitry Andric } 34770b57cec5SDimitry Andric 34780b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 34790b57cec5SDimitry Andric if(MRI.getType(DstReg).isVector()) 34800b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 34810b57cec5SDimitry Andric else 34820b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 34830b57cec5SDimitry Andric MI.eraseFromParent(); 34840b57cec5SDimitry Andric return Legalized; 34850b57cec5SDimitry Andric } 34860b57cec5SDimitry Andric 34870b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 34880b57cec5SDimitry Andric LegalizerHelper::narrowScalarInsert(MachineInstr &MI, unsigned TypeIdx, 34890b57cec5SDimitry Andric LLT NarrowTy) { 34900b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 34910b57cec5SDimitry Andric if (TypeIdx != 0) 34920b57cec5SDimitry Andric return UnableToLegalize; 34930b57cec5SDimitry Andric 34940b57cec5SDimitry Andric uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 34950b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 34960b57cec5SDimitry Andric 34970b57cec5SDimitry Andric // FIXME: add support for when SizeOp0 isn't an exact multiple of 34980b57cec5SDimitry Andric // NarrowSize. 34990b57cec5SDimitry Andric if (SizeOp0 % NarrowSize != 0) 35000b57cec5SDimitry Andric return UnableToLegalize; 35010b57cec5SDimitry Andric 35020b57cec5SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 35030b57cec5SDimitry Andric 35040b57cec5SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 35050b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 35060b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 35070b57cec5SDimitry Andric 35080b57cec5SDimitry Andric Register OpReg = MI.getOperand(2).getReg(); 35090b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(3).getImm(); 35100b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 35110b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 35120b57cec5SDimitry Andric unsigned DstStart = i * NarrowSize; 35130b57cec5SDimitry Andric 35140b57cec5SDimitry Andric if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) { 35150b57cec5SDimitry Andric // No part of the insert affects this subregister, forward the original. 35160b57cec5SDimitry Andric DstRegs.push_back(SrcRegs[i]); 35170b57cec5SDimitry Andric continue; 35180b57cec5SDimitry Andric } else if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 35190b57cec5SDimitry Andric // The entire subregister is defined by this insert, forward the new 35200b57cec5SDimitry Andric // value. 35210b57cec5SDimitry Andric DstRegs.push_back(OpReg); 35220b57cec5SDimitry Andric continue; 35230b57cec5SDimitry Andric } 35240b57cec5SDimitry Andric 35250b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 35260b57cec5SDimitry Andric // extended infinitely in both directions. 35270b57cec5SDimitry Andric int64_t ExtractOffset, InsertOffset; 35280b57cec5SDimitry Andric uint64_t SegSize; 35290b57cec5SDimitry Andric if (OpStart < DstStart) { 35300b57cec5SDimitry Andric InsertOffset = 0; 35310b57cec5SDimitry Andric ExtractOffset = DstStart - OpStart; 35320b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart); 35330b57cec5SDimitry Andric } else { 35340b57cec5SDimitry Andric InsertOffset = OpStart - DstStart; 35350b57cec5SDimitry Andric ExtractOffset = 0; 35360b57cec5SDimitry Andric SegSize = 35370b57cec5SDimitry Andric std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart); 35380b57cec5SDimitry Andric } 35390b57cec5SDimitry Andric 35400b57cec5SDimitry Andric Register SegReg = OpReg; 35410b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != OpSize) { 35420b57cec5SDimitry Andric // A genuine extract is needed. 35430b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 35440b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset); 35450b57cec5SDimitry Andric } 35460b57cec5SDimitry Andric 35470b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy); 35480b57cec5SDimitry Andric MIRBuilder.buildInsert(DstReg, SrcRegs[i], SegReg, InsertOffset); 35490b57cec5SDimitry Andric DstRegs.push_back(DstReg); 35500b57cec5SDimitry Andric } 35510b57cec5SDimitry Andric 35520b57cec5SDimitry Andric assert(DstRegs.size() == (unsigned)NumParts && "not all parts covered"); 35530b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 35540b57cec5SDimitry Andric if(MRI.getType(DstReg).isVector()) 35550b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 35560b57cec5SDimitry Andric else 35570b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 35580b57cec5SDimitry Andric MI.eraseFromParent(); 35590b57cec5SDimitry Andric return Legalized; 35600b57cec5SDimitry Andric } 35610b57cec5SDimitry Andric 35620b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 35630b57cec5SDimitry Andric LegalizerHelper::narrowScalarBasic(MachineInstr &MI, unsigned TypeIdx, 35640b57cec5SDimitry Andric LLT NarrowTy) { 35650b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 35660b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 35670b57cec5SDimitry Andric 35680b57cec5SDimitry Andric assert(MI.getNumOperands() == 3 && TypeIdx == 0); 35690b57cec5SDimitry Andric 35700b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 35710b57cec5SDimitry Andric SmallVector<Register, 4> Src0Regs, Src0LeftoverRegs; 35720b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 35730b57cec5SDimitry Andric LLT LeftoverTy; 35740b57cec5SDimitry Andric if (!extractParts(MI.getOperand(1).getReg(), DstTy, NarrowTy, LeftoverTy, 35750b57cec5SDimitry Andric Src0Regs, Src0LeftoverRegs)) 35760b57cec5SDimitry Andric return UnableToLegalize; 35770b57cec5SDimitry Andric 35780b57cec5SDimitry Andric LLT Unused; 35790b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, Unused, 35800b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 35810b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 35820b57cec5SDimitry Andric 35830b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 35840b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, 35850b57cec5SDimitry Andric {Src0Regs[I], Src1Regs[I]}); 35860b57cec5SDimitry Andric DstRegs.push_back(Inst->getOperand(0).getReg()); 35870b57cec5SDimitry Andric } 35880b57cec5SDimitry Andric 35890b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 35900b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr( 35910b57cec5SDimitry Andric MI.getOpcode(), 35920b57cec5SDimitry Andric {LeftoverTy}, {Src0LeftoverRegs[I], Src1LeftoverRegs[I]}); 35930b57cec5SDimitry Andric DstLeftoverRegs.push_back(Inst->getOperand(0).getReg()); 35940b57cec5SDimitry Andric } 35950b57cec5SDimitry Andric 35960b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 35970b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 35980b57cec5SDimitry Andric 35990b57cec5SDimitry Andric MI.eraseFromParent(); 36000b57cec5SDimitry Andric return Legalized; 36010b57cec5SDimitry Andric } 36020b57cec5SDimitry Andric 36030b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 36040b57cec5SDimitry Andric LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx, 36050b57cec5SDimitry Andric LLT NarrowTy) { 36060b57cec5SDimitry Andric if (TypeIdx != 0) 36070b57cec5SDimitry Andric return UnableToLegalize; 36080b57cec5SDimitry Andric 36090b57cec5SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 36100b57cec5SDimitry Andric LLT CondTy = MRI.getType(CondReg); 36110b57cec5SDimitry Andric if (CondTy.isVector()) // TODO: Handle vselect 36120b57cec5SDimitry Andric return UnableToLegalize; 36130b57cec5SDimitry Andric 36140b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 36150b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 36160b57cec5SDimitry Andric 36170b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 36180b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 36190b57cec5SDimitry Andric SmallVector<Register, 4> Src2Regs, Src2LeftoverRegs; 36200b57cec5SDimitry Andric LLT LeftoverTy; 36210b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy, 36220b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 36230b57cec5SDimitry Andric return UnableToLegalize; 36240b57cec5SDimitry Andric 36250b57cec5SDimitry Andric LLT Unused; 36260b57cec5SDimitry Andric if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused, 36270b57cec5SDimitry Andric Src2Regs, Src2LeftoverRegs)) 36280b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 36290b57cec5SDimitry Andric 36300b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 36310b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect(NarrowTy, 36320b57cec5SDimitry Andric CondReg, Src1Regs[I], Src2Regs[I]); 36330b57cec5SDimitry Andric DstRegs.push_back(Select->getOperand(0).getReg()); 36340b57cec5SDimitry Andric } 36350b57cec5SDimitry Andric 36360b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 36370b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect( 36380b57cec5SDimitry Andric LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]); 36390b57cec5SDimitry Andric DstLeftoverRegs.push_back(Select->getOperand(0).getReg()); 36400b57cec5SDimitry Andric } 36410b57cec5SDimitry Andric 36420b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 36430b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 36440b57cec5SDimitry Andric 36450b57cec5SDimitry Andric MI.eraseFromParent(); 36460b57cec5SDimitry Andric return Legalized; 36470b57cec5SDimitry Andric } 36480b57cec5SDimitry Andric 36490b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 36500b57cec5SDimitry Andric LegalizerHelper::lowerBitCount(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 36510b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 36520b57cec5SDimitry Andric auto &TII = *MI.getMF()->getSubtarget().getInstrInfo(); 36530b57cec5SDimitry Andric auto isSupported = [this](const LegalityQuery &Q) { 36540b57cec5SDimitry Andric auto QAction = LI.getAction(Q).Action; 36550b57cec5SDimitry Andric return QAction == Legal || QAction == Libcall || QAction == Custom; 36560b57cec5SDimitry Andric }; 36570b57cec5SDimitry Andric switch (Opc) { 36580b57cec5SDimitry Andric default: 36590b57cec5SDimitry Andric return UnableToLegalize; 36600b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: { 36610b57cec5SDimitry Andric // This trivially expands to CTLZ. 36620b57cec5SDimitry Andric Observer.changingInstr(MI); 36630b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTLZ)); 36640b57cec5SDimitry Andric Observer.changedInstr(MI); 36650b57cec5SDimitry Andric return Legalized; 36660b57cec5SDimitry Andric } 36670b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: { 36680b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 36690b57cec5SDimitry Andric unsigned Len = Ty.getSizeInBits(); 36700b57cec5SDimitry Andric if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {Ty, Ty}})) { 36710b57cec5SDimitry Andric // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero. 36720b57cec5SDimitry Andric auto MIBCtlzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF, 36730b57cec5SDimitry Andric {Ty}, {SrcReg}); 36740b57cec5SDimitry Andric auto MIBZero = MIRBuilder.buildConstant(Ty, 0); 36750b57cec5SDimitry Andric auto MIBLen = MIRBuilder.buildConstant(Ty, Len); 36760b57cec5SDimitry Andric auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 36770b57cec5SDimitry Andric SrcReg, MIBZero); 36780b57cec5SDimitry Andric MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen, 36790b57cec5SDimitry Andric MIBCtlzZU); 36800b57cec5SDimitry Andric MI.eraseFromParent(); 36810b57cec5SDimitry Andric return Legalized; 36820b57cec5SDimitry Andric } 36830b57cec5SDimitry Andric // for now, we do this: 36840b57cec5SDimitry Andric // NewLen = NextPowerOf2(Len); 36850b57cec5SDimitry Andric // x = x | (x >> 1); 36860b57cec5SDimitry Andric // x = x | (x >> 2); 36870b57cec5SDimitry Andric // ... 36880b57cec5SDimitry Andric // x = x | (x >>16); 36890b57cec5SDimitry Andric // x = x | (x >>32); // for 64-bit input 36900b57cec5SDimitry Andric // Upto NewLen/2 36910b57cec5SDimitry Andric // return Len - popcount(x); 36920b57cec5SDimitry Andric // 36930b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 36940b57cec5SDimitry Andric Register Op = SrcReg; 36950b57cec5SDimitry Andric unsigned NewLen = PowerOf2Ceil(Len); 36960b57cec5SDimitry Andric for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) { 36970b57cec5SDimitry Andric auto MIBShiftAmt = MIRBuilder.buildConstant(Ty, 1ULL << i); 36980b57cec5SDimitry Andric auto MIBOp = MIRBuilder.buildInstr( 36990b57cec5SDimitry Andric TargetOpcode::G_OR, {Ty}, 37000b57cec5SDimitry Andric {Op, MIRBuilder.buildInstr(TargetOpcode::G_LSHR, {Ty}, 37010b57cec5SDimitry Andric {Op, MIBShiftAmt})}); 37020b57cec5SDimitry Andric Op = MIBOp->getOperand(0).getReg(); 37030b57cec5SDimitry Andric } 37040b57cec5SDimitry Andric auto MIBPop = MIRBuilder.buildInstr(TargetOpcode::G_CTPOP, {Ty}, {Op}); 37050b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_SUB, {MI.getOperand(0).getReg()}, 37060b57cec5SDimitry Andric {MIRBuilder.buildConstant(Ty, Len), MIBPop}); 37070b57cec5SDimitry Andric MI.eraseFromParent(); 37080b57cec5SDimitry Andric return Legalized; 37090b57cec5SDimitry Andric } 37100b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: { 37110b57cec5SDimitry Andric // This trivially expands to CTTZ. 37120b57cec5SDimitry Andric Observer.changingInstr(MI); 37130b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTTZ)); 37140b57cec5SDimitry Andric Observer.changedInstr(MI); 37150b57cec5SDimitry Andric return Legalized; 37160b57cec5SDimitry Andric } 37170b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: { 37180b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 37190b57cec5SDimitry Andric unsigned Len = Ty.getSizeInBits(); 37200b57cec5SDimitry Andric if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {Ty, Ty}})) { 37210b57cec5SDimitry Andric // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with 37220b57cec5SDimitry Andric // zero. 37230b57cec5SDimitry Andric auto MIBCttzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF, 37240b57cec5SDimitry Andric {Ty}, {SrcReg}); 37250b57cec5SDimitry Andric auto MIBZero = MIRBuilder.buildConstant(Ty, 0); 37260b57cec5SDimitry Andric auto MIBLen = MIRBuilder.buildConstant(Ty, Len); 37270b57cec5SDimitry Andric auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 37280b57cec5SDimitry Andric SrcReg, MIBZero); 37290b57cec5SDimitry Andric MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen, 37300b57cec5SDimitry Andric MIBCttzZU); 37310b57cec5SDimitry Andric MI.eraseFromParent(); 37320b57cec5SDimitry Andric return Legalized; 37330b57cec5SDimitry Andric } 37340b57cec5SDimitry Andric // for now, we use: { return popcount(~x & (x - 1)); } 37350b57cec5SDimitry Andric // unless the target has ctlz but not ctpop, in which case we use: 37360b57cec5SDimitry Andric // { return 32 - nlz(~x & (x-1)); } 37370b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 37380b57cec5SDimitry Andric auto MIBCstNeg1 = MIRBuilder.buildConstant(Ty, -1); 37390b57cec5SDimitry Andric auto MIBNot = 37400b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_XOR, {Ty}, {SrcReg, MIBCstNeg1}); 37410b57cec5SDimitry Andric auto MIBTmp = MIRBuilder.buildInstr( 37420b57cec5SDimitry Andric TargetOpcode::G_AND, {Ty}, 37430b57cec5SDimitry Andric {MIBNot, MIRBuilder.buildInstr(TargetOpcode::G_ADD, {Ty}, 37440b57cec5SDimitry Andric {SrcReg, MIBCstNeg1})}); 37450b57cec5SDimitry Andric if (!isSupported({TargetOpcode::G_CTPOP, {Ty, Ty}}) && 37460b57cec5SDimitry Andric isSupported({TargetOpcode::G_CTLZ, {Ty, Ty}})) { 37470b57cec5SDimitry Andric auto MIBCstLen = MIRBuilder.buildConstant(Ty, Len); 37480b57cec5SDimitry Andric MIRBuilder.buildInstr( 37490b57cec5SDimitry Andric TargetOpcode::G_SUB, {MI.getOperand(0).getReg()}, 37500b57cec5SDimitry Andric {MIBCstLen, 37510b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_CTLZ, {Ty}, {MIBTmp})}); 37520b57cec5SDimitry Andric MI.eraseFromParent(); 37530b57cec5SDimitry Andric return Legalized; 37540b57cec5SDimitry Andric } 37550b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTPOP)); 37560b57cec5SDimitry Andric MI.getOperand(1).setReg(MIBTmp->getOperand(0).getReg()); 37570b57cec5SDimitry Andric return Legalized; 37580b57cec5SDimitry Andric } 37590b57cec5SDimitry Andric } 37600b57cec5SDimitry Andric } 37610b57cec5SDimitry Andric 37620b57cec5SDimitry Andric // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float 37630b57cec5SDimitry Andric // representation. 37640b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 37650b57cec5SDimitry Andric LegalizerHelper::lowerU64ToF32BitOps(MachineInstr &MI) { 37660b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 37670b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 37680b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 37690b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 37700b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 37710b57cec5SDimitry Andric 37720b57cec5SDimitry Andric assert(MRI.getType(Src) == S64 && MRI.getType(Dst) == S32); 37730b57cec5SDimitry Andric 37740b57cec5SDimitry Andric // unsigned cul2f(ulong u) { 37750b57cec5SDimitry Andric // uint lz = clz(u); 37760b57cec5SDimitry Andric // uint e = (u != 0) ? 127U + 63U - lz : 0; 37770b57cec5SDimitry Andric // u = (u << lz) & 0x7fffffffffffffffUL; 37780b57cec5SDimitry Andric // ulong t = u & 0xffffffffffUL; 37790b57cec5SDimitry Andric // uint v = (e << 23) | (uint)(u >> 40); 37800b57cec5SDimitry Andric // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 37810b57cec5SDimitry Andric // return as_float(v + r); 37820b57cec5SDimitry Andric // } 37830b57cec5SDimitry Andric 37840b57cec5SDimitry Andric auto Zero32 = MIRBuilder.buildConstant(S32, 0); 37850b57cec5SDimitry Andric auto Zero64 = MIRBuilder.buildConstant(S64, 0); 37860b57cec5SDimitry Andric 37870b57cec5SDimitry Andric auto LZ = MIRBuilder.buildCTLZ_ZERO_UNDEF(S32, Src); 37880b57cec5SDimitry Andric 37890b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant(S32, 127U + 63U); 37900b57cec5SDimitry Andric auto Sub = MIRBuilder.buildSub(S32, K, LZ); 37910b57cec5SDimitry Andric 37920b57cec5SDimitry Andric auto NotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, Src, Zero64); 37930b57cec5SDimitry Andric auto E = MIRBuilder.buildSelect(S32, NotZero, Sub, Zero32); 37940b57cec5SDimitry Andric 37950b57cec5SDimitry Andric auto Mask0 = MIRBuilder.buildConstant(S64, (-1ULL) >> 1); 37960b57cec5SDimitry Andric auto ShlLZ = MIRBuilder.buildShl(S64, Src, LZ); 37970b57cec5SDimitry Andric 37980b57cec5SDimitry Andric auto U = MIRBuilder.buildAnd(S64, ShlLZ, Mask0); 37990b57cec5SDimitry Andric 38000b57cec5SDimitry Andric auto Mask1 = MIRBuilder.buildConstant(S64, 0xffffffffffULL); 38010b57cec5SDimitry Andric auto T = MIRBuilder.buildAnd(S64, U, Mask1); 38020b57cec5SDimitry Andric 38030b57cec5SDimitry Andric auto UShl = MIRBuilder.buildLShr(S64, U, MIRBuilder.buildConstant(S64, 40)); 38040b57cec5SDimitry Andric auto ShlE = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 23)); 38050b57cec5SDimitry Andric auto V = MIRBuilder.buildOr(S32, ShlE, MIRBuilder.buildTrunc(S32, UShl)); 38060b57cec5SDimitry Andric 38070b57cec5SDimitry Andric auto C = MIRBuilder.buildConstant(S64, 0x8000000000ULL); 38080b57cec5SDimitry Andric auto RCmp = MIRBuilder.buildICmp(CmpInst::ICMP_UGT, S1, T, C); 38090b57cec5SDimitry Andric auto TCmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, T, C); 38100b57cec5SDimitry Andric auto One = MIRBuilder.buildConstant(S32, 1); 38110b57cec5SDimitry Andric 38120b57cec5SDimitry Andric auto VTrunc1 = MIRBuilder.buildAnd(S32, V, One); 38130b57cec5SDimitry Andric auto Select0 = MIRBuilder.buildSelect(S32, TCmp, VTrunc1, Zero32); 38140b57cec5SDimitry Andric auto R = MIRBuilder.buildSelect(S32, RCmp, One, Select0); 38150b57cec5SDimitry Andric MIRBuilder.buildAdd(Dst, V, R); 38160b57cec5SDimitry Andric 38170b57cec5SDimitry Andric return Legalized; 38180b57cec5SDimitry Andric } 38190b57cec5SDimitry Andric 38200b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 38210b57cec5SDimitry Andric LegalizerHelper::lowerUITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 38220b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 38230b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 38240b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 38250b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 38260b57cec5SDimitry Andric 38270b57cec5SDimitry Andric if (SrcTy != LLT::scalar(64)) 38280b57cec5SDimitry Andric return UnableToLegalize; 38290b57cec5SDimitry Andric 38300b57cec5SDimitry Andric if (DstTy == LLT::scalar(32)) { 38310b57cec5SDimitry Andric // TODO: SelectionDAG has several alternative expansions to port which may 38320b57cec5SDimitry Andric // be more reasonble depending on the available instructions. If a target 38330b57cec5SDimitry Andric // has sitofp, does not have CTLZ, or can efficiently use f64 as an 38340b57cec5SDimitry Andric // intermediate type, this is probably worse. 38350b57cec5SDimitry Andric return lowerU64ToF32BitOps(MI); 38360b57cec5SDimitry Andric } 38370b57cec5SDimitry Andric 38380b57cec5SDimitry Andric return UnableToLegalize; 38390b57cec5SDimitry Andric } 38400b57cec5SDimitry Andric 38410b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 38420b57cec5SDimitry Andric LegalizerHelper::lowerSITOFP(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 38430b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 38440b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 38450b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 38460b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 38470b57cec5SDimitry Andric 38480b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 38490b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 38500b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 38510b57cec5SDimitry Andric 38520b57cec5SDimitry Andric if (SrcTy != S64) 38530b57cec5SDimitry Andric return UnableToLegalize; 38540b57cec5SDimitry Andric 38550b57cec5SDimitry Andric if (DstTy == S32) { 38560b57cec5SDimitry Andric // signed cl2f(long l) { 38570b57cec5SDimitry Andric // long s = l >> 63; 38580b57cec5SDimitry Andric // float r = cul2f((l + s) ^ s); 38590b57cec5SDimitry Andric // return s ? -r : r; 38600b57cec5SDimitry Andric // } 38610b57cec5SDimitry Andric Register L = Src; 38620b57cec5SDimitry Andric auto SignBit = MIRBuilder.buildConstant(S64, 63); 38630b57cec5SDimitry Andric auto S = MIRBuilder.buildAShr(S64, L, SignBit); 38640b57cec5SDimitry Andric 38650b57cec5SDimitry Andric auto LPlusS = MIRBuilder.buildAdd(S64, L, S); 38660b57cec5SDimitry Andric auto Xor = MIRBuilder.buildXor(S64, LPlusS, S); 38670b57cec5SDimitry Andric auto R = MIRBuilder.buildUITOFP(S32, Xor); 38680b57cec5SDimitry Andric 38690b57cec5SDimitry Andric auto RNeg = MIRBuilder.buildFNeg(S32, R); 38700b57cec5SDimitry Andric auto SignNotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, S, 38710b57cec5SDimitry Andric MIRBuilder.buildConstant(S64, 0)); 38720b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, SignNotZero, RNeg, R); 38730b57cec5SDimitry Andric return Legalized; 38740b57cec5SDimitry Andric } 38750b57cec5SDimitry Andric 38760b57cec5SDimitry Andric return UnableToLegalize; 38770b57cec5SDimitry Andric } 38780b57cec5SDimitry Andric 3879*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 3880*8bcb0991SDimitry Andric LegalizerHelper::lowerFPTOUI(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 3881*8bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 3882*8bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 3883*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 3884*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 3885*8bcb0991SDimitry Andric const LLT S64 = LLT::scalar(64); 3886*8bcb0991SDimitry Andric const LLT S32 = LLT::scalar(32); 3887*8bcb0991SDimitry Andric 3888*8bcb0991SDimitry Andric if (SrcTy != S64 && SrcTy != S32) 3889*8bcb0991SDimitry Andric return UnableToLegalize; 3890*8bcb0991SDimitry Andric if (DstTy != S32 && DstTy != S64) 3891*8bcb0991SDimitry Andric return UnableToLegalize; 3892*8bcb0991SDimitry Andric 3893*8bcb0991SDimitry Andric // FPTOSI gives same result as FPTOUI for positive signed integers. 3894*8bcb0991SDimitry Andric // FPTOUI needs to deal with fp values that convert to unsigned integers 3895*8bcb0991SDimitry Andric // greater or equal to 2^31 for float or 2^63 for double. For brevity 2^Exp. 3896*8bcb0991SDimitry Andric 3897*8bcb0991SDimitry Andric APInt TwoPExpInt = APInt::getSignMask(DstTy.getSizeInBits()); 3898*8bcb0991SDimitry Andric APFloat TwoPExpFP(SrcTy.getSizeInBits() == 32 ? APFloat::IEEEsingle() 3899*8bcb0991SDimitry Andric : APFloat::IEEEdouble(), 3900*8bcb0991SDimitry Andric APInt::getNullValue(SrcTy.getSizeInBits())); 3901*8bcb0991SDimitry Andric TwoPExpFP.convertFromAPInt(TwoPExpInt, false, APFloat::rmNearestTiesToEven); 3902*8bcb0991SDimitry Andric 3903*8bcb0991SDimitry Andric MachineInstrBuilder FPTOSI = MIRBuilder.buildFPTOSI(DstTy, Src); 3904*8bcb0991SDimitry Andric 3905*8bcb0991SDimitry Andric MachineInstrBuilder Threshold = MIRBuilder.buildFConstant(SrcTy, TwoPExpFP); 3906*8bcb0991SDimitry Andric // For fp Value greater or equal to Threshold(2^Exp), we use FPTOSI on 3907*8bcb0991SDimitry Andric // (Value - 2^Exp) and add 2^Exp by setting highest bit in result to 1. 3908*8bcb0991SDimitry Andric MachineInstrBuilder FSub = MIRBuilder.buildFSub(SrcTy, Src, Threshold); 3909*8bcb0991SDimitry Andric MachineInstrBuilder ResLowBits = MIRBuilder.buildFPTOSI(DstTy, FSub); 3910*8bcb0991SDimitry Andric MachineInstrBuilder ResHighBit = MIRBuilder.buildConstant(DstTy, TwoPExpInt); 3911*8bcb0991SDimitry Andric MachineInstrBuilder Res = MIRBuilder.buildXor(DstTy, ResLowBits, ResHighBit); 3912*8bcb0991SDimitry Andric 3913*8bcb0991SDimitry Andric MachineInstrBuilder FCMP = 3914*8bcb0991SDimitry Andric MIRBuilder.buildFCmp(CmpInst::FCMP_ULT, DstTy, Src, Threshold); 3915*8bcb0991SDimitry Andric MIRBuilder.buildSelect(Dst, FCMP, FPTOSI, Res); 3916*8bcb0991SDimitry Andric 3917*8bcb0991SDimitry Andric MI.eraseFromParent(); 3918*8bcb0991SDimitry Andric return Legalized; 3919*8bcb0991SDimitry Andric } 3920*8bcb0991SDimitry Andric 39210b57cec5SDimitry Andric static CmpInst::Predicate minMaxToCompare(unsigned Opc) { 39220b57cec5SDimitry Andric switch (Opc) { 39230b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 39240b57cec5SDimitry Andric return CmpInst::ICMP_SLT; 39250b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 39260b57cec5SDimitry Andric return CmpInst::ICMP_SGT; 39270b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 39280b57cec5SDimitry Andric return CmpInst::ICMP_ULT; 39290b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 39300b57cec5SDimitry Andric return CmpInst::ICMP_UGT; 39310b57cec5SDimitry Andric default: 39320b57cec5SDimitry Andric llvm_unreachable("not in integer min/max"); 39330b57cec5SDimitry Andric } 39340b57cec5SDimitry Andric } 39350b57cec5SDimitry Andric 39360b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 39370b57cec5SDimitry Andric LegalizerHelper::lowerMinMax(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 39380b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 39390b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 39400b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 39410b57cec5SDimitry Andric 39420b57cec5SDimitry Andric const CmpInst::Predicate Pred = minMaxToCompare(MI.getOpcode()); 39430b57cec5SDimitry Andric LLT CmpType = MRI.getType(Dst).changeElementSize(1); 39440b57cec5SDimitry Andric 39450b57cec5SDimitry Andric auto Cmp = MIRBuilder.buildICmp(Pred, CmpType, Src0, Src1); 39460b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, Cmp, Src0, Src1); 39470b57cec5SDimitry Andric 39480b57cec5SDimitry Andric MI.eraseFromParent(); 39490b57cec5SDimitry Andric return Legalized; 39500b57cec5SDimitry Andric } 39510b57cec5SDimitry Andric 39520b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 39530b57cec5SDimitry Andric LegalizerHelper::lowerFCopySign(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { 39540b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 39550b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 39560b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 39570b57cec5SDimitry Andric 39580b57cec5SDimitry Andric const LLT Src0Ty = MRI.getType(Src0); 39590b57cec5SDimitry Andric const LLT Src1Ty = MRI.getType(Src1); 39600b57cec5SDimitry Andric 39610b57cec5SDimitry Andric const int Src0Size = Src0Ty.getScalarSizeInBits(); 39620b57cec5SDimitry Andric const int Src1Size = Src1Ty.getScalarSizeInBits(); 39630b57cec5SDimitry Andric 39640b57cec5SDimitry Andric auto SignBitMask = MIRBuilder.buildConstant( 39650b57cec5SDimitry Andric Src0Ty, APInt::getSignMask(Src0Size)); 39660b57cec5SDimitry Andric 39670b57cec5SDimitry Andric auto NotSignBitMask = MIRBuilder.buildConstant( 39680b57cec5SDimitry Andric Src0Ty, APInt::getLowBitsSet(Src0Size, Src0Size - 1)); 39690b57cec5SDimitry Andric 39700b57cec5SDimitry Andric auto And0 = MIRBuilder.buildAnd(Src0Ty, Src0, NotSignBitMask); 39710b57cec5SDimitry Andric MachineInstr *Or; 39720b57cec5SDimitry Andric 39730b57cec5SDimitry Andric if (Src0Ty == Src1Ty) { 39740b57cec5SDimitry Andric auto And1 = MIRBuilder.buildAnd(Src1Ty, Src0, SignBitMask); 39750b57cec5SDimitry Andric Or = MIRBuilder.buildOr(Dst, And0, And1); 39760b57cec5SDimitry Andric } else if (Src0Size > Src1Size) { 39770b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src0Ty, Src0Size - Src1Size); 39780b57cec5SDimitry Andric auto Zext = MIRBuilder.buildZExt(Src0Ty, Src1); 39790b57cec5SDimitry Andric auto Shift = MIRBuilder.buildShl(Src0Ty, Zext, ShiftAmt); 39800b57cec5SDimitry Andric auto And1 = MIRBuilder.buildAnd(Src0Ty, Shift, SignBitMask); 39810b57cec5SDimitry Andric Or = MIRBuilder.buildOr(Dst, And0, And1); 39820b57cec5SDimitry Andric } else { 39830b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src1Ty, Src1Size - Src0Size); 39840b57cec5SDimitry Andric auto Shift = MIRBuilder.buildLShr(Src1Ty, Src1, ShiftAmt); 39850b57cec5SDimitry Andric auto Trunc = MIRBuilder.buildTrunc(Src0Ty, Shift); 39860b57cec5SDimitry Andric auto And1 = MIRBuilder.buildAnd(Src0Ty, Trunc, SignBitMask); 39870b57cec5SDimitry Andric Or = MIRBuilder.buildOr(Dst, And0, And1); 39880b57cec5SDimitry Andric } 39890b57cec5SDimitry Andric 39900b57cec5SDimitry Andric // Be careful about setting nsz/nnan/ninf on every instruction, since the 39910b57cec5SDimitry Andric // constants are a nan and -0.0, but the final result should preserve 39920b57cec5SDimitry Andric // everything. 39930b57cec5SDimitry Andric if (unsigned Flags = MI.getFlags()) 39940b57cec5SDimitry Andric Or->setFlags(Flags); 39950b57cec5SDimitry Andric 39960b57cec5SDimitry Andric MI.eraseFromParent(); 39970b57cec5SDimitry Andric return Legalized; 39980b57cec5SDimitry Andric } 39990b57cec5SDimitry Andric 40000b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 40010b57cec5SDimitry Andric LegalizerHelper::lowerFMinNumMaxNum(MachineInstr &MI) { 40020b57cec5SDimitry Andric unsigned NewOp = MI.getOpcode() == TargetOpcode::G_FMINNUM ? 40030b57cec5SDimitry Andric TargetOpcode::G_FMINNUM_IEEE : TargetOpcode::G_FMAXNUM_IEEE; 40040b57cec5SDimitry Andric 40050b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 40060b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 40070b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 40080b57cec5SDimitry Andric LLT Ty = MRI.getType(Dst); 40090b57cec5SDimitry Andric 40100b57cec5SDimitry Andric if (!MI.getFlag(MachineInstr::FmNoNans)) { 40110b57cec5SDimitry Andric // Insert canonicalizes if it's possible we need to quiet to get correct 40120b57cec5SDimitry Andric // sNaN behavior. 40130b57cec5SDimitry Andric 40140b57cec5SDimitry Andric // Note this must be done here, and not as an optimization combine in the 40150b57cec5SDimitry Andric // absence of a dedicate quiet-snan instruction as we're using an 40160b57cec5SDimitry Andric // omni-purpose G_FCANONICALIZE. 40170b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src0, MRI)) 40180b57cec5SDimitry Andric Src0 = MIRBuilder.buildFCanonicalize(Ty, Src0, MI.getFlags()).getReg(0); 40190b57cec5SDimitry Andric 40200b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src1, MRI)) 40210b57cec5SDimitry Andric Src1 = MIRBuilder.buildFCanonicalize(Ty, Src1, MI.getFlags()).getReg(0); 40220b57cec5SDimitry Andric } 40230b57cec5SDimitry Andric 40240b57cec5SDimitry Andric // If there are no nans, it's safe to simply replace this with the non-IEEE 40250b57cec5SDimitry Andric // version. 40260b57cec5SDimitry Andric MIRBuilder.buildInstr(NewOp, {Dst}, {Src0, Src1}, MI.getFlags()); 40270b57cec5SDimitry Andric MI.eraseFromParent(); 40280b57cec5SDimitry Andric return Legalized; 40290b57cec5SDimitry Andric } 4030*8bcb0991SDimitry Andric 4031*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFMad(MachineInstr &MI) { 4032*8bcb0991SDimitry Andric // Expand G_FMAD a, b, c -> G_FADD (G_FMUL a, b), c 4033*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4034*8bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 4035*8bcb0991SDimitry Andric unsigned Flags = MI.getFlags(); 4036*8bcb0991SDimitry Andric 4037*8bcb0991SDimitry Andric auto Mul = MIRBuilder.buildFMul(Ty, MI.getOperand(1), MI.getOperand(2), 4038*8bcb0991SDimitry Andric Flags); 4039*8bcb0991SDimitry Andric MIRBuilder.buildFAdd(DstReg, Mul, MI.getOperand(3), Flags); 4040*8bcb0991SDimitry Andric MI.eraseFromParent(); 4041*8bcb0991SDimitry Andric return Legalized; 4042*8bcb0991SDimitry Andric } 4043*8bcb0991SDimitry Andric 4044*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4045*8bcb0991SDimitry Andric LegalizerHelper::lowerUnmergeValues(MachineInstr &MI) { 4046*8bcb0991SDimitry Andric const unsigned NumDst = MI.getNumOperands() - 1; 4047*8bcb0991SDimitry Andric const Register SrcReg = MI.getOperand(NumDst).getReg(); 4048*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 4049*8bcb0991SDimitry Andric 4050*8bcb0991SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 4051*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 4052*8bcb0991SDimitry Andric 4053*8bcb0991SDimitry Andric 4054*8bcb0991SDimitry Andric // Expand scalarizing unmerge as bitcast to integer and shift. 4055*8bcb0991SDimitry Andric if (!DstTy.isVector() && SrcTy.isVector() && 4056*8bcb0991SDimitry Andric SrcTy.getElementType() == DstTy) { 4057*8bcb0991SDimitry Andric LLT IntTy = LLT::scalar(SrcTy.getSizeInBits()); 4058*8bcb0991SDimitry Andric Register Cast = MIRBuilder.buildBitcast(IntTy, SrcReg).getReg(0); 4059*8bcb0991SDimitry Andric 4060*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst0Reg, Cast); 4061*8bcb0991SDimitry Andric 4062*8bcb0991SDimitry Andric const unsigned DstSize = DstTy.getSizeInBits(); 4063*8bcb0991SDimitry Andric unsigned Offset = DstSize; 4064*8bcb0991SDimitry Andric for (unsigned I = 1; I != NumDst; ++I, Offset += DstSize) { 4065*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntTy, Offset); 4066*8bcb0991SDimitry Andric auto Shift = MIRBuilder.buildLShr(IntTy, Cast, ShiftAmt); 4067*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(I), Shift); 4068*8bcb0991SDimitry Andric } 4069*8bcb0991SDimitry Andric 4070*8bcb0991SDimitry Andric MI.eraseFromParent(); 4071*8bcb0991SDimitry Andric return Legalized; 4072*8bcb0991SDimitry Andric } 4073*8bcb0991SDimitry Andric 4074*8bcb0991SDimitry Andric return UnableToLegalize; 4075*8bcb0991SDimitry Andric } 4076*8bcb0991SDimitry Andric 4077*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4078*8bcb0991SDimitry Andric LegalizerHelper::lowerShuffleVector(MachineInstr &MI) { 4079*8bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4080*8bcb0991SDimitry Andric Register Src0Reg = MI.getOperand(1).getReg(); 4081*8bcb0991SDimitry Andric Register Src1Reg = MI.getOperand(2).getReg(); 4082*8bcb0991SDimitry Andric LLT Src0Ty = MRI.getType(Src0Reg); 4083*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 4084*8bcb0991SDimitry Andric LLT IdxTy = LLT::scalar(32); 4085*8bcb0991SDimitry Andric 4086*8bcb0991SDimitry Andric const Constant *ShufMask = MI.getOperand(3).getShuffleMask(); 4087*8bcb0991SDimitry Andric 4088*8bcb0991SDimitry Andric SmallVector<int, 32> Mask; 4089*8bcb0991SDimitry Andric ShuffleVectorInst::getShuffleMask(ShufMask, Mask); 4090*8bcb0991SDimitry Andric 4091*8bcb0991SDimitry Andric if (DstTy.isScalar()) { 4092*8bcb0991SDimitry Andric if (Src0Ty.isVector()) 4093*8bcb0991SDimitry Andric return UnableToLegalize; 4094*8bcb0991SDimitry Andric 4095*8bcb0991SDimitry Andric // This is just a SELECT. 4096*8bcb0991SDimitry Andric assert(Mask.size() == 1 && "Expected a single mask element"); 4097*8bcb0991SDimitry Andric Register Val; 4098*8bcb0991SDimitry Andric if (Mask[0] < 0 || Mask[0] > 1) 4099*8bcb0991SDimitry Andric Val = MIRBuilder.buildUndef(DstTy).getReg(0); 4100*8bcb0991SDimitry Andric else 4101*8bcb0991SDimitry Andric Val = Mask[0] == 0 ? Src0Reg : Src1Reg; 4102*8bcb0991SDimitry Andric MIRBuilder.buildCopy(DstReg, Val); 4103*8bcb0991SDimitry Andric MI.eraseFromParent(); 4104*8bcb0991SDimitry Andric return Legalized; 4105*8bcb0991SDimitry Andric } 4106*8bcb0991SDimitry Andric 4107*8bcb0991SDimitry Andric Register Undef; 4108*8bcb0991SDimitry Andric SmallVector<Register, 32> BuildVec; 4109*8bcb0991SDimitry Andric LLT EltTy = DstTy.getElementType(); 4110*8bcb0991SDimitry Andric 4111*8bcb0991SDimitry Andric for (int Idx : Mask) { 4112*8bcb0991SDimitry Andric if (Idx < 0) { 4113*8bcb0991SDimitry Andric if (!Undef.isValid()) 4114*8bcb0991SDimitry Andric Undef = MIRBuilder.buildUndef(EltTy).getReg(0); 4115*8bcb0991SDimitry Andric BuildVec.push_back(Undef); 4116*8bcb0991SDimitry Andric continue; 4117*8bcb0991SDimitry Andric } 4118*8bcb0991SDimitry Andric 4119*8bcb0991SDimitry Andric if (Src0Ty.isScalar()) { 4120*8bcb0991SDimitry Andric BuildVec.push_back(Idx == 0 ? Src0Reg : Src1Reg); 4121*8bcb0991SDimitry Andric } else { 4122*8bcb0991SDimitry Andric int NumElts = Src0Ty.getNumElements(); 4123*8bcb0991SDimitry Andric Register SrcVec = Idx < NumElts ? Src0Reg : Src1Reg; 4124*8bcb0991SDimitry Andric int ExtractIdx = Idx < NumElts ? Idx : Idx - NumElts; 4125*8bcb0991SDimitry Andric auto IdxK = MIRBuilder.buildConstant(IdxTy, ExtractIdx); 4126*8bcb0991SDimitry Andric auto Extract = MIRBuilder.buildExtractVectorElement(EltTy, SrcVec, IdxK); 4127*8bcb0991SDimitry Andric BuildVec.push_back(Extract.getReg(0)); 4128*8bcb0991SDimitry Andric } 4129*8bcb0991SDimitry Andric } 4130*8bcb0991SDimitry Andric 4131*8bcb0991SDimitry Andric MIRBuilder.buildBuildVector(DstReg, BuildVec); 4132*8bcb0991SDimitry Andric MI.eraseFromParent(); 4133*8bcb0991SDimitry Andric return Legalized; 4134*8bcb0991SDimitry Andric } 4135*8bcb0991SDimitry Andric 4136*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4137*8bcb0991SDimitry Andric LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) { 4138*8bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 4139*8bcb0991SDimitry Andric Register AllocSize = MI.getOperand(1).getReg(); 4140*8bcb0991SDimitry Andric unsigned Align = MI.getOperand(2).getImm(); 4141*8bcb0991SDimitry Andric 4142*8bcb0991SDimitry Andric const auto &MF = *MI.getMF(); 4143*8bcb0991SDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 4144*8bcb0991SDimitry Andric 4145*8bcb0991SDimitry Andric LLT PtrTy = MRI.getType(Dst); 4146*8bcb0991SDimitry Andric LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits()); 4147*8bcb0991SDimitry Andric 4148*8bcb0991SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 4149*8bcb0991SDimitry Andric auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg); 4150*8bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp); 4151*8bcb0991SDimitry Andric 4152*8bcb0991SDimitry Andric // Subtract the final alloc from the SP. We use G_PTRTOINT here so we don't 4153*8bcb0991SDimitry Andric // have to generate an extra instruction to negate the alloc and then use 4154*8bcb0991SDimitry Andric // G_GEP to add the negative offset. 4155*8bcb0991SDimitry Andric auto Alloc = MIRBuilder.buildSub(IntPtrTy, SPTmp, AllocSize); 4156*8bcb0991SDimitry Andric if (Align) { 4157*8bcb0991SDimitry Andric APInt AlignMask(IntPtrTy.getSizeInBits(), Align, true); 4158*8bcb0991SDimitry Andric AlignMask.negate(); 4159*8bcb0991SDimitry Andric auto AlignCst = MIRBuilder.buildConstant(IntPtrTy, AlignMask); 4160*8bcb0991SDimitry Andric Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst); 4161*8bcb0991SDimitry Andric } 4162*8bcb0991SDimitry Andric 4163*8bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(PtrTy, Alloc); 4164*8bcb0991SDimitry Andric MIRBuilder.buildCopy(SPReg, SPTmp); 4165*8bcb0991SDimitry Andric MIRBuilder.buildCopy(Dst, SPTmp); 4166*8bcb0991SDimitry Andric 4167*8bcb0991SDimitry Andric MI.eraseFromParent(); 4168*8bcb0991SDimitry Andric return Legalized; 4169*8bcb0991SDimitry Andric } 4170*8bcb0991SDimitry Andric 4171*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4172*8bcb0991SDimitry Andric LegalizerHelper::lowerExtract(MachineInstr &MI) { 4173*8bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 4174*8bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 4175*8bcb0991SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 4176*8bcb0991SDimitry Andric 4177*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 4178*8bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 4179*8bcb0991SDimitry Andric 4180*8bcb0991SDimitry Andric if (DstTy.isScalar() && 4181*8bcb0991SDimitry Andric (SrcTy.isScalar() || 4182*8bcb0991SDimitry Andric (SrcTy.isVector() && DstTy == SrcTy.getElementType()))) { 4183*8bcb0991SDimitry Andric LLT SrcIntTy = SrcTy; 4184*8bcb0991SDimitry Andric if (!SrcTy.isScalar()) { 4185*8bcb0991SDimitry Andric SrcIntTy = LLT::scalar(SrcTy.getSizeInBits()); 4186*8bcb0991SDimitry Andric Src = MIRBuilder.buildBitcast(SrcIntTy, Src).getReg(0); 4187*8bcb0991SDimitry Andric } 4188*8bcb0991SDimitry Andric 4189*8bcb0991SDimitry Andric if (Offset == 0) 4190*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Src); 4191*8bcb0991SDimitry Andric else { 4192*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(SrcIntTy, Offset); 4193*8bcb0991SDimitry Andric auto Shr = MIRBuilder.buildLShr(SrcIntTy, Src, ShiftAmt); 4194*8bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Shr); 4195*8bcb0991SDimitry Andric } 4196*8bcb0991SDimitry Andric 4197*8bcb0991SDimitry Andric MI.eraseFromParent(); 4198*8bcb0991SDimitry Andric return Legalized; 4199*8bcb0991SDimitry Andric } 4200*8bcb0991SDimitry Andric 4201*8bcb0991SDimitry Andric return UnableToLegalize; 4202*8bcb0991SDimitry Andric } 4203*8bcb0991SDimitry Andric 4204*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerInsert(MachineInstr &MI) { 4205*8bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 4206*8bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 4207*8bcb0991SDimitry Andric Register InsertSrc = MI.getOperand(2).getReg(); 4208*8bcb0991SDimitry Andric uint64_t Offset = MI.getOperand(3).getImm(); 4209*8bcb0991SDimitry Andric 4210*8bcb0991SDimitry Andric LLT DstTy = MRI.getType(Src); 4211*8bcb0991SDimitry Andric LLT InsertTy = MRI.getType(InsertSrc); 4212*8bcb0991SDimitry Andric 4213*8bcb0991SDimitry Andric if (InsertTy.isScalar() && 4214*8bcb0991SDimitry Andric (DstTy.isScalar() || 4215*8bcb0991SDimitry Andric (DstTy.isVector() && DstTy.getElementType() == InsertTy))) { 4216*8bcb0991SDimitry Andric LLT IntDstTy = DstTy; 4217*8bcb0991SDimitry Andric if (!DstTy.isScalar()) { 4218*8bcb0991SDimitry Andric IntDstTy = LLT::scalar(DstTy.getSizeInBits()); 4219*8bcb0991SDimitry Andric Src = MIRBuilder.buildBitcast(IntDstTy, Src).getReg(0); 4220*8bcb0991SDimitry Andric } 4221*8bcb0991SDimitry Andric 4222*8bcb0991SDimitry Andric Register ExtInsSrc = MIRBuilder.buildZExt(IntDstTy, InsertSrc).getReg(0); 4223*8bcb0991SDimitry Andric if (Offset != 0) { 4224*8bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntDstTy, Offset); 4225*8bcb0991SDimitry Andric ExtInsSrc = MIRBuilder.buildShl(IntDstTy, ExtInsSrc, ShiftAmt).getReg(0); 4226*8bcb0991SDimitry Andric } 4227*8bcb0991SDimitry Andric 4228*8bcb0991SDimitry Andric APInt MaskVal = ~APInt::getBitsSet(DstTy.getSizeInBits(), Offset, 4229*8bcb0991SDimitry Andric InsertTy.getSizeInBits()); 4230*8bcb0991SDimitry Andric 4231*8bcb0991SDimitry Andric auto Mask = MIRBuilder.buildConstant(IntDstTy, MaskVal); 4232*8bcb0991SDimitry Andric auto MaskedSrc = MIRBuilder.buildAnd(IntDstTy, Src, Mask); 4233*8bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(IntDstTy, MaskedSrc, ExtInsSrc); 4234*8bcb0991SDimitry Andric 4235*8bcb0991SDimitry Andric MIRBuilder.buildBitcast(Dst, Or); 4236*8bcb0991SDimitry Andric MI.eraseFromParent(); 4237*8bcb0991SDimitry Andric return Legalized; 4238*8bcb0991SDimitry Andric } 4239*8bcb0991SDimitry Andric 4240*8bcb0991SDimitry Andric return UnableToLegalize; 4241*8bcb0991SDimitry Andric } 4242*8bcb0991SDimitry Andric 4243*8bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4244*8bcb0991SDimitry Andric LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) { 4245*8bcb0991SDimitry Andric Register Dst0 = MI.getOperand(0).getReg(); 4246*8bcb0991SDimitry Andric Register Dst1 = MI.getOperand(1).getReg(); 4247*8bcb0991SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 4248*8bcb0991SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 4249*8bcb0991SDimitry Andric const bool IsAdd = MI.getOpcode() == TargetOpcode::G_SADDO; 4250*8bcb0991SDimitry Andric 4251*8bcb0991SDimitry Andric LLT Ty = MRI.getType(Dst0); 4252*8bcb0991SDimitry Andric LLT BoolTy = MRI.getType(Dst1); 4253*8bcb0991SDimitry Andric 4254*8bcb0991SDimitry Andric if (IsAdd) 4255*8bcb0991SDimitry Andric MIRBuilder.buildAdd(Dst0, LHS, RHS); 4256*8bcb0991SDimitry Andric else 4257*8bcb0991SDimitry Andric MIRBuilder.buildSub(Dst0, LHS, RHS); 4258*8bcb0991SDimitry Andric 4259*8bcb0991SDimitry Andric // TODO: If SADDSAT/SSUBSAT is legal, compare results to detect overflow. 4260*8bcb0991SDimitry Andric 4261*8bcb0991SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 4262*8bcb0991SDimitry Andric 4263*8bcb0991SDimitry Andric // For an addition, the result should be less than one of the operands (LHS) 4264*8bcb0991SDimitry Andric // if and only if the other operand (RHS) is negative, otherwise there will 4265*8bcb0991SDimitry Andric // be overflow. 4266*8bcb0991SDimitry Andric // For a subtraction, the result should be less than one of the operands 4267*8bcb0991SDimitry Andric // (LHS) if and only if the other operand (RHS) is (non-zero) positive, 4268*8bcb0991SDimitry Andric // otherwise there will be overflow. 4269*8bcb0991SDimitry Andric auto ResultLowerThanLHS = 4270*8bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Dst0, LHS); 4271*8bcb0991SDimitry Andric auto ConditionRHS = MIRBuilder.buildICmp( 4272*8bcb0991SDimitry Andric IsAdd ? CmpInst::ICMP_SLT : CmpInst::ICMP_SGT, BoolTy, RHS, Zero); 4273*8bcb0991SDimitry Andric 4274*8bcb0991SDimitry Andric MIRBuilder.buildXor(Dst1, ConditionRHS, ResultLowerThanLHS); 4275*8bcb0991SDimitry Andric MI.eraseFromParent(); 4276*8bcb0991SDimitry Andric return Legalized; 4277*8bcb0991SDimitry Andric } 4278