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" 1881ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 20fe6060f1SDimitry Andric #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h" 21e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" 2281ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 23fe6060f1SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h" 2481ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 268bcb0991SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 29fe6060f1SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 31fe6060f1SDimitry Andric #include "llvm/IR/Instructions.h" 320b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 330b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 340b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 35349cc55cSDimitry Andric #include "llvm/Target/TargetMachine.h" 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric #define DEBUG_TYPE "legalizer" 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric using namespace llvm; 400b57cec5SDimitry Andric using namespace LegalizeActions; 41e8d8bef9SDimitry Andric using namespace MIPatternMatch; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric /// Try to break down \p OrigTy into \p NarrowTy sized pieces. 440b57cec5SDimitry Andric /// 450b57cec5SDimitry Andric /// Returns the number of \p NarrowTy elements needed to reconstruct \p OrigTy, 460b57cec5SDimitry Andric /// with any leftover piece as type \p LeftoverTy 470b57cec5SDimitry Andric /// 480b57cec5SDimitry Andric /// Returns -1 in the first element of the pair if the breakdown is not 490b57cec5SDimitry Andric /// satisfiable. 500b57cec5SDimitry Andric static std::pair<int, int> 510b57cec5SDimitry Andric getNarrowTypeBreakDown(LLT OrigTy, LLT NarrowTy, LLT &LeftoverTy) { 520b57cec5SDimitry Andric assert(!LeftoverTy.isValid() && "this is an out argument"); 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric unsigned Size = OrigTy.getSizeInBits(); 550b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 560b57cec5SDimitry Andric unsigned NumParts = Size / NarrowSize; 570b57cec5SDimitry Andric unsigned LeftoverSize = Size - NumParts * NarrowSize; 580b57cec5SDimitry Andric assert(Size > NarrowSize); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric if (LeftoverSize == 0) 610b57cec5SDimitry Andric return {NumParts, 0}; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric if (NarrowTy.isVector()) { 640b57cec5SDimitry Andric unsigned EltSize = OrigTy.getScalarSizeInBits(); 650b57cec5SDimitry Andric if (LeftoverSize % EltSize != 0) 660b57cec5SDimitry Andric return {-1, -1}; 67fe6060f1SDimitry Andric LeftoverTy = LLT::scalarOrVector( 68fe6060f1SDimitry Andric ElementCount::getFixed(LeftoverSize / EltSize), EltSize); 690b57cec5SDimitry Andric } else { 700b57cec5SDimitry Andric LeftoverTy = LLT::scalar(LeftoverSize); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric int NumLeftover = LeftoverSize / LeftoverTy.getSizeInBits(); 740b57cec5SDimitry Andric return std::make_pair(NumParts, NumLeftover); 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 775ffd83dbSDimitry Andric static Type *getFloatTypeForLLT(LLVMContext &Ctx, LLT Ty) { 785ffd83dbSDimitry Andric 795ffd83dbSDimitry Andric if (!Ty.isScalar()) 805ffd83dbSDimitry Andric return nullptr; 815ffd83dbSDimitry Andric 825ffd83dbSDimitry Andric switch (Ty.getSizeInBits()) { 835ffd83dbSDimitry Andric case 16: 845ffd83dbSDimitry Andric return Type::getHalfTy(Ctx); 855ffd83dbSDimitry Andric case 32: 865ffd83dbSDimitry Andric return Type::getFloatTy(Ctx); 875ffd83dbSDimitry Andric case 64: 885ffd83dbSDimitry Andric return Type::getDoubleTy(Ctx); 89e8d8bef9SDimitry Andric case 80: 90e8d8bef9SDimitry Andric return Type::getX86_FP80Ty(Ctx); 915ffd83dbSDimitry Andric case 128: 925ffd83dbSDimitry Andric return Type::getFP128Ty(Ctx); 935ffd83dbSDimitry Andric default: 945ffd83dbSDimitry Andric return nullptr; 955ffd83dbSDimitry Andric } 965ffd83dbSDimitry Andric } 975ffd83dbSDimitry Andric 980b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, 990b57cec5SDimitry Andric GISelChangeObserver &Observer, 1000b57cec5SDimitry Andric MachineIRBuilder &Builder) 1015ffd83dbSDimitry Andric : MIRBuilder(Builder), Observer(Observer), MRI(MF.getRegInfo()), 102e8d8bef9SDimitry Andric LI(*MF.getSubtarget().getLegalizerInfo()), 103e8d8bef9SDimitry Andric TLI(*MF.getSubtarget().getTargetLowering()) { } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, const LegalizerInfo &LI, 1060b57cec5SDimitry Andric GISelChangeObserver &Observer, 1070b57cec5SDimitry Andric MachineIRBuilder &B) 108e8d8bef9SDimitry Andric : MIRBuilder(B), Observer(Observer), MRI(MF.getRegInfo()), LI(LI), 109e8d8bef9SDimitry Andric TLI(*MF.getSubtarget().getTargetLowering()) { } 110e8d8bef9SDimitry Andric 1110b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 112fe6060f1SDimitry Andric LegalizerHelper::legalizeInstrStep(MachineInstr &MI, 113fe6060f1SDimitry Andric LostDebugLocObserver &LocObserver) { 1145ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing: " << MI); 1155ffd83dbSDimitry Andric 1165ffd83dbSDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_INTRINSIC || 1190b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS) 1205ffd83dbSDimitry Andric return LI.legalizeIntrinsic(*this, MI) ? Legalized : UnableToLegalize; 1210b57cec5SDimitry Andric auto Step = LI.getAction(MI, MRI); 1220b57cec5SDimitry Andric switch (Step.Action) { 1230b57cec5SDimitry Andric case Legal: 1240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Already legal\n"); 1250b57cec5SDimitry Andric return AlreadyLegal; 1260b57cec5SDimitry Andric case Libcall: 1270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Convert to libcall\n"); 128fe6060f1SDimitry Andric return libcall(MI, LocObserver); 1290b57cec5SDimitry Andric case NarrowScalar: 1300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Narrow scalar\n"); 1310b57cec5SDimitry Andric return narrowScalar(MI, Step.TypeIdx, Step.NewType); 1320b57cec5SDimitry Andric case WidenScalar: 1330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Widen scalar\n"); 1340b57cec5SDimitry Andric return widenScalar(MI, Step.TypeIdx, Step.NewType); 1355ffd83dbSDimitry Andric case Bitcast: 1365ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << ".. Bitcast type\n"); 1375ffd83dbSDimitry Andric return bitcast(MI, Step.TypeIdx, Step.NewType); 1380b57cec5SDimitry Andric case Lower: 1390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Lower\n"); 1400b57cec5SDimitry Andric return lower(MI, Step.TypeIdx, Step.NewType); 1410b57cec5SDimitry Andric case FewerElements: 1420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Reduce number of elements\n"); 1430b57cec5SDimitry Andric return fewerElementsVector(MI, Step.TypeIdx, Step.NewType); 1440b57cec5SDimitry Andric case MoreElements: 1450b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Increase number of elements\n"); 1460b57cec5SDimitry Andric return moreElementsVector(MI, Step.TypeIdx, Step.NewType); 1470b57cec5SDimitry Andric case Custom: 1480b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Custom legalization\n"); 1495ffd83dbSDimitry Andric return LI.legalizeCustom(*this, MI) ? Legalized : UnableToLegalize; 1500b57cec5SDimitry Andric default: 1510b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Unable to legalize\n"); 1520b57cec5SDimitry Andric return UnableToLegalize; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric void LegalizerHelper::extractParts(Register Reg, LLT Ty, int NumParts, 1570b57cec5SDimitry Andric SmallVectorImpl<Register> &VRegs) { 1580b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) 1590b57cec5SDimitry Andric VRegs.push_back(MRI.createGenericVirtualRegister(Ty)); 1600b57cec5SDimitry Andric MIRBuilder.buildUnmerge(VRegs, Reg); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric bool LegalizerHelper::extractParts(Register Reg, LLT RegTy, 1640b57cec5SDimitry Andric LLT MainTy, LLT &LeftoverTy, 1650b57cec5SDimitry Andric SmallVectorImpl<Register> &VRegs, 1660b57cec5SDimitry Andric SmallVectorImpl<Register> &LeftoverRegs) { 1670b57cec5SDimitry Andric assert(!LeftoverTy.isValid() && "this is an out argument"); 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric unsigned RegSize = RegTy.getSizeInBits(); 1700b57cec5SDimitry Andric unsigned MainSize = MainTy.getSizeInBits(); 1710b57cec5SDimitry Andric unsigned NumParts = RegSize / MainSize; 1720b57cec5SDimitry Andric unsigned LeftoverSize = RegSize - NumParts * MainSize; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric // Use an unmerge when possible. 1750b57cec5SDimitry Andric if (LeftoverSize == 0) { 1760b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) 1770b57cec5SDimitry Andric VRegs.push_back(MRI.createGenericVirtualRegister(MainTy)); 1780b57cec5SDimitry Andric MIRBuilder.buildUnmerge(VRegs, Reg); 1790b57cec5SDimitry Andric return true; 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820eae32dcSDimitry Andric // Perform irregular split. Leftover is last element of RegPieces. 1830b57cec5SDimitry Andric if (MainTy.isVector()) { 1840eae32dcSDimitry Andric SmallVector<Register, 8> RegPieces; 1850eae32dcSDimitry Andric extractVectorParts(Reg, MainTy.getNumElements(), RegPieces); 1860eae32dcSDimitry Andric for (unsigned i = 0; i < RegPieces.size() - 1; ++i) 1870eae32dcSDimitry Andric VRegs.push_back(RegPieces[i]); 1880eae32dcSDimitry Andric LeftoverRegs.push_back(RegPieces[RegPieces.size() - 1]); 1890eae32dcSDimitry Andric LeftoverTy = MRI.getType(LeftoverRegs[0]); 1900eae32dcSDimitry Andric return true; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930eae32dcSDimitry Andric LeftoverTy = LLT::scalar(LeftoverSize); 1940b57cec5SDimitry Andric // For irregular sizes, extract the individual parts. 1950b57cec5SDimitry Andric for (unsigned I = 0; I != NumParts; ++I) { 1960b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(MainTy); 1970b57cec5SDimitry Andric VRegs.push_back(NewReg); 1980b57cec5SDimitry Andric MIRBuilder.buildExtract(NewReg, Reg, MainSize * I); 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric for (unsigned Offset = MainSize * NumParts; Offset < RegSize; 2020b57cec5SDimitry Andric Offset += LeftoverSize) { 2030b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy); 2040b57cec5SDimitry Andric LeftoverRegs.push_back(NewReg); 2050b57cec5SDimitry Andric MIRBuilder.buildExtract(NewReg, Reg, Offset); 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric return true; 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110eae32dcSDimitry Andric void LegalizerHelper::extractVectorParts(Register Reg, unsigned NumElts, 2120eae32dcSDimitry Andric SmallVectorImpl<Register> &VRegs) { 2130eae32dcSDimitry Andric LLT RegTy = MRI.getType(Reg); 2140eae32dcSDimitry Andric assert(RegTy.isVector() && "Expected a vector type"); 2150eae32dcSDimitry Andric 2160eae32dcSDimitry Andric LLT EltTy = RegTy.getElementType(); 2170eae32dcSDimitry Andric LLT NarrowTy = (NumElts == 1) ? EltTy : LLT::fixed_vector(NumElts, EltTy); 2180eae32dcSDimitry Andric unsigned RegNumElts = RegTy.getNumElements(); 2190eae32dcSDimitry Andric unsigned LeftoverNumElts = RegNumElts % NumElts; 2200eae32dcSDimitry Andric unsigned NumNarrowTyPieces = RegNumElts / NumElts; 2210eae32dcSDimitry Andric 2220eae32dcSDimitry Andric // Perfect split without leftover 2230eae32dcSDimitry Andric if (LeftoverNumElts == 0) 2240eae32dcSDimitry Andric return extractParts(Reg, NarrowTy, NumNarrowTyPieces, VRegs); 2250eae32dcSDimitry Andric 2260eae32dcSDimitry Andric // Irregular split. Provide direct access to all elements for artifact 2270eae32dcSDimitry Andric // combiner using unmerge to elements. Then build vectors with NumElts 2280eae32dcSDimitry Andric // elements. Remaining element(s) will be (used to build vector) Leftover. 2290eae32dcSDimitry Andric SmallVector<Register, 8> Elts; 2300eae32dcSDimitry Andric extractParts(Reg, EltTy, RegNumElts, Elts); 2310eae32dcSDimitry Andric 2320eae32dcSDimitry Andric unsigned Offset = 0; 2330eae32dcSDimitry Andric // Requested sub-vectors of NarrowTy. 2340eae32dcSDimitry Andric for (unsigned i = 0; i < NumNarrowTyPieces; ++i, Offset += NumElts) { 2350eae32dcSDimitry Andric ArrayRef<Register> Pieces(&Elts[Offset], NumElts); 2360eae32dcSDimitry Andric VRegs.push_back(MIRBuilder.buildMerge(NarrowTy, Pieces).getReg(0)); 2370eae32dcSDimitry Andric } 2380eae32dcSDimitry Andric 2390eae32dcSDimitry Andric // Leftover element(s). 2400eae32dcSDimitry Andric if (LeftoverNumElts == 1) { 2410eae32dcSDimitry Andric VRegs.push_back(Elts[Offset]); 2420eae32dcSDimitry Andric } else { 2430eae32dcSDimitry Andric LLT LeftoverTy = LLT::fixed_vector(LeftoverNumElts, EltTy); 2440eae32dcSDimitry Andric ArrayRef<Register> Pieces(&Elts[Offset], LeftoverNumElts); 2450eae32dcSDimitry Andric VRegs.push_back(MIRBuilder.buildMerge(LeftoverTy, Pieces).getReg(0)); 2460eae32dcSDimitry Andric } 2470eae32dcSDimitry Andric } 2480eae32dcSDimitry Andric 2490b57cec5SDimitry Andric void LegalizerHelper::insertParts(Register DstReg, 2500b57cec5SDimitry Andric LLT ResultTy, LLT PartTy, 2510b57cec5SDimitry Andric ArrayRef<Register> PartRegs, 2520b57cec5SDimitry Andric LLT LeftoverTy, 2530b57cec5SDimitry Andric ArrayRef<Register> LeftoverRegs) { 2540b57cec5SDimitry Andric if (!LeftoverTy.isValid()) { 2550b57cec5SDimitry Andric assert(LeftoverRegs.empty()); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric if (!ResultTy.isVector()) { 2580b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, PartRegs); 2590b57cec5SDimitry Andric return; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric if (PartTy.isVector()) 2630b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, PartRegs); 2640b57cec5SDimitry Andric else 2650b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, PartRegs); 2660b57cec5SDimitry Andric return; 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 2690eae32dcSDimitry Andric // Merge sub-vectors with different number of elements and insert into DstReg. 2700eae32dcSDimitry Andric if (ResultTy.isVector()) { 2710eae32dcSDimitry Andric assert(LeftoverRegs.size() == 1 && "Expected one leftover register"); 2720eae32dcSDimitry Andric SmallVector<Register, 8> AllRegs; 2730eae32dcSDimitry Andric for (auto Reg : concat<const Register>(PartRegs, LeftoverRegs)) 2740eae32dcSDimitry Andric AllRegs.push_back(Reg); 2750eae32dcSDimitry Andric return mergeMixedSubvectors(DstReg, AllRegs); 2760eae32dcSDimitry Andric } 2770eae32dcSDimitry Andric 278fe6060f1SDimitry Andric SmallVector<Register> GCDRegs; 279fe6060f1SDimitry Andric LLT GCDTy = getGCDType(getGCDType(ResultTy, LeftoverTy), PartTy); 280fe6060f1SDimitry Andric for (auto PartReg : concat<const Register>(PartRegs, LeftoverRegs)) 281fe6060f1SDimitry Andric extractGCDType(GCDRegs, GCDTy, PartReg); 282fe6060f1SDimitry Andric LLT ResultLCMTy = buildLCMMergePieces(ResultTy, LeftoverTy, GCDTy, GCDRegs); 283fe6060f1SDimitry Andric buildWidenedRemergeToDst(DstReg, ResultLCMTy, GCDRegs); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860eae32dcSDimitry Andric void LegalizerHelper::appendVectorElts(SmallVectorImpl<Register> &Elts, 2870eae32dcSDimitry Andric Register Reg) { 2880eae32dcSDimitry Andric LLT Ty = MRI.getType(Reg); 2890eae32dcSDimitry Andric SmallVector<Register, 8> RegElts; 2900eae32dcSDimitry Andric extractParts(Reg, Ty.getScalarType(), Ty.getNumElements(), RegElts); 2910eae32dcSDimitry Andric Elts.append(RegElts); 2920eae32dcSDimitry Andric } 2930eae32dcSDimitry Andric 2940eae32dcSDimitry Andric /// Merge \p PartRegs with different types into \p DstReg. 2950eae32dcSDimitry Andric void LegalizerHelper::mergeMixedSubvectors(Register DstReg, 2960eae32dcSDimitry Andric ArrayRef<Register> PartRegs) { 2970eae32dcSDimitry Andric SmallVector<Register, 8> AllElts; 2980eae32dcSDimitry Andric for (unsigned i = 0; i < PartRegs.size() - 1; ++i) 2990eae32dcSDimitry Andric appendVectorElts(AllElts, PartRegs[i]); 3000eae32dcSDimitry Andric 3010eae32dcSDimitry Andric Register Leftover = PartRegs[PartRegs.size() - 1]; 3020eae32dcSDimitry Andric if (MRI.getType(Leftover).isScalar()) 3030eae32dcSDimitry Andric AllElts.push_back(Leftover); 3040eae32dcSDimitry Andric else 3050eae32dcSDimitry Andric appendVectorElts(AllElts, Leftover); 3060eae32dcSDimitry Andric 3070eae32dcSDimitry Andric MIRBuilder.buildMerge(DstReg, AllElts); 3080eae32dcSDimitry Andric } 3090eae32dcSDimitry Andric 310e8d8bef9SDimitry Andric /// Append the result registers of G_UNMERGE_VALUES \p MI to \p Regs. 3115ffd83dbSDimitry Andric static void getUnmergeResults(SmallVectorImpl<Register> &Regs, 3125ffd83dbSDimitry Andric const MachineInstr &MI) { 3135ffd83dbSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES); 3145ffd83dbSDimitry Andric 315e8d8bef9SDimitry Andric const int StartIdx = Regs.size(); 3165ffd83dbSDimitry Andric const int NumResults = MI.getNumOperands() - 1; 317e8d8bef9SDimitry Andric Regs.resize(Regs.size() + NumResults); 3185ffd83dbSDimitry Andric for (int I = 0; I != NumResults; ++I) 319e8d8bef9SDimitry Andric Regs[StartIdx + I] = MI.getOperand(I).getReg(); 3205ffd83dbSDimitry Andric } 3215ffd83dbSDimitry Andric 322e8d8bef9SDimitry Andric void LegalizerHelper::extractGCDType(SmallVectorImpl<Register> &Parts, 323e8d8bef9SDimitry Andric LLT GCDTy, Register SrcReg) { 3245ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 3255ffd83dbSDimitry Andric if (SrcTy == GCDTy) { 3265ffd83dbSDimitry Andric // If the source already evenly divides the result type, we don't need to do 3275ffd83dbSDimitry Andric // anything. 3285ffd83dbSDimitry Andric Parts.push_back(SrcReg); 3295ffd83dbSDimitry Andric } else { 3305ffd83dbSDimitry Andric // Need to split into common type sized pieces. 3315ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 3325ffd83dbSDimitry Andric getUnmergeResults(Parts, *Unmerge); 3335ffd83dbSDimitry Andric } 334e8d8bef9SDimitry Andric } 3355ffd83dbSDimitry Andric 336e8d8bef9SDimitry Andric LLT LegalizerHelper::extractGCDType(SmallVectorImpl<Register> &Parts, LLT DstTy, 337e8d8bef9SDimitry Andric LLT NarrowTy, Register SrcReg) { 338e8d8bef9SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 339e8d8bef9SDimitry Andric LLT GCDTy = getGCDType(getGCDType(SrcTy, NarrowTy), DstTy); 340e8d8bef9SDimitry Andric extractGCDType(Parts, GCDTy, SrcReg); 3415ffd83dbSDimitry Andric return GCDTy; 3425ffd83dbSDimitry Andric } 3435ffd83dbSDimitry Andric 3445ffd83dbSDimitry Andric LLT LegalizerHelper::buildLCMMergePieces(LLT DstTy, LLT NarrowTy, LLT GCDTy, 3455ffd83dbSDimitry Andric SmallVectorImpl<Register> &VRegs, 3465ffd83dbSDimitry Andric unsigned PadStrategy) { 3475ffd83dbSDimitry Andric LLT LCMTy = getLCMType(DstTy, NarrowTy); 3485ffd83dbSDimitry Andric 3495ffd83dbSDimitry Andric int NumParts = LCMTy.getSizeInBits() / NarrowTy.getSizeInBits(); 3505ffd83dbSDimitry Andric int NumSubParts = NarrowTy.getSizeInBits() / GCDTy.getSizeInBits(); 3515ffd83dbSDimitry Andric int NumOrigSrc = VRegs.size(); 3525ffd83dbSDimitry Andric 3535ffd83dbSDimitry Andric Register PadReg; 3545ffd83dbSDimitry Andric 3555ffd83dbSDimitry Andric // Get a value we can use to pad the source value if the sources won't evenly 3565ffd83dbSDimitry Andric // cover the result type. 3575ffd83dbSDimitry Andric if (NumOrigSrc < NumParts * NumSubParts) { 3585ffd83dbSDimitry Andric if (PadStrategy == TargetOpcode::G_ZEXT) 3595ffd83dbSDimitry Andric PadReg = MIRBuilder.buildConstant(GCDTy, 0).getReg(0); 3605ffd83dbSDimitry Andric else if (PadStrategy == TargetOpcode::G_ANYEXT) 3615ffd83dbSDimitry Andric PadReg = MIRBuilder.buildUndef(GCDTy).getReg(0); 3625ffd83dbSDimitry Andric else { 3635ffd83dbSDimitry Andric assert(PadStrategy == TargetOpcode::G_SEXT); 3645ffd83dbSDimitry Andric 3655ffd83dbSDimitry Andric // Shift the sign bit of the low register through the high register. 3665ffd83dbSDimitry Andric auto ShiftAmt = 3675ffd83dbSDimitry Andric MIRBuilder.buildConstant(LLT::scalar(64), GCDTy.getSizeInBits() - 1); 3685ffd83dbSDimitry Andric PadReg = MIRBuilder.buildAShr(GCDTy, VRegs.back(), ShiftAmt).getReg(0); 3695ffd83dbSDimitry Andric } 3705ffd83dbSDimitry Andric } 3715ffd83dbSDimitry Andric 3725ffd83dbSDimitry Andric // Registers for the final merge to be produced. 3735ffd83dbSDimitry Andric SmallVector<Register, 4> Remerge(NumParts); 3745ffd83dbSDimitry Andric 3755ffd83dbSDimitry Andric // Registers needed for intermediate merges, which will be merged into a 3765ffd83dbSDimitry Andric // source for Remerge. 3775ffd83dbSDimitry Andric SmallVector<Register, 4> SubMerge(NumSubParts); 3785ffd83dbSDimitry Andric 3795ffd83dbSDimitry Andric // Once we've fully read off the end of the original source bits, we can reuse 3805ffd83dbSDimitry Andric // the same high bits for remaining padding elements. 3815ffd83dbSDimitry Andric Register AllPadReg; 3825ffd83dbSDimitry Andric 3835ffd83dbSDimitry Andric // Build merges to the LCM type to cover the original result type. 3845ffd83dbSDimitry Andric for (int I = 0; I != NumParts; ++I) { 3855ffd83dbSDimitry Andric bool AllMergePartsArePadding = true; 3865ffd83dbSDimitry Andric 3875ffd83dbSDimitry Andric // Build the requested merges to the requested type. 3885ffd83dbSDimitry Andric for (int J = 0; J != NumSubParts; ++J) { 3895ffd83dbSDimitry Andric int Idx = I * NumSubParts + J; 3905ffd83dbSDimitry Andric if (Idx >= NumOrigSrc) { 3915ffd83dbSDimitry Andric SubMerge[J] = PadReg; 3925ffd83dbSDimitry Andric continue; 3935ffd83dbSDimitry Andric } 3945ffd83dbSDimitry Andric 3955ffd83dbSDimitry Andric SubMerge[J] = VRegs[Idx]; 3965ffd83dbSDimitry Andric 3975ffd83dbSDimitry Andric // There are meaningful bits here we can't reuse later. 3985ffd83dbSDimitry Andric AllMergePartsArePadding = false; 3995ffd83dbSDimitry Andric } 4005ffd83dbSDimitry Andric 4015ffd83dbSDimitry Andric // If we've filled up a complete piece with padding bits, we can directly 4025ffd83dbSDimitry Andric // emit the natural sized constant if applicable, rather than a merge of 4035ffd83dbSDimitry Andric // smaller constants. 4045ffd83dbSDimitry Andric if (AllMergePartsArePadding && !AllPadReg) { 4055ffd83dbSDimitry Andric if (PadStrategy == TargetOpcode::G_ANYEXT) 4065ffd83dbSDimitry Andric AllPadReg = MIRBuilder.buildUndef(NarrowTy).getReg(0); 4075ffd83dbSDimitry Andric else if (PadStrategy == TargetOpcode::G_ZEXT) 4085ffd83dbSDimitry Andric AllPadReg = MIRBuilder.buildConstant(NarrowTy, 0).getReg(0); 4095ffd83dbSDimitry Andric 4105ffd83dbSDimitry Andric // If this is a sign extension, we can't materialize a trivial constant 4115ffd83dbSDimitry Andric // with the right type and have to produce a merge. 4125ffd83dbSDimitry Andric } 4135ffd83dbSDimitry Andric 4145ffd83dbSDimitry Andric if (AllPadReg) { 4155ffd83dbSDimitry Andric // Avoid creating additional instructions if we're just adding additional 4165ffd83dbSDimitry Andric // copies of padding bits. 4175ffd83dbSDimitry Andric Remerge[I] = AllPadReg; 4185ffd83dbSDimitry Andric continue; 4195ffd83dbSDimitry Andric } 4205ffd83dbSDimitry Andric 4215ffd83dbSDimitry Andric if (NumSubParts == 1) 4225ffd83dbSDimitry Andric Remerge[I] = SubMerge[0]; 4235ffd83dbSDimitry Andric else 4245ffd83dbSDimitry Andric Remerge[I] = MIRBuilder.buildMerge(NarrowTy, SubMerge).getReg(0); 4255ffd83dbSDimitry Andric 4265ffd83dbSDimitry Andric // In the sign extend padding case, re-use the first all-signbit merge. 4275ffd83dbSDimitry Andric if (AllMergePartsArePadding && !AllPadReg) 4285ffd83dbSDimitry Andric AllPadReg = Remerge[I]; 4295ffd83dbSDimitry Andric } 4305ffd83dbSDimitry Andric 4315ffd83dbSDimitry Andric VRegs = std::move(Remerge); 4325ffd83dbSDimitry Andric return LCMTy; 4335ffd83dbSDimitry Andric } 4345ffd83dbSDimitry Andric 4355ffd83dbSDimitry Andric void LegalizerHelper::buildWidenedRemergeToDst(Register DstReg, LLT LCMTy, 4365ffd83dbSDimitry Andric ArrayRef<Register> RemergeRegs) { 4375ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 4385ffd83dbSDimitry Andric 4395ffd83dbSDimitry Andric // Create the merge to the widened source, and extract the relevant bits into 4405ffd83dbSDimitry Andric // the result. 4415ffd83dbSDimitry Andric 4425ffd83dbSDimitry Andric if (DstTy == LCMTy) { 4435ffd83dbSDimitry Andric MIRBuilder.buildMerge(DstReg, RemergeRegs); 4445ffd83dbSDimitry Andric return; 4455ffd83dbSDimitry Andric } 4465ffd83dbSDimitry Andric 4475ffd83dbSDimitry Andric auto Remerge = MIRBuilder.buildMerge(LCMTy, RemergeRegs); 4485ffd83dbSDimitry Andric if (DstTy.isScalar() && LCMTy.isScalar()) { 4495ffd83dbSDimitry Andric MIRBuilder.buildTrunc(DstReg, Remerge); 4505ffd83dbSDimitry Andric return; 4515ffd83dbSDimitry Andric } 4525ffd83dbSDimitry Andric 4535ffd83dbSDimitry Andric if (LCMTy.isVector()) { 454e8d8bef9SDimitry Andric unsigned NumDefs = LCMTy.getSizeInBits() / DstTy.getSizeInBits(); 455e8d8bef9SDimitry Andric SmallVector<Register, 8> UnmergeDefs(NumDefs); 456e8d8bef9SDimitry Andric UnmergeDefs[0] = DstReg; 457e8d8bef9SDimitry Andric for (unsigned I = 1; I != NumDefs; ++I) 458e8d8bef9SDimitry Andric UnmergeDefs[I] = MRI.createGenericVirtualRegister(DstTy); 459e8d8bef9SDimitry Andric 460e8d8bef9SDimitry Andric MIRBuilder.buildUnmerge(UnmergeDefs, 461e8d8bef9SDimitry Andric MIRBuilder.buildMerge(LCMTy, RemergeRegs)); 4625ffd83dbSDimitry Andric return; 4635ffd83dbSDimitry Andric } 4645ffd83dbSDimitry Andric 4655ffd83dbSDimitry Andric llvm_unreachable("unhandled case"); 4665ffd83dbSDimitry Andric } 4675ffd83dbSDimitry Andric 4680b57cec5SDimitry Andric static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) { 469e8d8bef9SDimitry Andric #define RTLIBCASE_INT(LibcallPrefix) \ 4705ffd83dbSDimitry Andric do { \ 4715ffd83dbSDimitry Andric switch (Size) { \ 4725ffd83dbSDimitry Andric case 32: \ 4735ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##32; \ 4745ffd83dbSDimitry Andric case 64: \ 4755ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##64; \ 4765ffd83dbSDimitry Andric case 128: \ 4775ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##128; \ 4785ffd83dbSDimitry Andric default: \ 4795ffd83dbSDimitry Andric llvm_unreachable("unexpected size"); \ 4805ffd83dbSDimitry Andric } \ 4815ffd83dbSDimitry Andric } while (0) 4825ffd83dbSDimitry Andric 483e8d8bef9SDimitry Andric #define RTLIBCASE(LibcallPrefix) \ 484e8d8bef9SDimitry Andric do { \ 485e8d8bef9SDimitry Andric switch (Size) { \ 486e8d8bef9SDimitry Andric case 32: \ 487e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##32; \ 488e8d8bef9SDimitry Andric case 64: \ 489e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##64; \ 490e8d8bef9SDimitry Andric case 80: \ 491e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##80; \ 492e8d8bef9SDimitry Andric case 128: \ 493e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##128; \ 494e8d8bef9SDimitry Andric default: \ 495e8d8bef9SDimitry Andric llvm_unreachable("unexpected size"); \ 496e8d8bef9SDimitry Andric } \ 497e8d8bef9SDimitry Andric } while (0) 4985ffd83dbSDimitry Andric 4990b57cec5SDimitry Andric switch (Opcode) { 5000b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 501e8d8bef9SDimitry Andric RTLIBCASE_INT(SDIV_I); 5020b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 503e8d8bef9SDimitry Andric RTLIBCASE_INT(UDIV_I); 5040b57cec5SDimitry Andric case TargetOpcode::G_SREM: 505e8d8bef9SDimitry Andric RTLIBCASE_INT(SREM_I); 5060b57cec5SDimitry Andric case TargetOpcode::G_UREM: 507e8d8bef9SDimitry Andric RTLIBCASE_INT(UREM_I); 5080b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 509e8d8bef9SDimitry Andric RTLIBCASE_INT(CTLZ_I); 5100b57cec5SDimitry Andric case TargetOpcode::G_FADD: 5115ffd83dbSDimitry Andric RTLIBCASE(ADD_F); 5120b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 5135ffd83dbSDimitry Andric RTLIBCASE(SUB_F); 5140b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 5155ffd83dbSDimitry Andric RTLIBCASE(MUL_F); 5160b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 5175ffd83dbSDimitry Andric RTLIBCASE(DIV_F); 5180b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 5195ffd83dbSDimitry Andric RTLIBCASE(EXP_F); 5200b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 5215ffd83dbSDimitry Andric RTLIBCASE(EXP2_F); 5220b57cec5SDimitry Andric case TargetOpcode::G_FREM: 5235ffd83dbSDimitry Andric RTLIBCASE(REM_F); 5240b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 5255ffd83dbSDimitry Andric RTLIBCASE(POW_F); 5260b57cec5SDimitry Andric case TargetOpcode::G_FMA: 5275ffd83dbSDimitry Andric RTLIBCASE(FMA_F); 5280b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 5295ffd83dbSDimitry Andric RTLIBCASE(SIN_F); 5300b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 5315ffd83dbSDimitry Andric RTLIBCASE(COS_F); 5320b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 5335ffd83dbSDimitry Andric RTLIBCASE(LOG10_F); 5340b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 5355ffd83dbSDimitry Andric RTLIBCASE(LOG_F); 5360b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 5375ffd83dbSDimitry Andric RTLIBCASE(LOG2_F); 5380b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 5395ffd83dbSDimitry Andric RTLIBCASE(CEIL_F); 5400b57cec5SDimitry Andric case TargetOpcode::G_FFLOOR: 5415ffd83dbSDimitry Andric RTLIBCASE(FLOOR_F); 5425ffd83dbSDimitry Andric case TargetOpcode::G_FMINNUM: 5435ffd83dbSDimitry Andric RTLIBCASE(FMIN_F); 5445ffd83dbSDimitry Andric case TargetOpcode::G_FMAXNUM: 5455ffd83dbSDimitry Andric RTLIBCASE(FMAX_F); 5465ffd83dbSDimitry Andric case TargetOpcode::G_FSQRT: 5475ffd83dbSDimitry Andric RTLIBCASE(SQRT_F); 5485ffd83dbSDimitry Andric case TargetOpcode::G_FRINT: 5495ffd83dbSDimitry Andric RTLIBCASE(RINT_F); 5505ffd83dbSDimitry Andric case TargetOpcode::G_FNEARBYINT: 5515ffd83dbSDimitry Andric RTLIBCASE(NEARBYINT_F); 552e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: 553e8d8bef9SDimitry Andric RTLIBCASE(ROUNDEVEN_F); 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric llvm_unreachable("Unknown libcall function"); 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5588bcb0991SDimitry Andric /// True if an instruction is in tail position in its caller. Intended for 5598bcb0991SDimitry Andric /// legalizing libcalls as tail calls when possible. 560fe6060f1SDimitry Andric static bool isLibCallInTailPosition(MachineInstr &MI, 561fe6060f1SDimitry Andric const TargetInstrInfo &TII, 562fe6060f1SDimitry Andric MachineRegisterInfo &MRI) { 5635ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 5645ffd83dbSDimitry Andric const Function &F = MBB.getParent()->getFunction(); 5658bcb0991SDimitry Andric 5668bcb0991SDimitry Andric // Conservatively require the attributes of the call to match those of 5678bcb0991SDimitry Andric // the return. Ignore NoAlias and NonNull because they don't affect the 5688bcb0991SDimitry Andric // call sequence. 5698bcb0991SDimitry Andric AttributeList CallerAttrs = F.getAttributes(); 57004eeddc0SDimitry Andric if (AttrBuilder(F.getContext(), CallerAttrs.getRetAttrs()) 5718bcb0991SDimitry Andric .removeAttribute(Attribute::NoAlias) 5728bcb0991SDimitry Andric .removeAttribute(Attribute::NonNull) 5738bcb0991SDimitry Andric .hasAttributes()) 5748bcb0991SDimitry Andric return false; 5758bcb0991SDimitry Andric 5768bcb0991SDimitry Andric // It's not safe to eliminate the sign / zero extension of the return value. 577349cc55cSDimitry Andric if (CallerAttrs.hasRetAttr(Attribute::ZExt) || 578349cc55cSDimitry Andric CallerAttrs.hasRetAttr(Attribute::SExt)) 5798bcb0991SDimitry Andric return false; 5808bcb0991SDimitry Andric 581fe6060f1SDimitry Andric // Only tail call if the following instruction is a standard return or if we 582fe6060f1SDimitry Andric // have a `thisreturn` callee, and a sequence like: 583fe6060f1SDimitry Andric // 584fe6060f1SDimitry Andric // G_MEMCPY %0, %1, %2 585fe6060f1SDimitry Andric // $x0 = COPY %0 586fe6060f1SDimitry Andric // RET_ReallyLR implicit $x0 5875ffd83dbSDimitry Andric auto Next = next_nodbg(MI.getIterator(), MBB.instr_end()); 588fe6060f1SDimitry Andric if (Next != MBB.instr_end() && Next->isCopy()) { 589fe6060f1SDimitry Andric switch (MI.getOpcode()) { 590fe6060f1SDimitry Andric default: 591fe6060f1SDimitry Andric llvm_unreachable("unsupported opcode"); 592fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 593fe6060f1SDimitry Andric return false; 594fe6060f1SDimitry Andric case TargetOpcode::G_MEMCPY: 595fe6060f1SDimitry Andric case TargetOpcode::G_MEMMOVE: 596fe6060f1SDimitry Andric case TargetOpcode::G_MEMSET: 597fe6060f1SDimitry Andric break; 598fe6060f1SDimitry Andric } 599fe6060f1SDimitry Andric 600fe6060f1SDimitry Andric Register VReg = MI.getOperand(0).getReg(); 601fe6060f1SDimitry Andric if (!VReg.isVirtual() || VReg != Next->getOperand(1).getReg()) 602fe6060f1SDimitry Andric return false; 603fe6060f1SDimitry Andric 604fe6060f1SDimitry Andric Register PReg = Next->getOperand(0).getReg(); 605fe6060f1SDimitry Andric if (!PReg.isPhysical()) 606fe6060f1SDimitry Andric return false; 607fe6060f1SDimitry Andric 608fe6060f1SDimitry Andric auto Ret = next_nodbg(Next, MBB.instr_end()); 609fe6060f1SDimitry Andric if (Ret == MBB.instr_end() || !Ret->isReturn()) 610fe6060f1SDimitry Andric return false; 611fe6060f1SDimitry Andric 612fe6060f1SDimitry Andric if (Ret->getNumImplicitOperands() != 1) 613fe6060f1SDimitry Andric return false; 614fe6060f1SDimitry Andric 615fe6060f1SDimitry Andric if (PReg != Ret->getOperand(0).getReg()) 616fe6060f1SDimitry Andric return false; 617fe6060f1SDimitry Andric 618fe6060f1SDimitry Andric // Skip over the COPY that we just validated. 619fe6060f1SDimitry Andric Next = Ret; 620fe6060f1SDimitry Andric } 621fe6060f1SDimitry Andric 6225ffd83dbSDimitry Andric if (Next == MBB.instr_end() || TII.isTailCall(*Next) || !Next->isReturn()) 6238bcb0991SDimitry Andric return false; 6248bcb0991SDimitry Andric 6258bcb0991SDimitry Andric return true; 6268bcb0991SDimitry Andric } 6278bcb0991SDimitry Andric 6280b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 6295ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, const char *Name, 6300b57cec5SDimitry Andric const CallLowering::ArgInfo &Result, 6315ffd83dbSDimitry Andric ArrayRef<CallLowering::ArgInfo> Args, 6325ffd83dbSDimitry Andric const CallingConv::ID CC) { 6330b57cec5SDimitry Andric auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); 6340b57cec5SDimitry Andric 6358bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 6365ffd83dbSDimitry Andric Info.CallConv = CC; 6378bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 6388bcb0991SDimitry Andric Info.OrigRet = Result; 6398bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 6408bcb0991SDimitry Andric if (!CLI.lowerCall(MIRBuilder, Info)) 6410b57cec5SDimitry Andric return LegalizerHelper::UnableToLegalize; 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric return LegalizerHelper::Legalized; 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 6465ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 6475ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall, 6485ffd83dbSDimitry Andric const CallLowering::ArgInfo &Result, 6495ffd83dbSDimitry Andric ArrayRef<CallLowering::ArgInfo> Args) { 6505ffd83dbSDimitry Andric auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); 6515ffd83dbSDimitry Andric const char *Name = TLI.getLibcallName(Libcall); 6525ffd83dbSDimitry Andric const CallingConv::ID CC = TLI.getLibcallCallingConv(Libcall); 6535ffd83dbSDimitry Andric return createLibcall(MIRBuilder, Name, Result, Args, CC); 6545ffd83dbSDimitry Andric } 6555ffd83dbSDimitry Andric 6560b57cec5SDimitry Andric // Useful for libcalls where all operands have the same type. 6570b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult 6580b57cec5SDimitry Andric simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size, 6590b57cec5SDimitry Andric Type *OpType) { 6600b57cec5SDimitry Andric auto Libcall = getRTLibDesc(MI.getOpcode(), Size); 6610b57cec5SDimitry Andric 662fe6060f1SDimitry Andric // FIXME: What does the original arg index mean here? 6630b57cec5SDimitry Andric SmallVector<CallLowering::ArgInfo, 3> Args; 6644824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) 6654824e7fdSDimitry Andric Args.push_back({MO.getReg(), OpType, 0}); 666fe6060f1SDimitry Andric return createLibcall(MIRBuilder, Libcall, 667fe6060f1SDimitry Andric {MI.getOperand(0).getReg(), OpType, 0}, Args); 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric 6708bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 6718bcb0991SDimitry Andric llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 672fe6060f1SDimitry Andric MachineInstr &MI, LostDebugLocObserver &LocObserver) { 6738bcb0991SDimitry Andric auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); 6748bcb0991SDimitry Andric 6758bcb0991SDimitry Andric SmallVector<CallLowering::ArgInfo, 3> Args; 6768bcb0991SDimitry Andric // Add all the args, except for the last which is an imm denoting 'tail'. 677e8d8bef9SDimitry Andric for (unsigned i = 0; i < MI.getNumOperands() - 1; ++i) { 6788bcb0991SDimitry Andric Register Reg = MI.getOperand(i).getReg(); 6798bcb0991SDimitry Andric 6808bcb0991SDimitry Andric // Need derive an IR type for call lowering. 6818bcb0991SDimitry Andric LLT OpLLT = MRI.getType(Reg); 6828bcb0991SDimitry Andric Type *OpTy = nullptr; 6838bcb0991SDimitry Andric if (OpLLT.isPointer()) 6848bcb0991SDimitry Andric OpTy = Type::getInt8PtrTy(Ctx, OpLLT.getAddressSpace()); 6858bcb0991SDimitry Andric else 6868bcb0991SDimitry Andric OpTy = IntegerType::get(Ctx, OpLLT.getSizeInBits()); 687fe6060f1SDimitry Andric Args.push_back({Reg, OpTy, 0}); 6888bcb0991SDimitry Andric } 6898bcb0991SDimitry Andric 6908bcb0991SDimitry Andric auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); 6918bcb0991SDimitry Andric auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); 6928bcb0991SDimitry Andric RTLIB::Libcall RTLibcall; 693fe6060f1SDimitry Andric unsigned Opc = MI.getOpcode(); 694fe6060f1SDimitry Andric switch (Opc) { 695fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 696fe6060f1SDimitry Andric RTLibcall = RTLIB::BZERO; 697fe6060f1SDimitry Andric break; 698e8d8bef9SDimitry Andric case TargetOpcode::G_MEMCPY: 6998bcb0991SDimitry Andric RTLibcall = RTLIB::MEMCPY; 700fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 7018bcb0991SDimitry Andric break; 702e8d8bef9SDimitry Andric case TargetOpcode::G_MEMMOVE: 7038bcb0991SDimitry Andric RTLibcall = RTLIB::MEMMOVE; 704fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 7058bcb0991SDimitry Andric break; 706e8d8bef9SDimitry Andric case TargetOpcode::G_MEMSET: 707e8d8bef9SDimitry Andric RTLibcall = RTLIB::MEMSET; 708fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 709e8d8bef9SDimitry Andric break; 7108bcb0991SDimitry Andric default: 711fe6060f1SDimitry Andric llvm_unreachable("unsupported opcode"); 7128bcb0991SDimitry Andric } 7138bcb0991SDimitry Andric const char *Name = TLI.getLibcallName(RTLibcall); 7148bcb0991SDimitry Andric 715fe6060f1SDimitry Andric // Unsupported libcall on the target. 716fe6060f1SDimitry Andric if (!Name) { 717fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << ".. .. Could not find libcall name for " 718fe6060f1SDimitry Andric << MIRBuilder.getTII().getName(Opc) << "\n"); 719fe6060f1SDimitry Andric return LegalizerHelper::UnableToLegalize; 720fe6060f1SDimitry Andric } 721fe6060f1SDimitry Andric 7228bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 7238bcb0991SDimitry Andric Info.CallConv = TLI.getLibcallCallingConv(RTLibcall); 7248bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 725fe6060f1SDimitry Andric Info.OrigRet = CallLowering::ArgInfo({0}, Type::getVoidTy(Ctx), 0); 726e8d8bef9SDimitry Andric Info.IsTailCall = MI.getOperand(MI.getNumOperands() - 1).getImm() && 727fe6060f1SDimitry Andric isLibCallInTailPosition(MI, MIRBuilder.getTII(), MRI); 7288bcb0991SDimitry Andric 7298bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 7308bcb0991SDimitry Andric if (!CLI.lowerCall(MIRBuilder, Info)) 7318bcb0991SDimitry Andric return LegalizerHelper::UnableToLegalize; 7328bcb0991SDimitry Andric 7338bcb0991SDimitry Andric if (Info.LoweredTailCall) { 7348bcb0991SDimitry Andric assert(Info.IsTailCall && "Lowered tail call when it wasn't a tail call?"); 735fe6060f1SDimitry Andric 736fe6060f1SDimitry Andric // Check debug locations before removing the return. 737fe6060f1SDimitry Andric LocObserver.checkpoint(true); 738fe6060f1SDimitry Andric 7395ffd83dbSDimitry Andric // We must have a return following the call (or debug insts) to get past 7408bcb0991SDimitry Andric // isLibCallInTailPosition. 7415ffd83dbSDimitry Andric do { 7425ffd83dbSDimitry Andric MachineInstr *Next = MI.getNextNode(); 743fe6060f1SDimitry Andric assert(Next && 744fe6060f1SDimitry Andric (Next->isCopy() || Next->isReturn() || Next->isDebugInstr()) && 7455ffd83dbSDimitry Andric "Expected instr following MI to be return or debug inst?"); 7468bcb0991SDimitry Andric // We lowered a tail call, so the call is now the return from the block. 7478bcb0991SDimitry Andric // Delete the old return. 7485ffd83dbSDimitry Andric Next->eraseFromParent(); 7495ffd83dbSDimitry Andric } while (MI.getNextNode()); 750fe6060f1SDimitry Andric 751fe6060f1SDimitry Andric // We expect to lose the debug location from the return. 752fe6060f1SDimitry Andric LocObserver.checkpoint(false); 7538bcb0991SDimitry Andric } 7548bcb0991SDimitry Andric 7558bcb0991SDimitry Andric return LegalizerHelper::Legalized; 7568bcb0991SDimitry Andric } 7578bcb0991SDimitry Andric 7580b57cec5SDimitry Andric static RTLIB::Libcall getConvRTLibDesc(unsigned Opcode, Type *ToType, 7590b57cec5SDimitry Andric Type *FromType) { 7600b57cec5SDimitry Andric auto ToMVT = MVT::getVT(ToType); 7610b57cec5SDimitry Andric auto FromMVT = MVT::getVT(FromType); 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric switch (Opcode) { 7640b57cec5SDimitry Andric case TargetOpcode::G_FPEXT: 7650b57cec5SDimitry Andric return RTLIB::getFPEXT(FromMVT, ToMVT); 7660b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: 7670b57cec5SDimitry Andric return RTLIB::getFPROUND(FromMVT, ToMVT); 7680b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 7690b57cec5SDimitry Andric return RTLIB::getFPTOSINT(FromMVT, ToMVT); 7700b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: 7710b57cec5SDimitry Andric return RTLIB::getFPTOUINT(FromMVT, ToMVT); 7720b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 7730b57cec5SDimitry Andric return RTLIB::getSINTTOFP(FromMVT, ToMVT); 7740b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: 7750b57cec5SDimitry Andric return RTLIB::getUINTTOFP(FromMVT, ToMVT); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric llvm_unreachable("Unsupported libcall function"); 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult 7810b57cec5SDimitry Andric conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType, 7820b57cec5SDimitry Andric Type *FromType) { 7830b57cec5SDimitry Andric RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType); 784fe6060f1SDimitry Andric return createLibcall(MIRBuilder, Libcall, 785fe6060f1SDimitry Andric {MI.getOperand(0).getReg(), ToType, 0}, 786fe6060f1SDimitry Andric {{MI.getOperand(1).getReg(), FromType, 0}}); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 790fe6060f1SDimitry Andric LegalizerHelper::libcall(MachineInstr &MI, LostDebugLocObserver &LocObserver) { 7910b57cec5SDimitry Andric LLT LLTy = MRI.getType(MI.getOperand(0).getReg()); 7920b57cec5SDimitry Andric unsigned Size = LLTy.getSizeInBits(); 7930b57cec5SDimitry Andric auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric switch (MI.getOpcode()) { 7960b57cec5SDimitry Andric default: 7970b57cec5SDimitry Andric return UnableToLegalize; 7980b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 7990b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 8000b57cec5SDimitry Andric case TargetOpcode::G_SREM: 8010b57cec5SDimitry Andric case TargetOpcode::G_UREM: 8020b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: { 8030b57cec5SDimitry Andric Type *HLTy = IntegerType::get(Ctx, Size); 8040b57cec5SDimitry Andric auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); 8050b57cec5SDimitry Andric if (Status != Legalized) 8060b57cec5SDimitry Andric return Status; 8070b57cec5SDimitry Andric break; 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric case TargetOpcode::G_FADD: 8100b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 8110b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 8120b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 8130b57cec5SDimitry Andric case TargetOpcode::G_FMA: 8140b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 8150b57cec5SDimitry Andric case TargetOpcode::G_FREM: 8160b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 8170b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 8180b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 8190b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 8200b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 8210b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 8220b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 8230b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 8245ffd83dbSDimitry Andric case TargetOpcode::G_FFLOOR: 8255ffd83dbSDimitry Andric case TargetOpcode::G_FMINNUM: 8265ffd83dbSDimitry Andric case TargetOpcode::G_FMAXNUM: 8275ffd83dbSDimitry Andric case TargetOpcode::G_FSQRT: 8285ffd83dbSDimitry Andric case TargetOpcode::G_FRINT: 829e8d8bef9SDimitry Andric case TargetOpcode::G_FNEARBYINT: 830e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: { 8315ffd83dbSDimitry Andric Type *HLTy = getFloatTypeForLLT(Ctx, LLTy); 832e8d8bef9SDimitry Andric if (!HLTy || (Size != 32 && Size != 64 && Size != 80 && Size != 128)) { 833e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "No libcall available for type " << LLTy << ".\n"); 8340b57cec5SDimitry Andric return UnableToLegalize; 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); 8370b57cec5SDimitry Andric if (Status != Legalized) 8380b57cec5SDimitry Andric return Status; 8390b57cec5SDimitry Andric break; 8400b57cec5SDimitry Andric } 8415ffd83dbSDimitry Andric case TargetOpcode::G_FPEXT: 8420b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: { 8435ffd83dbSDimitry Andric Type *FromTy = getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(1).getReg())); 8445ffd83dbSDimitry Andric Type *ToTy = getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(0).getReg())); 8455ffd83dbSDimitry Andric if (!FromTy || !ToTy) 8460b57cec5SDimitry Andric return UnableToLegalize; 8475ffd83dbSDimitry Andric LegalizeResult Status = conversionLibcall(MI, MIRBuilder, ToTy, FromTy ); 8480b57cec5SDimitry Andric if (Status != Legalized) 8490b57cec5SDimitry Andric return Status; 8500b57cec5SDimitry Andric break; 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 8530b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: { 8540b57cec5SDimitry Andric // FIXME: Support other types 8550b57cec5SDimitry Andric unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 8560b57cec5SDimitry Andric unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 8570b57cec5SDimitry Andric if ((ToSize != 32 && ToSize != 64) || (FromSize != 32 && FromSize != 64)) 8580b57cec5SDimitry Andric return UnableToLegalize; 8590b57cec5SDimitry Andric LegalizeResult Status = conversionLibcall( 8600b57cec5SDimitry Andric MI, MIRBuilder, 8610b57cec5SDimitry Andric ToSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx), 8620b57cec5SDimitry Andric FromSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx)); 8630b57cec5SDimitry Andric if (Status != Legalized) 8640b57cec5SDimitry Andric return Status; 8650b57cec5SDimitry Andric break; 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 8680b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: { 8690b57cec5SDimitry Andric // FIXME: Support other types 8700b57cec5SDimitry Andric unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 8710b57cec5SDimitry Andric unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 8720b57cec5SDimitry Andric if ((FromSize != 32 && FromSize != 64) || (ToSize != 32 && ToSize != 64)) 8730b57cec5SDimitry Andric return UnableToLegalize; 8740b57cec5SDimitry Andric LegalizeResult Status = conversionLibcall( 8750b57cec5SDimitry Andric MI, MIRBuilder, 8760b57cec5SDimitry Andric ToSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx), 8770b57cec5SDimitry Andric FromSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx)); 8780b57cec5SDimitry Andric if (Status != Legalized) 8790b57cec5SDimitry Andric return Status; 8800b57cec5SDimitry Andric break; 8810b57cec5SDimitry Andric } 882fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 883e8d8bef9SDimitry Andric case TargetOpcode::G_MEMCPY: 884e8d8bef9SDimitry Andric case TargetOpcode::G_MEMMOVE: 885e8d8bef9SDimitry Andric case TargetOpcode::G_MEMSET: { 886fe6060f1SDimitry Andric LegalizeResult Result = 887fe6060f1SDimitry Andric createMemLibcall(MIRBuilder, *MIRBuilder.getMRI(), MI, LocObserver); 888fe6060f1SDimitry Andric if (Result != Legalized) 889fe6060f1SDimitry Andric return Result; 890e8d8bef9SDimitry Andric MI.eraseFromParent(); 891e8d8bef9SDimitry Andric return Result; 892e8d8bef9SDimitry Andric } 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric MI.eraseFromParent(); 8960b57cec5SDimitry Andric return Legalized; 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI, 9000b57cec5SDimitry Andric unsigned TypeIdx, 9010b57cec5SDimitry Andric LLT NarrowTy) { 9020b57cec5SDimitry Andric uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 9030b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric switch (MI.getOpcode()) { 9060b57cec5SDimitry Andric default: 9070b57cec5SDimitry Andric return UnableToLegalize; 9080b57cec5SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: { 9095ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 9105ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 9115ffd83dbSDimitry Andric 9125ffd83dbSDimitry Andric // If SizeOp0 is not an exact multiple of NarrowSize, emit 9135ffd83dbSDimitry Andric // G_ANYEXT(G_IMPLICIT_DEF). Cast result to vector if needed. 9145ffd83dbSDimitry Andric // FIXME: Although this would also be legal for the general case, it causes 9155ffd83dbSDimitry Andric // a lot of regressions in the emitted code (superfluous COPYs, artifact 9165ffd83dbSDimitry Andric // combines not being hit). This seems to be a problem related to the 9175ffd83dbSDimitry Andric // artifact combiner. 9185ffd83dbSDimitry Andric if (SizeOp0 % NarrowSize != 0) { 9195ffd83dbSDimitry Andric LLT ImplicitTy = NarrowTy; 9205ffd83dbSDimitry Andric if (DstTy.isVector()) 921fe6060f1SDimitry Andric ImplicitTy = LLT::vector(DstTy.getElementCount(), ImplicitTy); 9225ffd83dbSDimitry Andric 9235ffd83dbSDimitry Andric Register ImplicitReg = MIRBuilder.buildUndef(ImplicitTy).getReg(0); 9245ffd83dbSDimitry Andric MIRBuilder.buildAnyExt(DstReg, ImplicitReg); 9255ffd83dbSDimitry Andric 9265ffd83dbSDimitry Andric MI.eraseFromParent(); 9275ffd83dbSDimitry Andric return Legalized; 9285ffd83dbSDimitry Andric } 9295ffd83dbSDimitry Andric 9300b57cec5SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs; 9330b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) 9345ffd83dbSDimitry Andric DstRegs.push_back(MIRBuilder.buildUndef(NarrowTy).getReg(0)); 9350b57cec5SDimitry Andric 9365ffd83dbSDimitry Andric if (DstTy.isVector()) 9370b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 9380b57cec5SDimitry Andric else 9390b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 9400b57cec5SDimitry Andric MI.eraseFromParent(); 9410b57cec5SDimitry Andric return Legalized; 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: { 9440b57cec5SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 9450b57cec5SDimitry Andric const APInt &Val = MI.getOperand(1).getCImm()->getValue(); 9460b57cec5SDimitry Andric unsigned TotalSize = Ty.getSizeInBits(); 9470b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 9480b57cec5SDimitry Andric int NumParts = TotalSize / NarrowSize; 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs; 9510b57cec5SDimitry Andric for (int I = 0; I != NumParts; ++I) { 9520b57cec5SDimitry Andric unsigned Offset = I * NarrowSize; 9530b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant(NarrowTy, 9540b57cec5SDimitry Andric Val.lshr(Offset).trunc(NarrowSize)); 9550b57cec5SDimitry Andric PartRegs.push_back(K.getReg(0)); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric LLT LeftoverTy; 9590b57cec5SDimitry Andric unsigned LeftoverBits = TotalSize - NumParts * NarrowSize; 9600b57cec5SDimitry Andric SmallVector<Register, 1> LeftoverRegs; 9610b57cec5SDimitry Andric if (LeftoverBits != 0) { 9620b57cec5SDimitry Andric LeftoverTy = LLT::scalar(LeftoverBits); 9630b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant( 9640b57cec5SDimitry Andric LeftoverTy, 9650b57cec5SDimitry Andric Val.lshr(NumParts * NarrowSize).trunc(LeftoverBits)); 9660b57cec5SDimitry Andric LeftoverRegs.push_back(K.getReg(0)); 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric insertParts(MI.getOperand(0).getReg(), 9700b57cec5SDimitry Andric Ty, NarrowTy, PartRegs, LeftoverTy, LeftoverRegs); 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric MI.eraseFromParent(); 9730b57cec5SDimitry Andric return Legalized; 9740b57cec5SDimitry Andric } 9755ffd83dbSDimitry Andric case TargetOpcode::G_SEXT: 9765ffd83dbSDimitry Andric case TargetOpcode::G_ZEXT: 9775ffd83dbSDimitry Andric case TargetOpcode::G_ANYEXT: 9785ffd83dbSDimitry Andric return narrowScalarExt(MI, TypeIdx, NarrowTy); 9798bcb0991SDimitry Andric case TargetOpcode::G_TRUNC: { 9808bcb0991SDimitry Andric if (TypeIdx != 1) 9818bcb0991SDimitry Andric return UnableToLegalize; 9828bcb0991SDimitry Andric 9838bcb0991SDimitry Andric uint64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 9848bcb0991SDimitry Andric if (NarrowTy.getSizeInBits() * 2 != SizeOp1) { 9858bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow trunc to type " << NarrowTy << "\n"); 9868bcb0991SDimitry Andric return UnableToLegalize; 9878bcb0991SDimitry Andric } 9888bcb0991SDimitry Andric 9895ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1)); 9905ffd83dbSDimitry Andric MIRBuilder.buildCopy(MI.getOperand(0), Unmerge.getReg(0)); 9918bcb0991SDimitry Andric MI.eraseFromParent(); 9928bcb0991SDimitry Andric return Legalized; 9938bcb0991SDimitry Andric } 9948bcb0991SDimitry Andric 9950eae32dcSDimitry Andric case TargetOpcode::G_FREEZE: { 9960eae32dcSDimitry Andric if (TypeIdx != 0) 9970eae32dcSDimitry Andric return UnableToLegalize; 9980eae32dcSDimitry Andric 9990eae32dcSDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 10000eae32dcSDimitry Andric // Should widen scalar first 10010eae32dcSDimitry Andric if (Ty.getSizeInBits() % NarrowTy.getSizeInBits() != 0) 10020eae32dcSDimitry Andric return UnableToLegalize; 10030eae32dcSDimitry Andric 10040eae32dcSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1).getReg()); 10050eae32dcSDimitry Andric SmallVector<Register, 8> Parts; 10060eae32dcSDimitry Andric for (unsigned i = 0; i < Unmerge->getNumDefs(); ++i) { 10070eae32dcSDimitry Andric Parts.push_back( 10080eae32dcSDimitry Andric MIRBuilder.buildFreeze(NarrowTy, Unmerge.getReg(i)).getReg(0)); 10090eae32dcSDimitry Andric } 10100eae32dcSDimitry Andric 10110eae32dcSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0).getReg(), Parts); 10120eae32dcSDimitry Andric MI.eraseFromParent(); 10130eae32dcSDimitry Andric return Legalized; 10140eae32dcSDimitry Andric } 1015fe6060f1SDimitry Andric case TargetOpcode::G_ADD: 1016fe6060f1SDimitry Andric case TargetOpcode::G_SUB: 1017fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 1018fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 1019fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 1020fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 1021fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 1022fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 1023fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 1024fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 1025fe6060f1SDimitry Andric return narrowScalarAddSub(MI, TypeIdx, NarrowTy); 10260b57cec5SDimitry Andric case TargetOpcode::G_MUL: 10270b57cec5SDimitry Andric case TargetOpcode::G_UMULH: 10280b57cec5SDimitry Andric return narrowScalarMul(MI, NarrowTy); 10290b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 10300b57cec5SDimitry Andric return narrowScalarExtract(MI, TypeIdx, NarrowTy); 10310b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 10320b57cec5SDimitry Andric return narrowScalarInsert(MI, TypeIdx, NarrowTy); 10330b57cec5SDimitry Andric case TargetOpcode::G_LOAD: { 1034fe6060f1SDimitry Andric auto &LoadMI = cast<GLoad>(MI); 1035fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 10360b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 10370b57cec5SDimitry Andric if (DstTy.isVector()) 10380b57cec5SDimitry Andric return UnableToLegalize; 10390b57cec5SDimitry Andric 1040fe6060f1SDimitry Andric if (8 * LoadMI.getMemSize() != DstTy.getSizeInBits()) { 10410b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 1042fe6060f1SDimitry Andric MIRBuilder.buildLoad(TmpReg, LoadMI.getPointerReg(), LoadMI.getMMO()); 10430b57cec5SDimitry Andric MIRBuilder.buildAnyExt(DstReg, TmpReg); 1044fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 10450b57cec5SDimitry Andric return Legalized; 10460b57cec5SDimitry Andric } 10470b57cec5SDimitry Andric 1048fe6060f1SDimitry Andric return reduceLoadStoreWidth(LoadMI, TypeIdx, NarrowTy); 10490b57cec5SDimitry Andric } 10500b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 10510b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: { 1052fe6060f1SDimitry Andric auto &LoadMI = cast<GExtLoad>(MI); 1053fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 1054fe6060f1SDimitry Andric Register PtrReg = LoadMI.getPointerReg(); 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 1057fe6060f1SDimitry Andric auto &MMO = LoadMI.getMMO(); 1058e8d8bef9SDimitry Andric unsigned MemSize = MMO.getSizeInBits(); 1059e8d8bef9SDimitry Andric 1060e8d8bef9SDimitry Andric if (MemSize == NarrowSize) { 10610b57cec5SDimitry Andric MIRBuilder.buildLoad(TmpReg, PtrReg, MMO); 1062e8d8bef9SDimitry Andric } else if (MemSize < NarrowSize) { 1063fe6060f1SDimitry Andric MIRBuilder.buildLoadInstr(LoadMI.getOpcode(), TmpReg, PtrReg, MMO); 1064e8d8bef9SDimitry Andric } else if (MemSize > NarrowSize) { 1065e8d8bef9SDimitry Andric // FIXME: Need to split the load. 1066e8d8bef9SDimitry Andric return UnableToLegalize; 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 1069fe6060f1SDimitry Andric if (isa<GZExtLoad>(LoadMI)) 10700b57cec5SDimitry Andric MIRBuilder.buildZExt(DstReg, TmpReg); 10710b57cec5SDimitry Andric else 10720b57cec5SDimitry Andric MIRBuilder.buildSExt(DstReg, TmpReg); 10730b57cec5SDimitry Andric 1074fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 10750b57cec5SDimitry Andric return Legalized; 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric case TargetOpcode::G_STORE: { 1078fe6060f1SDimitry Andric auto &StoreMI = cast<GStore>(MI); 10790b57cec5SDimitry Andric 1080fe6060f1SDimitry Andric Register SrcReg = StoreMI.getValueReg(); 10810b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 10820b57cec5SDimitry Andric if (SrcTy.isVector()) 10830b57cec5SDimitry Andric return UnableToLegalize; 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 10860b57cec5SDimitry Andric unsigned HandledSize = NumParts * NarrowTy.getSizeInBits(); 10870b57cec5SDimitry Andric unsigned LeftoverBits = SrcTy.getSizeInBits() - HandledSize; 10880b57cec5SDimitry Andric if (SrcTy.isVector() && LeftoverBits != 0) 10890b57cec5SDimitry Andric return UnableToLegalize; 10900b57cec5SDimitry Andric 1091fe6060f1SDimitry Andric if (8 * StoreMI.getMemSize() != SrcTy.getSizeInBits()) { 10920b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 10930b57cec5SDimitry Andric MIRBuilder.buildTrunc(TmpReg, SrcReg); 1094fe6060f1SDimitry Andric MIRBuilder.buildStore(TmpReg, StoreMI.getPointerReg(), StoreMI.getMMO()); 1095fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 10960b57cec5SDimitry Andric return Legalized; 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 1099fe6060f1SDimitry Andric return reduceLoadStoreWidth(StoreMI, 0, NarrowTy); 11000b57cec5SDimitry Andric } 11010b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 11020b57cec5SDimitry Andric return narrowScalarSelect(MI, TypeIdx, NarrowTy); 11030b57cec5SDimitry Andric case TargetOpcode::G_AND: 11040b57cec5SDimitry Andric case TargetOpcode::G_OR: 11050b57cec5SDimitry Andric case TargetOpcode::G_XOR: { 11060b57cec5SDimitry Andric // Legalize bitwise operation: 11070b57cec5SDimitry Andric // A = BinOp<Ty> B, C 11080b57cec5SDimitry Andric // into: 11090b57cec5SDimitry Andric // B1, ..., BN = G_UNMERGE_VALUES B 11100b57cec5SDimitry Andric // C1, ..., CN = G_UNMERGE_VALUES C 11110b57cec5SDimitry Andric // A1 = BinOp<Ty/N> B1, C2 11120b57cec5SDimitry Andric // ... 11130b57cec5SDimitry Andric // AN = BinOp<Ty/N> BN, CN 11140b57cec5SDimitry Andric // A = G_MERGE_VALUES A1, ..., AN 11150b57cec5SDimitry Andric return narrowScalarBasic(MI, TypeIdx, NarrowTy); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric case TargetOpcode::G_SHL: 11180b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 11190b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 11200b57cec5SDimitry Andric return narrowScalarShift(MI, TypeIdx, NarrowTy); 11210b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 11220b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 11230b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 11240b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 11250b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: 11265ffd83dbSDimitry Andric if (TypeIdx == 1) 11275ffd83dbSDimitry Andric switch (MI.getOpcode()) { 11285ffd83dbSDimitry Andric case TargetOpcode::G_CTLZ: 11295ffd83dbSDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 11305ffd83dbSDimitry Andric return narrowScalarCTLZ(MI, TypeIdx, NarrowTy); 11315ffd83dbSDimitry Andric case TargetOpcode::G_CTTZ: 11325ffd83dbSDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 11335ffd83dbSDimitry Andric return narrowScalarCTTZ(MI, TypeIdx, NarrowTy); 11345ffd83dbSDimitry Andric case TargetOpcode::G_CTPOP: 11355ffd83dbSDimitry Andric return narrowScalarCTPOP(MI, TypeIdx, NarrowTy); 11365ffd83dbSDimitry Andric default: 11375ffd83dbSDimitry Andric return UnableToLegalize; 11385ffd83dbSDimitry Andric } 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric Observer.changingInstr(MI); 11410b57cec5SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); 11420b57cec5SDimitry Andric Observer.changedInstr(MI); 11430b57cec5SDimitry Andric return Legalized; 11440b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 11450b57cec5SDimitry Andric if (TypeIdx != 1) 11460b57cec5SDimitry Andric return UnableToLegalize; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric Observer.changingInstr(MI); 11490b57cec5SDimitry Andric narrowScalarSrc(MI, NarrowTy, 1); 11500b57cec5SDimitry Andric Observer.changedInstr(MI); 11510b57cec5SDimitry Andric return Legalized; 11520b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 11530b57cec5SDimitry Andric if (TypeIdx != 0) 11540b57cec5SDimitry Andric return UnableToLegalize; 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric Observer.changingInstr(MI); 11570b57cec5SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); 11580b57cec5SDimitry Andric Observer.changedInstr(MI); 11590b57cec5SDimitry Andric return Legalized; 11600b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 1161d409305fSDimitry Andric // FIXME: add support for when SizeOp0 isn't an exact multiple of 1162d409305fSDimitry Andric // NarrowSize. 1163d409305fSDimitry Andric if (SizeOp0 % NarrowSize != 0) 1164d409305fSDimitry Andric return UnableToLegalize; 1165d409305fSDimitry Andric 11660b57cec5SDimitry Andric unsigned NumParts = SizeOp0 / NarrowSize; 11675ffd83dbSDimitry Andric SmallVector<Register, 2> DstRegs(NumParts); 11685ffd83dbSDimitry Andric SmallVector<SmallVector<Register, 2>, 2> SrcRegs(MI.getNumOperands() / 2); 11690b57cec5SDimitry Andric Observer.changingInstr(MI); 11700b57cec5SDimitry Andric for (unsigned i = 1; i < MI.getNumOperands(); i += 2) { 11710b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(i + 1).getMBB(); 11720b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 11730b57cec5SDimitry Andric extractParts(MI.getOperand(i).getReg(), NarrowTy, NumParts, 11740b57cec5SDimitry Andric SrcRegs[i / 2]); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 11770b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, MI); 11780b57cec5SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 11790b57cec5SDimitry Andric DstRegs[i] = MRI.createGenericVirtualRegister(NarrowTy); 11800b57cec5SDimitry Andric MachineInstrBuilder MIB = 11810b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_PHI).addDef(DstRegs[i]); 11820b57cec5SDimitry Andric for (unsigned j = 1; j < MI.getNumOperands(); j += 2) 11830b57cec5SDimitry Andric MIB.addUse(SrcRegs[j / 2][i]).add(MI.getOperand(j + 1)); 11840b57cec5SDimitry Andric } 11858bcb0991SDimitry Andric MIRBuilder.setInsertPt(MBB, MBB.getFirstNonPHI()); 11865ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), DstRegs); 11870b57cec5SDimitry Andric Observer.changedInstr(MI); 11880b57cec5SDimitry Andric MI.eraseFromParent(); 11890b57cec5SDimitry Andric return Legalized; 11900b57cec5SDimitry Andric } 11910b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: 11920b57cec5SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: { 11930b57cec5SDimitry Andric if (TypeIdx != 2) 11940b57cec5SDimitry Andric return UnableToLegalize; 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric int OpIdx = MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT ? 2 : 3; 11970b57cec5SDimitry Andric Observer.changingInstr(MI); 11980b57cec5SDimitry Andric narrowScalarSrc(MI, NarrowTy, OpIdx); 11990b57cec5SDimitry Andric Observer.changedInstr(MI); 12000b57cec5SDimitry Andric return Legalized; 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric case TargetOpcode::G_ICMP: { 1203fe6060f1SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 1204fe6060f1SDimitry Andric LLT SrcTy = MRI.getType(LHS); 1205fe6060f1SDimitry Andric uint64_t SrcSize = SrcTy.getSizeInBits(); 12060b57cec5SDimitry Andric CmpInst::Predicate Pred = 12070b57cec5SDimitry Andric static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate()); 12080b57cec5SDimitry Andric 1209fe6060f1SDimitry Andric // TODO: Handle the non-equality case for weird sizes. 1210fe6060f1SDimitry Andric if (NarrowSize * 2 != SrcSize && !ICmpInst::isEquality(Pred)) 1211fe6060f1SDimitry Andric return UnableToLegalize; 1212fe6060f1SDimitry Andric 1213fe6060f1SDimitry Andric LLT LeftoverTy; // Example: s88 -> s64 (NarrowTy) + s24 (leftover) 1214fe6060f1SDimitry Andric SmallVector<Register, 4> LHSPartRegs, LHSLeftoverRegs; 1215fe6060f1SDimitry Andric if (!extractParts(LHS, SrcTy, NarrowTy, LeftoverTy, LHSPartRegs, 1216fe6060f1SDimitry Andric LHSLeftoverRegs)) 1217fe6060f1SDimitry Andric return UnableToLegalize; 1218fe6060f1SDimitry Andric 1219fe6060f1SDimitry Andric LLT Unused; // Matches LeftoverTy; G_ICMP LHS and RHS are the same type. 1220fe6060f1SDimitry Andric SmallVector<Register, 4> RHSPartRegs, RHSLeftoverRegs; 1221fe6060f1SDimitry Andric if (!extractParts(MI.getOperand(3).getReg(), SrcTy, NarrowTy, Unused, 1222fe6060f1SDimitry Andric RHSPartRegs, RHSLeftoverRegs)) 1223fe6060f1SDimitry Andric return UnableToLegalize; 1224fe6060f1SDimitry Andric 1225fe6060f1SDimitry Andric // We now have the LHS and RHS of the compare split into narrow-type 1226fe6060f1SDimitry Andric // registers, plus potentially some leftover type. 1227fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 1228fe6060f1SDimitry Andric LLT ResTy = MRI.getType(Dst); 1229fe6060f1SDimitry Andric if (ICmpInst::isEquality(Pred)) { 1230fe6060f1SDimitry Andric // For each part on the LHS and RHS, keep track of the result of XOR-ing 1231fe6060f1SDimitry Andric // them together. For each equal part, the result should be all 0s. For 1232fe6060f1SDimitry Andric // each non-equal part, we'll get at least one 1. 1233fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(NarrowTy, 0); 1234fe6060f1SDimitry Andric SmallVector<Register, 4> Xors; 1235fe6060f1SDimitry Andric for (auto LHSAndRHS : zip(LHSPartRegs, RHSPartRegs)) { 1236fe6060f1SDimitry Andric auto LHS = std::get<0>(LHSAndRHS); 1237fe6060f1SDimitry Andric auto RHS = std::get<1>(LHSAndRHS); 1238fe6060f1SDimitry Andric auto Xor = MIRBuilder.buildXor(NarrowTy, LHS, RHS).getReg(0); 1239fe6060f1SDimitry Andric Xors.push_back(Xor); 1240fe6060f1SDimitry Andric } 1241fe6060f1SDimitry Andric 1242fe6060f1SDimitry Andric // Build a G_XOR for each leftover register. Each G_XOR must be widened 1243fe6060f1SDimitry Andric // to the desired narrow type so that we can OR them together later. 1244fe6060f1SDimitry Andric SmallVector<Register, 4> WidenedXors; 1245fe6060f1SDimitry Andric for (auto LHSAndRHS : zip(LHSLeftoverRegs, RHSLeftoverRegs)) { 1246fe6060f1SDimitry Andric auto LHS = std::get<0>(LHSAndRHS); 1247fe6060f1SDimitry Andric auto RHS = std::get<1>(LHSAndRHS); 1248fe6060f1SDimitry Andric auto Xor = MIRBuilder.buildXor(LeftoverTy, LHS, RHS).getReg(0); 1249fe6060f1SDimitry Andric LLT GCDTy = extractGCDType(WidenedXors, NarrowTy, LeftoverTy, Xor); 1250fe6060f1SDimitry Andric buildLCMMergePieces(LeftoverTy, NarrowTy, GCDTy, WidenedXors, 1251fe6060f1SDimitry Andric /* PadStrategy = */ TargetOpcode::G_ZEXT); 1252fe6060f1SDimitry Andric Xors.insert(Xors.end(), WidenedXors.begin(), WidenedXors.end()); 1253fe6060f1SDimitry Andric } 1254fe6060f1SDimitry Andric 1255fe6060f1SDimitry Andric // Now, for each part we broke up, we know if they are equal/not equal 1256fe6060f1SDimitry Andric // based off the G_XOR. We can OR these all together and compare against 1257fe6060f1SDimitry Andric // 0 to get the result. 1258fe6060f1SDimitry Andric assert(Xors.size() >= 2 && "Should have gotten at least two Xors?"); 1259fe6060f1SDimitry Andric auto Or = MIRBuilder.buildOr(NarrowTy, Xors[0], Xors[1]); 1260fe6060f1SDimitry Andric for (unsigned I = 2, E = Xors.size(); I < E; ++I) 1261fe6060f1SDimitry Andric Or = MIRBuilder.buildOr(NarrowTy, Or, Xors[I]); 1262fe6060f1SDimitry Andric MIRBuilder.buildICmp(Pred, Dst, Or, Zero); 12630b57cec5SDimitry Andric } else { 1264fe6060f1SDimitry Andric // TODO: Handle non-power-of-two types. 1265fe6060f1SDimitry Andric assert(LHSPartRegs.size() == 2 && "Expected exactly 2 LHS part regs?"); 1266fe6060f1SDimitry Andric assert(RHSPartRegs.size() == 2 && "Expected exactly 2 RHS part regs?"); 1267fe6060f1SDimitry Andric Register LHSL = LHSPartRegs[0]; 1268fe6060f1SDimitry Andric Register LHSH = LHSPartRegs[1]; 1269fe6060f1SDimitry Andric Register RHSL = RHSPartRegs[0]; 1270fe6060f1SDimitry Andric Register RHSH = RHSPartRegs[1]; 12718bcb0991SDimitry Andric MachineInstrBuilder CmpH = MIRBuilder.buildICmp(Pred, ResTy, LHSH, RHSH); 12720b57cec5SDimitry Andric MachineInstrBuilder CmpHEQ = 12738bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::Predicate::ICMP_EQ, ResTy, LHSH, RHSH); 12740b57cec5SDimitry Andric MachineInstrBuilder CmpLU = MIRBuilder.buildICmp( 12758bcb0991SDimitry Andric ICmpInst::getUnsignedPredicate(Pred), ResTy, LHSL, RHSL); 1276fe6060f1SDimitry Andric MIRBuilder.buildSelect(Dst, CmpHEQ, CmpLU, CmpH); 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric MI.eraseFromParent(); 12790b57cec5SDimitry Andric return Legalized; 12800b57cec5SDimitry Andric } 12818bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 12828bcb0991SDimitry Andric if (TypeIdx != 0) 12838bcb0991SDimitry Andric return UnableToLegalize; 12848bcb0991SDimitry Andric 12858bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 12868bcb0991SDimitry Andric 12878bcb0991SDimitry Andric // So long as the new type has more bits than the bits we're extending we 12888bcb0991SDimitry Andric // don't need to break it apart. 12898bcb0991SDimitry Andric if (NarrowTy.getScalarSizeInBits() >= SizeInBits) { 12908bcb0991SDimitry Andric Observer.changingInstr(MI); 12918bcb0991SDimitry Andric // We don't lose any non-extension bits by truncating the src and 12928bcb0991SDimitry Andric // sign-extending the dst. 12938bcb0991SDimitry Andric MachineOperand &MO1 = MI.getOperand(1); 12945ffd83dbSDimitry Andric auto TruncMIB = MIRBuilder.buildTrunc(NarrowTy, MO1); 12955ffd83dbSDimitry Andric MO1.setReg(TruncMIB.getReg(0)); 12968bcb0991SDimitry Andric 12978bcb0991SDimitry Andric MachineOperand &MO2 = MI.getOperand(0); 12988bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(NarrowTy); 12998bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 13005ffd83dbSDimitry Andric MIRBuilder.buildSExt(MO2, DstExt); 13018bcb0991SDimitry Andric MO2.setReg(DstExt); 13028bcb0991SDimitry Andric Observer.changedInstr(MI); 13038bcb0991SDimitry Andric return Legalized; 13048bcb0991SDimitry Andric } 13058bcb0991SDimitry Andric 13068bcb0991SDimitry Andric // Break it apart. Components below the extension point are unmodified. The 13078bcb0991SDimitry Andric // component containing the extension point becomes a narrower SEXT_INREG. 13088bcb0991SDimitry Andric // Components above it are ashr'd from the component containing the 13098bcb0991SDimitry Andric // extension point. 13108bcb0991SDimitry Andric if (SizeOp0 % NarrowSize != 0) 13118bcb0991SDimitry Andric return UnableToLegalize; 13128bcb0991SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 13138bcb0991SDimitry Andric 13148bcb0991SDimitry Andric // List the registers where the destination will be scattered. 13158bcb0991SDimitry Andric SmallVector<Register, 2> DstRegs; 13168bcb0991SDimitry Andric // List the registers where the source will be split. 13178bcb0991SDimitry Andric SmallVector<Register, 2> SrcRegs; 13188bcb0991SDimitry Andric 13198bcb0991SDimitry Andric // Create all the temporary registers. 13208bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 13218bcb0991SDimitry Andric Register SrcReg = MRI.createGenericVirtualRegister(NarrowTy); 13228bcb0991SDimitry Andric 13238bcb0991SDimitry Andric SrcRegs.push_back(SrcReg); 13248bcb0991SDimitry Andric } 13258bcb0991SDimitry Andric 13268bcb0991SDimitry Andric // Explode the big arguments into smaller chunks. 13275ffd83dbSDimitry Andric MIRBuilder.buildUnmerge(SrcRegs, MI.getOperand(1)); 13288bcb0991SDimitry Andric 13298bcb0991SDimitry Andric Register AshrCstReg = 13308bcb0991SDimitry Andric MIRBuilder.buildConstant(NarrowTy, NarrowTy.getScalarSizeInBits() - 1) 13315ffd83dbSDimitry Andric .getReg(0); 13328bcb0991SDimitry Andric Register FullExtensionReg = 0; 13338bcb0991SDimitry Andric Register PartialExtensionReg = 0; 13348bcb0991SDimitry Andric 13358bcb0991SDimitry Andric // Do the operation on each small part. 13368bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 13378bcb0991SDimitry Andric if ((i + 1) * NarrowTy.getScalarSizeInBits() < SizeInBits) 13388bcb0991SDimitry Andric DstRegs.push_back(SrcRegs[i]); 13398bcb0991SDimitry Andric else if (i * NarrowTy.getScalarSizeInBits() > SizeInBits) { 13408bcb0991SDimitry Andric assert(PartialExtensionReg && 13418bcb0991SDimitry Andric "Expected to visit partial extension before full"); 13428bcb0991SDimitry Andric if (FullExtensionReg) { 13438bcb0991SDimitry Andric DstRegs.push_back(FullExtensionReg); 13448bcb0991SDimitry Andric continue; 13458bcb0991SDimitry Andric } 13465ffd83dbSDimitry Andric DstRegs.push_back( 13475ffd83dbSDimitry Andric MIRBuilder.buildAShr(NarrowTy, PartialExtensionReg, AshrCstReg) 13485ffd83dbSDimitry Andric .getReg(0)); 13498bcb0991SDimitry Andric FullExtensionReg = DstRegs.back(); 13508bcb0991SDimitry Andric } else { 13518bcb0991SDimitry Andric DstRegs.push_back( 13528bcb0991SDimitry Andric MIRBuilder 13538bcb0991SDimitry Andric .buildInstr( 13548bcb0991SDimitry Andric TargetOpcode::G_SEXT_INREG, {NarrowTy}, 13558bcb0991SDimitry Andric {SrcRegs[i], SizeInBits % NarrowTy.getScalarSizeInBits()}) 13565ffd83dbSDimitry Andric .getReg(0)); 13578bcb0991SDimitry Andric PartialExtensionReg = DstRegs.back(); 13588bcb0991SDimitry Andric } 13598bcb0991SDimitry Andric } 13608bcb0991SDimitry Andric 13618bcb0991SDimitry Andric // Gather the destination registers into the final destination. 13628bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 13638bcb0991SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 13648bcb0991SDimitry Andric MI.eraseFromParent(); 13658bcb0991SDimitry Andric return Legalized; 13668bcb0991SDimitry Andric } 1367480093f4SDimitry Andric case TargetOpcode::G_BSWAP: 1368480093f4SDimitry Andric case TargetOpcode::G_BITREVERSE: { 1369480093f4SDimitry Andric if (SizeOp0 % NarrowSize != 0) 1370480093f4SDimitry Andric return UnableToLegalize; 1371480093f4SDimitry Andric 1372480093f4SDimitry Andric Observer.changingInstr(MI); 1373480093f4SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 1374480093f4SDimitry Andric unsigned NumParts = SizeOp0 / NarrowSize; 1375480093f4SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 1376480093f4SDimitry Andric 1377480093f4SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 1378480093f4SDimitry Andric auto DstPart = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, 1379480093f4SDimitry Andric {SrcRegs[NumParts - 1 - i]}); 1380480093f4SDimitry Andric DstRegs.push_back(DstPart.getReg(0)); 1381480093f4SDimitry Andric } 1382480093f4SDimitry Andric 13835ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), DstRegs); 1384480093f4SDimitry Andric 1385480093f4SDimitry Andric Observer.changedInstr(MI); 1386480093f4SDimitry Andric MI.eraseFromParent(); 1387480093f4SDimitry Andric return Legalized; 1388480093f4SDimitry Andric } 1389e8d8bef9SDimitry Andric case TargetOpcode::G_PTR_ADD: 13905ffd83dbSDimitry Andric case TargetOpcode::G_PTRMASK: { 13915ffd83dbSDimitry Andric if (TypeIdx != 1) 13925ffd83dbSDimitry Andric return UnableToLegalize; 13935ffd83dbSDimitry Andric Observer.changingInstr(MI); 13945ffd83dbSDimitry Andric narrowScalarSrc(MI, NarrowTy, 2); 13955ffd83dbSDimitry Andric Observer.changedInstr(MI); 13965ffd83dbSDimitry Andric return Legalized; 13970b57cec5SDimitry Andric } 139823408297SDimitry Andric case TargetOpcode::G_FPTOUI: 139923408297SDimitry Andric case TargetOpcode::G_FPTOSI: 140023408297SDimitry Andric return narrowScalarFPTOI(MI, TypeIdx, NarrowTy); 1401e8d8bef9SDimitry Andric case TargetOpcode::G_FPEXT: 1402e8d8bef9SDimitry Andric if (TypeIdx != 0) 1403e8d8bef9SDimitry Andric return UnableToLegalize; 1404e8d8bef9SDimitry Andric Observer.changingInstr(MI); 1405e8d8bef9SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_FPEXT); 1406e8d8bef9SDimitry Andric Observer.changedInstr(MI); 1407e8d8bef9SDimitry Andric return Legalized; 14080b57cec5SDimitry Andric } 14095ffd83dbSDimitry Andric } 14105ffd83dbSDimitry Andric 14115ffd83dbSDimitry Andric Register LegalizerHelper::coerceToScalar(Register Val) { 14125ffd83dbSDimitry Andric LLT Ty = MRI.getType(Val); 14135ffd83dbSDimitry Andric if (Ty.isScalar()) 14145ffd83dbSDimitry Andric return Val; 14155ffd83dbSDimitry Andric 14165ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 14175ffd83dbSDimitry Andric LLT NewTy = LLT::scalar(Ty.getSizeInBits()); 14185ffd83dbSDimitry Andric if (Ty.isPointer()) { 14195ffd83dbSDimitry Andric if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace())) 14205ffd83dbSDimitry Andric return Register(); 14215ffd83dbSDimitry Andric return MIRBuilder.buildPtrToInt(NewTy, Val).getReg(0); 14225ffd83dbSDimitry Andric } 14235ffd83dbSDimitry Andric 14245ffd83dbSDimitry Andric Register NewVal = Val; 14255ffd83dbSDimitry Andric 14265ffd83dbSDimitry Andric assert(Ty.isVector()); 14275ffd83dbSDimitry Andric LLT EltTy = Ty.getElementType(); 14285ffd83dbSDimitry Andric if (EltTy.isPointer()) 14295ffd83dbSDimitry Andric NewVal = MIRBuilder.buildPtrToInt(NewTy, NewVal).getReg(0); 14305ffd83dbSDimitry Andric return MIRBuilder.buildBitcast(NewTy, NewVal).getReg(0); 14315ffd83dbSDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy, 14340b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 14350b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14365ffd83dbSDimitry Andric auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO}); 14375ffd83dbSDimitry Andric MO.setReg(ExtB.getReg(0)); 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy, 14410b57cec5SDimitry Andric unsigned OpIdx) { 14420b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14435ffd83dbSDimitry Andric auto ExtB = MIRBuilder.buildTrunc(NarrowTy, MO); 14445ffd83dbSDimitry Andric MO.setReg(ExtB.getReg(0)); 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy, 14480b57cec5SDimitry Andric unsigned OpIdx, unsigned TruncOpcode) { 14490b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14500b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 14510b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 14525ffd83dbSDimitry Andric MIRBuilder.buildInstr(TruncOpcode, {MO}, {DstExt}); 14530b57cec5SDimitry Andric MO.setReg(DstExt); 14540b57cec5SDimitry Andric } 14550b57cec5SDimitry Andric 14560b57cec5SDimitry Andric void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy, 14570b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 14580b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14590b57cec5SDimitry Andric Register DstTrunc = MRI.createGenericVirtualRegister(NarrowTy); 14600b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 14615ffd83dbSDimitry Andric MIRBuilder.buildInstr(ExtOpcode, {MO}, {DstTrunc}); 14620b57cec5SDimitry Andric MO.setReg(DstTrunc); 14630b57cec5SDimitry Andric } 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorDst(MachineInstr &MI, LLT WideTy, 14660b57cec5SDimitry Andric unsigned OpIdx) { 14670b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14680b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 14690eae32dcSDimitry Andric Register Dst = MO.getReg(); 14700eae32dcSDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 14710eae32dcSDimitry Andric MO.setReg(DstExt); 14720eae32dcSDimitry Andric MIRBuilder.buildDeleteTrailingVectorElements(Dst, DstExt); 14730b57cec5SDimitry Andric } 14740b57cec5SDimitry Andric 14750b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorSrc(MachineInstr &MI, LLT MoreTy, 14760b57cec5SDimitry Andric unsigned OpIdx) { 14770b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14780eae32dcSDimitry Andric SmallVector<Register, 8> Regs; 14790eae32dcSDimitry Andric MO.setReg(MIRBuilder.buildPadVectorWithUndefElements(MoreTy, MO).getReg(0)); 14800b57cec5SDimitry Andric } 14810b57cec5SDimitry Andric 14825ffd83dbSDimitry Andric void LegalizerHelper::bitcastSrc(MachineInstr &MI, LLT CastTy, unsigned OpIdx) { 14835ffd83dbSDimitry Andric MachineOperand &Op = MI.getOperand(OpIdx); 14845ffd83dbSDimitry Andric Op.setReg(MIRBuilder.buildBitcast(CastTy, Op).getReg(0)); 14855ffd83dbSDimitry Andric } 14865ffd83dbSDimitry Andric 14875ffd83dbSDimitry Andric void LegalizerHelper::bitcastDst(MachineInstr &MI, LLT CastTy, unsigned OpIdx) { 14885ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14895ffd83dbSDimitry Andric Register CastDst = MRI.createGenericVirtualRegister(CastTy); 14905ffd83dbSDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 14915ffd83dbSDimitry Andric MIRBuilder.buildBitcast(MO, CastDst); 14925ffd83dbSDimitry Andric MO.setReg(CastDst); 14935ffd83dbSDimitry Andric } 14945ffd83dbSDimitry Andric 14950b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 14960b57cec5SDimitry Andric LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx, 14970b57cec5SDimitry Andric LLT WideTy) { 14980b57cec5SDimitry Andric if (TypeIdx != 1) 14990b57cec5SDimitry Andric return UnableToLegalize; 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 15020b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 15030b57cec5SDimitry Andric if (DstTy.isVector()) 15040b57cec5SDimitry Andric return UnableToLegalize; 15050b57cec5SDimitry Andric 15060b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 15070b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src1); 15080b57cec5SDimitry Andric const int DstSize = DstTy.getSizeInBits(); 15090b57cec5SDimitry Andric const int SrcSize = SrcTy.getSizeInBits(); 15100b57cec5SDimitry Andric const int WideSize = WideTy.getSizeInBits(); 15110b57cec5SDimitry Andric const int NumMerge = (DstSize + WideSize - 1) / WideSize; 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric unsigned NumOps = MI.getNumOperands(); 15140b57cec5SDimitry Andric unsigned NumSrc = MI.getNumOperands() - 1; 15150b57cec5SDimitry Andric unsigned PartSize = DstTy.getSizeInBits() / NumSrc; 15160b57cec5SDimitry Andric 15170b57cec5SDimitry Andric if (WideSize >= DstSize) { 15180b57cec5SDimitry Andric // Directly pack the bits in the target type. 15190b57cec5SDimitry Andric Register ResultReg = MIRBuilder.buildZExt(WideTy, Src1).getReg(0); 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 15220b57cec5SDimitry Andric const unsigned Offset = (I - 1) * PartSize; 15230b57cec5SDimitry Andric 15240b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 15250b57cec5SDimitry Andric assert(MRI.getType(SrcReg) == LLT::scalar(PartSize)); 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg); 15280b57cec5SDimitry Andric 15298bcb0991SDimitry Andric Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg : 15300b57cec5SDimitry Andric MRI.createGenericVirtualRegister(WideTy); 15310b57cec5SDimitry Andric 15320b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset); 15330b57cec5SDimitry Andric auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt); 15340b57cec5SDimitry Andric MIRBuilder.buildOr(NextResult, ResultReg, Shl); 15350b57cec5SDimitry Andric ResultReg = NextResult; 15360b57cec5SDimitry Andric } 15370b57cec5SDimitry Andric 15380b57cec5SDimitry Andric if (WideSize > DstSize) 15390b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ResultReg); 15408bcb0991SDimitry Andric else if (DstTy.isPointer()) 15418bcb0991SDimitry Andric MIRBuilder.buildIntToPtr(DstReg, ResultReg); 15420b57cec5SDimitry Andric 15430b57cec5SDimitry Andric MI.eraseFromParent(); 15440b57cec5SDimitry Andric return Legalized; 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric // Unmerge the original values to the GCD type, and recombine to the next 15480b57cec5SDimitry Andric // multiple greater than the original type. 15490b57cec5SDimitry Andric // 15500b57cec5SDimitry Andric // %3:_(s12) = G_MERGE_VALUES %0:_(s4), %1:_(s4), %2:_(s4) -> s6 15510b57cec5SDimitry Andric // %4:_(s2), %5:_(s2) = G_UNMERGE_VALUES %0 15520b57cec5SDimitry Andric // %6:_(s2), %7:_(s2) = G_UNMERGE_VALUES %1 15530b57cec5SDimitry Andric // %8:_(s2), %9:_(s2) = G_UNMERGE_VALUES %2 15540b57cec5SDimitry Andric // %10:_(s6) = G_MERGE_VALUES %4, %5, %6 15550b57cec5SDimitry Andric // %11:_(s6) = G_MERGE_VALUES %7, %8, %9 15560b57cec5SDimitry Andric // %12:_(s12) = G_MERGE_VALUES %10, %11 15570b57cec5SDimitry Andric // 15580b57cec5SDimitry Andric // Padding with undef if necessary: 15590b57cec5SDimitry Andric // 15600b57cec5SDimitry Andric // %2:_(s8) = G_MERGE_VALUES %0:_(s4), %1:_(s4) -> s6 15610b57cec5SDimitry Andric // %3:_(s2), %4:_(s2) = G_UNMERGE_VALUES %0 15620b57cec5SDimitry Andric // %5:_(s2), %6:_(s2) = G_UNMERGE_VALUES %1 15630b57cec5SDimitry Andric // %7:_(s2) = G_IMPLICIT_DEF 15640b57cec5SDimitry Andric // %8:_(s6) = G_MERGE_VALUES %3, %4, %5 15650b57cec5SDimitry Andric // %9:_(s6) = G_MERGE_VALUES %6, %7, %7 15660b57cec5SDimitry Andric // %10:_(s12) = G_MERGE_VALUES %8, %9 15670b57cec5SDimitry Andric 15680b57cec5SDimitry Andric const int GCD = greatestCommonDivisor(SrcSize, WideSize); 15690b57cec5SDimitry Andric LLT GCDTy = LLT::scalar(GCD); 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric SmallVector<Register, 8> Parts; 15720b57cec5SDimitry Andric SmallVector<Register, 8> NewMergeRegs; 15730b57cec5SDimitry Andric SmallVector<Register, 8> Unmerges; 15740b57cec5SDimitry Andric LLT WideDstTy = LLT::scalar(NumMerge * WideSize); 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric // Decompose the original operands if they don't evenly divide. 15774824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) { 15784824e7fdSDimitry Andric Register SrcReg = MO.getReg(); 15790b57cec5SDimitry Andric if (GCD == SrcSize) { 15800b57cec5SDimitry Andric Unmerges.push_back(SrcReg); 15810b57cec5SDimitry Andric } else { 15820b57cec5SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 15830b57cec5SDimitry Andric for (int J = 0, JE = Unmerge->getNumOperands() - 1; J != JE; ++J) 15840b57cec5SDimitry Andric Unmerges.push_back(Unmerge.getReg(J)); 15850b57cec5SDimitry Andric } 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andric // Pad with undef to the next size that is a multiple of the requested size. 15890b57cec5SDimitry Andric if (static_cast<int>(Unmerges.size()) != NumMerge * WideSize) { 15900b57cec5SDimitry Andric Register UndefReg = MIRBuilder.buildUndef(GCDTy).getReg(0); 15910b57cec5SDimitry Andric for (int I = Unmerges.size(); I != NumMerge * WideSize; ++I) 15920b57cec5SDimitry Andric Unmerges.push_back(UndefReg); 15930b57cec5SDimitry Andric } 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric const int PartsPerGCD = WideSize / GCD; 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric // Build merges of each piece. 15980b57cec5SDimitry Andric ArrayRef<Register> Slicer(Unmerges); 15990b57cec5SDimitry Andric for (int I = 0; I != NumMerge; ++I, Slicer = Slicer.drop_front(PartsPerGCD)) { 16000b57cec5SDimitry Andric auto Merge = MIRBuilder.buildMerge(WideTy, Slicer.take_front(PartsPerGCD)); 16010b57cec5SDimitry Andric NewMergeRegs.push_back(Merge.getReg(0)); 16020b57cec5SDimitry Andric } 16030b57cec5SDimitry Andric 16040b57cec5SDimitry Andric // A truncate may be necessary if the requested type doesn't evenly divide the 16050b57cec5SDimitry Andric // original result type. 16060b57cec5SDimitry Andric if (DstTy.getSizeInBits() == WideDstTy.getSizeInBits()) { 16070b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, NewMergeRegs); 16080b57cec5SDimitry Andric } else { 16090b57cec5SDimitry Andric auto FinalMerge = MIRBuilder.buildMerge(WideDstTy, NewMergeRegs); 16100b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, FinalMerge.getReg(0)); 16110b57cec5SDimitry Andric } 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric MI.eraseFromParent(); 16140b57cec5SDimitry Andric return Legalized; 16150b57cec5SDimitry Andric } 16160b57cec5SDimitry Andric 16170b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 16180b57cec5SDimitry Andric LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx, 16190b57cec5SDimitry Andric LLT WideTy) { 16200b57cec5SDimitry Andric if (TypeIdx != 0) 16210b57cec5SDimitry Andric return UnableToLegalize; 16220b57cec5SDimitry Andric 16235ffd83dbSDimitry Andric int NumDst = MI.getNumOperands() - 1; 16240b57cec5SDimitry Andric Register SrcReg = MI.getOperand(NumDst).getReg(); 16250b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 16265ffd83dbSDimitry Andric if (SrcTy.isVector()) 16270b57cec5SDimitry Andric return UnableToLegalize; 16280b57cec5SDimitry Andric 16290b57cec5SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 16300b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 16310b57cec5SDimitry Andric if (!DstTy.isScalar()) 16320b57cec5SDimitry Andric return UnableToLegalize; 16330b57cec5SDimitry Andric 16345ffd83dbSDimitry Andric if (WideTy.getSizeInBits() >= SrcTy.getSizeInBits()) { 16355ffd83dbSDimitry Andric if (SrcTy.isPointer()) { 16365ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 16375ffd83dbSDimitry Andric if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) { 16385ffd83dbSDimitry Andric LLVM_DEBUG( 16395ffd83dbSDimitry Andric dbgs() << "Not casting non-integral address space integer\n"); 16405ffd83dbSDimitry Andric return UnableToLegalize; 16410b57cec5SDimitry Andric } 16420b57cec5SDimitry Andric 16435ffd83dbSDimitry Andric SrcTy = LLT::scalar(SrcTy.getSizeInBits()); 16445ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildPtrToInt(SrcTy, SrcReg).getReg(0); 16455ffd83dbSDimitry Andric } 16460b57cec5SDimitry Andric 16475ffd83dbSDimitry Andric // Widen SrcTy to WideTy. This does not affect the result, but since the 16485ffd83dbSDimitry Andric // user requested this size, it is probably better handled than SrcTy and 164904eeddc0SDimitry Andric // should reduce the total number of legalization artifacts. 16505ffd83dbSDimitry Andric if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) { 16515ffd83dbSDimitry Andric SrcTy = WideTy; 16525ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0); 16535ffd83dbSDimitry Andric } 16540b57cec5SDimitry Andric 16555ffd83dbSDimitry Andric // Theres no unmerge type to target. Directly extract the bits from the 16565ffd83dbSDimitry Andric // source type 16575ffd83dbSDimitry Andric unsigned DstSize = DstTy.getSizeInBits(); 16580b57cec5SDimitry Andric 16595ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst0Reg, SrcReg); 16605ffd83dbSDimitry Andric for (int I = 1; I != NumDst; ++I) { 16615ffd83dbSDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(SrcTy, DstSize * I); 16625ffd83dbSDimitry Andric auto Shr = MIRBuilder.buildLShr(SrcTy, SrcReg, ShiftAmt); 16635ffd83dbSDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(I), Shr); 16645ffd83dbSDimitry Andric } 16655ffd83dbSDimitry Andric 16665ffd83dbSDimitry Andric MI.eraseFromParent(); 16675ffd83dbSDimitry Andric return Legalized; 16685ffd83dbSDimitry Andric } 16695ffd83dbSDimitry Andric 16705ffd83dbSDimitry Andric // Extend the source to a wider type. 16715ffd83dbSDimitry Andric LLT LCMTy = getLCMType(SrcTy, WideTy); 16725ffd83dbSDimitry Andric 16735ffd83dbSDimitry Andric Register WideSrc = SrcReg; 16745ffd83dbSDimitry Andric if (LCMTy.getSizeInBits() != SrcTy.getSizeInBits()) { 16755ffd83dbSDimitry Andric // TODO: If this is an integral address space, cast to integer and anyext. 16765ffd83dbSDimitry Andric if (SrcTy.isPointer()) { 16775ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Widening pointer source types not implemented\n"); 16785ffd83dbSDimitry Andric return UnableToLegalize; 16795ffd83dbSDimitry Andric } 16805ffd83dbSDimitry Andric 16815ffd83dbSDimitry Andric WideSrc = MIRBuilder.buildAnyExt(LCMTy, WideSrc).getReg(0); 16825ffd83dbSDimitry Andric } 16835ffd83dbSDimitry Andric 16845ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(WideTy, WideSrc); 16855ffd83dbSDimitry Andric 1686e8d8bef9SDimitry Andric // Create a sequence of unmerges and merges to the original results. Since we 1687e8d8bef9SDimitry Andric // may have widened the source, we will need to pad the results with dead defs 1688e8d8bef9SDimitry Andric // to cover the source register. 1689e8d8bef9SDimitry Andric // e.g. widen s48 to s64: 1690e8d8bef9SDimitry Andric // %1:_(s48), %2:_(s48) = G_UNMERGE_VALUES %0:_(s96) 16915ffd83dbSDimitry Andric // 16925ffd83dbSDimitry Andric // => 1693e8d8bef9SDimitry Andric // %4:_(s192) = G_ANYEXT %0:_(s96) 1694e8d8bef9SDimitry Andric // %5:_(s64), %6, %7 = G_UNMERGE_VALUES %4 ; Requested unmerge 1695e8d8bef9SDimitry Andric // ; unpack to GCD type, with extra dead defs 1696e8d8bef9SDimitry Andric // %8:_(s16), %9, %10, %11 = G_UNMERGE_VALUES %5:_(s64) 1697e8d8bef9SDimitry Andric // %12:_(s16), %13, dead %14, dead %15 = G_UNMERGE_VALUES %6:_(s64) 1698e8d8bef9SDimitry Andric // dead %16:_(s16), dead %17, dead %18, dead %18 = G_UNMERGE_VALUES %7:_(s64) 1699e8d8bef9SDimitry Andric // %1:_(s48) = G_MERGE_VALUES %8:_(s16), %9, %10 ; Remerge to destination 1700e8d8bef9SDimitry Andric // %2:_(s48) = G_MERGE_VALUES %11:_(s16), %12, %13 ; Remerge to destination 1701e8d8bef9SDimitry Andric const LLT GCDTy = getGCDType(WideTy, DstTy); 17025ffd83dbSDimitry Andric const int NumUnmerge = Unmerge->getNumOperands() - 1; 1703e8d8bef9SDimitry Andric const int PartsPerRemerge = DstTy.getSizeInBits() / GCDTy.getSizeInBits(); 1704e8d8bef9SDimitry Andric 1705e8d8bef9SDimitry Andric // Directly unmerge to the destination without going through a GCD type 1706e8d8bef9SDimitry Andric // if possible 1707e8d8bef9SDimitry Andric if (PartsPerRemerge == 1) { 17085ffd83dbSDimitry Andric const int PartsPerUnmerge = WideTy.getSizeInBits() / DstTy.getSizeInBits(); 17095ffd83dbSDimitry Andric 17105ffd83dbSDimitry Andric for (int I = 0; I != NumUnmerge; ++I) { 17115ffd83dbSDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 17125ffd83dbSDimitry Andric 17135ffd83dbSDimitry Andric for (int J = 0; J != PartsPerUnmerge; ++J) { 17145ffd83dbSDimitry Andric int Idx = I * PartsPerUnmerge + J; 17155ffd83dbSDimitry Andric if (Idx < NumDst) 17165ffd83dbSDimitry Andric MIB.addDef(MI.getOperand(Idx).getReg()); 17175ffd83dbSDimitry Andric else { 17185ffd83dbSDimitry Andric // Create dead def for excess components. 17195ffd83dbSDimitry Andric MIB.addDef(MRI.createGenericVirtualRegister(DstTy)); 17205ffd83dbSDimitry Andric } 17215ffd83dbSDimitry Andric } 17225ffd83dbSDimitry Andric 17235ffd83dbSDimitry Andric MIB.addUse(Unmerge.getReg(I)); 17245ffd83dbSDimitry Andric } 1725e8d8bef9SDimitry Andric } else { 1726e8d8bef9SDimitry Andric SmallVector<Register, 16> Parts; 1727e8d8bef9SDimitry Andric for (int J = 0; J != NumUnmerge; ++J) 1728e8d8bef9SDimitry Andric extractGCDType(Parts, GCDTy, Unmerge.getReg(J)); 1729e8d8bef9SDimitry Andric 1730e8d8bef9SDimitry Andric SmallVector<Register, 8> RemergeParts; 1731e8d8bef9SDimitry Andric for (int I = 0; I != NumDst; ++I) { 1732e8d8bef9SDimitry Andric for (int J = 0; J < PartsPerRemerge; ++J) { 1733e8d8bef9SDimitry Andric const int Idx = I * PartsPerRemerge + J; 1734e8d8bef9SDimitry Andric RemergeParts.emplace_back(Parts[Idx]); 1735e8d8bef9SDimitry Andric } 1736e8d8bef9SDimitry Andric 1737e8d8bef9SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(I).getReg(), RemergeParts); 1738e8d8bef9SDimitry Andric RemergeParts.clear(); 1739e8d8bef9SDimitry Andric } 1740e8d8bef9SDimitry Andric } 17415ffd83dbSDimitry Andric 17425ffd83dbSDimitry Andric MI.eraseFromParent(); 17430b57cec5SDimitry Andric return Legalized; 17440b57cec5SDimitry Andric } 17450b57cec5SDimitry Andric 17460b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 17470b57cec5SDimitry Andric LegalizerHelper::widenScalarExtract(MachineInstr &MI, unsigned TypeIdx, 17480b57cec5SDimitry Andric LLT WideTy) { 17490b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 17500b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 17510b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 17520b57cec5SDimitry Andric 17530b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 17540b57cec5SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 17550b57cec5SDimitry Andric 17560b57cec5SDimitry Andric if (TypeIdx == 0) { 17570b57cec5SDimitry Andric if (SrcTy.isVector() || DstTy.isVector()) 17580b57cec5SDimitry Andric return UnableToLegalize; 17590b57cec5SDimitry Andric 17600b57cec5SDimitry Andric SrcOp Src(SrcReg); 17610b57cec5SDimitry Andric if (SrcTy.isPointer()) { 17620b57cec5SDimitry Andric // Extracts from pointers can be handled only if they are really just 17630b57cec5SDimitry Andric // simple integers. 17640b57cec5SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 17650b57cec5SDimitry Andric if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) 17660b57cec5SDimitry Andric return UnableToLegalize; 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric LLT SrcAsIntTy = LLT::scalar(SrcTy.getSizeInBits()); 17690b57cec5SDimitry Andric Src = MIRBuilder.buildPtrToInt(SrcAsIntTy, Src); 17700b57cec5SDimitry Andric SrcTy = SrcAsIntTy; 17710b57cec5SDimitry Andric } 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric if (DstTy.isPointer()) 17740b57cec5SDimitry Andric return UnableToLegalize; 17750b57cec5SDimitry Andric 17760b57cec5SDimitry Andric if (Offset == 0) { 17770b57cec5SDimitry Andric // Avoid a shift in the degenerate case. 17780b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, 17790b57cec5SDimitry Andric MIRBuilder.buildAnyExtOrTrunc(WideTy, Src)); 17800b57cec5SDimitry Andric MI.eraseFromParent(); 17810b57cec5SDimitry Andric return Legalized; 17820b57cec5SDimitry Andric } 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric // Do a shift in the source type. 17850b57cec5SDimitry Andric LLT ShiftTy = SrcTy; 17860b57cec5SDimitry Andric if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) { 17870b57cec5SDimitry Andric Src = MIRBuilder.buildAnyExt(WideTy, Src); 17880b57cec5SDimitry Andric ShiftTy = WideTy; 1789e8d8bef9SDimitry Andric } 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric auto LShr = MIRBuilder.buildLShr( 17920b57cec5SDimitry Andric ShiftTy, Src, MIRBuilder.buildConstant(ShiftTy, Offset)); 17930b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, LShr); 17940b57cec5SDimitry Andric MI.eraseFromParent(); 17950b57cec5SDimitry Andric return Legalized; 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric if (SrcTy.isScalar()) { 17990b57cec5SDimitry Andric Observer.changingInstr(MI); 18000b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 18010b57cec5SDimitry Andric Observer.changedInstr(MI); 18020b57cec5SDimitry Andric return Legalized; 18030b57cec5SDimitry Andric } 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric if (!SrcTy.isVector()) 18060b57cec5SDimitry Andric return UnableToLegalize; 18070b57cec5SDimitry Andric 18080b57cec5SDimitry Andric if (DstTy != SrcTy.getElementType()) 18090b57cec5SDimitry Andric return UnableToLegalize; 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric if (Offset % SrcTy.getScalarSizeInBits() != 0) 18120b57cec5SDimitry Andric return UnableToLegalize; 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric Observer.changingInstr(MI); 18150b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 18160b57cec5SDimitry Andric 18170b57cec5SDimitry Andric MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) * 18180b57cec5SDimitry Andric Offset); 18190b57cec5SDimitry Andric widenScalarDst(MI, WideTy.getScalarType(), 0); 18200b57cec5SDimitry Andric Observer.changedInstr(MI); 18210b57cec5SDimitry Andric return Legalized; 18220b57cec5SDimitry Andric } 18230b57cec5SDimitry Andric 18240b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 18250b57cec5SDimitry Andric LegalizerHelper::widenScalarInsert(MachineInstr &MI, unsigned TypeIdx, 18260b57cec5SDimitry Andric LLT WideTy) { 1827e8d8bef9SDimitry Andric if (TypeIdx != 0 || WideTy.isVector()) 18280b57cec5SDimitry Andric return UnableToLegalize; 18290b57cec5SDimitry Andric Observer.changingInstr(MI); 18300b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 18310b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 18320b57cec5SDimitry Andric Observer.changedInstr(MI); 18330b57cec5SDimitry Andric return Legalized; 18340b57cec5SDimitry Andric } 18350b57cec5SDimitry Andric 18360b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 1837fe6060f1SDimitry Andric LegalizerHelper::widenScalarAddSubOverflow(MachineInstr &MI, unsigned TypeIdx, 1838e8d8bef9SDimitry Andric LLT WideTy) { 1839fe6060f1SDimitry Andric unsigned Opcode; 1840fe6060f1SDimitry Andric unsigned ExtOpcode; 1841fe6060f1SDimitry Andric Optional<Register> CarryIn = None; 1842fe6060f1SDimitry Andric switch (MI.getOpcode()) { 1843fe6060f1SDimitry Andric default: 1844fe6060f1SDimitry Andric llvm_unreachable("Unexpected opcode!"); 1845fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 1846fe6060f1SDimitry Andric Opcode = TargetOpcode::G_ADD; 1847fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1848fe6060f1SDimitry Andric break; 1849fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 1850fe6060f1SDimitry Andric Opcode = TargetOpcode::G_SUB; 1851fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1852fe6060f1SDimitry Andric break; 1853fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 1854fe6060f1SDimitry Andric Opcode = TargetOpcode::G_ADD; 1855fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1856fe6060f1SDimitry Andric break; 1857fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 1858fe6060f1SDimitry Andric Opcode = TargetOpcode::G_SUB; 1859fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1860fe6060f1SDimitry Andric break; 1861fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 1862fe6060f1SDimitry Andric Opcode = TargetOpcode::G_UADDE; 1863fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1864fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1865fe6060f1SDimitry Andric break; 1866fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 1867fe6060f1SDimitry Andric Opcode = TargetOpcode::G_USUBE; 1868fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1869fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1870fe6060f1SDimitry Andric break; 1871fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 1872fe6060f1SDimitry Andric Opcode = TargetOpcode::G_UADDE; 1873fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1874fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1875fe6060f1SDimitry Andric break; 1876fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 1877fe6060f1SDimitry Andric Opcode = TargetOpcode::G_USUBE; 1878fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1879fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1880fe6060f1SDimitry Andric break; 1881fe6060f1SDimitry Andric } 1882fe6060f1SDimitry Andric 188381ad6265SDimitry Andric if (TypeIdx == 1) { 188481ad6265SDimitry Andric unsigned BoolExtOp = MIRBuilder.getBoolExtOp(WideTy.isVector(), false); 188581ad6265SDimitry Andric 188681ad6265SDimitry Andric Observer.changingInstr(MI); 188781ad6265SDimitry Andric widenScalarDst(MI, WideTy, 1); 188881ad6265SDimitry Andric if (CarryIn) 188981ad6265SDimitry Andric widenScalarSrc(MI, WideTy, 4, BoolExtOp); 189081ad6265SDimitry Andric 189181ad6265SDimitry Andric Observer.changedInstr(MI); 189281ad6265SDimitry Andric return Legalized; 189381ad6265SDimitry Andric } 189481ad6265SDimitry Andric 1895e8d8bef9SDimitry Andric auto LHSExt = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MI.getOperand(2)}); 1896e8d8bef9SDimitry Andric auto RHSExt = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MI.getOperand(3)}); 1897e8d8bef9SDimitry Andric // Do the arithmetic in the larger type. 1898fe6060f1SDimitry Andric Register NewOp; 1899fe6060f1SDimitry Andric if (CarryIn) { 1900fe6060f1SDimitry Andric LLT CarryOutTy = MRI.getType(MI.getOperand(1).getReg()); 1901fe6060f1SDimitry Andric NewOp = MIRBuilder 1902fe6060f1SDimitry Andric .buildInstr(Opcode, {WideTy, CarryOutTy}, 1903fe6060f1SDimitry Andric {LHSExt, RHSExt, *CarryIn}) 1904fe6060f1SDimitry Andric .getReg(0); 1905fe6060f1SDimitry Andric } else { 1906fe6060f1SDimitry Andric NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSExt, RHSExt}).getReg(0); 1907fe6060f1SDimitry Andric } 1908e8d8bef9SDimitry Andric LLT OrigTy = MRI.getType(MI.getOperand(0).getReg()); 1909e8d8bef9SDimitry Andric auto TruncOp = MIRBuilder.buildTrunc(OrigTy, NewOp); 1910e8d8bef9SDimitry Andric auto ExtOp = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {TruncOp}); 1911e8d8bef9SDimitry Andric // There is no overflow if the ExtOp is the same as NewOp. 1912e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1), NewOp, ExtOp); 1913e8d8bef9SDimitry Andric // Now trunc the NewOp to the original result. 1914e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(0), NewOp); 1915e8d8bef9SDimitry Andric MI.eraseFromParent(); 1916e8d8bef9SDimitry Andric return Legalized; 1917e8d8bef9SDimitry Andric } 1918e8d8bef9SDimitry Andric 1919e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 1920e8d8bef9SDimitry Andric LegalizerHelper::widenScalarAddSubShlSat(MachineInstr &MI, unsigned TypeIdx, 19215ffd83dbSDimitry Andric LLT WideTy) { 19225ffd83dbSDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SADDSAT || 1923e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_SSUBSAT || 1924e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_SSHLSAT; 1925e8d8bef9SDimitry Andric bool IsShift = MI.getOpcode() == TargetOpcode::G_SSHLSAT || 1926e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_USHLSAT; 19275ffd83dbSDimitry Andric // We can convert this to: 19285ffd83dbSDimitry Andric // 1. Any extend iN to iM 19295ffd83dbSDimitry Andric // 2. SHL by M-N 1930e8d8bef9SDimitry Andric // 3. [US][ADD|SUB|SHL]SAT 19315ffd83dbSDimitry Andric // 4. L/ASHR by M-N 19325ffd83dbSDimitry Andric // 19335ffd83dbSDimitry Andric // It may be more efficient to lower this to a min and a max operation in 19345ffd83dbSDimitry Andric // the higher precision arithmetic if the promoted operation isn't legal, 19355ffd83dbSDimitry Andric // but this decision is up to the target's lowering request. 19365ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 19370b57cec5SDimitry Andric 19385ffd83dbSDimitry Andric unsigned NewBits = WideTy.getScalarSizeInBits(); 19395ffd83dbSDimitry Andric unsigned SHLAmount = NewBits - MRI.getType(DstReg).getScalarSizeInBits(); 19405ffd83dbSDimitry Andric 1941e8d8bef9SDimitry Andric // Shifts must zero-extend the RHS to preserve the unsigned quantity, and 1942e8d8bef9SDimitry Andric // must not left shift the RHS to preserve the shift amount. 19435ffd83dbSDimitry Andric auto LHS = MIRBuilder.buildAnyExt(WideTy, MI.getOperand(1)); 1944e8d8bef9SDimitry Andric auto RHS = IsShift ? MIRBuilder.buildZExt(WideTy, MI.getOperand(2)) 1945e8d8bef9SDimitry Andric : MIRBuilder.buildAnyExt(WideTy, MI.getOperand(2)); 19465ffd83dbSDimitry Andric auto ShiftK = MIRBuilder.buildConstant(WideTy, SHLAmount); 19475ffd83dbSDimitry Andric auto ShiftL = MIRBuilder.buildShl(WideTy, LHS, ShiftK); 1948e8d8bef9SDimitry Andric auto ShiftR = IsShift ? RHS : MIRBuilder.buildShl(WideTy, RHS, ShiftK); 19495ffd83dbSDimitry Andric 19505ffd83dbSDimitry Andric auto WideInst = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, 19515ffd83dbSDimitry Andric {ShiftL, ShiftR}, MI.getFlags()); 19525ffd83dbSDimitry Andric 19535ffd83dbSDimitry Andric // Use a shift that will preserve the number of sign bits when the trunc is 19545ffd83dbSDimitry Andric // folded away. 19555ffd83dbSDimitry Andric auto Result = IsSigned ? MIRBuilder.buildAShr(WideTy, WideInst, ShiftK) 19565ffd83dbSDimitry Andric : MIRBuilder.buildLShr(WideTy, WideInst, ShiftK); 19575ffd83dbSDimitry Andric 19585ffd83dbSDimitry Andric MIRBuilder.buildTrunc(DstReg, Result); 19595ffd83dbSDimitry Andric MI.eraseFromParent(); 19605ffd83dbSDimitry Andric return Legalized; 19615ffd83dbSDimitry Andric } 19625ffd83dbSDimitry Andric 19635ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 1964fe6060f1SDimitry Andric LegalizerHelper::widenScalarMulo(MachineInstr &MI, unsigned TypeIdx, 1965fe6060f1SDimitry Andric LLT WideTy) { 196681ad6265SDimitry Andric if (TypeIdx == 1) { 196781ad6265SDimitry Andric Observer.changingInstr(MI); 196881ad6265SDimitry Andric widenScalarDst(MI, WideTy, 1); 196981ad6265SDimitry Andric Observer.changedInstr(MI); 197081ad6265SDimitry Andric return Legalized; 197181ad6265SDimitry Andric } 1972fe6060f1SDimitry Andric 1973fe6060f1SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SMULO; 1974fe6060f1SDimitry Andric Register Result = MI.getOperand(0).getReg(); 1975fe6060f1SDimitry Andric Register OriginalOverflow = MI.getOperand(1).getReg(); 1976fe6060f1SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 1977fe6060f1SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 1978fe6060f1SDimitry Andric LLT SrcTy = MRI.getType(LHS); 1979fe6060f1SDimitry Andric LLT OverflowTy = MRI.getType(OriginalOverflow); 1980fe6060f1SDimitry Andric unsigned SrcBitWidth = SrcTy.getScalarSizeInBits(); 1981fe6060f1SDimitry Andric 1982fe6060f1SDimitry Andric // To determine if the result overflowed in the larger type, we extend the 1983fe6060f1SDimitry Andric // input to the larger type, do the multiply (checking if it overflows), 1984fe6060f1SDimitry Andric // then also check the high bits of the result to see if overflow happened 1985fe6060f1SDimitry Andric // there. 1986fe6060f1SDimitry Andric unsigned ExtOp = IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 1987fe6060f1SDimitry Andric auto LeftOperand = MIRBuilder.buildInstr(ExtOp, {WideTy}, {LHS}); 1988fe6060f1SDimitry Andric auto RightOperand = MIRBuilder.buildInstr(ExtOp, {WideTy}, {RHS}); 1989fe6060f1SDimitry Andric 1990fe6060f1SDimitry Andric auto Mulo = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy, OverflowTy}, 1991fe6060f1SDimitry Andric {LeftOperand, RightOperand}); 1992fe6060f1SDimitry Andric auto Mul = Mulo->getOperand(0); 1993fe6060f1SDimitry Andric MIRBuilder.buildTrunc(Result, Mul); 1994fe6060f1SDimitry Andric 1995fe6060f1SDimitry Andric MachineInstrBuilder ExtResult; 1996fe6060f1SDimitry Andric // Overflow occurred if it occurred in the larger type, or if the high part 1997fe6060f1SDimitry Andric // of the result does not zero/sign-extend the low part. Check this second 1998fe6060f1SDimitry Andric // possibility first. 1999fe6060f1SDimitry Andric if (IsSigned) { 2000fe6060f1SDimitry Andric // For signed, overflow occurred when the high part does not sign-extend 2001fe6060f1SDimitry Andric // the low part. 2002fe6060f1SDimitry Andric ExtResult = MIRBuilder.buildSExtInReg(WideTy, Mul, SrcBitWidth); 2003fe6060f1SDimitry Andric } else { 2004fe6060f1SDimitry Andric // Unsigned overflow occurred when the high part does not zero-extend the 2005fe6060f1SDimitry Andric // low part. 2006fe6060f1SDimitry Andric ExtResult = MIRBuilder.buildZExtInReg(WideTy, Mul, SrcBitWidth); 2007fe6060f1SDimitry Andric } 2008fe6060f1SDimitry Andric 2009fe6060f1SDimitry Andric // Multiplication cannot overflow if the WideTy is >= 2 * original width, 2010fe6060f1SDimitry Andric // so we don't need to check the overflow result of larger type Mulo. 2011fe6060f1SDimitry Andric if (WideTy.getScalarSizeInBits() < 2 * SrcBitWidth) { 2012fe6060f1SDimitry Andric auto Overflow = 2013fe6060f1SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, OverflowTy, Mul, ExtResult); 2014fe6060f1SDimitry Andric // Finally check if the multiplication in the larger type itself overflowed. 2015fe6060f1SDimitry Andric MIRBuilder.buildOr(OriginalOverflow, Mulo->getOperand(1), Overflow); 2016fe6060f1SDimitry Andric } else { 2017fe6060f1SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, OriginalOverflow, Mul, ExtResult); 2018fe6060f1SDimitry Andric } 2019fe6060f1SDimitry Andric MI.eraseFromParent(); 2020fe6060f1SDimitry Andric return Legalized; 2021fe6060f1SDimitry Andric } 2022fe6060f1SDimitry Andric 2023fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 20245ffd83dbSDimitry Andric LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) { 20250b57cec5SDimitry Andric switch (MI.getOpcode()) { 20260b57cec5SDimitry Andric default: 20270b57cec5SDimitry Andric return UnableToLegalize; 2028fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_XCHG: 2029fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_ADD: 2030fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_SUB: 2031fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_AND: 2032fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_OR: 2033fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_XOR: 2034fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_MIN: 2035fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_MAX: 2036fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_UMIN: 2037fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_UMAX: 2038fe6060f1SDimitry Andric assert(TypeIdx == 0 && "atomicrmw with second scalar type"); 2039fe6060f1SDimitry Andric Observer.changingInstr(MI); 2040fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 2041fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 2042fe6060f1SDimitry Andric Observer.changedInstr(MI); 2043fe6060f1SDimitry Andric return Legalized; 2044fe6060f1SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG: 2045fe6060f1SDimitry Andric assert(TypeIdx == 0 && "G_ATOMIC_CMPXCHG with second scalar type"); 2046fe6060f1SDimitry Andric Observer.changingInstr(MI); 2047fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 2048fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 2049fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 2050fe6060f1SDimitry Andric Observer.changedInstr(MI); 2051fe6060f1SDimitry Andric return Legalized; 2052fe6060f1SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: 2053fe6060f1SDimitry Andric if (TypeIdx == 0) { 2054fe6060f1SDimitry Andric Observer.changingInstr(MI); 2055fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 2056fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 4, TargetOpcode::G_ANYEXT); 2057fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 2058fe6060f1SDimitry Andric Observer.changedInstr(MI); 2059fe6060f1SDimitry Andric return Legalized; 2060fe6060f1SDimitry Andric } 2061fe6060f1SDimitry Andric assert(TypeIdx == 1 && 2062fe6060f1SDimitry Andric "G_ATOMIC_CMPXCHG_WITH_SUCCESS with third scalar type"); 2063fe6060f1SDimitry Andric Observer.changingInstr(MI); 2064fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2065fe6060f1SDimitry Andric Observer.changedInstr(MI); 2066fe6060f1SDimitry Andric return Legalized; 20670b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 20680b57cec5SDimitry Andric return widenScalarExtract(MI, TypeIdx, WideTy); 20690b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 20700b57cec5SDimitry Andric return widenScalarInsert(MI, TypeIdx, WideTy); 20710b57cec5SDimitry Andric case TargetOpcode::G_MERGE_VALUES: 20720b57cec5SDimitry Andric return widenScalarMergeValues(MI, TypeIdx, WideTy); 20730b57cec5SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: 20740b57cec5SDimitry Andric return widenScalarUnmergeValues(MI, TypeIdx, WideTy); 2075e8d8bef9SDimitry Andric case TargetOpcode::G_SADDO: 2076e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBO: 20770b57cec5SDimitry Andric case TargetOpcode::G_UADDO: 2078e8d8bef9SDimitry Andric case TargetOpcode::G_USUBO: 2079fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 2080fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 2081fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 2082fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 2083fe6060f1SDimitry Andric return widenScalarAddSubOverflow(MI, TypeIdx, WideTy); 2084fe6060f1SDimitry Andric case TargetOpcode::G_UMULO: 2085fe6060f1SDimitry Andric case TargetOpcode::G_SMULO: 2086fe6060f1SDimitry Andric return widenScalarMulo(MI, TypeIdx, WideTy); 20875ffd83dbSDimitry Andric case TargetOpcode::G_SADDSAT: 20885ffd83dbSDimitry Andric case TargetOpcode::G_SSUBSAT: 2089e8d8bef9SDimitry Andric case TargetOpcode::G_SSHLSAT: 20905ffd83dbSDimitry Andric case TargetOpcode::G_UADDSAT: 20915ffd83dbSDimitry Andric case TargetOpcode::G_USUBSAT: 2092e8d8bef9SDimitry Andric case TargetOpcode::G_USHLSAT: 2093e8d8bef9SDimitry Andric return widenScalarAddSubShlSat(MI, TypeIdx, WideTy); 20940b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 20950b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 20960b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 20970b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 20980b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: { 20990b57cec5SDimitry Andric if (TypeIdx == 0) { 21000b57cec5SDimitry Andric Observer.changingInstr(MI); 21010b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 21020b57cec5SDimitry Andric Observer.changedInstr(MI); 21030b57cec5SDimitry Andric return Legalized; 21040b57cec5SDimitry Andric } 21050b57cec5SDimitry Andric 21060b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 21070b57cec5SDimitry Andric 2108349cc55cSDimitry Andric // First extend the input. 2109349cc55cSDimitry Andric unsigned ExtOpc = MI.getOpcode() == TargetOpcode::G_CTTZ || 2110349cc55cSDimitry Andric MI.getOpcode() == TargetOpcode::G_CTTZ_ZERO_UNDEF 2111349cc55cSDimitry Andric ? TargetOpcode::G_ANYEXT 2112349cc55cSDimitry Andric : TargetOpcode::G_ZEXT; 2113349cc55cSDimitry Andric auto MIBSrc = MIRBuilder.buildInstr(ExtOpc, {WideTy}, {SrcReg}); 21140b57cec5SDimitry Andric LLT CurTy = MRI.getType(SrcReg); 2115349cc55cSDimitry Andric unsigned NewOpc = MI.getOpcode(); 2116349cc55cSDimitry Andric if (NewOpc == TargetOpcode::G_CTTZ) { 21170b57cec5SDimitry Andric // The count is the same in the larger type except if the original 21180b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 21190b57cec5SDimitry Andric // the top of the original type. 21200b57cec5SDimitry Andric auto TopBit = 21210b57cec5SDimitry Andric APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits()); 21220b57cec5SDimitry Andric MIBSrc = MIRBuilder.buildOr( 21230b57cec5SDimitry Andric WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit)); 2124349cc55cSDimitry Andric // Now we know the operand is non-zero, use the more relaxed opcode. 2125349cc55cSDimitry Andric NewOpc = TargetOpcode::G_CTTZ_ZERO_UNDEF; 21260b57cec5SDimitry Andric } 21270b57cec5SDimitry Andric 21280b57cec5SDimitry Andric // Perform the operation at the larger size. 2129349cc55cSDimitry Andric auto MIBNewOp = MIRBuilder.buildInstr(NewOpc, {WideTy}, {MIBSrc}); 21300b57cec5SDimitry Andric // This is already the correct result for CTPOP and CTTZs 21310b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_CTLZ || 21320b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) { 21330b57cec5SDimitry Andric // The correct result is NewOp - (Difference in widety and current ty). 21340b57cec5SDimitry Andric unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits(); 21355ffd83dbSDimitry Andric MIBNewOp = MIRBuilder.buildSub( 21365ffd83dbSDimitry Andric WideTy, MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff)); 21370b57cec5SDimitry Andric } 21380b57cec5SDimitry Andric 21390b57cec5SDimitry Andric MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp); 21400b57cec5SDimitry Andric MI.eraseFromParent(); 21410b57cec5SDimitry Andric return Legalized; 21420b57cec5SDimitry Andric } 21430b57cec5SDimitry Andric case TargetOpcode::G_BSWAP: { 21440b57cec5SDimitry Andric Observer.changingInstr(MI); 21450b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 21460b57cec5SDimitry Andric 21470b57cec5SDimitry Andric Register ShrReg = MRI.createGenericVirtualRegister(WideTy); 21480b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 21490b57cec5SDimitry Andric Register ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy); 21500b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21510b57cec5SDimitry Andric 21520b57cec5SDimitry Andric MI.getOperand(0).setReg(DstExt); 21530b57cec5SDimitry Andric 21540b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 21550b57cec5SDimitry Andric 21560b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 21570b57cec5SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 21580b57cec5SDimitry Andric MIRBuilder.buildConstant(ShiftAmtReg, DiffBits); 21595ffd83dbSDimitry Andric MIRBuilder.buildLShr(ShrReg, DstExt, ShiftAmtReg); 21600b57cec5SDimitry Andric 21610b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ShrReg); 21620b57cec5SDimitry Andric Observer.changedInstr(MI); 21630b57cec5SDimitry Andric return Legalized; 21640b57cec5SDimitry Andric } 21658bcb0991SDimitry Andric case TargetOpcode::G_BITREVERSE: { 21668bcb0991SDimitry Andric Observer.changingInstr(MI); 21678bcb0991SDimitry Andric 21688bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 21698bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 21708bcb0991SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 21718bcb0991SDimitry Andric 21728bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 21738bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21748bcb0991SDimitry Andric MI.getOperand(0).setReg(DstExt); 21758bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 21768bcb0991SDimitry Andric 21778bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, DiffBits); 21788bcb0991SDimitry Andric auto Shift = MIRBuilder.buildLShr(WideTy, DstExt, ShiftAmt); 21798bcb0991SDimitry Andric MIRBuilder.buildTrunc(DstReg, Shift); 21808bcb0991SDimitry Andric Observer.changedInstr(MI); 21818bcb0991SDimitry Andric return Legalized; 21828bcb0991SDimitry Andric } 21835ffd83dbSDimitry Andric case TargetOpcode::G_FREEZE: 21845ffd83dbSDimitry Andric Observer.changingInstr(MI); 21855ffd83dbSDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21865ffd83dbSDimitry Andric widenScalarDst(MI, WideTy); 21875ffd83dbSDimitry Andric Observer.changedInstr(MI); 21885ffd83dbSDimitry Andric return Legalized; 21895ffd83dbSDimitry Andric 2190fe6060f1SDimitry Andric case TargetOpcode::G_ABS: 2191fe6060f1SDimitry Andric Observer.changingInstr(MI); 2192fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 2193fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2194fe6060f1SDimitry Andric Observer.changedInstr(MI); 2195fe6060f1SDimitry Andric return Legalized; 2196fe6060f1SDimitry Andric 21970b57cec5SDimitry Andric case TargetOpcode::G_ADD: 21980b57cec5SDimitry Andric case TargetOpcode::G_AND: 21990b57cec5SDimitry Andric case TargetOpcode::G_MUL: 22000b57cec5SDimitry Andric case TargetOpcode::G_OR: 22010b57cec5SDimitry Andric case TargetOpcode::G_XOR: 22020b57cec5SDimitry Andric case TargetOpcode::G_SUB: 22030b57cec5SDimitry Andric // Perform operation at larger width (any extension is fines here, high bits 22040b57cec5SDimitry Andric // don't affect the result) and then truncate the result back to the 22050b57cec5SDimitry Andric // original type. 22060b57cec5SDimitry Andric Observer.changingInstr(MI); 22070b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 22080b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 22090b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22100b57cec5SDimitry Andric Observer.changedInstr(MI); 22110b57cec5SDimitry Andric return Legalized; 22120b57cec5SDimitry Andric 2213fe6060f1SDimitry Andric case TargetOpcode::G_SBFX: 2214fe6060f1SDimitry Andric case TargetOpcode::G_UBFX: 2215fe6060f1SDimitry Andric Observer.changingInstr(MI); 2216fe6060f1SDimitry Andric 2217fe6060f1SDimitry Andric if (TypeIdx == 0) { 2218fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 2219fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2220fe6060f1SDimitry Andric } else { 2221fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 2222fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ZEXT); 2223fe6060f1SDimitry Andric } 2224fe6060f1SDimitry Andric 2225fe6060f1SDimitry Andric Observer.changedInstr(MI); 2226fe6060f1SDimitry Andric return Legalized; 2227fe6060f1SDimitry Andric 22280b57cec5SDimitry Andric case TargetOpcode::G_SHL: 22290b57cec5SDimitry Andric Observer.changingInstr(MI); 22300b57cec5SDimitry Andric 22310b57cec5SDimitry Andric if (TypeIdx == 0) { 22320b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 22330b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22340b57cec5SDimitry Andric } else { 22350b57cec5SDimitry Andric assert(TypeIdx == 1); 22360b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 22370b57cec5SDimitry Andric // unsigned integer: 22380b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 22390b57cec5SDimitry Andric } 22400b57cec5SDimitry Andric 22410b57cec5SDimitry Andric Observer.changedInstr(MI); 22420b57cec5SDimitry Andric return Legalized; 22430b57cec5SDimitry Andric 22440b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 22450b57cec5SDimitry Andric case TargetOpcode::G_SREM: 22460b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 22470b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 22480b57cec5SDimitry Andric Observer.changingInstr(MI); 22490b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 22500b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 22510b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22520b57cec5SDimitry Andric Observer.changedInstr(MI); 22530b57cec5SDimitry Andric return Legalized; 22540b57cec5SDimitry Andric 2255fe6060f1SDimitry Andric case TargetOpcode::G_SDIVREM: 2256fe6060f1SDimitry Andric Observer.changingInstr(MI); 2257fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 2258fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT); 2259fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2260fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2261fe6060f1SDimitry Andric Observer.changedInstr(MI); 2262fe6060f1SDimitry Andric return Legalized; 2263fe6060f1SDimitry Andric 22640b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 22650b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 22660b57cec5SDimitry Andric Observer.changingInstr(MI); 22670b57cec5SDimitry Andric 22680b57cec5SDimitry Andric if (TypeIdx == 0) { 22690b57cec5SDimitry Andric unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ? 22700b57cec5SDimitry Andric TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 22710b57cec5SDimitry Andric 22720b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, CvtOp); 22730b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22740b57cec5SDimitry Andric } else { 22750b57cec5SDimitry Andric assert(TypeIdx == 1); 22760b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 22770b57cec5SDimitry Andric // unsigned integer: 22780b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 22790b57cec5SDimitry Andric } 22800b57cec5SDimitry Andric 22810b57cec5SDimitry Andric Observer.changedInstr(MI); 22820b57cec5SDimitry Andric return Legalized; 22830b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 22840b57cec5SDimitry Andric case TargetOpcode::G_UREM: 22850b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 22860b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 22870b57cec5SDimitry Andric Observer.changingInstr(MI); 22880b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 22890b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 22900b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22910b57cec5SDimitry Andric Observer.changedInstr(MI); 22920b57cec5SDimitry Andric return Legalized; 22930b57cec5SDimitry Andric 2294fe6060f1SDimitry Andric case TargetOpcode::G_UDIVREM: 2295fe6060f1SDimitry Andric Observer.changingInstr(MI); 2296fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 2297fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ZEXT); 2298fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2299fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2300fe6060f1SDimitry Andric Observer.changedInstr(MI); 2301fe6060f1SDimitry Andric return Legalized; 2302fe6060f1SDimitry Andric 23030b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 23040b57cec5SDimitry Andric Observer.changingInstr(MI); 23050b57cec5SDimitry Andric if (TypeIdx == 0) { 23060b57cec5SDimitry Andric // Perform operation at larger width (any extension is fine here, high 23070b57cec5SDimitry Andric // bits don't affect the result) and then truncate the result back to the 23080b57cec5SDimitry Andric // original type. 23090b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 23100b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 23110b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23120b57cec5SDimitry Andric } else { 23130b57cec5SDimitry Andric bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector(); 23140b57cec5SDimitry Andric // Explicit extension is required here since high bits affect the result. 23150b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false)); 23160b57cec5SDimitry Andric } 23170b57cec5SDimitry Andric Observer.changedInstr(MI); 23180b57cec5SDimitry Andric return Legalized; 23190b57cec5SDimitry Andric 23200b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 23210b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: 23220b57cec5SDimitry Andric Observer.changingInstr(MI); 23238bcb0991SDimitry Andric 23248bcb0991SDimitry Andric if (TypeIdx == 0) 23250b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23268bcb0991SDimitry Andric else 23278bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT); 23288bcb0991SDimitry Andric 23290b57cec5SDimitry Andric Observer.changedInstr(MI); 23300b57cec5SDimitry Andric return Legalized; 23310b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 23320b57cec5SDimitry Andric Observer.changingInstr(MI); 2333e8d8bef9SDimitry Andric 2334e8d8bef9SDimitry Andric if (TypeIdx == 0) 2335e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2336e8d8bef9SDimitry Andric else 23370b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 2338e8d8bef9SDimitry Andric 23390b57cec5SDimitry Andric Observer.changedInstr(MI); 23400b57cec5SDimitry Andric return Legalized; 23410b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: 23420b57cec5SDimitry Andric Observer.changingInstr(MI); 2343e8d8bef9SDimitry Andric 2344e8d8bef9SDimitry Andric if (TypeIdx == 0) 2345e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2346e8d8bef9SDimitry Andric else 23470b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 2348e8d8bef9SDimitry Andric 23490b57cec5SDimitry Andric Observer.changedInstr(MI); 23500b57cec5SDimitry Andric return Legalized; 23510b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 23520b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: 23530b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 23540b57cec5SDimitry Andric Observer.changingInstr(MI); 23550b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23560b57cec5SDimitry Andric Observer.changedInstr(MI); 23570b57cec5SDimitry Andric return Legalized; 23580b57cec5SDimitry Andric 23590b57cec5SDimitry Andric case TargetOpcode::G_STORE: { 23600b57cec5SDimitry Andric if (TypeIdx != 0) 23610b57cec5SDimitry Andric return UnableToLegalize; 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 2364e8d8bef9SDimitry Andric if (!Ty.isScalar()) 23650b57cec5SDimitry Andric return UnableToLegalize; 23660b57cec5SDimitry Andric 23670b57cec5SDimitry Andric Observer.changingInstr(MI); 23680b57cec5SDimitry Andric 23690b57cec5SDimitry Andric unsigned ExtType = Ty.getScalarSizeInBits() == 1 ? 23700b57cec5SDimitry Andric TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT; 23710b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, ExtType); 23720b57cec5SDimitry Andric 23730b57cec5SDimitry Andric Observer.changedInstr(MI); 23740b57cec5SDimitry Andric return Legalized; 23750b57cec5SDimitry Andric } 23760b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: { 23770b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 23780b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 2379480093f4SDimitry Andric unsigned ExtOpc = LI.getExtOpcodeForWideningConstant( 2380480093f4SDimitry Andric MRI.getType(MI.getOperand(0).getReg())); 2381480093f4SDimitry Andric assert((ExtOpc == TargetOpcode::G_ZEXT || ExtOpc == TargetOpcode::G_SEXT || 2382480093f4SDimitry Andric ExtOpc == TargetOpcode::G_ANYEXT) && 2383480093f4SDimitry Andric "Illegal Extend"); 2384480093f4SDimitry Andric const APInt &SrcVal = SrcMO.getCImm()->getValue(); 2385480093f4SDimitry Andric const APInt &Val = (ExtOpc == TargetOpcode::G_SEXT) 2386480093f4SDimitry Andric ? SrcVal.sext(WideTy.getSizeInBits()) 2387480093f4SDimitry Andric : SrcVal.zext(WideTy.getSizeInBits()); 23880b57cec5SDimitry Andric Observer.changingInstr(MI); 23890b57cec5SDimitry Andric SrcMO.setCImm(ConstantInt::get(Ctx, Val)); 23900b57cec5SDimitry Andric 23910b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23920b57cec5SDimitry Andric Observer.changedInstr(MI); 23930b57cec5SDimitry Andric return Legalized; 23940b57cec5SDimitry Andric } 23950b57cec5SDimitry Andric case TargetOpcode::G_FCONSTANT: { 2396*fcaf7f86SDimitry Andric // To avoid changing the bits of the constant due to extension to a larger 2397*fcaf7f86SDimitry Andric // type and then using G_FPTRUNC, we simply convert to a G_CONSTANT. 23980b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 2399*fcaf7f86SDimitry Andric APInt Val = SrcMO.getFPImm()->getValueAPF().bitcastToAPInt(); 2400*fcaf7f86SDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 2401*fcaf7f86SDimitry Andric auto IntCst = MIRBuilder.buildConstant(MI.getOperand(0).getReg(), Val); 2402*fcaf7f86SDimitry Andric widenScalarDst(*IntCst, WideTy, 0, TargetOpcode::G_TRUNC); 2403*fcaf7f86SDimitry Andric MI.eraseFromParent(); 24040b57cec5SDimitry Andric return Legalized; 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: { 24070b57cec5SDimitry Andric Observer.changingInstr(MI); 24080b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 24090b57cec5SDimitry Andric Observer.changedInstr(MI); 24100b57cec5SDimitry Andric return Legalized; 24110b57cec5SDimitry Andric } 24120b57cec5SDimitry Andric case TargetOpcode::G_BRCOND: 24130b57cec5SDimitry Andric Observer.changingInstr(MI); 24140b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, MIRBuilder.getBoolExtOp(false, false)); 24150b57cec5SDimitry Andric Observer.changedInstr(MI); 24160b57cec5SDimitry Andric return Legalized; 24170b57cec5SDimitry Andric 24180b57cec5SDimitry Andric case TargetOpcode::G_FCMP: 24190b57cec5SDimitry Andric Observer.changingInstr(MI); 24200b57cec5SDimitry Andric if (TypeIdx == 0) 24210b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 24220b57cec5SDimitry Andric else { 24230b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT); 24240b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT); 24250b57cec5SDimitry Andric } 24260b57cec5SDimitry Andric Observer.changedInstr(MI); 24270b57cec5SDimitry Andric return Legalized; 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric case TargetOpcode::G_ICMP: 24300b57cec5SDimitry Andric Observer.changingInstr(MI); 24310b57cec5SDimitry Andric if (TypeIdx == 0) 24320b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 24330b57cec5SDimitry Andric else { 24340b57cec5SDimitry Andric unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>( 24350b57cec5SDimitry Andric MI.getOperand(1).getPredicate())) 24360b57cec5SDimitry Andric ? TargetOpcode::G_SEXT 24370b57cec5SDimitry Andric : TargetOpcode::G_ZEXT; 24380b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, ExtOpcode); 24390b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, ExtOpcode); 24400b57cec5SDimitry Andric } 24410b57cec5SDimitry Andric Observer.changedInstr(MI); 24420b57cec5SDimitry Andric return Legalized; 24430b57cec5SDimitry Andric 2444480093f4SDimitry Andric case TargetOpcode::G_PTR_ADD: 2445480093f4SDimitry Andric assert(TypeIdx == 1 && "unable to legalize pointer of G_PTR_ADD"); 24460b57cec5SDimitry Andric Observer.changingInstr(MI); 24470b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 24480b57cec5SDimitry Andric Observer.changedInstr(MI); 24490b57cec5SDimitry Andric return Legalized; 24500b57cec5SDimitry Andric 24510b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 24520b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 24530b57cec5SDimitry Andric 24540b57cec5SDimitry Andric Observer.changingInstr(MI); 24550b57cec5SDimitry Andric for (unsigned I = 1; I < MI.getNumOperands(); I += 2) { 24560b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 24570b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 24580b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT); 24590b57cec5SDimitry Andric } 24600b57cec5SDimitry Andric 24610b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 24620b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 24630b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 24640b57cec5SDimitry Andric Observer.changedInstr(MI); 24650b57cec5SDimitry Andric return Legalized; 24660b57cec5SDimitry Andric } 24670b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: { 24680b57cec5SDimitry Andric if (TypeIdx == 0) { 24690b57cec5SDimitry Andric Register VecReg = MI.getOperand(1).getReg(); 24700b57cec5SDimitry Andric LLT VecTy = MRI.getType(VecReg); 24710b57cec5SDimitry Andric Observer.changingInstr(MI); 24720b57cec5SDimitry Andric 2473fe6060f1SDimitry Andric widenScalarSrc( 2474fe6060f1SDimitry Andric MI, LLT::vector(VecTy.getElementCount(), WideTy.getSizeInBits()), 1, 2475349cc55cSDimitry Andric TargetOpcode::G_ANYEXT); 24760b57cec5SDimitry Andric 24770b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 24780b57cec5SDimitry Andric Observer.changedInstr(MI); 24790b57cec5SDimitry Andric return Legalized; 24800b57cec5SDimitry Andric } 24810b57cec5SDimitry Andric 24820b57cec5SDimitry Andric if (TypeIdx != 2) 24830b57cec5SDimitry Andric return UnableToLegalize; 24840b57cec5SDimitry Andric Observer.changingInstr(MI); 2485480093f4SDimitry Andric // TODO: Probably should be zext 24860b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 24870b57cec5SDimitry Andric Observer.changedInstr(MI); 24880b57cec5SDimitry Andric return Legalized; 24890b57cec5SDimitry Andric } 2490480093f4SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: { 2491480093f4SDimitry Andric if (TypeIdx == 1) { 2492480093f4SDimitry Andric Observer.changingInstr(MI); 2493480093f4SDimitry Andric 2494480093f4SDimitry Andric Register VecReg = MI.getOperand(1).getReg(); 2495480093f4SDimitry Andric LLT VecTy = MRI.getType(VecReg); 2496fe6060f1SDimitry Andric LLT WideVecTy = LLT::vector(VecTy.getElementCount(), WideTy); 2497480093f4SDimitry Andric 2498480093f4SDimitry Andric widenScalarSrc(MI, WideVecTy, 1, TargetOpcode::G_ANYEXT); 2499480093f4SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 2500480093f4SDimitry Andric widenScalarDst(MI, WideVecTy, 0); 2501480093f4SDimitry Andric Observer.changedInstr(MI); 2502480093f4SDimitry Andric return Legalized; 2503480093f4SDimitry Andric } 2504480093f4SDimitry Andric 2505480093f4SDimitry Andric if (TypeIdx == 2) { 2506480093f4SDimitry Andric Observer.changingInstr(MI); 2507480093f4SDimitry Andric // TODO: Probably should be zext 2508480093f4SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT); 2509480093f4SDimitry Andric Observer.changedInstr(MI); 25105ffd83dbSDimitry Andric return Legalized; 2511480093f4SDimitry Andric } 2512480093f4SDimitry Andric 25135ffd83dbSDimitry Andric return UnableToLegalize; 2514480093f4SDimitry Andric } 25150b57cec5SDimitry Andric case TargetOpcode::G_FADD: 25160b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 25170b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 25180b57cec5SDimitry Andric case TargetOpcode::G_FMA: 25198bcb0991SDimitry Andric case TargetOpcode::G_FMAD: 25200b57cec5SDimitry Andric case TargetOpcode::G_FNEG: 25210b57cec5SDimitry Andric case TargetOpcode::G_FABS: 25220b57cec5SDimitry Andric case TargetOpcode::G_FCANONICALIZE: 25230b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM: 25240b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM: 25250b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM_IEEE: 25260b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM_IEEE: 25270b57cec5SDimitry Andric case TargetOpcode::G_FMINIMUM: 25280b57cec5SDimitry Andric case TargetOpcode::G_FMAXIMUM: 25290b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 25300b57cec5SDimitry Andric case TargetOpcode::G_FREM: 25310b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 25320b57cec5SDimitry Andric case TargetOpcode::G_FFLOOR: 25330b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 25340b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 25350b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 25360b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 25370b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 25380b57cec5SDimitry Andric case TargetOpcode::G_FRINT: 25390b57cec5SDimitry Andric case TargetOpcode::G_FNEARBYINT: 25400b57cec5SDimitry Andric case TargetOpcode::G_FSQRT: 25410b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 25420b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 25430b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 25440b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_TRUNC: 25450b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUND: 2546e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: 25470b57cec5SDimitry Andric assert(TypeIdx == 0); 25480b57cec5SDimitry Andric Observer.changingInstr(MI); 25490b57cec5SDimitry Andric 25500b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) 25510b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT); 25520b57cec5SDimitry Andric 25530b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 25540b57cec5SDimitry Andric Observer.changedInstr(MI); 25550b57cec5SDimitry Andric return Legalized; 2556e8d8bef9SDimitry Andric case TargetOpcode::G_FPOWI: { 2557e8d8bef9SDimitry Andric if (TypeIdx != 0) 2558e8d8bef9SDimitry Andric return UnableToLegalize; 2559e8d8bef9SDimitry Andric Observer.changingInstr(MI); 2560e8d8bef9SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT); 2561e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2562e8d8bef9SDimitry Andric Observer.changedInstr(MI); 2563e8d8bef9SDimitry Andric return Legalized; 2564e8d8bef9SDimitry Andric } 25650b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 25660b57cec5SDimitry Andric if (TypeIdx != 1) 25670b57cec5SDimitry Andric return UnableToLegalize; 25680b57cec5SDimitry Andric 25690b57cec5SDimitry Andric Observer.changingInstr(MI); 25700b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 25710b57cec5SDimitry Andric Observer.changedInstr(MI); 25720b57cec5SDimitry Andric return Legalized; 25730b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 25740b57cec5SDimitry Andric if (TypeIdx != 0) 25750b57cec5SDimitry Andric return UnableToLegalize; 25760b57cec5SDimitry Andric 25770b57cec5SDimitry Andric Observer.changingInstr(MI); 25780b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 25790b57cec5SDimitry Andric Observer.changedInstr(MI); 25800b57cec5SDimitry Andric return Legalized; 25810b57cec5SDimitry Andric case TargetOpcode::G_BUILD_VECTOR: { 25820b57cec5SDimitry Andric Observer.changingInstr(MI); 25830b57cec5SDimitry Andric 25840b57cec5SDimitry Andric const LLT WideEltTy = TypeIdx == 1 ? WideTy : WideTy.getElementType(); 25850b57cec5SDimitry Andric for (int I = 1, E = MI.getNumOperands(); I != E; ++I) 25860b57cec5SDimitry Andric widenScalarSrc(MI, WideEltTy, I, TargetOpcode::G_ANYEXT); 25870b57cec5SDimitry Andric 25880b57cec5SDimitry Andric // Avoid changing the result vector type if the source element type was 25890b57cec5SDimitry Andric // requested. 25900b57cec5SDimitry Andric if (TypeIdx == 1) { 2591e8d8bef9SDimitry Andric MI.setDesc(MIRBuilder.getTII().get(TargetOpcode::G_BUILD_VECTOR_TRUNC)); 25920b57cec5SDimitry Andric } else { 25930b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 25940b57cec5SDimitry Andric } 25950b57cec5SDimitry Andric 25960b57cec5SDimitry Andric Observer.changedInstr(MI); 25970b57cec5SDimitry Andric return Legalized; 25980b57cec5SDimitry Andric } 25998bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: 26008bcb0991SDimitry Andric if (TypeIdx != 0) 26018bcb0991SDimitry Andric return UnableToLegalize; 26028bcb0991SDimitry Andric 26038bcb0991SDimitry Andric Observer.changingInstr(MI); 26048bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 26058bcb0991SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_TRUNC); 26068bcb0991SDimitry Andric Observer.changedInstr(MI); 26078bcb0991SDimitry Andric return Legalized; 26085ffd83dbSDimitry Andric case TargetOpcode::G_PTRMASK: { 26095ffd83dbSDimitry Andric if (TypeIdx != 1) 26105ffd83dbSDimitry Andric return UnableToLegalize; 26115ffd83dbSDimitry Andric Observer.changingInstr(MI); 26125ffd83dbSDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 26135ffd83dbSDimitry Andric Observer.changedInstr(MI); 26145ffd83dbSDimitry Andric return Legalized; 26155ffd83dbSDimitry Andric } 26165ffd83dbSDimitry Andric } 26175ffd83dbSDimitry Andric } 26185ffd83dbSDimitry Andric 26195ffd83dbSDimitry Andric static void getUnmergePieces(SmallVectorImpl<Register> &Pieces, 26205ffd83dbSDimitry Andric MachineIRBuilder &B, Register Src, LLT Ty) { 26215ffd83dbSDimitry Andric auto Unmerge = B.buildUnmerge(Ty, Src); 26225ffd83dbSDimitry Andric for (int I = 0, E = Unmerge->getNumOperands() - 1; I != E; ++I) 26235ffd83dbSDimitry Andric Pieces.push_back(Unmerge.getReg(I)); 26245ffd83dbSDimitry Andric } 26255ffd83dbSDimitry Andric 26265ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 26275ffd83dbSDimitry Andric LegalizerHelper::lowerBitcast(MachineInstr &MI) { 26285ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 26295ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 26305ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 26315ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 26325ffd83dbSDimitry Andric 26335ffd83dbSDimitry Andric if (SrcTy.isVector()) { 26345ffd83dbSDimitry Andric LLT SrcEltTy = SrcTy.getElementType(); 26355ffd83dbSDimitry Andric SmallVector<Register, 8> SrcRegs; 26365ffd83dbSDimitry Andric 26375ffd83dbSDimitry Andric if (DstTy.isVector()) { 26385ffd83dbSDimitry Andric int NumDstElt = DstTy.getNumElements(); 26395ffd83dbSDimitry Andric int NumSrcElt = SrcTy.getNumElements(); 26405ffd83dbSDimitry Andric 26415ffd83dbSDimitry Andric LLT DstEltTy = DstTy.getElementType(); 26425ffd83dbSDimitry Andric LLT DstCastTy = DstEltTy; // Intermediate bitcast result type 26435ffd83dbSDimitry Andric LLT SrcPartTy = SrcEltTy; // Original unmerge result type. 26445ffd83dbSDimitry Andric 26455ffd83dbSDimitry Andric // If there's an element size mismatch, insert intermediate casts to match 26465ffd83dbSDimitry Andric // the result element type. 26475ffd83dbSDimitry Andric if (NumSrcElt < NumDstElt) { // Source element type is larger. 26485ffd83dbSDimitry Andric // %1:_(<4 x s8>) = G_BITCAST %0:_(<2 x s16>) 26495ffd83dbSDimitry Andric // 26505ffd83dbSDimitry Andric // => 26515ffd83dbSDimitry Andric // 26525ffd83dbSDimitry Andric // %2:_(s16), %3:_(s16) = G_UNMERGE_VALUES %0 26535ffd83dbSDimitry Andric // %3:_(<2 x s8>) = G_BITCAST %2 26545ffd83dbSDimitry Andric // %4:_(<2 x s8>) = G_BITCAST %3 26555ffd83dbSDimitry Andric // %1:_(<4 x s16>) = G_CONCAT_VECTORS %3, %4 2656fe6060f1SDimitry Andric DstCastTy = LLT::fixed_vector(NumDstElt / NumSrcElt, DstEltTy); 26575ffd83dbSDimitry Andric SrcPartTy = SrcEltTy; 26585ffd83dbSDimitry Andric } else if (NumSrcElt > NumDstElt) { // Source element type is smaller. 26595ffd83dbSDimitry Andric // 26605ffd83dbSDimitry Andric // %1:_(<2 x s16>) = G_BITCAST %0:_(<4 x s8>) 26615ffd83dbSDimitry Andric // 26625ffd83dbSDimitry Andric // => 26635ffd83dbSDimitry Andric // 26645ffd83dbSDimitry Andric // %2:_(<2 x s8>), %3:_(<2 x s8>) = G_UNMERGE_VALUES %0 26655ffd83dbSDimitry Andric // %3:_(s16) = G_BITCAST %2 26665ffd83dbSDimitry Andric // %4:_(s16) = G_BITCAST %3 26675ffd83dbSDimitry Andric // %1:_(<2 x s16>) = G_BUILD_VECTOR %3, %4 2668fe6060f1SDimitry Andric SrcPartTy = LLT::fixed_vector(NumSrcElt / NumDstElt, SrcEltTy); 26695ffd83dbSDimitry Andric DstCastTy = DstEltTy; 26705ffd83dbSDimitry Andric } 26715ffd83dbSDimitry Andric 26725ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcPartTy); 26735ffd83dbSDimitry Andric for (Register &SrcReg : SrcRegs) 26745ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildBitcast(DstCastTy, SrcReg).getReg(0); 26755ffd83dbSDimitry Andric } else 26765ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcEltTy); 26775ffd83dbSDimitry Andric 26785ffd83dbSDimitry Andric MIRBuilder.buildMerge(Dst, SrcRegs); 26795ffd83dbSDimitry Andric MI.eraseFromParent(); 26805ffd83dbSDimitry Andric return Legalized; 26815ffd83dbSDimitry Andric } 26825ffd83dbSDimitry Andric 26835ffd83dbSDimitry Andric if (DstTy.isVector()) { 26845ffd83dbSDimitry Andric SmallVector<Register, 8> SrcRegs; 26855ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, DstTy.getElementType()); 26865ffd83dbSDimitry Andric MIRBuilder.buildMerge(Dst, SrcRegs); 26875ffd83dbSDimitry Andric MI.eraseFromParent(); 26885ffd83dbSDimitry Andric return Legalized; 26895ffd83dbSDimitry Andric } 26905ffd83dbSDimitry Andric 26915ffd83dbSDimitry Andric return UnableToLegalize; 26925ffd83dbSDimitry Andric } 26935ffd83dbSDimitry Andric 2694e8d8bef9SDimitry Andric /// Figure out the bit offset into a register when coercing a vector index for 2695e8d8bef9SDimitry Andric /// the wide element type. This is only for the case when promoting vector to 2696e8d8bef9SDimitry Andric /// one with larger elements. 2697e8d8bef9SDimitry Andric // 2698e8d8bef9SDimitry Andric /// 2699e8d8bef9SDimitry Andric /// %offset_idx = G_AND %idx, ~(-1 << Log2(DstEltSize / SrcEltSize)) 2700e8d8bef9SDimitry Andric /// %offset_bits = G_SHL %offset_idx, Log2(SrcEltSize) 2701e8d8bef9SDimitry Andric static Register getBitcastWiderVectorElementOffset(MachineIRBuilder &B, 2702e8d8bef9SDimitry Andric Register Idx, 2703e8d8bef9SDimitry Andric unsigned NewEltSize, 2704e8d8bef9SDimitry Andric unsigned OldEltSize) { 2705e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2706e8d8bef9SDimitry Andric LLT IdxTy = B.getMRI()->getType(Idx); 2707e8d8bef9SDimitry Andric 2708e8d8bef9SDimitry Andric // Now figure out the amount we need to shift to get the target bits. 2709e8d8bef9SDimitry Andric auto OffsetMask = B.buildConstant( 2710349cc55cSDimitry Andric IdxTy, ~(APInt::getAllOnes(IdxTy.getSizeInBits()) << Log2EltRatio)); 2711e8d8bef9SDimitry Andric auto OffsetIdx = B.buildAnd(IdxTy, Idx, OffsetMask); 2712e8d8bef9SDimitry Andric return B.buildShl(IdxTy, OffsetIdx, 2713e8d8bef9SDimitry Andric B.buildConstant(IdxTy, Log2_32(OldEltSize))).getReg(0); 2714e8d8bef9SDimitry Andric } 2715e8d8bef9SDimitry Andric 2716e8d8bef9SDimitry Andric /// Perform a G_EXTRACT_VECTOR_ELT in a different sized vector element. If this 2717e8d8bef9SDimitry Andric /// is casting to a vector with a smaller element size, perform multiple element 2718e8d8bef9SDimitry Andric /// extracts and merge the results. If this is coercing to a vector with larger 2719e8d8bef9SDimitry Andric /// elements, index the bitcasted vector and extract the target element with bit 2720e8d8bef9SDimitry Andric /// operations. This is intended to force the indexing in the native register 2721e8d8bef9SDimitry Andric /// size for architectures that can dynamically index the register file. 27225ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 2723e8d8bef9SDimitry Andric LegalizerHelper::bitcastExtractVectorElt(MachineInstr &MI, unsigned TypeIdx, 2724e8d8bef9SDimitry Andric LLT CastTy) { 2725e8d8bef9SDimitry Andric if (TypeIdx != 1) 2726e8d8bef9SDimitry Andric return UnableToLegalize; 2727e8d8bef9SDimitry Andric 2728e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 2729e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 2730e8d8bef9SDimitry Andric Register Idx = MI.getOperand(2).getReg(); 2731e8d8bef9SDimitry Andric LLT SrcVecTy = MRI.getType(SrcVec); 2732e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 2733e8d8bef9SDimitry Andric 2734e8d8bef9SDimitry Andric LLT SrcEltTy = SrcVecTy.getElementType(); 2735e8d8bef9SDimitry Andric unsigned NewNumElts = CastTy.isVector() ? CastTy.getNumElements() : 1; 2736e8d8bef9SDimitry Andric unsigned OldNumElts = SrcVecTy.getNumElements(); 2737e8d8bef9SDimitry Andric 2738e8d8bef9SDimitry Andric LLT NewEltTy = CastTy.isVector() ? CastTy.getElementType() : CastTy; 2739e8d8bef9SDimitry Andric Register CastVec = MIRBuilder.buildBitcast(CastTy, SrcVec).getReg(0); 2740e8d8bef9SDimitry Andric 2741e8d8bef9SDimitry Andric const unsigned NewEltSize = NewEltTy.getSizeInBits(); 2742e8d8bef9SDimitry Andric const unsigned OldEltSize = SrcEltTy.getSizeInBits(); 2743e8d8bef9SDimitry Andric if (NewNumElts > OldNumElts) { 2744e8d8bef9SDimitry Andric // Decreasing the vector element size 2745e8d8bef9SDimitry Andric // 2746e8d8bef9SDimitry Andric // e.g. i64 = extract_vector_elt x:v2i64, y:i32 2747e8d8bef9SDimitry Andric // => 2748e8d8bef9SDimitry Andric // v4i32:castx = bitcast x:v2i64 2749e8d8bef9SDimitry Andric // 2750e8d8bef9SDimitry Andric // i64 = bitcast 2751e8d8bef9SDimitry Andric // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 2752e8d8bef9SDimitry Andric // (i32 (extract_vector_elt castx, (2 * y + 1))) 2753e8d8bef9SDimitry Andric // 2754e8d8bef9SDimitry Andric if (NewNumElts % OldNumElts != 0) 2755e8d8bef9SDimitry Andric return UnableToLegalize; 2756e8d8bef9SDimitry Andric 2757e8d8bef9SDimitry Andric // Type of the intermediate result vector. 2758e8d8bef9SDimitry Andric const unsigned NewEltsPerOldElt = NewNumElts / OldNumElts; 2759fe6060f1SDimitry Andric LLT MidTy = 2760fe6060f1SDimitry Andric LLT::scalarOrVector(ElementCount::getFixed(NewEltsPerOldElt), NewEltTy); 2761e8d8bef9SDimitry Andric 2762e8d8bef9SDimitry Andric auto NewEltsPerOldEltK = MIRBuilder.buildConstant(IdxTy, NewEltsPerOldElt); 2763e8d8bef9SDimitry Andric 2764e8d8bef9SDimitry Andric SmallVector<Register, 8> NewOps(NewEltsPerOldElt); 2765e8d8bef9SDimitry Andric auto NewBaseIdx = MIRBuilder.buildMul(IdxTy, Idx, NewEltsPerOldEltK); 2766e8d8bef9SDimitry Andric 2767e8d8bef9SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 2768e8d8bef9SDimitry Andric auto IdxOffset = MIRBuilder.buildConstant(IdxTy, I); 2769e8d8bef9SDimitry Andric auto TmpIdx = MIRBuilder.buildAdd(IdxTy, NewBaseIdx, IdxOffset); 2770e8d8bef9SDimitry Andric auto Elt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, TmpIdx); 2771e8d8bef9SDimitry Andric NewOps[I] = Elt.getReg(0); 2772e8d8bef9SDimitry Andric } 2773e8d8bef9SDimitry Andric 2774e8d8bef9SDimitry Andric auto NewVec = MIRBuilder.buildBuildVector(MidTy, NewOps); 2775e8d8bef9SDimitry Andric MIRBuilder.buildBitcast(Dst, NewVec); 2776e8d8bef9SDimitry Andric MI.eraseFromParent(); 2777e8d8bef9SDimitry Andric return Legalized; 2778e8d8bef9SDimitry Andric } 2779e8d8bef9SDimitry Andric 2780e8d8bef9SDimitry Andric if (NewNumElts < OldNumElts) { 2781e8d8bef9SDimitry Andric if (NewEltSize % OldEltSize != 0) 2782e8d8bef9SDimitry Andric return UnableToLegalize; 2783e8d8bef9SDimitry Andric 2784e8d8bef9SDimitry Andric // This only depends on powers of 2 because we use bit tricks to figure out 2785e8d8bef9SDimitry Andric // the bit offset we need to shift to get the target element. A general 2786e8d8bef9SDimitry Andric // expansion could emit division/multiply. 2787e8d8bef9SDimitry Andric if (!isPowerOf2_32(NewEltSize / OldEltSize)) 2788e8d8bef9SDimitry Andric return UnableToLegalize; 2789e8d8bef9SDimitry Andric 2790e8d8bef9SDimitry Andric // Increasing the vector element size. 2791e8d8bef9SDimitry Andric // %elt:_(small_elt) = G_EXTRACT_VECTOR_ELT %vec:_(<N x small_elt>), %idx 2792e8d8bef9SDimitry Andric // 2793e8d8bef9SDimitry Andric // => 2794e8d8bef9SDimitry Andric // 2795e8d8bef9SDimitry Andric // %cast = G_BITCAST %vec 2796e8d8bef9SDimitry Andric // %scaled_idx = G_LSHR %idx, Log2(DstEltSize / SrcEltSize) 2797e8d8bef9SDimitry Andric // %wide_elt = G_EXTRACT_VECTOR_ELT %cast, %scaled_idx 2798e8d8bef9SDimitry Andric // %offset_idx = G_AND %idx, ~(-1 << Log2(DstEltSize / SrcEltSize)) 2799e8d8bef9SDimitry Andric // %offset_bits = G_SHL %offset_idx, Log2(SrcEltSize) 2800e8d8bef9SDimitry Andric // %elt_bits = G_LSHR %wide_elt, %offset_bits 2801e8d8bef9SDimitry Andric // %elt = G_TRUNC %elt_bits 2802e8d8bef9SDimitry Andric 2803e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2804e8d8bef9SDimitry Andric auto Log2Ratio = MIRBuilder.buildConstant(IdxTy, Log2EltRatio); 2805e8d8bef9SDimitry Andric 2806e8d8bef9SDimitry Andric // Divide to get the index in the wider element type. 2807e8d8bef9SDimitry Andric auto ScaledIdx = MIRBuilder.buildLShr(IdxTy, Idx, Log2Ratio); 2808e8d8bef9SDimitry Andric 2809e8d8bef9SDimitry Andric Register WideElt = CastVec; 2810e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2811e8d8bef9SDimitry Andric WideElt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, 2812e8d8bef9SDimitry Andric ScaledIdx).getReg(0); 2813e8d8bef9SDimitry Andric } 2814e8d8bef9SDimitry Andric 2815e8d8bef9SDimitry Andric // Compute the bit offset into the register of the target element. 2816e8d8bef9SDimitry Andric Register OffsetBits = getBitcastWiderVectorElementOffset( 2817e8d8bef9SDimitry Andric MIRBuilder, Idx, NewEltSize, OldEltSize); 2818e8d8bef9SDimitry Andric 2819e8d8bef9SDimitry Andric // Shift the wide element to get the target element. 2820e8d8bef9SDimitry Andric auto ExtractedBits = MIRBuilder.buildLShr(NewEltTy, WideElt, OffsetBits); 2821e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(Dst, ExtractedBits); 2822e8d8bef9SDimitry Andric MI.eraseFromParent(); 2823e8d8bef9SDimitry Andric return Legalized; 2824e8d8bef9SDimitry Andric } 2825e8d8bef9SDimitry Andric 2826e8d8bef9SDimitry Andric return UnableToLegalize; 2827e8d8bef9SDimitry Andric } 2828e8d8bef9SDimitry Andric 2829e8d8bef9SDimitry Andric /// Emit code to insert \p InsertReg into \p TargetRet at \p OffsetBits in \p 2830e8d8bef9SDimitry Andric /// TargetReg, while preserving other bits in \p TargetReg. 2831e8d8bef9SDimitry Andric /// 2832e8d8bef9SDimitry Andric /// (InsertReg << Offset) | (TargetReg & ~(-1 >> InsertReg.size()) << Offset) 2833e8d8bef9SDimitry Andric static Register buildBitFieldInsert(MachineIRBuilder &B, 2834e8d8bef9SDimitry Andric Register TargetReg, Register InsertReg, 2835e8d8bef9SDimitry Andric Register OffsetBits) { 2836e8d8bef9SDimitry Andric LLT TargetTy = B.getMRI()->getType(TargetReg); 2837e8d8bef9SDimitry Andric LLT InsertTy = B.getMRI()->getType(InsertReg); 2838e8d8bef9SDimitry Andric auto ZextVal = B.buildZExt(TargetTy, InsertReg); 2839e8d8bef9SDimitry Andric auto ShiftedInsertVal = B.buildShl(TargetTy, ZextVal, OffsetBits); 2840e8d8bef9SDimitry Andric 2841e8d8bef9SDimitry Andric // Produce a bitmask of the value to insert 2842e8d8bef9SDimitry Andric auto EltMask = B.buildConstant( 2843e8d8bef9SDimitry Andric TargetTy, APInt::getLowBitsSet(TargetTy.getSizeInBits(), 2844e8d8bef9SDimitry Andric InsertTy.getSizeInBits())); 2845e8d8bef9SDimitry Andric // Shift it into position 2846e8d8bef9SDimitry Andric auto ShiftedMask = B.buildShl(TargetTy, EltMask, OffsetBits); 2847e8d8bef9SDimitry Andric auto InvShiftedMask = B.buildNot(TargetTy, ShiftedMask); 2848e8d8bef9SDimitry Andric 2849e8d8bef9SDimitry Andric // Clear out the bits in the wide element 2850e8d8bef9SDimitry Andric auto MaskedOldElt = B.buildAnd(TargetTy, TargetReg, InvShiftedMask); 2851e8d8bef9SDimitry Andric 2852e8d8bef9SDimitry Andric // The value to insert has all zeros already, so stick it into the masked 2853e8d8bef9SDimitry Andric // wide element. 2854e8d8bef9SDimitry Andric return B.buildOr(TargetTy, MaskedOldElt, ShiftedInsertVal).getReg(0); 2855e8d8bef9SDimitry Andric } 2856e8d8bef9SDimitry Andric 2857e8d8bef9SDimitry Andric /// Perform a G_INSERT_VECTOR_ELT in a different sized vector element. If this 2858e8d8bef9SDimitry Andric /// is increasing the element size, perform the indexing in the target element 2859e8d8bef9SDimitry Andric /// type, and use bit operations to insert at the element position. This is 2860e8d8bef9SDimitry Andric /// intended for architectures that can dynamically index the register file and 2861e8d8bef9SDimitry Andric /// want to force indexing in the native register size. 2862e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 2863e8d8bef9SDimitry Andric LegalizerHelper::bitcastInsertVectorElt(MachineInstr &MI, unsigned TypeIdx, 2864e8d8bef9SDimitry Andric LLT CastTy) { 28655ffd83dbSDimitry Andric if (TypeIdx != 0) 28665ffd83dbSDimitry Andric return UnableToLegalize; 28675ffd83dbSDimitry Andric 2868e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 2869e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 2870e8d8bef9SDimitry Andric Register Val = MI.getOperand(2).getReg(); 2871e8d8bef9SDimitry Andric Register Idx = MI.getOperand(3).getReg(); 2872e8d8bef9SDimitry Andric 2873e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(Dst); 2874e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 2875e8d8bef9SDimitry Andric 2876e8d8bef9SDimitry Andric LLT VecEltTy = VecTy.getElementType(); 2877e8d8bef9SDimitry Andric LLT NewEltTy = CastTy.isVector() ? CastTy.getElementType() : CastTy; 2878e8d8bef9SDimitry Andric const unsigned NewEltSize = NewEltTy.getSizeInBits(); 2879e8d8bef9SDimitry Andric const unsigned OldEltSize = VecEltTy.getSizeInBits(); 2880e8d8bef9SDimitry Andric 2881e8d8bef9SDimitry Andric unsigned NewNumElts = CastTy.isVector() ? CastTy.getNumElements() : 1; 2882e8d8bef9SDimitry Andric unsigned OldNumElts = VecTy.getNumElements(); 2883e8d8bef9SDimitry Andric 2884e8d8bef9SDimitry Andric Register CastVec = MIRBuilder.buildBitcast(CastTy, SrcVec).getReg(0); 2885e8d8bef9SDimitry Andric if (NewNumElts < OldNumElts) { 2886e8d8bef9SDimitry Andric if (NewEltSize % OldEltSize != 0) 28875ffd83dbSDimitry Andric return UnableToLegalize; 28885ffd83dbSDimitry Andric 2889e8d8bef9SDimitry Andric // This only depends on powers of 2 because we use bit tricks to figure out 2890e8d8bef9SDimitry Andric // the bit offset we need to shift to get the target element. A general 2891e8d8bef9SDimitry Andric // expansion could emit division/multiply. 2892e8d8bef9SDimitry Andric if (!isPowerOf2_32(NewEltSize / OldEltSize)) 28935ffd83dbSDimitry Andric return UnableToLegalize; 28945ffd83dbSDimitry Andric 2895e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2896e8d8bef9SDimitry Andric auto Log2Ratio = MIRBuilder.buildConstant(IdxTy, Log2EltRatio); 2897e8d8bef9SDimitry Andric 2898e8d8bef9SDimitry Andric // Divide to get the index in the wider element type. 2899e8d8bef9SDimitry Andric auto ScaledIdx = MIRBuilder.buildLShr(IdxTy, Idx, Log2Ratio); 2900e8d8bef9SDimitry Andric 2901e8d8bef9SDimitry Andric Register ExtractedElt = CastVec; 2902e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2903e8d8bef9SDimitry Andric ExtractedElt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, 2904e8d8bef9SDimitry Andric ScaledIdx).getReg(0); 29055ffd83dbSDimitry Andric } 29065ffd83dbSDimitry Andric 2907e8d8bef9SDimitry Andric // Compute the bit offset into the register of the target element. 2908e8d8bef9SDimitry Andric Register OffsetBits = getBitcastWiderVectorElementOffset( 2909e8d8bef9SDimitry Andric MIRBuilder, Idx, NewEltSize, OldEltSize); 2910e8d8bef9SDimitry Andric 2911e8d8bef9SDimitry Andric Register InsertedElt = buildBitFieldInsert(MIRBuilder, ExtractedElt, 2912e8d8bef9SDimitry Andric Val, OffsetBits); 2913e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2914e8d8bef9SDimitry Andric InsertedElt = MIRBuilder.buildInsertVectorElement( 2915e8d8bef9SDimitry Andric CastTy, CastVec, InsertedElt, ScaledIdx).getReg(0); 2916e8d8bef9SDimitry Andric } 2917e8d8bef9SDimitry Andric 2918e8d8bef9SDimitry Andric MIRBuilder.buildBitcast(Dst, InsertedElt); 2919e8d8bef9SDimitry Andric MI.eraseFromParent(); 29205ffd83dbSDimitry Andric return Legalized; 29215ffd83dbSDimitry Andric } 2922e8d8bef9SDimitry Andric 29235ffd83dbSDimitry Andric return UnableToLegalize; 29240b57cec5SDimitry Andric } 29250b57cec5SDimitry Andric 2926fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerLoad(GAnyLoad &LoadMI) { 29270b57cec5SDimitry Andric // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT 2928fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 2929fe6060f1SDimitry Andric Register PtrReg = LoadMI.getPointerReg(); 29300b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 2931fe6060f1SDimitry Andric MachineMemOperand &MMO = LoadMI.getMMO(); 2932fe6060f1SDimitry Andric LLT MemTy = MMO.getMemoryType(); 2933fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 29340b57cec5SDimitry Andric 2935fe6060f1SDimitry Andric unsigned MemSizeInBits = MemTy.getSizeInBits(); 2936fe6060f1SDimitry Andric unsigned MemStoreSizeInBits = 8 * MemTy.getSizeInBytes(); 2937fe6060f1SDimitry Andric 2938fe6060f1SDimitry Andric if (MemSizeInBits != MemStoreSizeInBits) { 2939349cc55cSDimitry Andric if (MemTy.isVector()) 2940349cc55cSDimitry Andric return UnableToLegalize; 2941349cc55cSDimitry Andric 2942fe6060f1SDimitry Andric // Promote to a byte-sized load if not loading an integral number of 2943fe6060f1SDimitry Andric // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 2944fe6060f1SDimitry Andric LLT WideMemTy = LLT::scalar(MemStoreSizeInBits); 2945fe6060f1SDimitry Andric MachineMemOperand *NewMMO = 2946fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, MMO.getPointerInfo(), WideMemTy); 2947fe6060f1SDimitry Andric 2948fe6060f1SDimitry Andric Register LoadReg = DstReg; 2949fe6060f1SDimitry Andric LLT LoadTy = DstTy; 2950fe6060f1SDimitry Andric 2951fe6060f1SDimitry Andric // If this wasn't already an extending load, we need to widen the result 2952fe6060f1SDimitry Andric // register to avoid creating a load with a narrower result than the source. 2953fe6060f1SDimitry Andric if (MemStoreSizeInBits > DstTy.getSizeInBits()) { 2954fe6060f1SDimitry Andric LoadTy = WideMemTy; 2955fe6060f1SDimitry Andric LoadReg = MRI.createGenericVirtualRegister(WideMemTy); 2956fe6060f1SDimitry Andric } 2957fe6060f1SDimitry Andric 2958fe6060f1SDimitry Andric if (isa<GSExtLoad>(LoadMI)) { 2959fe6060f1SDimitry Andric auto NewLoad = MIRBuilder.buildLoad(LoadTy, PtrReg, *NewMMO); 2960fe6060f1SDimitry Andric MIRBuilder.buildSExtInReg(LoadReg, NewLoad, MemSizeInBits); 296181ad6265SDimitry Andric } else if (isa<GZExtLoad>(LoadMI) || WideMemTy == LoadTy) { 2962fe6060f1SDimitry Andric auto NewLoad = MIRBuilder.buildLoad(LoadTy, PtrReg, *NewMMO); 2963fe6060f1SDimitry Andric // The extra bits are guaranteed to be zero, since we stored them that 2964fe6060f1SDimitry Andric // way. A zext load from Wide thus automatically gives zext from MemVT. 2965fe6060f1SDimitry Andric MIRBuilder.buildAssertZExt(LoadReg, NewLoad, MemSizeInBits); 2966fe6060f1SDimitry Andric } else { 2967fe6060f1SDimitry Andric MIRBuilder.buildLoad(LoadReg, PtrReg, *NewMMO); 2968fe6060f1SDimitry Andric } 2969fe6060f1SDimitry Andric 2970fe6060f1SDimitry Andric if (DstTy != LoadTy) 2971fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, LoadReg); 2972fe6060f1SDimitry Andric 2973fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 2974fe6060f1SDimitry Andric return Legalized; 2975fe6060f1SDimitry Andric } 2976fe6060f1SDimitry Andric 2977fe6060f1SDimitry Andric // Big endian lowering not implemented. 2978fe6060f1SDimitry Andric if (MIRBuilder.getDataLayout().isBigEndian()) 2979fe6060f1SDimitry Andric return UnableToLegalize; 2980fe6060f1SDimitry Andric 2981349cc55cSDimitry Andric // This load needs splitting into power of 2 sized loads. 2982349cc55cSDimitry Andric // 29838bcb0991SDimitry Andric // Our strategy here is to generate anyextending loads for the smaller 29848bcb0991SDimitry Andric // types up to next power-2 result type, and then combine the two larger 29858bcb0991SDimitry Andric // result values together, before truncating back down to the non-pow-2 29868bcb0991SDimitry Andric // type. 29878bcb0991SDimitry Andric // E.g. v1 = i24 load => 29885ffd83dbSDimitry Andric // v2 = i32 zextload (2 byte) 29898bcb0991SDimitry Andric // v3 = i32 load (1 byte) 29908bcb0991SDimitry Andric // v4 = i32 shl v3, 16 29918bcb0991SDimitry Andric // v5 = i32 or v4, v2 29928bcb0991SDimitry Andric // v1 = i24 trunc v5 29938bcb0991SDimitry Andric // By doing this we generate the correct truncate which should get 29948bcb0991SDimitry Andric // combined away as an artifact with a matching extend. 2995349cc55cSDimitry Andric 2996349cc55cSDimitry Andric uint64_t LargeSplitSize, SmallSplitSize; 2997349cc55cSDimitry Andric 2998349cc55cSDimitry Andric if (!isPowerOf2_32(MemSizeInBits)) { 2999349cc55cSDimitry Andric // This load needs splitting into power of 2 sized loads. 3000349cc55cSDimitry Andric LargeSplitSize = PowerOf2Floor(MemSizeInBits); 3001349cc55cSDimitry Andric SmallSplitSize = MemSizeInBits - LargeSplitSize; 3002349cc55cSDimitry Andric } else { 3003349cc55cSDimitry Andric // This is already a power of 2, but we still need to split this in half. 3004349cc55cSDimitry Andric // 3005349cc55cSDimitry Andric // Assume we're being asked to decompose an unaligned load. 3006349cc55cSDimitry Andric // TODO: If this requires multiple splits, handle them all at once. 3007349cc55cSDimitry Andric auto &Ctx = MF.getFunction().getContext(); 3008349cc55cSDimitry Andric if (TLI.allowsMemoryAccess(Ctx, MIRBuilder.getDataLayout(), MemTy, MMO)) 3009349cc55cSDimitry Andric return UnableToLegalize; 3010349cc55cSDimitry Andric 3011349cc55cSDimitry Andric SmallSplitSize = LargeSplitSize = MemSizeInBits / 2; 3012349cc55cSDimitry Andric } 3013349cc55cSDimitry Andric 3014349cc55cSDimitry Andric if (MemTy.isVector()) { 3015349cc55cSDimitry Andric // TODO: Handle vector extloads 3016349cc55cSDimitry Andric if (MemTy != DstTy) 3017349cc55cSDimitry Andric return UnableToLegalize; 3018349cc55cSDimitry Andric 3019349cc55cSDimitry Andric // TODO: We can do better than scalarizing the vector and at least split it 3020349cc55cSDimitry Andric // in half. 3021349cc55cSDimitry Andric return reduceLoadStoreWidth(LoadMI, 0, DstTy.getElementType()); 3022349cc55cSDimitry Andric } 30238bcb0991SDimitry Andric 30248bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 30258bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 3026fe6060f1SDimitry Andric MachineMemOperand *SmallMMO = 3027fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8); 30288bcb0991SDimitry Andric 30298bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 3030fe6060f1SDimitry Andric unsigned AnyExtSize = PowerOf2Ceil(DstTy.getSizeInBits()); 30318bcb0991SDimitry Andric LLT AnyExtTy = LLT::scalar(AnyExtSize); 3032fe6060f1SDimitry Andric auto LargeLoad = MIRBuilder.buildLoadInstr(TargetOpcode::G_ZEXTLOAD, AnyExtTy, 3033fe6060f1SDimitry Andric PtrReg, *LargeMMO); 30348bcb0991SDimitry Andric 3035fe6060f1SDimitry Andric auto OffsetCst = MIRBuilder.buildConstant(LLT::scalar(PtrTy.getSizeInBits()), 3036fe6060f1SDimitry Andric LargeSplitSize / 8); 3037480093f4SDimitry Andric Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy); 3038fe6060f1SDimitry Andric auto SmallPtr = MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst); 3039fe6060f1SDimitry Andric auto SmallLoad = MIRBuilder.buildLoadInstr(LoadMI.getOpcode(), AnyExtTy, 3040fe6060f1SDimitry Andric SmallPtr, *SmallMMO); 30418bcb0991SDimitry Andric 30428bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(AnyExtTy, LargeSplitSize); 30438bcb0991SDimitry Andric auto Shift = MIRBuilder.buildShl(AnyExtTy, SmallLoad, ShiftAmt); 3044fe6060f1SDimitry Andric 3045fe6060f1SDimitry Andric if (AnyExtTy == DstTy) 3046fe6060f1SDimitry Andric MIRBuilder.buildOr(DstReg, Shift, LargeLoad); 3047349cc55cSDimitry Andric else if (AnyExtTy.getSizeInBits() != DstTy.getSizeInBits()) { 30488bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad); 3049fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, {Or}); 3050349cc55cSDimitry Andric } else { 3051349cc55cSDimitry Andric assert(DstTy.isPointer() && "expected pointer"); 3052349cc55cSDimitry Andric auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad); 3053349cc55cSDimitry Andric 3054349cc55cSDimitry Andric // FIXME: We currently consider this to be illegal for non-integral address 3055349cc55cSDimitry Andric // spaces, but we need still need a way to reinterpret the bits. 3056349cc55cSDimitry Andric MIRBuilder.buildIntToPtr(DstReg, Or); 3057fe6060f1SDimitry Andric } 3058fe6060f1SDimitry Andric 3059fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 30608bcb0991SDimitry Andric return Legalized; 30618bcb0991SDimitry Andric } 3062e8d8bef9SDimitry Andric 3063fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerStore(GStore &StoreMI) { 30648bcb0991SDimitry Andric // Lower a non-power of 2 store into multiple pow-2 stores. 30658bcb0991SDimitry Andric // E.g. split an i24 store into an i16 store + i8 store. 30668bcb0991SDimitry Andric // We do this by first extending the stored value to the next largest power 30678bcb0991SDimitry Andric // of 2 type, and then using truncating stores to store the components. 30688bcb0991SDimitry Andric // By doing this, likewise with G_LOAD, generate an extend that can be 30698bcb0991SDimitry Andric // artifact-combined away instead of leaving behind extracts. 3070fe6060f1SDimitry Andric Register SrcReg = StoreMI.getValueReg(); 3071fe6060f1SDimitry Andric Register PtrReg = StoreMI.getPointerReg(); 30728bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 3073fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 3074fe6060f1SDimitry Andric MachineMemOperand &MMO = **StoreMI.memoperands_begin(); 3075fe6060f1SDimitry Andric LLT MemTy = MMO.getMemoryType(); 3076fe6060f1SDimitry Andric 3077fe6060f1SDimitry Andric unsigned StoreWidth = MemTy.getSizeInBits(); 3078fe6060f1SDimitry Andric unsigned StoreSizeInBits = 8 * MemTy.getSizeInBytes(); 3079fe6060f1SDimitry Andric 3080fe6060f1SDimitry Andric if (StoreWidth != StoreSizeInBits) { 3081349cc55cSDimitry Andric if (SrcTy.isVector()) 3082349cc55cSDimitry Andric return UnableToLegalize; 3083349cc55cSDimitry Andric 3084fe6060f1SDimitry Andric // Promote to a byte-sized store with upper bits zero if not 3085fe6060f1SDimitry Andric // storing an integral number of bytes. For example, promote 3086fe6060f1SDimitry Andric // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 3087fe6060f1SDimitry Andric LLT WideTy = LLT::scalar(StoreSizeInBits); 3088fe6060f1SDimitry Andric 3089fe6060f1SDimitry Andric if (StoreSizeInBits > SrcTy.getSizeInBits()) { 3090fe6060f1SDimitry Andric // Avoid creating a store with a narrower source than result. 3091fe6060f1SDimitry Andric SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0); 3092fe6060f1SDimitry Andric SrcTy = WideTy; 3093fe6060f1SDimitry Andric } 3094fe6060f1SDimitry Andric 3095fe6060f1SDimitry Andric auto ZextInReg = MIRBuilder.buildZExtInReg(SrcTy, SrcReg, StoreWidth); 3096fe6060f1SDimitry Andric 3097fe6060f1SDimitry Andric MachineMemOperand *NewMMO = 3098fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, MMO.getPointerInfo(), WideTy); 3099fe6060f1SDimitry Andric MIRBuilder.buildStore(ZextInReg, PtrReg, *NewMMO); 3100fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 3101fe6060f1SDimitry Andric return Legalized; 3102fe6060f1SDimitry Andric } 3103fe6060f1SDimitry Andric 3104349cc55cSDimitry Andric if (MemTy.isVector()) { 3105349cc55cSDimitry Andric // TODO: Handle vector trunc stores 3106349cc55cSDimitry Andric if (MemTy != SrcTy) 3107349cc55cSDimitry Andric return UnableToLegalize; 3108349cc55cSDimitry Andric 3109349cc55cSDimitry Andric // TODO: We can do better than scalarizing the vector and at least split it 3110349cc55cSDimitry Andric // in half. 3111349cc55cSDimitry Andric return reduceLoadStoreWidth(StoreMI, 0, SrcTy.getElementType()); 3112349cc55cSDimitry Andric } 3113349cc55cSDimitry Andric 3114349cc55cSDimitry Andric unsigned MemSizeInBits = MemTy.getSizeInBits(); 3115349cc55cSDimitry Andric uint64_t LargeSplitSize, SmallSplitSize; 3116349cc55cSDimitry Andric 3117349cc55cSDimitry Andric if (!isPowerOf2_32(MemSizeInBits)) { 3118349cc55cSDimitry Andric LargeSplitSize = PowerOf2Floor(MemTy.getSizeInBits()); 3119349cc55cSDimitry Andric SmallSplitSize = MemTy.getSizeInBits() - LargeSplitSize; 3120349cc55cSDimitry Andric } else { 3121349cc55cSDimitry Andric auto &Ctx = MF.getFunction().getContext(); 3122349cc55cSDimitry Andric if (TLI.allowsMemoryAccess(Ctx, MIRBuilder.getDataLayout(), MemTy, MMO)) 31238bcb0991SDimitry Andric return UnableToLegalize; // Don't know what we're being asked to do. 31248bcb0991SDimitry Andric 3125349cc55cSDimitry Andric SmallSplitSize = LargeSplitSize = MemSizeInBits / 2; 3126349cc55cSDimitry Andric } 3127349cc55cSDimitry Andric 3128fe6060f1SDimitry Andric // Extend to the next pow-2. If this store was itself the result of lowering, 3129fe6060f1SDimitry Andric // e.g. an s56 store being broken into s32 + s24, we might have a stored type 3130349cc55cSDimitry Andric // that's wider than the stored size. 3131349cc55cSDimitry Andric unsigned AnyExtSize = PowerOf2Ceil(MemTy.getSizeInBits()); 3132349cc55cSDimitry Andric const LLT NewSrcTy = LLT::scalar(AnyExtSize); 3133349cc55cSDimitry Andric 3134349cc55cSDimitry Andric if (SrcTy.isPointer()) { 3135349cc55cSDimitry Andric const LLT IntPtrTy = LLT::scalar(SrcTy.getSizeInBits()); 3136349cc55cSDimitry Andric SrcReg = MIRBuilder.buildPtrToInt(IntPtrTy, SrcReg).getReg(0); 3137349cc55cSDimitry Andric } 3138349cc55cSDimitry Andric 3139fe6060f1SDimitry Andric auto ExtVal = MIRBuilder.buildAnyExtOrTrunc(NewSrcTy, SrcReg); 31408bcb0991SDimitry Andric 31418bcb0991SDimitry Andric // Obtain the smaller value by shifting away the larger value. 3142fe6060f1SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(NewSrcTy, LargeSplitSize); 3143fe6060f1SDimitry Andric auto SmallVal = MIRBuilder.buildLShr(NewSrcTy, ExtVal, ShiftAmt); 31448bcb0991SDimitry Andric 3145480093f4SDimitry Andric // Generate the PtrAdd and truncating stores. 31468bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 31475ffd83dbSDimitry Andric auto OffsetCst = MIRBuilder.buildConstant( 31485ffd83dbSDimitry Andric LLT::scalar(PtrTy.getSizeInBits()), LargeSplitSize / 8); 3149480093f4SDimitry Andric auto SmallPtr = 3150349cc55cSDimitry Andric MIRBuilder.buildPtrAdd(PtrTy, PtrReg, OffsetCst); 31518bcb0991SDimitry Andric 31528bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 31538bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 31548bcb0991SDimitry Andric MachineMemOperand *SmallMMO = 31558bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8); 3156fe6060f1SDimitry Andric MIRBuilder.buildStore(ExtVal, PtrReg, *LargeMMO); 3157fe6060f1SDimitry Andric MIRBuilder.buildStore(SmallVal, SmallPtr, *SmallMMO); 3158fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 31598bcb0991SDimitry Andric return Legalized; 31608bcb0991SDimitry Andric } 3161e8d8bef9SDimitry Andric 3162e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 3163e8d8bef9SDimitry Andric LegalizerHelper::bitcast(MachineInstr &MI, unsigned TypeIdx, LLT CastTy) { 3164e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 3165e8d8bef9SDimitry Andric case TargetOpcode::G_LOAD: { 3166e8d8bef9SDimitry Andric if (TypeIdx != 0) 3167e8d8bef9SDimitry Andric return UnableToLegalize; 3168fe6060f1SDimitry Andric MachineMemOperand &MMO = **MI.memoperands_begin(); 3169fe6060f1SDimitry Andric 3170fe6060f1SDimitry Andric // Not sure how to interpret a bitcast of an extending load. 3171fe6060f1SDimitry Andric if (MMO.getMemoryType().getSizeInBits() != CastTy.getSizeInBits()) 3172fe6060f1SDimitry Andric return UnableToLegalize; 3173e8d8bef9SDimitry Andric 3174e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3175e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3176fe6060f1SDimitry Andric MMO.setType(CastTy); 3177e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3178e8d8bef9SDimitry Andric return Legalized; 3179e8d8bef9SDimitry Andric } 3180e8d8bef9SDimitry Andric case TargetOpcode::G_STORE: { 3181e8d8bef9SDimitry Andric if (TypeIdx != 0) 3182e8d8bef9SDimitry Andric return UnableToLegalize; 3183e8d8bef9SDimitry Andric 3184fe6060f1SDimitry Andric MachineMemOperand &MMO = **MI.memoperands_begin(); 3185fe6060f1SDimitry Andric 3186fe6060f1SDimitry Andric // Not sure how to interpret a bitcast of a truncating store. 3187fe6060f1SDimitry Andric if (MMO.getMemoryType().getSizeInBits() != CastTy.getSizeInBits()) 3188fe6060f1SDimitry Andric return UnableToLegalize; 3189fe6060f1SDimitry Andric 3190e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3191e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 0); 3192fe6060f1SDimitry Andric MMO.setType(CastTy); 3193e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3194e8d8bef9SDimitry Andric return Legalized; 3195e8d8bef9SDimitry Andric } 3196e8d8bef9SDimitry Andric case TargetOpcode::G_SELECT: { 3197e8d8bef9SDimitry Andric if (TypeIdx != 0) 3198e8d8bef9SDimitry Andric return UnableToLegalize; 3199e8d8bef9SDimitry Andric 3200e8d8bef9SDimitry Andric if (MRI.getType(MI.getOperand(1).getReg()).isVector()) { 3201e8d8bef9SDimitry Andric LLVM_DEBUG( 3202e8d8bef9SDimitry Andric dbgs() << "bitcast action not implemented for vector select\n"); 3203e8d8bef9SDimitry Andric return UnableToLegalize; 3204e8d8bef9SDimitry Andric } 3205e8d8bef9SDimitry Andric 3206e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3207e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 2); 3208e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 3); 3209e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3210e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3211e8d8bef9SDimitry Andric return Legalized; 3212e8d8bef9SDimitry Andric } 3213e8d8bef9SDimitry Andric case TargetOpcode::G_AND: 3214e8d8bef9SDimitry Andric case TargetOpcode::G_OR: 3215e8d8bef9SDimitry Andric case TargetOpcode::G_XOR: { 3216e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3217e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 1); 3218e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 2); 3219e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3220e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3221e8d8bef9SDimitry Andric return Legalized; 3222e8d8bef9SDimitry Andric } 3223e8d8bef9SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: 3224e8d8bef9SDimitry Andric return bitcastExtractVectorElt(MI, TypeIdx, CastTy); 3225e8d8bef9SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: 3226e8d8bef9SDimitry Andric return bitcastInsertVectorElt(MI, TypeIdx, CastTy); 3227e8d8bef9SDimitry Andric default: 3228e8d8bef9SDimitry Andric return UnableToLegalize; 3229e8d8bef9SDimitry Andric } 3230e8d8bef9SDimitry Andric } 3231e8d8bef9SDimitry Andric 3232e8d8bef9SDimitry Andric // Legalize an instruction by changing the opcode in place. 3233e8d8bef9SDimitry Andric void LegalizerHelper::changeOpcode(MachineInstr &MI, unsigned NewOpcode) { 3234e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3235e8d8bef9SDimitry Andric MI.setDesc(MIRBuilder.getTII().get(NewOpcode)); 3236e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3237e8d8bef9SDimitry Andric } 3238e8d8bef9SDimitry Andric 3239e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 3240e8d8bef9SDimitry Andric LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT LowerHintTy) { 3241e8d8bef9SDimitry Andric using namespace TargetOpcode; 3242e8d8bef9SDimitry Andric 3243e8d8bef9SDimitry Andric switch(MI.getOpcode()) { 3244e8d8bef9SDimitry Andric default: 3245e8d8bef9SDimitry Andric return UnableToLegalize; 3246e8d8bef9SDimitry Andric case TargetOpcode::G_BITCAST: 3247e8d8bef9SDimitry Andric return lowerBitcast(MI); 3248e8d8bef9SDimitry Andric case TargetOpcode::G_SREM: 3249e8d8bef9SDimitry Andric case TargetOpcode::G_UREM: { 3250e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3251e8d8bef9SDimitry Andric auto Quot = 3252e8d8bef9SDimitry Andric MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV, {Ty}, 3253e8d8bef9SDimitry Andric {MI.getOperand(1), MI.getOperand(2)}); 3254e8d8bef9SDimitry Andric 3255e8d8bef9SDimitry Andric auto Prod = MIRBuilder.buildMul(Ty, Quot, MI.getOperand(2)); 3256e8d8bef9SDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MI.getOperand(1), Prod); 3257e8d8bef9SDimitry Andric MI.eraseFromParent(); 3258e8d8bef9SDimitry Andric return Legalized; 3259e8d8bef9SDimitry Andric } 3260e8d8bef9SDimitry Andric case TargetOpcode::G_SADDO: 3261e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBO: 3262e8d8bef9SDimitry Andric return lowerSADDO_SSUBO(MI); 3263e8d8bef9SDimitry Andric case TargetOpcode::G_UMULH: 3264e8d8bef9SDimitry Andric case TargetOpcode::G_SMULH: 3265e8d8bef9SDimitry Andric return lowerSMULH_UMULH(MI); 3266e8d8bef9SDimitry Andric case TargetOpcode::G_SMULO: 3267e8d8bef9SDimitry Andric case TargetOpcode::G_UMULO: { 3268e8d8bef9SDimitry Andric // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the 3269e8d8bef9SDimitry Andric // result. 3270e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3271e8d8bef9SDimitry Andric Register Overflow = MI.getOperand(1).getReg(); 3272e8d8bef9SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 3273e8d8bef9SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 3274e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3275e8d8bef9SDimitry Andric 3276e8d8bef9SDimitry Andric unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO 3277e8d8bef9SDimitry Andric ? TargetOpcode::G_SMULH 3278e8d8bef9SDimitry Andric : TargetOpcode::G_UMULH; 3279e8d8bef9SDimitry Andric 3280e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3281e8d8bef9SDimitry Andric const auto &TII = MIRBuilder.getTII(); 3282e8d8bef9SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_MUL)); 328381ad6265SDimitry Andric MI.removeOperand(1); 3284e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3285e8d8bef9SDimitry Andric 3286e8d8bef9SDimitry Andric auto HiPart = MIRBuilder.buildInstr(Opcode, {Ty}, {LHS, RHS}); 3287e8d8bef9SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 3288e8d8bef9SDimitry Andric 3289e8d8bef9SDimitry Andric // Move insert point forward so we can use the Res register if needed. 3290e8d8bef9SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 3291e8d8bef9SDimitry Andric 3292e8d8bef9SDimitry Andric // For *signed* multiply, overflow is detected by checking: 3293e8d8bef9SDimitry Andric // (hi != (lo >> bitwidth-1)) 3294e8d8bef9SDimitry Andric if (Opcode == TargetOpcode::G_SMULH) { 3295e8d8bef9SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, Ty.getSizeInBits() - 1); 3296e8d8bef9SDimitry Andric auto Shifted = MIRBuilder.buildAShr(Ty, Res, ShiftAmt); 3297e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted); 3298e8d8bef9SDimitry Andric } else { 3299e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero); 3300e8d8bef9SDimitry Andric } 3301e8d8bef9SDimitry Andric return Legalized; 3302e8d8bef9SDimitry Andric } 3303e8d8bef9SDimitry Andric case TargetOpcode::G_FNEG: { 3304e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3305e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3306e8d8bef9SDimitry Andric 3307e8d8bef9SDimitry Andric // TODO: Handle vector types once we are able to 3308e8d8bef9SDimitry Andric // represent them. 3309e8d8bef9SDimitry Andric if (Ty.isVector()) 3310e8d8bef9SDimitry Andric return UnableToLegalize; 3311e8d8bef9SDimitry Andric auto SignMask = 3312e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignMask(Ty.getSizeInBits())); 3313e8d8bef9SDimitry Andric Register SubByReg = MI.getOperand(1).getReg(); 3314e8d8bef9SDimitry Andric MIRBuilder.buildXor(Res, SubByReg, SignMask); 3315e8d8bef9SDimitry Andric MI.eraseFromParent(); 3316e8d8bef9SDimitry Andric return Legalized; 3317e8d8bef9SDimitry Andric } 3318e8d8bef9SDimitry Andric case TargetOpcode::G_FSUB: { 3319e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3320e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3321e8d8bef9SDimitry Andric 3322e8d8bef9SDimitry Andric // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)). 3323e8d8bef9SDimitry Andric // First, check if G_FNEG is marked as Lower. If so, we may 3324e8d8bef9SDimitry Andric // end up with an infinite loop as G_FSUB is used to legalize G_FNEG. 3325e8d8bef9SDimitry Andric if (LI.getAction({G_FNEG, {Ty}}).Action == Lower) 3326e8d8bef9SDimitry Andric return UnableToLegalize; 3327e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 3328e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 3329e8d8bef9SDimitry Andric Register Neg = MRI.createGenericVirtualRegister(Ty); 3330e8d8bef9SDimitry Andric MIRBuilder.buildFNeg(Neg, RHS); 3331e8d8bef9SDimitry Andric MIRBuilder.buildFAdd(Res, LHS, Neg, MI.getFlags()); 3332e8d8bef9SDimitry Andric MI.eraseFromParent(); 3333e8d8bef9SDimitry Andric return Legalized; 3334e8d8bef9SDimitry Andric } 3335e8d8bef9SDimitry Andric case TargetOpcode::G_FMAD: 3336e8d8bef9SDimitry Andric return lowerFMad(MI); 3337e8d8bef9SDimitry Andric case TargetOpcode::G_FFLOOR: 3338e8d8bef9SDimitry Andric return lowerFFloor(MI); 3339e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUND: 3340e8d8bef9SDimitry Andric return lowerIntrinsicRound(MI); 3341e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: { 3342e8d8bef9SDimitry Andric // Since round even is the assumed rounding mode for unconstrained FP 3343e8d8bef9SDimitry Andric // operations, rint and roundeven are the same operation. 3344e8d8bef9SDimitry Andric changeOpcode(MI, TargetOpcode::G_FRINT); 3345e8d8bef9SDimitry Andric return Legalized; 3346e8d8bef9SDimitry Andric } 3347e8d8bef9SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: { 3348e8d8bef9SDimitry Andric Register OldValRes = MI.getOperand(0).getReg(); 3349e8d8bef9SDimitry Andric Register SuccessRes = MI.getOperand(1).getReg(); 3350e8d8bef9SDimitry Andric Register Addr = MI.getOperand(2).getReg(); 3351e8d8bef9SDimitry Andric Register CmpVal = MI.getOperand(3).getReg(); 3352e8d8bef9SDimitry Andric Register NewVal = MI.getOperand(4).getReg(); 3353e8d8bef9SDimitry Andric MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal, 3354e8d8bef9SDimitry Andric **MI.memoperands_begin()); 3355e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal); 3356e8d8bef9SDimitry Andric MI.eraseFromParent(); 3357e8d8bef9SDimitry Andric return Legalized; 3358e8d8bef9SDimitry Andric } 3359e8d8bef9SDimitry Andric case TargetOpcode::G_LOAD: 3360e8d8bef9SDimitry Andric case TargetOpcode::G_SEXTLOAD: 3361e8d8bef9SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 3362fe6060f1SDimitry Andric return lowerLoad(cast<GAnyLoad>(MI)); 3363e8d8bef9SDimitry Andric case TargetOpcode::G_STORE: 3364fe6060f1SDimitry Andric return lowerStore(cast<GStore>(MI)); 33650b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 33660b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 33670b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 33680b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 33690b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: 3370e8d8bef9SDimitry Andric return lowerBitCount(MI); 33710b57cec5SDimitry Andric case G_UADDO: { 33720b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33730b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 33740b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33750b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33760b57cec5SDimitry Andric 33770b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, LHS, RHS); 33780b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS); 33790b57cec5SDimitry Andric 33800b57cec5SDimitry Andric MI.eraseFromParent(); 33810b57cec5SDimitry Andric return Legalized; 33820b57cec5SDimitry Andric } 33830b57cec5SDimitry Andric case G_UADDE: { 33840b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33850b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 33860b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33870b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33880b57cec5SDimitry Andric Register CarryIn = MI.getOperand(4).getReg(); 33895ffd83dbSDimitry Andric LLT Ty = MRI.getType(Res); 33900b57cec5SDimitry Andric 33915ffd83dbSDimitry Andric auto TmpRes = MIRBuilder.buildAdd(Ty, LHS, RHS); 33925ffd83dbSDimitry Andric auto ZExtCarryIn = MIRBuilder.buildZExt(Ty, CarryIn); 33930b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn); 33940b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS); 33950b57cec5SDimitry Andric 33960b57cec5SDimitry Andric MI.eraseFromParent(); 33970b57cec5SDimitry Andric return Legalized; 33980b57cec5SDimitry Andric } 33990b57cec5SDimitry Andric case G_USUBO: { 34000b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 34010b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 34020b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 34030b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 34040b57cec5SDimitry Andric 34050b57cec5SDimitry Andric MIRBuilder.buildSub(Res, LHS, RHS); 34060b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS); 34070b57cec5SDimitry Andric 34080b57cec5SDimitry Andric MI.eraseFromParent(); 34090b57cec5SDimitry Andric return Legalized; 34100b57cec5SDimitry Andric } 34110b57cec5SDimitry Andric case G_USUBE: { 34120b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 34130b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 34140b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 34150b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 34160b57cec5SDimitry Andric Register BorrowIn = MI.getOperand(4).getReg(); 34175ffd83dbSDimitry Andric const LLT CondTy = MRI.getType(BorrowOut); 34185ffd83dbSDimitry Andric const LLT Ty = MRI.getType(Res); 34190b57cec5SDimitry Andric 34205ffd83dbSDimitry Andric auto TmpRes = MIRBuilder.buildSub(Ty, LHS, RHS); 34215ffd83dbSDimitry Andric auto ZExtBorrowIn = MIRBuilder.buildZExt(Ty, BorrowIn); 34220b57cec5SDimitry Andric MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn); 34235ffd83dbSDimitry Andric 34245ffd83dbSDimitry Andric auto LHS_EQ_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, CondTy, LHS, RHS); 34255ffd83dbSDimitry Andric auto LHS_ULT_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CondTy, LHS, RHS); 34260b57cec5SDimitry Andric MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS); 34270b57cec5SDimitry Andric 34280b57cec5SDimitry Andric MI.eraseFromParent(); 34290b57cec5SDimitry Andric return Legalized; 34300b57cec5SDimitry Andric } 34310b57cec5SDimitry Andric case G_UITOFP: 3432e8d8bef9SDimitry Andric return lowerUITOFP(MI); 34330b57cec5SDimitry Andric case G_SITOFP: 3434e8d8bef9SDimitry Andric return lowerSITOFP(MI); 34358bcb0991SDimitry Andric case G_FPTOUI: 3436e8d8bef9SDimitry Andric return lowerFPTOUI(MI); 34375ffd83dbSDimitry Andric case G_FPTOSI: 34385ffd83dbSDimitry Andric return lowerFPTOSI(MI); 34395ffd83dbSDimitry Andric case G_FPTRUNC: 3440e8d8bef9SDimitry Andric return lowerFPTRUNC(MI); 3441e8d8bef9SDimitry Andric case G_FPOWI: 3442e8d8bef9SDimitry Andric return lowerFPOWI(MI); 34430b57cec5SDimitry Andric case G_SMIN: 34440b57cec5SDimitry Andric case G_SMAX: 34450b57cec5SDimitry Andric case G_UMIN: 34460b57cec5SDimitry Andric case G_UMAX: 3447e8d8bef9SDimitry Andric return lowerMinMax(MI); 34480b57cec5SDimitry Andric case G_FCOPYSIGN: 3449e8d8bef9SDimitry Andric return lowerFCopySign(MI); 34500b57cec5SDimitry Andric case G_FMINNUM: 34510b57cec5SDimitry Andric case G_FMAXNUM: 34520b57cec5SDimitry Andric return lowerFMinNumMaxNum(MI); 34535ffd83dbSDimitry Andric case G_MERGE_VALUES: 34545ffd83dbSDimitry Andric return lowerMergeValues(MI); 34558bcb0991SDimitry Andric case G_UNMERGE_VALUES: 34568bcb0991SDimitry Andric return lowerUnmergeValues(MI); 34578bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 34588bcb0991SDimitry Andric assert(MI.getOperand(2).isImm() && "Expected immediate"); 34598bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 34608bcb0991SDimitry Andric 34618bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 34628bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 34638bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 34648bcb0991SDimitry Andric Register TmpRes = MRI.createGenericVirtualRegister(DstTy); 34658bcb0991SDimitry Andric 34668bcb0991SDimitry Andric auto MIBSz = MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - SizeInBits); 34675ffd83dbSDimitry Andric MIRBuilder.buildShl(TmpRes, SrcReg, MIBSz->getOperand(0)); 34685ffd83dbSDimitry Andric MIRBuilder.buildAShr(DstReg, TmpRes, MIBSz->getOperand(0)); 34698bcb0991SDimitry Andric MI.eraseFromParent(); 34708bcb0991SDimitry Andric return Legalized; 34718bcb0991SDimitry Andric } 3472e8d8bef9SDimitry Andric case G_EXTRACT_VECTOR_ELT: 3473e8d8bef9SDimitry Andric case G_INSERT_VECTOR_ELT: 3474e8d8bef9SDimitry Andric return lowerExtractInsertVectorElt(MI); 34758bcb0991SDimitry Andric case G_SHUFFLE_VECTOR: 34768bcb0991SDimitry Andric return lowerShuffleVector(MI); 34778bcb0991SDimitry Andric case G_DYN_STACKALLOC: 34788bcb0991SDimitry Andric return lowerDynStackAlloc(MI); 34798bcb0991SDimitry Andric case G_EXTRACT: 34808bcb0991SDimitry Andric return lowerExtract(MI); 34818bcb0991SDimitry Andric case G_INSERT: 34828bcb0991SDimitry Andric return lowerInsert(MI); 3483480093f4SDimitry Andric case G_BSWAP: 3484480093f4SDimitry Andric return lowerBswap(MI); 3485480093f4SDimitry Andric case G_BITREVERSE: 3486480093f4SDimitry Andric return lowerBitreverse(MI); 3487480093f4SDimitry Andric case G_READ_REGISTER: 34885ffd83dbSDimitry Andric case G_WRITE_REGISTER: 34895ffd83dbSDimitry Andric return lowerReadWriteRegister(MI); 3490e8d8bef9SDimitry Andric case G_UADDSAT: 3491e8d8bef9SDimitry Andric case G_USUBSAT: { 3492e8d8bef9SDimitry Andric // Try to make a reasonable guess about which lowering strategy to use. The 3493e8d8bef9SDimitry Andric // target can override this with custom lowering and calling the 3494e8d8bef9SDimitry Andric // implementation functions. 3495e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3496e8d8bef9SDimitry Andric if (LI.isLegalOrCustom({G_UMIN, Ty})) 3497e8d8bef9SDimitry Andric return lowerAddSubSatToMinMax(MI); 3498e8d8bef9SDimitry Andric return lowerAddSubSatToAddoSubo(MI); 34990b57cec5SDimitry Andric } 3500e8d8bef9SDimitry Andric case G_SADDSAT: 3501e8d8bef9SDimitry Andric case G_SSUBSAT: { 3502e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3503e8d8bef9SDimitry Andric 3504e8d8bef9SDimitry Andric // FIXME: It would probably make more sense to see if G_SADDO is preferred, 3505e8d8bef9SDimitry Andric // since it's a shorter expansion. However, we would need to figure out the 3506e8d8bef9SDimitry Andric // preferred boolean type for the carry out for the query. 3507e8d8bef9SDimitry Andric if (LI.isLegalOrCustom({G_SMIN, Ty}) && LI.isLegalOrCustom({G_SMAX, Ty})) 3508e8d8bef9SDimitry Andric return lowerAddSubSatToMinMax(MI); 3509e8d8bef9SDimitry Andric return lowerAddSubSatToAddoSubo(MI); 3510e8d8bef9SDimitry Andric } 3511e8d8bef9SDimitry Andric case G_SSHLSAT: 3512e8d8bef9SDimitry Andric case G_USHLSAT: 3513e8d8bef9SDimitry Andric return lowerShlSat(MI); 3514fe6060f1SDimitry Andric case G_ABS: 3515fe6060f1SDimitry Andric return lowerAbsToAddXor(MI); 3516e8d8bef9SDimitry Andric case G_SELECT: 3517e8d8bef9SDimitry Andric return lowerSelect(MI); 3518fe6060f1SDimitry Andric case G_SDIVREM: 3519fe6060f1SDimitry Andric case G_UDIVREM: 3520fe6060f1SDimitry Andric return lowerDIVREM(MI); 3521fe6060f1SDimitry Andric case G_FSHL: 3522fe6060f1SDimitry Andric case G_FSHR: 3523fe6060f1SDimitry Andric return lowerFunnelShift(MI); 3524fe6060f1SDimitry Andric case G_ROTL: 3525fe6060f1SDimitry Andric case G_ROTR: 3526fe6060f1SDimitry Andric return lowerRotate(MI); 3527349cc55cSDimitry Andric case G_MEMSET: 3528349cc55cSDimitry Andric case G_MEMCPY: 3529349cc55cSDimitry Andric case G_MEMMOVE: 3530349cc55cSDimitry Andric return lowerMemCpyFamily(MI); 3531349cc55cSDimitry Andric case G_MEMCPY_INLINE: 3532349cc55cSDimitry Andric return lowerMemcpyInline(MI); 3533349cc55cSDimitry Andric GISEL_VECREDUCE_CASES_NONSEQ 3534349cc55cSDimitry Andric return lowerVectorReduction(MI); 3535e8d8bef9SDimitry Andric } 3536e8d8bef9SDimitry Andric } 3537e8d8bef9SDimitry Andric 3538e8d8bef9SDimitry Andric Align LegalizerHelper::getStackTemporaryAlignment(LLT Ty, 3539e8d8bef9SDimitry Andric Align MinAlign) const { 3540e8d8bef9SDimitry Andric // FIXME: We're missing a way to go back from LLT to llvm::Type to query the 3541e8d8bef9SDimitry Andric // datalayout for the preferred alignment. Also there should be a target hook 3542e8d8bef9SDimitry Andric // for this to allow targets to reduce the alignment and ignore the 3543e8d8bef9SDimitry Andric // datalayout. e.g. AMDGPU should always use a 4-byte alignment, regardless of 3544e8d8bef9SDimitry Andric // the type. 3545e8d8bef9SDimitry Andric return std::max(Align(PowerOf2Ceil(Ty.getSizeInBytes())), MinAlign); 3546e8d8bef9SDimitry Andric } 3547e8d8bef9SDimitry Andric 3548e8d8bef9SDimitry Andric MachineInstrBuilder 3549e8d8bef9SDimitry Andric LegalizerHelper::createStackTemporary(TypeSize Bytes, Align Alignment, 3550e8d8bef9SDimitry Andric MachinePointerInfo &PtrInfo) { 3551e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 3552e8d8bef9SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 3553e8d8bef9SDimitry Andric int FrameIdx = MF.getFrameInfo().CreateStackObject(Bytes, Alignment, false); 3554e8d8bef9SDimitry Andric 3555e8d8bef9SDimitry Andric unsigned AddrSpace = DL.getAllocaAddrSpace(); 3556e8d8bef9SDimitry Andric LLT FramePtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace)); 3557e8d8bef9SDimitry Andric 3558e8d8bef9SDimitry Andric PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIdx); 3559e8d8bef9SDimitry Andric return MIRBuilder.buildFrameIndex(FramePtrTy, FrameIdx); 3560e8d8bef9SDimitry Andric } 3561e8d8bef9SDimitry Andric 3562e8d8bef9SDimitry Andric static Register clampDynamicVectorIndex(MachineIRBuilder &B, Register IdxReg, 3563e8d8bef9SDimitry Andric LLT VecTy) { 3564e8d8bef9SDimitry Andric int64_t IdxVal; 3565e8d8bef9SDimitry Andric if (mi_match(IdxReg, *B.getMRI(), m_ICst(IdxVal))) 3566e8d8bef9SDimitry Andric return IdxReg; 3567e8d8bef9SDimitry Andric 3568e8d8bef9SDimitry Andric LLT IdxTy = B.getMRI()->getType(IdxReg); 3569e8d8bef9SDimitry Andric unsigned NElts = VecTy.getNumElements(); 3570e8d8bef9SDimitry Andric if (isPowerOf2_32(NElts)) { 3571e8d8bef9SDimitry Andric APInt Imm = APInt::getLowBitsSet(IdxTy.getSizeInBits(), Log2_32(NElts)); 3572e8d8bef9SDimitry Andric return B.buildAnd(IdxTy, IdxReg, B.buildConstant(IdxTy, Imm)).getReg(0); 3573e8d8bef9SDimitry Andric } 3574e8d8bef9SDimitry Andric 3575e8d8bef9SDimitry Andric return B.buildUMin(IdxTy, IdxReg, B.buildConstant(IdxTy, NElts - 1)) 3576e8d8bef9SDimitry Andric .getReg(0); 3577e8d8bef9SDimitry Andric } 3578e8d8bef9SDimitry Andric 3579e8d8bef9SDimitry Andric Register LegalizerHelper::getVectorElementPointer(Register VecPtr, LLT VecTy, 3580e8d8bef9SDimitry Andric Register Index) { 3581e8d8bef9SDimitry Andric LLT EltTy = VecTy.getElementType(); 3582e8d8bef9SDimitry Andric 3583e8d8bef9SDimitry Andric // Calculate the element offset and add it to the pointer. 3584e8d8bef9SDimitry Andric unsigned EltSize = EltTy.getSizeInBits() / 8; // FIXME: should be ABI size. 3585e8d8bef9SDimitry Andric assert(EltSize * 8 == EltTy.getSizeInBits() && 3586e8d8bef9SDimitry Andric "Converting bits to bytes lost precision"); 3587e8d8bef9SDimitry Andric 3588e8d8bef9SDimitry Andric Index = clampDynamicVectorIndex(MIRBuilder, Index, VecTy); 3589e8d8bef9SDimitry Andric 3590e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Index); 3591e8d8bef9SDimitry Andric auto Mul = MIRBuilder.buildMul(IdxTy, Index, 3592e8d8bef9SDimitry Andric MIRBuilder.buildConstant(IdxTy, EltSize)); 3593e8d8bef9SDimitry Andric 3594e8d8bef9SDimitry Andric LLT PtrTy = MRI.getType(VecPtr); 3595e8d8bef9SDimitry Andric return MIRBuilder.buildPtrAdd(PtrTy, VecPtr, Mul).getReg(0); 35960b57cec5SDimitry Andric } 35970b57cec5SDimitry Andric 35980eae32dcSDimitry Andric #ifndef NDEBUG 35990eae32dcSDimitry Andric /// Check that all vector operands have same number of elements. Other operands 36000eae32dcSDimitry Andric /// should be listed in NonVecOp. 36010eae32dcSDimitry Andric static bool hasSameNumEltsOnAllVectorOperands( 36020eae32dcSDimitry Andric GenericMachineInstr &MI, MachineRegisterInfo &MRI, 36030eae32dcSDimitry Andric std::initializer_list<unsigned> NonVecOpIndices) { 36040eae32dcSDimitry Andric if (MI.getNumMemOperands() != 0) 36050eae32dcSDimitry Andric return false; 36060b57cec5SDimitry Andric 36070eae32dcSDimitry Andric LLT VecTy = MRI.getType(MI.getReg(0)); 36080eae32dcSDimitry Andric if (!VecTy.isVector()) 36090eae32dcSDimitry Andric return false; 36100eae32dcSDimitry Andric unsigned NumElts = VecTy.getNumElements(); 36110b57cec5SDimitry Andric 36120eae32dcSDimitry Andric for (unsigned OpIdx = 1; OpIdx < MI.getNumOperands(); ++OpIdx) { 36130eae32dcSDimitry Andric MachineOperand &Op = MI.getOperand(OpIdx); 36140eae32dcSDimitry Andric if (!Op.isReg()) { 36150eae32dcSDimitry Andric if (!is_contained(NonVecOpIndices, OpIdx)) 36160eae32dcSDimitry Andric return false; 36170eae32dcSDimitry Andric continue; 36180eae32dcSDimitry Andric } 36190b57cec5SDimitry Andric 36200eae32dcSDimitry Andric LLT Ty = MRI.getType(Op.getReg()); 36210eae32dcSDimitry Andric if (!Ty.isVector()) { 36220eae32dcSDimitry Andric if (!is_contained(NonVecOpIndices, OpIdx)) 36230eae32dcSDimitry Andric return false; 36240eae32dcSDimitry Andric continue; 36250eae32dcSDimitry Andric } 36260eae32dcSDimitry Andric 36270eae32dcSDimitry Andric if (Ty.getNumElements() != NumElts) 36280eae32dcSDimitry Andric return false; 36290eae32dcSDimitry Andric } 36300eae32dcSDimitry Andric 36310eae32dcSDimitry Andric return true; 36320eae32dcSDimitry Andric } 36330eae32dcSDimitry Andric #endif 36340eae32dcSDimitry Andric 36350eae32dcSDimitry Andric /// Fill \p DstOps with DstOps that have same number of elements combined as 36360eae32dcSDimitry Andric /// the Ty. These DstOps have either scalar type when \p NumElts = 1 or are 36370eae32dcSDimitry Andric /// vectors with \p NumElts elements. When Ty.getNumElements() is not multiple 36380eae32dcSDimitry Andric /// of \p NumElts last DstOp (leftover) has fewer then \p NumElts elements. 36390eae32dcSDimitry Andric static void makeDstOps(SmallVectorImpl<DstOp> &DstOps, LLT Ty, 36400eae32dcSDimitry Andric unsigned NumElts) { 36410eae32dcSDimitry Andric LLT LeftoverTy; 36420eae32dcSDimitry Andric assert(Ty.isVector() && "Expected vector type"); 36430eae32dcSDimitry Andric LLT EltTy = Ty.getElementType(); 36440eae32dcSDimitry Andric LLT NarrowTy = (NumElts == 1) ? EltTy : LLT::fixed_vector(NumElts, EltTy); 36450eae32dcSDimitry Andric int NumParts, NumLeftover; 36460eae32dcSDimitry Andric std::tie(NumParts, NumLeftover) = 36470eae32dcSDimitry Andric getNarrowTypeBreakDown(Ty, NarrowTy, LeftoverTy); 36480eae32dcSDimitry Andric 36490eae32dcSDimitry Andric assert(NumParts > 0 && "Error in getNarrowTypeBreakDown"); 36500eae32dcSDimitry Andric for (int i = 0; i < NumParts; ++i) { 36510eae32dcSDimitry Andric DstOps.push_back(NarrowTy); 36520eae32dcSDimitry Andric } 36530eae32dcSDimitry Andric 36540eae32dcSDimitry Andric if (LeftoverTy.isValid()) { 36550eae32dcSDimitry Andric assert(NumLeftover == 1 && "expected exactly one leftover"); 36560eae32dcSDimitry Andric DstOps.push_back(LeftoverTy); 36570eae32dcSDimitry Andric } 36580eae32dcSDimitry Andric } 36590eae32dcSDimitry Andric 36600eae32dcSDimitry Andric /// Operand \p Op is used on \p N sub-instructions. Fill \p Ops with \p N SrcOps 36610eae32dcSDimitry Andric /// made from \p Op depending on operand type. 36620eae32dcSDimitry Andric static void broadcastSrcOp(SmallVectorImpl<SrcOp> &Ops, unsigned N, 36630eae32dcSDimitry Andric MachineOperand &Op) { 36640eae32dcSDimitry Andric for (unsigned i = 0; i < N; ++i) { 36650eae32dcSDimitry Andric if (Op.isReg()) 36660eae32dcSDimitry Andric Ops.push_back(Op.getReg()); 36670eae32dcSDimitry Andric else if (Op.isImm()) 36680eae32dcSDimitry Andric Ops.push_back(Op.getImm()); 36690eae32dcSDimitry Andric else if (Op.isPredicate()) 36700eae32dcSDimitry Andric Ops.push_back(static_cast<CmpInst::Predicate>(Op.getPredicate())); 36710eae32dcSDimitry Andric else 36720eae32dcSDimitry Andric llvm_unreachable("Unsupported type"); 36730eae32dcSDimitry Andric } 36740b57cec5SDimitry Andric } 36750b57cec5SDimitry Andric 36760b57cec5SDimitry Andric // Handle splitting vector operations which need to have the same number of 36770b57cec5SDimitry Andric // elements in each type index, but each type index may have a different element 36780b57cec5SDimitry Andric // type. 36790b57cec5SDimitry Andric // 36800b57cec5SDimitry Andric // e.g. <4 x s64> = G_SHL <4 x s64>, <4 x s32> -> 36810b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 36820b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 36830b57cec5SDimitry Andric // 36840b57cec5SDimitry Andric // Also handles some irregular breakdown cases, e.g. 36850b57cec5SDimitry Andric // e.g. <3 x s64> = G_SHL <3 x s64>, <3 x s32> -> 36860b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 36870b57cec5SDimitry Andric // s64 = G_SHL s64, s32 36880b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 36890b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorMultiEltType( 36900eae32dcSDimitry Andric GenericMachineInstr &MI, unsigned NumElts, 36910eae32dcSDimitry Andric std::initializer_list<unsigned> NonVecOpIndices) { 36920eae32dcSDimitry Andric assert(hasSameNumEltsOnAllVectorOperands(MI, MRI, NonVecOpIndices) && 36930eae32dcSDimitry Andric "Non-compatible opcode or not specified non-vector operands"); 36940eae32dcSDimitry Andric unsigned OrigNumElts = MRI.getType(MI.getReg(0)).getNumElements(); 36950b57cec5SDimitry Andric 36960eae32dcSDimitry Andric unsigned NumInputs = MI.getNumOperands() - MI.getNumDefs(); 36970eae32dcSDimitry Andric unsigned NumDefs = MI.getNumDefs(); 36980b57cec5SDimitry Andric 36990eae32dcSDimitry Andric // Create DstOps (sub-vectors with NumElts elts + Leftover) for each output. 37000eae32dcSDimitry Andric // Build instructions with DstOps to use instruction found by CSE directly. 37010eae32dcSDimitry Andric // CSE copies found instruction into given vreg when building with vreg dest. 37020eae32dcSDimitry Andric SmallVector<SmallVector<DstOp, 8>, 2> OutputOpsPieces(NumDefs); 37030eae32dcSDimitry Andric // Output registers will be taken from created instructions. 37040eae32dcSDimitry Andric SmallVector<SmallVector<Register, 8>, 2> OutputRegs(NumDefs); 37050eae32dcSDimitry Andric for (unsigned i = 0; i < NumDefs; ++i) { 37060eae32dcSDimitry Andric makeDstOps(OutputOpsPieces[i], MRI.getType(MI.getReg(i)), NumElts); 37070b57cec5SDimitry Andric } 37080b57cec5SDimitry Andric 37090eae32dcSDimitry Andric // Split vector input operands into sub-vectors with NumElts elts + Leftover. 37100eae32dcSDimitry Andric // Operands listed in NonVecOpIndices will be used as is without splitting; 37110eae32dcSDimitry Andric // examples: compare predicate in icmp and fcmp (op 1), vector select with i1 37120eae32dcSDimitry Andric // scalar condition (op 1), immediate in sext_inreg (op 2). 37130eae32dcSDimitry Andric SmallVector<SmallVector<SrcOp, 8>, 3> InputOpsPieces(NumInputs); 37140eae32dcSDimitry Andric for (unsigned UseIdx = NumDefs, UseNo = 0; UseIdx < MI.getNumOperands(); 37150eae32dcSDimitry Andric ++UseIdx, ++UseNo) { 37160eae32dcSDimitry Andric if (is_contained(NonVecOpIndices, UseIdx)) { 37170eae32dcSDimitry Andric broadcastSrcOp(InputOpsPieces[UseNo], OutputOpsPieces[0].size(), 37180eae32dcSDimitry Andric MI.getOperand(UseIdx)); 37190b57cec5SDimitry Andric } else { 37200eae32dcSDimitry Andric SmallVector<Register, 8> SplitPieces; 37210eae32dcSDimitry Andric extractVectorParts(MI.getReg(UseIdx), NumElts, SplitPieces); 37220eae32dcSDimitry Andric for (auto Reg : SplitPieces) 37230eae32dcSDimitry Andric InputOpsPieces[UseNo].push_back(Reg); 37240eae32dcSDimitry Andric } 37250b57cec5SDimitry Andric } 37260b57cec5SDimitry Andric 37270eae32dcSDimitry Andric unsigned NumLeftovers = OrigNumElts % NumElts ? 1 : 0; 37280eae32dcSDimitry Andric 37290eae32dcSDimitry Andric // Take i-th piece of each input operand split and build sub-vector/scalar 37300eae32dcSDimitry Andric // instruction. Set i-th DstOp(s) from OutputOpsPieces as destination(s). 37310eae32dcSDimitry Andric for (unsigned i = 0; i < OrigNumElts / NumElts + NumLeftovers; ++i) { 37320eae32dcSDimitry Andric SmallVector<DstOp, 2> Defs; 37330eae32dcSDimitry Andric for (unsigned DstNo = 0; DstNo < NumDefs; ++DstNo) 37340eae32dcSDimitry Andric Defs.push_back(OutputOpsPieces[DstNo][i]); 37350eae32dcSDimitry Andric 37360eae32dcSDimitry Andric SmallVector<SrcOp, 3> Uses; 37370eae32dcSDimitry Andric for (unsigned InputNo = 0; InputNo < NumInputs; ++InputNo) 37380eae32dcSDimitry Andric Uses.push_back(InputOpsPieces[InputNo][i]); 37390eae32dcSDimitry Andric 37400eae32dcSDimitry Andric auto I = MIRBuilder.buildInstr(MI.getOpcode(), Defs, Uses, MI.getFlags()); 37410eae32dcSDimitry Andric for (unsigned DstNo = 0; DstNo < NumDefs; ++DstNo) 37420eae32dcSDimitry Andric OutputRegs[DstNo].push_back(I.getReg(DstNo)); 37430b57cec5SDimitry Andric } 37440b57cec5SDimitry Andric 37450eae32dcSDimitry Andric // Merge small outputs into MI's output for each def operand. 37460eae32dcSDimitry Andric if (NumLeftovers) { 37470eae32dcSDimitry Andric for (unsigned i = 0; i < NumDefs; ++i) 37480eae32dcSDimitry Andric mergeMixedSubvectors(MI.getReg(i), OutputRegs[i]); 37490eae32dcSDimitry Andric } else { 37500eae32dcSDimitry Andric for (unsigned i = 0; i < NumDefs; ++i) 37510eae32dcSDimitry Andric MIRBuilder.buildMerge(MI.getReg(i), OutputRegs[i]); 37520eae32dcSDimitry Andric } 37530b57cec5SDimitry Andric 37540b57cec5SDimitry Andric MI.eraseFromParent(); 37550b57cec5SDimitry Andric return Legalized; 37560b57cec5SDimitry Andric } 37570b57cec5SDimitry Andric 37580b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 37590eae32dcSDimitry Andric LegalizerHelper::fewerElementsVectorPhi(GenericMachineInstr &MI, 37600eae32dcSDimitry Andric unsigned NumElts) { 37610eae32dcSDimitry Andric unsigned OrigNumElts = MRI.getType(MI.getReg(0)).getNumElements(); 37620b57cec5SDimitry Andric 37630eae32dcSDimitry Andric unsigned NumInputs = MI.getNumOperands() - MI.getNumDefs(); 37640eae32dcSDimitry Andric unsigned NumDefs = MI.getNumDefs(); 37650b57cec5SDimitry Andric 37660eae32dcSDimitry Andric SmallVector<DstOp, 8> OutputOpsPieces; 37670eae32dcSDimitry Andric SmallVector<Register, 8> OutputRegs; 37680eae32dcSDimitry Andric makeDstOps(OutputOpsPieces, MRI.getType(MI.getReg(0)), NumElts); 37690b57cec5SDimitry Andric 37700eae32dcSDimitry Andric // Instructions that perform register split will be inserted in basic block 37710eae32dcSDimitry Andric // where register is defined (basic block is in the next operand). 37720eae32dcSDimitry Andric SmallVector<SmallVector<Register, 8>, 3> InputOpsPieces(NumInputs / 2); 37730eae32dcSDimitry Andric for (unsigned UseIdx = NumDefs, UseNo = 0; UseIdx < MI.getNumOperands(); 37740eae32dcSDimitry Andric UseIdx += 2, ++UseNo) { 37750eae32dcSDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(UseIdx + 1).getMBB(); 37760b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 37770eae32dcSDimitry Andric extractVectorParts(MI.getReg(UseIdx), NumElts, InputOpsPieces[UseNo]); 37780b57cec5SDimitry Andric } 37790eae32dcSDimitry Andric 37800eae32dcSDimitry Andric // Build PHIs with fewer elements. 37810eae32dcSDimitry Andric unsigned NumLeftovers = OrigNumElts % NumElts ? 1 : 0; 37820eae32dcSDimitry Andric MIRBuilder.setInsertPt(*MI.getParent(), MI); 37830eae32dcSDimitry Andric for (unsigned i = 0; i < OrigNumElts / NumElts + NumLeftovers; ++i) { 37840eae32dcSDimitry Andric auto Phi = MIRBuilder.buildInstr(TargetOpcode::G_PHI); 37850eae32dcSDimitry Andric Phi.addDef( 37860eae32dcSDimitry Andric MRI.createGenericVirtualRegister(OutputOpsPieces[i].getLLTTy(MRI))); 37870eae32dcSDimitry Andric OutputRegs.push_back(Phi.getReg(0)); 37880eae32dcSDimitry Andric 37890eae32dcSDimitry Andric for (unsigned j = 0; j < NumInputs / 2; ++j) { 37900eae32dcSDimitry Andric Phi.addUse(InputOpsPieces[j][i]); 37910eae32dcSDimitry Andric Phi.add(MI.getOperand(1 + j * 2 + 1)); 37920eae32dcSDimitry Andric } 37930eae32dcSDimitry Andric } 37940eae32dcSDimitry Andric 37950eae32dcSDimitry Andric // Merge small outputs into MI's def. 37960eae32dcSDimitry Andric if (NumLeftovers) { 37970eae32dcSDimitry Andric mergeMixedSubvectors(MI.getReg(0), OutputRegs); 37980eae32dcSDimitry Andric } else { 37990eae32dcSDimitry Andric MIRBuilder.buildMerge(MI.getReg(0), OutputRegs); 38000b57cec5SDimitry Andric } 38010b57cec5SDimitry Andric 38020b57cec5SDimitry Andric MI.eraseFromParent(); 38030b57cec5SDimitry Andric return Legalized; 38040b57cec5SDimitry Andric } 38050b57cec5SDimitry Andric 38060b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 38078bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorUnmergeValues(MachineInstr &MI, 38088bcb0991SDimitry Andric unsigned TypeIdx, 38098bcb0991SDimitry Andric LLT NarrowTy) { 38108bcb0991SDimitry Andric const int NumDst = MI.getNumOperands() - 1; 38118bcb0991SDimitry Andric const Register SrcReg = MI.getOperand(NumDst).getReg(); 38120eae32dcSDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 38138bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 38148bcb0991SDimitry Andric 38150eae32dcSDimitry Andric if (TypeIdx != 1 || NarrowTy == DstTy) 38168bcb0991SDimitry Andric return UnableToLegalize; 38178bcb0991SDimitry Andric 38180eae32dcSDimitry Andric // Requires compatible types. Otherwise SrcReg should have been defined by 38190eae32dcSDimitry Andric // merge-like instruction that would get artifact combined. Most likely 38200eae32dcSDimitry Andric // instruction that defines SrcReg has to perform more/fewer elements 38210eae32dcSDimitry Andric // legalization compatible with NarrowTy. 38220eae32dcSDimitry Andric assert(SrcTy.isVector() && NarrowTy.isVector() && "Expected vector types"); 38230eae32dcSDimitry Andric assert((SrcTy.getScalarType() == NarrowTy.getScalarType()) && "bad type"); 38248bcb0991SDimitry Andric 38250eae32dcSDimitry Andric if ((SrcTy.getSizeInBits() % NarrowTy.getSizeInBits() != 0) || 38260eae32dcSDimitry Andric (NarrowTy.getSizeInBits() % DstTy.getSizeInBits() != 0)) 38270eae32dcSDimitry Andric return UnableToLegalize; 38280eae32dcSDimitry Andric 38290eae32dcSDimitry Andric // This is most likely DstTy (smaller then register size) packed in SrcTy 38300eae32dcSDimitry Andric // (larger then register size) and since unmerge was not combined it will be 38310eae32dcSDimitry Andric // lowered to bit sequence extracts from register. Unpack SrcTy to NarrowTy 38320eae32dcSDimitry Andric // (register size) pieces first. Then unpack each of NarrowTy pieces to DstTy. 38330eae32dcSDimitry Andric 38340eae32dcSDimitry Andric // %1:_(DstTy), %2, %3, %4 = G_UNMERGE_VALUES %0:_(SrcTy) 38350eae32dcSDimitry Andric // 38360eae32dcSDimitry Andric // %5:_(NarrowTy), %6 = G_UNMERGE_VALUES %0:_(SrcTy) - reg sequence 38370eae32dcSDimitry Andric // %1:_(DstTy), %2 = G_UNMERGE_VALUES %5:_(NarrowTy) - sequence of bits in reg 38380eae32dcSDimitry Andric // %3:_(DstTy), %4 = G_UNMERGE_VALUES %6:_(NarrowTy) 38390eae32dcSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, SrcReg); 38408bcb0991SDimitry Andric const int NumUnmerge = Unmerge->getNumOperands() - 1; 38418bcb0991SDimitry Andric const int PartsPerUnmerge = NumDst / NumUnmerge; 38428bcb0991SDimitry Andric 38438bcb0991SDimitry Andric for (int I = 0; I != NumUnmerge; ++I) { 38448bcb0991SDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 38458bcb0991SDimitry Andric 38468bcb0991SDimitry Andric for (int J = 0; J != PartsPerUnmerge; ++J) 38478bcb0991SDimitry Andric MIB.addDef(MI.getOperand(I * PartsPerUnmerge + J).getReg()); 38488bcb0991SDimitry Andric MIB.addUse(Unmerge.getReg(I)); 38498bcb0991SDimitry Andric } 38508bcb0991SDimitry Andric 38518bcb0991SDimitry Andric MI.eraseFromParent(); 38528bcb0991SDimitry Andric return Legalized; 38538bcb0991SDimitry Andric } 38548bcb0991SDimitry Andric 3855fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 3856e8d8bef9SDimitry Andric LegalizerHelper::fewerElementsVectorMerge(MachineInstr &MI, unsigned TypeIdx, 3857e8d8bef9SDimitry Andric LLT NarrowTy) { 3858e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 3859e8d8bef9SDimitry Andric LLT DstTy = MRI.getType(DstReg); 3860e8d8bef9SDimitry Andric LLT SrcTy = MRI.getType(MI.getOperand(1).getReg()); 38610eae32dcSDimitry Andric // Requires compatible types. Otherwise user of DstReg did not perform unmerge 38620eae32dcSDimitry Andric // that should have been artifact combined. Most likely instruction that uses 38630eae32dcSDimitry Andric // DstReg has to do more/fewer elements legalization compatible with NarrowTy. 38640eae32dcSDimitry Andric assert(DstTy.isVector() && NarrowTy.isVector() && "Expected vector types"); 38650eae32dcSDimitry Andric assert((DstTy.getScalarType() == NarrowTy.getScalarType()) && "bad type"); 38660eae32dcSDimitry Andric if (NarrowTy == SrcTy) 38670eae32dcSDimitry Andric return UnableToLegalize; 38688bcb0991SDimitry Andric 38690eae32dcSDimitry Andric // This attempts to lower part of LCMTy merge/unmerge sequence. Intended use 38700eae32dcSDimitry Andric // is for old mir tests. Since the changes to more/fewer elements it should no 38710eae32dcSDimitry Andric // longer be possible to generate MIR like this when starting from llvm-ir 38720eae32dcSDimitry Andric // because LCMTy approach was replaced with merge/unmerge to vector elements. 38730eae32dcSDimitry Andric if (TypeIdx == 1) { 38740eae32dcSDimitry Andric assert(SrcTy.isVector() && "Expected vector types"); 38750eae32dcSDimitry Andric assert((SrcTy.getScalarType() == NarrowTy.getScalarType()) && "bad type"); 38760eae32dcSDimitry Andric if ((DstTy.getSizeInBits() % NarrowTy.getSizeInBits() != 0) || 38770eae32dcSDimitry Andric (NarrowTy.getNumElements() >= SrcTy.getNumElements())) 38780eae32dcSDimitry Andric return UnableToLegalize; 38790eae32dcSDimitry Andric // %2:_(DstTy) = G_CONCAT_VECTORS %0:_(SrcTy), %1:_(SrcTy) 38800eae32dcSDimitry Andric // 38810eae32dcSDimitry Andric // %3:_(EltTy), %4, %5 = G_UNMERGE_VALUES %0:_(SrcTy) 38820eae32dcSDimitry Andric // %6:_(EltTy), %7, %8 = G_UNMERGE_VALUES %1:_(SrcTy) 38830eae32dcSDimitry Andric // %9:_(NarrowTy) = G_BUILD_VECTOR %3:_(EltTy), %4 38840eae32dcSDimitry Andric // %10:_(NarrowTy) = G_BUILD_VECTOR %5:_(EltTy), %6 38850eae32dcSDimitry Andric // %11:_(NarrowTy) = G_BUILD_VECTOR %7:_(EltTy), %8 38860eae32dcSDimitry Andric // %2:_(DstTy) = G_CONCAT_VECTORS %9:_(NarrowTy), %10, %11 3887e8d8bef9SDimitry Andric 38880eae32dcSDimitry Andric SmallVector<Register, 8> Elts; 38890eae32dcSDimitry Andric LLT EltTy = MRI.getType(MI.getOperand(1).getReg()).getScalarType(); 38900eae32dcSDimitry Andric for (unsigned i = 1; i < MI.getNumOperands(); ++i) { 38910eae32dcSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(EltTy, MI.getOperand(i).getReg()); 38920eae32dcSDimitry Andric for (unsigned j = 0; j < Unmerge->getNumDefs(); ++j) 38930eae32dcSDimitry Andric Elts.push_back(Unmerge.getReg(j)); 38940eae32dcSDimitry Andric } 3895e8d8bef9SDimitry Andric 38960eae32dcSDimitry Andric SmallVector<Register, 8> NarrowTyElts; 38970eae32dcSDimitry Andric unsigned NumNarrowTyElts = NarrowTy.getNumElements(); 38980eae32dcSDimitry Andric unsigned NumNarrowTyPieces = DstTy.getNumElements() / NumNarrowTyElts; 38990eae32dcSDimitry Andric for (unsigned i = 0, Offset = 0; i < NumNarrowTyPieces; 39000eae32dcSDimitry Andric ++i, Offset += NumNarrowTyElts) { 39010eae32dcSDimitry Andric ArrayRef<Register> Pieces(&Elts[Offset], NumNarrowTyElts); 39020eae32dcSDimitry Andric NarrowTyElts.push_back(MIRBuilder.buildMerge(NarrowTy, Pieces).getReg(0)); 39030eae32dcSDimitry Andric } 3904e8d8bef9SDimitry Andric 39050eae32dcSDimitry Andric MIRBuilder.buildMerge(DstReg, NarrowTyElts); 39060eae32dcSDimitry Andric MI.eraseFromParent(); 39070eae32dcSDimitry Andric return Legalized; 39080eae32dcSDimitry Andric } 39090eae32dcSDimitry Andric 39100eae32dcSDimitry Andric assert(TypeIdx == 0 && "Bad type index"); 39110eae32dcSDimitry Andric if ((NarrowTy.getSizeInBits() % SrcTy.getSizeInBits() != 0) || 39120eae32dcSDimitry Andric (DstTy.getSizeInBits() % NarrowTy.getSizeInBits() != 0)) 39130eae32dcSDimitry Andric return UnableToLegalize; 39140eae32dcSDimitry Andric 39150eae32dcSDimitry Andric // This is most likely SrcTy (smaller then register size) packed in DstTy 39160eae32dcSDimitry Andric // (larger then register size) and since merge was not combined it will be 39170eae32dcSDimitry Andric // lowered to bit sequence packing into register. Merge SrcTy to NarrowTy 39180eae32dcSDimitry Andric // (register size) pieces first. Then merge each of NarrowTy pieces to DstTy. 39190eae32dcSDimitry Andric 39200eae32dcSDimitry Andric // %0:_(DstTy) = G_MERGE_VALUES %1:_(SrcTy), %2, %3, %4 39210eae32dcSDimitry Andric // 39220eae32dcSDimitry Andric // %5:_(NarrowTy) = G_MERGE_VALUES %1:_(SrcTy), %2 - sequence of bits in reg 39230eae32dcSDimitry Andric // %6:_(NarrowTy) = G_MERGE_VALUES %3:_(SrcTy), %4 39240eae32dcSDimitry Andric // %0:_(DstTy) = G_MERGE_VALUES %5:_(NarrowTy), %6 - reg sequence 39250eae32dcSDimitry Andric SmallVector<Register, 8> NarrowTyElts; 39260eae32dcSDimitry Andric unsigned NumParts = DstTy.getNumElements() / NarrowTy.getNumElements(); 39270eae32dcSDimitry Andric unsigned NumSrcElts = SrcTy.isVector() ? SrcTy.getNumElements() : 1; 39280eae32dcSDimitry Andric unsigned NumElts = NarrowTy.getNumElements() / NumSrcElts; 39290eae32dcSDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 39300eae32dcSDimitry Andric SmallVector<Register, 8> Sources; 39310eae32dcSDimitry Andric for (unsigned j = 0; j < NumElts; ++j) 39320eae32dcSDimitry Andric Sources.push_back(MI.getOperand(1 + i * NumElts + j).getReg()); 39330eae32dcSDimitry Andric NarrowTyElts.push_back(MIRBuilder.buildMerge(NarrowTy, Sources).getReg(0)); 39340eae32dcSDimitry Andric } 39350eae32dcSDimitry Andric 39360eae32dcSDimitry Andric MIRBuilder.buildMerge(DstReg, NarrowTyElts); 3937e8d8bef9SDimitry Andric MI.eraseFromParent(); 3938e8d8bef9SDimitry Andric return Legalized; 39398bcb0991SDimitry Andric } 39408bcb0991SDimitry Andric 3941e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 3942e8d8bef9SDimitry Andric LegalizerHelper::fewerElementsVectorExtractInsertVectorElt(MachineInstr &MI, 3943e8d8bef9SDimitry Andric unsigned TypeIdx, 3944e8d8bef9SDimitry Andric LLT NarrowVecTy) { 3945e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 3946e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 3947e8d8bef9SDimitry Andric Register InsertVal; 3948e8d8bef9SDimitry Andric bool IsInsert = MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT; 3949e8d8bef9SDimitry Andric 3950e8d8bef9SDimitry Andric assert((IsInsert ? TypeIdx == 0 : TypeIdx == 1) && "not a vector type index"); 3951e8d8bef9SDimitry Andric if (IsInsert) 3952e8d8bef9SDimitry Andric InsertVal = MI.getOperand(2).getReg(); 3953e8d8bef9SDimitry Andric 3954e8d8bef9SDimitry Andric Register Idx = MI.getOperand(MI.getNumOperands() - 1).getReg(); 3955e8d8bef9SDimitry Andric 3956e8d8bef9SDimitry Andric // TODO: Handle total scalarization case. 3957e8d8bef9SDimitry Andric if (!NarrowVecTy.isVector()) 3958e8d8bef9SDimitry Andric return UnableToLegalize; 3959e8d8bef9SDimitry Andric 3960e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(SrcVec); 3961e8d8bef9SDimitry Andric 3962e8d8bef9SDimitry Andric // If the index is a constant, we can really break this down as you would 3963e8d8bef9SDimitry Andric // expect, and index into the target size pieces. 3964e8d8bef9SDimitry Andric int64_t IdxVal; 3965349cc55cSDimitry Andric auto MaybeCst = getIConstantVRegValWithLookThrough(Idx, MRI); 3966fe6060f1SDimitry Andric if (MaybeCst) { 3967fe6060f1SDimitry Andric IdxVal = MaybeCst->Value.getSExtValue(); 3968e8d8bef9SDimitry Andric // Avoid out of bounds indexing the pieces. 3969e8d8bef9SDimitry Andric if (IdxVal >= VecTy.getNumElements()) { 3970e8d8bef9SDimitry Andric MIRBuilder.buildUndef(DstReg); 3971e8d8bef9SDimitry Andric MI.eraseFromParent(); 3972e8d8bef9SDimitry Andric return Legalized; 39738bcb0991SDimitry Andric } 39748bcb0991SDimitry Andric 3975e8d8bef9SDimitry Andric SmallVector<Register, 8> VecParts; 3976e8d8bef9SDimitry Andric LLT GCDTy = extractGCDType(VecParts, VecTy, NarrowVecTy, SrcVec); 3977e8d8bef9SDimitry Andric 3978e8d8bef9SDimitry Andric // Build a sequence of NarrowTy pieces in VecParts for this operand. 3979e8d8bef9SDimitry Andric LLT LCMTy = buildLCMMergePieces(VecTy, NarrowVecTy, GCDTy, VecParts, 3980e8d8bef9SDimitry Andric TargetOpcode::G_ANYEXT); 3981e8d8bef9SDimitry Andric 3982e8d8bef9SDimitry Andric unsigned NewNumElts = NarrowVecTy.getNumElements(); 3983e8d8bef9SDimitry Andric 3984e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 3985e8d8bef9SDimitry Andric int64_t PartIdx = IdxVal / NewNumElts; 3986e8d8bef9SDimitry Andric auto NewIdx = 3987e8d8bef9SDimitry Andric MIRBuilder.buildConstant(IdxTy, IdxVal - NewNumElts * PartIdx); 3988e8d8bef9SDimitry Andric 3989e8d8bef9SDimitry Andric if (IsInsert) { 3990e8d8bef9SDimitry Andric LLT PartTy = MRI.getType(VecParts[PartIdx]); 3991e8d8bef9SDimitry Andric 3992e8d8bef9SDimitry Andric // Use the adjusted index to insert into one of the subvectors. 3993e8d8bef9SDimitry Andric auto InsertPart = MIRBuilder.buildInsertVectorElement( 3994e8d8bef9SDimitry Andric PartTy, VecParts[PartIdx], InsertVal, NewIdx); 3995e8d8bef9SDimitry Andric VecParts[PartIdx] = InsertPart.getReg(0); 3996e8d8bef9SDimitry Andric 3997e8d8bef9SDimitry Andric // Recombine the inserted subvector with the others to reform the result 3998e8d8bef9SDimitry Andric // vector. 3999e8d8bef9SDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, VecParts); 4000e8d8bef9SDimitry Andric } else { 4001e8d8bef9SDimitry Andric MIRBuilder.buildExtractVectorElement(DstReg, VecParts[PartIdx], NewIdx); 40028bcb0991SDimitry Andric } 40038bcb0991SDimitry Andric 40048bcb0991SDimitry Andric MI.eraseFromParent(); 40058bcb0991SDimitry Andric return Legalized; 40068bcb0991SDimitry Andric } 40078bcb0991SDimitry Andric 4008e8d8bef9SDimitry Andric // With a variable index, we can't perform the operation in a smaller type, so 4009e8d8bef9SDimitry Andric // we're forced to expand this. 4010e8d8bef9SDimitry Andric // 4011e8d8bef9SDimitry Andric // TODO: We could emit a chain of compare/select to figure out which piece to 4012e8d8bef9SDimitry Andric // index. 4013e8d8bef9SDimitry Andric return lowerExtractInsertVectorElt(MI); 4014e8d8bef9SDimitry Andric } 4015e8d8bef9SDimitry Andric 40168bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4017fe6060f1SDimitry Andric LegalizerHelper::reduceLoadStoreWidth(GLoadStore &LdStMI, unsigned TypeIdx, 40180b57cec5SDimitry Andric LLT NarrowTy) { 40190b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 40200b57cec5SDimitry Andric if (TypeIdx != 0) 40210b57cec5SDimitry Andric return UnableToLegalize; 40220b57cec5SDimitry Andric 40230b57cec5SDimitry Andric // This implementation doesn't work for atomics. Give up instead of doing 40240b57cec5SDimitry Andric // something invalid. 4025fe6060f1SDimitry Andric if (LdStMI.isAtomic()) 40260b57cec5SDimitry Andric return UnableToLegalize; 40270b57cec5SDimitry Andric 4028fe6060f1SDimitry Andric bool IsLoad = isa<GLoad>(LdStMI); 4029fe6060f1SDimitry Andric Register ValReg = LdStMI.getReg(0); 4030fe6060f1SDimitry Andric Register AddrReg = LdStMI.getPointerReg(); 40310b57cec5SDimitry Andric LLT ValTy = MRI.getType(ValReg); 40320b57cec5SDimitry Andric 40335ffd83dbSDimitry Andric // FIXME: Do we need a distinct NarrowMemory legalize action? 4034fe6060f1SDimitry Andric if (ValTy.getSizeInBits() != 8 * LdStMI.getMemSize()) { 40355ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow extload/truncstore\n"); 40365ffd83dbSDimitry Andric return UnableToLegalize; 40375ffd83dbSDimitry Andric } 40385ffd83dbSDimitry Andric 40390b57cec5SDimitry Andric int NumParts = -1; 40400b57cec5SDimitry Andric int NumLeftover = -1; 40410b57cec5SDimitry Andric LLT LeftoverTy; 40420b57cec5SDimitry Andric SmallVector<Register, 8> NarrowRegs, NarrowLeftoverRegs; 40430b57cec5SDimitry Andric if (IsLoad) { 40440b57cec5SDimitry Andric std::tie(NumParts, NumLeftover) = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy); 40450b57cec5SDimitry Andric } else { 40460b57cec5SDimitry Andric if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs, 40470b57cec5SDimitry Andric NarrowLeftoverRegs)) { 40480b57cec5SDimitry Andric NumParts = NarrowRegs.size(); 40490b57cec5SDimitry Andric NumLeftover = NarrowLeftoverRegs.size(); 40500b57cec5SDimitry Andric } 40510b57cec5SDimitry Andric } 40520b57cec5SDimitry Andric 40530b57cec5SDimitry Andric if (NumParts == -1) 40540b57cec5SDimitry Andric return UnableToLegalize; 40550b57cec5SDimitry Andric 4056e8d8bef9SDimitry Andric LLT PtrTy = MRI.getType(AddrReg); 4057e8d8bef9SDimitry Andric const LLT OffsetTy = LLT::scalar(PtrTy.getSizeInBits()); 40580b57cec5SDimitry Andric 40590b57cec5SDimitry Andric unsigned TotalSize = ValTy.getSizeInBits(); 40600b57cec5SDimitry Andric 40610b57cec5SDimitry Andric // Split the load/store into PartTy sized pieces starting at Offset. If this 40620b57cec5SDimitry Andric // is a load, return the new registers in ValRegs. For a store, each elements 40630b57cec5SDimitry Andric // of ValRegs should be PartTy. Returns the next offset that needs to be 40640b57cec5SDimitry Andric // handled. 406581ad6265SDimitry Andric bool isBigEndian = MIRBuilder.getDataLayout().isBigEndian(); 4066fe6060f1SDimitry Andric auto MMO = LdStMI.getMMO(); 40670b57cec5SDimitry Andric auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<Register> &ValRegs, 406881ad6265SDimitry Andric unsigned NumParts, unsigned Offset) -> unsigned { 40690b57cec5SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 40700b57cec5SDimitry Andric unsigned PartSize = PartTy.getSizeInBits(); 40710b57cec5SDimitry Andric for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize; 407281ad6265SDimitry Andric ++Idx) { 40730b57cec5SDimitry Andric unsigned ByteOffset = Offset / 8; 40740b57cec5SDimitry Andric Register NewAddrReg; 40750b57cec5SDimitry Andric 4076480093f4SDimitry Andric MIRBuilder.materializePtrAdd(NewAddrReg, AddrReg, OffsetTy, ByteOffset); 40770b57cec5SDimitry Andric 40780b57cec5SDimitry Andric MachineMemOperand *NewMMO = 4079fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, ByteOffset, PartTy); 40800b57cec5SDimitry Andric 40810b57cec5SDimitry Andric if (IsLoad) { 40820b57cec5SDimitry Andric Register Dst = MRI.createGenericVirtualRegister(PartTy); 40830b57cec5SDimitry Andric ValRegs.push_back(Dst); 40840b57cec5SDimitry Andric MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO); 40850b57cec5SDimitry Andric } else { 40860b57cec5SDimitry Andric MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO); 40870b57cec5SDimitry Andric } 408881ad6265SDimitry Andric Offset = isBigEndian ? Offset - PartSize : Offset + PartSize; 40890b57cec5SDimitry Andric } 40900b57cec5SDimitry Andric 40910b57cec5SDimitry Andric return Offset; 40920b57cec5SDimitry Andric }; 40930b57cec5SDimitry Andric 409481ad6265SDimitry Andric unsigned Offset = isBigEndian ? TotalSize - NarrowTy.getSizeInBits() : 0; 409581ad6265SDimitry Andric unsigned HandledOffset = 409681ad6265SDimitry Andric splitTypePieces(NarrowTy, NarrowRegs, NumParts, Offset); 40970b57cec5SDimitry Andric 40980b57cec5SDimitry Andric // Handle the rest of the register if this isn't an even type breakdown. 40990b57cec5SDimitry Andric if (LeftoverTy.isValid()) 410081ad6265SDimitry Andric splitTypePieces(LeftoverTy, NarrowLeftoverRegs, NumLeftover, HandledOffset); 41010b57cec5SDimitry Andric 41020b57cec5SDimitry Andric if (IsLoad) { 41030b57cec5SDimitry Andric insertParts(ValReg, ValTy, NarrowTy, NarrowRegs, 41040b57cec5SDimitry Andric LeftoverTy, NarrowLeftoverRegs); 41050b57cec5SDimitry Andric } 41060b57cec5SDimitry Andric 4107fe6060f1SDimitry Andric LdStMI.eraseFromParent(); 41080b57cec5SDimitry Andric return Legalized; 41090b57cec5SDimitry Andric } 41100b57cec5SDimitry Andric 41110b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 41120b57cec5SDimitry Andric LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx, 41130b57cec5SDimitry Andric LLT NarrowTy) { 41140b57cec5SDimitry Andric using namespace TargetOpcode; 41150eae32dcSDimitry Andric GenericMachineInstr &GMI = cast<GenericMachineInstr>(MI); 41160eae32dcSDimitry Andric unsigned NumElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; 41170b57cec5SDimitry Andric 41180b57cec5SDimitry Andric switch (MI.getOpcode()) { 41190b57cec5SDimitry Andric case G_IMPLICIT_DEF: 41205ffd83dbSDimitry Andric case G_TRUNC: 41210b57cec5SDimitry Andric case G_AND: 41220b57cec5SDimitry Andric case G_OR: 41230b57cec5SDimitry Andric case G_XOR: 41240b57cec5SDimitry Andric case G_ADD: 41250b57cec5SDimitry Andric case G_SUB: 41260b57cec5SDimitry Andric case G_MUL: 4127e8d8bef9SDimitry Andric case G_PTR_ADD: 41280b57cec5SDimitry Andric case G_SMULH: 41290b57cec5SDimitry Andric case G_UMULH: 41300b57cec5SDimitry Andric case G_FADD: 41310b57cec5SDimitry Andric case G_FMUL: 41320b57cec5SDimitry Andric case G_FSUB: 41330b57cec5SDimitry Andric case G_FNEG: 41340b57cec5SDimitry Andric case G_FABS: 41350b57cec5SDimitry Andric case G_FCANONICALIZE: 41360b57cec5SDimitry Andric case G_FDIV: 41370b57cec5SDimitry Andric case G_FREM: 41380b57cec5SDimitry Andric case G_FMA: 41398bcb0991SDimitry Andric case G_FMAD: 41400b57cec5SDimitry Andric case G_FPOW: 41410b57cec5SDimitry Andric case G_FEXP: 41420b57cec5SDimitry Andric case G_FEXP2: 41430b57cec5SDimitry Andric case G_FLOG: 41440b57cec5SDimitry Andric case G_FLOG2: 41450b57cec5SDimitry Andric case G_FLOG10: 41460b57cec5SDimitry Andric case G_FNEARBYINT: 41470b57cec5SDimitry Andric case G_FCEIL: 41480b57cec5SDimitry Andric case G_FFLOOR: 41490b57cec5SDimitry Andric case G_FRINT: 41500b57cec5SDimitry Andric case G_INTRINSIC_ROUND: 4151e8d8bef9SDimitry Andric case G_INTRINSIC_ROUNDEVEN: 41520b57cec5SDimitry Andric case G_INTRINSIC_TRUNC: 41530b57cec5SDimitry Andric case G_FCOS: 41540b57cec5SDimitry Andric case G_FSIN: 41550b57cec5SDimitry Andric case G_FSQRT: 41560b57cec5SDimitry Andric case G_BSWAP: 41578bcb0991SDimitry Andric case G_BITREVERSE: 41580b57cec5SDimitry Andric case G_SDIV: 4159480093f4SDimitry Andric case G_UDIV: 4160480093f4SDimitry Andric case G_SREM: 4161480093f4SDimitry Andric case G_UREM: 4162fe6060f1SDimitry Andric case G_SDIVREM: 4163fe6060f1SDimitry Andric case G_UDIVREM: 41640b57cec5SDimitry Andric case G_SMIN: 41650b57cec5SDimitry Andric case G_SMAX: 41660b57cec5SDimitry Andric case G_UMIN: 41670b57cec5SDimitry Andric case G_UMAX: 4168fe6060f1SDimitry Andric case G_ABS: 41690b57cec5SDimitry Andric case G_FMINNUM: 41700b57cec5SDimitry Andric case G_FMAXNUM: 41710b57cec5SDimitry Andric case G_FMINNUM_IEEE: 41720b57cec5SDimitry Andric case G_FMAXNUM_IEEE: 41730b57cec5SDimitry Andric case G_FMINIMUM: 41740b57cec5SDimitry Andric case G_FMAXIMUM: 41755ffd83dbSDimitry Andric case G_FSHL: 41765ffd83dbSDimitry Andric case G_FSHR: 4177349cc55cSDimitry Andric case G_ROTL: 4178349cc55cSDimitry Andric case G_ROTR: 41795ffd83dbSDimitry Andric case G_FREEZE: 41805ffd83dbSDimitry Andric case G_SADDSAT: 41815ffd83dbSDimitry Andric case G_SSUBSAT: 41825ffd83dbSDimitry Andric case G_UADDSAT: 41835ffd83dbSDimitry Andric case G_USUBSAT: 4184fe6060f1SDimitry Andric case G_UMULO: 4185fe6060f1SDimitry Andric case G_SMULO: 41860b57cec5SDimitry Andric case G_SHL: 41870b57cec5SDimitry Andric case G_LSHR: 41880b57cec5SDimitry Andric case G_ASHR: 4189e8d8bef9SDimitry Andric case G_SSHLSAT: 4190e8d8bef9SDimitry Andric case G_USHLSAT: 41910b57cec5SDimitry Andric case G_CTLZ: 41920b57cec5SDimitry Andric case G_CTLZ_ZERO_UNDEF: 41930b57cec5SDimitry Andric case G_CTTZ: 41940b57cec5SDimitry Andric case G_CTTZ_ZERO_UNDEF: 41950b57cec5SDimitry Andric case G_CTPOP: 41960b57cec5SDimitry Andric case G_FCOPYSIGN: 41970b57cec5SDimitry Andric case G_ZEXT: 41980b57cec5SDimitry Andric case G_SEXT: 41990b57cec5SDimitry Andric case G_ANYEXT: 42000b57cec5SDimitry Andric case G_FPEXT: 42010b57cec5SDimitry Andric case G_FPTRUNC: 42020b57cec5SDimitry Andric case G_SITOFP: 42030b57cec5SDimitry Andric case G_UITOFP: 42040b57cec5SDimitry Andric case G_FPTOSI: 42050b57cec5SDimitry Andric case G_FPTOUI: 42060b57cec5SDimitry Andric case G_INTTOPTR: 42070b57cec5SDimitry Andric case G_PTRTOINT: 42080b57cec5SDimitry Andric case G_ADDRSPACE_CAST: 420981ad6265SDimitry Andric case G_UADDO: 421081ad6265SDimitry Andric case G_USUBO: 421181ad6265SDimitry Andric case G_UADDE: 421281ad6265SDimitry Andric case G_USUBE: 421381ad6265SDimitry Andric case G_SADDO: 421481ad6265SDimitry Andric case G_SSUBO: 421581ad6265SDimitry Andric case G_SADDE: 421681ad6265SDimitry Andric case G_SSUBE: 42170eae32dcSDimitry Andric return fewerElementsVectorMultiEltType(GMI, NumElts); 42180b57cec5SDimitry Andric case G_ICMP: 42190b57cec5SDimitry Andric case G_FCMP: 42200eae32dcSDimitry Andric return fewerElementsVectorMultiEltType(GMI, NumElts, {1 /*cpm predicate*/}); 42210b57cec5SDimitry Andric case G_SELECT: 42220eae32dcSDimitry Andric if (MRI.getType(MI.getOperand(1).getReg()).isVector()) 42230eae32dcSDimitry Andric return fewerElementsVectorMultiEltType(GMI, NumElts); 42240eae32dcSDimitry Andric return fewerElementsVectorMultiEltType(GMI, NumElts, {1 /*scalar cond*/}); 42250b57cec5SDimitry Andric case G_PHI: 42260eae32dcSDimitry Andric return fewerElementsVectorPhi(GMI, NumElts); 42278bcb0991SDimitry Andric case G_UNMERGE_VALUES: 42288bcb0991SDimitry Andric return fewerElementsVectorUnmergeValues(MI, TypeIdx, NarrowTy); 42298bcb0991SDimitry Andric case G_BUILD_VECTOR: 4230e8d8bef9SDimitry Andric assert(TypeIdx == 0 && "not a vector type index"); 4231e8d8bef9SDimitry Andric return fewerElementsVectorMerge(MI, TypeIdx, NarrowTy); 4232e8d8bef9SDimitry Andric case G_CONCAT_VECTORS: 4233e8d8bef9SDimitry Andric if (TypeIdx != 1) // TODO: This probably does work as expected already. 4234e8d8bef9SDimitry Andric return UnableToLegalize; 4235e8d8bef9SDimitry Andric return fewerElementsVectorMerge(MI, TypeIdx, NarrowTy); 4236e8d8bef9SDimitry Andric case G_EXTRACT_VECTOR_ELT: 4237e8d8bef9SDimitry Andric case G_INSERT_VECTOR_ELT: 4238e8d8bef9SDimitry Andric return fewerElementsVectorExtractInsertVectorElt(MI, TypeIdx, NarrowTy); 42390b57cec5SDimitry Andric case G_LOAD: 42400b57cec5SDimitry Andric case G_STORE: 4241fe6060f1SDimitry Andric return reduceLoadStoreWidth(cast<GLoadStore>(MI), TypeIdx, NarrowTy); 42425ffd83dbSDimitry Andric case G_SEXT_INREG: 42430eae32dcSDimitry Andric return fewerElementsVectorMultiEltType(GMI, NumElts, {2 /*imm*/}); 4244fe6060f1SDimitry Andric GISEL_VECREDUCE_CASES_NONSEQ 4245fe6060f1SDimitry Andric return fewerElementsVectorReductions(MI, TypeIdx, NarrowTy); 4246fe6060f1SDimitry Andric case G_SHUFFLE_VECTOR: 4247fe6060f1SDimitry Andric return fewerElementsVectorShuffle(MI, TypeIdx, NarrowTy); 42480b57cec5SDimitry Andric default: 42490b57cec5SDimitry Andric return UnableToLegalize; 42500b57cec5SDimitry Andric } 42510b57cec5SDimitry Andric } 42520b57cec5SDimitry Andric 4253fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorShuffle( 4254fe6060f1SDimitry Andric MachineInstr &MI, unsigned int TypeIdx, LLT NarrowTy) { 4255fe6060f1SDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR); 4256fe6060f1SDimitry Andric if (TypeIdx != 0) 4257fe6060f1SDimitry Andric return UnableToLegalize; 4258fe6060f1SDimitry Andric 4259fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4260fe6060f1SDimitry Andric Register Src1Reg = MI.getOperand(1).getReg(); 4261fe6060f1SDimitry Andric Register Src2Reg = MI.getOperand(2).getReg(); 4262fe6060f1SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 4263fe6060f1SDimitry Andric LLT DstTy = MRI.getType(DstReg); 4264fe6060f1SDimitry Andric LLT Src1Ty = MRI.getType(Src1Reg); 4265fe6060f1SDimitry Andric LLT Src2Ty = MRI.getType(Src2Reg); 4266fe6060f1SDimitry Andric // The shuffle should be canonicalized by now. 4267fe6060f1SDimitry Andric if (DstTy != Src1Ty) 4268fe6060f1SDimitry Andric return UnableToLegalize; 4269fe6060f1SDimitry Andric if (DstTy != Src2Ty) 4270fe6060f1SDimitry Andric return UnableToLegalize; 4271fe6060f1SDimitry Andric 4272fe6060f1SDimitry Andric if (!isPowerOf2_32(DstTy.getNumElements())) 4273fe6060f1SDimitry Andric return UnableToLegalize; 4274fe6060f1SDimitry Andric 4275fe6060f1SDimitry Andric // We only support splitting a shuffle into 2, so adjust NarrowTy accordingly. 4276fe6060f1SDimitry Andric // Further legalization attempts will be needed to do split further. 4277fe6060f1SDimitry Andric NarrowTy = 4278fe6060f1SDimitry Andric DstTy.changeElementCount(DstTy.getElementCount().divideCoefficientBy(2)); 4279fe6060f1SDimitry Andric unsigned NewElts = NarrowTy.getNumElements(); 4280fe6060f1SDimitry Andric 4281fe6060f1SDimitry Andric SmallVector<Register> SplitSrc1Regs, SplitSrc2Regs; 4282fe6060f1SDimitry Andric extractParts(Src1Reg, NarrowTy, 2, SplitSrc1Regs); 4283fe6060f1SDimitry Andric extractParts(Src2Reg, NarrowTy, 2, SplitSrc2Regs); 4284fe6060f1SDimitry Andric Register Inputs[4] = {SplitSrc1Regs[0], SplitSrc1Regs[1], SplitSrc2Regs[0], 4285fe6060f1SDimitry Andric SplitSrc2Regs[1]}; 4286fe6060f1SDimitry Andric 4287fe6060f1SDimitry Andric Register Hi, Lo; 4288fe6060f1SDimitry Andric 4289fe6060f1SDimitry Andric // If Lo or Hi uses elements from at most two of the four input vectors, then 4290fe6060f1SDimitry Andric // express it as a vector shuffle of those two inputs. Otherwise extract the 4291fe6060f1SDimitry Andric // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR. 4292fe6060f1SDimitry Andric SmallVector<int, 16> Ops; 4293fe6060f1SDimitry Andric for (unsigned High = 0; High < 2; ++High) { 4294fe6060f1SDimitry Andric Register &Output = High ? Hi : Lo; 4295fe6060f1SDimitry Andric 4296fe6060f1SDimitry Andric // Build a shuffle mask for the output, discovering on the fly which 4297fe6060f1SDimitry Andric // input vectors to use as shuffle operands (recorded in InputUsed). 4298fe6060f1SDimitry Andric // If building a suitable shuffle vector proves too hard, then bail 4299fe6060f1SDimitry Andric // out with useBuildVector set. 4300fe6060f1SDimitry Andric unsigned InputUsed[2] = {-1U, -1U}; // Not yet discovered. 4301fe6060f1SDimitry Andric unsigned FirstMaskIdx = High * NewElts; 4302fe6060f1SDimitry Andric bool UseBuildVector = false; 4303fe6060f1SDimitry Andric for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) { 4304fe6060f1SDimitry Andric // The mask element. This indexes into the input. 4305fe6060f1SDimitry Andric int Idx = Mask[FirstMaskIdx + MaskOffset]; 4306fe6060f1SDimitry Andric 4307fe6060f1SDimitry Andric // The input vector this mask element indexes into. 4308fe6060f1SDimitry Andric unsigned Input = (unsigned)Idx / NewElts; 4309fe6060f1SDimitry Andric 4310fe6060f1SDimitry Andric if (Input >= array_lengthof(Inputs)) { 4311fe6060f1SDimitry Andric // The mask element does not index into any input vector. 4312fe6060f1SDimitry Andric Ops.push_back(-1); 4313fe6060f1SDimitry Andric continue; 4314fe6060f1SDimitry Andric } 4315fe6060f1SDimitry Andric 4316fe6060f1SDimitry Andric // Turn the index into an offset from the start of the input vector. 4317fe6060f1SDimitry Andric Idx -= Input * NewElts; 4318fe6060f1SDimitry Andric 4319fe6060f1SDimitry Andric // Find or create a shuffle vector operand to hold this input. 4320fe6060f1SDimitry Andric unsigned OpNo; 4321fe6060f1SDimitry Andric for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) { 4322fe6060f1SDimitry Andric if (InputUsed[OpNo] == Input) { 4323fe6060f1SDimitry Andric // This input vector is already an operand. 4324fe6060f1SDimitry Andric break; 4325fe6060f1SDimitry Andric } else if (InputUsed[OpNo] == -1U) { 4326fe6060f1SDimitry Andric // Create a new operand for this input vector. 4327fe6060f1SDimitry Andric InputUsed[OpNo] = Input; 4328fe6060f1SDimitry Andric break; 4329fe6060f1SDimitry Andric } 4330fe6060f1SDimitry Andric } 4331fe6060f1SDimitry Andric 4332fe6060f1SDimitry Andric if (OpNo >= array_lengthof(InputUsed)) { 4333fe6060f1SDimitry Andric // More than two input vectors used! Give up on trying to create a 4334fe6060f1SDimitry Andric // shuffle vector. Insert all elements into a BUILD_VECTOR instead. 4335fe6060f1SDimitry Andric UseBuildVector = true; 4336fe6060f1SDimitry Andric break; 4337fe6060f1SDimitry Andric } 4338fe6060f1SDimitry Andric 4339fe6060f1SDimitry Andric // Add the mask index for the new shuffle vector. 4340fe6060f1SDimitry Andric Ops.push_back(Idx + OpNo * NewElts); 4341fe6060f1SDimitry Andric } 4342fe6060f1SDimitry Andric 4343fe6060f1SDimitry Andric if (UseBuildVector) { 4344fe6060f1SDimitry Andric LLT EltTy = NarrowTy.getElementType(); 4345fe6060f1SDimitry Andric SmallVector<Register, 16> SVOps; 4346fe6060f1SDimitry Andric 4347fe6060f1SDimitry Andric // Extract the input elements by hand. 4348fe6060f1SDimitry Andric for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) { 4349fe6060f1SDimitry Andric // The mask element. This indexes into the input. 4350fe6060f1SDimitry Andric int Idx = Mask[FirstMaskIdx + MaskOffset]; 4351fe6060f1SDimitry Andric 4352fe6060f1SDimitry Andric // The input vector this mask element indexes into. 4353fe6060f1SDimitry Andric unsigned Input = (unsigned)Idx / NewElts; 4354fe6060f1SDimitry Andric 4355fe6060f1SDimitry Andric if (Input >= array_lengthof(Inputs)) { 4356fe6060f1SDimitry Andric // The mask element is "undef" or indexes off the end of the input. 4357fe6060f1SDimitry Andric SVOps.push_back(MIRBuilder.buildUndef(EltTy).getReg(0)); 4358fe6060f1SDimitry Andric continue; 4359fe6060f1SDimitry Andric } 4360fe6060f1SDimitry Andric 4361fe6060f1SDimitry Andric // Turn the index into an offset from the start of the input vector. 4362fe6060f1SDimitry Andric Idx -= Input * NewElts; 4363fe6060f1SDimitry Andric 4364fe6060f1SDimitry Andric // Extract the vector element by hand. 4365fe6060f1SDimitry Andric SVOps.push_back(MIRBuilder 4366fe6060f1SDimitry Andric .buildExtractVectorElement( 4367fe6060f1SDimitry Andric EltTy, Inputs[Input], 4368fe6060f1SDimitry Andric MIRBuilder.buildConstant(LLT::scalar(32), Idx)) 4369fe6060f1SDimitry Andric .getReg(0)); 4370fe6060f1SDimitry Andric } 4371fe6060f1SDimitry Andric 4372fe6060f1SDimitry Andric // Construct the Lo/Hi output using a G_BUILD_VECTOR. 4373fe6060f1SDimitry Andric Output = MIRBuilder.buildBuildVector(NarrowTy, SVOps).getReg(0); 4374fe6060f1SDimitry Andric } else if (InputUsed[0] == -1U) { 4375fe6060f1SDimitry Andric // No input vectors were used! The result is undefined. 4376fe6060f1SDimitry Andric Output = MIRBuilder.buildUndef(NarrowTy).getReg(0); 4377fe6060f1SDimitry Andric } else { 4378fe6060f1SDimitry Andric Register Op0 = Inputs[InputUsed[0]]; 4379fe6060f1SDimitry Andric // If only one input was used, use an undefined vector for the other. 4380fe6060f1SDimitry Andric Register Op1 = InputUsed[1] == -1U 4381fe6060f1SDimitry Andric ? MIRBuilder.buildUndef(NarrowTy).getReg(0) 4382fe6060f1SDimitry Andric : Inputs[InputUsed[1]]; 4383fe6060f1SDimitry Andric // At least one input vector was used. Create a new shuffle vector. 4384fe6060f1SDimitry Andric Output = MIRBuilder.buildShuffleVector(NarrowTy, Op0, Op1, Ops).getReg(0); 4385fe6060f1SDimitry Andric } 4386fe6060f1SDimitry Andric 4387fe6060f1SDimitry Andric Ops.clear(); 4388fe6060f1SDimitry Andric } 4389fe6060f1SDimitry Andric 4390fe6060f1SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, {Lo, Hi}); 4391fe6060f1SDimitry Andric MI.eraseFromParent(); 4392fe6060f1SDimitry Andric return Legalized; 4393fe6060f1SDimitry Andric } 4394fe6060f1SDimitry Andric 4395349cc55cSDimitry Andric static unsigned getScalarOpcForReduction(unsigned Opc) { 4396fe6060f1SDimitry Andric unsigned ScalarOpc; 4397fe6060f1SDimitry Andric switch (Opc) { 4398fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FADD: 4399fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FADD; 4400fe6060f1SDimitry Andric break; 4401fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMUL: 4402fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMUL; 4403fe6060f1SDimitry Andric break; 4404fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMAX: 4405fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMAXNUM; 4406fe6060f1SDimitry Andric break; 4407fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMIN: 4408fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMINNUM; 4409fe6060f1SDimitry Andric break; 4410fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_ADD: 4411fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_ADD; 4412fe6060f1SDimitry Andric break; 4413fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_MUL: 4414fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_MUL; 4415fe6060f1SDimitry Andric break; 4416fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_AND: 4417fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_AND; 4418fe6060f1SDimitry Andric break; 4419fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_OR: 4420fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_OR; 4421fe6060f1SDimitry Andric break; 4422fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_XOR: 4423fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_XOR; 4424fe6060f1SDimitry Andric break; 4425fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_SMAX: 4426fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_SMAX; 4427fe6060f1SDimitry Andric break; 4428fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_SMIN: 4429fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_SMIN; 4430fe6060f1SDimitry Andric break; 4431fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_UMAX: 4432fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_UMAX; 4433fe6060f1SDimitry Andric break; 4434fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_UMIN: 4435fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_UMIN; 4436fe6060f1SDimitry Andric break; 4437fe6060f1SDimitry Andric default: 4438349cc55cSDimitry Andric llvm_unreachable("Unhandled reduction"); 4439fe6060f1SDimitry Andric } 4440349cc55cSDimitry Andric return ScalarOpc; 4441349cc55cSDimitry Andric } 4442349cc55cSDimitry Andric 4443349cc55cSDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorReductions( 4444349cc55cSDimitry Andric MachineInstr &MI, unsigned int TypeIdx, LLT NarrowTy) { 4445349cc55cSDimitry Andric unsigned Opc = MI.getOpcode(); 4446349cc55cSDimitry Andric assert(Opc != TargetOpcode::G_VECREDUCE_SEQ_FADD && 4447349cc55cSDimitry Andric Opc != TargetOpcode::G_VECREDUCE_SEQ_FMUL && 4448349cc55cSDimitry Andric "Sequential reductions not expected"); 4449349cc55cSDimitry Andric 4450349cc55cSDimitry Andric if (TypeIdx != 1) 4451349cc55cSDimitry Andric return UnableToLegalize; 4452349cc55cSDimitry Andric 4453349cc55cSDimitry Andric // The semantics of the normal non-sequential reductions allow us to freely 4454349cc55cSDimitry Andric // re-associate the operation. 4455349cc55cSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 4456349cc55cSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 4457349cc55cSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4458349cc55cSDimitry Andric LLT DstTy = MRI.getType(DstReg); 4459349cc55cSDimitry Andric 4460349cc55cSDimitry Andric if (NarrowTy.isVector() && 4461349cc55cSDimitry Andric (SrcTy.getNumElements() % NarrowTy.getNumElements() != 0)) 4462349cc55cSDimitry Andric return UnableToLegalize; 4463349cc55cSDimitry Andric 4464349cc55cSDimitry Andric unsigned ScalarOpc = getScalarOpcForReduction(Opc); 4465349cc55cSDimitry Andric SmallVector<Register> SplitSrcs; 4466349cc55cSDimitry Andric // If NarrowTy is a scalar then we're being asked to scalarize. 4467349cc55cSDimitry Andric const unsigned NumParts = 4468349cc55cSDimitry Andric NarrowTy.isVector() ? SrcTy.getNumElements() / NarrowTy.getNumElements() 4469349cc55cSDimitry Andric : SrcTy.getNumElements(); 4470349cc55cSDimitry Andric 4471349cc55cSDimitry Andric extractParts(SrcReg, NarrowTy, NumParts, SplitSrcs); 4472349cc55cSDimitry Andric if (NarrowTy.isScalar()) { 4473349cc55cSDimitry Andric if (DstTy != NarrowTy) 4474349cc55cSDimitry Andric return UnableToLegalize; // FIXME: handle implicit extensions. 4475349cc55cSDimitry Andric 4476349cc55cSDimitry Andric if (isPowerOf2_32(NumParts)) { 4477349cc55cSDimitry Andric // Generate a tree of scalar operations to reduce the critical path. 4478349cc55cSDimitry Andric SmallVector<Register> PartialResults; 4479349cc55cSDimitry Andric unsigned NumPartsLeft = NumParts; 4480349cc55cSDimitry Andric while (NumPartsLeft > 1) { 4481349cc55cSDimitry Andric for (unsigned Idx = 0; Idx < NumPartsLeft - 1; Idx += 2) { 4482349cc55cSDimitry Andric PartialResults.emplace_back( 4483349cc55cSDimitry Andric MIRBuilder 4484349cc55cSDimitry Andric .buildInstr(ScalarOpc, {NarrowTy}, 4485349cc55cSDimitry Andric {SplitSrcs[Idx], SplitSrcs[Idx + 1]}) 4486349cc55cSDimitry Andric .getReg(0)); 4487349cc55cSDimitry Andric } 4488349cc55cSDimitry Andric SplitSrcs = PartialResults; 4489349cc55cSDimitry Andric PartialResults.clear(); 4490349cc55cSDimitry Andric NumPartsLeft = SplitSrcs.size(); 4491349cc55cSDimitry Andric } 4492349cc55cSDimitry Andric assert(SplitSrcs.size() == 1); 4493349cc55cSDimitry Andric MIRBuilder.buildCopy(DstReg, SplitSrcs[0]); 4494349cc55cSDimitry Andric MI.eraseFromParent(); 4495349cc55cSDimitry Andric return Legalized; 4496349cc55cSDimitry Andric } 4497349cc55cSDimitry Andric // If we can't generate a tree, then just do sequential operations. 4498349cc55cSDimitry Andric Register Acc = SplitSrcs[0]; 4499349cc55cSDimitry Andric for (unsigned Idx = 1; Idx < NumParts; ++Idx) 4500349cc55cSDimitry Andric Acc = MIRBuilder.buildInstr(ScalarOpc, {NarrowTy}, {Acc, SplitSrcs[Idx]}) 4501349cc55cSDimitry Andric .getReg(0); 4502349cc55cSDimitry Andric MIRBuilder.buildCopy(DstReg, Acc); 4503349cc55cSDimitry Andric MI.eraseFromParent(); 4504349cc55cSDimitry Andric return Legalized; 4505349cc55cSDimitry Andric } 4506349cc55cSDimitry Andric SmallVector<Register> PartialReductions; 4507349cc55cSDimitry Andric for (unsigned Part = 0; Part < NumParts; ++Part) { 4508349cc55cSDimitry Andric PartialReductions.push_back( 4509349cc55cSDimitry Andric MIRBuilder.buildInstr(Opc, {DstTy}, {SplitSrcs[Part]}).getReg(0)); 4510349cc55cSDimitry Andric } 4511349cc55cSDimitry Andric 4512fe6060f1SDimitry Andric 4513fe6060f1SDimitry Andric // If the types involved are powers of 2, we can generate intermediate vector 4514fe6060f1SDimitry Andric // ops, before generating a final reduction operation. 4515fe6060f1SDimitry Andric if (isPowerOf2_32(SrcTy.getNumElements()) && 4516fe6060f1SDimitry Andric isPowerOf2_32(NarrowTy.getNumElements())) { 4517fe6060f1SDimitry Andric return tryNarrowPow2Reduction(MI, SrcReg, SrcTy, NarrowTy, ScalarOpc); 4518fe6060f1SDimitry Andric } 4519fe6060f1SDimitry Andric 4520fe6060f1SDimitry Andric Register Acc = PartialReductions[0]; 4521fe6060f1SDimitry Andric for (unsigned Part = 1; Part < NumParts; ++Part) { 4522fe6060f1SDimitry Andric if (Part == NumParts - 1) { 4523fe6060f1SDimitry Andric MIRBuilder.buildInstr(ScalarOpc, {DstReg}, 4524fe6060f1SDimitry Andric {Acc, PartialReductions[Part]}); 4525fe6060f1SDimitry Andric } else { 4526fe6060f1SDimitry Andric Acc = MIRBuilder 4527fe6060f1SDimitry Andric .buildInstr(ScalarOpc, {DstTy}, {Acc, PartialReductions[Part]}) 4528fe6060f1SDimitry Andric .getReg(0); 4529fe6060f1SDimitry Andric } 4530fe6060f1SDimitry Andric } 4531fe6060f1SDimitry Andric MI.eraseFromParent(); 4532fe6060f1SDimitry Andric return Legalized; 4533fe6060f1SDimitry Andric } 4534fe6060f1SDimitry Andric 4535fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 4536fe6060f1SDimitry Andric LegalizerHelper::tryNarrowPow2Reduction(MachineInstr &MI, Register SrcReg, 4537fe6060f1SDimitry Andric LLT SrcTy, LLT NarrowTy, 4538fe6060f1SDimitry Andric unsigned ScalarOpc) { 4539fe6060f1SDimitry Andric SmallVector<Register> SplitSrcs; 4540fe6060f1SDimitry Andric // Split the sources into NarrowTy size pieces. 4541fe6060f1SDimitry Andric extractParts(SrcReg, NarrowTy, 4542fe6060f1SDimitry Andric SrcTy.getNumElements() / NarrowTy.getNumElements(), SplitSrcs); 4543fe6060f1SDimitry Andric // We're going to do a tree reduction using vector operations until we have 4544fe6060f1SDimitry Andric // one NarrowTy size value left. 4545fe6060f1SDimitry Andric while (SplitSrcs.size() > 1) { 4546fe6060f1SDimitry Andric SmallVector<Register> PartialRdxs; 4547fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < SplitSrcs.size()-1; Idx += 2) { 4548fe6060f1SDimitry Andric Register LHS = SplitSrcs[Idx]; 4549fe6060f1SDimitry Andric Register RHS = SplitSrcs[Idx + 1]; 4550fe6060f1SDimitry Andric // Create the intermediate vector op. 4551fe6060f1SDimitry Andric Register Res = 4552fe6060f1SDimitry Andric MIRBuilder.buildInstr(ScalarOpc, {NarrowTy}, {LHS, RHS}).getReg(0); 4553fe6060f1SDimitry Andric PartialRdxs.push_back(Res); 4554fe6060f1SDimitry Andric } 4555fe6060f1SDimitry Andric SplitSrcs = std::move(PartialRdxs); 4556fe6060f1SDimitry Andric } 4557fe6060f1SDimitry Andric // Finally generate the requested NarrowTy based reduction. 4558fe6060f1SDimitry Andric Observer.changingInstr(MI); 4559fe6060f1SDimitry Andric MI.getOperand(1).setReg(SplitSrcs[0]); 4560fe6060f1SDimitry Andric Observer.changedInstr(MI); 4561fe6060f1SDimitry Andric return Legalized; 4562fe6060f1SDimitry Andric } 4563fe6060f1SDimitry Andric 45640b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 45650b57cec5SDimitry Andric LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt, 45660b57cec5SDimitry Andric const LLT HalfTy, const LLT AmtTy) { 45670b57cec5SDimitry Andric 45680b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 45690b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 45705ffd83dbSDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1)); 45710b57cec5SDimitry Andric 4572349cc55cSDimitry Andric if (Amt.isZero()) { 45735ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), {InL, InH}); 45740b57cec5SDimitry Andric MI.eraseFromParent(); 45750b57cec5SDimitry Andric return Legalized; 45760b57cec5SDimitry Andric } 45770b57cec5SDimitry Andric 45780b57cec5SDimitry Andric LLT NVT = HalfTy; 45790b57cec5SDimitry Andric unsigned NVTBits = HalfTy.getSizeInBits(); 45800b57cec5SDimitry Andric unsigned VTBits = 2 * NVTBits; 45810b57cec5SDimitry Andric 45820b57cec5SDimitry Andric SrcOp Lo(Register(0)), Hi(Register(0)); 45830b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_SHL) { 45840b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 45850b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 45860b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 45870b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 45880b57cec5SDimitry Andric Hi = MIRBuilder.buildShl(NVT, InL, 45890b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 45900b57cec5SDimitry Andric } else if (Amt == NVTBits) { 45910b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 45920b57cec5SDimitry Andric Hi = InL; 45930b57cec5SDimitry Andric } else { 45940b57cec5SDimitry Andric Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt)); 45950b57cec5SDimitry Andric auto OrLHS = 45960b57cec5SDimitry Andric MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt)); 45970b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildLShr( 45980b57cec5SDimitry Andric NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 45990b57cec5SDimitry Andric Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 46000b57cec5SDimitry Andric } 46010b57cec5SDimitry Andric } else if (MI.getOpcode() == TargetOpcode::G_LSHR) { 46020b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 46030b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 46040b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 46050b57cec5SDimitry Andric Lo = MIRBuilder.buildLShr(NVT, InH, 46060b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 46070b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 46080b57cec5SDimitry Andric } else if (Amt == NVTBits) { 46090b57cec5SDimitry Andric Lo = InH; 46100b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 46110b57cec5SDimitry Andric } else { 46120b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 46130b57cec5SDimitry Andric 46140b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 46150b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 46160b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 46170b57cec5SDimitry Andric 46180b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 46190b57cec5SDimitry Andric Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst); 46200b57cec5SDimitry Andric } 46210b57cec5SDimitry Andric } else { 46220b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 46230b57cec5SDimitry Andric Hi = Lo = MIRBuilder.buildAShr( 46240b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 46250b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 46260b57cec5SDimitry Andric Lo = MIRBuilder.buildAShr(NVT, InH, 46270b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 46280b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 46290b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 46300b57cec5SDimitry Andric } else if (Amt == NVTBits) { 46310b57cec5SDimitry Andric Lo = InH; 46320b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 46330b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 46340b57cec5SDimitry Andric } else { 46350b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 46360b57cec5SDimitry Andric 46370b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 46380b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 46390b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 46400b57cec5SDimitry Andric 46410b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 46420b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst); 46430b57cec5SDimitry Andric } 46440b57cec5SDimitry Andric } 46450b57cec5SDimitry Andric 46465ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), {Lo, Hi}); 46470b57cec5SDimitry Andric MI.eraseFromParent(); 46480b57cec5SDimitry Andric 46490b57cec5SDimitry Andric return Legalized; 46500b57cec5SDimitry Andric } 46510b57cec5SDimitry Andric 46520b57cec5SDimitry Andric // TODO: Optimize if constant shift amount. 46530b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 46540b57cec5SDimitry Andric LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx, 46550b57cec5SDimitry Andric LLT RequestedTy) { 46560b57cec5SDimitry Andric if (TypeIdx == 1) { 46570b57cec5SDimitry Andric Observer.changingInstr(MI); 46580b57cec5SDimitry Andric narrowScalarSrc(MI, RequestedTy, 2); 46590b57cec5SDimitry Andric Observer.changedInstr(MI); 46600b57cec5SDimitry Andric return Legalized; 46610b57cec5SDimitry Andric } 46620b57cec5SDimitry Andric 46630b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 46640b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 46650b57cec5SDimitry Andric if (DstTy.isVector()) 46660b57cec5SDimitry Andric return UnableToLegalize; 46670b57cec5SDimitry Andric 46680b57cec5SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 46690b57cec5SDimitry Andric LLT ShiftAmtTy = MRI.getType(Amt); 46700b57cec5SDimitry Andric const unsigned DstEltSize = DstTy.getScalarSizeInBits(); 46710b57cec5SDimitry Andric if (DstEltSize % 2 != 0) 46720b57cec5SDimitry Andric return UnableToLegalize; 46730b57cec5SDimitry Andric 46740b57cec5SDimitry Andric // Ignore the input type. We can only go to exactly half the size of the 46750b57cec5SDimitry Andric // input. If that isn't small enough, the resulting pieces will be further 46760b57cec5SDimitry Andric // legalized. 46770b57cec5SDimitry Andric const unsigned NewBitSize = DstEltSize / 2; 46780b57cec5SDimitry Andric const LLT HalfTy = LLT::scalar(NewBitSize); 46790b57cec5SDimitry Andric const LLT CondTy = LLT::scalar(1); 46800b57cec5SDimitry Andric 4681349cc55cSDimitry Andric if (auto VRegAndVal = getIConstantVRegValWithLookThrough(Amt, MRI)) { 4682349cc55cSDimitry Andric return narrowScalarShiftByConstant(MI, VRegAndVal->Value, HalfTy, 4683349cc55cSDimitry Andric ShiftAmtTy); 46840b57cec5SDimitry Andric } 46850b57cec5SDimitry Andric 46860b57cec5SDimitry Andric // TODO: Expand with known bits. 46870b57cec5SDimitry Andric 46880b57cec5SDimitry Andric // Handle the fully general expansion by an unknown amount. 46890b57cec5SDimitry Andric auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize); 46900b57cec5SDimitry Andric 46910b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 46920b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 46935ffd83dbSDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1)); 46940b57cec5SDimitry Andric 46950b57cec5SDimitry Andric auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits); 46960b57cec5SDimitry Andric auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt); 46970b57cec5SDimitry Andric 46980b57cec5SDimitry Andric auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0); 46990b57cec5SDimitry Andric auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits); 47000b57cec5SDimitry Andric auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero); 47010b57cec5SDimitry Andric 47020b57cec5SDimitry Andric Register ResultRegs[2]; 47030b57cec5SDimitry Andric switch (MI.getOpcode()) { 47040b57cec5SDimitry Andric case TargetOpcode::G_SHL: { 47050b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 47068bcb0991SDimitry Andric auto LoS = MIRBuilder.buildShl(HalfTy, InL, Amt); 47070b57cec5SDimitry Andric 47088bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, AmtLack); 47098bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, Amt); 47108bcb0991SDimitry Andric auto HiS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 47110b57cec5SDimitry Andric 47120b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 47130b57cec5SDimitry Andric auto LoL = MIRBuilder.buildConstant(HalfTy, 0); // Lo part is zero. 47140b57cec5SDimitry Andric auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part. 47150b57cec5SDimitry Andric 47160b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL); 47170b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect( 47180b57cec5SDimitry Andric HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL)); 47190b57cec5SDimitry Andric 47200b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 47210b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 47220b57cec5SDimitry Andric break; 47230b57cec5SDimitry Andric } 47248bcb0991SDimitry Andric case TargetOpcode::G_LSHR: 47250b57cec5SDimitry Andric case TargetOpcode::G_ASHR: { 47260b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 47278bcb0991SDimitry Andric auto HiS = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, {InH, Amt}); 47280b57cec5SDimitry Andric 47298bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, Amt); 47308bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, AmtLack); 47318bcb0991SDimitry Andric auto LoS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 47320b57cec5SDimitry Andric 47330b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 47348bcb0991SDimitry Andric MachineInstrBuilder HiL; 47358bcb0991SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_LSHR) { 47368bcb0991SDimitry Andric HiL = MIRBuilder.buildConstant(HalfTy, 0); // Hi part is zero. 47378bcb0991SDimitry Andric } else { 47388bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1); 47398bcb0991SDimitry Andric HiL = MIRBuilder.buildAShr(HalfTy, InH, ShiftAmt); // Sign of Hi part. 47408bcb0991SDimitry Andric } 47418bcb0991SDimitry Andric auto LoL = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, 47428bcb0991SDimitry Andric {InH, AmtExcess}); // Lo from Hi part. 47430b57cec5SDimitry Andric 47440b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect( 47450b57cec5SDimitry Andric HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL)); 47460b57cec5SDimitry Andric 47470b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL); 47480b57cec5SDimitry Andric 47490b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 47500b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 47510b57cec5SDimitry Andric break; 47520b57cec5SDimitry Andric } 47530b57cec5SDimitry Andric default: 47540b57cec5SDimitry Andric llvm_unreachable("not a shift"); 47550b57cec5SDimitry Andric } 47560b57cec5SDimitry Andric 47570b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, ResultRegs); 47580b57cec5SDimitry Andric MI.eraseFromParent(); 47590b57cec5SDimitry Andric return Legalized; 47600b57cec5SDimitry Andric } 47610b57cec5SDimitry Andric 47620b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 47630b57cec5SDimitry Andric LegalizerHelper::moreElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx, 47640b57cec5SDimitry Andric LLT MoreTy) { 47650b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 47660b57cec5SDimitry Andric 47670b57cec5SDimitry Andric Observer.changingInstr(MI); 47680b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 47690b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 47700b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 47710b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, I); 47720b57cec5SDimitry Andric } 47730b57cec5SDimitry Andric 47740b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 47750b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 47760b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 47770b57cec5SDimitry Andric Observer.changedInstr(MI); 47780b57cec5SDimitry Andric return Legalized; 47790b57cec5SDimitry Andric } 47800b57cec5SDimitry Andric 47810b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 47820b57cec5SDimitry Andric LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx, 47830b57cec5SDimitry Andric LLT MoreTy) { 47840b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 47850b57cec5SDimitry Andric switch (Opc) { 47868bcb0991SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: 47878bcb0991SDimitry Andric case TargetOpcode::G_LOAD: { 47888bcb0991SDimitry Andric if (TypeIdx != 0) 47898bcb0991SDimitry Andric return UnableToLegalize; 47900b57cec5SDimitry Andric Observer.changingInstr(MI); 47910b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 47920b57cec5SDimitry Andric Observer.changedInstr(MI); 47930b57cec5SDimitry Andric return Legalized; 47940b57cec5SDimitry Andric } 47958bcb0991SDimitry Andric case TargetOpcode::G_STORE: 47968bcb0991SDimitry Andric if (TypeIdx != 0) 47978bcb0991SDimitry Andric return UnableToLegalize; 47988bcb0991SDimitry Andric Observer.changingInstr(MI); 47998bcb0991SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 0); 48008bcb0991SDimitry Andric Observer.changedInstr(MI); 48018bcb0991SDimitry Andric return Legalized; 48020b57cec5SDimitry Andric case TargetOpcode::G_AND: 48030b57cec5SDimitry Andric case TargetOpcode::G_OR: 48040b57cec5SDimitry Andric case TargetOpcode::G_XOR: 48050eae32dcSDimitry Andric case TargetOpcode::G_ADD: 48060eae32dcSDimitry Andric case TargetOpcode::G_SUB: 48070eae32dcSDimitry Andric case TargetOpcode::G_MUL: 48080eae32dcSDimitry Andric case TargetOpcode::G_FADD: 48090eae32dcSDimitry Andric case TargetOpcode::G_FMUL: 48100eae32dcSDimitry Andric case TargetOpcode::G_UADDSAT: 48110eae32dcSDimitry Andric case TargetOpcode::G_USUBSAT: 48120eae32dcSDimitry Andric case TargetOpcode::G_SADDSAT: 48130eae32dcSDimitry Andric case TargetOpcode::G_SSUBSAT: 48140b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 48150b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 48160b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 4817480093f4SDimitry Andric case TargetOpcode::G_UMAX: 4818480093f4SDimitry Andric case TargetOpcode::G_FMINNUM: 4819480093f4SDimitry Andric case TargetOpcode::G_FMAXNUM: 4820480093f4SDimitry Andric case TargetOpcode::G_FMINNUM_IEEE: 4821480093f4SDimitry Andric case TargetOpcode::G_FMAXNUM_IEEE: 4822480093f4SDimitry Andric case TargetOpcode::G_FMINIMUM: 4823480093f4SDimitry Andric case TargetOpcode::G_FMAXIMUM: { 48240b57cec5SDimitry Andric Observer.changingInstr(MI); 48250b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 48260b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 48270b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 48280b57cec5SDimitry Andric Observer.changedInstr(MI); 48290b57cec5SDimitry Andric return Legalized; 48300b57cec5SDimitry Andric } 48310eae32dcSDimitry Andric case TargetOpcode::G_FMA: 48320eae32dcSDimitry Andric case TargetOpcode::G_FSHR: 48330eae32dcSDimitry Andric case TargetOpcode::G_FSHL: { 48340eae32dcSDimitry Andric Observer.changingInstr(MI); 48350eae32dcSDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 48360eae32dcSDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 48370eae32dcSDimitry Andric moreElementsVectorSrc(MI, MoreTy, 3); 48380eae32dcSDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 48390eae32dcSDimitry Andric Observer.changedInstr(MI); 48400eae32dcSDimitry Andric return Legalized; 48410eae32dcSDimitry Andric } 48420b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 48430b57cec5SDimitry Andric if (TypeIdx != 1) 48440b57cec5SDimitry Andric return UnableToLegalize; 48450b57cec5SDimitry Andric Observer.changingInstr(MI); 48460b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 48470b57cec5SDimitry Andric Observer.changedInstr(MI); 48480b57cec5SDimitry Andric return Legalized; 48490b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 48505ffd83dbSDimitry Andric case TargetOpcode::G_FREEZE: 48510eae32dcSDimitry Andric case TargetOpcode::G_FNEG: 48520eae32dcSDimitry Andric case TargetOpcode::G_FABS: 48530eae32dcSDimitry Andric case TargetOpcode::G_BSWAP: 48540eae32dcSDimitry Andric case TargetOpcode::G_FCANONICALIZE: 48550eae32dcSDimitry Andric case TargetOpcode::G_SEXT_INREG: 48560b57cec5SDimitry Andric if (TypeIdx != 0) 48570b57cec5SDimitry Andric return UnableToLegalize; 48580b57cec5SDimitry Andric Observer.changingInstr(MI); 48590b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 48600b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 48610b57cec5SDimitry Andric Observer.changedInstr(MI); 48620b57cec5SDimitry Andric return Legalized; 486381ad6265SDimitry Andric case TargetOpcode::G_SELECT: { 486481ad6265SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 486581ad6265SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 486681ad6265SDimitry Andric LLT DstTy = MRI.getType(DstReg); 486781ad6265SDimitry Andric LLT CondTy = MRI.getType(CondReg); 486881ad6265SDimitry Andric if (TypeIdx == 1) { 486981ad6265SDimitry Andric if (!CondTy.isScalar() || 487081ad6265SDimitry Andric DstTy.getElementCount() != MoreTy.getElementCount()) 48710b57cec5SDimitry Andric return UnableToLegalize; 487281ad6265SDimitry Andric 487381ad6265SDimitry Andric // This is turning a scalar select of vectors into a vector 487481ad6265SDimitry Andric // select. Broadcast the select condition. 487581ad6265SDimitry Andric auto ShufSplat = MIRBuilder.buildShuffleSplat(MoreTy, CondReg); 487681ad6265SDimitry Andric Observer.changingInstr(MI); 487781ad6265SDimitry Andric MI.getOperand(1).setReg(ShufSplat.getReg(0)); 487881ad6265SDimitry Andric Observer.changedInstr(MI); 487981ad6265SDimitry Andric return Legalized; 488081ad6265SDimitry Andric } 488181ad6265SDimitry Andric 488281ad6265SDimitry Andric if (CondTy.isVector()) 48830b57cec5SDimitry Andric return UnableToLegalize; 48840b57cec5SDimitry Andric 48850b57cec5SDimitry Andric Observer.changingInstr(MI); 48860b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 48870b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 3); 48880b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 48890b57cec5SDimitry Andric Observer.changedInstr(MI); 48900b57cec5SDimitry Andric return Legalized; 489181ad6265SDimitry Andric } 48920eae32dcSDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: 48938bcb0991SDimitry Andric return UnableToLegalize; 48940b57cec5SDimitry Andric case TargetOpcode::G_PHI: 48950b57cec5SDimitry Andric return moreElementsVectorPhi(MI, TypeIdx, MoreTy); 4896fe6060f1SDimitry Andric case TargetOpcode::G_SHUFFLE_VECTOR: 4897fe6060f1SDimitry Andric return moreElementsVectorShuffle(MI, TypeIdx, MoreTy); 48980eae32dcSDimitry Andric case TargetOpcode::G_BUILD_VECTOR: { 48990eae32dcSDimitry Andric SmallVector<SrcOp, 8> Elts; 49000eae32dcSDimitry Andric for (auto Op : MI.uses()) { 49010eae32dcSDimitry Andric Elts.push_back(Op.getReg()); 49020eae32dcSDimitry Andric } 49030eae32dcSDimitry Andric 49040eae32dcSDimitry Andric for (unsigned i = Elts.size(); i < MoreTy.getNumElements(); ++i) { 49050eae32dcSDimitry Andric Elts.push_back(MIRBuilder.buildUndef(MoreTy.getScalarType())); 49060eae32dcSDimitry Andric } 49070eae32dcSDimitry Andric 49080eae32dcSDimitry Andric MIRBuilder.buildDeleteTrailingVectorElements( 49090eae32dcSDimitry Andric MI.getOperand(0).getReg(), MIRBuilder.buildInstr(Opc, {MoreTy}, Elts)); 49100eae32dcSDimitry Andric MI.eraseFromParent(); 49110eae32dcSDimitry Andric return Legalized; 49120eae32dcSDimitry Andric } 49130eae32dcSDimitry Andric case TargetOpcode::G_TRUNC: { 49140eae32dcSDimitry Andric Observer.changingInstr(MI); 49150eae32dcSDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 49160eae32dcSDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 49170eae32dcSDimitry Andric Observer.changedInstr(MI); 49180eae32dcSDimitry Andric return Legalized; 49190eae32dcSDimitry Andric } 49200b57cec5SDimitry Andric default: 49210b57cec5SDimitry Andric return UnableToLegalize; 49220b57cec5SDimitry Andric } 49230b57cec5SDimitry Andric } 49240b57cec5SDimitry Andric 4925fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 4926fe6060f1SDimitry Andric LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI, 4927fe6060f1SDimitry Andric unsigned int TypeIdx, LLT MoreTy) { 4928fe6060f1SDimitry Andric if (TypeIdx != 0) 4929fe6060f1SDimitry Andric return UnableToLegalize; 4930fe6060f1SDimitry Andric 4931fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4932fe6060f1SDimitry Andric Register Src1Reg = MI.getOperand(1).getReg(); 4933fe6060f1SDimitry Andric Register Src2Reg = MI.getOperand(2).getReg(); 4934fe6060f1SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 4935fe6060f1SDimitry Andric LLT DstTy = MRI.getType(DstReg); 4936fe6060f1SDimitry Andric LLT Src1Ty = MRI.getType(Src1Reg); 4937fe6060f1SDimitry Andric LLT Src2Ty = MRI.getType(Src2Reg); 4938fe6060f1SDimitry Andric unsigned NumElts = DstTy.getNumElements(); 4939fe6060f1SDimitry Andric unsigned WidenNumElts = MoreTy.getNumElements(); 4940fe6060f1SDimitry Andric 4941fe6060f1SDimitry Andric // Expect a canonicalized shuffle. 4942fe6060f1SDimitry Andric if (DstTy != Src1Ty || DstTy != Src2Ty) 4943fe6060f1SDimitry Andric return UnableToLegalize; 4944fe6060f1SDimitry Andric 4945fe6060f1SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 4946fe6060f1SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 4947fe6060f1SDimitry Andric 4948fe6060f1SDimitry Andric // Adjust mask based on new input vector length. 4949fe6060f1SDimitry Andric SmallVector<int, 16> NewMask; 4950fe6060f1SDimitry Andric for (unsigned I = 0; I != NumElts; ++I) { 4951fe6060f1SDimitry Andric int Idx = Mask[I]; 4952fe6060f1SDimitry Andric if (Idx < static_cast<int>(NumElts)) 4953fe6060f1SDimitry Andric NewMask.push_back(Idx); 4954fe6060f1SDimitry Andric else 4955fe6060f1SDimitry Andric NewMask.push_back(Idx - NumElts + WidenNumElts); 4956fe6060f1SDimitry Andric } 4957fe6060f1SDimitry Andric for (unsigned I = NumElts; I != WidenNumElts; ++I) 4958fe6060f1SDimitry Andric NewMask.push_back(-1); 4959fe6060f1SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 4960fe6060f1SDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 4961fe6060f1SDimitry Andric MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(), 4962fe6060f1SDimitry Andric MI.getOperand(1).getReg(), 4963fe6060f1SDimitry Andric MI.getOperand(2).getReg(), NewMask); 4964fe6060f1SDimitry Andric MI.eraseFromParent(); 4965fe6060f1SDimitry Andric return Legalized; 4966fe6060f1SDimitry Andric } 4967fe6060f1SDimitry Andric 49680b57cec5SDimitry Andric void LegalizerHelper::multiplyRegisters(SmallVectorImpl<Register> &DstRegs, 49690b57cec5SDimitry Andric ArrayRef<Register> Src1Regs, 49700b57cec5SDimitry Andric ArrayRef<Register> Src2Regs, 49710b57cec5SDimitry Andric LLT NarrowTy) { 49720b57cec5SDimitry Andric MachineIRBuilder &B = MIRBuilder; 49730b57cec5SDimitry Andric unsigned SrcParts = Src1Regs.size(); 49740b57cec5SDimitry Andric unsigned DstParts = DstRegs.size(); 49750b57cec5SDimitry Andric 49760b57cec5SDimitry Andric unsigned DstIdx = 0; // Low bits of the result. 49770b57cec5SDimitry Andric Register FactorSum = 49780b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx], Src2Regs[DstIdx]).getReg(0); 49790b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 49800b57cec5SDimitry Andric 49810b57cec5SDimitry Andric unsigned CarrySumPrevDstIdx; 49820b57cec5SDimitry Andric SmallVector<Register, 4> Factors; 49830b57cec5SDimitry Andric 49840b57cec5SDimitry Andric for (DstIdx = 1; DstIdx < DstParts; DstIdx++) { 49850b57cec5SDimitry Andric // Collect low parts of muls for DstIdx. 49860b57cec5SDimitry Andric for (unsigned i = DstIdx + 1 < SrcParts ? 0 : DstIdx - SrcParts + 1; 49870b57cec5SDimitry Andric i <= std::min(DstIdx, SrcParts - 1); ++i) { 49880b57cec5SDimitry Andric MachineInstrBuilder Mul = 49890b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx - i], Src2Regs[i]); 49900b57cec5SDimitry Andric Factors.push_back(Mul.getReg(0)); 49910b57cec5SDimitry Andric } 49920b57cec5SDimitry Andric // Collect high parts of muls from previous DstIdx. 49930b57cec5SDimitry Andric for (unsigned i = DstIdx < SrcParts ? 0 : DstIdx - SrcParts; 49940b57cec5SDimitry Andric i <= std::min(DstIdx - 1, SrcParts - 1); ++i) { 49950b57cec5SDimitry Andric MachineInstrBuilder Umulh = 49960b57cec5SDimitry Andric B.buildUMulH(NarrowTy, Src1Regs[DstIdx - 1 - i], Src2Regs[i]); 49970b57cec5SDimitry Andric Factors.push_back(Umulh.getReg(0)); 49980b57cec5SDimitry Andric } 4999480093f4SDimitry Andric // Add CarrySum from additions calculated for previous DstIdx. 50000b57cec5SDimitry Andric if (DstIdx != 1) { 50010b57cec5SDimitry Andric Factors.push_back(CarrySumPrevDstIdx); 50020b57cec5SDimitry Andric } 50030b57cec5SDimitry Andric 50040b57cec5SDimitry Andric Register CarrySum; 50050b57cec5SDimitry Andric // Add all factors and accumulate all carries into CarrySum. 50060b57cec5SDimitry Andric if (DstIdx != DstParts - 1) { 50070b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 50080b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), Factors[0], Factors[1]); 50090b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 50100b57cec5SDimitry Andric CarrySum = B.buildZExt(NarrowTy, Uaddo.getReg(1)).getReg(0); 50110b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) { 50120b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 50130b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), FactorSum, Factors[i]); 50140b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 50150b57cec5SDimitry Andric MachineInstrBuilder Carry = B.buildZExt(NarrowTy, Uaddo.getReg(1)); 50160b57cec5SDimitry Andric CarrySum = B.buildAdd(NarrowTy, CarrySum, Carry).getReg(0); 50170b57cec5SDimitry Andric } 50180b57cec5SDimitry Andric } else { 50190b57cec5SDimitry Andric // Since value for the next index is not calculated, neither is CarrySum. 50200b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, Factors[0], Factors[1]).getReg(0); 50210b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) 50220b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, FactorSum, Factors[i]).getReg(0); 50230b57cec5SDimitry Andric } 50240b57cec5SDimitry Andric 50250b57cec5SDimitry Andric CarrySumPrevDstIdx = CarrySum; 50260b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 50270b57cec5SDimitry Andric Factors.clear(); 50280b57cec5SDimitry Andric } 50290b57cec5SDimitry Andric } 50300b57cec5SDimitry Andric 50310b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 5032fe6060f1SDimitry Andric LegalizerHelper::narrowScalarAddSub(MachineInstr &MI, unsigned TypeIdx, 5033fe6060f1SDimitry Andric LLT NarrowTy) { 5034fe6060f1SDimitry Andric if (TypeIdx != 0) 5035fe6060f1SDimitry Andric return UnableToLegalize; 5036fe6060f1SDimitry Andric 5037fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 5038fe6060f1SDimitry Andric LLT DstType = MRI.getType(DstReg); 5039fe6060f1SDimitry Andric // FIXME: add support for vector types 5040fe6060f1SDimitry Andric if (DstType.isVector()) 5041fe6060f1SDimitry Andric return UnableToLegalize; 5042fe6060f1SDimitry Andric 5043fe6060f1SDimitry Andric unsigned Opcode = MI.getOpcode(); 5044fe6060f1SDimitry Andric unsigned OpO, OpE, OpF; 5045fe6060f1SDimitry Andric switch (Opcode) { 5046fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 5047fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 5048fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 5049fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 5050fe6060f1SDimitry Andric case TargetOpcode::G_ADD: 5051fe6060f1SDimitry Andric OpO = TargetOpcode::G_UADDO; 5052fe6060f1SDimitry Andric OpE = TargetOpcode::G_UADDE; 5053fe6060f1SDimitry Andric OpF = TargetOpcode::G_UADDE; 5054fe6060f1SDimitry Andric if (Opcode == TargetOpcode::G_SADDO || Opcode == TargetOpcode::G_SADDE) 5055fe6060f1SDimitry Andric OpF = TargetOpcode::G_SADDE; 5056fe6060f1SDimitry Andric break; 5057fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 5058fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 5059fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 5060fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 5061fe6060f1SDimitry Andric case TargetOpcode::G_SUB: 5062fe6060f1SDimitry Andric OpO = TargetOpcode::G_USUBO; 5063fe6060f1SDimitry Andric OpE = TargetOpcode::G_USUBE; 5064fe6060f1SDimitry Andric OpF = TargetOpcode::G_USUBE; 5065fe6060f1SDimitry Andric if (Opcode == TargetOpcode::G_SSUBO || Opcode == TargetOpcode::G_SSUBE) 5066fe6060f1SDimitry Andric OpF = TargetOpcode::G_SSUBE; 5067fe6060f1SDimitry Andric break; 5068fe6060f1SDimitry Andric default: 5069fe6060f1SDimitry Andric llvm_unreachable("Unexpected add/sub opcode!"); 5070fe6060f1SDimitry Andric } 5071fe6060f1SDimitry Andric 5072fe6060f1SDimitry Andric // 1 for a plain add/sub, 2 if this is an operation with a carry-out. 5073fe6060f1SDimitry Andric unsigned NumDefs = MI.getNumExplicitDefs(); 5074fe6060f1SDimitry Andric Register Src1 = MI.getOperand(NumDefs).getReg(); 5075fe6060f1SDimitry Andric Register Src2 = MI.getOperand(NumDefs + 1).getReg(); 5076fe6060f1SDimitry Andric Register CarryDst, CarryIn; 5077fe6060f1SDimitry Andric if (NumDefs == 2) 5078fe6060f1SDimitry Andric CarryDst = MI.getOperand(1).getReg(); 5079fe6060f1SDimitry Andric if (MI.getNumOperands() == NumDefs + 3) 5080fe6060f1SDimitry Andric CarryIn = MI.getOperand(NumDefs + 2).getReg(); 5081fe6060f1SDimitry Andric 5082fe6060f1SDimitry Andric LLT RegTy = MRI.getType(MI.getOperand(0).getReg()); 5083fe6060f1SDimitry Andric LLT LeftoverTy, DummyTy; 5084fe6060f1SDimitry Andric SmallVector<Register, 2> Src1Regs, Src2Regs, Src1Left, Src2Left, DstRegs; 5085fe6060f1SDimitry Andric extractParts(Src1, RegTy, NarrowTy, LeftoverTy, Src1Regs, Src1Left); 5086fe6060f1SDimitry Andric extractParts(Src2, RegTy, NarrowTy, DummyTy, Src2Regs, Src2Left); 5087fe6060f1SDimitry Andric 5088fe6060f1SDimitry Andric int NarrowParts = Src1Regs.size(); 5089fe6060f1SDimitry Andric for (int I = 0, E = Src1Left.size(); I != E; ++I) { 5090fe6060f1SDimitry Andric Src1Regs.push_back(Src1Left[I]); 5091fe6060f1SDimitry Andric Src2Regs.push_back(Src2Left[I]); 5092fe6060f1SDimitry Andric } 5093fe6060f1SDimitry Andric DstRegs.reserve(Src1Regs.size()); 5094fe6060f1SDimitry Andric 5095fe6060f1SDimitry Andric for (int i = 0, e = Src1Regs.size(); i != e; ++i) { 5096fe6060f1SDimitry Andric Register DstReg = 5097fe6060f1SDimitry Andric MRI.createGenericVirtualRegister(MRI.getType(Src1Regs[i])); 5098fe6060f1SDimitry Andric Register CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1)); 5099fe6060f1SDimitry Andric // Forward the final carry-out to the destination register 5100fe6060f1SDimitry Andric if (i == e - 1 && CarryDst) 5101fe6060f1SDimitry Andric CarryOut = CarryDst; 5102fe6060f1SDimitry Andric 5103fe6060f1SDimitry Andric if (!CarryIn) { 5104fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpO, {DstReg, CarryOut}, 5105fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i]}); 5106fe6060f1SDimitry Andric } else if (i == e - 1) { 5107fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpF, {DstReg, CarryOut}, 5108fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i], CarryIn}); 5109fe6060f1SDimitry Andric } else { 5110fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpE, {DstReg, CarryOut}, 5111fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i], CarryIn}); 5112fe6060f1SDimitry Andric } 5113fe6060f1SDimitry Andric 5114fe6060f1SDimitry Andric DstRegs.push_back(DstReg); 5115fe6060f1SDimitry Andric CarryIn = CarryOut; 5116fe6060f1SDimitry Andric } 5117fe6060f1SDimitry Andric insertParts(MI.getOperand(0).getReg(), RegTy, NarrowTy, 5118fe6060f1SDimitry Andric makeArrayRef(DstRegs).take_front(NarrowParts), LeftoverTy, 5119fe6060f1SDimitry Andric makeArrayRef(DstRegs).drop_front(NarrowParts)); 5120fe6060f1SDimitry Andric 5121fe6060f1SDimitry Andric MI.eraseFromParent(); 5122fe6060f1SDimitry Andric return Legalized; 5123fe6060f1SDimitry Andric } 5124fe6060f1SDimitry Andric 5125fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 51260b57cec5SDimitry Andric LegalizerHelper::narrowScalarMul(MachineInstr &MI, LLT NarrowTy) { 51270b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 51280b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 51290b57cec5SDimitry Andric Register Src2 = MI.getOperand(2).getReg(); 51300b57cec5SDimitry Andric 51310b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 51320b57cec5SDimitry Andric if (Ty.isVector()) 51330b57cec5SDimitry Andric return UnableToLegalize; 51340b57cec5SDimitry Andric 5135349cc55cSDimitry Andric unsigned Size = Ty.getSizeInBits(); 51360b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 5137349cc55cSDimitry Andric if (Size % NarrowSize != 0) 51380b57cec5SDimitry Andric return UnableToLegalize; 51390b57cec5SDimitry Andric 5140349cc55cSDimitry Andric unsigned NumParts = Size / NarrowSize; 51410b57cec5SDimitry Andric bool IsMulHigh = MI.getOpcode() == TargetOpcode::G_UMULH; 5142349cc55cSDimitry Andric unsigned DstTmpParts = NumParts * (IsMulHigh ? 2 : 1); 51430b57cec5SDimitry Andric 51445ffd83dbSDimitry Andric SmallVector<Register, 2> Src1Parts, Src2Parts; 51455ffd83dbSDimitry Andric SmallVector<Register, 2> DstTmpRegs(DstTmpParts); 5146349cc55cSDimitry Andric extractParts(Src1, NarrowTy, NumParts, Src1Parts); 5147349cc55cSDimitry Andric extractParts(Src2, NarrowTy, NumParts, Src2Parts); 51480b57cec5SDimitry Andric multiplyRegisters(DstTmpRegs, Src1Parts, Src2Parts, NarrowTy); 51490b57cec5SDimitry Andric 51500b57cec5SDimitry Andric // Take only high half of registers if this is high mul. 5151349cc55cSDimitry Andric ArrayRef<Register> DstRegs(&DstTmpRegs[DstTmpParts - NumParts], NumParts); 51520b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 51530b57cec5SDimitry Andric MI.eraseFromParent(); 51540b57cec5SDimitry Andric return Legalized; 51550b57cec5SDimitry Andric } 51560b57cec5SDimitry Andric 51570b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 515823408297SDimitry Andric LegalizerHelper::narrowScalarFPTOI(MachineInstr &MI, unsigned TypeIdx, 515923408297SDimitry Andric LLT NarrowTy) { 516023408297SDimitry Andric if (TypeIdx != 0) 516123408297SDimitry Andric return UnableToLegalize; 516223408297SDimitry Andric 516323408297SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_FPTOSI; 516423408297SDimitry Andric 516523408297SDimitry Andric Register Src = MI.getOperand(1).getReg(); 516623408297SDimitry Andric LLT SrcTy = MRI.getType(Src); 516723408297SDimitry Andric 516823408297SDimitry Andric // If all finite floats fit into the narrowed integer type, we can just swap 516923408297SDimitry Andric // out the result type. This is practically only useful for conversions from 517023408297SDimitry Andric // half to at least 16-bits, so just handle the one case. 517123408297SDimitry Andric if (SrcTy.getScalarType() != LLT::scalar(16) || 5172fe6060f1SDimitry Andric NarrowTy.getScalarSizeInBits() < (IsSigned ? 17u : 16u)) 517323408297SDimitry Andric return UnableToLegalize; 517423408297SDimitry Andric 517523408297SDimitry Andric Observer.changingInstr(MI); 517623408297SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, 517723408297SDimitry Andric IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT); 517823408297SDimitry Andric Observer.changedInstr(MI); 517923408297SDimitry Andric return Legalized; 518023408297SDimitry Andric } 518123408297SDimitry Andric 518223408297SDimitry Andric LegalizerHelper::LegalizeResult 51830b57cec5SDimitry Andric LegalizerHelper::narrowScalarExtract(MachineInstr &MI, unsigned TypeIdx, 51840b57cec5SDimitry Andric LLT NarrowTy) { 51850b57cec5SDimitry Andric if (TypeIdx != 1) 51860b57cec5SDimitry Andric return UnableToLegalize; 51870b57cec5SDimitry Andric 51880b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 51890b57cec5SDimitry Andric 51900b57cec5SDimitry Andric int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 51910b57cec5SDimitry Andric // FIXME: add support for when SizeOp1 isn't an exact multiple of 51920b57cec5SDimitry Andric // NarrowSize. 51930b57cec5SDimitry Andric if (SizeOp1 % NarrowSize != 0) 51940b57cec5SDimitry Andric return UnableToLegalize; 51950b57cec5SDimitry Andric int NumParts = SizeOp1 / NarrowSize; 51960b57cec5SDimitry Andric 51970b57cec5SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 51980b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 51990b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 52000b57cec5SDimitry Andric 52010b57cec5SDimitry Andric Register OpReg = MI.getOperand(0).getReg(); 52020b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(2).getImm(); 52030b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 52040b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 52050b57cec5SDimitry Andric unsigned SrcStart = i * NarrowSize; 52060b57cec5SDimitry Andric 52070b57cec5SDimitry Andric if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) { 52080b57cec5SDimitry Andric // No part of the extract uses this subregister, ignore it. 52090b57cec5SDimitry Andric continue; 52100b57cec5SDimitry Andric } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 52110b57cec5SDimitry Andric // The entire subregister is extracted, forward the value. 52120b57cec5SDimitry Andric DstRegs.push_back(SrcRegs[i]); 52130b57cec5SDimitry Andric continue; 52140b57cec5SDimitry Andric } 52150b57cec5SDimitry Andric 52160b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 52170b57cec5SDimitry Andric // extended infinitely in both directions. 52180b57cec5SDimitry Andric int64_t ExtractOffset; 52190b57cec5SDimitry Andric uint64_t SegSize; 52200b57cec5SDimitry Andric if (OpStart < SrcStart) { 52210b57cec5SDimitry Andric ExtractOffset = 0; 52220b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart); 52230b57cec5SDimitry Andric } else { 52240b57cec5SDimitry Andric ExtractOffset = OpStart - SrcStart; 52250b57cec5SDimitry Andric SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize); 52260b57cec5SDimitry Andric } 52270b57cec5SDimitry Andric 52280b57cec5SDimitry Andric Register SegReg = SrcRegs[i]; 52290b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != NarrowSize) { 52300b57cec5SDimitry Andric // A genuine extract is needed. 52310b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 52320b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset); 52330b57cec5SDimitry Andric } 52340b57cec5SDimitry Andric 52350b57cec5SDimitry Andric DstRegs.push_back(SegReg); 52360b57cec5SDimitry Andric } 52370b57cec5SDimitry Andric 52380b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 52390b57cec5SDimitry Andric if (MRI.getType(DstReg).isVector()) 52400b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 52415ffd83dbSDimitry Andric else if (DstRegs.size() > 1) 52420b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 52435ffd83dbSDimitry Andric else 52445ffd83dbSDimitry Andric MIRBuilder.buildCopy(DstReg, DstRegs[0]); 52450b57cec5SDimitry Andric MI.eraseFromParent(); 52460b57cec5SDimitry Andric return Legalized; 52470b57cec5SDimitry Andric } 52480b57cec5SDimitry Andric 52490b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 52500b57cec5SDimitry Andric LegalizerHelper::narrowScalarInsert(MachineInstr &MI, unsigned TypeIdx, 52510b57cec5SDimitry Andric LLT NarrowTy) { 52520b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 52530b57cec5SDimitry Andric if (TypeIdx != 0) 52540b57cec5SDimitry Andric return UnableToLegalize; 52550b57cec5SDimitry Andric 5256fe6060f1SDimitry Andric SmallVector<Register, 2> SrcRegs, LeftoverRegs, DstRegs; 52570b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 5258fe6060f1SDimitry Andric LLT RegTy = MRI.getType(MI.getOperand(0).getReg()); 5259fe6060f1SDimitry Andric LLT LeftoverTy; 5260fe6060f1SDimitry Andric extractParts(MI.getOperand(1).getReg(), RegTy, NarrowTy, LeftoverTy, SrcRegs, 5261fe6060f1SDimitry Andric LeftoverRegs); 52620b57cec5SDimitry Andric 5263fe6060f1SDimitry Andric for (Register Reg : LeftoverRegs) 5264fe6060f1SDimitry Andric SrcRegs.push_back(Reg); 5265fe6060f1SDimitry Andric 5266fe6060f1SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 52670b57cec5SDimitry Andric Register OpReg = MI.getOperand(2).getReg(); 52680b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(3).getImm(); 52690b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 5270fe6060f1SDimitry Andric for (int I = 0, E = SrcRegs.size(); I != E; ++I) { 5271fe6060f1SDimitry Andric unsigned DstStart = I * NarrowSize; 52720b57cec5SDimitry Andric 5273fe6060f1SDimitry Andric if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 52740b57cec5SDimitry Andric // The entire subregister is defined by this insert, forward the new 52750b57cec5SDimitry Andric // value. 52760b57cec5SDimitry Andric DstRegs.push_back(OpReg); 52770b57cec5SDimitry Andric continue; 52780b57cec5SDimitry Andric } 52790b57cec5SDimitry Andric 5280fe6060f1SDimitry Andric Register SrcReg = SrcRegs[I]; 5281fe6060f1SDimitry Andric if (MRI.getType(SrcRegs[I]) == LeftoverTy) { 5282fe6060f1SDimitry Andric // The leftover reg is smaller than NarrowTy, so we need to extend it. 5283fe6060f1SDimitry Andric SrcReg = MRI.createGenericVirtualRegister(NarrowTy); 5284fe6060f1SDimitry Andric MIRBuilder.buildAnyExt(SrcReg, SrcRegs[I]); 5285fe6060f1SDimitry Andric } 5286fe6060f1SDimitry Andric 5287fe6060f1SDimitry Andric if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) { 5288fe6060f1SDimitry Andric // No part of the insert affects this subregister, forward the original. 5289fe6060f1SDimitry Andric DstRegs.push_back(SrcReg); 5290fe6060f1SDimitry Andric continue; 5291fe6060f1SDimitry Andric } 5292fe6060f1SDimitry Andric 52930b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 52940b57cec5SDimitry Andric // extended infinitely in both directions. 52950b57cec5SDimitry Andric int64_t ExtractOffset, InsertOffset; 52960b57cec5SDimitry Andric uint64_t SegSize; 52970b57cec5SDimitry Andric if (OpStart < DstStart) { 52980b57cec5SDimitry Andric InsertOffset = 0; 52990b57cec5SDimitry Andric ExtractOffset = DstStart - OpStart; 53000b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart); 53010b57cec5SDimitry Andric } else { 53020b57cec5SDimitry Andric InsertOffset = OpStart - DstStart; 53030b57cec5SDimitry Andric ExtractOffset = 0; 53040b57cec5SDimitry Andric SegSize = 53050b57cec5SDimitry Andric std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart); 53060b57cec5SDimitry Andric } 53070b57cec5SDimitry Andric 53080b57cec5SDimitry Andric Register SegReg = OpReg; 53090b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != OpSize) { 53100b57cec5SDimitry Andric // A genuine extract is needed. 53110b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 53120b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset); 53130b57cec5SDimitry Andric } 53140b57cec5SDimitry Andric 53150b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy); 5316fe6060f1SDimitry Andric MIRBuilder.buildInsert(DstReg, SrcReg, SegReg, InsertOffset); 53170b57cec5SDimitry Andric DstRegs.push_back(DstReg); 53180b57cec5SDimitry Andric } 53190b57cec5SDimitry Andric 5320fe6060f1SDimitry Andric uint64_t WideSize = DstRegs.size() * NarrowSize; 53210b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 5322fe6060f1SDimitry Andric if (WideSize > RegTy.getSizeInBits()) { 5323fe6060f1SDimitry Andric Register MergeReg = MRI.createGenericVirtualRegister(LLT::scalar(WideSize)); 5324fe6060f1SDimitry Andric MIRBuilder.buildMerge(MergeReg, DstRegs); 5325fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, MergeReg); 5326fe6060f1SDimitry Andric } else 53270b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 5328fe6060f1SDimitry Andric 53290b57cec5SDimitry Andric MI.eraseFromParent(); 53300b57cec5SDimitry Andric return Legalized; 53310b57cec5SDimitry Andric } 53320b57cec5SDimitry Andric 53330b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 53340b57cec5SDimitry Andric LegalizerHelper::narrowScalarBasic(MachineInstr &MI, unsigned TypeIdx, 53350b57cec5SDimitry Andric LLT NarrowTy) { 53360b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 53370b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 53380b57cec5SDimitry Andric 53390b57cec5SDimitry Andric assert(MI.getNumOperands() == 3 && TypeIdx == 0); 53400b57cec5SDimitry Andric 53410b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 53420b57cec5SDimitry Andric SmallVector<Register, 4> Src0Regs, Src0LeftoverRegs; 53430b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 53440b57cec5SDimitry Andric LLT LeftoverTy; 53450b57cec5SDimitry Andric if (!extractParts(MI.getOperand(1).getReg(), DstTy, NarrowTy, LeftoverTy, 53460b57cec5SDimitry Andric Src0Regs, Src0LeftoverRegs)) 53470b57cec5SDimitry Andric return UnableToLegalize; 53480b57cec5SDimitry Andric 53490b57cec5SDimitry Andric LLT Unused; 53500b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, Unused, 53510b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 53520b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 53530b57cec5SDimitry Andric 53540b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 53550b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, 53560b57cec5SDimitry Andric {Src0Regs[I], Src1Regs[I]}); 53575ffd83dbSDimitry Andric DstRegs.push_back(Inst.getReg(0)); 53580b57cec5SDimitry Andric } 53590b57cec5SDimitry Andric 53600b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 53610b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr( 53620b57cec5SDimitry Andric MI.getOpcode(), 53630b57cec5SDimitry Andric {LeftoverTy}, {Src0LeftoverRegs[I], Src1LeftoverRegs[I]}); 53645ffd83dbSDimitry Andric DstLeftoverRegs.push_back(Inst.getReg(0)); 53650b57cec5SDimitry Andric } 53660b57cec5SDimitry Andric 53670b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 53680b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 53690b57cec5SDimitry Andric 53700b57cec5SDimitry Andric MI.eraseFromParent(); 53710b57cec5SDimitry Andric return Legalized; 53720b57cec5SDimitry Andric } 53730b57cec5SDimitry Andric 53740b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 53755ffd83dbSDimitry Andric LegalizerHelper::narrowScalarExt(MachineInstr &MI, unsigned TypeIdx, 53765ffd83dbSDimitry Andric LLT NarrowTy) { 53775ffd83dbSDimitry Andric if (TypeIdx != 0) 53785ffd83dbSDimitry Andric return UnableToLegalize; 53795ffd83dbSDimitry Andric 53805ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 53815ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 53825ffd83dbSDimitry Andric 53835ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 53845ffd83dbSDimitry Andric if (DstTy.isVector()) 53855ffd83dbSDimitry Andric return UnableToLegalize; 53865ffd83dbSDimitry Andric 53875ffd83dbSDimitry Andric SmallVector<Register, 8> Parts; 53885ffd83dbSDimitry Andric LLT GCDTy = extractGCDType(Parts, DstTy, NarrowTy, SrcReg); 53895ffd83dbSDimitry Andric LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts, MI.getOpcode()); 53905ffd83dbSDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, Parts); 53915ffd83dbSDimitry Andric 53925ffd83dbSDimitry Andric MI.eraseFromParent(); 53935ffd83dbSDimitry Andric return Legalized; 53945ffd83dbSDimitry Andric } 53955ffd83dbSDimitry Andric 53965ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 53970b57cec5SDimitry Andric LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx, 53980b57cec5SDimitry Andric LLT NarrowTy) { 53990b57cec5SDimitry Andric if (TypeIdx != 0) 54000b57cec5SDimitry Andric return UnableToLegalize; 54010b57cec5SDimitry Andric 54020b57cec5SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 54030b57cec5SDimitry Andric LLT CondTy = MRI.getType(CondReg); 54040b57cec5SDimitry Andric if (CondTy.isVector()) // TODO: Handle vselect 54050b57cec5SDimitry Andric return UnableToLegalize; 54060b57cec5SDimitry Andric 54070b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 54080b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 54090b57cec5SDimitry Andric 54100b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 54110b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 54120b57cec5SDimitry Andric SmallVector<Register, 4> Src2Regs, Src2LeftoverRegs; 54130b57cec5SDimitry Andric LLT LeftoverTy; 54140b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy, 54150b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 54160b57cec5SDimitry Andric return UnableToLegalize; 54170b57cec5SDimitry Andric 54180b57cec5SDimitry Andric LLT Unused; 54190b57cec5SDimitry Andric if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused, 54200b57cec5SDimitry Andric Src2Regs, Src2LeftoverRegs)) 54210b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 54220b57cec5SDimitry Andric 54230b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 54240b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect(NarrowTy, 54250b57cec5SDimitry Andric CondReg, Src1Regs[I], Src2Regs[I]); 54265ffd83dbSDimitry Andric DstRegs.push_back(Select.getReg(0)); 54270b57cec5SDimitry Andric } 54280b57cec5SDimitry Andric 54290b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 54300b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect( 54310b57cec5SDimitry Andric LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]); 54325ffd83dbSDimitry Andric DstLeftoverRegs.push_back(Select.getReg(0)); 54330b57cec5SDimitry Andric } 54340b57cec5SDimitry Andric 54350b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 54360b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 54370b57cec5SDimitry Andric 54380b57cec5SDimitry Andric MI.eraseFromParent(); 54390b57cec5SDimitry Andric return Legalized; 54400b57cec5SDimitry Andric } 54410b57cec5SDimitry Andric 54420b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 54435ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTLZ(MachineInstr &MI, unsigned TypeIdx, 54445ffd83dbSDimitry Andric LLT NarrowTy) { 54455ffd83dbSDimitry Andric if (TypeIdx != 1) 54465ffd83dbSDimitry Andric return UnableToLegalize; 54475ffd83dbSDimitry Andric 54485ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 54495ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 54505ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 54515ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 54525ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 54535ffd83dbSDimitry Andric 54545ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 54555ffd83dbSDimitry Andric const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF; 54565ffd83dbSDimitry Andric 54575ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 54585ffd83dbSDimitry Andric auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg); 54595ffd83dbSDimitry Andric // ctlz(Hi:Lo) -> Hi == 0 ? (NarrowSize + ctlz(Lo)) : ctlz(Hi) 54605ffd83dbSDimitry Andric auto C_0 = B.buildConstant(NarrowTy, 0); 54615ffd83dbSDimitry Andric auto HiIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 54625ffd83dbSDimitry Andric UnmergeSrc.getReg(1), C_0); 54635ffd83dbSDimitry Andric auto LoCTLZ = IsUndef ? 54645ffd83dbSDimitry Andric B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0)) : 54655ffd83dbSDimitry Andric B.buildCTLZ(DstTy, UnmergeSrc.getReg(0)); 54665ffd83dbSDimitry Andric auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize); 54675ffd83dbSDimitry Andric auto HiIsZeroCTLZ = B.buildAdd(DstTy, LoCTLZ, C_NarrowSize); 54685ffd83dbSDimitry Andric auto HiCTLZ = B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1)); 54695ffd83dbSDimitry Andric B.buildSelect(DstReg, HiIsZero, HiIsZeroCTLZ, HiCTLZ); 54705ffd83dbSDimitry Andric 54715ffd83dbSDimitry Andric MI.eraseFromParent(); 54725ffd83dbSDimitry Andric return Legalized; 54735ffd83dbSDimitry Andric } 54745ffd83dbSDimitry Andric 54755ffd83dbSDimitry Andric return UnableToLegalize; 54765ffd83dbSDimitry Andric } 54775ffd83dbSDimitry Andric 54785ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 54795ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTTZ(MachineInstr &MI, unsigned TypeIdx, 54805ffd83dbSDimitry Andric LLT NarrowTy) { 54815ffd83dbSDimitry Andric if (TypeIdx != 1) 54825ffd83dbSDimitry Andric return UnableToLegalize; 54835ffd83dbSDimitry Andric 54845ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 54855ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 54865ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 54875ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 54885ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 54895ffd83dbSDimitry Andric 54905ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 54915ffd83dbSDimitry Andric const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTTZ_ZERO_UNDEF; 54925ffd83dbSDimitry Andric 54935ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 54945ffd83dbSDimitry Andric auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg); 54955ffd83dbSDimitry Andric // cttz(Hi:Lo) -> Lo == 0 ? (cttz(Hi) + NarrowSize) : cttz(Lo) 54965ffd83dbSDimitry Andric auto C_0 = B.buildConstant(NarrowTy, 0); 54975ffd83dbSDimitry Andric auto LoIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 54985ffd83dbSDimitry Andric UnmergeSrc.getReg(0), C_0); 54995ffd83dbSDimitry Andric auto HiCTTZ = IsUndef ? 55005ffd83dbSDimitry Andric B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1)) : 55015ffd83dbSDimitry Andric B.buildCTTZ(DstTy, UnmergeSrc.getReg(1)); 55025ffd83dbSDimitry Andric auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize); 55035ffd83dbSDimitry Andric auto LoIsZeroCTTZ = B.buildAdd(DstTy, HiCTTZ, C_NarrowSize); 55045ffd83dbSDimitry Andric auto LoCTTZ = B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0)); 55055ffd83dbSDimitry Andric B.buildSelect(DstReg, LoIsZero, LoIsZeroCTTZ, LoCTTZ); 55065ffd83dbSDimitry Andric 55075ffd83dbSDimitry Andric MI.eraseFromParent(); 55085ffd83dbSDimitry Andric return Legalized; 55095ffd83dbSDimitry Andric } 55105ffd83dbSDimitry Andric 55115ffd83dbSDimitry Andric return UnableToLegalize; 55125ffd83dbSDimitry Andric } 55135ffd83dbSDimitry Andric 55145ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 55155ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTPOP(MachineInstr &MI, unsigned TypeIdx, 55165ffd83dbSDimitry Andric LLT NarrowTy) { 55175ffd83dbSDimitry Andric if (TypeIdx != 1) 55185ffd83dbSDimitry Andric return UnableToLegalize; 55195ffd83dbSDimitry Andric 55205ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 55215ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 55225ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(MI.getOperand(1).getReg()); 55235ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 55245ffd83dbSDimitry Andric 55255ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 55265ffd83dbSDimitry Andric auto UnmergeSrc = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1)); 55275ffd83dbSDimitry Andric 55285ffd83dbSDimitry Andric auto LoCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(0)); 55295ffd83dbSDimitry Andric auto HiCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(1)); 55305ffd83dbSDimitry Andric MIRBuilder.buildAdd(DstReg, HiCTPOP, LoCTPOP); 55315ffd83dbSDimitry Andric 55325ffd83dbSDimitry Andric MI.eraseFromParent(); 55335ffd83dbSDimitry Andric return Legalized; 55345ffd83dbSDimitry Andric } 55355ffd83dbSDimitry Andric 55365ffd83dbSDimitry Andric return UnableToLegalize; 55375ffd83dbSDimitry Andric } 55385ffd83dbSDimitry Andric 55395ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 5540e8d8bef9SDimitry Andric LegalizerHelper::lowerBitCount(MachineInstr &MI) { 55410b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 5542e8d8bef9SDimitry Andric const auto &TII = MIRBuilder.getTII(); 55430b57cec5SDimitry Andric auto isSupported = [this](const LegalityQuery &Q) { 55440b57cec5SDimitry Andric auto QAction = LI.getAction(Q).Action; 55450b57cec5SDimitry Andric return QAction == Legal || QAction == Libcall || QAction == Custom; 55460b57cec5SDimitry Andric }; 55470b57cec5SDimitry Andric switch (Opc) { 55480b57cec5SDimitry Andric default: 55490b57cec5SDimitry Andric return UnableToLegalize; 55500b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: { 55510b57cec5SDimitry Andric // This trivially expands to CTLZ. 55520b57cec5SDimitry Andric Observer.changingInstr(MI); 55530b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTLZ)); 55540b57cec5SDimitry Andric Observer.changedInstr(MI); 55550b57cec5SDimitry Andric return Legalized; 55560b57cec5SDimitry Andric } 55570b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: { 55585ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 55590b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 55605ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 55615ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 55625ffd83dbSDimitry Andric unsigned Len = SrcTy.getSizeInBits(); 55635ffd83dbSDimitry Andric 55645ffd83dbSDimitry Andric if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {DstTy, SrcTy}})) { 55650b57cec5SDimitry Andric // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero. 55665ffd83dbSDimitry Andric auto CtlzZU = MIRBuilder.buildCTLZ_ZERO_UNDEF(DstTy, SrcReg); 55675ffd83dbSDimitry Andric auto ZeroSrc = MIRBuilder.buildConstant(SrcTy, 0); 55685ffd83dbSDimitry Andric auto ICmp = MIRBuilder.buildICmp( 55695ffd83dbSDimitry Andric CmpInst::ICMP_EQ, SrcTy.changeElementSize(1), SrcReg, ZeroSrc); 55705ffd83dbSDimitry Andric auto LenConst = MIRBuilder.buildConstant(DstTy, Len); 55715ffd83dbSDimitry Andric MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CtlzZU); 55720b57cec5SDimitry Andric MI.eraseFromParent(); 55730b57cec5SDimitry Andric return Legalized; 55740b57cec5SDimitry Andric } 55750b57cec5SDimitry Andric // for now, we do this: 55760b57cec5SDimitry Andric // NewLen = NextPowerOf2(Len); 55770b57cec5SDimitry Andric // x = x | (x >> 1); 55780b57cec5SDimitry Andric // x = x | (x >> 2); 55790b57cec5SDimitry Andric // ... 55800b57cec5SDimitry Andric // x = x | (x >>16); 55810b57cec5SDimitry Andric // x = x | (x >>32); // for 64-bit input 55820b57cec5SDimitry Andric // Upto NewLen/2 55830b57cec5SDimitry Andric // return Len - popcount(x); 55840b57cec5SDimitry Andric // 55850b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 55860b57cec5SDimitry Andric Register Op = SrcReg; 55870b57cec5SDimitry Andric unsigned NewLen = PowerOf2Ceil(Len); 55880b57cec5SDimitry Andric for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) { 55895ffd83dbSDimitry Andric auto MIBShiftAmt = MIRBuilder.buildConstant(SrcTy, 1ULL << i); 55905ffd83dbSDimitry Andric auto MIBOp = MIRBuilder.buildOr( 55915ffd83dbSDimitry Andric SrcTy, Op, MIRBuilder.buildLShr(SrcTy, Op, MIBShiftAmt)); 55925ffd83dbSDimitry Andric Op = MIBOp.getReg(0); 55930b57cec5SDimitry Andric } 55945ffd83dbSDimitry Andric auto MIBPop = MIRBuilder.buildCTPOP(DstTy, Op); 55955ffd83dbSDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MIRBuilder.buildConstant(DstTy, Len), 55965ffd83dbSDimitry Andric MIBPop); 55970b57cec5SDimitry Andric MI.eraseFromParent(); 55980b57cec5SDimitry Andric return Legalized; 55990b57cec5SDimitry Andric } 56000b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: { 56010b57cec5SDimitry Andric // This trivially expands to CTTZ. 56020b57cec5SDimitry Andric Observer.changingInstr(MI); 56030b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTTZ)); 56040b57cec5SDimitry Andric Observer.changedInstr(MI); 56050b57cec5SDimitry Andric return Legalized; 56060b57cec5SDimitry Andric } 56070b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: { 56085ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 56090b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 56105ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 56115ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 56125ffd83dbSDimitry Andric 56135ffd83dbSDimitry Andric unsigned Len = SrcTy.getSizeInBits(); 56145ffd83dbSDimitry Andric if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {DstTy, SrcTy}})) { 56150b57cec5SDimitry Andric // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with 56160b57cec5SDimitry Andric // zero. 56175ffd83dbSDimitry Andric auto CttzZU = MIRBuilder.buildCTTZ_ZERO_UNDEF(DstTy, SrcReg); 56185ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildConstant(SrcTy, 0); 56195ffd83dbSDimitry Andric auto ICmp = MIRBuilder.buildICmp( 56205ffd83dbSDimitry Andric CmpInst::ICMP_EQ, DstTy.changeElementSize(1), SrcReg, Zero); 56215ffd83dbSDimitry Andric auto LenConst = MIRBuilder.buildConstant(DstTy, Len); 56225ffd83dbSDimitry Andric MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CttzZU); 56230b57cec5SDimitry Andric MI.eraseFromParent(); 56240b57cec5SDimitry Andric return Legalized; 56250b57cec5SDimitry Andric } 56260b57cec5SDimitry Andric // for now, we use: { return popcount(~x & (x - 1)); } 56270b57cec5SDimitry Andric // unless the target has ctlz but not ctpop, in which case we use: 56280b57cec5SDimitry Andric // { return 32 - nlz(~x & (x-1)); } 56290b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 5630e8d8bef9SDimitry Andric auto MIBCstNeg1 = MIRBuilder.buildConstant(SrcTy, -1); 5631e8d8bef9SDimitry Andric auto MIBNot = MIRBuilder.buildXor(SrcTy, SrcReg, MIBCstNeg1); 56325ffd83dbSDimitry Andric auto MIBTmp = MIRBuilder.buildAnd( 5633e8d8bef9SDimitry Andric SrcTy, MIBNot, MIRBuilder.buildAdd(SrcTy, SrcReg, MIBCstNeg1)); 5634e8d8bef9SDimitry Andric if (!isSupported({TargetOpcode::G_CTPOP, {SrcTy, SrcTy}}) && 5635e8d8bef9SDimitry Andric isSupported({TargetOpcode::G_CTLZ, {SrcTy, SrcTy}})) { 5636e8d8bef9SDimitry Andric auto MIBCstLen = MIRBuilder.buildConstant(SrcTy, Len); 56375ffd83dbSDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MIBCstLen, 5638e8d8bef9SDimitry Andric MIRBuilder.buildCTLZ(SrcTy, MIBTmp)); 56390b57cec5SDimitry Andric MI.eraseFromParent(); 56400b57cec5SDimitry Andric return Legalized; 56410b57cec5SDimitry Andric } 56420b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTPOP)); 56435ffd83dbSDimitry Andric MI.getOperand(1).setReg(MIBTmp.getReg(0)); 56445ffd83dbSDimitry Andric return Legalized; 56455ffd83dbSDimitry Andric } 56465ffd83dbSDimitry Andric case TargetOpcode::G_CTPOP: { 5647e8d8bef9SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 5648e8d8bef9SDimitry Andric LLT Ty = MRI.getType(SrcReg); 56495ffd83dbSDimitry Andric unsigned Size = Ty.getSizeInBits(); 56505ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 56515ffd83dbSDimitry Andric 56525ffd83dbSDimitry Andric // Count set bits in blocks of 2 bits. Default approach would be 56535ffd83dbSDimitry Andric // B2Count = { val & 0x55555555 } + { (val >> 1) & 0x55555555 } 56545ffd83dbSDimitry Andric // We use following formula instead: 56555ffd83dbSDimitry Andric // B2Count = val - { (val >> 1) & 0x55555555 } 56565ffd83dbSDimitry Andric // since it gives same result in blocks of 2 with one instruction less. 56575ffd83dbSDimitry Andric auto C_1 = B.buildConstant(Ty, 1); 5658e8d8bef9SDimitry Andric auto B2Set1LoTo1Hi = B.buildLShr(Ty, SrcReg, C_1); 56595ffd83dbSDimitry Andric APInt B2Mask1HiTo0 = APInt::getSplat(Size, APInt(8, 0x55)); 56605ffd83dbSDimitry Andric auto C_B2Mask1HiTo0 = B.buildConstant(Ty, B2Mask1HiTo0); 56615ffd83dbSDimitry Andric auto B2Count1Hi = B.buildAnd(Ty, B2Set1LoTo1Hi, C_B2Mask1HiTo0); 5662e8d8bef9SDimitry Andric auto B2Count = B.buildSub(Ty, SrcReg, B2Count1Hi); 56635ffd83dbSDimitry Andric 56645ffd83dbSDimitry Andric // In order to get count in blocks of 4 add values from adjacent block of 2. 56655ffd83dbSDimitry Andric // B4Count = { B2Count & 0x33333333 } + { (B2Count >> 2) & 0x33333333 } 56665ffd83dbSDimitry Andric auto C_2 = B.buildConstant(Ty, 2); 56675ffd83dbSDimitry Andric auto B4Set2LoTo2Hi = B.buildLShr(Ty, B2Count, C_2); 56685ffd83dbSDimitry Andric APInt B4Mask2HiTo0 = APInt::getSplat(Size, APInt(8, 0x33)); 56695ffd83dbSDimitry Andric auto C_B4Mask2HiTo0 = B.buildConstant(Ty, B4Mask2HiTo0); 56705ffd83dbSDimitry Andric auto B4HiB2Count = B.buildAnd(Ty, B4Set2LoTo2Hi, C_B4Mask2HiTo0); 56715ffd83dbSDimitry Andric auto B4LoB2Count = B.buildAnd(Ty, B2Count, C_B4Mask2HiTo0); 56725ffd83dbSDimitry Andric auto B4Count = B.buildAdd(Ty, B4HiB2Count, B4LoB2Count); 56735ffd83dbSDimitry Andric 56745ffd83dbSDimitry Andric // For count in blocks of 8 bits we don't have to mask high 4 bits before 56755ffd83dbSDimitry Andric // addition since count value sits in range {0,...,8} and 4 bits are enough 56765ffd83dbSDimitry Andric // to hold such binary values. After addition high 4 bits still hold count 56775ffd83dbSDimitry Andric // of set bits in high 4 bit block, set them to zero and get 8 bit result. 56785ffd83dbSDimitry Andric // B8Count = { B4Count + (B4Count >> 4) } & 0x0F0F0F0F 56795ffd83dbSDimitry Andric auto C_4 = B.buildConstant(Ty, 4); 56805ffd83dbSDimitry Andric auto B8HiB4Count = B.buildLShr(Ty, B4Count, C_4); 56815ffd83dbSDimitry Andric auto B8CountDirty4Hi = B.buildAdd(Ty, B8HiB4Count, B4Count); 56825ffd83dbSDimitry Andric APInt B8Mask4HiTo0 = APInt::getSplat(Size, APInt(8, 0x0F)); 56835ffd83dbSDimitry Andric auto C_B8Mask4HiTo0 = B.buildConstant(Ty, B8Mask4HiTo0); 56845ffd83dbSDimitry Andric auto B8Count = B.buildAnd(Ty, B8CountDirty4Hi, C_B8Mask4HiTo0); 56855ffd83dbSDimitry Andric 56865ffd83dbSDimitry Andric assert(Size<=128 && "Scalar size is too large for CTPOP lower algorithm"); 56875ffd83dbSDimitry Andric // 8 bits can hold CTPOP result of 128 bit int or smaller. Mul with this 56885ffd83dbSDimitry Andric // bitmask will set 8 msb in ResTmp to sum of all B8Counts in 8 bit blocks. 56895ffd83dbSDimitry Andric auto MulMask = B.buildConstant(Ty, APInt::getSplat(Size, APInt(8, 0x01))); 56905ffd83dbSDimitry Andric auto ResTmp = B.buildMul(Ty, B8Count, MulMask); 56915ffd83dbSDimitry Andric 56925ffd83dbSDimitry Andric // Shift count result from 8 high bits to low bits. 56935ffd83dbSDimitry Andric auto C_SizeM8 = B.buildConstant(Ty, Size - 8); 56945ffd83dbSDimitry Andric B.buildLShr(MI.getOperand(0).getReg(), ResTmp, C_SizeM8); 56955ffd83dbSDimitry Andric 56965ffd83dbSDimitry Andric MI.eraseFromParent(); 56970b57cec5SDimitry Andric return Legalized; 56980b57cec5SDimitry Andric } 56990b57cec5SDimitry Andric } 57000b57cec5SDimitry Andric } 57010b57cec5SDimitry Andric 5702fe6060f1SDimitry Andric // Check that (every element of) Reg is undef or not an exact multiple of BW. 5703fe6060f1SDimitry Andric static bool isNonZeroModBitWidthOrUndef(const MachineRegisterInfo &MRI, 5704fe6060f1SDimitry Andric Register Reg, unsigned BW) { 5705fe6060f1SDimitry Andric return matchUnaryPredicate( 5706fe6060f1SDimitry Andric MRI, Reg, 5707fe6060f1SDimitry Andric [=](const Constant *C) { 5708fe6060f1SDimitry Andric // Null constant here means an undef. 5709fe6060f1SDimitry Andric const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C); 5710fe6060f1SDimitry Andric return !CI || CI->getValue().urem(BW) != 0; 5711fe6060f1SDimitry Andric }, 5712fe6060f1SDimitry Andric /*AllowUndefs*/ true); 5713fe6060f1SDimitry Andric } 5714fe6060f1SDimitry Andric 5715fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5716fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShiftWithInverse(MachineInstr &MI) { 5717fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5718fe6060f1SDimitry Andric Register X = MI.getOperand(1).getReg(); 5719fe6060f1SDimitry Andric Register Y = MI.getOperand(2).getReg(); 5720fe6060f1SDimitry Andric Register Z = MI.getOperand(3).getReg(); 5721fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 5722fe6060f1SDimitry Andric LLT ShTy = MRI.getType(Z); 5723fe6060f1SDimitry Andric 5724fe6060f1SDimitry Andric unsigned BW = Ty.getScalarSizeInBits(); 5725fe6060f1SDimitry Andric 5726fe6060f1SDimitry Andric if (!isPowerOf2_32(BW)) 5727fe6060f1SDimitry Andric return UnableToLegalize; 5728fe6060f1SDimitry Andric 5729fe6060f1SDimitry Andric const bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 5730fe6060f1SDimitry Andric unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; 5731fe6060f1SDimitry Andric 5732fe6060f1SDimitry Andric if (isNonZeroModBitWidthOrUndef(MRI, Z, BW)) { 5733fe6060f1SDimitry Andric // fshl X, Y, Z -> fshr X, Y, -Z 5734fe6060f1SDimitry Andric // fshr X, Y, Z -> fshl X, Y, -Z 5735fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(ShTy, 0); 5736fe6060f1SDimitry Andric Z = MIRBuilder.buildSub(Ty, Zero, Z).getReg(0); 5737fe6060f1SDimitry Andric } else { 5738fe6060f1SDimitry Andric // fshl X, Y, Z -> fshr (srl X, 1), (fshr X, Y, 1), ~Z 5739fe6060f1SDimitry Andric // fshr X, Y, Z -> fshl (fshl X, Y, 1), (shl Y, 1), ~Z 5740fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(ShTy, 1); 5741fe6060f1SDimitry Andric if (IsFSHL) { 5742fe6060f1SDimitry Andric Y = MIRBuilder.buildInstr(RevOpcode, {Ty}, {X, Y, One}).getReg(0); 5743fe6060f1SDimitry Andric X = MIRBuilder.buildLShr(Ty, X, One).getReg(0); 5744fe6060f1SDimitry Andric } else { 5745fe6060f1SDimitry Andric X = MIRBuilder.buildInstr(RevOpcode, {Ty}, {X, Y, One}).getReg(0); 5746fe6060f1SDimitry Andric Y = MIRBuilder.buildShl(Ty, Y, One).getReg(0); 5747fe6060f1SDimitry Andric } 5748fe6060f1SDimitry Andric 5749fe6060f1SDimitry Andric Z = MIRBuilder.buildNot(ShTy, Z).getReg(0); 5750fe6060f1SDimitry Andric } 5751fe6060f1SDimitry Andric 5752fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevOpcode, {Dst}, {X, Y, Z}); 5753fe6060f1SDimitry Andric MI.eraseFromParent(); 5754fe6060f1SDimitry Andric return Legalized; 5755fe6060f1SDimitry Andric } 5756fe6060f1SDimitry Andric 5757fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5758fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShiftAsShifts(MachineInstr &MI) { 5759fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5760fe6060f1SDimitry Andric Register X = MI.getOperand(1).getReg(); 5761fe6060f1SDimitry Andric Register Y = MI.getOperand(2).getReg(); 5762fe6060f1SDimitry Andric Register Z = MI.getOperand(3).getReg(); 5763fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 5764fe6060f1SDimitry Andric LLT ShTy = MRI.getType(Z); 5765fe6060f1SDimitry Andric 5766fe6060f1SDimitry Andric const unsigned BW = Ty.getScalarSizeInBits(); 5767fe6060f1SDimitry Andric const bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 5768fe6060f1SDimitry Andric 5769fe6060f1SDimitry Andric Register ShX, ShY; 5770fe6060f1SDimitry Andric Register ShAmt, InvShAmt; 5771fe6060f1SDimitry Andric 5772fe6060f1SDimitry Andric // FIXME: Emit optimized urem by constant instead of letting it expand later. 5773fe6060f1SDimitry Andric if (isNonZeroModBitWidthOrUndef(MRI, Z, BW)) { 5774fe6060f1SDimitry Andric // fshl: X << C | Y >> (BW - C) 5775fe6060f1SDimitry Andric // fshr: X << (BW - C) | Y >> C 5776fe6060f1SDimitry Andric // where C = Z % BW is not zero 5777fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(ShTy, BW); 5778fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildURem(ShTy, Z, BitWidthC).getReg(0); 5779fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildSub(ShTy, BitWidthC, ShAmt).getReg(0); 5780fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, X, IsFSHL ? ShAmt : InvShAmt).getReg(0); 5781fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, Y, IsFSHL ? InvShAmt : ShAmt).getReg(0); 5782fe6060f1SDimitry Andric } else { 5783fe6060f1SDimitry Andric // fshl: X << (Z % BW) | Y >> 1 >> (BW - 1 - (Z % BW)) 5784fe6060f1SDimitry Andric // fshr: X << 1 << (BW - 1 - (Z % BW)) | Y >> (Z % BW) 5785fe6060f1SDimitry Andric auto Mask = MIRBuilder.buildConstant(ShTy, BW - 1); 5786fe6060f1SDimitry Andric if (isPowerOf2_32(BW)) { 5787fe6060f1SDimitry Andric // Z % BW -> Z & (BW - 1) 5788fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildAnd(ShTy, Z, Mask).getReg(0); 5789fe6060f1SDimitry Andric // (BW - 1) - (Z % BW) -> ~Z & (BW - 1) 5790fe6060f1SDimitry Andric auto NotZ = MIRBuilder.buildNot(ShTy, Z); 5791fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildAnd(ShTy, NotZ, Mask).getReg(0); 5792fe6060f1SDimitry Andric } else { 5793fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(ShTy, BW); 5794fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildURem(ShTy, Z, BitWidthC).getReg(0); 5795fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildSub(ShTy, Mask, ShAmt).getReg(0); 5796fe6060f1SDimitry Andric } 5797fe6060f1SDimitry Andric 5798fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(ShTy, 1); 5799fe6060f1SDimitry Andric if (IsFSHL) { 5800fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, X, ShAmt).getReg(0); 5801fe6060f1SDimitry Andric auto ShY1 = MIRBuilder.buildLShr(Ty, Y, One); 5802fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, ShY1, InvShAmt).getReg(0); 5803fe6060f1SDimitry Andric } else { 5804fe6060f1SDimitry Andric auto ShX1 = MIRBuilder.buildShl(Ty, X, One); 5805fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, ShX1, InvShAmt).getReg(0); 5806fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, Y, ShAmt).getReg(0); 5807fe6060f1SDimitry Andric } 5808fe6060f1SDimitry Andric } 5809fe6060f1SDimitry Andric 5810fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, ShX, ShY); 5811fe6060f1SDimitry Andric MI.eraseFromParent(); 5812fe6060f1SDimitry Andric return Legalized; 5813fe6060f1SDimitry Andric } 5814fe6060f1SDimitry Andric 5815fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5816fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShift(MachineInstr &MI) { 5817fe6060f1SDimitry Andric // These operations approximately do the following (while avoiding undefined 5818fe6060f1SDimitry Andric // shifts by BW): 5819fe6060f1SDimitry Andric // G_FSHL: (X << (Z % BW)) | (Y >> (BW - (Z % BW))) 5820fe6060f1SDimitry Andric // G_FSHR: (X << (BW - (Z % BW))) | (Y >> (Z % BW)) 5821fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5822fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 5823fe6060f1SDimitry Andric LLT ShTy = MRI.getType(MI.getOperand(3).getReg()); 5824fe6060f1SDimitry Andric 5825fe6060f1SDimitry Andric bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 5826fe6060f1SDimitry Andric unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; 5827fe6060f1SDimitry Andric 5828fe6060f1SDimitry Andric // TODO: Use smarter heuristic that accounts for vector legalization. 5829fe6060f1SDimitry Andric if (LI.getAction({RevOpcode, {Ty, ShTy}}).Action == Lower) 5830fe6060f1SDimitry Andric return lowerFunnelShiftAsShifts(MI); 5831fe6060f1SDimitry Andric 5832fe6060f1SDimitry Andric // This only works for powers of 2, fallback to shifts if it fails. 5833fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult Result = lowerFunnelShiftWithInverse(MI); 5834fe6060f1SDimitry Andric if (Result == UnableToLegalize) 5835fe6060f1SDimitry Andric return lowerFunnelShiftAsShifts(MI); 5836fe6060f1SDimitry Andric return Result; 5837fe6060f1SDimitry Andric } 5838fe6060f1SDimitry Andric 5839fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5840fe6060f1SDimitry Andric LegalizerHelper::lowerRotateWithReverseRotate(MachineInstr &MI) { 5841fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5842fe6060f1SDimitry Andric Register Src = MI.getOperand(1).getReg(); 5843fe6060f1SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 5844fe6060f1SDimitry Andric LLT AmtTy = MRI.getType(Amt); 5845fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(AmtTy, 0); 5846fe6060f1SDimitry Andric bool IsLeft = MI.getOpcode() == TargetOpcode::G_ROTL; 5847fe6060f1SDimitry Andric unsigned RevRot = IsLeft ? TargetOpcode::G_ROTR : TargetOpcode::G_ROTL; 5848fe6060f1SDimitry Andric auto Neg = MIRBuilder.buildSub(AmtTy, Zero, Amt); 5849fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevRot, {Dst}, {Src, Neg}); 5850fe6060f1SDimitry Andric MI.eraseFromParent(); 5851fe6060f1SDimitry Andric return Legalized; 5852fe6060f1SDimitry Andric } 5853fe6060f1SDimitry Andric 5854fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerRotate(MachineInstr &MI) { 5855fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5856fe6060f1SDimitry Andric Register Src = MI.getOperand(1).getReg(); 5857fe6060f1SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 5858fe6060f1SDimitry Andric LLT DstTy = MRI.getType(Dst); 5859349cc55cSDimitry Andric LLT SrcTy = MRI.getType(Src); 5860fe6060f1SDimitry Andric LLT AmtTy = MRI.getType(Amt); 5861fe6060f1SDimitry Andric 5862fe6060f1SDimitry Andric unsigned EltSizeInBits = DstTy.getScalarSizeInBits(); 5863fe6060f1SDimitry Andric bool IsLeft = MI.getOpcode() == TargetOpcode::G_ROTL; 5864fe6060f1SDimitry Andric 5865fe6060f1SDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 5866fe6060f1SDimitry Andric 5867fe6060f1SDimitry Andric // If a rotate in the other direction is supported, use it. 5868fe6060f1SDimitry Andric unsigned RevRot = IsLeft ? TargetOpcode::G_ROTR : TargetOpcode::G_ROTL; 5869fe6060f1SDimitry Andric if (LI.isLegalOrCustom({RevRot, {DstTy, SrcTy}}) && 5870fe6060f1SDimitry Andric isPowerOf2_32(EltSizeInBits)) 5871fe6060f1SDimitry Andric return lowerRotateWithReverseRotate(MI); 5872fe6060f1SDimitry Andric 5873349cc55cSDimitry Andric // If a funnel shift is supported, use it. 5874349cc55cSDimitry Andric unsigned FShOpc = IsLeft ? TargetOpcode::G_FSHL : TargetOpcode::G_FSHR; 5875349cc55cSDimitry Andric unsigned RevFsh = !IsLeft ? TargetOpcode::G_FSHL : TargetOpcode::G_FSHR; 5876349cc55cSDimitry Andric bool IsFShLegal = false; 5877349cc55cSDimitry Andric if ((IsFShLegal = LI.isLegalOrCustom({FShOpc, {DstTy, AmtTy}})) || 5878349cc55cSDimitry Andric LI.isLegalOrCustom({RevFsh, {DstTy, AmtTy}})) { 5879349cc55cSDimitry Andric auto buildFunnelShift = [&](unsigned Opc, Register R1, Register R2, 5880349cc55cSDimitry Andric Register R3) { 5881349cc55cSDimitry Andric MIRBuilder.buildInstr(Opc, {R1}, {R2, R2, R3}); 5882349cc55cSDimitry Andric MI.eraseFromParent(); 5883349cc55cSDimitry Andric return Legalized; 5884349cc55cSDimitry Andric }; 5885349cc55cSDimitry Andric // If a funnel shift in the other direction is supported, use it. 5886349cc55cSDimitry Andric if (IsFShLegal) { 5887349cc55cSDimitry Andric return buildFunnelShift(FShOpc, Dst, Src, Amt); 5888349cc55cSDimitry Andric } else if (isPowerOf2_32(EltSizeInBits)) { 5889349cc55cSDimitry Andric Amt = MIRBuilder.buildNeg(DstTy, Amt).getReg(0); 5890349cc55cSDimitry Andric return buildFunnelShift(RevFsh, Dst, Src, Amt); 5891349cc55cSDimitry Andric } 5892349cc55cSDimitry Andric } 5893349cc55cSDimitry Andric 5894fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(AmtTy, 0); 5895fe6060f1SDimitry Andric unsigned ShOpc = IsLeft ? TargetOpcode::G_SHL : TargetOpcode::G_LSHR; 5896fe6060f1SDimitry Andric unsigned RevShiftOpc = IsLeft ? TargetOpcode::G_LSHR : TargetOpcode::G_SHL; 5897fe6060f1SDimitry Andric auto BitWidthMinusOneC = MIRBuilder.buildConstant(AmtTy, EltSizeInBits - 1); 5898fe6060f1SDimitry Andric Register ShVal; 5899fe6060f1SDimitry Andric Register RevShiftVal; 5900fe6060f1SDimitry Andric if (isPowerOf2_32(EltSizeInBits)) { 5901fe6060f1SDimitry Andric // (rotl x, c) -> x << (c & (w - 1)) | x >> (-c & (w - 1)) 5902fe6060f1SDimitry Andric // (rotr x, c) -> x >> (c & (w - 1)) | x << (-c & (w - 1)) 5903fe6060f1SDimitry Andric auto NegAmt = MIRBuilder.buildSub(AmtTy, Zero, Amt); 5904fe6060f1SDimitry Andric auto ShAmt = MIRBuilder.buildAnd(AmtTy, Amt, BitWidthMinusOneC); 5905fe6060f1SDimitry Andric ShVal = MIRBuilder.buildInstr(ShOpc, {DstTy}, {Src, ShAmt}).getReg(0); 5906fe6060f1SDimitry Andric auto RevAmt = MIRBuilder.buildAnd(AmtTy, NegAmt, BitWidthMinusOneC); 5907fe6060f1SDimitry Andric RevShiftVal = 5908fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Src, RevAmt}).getReg(0); 5909fe6060f1SDimitry Andric } else { 5910fe6060f1SDimitry Andric // (rotl x, c) -> x << (c % w) | x >> 1 >> (w - 1 - (c % w)) 5911fe6060f1SDimitry Andric // (rotr x, c) -> x >> (c % w) | x << 1 << (w - 1 - (c % w)) 5912fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(AmtTy, EltSizeInBits); 5913fe6060f1SDimitry Andric auto ShAmt = MIRBuilder.buildURem(AmtTy, Amt, BitWidthC); 5914fe6060f1SDimitry Andric ShVal = MIRBuilder.buildInstr(ShOpc, {DstTy}, {Src, ShAmt}).getReg(0); 5915fe6060f1SDimitry Andric auto RevAmt = MIRBuilder.buildSub(AmtTy, BitWidthMinusOneC, ShAmt); 5916fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(AmtTy, 1); 5917fe6060f1SDimitry Andric auto Inner = MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Src, One}); 5918fe6060f1SDimitry Andric RevShiftVal = 5919fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Inner, RevAmt}).getReg(0); 5920fe6060f1SDimitry Andric } 5921fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, ShVal, RevShiftVal); 5922fe6060f1SDimitry Andric MI.eraseFromParent(); 5923fe6060f1SDimitry Andric return Legalized; 5924fe6060f1SDimitry Andric } 5925fe6060f1SDimitry Andric 59260b57cec5SDimitry Andric // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float 59270b57cec5SDimitry Andric // representation. 59280b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 59290b57cec5SDimitry Andric LegalizerHelper::lowerU64ToF32BitOps(MachineInstr &MI) { 59300b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 59310b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 59320b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 59330b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 59340b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 59350b57cec5SDimitry Andric 59360b57cec5SDimitry Andric assert(MRI.getType(Src) == S64 && MRI.getType(Dst) == S32); 59370b57cec5SDimitry Andric 59380b57cec5SDimitry Andric // unsigned cul2f(ulong u) { 59390b57cec5SDimitry Andric // uint lz = clz(u); 59400b57cec5SDimitry Andric // uint e = (u != 0) ? 127U + 63U - lz : 0; 59410b57cec5SDimitry Andric // u = (u << lz) & 0x7fffffffffffffffUL; 59420b57cec5SDimitry Andric // ulong t = u & 0xffffffffffUL; 59430b57cec5SDimitry Andric // uint v = (e << 23) | (uint)(u >> 40); 59440b57cec5SDimitry Andric // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 59450b57cec5SDimitry Andric // return as_float(v + r); 59460b57cec5SDimitry Andric // } 59470b57cec5SDimitry Andric 59480b57cec5SDimitry Andric auto Zero32 = MIRBuilder.buildConstant(S32, 0); 59490b57cec5SDimitry Andric auto Zero64 = MIRBuilder.buildConstant(S64, 0); 59500b57cec5SDimitry Andric 59510b57cec5SDimitry Andric auto LZ = MIRBuilder.buildCTLZ_ZERO_UNDEF(S32, Src); 59520b57cec5SDimitry Andric 59530b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant(S32, 127U + 63U); 59540b57cec5SDimitry Andric auto Sub = MIRBuilder.buildSub(S32, K, LZ); 59550b57cec5SDimitry Andric 59560b57cec5SDimitry Andric auto NotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, Src, Zero64); 59570b57cec5SDimitry Andric auto E = MIRBuilder.buildSelect(S32, NotZero, Sub, Zero32); 59580b57cec5SDimitry Andric 59590b57cec5SDimitry Andric auto Mask0 = MIRBuilder.buildConstant(S64, (-1ULL) >> 1); 59600b57cec5SDimitry Andric auto ShlLZ = MIRBuilder.buildShl(S64, Src, LZ); 59610b57cec5SDimitry Andric 59620b57cec5SDimitry Andric auto U = MIRBuilder.buildAnd(S64, ShlLZ, Mask0); 59630b57cec5SDimitry Andric 59640b57cec5SDimitry Andric auto Mask1 = MIRBuilder.buildConstant(S64, 0xffffffffffULL); 59650b57cec5SDimitry Andric auto T = MIRBuilder.buildAnd(S64, U, Mask1); 59660b57cec5SDimitry Andric 59670b57cec5SDimitry Andric auto UShl = MIRBuilder.buildLShr(S64, U, MIRBuilder.buildConstant(S64, 40)); 59680b57cec5SDimitry Andric auto ShlE = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 23)); 59690b57cec5SDimitry Andric auto V = MIRBuilder.buildOr(S32, ShlE, MIRBuilder.buildTrunc(S32, UShl)); 59700b57cec5SDimitry Andric 59710b57cec5SDimitry Andric auto C = MIRBuilder.buildConstant(S64, 0x8000000000ULL); 59720b57cec5SDimitry Andric auto RCmp = MIRBuilder.buildICmp(CmpInst::ICMP_UGT, S1, T, C); 59730b57cec5SDimitry Andric auto TCmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, T, C); 59740b57cec5SDimitry Andric auto One = MIRBuilder.buildConstant(S32, 1); 59750b57cec5SDimitry Andric 59760b57cec5SDimitry Andric auto VTrunc1 = MIRBuilder.buildAnd(S32, V, One); 59770b57cec5SDimitry Andric auto Select0 = MIRBuilder.buildSelect(S32, TCmp, VTrunc1, Zero32); 59780b57cec5SDimitry Andric auto R = MIRBuilder.buildSelect(S32, RCmp, One, Select0); 59790b57cec5SDimitry Andric MIRBuilder.buildAdd(Dst, V, R); 59800b57cec5SDimitry Andric 59815ffd83dbSDimitry Andric MI.eraseFromParent(); 59820b57cec5SDimitry Andric return Legalized; 59830b57cec5SDimitry Andric } 59840b57cec5SDimitry Andric 5985e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerUITOFP(MachineInstr &MI) { 59860b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 59870b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 59880b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 59890b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 59900b57cec5SDimitry Andric 5991480093f4SDimitry Andric if (SrcTy == LLT::scalar(1)) { 5992480093f4SDimitry Andric auto True = MIRBuilder.buildFConstant(DstTy, 1.0); 5993480093f4SDimitry Andric auto False = MIRBuilder.buildFConstant(DstTy, 0.0); 5994480093f4SDimitry Andric MIRBuilder.buildSelect(Dst, Src, True, False); 5995480093f4SDimitry Andric MI.eraseFromParent(); 5996480093f4SDimitry Andric return Legalized; 5997480093f4SDimitry Andric } 5998480093f4SDimitry Andric 59990b57cec5SDimitry Andric if (SrcTy != LLT::scalar(64)) 60000b57cec5SDimitry Andric return UnableToLegalize; 60010b57cec5SDimitry Andric 60020b57cec5SDimitry Andric if (DstTy == LLT::scalar(32)) { 60030b57cec5SDimitry Andric // TODO: SelectionDAG has several alternative expansions to port which may 60040b57cec5SDimitry Andric // be more reasonble depending on the available instructions. If a target 60050b57cec5SDimitry Andric // has sitofp, does not have CTLZ, or can efficiently use f64 as an 60060b57cec5SDimitry Andric // intermediate type, this is probably worse. 60070b57cec5SDimitry Andric return lowerU64ToF32BitOps(MI); 60080b57cec5SDimitry Andric } 60090b57cec5SDimitry Andric 60100b57cec5SDimitry Andric return UnableToLegalize; 60110b57cec5SDimitry Andric } 60120b57cec5SDimitry Andric 6013e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerSITOFP(MachineInstr &MI) { 60140b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 60150b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 60160b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 60170b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 60180b57cec5SDimitry Andric 60190b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 60200b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 60210b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 60220b57cec5SDimitry Andric 6023480093f4SDimitry Andric if (SrcTy == S1) { 6024480093f4SDimitry Andric auto True = MIRBuilder.buildFConstant(DstTy, -1.0); 6025480093f4SDimitry Andric auto False = MIRBuilder.buildFConstant(DstTy, 0.0); 6026480093f4SDimitry Andric MIRBuilder.buildSelect(Dst, Src, True, False); 6027480093f4SDimitry Andric MI.eraseFromParent(); 6028480093f4SDimitry Andric return Legalized; 6029480093f4SDimitry Andric } 6030480093f4SDimitry Andric 60310b57cec5SDimitry Andric if (SrcTy != S64) 60320b57cec5SDimitry Andric return UnableToLegalize; 60330b57cec5SDimitry Andric 60340b57cec5SDimitry Andric if (DstTy == S32) { 60350b57cec5SDimitry Andric // signed cl2f(long l) { 60360b57cec5SDimitry Andric // long s = l >> 63; 60370b57cec5SDimitry Andric // float r = cul2f((l + s) ^ s); 60380b57cec5SDimitry Andric // return s ? -r : r; 60390b57cec5SDimitry Andric // } 60400b57cec5SDimitry Andric Register L = Src; 60410b57cec5SDimitry Andric auto SignBit = MIRBuilder.buildConstant(S64, 63); 60420b57cec5SDimitry Andric auto S = MIRBuilder.buildAShr(S64, L, SignBit); 60430b57cec5SDimitry Andric 60440b57cec5SDimitry Andric auto LPlusS = MIRBuilder.buildAdd(S64, L, S); 60450b57cec5SDimitry Andric auto Xor = MIRBuilder.buildXor(S64, LPlusS, S); 60460b57cec5SDimitry Andric auto R = MIRBuilder.buildUITOFP(S32, Xor); 60470b57cec5SDimitry Andric 60480b57cec5SDimitry Andric auto RNeg = MIRBuilder.buildFNeg(S32, R); 60490b57cec5SDimitry Andric auto SignNotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, S, 60500b57cec5SDimitry Andric MIRBuilder.buildConstant(S64, 0)); 60510b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, SignNotZero, RNeg, R); 60525ffd83dbSDimitry Andric MI.eraseFromParent(); 60530b57cec5SDimitry Andric return Legalized; 60540b57cec5SDimitry Andric } 60550b57cec5SDimitry Andric 60560b57cec5SDimitry Andric return UnableToLegalize; 60570b57cec5SDimitry Andric } 60580b57cec5SDimitry Andric 6059e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOUI(MachineInstr &MI) { 60608bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 60618bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 60628bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 60638bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 60648bcb0991SDimitry Andric const LLT S64 = LLT::scalar(64); 60658bcb0991SDimitry Andric const LLT S32 = LLT::scalar(32); 60668bcb0991SDimitry Andric 60678bcb0991SDimitry Andric if (SrcTy != S64 && SrcTy != S32) 60688bcb0991SDimitry Andric return UnableToLegalize; 60698bcb0991SDimitry Andric if (DstTy != S32 && DstTy != S64) 60708bcb0991SDimitry Andric return UnableToLegalize; 60718bcb0991SDimitry Andric 60728bcb0991SDimitry Andric // FPTOSI gives same result as FPTOUI for positive signed integers. 60738bcb0991SDimitry Andric // FPTOUI needs to deal with fp values that convert to unsigned integers 60748bcb0991SDimitry Andric // greater or equal to 2^31 for float or 2^63 for double. For brevity 2^Exp. 60758bcb0991SDimitry Andric 60768bcb0991SDimitry Andric APInt TwoPExpInt = APInt::getSignMask(DstTy.getSizeInBits()); 60778bcb0991SDimitry Andric APFloat TwoPExpFP(SrcTy.getSizeInBits() == 32 ? APFloat::IEEEsingle() 60788bcb0991SDimitry Andric : APFloat::IEEEdouble(), 6079349cc55cSDimitry Andric APInt::getZero(SrcTy.getSizeInBits())); 60808bcb0991SDimitry Andric TwoPExpFP.convertFromAPInt(TwoPExpInt, false, APFloat::rmNearestTiesToEven); 60818bcb0991SDimitry Andric 60828bcb0991SDimitry Andric MachineInstrBuilder FPTOSI = MIRBuilder.buildFPTOSI(DstTy, Src); 60838bcb0991SDimitry Andric 60848bcb0991SDimitry Andric MachineInstrBuilder Threshold = MIRBuilder.buildFConstant(SrcTy, TwoPExpFP); 60858bcb0991SDimitry Andric // For fp Value greater or equal to Threshold(2^Exp), we use FPTOSI on 60868bcb0991SDimitry Andric // (Value - 2^Exp) and add 2^Exp by setting highest bit in result to 1. 60878bcb0991SDimitry Andric MachineInstrBuilder FSub = MIRBuilder.buildFSub(SrcTy, Src, Threshold); 60888bcb0991SDimitry Andric MachineInstrBuilder ResLowBits = MIRBuilder.buildFPTOSI(DstTy, FSub); 60898bcb0991SDimitry Andric MachineInstrBuilder ResHighBit = MIRBuilder.buildConstant(DstTy, TwoPExpInt); 60908bcb0991SDimitry Andric MachineInstrBuilder Res = MIRBuilder.buildXor(DstTy, ResLowBits, ResHighBit); 60918bcb0991SDimitry Andric 6092480093f4SDimitry Andric const LLT S1 = LLT::scalar(1); 6093480093f4SDimitry Andric 60948bcb0991SDimitry Andric MachineInstrBuilder FCMP = 6095480093f4SDimitry Andric MIRBuilder.buildFCmp(CmpInst::FCMP_ULT, S1, Src, Threshold); 60968bcb0991SDimitry Andric MIRBuilder.buildSelect(Dst, FCMP, FPTOSI, Res); 60978bcb0991SDimitry Andric 60988bcb0991SDimitry Andric MI.eraseFromParent(); 60998bcb0991SDimitry Andric return Legalized; 61008bcb0991SDimitry Andric } 61018bcb0991SDimitry Andric 61025ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOSI(MachineInstr &MI) { 61035ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 61045ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 61055ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 61065ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 61075ffd83dbSDimitry Andric const LLT S64 = LLT::scalar(64); 61085ffd83dbSDimitry Andric const LLT S32 = LLT::scalar(32); 61095ffd83dbSDimitry Andric 61105ffd83dbSDimitry Andric // FIXME: Only f32 to i64 conversions are supported. 61115ffd83dbSDimitry Andric if (SrcTy.getScalarType() != S32 || DstTy.getScalarType() != S64) 61125ffd83dbSDimitry Andric return UnableToLegalize; 61135ffd83dbSDimitry Andric 61145ffd83dbSDimitry Andric // Expand f32 -> i64 conversion 61155ffd83dbSDimitry Andric // This algorithm comes from compiler-rt's implementation of fixsfdi: 6116fe6060f1SDimitry Andric // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/fixsfdi.c 61175ffd83dbSDimitry Andric 61185ffd83dbSDimitry Andric unsigned SrcEltBits = SrcTy.getScalarSizeInBits(); 61195ffd83dbSDimitry Andric 61205ffd83dbSDimitry Andric auto ExponentMask = MIRBuilder.buildConstant(SrcTy, 0x7F800000); 61215ffd83dbSDimitry Andric auto ExponentLoBit = MIRBuilder.buildConstant(SrcTy, 23); 61225ffd83dbSDimitry Andric 61235ffd83dbSDimitry Andric auto AndExpMask = MIRBuilder.buildAnd(SrcTy, Src, ExponentMask); 61245ffd83dbSDimitry Andric auto ExponentBits = MIRBuilder.buildLShr(SrcTy, AndExpMask, ExponentLoBit); 61255ffd83dbSDimitry Andric 61265ffd83dbSDimitry Andric auto SignMask = MIRBuilder.buildConstant(SrcTy, 61275ffd83dbSDimitry Andric APInt::getSignMask(SrcEltBits)); 61285ffd83dbSDimitry Andric auto AndSignMask = MIRBuilder.buildAnd(SrcTy, Src, SignMask); 61295ffd83dbSDimitry Andric auto SignLowBit = MIRBuilder.buildConstant(SrcTy, SrcEltBits - 1); 61305ffd83dbSDimitry Andric auto Sign = MIRBuilder.buildAShr(SrcTy, AndSignMask, SignLowBit); 61315ffd83dbSDimitry Andric Sign = MIRBuilder.buildSExt(DstTy, Sign); 61325ffd83dbSDimitry Andric 61335ffd83dbSDimitry Andric auto MantissaMask = MIRBuilder.buildConstant(SrcTy, 0x007FFFFF); 61345ffd83dbSDimitry Andric auto AndMantissaMask = MIRBuilder.buildAnd(SrcTy, Src, MantissaMask); 61355ffd83dbSDimitry Andric auto K = MIRBuilder.buildConstant(SrcTy, 0x00800000); 61365ffd83dbSDimitry Andric 61375ffd83dbSDimitry Andric auto R = MIRBuilder.buildOr(SrcTy, AndMantissaMask, K); 61385ffd83dbSDimitry Andric R = MIRBuilder.buildZExt(DstTy, R); 61395ffd83dbSDimitry Andric 61405ffd83dbSDimitry Andric auto Bias = MIRBuilder.buildConstant(SrcTy, 127); 61415ffd83dbSDimitry Andric auto Exponent = MIRBuilder.buildSub(SrcTy, ExponentBits, Bias); 61425ffd83dbSDimitry Andric auto SubExponent = MIRBuilder.buildSub(SrcTy, Exponent, ExponentLoBit); 61435ffd83dbSDimitry Andric auto ExponentSub = MIRBuilder.buildSub(SrcTy, ExponentLoBit, Exponent); 61445ffd83dbSDimitry Andric 61455ffd83dbSDimitry Andric auto Shl = MIRBuilder.buildShl(DstTy, R, SubExponent); 61465ffd83dbSDimitry Andric auto Srl = MIRBuilder.buildLShr(DstTy, R, ExponentSub); 61475ffd83dbSDimitry Andric 61485ffd83dbSDimitry Andric const LLT S1 = LLT::scalar(1); 61495ffd83dbSDimitry Andric auto CmpGt = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, 61505ffd83dbSDimitry Andric S1, Exponent, ExponentLoBit); 61515ffd83dbSDimitry Andric 61525ffd83dbSDimitry Andric R = MIRBuilder.buildSelect(DstTy, CmpGt, Shl, Srl); 61535ffd83dbSDimitry Andric 61545ffd83dbSDimitry Andric auto XorSign = MIRBuilder.buildXor(DstTy, R, Sign); 61555ffd83dbSDimitry Andric auto Ret = MIRBuilder.buildSub(DstTy, XorSign, Sign); 61565ffd83dbSDimitry Andric 61575ffd83dbSDimitry Andric auto ZeroSrcTy = MIRBuilder.buildConstant(SrcTy, 0); 61585ffd83dbSDimitry Andric 61595ffd83dbSDimitry Andric auto ExponentLt0 = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, 61605ffd83dbSDimitry Andric S1, Exponent, ZeroSrcTy); 61615ffd83dbSDimitry Andric 61625ffd83dbSDimitry Andric auto ZeroDstTy = MIRBuilder.buildConstant(DstTy, 0); 61635ffd83dbSDimitry Andric MIRBuilder.buildSelect(Dst, ExponentLt0, ZeroDstTy, Ret); 61645ffd83dbSDimitry Andric 61655ffd83dbSDimitry Andric MI.eraseFromParent(); 61665ffd83dbSDimitry Andric return Legalized; 61675ffd83dbSDimitry Andric } 61685ffd83dbSDimitry Andric 61695ffd83dbSDimitry Andric // f64 -> f16 conversion using round-to-nearest-even rounding mode. 61705ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 61715ffd83dbSDimitry Andric LegalizerHelper::lowerFPTRUNC_F64_TO_F16(MachineInstr &MI) { 61725ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 61735ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 61745ffd83dbSDimitry Andric 61755ffd83dbSDimitry Andric if (MRI.getType(Src).isVector()) // TODO: Handle vectors directly. 61765ffd83dbSDimitry Andric return UnableToLegalize; 61775ffd83dbSDimitry Andric 61785ffd83dbSDimitry Andric const unsigned ExpMask = 0x7ff; 61795ffd83dbSDimitry Andric const unsigned ExpBiasf64 = 1023; 61805ffd83dbSDimitry Andric const unsigned ExpBiasf16 = 15; 61815ffd83dbSDimitry Andric const LLT S32 = LLT::scalar(32); 61825ffd83dbSDimitry Andric const LLT S1 = LLT::scalar(1); 61835ffd83dbSDimitry Andric 61845ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(S32, Src); 61855ffd83dbSDimitry Andric Register U = Unmerge.getReg(0); 61865ffd83dbSDimitry Andric Register UH = Unmerge.getReg(1); 61875ffd83dbSDimitry Andric 61885ffd83dbSDimitry Andric auto E = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 20)); 61895ffd83dbSDimitry Andric E = MIRBuilder.buildAnd(S32, E, MIRBuilder.buildConstant(S32, ExpMask)); 61905ffd83dbSDimitry Andric 61915ffd83dbSDimitry Andric // Subtract the fp64 exponent bias (1023) to get the real exponent and 61925ffd83dbSDimitry Andric // add the f16 bias (15) to get the biased exponent for the f16 format. 61935ffd83dbSDimitry Andric E = MIRBuilder.buildAdd( 61945ffd83dbSDimitry Andric S32, E, MIRBuilder.buildConstant(S32, -ExpBiasf64 + ExpBiasf16)); 61955ffd83dbSDimitry Andric 61965ffd83dbSDimitry Andric auto M = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 8)); 61975ffd83dbSDimitry Andric M = MIRBuilder.buildAnd(S32, M, MIRBuilder.buildConstant(S32, 0xffe)); 61985ffd83dbSDimitry Andric 61995ffd83dbSDimitry Andric auto MaskedSig = MIRBuilder.buildAnd(S32, UH, 62005ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x1ff)); 62015ffd83dbSDimitry Andric MaskedSig = MIRBuilder.buildOr(S32, MaskedSig, U); 62025ffd83dbSDimitry Andric 62035ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildConstant(S32, 0); 62045ffd83dbSDimitry Andric auto SigCmpNE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, MaskedSig, Zero); 62055ffd83dbSDimitry Andric auto Lo40Set = MIRBuilder.buildZExt(S32, SigCmpNE0); 62065ffd83dbSDimitry Andric M = MIRBuilder.buildOr(S32, M, Lo40Set); 62075ffd83dbSDimitry Andric 62085ffd83dbSDimitry Andric // (M != 0 ? 0x0200 : 0) | 0x7c00; 62095ffd83dbSDimitry Andric auto Bits0x200 = MIRBuilder.buildConstant(S32, 0x0200); 62105ffd83dbSDimitry Andric auto CmpM_NE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, M, Zero); 62115ffd83dbSDimitry Andric auto SelectCC = MIRBuilder.buildSelect(S32, CmpM_NE0, Bits0x200, Zero); 62125ffd83dbSDimitry Andric 62135ffd83dbSDimitry Andric auto Bits0x7c00 = MIRBuilder.buildConstant(S32, 0x7c00); 62145ffd83dbSDimitry Andric auto I = MIRBuilder.buildOr(S32, SelectCC, Bits0x7c00); 62155ffd83dbSDimitry Andric 62165ffd83dbSDimitry Andric // N = M | (E << 12); 62175ffd83dbSDimitry Andric auto EShl12 = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 12)); 62185ffd83dbSDimitry Andric auto N = MIRBuilder.buildOr(S32, M, EShl12); 62195ffd83dbSDimitry Andric 62205ffd83dbSDimitry Andric // B = clamp(1-E, 0, 13); 62215ffd83dbSDimitry Andric auto One = MIRBuilder.buildConstant(S32, 1); 62225ffd83dbSDimitry Andric auto OneSubExp = MIRBuilder.buildSub(S32, One, E); 62235ffd83dbSDimitry Andric auto B = MIRBuilder.buildSMax(S32, OneSubExp, Zero); 62245ffd83dbSDimitry Andric B = MIRBuilder.buildSMin(S32, B, MIRBuilder.buildConstant(S32, 13)); 62255ffd83dbSDimitry Andric 62265ffd83dbSDimitry Andric auto SigSetHigh = MIRBuilder.buildOr(S32, M, 62275ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x1000)); 62285ffd83dbSDimitry Andric 62295ffd83dbSDimitry Andric auto D = MIRBuilder.buildLShr(S32, SigSetHigh, B); 62305ffd83dbSDimitry Andric auto D0 = MIRBuilder.buildShl(S32, D, B); 62315ffd83dbSDimitry Andric 62325ffd83dbSDimitry Andric auto D0_NE_SigSetHigh = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, 62335ffd83dbSDimitry Andric D0, SigSetHigh); 62345ffd83dbSDimitry Andric auto D1 = MIRBuilder.buildZExt(S32, D0_NE_SigSetHigh); 62355ffd83dbSDimitry Andric D = MIRBuilder.buildOr(S32, D, D1); 62365ffd83dbSDimitry Andric 62375ffd83dbSDimitry Andric auto CmpELtOne = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, S1, E, One); 62385ffd83dbSDimitry Andric auto V = MIRBuilder.buildSelect(S32, CmpELtOne, D, N); 62395ffd83dbSDimitry Andric 62405ffd83dbSDimitry Andric auto VLow3 = MIRBuilder.buildAnd(S32, V, MIRBuilder.buildConstant(S32, 7)); 62415ffd83dbSDimitry Andric V = MIRBuilder.buildLShr(S32, V, MIRBuilder.buildConstant(S32, 2)); 62425ffd83dbSDimitry Andric 62435ffd83dbSDimitry Andric auto VLow3Eq3 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, VLow3, 62445ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 3)); 62455ffd83dbSDimitry Andric auto V0 = MIRBuilder.buildZExt(S32, VLow3Eq3); 62465ffd83dbSDimitry Andric 62475ffd83dbSDimitry Andric auto VLow3Gt5 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, S1, VLow3, 62485ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 5)); 62495ffd83dbSDimitry Andric auto V1 = MIRBuilder.buildZExt(S32, VLow3Gt5); 62505ffd83dbSDimitry Andric 62515ffd83dbSDimitry Andric V1 = MIRBuilder.buildOr(S32, V0, V1); 62525ffd83dbSDimitry Andric V = MIRBuilder.buildAdd(S32, V, V1); 62535ffd83dbSDimitry Andric 62545ffd83dbSDimitry Andric auto CmpEGt30 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, S1, 62555ffd83dbSDimitry Andric E, MIRBuilder.buildConstant(S32, 30)); 62565ffd83dbSDimitry Andric V = MIRBuilder.buildSelect(S32, CmpEGt30, 62575ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x7c00), V); 62585ffd83dbSDimitry Andric 62595ffd83dbSDimitry Andric auto CmpEGt1039 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, 62605ffd83dbSDimitry Andric E, MIRBuilder.buildConstant(S32, 1039)); 62615ffd83dbSDimitry Andric V = MIRBuilder.buildSelect(S32, CmpEGt1039, I, V); 62625ffd83dbSDimitry Andric 62635ffd83dbSDimitry Andric // Extract the sign bit. 62645ffd83dbSDimitry Andric auto Sign = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 16)); 62655ffd83dbSDimitry Andric Sign = MIRBuilder.buildAnd(S32, Sign, MIRBuilder.buildConstant(S32, 0x8000)); 62665ffd83dbSDimitry Andric 62675ffd83dbSDimitry Andric // Insert the sign bit 62685ffd83dbSDimitry Andric V = MIRBuilder.buildOr(S32, Sign, V); 62695ffd83dbSDimitry Andric 62705ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst, V); 62715ffd83dbSDimitry Andric MI.eraseFromParent(); 62725ffd83dbSDimitry Andric return Legalized; 62735ffd83dbSDimitry Andric } 62745ffd83dbSDimitry Andric 62755ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 6276e8d8bef9SDimitry Andric LegalizerHelper::lowerFPTRUNC(MachineInstr &MI) { 62775ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 62785ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 62795ffd83dbSDimitry Andric 62805ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 62815ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 62825ffd83dbSDimitry Andric const LLT S64 = LLT::scalar(64); 62835ffd83dbSDimitry Andric const LLT S16 = LLT::scalar(16); 62845ffd83dbSDimitry Andric 62855ffd83dbSDimitry Andric if (DstTy.getScalarType() == S16 && SrcTy.getScalarType() == S64) 62865ffd83dbSDimitry Andric return lowerFPTRUNC_F64_TO_F16(MI); 62875ffd83dbSDimitry Andric 62885ffd83dbSDimitry Andric return UnableToLegalize; 62895ffd83dbSDimitry Andric } 62905ffd83dbSDimitry Andric 6291e8d8bef9SDimitry Andric // TODO: If RHS is a constant SelectionDAGBuilder expands this into a 6292e8d8bef9SDimitry Andric // multiplication tree. 6293e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPOWI(MachineInstr &MI) { 6294e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 6295e8d8bef9SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 6296e8d8bef9SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 6297e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Dst); 6298e8d8bef9SDimitry Andric 6299e8d8bef9SDimitry Andric auto CvtSrc1 = MIRBuilder.buildSITOFP(Ty, Src1); 6300e8d8bef9SDimitry Andric MIRBuilder.buildFPow(Dst, Src0, CvtSrc1, MI.getFlags()); 6301e8d8bef9SDimitry Andric MI.eraseFromParent(); 6302e8d8bef9SDimitry Andric return Legalized; 6303e8d8bef9SDimitry Andric } 6304e8d8bef9SDimitry Andric 63050b57cec5SDimitry Andric static CmpInst::Predicate minMaxToCompare(unsigned Opc) { 63060b57cec5SDimitry Andric switch (Opc) { 63070b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 63080b57cec5SDimitry Andric return CmpInst::ICMP_SLT; 63090b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 63100b57cec5SDimitry Andric return CmpInst::ICMP_SGT; 63110b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 63120b57cec5SDimitry Andric return CmpInst::ICMP_ULT; 63130b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 63140b57cec5SDimitry Andric return CmpInst::ICMP_UGT; 63150b57cec5SDimitry Andric default: 63160b57cec5SDimitry Andric llvm_unreachable("not in integer min/max"); 63170b57cec5SDimitry Andric } 63180b57cec5SDimitry Andric } 63190b57cec5SDimitry Andric 6320e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerMinMax(MachineInstr &MI) { 63210b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 63220b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 63230b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 63240b57cec5SDimitry Andric 63250b57cec5SDimitry Andric const CmpInst::Predicate Pred = minMaxToCompare(MI.getOpcode()); 63260b57cec5SDimitry Andric LLT CmpType = MRI.getType(Dst).changeElementSize(1); 63270b57cec5SDimitry Andric 63280b57cec5SDimitry Andric auto Cmp = MIRBuilder.buildICmp(Pred, CmpType, Src0, Src1); 63290b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, Cmp, Src0, Src1); 63300b57cec5SDimitry Andric 63310b57cec5SDimitry Andric MI.eraseFromParent(); 63320b57cec5SDimitry Andric return Legalized; 63330b57cec5SDimitry Andric } 63340b57cec5SDimitry Andric 63350b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 6336e8d8bef9SDimitry Andric LegalizerHelper::lowerFCopySign(MachineInstr &MI) { 63370b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 63380b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 63390b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 63400b57cec5SDimitry Andric 63410b57cec5SDimitry Andric const LLT Src0Ty = MRI.getType(Src0); 63420b57cec5SDimitry Andric const LLT Src1Ty = MRI.getType(Src1); 63430b57cec5SDimitry Andric 63440b57cec5SDimitry Andric const int Src0Size = Src0Ty.getScalarSizeInBits(); 63450b57cec5SDimitry Andric const int Src1Size = Src1Ty.getScalarSizeInBits(); 63460b57cec5SDimitry Andric 63470b57cec5SDimitry Andric auto SignBitMask = MIRBuilder.buildConstant( 63480b57cec5SDimitry Andric Src0Ty, APInt::getSignMask(Src0Size)); 63490b57cec5SDimitry Andric 63500b57cec5SDimitry Andric auto NotSignBitMask = MIRBuilder.buildConstant( 63510b57cec5SDimitry Andric Src0Ty, APInt::getLowBitsSet(Src0Size, Src0Size - 1)); 63520b57cec5SDimitry Andric 6353fe6060f1SDimitry Andric Register And0 = MIRBuilder.buildAnd(Src0Ty, Src0, NotSignBitMask).getReg(0); 6354fe6060f1SDimitry Andric Register And1; 63550b57cec5SDimitry Andric if (Src0Ty == Src1Ty) { 6356fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src1Ty, Src1, SignBitMask).getReg(0); 63570b57cec5SDimitry Andric } else if (Src0Size > Src1Size) { 63580b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src0Ty, Src0Size - Src1Size); 63590b57cec5SDimitry Andric auto Zext = MIRBuilder.buildZExt(Src0Ty, Src1); 63600b57cec5SDimitry Andric auto Shift = MIRBuilder.buildShl(Src0Ty, Zext, ShiftAmt); 6361fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src0Ty, Shift, SignBitMask).getReg(0); 63620b57cec5SDimitry Andric } else { 63630b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src1Ty, Src1Size - Src0Size); 63640b57cec5SDimitry Andric auto Shift = MIRBuilder.buildLShr(Src1Ty, Src1, ShiftAmt); 63650b57cec5SDimitry Andric auto Trunc = MIRBuilder.buildTrunc(Src0Ty, Shift); 6366fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src0Ty, Trunc, SignBitMask).getReg(0); 63670b57cec5SDimitry Andric } 63680b57cec5SDimitry Andric 63690b57cec5SDimitry Andric // Be careful about setting nsz/nnan/ninf on every instruction, since the 63700b57cec5SDimitry Andric // constants are a nan and -0.0, but the final result should preserve 63710b57cec5SDimitry Andric // everything. 6372fe6060f1SDimitry Andric unsigned Flags = MI.getFlags(); 6373fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, And0, And1, Flags); 63740b57cec5SDimitry Andric 63750b57cec5SDimitry Andric MI.eraseFromParent(); 63760b57cec5SDimitry Andric return Legalized; 63770b57cec5SDimitry Andric } 63780b57cec5SDimitry Andric 63790b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 63800b57cec5SDimitry Andric LegalizerHelper::lowerFMinNumMaxNum(MachineInstr &MI) { 63810b57cec5SDimitry Andric unsigned NewOp = MI.getOpcode() == TargetOpcode::G_FMINNUM ? 63820b57cec5SDimitry Andric TargetOpcode::G_FMINNUM_IEEE : TargetOpcode::G_FMAXNUM_IEEE; 63830b57cec5SDimitry Andric 63840b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 63850b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 63860b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 63870b57cec5SDimitry Andric LLT Ty = MRI.getType(Dst); 63880b57cec5SDimitry Andric 63890b57cec5SDimitry Andric if (!MI.getFlag(MachineInstr::FmNoNans)) { 63900b57cec5SDimitry Andric // Insert canonicalizes if it's possible we need to quiet to get correct 63910b57cec5SDimitry Andric // sNaN behavior. 63920b57cec5SDimitry Andric 63930b57cec5SDimitry Andric // Note this must be done here, and not as an optimization combine in the 63940b57cec5SDimitry Andric // absence of a dedicate quiet-snan instruction as we're using an 63950b57cec5SDimitry Andric // omni-purpose G_FCANONICALIZE. 63960b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src0, MRI)) 63970b57cec5SDimitry Andric Src0 = MIRBuilder.buildFCanonicalize(Ty, Src0, MI.getFlags()).getReg(0); 63980b57cec5SDimitry Andric 63990b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src1, MRI)) 64000b57cec5SDimitry Andric Src1 = MIRBuilder.buildFCanonicalize(Ty, Src1, MI.getFlags()).getReg(0); 64010b57cec5SDimitry Andric } 64020b57cec5SDimitry Andric 64030b57cec5SDimitry Andric // If there are no nans, it's safe to simply replace this with the non-IEEE 64040b57cec5SDimitry Andric // version. 64050b57cec5SDimitry Andric MIRBuilder.buildInstr(NewOp, {Dst}, {Src0, Src1}, MI.getFlags()); 64060b57cec5SDimitry Andric MI.eraseFromParent(); 64070b57cec5SDimitry Andric return Legalized; 64080b57cec5SDimitry Andric } 64098bcb0991SDimitry Andric 64108bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFMad(MachineInstr &MI) { 64118bcb0991SDimitry Andric // Expand G_FMAD a, b, c -> G_FADD (G_FMUL a, b), c 64128bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 64138bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 64148bcb0991SDimitry Andric unsigned Flags = MI.getFlags(); 64158bcb0991SDimitry Andric 64168bcb0991SDimitry Andric auto Mul = MIRBuilder.buildFMul(Ty, MI.getOperand(1), MI.getOperand(2), 64178bcb0991SDimitry Andric Flags); 64188bcb0991SDimitry Andric MIRBuilder.buildFAdd(DstReg, Mul, MI.getOperand(3), Flags); 64198bcb0991SDimitry Andric MI.eraseFromParent(); 64208bcb0991SDimitry Andric return Legalized; 64218bcb0991SDimitry Andric } 64228bcb0991SDimitry Andric 64238bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 6424480093f4SDimitry Andric LegalizerHelper::lowerIntrinsicRound(MachineInstr &MI) { 6425480093f4SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 64265ffd83dbSDimitry Andric Register X = MI.getOperand(1).getReg(); 64275ffd83dbSDimitry Andric const unsigned Flags = MI.getFlags(); 64285ffd83dbSDimitry Andric const LLT Ty = MRI.getType(DstReg); 64295ffd83dbSDimitry Andric const LLT CondTy = Ty.changeElementSize(1); 64305ffd83dbSDimitry Andric 64315ffd83dbSDimitry Andric // round(x) => 64325ffd83dbSDimitry Andric // t = trunc(x); 64335ffd83dbSDimitry Andric // d = fabs(x - t); 64345ffd83dbSDimitry Andric // o = copysign(1.0f, x); 64355ffd83dbSDimitry Andric // return t + (d >= 0.5 ? o : 0.0); 64365ffd83dbSDimitry Andric 64375ffd83dbSDimitry Andric auto T = MIRBuilder.buildIntrinsicTrunc(Ty, X, Flags); 64385ffd83dbSDimitry Andric 64395ffd83dbSDimitry Andric auto Diff = MIRBuilder.buildFSub(Ty, X, T, Flags); 64405ffd83dbSDimitry Andric auto AbsDiff = MIRBuilder.buildFAbs(Ty, Diff, Flags); 64415ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildFConstant(Ty, 0.0); 64425ffd83dbSDimitry Andric auto One = MIRBuilder.buildFConstant(Ty, 1.0); 64435ffd83dbSDimitry Andric auto Half = MIRBuilder.buildFConstant(Ty, 0.5); 64445ffd83dbSDimitry Andric auto SignOne = MIRBuilder.buildFCopysign(Ty, One, X); 64455ffd83dbSDimitry Andric 64465ffd83dbSDimitry Andric auto Cmp = MIRBuilder.buildFCmp(CmpInst::FCMP_OGE, CondTy, AbsDiff, Half, 64475ffd83dbSDimitry Andric Flags); 64485ffd83dbSDimitry Andric auto Sel = MIRBuilder.buildSelect(Ty, Cmp, SignOne, Zero, Flags); 64495ffd83dbSDimitry Andric 64505ffd83dbSDimitry Andric MIRBuilder.buildFAdd(DstReg, T, Sel, Flags); 64515ffd83dbSDimitry Andric 64525ffd83dbSDimitry Andric MI.eraseFromParent(); 64535ffd83dbSDimitry Andric return Legalized; 64545ffd83dbSDimitry Andric } 64555ffd83dbSDimitry Andric 64565ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 64575ffd83dbSDimitry Andric LegalizerHelper::lowerFFloor(MachineInstr &MI) { 64585ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 6459480093f4SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 6460480093f4SDimitry Andric unsigned Flags = MI.getFlags(); 6461480093f4SDimitry Andric LLT Ty = MRI.getType(DstReg); 6462480093f4SDimitry Andric const LLT CondTy = Ty.changeElementSize(1); 6463480093f4SDimitry Andric 6464480093f4SDimitry Andric // result = trunc(src); 6465480093f4SDimitry Andric // if (src < 0.0 && src != result) 6466480093f4SDimitry Andric // result += -1.0. 6467480093f4SDimitry Andric 6468480093f4SDimitry Andric auto Trunc = MIRBuilder.buildIntrinsicTrunc(Ty, SrcReg, Flags); 64695ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildFConstant(Ty, 0.0); 6470480093f4SDimitry Andric 6471480093f4SDimitry Andric auto Lt0 = MIRBuilder.buildFCmp(CmpInst::FCMP_OLT, CondTy, 6472480093f4SDimitry Andric SrcReg, Zero, Flags); 6473480093f4SDimitry Andric auto NeTrunc = MIRBuilder.buildFCmp(CmpInst::FCMP_ONE, CondTy, 6474480093f4SDimitry Andric SrcReg, Trunc, Flags); 6475480093f4SDimitry Andric auto And = MIRBuilder.buildAnd(CondTy, Lt0, NeTrunc); 6476480093f4SDimitry Andric auto AddVal = MIRBuilder.buildSITOFP(Ty, And); 6477480093f4SDimitry Andric 64785ffd83dbSDimitry Andric MIRBuilder.buildFAdd(DstReg, Trunc, AddVal, Flags); 64795ffd83dbSDimitry Andric MI.eraseFromParent(); 64805ffd83dbSDimitry Andric return Legalized; 64815ffd83dbSDimitry Andric } 64825ffd83dbSDimitry Andric 64835ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 64845ffd83dbSDimitry Andric LegalizerHelper::lowerMergeValues(MachineInstr &MI) { 64855ffd83dbSDimitry Andric const unsigned NumOps = MI.getNumOperands(); 64865ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 64875ffd83dbSDimitry Andric Register Src0Reg = MI.getOperand(1).getReg(); 64885ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 64895ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src0Reg); 64905ffd83dbSDimitry Andric unsigned PartSize = SrcTy.getSizeInBits(); 64915ffd83dbSDimitry Andric 64925ffd83dbSDimitry Andric LLT WideTy = LLT::scalar(DstTy.getSizeInBits()); 64935ffd83dbSDimitry Andric Register ResultReg = MIRBuilder.buildZExt(WideTy, Src0Reg).getReg(0); 64945ffd83dbSDimitry Andric 64955ffd83dbSDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 64965ffd83dbSDimitry Andric const unsigned Offset = (I - 1) * PartSize; 64975ffd83dbSDimitry Andric 64985ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 64995ffd83dbSDimitry Andric auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg); 65005ffd83dbSDimitry Andric 65015ffd83dbSDimitry Andric Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg : 65025ffd83dbSDimitry Andric MRI.createGenericVirtualRegister(WideTy); 65035ffd83dbSDimitry Andric 65045ffd83dbSDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset); 65055ffd83dbSDimitry Andric auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt); 65065ffd83dbSDimitry Andric MIRBuilder.buildOr(NextResult, ResultReg, Shl); 65075ffd83dbSDimitry Andric ResultReg = NextResult; 65085ffd83dbSDimitry Andric } 65095ffd83dbSDimitry Andric 65105ffd83dbSDimitry Andric if (DstTy.isPointer()) { 65115ffd83dbSDimitry Andric if (MIRBuilder.getDataLayout().isNonIntegralAddressSpace( 65125ffd83dbSDimitry Andric DstTy.getAddressSpace())) { 65135ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Not casting nonintegral address space\n"); 65145ffd83dbSDimitry Andric return UnableToLegalize; 65155ffd83dbSDimitry Andric } 65165ffd83dbSDimitry Andric 65175ffd83dbSDimitry Andric MIRBuilder.buildIntToPtr(DstReg, ResultReg); 65185ffd83dbSDimitry Andric } 65195ffd83dbSDimitry Andric 6520480093f4SDimitry Andric MI.eraseFromParent(); 6521480093f4SDimitry Andric return Legalized; 6522480093f4SDimitry Andric } 6523480093f4SDimitry Andric 6524480093f4SDimitry Andric LegalizerHelper::LegalizeResult 65258bcb0991SDimitry Andric LegalizerHelper::lowerUnmergeValues(MachineInstr &MI) { 65268bcb0991SDimitry Andric const unsigned NumDst = MI.getNumOperands() - 1; 65275ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(NumDst).getReg(); 65288bcb0991SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 65298bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 65305ffd83dbSDimitry Andric if (DstTy.isPointer()) 65315ffd83dbSDimitry Andric return UnableToLegalize; // TODO 65328bcb0991SDimitry Andric 65335ffd83dbSDimitry Andric SrcReg = coerceToScalar(SrcReg); 65345ffd83dbSDimitry Andric if (!SrcReg) 65355ffd83dbSDimitry Andric return UnableToLegalize; 65368bcb0991SDimitry Andric 65378bcb0991SDimitry Andric // Expand scalarizing unmerge as bitcast to integer and shift. 65385ffd83dbSDimitry Andric LLT IntTy = MRI.getType(SrcReg); 65398bcb0991SDimitry Andric 65405ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst0Reg, SrcReg); 65418bcb0991SDimitry Andric 65428bcb0991SDimitry Andric const unsigned DstSize = DstTy.getSizeInBits(); 65438bcb0991SDimitry Andric unsigned Offset = DstSize; 65448bcb0991SDimitry Andric for (unsigned I = 1; I != NumDst; ++I, Offset += DstSize) { 65458bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntTy, Offset); 65465ffd83dbSDimitry Andric auto Shift = MIRBuilder.buildLShr(IntTy, SrcReg, ShiftAmt); 65478bcb0991SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(I), Shift); 65488bcb0991SDimitry Andric } 65498bcb0991SDimitry Andric 65508bcb0991SDimitry Andric MI.eraseFromParent(); 65518bcb0991SDimitry Andric return Legalized; 65528bcb0991SDimitry Andric } 65538bcb0991SDimitry Andric 6554e8d8bef9SDimitry Andric /// Lower a vector extract or insert by writing the vector to a stack temporary 6555e8d8bef9SDimitry Andric /// and reloading the element or vector. 6556e8d8bef9SDimitry Andric /// 6557e8d8bef9SDimitry Andric /// %dst = G_EXTRACT_VECTOR_ELT %vec, %idx 6558e8d8bef9SDimitry Andric /// => 6559e8d8bef9SDimitry Andric /// %stack_temp = G_FRAME_INDEX 6560e8d8bef9SDimitry Andric /// G_STORE %vec, %stack_temp 6561e8d8bef9SDimitry Andric /// %idx = clamp(%idx, %vec.getNumElements()) 6562e8d8bef9SDimitry Andric /// %element_ptr = G_PTR_ADD %stack_temp, %idx 6563e8d8bef9SDimitry Andric /// %dst = G_LOAD %element_ptr 6564e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 6565e8d8bef9SDimitry Andric LegalizerHelper::lowerExtractInsertVectorElt(MachineInstr &MI) { 6566e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 6567e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 6568e8d8bef9SDimitry Andric Register InsertVal; 6569e8d8bef9SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT) 6570e8d8bef9SDimitry Andric InsertVal = MI.getOperand(2).getReg(); 6571e8d8bef9SDimitry Andric 6572e8d8bef9SDimitry Andric Register Idx = MI.getOperand(MI.getNumOperands() - 1).getReg(); 6573e8d8bef9SDimitry Andric 6574e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(SrcVec); 6575e8d8bef9SDimitry Andric LLT EltTy = VecTy.getElementType(); 65760eae32dcSDimitry Andric unsigned NumElts = VecTy.getNumElements(); 65770eae32dcSDimitry Andric 65780eae32dcSDimitry Andric int64_t IdxVal; 65790eae32dcSDimitry Andric if (mi_match(Idx, MRI, m_ICst(IdxVal)) && IdxVal <= NumElts) { 65800eae32dcSDimitry Andric SmallVector<Register, 8> SrcRegs; 65810eae32dcSDimitry Andric extractParts(SrcVec, EltTy, NumElts, SrcRegs); 65820eae32dcSDimitry Andric 65830eae32dcSDimitry Andric if (InsertVal) { 65840eae32dcSDimitry Andric SrcRegs[IdxVal] = MI.getOperand(2).getReg(); 65850eae32dcSDimitry Andric MIRBuilder.buildMerge(DstReg, SrcRegs); 65860eae32dcSDimitry Andric } else { 65870eae32dcSDimitry Andric MIRBuilder.buildCopy(DstReg, SrcRegs[IdxVal]); 65880eae32dcSDimitry Andric } 65890eae32dcSDimitry Andric 65900eae32dcSDimitry Andric MI.eraseFromParent(); 65910eae32dcSDimitry Andric return Legalized; 65920eae32dcSDimitry Andric } 65930eae32dcSDimitry Andric 6594e8d8bef9SDimitry Andric if (!EltTy.isByteSized()) { // Not implemented. 6595e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Can't handle non-byte element vectors yet\n"); 6596e8d8bef9SDimitry Andric return UnableToLegalize; 6597e8d8bef9SDimitry Andric } 6598e8d8bef9SDimitry Andric 6599e8d8bef9SDimitry Andric unsigned EltBytes = EltTy.getSizeInBytes(); 6600e8d8bef9SDimitry Andric Align VecAlign = getStackTemporaryAlignment(VecTy); 6601e8d8bef9SDimitry Andric Align EltAlign; 6602e8d8bef9SDimitry Andric 6603e8d8bef9SDimitry Andric MachinePointerInfo PtrInfo; 6604e8d8bef9SDimitry Andric auto StackTemp = createStackTemporary(TypeSize::Fixed(VecTy.getSizeInBytes()), 6605e8d8bef9SDimitry Andric VecAlign, PtrInfo); 6606e8d8bef9SDimitry Andric MIRBuilder.buildStore(SrcVec, StackTemp, PtrInfo, VecAlign); 6607e8d8bef9SDimitry Andric 6608e8d8bef9SDimitry Andric // Get the pointer to the element, and be sure not to hit undefined behavior 6609e8d8bef9SDimitry Andric // if the index is out of bounds. 6610e8d8bef9SDimitry Andric Register EltPtr = getVectorElementPointer(StackTemp.getReg(0), VecTy, Idx); 6611e8d8bef9SDimitry Andric 6612e8d8bef9SDimitry Andric if (mi_match(Idx, MRI, m_ICst(IdxVal))) { 6613e8d8bef9SDimitry Andric int64_t Offset = IdxVal * EltBytes; 6614e8d8bef9SDimitry Andric PtrInfo = PtrInfo.getWithOffset(Offset); 6615e8d8bef9SDimitry Andric EltAlign = commonAlignment(VecAlign, Offset); 6616e8d8bef9SDimitry Andric } else { 6617e8d8bef9SDimitry Andric // We lose information with a variable offset. 6618e8d8bef9SDimitry Andric EltAlign = getStackTemporaryAlignment(EltTy); 6619e8d8bef9SDimitry Andric PtrInfo = MachinePointerInfo(MRI.getType(EltPtr).getAddressSpace()); 6620e8d8bef9SDimitry Andric } 6621e8d8bef9SDimitry Andric 6622e8d8bef9SDimitry Andric if (InsertVal) { 6623e8d8bef9SDimitry Andric // Write the inserted element 6624e8d8bef9SDimitry Andric MIRBuilder.buildStore(InsertVal, EltPtr, PtrInfo, EltAlign); 6625e8d8bef9SDimitry Andric 6626e8d8bef9SDimitry Andric // Reload the whole vector. 6627e8d8bef9SDimitry Andric MIRBuilder.buildLoad(DstReg, StackTemp, PtrInfo, VecAlign); 6628e8d8bef9SDimitry Andric } else { 6629e8d8bef9SDimitry Andric MIRBuilder.buildLoad(DstReg, EltPtr, PtrInfo, EltAlign); 6630e8d8bef9SDimitry Andric } 6631e8d8bef9SDimitry Andric 6632e8d8bef9SDimitry Andric MI.eraseFromParent(); 6633e8d8bef9SDimitry Andric return Legalized; 6634e8d8bef9SDimitry Andric } 6635e8d8bef9SDimitry Andric 66368bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 66378bcb0991SDimitry Andric LegalizerHelper::lowerShuffleVector(MachineInstr &MI) { 66388bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 66398bcb0991SDimitry Andric Register Src0Reg = MI.getOperand(1).getReg(); 66408bcb0991SDimitry Andric Register Src1Reg = MI.getOperand(2).getReg(); 66418bcb0991SDimitry Andric LLT Src0Ty = MRI.getType(Src0Reg); 66428bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 66438bcb0991SDimitry Andric LLT IdxTy = LLT::scalar(32); 66448bcb0991SDimitry Andric 6645480093f4SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 66468bcb0991SDimitry Andric 66478bcb0991SDimitry Andric if (DstTy.isScalar()) { 66488bcb0991SDimitry Andric if (Src0Ty.isVector()) 66498bcb0991SDimitry Andric return UnableToLegalize; 66508bcb0991SDimitry Andric 66518bcb0991SDimitry Andric // This is just a SELECT. 66528bcb0991SDimitry Andric assert(Mask.size() == 1 && "Expected a single mask element"); 66538bcb0991SDimitry Andric Register Val; 66548bcb0991SDimitry Andric if (Mask[0] < 0 || Mask[0] > 1) 66558bcb0991SDimitry Andric Val = MIRBuilder.buildUndef(DstTy).getReg(0); 66568bcb0991SDimitry Andric else 66578bcb0991SDimitry Andric Val = Mask[0] == 0 ? Src0Reg : Src1Reg; 66588bcb0991SDimitry Andric MIRBuilder.buildCopy(DstReg, Val); 66598bcb0991SDimitry Andric MI.eraseFromParent(); 66608bcb0991SDimitry Andric return Legalized; 66618bcb0991SDimitry Andric } 66628bcb0991SDimitry Andric 66638bcb0991SDimitry Andric Register Undef; 66648bcb0991SDimitry Andric SmallVector<Register, 32> BuildVec; 66658bcb0991SDimitry Andric LLT EltTy = DstTy.getElementType(); 66668bcb0991SDimitry Andric 66678bcb0991SDimitry Andric for (int Idx : Mask) { 66688bcb0991SDimitry Andric if (Idx < 0) { 66698bcb0991SDimitry Andric if (!Undef.isValid()) 66708bcb0991SDimitry Andric Undef = MIRBuilder.buildUndef(EltTy).getReg(0); 66718bcb0991SDimitry Andric BuildVec.push_back(Undef); 66728bcb0991SDimitry Andric continue; 66738bcb0991SDimitry Andric } 66748bcb0991SDimitry Andric 66758bcb0991SDimitry Andric if (Src0Ty.isScalar()) { 66768bcb0991SDimitry Andric BuildVec.push_back(Idx == 0 ? Src0Reg : Src1Reg); 66778bcb0991SDimitry Andric } else { 66788bcb0991SDimitry Andric int NumElts = Src0Ty.getNumElements(); 66798bcb0991SDimitry Andric Register SrcVec = Idx < NumElts ? Src0Reg : Src1Reg; 66808bcb0991SDimitry Andric int ExtractIdx = Idx < NumElts ? Idx : Idx - NumElts; 66818bcb0991SDimitry Andric auto IdxK = MIRBuilder.buildConstant(IdxTy, ExtractIdx); 66828bcb0991SDimitry Andric auto Extract = MIRBuilder.buildExtractVectorElement(EltTy, SrcVec, IdxK); 66838bcb0991SDimitry Andric BuildVec.push_back(Extract.getReg(0)); 66848bcb0991SDimitry Andric } 66858bcb0991SDimitry Andric } 66868bcb0991SDimitry Andric 66878bcb0991SDimitry Andric MIRBuilder.buildBuildVector(DstReg, BuildVec); 66888bcb0991SDimitry Andric MI.eraseFromParent(); 66898bcb0991SDimitry Andric return Legalized; 66908bcb0991SDimitry Andric } 66918bcb0991SDimitry Andric 66928bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 66938bcb0991SDimitry Andric LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) { 66945ffd83dbSDimitry Andric const auto &MF = *MI.getMF(); 66955ffd83dbSDimitry Andric const auto &TFI = *MF.getSubtarget().getFrameLowering(); 66965ffd83dbSDimitry Andric if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp) 66975ffd83dbSDimitry Andric return UnableToLegalize; 66985ffd83dbSDimitry Andric 66998bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 67008bcb0991SDimitry Andric Register AllocSize = MI.getOperand(1).getReg(); 67015ffd83dbSDimitry Andric Align Alignment = assumeAligned(MI.getOperand(2).getImm()); 67028bcb0991SDimitry Andric 67038bcb0991SDimitry Andric LLT PtrTy = MRI.getType(Dst); 67048bcb0991SDimitry Andric LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits()); 67058bcb0991SDimitry Andric 67068bcb0991SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 67078bcb0991SDimitry Andric auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg); 67088bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp); 67098bcb0991SDimitry Andric 67108bcb0991SDimitry Andric // Subtract the final alloc from the SP. We use G_PTRTOINT here so we don't 67118bcb0991SDimitry Andric // have to generate an extra instruction to negate the alloc and then use 6712480093f4SDimitry Andric // G_PTR_ADD to add the negative offset. 67138bcb0991SDimitry Andric auto Alloc = MIRBuilder.buildSub(IntPtrTy, SPTmp, AllocSize); 67145ffd83dbSDimitry Andric if (Alignment > Align(1)) { 67155ffd83dbSDimitry Andric APInt AlignMask(IntPtrTy.getSizeInBits(), Alignment.value(), true); 67168bcb0991SDimitry Andric AlignMask.negate(); 67178bcb0991SDimitry Andric auto AlignCst = MIRBuilder.buildConstant(IntPtrTy, AlignMask); 67188bcb0991SDimitry Andric Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst); 67198bcb0991SDimitry Andric } 67208bcb0991SDimitry Andric 67218bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(PtrTy, Alloc); 67228bcb0991SDimitry Andric MIRBuilder.buildCopy(SPReg, SPTmp); 67238bcb0991SDimitry Andric MIRBuilder.buildCopy(Dst, SPTmp); 67248bcb0991SDimitry Andric 67258bcb0991SDimitry Andric MI.eraseFromParent(); 67268bcb0991SDimitry Andric return Legalized; 67278bcb0991SDimitry Andric } 67288bcb0991SDimitry Andric 67298bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 67308bcb0991SDimitry Andric LegalizerHelper::lowerExtract(MachineInstr &MI) { 67318bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 67328bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 67338bcb0991SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 67348bcb0991SDimitry Andric 67358bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 67368bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 67378bcb0991SDimitry Andric 67380eae32dcSDimitry Andric // Extract sub-vector or one element 67390eae32dcSDimitry Andric if (SrcTy.isVector()) { 67400eae32dcSDimitry Andric unsigned SrcEltSize = SrcTy.getElementType().getSizeInBits(); 67410eae32dcSDimitry Andric unsigned DstSize = DstTy.getSizeInBits(); 67420eae32dcSDimitry Andric 67430eae32dcSDimitry Andric if ((Offset % SrcEltSize == 0) && (DstSize % SrcEltSize == 0) && 67440eae32dcSDimitry Andric (Offset + DstSize <= SrcTy.getSizeInBits())) { 67450eae32dcSDimitry Andric // Unmerge and allow access to each Src element for the artifact combiner. 67460eae32dcSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(SrcTy.getElementType(), Src); 67470eae32dcSDimitry Andric 67480eae32dcSDimitry Andric // Take element(s) we need to extract and copy it (merge them). 67490eae32dcSDimitry Andric SmallVector<Register, 8> SubVectorElts; 67500eae32dcSDimitry Andric for (unsigned Idx = Offset / SrcEltSize; 67510eae32dcSDimitry Andric Idx < (Offset + DstSize) / SrcEltSize; ++Idx) { 67520eae32dcSDimitry Andric SubVectorElts.push_back(Unmerge.getReg(Idx)); 67530eae32dcSDimitry Andric } 67540eae32dcSDimitry Andric if (SubVectorElts.size() == 1) 67550eae32dcSDimitry Andric MIRBuilder.buildCopy(Dst, SubVectorElts[0]); 67560eae32dcSDimitry Andric else 67570eae32dcSDimitry Andric MIRBuilder.buildMerge(Dst, SubVectorElts); 67580eae32dcSDimitry Andric 67590eae32dcSDimitry Andric MI.eraseFromParent(); 67600eae32dcSDimitry Andric return Legalized; 67610eae32dcSDimitry Andric } 67620eae32dcSDimitry Andric } 67630eae32dcSDimitry Andric 67648bcb0991SDimitry Andric if (DstTy.isScalar() && 67658bcb0991SDimitry Andric (SrcTy.isScalar() || 67668bcb0991SDimitry Andric (SrcTy.isVector() && DstTy == SrcTy.getElementType()))) { 67678bcb0991SDimitry Andric LLT SrcIntTy = SrcTy; 67688bcb0991SDimitry Andric if (!SrcTy.isScalar()) { 67698bcb0991SDimitry Andric SrcIntTy = LLT::scalar(SrcTy.getSizeInBits()); 67708bcb0991SDimitry Andric Src = MIRBuilder.buildBitcast(SrcIntTy, Src).getReg(0); 67718bcb0991SDimitry Andric } 67728bcb0991SDimitry Andric 67738bcb0991SDimitry Andric if (Offset == 0) 67748bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Src); 67758bcb0991SDimitry Andric else { 67768bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(SrcIntTy, Offset); 67778bcb0991SDimitry Andric auto Shr = MIRBuilder.buildLShr(SrcIntTy, Src, ShiftAmt); 67788bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Shr); 67798bcb0991SDimitry Andric } 67808bcb0991SDimitry Andric 67818bcb0991SDimitry Andric MI.eraseFromParent(); 67828bcb0991SDimitry Andric return Legalized; 67838bcb0991SDimitry Andric } 67848bcb0991SDimitry Andric 67858bcb0991SDimitry Andric return UnableToLegalize; 67868bcb0991SDimitry Andric } 67878bcb0991SDimitry Andric 67888bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerInsert(MachineInstr &MI) { 67898bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 67908bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 67918bcb0991SDimitry Andric Register InsertSrc = MI.getOperand(2).getReg(); 67928bcb0991SDimitry Andric uint64_t Offset = MI.getOperand(3).getImm(); 67938bcb0991SDimitry Andric 67948bcb0991SDimitry Andric LLT DstTy = MRI.getType(Src); 67958bcb0991SDimitry Andric LLT InsertTy = MRI.getType(InsertSrc); 67968bcb0991SDimitry Andric 67970eae32dcSDimitry Andric // Insert sub-vector or one element 67980eae32dcSDimitry Andric if (DstTy.isVector() && !InsertTy.isPointer()) { 67990eae32dcSDimitry Andric LLT EltTy = DstTy.getElementType(); 68000eae32dcSDimitry Andric unsigned EltSize = EltTy.getSizeInBits(); 68010eae32dcSDimitry Andric unsigned InsertSize = InsertTy.getSizeInBits(); 68020eae32dcSDimitry Andric 68030eae32dcSDimitry Andric if ((Offset % EltSize == 0) && (InsertSize % EltSize == 0) && 68040eae32dcSDimitry Andric (Offset + InsertSize <= DstTy.getSizeInBits())) { 68050eae32dcSDimitry Andric auto UnmergeSrc = MIRBuilder.buildUnmerge(EltTy, Src); 68060eae32dcSDimitry Andric SmallVector<Register, 8> DstElts; 68070eae32dcSDimitry Andric unsigned Idx = 0; 68080eae32dcSDimitry Andric // Elements from Src before insert start Offset 68090eae32dcSDimitry Andric for (; Idx < Offset / EltSize; ++Idx) { 68100eae32dcSDimitry Andric DstElts.push_back(UnmergeSrc.getReg(Idx)); 68110eae32dcSDimitry Andric } 68120eae32dcSDimitry Andric 68130eae32dcSDimitry Andric // Replace elements in Src with elements from InsertSrc 68140eae32dcSDimitry Andric if (InsertTy.getSizeInBits() > EltSize) { 68150eae32dcSDimitry Andric auto UnmergeInsertSrc = MIRBuilder.buildUnmerge(EltTy, InsertSrc); 68160eae32dcSDimitry Andric for (unsigned i = 0; Idx < (Offset + InsertSize) / EltSize; 68170eae32dcSDimitry Andric ++Idx, ++i) { 68180eae32dcSDimitry Andric DstElts.push_back(UnmergeInsertSrc.getReg(i)); 68190eae32dcSDimitry Andric } 68200eae32dcSDimitry Andric } else { 68210eae32dcSDimitry Andric DstElts.push_back(InsertSrc); 68220eae32dcSDimitry Andric ++Idx; 68230eae32dcSDimitry Andric } 68240eae32dcSDimitry Andric 68250eae32dcSDimitry Andric // Remaining elements from Src after insert 68260eae32dcSDimitry Andric for (; Idx < DstTy.getNumElements(); ++Idx) { 68270eae32dcSDimitry Andric DstElts.push_back(UnmergeSrc.getReg(Idx)); 68280eae32dcSDimitry Andric } 68290eae32dcSDimitry Andric 68300eae32dcSDimitry Andric MIRBuilder.buildMerge(Dst, DstElts); 68310eae32dcSDimitry Andric MI.eraseFromParent(); 68320eae32dcSDimitry Andric return Legalized; 68330eae32dcSDimitry Andric } 68340eae32dcSDimitry Andric } 68350eae32dcSDimitry Andric 68365ffd83dbSDimitry Andric if (InsertTy.isVector() || 68375ffd83dbSDimitry Andric (DstTy.isVector() && DstTy.getElementType() != InsertTy)) 68385ffd83dbSDimitry Andric return UnableToLegalize; 68395ffd83dbSDimitry Andric 68405ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 68415ffd83dbSDimitry Andric if ((DstTy.isPointer() && 68425ffd83dbSDimitry Andric DL.isNonIntegralAddressSpace(DstTy.getAddressSpace())) || 68435ffd83dbSDimitry Andric (InsertTy.isPointer() && 68445ffd83dbSDimitry Andric DL.isNonIntegralAddressSpace(InsertTy.getAddressSpace()))) { 68455ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Not casting non-integral address space integer\n"); 68465ffd83dbSDimitry Andric return UnableToLegalize; 68475ffd83dbSDimitry Andric } 68485ffd83dbSDimitry Andric 68498bcb0991SDimitry Andric LLT IntDstTy = DstTy; 68505ffd83dbSDimitry Andric 68518bcb0991SDimitry Andric if (!DstTy.isScalar()) { 68528bcb0991SDimitry Andric IntDstTy = LLT::scalar(DstTy.getSizeInBits()); 68535ffd83dbSDimitry Andric Src = MIRBuilder.buildCast(IntDstTy, Src).getReg(0); 68545ffd83dbSDimitry Andric } 68555ffd83dbSDimitry Andric 68565ffd83dbSDimitry Andric if (!InsertTy.isScalar()) { 68575ffd83dbSDimitry Andric const LLT IntInsertTy = LLT::scalar(InsertTy.getSizeInBits()); 68585ffd83dbSDimitry Andric InsertSrc = MIRBuilder.buildPtrToInt(IntInsertTy, InsertSrc).getReg(0); 68598bcb0991SDimitry Andric } 68608bcb0991SDimitry Andric 68618bcb0991SDimitry Andric Register ExtInsSrc = MIRBuilder.buildZExt(IntDstTy, InsertSrc).getReg(0); 68628bcb0991SDimitry Andric if (Offset != 0) { 68638bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntDstTy, Offset); 68648bcb0991SDimitry Andric ExtInsSrc = MIRBuilder.buildShl(IntDstTy, ExtInsSrc, ShiftAmt).getReg(0); 68658bcb0991SDimitry Andric } 68668bcb0991SDimitry Andric 68675ffd83dbSDimitry Andric APInt MaskVal = APInt::getBitsSetWithWrap( 68685ffd83dbSDimitry Andric DstTy.getSizeInBits(), Offset + InsertTy.getSizeInBits(), Offset); 68698bcb0991SDimitry Andric 68708bcb0991SDimitry Andric auto Mask = MIRBuilder.buildConstant(IntDstTy, MaskVal); 68718bcb0991SDimitry Andric auto MaskedSrc = MIRBuilder.buildAnd(IntDstTy, Src, Mask); 68728bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(IntDstTy, MaskedSrc, ExtInsSrc); 68738bcb0991SDimitry Andric 68745ffd83dbSDimitry Andric MIRBuilder.buildCast(Dst, Or); 68758bcb0991SDimitry Andric MI.eraseFromParent(); 68768bcb0991SDimitry Andric return Legalized; 68778bcb0991SDimitry Andric } 68788bcb0991SDimitry Andric 68798bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 68808bcb0991SDimitry Andric LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) { 68818bcb0991SDimitry Andric Register Dst0 = MI.getOperand(0).getReg(); 68828bcb0991SDimitry Andric Register Dst1 = MI.getOperand(1).getReg(); 68838bcb0991SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 68848bcb0991SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 68858bcb0991SDimitry Andric const bool IsAdd = MI.getOpcode() == TargetOpcode::G_SADDO; 68868bcb0991SDimitry Andric 68878bcb0991SDimitry Andric LLT Ty = MRI.getType(Dst0); 68888bcb0991SDimitry Andric LLT BoolTy = MRI.getType(Dst1); 68898bcb0991SDimitry Andric 68908bcb0991SDimitry Andric if (IsAdd) 68918bcb0991SDimitry Andric MIRBuilder.buildAdd(Dst0, LHS, RHS); 68928bcb0991SDimitry Andric else 68938bcb0991SDimitry Andric MIRBuilder.buildSub(Dst0, LHS, RHS); 68948bcb0991SDimitry Andric 68958bcb0991SDimitry Andric // TODO: If SADDSAT/SSUBSAT is legal, compare results to detect overflow. 68968bcb0991SDimitry Andric 68978bcb0991SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 68988bcb0991SDimitry Andric 68998bcb0991SDimitry Andric // For an addition, the result should be less than one of the operands (LHS) 69008bcb0991SDimitry Andric // if and only if the other operand (RHS) is negative, otherwise there will 69018bcb0991SDimitry Andric // be overflow. 69028bcb0991SDimitry Andric // For a subtraction, the result should be less than one of the operands 69038bcb0991SDimitry Andric // (LHS) if and only if the other operand (RHS) is (non-zero) positive, 69048bcb0991SDimitry Andric // otherwise there will be overflow. 69058bcb0991SDimitry Andric auto ResultLowerThanLHS = 69068bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Dst0, LHS); 69078bcb0991SDimitry Andric auto ConditionRHS = MIRBuilder.buildICmp( 69088bcb0991SDimitry Andric IsAdd ? CmpInst::ICMP_SLT : CmpInst::ICMP_SGT, BoolTy, RHS, Zero); 69098bcb0991SDimitry Andric 69108bcb0991SDimitry Andric MIRBuilder.buildXor(Dst1, ConditionRHS, ResultLowerThanLHS); 69118bcb0991SDimitry Andric MI.eraseFromParent(); 69128bcb0991SDimitry Andric return Legalized; 69138bcb0991SDimitry Andric } 6914480093f4SDimitry Andric 6915480093f4SDimitry Andric LegalizerHelper::LegalizeResult 6916e8d8bef9SDimitry Andric LegalizerHelper::lowerAddSubSatToMinMax(MachineInstr &MI) { 6917e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 6918e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 6919e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 6920e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 6921e8d8bef9SDimitry Andric bool IsSigned; 6922e8d8bef9SDimitry Andric bool IsAdd; 6923e8d8bef9SDimitry Andric unsigned BaseOp; 6924e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 6925e8d8bef9SDimitry Andric default: 6926e8d8bef9SDimitry Andric llvm_unreachable("unexpected addsat/subsat opcode"); 6927e8d8bef9SDimitry Andric case TargetOpcode::G_UADDSAT: 6928e8d8bef9SDimitry Andric IsSigned = false; 6929e8d8bef9SDimitry Andric IsAdd = true; 6930e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_ADD; 6931e8d8bef9SDimitry Andric break; 6932e8d8bef9SDimitry Andric case TargetOpcode::G_SADDSAT: 6933e8d8bef9SDimitry Andric IsSigned = true; 6934e8d8bef9SDimitry Andric IsAdd = true; 6935e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_ADD; 6936e8d8bef9SDimitry Andric break; 6937e8d8bef9SDimitry Andric case TargetOpcode::G_USUBSAT: 6938e8d8bef9SDimitry Andric IsSigned = false; 6939e8d8bef9SDimitry Andric IsAdd = false; 6940e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_SUB; 6941e8d8bef9SDimitry Andric break; 6942e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBSAT: 6943e8d8bef9SDimitry Andric IsSigned = true; 6944e8d8bef9SDimitry Andric IsAdd = false; 6945e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_SUB; 6946e8d8bef9SDimitry Andric break; 6947e8d8bef9SDimitry Andric } 6948e8d8bef9SDimitry Andric 6949e8d8bef9SDimitry Andric if (IsSigned) { 6950e8d8bef9SDimitry Andric // sadd.sat(a, b) -> 6951e8d8bef9SDimitry Andric // hi = 0x7fffffff - smax(a, 0) 6952e8d8bef9SDimitry Andric // lo = 0x80000000 - smin(a, 0) 6953e8d8bef9SDimitry Andric // a + smin(smax(lo, b), hi) 6954e8d8bef9SDimitry Andric // ssub.sat(a, b) -> 6955e8d8bef9SDimitry Andric // lo = smax(a, -1) - 0x7fffffff 6956e8d8bef9SDimitry Andric // hi = smin(a, -1) - 0x80000000 6957e8d8bef9SDimitry Andric // a - smin(smax(lo, b), hi) 6958e8d8bef9SDimitry Andric // TODO: AMDGPU can use a "median of 3" instruction here: 6959e8d8bef9SDimitry Andric // a +/- med3(lo, b, hi) 6960e8d8bef9SDimitry Andric uint64_t NumBits = Ty.getScalarSizeInBits(); 6961e8d8bef9SDimitry Andric auto MaxVal = 6962e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMaxValue(NumBits)); 6963e8d8bef9SDimitry Andric auto MinVal = 6964e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(NumBits)); 6965e8d8bef9SDimitry Andric MachineInstrBuilder Hi, Lo; 6966e8d8bef9SDimitry Andric if (IsAdd) { 6967e8d8bef9SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 6968e8d8bef9SDimitry Andric Hi = MIRBuilder.buildSub(Ty, MaxVal, MIRBuilder.buildSMax(Ty, LHS, Zero)); 6969e8d8bef9SDimitry Andric Lo = MIRBuilder.buildSub(Ty, MinVal, MIRBuilder.buildSMin(Ty, LHS, Zero)); 6970e8d8bef9SDimitry Andric } else { 6971e8d8bef9SDimitry Andric auto NegOne = MIRBuilder.buildConstant(Ty, -1); 6972e8d8bef9SDimitry Andric Lo = MIRBuilder.buildSub(Ty, MIRBuilder.buildSMax(Ty, LHS, NegOne), 6973e8d8bef9SDimitry Andric MaxVal); 6974e8d8bef9SDimitry Andric Hi = MIRBuilder.buildSub(Ty, MIRBuilder.buildSMin(Ty, LHS, NegOne), 6975e8d8bef9SDimitry Andric MinVal); 6976e8d8bef9SDimitry Andric } 6977e8d8bef9SDimitry Andric auto RHSClamped = 6978e8d8bef9SDimitry Andric MIRBuilder.buildSMin(Ty, MIRBuilder.buildSMax(Ty, Lo, RHS), Hi); 6979e8d8bef9SDimitry Andric MIRBuilder.buildInstr(BaseOp, {Res}, {LHS, RHSClamped}); 6980e8d8bef9SDimitry Andric } else { 6981e8d8bef9SDimitry Andric // uadd.sat(a, b) -> a + umin(~a, b) 6982e8d8bef9SDimitry Andric // usub.sat(a, b) -> a - umin(a, b) 6983e8d8bef9SDimitry Andric Register Not = IsAdd ? MIRBuilder.buildNot(Ty, LHS).getReg(0) : LHS; 6984e8d8bef9SDimitry Andric auto Min = MIRBuilder.buildUMin(Ty, Not, RHS); 6985e8d8bef9SDimitry Andric MIRBuilder.buildInstr(BaseOp, {Res}, {LHS, Min}); 6986e8d8bef9SDimitry Andric } 6987e8d8bef9SDimitry Andric 6988e8d8bef9SDimitry Andric MI.eraseFromParent(); 6989e8d8bef9SDimitry Andric return Legalized; 6990e8d8bef9SDimitry Andric } 6991e8d8bef9SDimitry Andric 6992e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 6993e8d8bef9SDimitry Andric LegalizerHelper::lowerAddSubSatToAddoSubo(MachineInstr &MI) { 6994e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 6995e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 6996e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 6997e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 6998e8d8bef9SDimitry Andric LLT BoolTy = Ty.changeElementSize(1); 6999e8d8bef9SDimitry Andric bool IsSigned; 7000e8d8bef9SDimitry Andric bool IsAdd; 7001e8d8bef9SDimitry Andric unsigned OverflowOp; 7002e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 7003e8d8bef9SDimitry Andric default: 7004e8d8bef9SDimitry Andric llvm_unreachable("unexpected addsat/subsat opcode"); 7005e8d8bef9SDimitry Andric case TargetOpcode::G_UADDSAT: 7006e8d8bef9SDimitry Andric IsSigned = false; 7007e8d8bef9SDimitry Andric IsAdd = true; 7008e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_UADDO; 7009e8d8bef9SDimitry Andric break; 7010e8d8bef9SDimitry Andric case TargetOpcode::G_SADDSAT: 7011e8d8bef9SDimitry Andric IsSigned = true; 7012e8d8bef9SDimitry Andric IsAdd = true; 7013e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_SADDO; 7014e8d8bef9SDimitry Andric break; 7015e8d8bef9SDimitry Andric case TargetOpcode::G_USUBSAT: 7016e8d8bef9SDimitry Andric IsSigned = false; 7017e8d8bef9SDimitry Andric IsAdd = false; 7018e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_USUBO; 7019e8d8bef9SDimitry Andric break; 7020e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBSAT: 7021e8d8bef9SDimitry Andric IsSigned = true; 7022e8d8bef9SDimitry Andric IsAdd = false; 7023e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_SSUBO; 7024e8d8bef9SDimitry Andric break; 7025e8d8bef9SDimitry Andric } 7026e8d8bef9SDimitry Andric 7027e8d8bef9SDimitry Andric auto OverflowRes = 7028e8d8bef9SDimitry Andric MIRBuilder.buildInstr(OverflowOp, {Ty, BoolTy}, {LHS, RHS}); 7029e8d8bef9SDimitry Andric Register Tmp = OverflowRes.getReg(0); 7030e8d8bef9SDimitry Andric Register Ov = OverflowRes.getReg(1); 7031e8d8bef9SDimitry Andric MachineInstrBuilder Clamp; 7032e8d8bef9SDimitry Andric if (IsSigned) { 7033e8d8bef9SDimitry Andric // sadd.sat(a, b) -> 7034e8d8bef9SDimitry Andric // {tmp, ov} = saddo(a, b) 7035e8d8bef9SDimitry Andric // ov ? (tmp >>s 31) + 0x80000000 : r 7036e8d8bef9SDimitry Andric // ssub.sat(a, b) -> 7037e8d8bef9SDimitry Andric // {tmp, ov} = ssubo(a, b) 7038e8d8bef9SDimitry Andric // ov ? (tmp >>s 31) + 0x80000000 : r 7039e8d8bef9SDimitry Andric uint64_t NumBits = Ty.getScalarSizeInBits(); 7040e8d8bef9SDimitry Andric auto ShiftAmount = MIRBuilder.buildConstant(Ty, NumBits - 1); 7041e8d8bef9SDimitry Andric auto Sign = MIRBuilder.buildAShr(Ty, Tmp, ShiftAmount); 7042e8d8bef9SDimitry Andric auto MinVal = 7043e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(NumBits)); 7044e8d8bef9SDimitry Andric Clamp = MIRBuilder.buildAdd(Ty, Sign, MinVal); 7045e8d8bef9SDimitry Andric } else { 7046e8d8bef9SDimitry Andric // uadd.sat(a, b) -> 7047e8d8bef9SDimitry Andric // {tmp, ov} = uaddo(a, b) 7048e8d8bef9SDimitry Andric // ov ? 0xffffffff : tmp 7049e8d8bef9SDimitry Andric // usub.sat(a, b) -> 7050e8d8bef9SDimitry Andric // {tmp, ov} = usubo(a, b) 7051e8d8bef9SDimitry Andric // ov ? 0 : tmp 7052e8d8bef9SDimitry Andric Clamp = MIRBuilder.buildConstant(Ty, IsAdd ? -1 : 0); 7053e8d8bef9SDimitry Andric } 7054e8d8bef9SDimitry Andric MIRBuilder.buildSelect(Res, Ov, Clamp, Tmp); 7055e8d8bef9SDimitry Andric 7056e8d8bef9SDimitry Andric MI.eraseFromParent(); 7057e8d8bef9SDimitry Andric return Legalized; 7058e8d8bef9SDimitry Andric } 7059e8d8bef9SDimitry Andric 7060e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7061e8d8bef9SDimitry Andric LegalizerHelper::lowerShlSat(MachineInstr &MI) { 7062e8d8bef9SDimitry Andric assert((MI.getOpcode() == TargetOpcode::G_SSHLSAT || 7063e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_USHLSAT) && 7064e8d8bef9SDimitry Andric "Expected shlsat opcode!"); 7065e8d8bef9SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SSHLSAT; 7066e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 7067e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 7068e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 7069e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 7070e8d8bef9SDimitry Andric LLT BoolTy = Ty.changeElementSize(1); 7071e8d8bef9SDimitry Andric 7072e8d8bef9SDimitry Andric unsigned BW = Ty.getScalarSizeInBits(); 7073e8d8bef9SDimitry Andric auto Result = MIRBuilder.buildShl(Ty, LHS, RHS); 7074e8d8bef9SDimitry Andric auto Orig = IsSigned ? MIRBuilder.buildAShr(Ty, Result, RHS) 7075e8d8bef9SDimitry Andric : MIRBuilder.buildLShr(Ty, Result, RHS); 7076e8d8bef9SDimitry Andric 7077e8d8bef9SDimitry Andric MachineInstrBuilder SatVal; 7078e8d8bef9SDimitry Andric if (IsSigned) { 7079e8d8bef9SDimitry Andric auto SatMin = MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(BW)); 7080e8d8bef9SDimitry Andric auto SatMax = MIRBuilder.buildConstant(Ty, APInt::getSignedMaxValue(BW)); 7081e8d8bef9SDimitry Andric auto Cmp = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, LHS, 7082e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, 0)); 7083e8d8bef9SDimitry Andric SatVal = MIRBuilder.buildSelect(Ty, Cmp, SatMin, SatMax); 7084e8d8bef9SDimitry Andric } else { 7085e8d8bef9SDimitry Andric SatVal = MIRBuilder.buildConstant(Ty, APInt::getMaxValue(BW)); 7086e8d8bef9SDimitry Andric } 7087e8d8bef9SDimitry Andric auto Ov = MIRBuilder.buildICmp(CmpInst::ICMP_NE, BoolTy, LHS, Orig); 7088e8d8bef9SDimitry Andric MIRBuilder.buildSelect(Res, Ov, SatVal, Result); 7089e8d8bef9SDimitry Andric 7090e8d8bef9SDimitry Andric MI.eraseFromParent(); 7091e8d8bef9SDimitry Andric return Legalized; 7092e8d8bef9SDimitry Andric } 7093e8d8bef9SDimitry Andric 7094e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7095480093f4SDimitry Andric LegalizerHelper::lowerBswap(MachineInstr &MI) { 7096480093f4SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7097480093f4SDimitry Andric Register Src = MI.getOperand(1).getReg(); 7098480093f4SDimitry Andric const LLT Ty = MRI.getType(Src); 70995ffd83dbSDimitry Andric unsigned SizeInBytes = (Ty.getScalarSizeInBits() + 7) / 8; 7100480093f4SDimitry Andric unsigned BaseShiftAmt = (SizeInBytes - 1) * 8; 7101480093f4SDimitry Andric 7102480093f4SDimitry Andric // Swap most and least significant byte, set remaining bytes in Res to zero. 7103480093f4SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt); 7104480093f4SDimitry Andric auto LSByteShiftedLeft = MIRBuilder.buildShl(Ty, Src, ShiftAmt); 7105480093f4SDimitry Andric auto MSByteShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); 7106480093f4SDimitry Andric auto Res = MIRBuilder.buildOr(Ty, MSByteShiftedRight, LSByteShiftedLeft); 7107480093f4SDimitry Andric 7108480093f4SDimitry Andric // Set i-th high/low byte in Res to i-th low/high byte from Src. 7109480093f4SDimitry Andric for (unsigned i = 1; i < SizeInBytes / 2; ++i) { 7110480093f4SDimitry Andric // AND with Mask leaves byte i unchanged and sets remaining bytes to 0. 7111480093f4SDimitry Andric APInt APMask(SizeInBytes * 8, 0xFF << (i * 8)); 7112480093f4SDimitry Andric auto Mask = MIRBuilder.buildConstant(Ty, APMask); 7113480093f4SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt - 16 * i); 7114480093f4SDimitry Andric // Low byte shifted left to place of high byte: (Src & Mask) << ShiftAmt. 7115480093f4SDimitry Andric auto LoByte = MIRBuilder.buildAnd(Ty, Src, Mask); 7116480093f4SDimitry Andric auto LoShiftedLeft = MIRBuilder.buildShl(Ty, LoByte, ShiftAmt); 7117480093f4SDimitry Andric Res = MIRBuilder.buildOr(Ty, Res, LoShiftedLeft); 7118480093f4SDimitry Andric // High byte shifted right to place of low byte: (Src >> ShiftAmt) & Mask. 7119480093f4SDimitry Andric auto SrcShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); 7120480093f4SDimitry Andric auto HiShiftedRight = MIRBuilder.buildAnd(Ty, SrcShiftedRight, Mask); 7121480093f4SDimitry Andric Res = MIRBuilder.buildOr(Ty, Res, HiShiftedRight); 7122480093f4SDimitry Andric } 7123480093f4SDimitry Andric Res.getInstr()->getOperand(0).setReg(Dst); 7124480093f4SDimitry Andric 7125480093f4SDimitry Andric MI.eraseFromParent(); 7126480093f4SDimitry Andric return Legalized; 7127480093f4SDimitry Andric } 7128480093f4SDimitry Andric 7129480093f4SDimitry Andric //{ (Src & Mask) >> N } | { (Src << N) & Mask } 7130480093f4SDimitry Andric static MachineInstrBuilder SwapN(unsigned N, DstOp Dst, MachineIRBuilder &B, 7131480093f4SDimitry Andric MachineInstrBuilder Src, APInt Mask) { 7132480093f4SDimitry Andric const LLT Ty = Dst.getLLTTy(*B.getMRI()); 7133480093f4SDimitry Andric MachineInstrBuilder C_N = B.buildConstant(Ty, N); 7134480093f4SDimitry Andric MachineInstrBuilder MaskLoNTo0 = B.buildConstant(Ty, Mask); 7135480093f4SDimitry Andric auto LHS = B.buildLShr(Ty, B.buildAnd(Ty, Src, MaskLoNTo0), C_N); 7136480093f4SDimitry Andric auto RHS = B.buildAnd(Ty, B.buildShl(Ty, Src, C_N), MaskLoNTo0); 7137480093f4SDimitry Andric return B.buildOr(Dst, LHS, RHS); 7138480093f4SDimitry Andric } 7139480093f4SDimitry Andric 7140480093f4SDimitry Andric LegalizerHelper::LegalizeResult 7141480093f4SDimitry Andric LegalizerHelper::lowerBitreverse(MachineInstr &MI) { 7142480093f4SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7143480093f4SDimitry Andric Register Src = MI.getOperand(1).getReg(); 7144480093f4SDimitry Andric const LLT Ty = MRI.getType(Src); 7145480093f4SDimitry Andric unsigned Size = Ty.getSizeInBits(); 7146480093f4SDimitry Andric 7147480093f4SDimitry Andric MachineInstrBuilder BSWAP = 7148480093f4SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_BSWAP, {Ty}, {Src}); 7149480093f4SDimitry Andric 7150480093f4SDimitry Andric // swap high and low 4 bits in 8 bit blocks 7654|3210 -> 3210|7654 7151480093f4SDimitry Andric // [(val & 0xF0F0F0F0) >> 4] | [(val & 0x0F0F0F0F) << 4] 7152480093f4SDimitry Andric // -> [(val & 0xF0F0F0F0) >> 4] | [(val << 4) & 0xF0F0F0F0] 7153480093f4SDimitry Andric MachineInstrBuilder Swap4 = 7154480093f4SDimitry Andric SwapN(4, Ty, MIRBuilder, BSWAP, APInt::getSplat(Size, APInt(8, 0xF0))); 7155480093f4SDimitry Andric 7156480093f4SDimitry Andric // swap high and low 2 bits in 4 bit blocks 32|10 76|54 -> 10|32 54|76 7157480093f4SDimitry Andric // [(val & 0xCCCCCCCC) >> 2] & [(val & 0x33333333) << 2] 7158480093f4SDimitry Andric // -> [(val & 0xCCCCCCCC) >> 2] & [(val << 2) & 0xCCCCCCCC] 7159480093f4SDimitry Andric MachineInstrBuilder Swap2 = 7160480093f4SDimitry Andric SwapN(2, Ty, MIRBuilder, Swap4, APInt::getSplat(Size, APInt(8, 0xCC))); 7161480093f4SDimitry Andric 7162480093f4SDimitry Andric // swap high and low 1 bit in 2 bit blocks 1|0 3|2 5|4 7|6 -> 0|1 2|3 4|5 6|7 7163480093f4SDimitry Andric // [(val & 0xAAAAAAAA) >> 1] & [(val & 0x55555555) << 1] 7164480093f4SDimitry Andric // -> [(val & 0xAAAAAAAA) >> 1] & [(val << 1) & 0xAAAAAAAA] 7165480093f4SDimitry Andric SwapN(1, Dst, MIRBuilder, Swap2, APInt::getSplat(Size, APInt(8, 0xAA))); 7166480093f4SDimitry Andric 7167480093f4SDimitry Andric MI.eraseFromParent(); 7168480093f4SDimitry Andric return Legalized; 7169480093f4SDimitry Andric } 7170480093f4SDimitry Andric 7171480093f4SDimitry Andric LegalizerHelper::LegalizeResult 71725ffd83dbSDimitry Andric LegalizerHelper::lowerReadWriteRegister(MachineInstr &MI) { 7173480093f4SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 71745ffd83dbSDimitry Andric 71755ffd83dbSDimitry Andric bool IsRead = MI.getOpcode() == TargetOpcode::G_READ_REGISTER; 71765ffd83dbSDimitry Andric int NameOpIdx = IsRead ? 1 : 0; 71775ffd83dbSDimitry Andric int ValRegIndex = IsRead ? 0 : 1; 71785ffd83dbSDimitry Andric 71795ffd83dbSDimitry Andric Register ValReg = MI.getOperand(ValRegIndex).getReg(); 71805ffd83dbSDimitry Andric const LLT Ty = MRI.getType(ValReg); 71815ffd83dbSDimitry Andric const MDString *RegStr = cast<MDString>( 71825ffd83dbSDimitry Andric cast<MDNode>(MI.getOperand(NameOpIdx).getMetadata())->getOperand(0)); 71835ffd83dbSDimitry Andric 7184e8d8bef9SDimitry Andric Register PhysReg = TLI.getRegisterByName(RegStr->getString().data(), Ty, MF); 71855ffd83dbSDimitry Andric if (!PhysReg.isValid()) 7186480093f4SDimitry Andric return UnableToLegalize; 7187480093f4SDimitry Andric 71885ffd83dbSDimitry Andric if (IsRead) 71895ffd83dbSDimitry Andric MIRBuilder.buildCopy(ValReg, PhysReg); 71905ffd83dbSDimitry Andric else 71915ffd83dbSDimitry Andric MIRBuilder.buildCopy(PhysReg, ValReg); 71925ffd83dbSDimitry Andric 7193480093f4SDimitry Andric MI.eraseFromParent(); 7194480093f4SDimitry Andric return Legalized; 7195480093f4SDimitry Andric } 7196e8d8bef9SDimitry Andric 7197e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7198e8d8bef9SDimitry Andric LegalizerHelper::lowerSMULH_UMULH(MachineInstr &MI) { 7199e8d8bef9SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SMULH; 7200e8d8bef9SDimitry Andric unsigned ExtOp = IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 7201e8d8bef9SDimitry Andric Register Result = MI.getOperand(0).getReg(); 7202e8d8bef9SDimitry Andric LLT OrigTy = MRI.getType(Result); 7203e8d8bef9SDimitry Andric auto SizeInBits = OrigTy.getScalarSizeInBits(); 7204e8d8bef9SDimitry Andric LLT WideTy = OrigTy.changeElementSize(SizeInBits * 2); 7205e8d8bef9SDimitry Andric 7206e8d8bef9SDimitry Andric auto LHS = MIRBuilder.buildInstr(ExtOp, {WideTy}, {MI.getOperand(1)}); 7207e8d8bef9SDimitry Andric auto RHS = MIRBuilder.buildInstr(ExtOp, {WideTy}, {MI.getOperand(2)}); 7208e8d8bef9SDimitry Andric auto Mul = MIRBuilder.buildMul(WideTy, LHS, RHS); 7209e8d8bef9SDimitry Andric unsigned ShiftOp = IsSigned ? TargetOpcode::G_ASHR : TargetOpcode::G_LSHR; 7210e8d8bef9SDimitry Andric 7211e8d8bef9SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, SizeInBits); 7212e8d8bef9SDimitry Andric auto Shifted = MIRBuilder.buildInstr(ShiftOp, {WideTy}, {Mul, ShiftAmt}); 7213e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(Result, Shifted); 7214e8d8bef9SDimitry Andric 7215e8d8bef9SDimitry Andric MI.eraseFromParent(); 7216e8d8bef9SDimitry Andric return Legalized; 7217e8d8bef9SDimitry Andric } 7218e8d8bef9SDimitry Andric 7219e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerSelect(MachineInstr &MI) { 7220e8d8bef9SDimitry Andric // Implement vector G_SELECT in terms of XOR, AND, OR. 7221e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 7222e8d8bef9SDimitry Andric Register MaskReg = MI.getOperand(1).getReg(); 7223e8d8bef9SDimitry Andric Register Op1Reg = MI.getOperand(2).getReg(); 7224e8d8bef9SDimitry Andric Register Op2Reg = MI.getOperand(3).getReg(); 7225e8d8bef9SDimitry Andric LLT DstTy = MRI.getType(DstReg); 7226e8d8bef9SDimitry Andric LLT MaskTy = MRI.getType(MaskReg); 7227e8d8bef9SDimitry Andric if (!DstTy.isVector()) 7228e8d8bef9SDimitry Andric return UnableToLegalize; 7229e8d8bef9SDimitry Andric 7230e8d8bef9SDimitry Andric if (MaskTy.isScalar()) { 723181ad6265SDimitry Andric // Turn the scalar condition into a vector condition mask. 723281ad6265SDimitry Andric 7233e8d8bef9SDimitry Andric Register MaskElt = MaskReg; 723481ad6265SDimitry Andric 723581ad6265SDimitry Andric // The condition was potentially zero extended before, but we want a sign 723681ad6265SDimitry Andric // extended boolean. 723781ad6265SDimitry Andric if (MaskTy.getSizeInBits() <= DstTy.getScalarSizeInBits() && 723881ad6265SDimitry Andric MaskTy != LLT::scalar(1)) { 723981ad6265SDimitry Andric MaskElt = MIRBuilder.buildSExtInReg(MaskTy, MaskElt, 1).getReg(0); 7240e8d8bef9SDimitry Andric } 7241e8d8bef9SDimitry Andric 724281ad6265SDimitry Andric // Continue the sign extension (or truncate) to match the data type. 724381ad6265SDimitry Andric MaskElt = MIRBuilder.buildSExtOrTrunc(DstTy.getElementType(), 724481ad6265SDimitry Andric MaskElt).getReg(0); 724581ad6265SDimitry Andric 724681ad6265SDimitry Andric // Generate a vector splat idiom. 724781ad6265SDimitry Andric auto ShufSplat = MIRBuilder.buildShuffleSplat(DstTy, MaskElt); 724881ad6265SDimitry Andric MaskReg = ShufSplat.getReg(0); 724981ad6265SDimitry Andric MaskTy = DstTy; 725081ad6265SDimitry Andric } 725181ad6265SDimitry Andric 725281ad6265SDimitry Andric if (MaskTy.getSizeInBits() != DstTy.getSizeInBits()) { 7253e8d8bef9SDimitry Andric return UnableToLegalize; 7254e8d8bef9SDimitry Andric } 7255e8d8bef9SDimitry Andric 7256e8d8bef9SDimitry Andric auto NotMask = MIRBuilder.buildNot(MaskTy, MaskReg); 7257e8d8bef9SDimitry Andric auto NewOp1 = MIRBuilder.buildAnd(MaskTy, Op1Reg, MaskReg); 7258e8d8bef9SDimitry Andric auto NewOp2 = MIRBuilder.buildAnd(MaskTy, Op2Reg, NotMask); 7259e8d8bef9SDimitry Andric MIRBuilder.buildOr(DstReg, NewOp1, NewOp2); 7260e8d8bef9SDimitry Andric MI.eraseFromParent(); 7261e8d8bef9SDimitry Andric return Legalized; 7262e8d8bef9SDimitry Andric } 7263fe6060f1SDimitry Andric 7264fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerDIVREM(MachineInstr &MI) { 7265fe6060f1SDimitry Andric // Split DIVREM into individual instructions. 7266fe6060f1SDimitry Andric unsigned Opcode = MI.getOpcode(); 7267fe6060f1SDimitry Andric 7268fe6060f1SDimitry Andric MIRBuilder.buildInstr( 7269fe6060f1SDimitry Andric Opcode == TargetOpcode::G_SDIVREM ? TargetOpcode::G_SDIV 7270fe6060f1SDimitry Andric : TargetOpcode::G_UDIV, 7271fe6060f1SDimitry Andric {MI.getOperand(0).getReg()}, {MI.getOperand(2), MI.getOperand(3)}); 7272fe6060f1SDimitry Andric MIRBuilder.buildInstr( 7273fe6060f1SDimitry Andric Opcode == TargetOpcode::G_SDIVREM ? TargetOpcode::G_SREM 7274fe6060f1SDimitry Andric : TargetOpcode::G_UREM, 7275fe6060f1SDimitry Andric {MI.getOperand(1).getReg()}, {MI.getOperand(2), MI.getOperand(3)}); 7276fe6060f1SDimitry Andric MI.eraseFromParent(); 7277fe6060f1SDimitry Andric return Legalized; 7278fe6060f1SDimitry Andric } 7279fe6060f1SDimitry Andric 7280fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 7281fe6060f1SDimitry Andric LegalizerHelper::lowerAbsToAddXor(MachineInstr &MI) { 7282fe6060f1SDimitry Andric // Expand %res = G_ABS %a into: 7283fe6060f1SDimitry Andric // %v1 = G_ASHR %a, scalar_size-1 7284fe6060f1SDimitry Andric // %v2 = G_ADD %a, %v1 7285fe6060f1SDimitry Andric // %res = G_XOR %v2, %v1 7286fe6060f1SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 7287fe6060f1SDimitry Andric Register OpReg = MI.getOperand(1).getReg(); 7288fe6060f1SDimitry Andric auto ShiftAmt = 7289fe6060f1SDimitry Andric MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - 1); 7290fe6060f1SDimitry Andric auto Shift = MIRBuilder.buildAShr(DstTy, OpReg, ShiftAmt); 7291fe6060f1SDimitry Andric auto Add = MIRBuilder.buildAdd(DstTy, OpReg, Shift); 7292fe6060f1SDimitry Andric MIRBuilder.buildXor(MI.getOperand(0).getReg(), Add, Shift); 7293fe6060f1SDimitry Andric MI.eraseFromParent(); 7294fe6060f1SDimitry Andric return Legalized; 7295fe6060f1SDimitry Andric } 7296fe6060f1SDimitry Andric 7297fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 7298fe6060f1SDimitry Andric LegalizerHelper::lowerAbsToMaxNeg(MachineInstr &MI) { 7299fe6060f1SDimitry Andric // Expand %res = G_ABS %a into: 7300fe6060f1SDimitry Andric // %v1 = G_CONSTANT 0 7301fe6060f1SDimitry Andric // %v2 = G_SUB %v1, %a 7302fe6060f1SDimitry Andric // %res = G_SMAX %a, %v2 7303fe6060f1SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 7304fe6060f1SDimitry Andric LLT Ty = MRI.getType(SrcReg); 7305fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0).getReg(0); 7306fe6060f1SDimitry Andric auto Sub = MIRBuilder.buildSub(Ty, Zero, SrcReg).getReg(0); 7307fe6060f1SDimitry Andric MIRBuilder.buildSMax(MI.getOperand(0), SrcReg, Sub); 7308fe6060f1SDimitry Andric MI.eraseFromParent(); 7309fe6060f1SDimitry Andric return Legalized; 7310fe6060f1SDimitry Andric } 7311349cc55cSDimitry Andric 7312349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7313349cc55cSDimitry Andric LegalizerHelper::lowerVectorReduction(MachineInstr &MI) { 7314349cc55cSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 7315349cc55cSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 7316349cc55cSDimitry Andric LLT DstTy = MRI.getType(SrcReg); 7317349cc55cSDimitry Andric 7318349cc55cSDimitry Andric // The source could be a scalar if the IR type was <1 x sN>. 7319349cc55cSDimitry Andric if (SrcTy.isScalar()) { 7320349cc55cSDimitry Andric if (DstTy.getSizeInBits() > SrcTy.getSizeInBits()) 7321349cc55cSDimitry Andric return UnableToLegalize; // FIXME: handle extension. 7322349cc55cSDimitry Andric // This can be just a plain copy. 7323349cc55cSDimitry Andric Observer.changingInstr(MI); 7324349cc55cSDimitry Andric MI.setDesc(MIRBuilder.getTII().get(TargetOpcode::COPY)); 7325349cc55cSDimitry Andric Observer.changedInstr(MI); 7326349cc55cSDimitry Andric return Legalized; 7327349cc55cSDimitry Andric } 7328349cc55cSDimitry Andric return UnableToLegalize;; 7329349cc55cSDimitry Andric } 7330349cc55cSDimitry Andric 7331349cc55cSDimitry Andric static bool shouldLowerMemFuncForSize(const MachineFunction &MF) { 7332349cc55cSDimitry Andric // On Darwin, -Os means optimize for size without hurting performance, so 7333349cc55cSDimitry Andric // only really optimize for size when -Oz (MinSize) is used. 7334349cc55cSDimitry Andric if (MF.getTarget().getTargetTriple().isOSDarwin()) 7335349cc55cSDimitry Andric return MF.getFunction().hasMinSize(); 7336349cc55cSDimitry Andric return MF.getFunction().hasOptSize(); 7337349cc55cSDimitry Andric } 7338349cc55cSDimitry Andric 7339349cc55cSDimitry Andric // Returns a list of types to use for memory op lowering in MemOps. A partial 7340349cc55cSDimitry Andric // port of findOptimalMemOpLowering in TargetLowering. 7341349cc55cSDimitry Andric static bool findGISelOptimalMemOpLowering(std::vector<LLT> &MemOps, 7342349cc55cSDimitry Andric unsigned Limit, const MemOp &Op, 7343349cc55cSDimitry Andric unsigned DstAS, unsigned SrcAS, 7344349cc55cSDimitry Andric const AttributeList &FuncAttributes, 7345349cc55cSDimitry Andric const TargetLowering &TLI) { 7346349cc55cSDimitry Andric if (Op.isMemcpyWithFixedDstAlign() && Op.getSrcAlign() < Op.getDstAlign()) 7347349cc55cSDimitry Andric return false; 7348349cc55cSDimitry Andric 7349349cc55cSDimitry Andric LLT Ty = TLI.getOptimalMemOpLLT(Op, FuncAttributes); 7350349cc55cSDimitry Andric 7351349cc55cSDimitry Andric if (Ty == LLT()) { 7352349cc55cSDimitry Andric // Use the largest scalar type whose alignment constraints are satisfied. 7353349cc55cSDimitry Andric // We only need to check DstAlign here as SrcAlign is always greater or 7354349cc55cSDimitry Andric // equal to DstAlign (or zero). 7355349cc55cSDimitry Andric Ty = LLT::scalar(64); 7356349cc55cSDimitry Andric if (Op.isFixedDstAlign()) 7357349cc55cSDimitry Andric while (Op.getDstAlign() < Ty.getSizeInBytes() && 7358349cc55cSDimitry Andric !TLI.allowsMisalignedMemoryAccesses(Ty, DstAS, Op.getDstAlign())) 7359349cc55cSDimitry Andric Ty = LLT::scalar(Ty.getSizeInBytes()); 7360349cc55cSDimitry Andric assert(Ty.getSizeInBits() > 0 && "Could not find valid type"); 7361349cc55cSDimitry Andric // FIXME: check for the largest legal type we can load/store to. 7362349cc55cSDimitry Andric } 7363349cc55cSDimitry Andric 7364349cc55cSDimitry Andric unsigned NumMemOps = 0; 7365349cc55cSDimitry Andric uint64_t Size = Op.size(); 7366349cc55cSDimitry Andric while (Size) { 7367349cc55cSDimitry Andric unsigned TySize = Ty.getSizeInBytes(); 7368349cc55cSDimitry Andric while (TySize > Size) { 7369349cc55cSDimitry Andric // For now, only use non-vector load / store's for the left-over pieces. 7370349cc55cSDimitry Andric LLT NewTy = Ty; 7371349cc55cSDimitry Andric // FIXME: check for mem op safety and legality of the types. Not all of 7372349cc55cSDimitry Andric // SDAGisms map cleanly to GISel concepts. 7373349cc55cSDimitry Andric if (NewTy.isVector()) 7374349cc55cSDimitry Andric NewTy = NewTy.getSizeInBits() > 64 ? LLT::scalar(64) : LLT::scalar(32); 7375349cc55cSDimitry Andric NewTy = LLT::scalar(PowerOf2Floor(NewTy.getSizeInBits() - 1)); 7376349cc55cSDimitry Andric unsigned NewTySize = NewTy.getSizeInBytes(); 7377349cc55cSDimitry Andric assert(NewTySize > 0 && "Could not find appropriate type"); 7378349cc55cSDimitry Andric 7379349cc55cSDimitry Andric // If the new LLT cannot cover all of the remaining bits, then consider 7380349cc55cSDimitry Andric // issuing a (or a pair of) unaligned and overlapping load / store. 7381349cc55cSDimitry Andric bool Fast; 7382349cc55cSDimitry Andric // Need to get a VT equivalent for allowMisalignedMemoryAccesses(). 7383349cc55cSDimitry Andric MVT VT = getMVTForLLT(Ty); 7384349cc55cSDimitry Andric if (NumMemOps && Op.allowOverlap() && NewTySize < Size && 7385349cc55cSDimitry Andric TLI.allowsMisalignedMemoryAccesses( 7386349cc55cSDimitry Andric VT, DstAS, Op.isFixedDstAlign() ? Op.getDstAlign() : Align(1), 7387349cc55cSDimitry Andric MachineMemOperand::MONone, &Fast) && 7388349cc55cSDimitry Andric Fast) 7389349cc55cSDimitry Andric TySize = Size; 7390349cc55cSDimitry Andric else { 7391349cc55cSDimitry Andric Ty = NewTy; 7392349cc55cSDimitry Andric TySize = NewTySize; 7393349cc55cSDimitry Andric } 7394349cc55cSDimitry Andric } 7395349cc55cSDimitry Andric 7396349cc55cSDimitry Andric if (++NumMemOps > Limit) 7397349cc55cSDimitry Andric return false; 7398349cc55cSDimitry Andric 7399349cc55cSDimitry Andric MemOps.push_back(Ty); 7400349cc55cSDimitry Andric Size -= TySize; 7401349cc55cSDimitry Andric } 7402349cc55cSDimitry Andric 7403349cc55cSDimitry Andric return true; 7404349cc55cSDimitry Andric } 7405349cc55cSDimitry Andric 7406349cc55cSDimitry Andric static Type *getTypeForLLT(LLT Ty, LLVMContext &C) { 7407349cc55cSDimitry Andric if (Ty.isVector()) 7408349cc55cSDimitry Andric return FixedVectorType::get(IntegerType::get(C, Ty.getScalarSizeInBits()), 7409349cc55cSDimitry Andric Ty.getNumElements()); 7410349cc55cSDimitry Andric return IntegerType::get(C, Ty.getSizeInBits()); 7411349cc55cSDimitry Andric } 7412349cc55cSDimitry Andric 7413349cc55cSDimitry Andric // Get a vectorized representation of the memset value operand, GISel edition. 7414349cc55cSDimitry Andric static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB) { 7415349cc55cSDimitry Andric MachineRegisterInfo &MRI = *MIB.getMRI(); 7416349cc55cSDimitry Andric unsigned NumBits = Ty.getScalarSizeInBits(); 7417349cc55cSDimitry Andric auto ValVRegAndVal = getIConstantVRegValWithLookThrough(Val, MRI); 7418349cc55cSDimitry Andric if (!Ty.isVector() && ValVRegAndVal) { 741981ad6265SDimitry Andric APInt Scalar = ValVRegAndVal->Value.trunc(8); 7420349cc55cSDimitry Andric APInt SplatVal = APInt::getSplat(NumBits, Scalar); 7421349cc55cSDimitry Andric return MIB.buildConstant(Ty, SplatVal).getReg(0); 7422349cc55cSDimitry Andric } 7423349cc55cSDimitry Andric 7424349cc55cSDimitry Andric // Extend the byte value to the larger type, and then multiply by a magic 7425349cc55cSDimitry Andric // value 0x010101... in order to replicate it across every byte. 7426349cc55cSDimitry Andric // Unless it's zero, in which case just emit a larger G_CONSTANT 0. 7427349cc55cSDimitry Andric if (ValVRegAndVal && ValVRegAndVal->Value == 0) { 7428349cc55cSDimitry Andric return MIB.buildConstant(Ty, 0).getReg(0); 7429349cc55cSDimitry Andric } 7430349cc55cSDimitry Andric 7431349cc55cSDimitry Andric LLT ExtType = Ty.getScalarType(); 7432349cc55cSDimitry Andric auto ZExt = MIB.buildZExtOrTrunc(ExtType, Val); 7433349cc55cSDimitry Andric if (NumBits > 8) { 7434349cc55cSDimitry Andric APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01)); 7435349cc55cSDimitry Andric auto MagicMI = MIB.buildConstant(ExtType, Magic); 7436349cc55cSDimitry Andric Val = MIB.buildMul(ExtType, ZExt, MagicMI).getReg(0); 7437349cc55cSDimitry Andric } 7438349cc55cSDimitry Andric 7439349cc55cSDimitry Andric // For vector types create a G_BUILD_VECTOR. 7440349cc55cSDimitry Andric if (Ty.isVector()) 7441349cc55cSDimitry Andric Val = MIB.buildSplatVector(Ty, Val).getReg(0); 7442349cc55cSDimitry Andric 7443349cc55cSDimitry Andric return Val; 7444349cc55cSDimitry Andric } 7445349cc55cSDimitry Andric 7446349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7447349cc55cSDimitry Andric LegalizerHelper::lowerMemset(MachineInstr &MI, Register Dst, Register Val, 7448349cc55cSDimitry Andric uint64_t KnownLen, Align Alignment, 7449349cc55cSDimitry Andric bool IsVolatile) { 7450349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7451349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7452349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7453349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7454349cc55cSDimitry Andric 7455349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memset length!"); 7456349cc55cSDimitry Andric 7457349cc55cSDimitry Andric bool DstAlignCanChange = false; 7458349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7459349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 7460349cc55cSDimitry Andric 7461349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7462349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7463349cc55cSDimitry Andric DstAlignCanChange = true; 7464349cc55cSDimitry Andric 7465349cc55cSDimitry Andric unsigned Limit = TLI.getMaxStoresPerMemset(OptSize); 7466349cc55cSDimitry Andric std::vector<LLT> MemOps; 7467349cc55cSDimitry Andric 7468349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7469349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7470349cc55cSDimitry Andric 7471349cc55cSDimitry Andric auto ValVRegAndVal = getIConstantVRegValWithLookThrough(Val, MRI); 7472349cc55cSDimitry Andric bool IsZeroVal = ValVRegAndVal && ValVRegAndVal->Value == 0; 7473349cc55cSDimitry Andric 7474349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering(MemOps, Limit, 7475349cc55cSDimitry Andric MemOp::Set(KnownLen, DstAlignCanChange, 7476349cc55cSDimitry Andric Alignment, 7477349cc55cSDimitry Andric /*IsZeroMemset=*/IsZeroVal, 7478349cc55cSDimitry Andric /*IsVolatile=*/IsVolatile), 7479349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), ~0u, 7480349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7481349cc55cSDimitry Andric return UnableToLegalize; 7482349cc55cSDimitry Andric 7483349cc55cSDimitry Andric if (DstAlignCanChange) { 7484349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7485349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7486349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7487349cc55cSDimitry Andric if (NewAlign > Alignment) { 7488349cc55cSDimitry Andric Alignment = NewAlign; 7489349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7490349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7491349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7492349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7493349cc55cSDimitry Andric } 7494349cc55cSDimitry Andric } 7495349cc55cSDimitry Andric 7496349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7497349cc55cSDimitry Andric // Find the largest store and generate the bit pattern for it. 7498349cc55cSDimitry Andric LLT LargestTy = MemOps[0]; 7499349cc55cSDimitry Andric for (unsigned i = 1; i < MemOps.size(); i++) 7500349cc55cSDimitry Andric if (MemOps[i].getSizeInBits() > LargestTy.getSizeInBits()) 7501349cc55cSDimitry Andric LargestTy = MemOps[i]; 7502349cc55cSDimitry Andric 7503349cc55cSDimitry Andric // The memset stored value is always defined as an s8, so in order to make it 7504349cc55cSDimitry Andric // work with larger store types we need to repeat the bit pattern across the 7505349cc55cSDimitry Andric // wider type. 7506349cc55cSDimitry Andric Register MemSetValue = getMemsetValue(Val, LargestTy, MIB); 7507349cc55cSDimitry Andric 7508349cc55cSDimitry Andric if (!MemSetValue) 7509349cc55cSDimitry Andric return UnableToLegalize; 7510349cc55cSDimitry Andric 7511349cc55cSDimitry Andric // Generate the stores. For each store type in the list, we generate the 7512349cc55cSDimitry Andric // matching store of that type to the destination address. 7513349cc55cSDimitry Andric LLT PtrTy = MRI.getType(Dst); 7514349cc55cSDimitry Andric unsigned DstOff = 0; 7515349cc55cSDimitry Andric unsigned Size = KnownLen; 7516349cc55cSDimitry Andric for (unsigned I = 0; I < MemOps.size(); I++) { 7517349cc55cSDimitry Andric LLT Ty = MemOps[I]; 7518349cc55cSDimitry Andric unsigned TySize = Ty.getSizeInBytes(); 7519349cc55cSDimitry Andric if (TySize > Size) { 7520349cc55cSDimitry Andric // Issuing an unaligned load / store pair that overlaps with the previous 7521349cc55cSDimitry Andric // pair. Adjust the offset accordingly. 7522349cc55cSDimitry Andric assert(I == MemOps.size() - 1 && I != 0); 7523349cc55cSDimitry Andric DstOff -= TySize - Size; 7524349cc55cSDimitry Andric } 7525349cc55cSDimitry Andric 7526349cc55cSDimitry Andric // If this store is smaller than the largest store see whether we can get 7527349cc55cSDimitry Andric // the smaller value for free with a truncate. 7528349cc55cSDimitry Andric Register Value = MemSetValue; 7529349cc55cSDimitry Andric if (Ty.getSizeInBits() < LargestTy.getSizeInBits()) { 7530349cc55cSDimitry Andric MVT VT = getMVTForLLT(Ty); 7531349cc55cSDimitry Andric MVT LargestVT = getMVTForLLT(LargestTy); 7532349cc55cSDimitry Andric if (!LargestTy.isVector() && !Ty.isVector() && 7533349cc55cSDimitry Andric TLI.isTruncateFree(LargestVT, VT)) 7534349cc55cSDimitry Andric Value = MIB.buildTrunc(Ty, MemSetValue).getReg(0); 7535349cc55cSDimitry Andric else 7536349cc55cSDimitry Andric Value = getMemsetValue(Val, Ty, MIB); 7537349cc55cSDimitry Andric if (!Value) 7538349cc55cSDimitry Andric return UnableToLegalize; 7539349cc55cSDimitry Andric } 7540349cc55cSDimitry Andric 7541349cc55cSDimitry Andric auto *StoreMMO = MF.getMachineMemOperand(&DstMMO, DstOff, Ty); 7542349cc55cSDimitry Andric 7543349cc55cSDimitry Andric Register Ptr = Dst; 7544349cc55cSDimitry Andric if (DstOff != 0) { 7545349cc55cSDimitry Andric auto Offset = 7546349cc55cSDimitry Andric MIB.buildConstant(LLT::scalar(PtrTy.getSizeInBits()), DstOff); 7547349cc55cSDimitry Andric Ptr = MIB.buildPtrAdd(PtrTy, Dst, Offset).getReg(0); 7548349cc55cSDimitry Andric } 7549349cc55cSDimitry Andric 7550349cc55cSDimitry Andric MIB.buildStore(Value, Ptr, *StoreMMO); 7551349cc55cSDimitry Andric DstOff += Ty.getSizeInBytes(); 7552349cc55cSDimitry Andric Size -= TySize; 7553349cc55cSDimitry Andric } 7554349cc55cSDimitry Andric 7555349cc55cSDimitry Andric MI.eraseFromParent(); 7556349cc55cSDimitry Andric return Legalized; 7557349cc55cSDimitry Andric } 7558349cc55cSDimitry Andric 7559349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7560349cc55cSDimitry Andric LegalizerHelper::lowerMemcpyInline(MachineInstr &MI) { 7561349cc55cSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_MEMCPY_INLINE); 7562349cc55cSDimitry Andric 7563349cc55cSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7564349cc55cSDimitry Andric Register Src = MI.getOperand(1).getReg(); 7565349cc55cSDimitry Andric Register Len = MI.getOperand(2).getReg(); 7566349cc55cSDimitry Andric 7567349cc55cSDimitry Andric const auto *MMOIt = MI.memoperands_begin(); 7568349cc55cSDimitry Andric const MachineMemOperand *MemOp = *MMOIt; 7569349cc55cSDimitry Andric bool IsVolatile = MemOp->isVolatile(); 7570349cc55cSDimitry Andric 7571349cc55cSDimitry Andric // See if this is a constant length copy 7572349cc55cSDimitry Andric auto LenVRegAndVal = getIConstantVRegValWithLookThrough(Len, MRI); 7573349cc55cSDimitry Andric // FIXME: support dynamically sized G_MEMCPY_INLINE 757481ad6265SDimitry Andric assert(LenVRegAndVal && 7575349cc55cSDimitry Andric "inline memcpy with dynamic size is not yet supported"); 7576349cc55cSDimitry Andric uint64_t KnownLen = LenVRegAndVal->Value.getZExtValue(); 7577349cc55cSDimitry Andric if (KnownLen == 0) { 7578349cc55cSDimitry Andric MI.eraseFromParent(); 7579349cc55cSDimitry Andric return Legalized; 7580349cc55cSDimitry Andric } 7581349cc55cSDimitry Andric 7582349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7583349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7584349cc55cSDimitry Andric Align DstAlign = DstMMO.getBaseAlign(); 7585349cc55cSDimitry Andric Align SrcAlign = SrcMMO.getBaseAlign(); 7586349cc55cSDimitry Andric 7587349cc55cSDimitry Andric return lowerMemcpyInline(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, 7588349cc55cSDimitry Andric IsVolatile); 7589349cc55cSDimitry Andric } 7590349cc55cSDimitry Andric 7591349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7592349cc55cSDimitry Andric LegalizerHelper::lowerMemcpyInline(MachineInstr &MI, Register Dst, Register Src, 7593349cc55cSDimitry Andric uint64_t KnownLen, Align DstAlign, 7594349cc55cSDimitry Andric Align SrcAlign, bool IsVolatile) { 7595349cc55cSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_MEMCPY_INLINE); 7596349cc55cSDimitry Andric return lowerMemcpy(MI, Dst, Src, KnownLen, 7597349cc55cSDimitry Andric std::numeric_limits<uint64_t>::max(), DstAlign, SrcAlign, 7598349cc55cSDimitry Andric IsVolatile); 7599349cc55cSDimitry Andric } 7600349cc55cSDimitry Andric 7601349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7602349cc55cSDimitry Andric LegalizerHelper::lowerMemcpy(MachineInstr &MI, Register Dst, Register Src, 7603349cc55cSDimitry Andric uint64_t KnownLen, uint64_t Limit, Align DstAlign, 7604349cc55cSDimitry Andric Align SrcAlign, bool IsVolatile) { 7605349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7606349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7607349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7608349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7609349cc55cSDimitry Andric 7610349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memcpy length!"); 7611349cc55cSDimitry Andric 7612349cc55cSDimitry Andric bool DstAlignCanChange = false; 7613349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 761481ad6265SDimitry Andric Align Alignment = std::min(DstAlign, SrcAlign); 7615349cc55cSDimitry Andric 7616349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7617349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7618349cc55cSDimitry Andric DstAlignCanChange = true; 7619349cc55cSDimitry Andric 7620349cc55cSDimitry Andric // FIXME: infer better src pointer alignment like SelectionDAG does here. 7621349cc55cSDimitry Andric // FIXME: also use the equivalent of isMemSrcFromConstant and alwaysinlining 7622349cc55cSDimitry Andric // if the memcpy is in a tail call position. 7623349cc55cSDimitry Andric 7624349cc55cSDimitry Andric std::vector<LLT> MemOps; 7625349cc55cSDimitry Andric 7626349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7627349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7628349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7629349cc55cSDimitry Andric MachinePointerInfo SrcPtrInfo = SrcMMO.getPointerInfo(); 7630349cc55cSDimitry Andric 7631349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering( 7632349cc55cSDimitry Andric MemOps, Limit, 7633349cc55cSDimitry Andric MemOp::Copy(KnownLen, DstAlignCanChange, Alignment, SrcAlign, 7634349cc55cSDimitry Andric IsVolatile), 7635349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(), 7636349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7637349cc55cSDimitry Andric return UnableToLegalize; 7638349cc55cSDimitry Andric 7639349cc55cSDimitry Andric if (DstAlignCanChange) { 7640349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7641349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7642349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7643349cc55cSDimitry Andric 7644349cc55cSDimitry Andric // Don't promote to an alignment that would require dynamic stack 7645349cc55cSDimitry Andric // realignment. 7646349cc55cSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 7647349cc55cSDimitry Andric if (!TRI->hasStackRealignment(MF)) 7648349cc55cSDimitry Andric while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) 764981ad6265SDimitry Andric NewAlign = NewAlign.previous(); 7650349cc55cSDimitry Andric 7651349cc55cSDimitry Andric if (NewAlign > Alignment) { 7652349cc55cSDimitry Andric Alignment = NewAlign; 7653349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7654349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7655349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7656349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7657349cc55cSDimitry Andric } 7658349cc55cSDimitry Andric } 7659349cc55cSDimitry Andric 7660349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Inlining memcpy: " << MI << " into loads & stores\n"); 7661349cc55cSDimitry Andric 7662349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7663349cc55cSDimitry Andric // Now we need to emit a pair of load and stores for each of the types we've 7664349cc55cSDimitry Andric // collected. I.e. for each type, generate a load from the source pointer of 7665349cc55cSDimitry Andric // that type width, and then generate a corresponding store to the dest buffer 7666349cc55cSDimitry Andric // of that value loaded. This can result in a sequence of loads and stores 7667349cc55cSDimitry Andric // mixed types, depending on what the target specifies as good types to use. 7668349cc55cSDimitry Andric unsigned CurrOffset = 0; 7669349cc55cSDimitry Andric unsigned Size = KnownLen; 7670349cc55cSDimitry Andric for (auto CopyTy : MemOps) { 7671349cc55cSDimitry Andric // Issuing an unaligned load / store pair that overlaps with the previous 7672349cc55cSDimitry Andric // pair. Adjust the offset accordingly. 7673349cc55cSDimitry Andric if (CopyTy.getSizeInBytes() > Size) 7674349cc55cSDimitry Andric CurrOffset -= CopyTy.getSizeInBytes() - Size; 7675349cc55cSDimitry Andric 7676349cc55cSDimitry Andric // Construct MMOs for the accesses. 7677349cc55cSDimitry Andric auto *LoadMMO = 7678349cc55cSDimitry Andric MF.getMachineMemOperand(&SrcMMO, CurrOffset, CopyTy.getSizeInBytes()); 7679349cc55cSDimitry Andric auto *StoreMMO = 7680349cc55cSDimitry Andric MF.getMachineMemOperand(&DstMMO, CurrOffset, CopyTy.getSizeInBytes()); 7681349cc55cSDimitry Andric 7682349cc55cSDimitry Andric // Create the load. 7683349cc55cSDimitry Andric Register LoadPtr = Src; 7684349cc55cSDimitry Andric Register Offset; 7685349cc55cSDimitry Andric if (CurrOffset != 0) { 76864824e7fdSDimitry Andric LLT SrcTy = MRI.getType(Src); 76874824e7fdSDimitry Andric Offset = MIB.buildConstant(LLT::scalar(SrcTy.getSizeInBits()), CurrOffset) 7688349cc55cSDimitry Andric .getReg(0); 76894824e7fdSDimitry Andric LoadPtr = MIB.buildPtrAdd(SrcTy, Src, Offset).getReg(0); 7690349cc55cSDimitry Andric } 7691349cc55cSDimitry Andric auto LdVal = MIB.buildLoad(CopyTy, LoadPtr, *LoadMMO); 7692349cc55cSDimitry Andric 7693349cc55cSDimitry Andric // Create the store. 76944824e7fdSDimitry Andric Register StorePtr = Dst; 76954824e7fdSDimitry Andric if (CurrOffset != 0) { 76964824e7fdSDimitry Andric LLT DstTy = MRI.getType(Dst); 76974824e7fdSDimitry Andric StorePtr = MIB.buildPtrAdd(DstTy, Dst, Offset).getReg(0); 76984824e7fdSDimitry Andric } 7699349cc55cSDimitry Andric MIB.buildStore(LdVal, StorePtr, *StoreMMO); 7700349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7701349cc55cSDimitry Andric Size -= CopyTy.getSizeInBytes(); 7702349cc55cSDimitry Andric } 7703349cc55cSDimitry Andric 7704349cc55cSDimitry Andric MI.eraseFromParent(); 7705349cc55cSDimitry Andric return Legalized; 7706349cc55cSDimitry Andric } 7707349cc55cSDimitry Andric 7708349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7709349cc55cSDimitry Andric LegalizerHelper::lowerMemmove(MachineInstr &MI, Register Dst, Register Src, 7710349cc55cSDimitry Andric uint64_t KnownLen, Align DstAlign, Align SrcAlign, 7711349cc55cSDimitry Andric bool IsVolatile) { 7712349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7713349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7714349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7715349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7716349cc55cSDimitry Andric 7717349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memmove length!"); 7718349cc55cSDimitry Andric 7719349cc55cSDimitry Andric bool DstAlignCanChange = false; 7720349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7721349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 772281ad6265SDimitry Andric Align Alignment = std::min(DstAlign, SrcAlign); 7723349cc55cSDimitry Andric 7724349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7725349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7726349cc55cSDimitry Andric DstAlignCanChange = true; 7727349cc55cSDimitry Andric 7728349cc55cSDimitry Andric unsigned Limit = TLI.getMaxStoresPerMemmove(OptSize); 7729349cc55cSDimitry Andric std::vector<LLT> MemOps; 7730349cc55cSDimitry Andric 7731349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7732349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7733349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7734349cc55cSDimitry Andric MachinePointerInfo SrcPtrInfo = SrcMMO.getPointerInfo(); 7735349cc55cSDimitry Andric 7736349cc55cSDimitry Andric // FIXME: SelectionDAG always passes false for 'AllowOverlap', apparently due 7737349cc55cSDimitry Andric // to a bug in it's findOptimalMemOpLowering implementation. For now do the 7738349cc55cSDimitry Andric // same thing here. 7739349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering( 7740349cc55cSDimitry Andric MemOps, Limit, 7741349cc55cSDimitry Andric MemOp::Copy(KnownLen, DstAlignCanChange, Alignment, SrcAlign, 7742349cc55cSDimitry Andric /*IsVolatile*/ true), 7743349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(), 7744349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7745349cc55cSDimitry Andric return UnableToLegalize; 7746349cc55cSDimitry Andric 7747349cc55cSDimitry Andric if (DstAlignCanChange) { 7748349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7749349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7750349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7751349cc55cSDimitry Andric 7752349cc55cSDimitry Andric // Don't promote to an alignment that would require dynamic stack 7753349cc55cSDimitry Andric // realignment. 7754349cc55cSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 7755349cc55cSDimitry Andric if (!TRI->hasStackRealignment(MF)) 7756349cc55cSDimitry Andric while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) 775781ad6265SDimitry Andric NewAlign = NewAlign.previous(); 7758349cc55cSDimitry Andric 7759349cc55cSDimitry Andric if (NewAlign > Alignment) { 7760349cc55cSDimitry Andric Alignment = NewAlign; 7761349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7762349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7763349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7764349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7765349cc55cSDimitry Andric } 7766349cc55cSDimitry Andric } 7767349cc55cSDimitry Andric 7768349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Inlining memmove: " << MI << " into loads & stores\n"); 7769349cc55cSDimitry Andric 7770349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7771349cc55cSDimitry Andric // Memmove requires that we perform the loads first before issuing the stores. 7772349cc55cSDimitry Andric // Apart from that, this loop is pretty much doing the same thing as the 7773349cc55cSDimitry Andric // memcpy codegen function. 7774349cc55cSDimitry Andric unsigned CurrOffset = 0; 7775349cc55cSDimitry Andric SmallVector<Register, 16> LoadVals; 7776349cc55cSDimitry Andric for (auto CopyTy : MemOps) { 7777349cc55cSDimitry Andric // Construct MMO for the load. 7778349cc55cSDimitry Andric auto *LoadMMO = 7779349cc55cSDimitry Andric MF.getMachineMemOperand(&SrcMMO, CurrOffset, CopyTy.getSizeInBytes()); 7780349cc55cSDimitry Andric 7781349cc55cSDimitry Andric // Create the load. 7782349cc55cSDimitry Andric Register LoadPtr = Src; 7783349cc55cSDimitry Andric if (CurrOffset != 0) { 77844824e7fdSDimitry Andric LLT SrcTy = MRI.getType(Src); 7785349cc55cSDimitry Andric auto Offset = 77864824e7fdSDimitry Andric MIB.buildConstant(LLT::scalar(SrcTy.getSizeInBits()), CurrOffset); 77874824e7fdSDimitry Andric LoadPtr = MIB.buildPtrAdd(SrcTy, Src, Offset).getReg(0); 7788349cc55cSDimitry Andric } 7789349cc55cSDimitry Andric LoadVals.push_back(MIB.buildLoad(CopyTy, LoadPtr, *LoadMMO).getReg(0)); 7790349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7791349cc55cSDimitry Andric } 7792349cc55cSDimitry Andric 7793349cc55cSDimitry Andric CurrOffset = 0; 7794349cc55cSDimitry Andric for (unsigned I = 0; I < MemOps.size(); ++I) { 7795349cc55cSDimitry Andric LLT CopyTy = MemOps[I]; 7796349cc55cSDimitry Andric // Now store the values loaded. 7797349cc55cSDimitry Andric auto *StoreMMO = 7798349cc55cSDimitry Andric MF.getMachineMemOperand(&DstMMO, CurrOffset, CopyTy.getSizeInBytes()); 7799349cc55cSDimitry Andric 7800349cc55cSDimitry Andric Register StorePtr = Dst; 7801349cc55cSDimitry Andric if (CurrOffset != 0) { 78024824e7fdSDimitry Andric LLT DstTy = MRI.getType(Dst); 7803349cc55cSDimitry Andric auto Offset = 78044824e7fdSDimitry Andric MIB.buildConstant(LLT::scalar(DstTy.getSizeInBits()), CurrOffset); 78054824e7fdSDimitry Andric StorePtr = MIB.buildPtrAdd(DstTy, Dst, Offset).getReg(0); 7806349cc55cSDimitry Andric } 7807349cc55cSDimitry Andric MIB.buildStore(LoadVals[I], StorePtr, *StoreMMO); 7808349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7809349cc55cSDimitry Andric } 7810349cc55cSDimitry Andric MI.eraseFromParent(); 7811349cc55cSDimitry Andric return Legalized; 7812349cc55cSDimitry Andric } 7813349cc55cSDimitry Andric 7814349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7815349cc55cSDimitry Andric LegalizerHelper::lowerMemCpyFamily(MachineInstr &MI, unsigned MaxLen) { 7816349cc55cSDimitry Andric const unsigned Opc = MI.getOpcode(); 7817349cc55cSDimitry Andric // This combine is fairly complex so it's not written with a separate 7818349cc55cSDimitry Andric // matcher function. 7819349cc55cSDimitry Andric assert((Opc == TargetOpcode::G_MEMCPY || Opc == TargetOpcode::G_MEMMOVE || 7820349cc55cSDimitry Andric Opc == TargetOpcode::G_MEMSET) && 7821349cc55cSDimitry Andric "Expected memcpy like instruction"); 7822349cc55cSDimitry Andric 7823349cc55cSDimitry Andric auto MMOIt = MI.memoperands_begin(); 7824349cc55cSDimitry Andric const MachineMemOperand *MemOp = *MMOIt; 7825349cc55cSDimitry Andric 7826349cc55cSDimitry Andric Align DstAlign = MemOp->getBaseAlign(); 7827349cc55cSDimitry Andric Align SrcAlign; 7828349cc55cSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7829349cc55cSDimitry Andric Register Src = MI.getOperand(1).getReg(); 7830349cc55cSDimitry Andric Register Len = MI.getOperand(2).getReg(); 7831349cc55cSDimitry Andric 7832349cc55cSDimitry Andric if (Opc != TargetOpcode::G_MEMSET) { 7833349cc55cSDimitry Andric assert(MMOIt != MI.memoperands_end() && "Expected a second MMO on MI"); 7834349cc55cSDimitry Andric MemOp = *(++MMOIt); 7835349cc55cSDimitry Andric SrcAlign = MemOp->getBaseAlign(); 7836349cc55cSDimitry Andric } 7837349cc55cSDimitry Andric 7838349cc55cSDimitry Andric // See if this is a constant length copy 7839349cc55cSDimitry Andric auto LenVRegAndVal = getIConstantVRegValWithLookThrough(Len, MRI); 7840349cc55cSDimitry Andric if (!LenVRegAndVal) 7841349cc55cSDimitry Andric return UnableToLegalize; 7842349cc55cSDimitry Andric uint64_t KnownLen = LenVRegAndVal->Value.getZExtValue(); 7843349cc55cSDimitry Andric 7844349cc55cSDimitry Andric if (KnownLen == 0) { 7845349cc55cSDimitry Andric MI.eraseFromParent(); 7846349cc55cSDimitry Andric return Legalized; 7847349cc55cSDimitry Andric } 7848349cc55cSDimitry Andric 7849349cc55cSDimitry Andric bool IsVolatile = MemOp->isVolatile(); 7850349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMCPY_INLINE) 7851349cc55cSDimitry Andric return lowerMemcpyInline(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, 7852349cc55cSDimitry Andric IsVolatile); 7853349cc55cSDimitry Andric 7854349cc55cSDimitry Andric // Don't try to optimize volatile. 7855349cc55cSDimitry Andric if (IsVolatile) 7856349cc55cSDimitry Andric return UnableToLegalize; 7857349cc55cSDimitry Andric 7858349cc55cSDimitry Andric if (MaxLen && KnownLen > MaxLen) 7859349cc55cSDimitry Andric return UnableToLegalize; 7860349cc55cSDimitry Andric 7861349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMCPY) { 7862349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7863349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7864349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 7865349cc55cSDimitry Andric uint64_t Limit = TLI.getMaxStoresPerMemcpy(OptSize); 7866349cc55cSDimitry Andric return lowerMemcpy(MI, Dst, Src, KnownLen, Limit, DstAlign, SrcAlign, 7867349cc55cSDimitry Andric IsVolatile); 7868349cc55cSDimitry Andric } 7869349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMMOVE) 7870349cc55cSDimitry Andric return lowerMemmove(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, IsVolatile); 7871349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMSET) 7872349cc55cSDimitry Andric return lowerMemset(MI, Dst, Src, KnownLen, DstAlign, IsVolatile); 7873349cc55cSDimitry Andric return UnableToLegalize; 7874349cc55cSDimitry Andric } 7875