10b57cec5SDimitry Andric //===-- llvm/CodeGen/GlobalISel/LegalizerHelper.cpp -----------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file This file implements the LegalizerHelper class to legalize 100b57cec5SDimitry Andric /// individual instructions and the LegalizeMachineIR wrapper pass for the 110b57cec5SDimitry Andric /// primary legalization. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/CallLowering.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 19fe6060f1SDimitry Andric #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h" 20e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" 21fe6060f1SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 238bcb0991SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 26fe6060f1SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 28fe6060f1SDimitry Andric #include "llvm/IR/Instructions.h" 290b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 300b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 310b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 32349cc55cSDimitry Andric #include "llvm/Target/TargetMachine.h" 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric #define DEBUG_TYPE "legalizer" 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric using namespace llvm; 370b57cec5SDimitry Andric using namespace LegalizeActions; 38e8d8bef9SDimitry Andric using namespace MIPatternMatch; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric /// Try to break down \p OrigTy into \p NarrowTy sized pieces. 410b57cec5SDimitry Andric /// 420b57cec5SDimitry Andric /// Returns the number of \p NarrowTy elements needed to reconstruct \p OrigTy, 430b57cec5SDimitry Andric /// with any leftover piece as type \p LeftoverTy 440b57cec5SDimitry Andric /// 450b57cec5SDimitry Andric /// Returns -1 in the first element of the pair if the breakdown is not 460b57cec5SDimitry Andric /// satisfiable. 470b57cec5SDimitry Andric static std::pair<int, int> 480b57cec5SDimitry Andric getNarrowTypeBreakDown(LLT OrigTy, LLT NarrowTy, LLT &LeftoverTy) { 490b57cec5SDimitry Andric assert(!LeftoverTy.isValid() && "this is an out argument"); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric unsigned Size = OrigTy.getSizeInBits(); 520b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 530b57cec5SDimitry Andric unsigned NumParts = Size / NarrowSize; 540b57cec5SDimitry Andric unsigned LeftoverSize = Size - NumParts * NarrowSize; 550b57cec5SDimitry Andric assert(Size > NarrowSize); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric if (LeftoverSize == 0) 580b57cec5SDimitry Andric return {NumParts, 0}; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric if (NarrowTy.isVector()) { 610b57cec5SDimitry Andric unsigned EltSize = OrigTy.getScalarSizeInBits(); 620b57cec5SDimitry Andric if (LeftoverSize % EltSize != 0) 630b57cec5SDimitry Andric return {-1, -1}; 64fe6060f1SDimitry Andric LeftoverTy = LLT::scalarOrVector( 65fe6060f1SDimitry Andric ElementCount::getFixed(LeftoverSize / EltSize), EltSize); 660b57cec5SDimitry Andric } else { 670b57cec5SDimitry Andric LeftoverTy = LLT::scalar(LeftoverSize); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric int NumLeftover = LeftoverSize / LeftoverTy.getSizeInBits(); 710b57cec5SDimitry Andric return std::make_pair(NumParts, NumLeftover); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 745ffd83dbSDimitry Andric static Type *getFloatTypeForLLT(LLVMContext &Ctx, LLT Ty) { 755ffd83dbSDimitry Andric 765ffd83dbSDimitry Andric if (!Ty.isScalar()) 775ffd83dbSDimitry Andric return nullptr; 785ffd83dbSDimitry Andric 795ffd83dbSDimitry Andric switch (Ty.getSizeInBits()) { 805ffd83dbSDimitry Andric case 16: 815ffd83dbSDimitry Andric return Type::getHalfTy(Ctx); 825ffd83dbSDimitry Andric case 32: 835ffd83dbSDimitry Andric return Type::getFloatTy(Ctx); 845ffd83dbSDimitry Andric case 64: 855ffd83dbSDimitry Andric return Type::getDoubleTy(Ctx); 86e8d8bef9SDimitry Andric case 80: 87e8d8bef9SDimitry Andric return Type::getX86_FP80Ty(Ctx); 885ffd83dbSDimitry Andric case 128: 895ffd83dbSDimitry Andric return Type::getFP128Ty(Ctx); 905ffd83dbSDimitry Andric default: 915ffd83dbSDimitry Andric return nullptr; 925ffd83dbSDimitry Andric } 935ffd83dbSDimitry Andric } 945ffd83dbSDimitry Andric 950b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, 960b57cec5SDimitry Andric GISelChangeObserver &Observer, 970b57cec5SDimitry Andric MachineIRBuilder &Builder) 985ffd83dbSDimitry Andric : MIRBuilder(Builder), Observer(Observer), MRI(MF.getRegInfo()), 99e8d8bef9SDimitry Andric LI(*MF.getSubtarget().getLegalizerInfo()), 100e8d8bef9SDimitry Andric TLI(*MF.getSubtarget().getTargetLowering()) { } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric LegalizerHelper::LegalizerHelper(MachineFunction &MF, const LegalizerInfo &LI, 1030b57cec5SDimitry Andric GISelChangeObserver &Observer, 1040b57cec5SDimitry Andric MachineIRBuilder &B) 105e8d8bef9SDimitry Andric : MIRBuilder(B), Observer(Observer), MRI(MF.getRegInfo()), LI(LI), 106e8d8bef9SDimitry Andric TLI(*MF.getSubtarget().getTargetLowering()) { } 107e8d8bef9SDimitry Andric 1080b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 109fe6060f1SDimitry Andric LegalizerHelper::legalizeInstrStep(MachineInstr &MI, 110fe6060f1SDimitry Andric LostDebugLocObserver &LocObserver) { 1115ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing: " << MI); 1125ffd83dbSDimitry Andric 1135ffd83dbSDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_INTRINSIC || 1160b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS) 1175ffd83dbSDimitry Andric return LI.legalizeIntrinsic(*this, MI) ? Legalized : UnableToLegalize; 1180b57cec5SDimitry Andric auto Step = LI.getAction(MI, MRI); 1190b57cec5SDimitry Andric switch (Step.Action) { 1200b57cec5SDimitry Andric case Legal: 1210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Already legal\n"); 1220b57cec5SDimitry Andric return AlreadyLegal; 1230b57cec5SDimitry Andric case Libcall: 1240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Convert to libcall\n"); 125fe6060f1SDimitry Andric return libcall(MI, LocObserver); 1260b57cec5SDimitry Andric case NarrowScalar: 1270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Narrow scalar\n"); 1280b57cec5SDimitry Andric return narrowScalar(MI, Step.TypeIdx, Step.NewType); 1290b57cec5SDimitry Andric case WidenScalar: 1300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Widen scalar\n"); 1310b57cec5SDimitry Andric return widenScalar(MI, Step.TypeIdx, Step.NewType); 1325ffd83dbSDimitry Andric case Bitcast: 1335ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << ".. Bitcast type\n"); 1345ffd83dbSDimitry Andric return bitcast(MI, Step.TypeIdx, Step.NewType); 1350b57cec5SDimitry Andric case Lower: 1360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Lower\n"); 1370b57cec5SDimitry Andric return lower(MI, Step.TypeIdx, Step.NewType); 1380b57cec5SDimitry Andric case FewerElements: 1390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Reduce number of elements\n"); 1400b57cec5SDimitry Andric return fewerElementsVector(MI, Step.TypeIdx, Step.NewType); 1410b57cec5SDimitry Andric case MoreElements: 1420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Increase number of elements\n"); 1430b57cec5SDimitry Andric return moreElementsVector(MI, Step.TypeIdx, Step.NewType); 1440b57cec5SDimitry Andric case Custom: 1450b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Custom legalization\n"); 1465ffd83dbSDimitry Andric return LI.legalizeCustom(*this, MI) ? Legalized : UnableToLegalize; 1470b57cec5SDimitry Andric default: 1480b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ".. Unable to legalize\n"); 1490b57cec5SDimitry Andric return UnableToLegalize; 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric void LegalizerHelper::extractParts(Register Reg, LLT Ty, int NumParts, 1540b57cec5SDimitry Andric SmallVectorImpl<Register> &VRegs) { 1550b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) 1560b57cec5SDimitry Andric VRegs.push_back(MRI.createGenericVirtualRegister(Ty)); 1570b57cec5SDimitry Andric MIRBuilder.buildUnmerge(VRegs, Reg); 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric bool LegalizerHelper::extractParts(Register Reg, LLT RegTy, 1610b57cec5SDimitry Andric LLT MainTy, LLT &LeftoverTy, 1620b57cec5SDimitry Andric SmallVectorImpl<Register> &VRegs, 1630b57cec5SDimitry Andric SmallVectorImpl<Register> &LeftoverRegs) { 1640b57cec5SDimitry Andric assert(!LeftoverTy.isValid() && "this is an out argument"); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric unsigned RegSize = RegTy.getSizeInBits(); 1670b57cec5SDimitry Andric unsigned MainSize = MainTy.getSizeInBits(); 1680b57cec5SDimitry Andric unsigned NumParts = RegSize / MainSize; 1690b57cec5SDimitry Andric unsigned LeftoverSize = RegSize - NumParts * MainSize; 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric // Use an unmerge when possible. 1720b57cec5SDimitry Andric if (LeftoverSize == 0) { 1730b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) 1740b57cec5SDimitry Andric VRegs.push_back(MRI.createGenericVirtualRegister(MainTy)); 1750b57cec5SDimitry Andric MIRBuilder.buildUnmerge(VRegs, Reg); 1760b57cec5SDimitry Andric return true; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric if (MainTy.isVector()) { 1800b57cec5SDimitry Andric unsigned EltSize = MainTy.getScalarSizeInBits(); 1810b57cec5SDimitry Andric if (LeftoverSize % EltSize != 0) 1820b57cec5SDimitry Andric return false; 183fe6060f1SDimitry Andric LeftoverTy = LLT::scalarOrVector( 184fe6060f1SDimitry Andric ElementCount::getFixed(LeftoverSize / EltSize), EltSize); 1850b57cec5SDimitry Andric } else { 1860b57cec5SDimitry Andric LeftoverTy = LLT::scalar(LeftoverSize); 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // For irregular sizes, extract the individual parts. 1900b57cec5SDimitry Andric for (unsigned I = 0; I != NumParts; ++I) { 1910b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(MainTy); 1920b57cec5SDimitry Andric VRegs.push_back(NewReg); 1930b57cec5SDimitry Andric MIRBuilder.buildExtract(NewReg, Reg, MainSize * I); 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric for (unsigned Offset = MainSize * NumParts; Offset < RegSize; 1970b57cec5SDimitry Andric Offset += LeftoverSize) { 1980b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(LeftoverTy); 1990b57cec5SDimitry Andric LeftoverRegs.push_back(NewReg); 2000b57cec5SDimitry Andric MIRBuilder.buildExtract(NewReg, Reg, Offset); 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric return true; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric void LegalizerHelper::insertParts(Register DstReg, 2070b57cec5SDimitry Andric LLT ResultTy, LLT PartTy, 2080b57cec5SDimitry Andric ArrayRef<Register> PartRegs, 2090b57cec5SDimitry Andric LLT LeftoverTy, 2100b57cec5SDimitry Andric ArrayRef<Register> LeftoverRegs) { 2110b57cec5SDimitry Andric if (!LeftoverTy.isValid()) { 2120b57cec5SDimitry Andric assert(LeftoverRegs.empty()); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric if (!ResultTy.isVector()) { 2150b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, PartRegs); 2160b57cec5SDimitry Andric return; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric if (PartTy.isVector()) 2200b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, PartRegs); 2210b57cec5SDimitry Andric else 2220b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, PartRegs); 2230b57cec5SDimitry Andric return; 2240b57cec5SDimitry Andric } 2250b57cec5SDimitry Andric 226fe6060f1SDimitry Andric SmallVector<Register> GCDRegs; 227fe6060f1SDimitry Andric LLT GCDTy = getGCDType(getGCDType(ResultTy, LeftoverTy), PartTy); 228fe6060f1SDimitry Andric for (auto PartReg : concat<const Register>(PartRegs, LeftoverRegs)) 229fe6060f1SDimitry Andric extractGCDType(GCDRegs, GCDTy, PartReg); 230fe6060f1SDimitry Andric LLT ResultLCMTy = buildLCMMergePieces(ResultTy, LeftoverTy, GCDTy, GCDRegs); 231fe6060f1SDimitry Andric buildWidenedRemergeToDst(DstReg, ResultLCMTy, GCDRegs); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 234e8d8bef9SDimitry Andric /// Append the result registers of G_UNMERGE_VALUES \p MI to \p Regs. 2355ffd83dbSDimitry Andric static void getUnmergeResults(SmallVectorImpl<Register> &Regs, 2365ffd83dbSDimitry Andric const MachineInstr &MI) { 2375ffd83dbSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES); 2385ffd83dbSDimitry Andric 239e8d8bef9SDimitry Andric const int StartIdx = Regs.size(); 2405ffd83dbSDimitry Andric const int NumResults = MI.getNumOperands() - 1; 241e8d8bef9SDimitry Andric Regs.resize(Regs.size() + NumResults); 2425ffd83dbSDimitry Andric for (int I = 0; I != NumResults; ++I) 243e8d8bef9SDimitry Andric Regs[StartIdx + I] = MI.getOperand(I).getReg(); 2445ffd83dbSDimitry Andric } 2455ffd83dbSDimitry Andric 246e8d8bef9SDimitry Andric void LegalizerHelper::extractGCDType(SmallVectorImpl<Register> &Parts, 247e8d8bef9SDimitry Andric LLT GCDTy, Register SrcReg) { 2485ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 2495ffd83dbSDimitry Andric if (SrcTy == GCDTy) { 2505ffd83dbSDimitry Andric // If the source already evenly divides the result type, we don't need to do 2515ffd83dbSDimitry Andric // anything. 2525ffd83dbSDimitry Andric Parts.push_back(SrcReg); 2535ffd83dbSDimitry Andric } else { 2545ffd83dbSDimitry Andric // Need to split into common type sized pieces. 2555ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 2565ffd83dbSDimitry Andric getUnmergeResults(Parts, *Unmerge); 2575ffd83dbSDimitry Andric } 258e8d8bef9SDimitry Andric } 2595ffd83dbSDimitry Andric 260e8d8bef9SDimitry Andric LLT LegalizerHelper::extractGCDType(SmallVectorImpl<Register> &Parts, LLT DstTy, 261e8d8bef9SDimitry Andric LLT NarrowTy, Register SrcReg) { 262e8d8bef9SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 263e8d8bef9SDimitry Andric LLT GCDTy = getGCDType(getGCDType(SrcTy, NarrowTy), DstTy); 264e8d8bef9SDimitry Andric extractGCDType(Parts, GCDTy, SrcReg); 2655ffd83dbSDimitry Andric return GCDTy; 2665ffd83dbSDimitry Andric } 2675ffd83dbSDimitry Andric 2685ffd83dbSDimitry Andric LLT LegalizerHelper::buildLCMMergePieces(LLT DstTy, LLT NarrowTy, LLT GCDTy, 2695ffd83dbSDimitry Andric SmallVectorImpl<Register> &VRegs, 2705ffd83dbSDimitry Andric unsigned PadStrategy) { 2715ffd83dbSDimitry Andric LLT LCMTy = getLCMType(DstTy, NarrowTy); 2725ffd83dbSDimitry Andric 2735ffd83dbSDimitry Andric int NumParts = LCMTy.getSizeInBits() / NarrowTy.getSizeInBits(); 2745ffd83dbSDimitry Andric int NumSubParts = NarrowTy.getSizeInBits() / GCDTy.getSizeInBits(); 2755ffd83dbSDimitry Andric int NumOrigSrc = VRegs.size(); 2765ffd83dbSDimitry Andric 2775ffd83dbSDimitry Andric Register PadReg; 2785ffd83dbSDimitry Andric 2795ffd83dbSDimitry Andric // Get a value we can use to pad the source value if the sources won't evenly 2805ffd83dbSDimitry Andric // cover the result type. 2815ffd83dbSDimitry Andric if (NumOrigSrc < NumParts * NumSubParts) { 2825ffd83dbSDimitry Andric if (PadStrategy == TargetOpcode::G_ZEXT) 2835ffd83dbSDimitry Andric PadReg = MIRBuilder.buildConstant(GCDTy, 0).getReg(0); 2845ffd83dbSDimitry Andric else if (PadStrategy == TargetOpcode::G_ANYEXT) 2855ffd83dbSDimitry Andric PadReg = MIRBuilder.buildUndef(GCDTy).getReg(0); 2865ffd83dbSDimitry Andric else { 2875ffd83dbSDimitry Andric assert(PadStrategy == TargetOpcode::G_SEXT); 2885ffd83dbSDimitry Andric 2895ffd83dbSDimitry Andric // Shift the sign bit of the low register through the high register. 2905ffd83dbSDimitry Andric auto ShiftAmt = 2915ffd83dbSDimitry Andric MIRBuilder.buildConstant(LLT::scalar(64), GCDTy.getSizeInBits() - 1); 2925ffd83dbSDimitry Andric PadReg = MIRBuilder.buildAShr(GCDTy, VRegs.back(), ShiftAmt).getReg(0); 2935ffd83dbSDimitry Andric } 2945ffd83dbSDimitry Andric } 2955ffd83dbSDimitry Andric 2965ffd83dbSDimitry Andric // Registers for the final merge to be produced. 2975ffd83dbSDimitry Andric SmallVector<Register, 4> Remerge(NumParts); 2985ffd83dbSDimitry Andric 2995ffd83dbSDimitry Andric // Registers needed for intermediate merges, which will be merged into a 3005ffd83dbSDimitry Andric // source for Remerge. 3015ffd83dbSDimitry Andric SmallVector<Register, 4> SubMerge(NumSubParts); 3025ffd83dbSDimitry Andric 3035ffd83dbSDimitry Andric // Once we've fully read off the end of the original source bits, we can reuse 3045ffd83dbSDimitry Andric // the same high bits for remaining padding elements. 3055ffd83dbSDimitry Andric Register AllPadReg; 3065ffd83dbSDimitry Andric 3075ffd83dbSDimitry Andric // Build merges to the LCM type to cover the original result type. 3085ffd83dbSDimitry Andric for (int I = 0; I != NumParts; ++I) { 3095ffd83dbSDimitry Andric bool AllMergePartsArePadding = true; 3105ffd83dbSDimitry Andric 3115ffd83dbSDimitry Andric // Build the requested merges to the requested type. 3125ffd83dbSDimitry Andric for (int J = 0; J != NumSubParts; ++J) { 3135ffd83dbSDimitry Andric int Idx = I * NumSubParts + J; 3145ffd83dbSDimitry Andric if (Idx >= NumOrigSrc) { 3155ffd83dbSDimitry Andric SubMerge[J] = PadReg; 3165ffd83dbSDimitry Andric continue; 3175ffd83dbSDimitry Andric } 3185ffd83dbSDimitry Andric 3195ffd83dbSDimitry Andric SubMerge[J] = VRegs[Idx]; 3205ffd83dbSDimitry Andric 3215ffd83dbSDimitry Andric // There are meaningful bits here we can't reuse later. 3225ffd83dbSDimitry Andric AllMergePartsArePadding = false; 3235ffd83dbSDimitry Andric } 3245ffd83dbSDimitry Andric 3255ffd83dbSDimitry Andric // If we've filled up a complete piece with padding bits, we can directly 3265ffd83dbSDimitry Andric // emit the natural sized constant if applicable, rather than a merge of 3275ffd83dbSDimitry Andric // smaller constants. 3285ffd83dbSDimitry Andric if (AllMergePartsArePadding && !AllPadReg) { 3295ffd83dbSDimitry Andric if (PadStrategy == TargetOpcode::G_ANYEXT) 3305ffd83dbSDimitry Andric AllPadReg = MIRBuilder.buildUndef(NarrowTy).getReg(0); 3315ffd83dbSDimitry Andric else if (PadStrategy == TargetOpcode::G_ZEXT) 3325ffd83dbSDimitry Andric AllPadReg = MIRBuilder.buildConstant(NarrowTy, 0).getReg(0); 3335ffd83dbSDimitry Andric 3345ffd83dbSDimitry Andric // If this is a sign extension, we can't materialize a trivial constant 3355ffd83dbSDimitry Andric // with the right type and have to produce a merge. 3365ffd83dbSDimitry Andric } 3375ffd83dbSDimitry Andric 3385ffd83dbSDimitry Andric if (AllPadReg) { 3395ffd83dbSDimitry Andric // Avoid creating additional instructions if we're just adding additional 3405ffd83dbSDimitry Andric // copies of padding bits. 3415ffd83dbSDimitry Andric Remerge[I] = AllPadReg; 3425ffd83dbSDimitry Andric continue; 3435ffd83dbSDimitry Andric } 3445ffd83dbSDimitry Andric 3455ffd83dbSDimitry Andric if (NumSubParts == 1) 3465ffd83dbSDimitry Andric Remerge[I] = SubMerge[0]; 3475ffd83dbSDimitry Andric else 3485ffd83dbSDimitry Andric Remerge[I] = MIRBuilder.buildMerge(NarrowTy, SubMerge).getReg(0); 3495ffd83dbSDimitry Andric 3505ffd83dbSDimitry Andric // In the sign extend padding case, re-use the first all-signbit merge. 3515ffd83dbSDimitry Andric if (AllMergePartsArePadding && !AllPadReg) 3525ffd83dbSDimitry Andric AllPadReg = Remerge[I]; 3535ffd83dbSDimitry Andric } 3545ffd83dbSDimitry Andric 3555ffd83dbSDimitry Andric VRegs = std::move(Remerge); 3565ffd83dbSDimitry Andric return LCMTy; 3575ffd83dbSDimitry Andric } 3585ffd83dbSDimitry Andric 3595ffd83dbSDimitry Andric void LegalizerHelper::buildWidenedRemergeToDst(Register DstReg, LLT LCMTy, 3605ffd83dbSDimitry Andric ArrayRef<Register> RemergeRegs) { 3615ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 3625ffd83dbSDimitry Andric 3635ffd83dbSDimitry Andric // Create the merge to the widened source, and extract the relevant bits into 3645ffd83dbSDimitry Andric // the result. 3655ffd83dbSDimitry Andric 3665ffd83dbSDimitry Andric if (DstTy == LCMTy) { 3675ffd83dbSDimitry Andric MIRBuilder.buildMerge(DstReg, RemergeRegs); 3685ffd83dbSDimitry Andric return; 3695ffd83dbSDimitry Andric } 3705ffd83dbSDimitry Andric 3715ffd83dbSDimitry Andric auto Remerge = MIRBuilder.buildMerge(LCMTy, RemergeRegs); 3725ffd83dbSDimitry Andric if (DstTy.isScalar() && LCMTy.isScalar()) { 3735ffd83dbSDimitry Andric MIRBuilder.buildTrunc(DstReg, Remerge); 3745ffd83dbSDimitry Andric return; 3755ffd83dbSDimitry Andric } 3765ffd83dbSDimitry Andric 3775ffd83dbSDimitry Andric if (LCMTy.isVector()) { 378e8d8bef9SDimitry Andric unsigned NumDefs = LCMTy.getSizeInBits() / DstTy.getSizeInBits(); 379e8d8bef9SDimitry Andric SmallVector<Register, 8> UnmergeDefs(NumDefs); 380e8d8bef9SDimitry Andric UnmergeDefs[0] = DstReg; 381e8d8bef9SDimitry Andric for (unsigned I = 1; I != NumDefs; ++I) 382e8d8bef9SDimitry Andric UnmergeDefs[I] = MRI.createGenericVirtualRegister(DstTy); 383e8d8bef9SDimitry Andric 384e8d8bef9SDimitry Andric MIRBuilder.buildUnmerge(UnmergeDefs, 385e8d8bef9SDimitry Andric MIRBuilder.buildMerge(LCMTy, RemergeRegs)); 3865ffd83dbSDimitry Andric return; 3875ffd83dbSDimitry Andric } 3885ffd83dbSDimitry Andric 3895ffd83dbSDimitry Andric llvm_unreachable("unhandled case"); 3905ffd83dbSDimitry Andric } 3915ffd83dbSDimitry Andric 3920b57cec5SDimitry Andric static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) { 393e8d8bef9SDimitry Andric #define RTLIBCASE_INT(LibcallPrefix) \ 3945ffd83dbSDimitry Andric do { \ 3955ffd83dbSDimitry Andric switch (Size) { \ 3965ffd83dbSDimitry Andric case 32: \ 3975ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##32; \ 3985ffd83dbSDimitry Andric case 64: \ 3995ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##64; \ 4005ffd83dbSDimitry Andric case 128: \ 4015ffd83dbSDimitry Andric return RTLIB::LibcallPrefix##128; \ 4025ffd83dbSDimitry Andric default: \ 4035ffd83dbSDimitry Andric llvm_unreachable("unexpected size"); \ 4045ffd83dbSDimitry Andric } \ 4055ffd83dbSDimitry Andric } while (0) 4065ffd83dbSDimitry Andric 407e8d8bef9SDimitry Andric #define RTLIBCASE(LibcallPrefix) \ 408e8d8bef9SDimitry Andric do { \ 409e8d8bef9SDimitry Andric switch (Size) { \ 410e8d8bef9SDimitry Andric case 32: \ 411e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##32; \ 412e8d8bef9SDimitry Andric case 64: \ 413e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##64; \ 414e8d8bef9SDimitry Andric case 80: \ 415e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##80; \ 416e8d8bef9SDimitry Andric case 128: \ 417e8d8bef9SDimitry Andric return RTLIB::LibcallPrefix##128; \ 418e8d8bef9SDimitry Andric default: \ 419e8d8bef9SDimitry Andric llvm_unreachable("unexpected size"); \ 420e8d8bef9SDimitry Andric } \ 421e8d8bef9SDimitry Andric } while (0) 4225ffd83dbSDimitry Andric 4230b57cec5SDimitry Andric switch (Opcode) { 4240b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 425e8d8bef9SDimitry Andric RTLIBCASE_INT(SDIV_I); 4260b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 427e8d8bef9SDimitry Andric RTLIBCASE_INT(UDIV_I); 4280b57cec5SDimitry Andric case TargetOpcode::G_SREM: 429e8d8bef9SDimitry Andric RTLIBCASE_INT(SREM_I); 4300b57cec5SDimitry Andric case TargetOpcode::G_UREM: 431e8d8bef9SDimitry Andric RTLIBCASE_INT(UREM_I); 4320b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 433e8d8bef9SDimitry Andric RTLIBCASE_INT(CTLZ_I); 4340b57cec5SDimitry Andric case TargetOpcode::G_FADD: 4355ffd83dbSDimitry Andric RTLIBCASE(ADD_F); 4360b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 4375ffd83dbSDimitry Andric RTLIBCASE(SUB_F); 4380b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 4395ffd83dbSDimitry Andric RTLIBCASE(MUL_F); 4400b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 4415ffd83dbSDimitry Andric RTLIBCASE(DIV_F); 4420b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 4435ffd83dbSDimitry Andric RTLIBCASE(EXP_F); 4440b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 4455ffd83dbSDimitry Andric RTLIBCASE(EXP2_F); 4460b57cec5SDimitry Andric case TargetOpcode::G_FREM: 4475ffd83dbSDimitry Andric RTLIBCASE(REM_F); 4480b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 4495ffd83dbSDimitry Andric RTLIBCASE(POW_F); 4500b57cec5SDimitry Andric case TargetOpcode::G_FMA: 4515ffd83dbSDimitry Andric RTLIBCASE(FMA_F); 4520b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 4535ffd83dbSDimitry Andric RTLIBCASE(SIN_F); 4540b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 4555ffd83dbSDimitry Andric RTLIBCASE(COS_F); 4560b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 4575ffd83dbSDimitry Andric RTLIBCASE(LOG10_F); 4580b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 4595ffd83dbSDimitry Andric RTLIBCASE(LOG_F); 4600b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 4615ffd83dbSDimitry Andric RTLIBCASE(LOG2_F); 4620b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 4635ffd83dbSDimitry Andric RTLIBCASE(CEIL_F); 4640b57cec5SDimitry Andric case TargetOpcode::G_FFLOOR: 4655ffd83dbSDimitry Andric RTLIBCASE(FLOOR_F); 4665ffd83dbSDimitry Andric case TargetOpcode::G_FMINNUM: 4675ffd83dbSDimitry Andric RTLIBCASE(FMIN_F); 4685ffd83dbSDimitry Andric case TargetOpcode::G_FMAXNUM: 4695ffd83dbSDimitry Andric RTLIBCASE(FMAX_F); 4705ffd83dbSDimitry Andric case TargetOpcode::G_FSQRT: 4715ffd83dbSDimitry Andric RTLIBCASE(SQRT_F); 4725ffd83dbSDimitry Andric case TargetOpcode::G_FRINT: 4735ffd83dbSDimitry Andric RTLIBCASE(RINT_F); 4745ffd83dbSDimitry Andric case TargetOpcode::G_FNEARBYINT: 4755ffd83dbSDimitry Andric RTLIBCASE(NEARBYINT_F); 476e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: 477e8d8bef9SDimitry Andric RTLIBCASE(ROUNDEVEN_F); 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric llvm_unreachable("Unknown libcall function"); 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric 4828bcb0991SDimitry Andric /// True if an instruction is in tail position in its caller. Intended for 4838bcb0991SDimitry Andric /// legalizing libcalls as tail calls when possible. 484fe6060f1SDimitry Andric static bool isLibCallInTailPosition(MachineInstr &MI, 485fe6060f1SDimitry Andric const TargetInstrInfo &TII, 486fe6060f1SDimitry Andric MachineRegisterInfo &MRI) { 4875ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4885ffd83dbSDimitry Andric const Function &F = MBB.getParent()->getFunction(); 4898bcb0991SDimitry Andric 4908bcb0991SDimitry Andric // Conservatively require the attributes of the call to match those of 4918bcb0991SDimitry Andric // the return. Ignore NoAlias and NonNull because they don't affect the 4928bcb0991SDimitry Andric // call sequence. 4938bcb0991SDimitry Andric AttributeList CallerAttrs = F.getAttributes(); 4948bcb0991SDimitry Andric if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex) 4958bcb0991SDimitry Andric .removeAttribute(Attribute::NoAlias) 4968bcb0991SDimitry Andric .removeAttribute(Attribute::NonNull) 4978bcb0991SDimitry Andric .hasAttributes()) 4988bcb0991SDimitry Andric return false; 4998bcb0991SDimitry Andric 5008bcb0991SDimitry Andric // It's not safe to eliminate the sign / zero extension of the return value. 501349cc55cSDimitry Andric if (CallerAttrs.hasRetAttr(Attribute::ZExt) || 502349cc55cSDimitry Andric CallerAttrs.hasRetAttr(Attribute::SExt)) 5038bcb0991SDimitry Andric return false; 5048bcb0991SDimitry Andric 505fe6060f1SDimitry Andric // Only tail call if the following instruction is a standard return or if we 506fe6060f1SDimitry Andric // have a `thisreturn` callee, and a sequence like: 507fe6060f1SDimitry Andric // 508fe6060f1SDimitry Andric // G_MEMCPY %0, %1, %2 509fe6060f1SDimitry Andric // $x0 = COPY %0 510fe6060f1SDimitry Andric // RET_ReallyLR implicit $x0 5115ffd83dbSDimitry Andric auto Next = next_nodbg(MI.getIterator(), MBB.instr_end()); 512fe6060f1SDimitry Andric if (Next != MBB.instr_end() && Next->isCopy()) { 513fe6060f1SDimitry Andric switch (MI.getOpcode()) { 514fe6060f1SDimitry Andric default: 515fe6060f1SDimitry Andric llvm_unreachable("unsupported opcode"); 516fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 517fe6060f1SDimitry Andric return false; 518fe6060f1SDimitry Andric case TargetOpcode::G_MEMCPY: 519fe6060f1SDimitry Andric case TargetOpcode::G_MEMMOVE: 520fe6060f1SDimitry Andric case TargetOpcode::G_MEMSET: 521fe6060f1SDimitry Andric break; 522fe6060f1SDimitry Andric } 523fe6060f1SDimitry Andric 524fe6060f1SDimitry Andric Register VReg = MI.getOperand(0).getReg(); 525fe6060f1SDimitry Andric if (!VReg.isVirtual() || VReg != Next->getOperand(1).getReg()) 526fe6060f1SDimitry Andric return false; 527fe6060f1SDimitry Andric 528fe6060f1SDimitry Andric Register PReg = Next->getOperand(0).getReg(); 529fe6060f1SDimitry Andric if (!PReg.isPhysical()) 530fe6060f1SDimitry Andric return false; 531fe6060f1SDimitry Andric 532fe6060f1SDimitry Andric auto Ret = next_nodbg(Next, MBB.instr_end()); 533fe6060f1SDimitry Andric if (Ret == MBB.instr_end() || !Ret->isReturn()) 534fe6060f1SDimitry Andric return false; 535fe6060f1SDimitry Andric 536fe6060f1SDimitry Andric if (Ret->getNumImplicitOperands() != 1) 537fe6060f1SDimitry Andric return false; 538fe6060f1SDimitry Andric 539fe6060f1SDimitry Andric if (PReg != Ret->getOperand(0).getReg()) 540fe6060f1SDimitry Andric return false; 541fe6060f1SDimitry Andric 542fe6060f1SDimitry Andric // Skip over the COPY that we just validated. 543fe6060f1SDimitry Andric Next = Ret; 544fe6060f1SDimitry Andric } 545fe6060f1SDimitry Andric 5465ffd83dbSDimitry Andric if (Next == MBB.instr_end() || TII.isTailCall(*Next) || !Next->isReturn()) 5478bcb0991SDimitry Andric return false; 5488bcb0991SDimitry Andric 5498bcb0991SDimitry Andric return true; 5508bcb0991SDimitry Andric } 5518bcb0991SDimitry Andric 5520b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 5535ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, const char *Name, 5540b57cec5SDimitry Andric const CallLowering::ArgInfo &Result, 5555ffd83dbSDimitry Andric ArrayRef<CallLowering::ArgInfo> Args, 5565ffd83dbSDimitry Andric const CallingConv::ID CC) { 5570b57cec5SDimitry Andric auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); 5580b57cec5SDimitry Andric 5598bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 5605ffd83dbSDimitry Andric Info.CallConv = CC; 5618bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 5628bcb0991SDimitry Andric Info.OrigRet = Result; 5638bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 5648bcb0991SDimitry Andric if (!CLI.lowerCall(MIRBuilder, Info)) 5650b57cec5SDimitry Andric return LegalizerHelper::UnableToLegalize; 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric return LegalizerHelper::Legalized; 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5705ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 5715ffd83dbSDimitry Andric llvm::createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall, 5725ffd83dbSDimitry Andric const CallLowering::ArgInfo &Result, 5735ffd83dbSDimitry Andric ArrayRef<CallLowering::ArgInfo> Args) { 5745ffd83dbSDimitry Andric auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); 5755ffd83dbSDimitry Andric const char *Name = TLI.getLibcallName(Libcall); 5765ffd83dbSDimitry Andric const CallingConv::ID CC = TLI.getLibcallCallingConv(Libcall); 5775ffd83dbSDimitry Andric return createLibcall(MIRBuilder, Name, Result, Args, CC); 5785ffd83dbSDimitry Andric } 5795ffd83dbSDimitry Andric 5800b57cec5SDimitry Andric // Useful for libcalls where all operands have the same type. 5810b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult 5820b57cec5SDimitry Andric simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size, 5830b57cec5SDimitry Andric Type *OpType) { 5840b57cec5SDimitry Andric auto Libcall = getRTLibDesc(MI.getOpcode(), Size); 5850b57cec5SDimitry Andric 586fe6060f1SDimitry Andric // FIXME: What does the original arg index mean here? 5870b57cec5SDimitry Andric SmallVector<CallLowering::ArgInfo, 3> Args; 588*4824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) 589*4824e7fdSDimitry Andric Args.push_back({MO.getReg(), OpType, 0}); 590fe6060f1SDimitry Andric return createLibcall(MIRBuilder, Libcall, 591fe6060f1SDimitry Andric {MI.getOperand(0).getReg(), OpType, 0}, Args); 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric 5948bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 5958bcb0991SDimitry Andric llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 596fe6060f1SDimitry Andric MachineInstr &MI, LostDebugLocObserver &LocObserver) { 5978bcb0991SDimitry Andric auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); 5988bcb0991SDimitry Andric 5998bcb0991SDimitry Andric SmallVector<CallLowering::ArgInfo, 3> Args; 6008bcb0991SDimitry Andric // Add all the args, except for the last which is an imm denoting 'tail'. 601e8d8bef9SDimitry Andric for (unsigned i = 0; i < MI.getNumOperands() - 1; ++i) { 6028bcb0991SDimitry Andric Register Reg = MI.getOperand(i).getReg(); 6038bcb0991SDimitry Andric 6048bcb0991SDimitry Andric // Need derive an IR type for call lowering. 6058bcb0991SDimitry Andric LLT OpLLT = MRI.getType(Reg); 6068bcb0991SDimitry Andric Type *OpTy = nullptr; 6078bcb0991SDimitry Andric if (OpLLT.isPointer()) 6088bcb0991SDimitry Andric OpTy = Type::getInt8PtrTy(Ctx, OpLLT.getAddressSpace()); 6098bcb0991SDimitry Andric else 6108bcb0991SDimitry Andric OpTy = IntegerType::get(Ctx, OpLLT.getSizeInBits()); 611fe6060f1SDimitry Andric Args.push_back({Reg, OpTy, 0}); 6128bcb0991SDimitry Andric } 6138bcb0991SDimitry Andric 6148bcb0991SDimitry Andric auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); 6158bcb0991SDimitry Andric auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); 6168bcb0991SDimitry Andric RTLIB::Libcall RTLibcall; 617fe6060f1SDimitry Andric unsigned Opc = MI.getOpcode(); 618fe6060f1SDimitry Andric switch (Opc) { 619fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 620fe6060f1SDimitry Andric RTLibcall = RTLIB::BZERO; 621fe6060f1SDimitry Andric break; 622e8d8bef9SDimitry Andric case TargetOpcode::G_MEMCPY: 6238bcb0991SDimitry Andric RTLibcall = RTLIB::MEMCPY; 624fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 6258bcb0991SDimitry Andric break; 626e8d8bef9SDimitry Andric case TargetOpcode::G_MEMMOVE: 6278bcb0991SDimitry Andric RTLibcall = RTLIB::MEMMOVE; 628fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 6298bcb0991SDimitry Andric break; 630e8d8bef9SDimitry Andric case TargetOpcode::G_MEMSET: 631e8d8bef9SDimitry Andric RTLibcall = RTLIB::MEMSET; 632fe6060f1SDimitry Andric Args[0].Flags[0].setReturned(); 633e8d8bef9SDimitry Andric break; 6348bcb0991SDimitry Andric default: 635fe6060f1SDimitry Andric llvm_unreachable("unsupported opcode"); 6368bcb0991SDimitry Andric } 6378bcb0991SDimitry Andric const char *Name = TLI.getLibcallName(RTLibcall); 6388bcb0991SDimitry Andric 639fe6060f1SDimitry Andric // Unsupported libcall on the target. 640fe6060f1SDimitry Andric if (!Name) { 641fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << ".. .. Could not find libcall name for " 642fe6060f1SDimitry Andric << MIRBuilder.getTII().getName(Opc) << "\n"); 643fe6060f1SDimitry Andric return LegalizerHelper::UnableToLegalize; 644fe6060f1SDimitry Andric } 645fe6060f1SDimitry Andric 6468bcb0991SDimitry Andric CallLowering::CallLoweringInfo Info; 6478bcb0991SDimitry Andric Info.CallConv = TLI.getLibcallCallingConv(RTLibcall); 6488bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateES(Name); 649fe6060f1SDimitry Andric Info.OrigRet = CallLowering::ArgInfo({0}, Type::getVoidTy(Ctx), 0); 650e8d8bef9SDimitry Andric Info.IsTailCall = MI.getOperand(MI.getNumOperands() - 1).getImm() && 651fe6060f1SDimitry Andric isLibCallInTailPosition(MI, MIRBuilder.getTII(), MRI); 6528bcb0991SDimitry Andric 6538bcb0991SDimitry Andric std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs)); 6548bcb0991SDimitry Andric if (!CLI.lowerCall(MIRBuilder, Info)) 6558bcb0991SDimitry Andric return LegalizerHelper::UnableToLegalize; 6568bcb0991SDimitry Andric 6578bcb0991SDimitry Andric if (Info.LoweredTailCall) { 6588bcb0991SDimitry Andric assert(Info.IsTailCall && "Lowered tail call when it wasn't a tail call?"); 659fe6060f1SDimitry Andric 660fe6060f1SDimitry Andric // Check debug locations before removing the return. 661fe6060f1SDimitry Andric LocObserver.checkpoint(true); 662fe6060f1SDimitry Andric 6635ffd83dbSDimitry Andric // We must have a return following the call (or debug insts) to get past 6648bcb0991SDimitry Andric // isLibCallInTailPosition. 6655ffd83dbSDimitry Andric do { 6665ffd83dbSDimitry Andric MachineInstr *Next = MI.getNextNode(); 667fe6060f1SDimitry Andric assert(Next && 668fe6060f1SDimitry Andric (Next->isCopy() || Next->isReturn() || Next->isDebugInstr()) && 6695ffd83dbSDimitry Andric "Expected instr following MI to be return or debug inst?"); 6708bcb0991SDimitry Andric // We lowered a tail call, so the call is now the return from the block. 6718bcb0991SDimitry Andric // Delete the old return. 6725ffd83dbSDimitry Andric Next->eraseFromParent(); 6735ffd83dbSDimitry Andric } while (MI.getNextNode()); 674fe6060f1SDimitry Andric 675fe6060f1SDimitry Andric // We expect to lose the debug location from the return. 676fe6060f1SDimitry Andric LocObserver.checkpoint(false); 6778bcb0991SDimitry Andric } 6788bcb0991SDimitry Andric 6798bcb0991SDimitry Andric return LegalizerHelper::Legalized; 6808bcb0991SDimitry Andric } 6818bcb0991SDimitry Andric 6820b57cec5SDimitry Andric static RTLIB::Libcall getConvRTLibDesc(unsigned Opcode, Type *ToType, 6830b57cec5SDimitry Andric Type *FromType) { 6840b57cec5SDimitry Andric auto ToMVT = MVT::getVT(ToType); 6850b57cec5SDimitry Andric auto FromMVT = MVT::getVT(FromType); 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric switch (Opcode) { 6880b57cec5SDimitry Andric case TargetOpcode::G_FPEXT: 6890b57cec5SDimitry Andric return RTLIB::getFPEXT(FromMVT, ToMVT); 6900b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: 6910b57cec5SDimitry Andric return RTLIB::getFPROUND(FromMVT, ToMVT); 6920b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 6930b57cec5SDimitry Andric return RTLIB::getFPTOSINT(FromMVT, ToMVT); 6940b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: 6950b57cec5SDimitry Andric return RTLIB::getFPTOUINT(FromMVT, ToMVT); 6960b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 6970b57cec5SDimitry Andric return RTLIB::getSINTTOFP(FromMVT, ToMVT); 6980b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: 6990b57cec5SDimitry Andric return RTLIB::getUINTTOFP(FromMVT, ToMVT); 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric llvm_unreachable("Unsupported libcall function"); 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric static LegalizerHelper::LegalizeResult 7050b57cec5SDimitry Andric conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType, 7060b57cec5SDimitry Andric Type *FromType) { 7070b57cec5SDimitry Andric RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType); 708fe6060f1SDimitry Andric return createLibcall(MIRBuilder, Libcall, 709fe6060f1SDimitry Andric {MI.getOperand(0).getReg(), ToType, 0}, 710fe6060f1SDimitry Andric {{MI.getOperand(1).getReg(), FromType, 0}}); 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 714fe6060f1SDimitry Andric LegalizerHelper::libcall(MachineInstr &MI, LostDebugLocObserver &LocObserver) { 7150b57cec5SDimitry Andric LLT LLTy = MRI.getType(MI.getOperand(0).getReg()); 7160b57cec5SDimitry Andric unsigned Size = LLTy.getSizeInBits(); 7170b57cec5SDimitry Andric auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric switch (MI.getOpcode()) { 7200b57cec5SDimitry Andric default: 7210b57cec5SDimitry Andric return UnableToLegalize; 7220b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 7230b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 7240b57cec5SDimitry Andric case TargetOpcode::G_SREM: 7250b57cec5SDimitry Andric case TargetOpcode::G_UREM: 7260b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: { 7270b57cec5SDimitry Andric Type *HLTy = IntegerType::get(Ctx, Size); 7280b57cec5SDimitry Andric auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); 7290b57cec5SDimitry Andric if (Status != Legalized) 7300b57cec5SDimitry Andric return Status; 7310b57cec5SDimitry Andric break; 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric case TargetOpcode::G_FADD: 7340b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 7350b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 7360b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 7370b57cec5SDimitry Andric case TargetOpcode::G_FMA: 7380b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 7390b57cec5SDimitry Andric case TargetOpcode::G_FREM: 7400b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 7410b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 7420b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 7430b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 7440b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 7450b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 7460b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 7470b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 7485ffd83dbSDimitry Andric case TargetOpcode::G_FFLOOR: 7495ffd83dbSDimitry Andric case TargetOpcode::G_FMINNUM: 7505ffd83dbSDimitry Andric case TargetOpcode::G_FMAXNUM: 7515ffd83dbSDimitry Andric case TargetOpcode::G_FSQRT: 7525ffd83dbSDimitry Andric case TargetOpcode::G_FRINT: 753e8d8bef9SDimitry Andric case TargetOpcode::G_FNEARBYINT: 754e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: { 7555ffd83dbSDimitry Andric Type *HLTy = getFloatTypeForLLT(Ctx, LLTy); 756e8d8bef9SDimitry Andric if (!HLTy || (Size != 32 && Size != 64 && Size != 80 && Size != 128)) { 757e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "No libcall available for type " << LLTy << ".\n"); 7580b57cec5SDimitry Andric return UnableToLegalize; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); 7610b57cec5SDimitry Andric if (Status != Legalized) 7620b57cec5SDimitry Andric return Status; 7630b57cec5SDimitry Andric break; 7640b57cec5SDimitry Andric } 7655ffd83dbSDimitry Andric case TargetOpcode::G_FPEXT: 7660b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: { 7675ffd83dbSDimitry Andric Type *FromTy = getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(1).getReg())); 7685ffd83dbSDimitry Andric Type *ToTy = getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(0).getReg())); 7695ffd83dbSDimitry Andric if (!FromTy || !ToTy) 7700b57cec5SDimitry Andric return UnableToLegalize; 7715ffd83dbSDimitry Andric LegalizeResult Status = conversionLibcall(MI, MIRBuilder, ToTy, FromTy ); 7720b57cec5SDimitry Andric if (Status != Legalized) 7730b57cec5SDimitry Andric return Status; 7740b57cec5SDimitry Andric break; 7750b57cec5SDimitry Andric } 7760b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 7770b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: { 7780b57cec5SDimitry Andric // FIXME: Support other types 7790b57cec5SDimitry Andric unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 7800b57cec5SDimitry Andric unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 7810b57cec5SDimitry Andric if ((ToSize != 32 && ToSize != 64) || (FromSize != 32 && FromSize != 64)) 7820b57cec5SDimitry Andric return UnableToLegalize; 7830b57cec5SDimitry Andric LegalizeResult Status = conversionLibcall( 7840b57cec5SDimitry Andric MI, MIRBuilder, 7850b57cec5SDimitry Andric ToSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx), 7860b57cec5SDimitry Andric FromSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx)); 7870b57cec5SDimitry Andric if (Status != Legalized) 7880b57cec5SDimitry Andric return Status; 7890b57cec5SDimitry Andric break; 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 7920b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: { 7930b57cec5SDimitry Andric // FIXME: Support other types 7940b57cec5SDimitry Andric unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 7950b57cec5SDimitry Andric unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 7960b57cec5SDimitry Andric if ((FromSize != 32 && FromSize != 64) || (ToSize != 32 && ToSize != 64)) 7970b57cec5SDimitry Andric return UnableToLegalize; 7980b57cec5SDimitry Andric LegalizeResult Status = conversionLibcall( 7990b57cec5SDimitry Andric MI, MIRBuilder, 8000b57cec5SDimitry Andric ToSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx), 8010b57cec5SDimitry Andric FromSize == 32 ? Type::getInt32Ty(Ctx) : Type::getInt64Ty(Ctx)); 8020b57cec5SDimitry Andric if (Status != Legalized) 8030b57cec5SDimitry Andric return Status; 8040b57cec5SDimitry Andric break; 8050b57cec5SDimitry Andric } 806fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 807e8d8bef9SDimitry Andric case TargetOpcode::G_MEMCPY: 808e8d8bef9SDimitry Andric case TargetOpcode::G_MEMMOVE: 809e8d8bef9SDimitry Andric case TargetOpcode::G_MEMSET: { 810fe6060f1SDimitry Andric LegalizeResult Result = 811fe6060f1SDimitry Andric createMemLibcall(MIRBuilder, *MIRBuilder.getMRI(), MI, LocObserver); 812fe6060f1SDimitry Andric if (Result != Legalized) 813fe6060f1SDimitry Andric return Result; 814e8d8bef9SDimitry Andric MI.eraseFromParent(); 815e8d8bef9SDimitry Andric return Result; 816e8d8bef9SDimitry Andric } 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric MI.eraseFromParent(); 8200b57cec5SDimitry Andric return Legalized; 8210b57cec5SDimitry Andric } 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI, 8240b57cec5SDimitry Andric unsigned TypeIdx, 8250b57cec5SDimitry Andric LLT NarrowTy) { 8260b57cec5SDimitry Andric uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); 8270b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric switch (MI.getOpcode()) { 8300b57cec5SDimitry Andric default: 8310b57cec5SDimitry Andric return UnableToLegalize; 8320b57cec5SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: { 8335ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 8345ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 8355ffd83dbSDimitry Andric 8365ffd83dbSDimitry Andric // If SizeOp0 is not an exact multiple of NarrowSize, emit 8375ffd83dbSDimitry Andric // G_ANYEXT(G_IMPLICIT_DEF). Cast result to vector if needed. 8385ffd83dbSDimitry Andric // FIXME: Although this would also be legal for the general case, it causes 8395ffd83dbSDimitry Andric // a lot of regressions in the emitted code (superfluous COPYs, artifact 8405ffd83dbSDimitry Andric // combines not being hit). This seems to be a problem related to the 8415ffd83dbSDimitry Andric // artifact combiner. 8425ffd83dbSDimitry Andric if (SizeOp0 % NarrowSize != 0) { 8435ffd83dbSDimitry Andric LLT ImplicitTy = NarrowTy; 8445ffd83dbSDimitry Andric if (DstTy.isVector()) 845fe6060f1SDimitry Andric ImplicitTy = LLT::vector(DstTy.getElementCount(), ImplicitTy); 8465ffd83dbSDimitry Andric 8475ffd83dbSDimitry Andric Register ImplicitReg = MIRBuilder.buildUndef(ImplicitTy).getReg(0); 8485ffd83dbSDimitry Andric MIRBuilder.buildAnyExt(DstReg, ImplicitReg); 8495ffd83dbSDimitry Andric 8505ffd83dbSDimitry Andric MI.eraseFromParent(); 8515ffd83dbSDimitry Andric return Legalized; 8525ffd83dbSDimitry Andric } 8535ffd83dbSDimitry Andric 8540b57cec5SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs; 8570b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) 8585ffd83dbSDimitry Andric DstRegs.push_back(MIRBuilder.buildUndef(NarrowTy).getReg(0)); 8590b57cec5SDimitry Andric 8605ffd83dbSDimitry Andric if (DstTy.isVector()) 8610b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 8620b57cec5SDimitry Andric else 8630b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 8640b57cec5SDimitry Andric MI.eraseFromParent(); 8650b57cec5SDimitry Andric return Legalized; 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: { 8680b57cec5SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 8690b57cec5SDimitry Andric const APInt &Val = MI.getOperand(1).getCImm()->getValue(); 8700b57cec5SDimitry Andric unsigned TotalSize = Ty.getSizeInBits(); 8710b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 8720b57cec5SDimitry Andric int NumParts = TotalSize / NarrowSize; 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs; 8750b57cec5SDimitry Andric for (int I = 0; I != NumParts; ++I) { 8760b57cec5SDimitry Andric unsigned Offset = I * NarrowSize; 8770b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant(NarrowTy, 8780b57cec5SDimitry Andric Val.lshr(Offset).trunc(NarrowSize)); 8790b57cec5SDimitry Andric PartRegs.push_back(K.getReg(0)); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric LLT LeftoverTy; 8830b57cec5SDimitry Andric unsigned LeftoverBits = TotalSize - NumParts * NarrowSize; 8840b57cec5SDimitry Andric SmallVector<Register, 1> LeftoverRegs; 8850b57cec5SDimitry Andric if (LeftoverBits != 0) { 8860b57cec5SDimitry Andric LeftoverTy = LLT::scalar(LeftoverBits); 8870b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant( 8880b57cec5SDimitry Andric LeftoverTy, 8890b57cec5SDimitry Andric Val.lshr(NumParts * NarrowSize).trunc(LeftoverBits)); 8900b57cec5SDimitry Andric LeftoverRegs.push_back(K.getReg(0)); 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric insertParts(MI.getOperand(0).getReg(), 8940b57cec5SDimitry Andric Ty, NarrowTy, PartRegs, LeftoverTy, LeftoverRegs); 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric MI.eraseFromParent(); 8970b57cec5SDimitry Andric return Legalized; 8980b57cec5SDimitry Andric } 8995ffd83dbSDimitry Andric case TargetOpcode::G_SEXT: 9005ffd83dbSDimitry Andric case TargetOpcode::G_ZEXT: 9015ffd83dbSDimitry Andric case TargetOpcode::G_ANYEXT: 9025ffd83dbSDimitry Andric return narrowScalarExt(MI, TypeIdx, NarrowTy); 9038bcb0991SDimitry Andric case TargetOpcode::G_TRUNC: { 9048bcb0991SDimitry Andric if (TypeIdx != 1) 9058bcb0991SDimitry Andric return UnableToLegalize; 9068bcb0991SDimitry Andric 9078bcb0991SDimitry Andric uint64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 9088bcb0991SDimitry Andric if (NarrowTy.getSizeInBits() * 2 != SizeOp1) { 9098bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow trunc to type " << NarrowTy << "\n"); 9108bcb0991SDimitry Andric return UnableToLegalize; 9118bcb0991SDimitry Andric } 9128bcb0991SDimitry Andric 9135ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1)); 9145ffd83dbSDimitry Andric MIRBuilder.buildCopy(MI.getOperand(0), Unmerge.getReg(0)); 9158bcb0991SDimitry Andric MI.eraseFromParent(); 9168bcb0991SDimitry Andric return Legalized; 9178bcb0991SDimitry Andric } 9188bcb0991SDimitry Andric 9195ffd83dbSDimitry Andric case TargetOpcode::G_FREEZE: 9205ffd83dbSDimitry Andric return reduceOperationWidth(MI, TypeIdx, NarrowTy); 921fe6060f1SDimitry Andric case TargetOpcode::G_ADD: 922fe6060f1SDimitry Andric case TargetOpcode::G_SUB: 923fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 924fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 925fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 926fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 927fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 928fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 929fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 930fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 931fe6060f1SDimitry Andric return narrowScalarAddSub(MI, TypeIdx, NarrowTy); 9320b57cec5SDimitry Andric case TargetOpcode::G_MUL: 9330b57cec5SDimitry Andric case TargetOpcode::G_UMULH: 9340b57cec5SDimitry Andric return narrowScalarMul(MI, NarrowTy); 9350b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 9360b57cec5SDimitry Andric return narrowScalarExtract(MI, TypeIdx, NarrowTy); 9370b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 9380b57cec5SDimitry Andric return narrowScalarInsert(MI, TypeIdx, NarrowTy); 9390b57cec5SDimitry Andric case TargetOpcode::G_LOAD: { 940fe6060f1SDimitry Andric auto &LoadMI = cast<GLoad>(MI); 941fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 9420b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 9430b57cec5SDimitry Andric if (DstTy.isVector()) 9440b57cec5SDimitry Andric return UnableToLegalize; 9450b57cec5SDimitry Andric 946fe6060f1SDimitry Andric if (8 * LoadMI.getMemSize() != DstTy.getSizeInBits()) { 9470b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 948fe6060f1SDimitry Andric MIRBuilder.buildLoad(TmpReg, LoadMI.getPointerReg(), LoadMI.getMMO()); 9490b57cec5SDimitry Andric MIRBuilder.buildAnyExt(DstReg, TmpReg); 950fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 9510b57cec5SDimitry Andric return Legalized; 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 954fe6060f1SDimitry Andric return reduceLoadStoreWidth(LoadMI, TypeIdx, NarrowTy); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 9570b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: { 958fe6060f1SDimitry Andric auto &LoadMI = cast<GExtLoad>(MI); 959fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 960fe6060f1SDimitry Andric Register PtrReg = LoadMI.getPointerReg(); 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 963fe6060f1SDimitry Andric auto &MMO = LoadMI.getMMO(); 964e8d8bef9SDimitry Andric unsigned MemSize = MMO.getSizeInBits(); 965e8d8bef9SDimitry Andric 966e8d8bef9SDimitry Andric if (MemSize == NarrowSize) { 9670b57cec5SDimitry Andric MIRBuilder.buildLoad(TmpReg, PtrReg, MMO); 968e8d8bef9SDimitry Andric } else if (MemSize < NarrowSize) { 969fe6060f1SDimitry Andric MIRBuilder.buildLoadInstr(LoadMI.getOpcode(), TmpReg, PtrReg, MMO); 970e8d8bef9SDimitry Andric } else if (MemSize > NarrowSize) { 971e8d8bef9SDimitry Andric // FIXME: Need to split the load. 972e8d8bef9SDimitry Andric return UnableToLegalize; 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 975fe6060f1SDimitry Andric if (isa<GZExtLoad>(LoadMI)) 9760b57cec5SDimitry Andric MIRBuilder.buildZExt(DstReg, TmpReg); 9770b57cec5SDimitry Andric else 9780b57cec5SDimitry Andric MIRBuilder.buildSExt(DstReg, TmpReg); 9790b57cec5SDimitry Andric 980fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 9810b57cec5SDimitry Andric return Legalized; 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric case TargetOpcode::G_STORE: { 984fe6060f1SDimitry Andric auto &StoreMI = cast<GStore>(MI); 9850b57cec5SDimitry Andric 986fe6060f1SDimitry Andric Register SrcReg = StoreMI.getValueReg(); 9870b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 9880b57cec5SDimitry Andric if (SrcTy.isVector()) 9890b57cec5SDimitry Andric return UnableToLegalize; 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 9920b57cec5SDimitry Andric unsigned HandledSize = NumParts * NarrowTy.getSizeInBits(); 9930b57cec5SDimitry Andric unsigned LeftoverBits = SrcTy.getSizeInBits() - HandledSize; 9940b57cec5SDimitry Andric if (SrcTy.isVector() && LeftoverBits != 0) 9950b57cec5SDimitry Andric return UnableToLegalize; 9960b57cec5SDimitry Andric 997fe6060f1SDimitry Andric if (8 * StoreMI.getMemSize() != SrcTy.getSizeInBits()) { 9980b57cec5SDimitry Andric Register TmpReg = MRI.createGenericVirtualRegister(NarrowTy); 9990b57cec5SDimitry Andric MIRBuilder.buildTrunc(TmpReg, SrcReg); 1000fe6060f1SDimitry Andric MIRBuilder.buildStore(TmpReg, StoreMI.getPointerReg(), StoreMI.getMMO()); 1001fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 10020b57cec5SDimitry Andric return Legalized; 10030b57cec5SDimitry Andric } 10040b57cec5SDimitry Andric 1005fe6060f1SDimitry Andric return reduceLoadStoreWidth(StoreMI, 0, NarrowTy); 10060b57cec5SDimitry Andric } 10070b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 10080b57cec5SDimitry Andric return narrowScalarSelect(MI, TypeIdx, NarrowTy); 10090b57cec5SDimitry Andric case TargetOpcode::G_AND: 10100b57cec5SDimitry Andric case TargetOpcode::G_OR: 10110b57cec5SDimitry Andric case TargetOpcode::G_XOR: { 10120b57cec5SDimitry Andric // Legalize bitwise operation: 10130b57cec5SDimitry Andric // A = BinOp<Ty> B, C 10140b57cec5SDimitry Andric // into: 10150b57cec5SDimitry Andric // B1, ..., BN = G_UNMERGE_VALUES B 10160b57cec5SDimitry Andric // C1, ..., CN = G_UNMERGE_VALUES C 10170b57cec5SDimitry Andric // A1 = BinOp<Ty/N> B1, C2 10180b57cec5SDimitry Andric // ... 10190b57cec5SDimitry Andric // AN = BinOp<Ty/N> BN, CN 10200b57cec5SDimitry Andric // A = G_MERGE_VALUES A1, ..., AN 10210b57cec5SDimitry Andric return narrowScalarBasic(MI, TypeIdx, NarrowTy); 10220b57cec5SDimitry Andric } 10230b57cec5SDimitry Andric case TargetOpcode::G_SHL: 10240b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 10250b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 10260b57cec5SDimitry Andric return narrowScalarShift(MI, TypeIdx, NarrowTy); 10270b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 10280b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 10290b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 10300b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 10310b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: 10325ffd83dbSDimitry Andric if (TypeIdx == 1) 10335ffd83dbSDimitry Andric switch (MI.getOpcode()) { 10345ffd83dbSDimitry Andric case TargetOpcode::G_CTLZ: 10355ffd83dbSDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 10365ffd83dbSDimitry Andric return narrowScalarCTLZ(MI, TypeIdx, NarrowTy); 10375ffd83dbSDimitry Andric case TargetOpcode::G_CTTZ: 10385ffd83dbSDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 10395ffd83dbSDimitry Andric return narrowScalarCTTZ(MI, TypeIdx, NarrowTy); 10405ffd83dbSDimitry Andric case TargetOpcode::G_CTPOP: 10415ffd83dbSDimitry Andric return narrowScalarCTPOP(MI, TypeIdx, NarrowTy); 10425ffd83dbSDimitry Andric default: 10435ffd83dbSDimitry Andric return UnableToLegalize; 10445ffd83dbSDimitry Andric } 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric Observer.changingInstr(MI); 10470b57cec5SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); 10480b57cec5SDimitry Andric Observer.changedInstr(MI); 10490b57cec5SDimitry Andric return Legalized; 10500b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 10510b57cec5SDimitry Andric if (TypeIdx != 1) 10520b57cec5SDimitry Andric return UnableToLegalize; 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric Observer.changingInstr(MI); 10550b57cec5SDimitry Andric narrowScalarSrc(MI, NarrowTy, 1); 10560b57cec5SDimitry Andric Observer.changedInstr(MI); 10570b57cec5SDimitry Andric return Legalized; 10580b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 10590b57cec5SDimitry Andric if (TypeIdx != 0) 10600b57cec5SDimitry Andric return UnableToLegalize; 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric Observer.changingInstr(MI); 10630b57cec5SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); 10640b57cec5SDimitry Andric Observer.changedInstr(MI); 10650b57cec5SDimitry Andric return Legalized; 10660b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 1067d409305fSDimitry Andric // FIXME: add support for when SizeOp0 isn't an exact multiple of 1068d409305fSDimitry Andric // NarrowSize. 1069d409305fSDimitry Andric if (SizeOp0 % NarrowSize != 0) 1070d409305fSDimitry Andric return UnableToLegalize; 1071d409305fSDimitry Andric 10720b57cec5SDimitry Andric unsigned NumParts = SizeOp0 / NarrowSize; 10735ffd83dbSDimitry Andric SmallVector<Register, 2> DstRegs(NumParts); 10745ffd83dbSDimitry Andric SmallVector<SmallVector<Register, 2>, 2> SrcRegs(MI.getNumOperands() / 2); 10750b57cec5SDimitry Andric Observer.changingInstr(MI); 10760b57cec5SDimitry Andric for (unsigned i = 1; i < MI.getNumOperands(); i += 2) { 10770b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(i + 1).getMBB(); 10780b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 10790b57cec5SDimitry Andric extractParts(MI.getOperand(i).getReg(), NarrowTy, NumParts, 10800b57cec5SDimitry Andric SrcRegs[i / 2]); 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 10830b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, MI); 10840b57cec5SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 10850b57cec5SDimitry Andric DstRegs[i] = MRI.createGenericVirtualRegister(NarrowTy); 10860b57cec5SDimitry Andric MachineInstrBuilder MIB = 10870b57cec5SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_PHI).addDef(DstRegs[i]); 10880b57cec5SDimitry Andric for (unsigned j = 1; j < MI.getNumOperands(); j += 2) 10890b57cec5SDimitry Andric MIB.addUse(SrcRegs[j / 2][i]).add(MI.getOperand(j + 1)); 10900b57cec5SDimitry Andric } 10918bcb0991SDimitry Andric MIRBuilder.setInsertPt(MBB, MBB.getFirstNonPHI()); 10925ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), DstRegs); 10930b57cec5SDimitry Andric Observer.changedInstr(MI); 10940b57cec5SDimitry Andric MI.eraseFromParent(); 10950b57cec5SDimitry Andric return Legalized; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: 10980b57cec5SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: { 10990b57cec5SDimitry Andric if (TypeIdx != 2) 11000b57cec5SDimitry Andric return UnableToLegalize; 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric int OpIdx = MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT ? 2 : 3; 11030b57cec5SDimitry Andric Observer.changingInstr(MI); 11040b57cec5SDimitry Andric narrowScalarSrc(MI, NarrowTy, OpIdx); 11050b57cec5SDimitry Andric Observer.changedInstr(MI); 11060b57cec5SDimitry Andric return Legalized; 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric case TargetOpcode::G_ICMP: { 1109fe6060f1SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 1110fe6060f1SDimitry Andric LLT SrcTy = MRI.getType(LHS); 1111fe6060f1SDimitry Andric uint64_t SrcSize = SrcTy.getSizeInBits(); 11120b57cec5SDimitry Andric CmpInst::Predicate Pred = 11130b57cec5SDimitry Andric static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate()); 11140b57cec5SDimitry Andric 1115fe6060f1SDimitry Andric // TODO: Handle the non-equality case for weird sizes. 1116fe6060f1SDimitry Andric if (NarrowSize * 2 != SrcSize && !ICmpInst::isEquality(Pred)) 1117fe6060f1SDimitry Andric return UnableToLegalize; 1118fe6060f1SDimitry Andric 1119fe6060f1SDimitry Andric LLT LeftoverTy; // Example: s88 -> s64 (NarrowTy) + s24 (leftover) 1120fe6060f1SDimitry Andric SmallVector<Register, 4> LHSPartRegs, LHSLeftoverRegs; 1121fe6060f1SDimitry Andric if (!extractParts(LHS, SrcTy, NarrowTy, LeftoverTy, LHSPartRegs, 1122fe6060f1SDimitry Andric LHSLeftoverRegs)) 1123fe6060f1SDimitry Andric return UnableToLegalize; 1124fe6060f1SDimitry Andric 1125fe6060f1SDimitry Andric LLT Unused; // Matches LeftoverTy; G_ICMP LHS and RHS are the same type. 1126fe6060f1SDimitry Andric SmallVector<Register, 4> RHSPartRegs, RHSLeftoverRegs; 1127fe6060f1SDimitry Andric if (!extractParts(MI.getOperand(3).getReg(), SrcTy, NarrowTy, Unused, 1128fe6060f1SDimitry Andric RHSPartRegs, RHSLeftoverRegs)) 1129fe6060f1SDimitry Andric return UnableToLegalize; 1130fe6060f1SDimitry Andric 1131fe6060f1SDimitry Andric // We now have the LHS and RHS of the compare split into narrow-type 1132fe6060f1SDimitry Andric // registers, plus potentially some leftover type. 1133fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 1134fe6060f1SDimitry Andric LLT ResTy = MRI.getType(Dst); 1135fe6060f1SDimitry Andric if (ICmpInst::isEquality(Pred)) { 1136fe6060f1SDimitry Andric // For each part on the LHS and RHS, keep track of the result of XOR-ing 1137fe6060f1SDimitry Andric // them together. For each equal part, the result should be all 0s. For 1138fe6060f1SDimitry Andric // each non-equal part, we'll get at least one 1. 1139fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(NarrowTy, 0); 1140fe6060f1SDimitry Andric SmallVector<Register, 4> Xors; 1141fe6060f1SDimitry Andric for (auto LHSAndRHS : zip(LHSPartRegs, RHSPartRegs)) { 1142fe6060f1SDimitry Andric auto LHS = std::get<0>(LHSAndRHS); 1143fe6060f1SDimitry Andric auto RHS = std::get<1>(LHSAndRHS); 1144fe6060f1SDimitry Andric auto Xor = MIRBuilder.buildXor(NarrowTy, LHS, RHS).getReg(0); 1145fe6060f1SDimitry Andric Xors.push_back(Xor); 1146fe6060f1SDimitry Andric } 1147fe6060f1SDimitry Andric 1148fe6060f1SDimitry Andric // Build a G_XOR for each leftover register. Each G_XOR must be widened 1149fe6060f1SDimitry Andric // to the desired narrow type so that we can OR them together later. 1150fe6060f1SDimitry Andric SmallVector<Register, 4> WidenedXors; 1151fe6060f1SDimitry Andric for (auto LHSAndRHS : zip(LHSLeftoverRegs, RHSLeftoverRegs)) { 1152fe6060f1SDimitry Andric auto LHS = std::get<0>(LHSAndRHS); 1153fe6060f1SDimitry Andric auto RHS = std::get<1>(LHSAndRHS); 1154fe6060f1SDimitry Andric auto Xor = MIRBuilder.buildXor(LeftoverTy, LHS, RHS).getReg(0); 1155fe6060f1SDimitry Andric LLT GCDTy = extractGCDType(WidenedXors, NarrowTy, LeftoverTy, Xor); 1156fe6060f1SDimitry Andric buildLCMMergePieces(LeftoverTy, NarrowTy, GCDTy, WidenedXors, 1157fe6060f1SDimitry Andric /* PadStrategy = */ TargetOpcode::G_ZEXT); 1158fe6060f1SDimitry Andric Xors.insert(Xors.end(), WidenedXors.begin(), WidenedXors.end()); 1159fe6060f1SDimitry Andric } 1160fe6060f1SDimitry Andric 1161fe6060f1SDimitry Andric // Now, for each part we broke up, we know if they are equal/not equal 1162fe6060f1SDimitry Andric // based off the G_XOR. We can OR these all together and compare against 1163fe6060f1SDimitry Andric // 0 to get the result. 1164fe6060f1SDimitry Andric assert(Xors.size() >= 2 && "Should have gotten at least two Xors?"); 1165fe6060f1SDimitry Andric auto Or = MIRBuilder.buildOr(NarrowTy, Xors[0], Xors[1]); 1166fe6060f1SDimitry Andric for (unsigned I = 2, E = Xors.size(); I < E; ++I) 1167fe6060f1SDimitry Andric Or = MIRBuilder.buildOr(NarrowTy, Or, Xors[I]); 1168fe6060f1SDimitry Andric MIRBuilder.buildICmp(Pred, Dst, Or, Zero); 11690b57cec5SDimitry Andric } else { 1170fe6060f1SDimitry Andric // TODO: Handle non-power-of-two types. 1171fe6060f1SDimitry Andric assert(LHSPartRegs.size() == 2 && "Expected exactly 2 LHS part regs?"); 1172fe6060f1SDimitry Andric assert(RHSPartRegs.size() == 2 && "Expected exactly 2 RHS part regs?"); 1173fe6060f1SDimitry Andric Register LHSL = LHSPartRegs[0]; 1174fe6060f1SDimitry Andric Register LHSH = LHSPartRegs[1]; 1175fe6060f1SDimitry Andric Register RHSL = RHSPartRegs[0]; 1176fe6060f1SDimitry Andric Register RHSH = RHSPartRegs[1]; 11778bcb0991SDimitry Andric MachineInstrBuilder CmpH = MIRBuilder.buildICmp(Pred, ResTy, LHSH, RHSH); 11780b57cec5SDimitry Andric MachineInstrBuilder CmpHEQ = 11798bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::Predicate::ICMP_EQ, ResTy, LHSH, RHSH); 11800b57cec5SDimitry Andric MachineInstrBuilder CmpLU = MIRBuilder.buildICmp( 11818bcb0991SDimitry Andric ICmpInst::getUnsignedPredicate(Pred), ResTy, LHSL, RHSL); 1182fe6060f1SDimitry Andric MIRBuilder.buildSelect(Dst, CmpHEQ, CmpLU, CmpH); 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric MI.eraseFromParent(); 11850b57cec5SDimitry Andric return Legalized; 11860b57cec5SDimitry Andric } 11878bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 11888bcb0991SDimitry Andric if (TypeIdx != 0) 11898bcb0991SDimitry Andric return UnableToLegalize; 11908bcb0991SDimitry Andric 11918bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 11928bcb0991SDimitry Andric 11938bcb0991SDimitry Andric // So long as the new type has more bits than the bits we're extending we 11948bcb0991SDimitry Andric // don't need to break it apart. 11958bcb0991SDimitry Andric if (NarrowTy.getScalarSizeInBits() >= SizeInBits) { 11968bcb0991SDimitry Andric Observer.changingInstr(MI); 11978bcb0991SDimitry Andric // We don't lose any non-extension bits by truncating the src and 11988bcb0991SDimitry Andric // sign-extending the dst. 11998bcb0991SDimitry Andric MachineOperand &MO1 = MI.getOperand(1); 12005ffd83dbSDimitry Andric auto TruncMIB = MIRBuilder.buildTrunc(NarrowTy, MO1); 12015ffd83dbSDimitry Andric MO1.setReg(TruncMIB.getReg(0)); 12028bcb0991SDimitry Andric 12038bcb0991SDimitry Andric MachineOperand &MO2 = MI.getOperand(0); 12048bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(NarrowTy); 12058bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 12065ffd83dbSDimitry Andric MIRBuilder.buildSExt(MO2, DstExt); 12078bcb0991SDimitry Andric MO2.setReg(DstExt); 12088bcb0991SDimitry Andric Observer.changedInstr(MI); 12098bcb0991SDimitry Andric return Legalized; 12108bcb0991SDimitry Andric } 12118bcb0991SDimitry Andric 12128bcb0991SDimitry Andric // Break it apart. Components below the extension point are unmodified. The 12138bcb0991SDimitry Andric // component containing the extension point becomes a narrower SEXT_INREG. 12148bcb0991SDimitry Andric // Components above it are ashr'd from the component containing the 12158bcb0991SDimitry Andric // extension point. 12168bcb0991SDimitry Andric if (SizeOp0 % NarrowSize != 0) 12178bcb0991SDimitry Andric return UnableToLegalize; 12188bcb0991SDimitry Andric int NumParts = SizeOp0 / NarrowSize; 12198bcb0991SDimitry Andric 12208bcb0991SDimitry Andric // List the registers where the destination will be scattered. 12218bcb0991SDimitry Andric SmallVector<Register, 2> DstRegs; 12228bcb0991SDimitry Andric // List the registers where the source will be split. 12238bcb0991SDimitry Andric SmallVector<Register, 2> SrcRegs; 12248bcb0991SDimitry Andric 12258bcb0991SDimitry Andric // Create all the temporary registers. 12268bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 12278bcb0991SDimitry Andric Register SrcReg = MRI.createGenericVirtualRegister(NarrowTy); 12288bcb0991SDimitry Andric 12298bcb0991SDimitry Andric SrcRegs.push_back(SrcReg); 12308bcb0991SDimitry Andric } 12318bcb0991SDimitry Andric 12328bcb0991SDimitry Andric // Explode the big arguments into smaller chunks. 12335ffd83dbSDimitry Andric MIRBuilder.buildUnmerge(SrcRegs, MI.getOperand(1)); 12348bcb0991SDimitry Andric 12358bcb0991SDimitry Andric Register AshrCstReg = 12368bcb0991SDimitry Andric MIRBuilder.buildConstant(NarrowTy, NarrowTy.getScalarSizeInBits() - 1) 12375ffd83dbSDimitry Andric .getReg(0); 12388bcb0991SDimitry Andric Register FullExtensionReg = 0; 12398bcb0991SDimitry Andric Register PartialExtensionReg = 0; 12408bcb0991SDimitry Andric 12418bcb0991SDimitry Andric // Do the operation on each small part. 12428bcb0991SDimitry Andric for (int i = 0; i < NumParts; ++i) { 12438bcb0991SDimitry Andric if ((i + 1) * NarrowTy.getScalarSizeInBits() < SizeInBits) 12448bcb0991SDimitry Andric DstRegs.push_back(SrcRegs[i]); 12458bcb0991SDimitry Andric else if (i * NarrowTy.getScalarSizeInBits() > SizeInBits) { 12468bcb0991SDimitry Andric assert(PartialExtensionReg && 12478bcb0991SDimitry Andric "Expected to visit partial extension before full"); 12488bcb0991SDimitry Andric if (FullExtensionReg) { 12498bcb0991SDimitry Andric DstRegs.push_back(FullExtensionReg); 12508bcb0991SDimitry Andric continue; 12518bcb0991SDimitry Andric } 12525ffd83dbSDimitry Andric DstRegs.push_back( 12535ffd83dbSDimitry Andric MIRBuilder.buildAShr(NarrowTy, PartialExtensionReg, AshrCstReg) 12545ffd83dbSDimitry Andric .getReg(0)); 12558bcb0991SDimitry Andric FullExtensionReg = DstRegs.back(); 12568bcb0991SDimitry Andric } else { 12578bcb0991SDimitry Andric DstRegs.push_back( 12588bcb0991SDimitry Andric MIRBuilder 12598bcb0991SDimitry Andric .buildInstr( 12608bcb0991SDimitry Andric TargetOpcode::G_SEXT_INREG, {NarrowTy}, 12618bcb0991SDimitry Andric {SrcRegs[i], SizeInBits % NarrowTy.getScalarSizeInBits()}) 12625ffd83dbSDimitry Andric .getReg(0)); 12638bcb0991SDimitry Andric PartialExtensionReg = DstRegs.back(); 12648bcb0991SDimitry Andric } 12658bcb0991SDimitry Andric } 12668bcb0991SDimitry Andric 12678bcb0991SDimitry Andric // Gather the destination registers into the final destination. 12688bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 12698bcb0991SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 12708bcb0991SDimitry Andric MI.eraseFromParent(); 12718bcb0991SDimitry Andric return Legalized; 12728bcb0991SDimitry Andric } 1273480093f4SDimitry Andric case TargetOpcode::G_BSWAP: 1274480093f4SDimitry Andric case TargetOpcode::G_BITREVERSE: { 1275480093f4SDimitry Andric if (SizeOp0 % NarrowSize != 0) 1276480093f4SDimitry Andric return UnableToLegalize; 1277480093f4SDimitry Andric 1278480093f4SDimitry Andric Observer.changingInstr(MI); 1279480093f4SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 1280480093f4SDimitry Andric unsigned NumParts = SizeOp0 / NarrowSize; 1281480093f4SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 1282480093f4SDimitry Andric 1283480093f4SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 1284480093f4SDimitry Andric auto DstPart = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, 1285480093f4SDimitry Andric {SrcRegs[NumParts - 1 - i]}); 1286480093f4SDimitry Andric DstRegs.push_back(DstPart.getReg(0)); 1287480093f4SDimitry Andric } 1288480093f4SDimitry Andric 12895ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), DstRegs); 1290480093f4SDimitry Andric 1291480093f4SDimitry Andric Observer.changedInstr(MI); 1292480093f4SDimitry Andric MI.eraseFromParent(); 1293480093f4SDimitry Andric return Legalized; 1294480093f4SDimitry Andric } 1295e8d8bef9SDimitry Andric case TargetOpcode::G_PTR_ADD: 12965ffd83dbSDimitry Andric case TargetOpcode::G_PTRMASK: { 12975ffd83dbSDimitry Andric if (TypeIdx != 1) 12985ffd83dbSDimitry Andric return UnableToLegalize; 12995ffd83dbSDimitry Andric Observer.changingInstr(MI); 13005ffd83dbSDimitry Andric narrowScalarSrc(MI, NarrowTy, 2); 13015ffd83dbSDimitry Andric Observer.changedInstr(MI); 13025ffd83dbSDimitry Andric return Legalized; 13030b57cec5SDimitry Andric } 130423408297SDimitry Andric case TargetOpcode::G_FPTOUI: 130523408297SDimitry Andric case TargetOpcode::G_FPTOSI: 130623408297SDimitry Andric return narrowScalarFPTOI(MI, TypeIdx, NarrowTy); 1307e8d8bef9SDimitry Andric case TargetOpcode::G_FPEXT: 1308e8d8bef9SDimitry Andric if (TypeIdx != 0) 1309e8d8bef9SDimitry Andric return UnableToLegalize; 1310e8d8bef9SDimitry Andric Observer.changingInstr(MI); 1311e8d8bef9SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_FPEXT); 1312e8d8bef9SDimitry Andric Observer.changedInstr(MI); 1313e8d8bef9SDimitry Andric return Legalized; 13140b57cec5SDimitry Andric } 13155ffd83dbSDimitry Andric } 13165ffd83dbSDimitry Andric 13175ffd83dbSDimitry Andric Register LegalizerHelper::coerceToScalar(Register Val) { 13185ffd83dbSDimitry Andric LLT Ty = MRI.getType(Val); 13195ffd83dbSDimitry Andric if (Ty.isScalar()) 13205ffd83dbSDimitry Andric return Val; 13215ffd83dbSDimitry Andric 13225ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 13235ffd83dbSDimitry Andric LLT NewTy = LLT::scalar(Ty.getSizeInBits()); 13245ffd83dbSDimitry Andric if (Ty.isPointer()) { 13255ffd83dbSDimitry Andric if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace())) 13265ffd83dbSDimitry Andric return Register(); 13275ffd83dbSDimitry Andric return MIRBuilder.buildPtrToInt(NewTy, Val).getReg(0); 13285ffd83dbSDimitry Andric } 13295ffd83dbSDimitry Andric 13305ffd83dbSDimitry Andric Register NewVal = Val; 13315ffd83dbSDimitry Andric 13325ffd83dbSDimitry Andric assert(Ty.isVector()); 13335ffd83dbSDimitry Andric LLT EltTy = Ty.getElementType(); 13345ffd83dbSDimitry Andric if (EltTy.isPointer()) 13355ffd83dbSDimitry Andric NewVal = MIRBuilder.buildPtrToInt(NewTy, NewVal).getReg(0); 13365ffd83dbSDimitry Andric return MIRBuilder.buildBitcast(NewTy, NewVal).getReg(0); 13375ffd83dbSDimitry Andric } 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy, 13400b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 13410b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13425ffd83dbSDimitry Andric auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO}); 13435ffd83dbSDimitry Andric MO.setReg(ExtB.getReg(0)); 13440b57cec5SDimitry Andric } 13450b57cec5SDimitry Andric 13460b57cec5SDimitry Andric void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy, 13470b57cec5SDimitry Andric unsigned OpIdx) { 13480b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13495ffd83dbSDimitry Andric auto ExtB = MIRBuilder.buildTrunc(NarrowTy, MO); 13505ffd83dbSDimitry Andric MO.setReg(ExtB.getReg(0)); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy, 13540b57cec5SDimitry Andric unsigned OpIdx, unsigned TruncOpcode) { 13550b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13560b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 13570b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 13585ffd83dbSDimitry Andric MIRBuilder.buildInstr(TruncOpcode, {MO}, {DstExt}); 13590b57cec5SDimitry Andric MO.setReg(DstExt); 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy, 13630b57cec5SDimitry Andric unsigned OpIdx, unsigned ExtOpcode) { 13640b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13650b57cec5SDimitry Andric Register DstTrunc = MRI.createGenericVirtualRegister(NarrowTy); 13660b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 13675ffd83dbSDimitry Andric MIRBuilder.buildInstr(ExtOpcode, {MO}, {DstTrunc}); 13680b57cec5SDimitry Andric MO.setReg(DstTrunc); 13690b57cec5SDimitry Andric } 13700b57cec5SDimitry Andric 13710b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorDst(MachineInstr &MI, LLT WideTy, 13720b57cec5SDimitry Andric unsigned OpIdx) { 13730b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13740b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 1375e8d8bef9SDimitry Andric MO.setReg(widenWithUnmerge(WideTy, MO.getReg())); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric 13780b57cec5SDimitry Andric void LegalizerHelper::moreElementsVectorSrc(MachineInstr &MI, LLT MoreTy, 13790b57cec5SDimitry Andric unsigned OpIdx) { 13800b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric LLT OldTy = MRI.getType(MO.getReg()); 13830b57cec5SDimitry Andric unsigned OldElts = OldTy.getNumElements(); 13840b57cec5SDimitry Andric unsigned NewElts = MoreTy.getNumElements(); 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric unsigned NumParts = NewElts / OldElts; 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric // Use concat_vectors if the result is a multiple of the number of elements. 13890b57cec5SDimitry Andric if (NumParts * OldElts == NewElts) { 13900b57cec5SDimitry Andric SmallVector<Register, 8> Parts; 13910b57cec5SDimitry Andric Parts.push_back(MO.getReg()); 13920b57cec5SDimitry Andric 13930b57cec5SDimitry Andric Register ImpDef = MIRBuilder.buildUndef(OldTy).getReg(0); 13940b57cec5SDimitry Andric for (unsigned I = 1; I != NumParts; ++I) 13950b57cec5SDimitry Andric Parts.push_back(ImpDef); 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric auto Concat = MIRBuilder.buildConcatVectors(MoreTy, Parts); 13980b57cec5SDimitry Andric MO.setReg(Concat.getReg(0)); 13990b57cec5SDimitry Andric return; 14000b57cec5SDimitry Andric } 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric Register MoreReg = MRI.createGenericVirtualRegister(MoreTy); 14030b57cec5SDimitry Andric Register ImpDef = MIRBuilder.buildUndef(MoreTy).getReg(0); 14040b57cec5SDimitry Andric MIRBuilder.buildInsert(MoreReg, ImpDef, MO.getReg(), 0); 14050b57cec5SDimitry Andric MO.setReg(MoreReg); 14060b57cec5SDimitry Andric } 14070b57cec5SDimitry Andric 14085ffd83dbSDimitry Andric void LegalizerHelper::bitcastSrc(MachineInstr &MI, LLT CastTy, unsigned OpIdx) { 14095ffd83dbSDimitry Andric MachineOperand &Op = MI.getOperand(OpIdx); 14105ffd83dbSDimitry Andric Op.setReg(MIRBuilder.buildBitcast(CastTy, Op).getReg(0)); 14115ffd83dbSDimitry Andric } 14125ffd83dbSDimitry Andric 14135ffd83dbSDimitry Andric void LegalizerHelper::bitcastDst(MachineInstr &MI, LLT CastTy, unsigned OpIdx) { 14145ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(OpIdx); 14155ffd83dbSDimitry Andric Register CastDst = MRI.createGenericVirtualRegister(CastTy); 14165ffd83dbSDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 14175ffd83dbSDimitry Andric MIRBuilder.buildBitcast(MO, CastDst); 14185ffd83dbSDimitry Andric MO.setReg(CastDst); 14195ffd83dbSDimitry Andric } 14205ffd83dbSDimitry Andric 14210b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 14220b57cec5SDimitry Andric LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx, 14230b57cec5SDimitry Andric LLT WideTy) { 14240b57cec5SDimitry Andric if (TypeIdx != 1) 14250b57cec5SDimitry Andric return UnableToLegalize; 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 14280b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 14290b57cec5SDimitry Andric if (DstTy.isVector()) 14300b57cec5SDimitry Andric return UnableToLegalize; 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 14330b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src1); 14340b57cec5SDimitry Andric const int DstSize = DstTy.getSizeInBits(); 14350b57cec5SDimitry Andric const int SrcSize = SrcTy.getSizeInBits(); 14360b57cec5SDimitry Andric const int WideSize = WideTy.getSizeInBits(); 14370b57cec5SDimitry Andric const int NumMerge = (DstSize + WideSize - 1) / WideSize; 14380b57cec5SDimitry Andric 14390b57cec5SDimitry Andric unsigned NumOps = MI.getNumOperands(); 14400b57cec5SDimitry Andric unsigned NumSrc = MI.getNumOperands() - 1; 14410b57cec5SDimitry Andric unsigned PartSize = DstTy.getSizeInBits() / NumSrc; 14420b57cec5SDimitry Andric 14430b57cec5SDimitry Andric if (WideSize >= DstSize) { 14440b57cec5SDimitry Andric // Directly pack the bits in the target type. 14450b57cec5SDimitry Andric Register ResultReg = MIRBuilder.buildZExt(WideTy, Src1).getReg(0); 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 14480b57cec5SDimitry Andric const unsigned Offset = (I - 1) * PartSize; 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 14510b57cec5SDimitry Andric assert(MRI.getType(SrcReg) == LLT::scalar(PartSize)); 14520b57cec5SDimitry Andric 14530b57cec5SDimitry Andric auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg); 14540b57cec5SDimitry Andric 14558bcb0991SDimitry Andric Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg : 14560b57cec5SDimitry Andric MRI.createGenericVirtualRegister(WideTy); 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset); 14590b57cec5SDimitry Andric auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt); 14600b57cec5SDimitry Andric MIRBuilder.buildOr(NextResult, ResultReg, Shl); 14610b57cec5SDimitry Andric ResultReg = NextResult; 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric if (WideSize > DstSize) 14650b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ResultReg); 14668bcb0991SDimitry Andric else if (DstTy.isPointer()) 14678bcb0991SDimitry Andric MIRBuilder.buildIntToPtr(DstReg, ResultReg); 14680b57cec5SDimitry Andric 14690b57cec5SDimitry Andric MI.eraseFromParent(); 14700b57cec5SDimitry Andric return Legalized; 14710b57cec5SDimitry Andric } 14720b57cec5SDimitry Andric 14730b57cec5SDimitry Andric // Unmerge the original values to the GCD type, and recombine to the next 14740b57cec5SDimitry Andric // multiple greater than the original type. 14750b57cec5SDimitry Andric // 14760b57cec5SDimitry Andric // %3:_(s12) = G_MERGE_VALUES %0:_(s4), %1:_(s4), %2:_(s4) -> s6 14770b57cec5SDimitry Andric // %4:_(s2), %5:_(s2) = G_UNMERGE_VALUES %0 14780b57cec5SDimitry Andric // %6:_(s2), %7:_(s2) = G_UNMERGE_VALUES %1 14790b57cec5SDimitry Andric // %8:_(s2), %9:_(s2) = G_UNMERGE_VALUES %2 14800b57cec5SDimitry Andric // %10:_(s6) = G_MERGE_VALUES %4, %5, %6 14810b57cec5SDimitry Andric // %11:_(s6) = G_MERGE_VALUES %7, %8, %9 14820b57cec5SDimitry Andric // %12:_(s12) = G_MERGE_VALUES %10, %11 14830b57cec5SDimitry Andric // 14840b57cec5SDimitry Andric // Padding with undef if necessary: 14850b57cec5SDimitry Andric // 14860b57cec5SDimitry Andric // %2:_(s8) = G_MERGE_VALUES %0:_(s4), %1:_(s4) -> s6 14870b57cec5SDimitry Andric // %3:_(s2), %4:_(s2) = G_UNMERGE_VALUES %0 14880b57cec5SDimitry Andric // %5:_(s2), %6:_(s2) = G_UNMERGE_VALUES %1 14890b57cec5SDimitry Andric // %7:_(s2) = G_IMPLICIT_DEF 14900b57cec5SDimitry Andric // %8:_(s6) = G_MERGE_VALUES %3, %4, %5 14910b57cec5SDimitry Andric // %9:_(s6) = G_MERGE_VALUES %6, %7, %7 14920b57cec5SDimitry Andric // %10:_(s12) = G_MERGE_VALUES %8, %9 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric const int GCD = greatestCommonDivisor(SrcSize, WideSize); 14950b57cec5SDimitry Andric LLT GCDTy = LLT::scalar(GCD); 14960b57cec5SDimitry Andric 14970b57cec5SDimitry Andric SmallVector<Register, 8> Parts; 14980b57cec5SDimitry Andric SmallVector<Register, 8> NewMergeRegs; 14990b57cec5SDimitry Andric SmallVector<Register, 8> Unmerges; 15000b57cec5SDimitry Andric LLT WideDstTy = LLT::scalar(NumMerge * WideSize); 15010b57cec5SDimitry Andric 15020b57cec5SDimitry Andric // Decompose the original operands if they don't evenly divide. 1503*4824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) { 1504*4824e7fdSDimitry Andric Register SrcReg = MO.getReg(); 15050b57cec5SDimitry Andric if (GCD == SrcSize) { 15060b57cec5SDimitry Andric Unmerges.push_back(SrcReg); 15070b57cec5SDimitry Andric } else { 15080b57cec5SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 15090b57cec5SDimitry Andric for (int J = 0, JE = Unmerge->getNumOperands() - 1; J != JE; ++J) 15100b57cec5SDimitry Andric Unmerges.push_back(Unmerge.getReg(J)); 15110b57cec5SDimitry Andric } 15120b57cec5SDimitry Andric } 15130b57cec5SDimitry Andric 15140b57cec5SDimitry Andric // Pad with undef to the next size that is a multiple of the requested size. 15150b57cec5SDimitry Andric if (static_cast<int>(Unmerges.size()) != NumMerge * WideSize) { 15160b57cec5SDimitry Andric Register UndefReg = MIRBuilder.buildUndef(GCDTy).getReg(0); 15170b57cec5SDimitry Andric for (int I = Unmerges.size(); I != NumMerge * WideSize; ++I) 15180b57cec5SDimitry Andric Unmerges.push_back(UndefReg); 15190b57cec5SDimitry Andric } 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric const int PartsPerGCD = WideSize / GCD; 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andric // Build merges of each piece. 15240b57cec5SDimitry Andric ArrayRef<Register> Slicer(Unmerges); 15250b57cec5SDimitry Andric for (int I = 0; I != NumMerge; ++I, Slicer = Slicer.drop_front(PartsPerGCD)) { 15260b57cec5SDimitry Andric auto Merge = MIRBuilder.buildMerge(WideTy, Slicer.take_front(PartsPerGCD)); 15270b57cec5SDimitry Andric NewMergeRegs.push_back(Merge.getReg(0)); 15280b57cec5SDimitry Andric } 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric // A truncate may be necessary if the requested type doesn't evenly divide the 15310b57cec5SDimitry Andric // original result type. 15320b57cec5SDimitry Andric if (DstTy.getSizeInBits() == WideDstTy.getSizeInBits()) { 15330b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, NewMergeRegs); 15340b57cec5SDimitry Andric } else { 15350b57cec5SDimitry Andric auto FinalMerge = MIRBuilder.buildMerge(WideDstTy, NewMergeRegs); 15360b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, FinalMerge.getReg(0)); 15370b57cec5SDimitry Andric } 15380b57cec5SDimitry Andric 15390b57cec5SDimitry Andric MI.eraseFromParent(); 15400b57cec5SDimitry Andric return Legalized; 15410b57cec5SDimitry Andric } 15420b57cec5SDimitry Andric 1543e8d8bef9SDimitry Andric Register LegalizerHelper::widenWithUnmerge(LLT WideTy, Register OrigReg) { 1544e8d8bef9SDimitry Andric Register WideReg = MRI.createGenericVirtualRegister(WideTy); 1545e8d8bef9SDimitry Andric LLT OrigTy = MRI.getType(OrigReg); 1546e8d8bef9SDimitry Andric LLT LCMTy = getLCMType(WideTy, OrigTy); 1547e8d8bef9SDimitry Andric 1548e8d8bef9SDimitry Andric const int NumMergeParts = LCMTy.getSizeInBits() / WideTy.getSizeInBits(); 1549e8d8bef9SDimitry Andric const int NumUnmergeParts = LCMTy.getSizeInBits() / OrigTy.getSizeInBits(); 1550e8d8bef9SDimitry Andric 1551e8d8bef9SDimitry Andric Register UnmergeSrc = WideReg; 1552e8d8bef9SDimitry Andric 1553e8d8bef9SDimitry Andric // Create a merge to the LCM type, padding with undef 1554e8d8bef9SDimitry Andric // %0:_(<3 x s32>) = G_FOO => <4 x s32> 1555e8d8bef9SDimitry Andric // => 1556e8d8bef9SDimitry Andric // %1:_(<4 x s32>) = G_FOO 1557e8d8bef9SDimitry Andric // %2:_(<4 x s32>) = G_IMPLICIT_DEF 1558e8d8bef9SDimitry Andric // %3:_(<12 x s32>) = G_CONCAT_VECTORS %1, %2, %2 1559e8d8bef9SDimitry Andric // %0:_(<3 x s32>), %4:_, %5:_, %6:_ = G_UNMERGE_VALUES %3 1560e8d8bef9SDimitry Andric if (NumMergeParts > 1) { 1561e8d8bef9SDimitry Andric Register Undef = MIRBuilder.buildUndef(WideTy).getReg(0); 1562e8d8bef9SDimitry Andric SmallVector<Register, 8> MergeParts(NumMergeParts, Undef); 1563e8d8bef9SDimitry Andric MergeParts[0] = WideReg; 1564e8d8bef9SDimitry Andric UnmergeSrc = MIRBuilder.buildMerge(LCMTy, MergeParts).getReg(0); 1565e8d8bef9SDimitry Andric } 1566e8d8bef9SDimitry Andric 1567e8d8bef9SDimitry Andric // Unmerge to the original register and pad with dead defs. 1568e8d8bef9SDimitry Andric SmallVector<Register, 8> UnmergeResults(NumUnmergeParts); 1569e8d8bef9SDimitry Andric UnmergeResults[0] = OrigReg; 1570e8d8bef9SDimitry Andric for (int I = 1; I != NumUnmergeParts; ++I) 1571e8d8bef9SDimitry Andric UnmergeResults[I] = MRI.createGenericVirtualRegister(OrigTy); 1572e8d8bef9SDimitry Andric 1573e8d8bef9SDimitry Andric MIRBuilder.buildUnmerge(UnmergeResults, UnmergeSrc); 1574e8d8bef9SDimitry Andric return WideReg; 1575e8d8bef9SDimitry Andric } 1576e8d8bef9SDimitry Andric 15770b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 15780b57cec5SDimitry Andric LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx, 15790b57cec5SDimitry Andric LLT WideTy) { 15800b57cec5SDimitry Andric if (TypeIdx != 0) 15810b57cec5SDimitry Andric return UnableToLegalize; 15820b57cec5SDimitry Andric 15835ffd83dbSDimitry Andric int NumDst = MI.getNumOperands() - 1; 15840b57cec5SDimitry Andric Register SrcReg = MI.getOperand(NumDst).getReg(); 15850b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 15865ffd83dbSDimitry Andric if (SrcTy.isVector()) 15870b57cec5SDimitry Andric return UnableToLegalize; 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 15900b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 15910b57cec5SDimitry Andric if (!DstTy.isScalar()) 15920b57cec5SDimitry Andric return UnableToLegalize; 15930b57cec5SDimitry Andric 15945ffd83dbSDimitry Andric if (WideTy.getSizeInBits() >= SrcTy.getSizeInBits()) { 15955ffd83dbSDimitry Andric if (SrcTy.isPointer()) { 15965ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 15975ffd83dbSDimitry Andric if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) { 15985ffd83dbSDimitry Andric LLVM_DEBUG( 15995ffd83dbSDimitry Andric dbgs() << "Not casting non-integral address space integer\n"); 16005ffd83dbSDimitry Andric return UnableToLegalize; 16010b57cec5SDimitry Andric } 16020b57cec5SDimitry Andric 16035ffd83dbSDimitry Andric SrcTy = LLT::scalar(SrcTy.getSizeInBits()); 16045ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildPtrToInt(SrcTy, SrcReg).getReg(0); 16055ffd83dbSDimitry Andric } 16060b57cec5SDimitry Andric 16075ffd83dbSDimitry Andric // Widen SrcTy to WideTy. This does not affect the result, but since the 16085ffd83dbSDimitry Andric // user requested this size, it is probably better handled than SrcTy and 16095ffd83dbSDimitry Andric // should reduce the total number of legalization artifacts 16105ffd83dbSDimitry Andric if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) { 16115ffd83dbSDimitry Andric SrcTy = WideTy; 16125ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0); 16135ffd83dbSDimitry Andric } 16140b57cec5SDimitry Andric 16155ffd83dbSDimitry Andric // Theres no unmerge type to target. Directly extract the bits from the 16165ffd83dbSDimitry Andric // source type 16175ffd83dbSDimitry Andric unsigned DstSize = DstTy.getSizeInBits(); 16180b57cec5SDimitry Andric 16195ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst0Reg, SrcReg); 16205ffd83dbSDimitry Andric for (int I = 1; I != NumDst; ++I) { 16215ffd83dbSDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(SrcTy, DstSize * I); 16225ffd83dbSDimitry Andric auto Shr = MIRBuilder.buildLShr(SrcTy, SrcReg, ShiftAmt); 16235ffd83dbSDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(I), Shr); 16245ffd83dbSDimitry Andric } 16255ffd83dbSDimitry Andric 16265ffd83dbSDimitry Andric MI.eraseFromParent(); 16275ffd83dbSDimitry Andric return Legalized; 16285ffd83dbSDimitry Andric } 16295ffd83dbSDimitry Andric 16305ffd83dbSDimitry Andric // Extend the source to a wider type. 16315ffd83dbSDimitry Andric LLT LCMTy = getLCMType(SrcTy, WideTy); 16325ffd83dbSDimitry Andric 16335ffd83dbSDimitry Andric Register WideSrc = SrcReg; 16345ffd83dbSDimitry Andric if (LCMTy.getSizeInBits() != SrcTy.getSizeInBits()) { 16355ffd83dbSDimitry Andric // TODO: If this is an integral address space, cast to integer and anyext. 16365ffd83dbSDimitry Andric if (SrcTy.isPointer()) { 16375ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Widening pointer source types not implemented\n"); 16385ffd83dbSDimitry Andric return UnableToLegalize; 16395ffd83dbSDimitry Andric } 16405ffd83dbSDimitry Andric 16415ffd83dbSDimitry Andric WideSrc = MIRBuilder.buildAnyExt(LCMTy, WideSrc).getReg(0); 16425ffd83dbSDimitry Andric } 16435ffd83dbSDimitry Andric 16445ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(WideTy, WideSrc); 16455ffd83dbSDimitry Andric 1646e8d8bef9SDimitry Andric // Create a sequence of unmerges and merges to the original results. Since we 1647e8d8bef9SDimitry Andric // may have widened the source, we will need to pad the results with dead defs 1648e8d8bef9SDimitry Andric // to cover the source register. 1649e8d8bef9SDimitry Andric // e.g. widen s48 to s64: 1650e8d8bef9SDimitry Andric // %1:_(s48), %2:_(s48) = G_UNMERGE_VALUES %0:_(s96) 16515ffd83dbSDimitry Andric // 16525ffd83dbSDimitry Andric // => 1653e8d8bef9SDimitry Andric // %4:_(s192) = G_ANYEXT %0:_(s96) 1654e8d8bef9SDimitry Andric // %5:_(s64), %6, %7 = G_UNMERGE_VALUES %4 ; Requested unmerge 1655e8d8bef9SDimitry Andric // ; unpack to GCD type, with extra dead defs 1656e8d8bef9SDimitry Andric // %8:_(s16), %9, %10, %11 = G_UNMERGE_VALUES %5:_(s64) 1657e8d8bef9SDimitry Andric // %12:_(s16), %13, dead %14, dead %15 = G_UNMERGE_VALUES %6:_(s64) 1658e8d8bef9SDimitry Andric // dead %16:_(s16), dead %17, dead %18, dead %18 = G_UNMERGE_VALUES %7:_(s64) 1659e8d8bef9SDimitry Andric // %1:_(s48) = G_MERGE_VALUES %8:_(s16), %9, %10 ; Remerge to destination 1660e8d8bef9SDimitry Andric // %2:_(s48) = G_MERGE_VALUES %11:_(s16), %12, %13 ; Remerge to destination 1661e8d8bef9SDimitry Andric const LLT GCDTy = getGCDType(WideTy, DstTy); 16625ffd83dbSDimitry Andric const int NumUnmerge = Unmerge->getNumOperands() - 1; 1663e8d8bef9SDimitry Andric const int PartsPerRemerge = DstTy.getSizeInBits() / GCDTy.getSizeInBits(); 1664e8d8bef9SDimitry Andric 1665e8d8bef9SDimitry Andric // Directly unmerge to the destination without going through a GCD type 1666e8d8bef9SDimitry Andric // if possible 1667e8d8bef9SDimitry Andric if (PartsPerRemerge == 1) { 16685ffd83dbSDimitry Andric const int PartsPerUnmerge = WideTy.getSizeInBits() / DstTy.getSizeInBits(); 16695ffd83dbSDimitry Andric 16705ffd83dbSDimitry Andric for (int I = 0; I != NumUnmerge; ++I) { 16715ffd83dbSDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 16725ffd83dbSDimitry Andric 16735ffd83dbSDimitry Andric for (int J = 0; J != PartsPerUnmerge; ++J) { 16745ffd83dbSDimitry Andric int Idx = I * PartsPerUnmerge + J; 16755ffd83dbSDimitry Andric if (Idx < NumDst) 16765ffd83dbSDimitry Andric MIB.addDef(MI.getOperand(Idx).getReg()); 16775ffd83dbSDimitry Andric else { 16785ffd83dbSDimitry Andric // Create dead def for excess components. 16795ffd83dbSDimitry Andric MIB.addDef(MRI.createGenericVirtualRegister(DstTy)); 16805ffd83dbSDimitry Andric } 16815ffd83dbSDimitry Andric } 16825ffd83dbSDimitry Andric 16835ffd83dbSDimitry Andric MIB.addUse(Unmerge.getReg(I)); 16845ffd83dbSDimitry Andric } 1685e8d8bef9SDimitry Andric } else { 1686e8d8bef9SDimitry Andric SmallVector<Register, 16> Parts; 1687e8d8bef9SDimitry Andric for (int J = 0; J != NumUnmerge; ++J) 1688e8d8bef9SDimitry Andric extractGCDType(Parts, GCDTy, Unmerge.getReg(J)); 1689e8d8bef9SDimitry Andric 1690e8d8bef9SDimitry Andric SmallVector<Register, 8> RemergeParts; 1691e8d8bef9SDimitry Andric for (int I = 0; I != NumDst; ++I) { 1692e8d8bef9SDimitry Andric for (int J = 0; J < PartsPerRemerge; ++J) { 1693e8d8bef9SDimitry Andric const int Idx = I * PartsPerRemerge + J; 1694e8d8bef9SDimitry Andric RemergeParts.emplace_back(Parts[Idx]); 1695e8d8bef9SDimitry Andric } 1696e8d8bef9SDimitry Andric 1697e8d8bef9SDimitry Andric MIRBuilder.buildMerge(MI.getOperand(I).getReg(), RemergeParts); 1698e8d8bef9SDimitry Andric RemergeParts.clear(); 1699e8d8bef9SDimitry Andric } 1700e8d8bef9SDimitry Andric } 17015ffd83dbSDimitry Andric 17025ffd83dbSDimitry Andric MI.eraseFromParent(); 17030b57cec5SDimitry Andric return Legalized; 17040b57cec5SDimitry Andric } 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 17070b57cec5SDimitry Andric LegalizerHelper::widenScalarExtract(MachineInstr &MI, unsigned TypeIdx, 17080b57cec5SDimitry Andric LLT WideTy) { 17090b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 17100b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 17110b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 17140b57cec5SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric if (TypeIdx == 0) { 17170b57cec5SDimitry Andric if (SrcTy.isVector() || DstTy.isVector()) 17180b57cec5SDimitry Andric return UnableToLegalize; 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric SrcOp Src(SrcReg); 17210b57cec5SDimitry Andric if (SrcTy.isPointer()) { 17220b57cec5SDimitry Andric // Extracts from pointers can be handled only if they are really just 17230b57cec5SDimitry Andric // simple integers. 17240b57cec5SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 17250b57cec5SDimitry Andric if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) 17260b57cec5SDimitry Andric return UnableToLegalize; 17270b57cec5SDimitry Andric 17280b57cec5SDimitry Andric LLT SrcAsIntTy = LLT::scalar(SrcTy.getSizeInBits()); 17290b57cec5SDimitry Andric Src = MIRBuilder.buildPtrToInt(SrcAsIntTy, Src); 17300b57cec5SDimitry Andric SrcTy = SrcAsIntTy; 17310b57cec5SDimitry Andric } 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric if (DstTy.isPointer()) 17340b57cec5SDimitry Andric return UnableToLegalize; 17350b57cec5SDimitry Andric 17360b57cec5SDimitry Andric if (Offset == 0) { 17370b57cec5SDimitry Andric // Avoid a shift in the degenerate case. 17380b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, 17390b57cec5SDimitry Andric MIRBuilder.buildAnyExtOrTrunc(WideTy, Src)); 17400b57cec5SDimitry Andric MI.eraseFromParent(); 17410b57cec5SDimitry Andric return Legalized; 17420b57cec5SDimitry Andric } 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric // Do a shift in the source type. 17450b57cec5SDimitry Andric LLT ShiftTy = SrcTy; 17460b57cec5SDimitry Andric if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) { 17470b57cec5SDimitry Andric Src = MIRBuilder.buildAnyExt(WideTy, Src); 17480b57cec5SDimitry Andric ShiftTy = WideTy; 1749e8d8bef9SDimitry Andric } 17500b57cec5SDimitry Andric 17510b57cec5SDimitry Andric auto LShr = MIRBuilder.buildLShr( 17520b57cec5SDimitry Andric ShiftTy, Src, MIRBuilder.buildConstant(ShiftTy, Offset)); 17530b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, LShr); 17540b57cec5SDimitry Andric MI.eraseFromParent(); 17550b57cec5SDimitry Andric return Legalized; 17560b57cec5SDimitry Andric } 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric if (SrcTy.isScalar()) { 17590b57cec5SDimitry Andric Observer.changingInstr(MI); 17600b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 17610b57cec5SDimitry Andric Observer.changedInstr(MI); 17620b57cec5SDimitry Andric return Legalized; 17630b57cec5SDimitry Andric } 17640b57cec5SDimitry Andric 17650b57cec5SDimitry Andric if (!SrcTy.isVector()) 17660b57cec5SDimitry Andric return UnableToLegalize; 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric if (DstTy != SrcTy.getElementType()) 17690b57cec5SDimitry Andric return UnableToLegalize; 17700b57cec5SDimitry Andric 17710b57cec5SDimitry Andric if (Offset % SrcTy.getScalarSizeInBits() != 0) 17720b57cec5SDimitry Andric return UnableToLegalize; 17730b57cec5SDimitry Andric 17740b57cec5SDimitry Andric Observer.changingInstr(MI); 17750b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) * 17780b57cec5SDimitry Andric Offset); 17790b57cec5SDimitry Andric widenScalarDst(MI, WideTy.getScalarType(), 0); 17800b57cec5SDimitry Andric Observer.changedInstr(MI); 17810b57cec5SDimitry Andric return Legalized; 17820b57cec5SDimitry Andric } 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 17850b57cec5SDimitry Andric LegalizerHelper::widenScalarInsert(MachineInstr &MI, unsigned TypeIdx, 17860b57cec5SDimitry Andric LLT WideTy) { 1787e8d8bef9SDimitry Andric if (TypeIdx != 0 || WideTy.isVector()) 17880b57cec5SDimitry Andric return UnableToLegalize; 17890b57cec5SDimitry Andric Observer.changingInstr(MI); 17900b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 17910b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 17920b57cec5SDimitry Andric Observer.changedInstr(MI); 17930b57cec5SDimitry Andric return Legalized; 17940b57cec5SDimitry Andric } 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 1797fe6060f1SDimitry Andric LegalizerHelper::widenScalarAddSubOverflow(MachineInstr &MI, unsigned TypeIdx, 1798e8d8bef9SDimitry Andric LLT WideTy) { 1799e8d8bef9SDimitry Andric if (TypeIdx == 1) 1800e8d8bef9SDimitry Andric return UnableToLegalize; // TODO 1801fe6060f1SDimitry Andric 1802fe6060f1SDimitry Andric unsigned Opcode; 1803fe6060f1SDimitry Andric unsigned ExtOpcode; 1804fe6060f1SDimitry Andric Optional<Register> CarryIn = None; 1805fe6060f1SDimitry Andric switch (MI.getOpcode()) { 1806fe6060f1SDimitry Andric default: 1807fe6060f1SDimitry Andric llvm_unreachable("Unexpected opcode!"); 1808fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 1809fe6060f1SDimitry Andric Opcode = TargetOpcode::G_ADD; 1810fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1811fe6060f1SDimitry Andric break; 1812fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 1813fe6060f1SDimitry Andric Opcode = TargetOpcode::G_SUB; 1814fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1815fe6060f1SDimitry Andric break; 1816fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 1817fe6060f1SDimitry Andric Opcode = TargetOpcode::G_ADD; 1818fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1819fe6060f1SDimitry Andric break; 1820fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 1821fe6060f1SDimitry Andric Opcode = TargetOpcode::G_SUB; 1822fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1823fe6060f1SDimitry Andric break; 1824fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 1825fe6060f1SDimitry Andric Opcode = TargetOpcode::G_UADDE; 1826fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1827fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1828fe6060f1SDimitry Andric break; 1829fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 1830fe6060f1SDimitry Andric Opcode = TargetOpcode::G_USUBE; 1831fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_SEXT; 1832fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1833fe6060f1SDimitry Andric break; 1834fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 1835fe6060f1SDimitry Andric Opcode = TargetOpcode::G_UADDE; 1836fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1837fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1838fe6060f1SDimitry Andric break; 1839fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 1840fe6060f1SDimitry Andric Opcode = TargetOpcode::G_USUBE; 1841fe6060f1SDimitry Andric ExtOpcode = TargetOpcode::G_ZEXT; 1842fe6060f1SDimitry Andric CarryIn = MI.getOperand(4).getReg(); 1843fe6060f1SDimitry Andric break; 1844fe6060f1SDimitry Andric } 1845fe6060f1SDimitry Andric 1846e8d8bef9SDimitry Andric auto LHSExt = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MI.getOperand(2)}); 1847e8d8bef9SDimitry Andric auto RHSExt = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MI.getOperand(3)}); 1848e8d8bef9SDimitry Andric // Do the arithmetic in the larger type. 1849fe6060f1SDimitry Andric Register NewOp; 1850fe6060f1SDimitry Andric if (CarryIn) { 1851fe6060f1SDimitry Andric LLT CarryOutTy = MRI.getType(MI.getOperand(1).getReg()); 1852fe6060f1SDimitry Andric NewOp = MIRBuilder 1853fe6060f1SDimitry Andric .buildInstr(Opcode, {WideTy, CarryOutTy}, 1854fe6060f1SDimitry Andric {LHSExt, RHSExt, *CarryIn}) 1855fe6060f1SDimitry Andric .getReg(0); 1856fe6060f1SDimitry Andric } else { 1857fe6060f1SDimitry Andric NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSExt, RHSExt}).getReg(0); 1858fe6060f1SDimitry Andric } 1859e8d8bef9SDimitry Andric LLT OrigTy = MRI.getType(MI.getOperand(0).getReg()); 1860e8d8bef9SDimitry Andric auto TruncOp = MIRBuilder.buildTrunc(OrigTy, NewOp); 1861e8d8bef9SDimitry Andric auto ExtOp = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {TruncOp}); 1862e8d8bef9SDimitry Andric // There is no overflow if the ExtOp is the same as NewOp. 1863e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1), NewOp, ExtOp); 1864e8d8bef9SDimitry Andric // Now trunc the NewOp to the original result. 1865e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(0), NewOp); 1866e8d8bef9SDimitry Andric MI.eraseFromParent(); 1867e8d8bef9SDimitry Andric return Legalized; 1868e8d8bef9SDimitry Andric } 1869e8d8bef9SDimitry Andric 1870e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 1871e8d8bef9SDimitry Andric LegalizerHelper::widenScalarAddSubShlSat(MachineInstr &MI, unsigned TypeIdx, 18725ffd83dbSDimitry Andric LLT WideTy) { 18735ffd83dbSDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SADDSAT || 1874e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_SSUBSAT || 1875e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_SSHLSAT; 1876e8d8bef9SDimitry Andric bool IsShift = MI.getOpcode() == TargetOpcode::G_SSHLSAT || 1877e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_USHLSAT; 18785ffd83dbSDimitry Andric // We can convert this to: 18795ffd83dbSDimitry Andric // 1. Any extend iN to iM 18805ffd83dbSDimitry Andric // 2. SHL by M-N 1881e8d8bef9SDimitry Andric // 3. [US][ADD|SUB|SHL]SAT 18825ffd83dbSDimitry Andric // 4. L/ASHR by M-N 18835ffd83dbSDimitry Andric // 18845ffd83dbSDimitry Andric // It may be more efficient to lower this to a min and a max operation in 18855ffd83dbSDimitry Andric // the higher precision arithmetic if the promoted operation isn't legal, 18865ffd83dbSDimitry Andric // but this decision is up to the target's lowering request. 18875ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 18880b57cec5SDimitry Andric 18895ffd83dbSDimitry Andric unsigned NewBits = WideTy.getScalarSizeInBits(); 18905ffd83dbSDimitry Andric unsigned SHLAmount = NewBits - MRI.getType(DstReg).getScalarSizeInBits(); 18915ffd83dbSDimitry Andric 1892e8d8bef9SDimitry Andric // Shifts must zero-extend the RHS to preserve the unsigned quantity, and 1893e8d8bef9SDimitry Andric // must not left shift the RHS to preserve the shift amount. 18945ffd83dbSDimitry Andric auto LHS = MIRBuilder.buildAnyExt(WideTy, MI.getOperand(1)); 1895e8d8bef9SDimitry Andric auto RHS = IsShift ? MIRBuilder.buildZExt(WideTy, MI.getOperand(2)) 1896e8d8bef9SDimitry Andric : MIRBuilder.buildAnyExt(WideTy, MI.getOperand(2)); 18975ffd83dbSDimitry Andric auto ShiftK = MIRBuilder.buildConstant(WideTy, SHLAmount); 18985ffd83dbSDimitry Andric auto ShiftL = MIRBuilder.buildShl(WideTy, LHS, ShiftK); 1899e8d8bef9SDimitry Andric auto ShiftR = IsShift ? RHS : MIRBuilder.buildShl(WideTy, RHS, ShiftK); 19005ffd83dbSDimitry Andric 19015ffd83dbSDimitry Andric auto WideInst = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, 19025ffd83dbSDimitry Andric {ShiftL, ShiftR}, MI.getFlags()); 19035ffd83dbSDimitry Andric 19045ffd83dbSDimitry Andric // Use a shift that will preserve the number of sign bits when the trunc is 19055ffd83dbSDimitry Andric // folded away. 19065ffd83dbSDimitry Andric auto Result = IsSigned ? MIRBuilder.buildAShr(WideTy, WideInst, ShiftK) 19075ffd83dbSDimitry Andric : MIRBuilder.buildLShr(WideTy, WideInst, ShiftK); 19085ffd83dbSDimitry Andric 19095ffd83dbSDimitry Andric MIRBuilder.buildTrunc(DstReg, Result); 19105ffd83dbSDimitry Andric MI.eraseFromParent(); 19115ffd83dbSDimitry Andric return Legalized; 19125ffd83dbSDimitry Andric } 19135ffd83dbSDimitry Andric 19145ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 1915fe6060f1SDimitry Andric LegalizerHelper::widenScalarMulo(MachineInstr &MI, unsigned TypeIdx, 1916fe6060f1SDimitry Andric LLT WideTy) { 1917fe6060f1SDimitry Andric if (TypeIdx == 1) 1918fe6060f1SDimitry Andric return UnableToLegalize; 1919fe6060f1SDimitry Andric 1920fe6060f1SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SMULO; 1921fe6060f1SDimitry Andric Register Result = MI.getOperand(0).getReg(); 1922fe6060f1SDimitry Andric Register OriginalOverflow = MI.getOperand(1).getReg(); 1923fe6060f1SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 1924fe6060f1SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 1925fe6060f1SDimitry Andric LLT SrcTy = MRI.getType(LHS); 1926fe6060f1SDimitry Andric LLT OverflowTy = MRI.getType(OriginalOverflow); 1927fe6060f1SDimitry Andric unsigned SrcBitWidth = SrcTy.getScalarSizeInBits(); 1928fe6060f1SDimitry Andric 1929fe6060f1SDimitry Andric // To determine if the result overflowed in the larger type, we extend the 1930fe6060f1SDimitry Andric // input to the larger type, do the multiply (checking if it overflows), 1931fe6060f1SDimitry Andric // then also check the high bits of the result to see if overflow happened 1932fe6060f1SDimitry Andric // there. 1933fe6060f1SDimitry Andric unsigned ExtOp = IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 1934fe6060f1SDimitry Andric auto LeftOperand = MIRBuilder.buildInstr(ExtOp, {WideTy}, {LHS}); 1935fe6060f1SDimitry Andric auto RightOperand = MIRBuilder.buildInstr(ExtOp, {WideTy}, {RHS}); 1936fe6060f1SDimitry Andric 1937fe6060f1SDimitry Andric auto Mulo = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy, OverflowTy}, 1938fe6060f1SDimitry Andric {LeftOperand, RightOperand}); 1939fe6060f1SDimitry Andric auto Mul = Mulo->getOperand(0); 1940fe6060f1SDimitry Andric MIRBuilder.buildTrunc(Result, Mul); 1941fe6060f1SDimitry Andric 1942fe6060f1SDimitry Andric MachineInstrBuilder ExtResult; 1943fe6060f1SDimitry Andric // Overflow occurred if it occurred in the larger type, or if the high part 1944fe6060f1SDimitry Andric // of the result does not zero/sign-extend the low part. Check this second 1945fe6060f1SDimitry Andric // possibility first. 1946fe6060f1SDimitry Andric if (IsSigned) { 1947fe6060f1SDimitry Andric // For signed, overflow occurred when the high part does not sign-extend 1948fe6060f1SDimitry Andric // the low part. 1949fe6060f1SDimitry Andric ExtResult = MIRBuilder.buildSExtInReg(WideTy, Mul, SrcBitWidth); 1950fe6060f1SDimitry Andric } else { 1951fe6060f1SDimitry Andric // Unsigned overflow occurred when the high part does not zero-extend the 1952fe6060f1SDimitry Andric // low part. 1953fe6060f1SDimitry Andric ExtResult = MIRBuilder.buildZExtInReg(WideTy, Mul, SrcBitWidth); 1954fe6060f1SDimitry Andric } 1955fe6060f1SDimitry Andric 1956fe6060f1SDimitry Andric // Multiplication cannot overflow if the WideTy is >= 2 * original width, 1957fe6060f1SDimitry Andric // so we don't need to check the overflow result of larger type Mulo. 1958fe6060f1SDimitry Andric if (WideTy.getScalarSizeInBits() < 2 * SrcBitWidth) { 1959fe6060f1SDimitry Andric auto Overflow = 1960fe6060f1SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, OverflowTy, Mul, ExtResult); 1961fe6060f1SDimitry Andric // Finally check if the multiplication in the larger type itself overflowed. 1962fe6060f1SDimitry Andric MIRBuilder.buildOr(OriginalOverflow, Mulo->getOperand(1), Overflow); 1963fe6060f1SDimitry Andric } else { 1964fe6060f1SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, OriginalOverflow, Mul, ExtResult); 1965fe6060f1SDimitry Andric } 1966fe6060f1SDimitry Andric MI.eraseFromParent(); 1967fe6060f1SDimitry Andric return Legalized; 1968fe6060f1SDimitry Andric } 1969fe6060f1SDimitry Andric 1970fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 19715ffd83dbSDimitry Andric LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) { 19720b57cec5SDimitry Andric switch (MI.getOpcode()) { 19730b57cec5SDimitry Andric default: 19740b57cec5SDimitry Andric return UnableToLegalize; 1975fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_XCHG: 1976fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_ADD: 1977fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_SUB: 1978fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_AND: 1979fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_OR: 1980fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_XOR: 1981fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_MIN: 1982fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_MAX: 1983fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_UMIN: 1984fe6060f1SDimitry Andric case TargetOpcode::G_ATOMICRMW_UMAX: 1985fe6060f1SDimitry Andric assert(TypeIdx == 0 && "atomicrmw with second scalar type"); 1986fe6060f1SDimitry Andric Observer.changingInstr(MI); 1987fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 1988fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 1989fe6060f1SDimitry Andric Observer.changedInstr(MI); 1990fe6060f1SDimitry Andric return Legalized; 1991fe6060f1SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG: 1992fe6060f1SDimitry Andric assert(TypeIdx == 0 && "G_ATOMIC_CMPXCHG with second scalar type"); 1993fe6060f1SDimitry Andric Observer.changingInstr(MI); 1994fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 1995fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 1996fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 1997fe6060f1SDimitry Andric Observer.changedInstr(MI); 1998fe6060f1SDimitry Andric return Legalized; 1999fe6060f1SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: 2000fe6060f1SDimitry Andric if (TypeIdx == 0) { 2001fe6060f1SDimitry Andric Observer.changingInstr(MI); 2002fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 2003fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 4, TargetOpcode::G_ANYEXT); 2004fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 0); 2005fe6060f1SDimitry Andric Observer.changedInstr(MI); 2006fe6060f1SDimitry Andric return Legalized; 2007fe6060f1SDimitry Andric } 2008fe6060f1SDimitry Andric assert(TypeIdx == 1 && 2009fe6060f1SDimitry Andric "G_ATOMIC_CMPXCHG_WITH_SUCCESS with third scalar type"); 2010fe6060f1SDimitry Andric Observer.changingInstr(MI); 2011fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2012fe6060f1SDimitry Andric Observer.changedInstr(MI); 2013fe6060f1SDimitry Andric return Legalized; 20140b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 20150b57cec5SDimitry Andric return widenScalarExtract(MI, TypeIdx, WideTy); 20160b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 20170b57cec5SDimitry Andric return widenScalarInsert(MI, TypeIdx, WideTy); 20180b57cec5SDimitry Andric case TargetOpcode::G_MERGE_VALUES: 20190b57cec5SDimitry Andric return widenScalarMergeValues(MI, TypeIdx, WideTy); 20200b57cec5SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: 20210b57cec5SDimitry Andric return widenScalarUnmergeValues(MI, TypeIdx, WideTy); 2022e8d8bef9SDimitry Andric case TargetOpcode::G_SADDO: 2023e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBO: 20240b57cec5SDimitry Andric case TargetOpcode::G_UADDO: 2025e8d8bef9SDimitry Andric case TargetOpcode::G_USUBO: 2026fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 2027fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 2028fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 2029fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 2030fe6060f1SDimitry Andric return widenScalarAddSubOverflow(MI, TypeIdx, WideTy); 2031fe6060f1SDimitry Andric case TargetOpcode::G_UMULO: 2032fe6060f1SDimitry Andric case TargetOpcode::G_SMULO: 2033fe6060f1SDimitry Andric return widenScalarMulo(MI, TypeIdx, WideTy); 20345ffd83dbSDimitry Andric case TargetOpcode::G_SADDSAT: 20355ffd83dbSDimitry Andric case TargetOpcode::G_SSUBSAT: 2036e8d8bef9SDimitry Andric case TargetOpcode::G_SSHLSAT: 20375ffd83dbSDimitry Andric case TargetOpcode::G_UADDSAT: 20385ffd83dbSDimitry Andric case TargetOpcode::G_USUBSAT: 2039e8d8bef9SDimitry Andric case TargetOpcode::G_USHLSAT: 2040e8d8bef9SDimitry Andric return widenScalarAddSubShlSat(MI, TypeIdx, WideTy); 20410b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 20420b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 20430b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 20440b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 20450b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: { 20460b57cec5SDimitry Andric if (TypeIdx == 0) { 20470b57cec5SDimitry Andric Observer.changingInstr(MI); 20480b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 20490b57cec5SDimitry Andric Observer.changedInstr(MI); 20500b57cec5SDimitry Andric return Legalized; 20510b57cec5SDimitry Andric } 20520b57cec5SDimitry Andric 20530b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 20540b57cec5SDimitry Andric 2055349cc55cSDimitry Andric // First extend the input. 2056349cc55cSDimitry Andric unsigned ExtOpc = MI.getOpcode() == TargetOpcode::G_CTTZ || 2057349cc55cSDimitry Andric MI.getOpcode() == TargetOpcode::G_CTTZ_ZERO_UNDEF 2058349cc55cSDimitry Andric ? TargetOpcode::G_ANYEXT 2059349cc55cSDimitry Andric : TargetOpcode::G_ZEXT; 2060349cc55cSDimitry Andric auto MIBSrc = MIRBuilder.buildInstr(ExtOpc, {WideTy}, {SrcReg}); 20610b57cec5SDimitry Andric LLT CurTy = MRI.getType(SrcReg); 2062349cc55cSDimitry Andric unsigned NewOpc = MI.getOpcode(); 2063349cc55cSDimitry Andric if (NewOpc == TargetOpcode::G_CTTZ) { 20640b57cec5SDimitry Andric // The count is the same in the larger type except if the original 20650b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 20660b57cec5SDimitry Andric // the top of the original type. 20670b57cec5SDimitry Andric auto TopBit = 20680b57cec5SDimitry Andric APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits()); 20690b57cec5SDimitry Andric MIBSrc = MIRBuilder.buildOr( 20700b57cec5SDimitry Andric WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit)); 2071349cc55cSDimitry Andric // Now we know the operand is non-zero, use the more relaxed opcode. 2072349cc55cSDimitry Andric NewOpc = TargetOpcode::G_CTTZ_ZERO_UNDEF; 20730b57cec5SDimitry Andric } 20740b57cec5SDimitry Andric 20750b57cec5SDimitry Andric // Perform the operation at the larger size. 2076349cc55cSDimitry Andric auto MIBNewOp = MIRBuilder.buildInstr(NewOpc, {WideTy}, {MIBSrc}); 20770b57cec5SDimitry Andric // This is already the correct result for CTPOP and CTTZs 20780b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_CTLZ || 20790b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) { 20800b57cec5SDimitry Andric // The correct result is NewOp - (Difference in widety and current ty). 20810b57cec5SDimitry Andric unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits(); 20825ffd83dbSDimitry Andric MIBNewOp = MIRBuilder.buildSub( 20835ffd83dbSDimitry Andric WideTy, MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff)); 20840b57cec5SDimitry Andric } 20850b57cec5SDimitry Andric 20860b57cec5SDimitry Andric MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp); 20870b57cec5SDimitry Andric MI.eraseFromParent(); 20880b57cec5SDimitry Andric return Legalized; 20890b57cec5SDimitry Andric } 20900b57cec5SDimitry Andric case TargetOpcode::G_BSWAP: { 20910b57cec5SDimitry Andric Observer.changingInstr(MI); 20920b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20930b57cec5SDimitry Andric 20940b57cec5SDimitry Andric Register ShrReg = MRI.createGenericVirtualRegister(WideTy); 20950b57cec5SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 20960b57cec5SDimitry Andric Register ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy); 20970b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 20980b57cec5SDimitry Andric 20990b57cec5SDimitry Andric MI.getOperand(0).setReg(DstExt); 21000b57cec5SDimitry Andric 21010b57cec5SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 21020b57cec5SDimitry Andric 21030b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 21040b57cec5SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 21050b57cec5SDimitry Andric MIRBuilder.buildConstant(ShiftAmtReg, DiffBits); 21065ffd83dbSDimitry Andric MIRBuilder.buildLShr(ShrReg, DstExt, ShiftAmtReg); 21070b57cec5SDimitry Andric 21080b57cec5SDimitry Andric MIRBuilder.buildTrunc(DstReg, ShrReg); 21090b57cec5SDimitry Andric Observer.changedInstr(MI); 21100b57cec5SDimitry Andric return Legalized; 21110b57cec5SDimitry Andric } 21128bcb0991SDimitry Andric case TargetOpcode::G_BITREVERSE: { 21138bcb0991SDimitry Andric Observer.changingInstr(MI); 21148bcb0991SDimitry Andric 21158bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 21168bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 21178bcb0991SDimitry Andric unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); 21188bcb0991SDimitry Andric 21198bcb0991SDimitry Andric Register DstExt = MRI.createGenericVirtualRegister(WideTy); 21208bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21218bcb0991SDimitry Andric MI.getOperand(0).setReg(DstExt); 21228bcb0991SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 21238bcb0991SDimitry Andric 21248bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, DiffBits); 21258bcb0991SDimitry Andric auto Shift = MIRBuilder.buildLShr(WideTy, DstExt, ShiftAmt); 21268bcb0991SDimitry Andric MIRBuilder.buildTrunc(DstReg, Shift); 21278bcb0991SDimitry Andric Observer.changedInstr(MI); 21288bcb0991SDimitry Andric return Legalized; 21298bcb0991SDimitry Andric } 21305ffd83dbSDimitry Andric case TargetOpcode::G_FREEZE: 21315ffd83dbSDimitry Andric Observer.changingInstr(MI); 21325ffd83dbSDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21335ffd83dbSDimitry Andric widenScalarDst(MI, WideTy); 21345ffd83dbSDimitry Andric Observer.changedInstr(MI); 21355ffd83dbSDimitry Andric return Legalized; 21365ffd83dbSDimitry Andric 2137fe6060f1SDimitry Andric case TargetOpcode::G_ABS: 2138fe6060f1SDimitry Andric Observer.changingInstr(MI); 2139fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 2140fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2141fe6060f1SDimitry Andric Observer.changedInstr(MI); 2142fe6060f1SDimitry Andric return Legalized; 2143fe6060f1SDimitry Andric 21440b57cec5SDimitry Andric case TargetOpcode::G_ADD: 21450b57cec5SDimitry Andric case TargetOpcode::G_AND: 21460b57cec5SDimitry Andric case TargetOpcode::G_MUL: 21470b57cec5SDimitry Andric case TargetOpcode::G_OR: 21480b57cec5SDimitry Andric case TargetOpcode::G_XOR: 21490b57cec5SDimitry Andric case TargetOpcode::G_SUB: 21500b57cec5SDimitry Andric // Perform operation at larger width (any extension is fines here, high bits 21510b57cec5SDimitry Andric // don't affect the result) and then truncate the result back to the 21520b57cec5SDimitry Andric // original type. 21530b57cec5SDimitry Andric Observer.changingInstr(MI); 21540b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21550b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 21560b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 21570b57cec5SDimitry Andric Observer.changedInstr(MI); 21580b57cec5SDimitry Andric return Legalized; 21590b57cec5SDimitry Andric 2160fe6060f1SDimitry Andric case TargetOpcode::G_SBFX: 2161fe6060f1SDimitry Andric case TargetOpcode::G_UBFX: 2162fe6060f1SDimitry Andric Observer.changingInstr(MI); 2163fe6060f1SDimitry Andric 2164fe6060f1SDimitry Andric if (TypeIdx == 0) { 2165fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 2166fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2167fe6060f1SDimitry Andric } else { 2168fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 2169fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ZEXT); 2170fe6060f1SDimitry Andric } 2171fe6060f1SDimitry Andric 2172fe6060f1SDimitry Andric Observer.changedInstr(MI); 2173fe6060f1SDimitry Andric return Legalized; 2174fe6060f1SDimitry Andric 21750b57cec5SDimitry Andric case TargetOpcode::G_SHL: 21760b57cec5SDimitry Andric Observer.changingInstr(MI); 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric if (TypeIdx == 0) { 21790b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 21800b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 21810b57cec5SDimitry Andric } else { 21820b57cec5SDimitry Andric assert(TypeIdx == 1); 21830b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 21840b57cec5SDimitry Andric // unsigned integer: 21850b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 21860b57cec5SDimitry Andric } 21870b57cec5SDimitry Andric 21880b57cec5SDimitry Andric Observer.changedInstr(MI); 21890b57cec5SDimitry Andric return Legalized; 21900b57cec5SDimitry Andric 21910b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 21920b57cec5SDimitry Andric case TargetOpcode::G_SREM: 21930b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 21940b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 21950b57cec5SDimitry Andric Observer.changingInstr(MI); 21960b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 21970b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 21980b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 21990b57cec5SDimitry Andric Observer.changedInstr(MI); 22000b57cec5SDimitry Andric return Legalized; 22010b57cec5SDimitry Andric 2202fe6060f1SDimitry Andric case TargetOpcode::G_SDIVREM: 2203fe6060f1SDimitry Andric Observer.changingInstr(MI); 2204fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 2205fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT); 2206fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2207fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2208fe6060f1SDimitry Andric Observer.changedInstr(MI); 2209fe6060f1SDimitry Andric return Legalized; 2210fe6060f1SDimitry Andric 22110b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 22120b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 22130b57cec5SDimitry Andric Observer.changingInstr(MI); 22140b57cec5SDimitry Andric 22150b57cec5SDimitry Andric if (TypeIdx == 0) { 22160b57cec5SDimitry Andric unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ? 22170b57cec5SDimitry Andric TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 22180b57cec5SDimitry Andric 22190b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, CvtOp); 22200b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22210b57cec5SDimitry Andric } else { 22220b57cec5SDimitry Andric assert(TypeIdx == 1); 22230b57cec5SDimitry Andric // The "number of bits to shift" operand must preserve its value as an 22240b57cec5SDimitry Andric // unsigned integer: 22250b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 22260b57cec5SDimitry Andric } 22270b57cec5SDimitry Andric 22280b57cec5SDimitry Andric Observer.changedInstr(MI); 22290b57cec5SDimitry Andric return Legalized; 22300b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 22310b57cec5SDimitry Andric case TargetOpcode::G_UREM: 22320b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 22330b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 22340b57cec5SDimitry Andric Observer.changingInstr(MI); 22350b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 22360b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 22370b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22380b57cec5SDimitry Andric Observer.changedInstr(MI); 22390b57cec5SDimitry Andric return Legalized; 22400b57cec5SDimitry Andric 2241fe6060f1SDimitry Andric case TargetOpcode::G_UDIVREM: 2242fe6060f1SDimitry Andric Observer.changingInstr(MI); 2243fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 2244fe6060f1SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ZEXT); 2245fe6060f1SDimitry Andric widenScalarDst(MI, WideTy); 2246fe6060f1SDimitry Andric widenScalarDst(MI, WideTy, 1); 2247fe6060f1SDimitry Andric Observer.changedInstr(MI); 2248fe6060f1SDimitry Andric return Legalized; 2249fe6060f1SDimitry Andric 22500b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 22510b57cec5SDimitry Andric Observer.changingInstr(MI); 22520b57cec5SDimitry Andric if (TypeIdx == 0) { 22530b57cec5SDimitry Andric // Perform operation at larger width (any extension is fine here, high 22540b57cec5SDimitry Andric // bits don't affect the result) and then truncate the result back to the 22550b57cec5SDimitry Andric // original type. 22560b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 22570b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); 22580b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22590b57cec5SDimitry Andric } else { 22600b57cec5SDimitry Andric bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector(); 22610b57cec5SDimitry Andric // Explicit extension is required here since high bits affect the result. 22620b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false)); 22630b57cec5SDimitry Andric } 22640b57cec5SDimitry Andric Observer.changedInstr(MI); 22650b57cec5SDimitry Andric return Legalized; 22660b57cec5SDimitry Andric 22670b57cec5SDimitry Andric case TargetOpcode::G_FPTOSI: 22680b57cec5SDimitry Andric case TargetOpcode::G_FPTOUI: 22690b57cec5SDimitry Andric Observer.changingInstr(MI); 22708bcb0991SDimitry Andric 22718bcb0991SDimitry Andric if (TypeIdx == 0) 22720b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 22738bcb0991SDimitry Andric else 22748bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT); 22758bcb0991SDimitry Andric 22760b57cec5SDimitry Andric Observer.changedInstr(MI); 22770b57cec5SDimitry Andric return Legalized; 22780b57cec5SDimitry Andric case TargetOpcode::G_SITOFP: 22790b57cec5SDimitry Andric Observer.changingInstr(MI); 2280e8d8bef9SDimitry Andric 2281e8d8bef9SDimitry Andric if (TypeIdx == 0) 2282e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2283e8d8bef9SDimitry Andric else 22840b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); 2285e8d8bef9SDimitry Andric 22860b57cec5SDimitry Andric Observer.changedInstr(MI); 22870b57cec5SDimitry Andric return Legalized; 22880b57cec5SDimitry Andric case TargetOpcode::G_UITOFP: 22890b57cec5SDimitry Andric Observer.changingInstr(MI); 2290e8d8bef9SDimitry Andric 2291e8d8bef9SDimitry Andric if (TypeIdx == 0) 2292e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2293e8d8bef9SDimitry Andric else 22940b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 2295e8d8bef9SDimitry Andric 22960b57cec5SDimitry Andric Observer.changedInstr(MI); 22970b57cec5SDimitry Andric return Legalized; 22980b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 22990b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: 23000b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 23010b57cec5SDimitry Andric Observer.changingInstr(MI); 23020b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23030b57cec5SDimitry Andric Observer.changedInstr(MI); 23040b57cec5SDimitry Andric return Legalized; 23050b57cec5SDimitry Andric 23060b57cec5SDimitry Andric case TargetOpcode::G_STORE: { 23070b57cec5SDimitry Andric if (TypeIdx != 0) 23080b57cec5SDimitry Andric return UnableToLegalize; 23090b57cec5SDimitry Andric 23100b57cec5SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 2311e8d8bef9SDimitry Andric if (!Ty.isScalar()) 23120b57cec5SDimitry Andric return UnableToLegalize; 23130b57cec5SDimitry Andric 23140b57cec5SDimitry Andric Observer.changingInstr(MI); 23150b57cec5SDimitry Andric 23160b57cec5SDimitry Andric unsigned ExtType = Ty.getScalarSizeInBits() == 1 ? 23170b57cec5SDimitry Andric TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT; 23180b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, ExtType); 23190b57cec5SDimitry Andric 23200b57cec5SDimitry Andric Observer.changedInstr(MI); 23210b57cec5SDimitry Andric return Legalized; 23220b57cec5SDimitry Andric } 23230b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: { 23240b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 23250b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 2326480093f4SDimitry Andric unsigned ExtOpc = LI.getExtOpcodeForWideningConstant( 2327480093f4SDimitry Andric MRI.getType(MI.getOperand(0).getReg())); 2328480093f4SDimitry Andric assert((ExtOpc == TargetOpcode::G_ZEXT || ExtOpc == TargetOpcode::G_SEXT || 2329480093f4SDimitry Andric ExtOpc == TargetOpcode::G_ANYEXT) && 2330480093f4SDimitry Andric "Illegal Extend"); 2331480093f4SDimitry Andric const APInt &SrcVal = SrcMO.getCImm()->getValue(); 2332480093f4SDimitry Andric const APInt &Val = (ExtOpc == TargetOpcode::G_SEXT) 2333480093f4SDimitry Andric ? SrcVal.sext(WideTy.getSizeInBits()) 2334480093f4SDimitry Andric : SrcVal.zext(WideTy.getSizeInBits()); 23350b57cec5SDimitry Andric Observer.changingInstr(MI); 23360b57cec5SDimitry Andric SrcMO.setCImm(ConstantInt::get(Ctx, Val)); 23370b57cec5SDimitry Andric 23380b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23390b57cec5SDimitry Andric Observer.changedInstr(MI); 23400b57cec5SDimitry Andric return Legalized; 23410b57cec5SDimitry Andric } 23420b57cec5SDimitry Andric case TargetOpcode::G_FCONSTANT: { 23430b57cec5SDimitry Andric MachineOperand &SrcMO = MI.getOperand(1); 23440b57cec5SDimitry Andric LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); 23450b57cec5SDimitry Andric APFloat Val = SrcMO.getFPImm()->getValueAPF(); 23460b57cec5SDimitry Andric bool LosesInfo; 23470b57cec5SDimitry Andric switch (WideTy.getSizeInBits()) { 23480b57cec5SDimitry Andric case 32: 23490b57cec5SDimitry Andric Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 23500b57cec5SDimitry Andric &LosesInfo); 23510b57cec5SDimitry Andric break; 23520b57cec5SDimitry Andric case 64: 23530b57cec5SDimitry Andric Val.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, 23540b57cec5SDimitry Andric &LosesInfo); 23550b57cec5SDimitry Andric break; 23560b57cec5SDimitry Andric default: 23570b57cec5SDimitry Andric return UnableToLegalize; 23580b57cec5SDimitry Andric } 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric assert(!LosesInfo && "extend should always be lossless"); 23610b57cec5SDimitry Andric 23620b57cec5SDimitry Andric Observer.changingInstr(MI); 23630b57cec5SDimitry Andric SrcMO.setFPImm(ConstantFP::get(Ctx, Val)); 23640b57cec5SDimitry Andric 23650b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 23660b57cec5SDimitry Andric Observer.changedInstr(MI); 23670b57cec5SDimitry Andric return Legalized; 23680b57cec5SDimitry Andric } 23690b57cec5SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: { 23700b57cec5SDimitry Andric Observer.changingInstr(MI); 23710b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23720b57cec5SDimitry Andric Observer.changedInstr(MI); 23730b57cec5SDimitry Andric return Legalized; 23740b57cec5SDimitry Andric } 23750b57cec5SDimitry Andric case TargetOpcode::G_BRCOND: 23760b57cec5SDimitry Andric Observer.changingInstr(MI); 23770b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 0, MIRBuilder.getBoolExtOp(false, false)); 23780b57cec5SDimitry Andric Observer.changedInstr(MI); 23790b57cec5SDimitry Andric return Legalized; 23800b57cec5SDimitry Andric 23810b57cec5SDimitry Andric case TargetOpcode::G_FCMP: 23820b57cec5SDimitry Andric Observer.changingInstr(MI); 23830b57cec5SDimitry Andric if (TypeIdx == 0) 23840b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23850b57cec5SDimitry Andric else { 23860b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT); 23870b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT); 23880b57cec5SDimitry Andric } 23890b57cec5SDimitry Andric Observer.changedInstr(MI); 23900b57cec5SDimitry Andric return Legalized; 23910b57cec5SDimitry Andric 23920b57cec5SDimitry Andric case TargetOpcode::G_ICMP: 23930b57cec5SDimitry Andric Observer.changingInstr(MI); 23940b57cec5SDimitry Andric if (TypeIdx == 0) 23950b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 23960b57cec5SDimitry Andric else { 23970b57cec5SDimitry Andric unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>( 23980b57cec5SDimitry Andric MI.getOperand(1).getPredicate())) 23990b57cec5SDimitry Andric ? TargetOpcode::G_SEXT 24000b57cec5SDimitry Andric : TargetOpcode::G_ZEXT; 24010b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, ExtOpcode); 24020b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 3, ExtOpcode); 24030b57cec5SDimitry Andric } 24040b57cec5SDimitry Andric Observer.changedInstr(MI); 24050b57cec5SDimitry Andric return Legalized; 24060b57cec5SDimitry Andric 2407480093f4SDimitry Andric case TargetOpcode::G_PTR_ADD: 2408480093f4SDimitry Andric assert(TypeIdx == 1 && "unable to legalize pointer of G_PTR_ADD"); 24090b57cec5SDimitry Andric Observer.changingInstr(MI); 24100b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 24110b57cec5SDimitry Andric Observer.changedInstr(MI); 24120b57cec5SDimitry Andric return Legalized; 24130b57cec5SDimitry Andric 24140b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 24150b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 24160b57cec5SDimitry Andric 24170b57cec5SDimitry Andric Observer.changingInstr(MI); 24180b57cec5SDimitry Andric for (unsigned I = 1; I < MI.getNumOperands(); I += 2) { 24190b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 24200b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 24210b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT); 24220b57cec5SDimitry Andric } 24230b57cec5SDimitry Andric 24240b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 24250b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 24260b57cec5SDimitry Andric widenScalarDst(MI, WideTy); 24270b57cec5SDimitry Andric Observer.changedInstr(MI); 24280b57cec5SDimitry Andric return Legalized; 24290b57cec5SDimitry Andric } 24300b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: { 24310b57cec5SDimitry Andric if (TypeIdx == 0) { 24320b57cec5SDimitry Andric Register VecReg = MI.getOperand(1).getReg(); 24330b57cec5SDimitry Andric LLT VecTy = MRI.getType(VecReg); 24340b57cec5SDimitry Andric Observer.changingInstr(MI); 24350b57cec5SDimitry Andric 2436fe6060f1SDimitry Andric widenScalarSrc( 2437fe6060f1SDimitry Andric MI, LLT::vector(VecTy.getElementCount(), WideTy.getSizeInBits()), 1, 2438349cc55cSDimitry Andric TargetOpcode::G_ANYEXT); 24390b57cec5SDimitry Andric 24400b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 24410b57cec5SDimitry Andric Observer.changedInstr(MI); 24420b57cec5SDimitry Andric return Legalized; 24430b57cec5SDimitry Andric } 24440b57cec5SDimitry Andric 24450b57cec5SDimitry Andric if (TypeIdx != 2) 24460b57cec5SDimitry Andric return UnableToLegalize; 24470b57cec5SDimitry Andric Observer.changingInstr(MI); 2448480093f4SDimitry Andric // TODO: Probably should be zext 24490b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); 24500b57cec5SDimitry Andric Observer.changedInstr(MI); 24510b57cec5SDimitry Andric return Legalized; 24520b57cec5SDimitry Andric } 2453480093f4SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: { 2454480093f4SDimitry Andric if (TypeIdx == 1) { 2455480093f4SDimitry Andric Observer.changingInstr(MI); 2456480093f4SDimitry Andric 2457480093f4SDimitry Andric Register VecReg = MI.getOperand(1).getReg(); 2458480093f4SDimitry Andric LLT VecTy = MRI.getType(VecReg); 2459fe6060f1SDimitry Andric LLT WideVecTy = LLT::vector(VecTy.getElementCount(), WideTy); 2460480093f4SDimitry Andric 2461480093f4SDimitry Andric widenScalarSrc(MI, WideVecTy, 1, TargetOpcode::G_ANYEXT); 2462480093f4SDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); 2463480093f4SDimitry Andric widenScalarDst(MI, WideVecTy, 0); 2464480093f4SDimitry Andric Observer.changedInstr(MI); 2465480093f4SDimitry Andric return Legalized; 2466480093f4SDimitry Andric } 2467480093f4SDimitry Andric 2468480093f4SDimitry Andric if (TypeIdx == 2) { 2469480093f4SDimitry Andric Observer.changingInstr(MI); 2470480093f4SDimitry Andric // TODO: Probably should be zext 2471480093f4SDimitry Andric widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_SEXT); 2472480093f4SDimitry Andric Observer.changedInstr(MI); 24735ffd83dbSDimitry Andric return Legalized; 2474480093f4SDimitry Andric } 2475480093f4SDimitry Andric 24765ffd83dbSDimitry Andric return UnableToLegalize; 2477480093f4SDimitry Andric } 24780b57cec5SDimitry Andric case TargetOpcode::G_FADD: 24790b57cec5SDimitry Andric case TargetOpcode::G_FMUL: 24800b57cec5SDimitry Andric case TargetOpcode::G_FSUB: 24810b57cec5SDimitry Andric case TargetOpcode::G_FMA: 24828bcb0991SDimitry Andric case TargetOpcode::G_FMAD: 24830b57cec5SDimitry Andric case TargetOpcode::G_FNEG: 24840b57cec5SDimitry Andric case TargetOpcode::G_FABS: 24850b57cec5SDimitry Andric case TargetOpcode::G_FCANONICALIZE: 24860b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM: 24870b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM: 24880b57cec5SDimitry Andric case TargetOpcode::G_FMINNUM_IEEE: 24890b57cec5SDimitry Andric case TargetOpcode::G_FMAXNUM_IEEE: 24900b57cec5SDimitry Andric case TargetOpcode::G_FMINIMUM: 24910b57cec5SDimitry Andric case TargetOpcode::G_FMAXIMUM: 24920b57cec5SDimitry Andric case TargetOpcode::G_FDIV: 24930b57cec5SDimitry Andric case TargetOpcode::G_FREM: 24940b57cec5SDimitry Andric case TargetOpcode::G_FCEIL: 24950b57cec5SDimitry Andric case TargetOpcode::G_FFLOOR: 24960b57cec5SDimitry Andric case TargetOpcode::G_FCOS: 24970b57cec5SDimitry Andric case TargetOpcode::G_FSIN: 24980b57cec5SDimitry Andric case TargetOpcode::G_FLOG10: 24990b57cec5SDimitry Andric case TargetOpcode::G_FLOG: 25000b57cec5SDimitry Andric case TargetOpcode::G_FLOG2: 25010b57cec5SDimitry Andric case TargetOpcode::G_FRINT: 25020b57cec5SDimitry Andric case TargetOpcode::G_FNEARBYINT: 25030b57cec5SDimitry Andric case TargetOpcode::G_FSQRT: 25040b57cec5SDimitry Andric case TargetOpcode::G_FEXP: 25050b57cec5SDimitry Andric case TargetOpcode::G_FEXP2: 25060b57cec5SDimitry Andric case TargetOpcode::G_FPOW: 25070b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_TRUNC: 25080b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUND: 2509e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: 25100b57cec5SDimitry Andric assert(TypeIdx == 0); 25110b57cec5SDimitry Andric Observer.changingInstr(MI); 25120b57cec5SDimitry Andric 25130b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) 25140b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT); 25150b57cec5SDimitry Andric 25160b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 25170b57cec5SDimitry Andric Observer.changedInstr(MI); 25180b57cec5SDimitry Andric return Legalized; 2519e8d8bef9SDimitry Andric case TargetOpcode::G_FPOWI: { 2520e8d8bef9SDimitry Andric if (TypeIdx != 0) 2521e8d8bef9SDimitry Andric return UnableToLegalize; 2522e8d8bef9SDimitry Andric Observer.changingInstr(MI); 2523e8d8bef9SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_FPEXT); 2524e8d8bef9SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); 2525e8d8bef9SDimitry Andric Observer.changedInstr(MI); 2526e8d8bef9SDimitry Andric return Legalized; 2527e8d8bef9SDimitry Andric } 25280b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 25290b57cec5SDimitry Andric if (TypeIdx != 1) 25300b57cec5SDimitry Andric return UnableToLegalize; 25310b57cec5SDimitry Andric 25320b57cec5SDimitry Andric Observer.changingInstr(MI); 25330b57cec5SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); 25340b57cec5SDimitry Andric Observer.changedInstr(MI); 25350b57cec5SDimitry Andric return Legalized; 25360b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 25370b57cec5SDimitry Andric if (TypeIdx != 0) 25380b57cec5SDimitry Andric return UnableToLegalize; 25390b57cec5SDimitry Andric 25400b57cec5SDimitry Andric Observer.changingInstr(MI); 25410b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 25420b57cec5SDimitry Andric Observer.changedInstr(MI); 25430b57cec5SDimitry Andric return Legalized; 25440b57cec5SDimitry Andric case TargetOpcode::G_BUILD_VECTOR: { 25450b57cec5SDimitry Andric Observer.changingInstr(MI); 25460b57cec5SDimitry Andric 25470b57cec5SDimitry Andric const LLT WideEltTy = TypeIdx == 1 ? WideTy : WideTy.getElementType(); 25480b57cec5SDimitry Andric for (int I = 1, E = MI.getNumOperands(); I != E; ++I) 25490b57cec5SDimitry Andric widenScalarSrc(MI, WideEltTy, I, TargetOpcode::G_ANYEXT); 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric // Avoid changing the result vector type if the source element type was 25520b57cec5SDimitry Andric // requested. 25530b57cec5SDimitry Andric if (TypeIdx == 1) { 2554e8d8bef9SDimitry Andric MI.setDesc(MIRBuilder.getTII().get(TargetOpcode::G_BUILD_VECTOR_TRUNC)); 25550b57cec5SDimitry Andric } else { 25560b57cec5SDimitry Andric widenScalarDst(MI, WideTy, 0); 25570b57cec5SDimitry Andric } 25580b57cec5SDimitry Andric 25590b57cec5SDimitry Andric Observer.changedInstr(MI); 25600b57cec5SDimitry Andric return Legalized; 25610b57cec5SDimitry Andric } 25628bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: 25638bcb0991SDimitry Andric if (TypeIdx != 0) 25648bcb0991SDimitry Andric return UnableToLegalize; 25658bcb0991SDimitry Andric 25668bcb0991SDimitry Andric Observer.changingInstr(MI); 25678bcb0991SDimitry Andric widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); 25688bcb0991SDimitry Andric widenScalarDst(MI, WideTy, 0, TargetOpcode::G_TRUNC); 25698bcb0991SDimitry Andric Observer.changedInstr(MI); 25708bcb0991SDimitry Andric return Legalized; 25715ffd83dbSDimitry Andric case TargetOpcode::G_PTRMASK: { 25725ffd83dbSDimitry Andric if (TypeIdx != 1) 25735ffd83dbSDimitry Andric return UnableToLegalize; 25745ffd83dbSDimitry Andric Observer.changingInstr(MI); 25755ffd83dbSDimitry Andric widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); 25765ffd83dbSDimitry Andric Observer.changedInstr(MI); 25775ffd83dbSDimitry Andric return Legalized; 25785ffd83dbSDimitry Andric } 25795ffd83dbSDimitry Andric } 25805ffd83dbSDimitry Andric } 25815ffd83dbSDimitry Andric 25825ffd83dbSDimitry Andric static void getUnmergePieces(SmallVectorImpl<Register> &Pieces, 25835ffd83dbSDimitry Andric MachineIRBuilder &B, Register Src, LLT Ty) { 25845ffd83dbSDimitry Andric auto Unmerge = B.buildUnmerge(Ty, Src); 25855ffd83dbSDimitry Andric for (int I = 0, E = Unmerge->getNumOperands() - 1; I != E; ++I) 25865ffd83dbSDimitry Andric Pieces.push_back(Unmerge.getReg(I)); 25875ffd83dbSDimitry Andric } 25885ffd83dbSDimitry Andric 25895ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 25905ffd83dbSDimitry Andric LegalizerHelper::lowerBitcast(MachineInstr &MI) { 25915ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 25925ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 25935ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 25945ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 25955ffd83dbSDimitry Andric 25965ffd83dbSDimitry Andric if (SrcTy.isVector()) { 25975ffd83dbSDimitry Andric LLT SrcEltTy = SrcTy.getElementType(); 25985ffd83dbSDimitry Andric SmallVector<Register, 8> SrcRegs; 25995ffd83dbSDimitry Andric 26005ffd83dbSDimitry Andric if (DstTy.isVector()) { 26015ffd83dbSDimitry Andric int NumDstElt = DstTy.getNumElements(); 26025ffd83dbSDimitry Andric int NumSrcElt = SrcTy.getNumElements(); 26035ffd83dbSDimitry Andric 26045ffd83dbSDimitry Andric LLT DstEltTy = DstTy.getElementType(); 26055ffd83dbSDimitry Andric LLT DstCastTy = DstEltTy; // Intermediate bitcast result type 26065ffd83dbSDimitry Andric LLT SrcPartTy = SrcEltTy; // Original unmerge result type. 26075ffd83dbSDimitry Andric 26085ffd83dbSDimitry Andric // If there's an element size mismatch, insert intermediate casts to match 26095ffd83dbSDimitry Andric // the result element type. 26105ffd83dbSDimitry Andric if (NumSrcElt < NumDstElt) { // Source element type is larger. 26115ffd83dbSDimitry Andric // %1:_(<4 x s8>) = G_BITCAST %0:_(<2 x s16>) 26125ffd83dbSDimitry Andric // 26135ffd83dbSDimitry Andric // => 26145ffd83dbSDimitry Andric // 26155ffd83dbSDimitry Andric // %2:_(s16), %3:_(s16) = G_UNMERGE_VALUES %0 26165ffd83dbSDimitry Andric // %3:_(<2 x s8>) = G_BITCAST %2 26175ffd83dbSDimitry Andric // %4:_(<2 x s8>) = G_BITCAST %3 26185ffd83dbSDimitry Andric // %1:_(<4 x s16>) = G_CONCAT_VECTORS %3, %4 2619fe6060f1SDimitry Andric DstCastTy = LLT::fixed_vector(NumDstElt / NumSrcElt, DstEltTy); 26205ffd83dbSDimitry Andric SrcPartTy = SrcEltTy; 26215ffd83dbSDimitry Andric } else if (NumSrcElt > NumDstElt) { // Source element type is smaller. 26225ffd83dbSDimitry Andric // 26235ffd83dbSDimitry Andric // %1:_(<2 x s16>) = G_BITCAST %0:_(<4 x s8>) 26245ffd83dbSDimitry Andric // 26255ffd83dbSDimitry Andric // => 26265ffd83dbSDimitry Andric // 26275ffd83dbSDimitry Andric // %2:_(<2 x s8>), %3:_(<2 x s8>) = G_UNMERGE_VALUES %0 26285ffd83dbSDimitry Andric // %3:_(s16) = G_BITCAST %2 26295ffd83dbSDimitry Andric // %4:_(s16) = G_BITCAST %3 26305ffd83dbSDimitry Andric // %1:_(<2 x s16>) = G_BUILD_VECTOR %3, %4 2631fe6060f1SDimitry Andric SrcPartTy = LLT::fixed_vector(NumSrcElt / NumDstElt, SrcEltTy); 26325ffd83dbSDimitry Andric DstCastTy = DstEltTy; 26335ffd83dbSDimitry Andric } 26345ffd83dbSDimitry Andric 26355ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcPartTy); 26365ffd83dbSDimitry Andric for (Register &SrcReg : SrcRegs) 26375ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildBitcast(DstCastTy, SrcReg).getReg(0); 26385ffd83dbSDimitry Andric } else 26395ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, SrcEltTy); 26405ffd83dbSDimitry Andric 26415ffd83dbSDimitry Andric MIRBuilder.buildMerge(Dst, SrcRegs); 26425ffd83dbSDimitry Andric MI.eraseFromParent(); 26435ffd83dbSDimitry Andric return Legalized; 26445ffd83dbSDimitry Andric } 26455ffd83dbSDimitry Andric 26465ffd83dbSDimitry Andric if (DstTy.isVector()) { 26475ffd83dbSDimitry Andric SmallVector<Register, 8> SrcRegs; 26485ffd83dbSDimitry Andric getUnmergePieces(SrcRegs, MIRBuilder, Src, DstTy.getElementType()); 26495ffd83dbSDimitry Andric MIRBuilder.buildMerge(Dst, SrcRegs); 26505ffd83dbSDimitry Andric MI.eraseFromParent(); 26515ffd83dbSDimitry Andric return Legalized; 26525ffd83dbSDimitry Andric } 26535ffd83dbSDimitry Andric 26545ffd83dbSDimitry Andric return UnableToLegalize; 26555ffd83dbSDimitry Andric } 26565ffd83dbSDimitry Andric 2657e8d8bef9SDimitry Andric /// Figure out the bit offset into a register when coercing a vector index for 2658e8d8bef9SDimitry Andric /// the wide element type. This is only for the case when promoting vector to 2659e8d8bef9SDimitry Andric /// one with larger elements. 2660e8d8bef9SDimitry Andric // 2661e8d8bef9SDimitry Andric /// 2662e8d8bef9SDimitry Andric /// %offset_idx = G_AND %idx, ~(-1 << Log2(DstEltSize / SrcEltSize)) 2663e8d8bef9SDimitry Andric /// %offset_bits = G_SHL %offset_idx, Log2(SrcEltSize) 2664e8d8bef9SDimitry Andric static Register getBitcastWiderVectorElementOffset(MachineIRBuilder &B, 2665e8d8bef9SDimitry Andric Register Idx, 2666e8d8bef9SDimitry Andric unsigned NewEltSize, 2667e8d8bef9SDimitry Andric unsigned OldEltSize) { 2668e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2669e8d8bef9SDimitry Andric LLT IdxTy = B.getMRI()->getType(Idx); 2670e8d8bef9SDimitry Andric 2671e8d8bef9SDimitry Andric // Now figure out the amount we need to shift to get the target bits. 2672e8d8bef9SDimitry Andric auto OffsetMask = B.buildConstant( 2673349cc55cSDimitry Andric IdxTy, ~(APInt::getAllOnes(IdxTy.getSizeInBits()) << Log2EltRatio)); 2674e8d8bef9SDimitry Andric auto OffsetIdx = B.buildAnd(IdxTy, Idx, OffsetMask); 2675e8d8bef9SDimitry Andric return B.buildShl(IdxTy, OffsetIdx, 2676e8d8bef9SDimitry Andric B.buildConstant(IdxTy, Log2_32(OldEltSize))).getReg(0); 2677e8d8bef9SDimitry Andric } 2678e8d8bef9SDimitry Andric 2679e8d8bef9SDimitry Andric /// Perform a G_EXTRACT_VECTOR_ELT in a different sized vector element. If this 2680e8d8bef9SDimitry Andric /// is casting to a vector with a smaller element size, perform multiple element 2681e8d8bef9SDimitry Andric /// extracts and merge the results. If this is coercing to a vector with larger 2682e8d8bef9SDimitry Andric /// elements, index the bitcasted vector and extract the target element with bit 2683e8d8bef9SDimitry Andric /// operations. This is intended to force the indexing in the native register 2684e8d8bef9SDimitry Andric /// size for architectures that can dynamically index the register file. 26855ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 2686e8d8bef9SDimitry Andric LegalizerHelper::bitcastExtractVectorElt(MachineInstr &MI, unsigned TypeIdx, 2687e8d8bef9SDimitry Andric LLT CastTy) { 2688e8d8bef9SDimitry Andric if (TypeIdx != 1) 2689e8d8bef9SDimitry Andric return UnableToLegalize; 2690e8d8bef9SDimitry Andric 2691e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 2692e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 2693e8d8bef9SDimitry Andric Register Idx = MI.getOperand(2).getReg(); 2694e8d8bef9SDimitry Andric LLT SrcVecTy = MRI.getType(SrcVec); 2695e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 2696e8d8bef9SDimitry Andric 2697e8d8bef9SDimitry Andric LLT SrcEltTy = SrcVecTy.getElementType(); 2698e8d8bef9SDimitry Andric unsigned NewNumElts = CastTy.isVector() ? CastTy.getNumElements() : 1; 2699e8d8bef9SDimitry Andric unsigned OldNumElts = SrcVecTy.getNumElements(); 2700e8d8bef9SDimitry Andric 2701e8d8bef9SDimitry Andric LLT NewEltTy = CastTy.isVector() ? CastTy.getElementType() : CastTy; 2702e8d8bef9SDimitry Andric Register CastVec = MIRBuilder.buildBitcast(CastTy, SrcVec).getReg(0); 2703e8d8bef9SDimitry Andric 2704e8d8bef9SDimitry Andric const unsigned NewEltSize = NewEltTy.getSizeInBits(); 2705e8d8bef9SDimitry Andric const unsigned OldEltSize = SrcEltTy.getSizeInBits(); 2706e8d8bef9SDimitry Andric if (NewNumElts > OldNumElts) { 2707e8d8bef9SDimitry Andric // Decreasing the vector element size 2708e8d8bef9SDimitry Andric // 2709e8d8bef9SDimitry Andric // e.g. i64 = extract_vector_elt x:v2i64, y:i32 2710e8d8bef9SDimitry Andric // => 2711e8d8bef9SDimitry Andric // v4i32:castx = bitcast x:v2i64 2712e8d8bef9SDimitry Andric // 2713e8d8bef9SDimitry Andric // i64 = bitcast 2714e8d8bef9SDimitry Andric // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 2715e8d8bef9SDimitry Andric // (i32 (extract_vector_elt castx, (2 * y + 1))) 2716e8d8bef9SDimitry Andric // 2717e8d8bef9SDimitry Andric if (NewNumElts % OldNumElts != 0) 2718e8d8bef9SDimitry Andric return UnableToLegalize; 2719e8d8bef9SDimitry Andric 2720e8d8bef9SDimitry Andric // Type of the intermediate result vector. 2721e8d8bef9SDimitry Andric const unsigned NewEltsPerOldElt = NewNumElts / OldNumElts; 2722fe6060f1SDimitry Andric LLT MidTy = 2723fe6060f1SDimitry Andric LLT::scalarOrVector(ElementCount::getFixed(NewEltsPerOldElt), NewEltTy); 2724e8d8bef9SDimitry Andric 2725e8d8bef9SDimitry Andric auto NewEltsPerOldEltK = MIRBuilder.buildConstant(IdxTy, NewEltsPerOldElt); 2726e8d8bef9SDimitry Andric 2727e8d8bef9SDimitry Andric SmallVector<Register, 8> NewOps(NewEltsPerOldElt); 2728e8d8bef9SDimitry Andric auto NewBaseIdx = MIRBuilder.buildMul(IdxTy, Idx, NewEltsPerOldEltK); 2729e8d8bef9SDimitry Andric 2730e8d8bef9SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 2731e8d8bef9SDimitry Andric auto IdxOffset = MIRBuilder.buildConstant(IdxTy, I); 2732e8d8bef9SDimitry Andric auto TmpIdx = MIRBuilder.buildAdd(IdxTy, NewBaseIdx, IdxOffset); 2733e8d8bef9SDimitry Andric auto Elt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, TmpIdx); 2734e8d8bef9SDimitry Andric NewOps[I] = Elt.getReg(0); 2735e8d8bef9SDimitry Andric } 2736e8d8bef9SDimitry Andric 2737e8d8bef9SDimitry Andric auto NewVec = MIRBuilder.buildBuildVector(MidTy, NewOps); 2738e8d8bef9SDimitry Andric MIRBuilder.buildBitcast(Dst, NewVec); 2739e8d8bef9SDimitry Andric MI.eraseFromParent(); 2740e8d8bef9SDimitry Andric return Legalized; 2741e8d8bef9SDimitry Andric } 2742e8d8bef9SDimitry Andric 2743e8d8bef9SDimitry Andric if (NewNumElts < OldNumElts) { 2744e8d8bef9SDimitry Andric if (NewEltSize % OldEltSize != 0) 2745e8d8bef9SDimitry Andric return UnableToLegalize; 2746e8d8bef9SDimitry Andric 2747e8d8bef9SDimitry Andric // This only depends on powers of 2 because we use bit tricks to figure out 2748e8d8bef9SDimitry Andric // the bit offset we need to shift to get the target element. A general 2749e8d8bef9SDimitry Andric // expansion could emit division/multiply. 2750e8d8bef9SDimitry Andric if (!isPowerOf2_32(NewEltSize / OldEltSize)) 2751e8d8bef9SDimitry Andric return UnableToLegalize; 2752e8d8bef9SDimitry Andric 2753e8d8bef9SDimitry Andric // Increasing the vector element size. 2754e8d8bef9SDimitry Andric // %elt:_(small_elt) = G_EXTRACT_VECTOR_ELT %vec:_(<N x small_elt>), %idx 2755e8d8bef9SDimitry Andric // 2756e8d8bef9SDimitry Andric // => 2757e8d8bef9SDimitry Andric // 2758e8d8bef9SDimitry Andric // %cast = G_BITCAST %vec 2759e8d8bef9SDimitry Andric // %scaled_idx = G_LSHR %idx, Log2(DstEltSize / SrcEltSize) 2760e8d8bef9SDimitry Andric // %wide_elt = G_EXTRACT_VECTOR_ELT %cast, %scaled_idx 2761e8d8bef9SDimitry Andric // %offset_idx = G_AND %idx, ~(-1 << Log2(DstEltSize / SrcEltSize)) 2762e8d8bef9SDimitry Andric // %offset_bits = G_SHL %offset_idx, Log2(SrcEltSize) 2763e8d8bef9SDimitry Andric // %elt_bits = G_LSHR %wide_elt, %offset_bits 2764e8d8bef9SDimitry Andric // %elt = G_TRUNC %elt_bits 2765e8d8bef9SDimitry Andric 2766e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2767e8d8bef9SDimitry Andric auto Log2Ratio = MIRBuilder.buildConstant(IdxTy, Log2EltRatio); 2768e8d8bef9SDimitry Andric 2769e8d8bef9SDimitry Andric // Divide to get the index in the wider element type. 2770e8d8bef9SDimitry Andric auto ScaledIdx = MIRBuilder.buildLShr(IdxTy, Idx, Log2Ratio); 2771e8d8bef9SDimitry Andric 2772e8d8bef9SDimitry Andric Register WideElt = CastVec; 2773e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2774e8d8bef9SDimitry Andric WideElt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, 2775e8d8bef9SDimitry Andric ScaledIdx).getReg(0); 2776e8d8bef9SDimitry Andric } 2777e8d8bef9SDimitry Andric 2778e8d8bef9SDimitry Andric // Compute the bit offset into the register of the target element. 2779e8d8bef9SDimitry Andric Register OffsetBits = getBitcastWiderVectorElementOffset( 2780e8d8bef9SDimitry Andric MIRBuilder, Idx, NewEltSize, OldEltSize); 2781e8d8bef9SDimitry Andric 2782e8d8bef9SDimitry Andric // Shift the wide element to get the target element. 2783e8d8bef9SDimitry Andric auto ExtractedBits = MIRBuilder.buildLShr(NewEltTy, WideElt, OffsetBits); 2784e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(Dst, ExtractedBits); 2785e8d8bef9SDimitry Andric MI.eraseFromParent(); 2786e8d8bef9SDimitry Andric return Legalized; 2787e8d8bef9SDimitry Andric } 2788e8d8bef9SDimitry Andric 2789e8d8bef9SDimitry Andric return UnableToLegalize; 2790e8d8bef9SDimitry Andric } 2791e8d8bef9SDimitry Andric 2792e8d8bef9SDimitry Andric /// Emit code to insert \p InsertReg into \p TargetRet at \p OffsetBits in \p 2793e8d8bef9SDimitry Andric /// TargetReg, while preserving other bits in \p TargetReg. 2794e8d8bef9SDimitry Andric /// 2795e8d8bef9SDimitry Andric /// (InsertReg << Offset) | (TargetReg & ~(-1 >> InsertReg.size()) << Offset) 2796e8d8bef9SDimitry Andric static Register buildBitFieldInsert(MachineIRBuilder &B, 2797e8d8bef9SDimitry Andric Register TargetReg, Register InsertReg, 2798e8d8bef9SDimitry Andric Register OffsetBits) { 2799e8d8bef9SDimitry Andric LLT TargetTy = B.getMRI()->getType(TargetReg); 2800e8d8bef9SDimitry Andric LLT InsertTy = B.getMRI()->getType(InsertReg); 2801e8d8bef9SDimitry Andric auto ZextVal = B.buildZExt(TargetTy, InsertReg); 2802e8d8bef9SDimitry Andric auto ShiftedInsertVal = B.buildShl(TargetTy, ZextVal, OffsetBits); 2803e8d8bef9SDimitry Andric 2804e8d8bef9SDimitry Andric // Produce a bitmask of the value to insert 2805e8d8bef9SDimitry Andric auto EltMask = B.buildConstant( 2806e8d8bef9SDimitry Andric TargetTy, APInt::getLowBitsSet(TargetTy.getSizeInBits(), 2807e8d8bef9SDimitry Andric InsertTy.getSizeInBits())); 2808e8d8bef9SDimitry Andric // Shift it into position 2809e8d8bef9SDimitry Andric auto ShiftedMask = B.buildShl(TargetTy, EltMask, OffsetBits); 2810e8d8bef9SDimitry Andric auto InvShiftedMask = B.buildNot(TargetTy, ShiftedMask); 2811e8d8bef9SDimitry Andric 2812e8d8bef9SDimitry Andric // Clear out the bits in the wide element 2813e8d8bef9SDimitry Andric auto MaskedOldElt = B.buildAnd(TargetTy, TargetReg, InvShiftedMask); 2814e8d8bef9SDimitry Andric 2815e8d8bef9SDimitry Andric // The value to insert has all zeros already, so stick it into the masked 2816e8d8bef9SDimitry Andric // wide element. 2817e8d8bef9SDimitry Andric return B.buildOr(TargetTy, MaskedOldElt, ShiftedInsertVal).getReg(0); 2818e8d8bef9SDimitry Andric } 2819e8d8bef9SDimitry Andric 2820e8d8bef9SDimitry Andric /// Perform a G_INSERT_VECTOR_ELT in a different sized vector element. If this 2821e8d8bef9SDimitry Andric /// is increasing the element size, perform the indexing in the target element 2822e8d8bef9SDimitry Andric /// type, and use bit operations to insert at the element position. This is 2823e8d8bef9SDimitry Andric /// intended for architectures that can dynamically index the register file and 2824e8d8bef9SDimitry Andric /// want to force indexing in the native register size. 2825e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 2826e8d8bef9SDimitry Andric LegalizerHelper::bitcastInsertVectorElt(MachineInstr &MI, unsigned TypeIdx, 2827e8d8bef9SDimitry Andric LLT CastTy) { 28285ffd83dbSDimitry Andric if (TypeIdx != 0) 28295ffd83dbSDimitry Andric return UnableToLegalize; 28305ffd83dbSDimitry Andric 2831e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 2832e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 2833e8d8bef9SDimitry Andric Register Val = MI.getOperand(2).getReg(); 2834e8d8bef9SDimitry Andric Register Idx = MI.getOperand(3).getReg(); 2835e8d8bef9SDimitry Andric 2836e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(Dst); 2837e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 2838e8d8bef9SDimitry Andric 2839e8d8bef9SDimitry Andric LLT VecEltTy = VecTy.getElementType(); 2840e8d8bef9SDimitry Andric LLT NewEltTy = CastTy.isVector() ? CastTy.getElementType() : CastTy; 2841e8d8bef9SDimitry Andric const unsigned NewEltSize = NewEltTy.getSizeInBits(); 2842e8d8bef9SDimitry Andric const unsigned OldEltSize = VecEltTy.getSizeInBits(); 2843e8d8bef9SDimitry Andric 2844e8d8bef9SDimitry Andric unsigned NewNumElts = CastTy.isVector() ? CastTy.getNumElements() : 1; 2845e8d8bef9SDimitry Andric unsigned OldNumElts = VecTy.getNumElements(); 2846e8d8bef9SDimitry Andric 2847e8d8bef9SDimitry Andric Register CastVec = MIRBuilder.buildBitcast(CastTy, SrcVec).getReg(0); 2848e8d8bef9SDimitry Andric if (NewNumElts < OldNumElts) { 2849e8d8bef9SDimitry Andric if (NewEltSize % OldEltSize != 0) 28505ffd83dbSDimitry Andric return UnableToLegalize; 28515ffd83dbSDimitry Andric 2852e8d8bef9SDimitry Andric // This only depends on powers of 2 because we use bit tricks to figure out 2853e8d8bef9SDimitry Andric // the bit offset we need to shift to get the target element. A general 2854e8d8bef9SDimitry Andric // expansion could emit division/multiply. 2855e8d8bef9SDimitry Andric if (!isPowerOf2_32(NewEltSize / OldEltSize)) 28565ffd83dbSDimitry Andric return UnableToLegalize; 28575ffd83dbSDimitry Andric 2858e8d8bef9SDimitry Andric const unsigned Log2EltRatio = Log2_32(NewEltSize / OldEltSize); 2859e8d8bef9SDimitry Andric auto Log2Ratio = MIRBuilder.buildConstant(IdxTy, Log2EltRatio); 2860e8d8bef9SDimitry Andric 2861e8d8bef9SDimitry Andric // Divide to get the index in the wider element type. 2862e8d8bef9SDimitry Andric auto ScaledIdx = MIRBuilder.buildLShr(IdxTy, Idx, Log2Ratio); 2863e8d8bef9SDimitry Andric 2864e8d8bef9SDimitry Andric Register ExtractedElt = CastVec; 2865e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2866e8d8bef9SDimitry Andric ExtractedElt = MIRBuilder.buildExtractVectorElement(NewEltTy, CastVec, 2867e8d8bef9SDimitry Andric ScaledIdx).getReg(0); 28685ffd83dbSDimitry Andric } 28695ffd83dbSDimitry Andric 2870e8d8bef9SDimitry Andric // Compute the bit offset into the register of the target element. 2871e8d8bef9SDimitry Andric Register OffsetBits = getBitcastWiderVectorElementOffset( 2872e8d8bef9SDimitry Andric MIRBuilder, Idx, NewEltSize, OldEltSize); 2873e8d8bef9SDimitry Andric 2874e8d8bef9SDimitry Andric Register InsertedElt = buildBitFieldInsert(MIRBuilder, ExtractedElt, 2875e8d8bef9SDimitry Andric Val, OffsetBits); 2876e8d8bef9SDimitry Andric if (CastTy.isVector()) { 2877e8d8bef9SDimitry Andric InsertedElt = MIRBuilder.buildInsertVectorElement( 2878e8d8bef9SDimitry Andric CastTy, CastVec, InsertedElt, ScaledIdx).getReg(0); 2879e8d8bef9SDimitry Andric } 2880e8d8bef9SDimitry Andric 2881e8d8bef9SDimitry Andric MIRBuilder.buildBitcast(Dst, InsertedElt); 2882e8d8bef9SDimitry Andric MI.eraseFromParent(); 28835ffd83dbSDimitry Andric return Legalized; 28845ffd83dbSDimitry Andric } 2885e8d8bef9SDimitry Andric 28865ffd83dbSDimitry Andric return UnableToLegalize; 28870b57cec5SDimitry Andric } 28880b57cec5SDimitry Andric 2889fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerLoad(GAnyLoad &LoadMI) { 28900b57cec5SDimitry Andric // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT 2891fe6060f1SDimitry Andric Register DstReg = LoadMI.getDstReg(); 2892fe6060f1SDimitry Andric Register PtrReg = LoadMI.getPointerReg(); 28930b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 2894fe6060f1SDimitry Andric MachineMemOperand &MMO = LoadMI.getMMO(); 2895fe6060f1SDimitry Andric LLT MemTy = MMO.getMemoryType(); 2896fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 28970b57cec5SDimitry Andric 2898fe6060f1SDimitry Andric unsigned MemSizeInBits = MemTy.getSizeInBits(); 2899fe6060f1SDimitry Andric unsigned MemStoreSizeInBits = 8 * MemTy.getSizeInBytes(); 2900fe6060f1SDimitry Andric 2901fe6060f1SDimitry Andric if (MemSizeInBits != MemStoreSizeInBits) { 2902349cc55cSDimitry Andric if (MemTy.isVector()) 2903349cc55cSDimitry Andric return UnableToLegalize; 2904349cc55cSDimitry Andric 2905fe6060f1SDimitry Andric // Promote to a byte-sized load if not loading an integral number of 2906fe6060f1SDimitry Andric // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 2907fe6060f1SDimitry Andric LLT WideMemTy = LLT::scalar(MemStoreSizeInBits); 2908fe6060f1SDimitry Andric MachineMemOperand *NewMMO = 2909fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, MMO.getPointerInfo(), WideMemTy); 2910fe6060f1SDimitry Andric 2911fe6060f1SDimitry Andric Register LoadReg = DstReg; 2912fe6060f1SDimitry Andric LLT LoadTy = DstTy; 2913fe6060f1SDimitry Andric 2914fe6060f1SDimitry Andric // If this wasn't already an extending load, we need to widen the result 2915fe6060f1SDimitry Andric // register to avoid creating a load with a narrower result than the source. 2916fe6060f1SDimitry Andric if (MemStoreSizeInBits > DstTy.getSizeInBits()) { 2917fe6060f1SDimitry Andric LoadTy = WideMemTy; 2918fe6060f1SDimitry Andric LoadReg = MRI.createGenericVirtualRegister(WideMemTy); 2919fe6060f1SDimitry Andric } 2920fe6060f1SDimitry Andric 2921fe6060f1SDimitry Andric if (isa<GSExtLoad>(LoadMI)) { 2922fe6060f1SDimitry Andric auto NewLoad = MIRBuilder.buildLoad(LoadTy, PtrReg, *NewMMO); 2923fe6060f1SDimitry Andric MIRBuilder.buildSExtInReg(LoadReg, NewLoad, MemSizeInBits); 2924fe6060f1SDimitry Andric } else if (isa<GZExtLoad>(LoadMI) || WideMemTy == DstTy) { 2925fe6060f1SDimitry Andric auto NewLoad = MIRBuilder.buildLoad(LoadTy, PtrReg, *NewMMO); 2926fe6060f1SDimitry Andric // The extra bits are guaranteed to be zero, since we stored them that 2927fe6060f1SDimitry Andric // way. A zext load from Wide thus automatically gives zext from MemVT. 2928fe6060f1SDimitry Andric MIRBuilder.buildAssertZExt(LoadReg, NewLoad, MemSizeInBits); 2929fe6060f1SDimitry Andric } else { 2930fe6060f1SDimitry Andric MIRBuilder.buildLoad(LoadReg, PtrReg, *NewMMO); 2931fe6060f1SDimitry Andric } 2932fe6060f1SDimitry Andric 2933fe6060f1SDimitry Andric if (DstTy != LoadTy) 2934fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, LoadReg); 2935fe6060f1SDimitry Andric 2936fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 2937fe6060f1SDimitry Andric return Legalized; 2938fe6060f1SDimitry Andric } 2939fe6060f1SDimitry Andric 2940fe6060f1SDimitry Andric // Big endian lowering not implemented. 2941fe6060f1SDimitry Andric if (MIRBuilder.getDataLayout().isBigEndian()) 2942fe6060f1SDimitry Andric return UnableToLegalize; 2943fe6060f1SDimitry Andric 2944349cc55cSDimitry Andric // This load needs splitting into power of 2 sized loads. 2945349cc55cSDimitry Andric // 29468bcb0991SDimitry Andric // Our strategy here is to generate anyextending loads for the smaller 29478bcb0991SDimitry Andric // types up to next power-2 result type, and then combine the two larger 29488bcb0991SDimitry Andric // result values together, before truncating back down to the non-pow-2 29498bcb0991SDimitry Andric // type. 29508bcb0991SDimitry Andric // E.g. v1 = i24 load => 29515ffd83dbSDimitry Andric // v2 = i32 zextload (2 byte) 29528bcb0991SDimitry Andric // v3 = i32 load (1 byte) 29538bcb0991SDimitry Andric // v4 = i32 shl v3, 16 29548bcb0991SDimitry Andric // v5 = i32 or v4, v2 29558bcb0991SDimitry Andric // v1 = i24 trunc v5 29568bcb0991SDimitry Andric // By doing this we generate the correct truncate which should get 29578bcb0991SDimitry Andric // combined away as an artifact with a matching extend. 2958349cc55cSDimitry Andric 2959349cc55cSDimitry Andric uint64_t LargeSplitSize, SmallSplitSize; 2960349cc55cSDimitry Andric 2961349cc55cSDimitry Andric if (!isPowerOf2_32(MemSizeInBits)) { 2962349cc55cSDimitry Andric // This load needs splitting into power of 2 sized loads. 2963349cc55cSDimitry Andric LargeSplitSize = PowerOf2Floor(MemSizeInBits); 2964349cc55cSDimitry Andric SmallSplitSize = MemSizeInBits - LargeSplitSize; 2965349cc55cSDimitry Andric } else { 2966349cc55cSDimitry Andric // This is already a power of 2, but we still need to split this in half. 2967349cc55cSDimitry Andric // 2968349cc55cSDimitry Andric // Assume we're being asked to decompose an unaligned load. 2969349cc55cSDimitry Andric // TODO: If this requires multiple splits, handle them all at once. 2970349cc55cSDimitry Andric auto &Ctx = MF.getFunction().getContext(); 2971349cc55cSDimitry Andric if (TLI.allowsMemoryAccess(Ctx, MIRBuilder.getDataLayout(), MemTy, MMO)) 2972349cc55cSDimitry Andric return UnableToLegalize; 2973349cc55cSDimitry Andric 2974349cc55cSDimitry Andric SmallSplitSize = LargeSplitSize = MemSizeInBits / 2; 2975349cc55cSDimitry Andric } 2976349cc55cSDimitry Andric 2977349cc55cSDimitry Andric if (MemTy.isVector()) { 2978349cc55cSDimitry Andric // TODO: Handle vector extloads 2979349cc55cSDimitry Andric if (MemTy != DstTy) 2980349cc55cSDimitry Andric return UnableToLegalize; 2981349cc55cSDimitry Andric 2982349cc55cSDimitry Andric // TODO: We can do better than scalarizing the vector and at least split it 2983349cc55cSDimitry Andric // in half. 2984349cc55cSDimitry Andric return reduceLoadStoreWidth(LoadMI, 0, DstTy.getElementType()); 2985349cc55cSDimitry Andric } 29868bcb0991SDimitry Andric 29878bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 29888bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 2989fe6060f1SDimitry Andric MachineMemOperand *SmallMMO = 2990fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8); 29918bcb0991SDimitry Andric 29928bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 2993fe6060f1SDimitry Andric unsigned AnyExtSize = PowerOf2Ceil(DstTy.getSizeInBits()); 29948bcb0991SDimitry Andric LLT AnyExtTy = LLT::scalar(AnyExtSize); 2995fe6060f1SDimitry Andric auto LargeLoad = MIRBuilder.buildLoadInstr(TargetOpcode::G_ZEXTLOAD, AnyExtTy, 2996fe6060f1SDimitry Andric PtrReg, *LargeMMO); 29978bcb0991SDimitry Andric 2998fe6060f1SDimitry Andric auto OffsetCst = MIRBuilder.buildConstant(LLT::scalar(PtrTy.getSizeInBits()), 2999fe6060f1SDimitry Andric LargeSplitSize / 8); 3000480093f4SDimitry Andric Register PtrAddReg = MRI.createGenericVirtualRegister(PtrTy); 3001fe6060f1SDimitry Andric auto SmallPtr = MIRBuilder.buildPtrAdd(PtrAddReg, PtrReg, OffsetCst); 3002fe6060f1SDimitry Andric auto SmallLoad = MIRBuilder.buildLoadInstr(LoadMI.getOpcode(), AnyExtTy, 3003fe6060f1SDimitry Andric SmallPtr, *SmallMMO); 30048bcb0991SDimitry Andric 30058bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(AnyExtTy, LargeSplitSize); 30068bcb0991SDimitry Andric auto Shift = MIRBuilder.buildShl(AnyExtTy, SmallLoad, ShiftAmt); 3007fe6060f1SDimitry Andric 3008fe6060f1SDimitry Andric if (AnyExtTy == DstTy) 3009fe6060f1SDimitry Andric MIRBuilder.buildOr(DstReg, Shift, LargeLoad); 3010349cc55cSDimitry Andric else if (AnyExtTy.getSizeInBits() != DstTy.getSizeInBits()) { 30118bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad); 3012fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, {Or}); 3013349cc55cSDimitry Andric } else { 3014349cc55cSDimitry Andric assert(DstTy.isPointer() && "expected pointer"); 3015349cc55cSDimitry Andric auto Or = MIRBuilder.buildOr(AnyExtTy, Shift, LargeLoad); 3016349cc55cSDimitry Andric 3017349cc55cSDimitry Andric // FIXME: We currently consider this to be illegal for non-integral address 3018349cc55cSDimitry Andric // spaces, but we need still need a way to reinterpret the bits. 3019349cc55cSDimitry Andric MIRBuilder.buildIntToPtr(DstReg, Or); 3020fe6060f1SDimitry Andric } 3021fe6060f1SDimitry Andric 3022fe6060f1SDimitry Andric LoadMI.eraseFromParent(); 30238bcb0991SDimitry Andric return Legalized; 30248bcb0991SDimitry Andric } 3025e8d8bef9SDimitry Andric 3026fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerStore(GStore &StoreMI) { 30278bcb0991SDimitry Andric // Lower a non-power of 2 store into multiple pow-2 stores. 30288bcb0991SDimitry Andric // E.g. split an i24 store into an i16 store + i8 store. 30298bcb0991SDimitry Andric // We do this by first extending the stored value to the next largest power 30308bcb0991SDimitry Andric // of 2 type, and then using truncating stores to store the components. 30318bcb0991SDimitry Andric // By doing this, likewise with G_LOAD, generate an extend that can be 30328bcb0991SDimitry Andric // artifact-combined away instead of leaving behind extracts. 3033fe6060f1SDimitry Andric Register SrcReg = StoreMI.getValueReg(); 3034fe6060f1SDimitry Andric Register PtrReg = StoreMI.getPointerReg(); 30358bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 3036fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 3037fe6060f1SDimitry Andric MachineMemOperand &MMO = **StoreMI.memoperands_begin(); 3038fe6060f1SDimitry Andric LLT MemTy = MMO.getMemoryType(); 3039fe6060f1SDimitry Andric 3040fe6060f1SDimitry Andric unsigned StoreWidth = MemTy.getSizeInBits(); 3041fe6060f1SDimitry Andric unsigned StoreSizeInBits = 8 * MemTy.getSizeInBytes(); 3042fe6060f1SDimitry Andric 3043fe6060f1SDimitry Andric if (StoreWidth != StoreSizeInBits) { 3044349cc55cSDimitry Andric if (SrcTy.isVector()) 3045349cc55cSDimitry Andric return UnableToLegalize; 3046349cc55cSDimitry Andric 3047fe6060f1SDimitry Andric // Promote to a byte-sized store with upper bits zero if not 3048fe6060f1SDimitry Andric // storing an integral number of bytes. For example, promote 3049fe6060f1SDimitry Andric // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 3050fe6060f1SDimitry Andric LLT WideTy = LLT::scalar(StoreSizeInBits); 3051fe6060f1SDimitry Andric 3052fe6060f1SDimitry Andric if (StoreSizeInBits > SrcTy.getSizeInBits()) { 3053fe6060f1SDimitry Andric // Avoid creating a store with a narrower source than result. 3054fe6060f1SDimitry Andric SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0); 3055fe6060f1SDimitry Andric SrcTy = WideTy; 3056fe6060f1SDimitry Andric } 3057fe6060f1SDimitry Andric 3058fe6060f1SDimitry Andric auto ZextInReg = MIRBuilder.buildZExtInReg(SrcTy, SrcReg, StoreWidth); 3059fe6060f1SDimitry Andric 3060fe6060f1SDimitry Andric MachineMemOperand *NewMMO = 3061fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, MMO.getPointerInfo(), WideTy); 3062fe6060f1SDimitry Andric MIRBuilder.buildStore(ZextInReg, PtrReg, *NewMMO); 3063fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 3064fe6060f1SDimitry Andric return Legalized; 3065fe6060f1SDimitry Andric } 3066fe6060f1SDimitry Andric 3067349cc55cSDimitry Andric if (MemTy.isVector()) { 3068349cc55cSDimitry Andric // TODO: Handle vector trunc stores 3069349cc55cSDimitry Andric if (MemTy != SrcTy) 3070349cc55cSDimitry Andric return UnableToLegalize; 3071349cc55cSDimitry Andric 3072349cc55cSDimitry Andric // TODO: We can do better than scalarizing the vector and at least split it 3073349cc55cSDimitry Andric // in half. 3074349cc55cSDimitry Andric return reduceLoadStoreWidth(StoreMI, 0, SrcTy.getElementType()); 3075349cc55cSDimitry Andric } 3076349cc55cSDimitry Andric 3077349cc55cSDimitry Andric unsigned MemSizeInBits = MemTy.getSizeInBits(); 3078349cc55cSDimitry Andric uint64_t LargeSplitSize, SmallSplitSize; 3079349cc55cSDimitry Andric 3080349cc55cSDimitry Andric if (!isPowerOf2_32(MemSizeInBits)) { 3081349cc55cSDimitry Andric LargeSplitSize = PowerOf2Floor(MemTy.getSizeInBits()); 3082349cc55cSDimitry Andric SmallSplitSize = MemTy.getSizeInBits() - LargeSplitSize; 3083349cc55cSDimitry Andric } else { 3084349cc55cSDimitry Andric auto &Ctx = MF.getFunction().getContext(); 3085349cc55cSDimitry Andric if (TLI.allowsMemoryAccess(Ctx, MIRBuilder.getDataLayout(), MemTy, MMO)) 30868bcb0991SDimitry Andric return UnableToLegalize; // Don't know what we're being asked to do. 30878bcb0991SDimitry Andric 3088349cc55cSDimitry Andric SmallSplitSize = LargeSplitSize = MemSizeInBits / 2; 3089349cc55cSDimitry Andric } 3090349cc55cSDimitry Andric 3091fe6060f1SDimitry Andric // Extend to the next pow-2. If this store was itself the result of lowering, 3092fe6060f1SDimitry Andric // e.g. an s56 store being broken into s32 + s24, we might have a stored type 3093349cc55cSDimitry Andric // that's wider than the stored size. 3094349cc55cSDimitry Andric unsigned AnyExtSize = PowerOf2Ceil(MemTy.getSizeInBits()); 3095349cc55cSDimitry Andric const LLT NewSrcTy = LLT::scalar(AnyExtSize); 3096349cc55cSDimitry Andric 3097349cc55cSDimitry Andric if (SrcTy.isPointer()) { 3098349cc55cSDimitry Andric const LLT IntPtrTy = LLT::scalar(SrcTy.getSizeInBits()); 3099349cc55cSDimitry Andric SrcReg = MIRBuilder.buildPtrToInt(IntPtrTy, SrcReg).getReg(0); 3100349cc55cSDimitry Andric } 3101349cc55cSDimitry Andric 3102fe6060f1SDimitry Andric auto ExtVal = MIRBuilder.buildAnyExtOrTrunc(NewSrcTy, SrcReg); 31038bcb0991SDimitry Andric 31048bcb0991SDimitry Andric // Obtain the smaller value by shifting away the larger value. 3105fe6060f1SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(NewSrcTy, LargeSplitSize); 3106fe6060f1SDimitry Andric auto SmallVal = MIRBuilder.buildLShr(NewSrcTy, ExtVal, ShiftAmt); 31078bcb0991SDimitry Andric 3108480093f4SDimitry Andric // Generate the PtrAdd and truncating stores. 31098bcb0991SDimitry Andric LLT PtrTy = MRI.getType(PtrReg); 31105ffd83dbSDimitry Andric auto OffsetCst = MIRBuilder.buildConstant( 31115ffd83dbSDimitry Andric LLT::scalar(PtrTy.getSizeInBits()), LargeSplitSize / 8); 3112480093f4SDimitry Andric auto SmallPtr = 3113349cc55cSDimitry Andric MIRBuilder.buildPtrAdd(PtrTy, PtrReg, OffsetCst); 31148bcb0991SDimitry Andric 31158bcb0991SDimitry Andric MachineMemOperand *LargeMMO = 31168bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, 0, LargeSplitSize / 8); 31178bcb0991SDimitry Andric MachineMemOperand *SmallMMO = 31188bcb0991SDimitry Andric MF.getMachineMemOperand(&MMO, LargeSplitSize / 8, SmallSplitSize / 8); 3119fe6060f1SDimitry Andric MIRBuilder.buildStore(ExtVal, PtrReg, *LargeMMO); 3120fe6060f1SDimitry Andric MIRBuilder.buildStore(SmallVal, SmallPtr, *SmallMMO); 3121fe6060f1SDimitry Andric StoreMI.eraseFromParent(); 31228bcb0991SDimitry Andric return Legalized; 31238bcb0991SDimitry Andric } 3124e8d8bef9SDimitry Andric 3125e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 3126e8d8bef9SDimitry Andric LegalizerHelper::bitcast(MachineInstr &MI, unsigned TypeIdx, LLT CastTy) { 3127e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 3128e8d8bef9SDimitry Andric case TargetOpcode::G_LOAD: { 3129e8d8bef9SDimitry Andric if (TypeIdx != 0) 3130e8d8bef9SDimitry Andric return UnableToLegalize; 3131fe6060f1SDimitry Andric MachineMemOperand &MMO = **MI.memoperands_begin(); 3132fe6060f1SDimitry Andric 3133fe6060f1SDimitry Andric // Not sure how to interpret a bitcast of an extending load. 3134fe6060f1SDimitry Andric if (MMO.getMemoryType().getSizeInBits() != CastTy.getSizeInBits()) 3135fe6060f1SDimitry Andric return UnableToLegalize; 3136e8d8bef9SDimitry Andric 3137e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3138e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3139fe6060f1SDimitry Andric MMO.setType(CastTy); 3140e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3141e8d8bef9SDimitry Andric return Legalized; 3142e8d8bef9SDimitry Andric } 3143e8d8bef9SDimitry Andric case TargetOpcode::G_STORE: { 3144e8d8bef9SDimitry Andric if (TypeIdx != 0) 3145e8d8bef9SDimitry Andric return UnableToLegalize; 3146e8d8bef9SDimitry Andric 3147fe6060f1SDimitry Andric MachineMemOperand &MMO = **MI.memoperands_begin(); 3148fe6060f1SDimitry Andric 3149fe6060f1SDimitry Andric // Not sure how to interpret a bitcast of a truncating store. 3150fe6060f1SDimitry Andric if (MMO.getMemoryType().getSizeInBits() != CastTy.getSizeInBits()) 3151fe6060f1SDimitry Andric return UnableToLegalize; 3152fe6060f1SDimitry Andric 3153e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3154e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 0); 3155fe6060f1SDimitry Andric MMO.setType(CastTy); 3156e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3157e8d8bef9SDimitry Andric return Legalized; 3158e8d8bef9SDimitry Andric } 3159e8d8bef9SDimitry Andric case TargetOpcode::G_SELECT: { 3160e8d8bef9SDimitry Andric if (TypeIdx != 0) 3161e8d8bef9SDimitry Andric return UnableToLegalize; 3162e8d8bef9SDimitry Andric 3163e8d8bef9SDimitry Andric if (MRI.getType(MI.getOperand(1).getReg()).isVector()) { 3164e8d8bef9SDimitry Andric LLVM_DEBUG( 3165e8d8bef9SDimitry Andric dbgs() << "bitcast action not implemented for vector select\n"); 3166e8d8bef9SDimitry Andric return UnableToLegalize; 3167e8d8bef9SDimitry Andric } 3168e8d8bef9SDimitry Andric 3169e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3170e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 2); 3171e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 3); 3172e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3173e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3174e8d8bef9SDimitry Andric return Legalized; 3175e8d8bef9SDimitry Andric } 3176e8d8bef9SDimitry Andric case TargetOpcode::G_AND: 3177e8d8bef9SDimitry Andric case TargetOpcode::G_OR: 3178e8d8bef9SDimitry Andric case TargetOpcode::G_XOR: { 3179e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3180e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 1); 3181e8d8bef9SDimitry Andric bitcastSrc(MI, CastTy, 2); 3182e8d8bef9SDimitry Andric bitcastDst(MI, CastTy, 0); 3183e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3184e8d8bef9SDimitry Andric return Legalized; 3185e8d8bef9SDimitry Andric } 3186e8d8bef9SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: 3187e8d8bef9SDimitry Andric return bitcastExtractVectorElt(MI, TypeIdx, CastTy); 3188e8d8bef9SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: 3189e8d8bef9SDimitry Andric return bitcastInsertVectorElt(MI, TypeIdx, CastTy); 3190e8d8bef9SDimitry Andric default: 3191e8d8bef9SDimitry Andric return UnableToLegalize; 3192e8d8bef9SDimitry Andric } 3193e8d8bef9SDimitry Andric } 3194e8d8bef9SDimitry Andric 3195e8d8bef9SDimitry Andric // Legalize an instruction by changing the opcode in place. 3196e8d8bef9SDimitry Andric void LegalizerHelper::changeOpcode(MachineInstr &MI, unsigned NewOpcode) { 3197e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3198e8d8bef9SDimitry Andric MI.setDesc(MIRBuilder.getTII().get(NewOpcode)); 3199e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3200e8d8bef9SDimitry Andric } 3201e8d8bef9SDimitry Andric 3202e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 3203e8d8bef9SDimitry Andric LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT LowerHintTy) { 3204e8d8bef9SDimitry Andric using namespace TargetOpcode; 3205e8d8bef9SDimitry Andric 3206e8d8bef9SDimitry Andric switch(MI.getOpcode()) { 3207e8d8bef9SDimitry Andric default: 3208e8d8bef9SDimitry Andric return UnableToLegalize; 3209e8d8bef9SDimitry Andric case TargetOpcode::G_BITCAST: 3210e8d8bef9SDimitry Andric return lowerBitcast(MI); 3211e8d8bef9SDimitry Andric case TargetOpcode::G_SREM: 3212e8d8bef9SDimitry Andric case TargetOpcode::G_UREM: { 3213e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3214e8d8bef9SDimitry Andric auto Quot = 3215e8d8bef9SDimitry Andric MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV, {Ty}, 3216e8d8bef9SDimitry Andric {MI.getOperand(1), MI.getOperand(2)}); 3217e8d8bef9SDimitry Andric 3218e8d8bef9SDimitry Andric auto Prod = MIRBuilder.buildMul(Ty, Quot, MI.getOperand(2)); 3219e8d8bef9SDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MI.getOperand(1), Prod); 3220e8d8bef9SDimitry Andric MI.eraseFromParent(); 3221e8d8bef9SDimitry Andric return Legalized; 3222e8d8bef9SDimitry Andric } 3223e8d8bef9SDimitry Andric case TargetOpcode::G_SADDO: 3224e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBO: 3225e8d8bef9SDimitry Andric return lowerSADDO_SSUBO(MI); 3226e8d8bef9SDimitry Andric case TargetOpcode::G_UMULH: 3227e8d8bef9SDimitry Andric case TargetOpcode::G_SMULH: 3228e8d8bef9SDimitry Andric return lowerSMULH_UMULH(MI); 3229e8d8bef9SDimitry Andric case TargetOpcode::G_SMULO: 3230e8d8bef9SDimitry Andric case TargetOpcode::G_UMULO: { 3231e8d8bef9SDimitry Andric // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the 3232e8d8bef9SDimitry Andric // result. 3233e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3234e8d8bef9SDimitry Andric Register Overflow = MI.getOperand(1).getReg(); 3235e8d8bef9SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 3236e8d8bef9SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 3237e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3238e8d8bef9SDimitry Andric 3239e8d8bef9SDimitry Andric unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO 3240e8d8bef9SDimitry Andric ? TargetOpcode::G_SMULH 3241e8d8bef9SDimitry Andric : TargetOpcode::G_UMULH; 3242e8d8bef9SDimitry Andric 3243e8d8bef9SDimitry Andric Observer.changingInstr(MI); 3244e8d8bef9SDimitry Andric const auto &TII = MIRBuilder.getTII(); 3245e8d8bef9SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_MUL)); 3246e8d8bef9SDimitry Andric MI.RemoveOperand(1); 3247e8d8bef9SDimitry Andric Observer.changedInstr(MI); 3248e8d8bef9SDimitry Andric 3249e8d8bef9SDimitry Andric auto HiPart = MIRBuilder.buildInstr(Opcode, {Ty}, {LHS, RHS}); 3250e8d8bef9SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 3251e8d8bef9SDimitry Andric 3252e8d8bef9SDimitry Andric // Move insert point forward so we can use the Res register if needed. 3253e8d8bef9SDimitry Andric MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); 3254e8d8bef9SDimitry Andric 3255e8d8bef9SDimitry Andric // For *signed* multiply, overflow is detected by checking: 3256e8d8bef9SDimitry Andric // (hi != (lo >> bitwidth-1)) 3257e8d8bef9SDimitry Andric if (Opcode == TargetOpcode::G_SMULH) { 3258e8d8bef9SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, Ty.getSizeInBits() - 1); 3259e8d8bef9SDimitry Andric auto Shifted = MIRBuilder.buildAShr(Ty, Res, ShiftAmt); 3260e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted); 3261e8d8bef9SDimitry Andric } else { 3262e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero); 3263e8d8bef9SDimitry Andric } 3264e8d8bef9SDimitry Andric return Legalized; 3265e8d8bef9SDimitry Andric } 3266e8d8bef9SDimitry Andric case TargetOpcode::G_FNEG: { 3267e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3268e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3269e8d8bef9SDimitry Andric 3270e8d8bef9SDimitry Andric // TODO: Handle vector types once we are able to 3271e8d8bef9SDimitry Andric // represent them. 3272e8d8bef9SDimitry Andric if (Ty.isVector()) 3273e8d8bef9SDimitry Andric return UnableToLegalize; 3274e8d8bef9SDimitry Andric auto SignMask = 3275e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignMask(Ty.getSizeInBits())); 3276e8d8bef9SDimitry Andric Register SubByReg = MI.getOperand(1).getReg(); 3277e8d8bef9SDimitry Andric MIRBuilder.buildXor(Res, SubByReg, SignMask); 3278e8d8bef9SDimitry Andric MI.eraseFromParent(); 3279e8d8bef9SDimitry Andric return Legalized; 3280e8d8bef9SDimitry Andric } 3281e8d8bef9SDimitry Andric case TargetOpcode::G_FSUB: { 3282e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 3283e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 3284e8d8bef9SDimitry Andric 3285e8d8bef9SDimitry Andric // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)). 3286e8d8bef9SDimitry Andric // First, check if G_FNEG is marked as Lower. If so, we may 3287e8d8bef9SDimitry Andric // end up with an infinite loop as G_FSUB is used to legalize G_FNEG. 3288e8d8bef9SDimitry Andric if (LI.getAction({G_FNEG, {Ty}}).Action == Lower) 3289e8d8bef9SDimitry Andric return UnableToLegalize; 3290e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 3291e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 3292e8d8bef9SDimitry Andric Register Neg = MRI.createGenericVirtualRegister(Ty); 3293e8d8bef9SDimitry Andric MIRBuilder.buildFNeg(Neg, RHS); 3294e8d8bef9SDimitry Andric MIRBuilder.buildFAdd(Res, LHS, Neg, MI.getFlags()); 3295e8d8bef9SDimitry Andric MI.eraseFromParent(); 3296e8d8bef9SDimitry Andric return Legalized; 3297e8d8bef9SDimitry Andric } 3298e8d8bef9SDimitry Andric case TargetOpcode::G_FMAD: 3299e8d8bef9SDimitry Andric return lowerFMad(MI); 3300e8d8bef9SDimitry Andric case TargetOpcode::G_FFLOOR: 3301e8d8bef9SDimitry Andric return lowerFFloor(MI); 3302e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUND: 3303e8d8bef9SDimitry Andric return lowerIntrinsicRound(MI); 3304e8d8bef9SDimitry Andric case TargetOpcode::G_INTRINSIC_ROUNDEVEN: { 3305e8d8bef9SDimitry Andric // Since round even is the assumed rounding mode for unconstrained FP 3306e8d8bef9SDimitry Andric // operations, rint and roundeven are the same operation. 3307e8d8bef9SDimitry Andric changeOpcode(MI, TargetOpcode::G_FRINT); 3308e8d8bef9SDimitry Andric return Legalized; 3309e8d8bef9SDimitry Andric } 3310e8d8bef9SDimitry Andric case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: { 3311e8d8bef9SDimitry Andric Register OldValRes = MI.getOperand(0).getReg(); 3312e8d8bef9SDimitry Andric Register SuccessRes = MI.getOperand(1).getReg(); 3313e8d8bef9SDimitry Andric Register Addr = MI.getOperand(2).getReg(); 3314e8d8bef9SDimitry Andric Register CmpVal = MI.getOperand(3).getReg(); 3315e8d8bef9SDimitry Andric Register NewVal = MI.getOperand(4).getReg(); 3316e8d8bef9SDimitry Andric MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal, 3317e8d8bef9SDimitry Andric **MI.memoperands_begin()); 3318e8d8bef9SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal); 3319e8d8bef9SDimitry Andric MI.eraseFromParent(); 3320e8d8bef9SDimitry Andric return Legalized; 3321e8d8bef9SDimitry Andric } 3322e8d8bef9SDimitry Andric case TargetOpcode::G_LOAD: 3323e8d8bef9SDimitry Andric case TargetOpcode::G_SEXTLOAD: 3324e8d8bef9SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 3325fe6060f1SDimitry Andric return lowerLoad(cast<GAnyLoad>(MI)); 3326e8d8bef9SDimitry Andric case TargetOpcode::G_STORE: 3327fe6060f1SDimitry Andric return lowerStore(cast<GStore>(MI)); 33280b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: 33290b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: 33300b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: 33310b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: 33320b57cec5SDimitry Andric case TargetOpcode::G_CTPOP: 3333e8d8bef9SDimitry Andric return lowerBitCount(MI); 33340b57cec5SDimitry Andric case G_UADDO: { 33350b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33360b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 33370b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33380b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33390b57cec5SDimitry Andric 33400b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, LHS, RHS); 33410b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS); 33420b57cec5SDimitry Andric 33430b57cec5SDimitry Andric MI.eraseFromParent(); 33440b57cec5SDimitry Andric return Legalized; 33450b57cec5SDimitry Andric } 33460b57cec5SDimitry Andric case G_UADDE: { 33470b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33480b57cec5SDimitry Andric Register CarryOut = MI.getOperand(1).getReg(); 33490b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33500b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33510b57cec5SDimitry Andric Register CarryIn = MI.getOperand(4).getReg(); 33525ffd83dbSDimitry Andric LLT Ty = MRI.getType(Res); 33530b57cec5SDimitry Andric 33545ffd83dbSDimitry Andric auto TmpRes = MIRBuilder.buildAdd(Ty, LHS, RHS); 33555ffd83dbSDimitry Andric auto ZExtCarryIn = MIRBuilder.buildZExt(Ty, CarryIn); 33560b57cec5SDimitry Andric MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn); 33570b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS); 33580b57cec5SDimitry Andric 33590b57cec5SDimitry Andric MI.eraseFromParent(); 33600b57cec5SDimitry Andric return Legalized; 33610b57cec5SDimitry Andric } 33620b57cec5SDimitry Andric case G_USUBO: { 33630b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33640b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 33650b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33660b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33670b57cec5SDimitry Andric 33680b57cec5SDimitry Andric MIRBuilder.buildSub(Res, LHS, RHS); 33690b57cec5SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS); 33700b57cec5SDimitry Andric 33710b57cec5SDimitry Andric MI.eraseFromParent(); 33720b57cec5SDimitry Andric return Legalized; 33730b57cec5SDimitry Andric } 33740b57cec5SDimitry Andric case G_USUBE: { 33750b57cec5SDimitry Andric Register Res = MI.getOperand(0).getReg(); 33760b57cec5SDimitry Andric Register BorrowOut = MI.getOperand(1).getReg(); 33770b57cec5SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 33780b57cec5SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 33790b57cec5SDimitry Andric Register BorrowIn = MI.getOperand(4).getReg(); 33805ffd83dbSDimitry Andric const LLT CondTy = MRI.getType(BorrowOut); 33815ffd83dbSDimitry Andric const LLT Ty = MRI.getType(Res); 33820b57cec5SDimitry Andric 33835ffd83dbSDimitry Andric auto TmpRes = MIRBuilder.buildSub(Ty, LHS, RHS); 33845ffd83dbSDimitry Andric auto ZExtBorrowIn = MIRBuilder.buildZExt(Ty, BorrowIn); 33850b57cec5SDimitry Andric MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn); 33865ffd83dbSDimitry Andric 33875ffd83dbSDimitry Andric auto LHS_EQ_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, CondTy, LHS, RHS); 33885ffd83dbSDimitry Andric auto LHS_ULT_RHS = MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CondTy, LHS, RHS); 33890b57cec5SDimitry Andric MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS); 33900b57cec5SDimitry Andric 33910b57cec5SDimitry Andric MI.eraseFromParent(); 33920b57cec5SDimitry Andric return Legalized; 33930b57cec5SDimitry Andric } 33940b57cec5SDimitry Andric case G_UITOFP: 3395e8d8bef9SDimitry Andric return lowerUITOFP(MI); 33960b57cec5SDimitry Andric case G_SITOFP: 3397e8d8bef9SDimitry Andric return lowerSITOFP(MI); 33988bcb0991SDimitry Andric case G_FPTOUI: 3399e8d8bef9SDimitry Andric return lowerFPTOUI(MI); 34005ffd83dbSDimitry Andric case G_FPTOSI: 34015ffd83dbSDimitry Andric return lowerFPTOSI(MI); 34025ffd83dbSDimitry Andric case G_FPTRUNC: 3403e8d8bef9SDimitry Andric return lowerFPTRUNC(MI); 3404e8d8bef9SDimitry Andric case G_FPOWI: 3405e8d8bef9SDimitry Andric return lowerFPOWI(MI); 34060b57cec5SDimitry Andric case G_SMIN: 34070b57cec5SDimitry Andric case G_SMAX: 34080b57cec5SDimitry Andric case G_UMIN: 34090b57cec5SDimitry Andric case G_UMAX: 3410e8d8bef9SDimitry Andric return lowerMinMax(MI); 34110b57cec5SDimitry Andric case G_FCOPYSIGN: 3412e8d8bef9SDimitry Andric return lowerFCopySign(MI); 34130b57cec5SDimitry Andric case G_FMINNUM: 34140b57cec5SDimitry Andric case G_FMAXNUM: 34150b57cec5SDimitry Andric return lowerFMinNumMaxNum(MI); 34165ffd83dbSDimitry Andric case G_MERGE_VALUES: 34175ffd83dbSDimitry Andric return lowerMergeValues(MI); 34188bcb0991SDimitry Andric case G_UNMERGE_VALUES: 34198bcb0991SDimitry Andric return lowerUnmergeValues(MI); 34208bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 34218bcb0991SDimitry Andric assert(MI.getOperand(2).isImm() && "Expected immediate"); 34228bcb0991SDimitry Andric int64_t SizeInBits = MI.getOperand(2).getImm(); 34238bcb0991SDimitry Andric 34248bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 34258bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 34268bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 34278bcb0991SDimitry Andric Register TmpRes = MRI.createGenericVirtualRegister(DstTy); 34288bcb0991SDimitry Andric 34298bcb0991SDimitry Andric auto MIBSz = MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - SizeInBits); 34305ffd83dbSDimitry Andric MIRBuilder.buildShl(TmpRes, SrcReg, MIBSz->getOperand(0)); 34315ffd83dbSDimitry Andric MIRBuilder.buildAShr(DstReg, TmpRes, MIBSz->getOperand(0)); 34328bcb0991SDimitry Andric MI.eraseFromParent(); 34338bcb0991SDimitry Andric return Legalized; 34348bcb0991SDimitry Andric } 3435e8d8bef9SDimitry Andric case G_EXTRACT_VECTOR_ELT: 3436e8d8bef9SDimitry Andric case G_INSERT_VECTOR_ELT: 3437e8d8bef9SDimitry Andric return lowerExtractInsertVectorElt(MI); 34388bcb0991SDimitry Andric case G_SHUFFLE_VECTOR: 34398bcb0991SDimitry Andric return lowerShuffleVector(MI); 34408bcb0991SDimitry Andric case G_DYN_STACKALLOC: 34418bcb0991SDimitry Andric return lowerDynStackAlloc(MI); 34428bcb0991SDimitry Andric case G_EXTRACT: 34438bcb0991SDimitry Andric return lowerExtract(MI); 34448bcb0991SDimitry Andric case G_INSERT: 34458bcb0991SDimitry Andric return lowerInsert(MI); 3446480093f4SDimitry Andric case G_BSWAP: 3447480093f4SDimitry Andric return lowerBswap(MI); 3448480093f4SDimitry Andric case G_BITREVERSE: 3449480093f4SDimitry Andric return lowerBitreverse(MI); 3450480093f4SDimitry Andric case G_READ_REGISTER: 34515ffd83dbSDimitry Andric case G_WRITE_REGISTER: 34525ffd83dbSDimitry Andric return lowerReadWriteRegister(MI); 3453e8d8bef9SDimitry Andric case G_UADDSAT: 3454e8d8bef9SDimitry Andric case G_USUBSAT: { 3455e8d8bef9SDimitry Andric // Try to make a reasonable guess about which lowering strategy to use. The 3456e8d8bef9SDimitry Andric // target can override this with custom lowering and calling the 3457e8d8bef9SDimitry Andric // implementation functions. 3458e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3459e8d8bef9SDimitry Andric if (LI.isLegalOrCustom({G_UMIN, Ty})) 3460e8d8bef9SDimitry Andric return lowerAddSubSatToMinMax(MI); 3461e8d8bef9SDimitry Andric return lowerAddSubSatToAddoSubo(MI); 34620b57cec5SDimitry Andric } 3463e8d8bef9SDimitry Andric case G_SADDSAT: 3464e8d8bef9SDimitry Andric case G_SSUBSAT: { 3465e8d8bef9SDimitry Andric LLT Ty = MRI.getType(MI.getOperand(0).getReg()); 3466e8d8bef9SDimitry Andric 3467e8d8bef9SDimitry Andric // FIXME: It would probably make more sense to see if G_SADDO is preferred, 3468e8d8bef9SDimitry Andric // since it's a shorter expansion. However, we would need to figure out the 3469e8d8bef9SDimitry Andric // preferred boolean type for the carry out for the query. 3470e8d8bef9SDimitry Andric if (LI.isLegalOrCustom({G_SMIN, Ty}) && LI.isLegalOrCustom({G_SMAX, Ty})) 3471e8d8bef9SDimitry Andric return lowerAddSubSatToMinMax(MI); 3472e8d8bef9SDimitry Andric return lowerAddSubSatToAddoSubo(MI); 3473e8d8bef9SDimitry Andric } 3474e8d8bef9SDimitry Andric case G_SSHLSAT: 3475e8d8bef9SDimitry Andric case G_USHLSAT: 3476e8d8bef9SDimitry Andric return lowerShlSat(MI); 3477fe6060f1SDimitry Andric case G_ABS: 3478fe6060f1SDimitry Andric return lowerAbsToAddXor(MI); 3479e8d8bef9SDimitry Andric case G_SELECT: 3480e8d8bef9SDimitry Andric return lowerSelect(MI); 3481fe6060f1SDimitry Andric case G_SDIVREM: 3482fe6060f1SDimitry Andric case G_UDIVREM: 3483fe6060f1SDimitry Andric return lowerDIVREM(MI); 3484fe6060f1SDimitry Andric case G_FSHL: 3485fe6060f1SDimitry Andric case G_FSHR: 3486fe6060f1SDimitry Andric return lowerFunnelShift(MI); 3487fe6060f1SDimitry Andric case G_ROTL: 3488fe6060f1SDimitry Andric case G_ROTR: 3489fe6060f1SDimitry Andric return lowerRotate(MI); 3490349cc55cSDimitry Andric case G_MEMSET: 3491349cc55cSDimitry Andric case G_MEMCPY: 3492349cc55cSDimitry Andric case G_MEMMOVE: 3493349cc55cSDimitry Andric return lowerMemCpyFamily(MI); 3494349cc55cSDimitry Andric case G_MEMCPY_INLINE: 3495349cc55cSDimitry Andric return lowerMemcpyInline(MI); 3496349cc55cSDimitry Andric GISEL_VECREDUCE_CASES_NONSEQ 3497349cc55cSDimitry Andric return lowerVectorReduction(MI); 3498e8d8bef9SDimitry Andric } 3499e8d8bef9SDimitry Andric } 3500e8d8bef9SDimitry Andric 3501e8d8bef9SDimitry Andric Align LegalizerHelper::getStackTemporaryAlignment(LLT Ty, 3502e8d8bef9SDimitry Andric Align MinAlign) const { 3503e8d8bef9SDimitry Andric // FIXME: We're missing a way to go back from LLT to llvm::Type to query the 3504e8d8bef9SDimitry Andric // datalayout for the preferred alignment. Also there should be a target hook 3505e8d8bef9SDimitry Andric // for this to allow targets to reduce the alignment and ignore the 3506e8d8bef9SDimitry Andric // datalayout. e.g. AMDGPU should always use a 4-byte alignment, regardless of 3507e8d8bef9SDimitry Andric // the type. 3508e8d8bef9SDimitry Andric return std::max(Align(PowerOf2Ceil(Ty.getSizeInBytes())), MinAlign); 3509e8d8bef9SDimitry Andric } 3510e8d8bef9SDimitry Andric 3511e8d8bef9SDimitry Andric MachineInstrBuilder 3512e8d8bef9SDimitry Andric LegalizerHelper::createStackTemporary(TypeSize Bytes, Align Alignment, 3513e8d8bef9SDimitry Andric MachinePointerInfo &PtrInfo) { 3514e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 3515e8d8bef9SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 3516e8d8bef9SDimitry Andric int FrameIdx = MF.getFrameInfo().CreateStackObject(Bytes, Alignment, false); 3517e8d8bef9SDimitry Andric 3518e8d8bef9SDimitry Andric unsigned AddrSpace = DL.getAllocaAddrSpace(); 3519e8d8bef9SDimitry Andric LLT FramePtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace)); 3520e8d8bef9SDimitry Andric 3521e8d8bef9SDimitry Andric PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIdx); 3522e8d8bef9SDimitry Andric return MIRBuilder.buildFrameIndex(FramePtrTy, FrameIdx); 3523e8d8bef9SDimitry Andric } 3524e8d8bef9SDimitry Andric 3525e8d8bef9SDimitry Andric static Register clampDynamicVectorIndex(MachineIRBuilder &B, Register IdxReg, 3526e8d8bef9SDimitry Andric LLT VecTy) { 3527e8d8bef9SDimitry Andric int64_t IdxVal; 3528e8d8bef9SDimitry Andric if (mi_match(IdxReg, *B.getMRI(), m_ICst(IdxVal))) 3529e8d8bef9SDimitry Andric return IdxReg; 3530e8d8bef9SDimitry Andric 3531e8d8bef9SDimitry Andric LLT IdxTy = B.getMRI()->getType(IdxReg); 3532e8d8bef9SDimitry Andric unsigned NElts = VecTy.getNumElements(); 3533e8d8bef9SDimitry Andric if (isPowerOf2_32(NElts)) { 3534e8d8bef9SDimitry Andric APInt Imm = APInt::getLowBitsSet(IdxTy.getSizeInBits(), Log2_32(NElts)); 3535e8d8bef9SDimitry Andric return B.buildAnd(IdxTy, IdxReg, B.buildConstant(IdxTy, Imm)).getReg(0); 3536e8d8bef9SDimitry Andric } 3537e8d8bef9SDimitry Andric 3538e8d8bef9SDimitry Andric return B.buildUMin(IdxTy, IdxReg, B.buildConstant(IdxTy, NElts - 1)) 3539e8d8bef9SDimitry Andric .getReg(0); 3540e8d8bef9SDimitry Andric } 3541e8d8bef9SDimitry Andric 3542e8d8bef9SDimitry Andric Register LegalizerHelper::getVectorElementPointer(Register VecPtr, LLT VecTy, 3543e8d8bef9SDimitry Andric Register Index) { 3544e8d8bef9SDimitry Andric LLT EltTy = VecTy.getElementType(); 3545e8d8bef9SDimitry Andric 3546e8d8bef9SDimitry Andric // Calculate the element offset and add it to the pointer. 3547e8d8bef9SDimitry Andric unsigned EltSize = EltTy.getSizeInBits() / 8; // FIXME: should be ABI size. 3548e8d8bef9SDimitry Andric assert(EltSize * 8 == EltTy.getSizeInBits() && 3549e8d8bef9SDimitry Andric "Converting bits to bytes lost precision"); 3550e8d8bef9SDimitry Andric 3551e8d8bef9SDimitry Andric Index = clampDynamicVectorIndex(MIRBuilder, Index, VecTy); 3552e8d8bef9SDimitry Andric 3553e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Index); 3554e8d8bef9SDimitry Andric auto Mul = MIRBuilder.buildMul(IdxTy, Index, 3555e8d8bef9SDimitry Andric MIRBuilder.buildConstant(IdxTy, EltSize)); 3556e8d8bef9SDimitry Andric 3557e8d8bef9SDimitry Andric LLT PtrTy = MRI.getType(VecPtr); 3558e8d8bef9SDimitry Andric return MIRBuilder.buildPtrAdd(PtrTy, VecPtr, Mul).getReg(0); 35590b57cec5SDimitry Andric } 35600b57cec5SDimitry Andric 35610b57cec5SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorImplicitDef( 35620b57cec5SDimitry Andric MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy) { 35630b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 3564e8d8bef9SDimitry Andric LLT DstTy = MRI.getType(DstReg); 3565e8d8bef9SDimitry Andric LLT LCMTy = getLCMType(DstTy, NarrowTy); 35660b57cec5SDimitry Andric 3567e8d8bef9SDimitry Andric unsigned NumParts = LCMTy.getSizeInBits() / NarrowTy.getSizeInBits(); 35680b57cec5SDimitry Andric 3569e8d8bef9SDimitry Andric auto NewUndef = MIRBuilder.buildUndef(NarrowTy); 3570e8d8bef9SDimitry Andric SmallVector<Register, 8> Parts(NumParts, NewUndef.getReg(0)); 35710b57cec5SDimitry Andric 3572e8d8bef9SDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, Parts); 35730b57cec5SDimitry Andric MI.eraseFromParent(); 35740b57cec5SDimitry Andric return Legalized; 35750b57cec5SDimitry Andric } 35760b57cec5SDimitry Andric 35770b57cec5SDimitry Andric // Handle splitting vector operations which need to have the same number of 35780b57cec5SDimitry Andric // elements in each type index, but each type index may have a different element 35790b57cec5SDimitry Andric // type. 35800b57cec5SDimitry Andric // 35810b57cec5SDimitry Andric // e.g. <4 x s64> = G_SHL <4 x s64>, <4 x s32> -> 35820b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 35830b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 35840b57cec5SDimitry Andric // 35850b57cec5SDimitry Andric // Also handles some irregular breakdown cases, e.g. 35860b57cec5SDimitry Andric // e.g. <3 x s64> = G_SHL <3 x s64>, <3 x s32> -> 35870b57cec5SDimitry Andric // <2 x s64> = G_SHL <2 x s64>, <2 x s32> 35880b57cec5SDimitry Andric // s64 = G_SHL s64, s32 35890b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 35900b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorMultiEltType( 35910b57cec5SDimitry Andric MachineInstr &MI, unsigned TypeIdx, LLT NarrowTyArg) { 35920b57cec5SDimitry Andric if (TypeIdx != 0) 35930b57cec5SDimitry Andric return UnableToLegalize; 35940b57cec5SDimitry Andric 35950b57cec5SDimitry Andric const LLT NarrowTy0 = NarrowTyArg; 35960b57cec5SDimitry Andric const Register DstReg = MI.getOperand(0).getReg(); 35970b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 35980b57cec5SDimitry Andric LLT LeftoverTy0; 35990b57cec5SDimitry Andric 36000b57cec5SDimitry Andric // All of the operands need to have the same number of elements, so if we can 36010b57cec5SDimitry Andric // determine a type breakdown for the result type, we can for all of the 36020b57cec5SDimitry Andric // source types. 36030b57cec5SDimitry Andric int NumParts = getNarrowTypeBreakDown(DstTy, NarrowTy0, LeftoverTy0).first; 36040b57cec5SDimitry Andric if (NumParts < 0) 36050b57cec5SDimitry Andric return UnableToLegalize; 36060b57cec5SDimitry Andric 36070b57cec5SDimitry Andric SmallVector<MachineInstrBuilder, 4> NewInsts; 36080b57cec5SDimitry Andric 36090b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, LeftoverDstRegs; 36100b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs, LeftoverRegs; 36110b57cec5SDimitry Andric 36120b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { 36130b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 36140b57cec5SDimitry Andric LLT SrcTyI = MRI.getType(SrcReg); 3615fe6060f1SDimitry Andric const auto NewEC = NarrowTy0.isVector() ? NarrowTy0.getElementCount() 3616fe6060f1SDimitry Andric : ElementCount::getFixed(1); 3617fe6060f1SDimitry Andric LLT NarrowTyI = LLT::scalarOrVector(NewEC, SrcTyI.getScalarType()); 36180b57cec5SDimitry Andric LLT LeftoverTyI; 36190b57cec5SDimitry Andric 36200b57cec5SDimitry Andric // Split this operand into the requested typed registers, and any leftover 36210b57cec5SDimitry Andric // required to reproduce the original type. 36220b57cec5SDimitry Andric if (!extractParts(SrcReg, SrcTyI, NarrowTyI, LeftoverTyI, PartRegs, 36230b57cec5SDimitry Andric LeftoverRegs)) 36240b57cec5SDimitry Andric return UnableToLegalize; 36250b57cec5SDimitry Andric 36260b57cec5SDimitry Andric if (I == 1) { 36270b57cec5SDimitry Andric // For the first operand, create an instruction for each part and setup 36280b57cec5SDimitry Andric // the result. 36290b57cec5SDimitry Andric for (Register PartReg : PartRegs) { 36300b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(NarrowTy0); 36310b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) 36320b57cec5SDimitry Andric .addDef(PartDstReg) 36330b57cec5SDimitry Andric .addUse(PartReg)); 36340b57cec5SDimitry Andric DstRegs.push_back(PartDstReg); 36350b57cec5SDimitry Andric } 36360b57cec5SDimitry Andric 36370b57cec5SDimitry Andric for (Register LeftoverReg : LeftoverRegs) { 36380b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(LeftoverTy0); 36390b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) 36400b57cec5SDimitry Andric .addDef(PartDstReg) 36410b57cec5SDimitry Andric .addUse(LeftoverReg)); 36420b57cec5SDimitry Andric LeftoverDstRegs.push_back(PartDstReg); 36430b57cec5SDimitry Andric } 36440b57cec5SDimitry Andric } else { 36450b57cec5SDimitry Andric assert(NewInsts.size() == PartRegs.size() + LeftoverRegs.size()); 36460b57cec5SDimitry Andric 36470b57cec5SDimitry Andric // Add the newly created operand splits to the existing instructions. The 36480b57cec5SDimitry Andric // odd-sized pieces are ordered after the requested NarrowTyArg sized 36490b57cec5SDimitry Andric // pieces. 36500b57cec5SDimitry Andric unsigned InstCount = 0; 36510b57cec5SDimitry Andric for (unsigned J = 0, JE = PartRegs.size(); J != JE; ++J) 36520b57cec5SDimitry Andric NewInsts[InstCount++].addUse(PartRegs[J]); 36530b57cec5SDimitry Andric for (unsigned J = 0, JE = LeftoverRegs.size(); J != JE; ++J) 36540b57cec5SDimitry Andric NewInsts[InstCount++].addUse(LeftoverRegs[J]); 36550b57cec5SDimitry Andric } 36560b57cec5SDimitry Andric 36570b57cec5SDimitry Andric PartRegs.clear(); 36580b57cec5SDimitry Andric LeftoverRegs.clear(); 36590b57cec5SDimitry Andric } 36600b57cec5SDimitry Andric 36610b57cec5SDimitry Andric // Insert the newly built operations and rebuild the result register. 36620b57cec5SDimitry Andric for (auto &MIB : NewInsts) 36630b57cec5SDimitry Andric MIRBuilder.insertInstr(MIB); 36640b57cec5SDimitry Andric 36650b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy0, DstRegs, LeftoverTy0, LeftoverDstRegs); 36660b57cec5SDimitry Andric 36670b57cec5SDimitry Andric MI.eraseFromParent(); 36680b57cec5SDimitry Andric return Legalized; 36690b57cec5SDimitry Andric } 36700b57cec5SDimitry Andric 36710b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 36720b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCasts(MachineInstr &MI, unsigned TypeIdx, 36730b57cec5SDimitry Andric LLT NarrowTy) { 36740b57cec5SDimitry Andric if (TypeIdx != 0) 36750b57cec5SDimitry Andric return UnableToLegalize; 36760b57cec5SDimitry Andric 36770b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 36780b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 36790b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 36800b57cec5SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 36810b57cec5SDimitry Andric 36820b57cec5SDimitry Andric LLT NarrowTy0 = NarrowTy; 36830b57cec5SDimitry Andric LLT NarrowTy1; 36840b57cec5SDimitry Andric unsigned NumParts; 36850b57cec5SDimitry Andric 36860b57cec5SDimitry Andric if (NarrowTy.isVector()) { 36870b57cec5SDimitry Andric // Uneven breakdown not handled. 36880b57cec5SDimitry Andric NumParts = DstTy.getNumElements() / NarrowTy.getNumElements(); 36890b57cec5SDimitry Andric if (NumParts * NarrowTy.getNumElements() != DstTy.getNumElements()) 36900b57cec5SDimitry Andric return UnableToLegalize; 36910b57cec5SDimitry Andric 3692fe6060f1SDimitry Andric NarrowTy1 = LLT::vector(NarrowTy.getElementCount(), SrcTy.getElementType()); 36930b57cec5SDimitry Andric } else { 36940b57cec5SDimitry Andric NumParts = DstTy.getNumElements(); 36950b57cec5SDimitry Andric NarrowTy1 = SrcTy.getElementType(); 36960b57cec5SDimitry Andric } 36970b57cec5SDimitry Andric 36980b57cec5SDimitry Andric SmallVector<Register, 4> SrcRegs, DstRegs; 36990b57cec5SDimitry Andric extractParts(SrcReg, NarrowTy1, NumParts, SrcRegs); 37000b57cec5SDimitry Andric 37010b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) { 37020b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 37035ffd83dbSDimitry Andric MachineInstr *NewInst = 37045ffd83dbSDimitry Andric MIRBuilder.buildInstr(MI.getOpcode(), {DstReg}, {SrcRegs[I]}); 37050b57cec5SDimitry Andric 37060b57cec5SDimitry Andric NewInst->setFlags(MI.getFlags()); 37070b57cec5SDimitry Andric DstRegs.push_back(DstReg); 37080b57cec5SDimitry Andric } 37090b57cec5SDimitry Andric 37100b57cec5SDimitry Andric if (NarrowTy.isVector()) 37110b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 37120b57cec5SDimitry Andric else 37130b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 37140b57cec5SDimitry Andric 37150b57cec5SDimitry Andric MI.eraseFromParent(); 37160b57cec5SDimitry Andric return Legalized; 37170b57cec5SDimitry Andric } 37180b57cec5SDimitry Andric 37190b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 37200b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorCmp(MachineInstr &MI, unsigned TypeIdx, 37210b57cec5SDimitry Andric LLT NarrowTy) { 37220b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 37230b57cec5SDimitry Andric Register Src0Reg = MI.getOperand(2).getReg(); 37240b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 37250b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src0Reg); 37260b57cec5SDimitry Andric 37270b57cec5SDimitry Andric unsigned NumParts; 37280b57cec5SDimitry Andric LLT NarrowTy0, NarrowTy1; 37290b57cec5SDimitry Andric 37300b57cec5SDimitry Andric if (TypeIdx == 0) { 37310b57cec5SDimitry Andric unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; 37320b57cec5SDimitry Andric unsigned OldElts = DstTy.getNumElements(); 37330b57cec5SDimitry Andric 37340b57cec5SDimitry Andric NarrowTy0 = NarrowTy; 37350b57cec5SDimitry Andric NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : DstTy.getNumElements(); 3736fe6060f1SDimitry Andric NarrowTy1 = NarrowTy.isVector() ? LLT::vector(NarrowTy.getElementCount(), 3737fe6060f1SDimitry Andric SrcTy.getScalarSizeInBits()) 3738fe6060f1SDimitry Andric : SrcTy.getElementType(); 37390b57cec5SDimitry Andric 37400b57cec5SDimitry Andric } else { 37410b57cec5SDimitry Andric unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; 37420b57cec5SDimitry Andric unsigned OldElts = SrcTy.getNumElements(); 37430b57cec5SDimitry Andric 37440b57cec5SDimitry Andric NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : 37450b57cec5SDimitry Andric NarrowTy.getNumElements(); 3746fe6060f1SDimitry Andric NarrowTy0 = 3747fe6060f1SDimitry Andric LLT::vector(NarrowTy.getElementCount(), DstTy.getScalarSizeInBits()); 37480b57cec5SDimitry Andric NarrowTy1 = NarrowTy; 37490b57cec5SDimitry Andric } 37500b57cec5SDimitry Andric 37510b57cec5SDimitry Andric // FIXME: Don't know how to handle the situation where the small vectors 37520b57cec5SDimitry Andric // aren't all the same size yet. 37530b57cec5SDimitry Andric if (NarrowTy1.isVector() && 37540b57cec5SDimitry Andric NarrowTy1.getNumElements() * NumParts != DstTy.getNumElements()) 37550b57cec5SDimitry Andric return UnableToLegalize; 37560b57cec5SDimitry Andric 37570b57cec5SDimitry Andric CmpInst::Predicate Pred 37580b57cec5SDimitry Andric = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate()); 37590b57cec5SDimitry Andric 37600b57cec5SDimitry Andric SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs; 37610b57cec5SDimitry Andric extractParts(MI.getOperand(2).getReg(), NarrowTy1, NumParts, Src1Regs); 37620b57cec5SDimitry Andric extractParts(MI.getOperand(3).getReg(), NarrowTy1, NumParts, Src2Regs); 37630b57cec5SDimitry Andric 37640b57cec5SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) { 37650b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 37660b57cec5SDimitry Andric DstRegs.push_back(DstReg); 37670b57cec5SDimitry Andric 37680b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_ICMP) 37690b57cec5SDimitry Andric MIRBuilder.buildICmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); 37700b57cec5SDimitry Andric else { 37710b57cec5SDimitry Andric MachineInstr *NewCmp 37720b57cec5SDimitry Andric = MIRBuilder.buildFCmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); 37730b57cec5SDimitry Andric NewCmp->setFlags(MI.getFlags()); 37740b57cec5SDimitry Andric } 37750b57cec5SDimitry Andric } 37760b57cec5SDimitry Andric 37770b57cec5SDimitry Andric if (NarrowTy1.isVector()) 37780b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 37790b57cec5SDimitry Andric else 37800b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 37810b57cec5SDimitry Andric 37820b57cec5SDimitry Andric MI.eraseFromParent(); 37830b57cec5SDimitry Andric return Legalized; 37840b57cec5SDimitry Andric } 37850b57cec5SDimitry Andric 37860b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 37870b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorSelect(MachineInstr &MI, unsigned TypeIdx, 37880b57cec5SDimitry Andric LLT NarrowTy) { 37890b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 37900b57cec5SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 37910b57cec5SDimitry Andric 37920b57cec5SDimitry Andric unsigned NumParts = 0; 37930b57cec5SDimitry Andric LLT NarrowTy0, NarrowTy1; 37940b57cec5SDimitry Andric 37950b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 37960b57cec5SDimitry Andric LLT CondTy = MRI.getType(CondReg); 37970b57cec5SDimitry Andric unsigned Size = DstTy.getSizeInBits(); 37980b57cec5SDimitry Andric 37990b57cec5SDimitry Andric assert(TypeIdx == 0 || CondTy.isVector()); 38000b57cec5SDimitry Andric 38010b57cec5SDimitry Andric if (TypeIdx == 0) { 38020b57cec5SDimitry Andric NarrowTy0 = NarrowTy; 38030b57cec5SDimitry Andric NarrowTy1 = CondTy; 38040b57cec5SDimitry Andric 38050b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy0.getSizeInBits(); 38060b57cec5SDimitry Andric // FIXME: Don't know how to handle the situation where the small vectors 38070b57cec5SDimitry Andric // aren't all the same size yet. 38080b57cec5SDimitry Andric if (Size % NarrowSize != 0) 38090b57cec5SDimitry Andric return UnableToLegalize; 38100b57cec5SDimitry Andric 38110b57cec5SDimitry Andric NumParts = Size / NarrowSize; 38120b57cec5SDimitry Andric 38130b57cec5SDimitry Andric // Need to break down the condition type 38140b57cec5SDimitry Andric if (CondTy.isVector()) { 38150b57cec5SDimitry Andric if (CondTy.getNumElements() == NumParts) 38160b57cec5SDimitry Andric NarrowTy1 = CondTy.getElementType(); 38170b57cec5SDimitry Andric else 3818fe6060f1SDimitry Andric NarrowTy1 = 3819fe6060f1SDimitry Andric LLT::vector(CondTy.getElementCount().divideCoefficientBy(NumParts), 38200b57cec5SDimitry Andric CondTy.getScalarSizeInBits()); 38210b57cec5SDimitry Andric } 38220b57cec5SDimitry Andric } else { 38230b57cec5SDimitry Andric NumParts = CondTy.getNumElements(); 38240b57cec5SDimitry Andric if (NarrowTy.isVector()) { 38250b57cec5SDimitry Andric // TODO: Handle uneven breakdown. 38260b57cec5SDimitry Andric if (NumParts * NarrowTy.getNumElements() != CondTy.getNumElements()) 38270b57cec5SDimitry Andric return UnableToLegalize; 38280b57cec5SDimitry Andric 38290b57cec5SDimitry Andric return UnableToLegalize; 38300b57cec5SDimitry Andric } else { 38310b57cec5SDimitry Andric NarrowTy0 = DstTy.getElementType(); 38320b57cec5SDimitry Andric NarrowTy1 = NarrowTy; 38330b57cec5SDimitry Andric } 38340b57cec5SDimitry Andric } 38350b57cec5SDimitry Andric 38360b57cec5SDimitry Andric SmallVector<Register, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs; 38370b57cec5SDimitry Andric if (CondTy.isVector()) 38380b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy1, NumParts, Src0Regs); 38390b57cec5SDimitry Andric 38400b57cec5SDimitry Andric extractParts(MI.getOperand(2).getReg(), NarrowTy0, NumParts, Src1Regs); 38410b57cec5SDimitry Andric extractParts(MI.getOperand(3).getReg(), NarrowTy0, NumParts, Src2Regs); 38420b57cec5SDimitry Andric 38430b57cec5SDimitry Andric for (unsigned i = 0; i < NumParts; ++i) { 38440b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy0); 38450b57cec5SDimitry Andric MIRBuilder.buildSelect(DstReg, CondTy.isVector() ? Src0Regs[i] : CondReg, 38460b57cec5SDimitry Andric Src1Regs[i], Src2Regs[i]); 38470b57cec5SDimitry Andric DstRegs.push_back(DstReg); 38480b57cec5SDimitry Andric } 38490b57cec5SDimitry Andric 38500b57cec5SDimitry Andric if (NarrowTy0.isVector()) 38510b57cec5SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, DstRegs); 38520b57cec5SDimitry Andric else 38530b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 38540b57cec5SDimitry Andric 38550b57cec5SDimitry Andric MI.eraseFromParent(); 38560b57cec5SDimitry Andric return Legalized; 38570b57cec5SDimitry Andric } 38580b57cec5SDimitry Andric 38590b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 38600b57cec5SDimitry Andric LegalizerHelper::fewerElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx, 38610b57cec5SDimitry Andric LLT NarrowTy) { 38620b57cec5SDimitry Andric const Register DstReg = MI.getOperand(0).getReg(); 38630b57cec5SDimitry Andric LLT PhiTy = MRI.getType(DstReg); 38640b57cec5SDimitry Andric LLT LeftoverTy; 38650b57cec5SDimitry Andric 38660b57cec5SDimitry Andric // All of the operands need to have the same number of elements, so if we can 38670b57cec5SDimitry Andric // determine a type breakdown for the result type, we can for all of the 38680b57cec5SDimitry Andric // source types. 38690b57cec5SDimitry Andric int NumParts, NumLeftover; 38700b57cec5SDimitry Andric std::tie(NumParts, NumLeftover) 38710b57cec5SDimitry Andric = getNarrowTypeBreakDown(PhiTy, NarrowTy, LeftoverTy); 38720b57cec5SDimitry Andric if (NumParts < 0) 38730b57cec5SDimitry Andric return UnableToLegalize; 38740b57cec5SDimitry Andric 38750b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, LeftoverDstRegs; 38760b57cec5SDimitry Andric SmallVector<MachineInstrBuilder, 4> NewInsts; 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric const int TotalNumParts = NumParts + NumLeftover; 38790b57cec5SDimitry Andric 38800b57cec5SDimitry Andric // Insert the new phis in the result block first. 38810b57cec5SDimitry Andric for (int I = 0; I != TotalNumParts; ++I) { 38820b57cec5SDimitry Andric LLT Ty = I < NumParts ? NarrowTy : LeftoverTy; 38830b57cec5SDimitry Andric Register PartDstReg = MRI.createGenericVirtualRegister(Ty); 38840b57cec5SDimitry Andric NewInsts.push_back(MIRBuilder.buildInstr(TargetOpcode::G_PHI) 38850b57cec5SDimitry Andric .addDef(PartDstReg)); 38860b57cec5SDimitry Andric if (I < NumParts) 38870b57cec5SDimitry Andric DstRegs.push_back(PartDstReg); 38880b57cec5SDimitry Andric else 38890b57cec5SDimitry Andric LeftoverDstRegs.push_back(PartDstReg); 38900b57cec5SDimitry Andric } 38910b57cec5SDimitry Andric 38920b57cec5SDimitry Andric MachineBasicBlock *MBB = MI.getParent(); 38930b57cec5SDimitry Andric MIRBuilder.setInsertPt(*MBB, MBB->getFirstNonPHI()); 38940b57cec5SDimitry Andric insertParts(DstReg, PhiTy, NarrowTy, DstRegs, LeftoverTy, LeftoverDstRegs); 38950b57cec5SDimitry Andric 38960b57cec5SDimitry Andric SmallVector<Register, 4> PartRegs, LeftoverRegs; 38970b57cec5SDimitry Andric 38980b57cec5SDimitry Andric // Insert code to extract the incoming values in each predecessor block. 38990b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 39000b57cec5SDimitry Andric PartRegs.clear(); 39010b57cec5SDimitry Andric LeftoverRegs.clear(); 39020b57cec5SDimitry Andric 39030b57cec5SDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 39040b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 39050b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 39060b57cec5SDimitry Andric 39070b57cec5SDimitry Andric LLT Unused; 39080b57cec5SDimitry Andric if (!extractParts(SrcReg, PhiTy, NarrowTy, Unused, PartRegs, 39090b57cec5SDimitry Andric LeftoverRegs)) 39100b57cec5SDimitry Andric return UnableToLegalize; 39110b57cec5SDimitry Andric 39120b57cec5SDimitry Andric // Add the newly created operand splits to the existing instructions. The 39130b57cec5SDimitry Andric // odd-sized pieces are ordered after the requested NarrowTyArg sized 39140b57cec5SDimitry Andric // pieces. 39150b57cec5SDimitry Andric for (int J = 0; J != TotalNumParts; ++J) { 39160b57cec5SDimitry Andric MachineInstrBuilder MIB = NewInsts[J]; 39170b57cec5SDimitry Andric MIB.addUse(J < NumParts ? PartRegs[J] : LeftoverRegs[J - NumParts]); 39180b57cec5SDimitry Andric MIB.addMBB(&OpMBB); 39190b57cec5SDimitry Andric } 39200b57cec5SDimitry Andric } 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric MI.eraseFromParent(); 39230b57cec5SDimitry Andric return Legalized; 39240b57cec5SDimitry Andric } 39250b57cec5SDimitry Andric 39260b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 39278bcb0991SDimitry Andric LegalizerHelper::fewerElementsVectorUnmergeValues(MachineInstr &MI, 39288bcb0991SDimitry Andric unsigned TypeIdx, 39298bcb0991SDimitry Andric LLT NarrowTy) { 39308bcb0991SDimitry Andric if (TypeIdx != 1) 39318bcb0991SDimitry Andric return UnableToLegalize; 39328bcb0991SDimitry Andric 39338bcb0991SDimitry Andric const int NumDst = MI.getNumOperands() - 1; 39348bcb0991SDimitry Andric const Register SrcReg = MI.getOperand(NumDst).getReg(); 39358bcb0991SDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 39368bcb0991SDimitry Andric 39378bcb0991SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 39388bcb0991SDimitry Andric 39398bcb0991SDimitry Andric // TODO: Create sequence of extracts. 39408bcb0991SDimitry Andric if (DstTy == NarrowTy) 39418bcb0991SDimitry Andric return UnableToLegalize; 39428bcb0991SDimitry Andric 39438bcb0991SDimitry Andric LLT GCDTy = getGCDType(SrcTy, NarrowTy); 39448bcb0991SDimitry Andric if (DstTy == GCDTy) { 39458bcb0991SDimitry Andric // This would just be a copy of the same unmerge. 39468bcb0991SDimitry Andric // TODO: Create extracts, pad with undef and create intermediate merges. 39478bcb0991SDimitry Andric return UnableToLegalize; 39488bcb0991SDimitry Andric } 39498bcb0991SDimitry Andric 39508bcb0991SDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(GCDTy, SrcReg); 39518bcb0991SDimitry Andric const int NumUnmerge = Unmerge->getNumOperands() - 1; 39528bcb0991SDimitry Andric const int PartsPerUnmerge = NumDst / NumUnmerge; 39538bcb0991SDimitry Andric 39548bcb0991SDimitry Andric for (int I = 0; I != NumUnmerge; ++I) { 39558bcb0991SDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 39568bcb0991SDimitry Andric 39578bcb0991SDimitry Andric for (int J = 0; J != PartsPerUnmerge; ++J) 39588bcb0991SDimitry Andric MIB.addDef(MI.getOperand(I * PartsPerUnmerge + J).getReg()); 39598bcb0991SDimitry Andric MIB.addUse(Unmerge.getReg(I)); 39608bcb0991SDimitry Andric } 39618bcb0991SDimitry Andric 39628bcb0991SDimitry Andric MI.eraseFromParent(); 39638bcb0991SDimitry Andric return Legalized; 39648bcb0991SDimitry Andric } 39658bcb0991SDimitry Andric 3966fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 3967fe6060f1SDimitry Andric LegalizerHelper::fewerElementsVectorMulo(MachineInstr &MI, unsigned TypeIdx, 3968fe6060f1SDimitry Andric LLT NarrowTy) { 3969fe6060f1SDimitry Andric Register Result = MI.getOperand(0).getReg(); 3970fe6060f1SDimitry Andric Register Overflow = MI.getOperand(1).getReg(); 3971fe6060f1SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 3972fe6060f1SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 3973fe6060f1SDimitry Andric 3974fe6060f1SDimitry Andric LLT SrcTy = MRI.getType(LHS); 3975fe6060f1SDimitry Andric if (!SrcTy.isVector()) 3976fe6060f1SDimitry Andric return UnableToLegalize; 3977fe6060f1SDimitry Andric 3978fe6060f1SDimitry Andric LLT ElementType = SrcTy.getElementType(); 3979fe6060f1SDimitry Andric LLT OverflowElementTy = MRI.getType(Overflow).getElementType(); 3980fe6060f1SDimitry Andric const ElementCount NumResult = SrcTy.getElementCount(); 3981fe6060f1SDimitry Andric LLT GCDTy = getGCDType(SrcTy, NarrowTy); 3982fe6060f1SDimitry Andric 3983fe6060f1SDimitry Andric // Unmerge the operands to smaller parts of GCD type. 3984fe6060f1SDimitry Andric auto UnmergeLHS = MIRBuilder.buildUnmerge(GCDTy, LHS); 3985fe6060f1SDimitry Andric auto UnmergeRHS = MIRBuilder.buildUnmerge(GCDTy, RHS); 3986fe6060f1SDimitry Andric 3987fe6060f1SDimitry Andric const int NumOps = UnmergeLHS->getNumOperands() - 1; 3988fe6060f1SDimitry Andric const ElementCount PartsPerUnmerge = NumResult.divideCoefficientBy(NumOps); 3989fe6060f1SDimitry Andric LLT OverflowTy = LLT::scalarOrVector(PartsPerUnmerge, OverflowElementTy); 3990fe6060f1SDimitry Andric LLT ResultTy = LLT::scalarOrVector(PartsPerUnmerge, ElementType); 3991fe6060f1SDimitry Andric 3992fe6060f1SDimitry Andric // Perform the operation over unmerged parts. 3993fe6060f1SDimitry Andric SmallVector<Register, 8> ResultParts; 3994fe6060f1SDimitry Andric SmallVector<Register, 8> OverflowParts; 3995fe6060f1SDimitry Andric for (int I = 0; I != NumOps; ++I) { 3996fe6060f1SDimitry Andric Register Operand1 = UnmergeLHS->getOperand(I).getReg(); 3997fe6060f1SDimitry Andric Register Operand2 = UnmergeRHS->getOperand(I).getReg(); 3998fe6060f1SDimitry Andric auto PartMul = MIRBuilder.buildInstr(MI.getOpcode(), {ResultTy, OverflowTy}, 3999fe6060f1SDimitry Andric {Operand1, Operand2}); 4000fe6060f1SDimitry Andric ResultParts.push_back(PartMul->getOperand(0).getReg()); 4001fe6060f1SDimitry Andric OverflowParts.push_back(PartMul->getOperand(1).getReg()); 4002fe6060f1SDimitry Andric } 4003fe6060f1SDimitry Andric 4004fe6060f1SDimitry Andric LLT ResultLCMTy = buildLCMMergePieces(SrcTy, NarrowTy, GCDTy, ResultParts); 4005fe6060f1SDimitry Andric LLT OverflowLCMTy = 4006fe6060f1SDimitry Andric LLT::scalarOrVector(ResultLCMTy.getElementCount(), OverflowElementTy); 4007fe6060f1SDimitry Andric 4008fe6060f1SDimitry Andric // Recombine the pieces to the original result and overflow registers. 4009fe6060f1SDimitry Andric buildWidenedRemergeToDst(Result, ResultLCMTy, ResultParts); 4010fe6060f1SDimitry Andric buildWidenedRemergeToDst(Overflow, OverflowLCMTy, OverflowParts); 4011fe6060f1SDimitry Andric MI.eraseFromParent(); 4012fe6060f1SDimitry Andric return Legalized; 4013fe6060f1SDimitry Andric } 4014fe6060f1SDimitry Andric 4015e8d8bef9SDimitry Andric // Handle FewerElementsVector a G_BUILD_VECTOR or G_CONCAT_VECTORS that produces 4016e8d8bef9SDimitry Andric // a vector 4017e8d8bef9SDimitry Andric // 4018e8d8bef9SDimitry Andric // Create a G_BUILD_VECTOR or G_CONCAT_VECTORS of NarrowTy pieces, padding with 4019e8d8bef9SDimitry Andric // undef as necessary. 40208bcb0991SDimitry Andric // 40218bcb0991SDimitry Andric // %3:_(<3 x s16>) = G_BUILD_VECTOR %0, %1, %2 40228bcb0991SDimitry Andric // -> <2 x s16> 40238bcb0991SDimitry Andric // 40248bcb0991SDimitry Andric // %4:_(s16) = G_IMPLICIT_DEF 40258bcb0991SDimitry Andric // %5:_(<2 x s16>) = G_BUILD_VECTOR %0, %1 40268bcb0991SDimitry Andric // %6:_(<2 x s16>) = G_BUILD_VECTOR %2, %4 4027e8d8bef9SDimitry Andric // %7:_(<2 x s16>) = G_IMPLICIT_DEF 4028e8d8bef9SDimitry Andric // %8:_(<6 x s16>) = G_CONCAT_VECTORS %5, %6, %7 4029e8d8bef9SDimitry Andric // %3:_(<3 x s16>), %8:_(<3 x s16>) = G_UNMERGE_VALUES %8 4030e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 4031e8d8bef9SDimitry Andric LegalizerHelper::fewerElementsVectorMerge(MachineInstr &MI, unsigned TypeIdx, 4032e8d8bef9SDimitry Andric LLT NarrowTy) { 4033e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4034e8d8bef9SDimitry Andric LLT DstTy = MRI.getType(DstReg); 4035e8d8bef9SDimitry Andric LLT SrcTy = MRI.getType(MI.getOperand(1).getReg()); 4036e8d8bef9SDimitry Andric LLT GCDTy = getGCDType(getGCDType(SrcTy, NarrowTy), DstTy); 40378bcb0991SDimitry Andric 4038e8d8bef9SDimitry Andric // Break into a common type 4039e8d8bef9SDimitry Andric SmallVector<Register, 16> Parts; 4040*4824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) 4041*4824e7fdSDimitry Andric extractGCDType(Parts, GCDTy, MO.getReg()); 4042e8d8bef9SDimitry Andric 4043e8d8bef9SDimitry Andric // Build the requested new merge, padding with undef. 4044e8d8bef9SDimitry Andric LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts, 4045e8d8bef9SDimitry Andric TargetOpcode::G_ANYEXT); 4046e8d8bef9SDimitry Andric 4047e8d8bef9SDimitry Andric // Pack into the original result register. 4048e8d8bef9SDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, Parts); 4049e8d8bef9SDimitry Andric 4050e8d8bef9SDimitry Andric MI.eraseFromParent(); 4051e8d8bef9SDimitry Andric return Legalized; 40528bcb0991SDimitry Andric } 40538bcb0991SDimitry Andric 4054e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 4055e8d8bef9SDimitry Andric LegalizerHelper::fewerElementsVectorExtractInsertVectorElt(MachineInstr &MI, 4056e8d8bef9SDimitry Andric unsigned TypeIdx, 4057e8d8bef9SDimitry Andric LLT NarrowVecTy) { 4058e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4059e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 4060e8d8bef9SDimitry Andric Register InsertVal; 4061e8d8bef9SDimitry Andric bool IsInsert = MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT; 4062e8d8bef9SDimitry Andric 4063e8d8bef9SDimitry Andric assert((IsInsert ? TypeIdx == 0 : TypeIdx == 1) && "not a vector type index"); 4064e8d8bef9SDimitry Andric if (IsInsert) 4065e8d8bef9SDimitry Andric InsertVal = MI.getOperand(2).getReg(); 4066e8d8bef9SDimitry Andric 4067e8d8bef9SDimitry Andric Register Idx = MI.getOperand(MI.getNumOperands() - 1).getReg(); 4068e8d8bef9SDimitry Andric 4069e8d8bef9SDimitry Andric // TODO: Handle total scalarization case. 4070e8d8bef9SDimitry Andric if (!NarrowVecTy.isVector()) 4071e8d8bef9SDimitry Andric return UnableToLegalize; 4072e8d8bef9SDimitry Andric 4073e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(SrcVec); 4074e8d8bef9SDimitry Andric 4075e8d8bef9SDimitry Andric // If the index is a constant, we can really break this down as you would 4076e8d8bef9SDimitry Andric // expect, and index into the target size pieces. 4077e8d8bef9SDimitry Andric int64_t IdxVal; 4078349cc55cSDimitry Andric auto MaybeCst = getIConstantVRegValWithLookThrough(Idx, MRI); 4079fe6060f1SDimitry Andric if (MaybeCst) { 4080fe6060f1SDimitry Andric IdxVal = MaybeCst->Value.getSExtValue(); 4081e8d8bef9SDimitry Andric // Avoid out of bounds indexing the pieces. 4082e8d8bef9SDimitry Andric if (IdxVal >= VecTy.getNumElements()) { 4083e8d8bef9SDimitry Andric MIRBuilder.buildUndef(DstReg); 4084e8d8bef9SDimitry Andric MI.eraseFromParent(); 4085e8d8bef9SDimitry Andric return Legalized; 40868bcb0991SDimitry Andric } 40878bcb0991SDimitry Andric 4088e8d8bef9SDimitry Andric SmallVector<Register, 8> VecParts; 4089e8d8bef9SDimitry Andric LLT GCDTy = extractGCDType(VecParts, VecTy, NarrowVecTy, SrcVec); 4090e8d8bef9SDimitry Andric 4091e8d8bef9SDimitry Andric // Build a sequence of NarrowTy pieces in VecParts for this operand. 4092e8d8bef9SDimitry Andric LLT LCMTy = buildLCMMergePieces(VecTy, NarrowVecTy, GCDTy, VecParts, 4093e8d8bef9SDimitry Andric TargetOpcode::G_ANYEXT); 4094e8d8bef9SDimitry Andric 4095e8d8bef9SDimitry Andric unsigned NewNumElts = NarrowVecTy.getNumElements(); 4096e8d8bef9SDimitry Andric 4097e8d8bef9SDimitry Andric LLT IdxTy = MRI.getType(Idx); 4098e8d8bef9SDimitry Andric int64_t PartIdx = IdxVal / NewNumElts; 4099e8d8bef9SDimitry Andric auto NewIdx = 4100e8d8bef9SDimitry Andric MIRBuilder.buildConstant(IdxTy, IdxVal - NewNumElts * PartIdx); 4101e8d8bef9SDimitry Andric 4102e8d8bef9SDimitry Andric if (IsInsert) { 4103e8d8bef9SDimitry Andric LLT PartTy = MRI.getType(VecParts[PartIdx]); 4104e8d8bef9SDimitry Andric 4105e8d8bef9SDimitry Andric // Use the adjusted index to insert into one of the subvectors. 4106e8d8bef9SDimitry Andric auto InsertPart = MIRBuilder.buildInsertVectorElement( 4107e8d8bef9SDimitry Andric PartTy, VecParts[PartIdx], InsertVal, NewIdx); 4108e8d8bef9SDimitry Andric VecParts[PartIdx] = InsertPart.getReg(0); 4109e8d8bef9SDimitry Andric 4110e8d8bef9SDimitry Andric // Recombine the inserted subvector with the others to reform the result 4111e8d8bef9SDimitry Andric // vector. 4112e8d8bef9SDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, VecParts); 4113e8d8bef9SDimitry Andric } else { 4114e8d8bef9SDimitry Andric MIRBuilder.buildExtractVectorElement(DstReg, VecParts[PartIdx], NewIdx); 41158bcb0991SDimitry Andric } 41168bcb0991SDimitry Andric 41178bcb0991SDimitry Andric MI.eraseFromParent(); 41188bcb0991SDimitry Andric return Legalized; 41198bcb0991SDimitry Andric } 41208bcb0991SDimitry Andric 4121e8d8bef9SDimitry Andric // With a variable index, we can't perform the operation in a smaller type, so 4122e8d8bef9SDimitry Andric // we're forced to expand this. 4123e8d8bef9SDimitry Andric // 4124e8d8bef9SDimitry Andric // TODO: We could emit a chain of compare/select to figure out which piece to 4125e8d8bef9SDimitry Andric // index. 4126e8d8bef9SDimitry Andric return lowerExtractInsertVectorElt(MI); 4127e8d8bef9SDimitry Andric } 4128e8d8bef9SDimitry Andric 41298bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 4130fe6060f1SDimitry Andric LegalizerHelper::reduceLoadStoreWidth(GLoadStore &LdStMI, unsigned TypeIdx, 41310b57cec5SDimitry Andric LLT NarrowTy) { 41320b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 41330b57cec5SDimitry Andric if (TypeIdx != 0) 41340b57cec5SDimitry Andric return UnableToLegalize; 41350b57cec5SDimitry Andric 41360b57cec5SDimitry Andric // This implementation doesn't work for atomics. Give up instead of doing 41370b57cec5SDimitry Andric // something invalid. 4138fe6060f1SDimitry Andric if (LdStMI.isAtomic()) 41390b57cec5SDimitry Andric return UnableToLegalize; 41400b57cec5SDimitry Andric 4141fe6060f1SDimitry Andric bool IsLoad = isa<GLoad>(LdStMI); 4142fe6060f1SDimitry Andric Register ValReg = LdStMI.getReg(0); 4143fe6060f1SDimitry Andric Register AddrReg = LdStMI.getPointerReg(); 41440b57cec5SDimitry Andric LLT ValTy = MRI.getType(ValReg); 41450b57cec5SDimitry Andric 41465ffd83dbSDimitry Andric // FIXME: Do we need a distinct NarrowMemory legalize action? 4147fe6060f1SDimitry Andric if (ValTy.getSizeInBits() != 8 * LdStMI.getMemSize()) { 41485ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Can't narrow extload/truncstore\n"); 41495ffd83dbSDimitry Andric return UnableToLegalize; 41505ffd83dbSDimitry Andric } 41515ffd83dbSDimitry Andric 41520b57cec5SDimitry Andric int NumParts = -1; 41530b57cec5SDimitry Andric int NumLeftover = -1; 41540b57cec5SDimitry Andric LLT LeftoverTy; 41550b57cec5SDimitry Andric SmallVector<Register, 8> NarrowRegs, NarrowLeftoverRegs; 41560b57cec5SDimitry Andric if (IsLoad) { 41570b57cec5SDimitry Andric std::tie(NumParts, NumLeftover) = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy); 41580b57cec5SDimitry Andric } else { 41590b57cec5SDimitry Andric if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs, 41600b57cec5SDimitry Andric NarrowLeftoverRegs)) { 41610b57cec5SDimitry Andric NumParts = NarrowRegs.size(); 41620b57cec5SDimitry Andric NumLeftover = NarrowLeftoverRegs.size(); 41630b57cec5SDimitry Andric } 41640b57cec5SDimitry Andric } 41650b57cec5SDimitry Andric 41660b57cec5SDimitry Andric if (NumParts == -1) 41670b57cec5SDimitry Andric return UnableToLegalize; 41680b57cec5SDimitry Andric 4169e8d8bef9SDimitry Andric LLT PtrTy = MRI.getType(AddrReg); 4170e8d8bef9SDimitry Andric const LLT OffsetTy = LLT::scalar(PtrTy.getSizeInBits()); 41710b57cec5SDimitry Andric 41720b57cec5SDimitry Andric unsigned TotalSize = ValTy.getSizeInBits(); 41730b57cec5SDimitry Andric 41740b57cec5SDimitry Andric // Split the load/store into PartTy sized pieces starting at Offset. If this 41750b57cec5SDimitry Andric // is a load, return the new registers in ValRegs. For a store, each elements 41760b57cec5SDimitry Andric // of ValRegs should be PartTy. Returns the next offset that needs to be 41770b57cec5SDimitry Andric // handled. 4178fe6060f1SDimitry Andric auto MMO = LdStMI.getMMO(); 41790b57cec5SDimitry Andric auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<Register> &ValRegs, 41800b57cec5SDimitry Andric unsigned Offset) -> unsigned { 41810b57cec5SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 41820b57cec5SDimitry Andric unsigned PartSize = PartTy.getSizeInBits(); 41830b57cec5SDimitry Andric for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize; 41840b57cec5SDimitry Andric Offset += PartSize, ++Idx) { 41850b57cec5SDimitry Andric unsigned ByteOffset = Offset / 8; 41860b57cec5SDimitry Andric Register NewAddrReg; 41870b57cec5SDimitry Andric 4188480093f4SDimitry Andric MIRBuilder.materializePtrAdd(NewAddrReg, AddrReg, OffsetTy, ByteOffset); 41890b57cec5SDimitry Andric 41900b57cec5SDimitry Andric MachineMemOperand *NewMMO = 4191fe6060f1SDimitry Andric MF.getMachineMemOperand(&MMO, ByteOffset, PartTy); 41920b57cec5SDimitry Andric 41930b57cec5SDimitry Andric if (IsLoad) { 41940b57cec5SDimitry Andric Register Dst = MRI.createGenericVirtualRegister(PartTy); 41950b57cec5SDimitry Andric ValRegs.push_back(Dst); 41960b57cec5SDimitry Andric MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO); 41970b57cec5SDimitry Andric } else { 41980b57cec5SDimitry Andric MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO); 41990b57cec5SDimitry Andric } 42000b57cec5SDimitry Andric } 42010b57cec5SDimitry Andric 42020b57cec5SDimitry Andric return Offset; 42030b57cec5SDimitry Andric }; 42040b57cec5SDimitry Andric 42050b57cec5SDimitry Andric unsigned HandledOffset = splitTypePieces(NarrowTy, NarrowRegs, 0); 42060b57cec5SDimitry Andric 42070b57cec5SDimitry Andric // Handle the rest of the register if this isn't an even type breakdown. 42080b57cec5SDimitry Andric if (LeftoverTy.isValid()) 42090b57cec5SDimitry Andric splitTypePieces(LeftoverTy, NarrowLeftoverRegs, HandledOffset); 42100b57cec5SDimitry Andric 42110b57cec5SDimitry Andric if (IsLoad) { 42120b57cec5SDimitry Andric insertParts(ValReg, ValTy, NarrowTy, NarrowRegs, 42130b57cec5SDimitry Andric LeftoverTy, NarrowLeftoverRegs); 42140b57cec5SDimitry Andric } 42150b57cec5SDimitry Andric 4216fe6060f1SDimitry Andric LdStMI.eraseFromParent(); 42170b57cec5SDimitry Andric return Legalized; 42180b57cec5SDimitry Andric } 42190b57cec5SDimitry Andric 42200b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 42215ffd83dbSDimitry Andric LegalizerHelper::reduceOperationWidth(MachineInstr &MI, unsigned int TypeIdx, 42225ffd83dbSDimitry Andric LLT NarrowTy) { 42235ffd83dbSDimitry Andric assert(TypeIdx == 0 && "only one type index expected"); 42245ffd83dbSDimitry Andric 42255ffd83dbSDimitry Andric const unsigned Opc = MI.getOpcode(); 4226fe6060f1SDimitry Andric const int NumDefOps = MI.getNumExplicitDefs(); 4227fe6060f1SDimitry Andric const int NumSrcOps = MI.getNumOperands() - NumDefOps; 42285ffd83dbSDimitry Andric const unsigned Flags = MI.getFlags(); 42295ffd83dbSDimitry Andric const unsigned NarrowSize = NarrowTy.getSizeInBits(); 42305ffd83dbSDimitry Andric const LLT NarrowScalarTy = LLT::scalar(NarrowSize); 42315ffd83dbSDimitry Andric 4232fe6060f1SDimitry Andric assert(MI.getNumOperands() <= 4 && "expected instruction with either 1 " 4233fe6060f1SDimitry Andric "result and 1-3 sources or 2 results and " 4234fe6060f1SDimitry Andric "1-2 sources"); 4235fe6060f1SDimitry Andric 4236fe6060f1SDimitry Andric SmallVector<Register, 2> DstRegs; 4237fe6060f1SDimitry Andric for (int I = 0; I < NumDefOps; ++I) 4238fe6060f1SDimitry Andric DstRegs.push_back(MI.getOperand(I).getReg()); 42395ffd83dbSDimitry Andric 42405ffd83dbSDimitry Andric // First of all check whether we are narrowing (changing the element type) 42415ffd83dbSDimitry Andric // or reducing the vector elements 4242fe6060f1SDimitry Andric const LLT DstTy = MRI.getType(DstRegs[0]); 42435ffd83dbSDimitry Andric const bool IsNarrow = NarrowTy.getScalarType() != DstTy.getScalarType(); 42445ffd83dbSDimitry Andric 42455ffd83dbSDimitry Andric SmallVector<Register, 8> ExtractedRegs[3]; 42465ffd83dbSDimitry Andric SmallVector<Register, 8> Parts; 42475ffd83dbSDimitry Andric 42485ffd83dbSDimitry Andric // Break down all the sources into NarrowTy pieces we can operate on. This may 42495ffd83dbSDimitry Andric // involve creating merges to a wider type, padded with undef. 4250fe6060f1SDimitry Andric for (int I = 0; I != NumSrcOps; ++I) { 4251fe6060f1SDimitry Andric Register SrcReg = MI.getOperand(I + NumDefOps).getReg(); 42525ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 42535ffd83dbSDimitry Andric 42545ffd83dbSDimitry Andric // The type to narrow SrcReg to. For narrowing, this is a smaller scalar. 42555ffd83dbSDimitry Andric // For fewerElements, this is a smaller vector with the same element type. 42565ffd83dbSDimitry Andric LLT OpNarrowTy; 42575ffd83dbSDimitry Andric if (IsNarrow) { 42585ffd83dbSDimitry Andric OpNarrowTy = NarrowScalarTy; 42595ffd83dbSDimitry Andric 42605ffd83dbSDimitry Andric // In case of narrowing, we need to cast vectors to scalars for this to 42615ffd83dbSDimitry Andric // work properly 42625ffd83dbSDimitry Andric // FIXME: Can we do without the bitcast here if we're narrowing? 42635ffd83dbSDimitry Andric if (SrcTy.isVector()) { 42645ffd83dbSDimitry Andric SrcTy = LLT::scalar(SrcTy.getSizeInBits()); 42655ffd83dbSDimitry Andric SrcReg = MIRBuilder.buildBitcast(SrcTy, SrcReg).getReg(0); 42665ffd83dbSDimitry Andric } 42675ffd83dbSDimitry Andric } else { 4268fe6060f1SDimitry Andric auto NarrowEC = NarrowTy.isVector() ? NarrowTy.getElementCount() 4269fe6060f1SDimitry Andric : ElementCount::getFixed(1); 4270fe6060f1SDimitry Andric OpNarrowTy = LLT::scalarOrVector(NarrowEC, SrcTy.getScalarType()); 42715ffd83dbSDimitry Andric } 42725ffd83dbSDimitry Andric 42735ffd83dbSDimitry Andric LLT GCDTy = extractGCDType(ExtractedRegs[I], SrcTy, OpNarrowTy, SrcReg); 42745ffd83dbSDimitry Andric 42755ffd83dbSDimitry Andric // Build a sequence of NarrowTy pieces in ExtractedRegs for this operand. 42765ffd83dbSDimitry Andric buildLCMMergePieces(SrcTy, OpNarrowTy, GCDTy, ExtractedRegs[I], 42775ffd83dbSDimitry Andric TargetOpcode::G_ANYEXT); 42785ffd83dbSDimitry Andric } 42795ffd83dbSDimitry Andric 4280fe6060f1SDimitry Andric SmallVector<Register, 8> ResultRegs[2]; 42815ffd83dbSDimitry Andric 42825ffd83dbSDimitry Andric // Input operands for each sub-instruction. 4283fe6060f1SDimitry Andric SmallVector<SrcOp, 4> InputRegs(NumSrcOps, Register()); 42845ffd83dbSDimitry Andric 42855ffd83dbSDimitry Andric int NumParts = ExtractedRegs[0].size(); 42865ffd83dbSDimitry Andric const unsigned DstSize = DstTy.getSizeInBits(); 42875ffd83dbSDimitry Andric const LLT DstScalarTy = LLT::scalar(DstSize); 42885ffd83dbSDimitry Andric 42895ffd83dbSDimitry Andric // Narrowing needs to use scalar types 42905ffd83dbSDimitry Andric LLT DstLCMTy, NarrowDstTy; 42915ffd83dbSDimitry Andric if (IsNarrow) { 42925ffd83dbSDimitry Andric DstLCMTy = getLCMType(DstScalarTy, NarrowScalarTy); 42935ffd83dbSDimitry Andric NarrowDstTy = NarrowScalarTy; 42945ffd83dbSDimitry Andric } else { 42955ffd83dbSDimitry Andric DstLCMTy = getLCMType(DstTy, NarrowTy); 42965ffd83dbSDimitry Andric NarrowDstTy = NarrowTy; 42975ffd83dbSDimitry Andric } 42985ffd83dbSDimitry Andric 42995ffd83dbSDimitry Andric // We widened the source registers to satisfy merge/unmerge size 43005ffd83dbSDimitry Andric // constraints. We'll have some extra fully undef parts. 43015ffd83dbSDimitry Andric const int NumRealParts = (DstSize + NarrowSize - 1) / NarrowSize; 43025ffd83dbSDimitry Andric 43035ffd83dbSDimitry Andric for (int I = 0; I != NumRealParts; ++I) { 43045ffd83dbSDimitry Andric // Emit this instruction on each of the split pieces. 4305fe6060f1SDimitry Andric for (int J = 0; J != NumSrcOps; ++J) 43065ffd83dbSDimitry Andric InputRegs[J] = ExtractedRegs[J][I]; 43075ffd83dbSDimitry Andric 4308fe6060f1SDimitry Andric MachineInstrBuilder Inst; 4309fe6060f1SDimitry Andric if (NumDefOps == 1) 4310fe6060f1SDimitry Andric Inst = MIRBuilder.buildInstr(Opc, {NarrowDstTy}, InputRegs, Flags); 4311fe6060f1SDimitry Andric else 4312fe6060f1SDimitry Andric Inst = MIRBuilder.buildInstr(Opc, {NarrowDstTy, NarrowDstTy}, InputRegs, 4313fe6060f1SDimitry Andric Flags); 4314fe6060f1SDimitry Andric 4315fe6060f1SDimitry Andric for (int J = 0; J != NumDefOps; ++J) 4316fe6060f1SDimitry Andric ResultRegs[J].push_back(Inst.getReg(J)); 43175ffd83dbSDimitry Andric } 43185ffd83dbSDimitry Andric 43195ffd83dbSDimitry Andric // Fill out the widened result with undef instead of creating instructions 43205ffd83dbSDimitry Andric // with undef inputs. 43215ffd83dbSDimitry Andric int NumUndefParts = NumParts - NumRealParts; 4322fe6060f1SDimitry Andric if (NumUndefParts != 0) { 4323fe6060f1SDimitry Andric Register Undef = MIRBuilder.buildUndef(NarrowDstTy).getReg(0); 4324fe6060f1SDimitry Andric for (int I = 0; I != NumDefOps; ++I) 4325fe6060f1SDimitry Andric ResultRegs[I].append(NumUndefParts, Undef); 4326fe6060f1SDimitry Andric } 43275ffd83dbSDimitry Andric 43285ffd83dbSDimitry Andric // Extract the possibly padded result. Use a scratch register if we need to do 43295ffd83dbSDimitry Andric // a final bitcast, otherwise use the original result register. 43305ffd83dbSDimitry Andric Register MergeDstReg; 4331fe6060f1SDimitry Andric for (int I = 0; I != NumDefOps; ++I) { 43325ffd83dbSDimitry Andric if (IsNarrow && DstTy.isVector()) 43335ffd83dbSDimitry Andric MergeDstReg = MRI.createGenericVirtualRegister(DstScalarTy); 43345ffd83dbSDimitry Andric else 4335fe6060f1SDimitry Andric MergeDstReg = DstRegs[I]; 43365ffd83dbSDimitry Andric 4337fe6060f1SDimitry Andric buildWidenedRemergeToDst(MergeDstReg, DstLCMTy, ResultRegs[I]); 43385ffd83dbSDimitry Andric 43395ffd83dbSDimitry Andric // Recast to vector if we narrowed a vector 43405ffd83dbSDimitry Andric if (IsNarrow && DstTy.isVector()) 4341fe6060f1SDimitry Andric MIRBuilder.buildBitcast(DstRegs[I], MergeDstReg); 4342fe6060f1SDimitry Andric } 43435ffd83dbSDimitry Andric 43445ffd83dbSDimitry Andric MI.eraseFromParent(); 43455ffd83dbSDimitry Andric return Legalized; 43465ffd83dbSDimitry Andric } 43475ffd83dbSDimitry Andric 43485ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 43495ffd83dbSDimitry Andric LegalizerHelper::fewerElementsVectorSextInReg(MachineInstr &MI, unsigned TypeIdx, 43505ffd83dbSDimitry Andric LLT NarrowTy) { 43515ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 43525ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 43535ffd83dbSDimitry Andric int64_t Imm = MI.getOperand(2).getImm(); 43545ffd83dbSDimitry Andric 43555ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 43565ffd83dbSDimitry Andric 43575ffd83dbSDimitry Andric SmallVector<Register, 8> Parts; 43585ffd83dbSDimitry Andric LLT GCDTy = extractGCDType(Parts, DstTy, NarrowTy, SrcReg); 43595ffd83dbSDimitry Andric LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts); 43605ffd83dbSDimitry Andric 43615ffd83dbSDimitry Andric for (Register &R : Parts) 43625ffd83dbSDimitry Andric R = MIRBuilder.buildSExtInReg(NarrowTy, R, Imm).getReg(0); 43635ffd83dbSDimitry Andric 43645ffd83dbSDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, Parts); 43655ffd83dbSDimitry Andric 43665ffd83dbSDimitry Andric MI.eraseFromParent(); 43675ffd83dbSDimitry Andric return Legalized; 43685ffd83dbSDimitry Andric } 43695ffd83dbSDimitry Andric 43705ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 43710b57cec5SDimitry Andric LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx, 43720b57cec5SDimitry Andric LLT NarrowTy) { 43730b57cec5SDimitry Andric using namespace TargetOpcode; 43740b57cec5SDimitry Andric 43750b57cec5SDimitry Andric switch (MI.getOpcode()) { 43760b57cec5SDimitry Andric case G_IMPLICIT_DEF: 43770b57cec5SDimitry Andric return fewerElementsVectorImplicitDef(MI, TypeIdx, NarrowTy); 43785ffd83dbSDimitry Andric case G_TRUNC: 43790b57cec5SDimitry Andric case G_AND: 43800b57cec5SDimitry Andric case G_OR: 43810b57cec5SDimitry Andric case G_XOR: 43820b57cec5SDimitry Andric case G_ADD: 43830b57cec5SDimitry Andric case G_SUB: 43840b57cec5SDimitry Andric case G_MUL: 4385e8d8bef9SDimitry Andric case G_PTR_ADD: 43860b57cec5SDimitry Andric case G_SMULH: 43870b57cec5SDimitry Andric case G_UMULH: 43880b57cec5SDimitry Andric case G_FADD: 43890b57cec5SDimitry Andric case G_FMUL: 43900b57cec5SDimitry Andric case G_FSUB: 43910b57cec5SDimitry Andric case G_FNEG: 43920b57cec5SDimitry Andric case G_FABS: 43930b57cec5SDimitry Andric case G_FCANONICALIZE: 43940b57cec5SDimitry Andric case G_FDIV: 43950b57cec5SDimitry Andric case G_FREM: 43960b57cec5SDimitry Andric case G_FMA: 43978bcb0991SDimitry Andric case G_FMAD: 43980b57cec5SDimitry Andric case G_FPOW: 43990b57cec5SDimitry Andric case G_FEXP: 44000b57cec5SDimitry Andric case G_FEXP2: 44010b57cec5SDimitry Andric case G_FLOG: 44020b57cec5SDimitry Andric case G_FLOG2: 44030b57cec5SDimitry Andric case G_FLOG10: 44040b57cec5SDimitry Andric case G_FNEARBYINT: 44050b57cec5SDimitry Andric case G_FCEIL: 44060b57cec5SDimitry Andric case G_FFLOOR: 44070b57cec5SDimitry Andric case G_FRINT: 44080b57cec5SDimitry Andric case G_INTRINSIC_ROUND: 4409e8d8bef9SDimitry Andric case G_INTRINSIC_ROUNDEVEN: 44100b57cec5SDimitry Andric case G_INTRINSIC_TRUNC: 44110b57cec5SDimitry Andric case G_FCOS: 44120b57cec5SDimitry Andric case G_FSIN: 44130b57cec5SDimitry Andric case G_FSQRT: 44140b57cec5SDimitry Andric case G_BSWAP: 44158bcb0991SDimitry Andric case G_BITREVERSE: 44160b57cec5SDimitry Andric case G_SDIV: 4417480093f4SDimitry Andric case G_UDIV: 4418480093f4SDimitry Andric case G_SREM: 4419480093f4SDimitry Andric case G_UREM: 4420fe6060f1SDimitry Andric case G_SDIVREM: 4421fe6060f1SDimitry Andric case G_UDIVREM: 44220b57cec5SDimitry Andric case G_SMIN: 44230b57cec5SDimitry Andric case G_SMAX: 44240b57cec5SDimitry Andric case G_UMIN: 44250b57cec5SDimitry Andric case G_UMAX: 4426fe6060f1SDimitry Andric case G_ABS: 44270b57cec5SDimitry Andric case G_FMINNUM: 44280b57cec5SDimitry Andric case G_FMAXNUM: 44290b57cec5SDimitry Andric case G_FMINNUM_IEEE: 44300b57cec5SDimitry Andric case G_FMAXNUM_IEEE: 44310b57cec5SDimitry Andric case G_FMINIMUM: 44320b57cec5SDimitry Andric case G_FMAXIMUM: 44335ffd83dbSDimitry Andric case G_FSHL: 44345ffd83dbSDimitry Andric case G_FSHR: 4435349cc55cSDimitry Andric case G_ROTL: 4436349cc55cSDimitry Andric case G_ROTR: 44375ffd83dbSDimitry Andric case G_FREEZE: 44385ffd83dbSDimitry Andric case G_SADDSAT: 44395ffd83dbSDimitry Andric case G_SSUBSAT: 44405ffd83dbSDimitry Andric case G_UADDSAT: 44415ffd83dbSDimitry Andric case G_USUBSAT: 44425ffd83dbSDimitry Andric return reduceOperationWidth(MI, TypeIdx, NarrowTy); 4443fe6060f1SDimitry Andric case G_UMULO: 4444fe6060f1SDimitry Andric case G_SMULO: 4445fe6060f1SDimitry Andric return fewerElementsVectorMulo(MI, TypeIdx, NarrowTy); 44460b57cec5SDimitry Andric case G_SHL: 44470b57cec5SDimitry Andric case G_LSHR: 44480b57cec5SDimitry Andric case G_ASHR: 4449e8d8bef9SDimitry Andric case G_SSHLSAT: 4450e8d8bef9SDimitry Andric case G_USHLSAT: 44510b57cec5SDimitry Andric case G_CTLZ: 44520b57cec5SDimitry Andric case G_CTLZ_ZERO_UNDEF: 44530b57cec5SDimitry Andric case G_CTTZ: 44540b57cec5SDimitry Andric case G_CTTZ_ZERO_UNDEF: 44550b57cec5SDimitry Andric case G_CTPOP: 44560b57cec5SDimitry Andric case G_FCOPYSIGN: 44570b57cec5SDimitry Andric return fewerElementsVectorMultiEltType(MI, TypeIdx, NarrowTy); 44580b57cec5SDimitry Andric case G_ZEXT: 44590b57cec5SDimitry Andric case G_SEXT: 44600b57cec5SDimitry Andric case G_ANYEXT: 44610b57cec5SDimitry Andric case G_FPEXT: 44620b57cec5SDimitry Andric case G_FPTRUNC: 44630b57cec5SDimitry Andric case G_SITOFP: 44640b57cec5SDimitry Andric case G_UITOFP: 44650b57cec5SDimitry Andric case G_FPTOSI: 44660b57cec5SDimitry Andric case G_FPTOUI: 44670b57cec5SDimitry Andric case G_INTTOPTR: 44680b57cec5SDimitry Andric case G_PTRTOINT: 44690b57cec5SDimitry Andric case G_ADDRSPACE_CAST: 44700b57cec5SDimitry Andric return fewerElementsVectorCasts(MI, TypeIdx, NarrowTy); 44710b57cec5SDimitry Andric case G_ICMP: 44720b57cec5SDimitry Andric case G_FCMP: 44730b57cec5SDimitry Andric return fewerElementsVectorCmp(MI, TypeIdx, NarrowTy); 44740b57cec5SDimitry Andric case G_SELECT: 44750b57cec5SDimitry Andric return fewerElementsVectorSelect(MI, TypeIdx, NarrowTy); 44760b57cec5SDimitry Andric case G_PHI: 44770b57cec5SDimitry Andric return fewerElementsVectorPhi(MI, TypeIdx, NarrowTy); 44788bcb0991SDimitry Andric case G_UNMERGE_VALUES: 44798bcb0991SDimitry Andric return fewerElementsVectorUnmergeValues(MI, TypeIdx, NarrowTy); 44808bcb0991SDimitry Andric case G_BUILD_VECTOR: 4481e8d8bef9SDimitry Andric assert(TypeIdx == 0 && "not a vector type index"); 4482e8d8bef9SDimitry Andric return fewerElementsVectorMerge(MI, TypeIdx, NarrowTy); 4483e8d8bef9SDimitry Andric case G_CONCAT_VECTORS: 4484e8d8bef9SDimitry Andric if (TypeIdx != 1) // TODO: This probably does work as expected already. 4485e8d8bef9SDimitry Andric return UnableToLegalize; 4486e8d8bef9SDimitry Andric return fewerElementsVectorMerge(MI, TypeIdx, NarrowTy); 4487e8d8bef9SDimitry Andric case G_EXTRACT_VECTOR_ELT: 4488e8d8bef9SDimitry Andric case G_INSERT_VECTOR_ELT: 4489e8d8bef9SDimitry Andric return fewerElementsVectorExtractInsertVectorElt(MI, TypeIdx, NarrowTy); 44900b57cec5SDimitry Andric case G_LOAD: 44910b57cec5SDimitry Andric case G_STORE: 4492fe6060f1SDimitry Andric return reduceLoadStoreWidth(cast<GLoadStore>(MI), TypeIdx, NarrowTy); 44935ffd83dbSDimitry Andric case G_SEXT_INREG: 44945ffd83dbSDimitry Andric return fewerElementsVectorSextInReg(MI, TypeIdx, NarrowTy); 4495fe6060f1SDimitry Andric GISEL_VECREDUCE_CASES_NONSEQ 4496fe6060f1SDimitry Andric return fewerElementsVectorReductions(MI, TypeIdx, NarrowTy); 4497fe6060f1SDimitry Andric case G_SHUFFLE_VECTOR: 4498fe6060f1SDimitry Andric return fewerElementsVectorShuffle(MI, TypeIdx, NarrowTy); 44990b57cec5SDimitry Andric default: 45000b57cec5SDimitry Andric return UnableToLegalize; 45010b57cec5SDimitry Andric } 45020b57cec5SDimitry Andric } 45030b57cec5SDimitry Andric 4504fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorShuffle( 4505fe6060f1SDimitry Andric MachineInstr &MI, unsigned int TypeIdx, LLT NarrowTy) { 4506fe6060f1SDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR); 4507fe6060f1SDimitry Andric if (TypeIdx != 0) 4508fe6060f1SDimitry Andric return UnableToLegalize; 4509fe6060f1SDimitry Andric 4510fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4511fe6060f1SDimitry Andric Register Src1Reg = MI.getOperand(1).getReg(); 4512fe6060f1SDimitry Andric Register Src2Reg = MI.getOperand(2).getReg(); 4513fe6060f1SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 4514fe6060f1SDimitry Andric LLT DstTy = MRI.getType(DstReg); 4515fe6060f1SDimitry Andric LLT Src1Ty = MRI.getType(Src1Reg); 4516fe6060f1SDimitry Andric LLT Src2Ty = MRI.getType(Src2Reg); 4517fe6060f1SDimitry Andric // The shuffle should be canonicalized by now. 4518fe6060f1SDimitry Andric if (DstTy != Src1Ty) 4519fe6060f1SDimitry Andric return UnableToLegalize; 4520fe6060f1SDimitry Andric if (DstTy != Src2Ty) 4521fe6060f1SDimitry Andric return UnableToLegalize; 4522fe6060f1SDimitry Andric 4523fe6060f1SDimitry Andric if (!isPowerOf2_32(DstTy.getNumElements())) 4524fe6060f1SDimitry Andric return UnableToLegalize; 4525fe6060f1SDimitry Andric 4526fe6060f1SDimitry Andric // We only support splitting a shuffle into 2, so adjust NarrowTy accordingly. 4527fe6060f1SDimitry Andric // Further legalization attempts will be needed to do split further. 4528fe6060f1SDimitry Andric NarrowTy = 4529fe6060f1SDimitry Andric DstTy.changeElementCount(DstTy.getElementCount().divideCoefficientBy(2)); 4530fe6060f1SDimitry Andric unsigned NewElts = NarrowTy.getNumElements(); 4531fe6060f1SDimitry Andric 4532fe6060f1SDimitry Andric SmallVector<Register> SplitSrc1Regs, SplitSrc2Regs; 4533fe6060f1SDimitry Andric extractParts(Src1Reg, NarrowTy, 2, SplitSrc1Regs); 4534fe6060f1SDimitry Andric extractParts(Src2Reg, NarrowTy, 2, SplitSrc2Regs); 4535fe6060f1SDimitry Andric Register Inputs[4] = {SplitSrc1Regs[0], SplitSrc1Regs[1], SplitSrc2Regs[0], 4536fe6060f1SDimitry Andric SplitSrc2Regs[1]}; 4537fe6060f1SDimitry Andric 4538fe6060f1SDimitry Andric Register Hi, Lo; 4539fe6060f1SDimitry Andric 4540fe6060f1SDimitry Andric // If Lo or Hi uses elements from at most two of the four input vectors, then 4541fe6060f1SDimitry Andric // express it as a vector shuffle of those two inputs. Otherwise extract the 4542fe6060f1SDimitry Andric // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR. 4543fe6060f1SDimitry Andric SmallVector<int, 16> Ops; 4544fe6060f1SDimitry Andric for (unsigned High = 0; High < 2; ++High) { 4545fe6060f1SDimitry Andric Register &Output = High ? Hi : Lo; 4546fe6060f1SDimitry Andric 4547fe6060f1SDimitry Andric // Build a shuffle mask for the output, discovering on the fly which 4548fe6060f1SDimitry Andric // input vectors to use as shuffle operands (recorded in InputUsed). 4549fe6060f1SDimitry Andric // If building a suitable shuffle vector proves too hard, then bail 4550fe6060f1SDimitry Andric // out with useBuildVector set. 4551fe6060f1SDimitry Andric unsigned InputUsed[2] = {-1U, -1U}; // Not yet discovered. 4552fe6060f1SDimitry Andric unsigned FirstMaskIdx = High * NewElts; 4553fe6060f1SDimitry Andric bool UseBuildVector = false; 4554fe6060f1SDimitry Andric for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) { 4555fe6060f1SDimitry Andric // The mask element. This indexes into the input. 4556fe6060f1SDimitry Andric int Idx = Mask[FirstMaskIdx + MaskOffset]; 4557fe6060f1SDimitry Andric 4558fe6060f1SDimitry Andric // The input vector this mask element indexes into. 4559fe6060f1SDimitry Andric unsigned Input = (unsigned)Idx / NewElts; 4560fe6060f1SDimitry Andric 4561fe6060f1SDimitry Andric if (Input >= array_lengthof(Inputs)) { 4562fe6060f1SDimitry Andric // The mask element does not index into any input vector. 4563fe6060f1SDimitry Andric Ops.push_back(-1); 4564fe6060f1SDimitry Andric continue; 4565fe6060f1SDimitry Andric } 4566fe6060f1SDimitry Andric 4567fe6060f1SDimitry Andric // Turn the index into an offset from the start of the input vector. 4568fe6060f1SDimitry Andric Idx -= Input * NewElts; 4569fe6060f1SDimitry Andric 4570fe6060f1SDimitry Andric // Find or create a shuffle vector operand to hold this input. 4571fe6060f1SDimitry Andric unsigned OpNo; 4572fe6060f1SDimitry Andric for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) { 4573fe6060f1SDimitry Andric if (InputUsed[OpNo] == Input) { 4574fe6060f1SDimitry Andric // This input vector is already an operand. 4575fe6060f1SDimitry Andric break; 4576fe6060f1SDimitry Andric } else if (InputUsed[OpNo] == -1U) { 4577fe6060f1SDimitry Andric // Create a new operand for this input vector. 4578fe6060f1SDimitry Andric InputUsed[OpNo] = Input; 4579fe6060f1SDimitry Andric break; 4580fe6060f1SDimitry Andric } 4581fe6060f1SDimitry Andric } 4582fe6060f1SDimitry Andric 4583fe6060f1SDimitry Andric if (OpNo >= array_lengthof(InputUsed)) { 4584fe6060f1SDimitry Andric // More than two input vectors used! Give up on trying to create a 4585fe6060f1SDimitry Andric // shuffle vector. Insert all elements into a BUILD_VECTOR instead. 4586fe6060f1SDimitry Andric UseBuildVector = true; 4587fe6060f1SDimitry Andric break; 4588fe6060f1SDimitry Andric } 4589fe6060f1SDimitry Andric 4590fe6060f1SDimitry Andric // Add the mask index for the new shuffle vector. 4591fe6060f1SDimitry Andric Ops.push_back(Idx + OpNo * NewElts); 4592fe6060f1SDimitry Andric } 4593fe6060f1SDimitry Andric 4594fe6060f1SDimitry Andric if (UseBuildVector) { 4595fe6060f1SDimitry Andric LLT EltTy = NarrowTy.getElementType(); 4596fe6060f1SDimitry Andric SmallVector<Register, 16> SVOps; 4597fe6060f1SDimitry Andric 4598fe6060f1SDimitry Andric // Extract the input elements by hand. 4599fe6060f1SDimitry Andric for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) { 4600fe6060f1SDimitry Andric // The mask element. This indexes into the input. 4601fe6060f1SDimitry Andric int Idx = Mask[FirstMaskIdx + MaskOffset]; 4602fe6060f1SDimitry Andric 4603fe6060f1SDimitry Andric // The input vector this mask element indexes into. 4604fe6060f1SDimitry Andric unsigned Input = (unsigned)Idx / NewElts; 4605fe6060f1SDimitry Andric 4606fe6060f1SDimitry Andric if (Input >= array_lengthof(Inputs)) { 4607fe6060f1SDimitry Andric // The mask element is "undef" or indexes off the end of the input. 4608fe6060f1SDimitry Andric SVOps.push_back(MIRBuilder.buildUndef(EltTy).getReg(0)); 4609fe6060f1SDimitry Andric continue; 4610fe6060f1SDimitry Andric } 4611fe6060f1SDimitry Andric 4612fe6060f1SDimitry Andric // Turn the index into an offset from the start of the input vector. 4613fe6060f1SDimitry Andric Idx -= Input * NewElts; 4614fe6060f1SDimitry Andric 4615fe6060f1SDimitry Andric // Extract the vector element by hand. 4616fe6060f1SDimitry Andric SVOps.push_back(MIRBuilder 4617fe6060f1SDimitry Andric .buildExtractVectorElement( 4618fe6060f1SDimitry Andric EltTy, Inputs[Input], 4619fe6060f1SDimitry Andric MIRBuilder.buildConstant(LLT::scalar(32), Idx)) 4620fe6060f1SDimitry Andric .getReg(0)); 4621fe6060f1SDimitry Andric } 4622fe6060f1SDimitry Andric 4623fe6060f1SDimitry Andric // Construct the Lo/Hi output using a G_BUILD_VECTOR. 4624fe6060f1SDimitry Andric Output = MIRBuilder.buildBuildVector(NarrowTy, SVOps).getReg(0); 4625fe6060f1SDimitry Andric } else if (InputUsed[0] == -1U) { 4626fe6060f1SDimitry Andric // No input vectors were used! The result is undefined. 4627fe6060f1SDimitry Andric Output = MIRBuilder.buildUndef(NarrowTy).getReg(0); 4628fe6060f1SDimitry Andric } else { 4629fe6060f1SDimitry Andric Register Op0 = Inputs[InputUsed[0]]; 4630fe6060f1SDimitry Andric // If only one input was used, use an undefined vector for the other. 4631fe6060f1SDimitry Andric Register Op1 = InputUsed[1] == -1U 4632fe6060f1SDimitry Andric ? MIRBuilder.buildUndef(NarrowTy).getReg(0) 4633fe6060f1SDimitry Andric : Inputs[InputUsed[1]]; 4634fe6060f1SDimitry Andric // At least one input vector was used. Create a new shuffle vector. 4635fe6060f1SDimitry Andric Output = MIRBuilder.buildShuffleVector(NarrowTy, Op0, Op1, Ops).getReg(0); 4636fe6060f1SDimitry Andric } 4637fe6060f1SDimitry Andric 4638fe6060f1SDimitry Andric Ops.clear(); 4639fe6060f1SDimitry Andric } 4640fe6060f1SDimitry Andric 4641fe6060f1SDimitry Andric MIRBuilder.buildConcatVectors(DstReg, {Lo, Hi}); 4642fe6060f1SDimitry Andric MI.eraseFromParent(); 4643fe6060f1SDimitry Andric return Legalized; 4644fe6060f1SDimitry Andric } 4645fe6060f1SDimitry Andric 4646349cc55cSDimitry Andric static unsigned getScalarOpcForReduction(unsigned Opc) { 4647fe6060f1SDimitry Andric unsigned ScalarOpc; 4648fe6060f1SDimitry Andric switch (Opc) { 4649fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FADD: 4650fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FADD; 4651fe6060f1SDimitry Andric break; 4652fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMUL: 4653fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMUL; 4654fe6060f1SDimitry Andric break; 4655fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMAX: 4656fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMAXNUM; 4657fe6060f1SDimitry Andric break; 4658fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_FMIN: 4659fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_FMINNUM; 4660fe6060f1SDimitry Andric break; 4661fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_ADD: 4662fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_ADD; 4663fe6060f1SDimitry Andric break; 4664fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_MUL: 4665fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_MUL; 4666fe6060f1SDimitry Andric break; 4667fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_AND: 4668fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_AND; 4669fe6060f1SDimitry Andric break; 4670fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_OR: 4671fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_OR; 4672fe6060f1SDimitry Andric break; 4673fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_XOR: 4674fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_XOR; 4675fe6060f1SDimitry Andric break; 4676fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_SMAX: 4677fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_SMAX; 4678fe6060f1SDimitry Andric break; 4679fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_SMIN: 4680fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_SMIN; 4681fe6060f1SDimitry Andric break; 4682fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_UMAX: 4683fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_UMAX; 4684fe6060f1SDimitry Andric break; 4685fe6060f1SDimitry Andric case TargetOpcode::G_VECREDUCE_UMIN: 4686fe6060f1SDimitry Andric ScalarOpc = TargetOpcode::G_UMIN; 4687fe6060f1SDimitry Andric break; 4688fe6060f1SDimitry Andric default: 4689349cc55cSDimitry Andric llvm_unreachable("Unhandled reduction"); 4690fe6060f1SDimitry Andric } 4691349cc55cSDimitry Andric return ScalarOpc; 4692349cc55cSDimitry Andric } 4693349cc55cSDimitry Andric 4694349cc55cSDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorReductions( 4695349cc55cSDimitry Andric MachineInstr &MI, unsigned int TypeIdx, LLT NarrowTy) { 4696349cc55cSDimitry Andric unsigned Opc = MI.getOpcode(); 4697349cc55cSDimitry Andric assert(Opc != TargetOpcode::G_VECREDUCE_SEQ_FADD && 4698349cc55cSDimitry Andric Opc != TargetOpcode::G_VECREDUCE_SEQ_FMUL && 4699349cc55cSDimitry Andric "Sequential reductions not expected"); 4700349cc55cSDimitry Andric 4701349cc55cSDimitry Andric if (TypeIdx != 1) 4702349cc55cSDimitry Andric return UnableToLegalize; 4703349cc55cSDimitry Andric 4704349cc55cSDimitry Andric // The semantics of the normal non-sequential reductions allow us to freely 4705349cc55cSDimitry Andric // re-associate the operation. 4706349cc55cSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 4707349cc55cSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 4708349cc55cSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 4709349cc55cSDimitry Andric LLT DstTy = MRI.getType(DstReg); 4710349cc55cSDimitry Andric 4711349cc55cSDimitry Andric if (NarrowTy.isVector() && 4712349cc55cSDimitry Andric (SrcTy.getNumElements() % NarrowTy.getNumElements() != 0)) 4713349cc55cSDimitry Andric return UnableToLegalize; 4714349cc55cSDimitry Andric 4715349cc55cSDimitry Andric unsigned ScalarOpc = getScalarOpcForReduction(Opc); 4716349cc55cSDimitry Andric SmallVector<Register> SplitSrcs; 4717349cc55cSDimitry Andric // If NarrowTy is a scalar then we're being asked to scalarize. 4718349cc55cSDimitry Andric const unsigned NumParts = 4719349cc55cSDimitry Andric NarrowTy.isVector() ? SrcTy.getNumElements() / NarrowTy.getNumElements() 4720349cc55cSDimitry Andric : SrcTy.getNumElements(); 4721349cc55cSDimitry Andric 4722349cc55cSDimitry Andric extractParts(SrcReg, NarrowTy, NumParts, SplitSrcs); 4723349cc55cSDimitry Andric if (NarrowTy.isScalar()) { 4724349cc55cSDimitry Andric if (DstTy != NarrowTy) 4725349cc55cSDimitry Andric return UnableToLegalize; // FIXME: handle implicit extensions. 4726349cc55cSDimitry Andric 4727349cc55cSDimitry Andric if (isPowerOf2_32(NumParts)) { 4728349cc55cSDimitry Andric // Generate a tree of scalar operations to reduce the critical path. 4729349cc55cSDimitry Andric SmallVector<Register> PartialResults; 4730349cc55cSDimitry Andric unsigned NumPartsLeft = NumParts; 4731349cc55cSDimitry Andric while (NumPartsLeft > 1) { 4732349cc55cSDimitry Andric for (unsigned Idx = 0; Idx < NumPartsLeft - 1; Idx += 2) { 4733349cc55cSDimitry Andric PartialResults.emplace_back( 4734349cc55cSDimitry Andric MIRBuilder 4735349cc55cSDimitry Andric .buildInstr(ScalarOpc, {NarrowTy}, 4736349cc55cSDimitry Andric {SplitSrcs[Idx], SplitSrcs[Idx + 1]}) 4737349cc55cSDimitry Andric .getReg(0)); 4738349cc55cSDimitry Andric } 4739349cc55cSDimitry Andric SplitSrcs = PartialResults; 4740349cc55cSDimitry Andric PartialResults.clear(); 4741349cc55cSDimitry Andric NumPartsLeft = SplitSrcs.size(); 4742349cc55cSDimitry Andric } 4743349cc55cSDimitry Andric assert(SplitSrcs.size() == 1); 4744349cc55cSDimitry Andric MIRBuilder.buildCopy(DstReg, SplitSrcs[0]); 4745349cc55cSDimitry Andric MI.eraseFromParent(); 4746349cc55cSDimitry Andric return Legalized; 4747349cc55cSDimitry Andric } 4748349cc55cSDimitry Andric // If we can't generate a tree, then just do sequential operations. 4749349cc55cSDimitry Andric Register Acc = SplitSrcs[0]; 4750349cc55cSDimitry Andric for (unsigned Idx = 1; Idx < NumParts; ++Idx) 4751349cc55cSDimitry Andric Acc = MIRBuilder.buildInstr(ScalarOpc, {NarrowTy}, {Acc, SplitSrcs[Idx]}) 4752349cc55cSDimitry Andric .getReg(0); 4753349cc55cSDimitry Andric MIRBuilder.buildCopy(DstReg, Acc); 4754349cc55cSDimitry Andric MI.eraseFromParent(); 4755349cc55cSDimitry Andric return Legalized; 4756349cc55cSDimitry Andric } 4757349cc55cSDimitry Andric SmallVector<Register> PartialReductions; 4758349cc55cSDimitry Andric for (unsigned Part = 0; Part < NumParts; ++Part) { 4759349cc55cSDimitry Andric PartialReductions.push_back( 4760349cc55cSDimitry Andric MIRBuilder.buildInstr(Opc, {DstTy}, {SplitSrcs[Part]}).getReg(0)); 4761349cc55cSDimitry Andric } 4762349cc55cSDimitry Andric 4763fe6060f1SDimitry Andric 4764fe6060f1SDimitry Andric // If the types involved are powers of 2, we can generate intermediate vector 4765fe6060f1SDimitry Andric // ops, before generating a final reduction operation. 4766fe6060f1SDimitry Andric if (isPowerOf2_32(SrcTy.getNumElements()) && 4767fe6060f1SDimitry Andric isPowerOf2_32(NarrowTy.getNumElements())) { 4768fe6060f1SDimitry Andric return tryNarrowPow2Reduction(MI, SrcReg, SrcTy, NarrowTy, ScalarOpc); 4769fe6060f1SDimitry Andric } 4770fe6060f1SDimitry Andric 4771fe6060f1SDimitry Andric Register Acc = PartialReductions[0]; 4772fe6060f1SDimitry Andric for (unsigned Part = 1; Part < NumParts; ++Part) { 4773fe6060f1SDimitry Andric if (Part == NumParts - 1) { 4774fe6060f1SDimitry Andric MIRBuilder.buildInstr(ScalarOpc, {DstReg}, 4775fe6060f1SDimitry Andric {Acc, PartialReductions[Part]}); 4776fe6060f1SDimitry Andric } else { 4777fe6060f1SDimitry Andric Acc = MIRBuilder 4778fe6060f1SDimitry Andric .buildInstr(ScalarOpc, {DstTy}, {Acc, PartialReductions[Part]}) 4779fe6060f1SDimitry Andric .getReg(0); 4780fe6060f1SDimitry Andric } 4781fe6060f1SDimitry Andric } 4782fe6060f1SDimitry Andric MI.eraseFromParent(); 4783fe6060f1SDimitry Andric return Legalized; 4784fe6060f1SDimitry Andric } 4785fe6060f1SDimitry Andric 4786fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 4787fe6060f1SDimitry Andric LegalizerHelper::tryNarrowPow2Reduction(MachineInstr &MI, Register SrcReg, 4788fe6060f1SDimitry Andric LLT SrcTy, LLT NarrowTy, 4789fe6060f1SDimitry Andric unsigned ScalarOpc) { 4790fe6060f1SDimitry Andric SmallVector<Register> SplitSrcs; 4791fe6060f1SDimitry Andric // Split the sources into NarrowTy size pieces. 4792fe6060f1SDimitry Andric extractParts(SrcReg, NarrowTy, 4793fe6060f1SDimitry Andric SrcTy.getNumElements() / NarrowTy.getNumElements(), SplitSrcs); 4794fe6060f1SDimitry Andric // We're going to do a tree reduction using vector operations until we have 4795fe6060f1SDimitry Andric // one NarrowTy size value left. 4796fe6060f1SDimitry Andric while (SplitSrcs.size() > 1) { 4797fe6060f1SDimitry Andric SmallVector<Register> PartialRdxs; 4798fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < SplitSrcs.size()-1; Idx += 2) { 4799fe6060f1SDimitry Andric Register LHS = SplitSrcs[Idx]; 4800fe6060f1SDimitry Andric Register RHS = SplitSrcs[Idx + 1]; 4801fe6060f1SDimitry Andric // Create the intermediate vector op. 4802fe6060f1SDimitry Andric Register Res = 4803fe6060f1SDimitry Andric MIRBuilder.buildInstr(ScalarOpc, {NarrowTy}, {LHS, RHS}).getReg(0); 4804fe6060f1SDimitry Andric PartialRdxs.push_back(Res); 4805fe6060f1SDimitry Andric } 4806fe6060f1SDimitry Andric SplitSrcs = std::move(PartialRdxs); 4807fe6060f1SDimitry Andric } 4808fe6060f1SDimitry Andric // Finally generate the requested NarrowTy based reduction. 4809fe6060f1SDimitry Andric Observer.changingInstr(MI); 4810fe6060f1SDimitry Andric MI.getOperand(1).setReg(SplitSrcs[0]); 4811fe6060f1SDimitry Andric Observer.changedInstr(MI); 4812fe6060f1SDimitry Andric return Legalized; 4813fe6060f1SDimitry Andric } 4814fe6060f1SDimitry Andric 48150b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 48160b57cec5SDimitry Andric LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt, 48170b57cec5SDimitry Andric const LLT HalfTy, const LLT AmtTy) { 48180b57cec5SDimitry Andric 48190b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 48200b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 48215ffd83dbSDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1)); 48220b57cec5SDimitry Andric 4823349cc55cSDimitry Andric if (Amt.isZero()) { 48245ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), {InL, InH}); 48250b57cec5SDimitry Andric MI.eraseFromParent(); 48260b57cec5SDimitry Andric return Legalized; 48270b57cec5SDimitry Andric } 48280b57cec5SDimitry Andric 48290b57cec5SDimitry Andric LLT NVT = HalfTy; 48300b57cec5SDimitry Andric unsigned NVTBits = HalfTy.getSizeInBits(); 48310b57cec5SDimitry Andric unsigned VTBits = 2 * NVTBits; 48320b57cec5SDimitry Andric 48330b57cec5SDimitry Andric SrcOp Lo(Register(0)), Hi(Register(0)); 48340b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_SHL) { 48350b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 48360b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 48370b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 48380b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 48390b57cec5SDimitry Andric Hi = MIRBuilder.buildShl(NVT, InL, 48400b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 48410b57cec5SDimitry Andric } else if (Amt == NVTBits) { 48420b57cec5SDimitry Andric Lo = MIRBuilder.buildConstant(NVT, 0); 48430b57cec5SDimitry Andric Hi = InL; 48440b57cec5SDimitry Andric } else { 48450b57cec5SDimitry Andric Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt)); 48460b57cec5SDimitry Andric auto OrLHS = 48470b57cec5SDimitry Andric MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt)); 48480b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildLShr( 48490b57cec5SDimitry Andric NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 48500b57cec5SDimitry Andric Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 48510b57cec5SDimitry Andric } 48520b57cec5SDimitry Andric } else if (MI.getOpcode() == TargetOpcode::G_LSHR) { 48530b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 48540b57cec5SDimitry Andric Lo = Hi = MIRBuilder.buildConstant(NVT, 0); 48550b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 48560b57cec5SDimitry Andric Lo = MIRBuilder.buildLShr(NVT, InH, 48570b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 48580b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 48590b57cec5SDimitry Andric } else if (Amt == NVTBits) { 48600b57cec5SDimitry Andric Lo = InH; 48610b57cec5SDimitry Andric Hi = MIRBuilder.buildConstant(NVT, 0); 48620b57cec5SDimitry Andric } else { 48630b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 48640b57cec5SDimitry Andric 48650b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 48660b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 48670b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 48680b57cec5SDimitry Andric 48690b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 48700b57cec5SDimitry Andric Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst); 48710b57cec5SDimitry Andric } 48720b57cec5SDimitry Andric } else { 48730b57cec5SDimitry Andric if (Amt.ugt(VTBits)) { 48740b57cec5SDimitry Andric Hi = Lo = MIRBuilder.buildAShr( 48750b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 48760b57cec5SDimitry Andric } else if (Amt.ugt(NVTBits)) { 48770b57cec5SDimitry Andric Lo = MIRBuilder.buildAShr(NVT, InH, 48780b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); 48790b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 48800b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 48810b57cec5SDimitry Andric } else if (Amt == NVTBits) { 48820b57cec5SDimitry Andric Lo = InH; 48830b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, 48840b57cec5SDimitry Andric MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); 48850b57cec5SDimitry Andric } else { 48860b57cec5SDimitry Andric auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); 48870b57cec5SDimitry Andric 48880b57cec5SDimitry Andric auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); 48890b57cec5SDimitry Andric auto OrRHS = MIRBuilder.buildShl( 48900b57cec5SDimitry Andric NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); 48910b57cec5SDimitry Andric 48920b57cec5SDimitry Andric Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); 48930b57cec5SDimitry Andric Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst); 48940b57cec5SDimitry Andric } 48950b57cec5SDimitry Andric } 48960b57cec5SDimitry Andric 48975ffd83dbSDimitry Andric MIRBuilder.buildMerge(MI.getOperand(0), {Lo, Hi}); 48980b57cec5SDimitry Andric MI.eraseFromParent(); 48990b57cec5SDimitry Andric 49000b57cec5SDimitry Andric return Legalized; 49010b57cec5SDimitry Andric } 49020b57cec5SDimitry Andric 49030b57cec5SDimitry Andric // TODO: Optimize if constant shift amount. 49040b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 49050b57cec5SDimitry Andric LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx, 49060b57cec5SDimitry Andric LLT RequestedTy) { 49070b57cec5SDimitry Andric if (TypeIdx == 1) { 49080b57cec5SDimitry Andric Observer.changingInstr(MI); 49090b57cec5SDimitry Andric narrowScalarSrc(MI, RequestedTy, 2); 49100b57cec5SDimitry Andric Observer.changedInstr(MI); 49110b57cec5SDimitry Andric return Legalized; 49120b57cec5SDimitry Andric } 49130b57cec5SDimitry Andric 49140b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 49150b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 49160b57cec5SDimitry Andric if (DstTy.isVector()) 49170b57cec5SDimitry Andric return UnableToLegalize; 49180b57cec5SDimitry Andric 49190b57cec5SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 49200b57cec5SDimitry Andric LLT ShiftAmtTy = MRI.getType(Amt); 49210b57cec5SDimitry Andric const unsigned DstEltSize = DstTy.getScalarSizeInBits(); 49220b57cec5SDimitry Andric if (DstEltSize % 2 != 0) 49230b57cec5SDimitry Andric return UnableToLegalize; 49240b57cec5SDimitry Andric 49250b57cec5SDimitry Andric // Ignore the input type. We can only go to exactly half the size of the 49260b57cec5SDimitry Andric // input. If that isn't small enough, the resulting pieces will be further 49270b57cec5SDimitry Andric // legalized. 49280b57cec5SDimitry Andric const unsigned NewBitSize = DstEltSize / 2; 49290b57cec5SDimitry Andric const LLT HalfTy = LLT::scalar(NewBitSize); 49300b57cec5SDimitry Andric const LLT CondTy = LLT::scalar(1); 49310b57cec5SDimitry Andric 4932349cc55cSDimitry Andric if (auto VRegAndVal = getIConstantVRegValWithLookThrough(Amt, MRI)) { 4933349cc55cSDimitry Andric return narrowScalarShiftByConstant(MI, VRegAndVal->Value, HalfTy, 4934349cc55cSDimitry Andric ShiftAmtTy); 49350b57cec5SDimitry Andric } 49360b57cec5SDimitry Andric 49370b57cec5SDimitry Andric // TODO: Expand with known bits. 49380b57cec5SDimitry Andric 49390b57cec5SDimitry Andric // Handle the fully general expansion by an unknown amount. 49400b57cec5SDimitry Andric auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize); 49410b57cec5SDimitry Andric 49420b57cec5SDimitry Andric Register InL = MRI.createGenericVirtualRegister(HalfTy); 49430b57cec5SDimitry Andric Register InH = MRI.createGenericVirtualRegister(HalfTy); 49445ffd83dbSDimitry Andric MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1)); 49450b57cec5SDimitry Andric 49460b57cec5SDimitry Andric auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits); 49470b57cec5SDimitry Andric auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt); 49480b57cec5SDimitry Andric 49490b57cec5SDimitry Andric auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0); 49500b57cec5SDimitry Andric auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits); 49510b57cec5SDimitry Andric auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero); 49520b57cec5SDimitry Andric 49530b57cec5SDimitry Andric Register ResultRegs[2]; 49540b57cec5SDimitry Andric switch (MI.getOpcode()) { 49550b57cec5SDimitry Andric case TargetOpcode::G_SHL: { 49560b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 49578bcb0991SDimitry Andric auto LoS = MIRBuilder.buildShl(HalfTy, InL, Amt); 49580b57cec5SDimitry Andric 49598bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, AmtLack); 49608bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, Amt); 49618bcb0991SDimitry Andric auto HiS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 49620b57cec5SDimitry Andric 49630b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 49640b57cec5SDimitry Andric auto LoL = MIRBuilder.buildConstant(HalfTy, 0); // Lo part is zero. 49650b57cec5SDimitry Andric auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part. 49660b57cec5SDimitry Andric 49670b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL); 49680b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect( 49690b57cec5SDimitry Andric HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL)); 49700b57cec5SDimitry Andric 49710b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 49720b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 49730b57cec5SDimitry Andric break; 49740b57cec5SDimitry Andric } 49758bcb0991SDimitry Andric case TargetOpcode::G_LSHR: 49760b57cec5SDimitry Andric case TargetOpcode::G_ASHR: { 49770b57cec5SDimitry Andric // Short: ShAmt < NewBitSize 49788bcb0991SDimitry Andric auto HiS = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, {InH, Amt}); 49790b57cec5SDimitry Andric 49808bcb0991SDimitry Andric auto LoOr = MIRBuilder.buildLShr(HalfTy, InL, Amt); 49818bcb0991SDimitry Andric auto HiOr = MIRBuilder.buildShl(HalfTy, InH, AmtLack); 49828bcb0991SDimitry Andric auto LoS = MIRBuilder.buildOr(HalfTy, LoOr, HiOr); 49830b57cec5SDimitry Andric 49840b57cec5SDimitry Andric // Long: ShAmt >= NewBitSize 49858bcb0991SDimitry Andric MachineInstrBuilder HiL; 49868bcb0991SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_LSHR) { 49878bcb0991SDimitry Andric HiL = MIRBuilder.buildConstant(HalfTy, 0); // Hi part is zero. 49888bcb0991SDimitry Andric } else { 49898bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1); 49908bcb0991SDimitry Andric HiL = MIRBuilder.buildAShr(HalfTy, InH, ShiftAmt); // Sign of Hi part. 49918bcb0991SDimitry Andric } 49928bcb0991SDimitry Andric auto LoL = MIRBuilder.buildInstr(MI.getOpcode(), {HalfTy}, 49938bcb0991SDimitry Andric {InH, AmtExcess}); // Lo from Hi part. 49940b57cec5SDimitry Andric 49950b57cec5SDimitry Andric auto Lo = MIRBuilder.buildSelect( 49960b57cec5SDimitry Andric HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL)); 49970b57cec5SDimitry Andric 49980b57cec5SDimitry Andric auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL); 49990b57cec5SDimitry Andric 50000b57cec5SDimitry Andric ResultRegs[0] = Lo.getReg(0); 50010b57cec5SDimitry Andric ResultRegs[1] = Hi.getReg(0); 50020b57cec5SDimitry Andric break; 50030b57cec5SDimitry Andric } 50040b57cec5SDimitry Andric default: 50050b57cec5SDimitry Andric llvm_unreachable("not a shift"); 50060b57cec5SDimitry Andric } 50070b57cec5SDimitry Andric 50080b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, ResultRegs); 50090b57cec5SDimitry Andric MI.eraseFromParent(); 50100b57cec5SDimitry Andric return Legalized; 50110b57cec5SDimitry Andric } 50120b57cec5SDimitry Andric 50130b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 50140b57cec5SDimitry Andric LegalizerHelper::moreElementsVectorPhi(MachineInstr &MI, unsigned TypeIdx, 50150b57cec5SDimitry Andric LLT MoreTy) { 50160b57cec5SDimitry Andric assert(TypeIdx == 0 && "Expecting only Idx 0"); 50170b57cec5SDimitry Andric 50180b57cec5SDimitry Andric Observer.changingInstr(MI); 50190b57cec5SDimitry Andric for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 50200b57cec5SDimitry Andric MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); 50210b57cec5SDimitry Andric MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); 50220b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, I); 50230b57cec5SDimitry Andric } 50240b57cec5SDimitry Andric 50250b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 50260b57cec5SDimitry Andric MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); 50270b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 50280b57cec5SDimitry Andric Observer.changedInstr(MI); 50290b57cec5SDimitry Andric return Legalized; 50300b57cec5SDimitry Andric } 50310b57cec5SDimitry Andric 50320b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 50330b57cec5SDimitry Andric LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx, 50340b57cec5SDimitry Andric LLT MoreTy) { 50350b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 50360b57cec5SDimitry Andric switch (Opc) { 50378bcb0991SDimitry Andric case TargetOpcode::G_IMPLICIT_DEF: 50388bcb0991SDimitry Andric case TargetOpcode::G_LOAD: { 50398bcb0991SDimitry Andric if (TypeIdx != 0) 50408bcb0991SDimitry Andric return UnableToLegalize; 50410b57cec5SDimitry Andric Observer.changingInstr(MI); 50420b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 50430b57cec5SDimitry Andric Observer.changedInstr(MI); 50440b57cec5SDimitry Andric return Legalized; 50450b57cec5SDimitry Andric } 50468bcb0991SDimitry Andric case TargetOpcode::G_STORE: 50478bcb0991SDimitry Andric if (TypeIdx != 0) 50488bcb0991SDimitry Andric return UnableToLegalize; 50498bcb0991SDimitry Andric Observer.changingInstr(MI); 50508bcb0991SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 0); 50518bcb0991SDimitry Andric Observer.changedInstr(MI); 50528bcb0991SDimitry Andric return Legalized; 50530b57cec5SDimitry Andric case TargetOpcode::G_AND: 50540b57cec5SDimitry Andric case TargetOpcode::G_OR: 50550b57cec5SDimitry Andric case TargetOpcode::G_XOR: 50560b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 50570b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 50580b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 5059480093f4SDimitry Andric case TargetOpcode::G_UMAX: 5060480093f4SDimitry Andric case TargetOpcode::G_FMINNUM: 5061480093f4SDimitry Andric case TargetOpcode::G_FMAXNUM: 5062480093f4SDimitry Andric case TargetOpcode::G_FMINNUM_IEEE: 5063480093f4SDimitry Andric case TargetOpcode::G_FMAXNUM_IEEE: 5064480093f4SDimitry Andric case TargetOpcode::G_FMINIMUM: 5065480093f4SDimitry Andric case TargetOpcode::G_FMAXIMUM: { 50660b57cec5SDimitry Andric Observer.changingInstr(MI); 50670b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 50680b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 50690b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 50700b57cec5SDimitry Andric Observer.changedInstr(MI); 50710b57cec5SDimitry Andric return Legalized; 50720b57cec5SDimitry Andric } 50730b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: 50740b57cec5SDimitry Andric if (TypeIdx != 1) 50750b57cec5SDimitry Andric return UnableToLegalize; 50760b57cec5SDimitry Andric Observer.changingInstr(MI); 50770b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 50780b57cec5SDimitry Andric Observer.changedInstr(MI); 50790b57cec5SDimitry Andric return Legalized; 50800b57cec5SDimitry Andric case TargetOpcode::G_INSERT: 50815ffd83dbSDimitry Andric case TargetOpcode::G_FREEZE: 50820b57cec5SDimitry Andric if (TypeIdx != 0) 50830b57cec5SDimitry Andric return UnableToLegalize; 50840b57cec5SDimitry Andric Observer.changingInstr(MI); 50850b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 50860b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 50870b57cec5SDimitry Andric Observer.changedInstr(MI); 50880b57cec5SDimitry Andric return Legalized; 50890b57cec5SDimitry Andric case TargetOpcode::G_SELECT: 50900b57cec5SDimitry Andric if (TypeIdx != 0) 50910b57cec5SDimitry Andric return UnableToLegalize; 50920b57cec5SDimitry Andric if (MRI.getType(MI.getOperand(1).getReg()).isVector()) 50930b57cec5SDimitry Andric return UnableToLegalize; 50940b57cec5SDimitry Andric 50950b57cec5SDimitry Andric Observer.changingInstr(MI); 50960b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 50970b57cec5SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 3); 50980b57cec5SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 50990b57cec5SDimitry Andric Observer.changedInstr(MI); 51000b57cec5SDimitry Andric return Legalized; 51018bcb0991SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: { 51028bcb0991SDimitry Andric if (TypeIdx != 1) 51038bcb0991SDimitry Andric return UnableToLegalize; 51048bcb0991SDimitry Andric 51058bcb0991SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 51068bcb0991SDimitry Andric int NumDst = MI.getNumOperands() - 1; 51078bcb0991SDimitry Andric moreElementsVectorSrc(MI, MoreTy, NumDst); 51088bcb0991SDimitry Andric 51098bcb0991SDimitry Andric auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_UNMERGE_VALUES); 51108bcb0991SDimitry Andric for (int I = 0; I != NumDst; ++I) 51118bcb0991SDimitry Andric MIB.addDef(MI.getOperand(I).getReg()); 51128bcb0991SDimitry Andric 51138bcb0991SDimitry Andric int NewNumDst = MoreTy.getSizeInBits() / DstTy.getSizeInBits(); 51148bcb0991SDimitry Andric for (int I = NumDst; I != NewNumDst; ++I) 51158bcb0991SDimitry Andric MIB.addDef(MRI.createGenericVirtualRegister(DstTy)); 51168bcb0991SDimitry Andric 51178bcb0991SDimitry Andric MIB.addUse(MI.getOperand(NumDst).getReg()); 51188bcb0991SDimitry Andric MI.eraseFromParent(); 51198bcb0991SDimitry Andric return Legalized; 51208bcb0991SDimitry Andric } 51210b57cec5SDimitry Andric case TargetOpcode::G_PHI: 51220b57cec5SDimitry Andric return moreElementsVectorPhi(MI, TypeIdx, MoreTy); 5123fe6060f1SDimitry Andric case TargetOpcode::G_SHUFFLE_VECTOR: 5124fe6060f1SDimitry Andric return moreElementsVectorShuffle(MI, TypeIdx, MoreTy); 51250b57cec5SDimitry Andric default: 51260b57cec5SDimitry Andric return UnableToLegalize; 51270b57cec5SDimitry Andric } 51280b57cec5SDimitry Andric } 51290b57cec5SDimitry Andric 5130fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5131fe6060f1SDimitry Andric LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI, 5132fe6060f1SDimitry Andric unsigned int TypeIdx, LLT MoreTy) { 5133fe6060f1SDimitry Andric if (TypeIdx != 0) 5134fe6060f1SDimitry Andric return UnableToLegalize; 5135fe6060f1SDimitry Andric 5136fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 5137fe6060f1SDimitry Andric Register Src1Reg = MI.getOperand(1).getReg(); 5138fe6060f1SDimitry Andric Register Src2Reg = MI.getOperand(2).getReg(); 5139fe6060f1SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 5140fe6060f1SDimitry Andric LLT DstTy = MRI.getType(DstReg); 5141fe6060f1SDimitry Andric LLT Src1Ty = MRI.getType(Src1Reg); 5142fe6060f1SDimitry Andric LLT Src2Ty = MRI.getType(Src2Reg); 5143fe6060f1SDimitry Andric unsigned NumElts = DstTy.getNumElements(); 5144fe6060f1SDimitry Andric unsigned WidenNumElts = MoreTy.getNumElements(); 5145fe6060f1SDimitry Andric 5146fe6060f1SDimitry Andric // Expect a canonicalized shuffle. 5147fe6060f1SDimitry Andric if (DstTy != Src1Ty || DstTy != Src2Ty) 5148fe6060f1SDimitry Andric return UnableToLegalize; 5149fe6060f1SDimitry Andric 5150fe6060f1SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 1); 5151fe6060f1SDimitry Andric moreElementsVectorSrc(MI, MoreTy, 2); 5152fe6060f1SDimitry Andric 5153fe6060f1SDimitry Andric // Adjust mask based on new input vector length. 5154fe6060f1SDimitry Andric SmallVector<int, 16> NewMask; 5155fe6060f1SDimitry Andric for (unsigned I = 0; I != NumElts; ++I) { 5156fe6060f1SDimitry Andric int Idx = Mask[I]; 5157fe6060f1SDimitry Andric if (Idx < static_cast<int>(NumElts)) 5158fe6060f1SDimitry Andric NewMask.push_back(Idx); 5159fe6060f1SDimitry Andric else 5160fe6060f1SDimitry Andric NewMask.push_back(Idx - NumElts + WidenNumElts); 5161fe6060f1SDimitry Andric } 5162fe6060f1SDimitry Andric for (unsigned I = NumElts; I != WidenNumElts; ++I) 5163fe6060f1SDimitry Andric NewMask.push_back(-1); 5164fe6060f1SDimitry Andric moreElementsVectorDst(MI, MoreTy, 0); 5165fe6060f1SDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 5166fe6060f1SDimitry Andric MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(), 5167fe6060f1SDimitry Andric MI.getOperand(1).getReg(), 5168fe6060f1SDimitry Andric MI.getOperand(2).getReg(), NewMask); 5169fe6060f1SDimitry Andric MI.eraseFromParent(); 5170fe6060f1SDimitry Andric return Legalized; 5171fe6060f1SDimitry Andric } 5172fe6060f1SDimitry Andric 51730b57cec5SDimitry Andric void LegalizerHelper::multiplyRegisters(SmallVectorImpl<Register> &DstRegs, 51740b57cec5SDimitry Andric ArrayRef<Register> Src1Regs, 51750b57cec5SDimitry Andric ArrayRef<Register> Src2Regs, 51760b57cec5SDimitry Andric LLT NarrowTy) { 51770b57cec5SDimitry Andric MachineIRBuilder &B = MIRBuilder; 51780b57cec5SDimitry Andric unsigned SrcParts = Src1Regs.size(); 51790b57cec5SDimitry Andric unsigned DstParts = DstRegs.size(); 51800b57cec5SDimitry Andric 51810b57cec5SDimitry Andric unsigned DstIdx = 0; // Low bits of the result. 51820b57cec5SDimitry Andric Register FactorSum = 51830b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx], Src2Regs[DstIdx]).getReg(0); 51840b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 51850b57cec5SDimitry Andric 51860b57cec5SDimitry Andric unsigned CarrySumPrevDstIdx; 51870b57cec5SDimitry Andric SmallVector<Register, 4> Factors; 51880b57cec5SDimitry Andric 51890b57cec5SDimitry Andric for (DstIdx = 1; DstIdx < DstParts; DstIdx++) { 51900b57cec5SDimitry Andric // Collect low parts of muls for DstIdx. 51910b57cec5SDimitry Andric for (unsigned i = DstIdx + 1 < SrcParts ? 0 : DstIdx - SrcParts + 1; 51920b57cec5SDimitry Andric i <= std::min(DstIdx, SrcParts - 1); ++i) { 51930b57cec5SDimitry Andric MachineInstrBuilder Mul = 51940b57cec5SDimitry Andric B.buildMul(NarrowTy, Src1Regs[DstIdx - i], Src2Regs[i]); 51950b57cec5SDimitry Andric Factors.push_back(Mul.getReg(0)); 51960b57cec5SDimitry Andric } 51970b57cec5SDimitry Andric // Collect high parts of muls from previous DstIdx. 51980b57cec5SDimitry Andric for (unsigned i = DstIdx < SrcParts ? 0 : DstIdx - SrcParts; 51990b57cec5SDimitry Andric i <= std::min(DstIdx - 1, SrcParts - 1); ++i) { 52000b57cec5SDimitry Andric MachineInstrBuilder Umulh = 52010b57cec5SDimitry Andric B.buildUMulH(NarrowTy, Src1Regs[DstIdx - 1 - i], Src2Regs[i]); 52020b57cec5SDimitry Andric Factors.push_back(Umulh.getReg(0)); 52030b57cec5SDimitry Andric } 5204480093f4SDimitry Andric // Add CarrySum from additions calculated for previous DstIdx. 52050b57cec5SDimitry Andric if (DstIdx != 1) { 52060b57cec5SDimitry Andric Factors.push_back(CarrySumPrevDstIdx); 52070b57cec5SDimitry Andric } 52080b57cec5SDimitry Andric 52090b57cec5SDimitry Andric Register CarrySum; 52100b57cec5SDimitry Andric // Add all factors and accumulate all carries into CarrySum. 52110b57cec5SDimitry Andric if (DstIdx != DstParts - 1) { 52120b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 52130b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), Factors[0], Factors[1]); 52140b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 52150b57cec5SDimitry Andric CarrySum = B.buildZExt(NarrowTy, Uaddo.getReg(1)).getReg(0); 52160b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) { 52170b57cec5SDimitry Andric MachineInstrBuilder Uaddo = 52180b57cec5SDimitry Andric B.buildUAddo(NarrowTy, LLT::scalar(1), FactorSum, Factors[i]); 52190b57cec5SDimitry Andric FactorSum = Uaddo.getReg(0); 52200b57cec5SDimitry Andric MachineInstrBuilder Carry = B.buildZExt(NarrowTy, Uaddo.getReg(1)); 52210b57cec5SDimitry Andric CarrySum = B.buildAdd(NarrowTy, CarrySum, Carry).getReg(0); 52220b57cec5SDimitry Andric } 52230b57cec5SDimitry Andric } else { 52240b57cec5SDimitry Andric // Since value for the next index is not calculated, neither is CarrySum. 52250b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, Factors[0], Factors[1]).getReg(0); 52260b57cec5SDimitry Andric for (unsigned i = 2; i < Factors.size(); ++i) 52270b57cec5SDimitry Andric FactorSum = B.buildAdd(NarrowTy, FactorSum, Factors[i]).getReg(0); 52280b57cec5SDimitry Andric } 52290b57cec5SDimitry Andric 52300b57cec5SDimitry Andric CarrySumPrevDstIdx = CarrySum; 52310b57cec5SDimitry Andric DstRegs[DstIdx] = FactorSum; 52320b57cec5SDimitry Andric Factors.clear(); 52330b57cec5SDimitry Andric } 52340b57cec5SDimitry Andric } 52350b57cec5SDimitry Andric 52360b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 5237fe6060f1SDimitry Andric LegalizerHelper::narrowScalarAddSub(MachineInstr &MI, unsigned TypeIdx, 5238fe6060f1SDimitry Andric LLT NarrowTy) { 5239fe6060f1SDimitry Andric if (TypeIdx != 0) 5240fe6060f1SDimitry Andric return UnableToLegalize; 5241fe6060f1SDimitry Andric 5242fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 5243fe6060f1SDimitry Andric LLT DstType = MRI.getType(DstReg); 5244fe6060f1SDimitry Andric // FIXME: add support for vector types 5245fe6060f1SDimitry Andric if (DstType.isVector()) 5246fe6060f1SDimitry Andric return UnableToLegalize; 5247fe6060f1SDimitry Andric 5248fe6060f1SDimitry Andric unsigned Opcode = MI.getOpcode(); 5249fe6060f1SDimitry Andric unsigned OpO, OpE, OpF; 5250fe6060f1SDimitry Andric switch (Opcode) { 5251fe6060f1SDimitry Andric case TargetOpcode::G_SADDO: 5252fe6060f1SDimitry Andric case TargetOpcode::G_SADDE: 5253fe6060f1SDimitry Andric case TargetOpcode::G_UADDO: 5254fe6060f1SDimitry Andric case TargetOpcode::G_UADDE: 5255fe6060f1SDimitry Andric case TargetOpcode::G_ADD: 5256fe6060f1SDimitry Andric OpO = TargetOpcode::G_UADDO; 5257fe6060f1SDimitry Andric OpE = TargetOpcode::G_UADDE; 5258fe6060f1SDimitry Andric OpF = TargetOpcode::G_UADDE; 5259fe6060f1SDimitry Andric if (Opcode == TargetOpcode::G_SADDO || Opcode == TargetOpcode::G_SADDE) 5260fe6060f1SDimitry Andric OpF = TargetOpcode::G_SADDE; 5261fe6060f1SDimitry Andric break; 5262fe6060f1SDimitry Andric case TargetOpcode::G_SSUBO: 5263fe6060f1SDimitry Andric case TargetOpcode::G_SSUBE: 5264fe6060f1SDimitry Andric case TargetOpcode::G_USUBO: 5265fe6060f1SDimitry Andric case TargetOpcode::G_USUBE: 5266fe6060f1SDimitry Andric case TargetOpcode::G_SUB: 5267fe6060f1SDimitry Andric OpO = TargetOpcode::G_USUBO; 5268fe6060f1SDimitry Andric OpE = TargetOpcode::G_USUBE; 5269fe6060f1SDimitry Andric OpF = TargetOpcode::G_USUBE; 5270fe6060f1SDimitry Andric if (Opcode == TargetOpcode::G_SSUBO || Opcode == TargetOpcode::G_SSUBE) 5271fe6060f1SDimitry Andric OpF = TargetOpcode::G_SSUBE; 5272fe6060f1SDimitry Andric break; 5273fe6060f1SDimitry Andric default: 5274fe6060f1SDimitry Andric llvm_unreachable("Unexpected add/sub opcode!"); 5275fe6060f1SDimitry Andric } 5276fe6060f1SDimitry Andric 5277fe6060f1SDimitry Andric // 1 for a plain add/sub, 2 if this is an operation with a carry-out. 5278fe6060f1SDimitry Andric unsigned NumDefs = MI.getNumExplicitDefs(); 5279fe6060f1SDimitry Andric Register Src1 = MI.getOperand(NumDefs).getReg(); 5280fe6060f1SDimitry Andric Register Src2 = MI.getOperand(NumDefs + 1).getReg(); 5281fe6060f1SDimitry Andric Register CarryDst, CarryIn; 5282fe6060f1SDimitry Andric if (NumDefs == 2) 5283fe6060f1SDimitry Andric CarryDst = MI.getOperand(1).getReg(); 5284fe6060f1SDimitry Andric if (MI.getNumOperands() == NumDefs + 3) 5285fe6060f1SDimitry Andric CarryIn = MI.getOperand(NumDefs + 2).getReg(); 5286fe6060f1SDimitry Andric 5287fe6060f1SDimitry Andric LLT RegTy = MRI.getType(MI.getOperand(0).getReg()); 5288fe6060f1SDimitry Andric LLT LeftoverTy, DummyTy; 5289fe6060f1SDimitry Andric SmallVector<Register, 2> Src1Regs, Src2Regs, Src1Left, Src2Left, DstRegs; 5290fe6060f1SDimitry Andric extractParts(Src1, RegTy, NarrowTy, LeftoverTy, Src1Regs, Src1Left); 5291fe6060f1SDimitry Andric extractParts(Src2, RegTy, NarrowTy, DummyTy, Src2Regs, Src2Left); 5292fe6060f1SDimitry Andric 5293fe6060f1SDimitry Andric int NarrowParts = Src1Regs.size(); 5294fe6060f1SDimitry Andric for (int I = 0, E = Src1Left.size(); I != E; ++I) { 5295fe6060f1SDimitry Andric Src1Regs.push_back(Src1Left[I]); 5296fe6060f1SDimitry Andric Src2Regs.push_back(Src2Left[I]); 5297fe6060f1SDimitry Andric } 5298fe6060f1SDimitry Andric DstRegs.reserve(Src1Regs.size()); 5299fe6060f1SDimitry Andric 5300fe6060f1SDimitry Andric for (int i = 0, e = Src1Regs.size(); i != e; ++i) { 5301fe6060f1SDimitry Andric Register DstReg = 5302fe6060f1SDimitry Andric MRI.createGenericVirtualRegister(MRI.getType(Src1Regs[i])); 5303fe6060f1SDimitry Andric Register CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1)); 5304fe6060f1SDimitry Andric // Forward the final carry-out to the destination register 5305fe6060f1SDimitry Andric if (i == e - 1 && CarryDst) 5306fe6060f1SDimitry Andric CarryOut = CarryDst; 5307fe6060f1SDimitry Andric 5308fe6060f1SDimitry Andric if (!CarryIn) { 5309fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpO, {DstReg, CarryOut}, 5310fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i]}); 5311fe6060f1SDimitry Andric } else if (i == e - 1) { 5312fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpF, {DstReg, CarryOut}, 5313fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i], CarryIn}); 5314fe6060f1SDimitry Andric } else { 5315fe6060f1SDimitry Andric MIRBuilder.buildInstr(OpE, {DstReg, CarryOut}, 5316fe6060f1SDimitry Andric {Src1Regs[i], Src2Regs[i], CarryIn}); 5317fe6060f1SDimitry Andric } 5318fe6060f1SDimitry Andric 5319fe6060f1SDimitry Andric DstRegs.push_back(DstReg); 5320fe6060f1SDimitry Andric CarryIn = CarryOut; 5321fe6060f1SDimitry Andric } 5322fe6060f1SDimitry Andric insertParts(MI.getOperand(0).getReg(), RegTy, NarrowTy, 5323fe6060f1SDimitry Andric makeArrayRef(DstRegs).take_front(NarrowParts), LeftoverTy, 5324fe6060f1SDimitry Andric makeArrayRef(DstRegs).drop_front(NarrowParts)); 5325fe6060f1SDimitry Andric 5326fe6060f1SDimitry Andric MI.eraseFromParent(); 5327fe6060f1SDimitry Andric return Legalized; 5328fe6060f1SDimitry Andric } 5329fe6060f1SDimitry Andric 5330fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 53310b57cec5SDimitry Andric LegalizerHelper::narrowScalarMul(MachineInstr &MI, LLT NarrowTy) { 53320b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 53330b57cec5SDimitry Andric Register Src1 = MI.getOperand(1).getReg(); 53340b57cec5SDimitry Andric Register Src2 = MI.getOperand(2).getReg(); 53350b57cec5SDimitry Andric 53360b57cec5SDimitry Andric LLT Ty = MRI.getType(DstReg); 53370b57cec5SDimitry Andric if (Ty.isVector()) 53380b57cec5SDimitry Andric return UnableToLegalize; 53390b57cec5SDimitry Andric 5340349cc55cSDimitry Andric unsigned Size = Ty.getSizeInBits(); 53410b57cec5SDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 5342349cc55cSDimitry Andric if (Size % NarrowSize != 0) 53430b57cec5SDimitry Andric return UnableToLegalize; 53440b57cec5SDimitry Andric 5345349cc55cSDimitry Andric unsigned NumParts = Size / NarrowSize; 53460b57cec5SDimitry Andric bool IsMulHigh = MI.getOpcode() == TargetOpcode::G_UMULH; 5347349cc55cSDimitry Andric unsigned DstTmpParts = NumParts * (IsMulHigh ? 2 : 1); 53480b57cec5SDimitry Andric 53495ffd83dbSDimitry Andric SmallVector<Register, 2> Src1Parts, Src2Parts; 53505ffd83dbSDimitry Andric SmallVector<Register, 2> DstTmpRegs(DstTmpParts); 5351349cc55cSDimitry Andric extractParts(Src1, NarrowTy, NumParts, Src1Parts); 5352349cc55cSDimitry Andric extractParts(Src2, NarrowTy, NumParts, Src2Parts); 53530b57cec5SDimitry Andric multiplyRegisters(DstTmpRegs, Src1Parts, Src2Parts, NarrowTy); 53540b57cec5SDimitry Andric 53550b57cec5SDimitry Andric // Take only high half of registers if this is high mul. 5356349cc55cSDimitry Andric ArrayRef<Register> DstRegs(&DstTmpRegs[DstTmpParts - NumParts], NumParts); 53570b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 53580b57cec5SDimitry Andric MI.eraseFromParent(); 53590b57cec5SDimitry Andric return Legalized; 53600b57cec5SDimitry Andric } 53610b57cec5SDimitry Andric 53620b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 536323408297SDimitry Andric LegalizerHelper::narrowScalarFPTOI(MachineInstr &MI, unsigned TypeIdx, 536423408297SDimitry Andric LLT NarrowTy) { 536523408297SDimitry Andric if (TypeIdx != 0) 536623408297SDimitry Andric return UnableToLegalize; 536723408297SDimitry Andric 536823408297SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_FPTOSI; 536923408297SDimitry Andric 537023408297SDimitry Andric Register Src = MI.getOperand(1).getReg(); 537123408297SDimitry Andric LLT SrcTy = MRI.getType(Src); 537223408297SDimitry Andric 537323408297SDimitry Andric // If all finite floats fit into the narrowed integer type, we can just swap 537423408297SDimitry Andric // out the result type. This is practically only useful for conversions from 537523408297SDimitry Andric // half to at least 16-bits, so just handle the one case. 537623408297SDimitry Andric if (SrcTy.getScalarType() != LLT::scalar(16) || 5377fe6060f1SDimitry Andric NarrowTy.getScalarSizeInBits() < (IsSigned ? 17u : 16u)) 537823408297SDimitry Andric return UnableToLegalize; 537923408297SDimitry Andric 538023408297SDimitry Andric Observer.changingInstr(MI); 538123408297SDimitry Andric narrowScalarDst(MI, NarrowTy, 0, 538223408297SDimitry Andric IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT); 538323408297SDimitry Andric Observer.changedInstr(MI); 538423408297SDimitry Andric return Legalized; 538523408297SDimitry Andric } 538623408297SDimitry Andric 538723408297SDimitry Andric LegalizerHelper::LegalizeResult 53880b57cec5SDimitry Andric LegalizerHelper::narrowScalarExtract(MachineInstr &MI, unsigned TypeIdx, 53890b57cec5SDimitry Andric LLT NarrowTy) { 53900b57cec5SDimitry Andric if (TypeIdx != 1) 53910b57cec5SDimitry Andric return UnableToLegalize; 53920b57cec5SDimitry Andric 53930b57cec5SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 53940b57cec5SDimitry Andric 53950b57cec5SDimitry Andric int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 53960b57cec5SDimitry Andric // FIXME: add support for when SizeOp1 isn't an exact multiple of 53970b57cec5SDimitry Andric // NarrowSize. 53980b57cec5SDimitry Andric if (SizeOp1 % NarrowSize != 0) 53990b57cec5SDimitry Andric return UnableToLegalize; 54000b57cec5SDimitry Andric int NumParts = SizeOp1 / NarrowSize; 54010b57cec5SDimitry Andric 54020b57cec5SDimitry Andric SmallVector<Register, 2> SrcRegs, DstRegs; 54030b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 54040b57cec5SDimitry Andric extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); 54050b57cec5SDimitry Andric 54060b57cec5SDimitry Andric Register OpReg = MI.getOperand(0).getReg(); 54070b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(2).getImm(); 54080b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 54090b57cec5SDimitry Andric for (int i = 0; i < NumParts; ++i) { 54100b57cec5SDimitry Andric unsigned SrcStart = i * NarrowSize; 54110b57cec5SDimitry Andric 54120b57cec5SDimitry Andric if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) { 54130b57cec5SDimitry Andric // No part of the extract uses this subregister, ignore it. 54140b57cec5SDimitry Andric continue; 54150b57cec5SDimitry Andric } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 54160b57cec5SDimitry Andric // The entire subregister is extracted, forward the value. 54170b57cec5SDimitry Andric DstRegs.push_back(SrcRegs[i]); 54180b57cec5SDimitry Andric continue; 54190b57cec5SDimitry Andric } 54200b57cec5SDimitry Andric 54210b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 54220b57cec5SDimitry Andric // extended infinitely in both directions. 54230b57cec5SDimitry Andric int64_t ExtractOffset; 54240b57cec5SDimitry Andric uint64_t SegSize; 54250b57cec5SDimitry Andric if (OpStart < SrcStart) { 54260b57cec5SDimitry Andric ExtractOffset = 0; 54270b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart); 54280b57cec5SDimitry Andric } else { 54290b57cec5SDimitry Andric ExtractOffset = OpStart - SrcStart; 54300b57cec5SDimitry Andric SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize); 54310b57cec5SDimitry Andric } 54320b57cec5SDimitry Andric 54330b57cec5SDimitry Andric Register SegReg = SrcRegs[i]; 54340b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != NarrowSize) { 54350b57cec5SDimitry Andric // A genuine extract is needed. 54360b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 54370b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset); 54380b57cec5SDimitry Andric } 54390b57cec5SDimitry Andric 54400b57cec5SDimitry Andric DstRegs.push_back(SegReg); 54410b57cec5SDimitry Andric } 54420b57cec5SDimitry Andric 54430b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 54440b57cec5SDimitry Andric if (MRI.getType(DstReg).isVector()) 54450b57cec5SDimitry Andric MIRBuilder.buildBuildVector(DstReg, DstRegs); 54465ffd83dbSDimitry Andric else if (DstRegs.size() > 1) 54470b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 54485ffd83dbSDimitry Andric else 54495ffd83dbSDimitry Andric MIRBuilder.buildCopy(DstReg, DstRegs[0]); 54500b57cec5SDimitry Andric MI.eraseFromParent(); 54510b57cec5SDimitry Andric return Legalized; 54520b57cec5SDimitry Andric } 54530b57cec5SDimitry Andric 54540b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 54550b57cec5SDimitry Andric LegalizerHelper::narrowScalarInsert(MachineInstr &MI, unsigned TypeIdx, 54560b57cec5SDimitry Andric LLT NarrowTy) { 54570b57cec5SDimitry Andric // FIXME: Don't know how to handle secondary types yet. 54580b57cec5SDimitry Andric if (TypeIdx != 0) 54590b57cec5SDimitry Andric return UnableToLegalize; 54600b57cec5SDimitry Andric 5461fe6060f1SDimitry Andric SmallVector<Register, 2> SrcRegs, LeftoverRegs, DstRegs; 54620b57cec5SDimitry Andric SmallVector<uint64_t, 2> Indexes; 5463fe6060f1SDimitry Andric LLT RegTy = MRI.getType(MI.getOperand(0).getReg()); 5464fe6060f1SDimitry Andric LLT LeftoverTy; 5465fe6060f1SDimitry Andric extractParts(MI.getOperand(1).getReg(), RegTy, NarrowTy, LeftoverTy, SrcRegs, 5466fe6060f1SDimitry Andric LeftoverRegs); 54670b57cec5SDimitry Andric 5468fe6060f1SDimitry Andric for (Register Reg : LeftoverRegs) 5469fe6060f1SDimitry Andric SrcRegs.push_back(Reg); 5470fe6060f1SDimitry Andric 5471fe6060f1SDimitry Andric uint64_t NarrowSize = NarrowTy.getSizeInBits(); 54720b57cec5SDimitry Andric Register OpReg = MI.getOperand(2).getReg(); 54730b57cec5SDimitry Andric uint64_t OpStart = MI.getOperand(3).getImm(); 54740b57cec5SDimitry Andric uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); 5475fe6060f1SDimitry Andric for (int I = 0, E = SrcRegs.size(); I != E; ++I) { 5476fe6060f1SDimitry Andric unsigned DstStart = I * NarrowSize; 54770b57cec5SDimitry Andric 5478fe6060f1SDimitry Andric if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) { 54790b57cec5SDimitry Andric // The entire subregister is defined by this insert, forward the new 54800b57cec5SDimitry Andric // value. 54810b57cec5SDimitry Andric DstRegs.push_back(OpReg); 54820b57cec5SDimitry Andric continue; 54830b57cec5SDimitry Andric } 54840b57cec5SDimitry Andric 5485fe6060f1SDimitry Andric Register SrcReg = SrcRegs[I]; 5486fe6060f1SDimitry Andric if (MRI.getType(SrcRegs[I]) == LeftoverTy) { 5487fe6060f1SDimitry Andric // The leftover reg is smaller than NarrowTy, so we need to extend it. 5488fe6060f1SDimitry Andric SrcReg = MRI.createGenericVirtualRegister(NarrowTy); 5489fe6060f1SDimitry Andric MIRBuilder.buildAnyExt(SrcReg, SrcRegs[I]); 5490fe6060f1SDimitry Andric } 5491fe6060f1SDimitry Andric 5492fe6060f1SDimitry Andric if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) { 5493fe6060f1SDimitry Andric // No part of the insert affects this subregister, forward the original. 5494fe6060f1SDimitry Andric DstRegs.push_back(SrcReg); 5495fe6060f1SDimitry Andric continue; 5496fe6060f1SDimitry Andric } 5497fe6060f1SDimitry Andric 54980b57cec5SDimitry Andric // OpSegStart is where this destination segment would start in OpReg if it 54990b57cec5SDimitry Andric // extended infinitely in both directions. 55000b57cec5SDimitry Andric int64_t ExtractOffset, InsertOffset; 55010b57cec5SDimitry Andric uint64_t SegSize; 55020b57cec5SDimitry Andric if (OpStart < DstStart) { 55030b57cec5SDimitry Andric InsertOffset = 0; 55040b57cec5SDimitry Andric ExtractOffset = DstStart - OpStart; 55050b57cec5SDimitry Andric SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart); 55060b57cec5SDimitry Andric } else { 55070b57cec5SDimitry Andric InsertOffset = OpStart - DstStart; 55080b57cec5SDimitry Andric ExtractOffset = 0; 55090b57cec5SDimitry Andric SegSize = 55100b57cec5SDimitry Andric std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart); 55110b57cec5SDimitry Andric } 55120b57cec5SDimitry Andric 55130b57cec5SDimitry Andric Register SegReg = OpReg; 55140b57cec5SDimitry Andric if (ExtractOffset != 0 || SegSize != OpSize) { 55150b57cec5SDimitry Andric // A genuine extract is needed. 55160b57cec5SDimitry Andric SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); 55170b57cec5SDimitry Andric MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset); 55180b57cec5SDimitry Andric } 55190b57cec5SDimitry Andric 55200b57cec5SDimitry Andric Register DstReg = MRI.createGenericVirtualRegister(NarrowTy); 5521fe6060f1SDimitry Andric MIRBuilder.buildInsert(DstReg, SrcReg, SegReg, InsertOffset); 55220b57cec5SDimitry Andric DstRegs.push_back(DstReg); 55230b57cec5SDimitry Andric } 55240b57cec5SDimitry Andric 5525fe6060f1SDimitry Andric uint64_t WideSize = DstRegs.size() * NarrowSize; 55260b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 5527fe6060f1SDimitry Andric if (WideSize > RegTy.getSizeInBits()) { 5528fe6060f1SDimitry Andric Register MergeReg = MRI.createGenericVirtualRegister(LLT::scalar(WideSize)); 5529fe6060f1SDimitry Andric MIRBuilder.buildMerge(MergeReg, DstRegs); 5530fe6060f1SDimitry Andric MIRBuilder.buildTrunc(DstReg, MergeReg); 5531fe6060f1SDimitry Andric } else 55320b57cec5SDimitry Andric MIRBuilder.buildMerge(DstReg, DstRegs); 5533fe6060f1SDimitry Andric 55340b57cec5SDimitry Andric MI.eraseFromParent(); 55350b57cec5SDimitry Andric return Legalized; 55360b57cec5SDimitry Andric } 55370b57cec5SDimitry Andric 55380b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 55390b57cec5SDimitry Andric LegalizerHelper::narrowScalarBasic(MachineInstr &MI, unsigned TypeIdx, 55400b57cec5SDimitry Andric LLT NarrowTy) { 55410b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 55420b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 55430b57cec5SDimitry Andric 55440b57cec5SDimitry Andric assert(MI.getNumOperands() == 3 && TypeIdx == 0); 55450b57cec5SDimitry Andric 55460b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 55470b57cec5SDimitry Andric SmallVector<Register, 4> Src0Regs, Src0LeftoverRegs; 55480b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 55490b57cec5SDimitry Andric LLT LeftoverTy; 55500b57cec5SDimitry Andric if (!extractParts(MI.getOperand(1).getReg(), DstTy, NarrowTy, LeftoverTy, 55510b57cec5SDimitry Andric Src0Regs, Src0LeftoverRegs)) 55520b57cec5SDimitry Andric return UnableToLegalize; 55530b57cec5SDimitry Andric 55540b57cec5SDimitry Andric LLT Unused; 55550b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, Unused, 55560b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 55570b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 55580b57cec5SDimitry Andric 55590b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 55600b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, 55610b57cec5SDimitry Andric {Src0Regs[I], Src1Regs[I]}); 55625ffd83dbSDimitry Andric DstRegs.push_back(Inst.getReg(0)); 55630b57cec5SDimitry Andric } 55640b57cec5SDimitry Andric 55650b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 55660b57cec5SDimitry Andric auto Inst = MIRBuilder.buildInstr( 55670b57cec5SDimitry Andric MI.getOpcode(), 55680b57cec5SDimitry Andric {LeftoverTy}, {Src0LeftoverRegs[I], Src1LeftoverRegs[I]}); 55695ffd83dbSDimitry Andric DstLeftoverRegs.push_back(Inst.getReg(0)); 55700b57cec5SDimitry Andric } 55710b57cec5SDimitry Andric 55720b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 55730b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 55740b57cec5SDimitry Andric 55750b57cec5SDimitry Andric MI.eraseFromParent(); 55760b57cec5SDimitry Andric return Legalized; 55770b57cec5SDimitry Andric } 55780b57cec5SDimitry Andric 55790b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 55805ffd83dbSDimitry Andric LegalizerHelper::narrowScalarExt(MachineInstr &MI, unsigned TypeIdx, 55815ffd83dbSDimitry Andric LLT NarrowTy) { 55825ffd83dbSDimitry Andric if (TypeIdx != 0) 55835ffd83dbSDimitry Andric return UnableToLegalize; 55845ffd83dbSDimitry Andric 55855ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 55865ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 55875ffd83dbSDimitry Andric 55885ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 55895ffd83dbSDimitry Andric if (DstTy.isVector()) 55905ffd83dbSDimitry Andric return UnableToLegalize; 55915ffd83dbSDimitry Andric 55925ffd83dbSDimitry Andric SmallVector<Register, 8> Parts; 55935ffd83dbSDimitry Andric LLT GCDTy = extractGCDType(Parts, DstTy, NarrowTy, SrcReg); 55945ffd83dbSDimitry Andric LLT LCMTy = buildLCMMergePieces(DstTy, NarrowTy, GCDTy, Parts, MI.getOpcode()); 55955ffd83dbSDimitry Andric buildWidenedRemergeToDst(DstReg, LCMTy, Parts); 55965ffd83dbSDimitry Andric 55975ffd83dbSDimitry Andric MI.eraseFromParent(); 55985ffd83dbSDimitry Andric return Legalized; 55995ffd83dbSDimitry Andric } 56005ffd83dbSDimitry Andric 56015ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 56020b57cec5SDimitry Andric LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx, 56030b57cec5SDimitry Andric LLT NarrowTy) { 56040b57cec5SDimitry Andric if (TypeIdx != 0) 56050b57cec5SDimitry Andric return UnableToLegalize; 56060b57cec5SDimitry Andric 56070b57cec5SDimitry Andric Register CondReg = MI.getOperand(1).getReg(); 56080b57cec5SDimitry Andric LLT CondTy = MRI.getType(CondReg); 56090b57cec5SDimitry Andric if (CondTy.isVector()) // TODO: Handle vselect 56100b57cec5SDimitry Andric return UnableToLegalize; 56110b57cec5SDimitry Andric 56120b57cec5SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 56130b57cec5SDimitry Andric LLT DstTy = MRI.getType(DstReg); 56140b57cec5SDimitry Andric 56150b57cec5SDimitry Andric SmallVector<Register, 4> DstRegs, DstLeftoverRegs; 56160b57cec5SDimitry Andric SmallVector<Register, 4> Src1Regs, Src1LeftoverRegs; 56170b57cec5SDimitry Andric SmallVector<Register, 4> Src2Regs, Src2LeftoverRegs; 56180b57cec5SDimitry Andric LLT LeftoverTy; 56190b57cec5SDimitry Andric if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy, 56200b57cec5SDimitry Andric Src1Regs, Src1LeftoverRegs)) 56210b57cec5SDimitry Andric return UnableToLegalize; 56220b57cec5SDimitry Andric 56230b57cec5SDimitry Andric LLT Unused; 56240b57cec5SDimitry Andric if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused, 56250b57cec5SDimitry Andric Src2Regs, Src2LeftoverRegs)) 56260b57cec5SDimitry Andric llvm_unreachable("inconsistent extractParts result"); 56270b57cec5SDimitry Andric 56280b57cec5SDimitry Andric for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { 56290b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect(NarrowTy, 56300b57cec5SDimitry Andric CondReg, Src1Regs[I], Src2Regs[I]); 56315ffd83dbSDimitry Andric DstRegs.push_back(Select.getReg(0)); 56320b57cec5SDimitry Andric } 56330b57cec5SDimitry Andric 56340b57cec5SDimitry Andric for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { 56350b57cec5SDimitry Andric auto Select = MIRBuilder.buildSelect( 56360b57cec5SDimitry Andric LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]); 56375ffd83dbSDimitry Andric DstLeftoverRegs.push_back(Select.getReg(0)); 56380b57cec5SDimitry Andric } 56390b57cec5SDimitry Andric 56400b57cec5SDimitry Andric insertParts(DstReg, DstTy, NarrowTy, DstRegs, 56410b57cec5SDimitry Andric LeftoverTy, DstLeftoverRegs); 56420b57cec5SDimitry Andric 56430b57cec5SDimitry Andric MI.eraseFromParent(); 56440b57cec5SDimitry Andric return Legalized; 56450b57cec5SDimitry Andric } 56460b57cec5SDimitry Andric 56470b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 56485ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTLZ(MachineInstr &MI, unsigned TypeIdx, 56495ffd83dbSDimitry Andric LLT NarrowTy) { 56505ffd83dbSDimitry Andric if (TypeIdx != 1) 56515ffd83dbSDimitry Andric return UnableToLegalize; 56525ffd83dbSDimitry Andric 56535ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 56545ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 56555ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 56565ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 56575ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 56585ffd83dbSDimitry Andric 56595ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 56605ffd83dbSDimitry Andric const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF; 56615ffd83dbSDimitry Andric 56625ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 56635ffd83dbSDimitry Andric auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg); 56645ffd83dbSDimitry Andric // ctlz(Hi:Lo) -> Hi == 0 ? (NarrowSize + ctlz(Lo)) : ctlz(Hi) 56655ffd83dbSDimitry Andric auto C_0 = B.buildConstant(NarrowTy, 0); 56665ffd83dbSDimitry Andric auto HiIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 56675ffd83dbSDimitry Andric UnmergeSrc.getReg(1), C_0); 56685ffd83dbSDimitry Andric auto LoCTLZ = IsUndef ? 56695ffd83dbSDimitry Andric B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0)) : 56705ffd83dbSDimitry Andric B.buildCTLZ(DstTy, UnmergeSrc.getReg(0)); 56715ffd83dbSDimitry Andric auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize); 56725ffd83dbSDimitry Andric auto HiIsZeroCTLZ = B.buildAdd(DstTy, LoCTLZ, C_NarrowSize); 56735ffd83dbSDimitry Andric auto HiCTLZ = B.buildCTLZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1)); 56745ffd83dbSDimitry Andric B.buildSelect(DstReg, HiIsZero, HiIsZeroCTLZ, HiCTLZ); 56755ffd83dbSDimitry Andric 56765ffd83dbSDimitry Andric MI.eraseFromParent(); 56775ffd83dbSDimitry Andric return Legalized; 56785ffd83dbSDimitry Andric } 56795ffd83dbSDimitry Andric 56805ffd83dbSDimitry Andric return UnableToLegalize; 56815ffd83dbSDimitry Andric } 56825ffd83dbSDimitry Andric 56835ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 56845ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTTZ(MachineInstr &MI, unsigned TypeIdx, 56855ffd83dbSDimitry Andric LLT NarrowTy) { 56865ffd83dbSDimitry Andric if (TypeIdx != 1) 56875ffd83dbSDimitry Andric return UnableToLegalize; 56885ffd83dbSDimitry Andric 56895ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 56905ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 56915ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 56925ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 56935ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 56945ffd83dbSDimitry Andric 56955ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 56965ffd83dbSDimitry Andric const bool IsUndef = MI.getOpcode() == TargetOpcode::G_CTTZ_ZERO_UNDEF; 56975ffd83dbSDimitry Andric 56985ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 56995ffd83dbSDimitry Andric auto UnmergeSrc = B.buildUnmerge(NarrowTy, SrcReg); 57005ffd83dbSDimitry Andric // cttz(Hi:Lo) -> Lo == 0 ? (cttz(Hi) + NarrowSize) : cttz(Lo) 57015ffd83dbSDimitry Andric auto C_0 = B.buildConstant(NarrowTy, 0); 57025ffd83dbSDimitry Andric auto LoIsZero = B.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), 57035ffd83dbSDimitry Andric UnmergeSrc.getReg(0), C_0); 57045ffd83dbSDimitry Andric auto HiCTTZ = IsUndef ? 57055ffd83dbSDimitry Andric B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(1)) : 57065ffd83dbSDimitry Andric B.buildCTTZ(DstTy, UnmergeSrc.getReg(1)); 57075ffd83dbSDimitry Andric auto C_NarrowSize = B.buildConstant(DstTy, NarrowSize); 57085ffd83dbSDimitry Andric auto LoIsZeroCTTZ = B.buildAdd(DstTy, HiCTTZ, C_NarrowSize); 57095ffd83dbSDimitry Andric auto LoCTTZ = B.buildCTTZ_ZERO_UNDEF(DstTy, UnmergeSrc.getReg(0)); 57105ffd83dbSDimitry Andric B.buildSelect(DstReg, LoIsZero, LoIsZeroCTTZ, LoCTTZ); 57115ffd83dbSDimitry Andric 57125ffd83dbSDimitry Andric MI.eraseFromParent(); 57135ffd83dbSDimitry Andric return Legalized; 57145ffd83dbSDimitry Andric } 57155ffd83dbSDimitry Andric 57165ffd83dbSDimitry Andric return UnableToLegalize; 57175ffd83dbSDimitry Andric } 57185ffd83dbSDimitry Andric 57195ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 57205ffd83dbSDimitry Andric LegalizerHelper::narrowScalarCTPOP(MachineInstr &MI, unsigned TypeIdx, 57215ffd83dbSDimitry Andric LLT NarrowTy) { 57225ffd83dbSDimitry Andric if (TypeIdx != 1) 57235ffd83dbSDimitry Andric return UnableToLegalize; 57245ffd83dbSDimitry Andric 57255ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 57265ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 57275ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(MI.getOperand(1).getReg()); 57285ffd83dbSDimitry Andric unsigned NarrowSize = NarrowTy.getSizeInBits(); 57295ffd83dbSDimitry Andric 57305ffd83dbSDimitry Andric if (SrcTy.isScalar() && SrcTy.getSizeInBits() == 2 * NarrowSize) { 57315ffd83dbSDimitry Andric auto UnmergeSrc = MIRBuilder.buildUnmerge(NarrowTy, MI.getOperand(1)); 57325ffd83dbSDimitry Andric 57335ffd83dbSDimitry Andric auto LoCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(0)); 57345ffd83dbSDimitry Andric auto HiCTPOP = MIRBuilder.buildCTPOP(DstTy, UnmergeSrc.getReg(1)); 57355ffd83dbSDimitry Andric MIRBuilder.buildAdd(DstReg, HiCTPOP, LoCTPOP); 57365ffd83dbSDimitry Andric 57375ffd83dbSDimitry Andric MI.eraseFromParent(); 57385ffd83dbSDimitry Andric return Legalized; 57395ffd83dbSDimitry Andric } 57405ffd83dbSDimitry Andric 57415ffd83dbSDimitry Andric return UnableToLegalize; 57425ffd83dbSDimitry Andric } 57435ffd83dbSDimitry Andric 57445ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 5745e8d8bef9SDimitry Andric LegalizerHelper::lowerBitCount(MachineInstr &MI) { 57460b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 5747e8d8bef9SDimitry Andric const auto &TII = MIRBuilder.getTII(); 57480b57cec5SDimitry Andric auto isSupported = [this](const LegalityQuery &Q) { 57490b57cec5SDimitry Andric auto QAction = LI.getAction(Q).Action; 57500b57cec5SDimitry Andric return QAction == Legal || QAction == Libcall || QAction == Custom; 57510b57cec5SDimitry Andric }; 57520b57cec5SDimitry Andric switch (Opc) { 57530b57cec5SDimitry Andric default: 57540b57cec5SDimitry Andric return UnableToLegalize; 57550b57cec5SDimitry Andric case TargetOpcode::G_CTLZ_ZERO_UNDEF: { 57560b57cec5SDimitry Andric // This trivially expands to CTLZ. 57570b57cec5SDimitry Andric Observer.changingInstr(MI); 57580b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTLZ)); 57590b57cec5SDimitry Andric Observer.changedInstr(MI); 57600b57cec5SDimitry Andric return Legalized; 57610b57cec5SDimitry Andric } 57620b57cec5SDimitry Andric case TargetOpcode::G_CTLZ: { 57635ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 57640b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 57655ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 57665ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 57675ffd83dbSDimitry Andric unsigned Len = SrcTy.getSizeInBits(); 57685ffd83dbSDimitry Andric 57695ffd83dbSDimitry Andric if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {DstTy, SrcTy}})) { 57700b57cec5SDimitry Andric // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero. 57715ffd83dbSDimitry Andric auto CtlzZU = MIRBuilder.buildCTLZ_ZERO_UNDEF(DstTy, SrcReg); 57725ffd83dbSDimitry Andric auto ZeroSrc = MIRBuilder.buildConstant(SrcTy, 0); 57735ffd83dbSDimitry Andric auto ICmp = MIRBuilder.buildICmp( 57745ffd83dbSDimitry Andric CmpInst::ICMP_EQ, SrcTy.changeElementSize(1), SrcReg, ZeroSrc); 57755ffd83dbSDimitry Andric auto LenConst = MIRBuilder.buildConstant(DstTy, Len); 57765ffd83dbSDimitry Andric MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CtlzZU); 57770b57cec5SDimitry Andric MI.eraseFromParent(); 57780b57cec5SDimitry Andric return Legalized; 57790b57cec5SDimitry Andric } 57800b57cec5SDimitry Andric // for now, we do this: 57810b57cec5SDimitry Andric // NewLen = NextPowerOf2(Len); 57820b57cec5SDimitry Andric // x = x | (x >> 1); 57830b57cec5SDimitry Andric // x = x | (x >> 2); 57840b57cec5SDimitry Andric // ... 57850b57cec5SDimitry Andric // x = x | (x >>16); 57860b57cec5SDimitry Andric // x = x | (x >>32); // for 64-bit input 57870b57cec5SDimitry Andric // Upto NewLen/2 57880b57cec5SDimitry Andric // return Len - popcount(x); 57890b57cec5SDimitry Andric // 57900b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 57910b57cec5SDimitry Andric Register Op = SrcReg; 57920b57cec5SDimitry Andric unsigned NewLen = PowerOf2Ceil(Len); 57930b57cec5SDimitry Andric for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) { 57945ffd83dbSDimitry Andric auto MIBShiftAmt = MIRBuilder.buildConstant(SrcTy, 1ULL << i); 57955ffd83dbSDimitry Andric auto MIBOp = MIRBuilder.buildOr( 57965ffd83dbSDimitry Andric SrcTy, Op, MIRBuilder.buildLShr(SrcTy, Op, MIBShiftAmt)); 57975ffd83dbSDimitry Andric Op = MIBOp.getReg(0); 57980b57cec5SDimitry Andric } 57995ffd83dbSDimitry Andric auto MIBPop = MIRBuilder.buildCTPOP(DstTy, Op); 58005ffd83dbSDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MIRBuilder.buildConstant(DstTy, Len), 58015ffd83dbSDimitry Andric MIBPop); 58020b57cec5SDimitry Andric MI.eraseFromParent(); 58030b57cec5SDimitry Andric return Legalized; 58040b57cec5SDimitry Andric } 58050b57cec5SDimitry Andric case TargetOpcode::G_CTTZ_ZERO_UNDEF: { 58060b57cec5SDimitry Andric // This trivially expands to CTTZ. 58070b57cec5SDimitry Andric Observer.changingInstr(MI); 58080b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTTZ)); 58090b57cec5SDimitry Andric Observer.changedInstr(MI); 58100b57cec5SDimitry Andric return Legalized; 58110b57cec5SDimitry Andric } 58120b57cec5SDimitry Andric case TargetOpcode::G_CTTZ: { 58135ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 58140b57cec5SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 58155ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 58165ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 58175ffd83dbSDimitry Andric 58185ffd83dbSDimitry Andric unsigned Len = SrcTy.getSizeInBits(); 58195ffd83dbSDimitry Andric if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {DstTy, SrcTy}})) { 58200b57cec5SDimitry Andric // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with 58210b57cec5SDimitry Andric // zero. 58225ffd83dbSDimitry Andric auto CttzZU = MIRBuilder.buildCTTZ_ZERO_UNDEF(DstTy, SrcReg); 58235ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildConstant(SrcTy, 0); 58245ffd83dbSDimitry Andric auto ICmp = MIRBuilder.buildICmp( 58255ffd83dbSDimitry Andric CmpInst::ICMP_EQ, DstTy.changeElementSize(1), SrcReg, Zero); 58265ffd83dbSDimitry Andric auto LenConst = MIRBuilder.buildConstant(DstTy, Len); 58275ffd83dbSDimitry Andric MIRBuilder.buildSelect(DstReg, ICmp, LenConst, CttzZU); 58280b57cec5SDimitry Andric MI.eraseFromParent(); 58290b57cec5SDimitry Andric return Legalized; 58300b57cec5SDimitry Andric } 58310b57cec5SDimitry Andric // for now, we use: { return popcount(~x & (x - 1)); } 58320b57cec5SDimitry Andric // unless the target has ctlz but not ctpop, in which case we use: 58330b57cec5SDimitry Andric // { return 32 - nlz(~x & (x-1)); } 58340b57cec5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren 5835e8d8bef9SDimitry Andric auto MIBCstNeg1 = MIRBuilder.buildConstant(SrcTy, -1); 5836e8d8bef9SDimitry Andric auto MIBNot = MIRBuilder.buildXor(SrcTy, SrcReg, MIBCstNeg1); 58375ffd83dbSDimitry Andric auto MIBTmp = MIRBuilder.buildAnd( 5838e8d8bef9SDimitry Andric SrcTy, MIBNot, MIRBuilder.buildAdd(SrcTy, SrcReg, MIBCstNeg1)); 5839e8d8bef9SDimitry Andric if (!isSupported({TargetOpcode::G_CTPOP, {SrcTy, SrcTy}}) && 5840e8d8bef9SDimitry Andric isSupported({TargetOpcode::G_CTLZ, {SrcTy, SrcTy}})) { 5841e8d8bef9SDimitry Andric auto MIBCstLen = MIRBuilder.buildConstant(SrcTy, Len); 58425ffd83dbSDimitry Andric MIRBuilder.buildSub(MI.getOperand(0), MIBCstLen, 5843e8d8bef9SDimitry Andric MIRBuilder.buildCTLZ(SrcTy, MIBTmp)); 58440b57cec5SDimitry Andric MI.eraseFromParent(); 58450b57cec5SDimitry Andric return Legalized; 58460b57cec5SDimitry Andric } 58470b57cec5SDimitry Andric MI.setDesc(TII.get(TargetOpcode::G_CTPOP)); 58485ffd83dbSDimitry Andric MI.getOperand(1).setReg(MIBTmp.getReg(0)); 58495ffd83dbSDimitry Andric return Legalized; 58505ffd83dbSDimitry Andric } 58515ffd83dbSDimitry Andric case TargetOpcode::G_CTPOP: { 5852e8d8bef9SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 5853e8d8bef9SDimitry Andric LLT Ty = MRI.getType(SrcReg); 58545ffd83dbSDimitry Andric unsigned Size = Ty.getSizeInBits(); 58555ffd83dbSDimitry Andric MachineIRBuilder &B = MIRBuilder; 58565ffd83dbSDimitry Andric 58575ffd83dbSDimitry Andric // Count set bits in blocks of 2 bits. Default approach would be 58585ffd83dbSDimitry Andric // B2Count = { val & 0x55555555 } + { (val >> 1) & 0x55555555 } 58595ffd83dbSDimitry Andric // We use following formula instead: 58605ffd83dbSDimitry Andric // B2Count = val - { (val >> 1) & 0x55555555 } 58615ffd83dbSDimitry Andric // since it gives same result in blocks of 2 with one instruction less. 58625ffd83dbSDimitry Andric auto C_1 = B.buildConstant(Ty, 1); 5863e8d8bef9SDimitry Andric auto B2Set1LoTo1Hi = B.buildLShr(Ty, SrcReg, C_1); 58645ffd83dbSDimitry Andric APInt B2Mask1HiTo0 = APInt::getSplat(Size, APInt(8, 0x55)); 58655ffd83dbSDimitry Andric auto C_B2Mask1HiTo0 = B.buildConstant(Ty, B2Mask1HiTo0); 58665ffd83dbSDimitry Andric auto B2Count1Hi = B.buildAnd(Ty, B2Set1LoTo1Hi, C_B2Mask1HiTo0); 5867e8d8bef9SDimitry Andric auto B2Count = B.buildSub(Ty, SrcReg, B2Count1Hi); 58685ffd83dbSDimitry Andric 58695ffd83dbSDimitry Andric // In order to get count in blocks of 4 add values from adjacent block of 2. 58705ffd83dbSDimitry Andric // B4Count = { B2Count & 0x33333333 } + { (B2Count >> 2) & 0x33333333 } 58715ffd83dbSDimitry Andric auto C_2 = B.buildConstant(Ty, 2); 58725ffd83dbSDimitry Andric auto B4Set2LoTo2Hi = B.buildLShr(Ty, B2Count, C_2); 58735ffd83dbSDimitry Andric APInt B4Mask2HiTo0 = APInt::getSplat(Size, APInt(8, 0x33)); 58745ffd83dbSDimitry Andric auto C_B4Mask2HiTo0 = B.buildConstant(Ty, B4Mask2HiTo0); 58755ffd83dbSDimitry Andric auto B4HiB2Count = B.buildAnd(Ty, B4Set2LoTo2Hi, C_B4Mask2HiTo0); 58765ffd83dbSDimitry Andric auto B4LoB2Count = B.buildAnd(Ty, B2Count, C_B4Mask2HiTo0); 58775ffd83dbSDimitry Andric auto B4Count = B.buildAdd(Ty, B4HiB2Count, B4LoB2Count); 58785ffd83dbSDimitry Andric 58795ffd83dbSDimitry Andric // For count in blocks of 8 bits we don't have to mask high 4 bits before 58805ffd83dbSDimitry Andric // addition since count value sits in range {0,...,8} and 4 bits are enough 58815ffd83dbSDimitry Andric // to hold such binary values. After addition high 4 bits still hold count 58825ffd83dbSDimitry Andric // of set bits in high 4 bit block, set them to zero and get 8 bit result. 58835ffd83dbSDimitry Andric // B8Count = { B4Count + (B4Count >> 4) } & 0x0F0F0F0F 58845ffd83dbSDimitry Andric auto C_4 = B.buildConstant(Ty, 4); 58855ffd83dbSDimitry Andric auto B8HiB4Count = B.buildLShr(Ty, B4Count, C_4); 58865ffd83dbSDimitry Andric auto B8CountDirty4Hi = B.buildAdd(Ty, B8HiB4Count, B4Count); 58875ffd83dbSDimitry Andric APInt B8Mask4HiTo0 = APInt::getSplat(Size, APInt(8, 0x0F)); 58885ffd83dbSDimitry Andric auto C_B8Mask4HiTo0 = B.buildConstant(Ty, B8Mask4HiTo0); 58895ffd83dbSDimitry Andric auto B8Count = B.buildAnd(Ty, B8CountDirty4Hi, C_B8Mask4HiTo0); 58905ffd83dbSDimitry Andric 58915ffd83dbSDimitry Andric assert(Size<=128 && "Scalar size is too large for CTPOP lower algorithm"); 58925ffd83dbSDimitry Andric // 8 bits can hold CTPOP result of 128 bit int or smaller. Mul with this 58935ffd83dbSDimitry Andric // bitmask will set 8 msb in ResTmp to sum of all B8Counts in 8 bit blocks. 58945ffd83dbSDimitry Andric auto MulMask = B.buildConstant(Ty, APInt::getSplat(Size, APInt(8, 0x01))); 58955ffd83dbSDimitry Andric auto ResTmp = B.buildMul(Ty, B8Count, MulMask); 58965ffd83dbSDimitry Andric 58975ffd83dbSDimitry Andric // Shift count result from 8 high bits to low bits. 58985ffd83dbSDimitry Andric auto C_SizeM8 = B.buildConstant(Ty, Size - 8); 58995ffd83dbSDimitry Andric B.buildLShr(MI.getOperand(0).getReg(), ResTmp, C_SizeM8); 59005ffd83dbSDimitry Andric 59015ffd83dbSDimitry Andric MI.eraseFromParent(); 59020b57cec5SDimitry Andric return Legalized; 59030b57cec5SDimitry Andric } 59040b57cec5SDimitry Andric } 59050b57cec5SDimitry Andric } 59060b57cec5SDimitry Andric 5907fe6060f1SDimitry Andric // Check that (every element of) Reg is undef or not an exact multiple of BW. 5908fe6060f1SDimitry Andric static bool isNonZeroModBitWidthOrUndef(const MachineRegisterInfo &MRI, 5909fe6060f1SDimitry Andric Register Reg, unsigned BW) { 5910fe6060f1SDimitry Andric return matchUnaryPredicate( 5911fe6060f1SDimitry Andric MRI, Reg, 5912fe6060f1SDimitry Andric [=](const Constant *C) { 5913fe6060f1SDimitry Andric // Null constant here means an undef. 5914fe6060f1SDimitry Andric const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C); 5915fe6060f1SDimitry Andric return !CI || CI->getValue().urem(BW) != 0; 5916fe6060f1SDimitry Andric }, 5917fe6060f1SDimitry Andric /*AllowUndefs*/ true); 5918fe6060f1SDimitry Andric } 5919fe6060f1SDimitry Andric 5920fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5921fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShiftWithInverse(MachineInstr &MI) { 5922fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5923fe6060f1SDimitry Andric Register X = MI.getOperand(1).getReg(); 5924fe6060f1SDimitry Andric Register Y = MI.getOperand(2).getReg(); 5925fe6060f1SDimitry Andric Register Z = MI.getOperand(3).getReg(); 5926fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 5927fe6060f1SDimitry Andric LLT ShTy = MRI.getType(Z); 5928fe6060f1SDimitry Andric 5929fe6060f1SDimitry Andric unsigned BW = Ty.getScalarSizeInBits(); 5930fe6060f1SDimitry Andric 5931fe6060f1SDimitry Andric if (!isPowerOf2_32(BW)) 5932fe6060f1SDimitry Andric return UnableToLegalize; 5933fe6060f1SDimitry Andric 5934fe6060f1SDimitry Andric const bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 5935fe6060f1SDimitry Andric unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; 5936fe6060f1SDimitry Andric 5937fe6060f1SDimitry Andric if (isNonZeroModBitWidthOrUndef(MRI, Z, BW)) { 5938fe6060f1SDimitry Andric // fshl X, Y, Z -> fshr X, Y, -Z 5939fe6060f1SDimitry Andric // fshr X, Y, Z -> fshl X, Y, -Z 5940fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(ShTy, 0); 5941fe6060f1SDimitry Andric Z = MIRBuilder.buildSub(Ty, Zero, Z).getReg(0); 5942fe6060f1SDimitry Andric } else { 5943fe6060f1SDimitry Andric // fshl X, Y, Z -> fshr (srl X, 1), (fshr X, Y, 1), ~Z 5944fe6060f1SDimitry Andric // fshr X, Y, Z -> fshl (fshl X, Y, 1), (shl Y, 1), ~Z 5945fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(ShTy, 1); 5946fe6060f1SDimitry Andric if (IsFSHL) { 5947fe6060f1SDimitry Andric Y = MIRBuilder.buildInstr(RevOpcode, {Ty}, {X, Y, One}).getReg(0); 5948fe6060f1SDimitry Andric X = MIRBuilder.buildLShr(Ty, X, One).getReg(0); 5949fe6060f1SDimitry Andric } else { 5950fe6060f1SDimitry Andric X = MIRBuilder.buildInstr(RevOpcode, {Ty}, {X, Y, One}).getReg(0); 5951fe6060f1SDimitry Andric Y = MIRBuilder.buildShl(Ty, Y, One).getReg(0); 5952fe6060f1SDimitry Andric } 5953fe6060f1SDimitry Andric 5954fe6060f1SDimitry Andric Z = MIRBuilder.buildNot(ShTy, Z).getReg(0); 5955fe6060f1SDimitry Andric } 5956fe6060f1SDimitry Andric 5957fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevOpcode, {Dst}, {X, Y, Z}); 5958fe6060f1SDimitry Andric MI.eraseFromParent(); 5959fe6060f1SDimitry Andric return Legalized; 5960fe6060f1SDimitry Andric } 5961fe6060f1SDimitry Andric 5962fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 5963fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShiftAsShifts(MachineInstr &MI) { 5964fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 5965fe6060f1SDimitry Andric Register X = MI.getOperand(1).getReg(); 5966fe6060f1SDimitry Andric Register Y = MI.getOperand(2).getReg(); 5967fe6060f1SDimitry Andric Register Z = MI.getOperand(3).getReg(); 5968fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 5969fe6060f1SDimitry Andric LLT ShTy = MRI.getType(Z); 5970fe6060f1SDimitry Andric 5971fe6060f1SDimitry Andric const unsigned BW = Ty.getScalarSizeInBits(); 5972fe6060f1SDimitry Andric const bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 5973fe6060f1SDimitry Andric 5974fe6060f1SDimitry Andric Register ShX, ShY; 5975fe6060f1SDimitry Andric Register ShAmt, InvShAmt; 5976fe6060f1SDimitry Andric 5977fe6060f1SDimitry Andric // FIXME: Emit optimized urem by constant instead of letting it expand later. 5978fe6060f1SDimitry Andric if (isNonZeroModBitWidthOrUndef(MRI, Z, BW)) { 5979fe6060f1SDimitry Andric // fshl: X << C | Y >> (BW - C) 5980fe6060f1SDimitry Andric // fshr: X << (BW - C) | Y >> C 5981fe6060f1SDimitry Andric // where C = Z % BW is not zero 5982fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(ShTy, BW); 5983fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildURem(ShTy, Z, BitWidthC).getReg(0); 5984fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildSub(ShTy, BitWidthC, ShAmt).getReg(0); 5985fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, X, IsFSHL ? ShAmt : InvShAmt).getReg(0); 5986fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, Y, IsFSHL ? InvShAmt : ShAmt).getReg(0); 5987fe6060f1SDimitry Andric } else { 5988fe6060f1SDimitry Andric // fshl: X << (Z % BW) | Y >> 1 >> (BW - 1 - (Z % BW)) 5989fe6060f1SDimitry Andric // fshr: X << 1 << (BW - 1 - (Z % BW)) | Y >> (Z % BW) 5990fe6060f1SDimitry Andric auto Mask = MIRBuilder.buildConstant(ShTy, BW - 1); 5991fe6060f1SDimitry Andric if (isPowerOf2_32(BW)) { 5992fe6060f1SDimitry Andric // Z % BW -> Z & (BW - 1) 5993fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildAnd(ShTy, Z, Mask).getReg(0); 5994fe6060f1SDimitry Andric // (BW - 1) - (Z % BW) -> ~Z & (BW - 1) 5995fe6060f1SDimitry Andric auto NotZ = MIRBuilder.buildNot(ShTy, Z); 5996fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildAnd(ShTy, NotZ, Mask).getReg(0); 5997fe6060f1SDimitry Andric } else { 5998fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(ShTy, BW); 5999fe6060f1SDimitry Andric ShAmt = MIRBuilder.buildURem(ShTy, Z, BitWidthC).getReg(0); 6000fe6060f1SDimitry Andric InvShAmt = MIRBuilder.buildSub(ShTy, Mask, ShAmt).getReg(0); 6001fe6060f1SDimitry Andric } 6002fe6060f1SDimitry Andric 6003fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(ShTy, 1); 6004fe6060f1SDimitry Andric if (IsFSHL) { 6005fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, X, ShAmt).getReg(0); 6006fe6060f1SDimitry Andric auto ShY1 = MIRBuilder.buildLShr(Ty, Y, One); 6007fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, ShY1, InvShAmt).getReg(0); 6008fe6060f1SDimitry Andric } else { 6009fe6060f1SDimitry Andric auto ShX1 = MIRBuilder.buildShl(Ty, X, One); 6010fe6060f1SDimitry Andric ShX = MIRBuilder.buildShl(Ty, ShX1, InvShAmt).getReg(0); 6011fe6060f1SDimitry Andric ShY = MIRBuilder.buildLShr(Ty, Y, ShAmt).getReg(0); 6012fe6060f1SDimitry Andric } 6013fe6060f1SDimitry Andric } 6014fe6060f1SDimitry Andric 6015fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, ShX, ShY); 6016fe6060f1SDimitry Andric MI.eraseFromParent(); 6017fe6060f1SDimitry Andric return Legalized; 6018fe6060f1SDimitry Andric } 6019fe6060f1SDimitry Andric 6020fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 6021fe6060f1SDimitry Andric LegalizerHelper::lowerFunnelShift(MachineInstr &MI) { 6022fe6060f1SDimitry Andric // These operations approximately do the following (while avoiding undefined 6023fe6060f1SDimitry Andric // shifts by BW): 6024fe6060f1SDimitry Andric // G_FSHL: (X << (Z % BW)) | (Y >> (BW - (Z % BW))) 6025fe6060f1SDimitry Andric // G_FSHR: (X << (BW - (Z % BW))) | (Y >> (Z % BW)) 6026fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 6027fe6060f1SDimitry Andric LLT Ty = MRI.getType(Dst); 6028fe6060f1SDimitry Andric LLT ShTy = MRI.getType(MI.getOperand(3).getReg()); 6029fe6060f1SDimitry Andric 6030fe6060f1SDimitry Andric bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; 6031fe6060f1SDimitry Andric unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; 6032fe6060f1SDimitry Andric 6033fe6060f1SDimitry Andric // TODO: Use smarter heuristic that accounts for vector legalization. 6034fe6060f1SDimitry Andric if (LI.getAction({RevOpcode, {Ty, ShTy}}).Action == Lower) 6035fe6060f1SDimitry Andric return lowerFunnelShiftAsShifts(MI); 6036fe6060f1SDimitry Andric 6037fe6060f1SDimitry Andric // This only works for powers of 2, fallback to shifts if it fails. 6038fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult Result = lowerFunnelShiftWithInverse(MI); 6039fe6060f1SDimitry Andric if (Result == UnableToLegalize) 6040fe6060f1SDimitry Andric return lowerFunnelShiftAsShifts(MI); 6041fe6060f1SDimitry Andric return Result; 6042fe6060f1SDimitry Andric } 6043fe6060f1SDimitry Andric 6044fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 6045fe6060f1SDimitry Andric LegalizerHelper::lowerRotateWithReverseRotate(MachineInstr &MI) { 6046fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 6047fe6060f1SDimitry Andric Register Src = MI.getOperand(1).getReg(); 6048fe6060f1SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 6049fe6060f1SDimitry Andric LLT AmtTy = MRI.getType(Amt); 6050fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(AmtTy, 0); 6051fe6060f1SDimitry Andric bool IsLeft = MI.getOpcode() == TargetOpcode::G_ROTL; 6052fe6060f1SDimitry Andric unsigned RevRot = IsLeft ? TargetOpcode::G_ROTR : TargetOpcode::G_ROTL; 6053fe6060f1SDimitry Andric auto Neg = MIRBuilder.buildSub(AmtTy, Zero, Amt); 6054fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevRot, {Dst}, {Src, Neg}); 6055fe6060f1SDimitry Andric MI.eraseFromParent(); 6056fe6060f1SDimitry Andric return Legalized; 6057fe6060f1SDimitry Andric } 6058fe6060f1SDimitry Andric 6059fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerRotate(MachineInstr &MI) { 6060fe6060f1SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 6061fe6060f1SDimitry Andric Register Src = MI.getOperand(1).getReg(); 6062fe6060f1SDimitry Andric Register Amt = MI.getOperand(2).getReg(); 6063fe6060f1SDimitry Andric LLT DstTy = MRI.getType(Dst); 6064349cc55cSDimitry Andric LLT SrcTy = MRI.getType(Src); 6065fe6060f1SDimitry Andric LLT AmtTy = MRI.getType(Amt); 6066fe6060f1SDimitry Andric 6067fe6060f1SDimitry Andric unsigned EltSizeInBits = DstTy.getScalarSizeInBits(); 6068fe6060f1SDimitry Andric bool IsLeft = MI.getOpcode() == TargetOpcode::G_ROTL; 6069fe6060f1SDimitry Andric 6070fe6060f1SDimitry Andric MIRBuilder.setInstrAndDebugLoc(MI); 6071fe6060f1SDimitry Andric 6072fe6060f1SDimitry Andric // If a rotate in the other direction is supported, use it. 6073fe6060f1SDimitry Andric unsigned RevRot = IsLeft ? TargetOpcode::G_ROTR : TargetOpcode::G_ROTL; 6074fe6060f1SDimitry Andric if (LI.isLegalOrCustom({RevRot, {DstTy, SrcTy}}) && 6075fe6060f1SDimitry Andric isPowerOf2_32(EltSizeInBits)) 6076fe6060f1SDimitry Andric return lowerRotateWithReverseRotate(MI); 6077fe6060f1SDimitry Andric 6078349cc55cSDimitry Andric // If a funnel shift is supported, use it. 6079349cc55cSDimitry Andric unsigned FShOpc = IsLeft ? TargetOpcode::G_FSHL : TargetOpcode::G_FSHR; 6080349cc55cSDimitry Andric unsigned RevFsh = !IsLeft ? TargetOpcode::G_FSHL : TargetOpcode::G_FSHR; 6081349cc55cSDimitry Andric bool IsFShLegal = false; 6082349cc55cSDimitry Andric if ((IsFShLegal = LI.isLegalOrCustom({FShOpc, {DstTy, AmtTy}})) || 6083349cc55cSDimitry Andric LI.isLegalOrCustom({RevFsh, {DstTy, AmtTy}})) { 6084349cc55cSDimitry Andric auto buildFunnelShift = [&](unsigned Opc, Register R1, Register R2, 6085349cc55cSDimitry Andric Register R3) { 6086349cc55cSDimitry Andric MIRBuilder.buildInstr(Opc, {R1}, {R2, R2, R3}); 6087349cc55cSDimitry Andric MI.eraseFromParent(); 6088349cc55cSDimitry Andric return Legalized; 6089349cc55cSDimitry Andric }; 6090349cc55cSDimitry Andric // If a funnel shift in the other direction is supported, use it. 6091349cc55cSDimitry Andric if (IsFShLegal) { 6092349cc55cSDimitry Andric return buildFunnelShift(FShOpc, Dst, Src, Amt); 6093349cc55cSDimitry Andric } else if (isPowerOf2_32(EltSizeInBits)) { 6094349cc55cSDimitry Andric Amt = MIRBuilder.buildNeg(DstTy, Amt).getReg(0); 6095349cc55cSDimitry Andric return buildFunnelShift(RevFsh, Dst, Src, Amt); 6096349cc55cSDimitry Andric } 6097349cc55cSDimitry Andric } 6098349cc55cSDimitry Andric 6099fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(AmtTy, 0); 6100fe6060f1SDimitry Andric unsigned ShOpc = IsLeft ? TargetOpcode::G_SHL : TargetOpcode::G_LSHR; 6101fe6060f1SDimitry Andric unsigned RevShiftOpc = IsLeft ? TargetOpcode::G_LSHR : TargetOpcode::G_SHL; 6102fe6060f1SDimitry Andric auto BitWidthMinusOneC = MIRBuilder.buildConstant(AmtTy, EltSizeInBits - 1); 6103fe6060f1SDimitry Andric Register ShVal; 6104fe6060f1SDimitry Andric Register RevShiftVal; 6105fe6060f1SDimitry Andric if (isPowerOf2_32(EltSizeInBits)) { 6106fe6060f1SDimitry Andric // (rotl x, c) -> x << (c & (w - 1)) | x >> (-c & (w - 1)) 6107fe6060f1SDimitry Andric // (rotr x, c) -> x >> (c & (w - 1)) | x << (-c & (w - 1)) 6108fe6060f1SDimitry Andric auto NegAmt = MIRBuilder.buildSub(AmtTy, Zero, Amt); 6109fe6060f1SDimitry Andric auto ShAmt = MIRBuilder.buildAnd(AmtTy, Amt, BitWidthMinusOneC); 6110fe6060f1SDimitry Andric ShVal = MIRBuilder.buildInstr(ShOpc, {DstTy}, {Src, ShAmt}).getReg(0); 6111fe6060f1SDimitry Andric auto RevAmt = MIRBuilder.buildAnd(AmtTy, NegAmt, BitWidthMinusOneC); 6112fe6060f1SDimitry Andric RevShiftVal = 6113fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Src, RevAmt}).getReg(0); 6114fe6060f1SDimitry Andric } else { 6115fe6060f1SDimitry Andric // (rotl x, c) -> x << (c % w) | x >> 1 >> (w - 1 - (c % w)) 6116fe6060f1SDimitry Andric // (rotr x, c) -> x >> (c % w) | x << 1 << (w - 1 - (c % w)) 6117fe6060f1SDimitry Andric auto BitWidthC = MIRBuilder.buildConstant(AmtTy, EltSizeInBits); 6118fe6060f1SDimitry Andric auto ShAmt = MIRBuilder.buildURem(AmtTy, Amt, BitWidthC); 6119fe6060f1SDimitry Andric ShVal = MIRBuilder.buildInstr(ShOpc, {DstTy}, {Src, ShAmt}).getReg(0); 6120fe6060f1SDimitry Andric auto RevAmt = MIRBuilder.buildSub(AmtTy, BitWidthMinusOneC, ShAmt); 6121fe6060f1SDimitry Andric auto One = MIRBuilder.buildConstant(AmtTy, 1); 6122fe6060f1SDimitry Andric auto Inner = MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Src, One}); 6123fe6060f1SDimitry Andric RevShiftVal = 6124fe6060f1SDimitry Andric MIRBuilder.buildInstr(RevShiftOpc, {DstTy}, {Inner, RevAmt}).getReg(0); 6125fe6060f1SDimitry Andric } 6126fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, ShVal, RevShiftVal); 6127fe6060f1SDimitry Andric MI.eraseFromParent(); 6128fe6060f1SDimitry Andric return Legalized; 6129fe6060f1SDimitry Andric } 6130fe6060f1SDimitry Andric 61310b57cec5SDimitry Andric // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float 61320b57cec5SDimitry Andric // representation. 61330b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 61340b57cec5SDimitry Andric LegalizerHelper::lowerU64ToF32BitOps(MachineInstr &MI) { 61350b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 61360b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 61370b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 61380b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 61390b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 61400b57cec5SDimitry Andric 61410b57cec5SDimitry Andric assert(MRI.getType(Src) == S64 && MRI.getType(Dst) == S32); 61420b57cec5SDimitry Andric 61430b57cec5SDimitry Andric // unsigned cul2f(ulong u) { 61440b57cec5SDimitry Andric // uint lz = clz(u); 61450b57cec5SDimitry Andric // uint e = (u != 0) ? 127U + 63U - lz : 0; 61460b57cec5SDimitry Andric // u = (u << lz) & 0x7fffffffffffffffUL; 61470b57cec5SDimitry Andric // ulong t = u & 0xffffffffffUL; 61480b57cec5SDimitry Andric // uint v = (e << 23) | (uint)(u >> 40); 61490b57cec5SDimitry Andric // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 61500b57cec5SDimitry Andric // return as_float(v + r); 61510b57cec5SDimitry Andric // } 61520b57cec5SDimitry Andric 61530b57cec5SDimitry Andric auto Zero32 = MIRBuilder.buildConstant(S32, 0); 61540b57cec5SDimitry Andric auto Zero64 = MIRBuilder.buildConstant(S64, 0); 61550b57cec5SDimitry Andric 61560b57cec5SDimitry Andric auto LZ = MIRBuilder.buildCTLZ_ZERO_UNDEF(S32, Src); 61570b57cec5SDimitry Andric 61580b57cec5SDimitry Andric auto K = MIRBuilder.buildConstant(S32, 127U + 63U); 61590b57cec5SDimitry Andric auto Sub = MIRBuilder.buildSub(S32, K, LZ); 61600b57cec5SDimitry Andric 61610b57cec5SDimitry Andric auto NotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, Src, Zero64); 61620b57cec5SDimitry Andric auto E = MIRBuilder.buildSelect(S32, NotZero, Sub, Zero32); 61630b57cec5SDimitry Andric 61640b57cec5SDimitry Andric auto Mask0 = MIRBuilder.buildConstant(S64, (-1ULL) >> 1); 61650b57cec5SDimitry Andric auto ShlLZ = MIRBuilder.buildShl(S64, Src, LZ); 61660b57cec5SDimitry Andric 61670b57cec5SDimitry Andric auto U = MIRBuilder.buildAnd(S64, ShlLZ, Mask0); 61680b57cec5SDimitry Andric 61690b57cec5SDimitry Andric auto Mask1 = MIRBuilder.buildConstant(S64, 0xffffffffffULL); 61700b57cec5SDimitry Andric auto T = MIRBuilder.buildAnd(S64, U, Mask1); 61710b57cec5SDimitry Andric 61720b57cec5SDimitry Andric auto UShl = MIRBuilder.buildLShr(S64, U, MIRBuilder.buildConstant(S64, 40)); 61730b57cec5SDimitry Andric auto ShlE = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 23)); 61740b57cec5SDimitry Andric auto V = MIRBuilder.buildOr(S32, ShlE, MIRBuilder.buildTrunc(S32, UShl)); 61750b57cec5SDimitry Andric 61760b57cec5SDimitry Andric auto C = MIRBuilder.buildConstant(S64, 0x8000000000ULL); 61770b57cec5SDimitry Andric auto RCmp = MIRBuilder.buildICmp(CmpInst::ICMP_UGT, S1, T, C); 61780b57cec5SDimitry Andric auto TCmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, T, C); 61790b57cec5SDimitry Andric auto One = MIRBuilder.buildConstant(S32, 1); 61800b57cec5SDimitry Andric 61810b57cec5SDimitry Andric auto VTrunc1 = MIRBuilder.buildAnd(S32, V, One); 61820b57cec5SDimitry Andric auto Select0 = MIRBuilder.buildSelect(S32, TCmp, VTrunc1, Zero32); 61830b57cec5SDimitry Andric auto R = MIRBuilder.buildSelect(S32, RCmp, One, Select0); 61840b57cec5SDimitry Andric MIRBuilder.buildAdd(Dst, V, R); 61850b57cec5SDimitry Andric 61865ffd83dbSDimitry Andric MI.eraseFromParent(); 61870b57cec5SDimitry Andric return Legalized; 61880b57cec5SDimitry Andric } 61890b57cec5SDimitry Andric 6190e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerUITOFP(MachineInstr &MI) { 61910b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 61920b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 61930b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 61940b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 61950b57cec5SDimitry Andric 6196480093f4SDimitry Andric if (SrcTy == LLT::scalar(1)) { 6197480093f4SDimitry Andric auto True = MIRBuilder.buildFConstant(DstTy, 1.0); 6198480093f4SDimitry Andric auto False = MIRBuilder.buildFConstant(DstTy, 0.0); 6199480093f4SDimitry Andric MIRBuilder.buildSelect(Dst, Src, True, False); 6200480093f4SDimitry Andric MI.eraseFromParent(); 6201480093f4SDimitry Andric return Legalized; 6202480093f4SDimitry Andric } 6203480093f4SDimitry Andric 62040b57cec5SDimitry Andric if (SrcTy != LLT::scalar(64)) 62050b57cec5SDimitry Andric return UnableToLegalize; 62060b57cec5SDimitry Andric 62070b57cec5SDimitry Andric if (DstTy == LLT::scalar(32)) { 62080b57cec5SDimitry Andric // TODO: SelectionDAG has several alternative expansions to port which may 62090b57cec5SDimitry Andric // be more reasonble depending on the available instructions. If a target 62100b57cec5SDimitry Andric // has sitofp, does not have CTLZ, or can efficiently use f64 as an 62110b57cec5SDimitry Andric // intermediate type, this is probably worse. 62120b57cec5SDimitry Andric return lowerU64ToF32BitOps(MI); 62130b57cec5SDimitry Andric } 62140b57cec5SDimitry Andric 62150b57cec5SDimitry Andric return UnableToLegalize; 62160b57cec5SDimitry Andric } 62170b57cec5SDimitry Andric 6218e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerSITOFP(MachineInstr &MI) { 62190b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 62200b57cec5SDimitry Andric Register Src = MI.getOperand(1).getReg(); 62210b57cec5SDimitry Andric LLT DstTy = MRI.getType(Dst); 62220b57cec5SDimitry Andric LLT SrcTy = MRI.getType(Src); 62230b57cec5SDimitry Andric 62240b57cec5SDimitry Andric const LLT S64 = LLT::scalar(64); 62250b57cec5SDimitry Andric const LLT S32 = LLT::scalar(32); 62260b57cec5SDimitry Andric const LLT S1 = LLT::scalar(1); 62270b57cec5SDimitry Andric 6228480093f4SDimitry Andric if (SrcTy == S1) { 6229480093f4SDimitry Andric auto True = MIRBuilder.buildFConstant(DstTy, -1.0); 6230480093f4SDimitry Andric auto False = MIRBuilder.buildFConstant(DstTy, 0.0); 6231480093f4SDimitry Andric MIRBuilder.buildSelect(Dst, Src, True, False); 6232480093f4SDimitry Andric MI.eraseFromParent(); 6233480093f4SDimitry Andric return Legalized; 6234480093f4SDimitry Andric } 6235480093f4SDimitry Andric 62360b57cec5SDimitry Andric if (SrcTy != S64) 62370b57cec5SDimitry Andric return UnableToLegalize; 62380b57cec5SDimitry Andric 62390b57cec5SDimitry Andric if (DstTy == S32) { 62400b57cec5SDimitry Andric // signed cl2f(long l) { 62410b57cec5SDimitry Andric // long s = l >> 63; 62420b57cec5SDimitry Andric // float r = cul2f((l + s) ^ s); 62430b57cec5SDimitry Andric // return s ? -r : r; 62440b57cec5SDimitry Andric // } 62450b57cec5SDimitry Andric Register L = Src; 62460b57cec5SDimitry Andric auto SignBit = MIRBuilder.buildConstant(S64, 63); 62470b57cec5SDimitry Andric auto S = MIRBuilder.buildAShr(S64, L, SignBit); 62480b57cec5SDimitry Andric 62490b57cec5SDimitry Andric auto LPlusS = MIRBuilder.buildAdd(S64, L, S); 62500b57cec5SDimitry Andric auto Xor = MIRBuilder.buildXor(S64, LPlusS, S); 62510b57cec5SDimitry Andric auto R = MIRBuilder.buildUITOFP(S32, Xor); 62520b57cec5SDimitry Andric 62530b57cec5SDimitry Andric auto RNeg = MIRBuilder.buildFNeg(S32, R); 62540b57cec5SDimitry Andric auto SignNotZero = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, S, 62550b57cec5SDimitry Andric MIRBuilder.buildConstant(S64, 0)); 62560b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, SignNotZero, RNeg, R); 62575ffd83dbSDimitry Andric MI.eraseFromParent(); 62580b57cec5SDimitry Andric return Legalized; 62590b57cec5SDimitry Andric } 62600b57cec5SDimitry Andric 62610b57cec5SDimitry Andric return UnableToLegalize; 62620b57cec5SDimitry Andric } 62630b57cec5SDimitry Andric 6264e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOUI(MachineInstr &MI) { 62658bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 62668bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 62678bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 62688bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 62698bcb0991SDimitry Andric const LLT S64 = LLT::scalar(64); 62708bcb0991SDimitry Andric const LLT S32 = LLT::scalar(32); 62718bcb0991SDimitry Andric 62728bcb0991SDimitry Andric if (SrcTy != S64 && SrcTy != S32) 62738bcb0991SDimitry Andric return UnableToLegalize; 62748bcb0991SDimitry Andric if (DstTy != S32 && DstTy != S64) 62758bcb0991SDimitry Andric return UnableToLegalize; 62768bcb0991SDimitry Andric 62778bcb0991SDimitry Andric // FPTOSI gives same result as FPTOUI for positive signed integers. 62788bcb0991SDimitry Andric // FPTOUI needs to deal with fp values that convert to unsigned integers 62798bcb0991SDimitry Andric // greater or equal to 2^31 for float or 2^63 for double. For brevity 2^Exp. 62808bcb0991SDimitry Andric 62818bcb0991SDimitry Andric APInt TwoPExpInt = APInt::getSignMask(DstTy.getSizeInBits()); 62828bcb0991SDimitry Andric APFloat TwoPExpFP(SrcTy.getSizeInBits() == 32 ? APFloat::IEEEsingle() 62838bcb0991SDimitry Andric : APFloat::IEEEdouble(), 6284349cc55cSDimitry Andric APInt::getZero(SrcTy.getSizeInBits())); 62858bcb0991SDimitry Andric TwoPExpFP.convertFromAPInt(TwoPExpInt, false, APFloat::rmNearestTiesToEven); 62868bcb0991SDimitry Andric 62878bcb0991SDimitry Andric MachineInstrBuilder FPTOSI = MIRBuilder.buildFPTOSI(DstTy, Src); 62888bcb0991SDimitry Andric 62898bcb0991SDimitry Andric MachineInstrBuilder Threshold = MIRBuilder.buildFConstant(SrcTy, TwoPExpFP); 62908bcb0991SDimitry Andric // For fp Value greater or equal to Threshold(2^Exp), we use FPTOSI on 62918bcb0991SDimitry Andric // (Value - 2^Exp) and add 2^Exp by setting highest bit in result to 1. 62928bcb0991SDimitry Andric MachineInstrBuilder FSub = MIRBuilder.buildFSub(SrcTy, Src, Threshold); 62938bcb0991SDimitry Andric MachineInstrBuilder ResLowBits = MIRBuilder.buildFPTOSI(DstTy, FSub); 62948bcb0991SDimitry Andric MachineInstrBuilder ResHighBit = MIRBuilder.buildConstant(DstTy, TwoPExpInt); 62958bcb0991SDimitry Andric MachineInstrBuilder Res = MIRBuilder.buildXor(DstTy, ResLowBits, ResHighBit); 62968bcb0991SDimitry Andric 6297480093f4SDimitry Andric const LLT S1 = LLT::scalar(1); 6298480093f4SDimitry Andric 62998bcb0991SDimitry Andric MachineInstrBuilder FCMP = 6300480093f4SDimitry Andric MIRBuilder.buildFCmp(CmpInst::FCMP_ULT, S1, Src, Threshold); 63018bcb0991SDimitry Andric MIRBuilder.buildSelect(Dst, FCMP, FPTOSI, Res); 63028bcb0991SDimitry Andric 63038bcb0991SDimitry Andric MI.eraseFromParent(); 63048bcb0991SDimitry Andric return Legalized; 63058bcb0991SDimitry Andric } 63068bcb0991SDimitry Andric 63075ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOSI(MachineInstr &MI) { 63085ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 63095ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 63105ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 63115ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 63125ffd83dbSDimitry Andric const LLT S64 = LLT::scalar(64); 63135ffd83dbSDimitry Andric const LLT S32 = LLT::scalar(32); 63145ffd83dbSDimitry Andric 63155ffd83dbSDimitry Andric // FIXME: Only f32 to i64 conversions are supported. 63165ffd83dbSDimitry Andric if (SrcTy.getScalarType() != S32 || DstTy.getScalarType() != S64) 63175ffd83dbSDimitry Andric return UnableToLegalize; 63185ffd83dbSDimitry Andric 63195ffd83dbSDimitry Andric // Expand f32 -> i64 conversion 63205ffd83dbSDimitry Andric // This algorithm comes from compiler-rt's implementation of fixsfdi: 6321fe6060f1SDimitry Andric // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/fixsfdi.c 63225ffd83dbSDimitry Andric 63235ffd83dbSDimitry Andric unsigned SrcEltBits = SrcTy.getScalarSizeInBits(); 63245ffd83dbSDimitry Andric 63255ffd83dbSDimitry Andric auto ExponentMask = MIRBuilder.buildConstant(SrcTy, 0x7F800000); 63265ffd83dbSDimitry Andric auto ExponentLoBit = MIRBuilder.buildConstant(SrcTy, 23); 63275ffd83dbSDimitry Andric 63285ffd83dbSDimitry Andric auto AndExpMask = MIRBuilder.buildAnd(SrcTy, Src, ExponentMask); 63295ffd83dbSDimitry Andric auto ExponentBits = MIRBuilder.buildLShr(SrcTy, AndExpMask, ExponentLoBit); 63305ffd83dbSDimitry Andric 63315ffd83dbSDimitry Andric auto SignMask = MIRBuilder.buildConstant(SrcTy, 63325ffd83dbSDimitry Andric APInt::getSignMask(SrcEltBits)); 63335ffd83dbSDimitry Andric auto AndSignMask = MIRBuilder.buildAnd(SrcTy, Src, SignMask); 63345ffd83dbSDimitry Andric auto SignLowBit = MIRBuilder.buildConstant(SrcTy, SrcEltBits - 1); 63355ffd83dbSDimitry Andric auto Sign = MIRBuilder.buildAShr(SrcTy, AndSignMask, SignLowBit); 63365ffd83dbSDimitry Andric Sign = MIRBuilder.buildSExt(DstTy, Sign); 63375ffd83dbSDimitry Andric 63385ffd83dbSDimitry Andric auto MantissaMask = MIRBuilder.buildConstant(SrcTy, 0x007FFFFF); 63395ffd83dbSDimitry Andric auto AndMantissaMask = MIRBuilder.buildAnd(SrcTy, Src, MantissaMask); 63405ffd83dbSDimitry Andric auto K = MIRBuilder.buildConstant(SrcTy, 0x00800000); 63415ffd83dbSDimitry Andric 63425ffd83dbSDimitry Andric auto R = MIRBuilder.buildOr(SrcTy, AndMantissaMask, K); 63435ffd83dbSDimitry Andric R = MIRBuilder.buildZExt(DstTy, R); 63445ffd83dbSDimitry Andric 63455ffd83dbSDimitry Andric auto Bias = MIRBuilder.buildConstant(SrcTy, 127); 63465ffd83dbSDimitry Andric auto Exponent = MIRBuilder.buildSub(SrcTy, ExponentBits, Bias); 63475ffd83dbSDimitry Andric auto SubExponent = MIRBuilder.buildSub(SrcTy, Exponent, ExponentLoBit); 63485ffd83dbSDimitry Andric auto ExponentSub = MIRBuilder.buildSub(SrcTy, ExponentLoBit, Exponent); 63495ffd83dbSDimitry Andric 63505ffd83dbSDimitry Andric auto Shl = MIRBuilder.buildShl(DstTy, R, SubExponent); 63515ffd83dbSDimitry Andric auto Srl = MIRBuilder.buildLShr(DstTy, R, ExponentSub); 63525ffd83dbSDimitry Andric 63535ffd83dbSDimitry Andric const LLT S1 = LLT::scalar(1); 63545ffd83dbSDimitry Andric auto CmpGt = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, 63555ffd83dbSDimitry Andric S1, Exponent, ExponentLoBit); 63565ffd83dbSDimitry Andric 63575ffd83dbSDimitry Andric R = MIRBuilder.buildSelect(DstTy, CmpGt, Shl, Srl); 63585ffd83dbSDimitry Andric 63595ffd83dbSDimitry Andric auto XorSign = MIRBuilder.buildXor(DstTy, R, Sign); 63605ffd83dbSDimitry Andric auto Ret = MIRBuilder.buildSub(DstTy, XorSign, Sign); 63615ffd83dbSDimitry Andric 63625ffd83dbSDimitry Andric auto ZeroSrcTy = MIRBuilder.buildConstant(SrcTy, 0); 63635ffd83dbSDimitry Andric 63645ffd83dbSDimitry Andric auto ExponentLt0 = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, 63655ffd83dbSDimitry Andric S1, Exponent, ZeroSrcTy); 63665ffd83dbSDimitry Andric 63675ffd83dbSDimitry Andric auto ZeroDstTy = MIRBuilder.buildConstant(DstTy, 0); 63685ffd83dbSDimitry Andric MIRBuilder.buildSelect(Dst, ExponentLt0, ZeroDstTy, Ret); 63695ffd83dbSDimitry Andric 63705ffd83dbSDimitry Andric MI.eraseFromParent(); 63715ffd83dbSDimitry Andric return Legalized; 63725ffd83dbSDimitry Andric } 63735ffd83dbSDimitry Andric 63745ffd83dbSDimitry Andric // f64 -> f16 conversion using round-to-nearest-even rounding mode. 63755ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 63765ffd83dbSDimitry Andric LegalizerHelper::lowerFPTRUNC_F64_TO_F16(MachineInstr &MI) { 63775ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 63785ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 63795ffd83dbSDimitry Andric 63805ffd83dbSDimitry Andric if (MRI.getType(Src).isVector()) // TODO: Handle vectors directly. 63815ffd83dbSDimitry Andric return UnableToLegalize; 63825ffd83dbSDimitry Andric 63835ffd83dbSDimitry Andric const unsigned ExpMask = 0x7ff; 63845ffd83dbSDimitry Andric const unsigned ExpBiasf64 = 1023; 63855ffd83dbSDimitry Andric const unsigned ExpBiasf16 = 15; 63865ffd83dbSDimitry Andric const LLT S32 = LLT::scalar(32); 63875ffd83dbSDimitry Andric const LLT S1 = LLT::scalar(1); 63885ffd83dbSDimitry Andric 63895ffd83dbSDimitry Andric auto Unmerge = MIRBuilder.buildUnmerge(S32, Src); 63905ffd83dbSDimitry Andric Register U = Unmerge.getReg(0); 63915ffd83dbSDimitry Andric Register UH = Unmerge.getReg(1); 63925ffd83dbSDimitry Andric 63935ffd83dbSDimitry Andric auto E = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 20)); 63945ffd83dbSDimitry Andric E = MIRBuilder.buildAnd(S32, E, MIRBuilder.buildConstant(S32, ExpMask)); 63955ffd83dbSDimitry Andric 63965ffd83dbSDimitry Andric // Subtract the fp64 exponent bias (1023) to get the real exponent and 63975ffd83dbSDimitry Andric // add the f16 bias (15) to get the biased exponent for the f16 format. 63985ffd83dbSDimitry Andric E = MIRBuilder.buildAdd( 63995ffd83dbSDimitry Andric S32, E, MIRBuilder.buildConstant(S32, -ExpBiasf64 + ExpBiasf16)); 64005ffd83dbSDimitry Andric 64015ffd83dbSDimitry Andric auto M = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 8)); 64025ffd83dbSDimitry Andric M = MIRBuilder.buildAnd(S32, M, MIRBuilder.buildConstant(S32, 0xffe)); 64035ffd83dbSDimitry Andric 64045ffd83dbSDimitry Andric auto MaskedSig = MIRBuilder.buildAnd(S32, UH, 64055ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x1ff)); 64065ffd83dbSDimitry Andric MaskedSig = MIRBuilder.buildOr(S32, MaskedSig, U); 64075ffd83dbSDimitry Andric 64085ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildConstant(S32, 0); 64095ffd83dbSDimitry Andric auto SigCmpNE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, MaskedSig, Zero); 64105ffd83dbSDimitry Andric auto Lo40Set = MIRBuilder.buildZExt(S32, SigCmpNE0); 64115ffd83dbSDimitry Andric M = MIRBuilder.buildOr(S32, M, Lo40Set); 64125ffd83dbSDimitry Andric 64135ffd83dbSDimitry Andric // (M != 0 ? 0x0200 : 0) | 0x7c00; 64145ffd83dbSDimitry Andric auto Bits0x200 = MIRBuilder.buildConstant(S32, 0x0200); 64155ffd83dbSDimitry Andric auto CmpM_NE0 = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, M, Zero); 64165ffd83dbSDimitry Andric auto SelectCC = MIRBuilder.buildSelect(S32, CmpM_NE0, Bits0x200, Zero); 64175ffd83dbSDimitry Andric 64185ffd83dbSDimitry Andric auto Bits0x7c00 = MIRBuilder.buildConstant(S32, 0x7c00); 64195ffd83dbSDimitry Andric auto I = MIRBuilder.buildOr(S32, SelectCC, Bits0x7c00); 64205ffd83dbSDimitry Andric 64215ffd83dbSDimitry Andric // N = M | (E << 12); 64225ffd83dbSDimitry Andric auto EShl12 = MIRBuilder.buildShl(S32, E, MIRBuilder.buildConstant(S32, 12)); 64235ffd83dbSDimitry Andric auto N = MIRBuilder.buildOr(S32, M, EShl12); 64245ffd83dbSDimitry Andric 64255ffd83dbSDimitry Andric // B = clamp(1-E, 0, 13); 64265ffd83dbSDimitry Andric auto One = MIRBuilder.buildConstant(S32, 1); 64275ffd83dbSDimitry Andric auto OneSubExp = MIRBuilder.buildSub(S32, One, E); 64285ffd83dbSDimitry Andric auto B = MIRBuilder.buildSMax(S32, OneSubExp, Zero); 64295ffd83dbSDimitry Andric B = MIRBuilder.buildSMin(S32, B, MIRBuilder.buildConstant(S32, 13)); 64305ffd83dbSDimitry Andric 64315ffd83dbSDimitry Andric auto SigSetHigh = MIRBuilder.buildOr(S32, M, 64325ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x1000)); 64335ffd83dbSDimitry Andric 64345ffd83dbSDimitry Andric auto D = MIRBuilder.buildLShr(S32, SigSetHigh, B); 64355ffd83dbSDimitry Andric auto D0 = MIRBuilder.buildShl(S32, D, B); 64365ffd83dbSDimitry Andric 64375ffd83dbSDimitry Andric auto D0_NE_SigSetHigh = MIRBuilder.buildICmp(CmpInst::ICMP_NE, S1, 64385ffd83dbSDimitry Andric D0, SigSetHigh); 64395ffd83dbSDimitry Andric auto D1 = MIRBuilder.buildZExt(S32, D0_NE_SigSetHigh); 64405ffd83dbSDimitry Andric D = MIRBuilder.buildOr(S32, D, D1); 64415ffd83dbSDimitry Andric 64425ffd83dbSDimitry Andric auto CmpELtOne = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, S1, E, One); 64435ffd83dbSDimitry Andric auto V = MIRBuilder.buildSelect(S32, CmpELtOne, D, N); 64445ffd83dbSDimitry Andric 64455ffd83dbSDimitry Andric auto VLow3 = MIRBuilder.buildAnd(S32, V, MIRBuilder.buildConstant(S32, 7)); 64465ffd83dbSDimitry Andric V = MIRBuilder.buildLShr(S32, V, MIRBuilder.buildConstant(S32, 2)); 64475ffd83dbSDimitry Andric 64485ffd83dbSDimitry Andric auto VLow3Eq3 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, VLow3, 64495ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 3)); 64505ffd83dbSDimitry Andric auto V0 = MIRBuilder.buildZExt(S32, VLow3Eq3); 64515ffd83dbSDimitry Andric 64525ffd83dbSDimitry Andric auto VLow3Gt5 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, S1, VLow3, 64535ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 5)); 64545ffd83dbSDimitry Andric auto V1 = MIRBuilder.buildZExt(S32, VLow3Gt5); 64555ffd83dbSDimitry Andric 64565ffd83dbSDimitry Andric V1 = MIRBuilder.buildOr(S32, V0, V1); 64575ffd83dbSDimitry Andric V = MIRBuilder.buildAdd(S32, V, V1); 64585ffd83dbSDimitry Andric 64595ffd83dbSDimitry Andric auto CmpEGt30 = MIRBuilder.buildICmp(CmpInst::ICMP_SGT, S1, 64605ffd83dbSDimitry Andric E, MIRBuilder.buildConstant(S32, 30)); 64615ffd83dbSDimitry Andric V = MIRBuilder.buildSelect(S32, CmpEGt30, 64625ffd83dbSDimitry Andric MIRBuilder.buildConstant(S32, 0x7c00), V); 64635ffd83dbSDimitry Andric 64645ffd83dbSDimitry Andric auto CmpEGt1039 = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, S1, 64655ffd83dbSDimitry Andric E, MIRBuilder.buildConstant(S32, 1039)); 64665ffd83dbSDimitry Andric V = MIRBuilder.buildSelect(S32, CmpEGt1039, I, V); 64675ffd83dbSDimitry Andric 64685ffd83dbSDimitry Andric // Extract the sign bit. 64695ffd83dbSDimitry Andric auto Sign = MIRBuilder.buildLShr(S32, UH, MIRBuilder.buildConstant(S32, 16)); 64705ffd83dbSDimitry Andric Sign = MIRBuilder.buildAnd(S32, Sign, MIRBuilder.buildConstant(S32, 0x8000)); 64715ffd83dbSDimitry Andric 64725ffd83dbSDimitry Andric // Insert the sign bit 64735ffd83dbSDimitry Andric V = MIRBuilder.buildOr(S32, Sign, V); 64745ffd83dbSDimitry Andric 64755ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst, V); 64765ffd83dbSDimitry Andric MI.eraseFromParent(); 64775ffd83dbSDimitry Andric return Legalized; 64785ffd83dbSDimitry Andric } 64795ffd83dbSDimitry Andric 64805ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 6481e8d8bef9SDimitry Andric LegalizerHelper::lowerFPTRUNC(MachineInstr &MI) { 64825ffd83dbSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 64835ffd83dbSDimitry Andric Register Src = MI.getOperand(1).getReg(); 64845ffd83dbSDimitry Andric 64855ffd83dbSDimitry Andric LLT DstTy = MRI.getType(Dst); 64865ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src); 64875ffd83dbSDimitry Andric const LLT S64 = LLT::scalar(64); 64885ffd83dbSDimitry Andric const LLT S16 = LLT::scalar(16); 64895ffd83dbSDimitry Andric 64905ffd83dbSDimitry Andric if (DstTy.getScalarType() == S16 && SrcTy.getScalarType() == S64) 64915ffd83dbSDimitry Andric return lowerFPTRUNC_F64_TO_F16(MI); 64925ffd83dbSDimitry Andric 64935ffd83dbSDimitry Andric return UnableToLegalize; 64945ffd83dbSDimitry Andric } 64955ffd83dbSDimitry Andric 6496e8d8bef9SDimitry Andric // TODO: If RHS is a constant SelectionDAGBuilder expands this into a 6497e8d8bef9SDimitry Andric // multiplication tree. 6498e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPOWI(MachineInstr &MI) { 6499e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 6500e8d8bef9SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 6501e8d8bef9SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 6502e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Dst); 6503e8d8bef9SDimitry Andric 6504e8d8bef9SDimitry Andric auto CvtSrc1 = MIRBuilder.buildSITOFP(Ty, Src1); 6505e8d8bef9SDimitry Andric MIRBuilder.buildFPow(Dst, Src0, CvtSrc1, MI.getFlags()); 6506e8d8bef9SDimitry Andric MI.eraseFromParent(); 6507e8d8bef9SDimitry Andric return Legalized; 6508e8d8bef9SDimitry Andric } 6509e8d8bef9SDimitry Andric 65100b57cec5SDimitry Andric static CmpInst::Predicate minMaxToCompare(unsigned Opc) { 65110b57cec5SDimitry Andric switch (Opc) { 65120b57cec5SDimitry Andric case TargetOpcode::G_SMIN: 65130b57cec5SDimitry Andric return CmpInst::ICMP_SLT; 65140b57cec5SDimitry Andric case TargetOpcode::G_SMAX: 65150b57cec5SDimitry Andric return CmpInst::ICMP_SGT; 65160b57cec5SDimitry Andric case TargetOpcode::G_UMIN: 65170b57cec5SDimitry Andric return CmpInst::ICMP_ULT; 65180b57cec5SDimitry Andric case TargetOpcode::G_UMAX: 65190b57cec5SDimitry Andric return CmpInst::ICMP_UGT; 65200b57cec5SDimitry Andric default: 65210b57cec5SDimitry Andric llvm_unreachable("not in integer min/max"); 65220b57cec5SDimitry Andric } 65230b57cec5SDimitry Andric } 65240b57cec5SDimitry Andric 6525e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerMinMax(MachineInstr &MI) { 65260b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 65270b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 65280b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 65290b57cec5SDimitry Andric 65300b57cec5SDimitry Andric const CmpInst::Predicate Pred = minMaxToCompare(MI.getOpcode()); 65310b57cec5SDimitry Andric LLT CmpType = MRI.getType(Dst).changeElementSize(1); 65320b57cec5SDimitry Andric 65330b57cec5SDimitry Andric auto Cmp = MIRBuilder.buildICmp(Pred, CmpType, Src0, Src1); 65340b57cec5SDimitry Andric MIRBuilder.buildSelect(Dst, Cmp, Src0, Src1); 65350b57cec5SDimitry Andric 65360b57cec5SDimitry Andric MI.eraseFromParent(); 65370b57cec5SDimitry Andric return Legalized; 65380b57cec5SDimitry Andric } 65390b57cec5SDimitry Andric 65400b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 6541e8d8bef9SDimitry Andric LegalizerHelper::lowerFCopySign(MachineInstr &MI) { 65420b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 65430b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 65440b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 65450b57cec5SDimitry Andric 65460b57cec5SDimitry Andric const LLT Src0Ty = MRI.getType(Src0); 65470b57cec5SDimitry Andric const LLT Src1Ty = MRI.getType(Src1); 65480b57cec5SDimitry Andric 65490b57cec5SDimitry Andric const int Src0Size = Src0Ty.getScalarSizeInBits(); 65500b57cec5SDimitry Andric const int Src1Size = Src1Ty.getScalarSizeInBits(); 65510b57cec5SDimitry Andric 65520b57cec5SDimitry Andric auto SignBitMask = MIRBuilder.buildConstant( 65530b57cec5SDimitry Andric Src0Ty, APInt::getSignMask(Src0Size)); 65540b57cec5SDimitry Andric 65550b57cec5SDimitry Andric auto NotSignBitMask = MIRBuilder.buildConstant( 65560b57cec5SDimitry Andric Src0Ty, APInt::getLowBitsSet(Src0Size, Src0Size - 1)); 65570b57cec5SDimitry Andric 6558fe6060f1SDimitry Andric Register And0 = MIRBuilder.buildAnd(Src0Ty, Src0, NotSignBitMask).getReg(0); 6559fe6060f1SDimitry Andric Register And1; 65600b57cec5SDimitry Andric if (Src0Ty == Src1Ty) { 6561fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src1Ty, Src1, SignBitMask).getReg(0); 65620b57cec5SDimitry Andric } else if (Src0Size > Src1Size) { 65630b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src0Ty, Src0Size - Src1Size); 65640b57cec5SDimitry Andric auto Zext = MIRBuilder.buildZExt(Src0Ty, Src1); 65650b57cec5SDimitry Andric auto Shift = MIRBuilder.buildShl(Src0Ty, Zext, ShiftAmt); 6566fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src0Ty, Shift, SignBitMask).getReg(0); 65670b57cec5SDimitry Andric } else { 65680b57cec5SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Src1Ty, Src1Size - Src0Size); 65690b57cec5SDimitry Andric auto Shift = MIRBuilder.buildLShr(Src1Ty, Src1, ShiftAmt); 65700b57cec5SDimitry Andric auto Trunc = MIRBuilder.buildTrunc(Src0Ty, Shift); 6571fe6060f1SDimitry Andric And1 = MIRBuilder.buildAnd(Src0Ty, Trunc, SignBitMask).getReg(0); 65720b57cec5SDimitry Andric } 65730b57cec5SDimitry Andric 65740b57cec5SDimitry Andric // Be careful about setting nsz/nnan/ninf on every instruction, since the 65750b57cec5SDimitry Andric // constants are a nan and -0.0, but the final result should preserve 65760b57cec5SDimitry Andric // everything. 6577fe6060f1SDimitry Andric unsigned Flags = MI.getFlags(); 6578fe6060f1SDimitry Andric MIRBuilder.buildOr(Dst, And0, And1, Flags); 65790b57cec5SDimitry Andric 65800b57cec5SDimitry Andric MI.eraseFromParent(); 65810b57cec5SDimitry Andric return Legalized; 65820b57cec5SDimitry Andric } 65830b57cec5SDimitry Andric 65840b57cec5SDimitry Andric LegalizerHelper::LegalizeResult 65850b57cec5SDimitry Andric LegalizerHelper::lowerFMinNumMaxNum(MachineInstr &MI) { 65860b57cec5SDimitry Andric unsigned NewOp = MI.getOpcode() == TargetOpcode::G_FMINNUM ? 65870b57cec5SDimitry Andric TargetOpcode::G_FMINNUM_IEEE : TargetOpcode::G_FMAXNUM_IEEE; 65880b57cec5SDimitry Andric 65890b57cec5SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 65900b57cec5SDimitry Andric Register Src0 = MI.getOperand(1).getReg(); 65910b57cec5SDimitry Andric Register Src1 = MI.getOperand(2).getReg(); 65920b57cec5SDimitry Andric LLT Ty = MRI.getType(Dst); 65930b57cec5SDimitry Andric 65940b57cec5SDimitry Andric if (!MI.getFlag(MachineInstr::FmNoNans)) { 65950b57cec5SDimitry Andric // Insert canonicalizes if it's possible we need to quiet to get correct 65960b57cec5SDimitry Andric // sNaN behavior. 65970b57cec5SDimitry Andric 65980b57cec5SDimitry Andric // Note this must be done here, and not as an optimization combine in the 65990b57cec5SDimitry Andric // absence of a dedicate quiet-snan instruction as we're using an 66000b57cec5SDimitry Andric // omni-purpose G_FCANONICALIZE. 66010b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src0, MRI)) 66020b57cec5SDimitry Andric Src0 = MIRBuilder.buildFCanonicalize(Ty, Src0, MI.getFlags()).getReg(0); 66030b57cec5SDimitry Andric 66040b57cec5SDimitry Andric if (!isKnownNeverSNaN(Src1, MRI)) 66050b57cec5SDimitry Andric Src1 = MIRBuilder.buildFCanonicalize(Ty, Src1, MI.getFlags()).getReg(0); 66060b57cec5SDimitry Andric } 66070b57cec5SDimitry Andric 66080b57cec5SDimitry Andric // If there are no nans, it's safe to simply replace this with the non-IEEE 66090b57cec5SDimitry Andric // version. 66100b57cec5SDimitry Andric MIRBuilder.buildInstr(NewOp, {Dst}, {Src0, Src1}, MI.getFlags()); 66110b57cec5SDimitry Andric MI.eraseFromParent(); 66120b57cec5SDimitry Andric return Legalized; 66130b57cec5SDimitry Andric } 66148bcb0991SDimitry Andric 66158bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerFMad(MachineInstr &MI) { 66168bcb0991SDimitry Andric // Expand G_FMAD a, b, c -> G_FADD (G_FMUL a, b), c 66178bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 66188bcb0991SDimitry Andric LLT Ty = MRI.getType(DstReg); 66198bcb0991SDimitry Andric unsigned Flags = MI.getFlags(); 66208bcb0991SDimitry Andric 66218bcb0991SDimitry Andric auto Mul = MIRBuilder.buildFMul(Ty, MI.getOperand(1), MI.getOperand(2), 66228bcb0991SDimitry Andric Flags); 66238bcb0991SDimitry Andric MIRBuilder.buildFAdd(DstReg, Mul, MI.getOperand(3), Flags); 66248bcb0991SDimitry Andric MI.eraseFromParent(); 66258bcb0991SDimitry Andric return Legalized; 66268bcb0991SDimitry Andric } 66278bcb0991SDimitry Andric 66288bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 6629480093f4SDimitry Andric LegalizerHelper::lowerIntrinsicRound(MachineInstr &MI) { 6630480093f4SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 66315ffd83dbSDimitry Andric Register X = MI.getOperand(1).getReg(); 66325ffd83dbSDimitry Andric const unsigned Flags = MI.getFlags(); 66335ffd83dbSDimitry Andric const LLT Ty = MRI.getType(DstReg); 66345ffd83dbSDimitry Andric const LLT CondTy = Ty.changeElementSize(1); 66355ffd83dbSDimitry Andric 66365ffd83dbSDimitry Andric // round(x) => 66375ffd83dbSDimitry Andric // t = trunc(x); 66385ffd83dbSDimitry Andric // d = fabs(x - t); 66395ffd83dbSDimitry Andric // o = copysign(1.0f, x); 66405ffd83dbSDimitry Andric // return t + (d >= 0.5 ? o : 0.0); 66415ffd83dbSDimitry Andric 66425ffd83dbSDimitry Andric auto T = MIRBuilder.buildIntrinsicTrunc(Ty, X, Flags); 66435ffd83dbSDimitry Andric 66445ffd83dbSDimitry Andric auto Diff = MIRBuilder.buildFSub(Ty, X, T, Flags); 66455ffd83dbSDimitry Andric auto AbsDiff = MIRBuilder.buildFAbs(Ty, Diff, Flags); 66465ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildFConstant(Ty, 0.0); 66475ffd83dbSDimitry Andric auto One = MIRBuilder.buildFConstant(Ty, 1.0); 66485ffd83dbSDimitry Andric auto Half = MIRBuilder.buildFConstant(Ty, 0.5); 66495ffd83dbSDimitry Andric auto SignOne = MIRBuilder.buildFCopysign(Ty, One, X); 66505ffd83dbSDimitry Andric 66515ffd83dbSDimitry Andric auto Cmp = MIRBuilder.buildFCmp(CmpInst::FCMP_OGE, CondTy, AbsDiff, Half, 66525ffd83dbSDimitry Andric Flags); 66535ffd83dbSDimitry Andric auto Sel = MIRBuilder.buildSelect(Ty, Cmp, SignOne, Zero, Flags); 66545ffd83dbSDimitry Andric 66555ffd83dbSDimitry Andric MIRBuilder.buildFAdd(DstReg, T, Sel, Flags); 66565ffd83dbSDimitry Andric 66575ffd83dbSDimitry Andric MI.eraseFromParent(); 66585ffd83dbSDimitry Andric return Legalized; 66595ffd83dbSDimitry Andric } 66605ffd83dbSDimitry Andric 66615ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 66625ffd83dbSDimitry Andric LegalizerHelper::lowerFFloor(MachineInstr &MI) { 66635ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 6664480093f4SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 6665480093f4SDimitry Andric unsigned Flags = MI.getFlags(); 6666480093f4SDimitry Andric LLT Ty = MRI.getType(DstReg); 6667480093f4SDimitry Andric const LLT CondTy = Ty.changeElementSize(1); 6668480093f4SDimitry Andric 6669480093f4SDimitry Andric // result = trunc(src); 6670480093f4SDimitry Andric // if (src < 0.0 && src != result) 6671480093f4SDimitry Andric // result += -1.0. 6672480093f4SDimitry Andric 6673480093f4SDimitry Andric auto Trunc = MIRBuilder.buildIntrinsicTrunc(Ty, SrcReg, Flags); 66745ffd83dbSDimitry Andric auto Zero = MIRBuilder.buildFConstant(Ty, 0.0); 6675480093f4SDimitry Andric 6676480093f4SDimitry Andric auto Lt0 = MIRBuilder.buildFCmp(CmpInst::FCMP_OLT, CondTy, 6677480093f4SDimitry Andric SrcReg, Zero, Flags); 6678480093f4SDimitry Andric auto NeTrunc = MIRBuilder.buildFCmp(CmpInst::FCMP_ONE, CondTy, 6679480093f4SDimitry Andric SrcReg, Trunc, Flags); 6680480093f4SDimitry Andric auto And = MIRBuilder.buildAnd(CondTy, Lt0, NeTrunc); 6681480093f4SDimitry Andric auto AddVal = MIRBuilder.buildSITOFP(Ty, And); 6682480093f4SDimitry Andric 66835ffd83dbSDimitry Andric MIRBuilder.buildFAdd(DstReg, Trunc, AddVal, Flags); 66845ffd83dbSDimitry Andric MI.eraseFromParent(); 66855ffd83dbSDimitry Andric return Legalized; 66865ffd83dbSDimitry Andric } 66875ffd83dbSDimitry Andric 66885ffd83dbSDimitry Andric LegalizerHelper::LegalizeResult 66895ffd83dbSDimitry Andric LegalizerHelper::lowerMergeValues(MachineInstr &MI) { 66905ffd83dbSDimitry Andric const unsigned NumOps = MI.getNumOperands(); 66915ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 66925ffd83dbSDimitry Andric Register Src0Reg = MI.getOperand(1).getReg(); 66935ffd83dbSDimitry Andric LLT DstTy = MRI.getType(DstReg); 66945ffd83dbSDimitry Andric LLT SrcTy = MRI.getType(Src0Reg); 66955ffd83dbSDimitry Andric unsigned PartSize = SrcTy.getSizeInBits(); 66965ffd83dbSDimitry Andric 66975ffd83dbSDimitry Andric LLT WideTy = LLT::scalar(DstTy.getSizeInBits()); 66985ffd83dbSDimitry Andric Register ResultReg = MIRBuilder.buildZExt(WideTy, Src0Reg).getReg(0); 66995ffd83dbSDimitry Andric 67005ffd83dbSDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 67015ffd83dbSDimitry Andric const unsigned Offset = (I - 1) * PartSize; 67025ffd83dbSDimitry Andric 67035ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(I).getReg(); 67045ffd83dbSDimitry Andric auto ZextInput = MIRBuilder.buildZExt(WideTy, SrcReg); 67055ffd83dbSDimitry Andric 67065ffd83dbSDimitry Andric Register NextResult = I + 1 == NumOps && WideTy == DstTy ? DstReg : 67075ffd83dbSDimitry Andric MRI.createGenericVirtualRegister(WideTy); 67085ffd83dbSDimitry Andric 67095ffd83dbSDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, Offset); 67105ffd83dbSDimitry Andric auto Shl = MIRBuilder.buildShl(WideTy, ZextInput, ShiftAmt); 67115ffd83dbSDimitry Andric MIRBuilder.buildOr(NextResult, ResultReg, Shl); 67125ffd83dbSDimitry Andric ResultReg = NextResult; 67135ffd83dbSDimitry Andric } 67145ffd83dbSDimitry Andric 67155ffd83dbSDimitry Andric if (DstTy.isPointer()) { 67165ffd83dbSDimitry Andric if (MIRBuilder.getDataLayout().isNonIntegralAddressSpace( 67175ffd83dbSDimitry Andric DstTy.getAddressSpace())) { 67185ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Not casting nonintegral address space\n"); 67195ffd83dbSDimitry Andric return UnableToLegalize; 67205ffd83dbSDimitry Andric } 67215ffd83dbSDimitry Andric 67225ffd83dbSDimitry Andric MIRBuilder.buildIntToPtr(DstReg, ResultReg); 67235ffd83dbSDimitry Andric } 67245ffd83dbSDimitry Andric 6725480093f4SDimitry Andric MI.eraseFromParent(); 6726480093f4SDimitry Andric return Legalized; 6727480093f4SDimitry Andric } 6728480093f4SDimitry Andric 6729480093f4SDimitry Andric LegalizerHelper::LegalizeResult 67308bcb0991SDimitry Andric LegalizerHelper::lowerUnmergeValues(MachineInstr &MI) { 67318bcb0991SDimitry Andric const unsigned NumDst = MI.getNumOperands() - 1; 67325ffd83dbSDimitry Andric Register SrcReg = MI.getOperand(NumDst).getReg(); 67338bcb0991SDimitry Andric Register Dst0Reg = MI.getOperand(0).getReg(); 67348bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst0Reg); 67355ffd83dbSDimitry Andric if (DstTy.isPointer()) 67365ffd83dbSDimitry Andric return UnableToLegalize; // TODO 67378bcb0991SDimitry Andric 67385ffd83dbSDimitry Andric SrcReg = coerceToScalar(SrcReg); 67395ffd83dbSDimitry Andric if (!SrcReg) 67405ffd83dbSDimitry Andric return UnableToLegalize; 67418bcb0991SDimitry Andric 67428bcb0991SDimitry Andric // Expand scalarizing unmerge as bitcast to integer and shift. 67435ffd83dbSDimitry Andric LLT IntTy = MRI.getType(SrcReg); 67448bcb0991SDimitry Andric 67455ffd83dbSDimitry Andric MIRBuilder.buildTrunc(Dst0Reg, SrcReg); 67468bcb0991SDimitry Andric 67478bcb0991SDimitry Andric const unsigned DstSize = DstTy.getSizeInBits(); 67488bcb0991SDimitry Andric unsigned Offset = DstSize; 67498bcb0991SDimitry Andric for (unsigned I = 1; I != NumDst; ++I, Offset += DstSize) { 67508bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntTy, Offset); 67515ffd83dbSDimitry Andric auto Shift = MIRBuilder.buildLShr(IntTy, SrcReg, ShiftAmt); 67528bcb0991SDimitry Andric MIRBuilder.buildTrunc(MI.getOperand(I), Shift); 67538bcb0991SDimitry Andric } 67548bcb0991SDimitry Andric 67558bcb0991SDimitry Andric MI.eraseFromParent(); 67568bcb0991SDimitry Andric return Legalized; 67578bcb0991SDimitry Andric } 67588bcb0991SDimitry Andric 6759e8d8bef9SDimitry Andric /// Lower a vector extract or insert by writing the vector to a stack temporary 6760e8d8bef9SDimitry Andric /// and reloading the element or vector. 6761e8d8bef9SDimitry Andric /// 6762e8d8bef9SDimitry Andric /// %dst = G_EXTRACT_VECTOR_ELT %vec, %idx 6763e8d8bef9SDimitry Andric /// => 6764e8d8bef9SDimitry Andric /// %stack_temp = G_FRAME_INDEX 6765e8d8bef9SDimitry Andric /// G_STORE %vec, %stack_temp 6766e8d8bef9SDimitry Andric /// %idx = clamp(%idx, %vec.getNumElements()) 6767e8d8bef9SDimitry Andric /// %element_ptr = G_PTR_ADD %stack_temp, %idx 6768e8d8bef9SDimitry Andric /// %dst = G_LOAD %element_ptr 6769e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 6770e8d8bef9SDimitry Andric LegalizerHelper::lowerExtractInsertVectorElt(MachineInstr &MI) { 6771e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 6772e8d8bef9SDimitry Andric Register SrcVec = MI.getOperand(1).getReg(); 6773e8d8bef9SDimitry Andric Register InsertVal; 6774e8d8bef9SDimitry Andric if (MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT) 6775e8d8bef9SDimitry Andric InsertVal = MI.getOperand(2).getReg(); 6776e8d8bef9SDimitry Andric 6777e8d8bef9SDimitry Andric Register Idx = MI.getOperand(MI.getNumOperands() - 1).getReg(); 6778e8d8bef9SDimitry Andric 6779e8d8bef9SDimitry Andric LLT VecTy = MRI.getType(SrcVec); 6780e8d8bef9SDimitry Andric LLT EltTy = VecTy.getElementType(); 6781e8d8bef9SDimitry Andric if (!EltTy.isByteSized()) { // Not implemented. 6782e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Can't handle non-byte element vectors yet\n"); 6783e8d8bef9SDimitry Andric return UnableToLegalize; 6784e8d8bef9SDimitry Andric } 6785e8d8bef9SDimitry Andric 6786e8d8bef9SDimitry Andric unsigned EltBytes = EltTy.getSizeInBytes(); 6787e8d8bef9SDimitry Andric Align VecAlign = getStackTemporaryAlignment(VecTy); 6788e8d8bef9SDimitry Andric Align EltAlign; 6789e8d8bef9SDimitry Andric 6790e8d8bef9SDimitry Andric MachinePointerInfo PtrInfo; 6791e8d8bef9SDimitry Andric auto StackTemp = createStackTemporary(TypeSize::Fixed(VecTy.getSizeInBytes()), 6792e8d8bef9SDimitry Andric VecAlign, PtrInfo); 6793e8d8bef9SDimitry Andric MIRBuilder.buildStore(SrcVec, StackTemp, PtrInfo, VecAlign); 6794e8d8bef9SDimitry Andric 6795e8d8bef9SDimitry Andric // Get the pointer to the element, and be sure not to hit undefined behavior 6796e8d8bef9SDimitry Andric // if the index is out of bounds. 6797e8d8bef9SDimitry Andric Register EltPtr = getVectorElementPointer(StackTemp.getReg(0), VecTy, Idx); 6798e8d8bef9SDimitry Andric 6799e8d8bef9SDimitry Andric int64_t IdxVal; 6800e8d8bef9SDimitry Andric if (mi_match(Idx, MRI, m_ICst(IdxVal))) { 6801e8d8bef9SDimitry Andric int64_t Offset = IdxVal * EltBytes; 6802e8d8bef9SDimitry Andric PtrInfo = PtrInfo.getWithOffset(Offset); 6803e8d8bef9SDimitry Andric EltAlign = commonAlignment(VecAlign, Offset); 6804e8d8bef9SDimitry Andric } else { 6805e8d8bef9SDimitry Andric // We lose information with a variable offset. 6806e8d8bef9SDimitry Andric EltAlign = getStackTemporaryAlignment(EltTy); 6807e8d8bef9SDimitry Andric PtrInfo = MachinePointerInfo(MRI.getType(EltPtr).getAddressSpace()); 6808e8d8bef9SDimitry Andric } 6809e8d8bef9SDimitry Andric 6810e8d8bef9SDimitry Andric if (InsertVal) { 6811e8d8bef9SDimitry Andric // Write the inserted element 6812e8d8bef9SDimitry Andric MIRBuilder.buildStore(InsertVal, EltPtr, PtrInfo, EltAlign); 6813e8d8bef9SDimitry Andric 6814e8d8bef9SDimitry Andric // Reload the whole vector. 6815e8d8bef9SDimitry Andric MIRBuilder.buildLoad(DstReg, StackTemp, PtrInfo, VecAlign); 6816e8d8bef9SDimitry Andric } else { 6817e8d8bef9SDimitry Andric MIRBuilder.buildLoad(DstReg, EltPtr, PtrInfo, EltAlign); 6818e8d8bef9SDimitry Andric } 6819e8d8bef9SDimitry Andric 6820e8d8bef9SDimitry Andric MI.eraseFromParent(); 6821e8d8bef9SDimitry Andric return Legalized; 6822e8d8bef9SDimitry Andric } 6823e8d8bef9SDimitry Andric 68248bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 68258bcb0991SDimitry Andric LegalizerHelper::lowerShuffleVector(MachineInstr &MI) { 68268bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 68278bcb0991SDimitry Andric Register Src0Reg = MI.getOperand(1).getReg(); 68288bcb0991SDimitry Andric Register Src1Reg = MI.getOperand(2).getReg(); 68298bcb0991SDimitry Andric LLT Src0Ty = MRI.getType(Src0Reg); 68308bcb0991SDimitry Andric LLT DstTy = MRI.getType(DstReg); 68318bcb0991SDimitry Andric LLT IdxTy = LLT::scalar(32); 68328bcb0991SDimitry Andric 6833480093f4SDimitry Andric ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask(); 68348bcb0991SDimitry Andric 68358bcb0991SDimitry Andric if (DstTy.isScalar()) { 68368bcb0991SDimitry Andric if (Src0Ty.isVector()) 68378bcb0991SDimitry Andric return UnableToLegalize; 68388bcb0991SDimitry Andric 68398bcb0991SDimitry Andric // This is just a SELECT. 68408bcb0991SDimitry Andric assert(Mask.size() == 1 && "Expected a single mask element"); 68418bcb0991SDimitry Andric Register Val; 68428bcb0991SDimitry Andric if (Mask[0] < 0 || Mask[0] > 1) 68438bcb0991SDimitry Andric Val = MIRBuilder.buildUndef(DstTy).getReg(0); 68448bcb0991SDimitry Andric else 68458bcb0991SDimitry Andric Val = Mask[0] == 0 ? Src0Reg : Src1Reg; 68468bcb0991SDimitry Andric MIRBuilder.buildCopy(DstReg, Val); 68478bcb0991SDimitry Andric MI.eraseFromParent(); 68488bcb0991SDimitry Andric return Legalized; 68498bcb0991SDimitry Andric } 68508bcb0991SDimitry Andric 68518bcb0991SDimitry Andric Register Undef; 68528bcb0991SDimitry Andric SmallVector<Register, 32> BuildVec; 68538bcb0991SDimitry Andric LLT EltTy = DstTy.getElementType(); 68548bcb0991SDimitry Andric 68558bcb0991SDimitry Andric for (int Idx : Mask) { 68568bcb0991SDimitry Andric if (Idx < 0) { 68578bcb0991SDimitry Andric if (!Undef.isValid()) 68588bcb0991SDimitry Andric Undef = MIRBuilder.buildUndef(EltTy).getReg(0); 68598bcb0991SDimitry Andric BuildVec.push_back(Undef); 68608bcb0991SDimitry Andric continue; 68618bcb0991SDimitry Andric } 68628bcb0991SDimitry Andric 68638bcb0991SDimitry Andric if (Src0Ty.isScalar()) { 68648bcb0991SDimitry Andric BuildVec.push_back(Idx == 0 ? Src0Reg : Src1Reg); 68658bcb0991SDimitry Andric } else { 68668bcb0991SDimitry Andric int NumElts = Src0Ty.getNumElements(); 68678bcb0991SDimitry Andric Register SrcVec = Idx < NumElts ? Src0Reg : Src1Reg; 68688bcb0991SDimitry Andric int ExtractIdx = Idx < NumElts ? Idx : Idx - NumElts; 68698bcb0991SDimitry Andric auto IdxK = MIRBuilder.buildConstant(IdxTy, ExtractIdx); 68708bcb0991SDimitry Andric auto Extract = MIRBuilder.buildExtractVectorElement(EltTy, SrcVec, IdxK); 68718bcb0991SDimitry Andric BuildVec.push_back(Extract.getReg(0)); 68728bcb0991SDimitry Andric } 68738bcb0991SDimitry Andric } 68748bcb0991SDimitry Andric 68758bcb0991SDimitry Andric MIRBuilder.buildBuildVector(DstReg, BuildVec); 68768bcb0991SDimitry Andric MI.eraseFromParent(); 68778bcb0991SDimitry Andric return Legalized; 68788bcb0991SDimitry Andric } 68798bcb0991SDimitry Andric 68808bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 68818bcb0991SDimitry Andric LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) { 68825ffd83dbSDimitry Andric const auto &MF = *MI.getMF(); 68835ffd83dbSDimitry Andric const auto &TFI = *MF.getSubtarget().getFrameLowering(); 68845ffd83dbSDimitry Andric if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp) 68855ffd83dbSDimitry Andric return UnableToLegalize; 68865ffd83dbSDimitry Andric 68878bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 68888bcb0991SDimitry Andric Register AllocSize = MI.getOperand(1).getReg(); 68895ffd83dbSDimitry Andric Align Alignment = assumeAligned(MI.getOperand(2).getImm()); 68908bcb0991SDimitry Andric 68918bcb0991SDimitry Andric LLT PtrTy = MRI.getType(Dst); 68928bcb0991SDimitry Andric LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits()); 68938bcb0991SDimitry Andric 68948bcb0991SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 68958bcb0991SDimitry Andric auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg); 68968bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp); 68978bcb0991SDimitry Andric 68988bcb0991SDimitry Andric // Subtract the final alloc from the SP. We use G_PTRTOINT here so we don't 68998bcb0991SDimitry Andric // have to generate an extra instruction to negate the alloc and then use 6900480093f4SDimitry Andric // G_PTR_ADD to add the negative offset. 69018bcb0991SDimitry Andric auto Alloc = MIRBuilder.buildSub(IntPtrTy, SPTmp, AllocSize); 69025ffd83dbSDimitry Andric if (Alignment > Align(1)) { 69035ffd83dbSDimitry Andric APInt AlignMask(IntPtrTy.getSizeInBits(), Alignment.value(), true); 69048bcb0991SDimitry Andric AlignMask.negate(); 69058bcb0991SDimitry Andric auto AlignCst = MIRBuilder.buildConstant(IntPtrTy, AlignMask); 69068bcb0991SDimitry Andric Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst); 69078bcb0991SDimitry Andric } 69088bcb0991SDimitry Andric 69098bcb0991SDimitry Andric SPTmp = MIRBuilder.buildCast(PtrTy, Alloc); 69108bcb0991SDimitry Andric MIRBuilder.buildCopy(SPReg, SPTmp); 69118bcb0991SDimitry Andric MIRBuilder.buildCopy(Dst, SPTmp); 69128bcb0991SDimitry Andric 69138bcb0991SDimitry Andric MI.eraseFromParent(); 69148bcb0991SDimitry Andric return Legalized; 69158bcb0991SDimitry Andric } 69168bcb0991SDimitry Andric 69178bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 69188bcb0991SDimitry Andric LegalizerHelper::lowerExtract(MachineInstr &MI) { 69198bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 69208bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 69218bcb0991SDimitry Andric unsigned Offset = MI.getOperand(2).getImm(); 69228bcb0991SDimitry Andric 69238bcb0991SDimitry Andric LLT DstTy = MRI.getType(Dst); 69248bcb0991SDimitry Andric LLT SrcTy = MRI.getType(Src); 69258bcb0991SDimitry Andric 69268bcb0991SDimitry Andric if (DstTy.isScalar() && 69278bcb0991SDimitry Andric (SrcTy.isScalar() || 69288bcb0991SDimitry Andric (SrcTy.isVector() && DstTy == SrcTy.getElementType()))) { 69298bcb0991SDimitry Andric LLT SrcIntTy = SrcTy; 69308bcb0991SDimitry Andric if (!SrcTy.isScalar()) { 69318bcb0991SDimitry Andric SrcIntTy = LLT::scalar(SrcTy.getSizeInBits()); 69328bcb0991SDimitry Andric Src = MIRBuilder.buildBitcast(SrcIntTy, Src).getReg(0); 69338bcb0991SDimitry Andric } 69348bcb0991SDimitry Andric 69358bcb0991SDimitry Andric if (Offset == 0) 69368bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Src); 69378bcb0991SDimitry Andric else { 69388bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(SrcIntTy, Offset); 69398bcb0991SDimitry Andric auto Shr = MIRBuilder.buildLShr(SrcIntTy, Src, ShiftAmt); 69408bcb0991SDimitry Andric MIRBuilder.buildTrunc(Dst, Shr); 69418bcb0991SDimitry Andric } 69428bcb0991SDimitry Andric 69438bcb0991SDimitry Andric MI.eraseFromParent(); 69448bcb0991SDimitry Andric return Legalized; 69458bcb0991SDimitry Andric } 69468bcb0991SDimitry Andric 69478bcb0991SDimitry Andric return UnableToLegalize; 69488bcb0991SDimitry Andric } 69498bcb0991SDimitry Andric 69508bcb0991SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerInsert(MachineInstr &MI) { 69518bcb0991SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 69528bcb0991SDimitry Andric Register Src = MI.getOperand(1).getReg(); 69538bcb0991SDimitry Andric Register InsertSrc = MI.getOperand(2).getReg(); 69548bcb0991SDimitry Andric uint64_t Offset = MI.getOperand(3).getImm(); 69558bcb0991SDimitry Andric 69568bcb0991SDimitry Andric LLT DstTy = MRI.getType(Src); 69578bcb0991SDimitry Andric LLT InsertTy = MRI.getType(InsertSrc); 69588bcb0991SDimitry Andric 69595ffd83dbSDimitry Andric if (InsertTy.isVector() || 69605ffd83dbSDimitry Andric (DstTy.isVector() && DstTy.getElementType() != InsertTy)) 69615ffd83dbSDimitry Andric return UnableToLegalize; 69625ffd83dbSDimitry Andric 69635ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 69645ffd83dbSDimitry Andric if ((DstTy.isPointer() && 69655ffd83dbSDimitry Andric DL.isNonIntegralAddressSpace(DstTy.getAddressSpace())) || 69665ffd83dbSDimitry Andric (InsertTy.isPointer() && 69675ffd83dbSDimitry Andric DL.isNonIntegralAddressSpace(InsertTy.getAddressSpace()))) { 69685ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Not casting non-integral address space integer\n"); 69695ffd83dbSDimitry Andric return UnableToLegalize; 69705ffd83dbSDimitry Andric } 69715ffd83dbSDimitry Andric 69728bcb0991SDimitry Andric LLT IntDstTy = DstTy; 69735ffd83dbSDimitry Andric 69748bcb0991SDimitry Andric if (!DstTy.isScalar()) { 69758bcb0991SDimitry Andric IntDstTy = LLT::scalar(DstTy.getSizeInBits()); 69765ffd83dbSDimitry Andric Src = MIRBuilder.buildCast(IntDstTy, Src).getReg(0); 69775ffd83dbSDimitry Andric } 69785ffd83dbSDimitry Andric 69795ffd83dbSDimitry Andric if (!InsertTy.isScalar()) { 69805ffd83dbSDimitry Andric const LLT IntInsertTy = LLT::scalar(InsertTy.getSizeInBits()); 69815ffd83dbSDimitry Andric InsertSrc = MIRBuilder.buildPtrToInt(IntInsertTy, InsertSrc).getReg(0); 69828bcb0991SDimitry Andric } 69838bcb0991SDimitry Andric 69848bcb0991SDimitry Andric Register ExtInsSrc = MIRBuilder.buildZExt(IntDstTy, InsertSrc).getReg(0); 69858bcb0991SDimitry Andric if (Offset != 0) { 69868bcb0991SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(IntDstTy, Offset); 69878bcb0991SDimitry Andric ExtInsSrc = MIRBuilder.buildShl(IntDstTy, ExtInsSrc, ShiftAmt).getReg(0); 69888bcb0991SDimitry Andric } 69898bcb0991SDimitry Andric 69905ffd83dbSDimitry Andric APInt MaskVal = APInt::getBitsSetWithWrap( 69915ffd83dbSDimitry Andric DstTy.getSizeInBits(), Offset + InsertTy.getSizeInBits(), Offset); 69928bcb0991SDimitry Andric 69938bcb0991SDimitry Andric auto Mask = MIRBuilder.buildConstant(IntDstTy, MaskVal); 69948bcb0991SDimitry Andric auto MaskedSrc = MIRBuilder.buildAnd(IntDstTy, Src, Mask); 69958bcb0991SDimitry Andric auto Or = MIRBuilder.buildOr(IntDstTy, MaskedSrc, ExtInsSrc); 69968bcb0991SDimitry Andric 69975ffd83dbSDimitry Andric MIRBuilder.buildCast(Dst, Or); 69988bcb0991SDimitry Andric MI.eraseFromParent(); 69998bcb0991SDimitry Andric return Legalized; 70008bcb0991SDimitry Andric } 70018bcb0991SDimitry Andric 70028bcb0991SDimitry Andric LegalizerHelper::LegalizeResult 70038bcb0991SDimitry Andric LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) { 70048bcb0991SDimitry Andric Register Dst0 = MI.getOperand(0).getReg(); 70058bcb0991SDimitry Andric Register Dst1 = MI.getOperand(1).getReg(); 70068bcb0991SDimitry Andric Register LHS = MI.getOperand(2).getReg(); 70078bcb0991SDimitry Andric Register RHS = MI.getOperand(3).getReg(); 70088bcb0991SDimitry Andric const bool IsAdd = MI.getOpcode() == TargetOpcode::G_SADDO; 70098bcb0991SDimitry Andric 70108bcb0991SDimitry Andric LLT Ty = MRI.getType(Dst0); 70118bcb0991SDimitry Andric LLT BoolTy = MRI.getType(Dst1); 70128bcb0991SDimitry Andric 70138bcb0991SDimitry Andric if (IsAdd) 70148bcb0991SDimitry Andric MIRBuilder.buildAdd(Dst0, LHS, RHS); 70158bcb0991SDimitry Andric else 70168bcb0991SDimitry Andric MIRBuilder.buildSub(Dst0, LHS, RHS); 70178bcb0991SDimitry Andric 70188bcb0991SDimitry Andric // TODO: If SADDSAT/SSUBSAT is legal, compare results to detect overflow. 70198bcb0991SDimitry Andric 70208bcb0991SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 70218bcb0991SDimitry Andric 70228bcb0991SDimitry Andric // For an addition, the result should be less than one of the operands (LHS) 70238bcb0991SDimitry Andric // if and only if the other operand (RHS) is negative, otherwise there will 70248bcb0991SDimitry Andric // be overflow. 70258bcb0991SDimitry Andric // For a subtraction, the result should be less than one of the operands 70268bcb0991SDimitry Andric // (LHS) if and only if the other operand (RHS) is (non-zero) positive, 70278bcb0991SDimitry Andric // otherwise there will be overflow. 70288bcb0991SDimitry Andric auto ResultLowerThanLHS = 70298bcb0991SDimitry Andric MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Dst0, LHS); 70308bcb0991SDimitry Andric auto ConditionRHS = MIRBuilder.buildICmp( 70318bcb0991SDimitry Andric IsAdd ? CmpInst::ICMP_SLT : CmpInst::ICMP_SGT, BoolTy, RHS, Zero); 70328bcb0991SDimitry Andric 70338bcb0991SDimitry Andric MIRBuilder.buildXor(Dst1, ConditionRHS, ResultLowerThanLHS); 70348bcb0991SDimitry Andric MI.eraseFromParent(); 70358bcb0991SDimitry Andric return Legalized; 70368bcb0991SDimitry Andric } 7037480093f4SDimitry Andric 7038480093f4SDimitry Andric LegalizerHelper::LegalizeResult 7039e8d8bef9SDimitry Andric LegalizerHelper::lowerAddSubSatToMinMax(MachineInstr &MI) { 7040e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 7041e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 7042e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 7043e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 7044e8d8bef9SDimitry Andric bool IsSigned; 7045e8d8bef9SDimitry Andric bool IsAdd; 7046e8d8bef9SDimitry Andric unsigned BaseOp; 7047e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 7048e8d8bef9SDimitry Andric default: 7049e8d8bef9SDimitry Andric llvm_unreachable("unexpected addsat/subsat opcode"); 7050e8d8bef9SDimitry Andric case TargetOpcode::G_UADDSAT: 7051e8d8bef9SDimitry Andric IsSigned = false; 7052e8d8bef9SDimitry Andric IsAdd = true; 7053e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_ADD; 7054e8d8bef9SDimitry Andric break; 7055e8d8bef9SDimitry Andric case TargetOpcode::G_SADDSAT: 7056e8d8bef9SDimitry Andric IsSigned = true; 7057e8d8bef9SDimitry Andric IsAdd = true; 7058e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_ADD; 7059e8d8bef9SDimitry Andric break; 7060e8d8bef9SDimitry Andric case TargetOpcode::G_USUBSAT: 7061e8d8bef9SDimitry Andric IsSigned = false; 7062e8d8bef9SDimitry Andric IsAdd = false; 7063e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_SUB; 7064e8d8bef9SDimitry Andric break; 7065e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBSAT: 7066e8d8bef9SDimitry Andric IsSigned = true; 7067e8d8bef9SDimitry Andric IsAdd = false; 7068e8d8bef9SDimitry Andric BaseOp = TargetOpcode::G_SUB; 7069e8d8bef9SDimitry Andric break; 7070e8d8bef9SDimitry Andric } 7071e8d8bef9SDimitry Andric 7072e8d8bef9SDimitry Andric if (IsSigned) { 7073e8d8bef9SDimitry Andric // sadd.sat(a, b) -> 7074e8d8bef9SDimitry Andric // hi = 0x7fffffff - smax(a, 0) 7075e8d8bef9SDimitry Andric // lo = 0x80000000 - smin(a, 0) 7076e8d8bef9SDimitry Andric // a + smin(smax(lo, b), hi) 7077e8d8bef9SDimitry Andric // ssub.sat(a, b) -> 7078e8d8bef9SDimitry Andric // lo = smax(a, -1) - 0x7fffffff 7079e8d8bef9SDimitry Andric // hi = smin(a, -1) - 0x80000000 7080e8d8bef9SDimitry Andric // a - smin(smax(lo, b), hi) 7081e8d8bef9SDimitry Andric // TODO: AMDGPU can use a "median of 3" instruction here: 7082e8d8bef9SDimitry Andric // a +/- med3(lo, b, hi) 7083e8d8bef9SDimitry Andric uint64_t NumBits = Ty.getScalarSizeInBits(); 7084e8d8bef9SDimitry Andric auto MaxVal = 7085e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMaxValue(NumBits)); 7086e8d8bef9SDimitry Andric auto MinVal = 7087e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(NumBits)); 7088e8d8bef9SDimitry Andric MachineInstrBuilder Hi, Lo; 7089e8d8bef9SDimitry Andric if (IsAdd) { 7090e8d8bef9SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0); 7091e8d8bef9SDimitry Andric Hi = MIRBuilder.buildSub(Ty, MaxVal, MIRBuilder.buildSMax(Ty, LHS, Zero)); 7092e8d8bef9SDimitry Andric Lo = MIRBuilder.buildSub(Ty, MinVal, MIRBuilder.buildSMin(Ty, LHS, Zero)); 7093e8d8bef9SDimitry Andric } else { 7094e8d8bef9SDimitry Andric auto NegOne = MIRBuilder.buildConstant(Ty, -1); 7095e8d8bef9SDimitry Andric Lo = MIRBuilder.buildSub(Ty, MIRBuilder.buildSMax(Ty, LHS, NegOne), 7096e8d8bef9SDimitry Andric MaxVal); 7097e8d8bef9SDimitry Andric Hi = MIRBuilder.buildSub(Ty, MIRBuilder.buildSMin(Ty, LHS, NegOne), 7098e8d8bef9SDimitry Andric MinVal); 7099e8d8bef9SDimitry Andric } 7100e8d8bef9SDimitry Andric auto RHSClamped = 7101e8d8bef9SDimitry Andric MIRBuilder.buildSMin(Ty, MIRBuilder.buildSMax(Ty, Lo, RHS), Hi); 7102e8d8bef9SDimitry Andric MIRBuilder.buildInstr(BaseOp, {Res}, {LHS, RHSClamped}); 7103e8d8bef9SDimitry Andric } else { 7104e8d8bef9SDimitry Andric // uadd.sat(a, b) -> a + umin(~a, b) 7105e8d8bef9SDimitry Andric // usub.sat(a, b) -> a - umin(a, b) 7106e8d8bef9SDimitry Andric Register Not = IsAdd ? MIRBuilder.buildNot(Ty, LHS).getReg(0) : LHS; 7107e8d8bef9SDimitry Andric auto Min = MIRBuilder.buildUMin(Ty, Not, RHS); 7108e8d8bef9SDimitry Andric MIRBuilder.buildInstr(BaseOp, {Res}, {LHS, Min}); 7109e8d8bef9SDimitry Andric } 7110e8d8bef9SDimitry Andric 7111e8d8bef9SDimitry Andric MI.eraseFromParent(); 7112e8d8bef9SDimitry Andric return Legalized; 7113e8d8bef9SDimitry Andric } 7114e8d8bef9SDimitry Andric 7115e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7116e8d8bef9SDimitry Andric LegalizerHelper::lowerAddSubSatToAddoSubo(MachineInstr &MI) { 7117e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 7118e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 7119e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 7120e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 7121e8d8bef9SDimitry Andric LLT BoolTy = Ty.changeElementSize(1); 7122e8d8bef9SDimitry Andric bool IsSigned; 7123e8d8bef9SDimitry Andric bool IsAdd; 7124e8d8bef9SDimitry Andric unsigned OverflowOp; 7125e8d8bef9SDimitry Andric switch (MI.getOpcode()) { 7126e8d8bef9SDimitry Andric default: 7127e8d8bef9SDimitry Andric llvm_unreachable("unexpected addsat/subsat opcode"); 7128e8d8bef9SDimitry Andric case TargetOpcode::G_UADDSAT: 7129e8d8bef9SDimitry Andric IsSigned = false; 7130e8d8bef9SDimitry Andric IsAdd = true; 7131e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_UADDO; 7132e8d8bef9SDimitry Andric break; 7133e8d8bef9SDimitry Andric case TargetOpcode::G_SADDSAT: 7134e8d8bef9SDimitry Andric IsSigned = true; 7135e8d8bef9SDimitry Andric IsAdd = true; 7136e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_SADDO; 7137e8d8bef9SDimitry Andric break; 7138e8d8bef9SDimitry Andric case TargetOpcode::G_USUBSAT: 7139e8d8bef9SDimitry Andric IsSigned = false; 7140e8d8bef9SDimitry Andric IsAdd = false; 7141e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_USUBO; 7142e8d8bef9SDimitry Andric break; 7143e8d8bef9SDimitry Andric case TargetOpcode::G_SSUBSAT: 7144e8d8bef9SDimitry Andric IsSigned = true; 7145e8d8bef9SDimitry Andric IsAdd = false; 7146e8d8bef9SDimitry Andric OverflowOp = TargetOpcode::G_SSUBO; 7147e8d8bef9SDimitry Andric break; 7148e8d8bef9SDimitry Andric } 7149e8d8bef9SDimitry Andric 7150e8d8bef9SDimitry Andric auto OverflowRes = 7151e8d8bef9SDimitry Andric MIRBuilder.buildInstr(OverflowOp, {Ty, BoolTy}, {LHS, RHS}); 7152e8d8bef9SDimitry Andric Register Tmp = OverflowRes.getReg(0); 7153e8d8bef9SDimitry Andric Register Ov = OverflowRes.getReg(1); 7154e8d8bef9SDimitry Andric MachineInstrBuilder Clamp; 7155e8d8bef9SDimitry Andric if (IsSigned) { 7156e8d8bef9SDimitry Andric // sadd.sat(a, b) -> 7157e8d8bef9SDimitry Andric // {tmp, ov} = saddo(a, b) 7158e8d8bef9SDimitry Andric // ov ? (tmp >>s 31) + 0x80000000 : r 7159e8d8bef9SDimitry Andric // ssub.sat(a, b) -> 7160e8d8bef9SDimitry Andric // {tmp, ov} = ssubo(a, b) 7161e8d8bef9SDimitry Andric // ov ? (tmp >>s 31) + 0x80000000 : r 7162e8d8bef9SDimitry Andric uint64_t NumBits = Ty.getScalarSizeInBits(); 7163e8d8bef9SDimitry Andric auto ShiftAmount = MIRBuilder.buildConstant(Ty, NumBits - 1); 7164e8d8bef9SDimitry Andric auto Sign = MIRBuilder.buildAShr(Ty, Tmp, ShiftAmount); 7165e8d8bef9SDimitry Andric auto MinVal = 7166e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(NumBits)); 7167e8d8bef9SDimitry Andric Clamp = MIRBuilder.buildAdd(Ty, Sign, MinVal); 7168e8d8bef9SDimitry Andric } else { 7169e8d8bef9SDimitry Andric // uadd.sat(a, b) -> 7170e8d8bef9SDimitry Andric // {tmp, ov} = uaddo(a, b) 7171e8d8bef9SDimitry Andric // ov ? 0xffffffff : tmp 7172e8d8bef9SDimitry Andric // usub.sat(a, b) -> 7173e8d8bef9SDimitry Andric // {tmp, ov} = usubo(a, b) 7174e8d8bef9SDimitry Andric // ov ? 0 : tmp 7175e8d8bef9SDimitry Andric Clamp = MIRBuilder.buildConstant(Ty, IsAdd ? -1 : 0); 7176e8d8bef9SDimitry Andric } 7177e8d8bef9SDimitry Andric MIRBuilder.buildSelect(Res, Ov, Clamp, Tmp); 7178e8d8bef9SDimitry Andric 7179e8d8bef9SDimitry Andric MI.eraseFromParent(); 7180e8d8bef9SDimitry Andric return Legalized; 7181e8d8bef9SDimitry Andric } 7182e8d8bef9SDimitry Andric 7183e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7184e8d8bef9SDimitry Andric LegalizerHelper::lowerShlSat(MachineInstr &MI) { 7185e8d8bef9SDimitry Andric assert((MI.getOpcode() == TargetOpcode::G_SSHLSAT || 7186e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::G_USHLSAT) && 7187e8d8bef9SDimitry Andric "Expected shlsat opcode!"); 7188e8d8bef9SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SSHLSAT; 7189e8d8bef9SDimitry Andric Register Res = MI.getOperand(0).getReg(); 7190e8d8bef9SDimitry Andric Register LHS = MI.getOperand(1).getReg(); 7191e8d8bef9SDimitry Andric Register RHS = MI.getOperand(2).getReg(); 7192e8d8bef9SDimitry Andric LLT Ty = MRI.getType(Res); 7193e8d8bef9SDimitry Andric LLT BoolTy = Ty.changeElementSize(1); 7194e8d8bef9SDimitry Andric 7195e8d8bef9SDimitry Andric unsigned BW = Ty.getScalarSizeInBits(); 7196e8d8bef9SDimitry Andric auto Result = MIRBuilder.buildShl(Ty, LHS, RHS); 7197e8d8bef9SDimitry Andric auto Orig = IsSigned ? MIRBuilder.buildAShr(Ty, Result, RHS) 7198e8d8bef9SDimitry Andric : MIRBuilder.buildLShr(Ty, Result, RHS); 7199e8d8bef9SDimitry Andric 7200e8d8bef9SDimitry Andric MachineInstrBuilder SatVal; 7201e8d8bef9SDimitry Andric if (IsSigned) { 7202e8d8bef9SDimitry Andric auto SatMin = MIRBuilder.buildConstant(Ty, APInt::getSignedMinValue(BW)); 7203e8d8bef9SDimitry Andric auto SatMax = MIRBuilder.buildConstant(Ty, APInt::getSignedMaxValue(BW)); 7204e8d8bef9SDimitry Andric auto Cmp = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, LHS, 7205e8d8bef9SDimitry Andric MIRBuilder.buildConstant(Ty, 0)); 7206e8d8bef9SDimitry Andric SatVal = MIRBuilder.buildSelect(Ty, Cmp, SatMin, SatMax); 7207e8d8bef9SDimitry Andric } else { 7208e8d8bef9SDimitry Andric SatVal = MIRBuilder.buildConstant(Ty, APInt::getMaxValue(BW)); 7209e8d8bef9SDimitry Andric } 7210e8d8bef9SDimitry Andric auto Ov = MIRBuilder.buildICmp(CmpInst::ICMP_NE, BoolTy, LHS, Orig); 7211e8d8bef9SDimitry Andric MIRBuilder.buildSelect(Res, Ov, SatVal, Result); 7212e8d8bef9SDimitry Andric 7213e8d8bef9SDimitry Andric MI.eraseFromParent(); 7214e8d8bef9SDimitry Andric return Legalized; 7215e8d8bef9SDimitry Andric } 7216e8d8bef9SDimitry Andric 7217e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7218480093f4SDimitry Andric LegalizerHelper::lowerBswap(MachineInstr &MI) { 7219480093f4SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7220480093f4SDimitry Andric Register Src = MI.getOperand(1).getReg(); 7221480093f4SDimitry Andric const LLT Ty = MRI.getType(Src); 72225ffd83dbSDimitry Andric unsigned SizeInBytes = (Ty.getScalarSizeInBits() + 7) / 8; 7223480093f4SDimitry Andric unsigned BaseShiftAmt = (SizeInBytes - 1) * 8; 7224480093f4SDimitry Andric 7225480093f4SDimitry Andric // Swap most and least significant byte, set remaining bytes in Res to zero. 7226480093f4SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt); 7227480093f4SDimitry Andric auto LSByteShiftedLeft = MIRBuilder.buildShl(Ty, Src, ShiftAmt); 7228480093f4SDimitry Andric auto MSByteShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); 7229480093f4SDimitry Andric auto Res = MIRBuilder.buildOr(Ty, MSByteShiftedRight, LSByteShiftedLeft); 7230480093f4SDimitry Andric 7231480093f4SDimitry Andric // Set i-th high/low byte in Res to i-th low/high byte from Src. 7232480093f4SDimitry Andric for (unsigned i = 1; i < SizeInBytes / 2; ++i) { 7233480093f4SDimitry Andric // AND with Mask leaves byte i unchanged and sets remaining bytes to 0. 7234480093f4SDimitry Andric APInt APMask(SizeInBytes * 8, 0xFF << (i * 8)); 7235480093f4SDimitry Andric auto Mask = MIRBuilder.buildConstant(Ty, APMask); 7236480093f4SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt - 16 * i); 7237480093f4SDimitry Andric // Low byte shifted left to place of high byte: (Src & Mask) << ShiftAmt. 7238480093f4SDimitry Andric auto LoByte = MIRBuilder.buildAnd(Ty, Src, Mask); 7239480093f4SDimitry Andric auto LoShiftedLeft = MIRBuilder.buildShl(Ty, LoByte, ShiftAmt); 7240480093f4SDimitry Andric Res = MIRBuilder.buildOr(Ty, Res, LoShiftedLeft); 7241480093f4SDimitry Andric // High byte shifted right to place of low byte: (Src >> ShiftAmt) & Mask. 7242480093f4SDimitry Andric auto SrcShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); 7243480093f4SDimitry Andric auto HiShiftedRight = MIRBuilder.buildAnd(Ty, SrcShiftedRight, Mask); 7244480093f4SDimitry Andric Res = MIRBuilder.buildOr(Ty, Res, HiShiftedRight); 7245480093f4SDimitry Andric } 7246480093f4SDimitry Andric Res.getInstr()->getOperand(0).setReg(Dst); 7247480093f4SDimitry Andric 7248480093f4SDimitry Andric MI.eraseFromParent(); 7249480093f4SDimitry Andric return Legalized; 7250480093f4SDimitry Andric } 7251480093f4SDimitry Andric 7252480093f4SDimitry Andric //{ (Src & Mask) >> N } | { (Src << N) & Mask } 7253480093f4SDimitry Andric static MachineInstrBuilder SwapN(unsigned N, DstOp Dst, MachineIRBuilder &B, 7254480093f4SDimitry Andric MachineInstrBuilder Src, APInt Mask) { 7255480093f4SDimitry Andric const LLT Ty = Dst.getLLTTy(*B.getMRI()); 7256480093f4SDimitry Andric MachineInstrBuilder C_N = B.buildConstant(Ty, N); 7257480093f4SDimitry Andric MachineInstrBuilder MaskLoNTo0 = B.buildConstant(Ty, Mask); 7258480093f4SDimitry Andric auto LHS = B.buildLShr(Ty, B.buildAnd(Ty, Src, MaskLoNTo0), C_N); 7259480093f4SDimitry Andric auto RHS = B.buildAnd(Ty, B.buildShl(Ty, Src, C_N), MaskLoNTo0); 7260480093f4SDimitry Andric return B.buildOr(Dst, LHS, RHS); 7261480093f4SDimitry Andric } 7262480093f4SDimitry Andric 7263480093f4SDimitry Andric LegalizerHelper::LegalizeResult 7264480093f4SDimitry Andric LegalizerHelper::lowerBitreverse(MachineInstr &MI) { 7265480093f4SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7266480093f4SDimitry Andric Register Src = MI.getOperand(1).getReg(); 7267480093f4SDimitry Andric const LLT Ty = MRI.getType(Src); 7268480093f4SDimitry Andric unsigned Size = Ty.getSizeInBits(); 7269480093f4SDimitry Andric 7270480093f4SDimitry Andric MachineInstrBuilder BSWAP = 7271480093f4SDimitry Andric MIRBuilder.buildInstr(TargetOpcode::G_BSWAP, {Ty}, {Src}); 7272480093f4SDimitry Andric 7273480093f4SDimitry Andric // swap high and low 4 bits in 8 bit blocks 7654|3210 -> 3210|7654 7274480093f4SDimitry Andric // [(val & 0xF0F0F0F0) >> 4] | [(val & 0x0F0F0F0F) << 4] 7275480093f4SDimitry Andric // -> [(val & 0xF0F0F0F0) >> 4] | [(val << 4) & 0xF0F0F0F0] 7276480093f4SDimitry Andric MachineInstrBuilder Swap4 = 7277480093f4SDimitry Andric SwapN(4, Ty, MIRBuilder, BSWAP, APInt::getSplat(Size, APInt(8, 0xF0))); 7278480093f4SDimitry Andric 7279480093f4SDimitry Andric // swap high and low 2 bits in 4 bit blocks 32|10 76|54 -> 10|32 54|76 7280480093f4SDimitry Andric // [(val & 0xCCCCCCCC) >> 2] & [(val & 0x33333333) << 2] 7281480093f4SDimitry Andric // -> [(val & 0xCCCCCCCC) >> 2] & [(val << 2) & 0xCCCCCCCC] 7282480093f4SDimitry Andric MachineInstrBuilder Swap2 = 7283480093f4SDimitry Andric SwapN(2, Ty, MIRBuilder, Swap4, APInt::getSplat(Size, APInt(8, 0xCC))); 7284480093f4SDimitry Andric 7285480093f4SDimitry 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 7286480093f4SDimitry Andric // [(val & 0xAAAAAAAA) >> 1] & [(val & 0x55555555) << 1] 7287480093f4SDimitry Andric // -> [(val & 0xAAAAAAAA) >> 1] & [(val << 1) & 0xAAAAAAAA] 7288480093f4SDimitry Andric SwapN(1, Dst, MIRBuilder, Swap2, APInt::getSplat(Size, APInt(8, 0xAA))); 7289480093f4SDimitry Andric 7290480093f4SDimitry Andric MI.eraseFromParent(); 7291480093f4SDimitry Andric return Legalized; 7292480093f4SDimitry Andric } 7293480093f4SDimitry Andric 7294480093f4SDimitry Andric LegalizerHelper::LegalizeResult 72955ffd83dbSDimitry Andric LegalizerHelper::lowerReadWriteRegister(MachineInstr &MI) { 7296480093f4SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 72975ffd83dbSDimitry Andric 72985ffd83dbSDimitry Andric bool IsRead = MI.getOpcode() == TargetOpcode::G_READ_REGISTER; 72995ffd83dbSDimitry Andric int NameOpIdx = IsRead ? 1 : 0; 73005ffd83dbSDimitry Andric int ValRegIndex = IsRead ? 0 : 1; 73015ffd83dbSDimitry Andric 73025ffd83dbSDimitry Andric Register ValReg = MI.getOperand(ValRegIndex).getReg(); 73035ffd83dbSDimitry Andric const LLT Ty = MRI.getType(ValReg); 73045ffd83dbSDimitry Andric const MDString *RegStr = cast<MDString>( 73055ffd83dbSDimitry Andric cast<MDNode>(MI.getOperand(NameOpIdx).getMetadata())->getOperand(0)); 73065ffd83dbSDimitry Andric 7307e8d8bef9SDimitry Andric Register PhysReg = TLI.getRegisterByName(RegStr->getString().data(), Ty, MF); 73085ffd83dbSDimitry Andric if (!PhysReg.isValid()) 7309480093f4SDimitry Andric return UnableToLegalize; 7310480093f4SDimitry Andric 73115ffd83dbSDimitry Andric if (IsRead) 73125ffd83dbSDimitry Andric MIRBuilder.buildCopy(ValReg, PhysReg); 73135ffd83dbSDimitry Andric else 73145ffd83dbSDimitry Andric MIRBuilder.buildCopy(PhysReg, ValReg); 73155ffd83dbSDimitry Andric 7316480093f4SDimitry Andric MI.eraseFromParent(); 7317480093f4SDimitry Andric return Legalized; 7318480093f4SDimitry Andric } 7319e8d8bef9SDimitry Andric 7320e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult 7321e8d8bef9SDimitry Andric LegalizerHelper::lowerSMULH_UMULH(MachineInstr &MI) { 7322e8d8bef9SDimitry Andric bool IsSigned = MI.getOpcode() == TargetOpcode::G_SMULH; 7323e8d8bef9SDimitry Andric unsigned ExtOp = IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; 7324e8d8bef9SDimitry Andric Register Result = MI.getOperand(0).getReg(); 7325e8d8bef9SDimitry Andric LLT OrigTy = MRI.getType(Result); 7326e8d8bef9SDimitry Andric auto SizeInBits = OrigTy.getScalarSizeInBits(); 7327e8d8bef9SDimitry Andric LLT WideTy = OrigTy.changeElementSize(SizeInBits * 2); 7328e8d8bef9SDimitry Andric 7329e8d8bef9SDimitry Andric auto LHS = MIRBuilder.buildInstr(ExtOp, {WideTy}, {MI.getOperand(1)}); 7330e8d8bef9SDimitry Andric auto RHS = MIRBuilder.buildInstr(ExtOp, {WideTy}, {MI.getOperand(2)}); 7331e8d8bef9SDimitry Andric auto Mul = MIRBuilder.buildMul(WideTy, LHS, RHS); 7332e8d8bef9SDimitry Andric unsigned ShiftOp = IsSigned ? TargetOpcode::G_ASHR : TargetOpcode::G_LSHR; 7333e8d8bef9SDimitry Andric 7334e8d8bef9SDimitry Andric auto ShiftAmt = MIRBuilder.buildConstant(WideTy, SizeInBits); 7335e8d8bef9SDimitry Andric auto Shifted = MIRBuilder.buildInstr(ShiftOp, {WideTy}, {Mul, ShiftAmt}); 7336e8d8bef9SDimitry Andric MIRBuilder.buildTrunc(Result, Shifted); 7337e8d8bef9SDimitry Andric 7338e8d8bef9SDimitry Andric MI.eraseFromParent(); 7339e8d8bef9SDimitry Andric return Legalized; 7340e8d8bef9SDimitry Andric } 7341e8d8bef9SDimitry Andric 7342e8d8bef9SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerSelect(MachineInstr &MI) { 7343e8d8bef9SDimitry Andric // Implement vector G_SELECT in terms of XOR, AND, OR. 7344e8d8bef9SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 7345e8d8bef9SDimitry Andric Register MaskReg = MI.getOperand(1).getReg(); 7346e8d8bef9SDimitry Andric Register Op1Reg = MI.getOperand(2).getReg(); 7347e8d8bef9SDimitry Andric Register Op2Reg = MI.getOperand(3).getReg(); 7348e8d8bef9SDimitry Andric LLT DstTy = MRI.getType(DstReg); 7349e8d8bef9SDimitry Andric LLT MaskTy = MRI.getType(MaskReg); 7350e8d8bef9SDimitry Andric LLT Op1Ty = MRI.getType(Op1Reg); 7351e8d8bef9SDimitry Andric if (!DstTy.isVector()) 7352e8d8bef9SDimitry Andric return UnableToLegalize; 7353e8d8bef9SDimitry Andric 7354e8d8bef9SDimitry Andric // Vector selects can have a scalar predicate. If so, splat into a vector and 7355e8d8bef9SDimitry Andric // finish for later legalization attempts to try again. 7356e8d8bef9SDimitry Andric if (MaskTy.isScalar()) { 7357e8d8bef9SDimitry Andric Register MaskElt = MaskReg; 7358e8d8bef9SDimitry Andric if (MaskTy.getSizeInBits() < DstTy.getScalarSizeInBits()) 7359e8d8bef9SDimitry Andric MaskElt = MIRBuilder.buildSExt(DstTy.getElementType(), MaskElt).getReg(0); 7360e8d8bef9SDimitry Andric // Generate a vector splat idiom to be pattern matched later. 7361e8d8bef9SDimitry Andric auto ShufSplat = MIRBuilder.buildShuffleSplat(DstTy, MaskElt); 7362e8d8bef9SDimitry Andric Observer.changingInstr(MI); 7363e8d8bef9SDimitry Andric MI.getOperand(1).setReg(ShufSplat.getReg(0)); 7364e8d8bef9SDimitry Andric Observer.changedInstr(MI); 7365e8d8bef9SDimitry Andric return Legalized; 7366e8d8bef9SDimitry Andric } 7367e8d8bef9SDimitry Andric 7368e8d8bef9SDimitry Andric if (MaskTy.getSizeInBits() != Op1Ty.getSizeInBits()) { 7369e8d8bef9SDimitry Andric return UnableToLegalize; 7370e8d8bef9SDimitry Andric } 7371e8d8bef9SDimitry Andric 7372e8d8bef9SDimitry Andric auto NotMask = MIRBuilder.buildNot(MaskTy, MaskReg); 7373e8d8bef9SDimitry Andric auto NewOp1 = MIRBuilder.buildAnd(MaskTy, Op1Reg, MaskReg); 7374e8d8bef9SDimitry Andric auto NewOp2 = MIRBuilder.buildAnd(MaskTy, Op2Reg, NotMask); 7375e8d8bef9SDimitry Andric MIRBuilder.buildOr(DstReg, NewOp1, NewOp2); 7376e8d8bef9SDimitry Andric MI.eraseFromParent(); 7377e8d8bef9SDimitry Andric return Legalized; 7378e8d8bef9SDimitry Andric } 7379fe6060f1SDimitry Andric 7380fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult LegalizerHelper::lowerDIVREM(MachineInstr &MI) { 7381fe6060f1SDimitry Andric // Split DIVREM into individual instructions. 7382fe6060f1SDimitry Andric unsigned Opcode = MI.getOpcode(); 7383fe6060f1SDimitry Andric 7384fe6060f1SDimitry Andric MIRBuilder.buildInstr( 7385fe6060f1SDimitry Andric Opcode == TargetOpcode::G_SDIVREM ? TargetOpcode::G_SDIV 7386fe6060f1SDimitry Andric : TargetOpcode::G_UDIV, 7387fe6060f1SDimitry Andric {MI.getOperand(0).getReg()}, {MI.getOperand(2), MI.getOperand(3)}); 7388fe6060f1SDimitry Andric MIRBuilder.buildInstr( 7389fe6060f1SDimitry Andric Opcode == TargetOpcode::G_SDIVREM ? TargetOpcode::G_SREM 7390fe6060f1SDimitry Andric : TargetOpcode::G_UREM, 7391fe6060f1SDimitry Andric {MI.getOperand(1).getReg()}, {MI.getOperand(2), MI.getOperand(3)}); 7392fe6060f1SDimitry Andric MI.eraseFromParent(); 7393fe6060f1SDimitry Andric return Legalized; 7394fe6060f1SDimitry Andric } 7395fe6060f1SDimitry Andric 7396fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 7397fe6060f1SDimitry Andric LegalizerHelper::lowerAbsToAddXor(MachineInstr &MI) { 7398fe6060f1SDimitry Andric // Expand %res = G_ABS %a into: 7399fe6060f1SDimitry Andric // %v1 = G_ASHR %a, scalar_size-1 7400fe6060f1SDimitry Andric // %v2 = G_ADD %a, %v1 7401fe6060f1SDimitry Andric // %res = G_XOR %v2, %v1 7402fe6060f1SDimitry Andric LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); 7403fe6060f1SDimitry Andric Register OpReg = MI.getOperand(1).getReg(); 7404fe6060f1SDimitry Andric auto ShiftAmt = 7405fe6060f1SDimitry Andric MIRBuilder.buildConstant(DstTy, DstTy.getScalarSizeInBits() - 1); 7406fe6060f1SDimitry Andric auto Shift = MIRBuilder.buildAShr(DstTy, OpReg, ShiftAmt); 7407fe6060f1SDimitry Andric auto Add = MIRBuilder.buildAdd(DstTy, OpReg, Shift); 7408fe6060f1SDimitry Andric MIRBuilder.buildXor(MI.getOperand(0).getReg(), Add, Shift); 7409fe6060f1SDimitry Andric MI.eraseFromParent(); 7410fe6060f1SDimitry Andric return Legalized; 7411fe6060f1SDimitry Andric } 7412fe6060f1SDimitry Andric 7413fe6060f1SDimitry Andric LegalizerHelper::LegalizeResult 7414fe6060f1SDimitry Andric LegalizerHelper::lowerAbsToMaxNeg(MachineInstr &MI) { 7415fe6060f1SDimitry Andric // Expand %res = G_ABS %a into: 7416fe6060f1SDimitry Andric // %v1 = G_CONSTANT 0 7417fe6060f1SDimitry Andric // %v2 = G_SUB %v1, %a 7418fe6060f1SDimitry Andric // %res = G_SMAX %a, %v2 7419fe6060f1SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 7420fe6060f1SDimitry Andric LLT Ty = MRI.getType(SrcReg); 7421fe6060f1SDimitry Andric auto Zero = MIRBuilder.buildConstant(Ty, 0).getReg(0); 7422fe6060f1SDimitry Andric auto Sub = MIRBuilder.buildSub(Ty, Zero, SrcReg).getReg(0); 7423fe6060f1SDimitry Andric MIRBuilder.buildSMax(MI.getOperand(0), SrcReg, Sub); 7424fe6060f1SDimitry Andric MI.eraseFromParent(); 7425fe6060f1SDimitry Andric return Legalized; 7426fe6060f1SDimitry Andric } 7427349cc55cSDimitry Andric 7428349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7429349cc55cSDimitry Andric LegalizerHelper::lowerVectorReduction(MachineInstr &MI) { 7430349cc55cSDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 7431349cc55cSDimitry Andric LLT SrcTy = MRI.getType(SrcReg); 7432349cc55cSDimitry Andric LLT DstTy = MRI.getType(SrcReg); 7433349cc55cSDimitry Andric 7434349cc55cSDimitry Andric // The source could be a scalar if the IR type was <1 x sN>. 7435349cc55cSDimitry Andric if (SrcTy.isScalar()) { 7436349cc55cSDimitry Andric if (DstTy.getSizeInBits() > SrcTy.getSizeInBits()) 7437349cc55cSDimitry Andric return UnableToLegalize; // FIXME: handle extension. 7438349cc55cSDimitry Andric // This can be just a plain copy. 7439349cc55cSDimitry Andric Observer.changingInstr(MI); 7440349cc55cSDimitry Andric MI.setDesc(MIRBuilder.getTII().get(TargetOpcode::COPY)); 7441349cc55cSDimitry Andric Observer.changedInstr(MI); 7442349cc55cSDimitry Andric return Legalized; 7443349cc55cSDimitry Andric } 7444349cc55cSDimitry Andric return UnableToLegalize;; 7445349cc55cSDimitry Andric } 7446349cc55cSDimitry Andric 7447349cc55cSDimitry Andric static bool shouldLowerMemFuncForSize(const MachineFunction &MF) { 7448349cc55cSDimitry Andric // On Darwin, -Os means optimize for size without hurting performance, so 7449349cc55cSDimitry Andric // only really optimize for size when -Oz (MinSize) is used. 7450349cc55cSDimitry Andric if (MF.getTarget().getTargetTriple().isOSDarwin()) 7451349cc55cSDimitry Andric return MF.getFunction().hasMinSize(); 7452349cc55cSDimitry Andric return MF.getFunction().hasOptSize(); 7453349cc55cSDimitry Andric } 7454349cc55cSDimitry Andric 7455349cc55cSDimitry Andric // Returns a list of types to use for memory op lowering in MemOps. A partial 7456349cc55cSDimitry Andric // port of findOptimalMemOpLowering in TargetLowering. 7457349cc55cSDimitry Andric static bool findGISelOptimalMemOpLowering(std::vector<LLT> &MemOps, 7458349cc55cSDimitry Andric unsigned Limit, const MemOp &Op, 7459349cc55cSDimitry Andric unsigned DstAS, unsigned SrcAS, 7460349cc55cSDimitry Andric const AttributeList &FuncAttributes, 7461349cc55cSDimitry Andric const TargetLowering &TLI) { 7462349cc55cSDimitry Andric if (Op.isMemcpyWithFixedDstAlign() && Op.getSrcAlign() < Op.getDstAlign()) 7463349cc55cSDimitry Andric return false; 7464349cc55cSDimitry Andric 7465349cc55cSDimitry Andric LLT Ty = TLI.getOptimalMemOpLLT(Op, FuncAttributes); 7466349cc55cSDimitry Andric 7467349cc55cSDimitry Andric if (Ty == LLT()) { 7468349cc55cSDimitry Andric // Use the largest scalar type whose alignment constraints are satisfied. 7469349cc55cSDimitry Andric // We only need to check DstAlign here as SrcAlign is always greater or 7470349cc55cSDimitry Andric // equal to DstAlign (or zero). 7471349cc55cSDimitry Andric Ty = LLT::scalar(64); 7472349cc55cSDimitry Andric if (Op.isFixedDstAlign()) 7473349cc55cSDimitry Andric while (Op.getDstAlign() < Ty.getSizeInBytes() && 7474349cc55cSDimitry Andric !TLI.allowsMisalignedMemoryAccesses(Ty, DstAS, Op.getDstAlign())) 7475349cc55cSDimitry Andric Ty = LLT::scalar(Ty.getSizeInBytes()); 7476349cc55cSDimitry Andric assert(Ty.getSizeInBits() > 0 && "Could not find valid type"); 7477349cc55cSDimitry Andric // FIXME: check for the largest legal type we can load/store to. 7478349cc55cSDimitry Andric } 7479349cc55cSDimitry Andric 7480349cc55cSDimitry Andric unsigned NumMemOps = 0; 7481349cc55cSDimitry Andric uint64_t Size = Op.size(); 7482349cc55cSDimitry Andric while (Size) { 7483349cc55cSDimitry Andric unsigned TySize = Ty.getSizeInBytes(); 7484349cc55cSDimitry Andric while (TySize > Size) { 7485349cc55cSDimitry Andric // For now, only use non-vector load / store's for the left-over pieces. 7486349cc55cSDimitry Andric LLT NewTy = Ty; 7487349cc55cSDimitry Andric // FIXME: check for mem op safety and legality of the types. Not all of 7488349cc55cSDimitry Andric // SDAGisms map cleanly to GISel concepts. 7489349cc55cSDimitry Andric if (NewTy.isVector()) 7490349cc55cSDimitry Andric NewTy = NewTy.getSizeInBits() > 64 ? LLT::scalar(64) : LLT::scalar(32); 7491349cc55cSDimitry Andric NewTy = LLT::scalar(PowerOf2Floor(NewTy.getSizeInBits() - 1)); 7492349cc55cSDimitry Andric unsigned NewTySize = NewTy.getSizeInBytes(); 7493349cc55cSDimitry Andric assert(NewTySize > 0 && "Could not find appropriate type"); 7494349cc55cSDimitry Andric 7495349cc55cSDimitry Andric // If the new LLT cannot cover all of the remaining bits, then consider 7496349cc55cSDimitry Andric // issuing a (or a pair of) unaligned and overlapping load / store. 7497349cc55cSDimitry Andric bool Fast; 7498349cc55cSDimitry Andric // Need to get a VT equivalent for allowMisalignedMemoryAccesses(). 7499349cc55cSDimitry Andric MVT VT = getMVTForLLT(Ty); 7500349cc55cSDimitry Andric if (NumMemOps && Op.allowOverlap() && NewTySize < Size && 7501349cc55cSDimitry Andric TLI.allowsMisalignedMemoryAccesses( 7502349cc55cSDimitry Andric VT, DstAS, Op.isFixedDstAlign() ? Op.getDstAlign() : Align(1), 7503349cc55cSDimitry Andric MachineMemOperand::MONone, &Fast) && 7504349cc55cSDimitry Andric Fast) 7505349cc55cSDimitry Andric TySize = Size; 7506349cc55cSDimitry Andric else { 7507349cc55cSDimitry Andric Ty = NewTy; 7508349cc55cSDimitry Andric TySize = NewTySize; 7509349cc55cSDimitry Andric } 7510349cc55cSDimitry Andric } 7511349cc55cSDimitry Andric 7512349cc55cSDimitry Andric if (++NumMemOps > Limit) 7513349cc55cSDimitry Andric return false; 7514349cc55cSDimitry Andric 7515349cc55cSDimitry Andric MemOps.push_back(Ty); 7516349cc55cSDimitry Andric Size -= TySize; 7517349cc55cSDimitry Andric } 7518349cc55cSDimitry Andric 7519349cc55cSDimitry Andric return true; 7520349cc55cSDimitry Andric } 7521349cc55cSDimitry Andric 7522349cc55cSDimitry Andric static Type *getTypeForLLT(LLT Ty, LLVMContext &C) { 7523349cc55cSDimitry Andric if (Ty.isVector()) 7524349cc55cSDimitry Andric return FixedVectorType::get(IntegerType::get(C, Ty.getScalarSizeInBits()), 7525349cc55cSDimitry Andric Ty.getNumElements()); 7526349cc55cSDimitry Andric return IntegerType::get(C, Ty.getSizeInBits()); 7527349cc55cSDimitry Andric } 7528349cc55cSDimitry Andric 7529349cc55cSDimitry Andric // Get a vectorized representation of the memset value operand, GISel edition. 7530349cc55cSDimitry Andric static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB) { 7531349cc55cSDimitry Andric MachineRegisterInfo &MRI = *MIB.getMRI(); 7532349cc55cSDimitry Andric unsigned NumBits = Ty.getScalarSizeInBits(); 7533349cc55cSDimitry Andric auto ValVRegAndVal = getIConstantVRegValWithLookThrough(Val, MRI); 7534349cc55cSDimitry Andric if (!Ty.isVector() && ValVRegAndVal) { 7535349cc55cSDimitry Andric APInt Scalar = ValVRegAndVal->Value.truncOrSelf(8); 7536349cc55cSDimitry Andric APInt SplatVal = APInt::getSplat(NumBits, Scalar); 7537349cc55cSDimitry Andric return MIB.buildConstant(Ty, SplatVal).getReg(0); 7538349cc55cSDimitry Andric } 7539349cc55cSDimitry Andric 7540349cc55cSDimitry Andric // Extend the byte value to the larger type, and then multiply by a magic 7541349cc55cSDimitry Andric // value 0x010101... in order to replicate it across every byte. 7542349cc55cSDimitry Andric // Unless it's zero, in which case just emit a larger G_CONSTANT 0. 7543349cc55cSDimitry Andric if (ValVRegAndVal && ValVRegAndVal->Value == 0) { 7544349cc55cSDimitry Andric return MIB.buildConstant(Ty, 0).getReg(0); 7545349cc55cSDimitry Andric } 7546349cc55cSDimitry Andric 7547349cc55cSDimitry Andric LLT ExtType = Ty.getScalarType(); 7548349cc55cSDimitry Andric auto ZExt = MIB.buildZExtOrTrunc(ExtType, Val); 7549349cc55cSDimitry Andric if (NumBits > 8) { 7550349cc55cSDimitry Andric APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01)); 7551349cc55cSDimitry Andric auto MagicMI = MIB.buildConstant(ExtType, Magic); 7552349cc55cSDimitry Andric Val = MIB.buildMul(ExtType, ZExt, MagicMI).getReg(0); 7553349cc55cSDimitry Andric } 7554349cc55cSDimitry Andric 7555349cc55cSDimitry Andric // For vector types create a G_BUILD_VECTOR. 7556349cc55cSDimitry Andric if (Ty.isVector()) 7557349cc55cSDimitry Andric Val = MIB.buildSplatVector(Ty, Val).getReg(0); 7558349cc55cSDimitry Andric 7559349cc55cSDimitry Andric return Val; 7560349cc55cSDimitry Andric } 7561349cc55cSDimitry Andric 7562349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7563349cc55cSDimitry Andric LegalizerHelper::lowerMemset(MachineInstr &MI, Register Dst, Register Val, 7564349cc55cSDimitry Andric uint64_t KnownLen, Align Alignment, 7565349cc55cSDimitry Andric bool IsVolatile) { 7566349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7567349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7568349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7569349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7570349cc55cSDimitry Andric 7571349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memset length!"); 7572349cc55cSDimitry Andric 7573349cc55cSDimitry Andric bool DstAlignCanChange = false; 7574349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7575349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 7576349cc55cSDimitry Andric 7577349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7578349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7579349cc55cSDimitry Andric DstAlignCanChange = true; 7580349cc55cSDimitry Andric 7581349cc55cSDimitry Andric unsigned Limit = TLI.getMaxStoresPerMemset(OptSize); 7582349cc55cSDimitry Andric std::vector<LLT> MemOps; 7583349cc55cSDimitry Andric 7584349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7585349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7586349cc55cSDimitry Andric 7587349cc55cSDimitry Andric auto ValVRegAndVal = getIConstantVRegValWithLookThrough(Val, MRI); 7588349cc55cSDimitry Andric bool IsZeroVal = ValVRegAndVal && ValVRegAndVal->Value == 0; 7589349cc55cSDimitry Andric 7590349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering(MemOps, Limit, 7591349cc55cSDimitry Andric MemOp::Set(KnownLen, DstAlignCanChange, 7592349cc55cSDimitry Andric Alignment, 7593349cc55cSDimitry Andric /*IsZeroMemset=*/IsZeroVal, 7594349cc55cSDimitry Andric /*IsVolatile=*/IsVolatile), 7595349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), ~0u, 7596349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7597349cc55cSDimitry Andric return UnableToLegalize; 7598349cc55cSDimitry Andric 7599349cc55cSDimitry Andric if (DstAlignCanChange) { 7600349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7601349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7602349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7603349cc55cSDimitry Andric if (NewAlign > Alignment) { 7604349cc55cSDimitry Andric Alignment = NewAlign; 7605349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7606349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7607349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7608349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7609349cc55cSDimitry Andric } 7610349cc55cSDimitry Andric } 7611349cc55cSDimitry Andric 7612349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7613349cc55cSDimitry Andric // Find the largest store and generate the bit pattern for it. 7614349cc55cSDimitry Andric LLT LargestTy = MemOps[0]; 7615349cc55cSDimitry Andric for (unsigned i = 1; i < MemOps.size(); i++) 7616349cc55cSDimitry Andric if (MemOps[i].getSizeInBits() > LargestTy.getSizeInBits()) 7617349cc55cSDimitry Andric LargestTy = MemOps[i]; 7618349cc55cSDimitry Andric 7619349cc55cSDimitry Andric // The memset stored value is always defined as an s8, so in order to make it 7620349cc55cSDimitry Andric // work with larger store types we need to repeat the bit pattern across the 7621349cc55cSDimitry Andric // wider type. 7622349cc55cSDimitry Andric Register MemSetValue = getMemsetValue(Val, LargestTy, MIB); 7623349cc55cSDimitry Andric 7624349cc55cSDimitry Andric if (!MemSetValue) 7625349cc55cSDimitry Andric return UnableToLegalize; 7626349cc55cSDimitry Andric 7627349cc55cSDimitry Andric // Generate the stores. For each store type in the list, we generate the 7628349cc55cSDimitry Andric // matching store of that type to the destination address. 7629349cc55cSDimitry Andric LLT PtrTy = MRI.getType(Dst); 7630349cc55cSDimitry Andric unsigned DstOff = 0; 7631349cc55cSDimitry Andric unsigned Size = KnownLen; 7632349cc55cSDimitry Andric for (unsigned I = 0; I < MemOps.size(); I++) { 7633349cc55cSDimitry Andric LLT Ty = MemOps[I]; 7634349cc55cSDimitry Andric unsigned TySize = Ty.getSizeInBytes(); 7635349cc55cSDimitry Andric if (TySize > Size) { 7636349cc55cSDimitry Andric // Issuing an unaligned load / store pair that overlaps with the previous 7637349cc55cSDimitry Andric // pair. Adjust the offset accordingly. 7638349cc55cSDimitry Andric assert(I == MemOps.size() - 1 && I != 0); 7639349cc55cSDimitry Andric DstOff -= TySize - Size; 7640349cc55cSDimitry Andric } 7641349cc55cSDimitry Andric 7642349cc55cSDimitry Andric // If this store is smaller than the largest store see whether we can get 7643349cc55cSDimitry Andric // the smaller value for free with a truncate. 7644349cc55cSDimitry Andric Register Value = MemSetValue; 7645349cc55cSDimitry Andric if (Ty.getSizeInBits() < LargestTy.getSizeInBits()) { 7646349cc55cSDimitry Andric MVT VT = getMVTForLLT(Ty); 7647349cc55cSDimitry Andric MVT LargestVT = getMVTForLLT(LargestTy); 7648349cc55cSDimitry Andric if (!LargestTy.isVector() && !Ty.isVector() && 7649349cc55cSDimitry Andric TLI.isTruncateFree(LargestVT, VT)) 7650349cc55cSDimitry Andric Value = MIB.buildTrunc(Ty, MemSetValue).getReg(0); 7651349cc55cSDimitry Andric else 7652349cc55cSDimitry Andric Value = getMemsetValue(Val, Ty, MIB); 7653349cc55cSDimitry Andric if (!Value) 7654349cc55cSDimitry Andric return UnableToLegalize; 7655349cc55cSDimitry Andric } 7656349cc55cSDimitry Andric 7657349cc55cSDimitry Andric auto *StoreMMO = MF.getMachineMemOperand(&DstMMO, DstOff, Ty); 7658349cc55cSDimitry Andric 7659349cc55cSDimitry Andric Register Ptr = Dst; 7660349cc55cSDimitry Andric if (DstOff != 0) { 7661349cc55cSDimitry Andric auto Offset = 7662349cc55cSDimitry Andric MIB.buildConstant(LLT::scalar(PtrTy.getSizeInBits()), DstOff); 7663349cc55cSDimitry Andric Ptr = MIB.buildPtrAdd(PtrTy, Dst, Offset).getReg(0); 7664349cc55cSDimitry Andric } 7665349cc55cSDimitry Andric 7666349cc55cSDimitry Andric MIB.buildStore(Value, Ptr, *StoreMMO); 7667349cc55cSDimitry Andric DstOff += Ty.getSizeInBytes(); 7668349cc55cSDimitry Andric Size -= TySize; 7669349cc55cSDimitry Andric } 7670349cc55cSDimitry Andric 7671349cc55cSDimitry Andric MI.eraseFromParent(); 7672349cc55cSDimitry Andric return Legalized; 7673349cc55cSDimitry Andric } 7674349cc55cSDimitry Andric 7675349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7676349cc55cSDimitry Andric LegalizerHelper::lowerMemcpyInline(MachineInstr &MI) { 7677349cc55cSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_MEMCPY_INLINE); 7678349cc55cSDimitry Andric 7679349cc55cSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7680349cc55cSDimitry Andric Register Src = MI.getOperand(1).getReg(); 7681349cc55cSDimitry Andric Register Len = MI.getOperand(2).getReg(); 7682349cc55cSDimitry Andric 7683349cc55cSDimitry Andric const auto *MMOIt = MI.memoperands_begin(); 7684349cc55cSDimitry Andric const MachineMemOperand *MemOp = *MMOIt; 7685349cc55cSDimitry Andric bool IsVolatile = MemOp->isVolatile(); 7686349cc55cSDimitry Andric 7687349cc55cSDimitry Andric // See if this is a constant length copy 7688349cc55cSDimitry Andric auto LenVRegAndVal = getIConstantVRegValWithLookThrough(Len, MRI); 7689349cc55cSDimitry Andric // FIXME: support dynamically sized G_MEMCPY_INLINE 7690349cc55cSDimitry Andric assert(LenVRegAndVal.hasValue() && 7691349cc55cSDimitry Andric "inline memcpy with dynamic size is not yet supported"); 7692349cc55cSDimitry Andric uint64_t KnownLen = LenVRegAndVal->Value.getZExtValue(); 7693349cc55cSDimitry Andric if (KnownLen == 0) { 7694349cc55cSDimitry Andric MI.eraseFromParent(); 7695349cc55cSDimitry Andric return Legalized; 7696349cc55cSDimitry Andric } 7697349cc55cSDimitry Andric 7698349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7699349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7700349cc55cSDimitry Andric Align DstAlign = DstMMO.getBaseAlign(); 7701349cc55cSDimitry Andric Align SrcAlign = SrcMMO.getBaseAlign(); 7702349cc55cSDimitry Andric 7703349cc55cSDimitry Andric return lowerMemcpyInline(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, 7704349cc55cSDimitry Andric IsVolatile); 7705349cc55cSDimitry Andric } 7706349cc55cSDimitry Andric 7707349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7708349cc55cSDimitry Andric LegalizerHelper::lowerMemcpyInline(MachineInstr &MI, Register Dst, Register Src, 7709349cc55cSDimitry Andric uint64_t KnownLen, Align DstAlign, 7710349cc55cSDimitry Andric Align SrcAlign, bool IsVolatile) { 7711349cc55cSDimitry Andric assert(MI.getOpcode() == TargetOpcode::G_MEMCPY_INLINE); 7712349cc55cSDimitry Andric return lowerMemcpy(MI, Dst, Src, KnownLen, 7713349cc55cSDimitry Andric std::numeric_limits<uint64_t>::max(), DstAlign, SrcAlign, 7714349cc55cSDimitry Andric IsVolatile); 7715349cc55cSDimitry Andric } 7716349cc55cSDimitry Andric 7717349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7718349cc55cSDimitry Andric LegalizerHelper::lowerMemcpy(MachineInstr &MI, Register Dst, Register Src, 7719349cc55cSDimitry Andric uint64_t KnownLen, uint64_t Limit, Align DstAlign, 7720349cc55cSDimitry Andric Align SrcAlign, bool IsVolatile) { 7721349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7722349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7723349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7724349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7725349cc55cSDimitry Andric 7726349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memcpy length!"); 7727349cc55cSDimitry Andric 7728349cc55cSDimitry Andric bool DstAlignCanChange = false; 7729349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7730349cc55cSDimitry Andric Align Alignment = commonAlignment(DstAlign, SrcAlign); 7731349cc55cSDimitry Andric 7732349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7733349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7734349cc55cSDimitry Andric DstAlignCanChange = true; 7735349cc55cSDimitry Andric 7736349cc55cSDimitry Andric // FIXME: infer better src pointer alignment like SelectionDAG does here. 7737349cc55cSDimitry Andric // FIXME: also use the equivalent of isMemSrcFromConstant and alwaysinlining 7738349cc55cSDimitry Andric // if the memcpy is in a tail call position. 7739349cc55cSDimitry Andric 7740349cc55cSDimitry Andric std::vector<LLT> MemOps; 7741349cc55cSDimitry Andric 7742349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7743349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7744349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7745349cc55cSDimitry Andric MachinePointerInfo SrcPtrInfo = SrcMMO.getPointerInfo(); 7746349cc55cSDimitry Andric 7747349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering( 7748349cc55cSDimitry Andric MemOps, Limit, 7749349cc55cSDimitry Andric MemOp::Copy(KnownLen, DstAlignCanChange, Alignment, SrcAlign, 7750349cc55cSDimitry Andric IsVolatile), 7751349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(), 7752349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7753349cc55cSDimitry Andric return UnableToLegalize; 7754349cc55cSDimitry Andric 7755349cc55cSDimitry Andric if (DstAlignCanChange) { 7756349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7757349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7758349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7759349cc55cSDimitry Andric 7760349cc55cSDimitry Andric // Don't promote to an alignment that would require dynamic stack 7761349cc55cSDimitry Andric // realignment. 7762349cc55cSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 7763349cc55cSDimitry Andric if (!TRI->hasStackRealignment(MF)) 7764349cc55cSDimitry Andric while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) 7765349cc55cSDimitry Andric NewAlign = NewAlign / 2; 7766349cc55cSDimitry Andric 7767349cc55cSDimitry Andric if (NewAlign > Alignment) { 7768349cc55cSDimitry Andric Alignment = NewAlign; 7769349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7770349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7771349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7772349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7773349cc55cSDimitry Andric } 7774349cc55cSDimitry Andric } 7775349cc55cSDimitry Andric 7776349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Inlining memcpy: " << MI << " into loads & stores\n"); 7777349cc55cSDimitry Andric 7778349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7779349cc55cSDimitry Andric // Now we need to emit a pair of load and stores for each of the types we've 7780349cc55cSDimitry Andric // collected. I.e. for each type, generate a load from the source pointer of 7781349cc55cSDimitry Andric // that type width, and then generate a corresponding store to the dest buffer 7782349cc55cSDimitry Andric // of that value loaded. This can result in a sequence of loads and stores 7783349cc55cSDimitry Andric // mixed types, depending on what the target specifies as good types to use. 7784349cc55cSDimitry Andric unsigned CurrOffset = 0; 7785349cc55cSDimitry Andric unsigned Size = KnownLen; 7786349cc55cSDimitry Andric for (auto CopyTy : MemOps) { 7787349cc55cSDimitry Andric // Issuing an unaligned load / store pair that overlaps with the previous 7788349cc55cSDimitry Andric // pair. Adjust the offset accordingly. 7789349cc55cSDimitry Andric if (CopyTy.getSizeInBytes() > Size) 7790349cc55cSDimitry Andric CurrOffset -= CopyTy.getSizeInBytes() - Size; 7791349cc55cSDimitry Andric 7792349cc55cSDimitry Andric // Construct MMOs for the accesses. 7793349cc55cSDimitry Andric auto *LoadMMO = 7794349cc55cSDimitry Andric MF.getMachineMemOperand(&SrcMMO, CurrOffset, CopyTy.getSizeInBytes()); 7795349cc55cSDimitry Andric auto *StoreMMO = 7796349cc55cSDimitry Andric MF.getMachineMemOperand(&DstMMO, CurrOffset, CopyTy.getSizeInBytes()); 7797349cc55cSDimitry Andric 7798349cc55cSDimitry Andric // Create the load. 7799349cc55cSDimitry Andric Register LoadPtr = Src; 7800349cc55cSDimitry Andric Register Offset; 7801349cc55cSDimitry Andric if (CurrOffset != 0) { 7802*4824e7fdSDimitry Andric LLT SrcTy = MRI.getType(Src); 7803*4824e7fdSDimitry Andric Offset = MIB.buildConstant(LLT::scalar(SrcTy.getSizeInBits()), CurrOffset) 7804349cc55cSDimitry Andric .getReg(0); 7805*4824e7fdSDimitry Andric LoadPtr = MIB.buildPtrAdd(SrcTy, Src, Offset).getReg(0); 7806349cc55cSDimitry Andric } 7807349cc55cSDimitry Andric auto LdVal = MIB.buildLoad(CopyTy, LoadPtr, *LoadMMO); 7808349cc55cSDimitry Andric 7809349cc55cSDimitry Andric // Create the store. 7810*4824e7fdSDimitry Andric Register StorePtr = Dst; 7811*4824e7fdSDimitry Andric if (CurrOffset != 0) { 7812*4824e7fdSDimitry Andric LLT DstTy = MRI.getType(Dst); 7813*4824e7fdSDimitry Andric StorePtr = MIB.buildPtrAdd(DstTy, Dst, Offset).getReg(0); 7814*4824e7fdSDimitry Andric } 7815349cc55cSDimitry Andric MIB.buildStore(LdVal, StorePtr, *StoreMMO); 7816349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7817349cc55cSDimitry Andric Size -= CopyTy.getSizeInBytes(); 7818349cc55cSDimitry Andric } 7819349cc55cSDimitry Andric 7820349cc55cSDimitry Andric MI.eraseFromParent(); 7821349cc55cSDimitry Andric return Legalized; 7822349cc55cSDimitry Andric } 7823349cc55cSDimitry Andric 7824349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7825349cc55cSDimitry Andric LegalizerHelper::lowerMemmove(MachineInstr &MI, Register Dst, Register Src, 7826349cc55cSDimitry Andric uint64_t KnownLen, Align DstAlign, Align SrcAlign, 7827349cc55cSDimitry Andric bool IsVolatile) { 7828349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7829349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7830349cc55cSDimitry Andric auto &DL = MF.getDataLayout(); 7831349cc55cSDimitry Andric LLVMContext &C = MF.getFunction().getContext(); 7832349cc55cSDimitry Andric 7833349cc55cSDimitry Andric assert(KnownLen != 0 && "Have a zero length memmove length!"); 7834349cc55cSDimitry Andric 7835349cc55cSDimitry Andric bool DstAlignCanChange = false; 7836349cc55cSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7837349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 7838349cc55cSDimitry Andric Align Alignment = commonAlignment(DstAlign, SrcAlign); 7839349cc55cSDimitry Andric 7840349cc55cSDimitry Andric MachineInstr *FIDef = getOpcodeDef(TargetOpcode::G_FRAME_INDEX, Dst, MRI); 7841349cc55cSDimitry Andric if (FIDef && !MFI.isFixedObjectIndex(FIDef->getOperand(1).getIndex())) 7842349cc55cSDimitry Andric DstAlignCanChange = true; 7843349cc55cSDimitry Andric 7844349cc55cSDimitry Andric unsigned Limit = TLI.getMaxStoresPerMemmove(OptSize); 7845349cc55cSDimitry Andric std::vector<LLT> MemOps; 7846349cc55cSDimitry Andric 7847349cc55cSDimitry Andric const auto &DstMMO = **MI.memoperands_begin(); 7848349cc55cSDimitry Andric const auto &SrcMMO = **std::next(MI.memoperands_begin()); 7849349cc55cSDimitry Andric MachinePointerInfo DstPtrInfo = DstMMO.getPointerInfo(); 7850349cc55cSDimitry Andric MachinePointerInfo SrcPtrInfo = SrcMMO.getPointerInfo(); 7851349cc55cSDimitry Andric 7852349cc55cSDimitry Andric // FIXME: SelectionDAG always passes false for 'AllowOverlap', apparently due 7853349cc55cSDimitry Andric // to a bug in it's findOptimalMemOpLowering implementation. For now do the 7854349cc55cSDimitry Andric // same thing here. 7855349cc55cSDimitry Andric if (!findGISelOptimalMemOpLowering( 7856349cc55cSDimitry Andric MemOps, Limit, 7857349cc55cSDimitry Andric MemOp::Copy(KnownLen, DstAlignCanChange, Alignment, SrcAlign, 7858349cc55cSDimitry Andric /*IsVolatile*/ true), 7859349cc55cSDimitry Andric DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(), 7860349cc55cSDimitry Andric MF.getFunction().getAttributes(), TLI)) 7861349cc55cSDimitry Andric return UnableToLegalize; 7862349cc55cSDimitry Andric 7863349cc55cSDimitry Andric if (DstAlignCanChange) { 7864349cc55cSDimitry Andric // Get an estimate of the type from the LLT. 7865349cc55cSDimitry Andric Type *IRTy = getTypeForLLT(MemOps[0], C); 7866349cc55cSDimitry Andric Align NewAlign = DL.getABITypeAlign(IRTy); 7867349cc55cSDimitry Andric 7868349cc55cSDimitry Andric // Don't promote to an alignment that would require dynamic stack 7869349cc55cSDimitry Andric // realignment. 7870349cc55cSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 7871349cc55cSDimitry Andric if (!TRI->hasStackRealignment(MF)) 7872349cc55cSDimitry Andric while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) 7873349cc55cSDimitry Andric NewAlign = NewAlign / 2; 7874349cc55cSDimitry Andric 7875349cc55cSDimitry Andric if (NewAlign > Alignment) { 7876349cc55cSDimitry Andric Alignment = NewAlign; 7877349cc55cSDimitry Andric unsigned FI = FIDef->getOperand(1).getIndex(); 7878349cc55cSDimitry Andric // Give the stack frame object a larger alignment if needed. 7879349cc55cSDimitry Andric if (MFI.getObjectAlign(FI) < Alignment) 7880349cc55cSDimitry Andric MFI.setObjectAlignment(FI, Alignment); 7881349cc55cSDimitry Andric } 7882349cc55cSDimitry Andric } 7883349cc55cSDimitry Andric 7884349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Inlining memmove: " << MI << " into loads & stores\n"); 7885349cc55cSDimitry Andric 7886349cc55cSDimitry Andric MachineIRBuilder MIB(MI); 7887349cc55cSDimitry Andric // Memmove requires that we perform the loads first before issuing the stores. 7888349cc55cSDimitry Andric // Apart from that, this loop is pretty much doing the same thing as the 7889349cc55cSDimitry Andric // memcpy codegen function. 7890349cc55cSDimitry Andric unsigned CurrOffset = 0; 7891349cc55cSDimitry Andric SmallVector<Register, 16> LoadVals; 7892349cc55cSDimitry Andric for (auto CopyTy : MemOps) { 7893349cc55cSDimitry Andric // Construct MMO for the load. 7894349cc55cSDimitry Andric auto *LoadMMO = 7895349cc55cSDimitry Andric MF.getMachineMemOperand(&SrcMMO, CurrOffset, CopyTy.getSizeInBytes()); 7896349cc55cSDimitry Andric 7897349cc55cSDimitry Andric // Create the load. 7898349cc55cSDimitry Andric Register LoadPtr = Src; 7899349cc55cSDimitry Andric if (CurrOffset != 0) { 7900*4824e7fdSDimitry Andric LLT SrcTy = MRI.getType(Src); 7901349cc55cSDimitry Andric auto Offset = 7902*4824e7fdSDimitry Andric MIB.buildConstant(LLT::scalar(SrcTy.getSizeInBits()), CurrOffset); 7903*4824e7fdSDimitry Andric LoadPtr = MIB.buildPtrAdd(SrcTy, Src, Offset).getReg(0); 7904349cc55cSDimitry Andric } 7905349cc55cSDimitry Andric LoadVals.push_back(MIB.buildLoad(CopyTy, LoadPtr, *LoadMMO).getReg(0)); 7906349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7907349cc55cSDimitry Andric } 7908349cc55cSDimitry Andric 7909349cc55cSDimitry Andric CurrOffset = 0; 7910349cc55cSDimitry Andric for (unsigned I = 0; I < MemOps.size(); ++I) { 7911349cc55cSDimitry Andric LLT CopyTy = MemOps[I]; 7912349cc55cSDimitry Andric // Now store the values loaded. 7913349cc55cSDimitry Andric auto *StoreMMO = 7914349cc55cSDimitry Andric MF.getMachineMemOperand(&DstMMO, CurrOffset, CopyTy.getSizeInBytes()); 7915349cc55cSDimitry Andric 7916349cc55cSDimitry Andric Register StorePtr = Dst; 7917349cc55cSDimitry Andric if (CurrOffset != 0) { 7918*4824e7fdSDimitry Andric LLT DstTy = MRI.getType(Dst); 7919349cc55cSDimitry Andric auto Offset = 7920*4824e7fdSDimitry Andric MIB.buildConstant(LLT::scalar(DstTy.getSizeInBits()), CurrOffset); 7921*4824e7fdSDimitry Andric StorePtr = MIB.buildPtrAdd(DstTy, Dst, Offset).getReg(0); 7922349cc55cSDimitry Andric } 7923349cc55cSDimitry Andric MIB.buildStore(LoadVals[I], StorePtr, *StoreMMO); 7924349cc55cSDimitry Andric CurrOffset += CopyTy.getSizeInBytes(); 7925349cc55cSDimitry Andric } 7926349cc55cSDimitry Andric MI.eraseFromParent(); 7927349cc55cSDimitry Andric return Legalized; 7928349cc55cSDimitry Andric } 7929349cc55cSDimitry Andric 7930349cc55cSDimitry Andric LegalizerHelper::LegalizeResult 7931349cc55cSDimitry Andric LegalizerHelper::lowerMemCpyFamily(MachineInstr &MI, unsigned MaxLen) { 7932349cc55cSDimitry Andric const unsigned Opc = MI.getOpcode(); 7933349cc55cSDimitry Andric // This combine is fairly complex so it's not written with a separate 7934349cc55cSDimitry Andric // matcher function. 7935349cc55cSDimitry Andric assert((Opc == TargetOpcode::G_MEMCPY || Opc == TargetOpcode::G_MEMMOVE || 7936349cc55cSDimitry Andric Opc == TargetOpcode::G_MEMSET) && 7937349cc55cSDimitry Andric "Expected memcpy like instruction"); 7938349cc55cSDimitry Andric 7939349cc55cSDimitry Andric auto MMOIt = MI.memoperands_begin(); 7940349cc55cSDimitry Andric const MachineMemOperand *MemOp = *MMOIt; 7941349cc55cSDimitry Andric 7942349cc55cSDimitry Andric Align DstAlign = MemOp->getBaseAlign(); 7943349cc55cSDimitry Andric Align SrcAlign; 7944349cc55cSDimitry Andric Register Dst = MI.getOperand(0).getReg(); 7945349cc55cSDimitry Andric Register Src = MI.getOperand(1).getReg(); 7946349cc55cSDimitry Andric Register Len = MI.getOperand(2).getReg(); 7947349cc55cSDimitry Andric 7948349cc55cSDimitry Andric if (Opc != TargetOpcode::G_MEMSET) { 7949349cc55cSDimitry Andric assert(MMOIt != MI.memoperands_end() && "Expected a second MMO on MI"); 7950349cc55cSDimitry Andric MemOp = *(++MMOIt); 7951349cc55cSDimitry Andric SrcAlign = MemOp->getBaseAlign(); 7952349cc55cSDimitry Andric } 7953349cc55cSDimitry Andric 7954349cc55cSDimitry Andric // See if this is a constant length copy 7955349cc55cSDimitry Andric auto LenVRegAndVal = getIConstantVRegValWithLookThrough(Len, MRI); 7956349cc55cSDimitry Andric if (!LenVRegAndVal) 7957349cc55cSDimitry Andric return UnableToLegalize; 7958349cc55cSDimitry Andric uint64_t KnownLen = LenVRegAndVal->Value.getZExtValue(); 7959349cc55cSDimitry Andric 7960349cc55cSDimitry Andric if (KnownLen == 0) { 7961349cc55cSDimitry Andric MI.eraseFromParent(); 7962349cc55cSDimitry Andric return Legalized; 7963349cc55cSDimitry Andric } 7964349cc55cSDimitry Andric 7965349cc55cSDimitry Andric bool IsVolatile = MemOp->isVolatile(); 7966349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMCPY_INLINE) 7967349cc55cSDimitry Andric return lowerMemcpyInline(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, 7968349cc55cSDimitry Andric IsVolatile); 7969349cc55cSDimitry Andric 7970349cc55cSDimitry Andric // Don't try to optimize volatile. 7971349cc55cSDimitry Andric if (IsVolatile) 7972349cc55cSDimitry Andric return UnableToLegalize; 7973349cc55cSDimitry Andric 7974349cc55cSDimitry Andric if (MaxLen && KnownLen > MaxLen) 7975349cc55cSDimitry Andric return UnableToLegalize; 7976349cc55cSDimitry Andric 7977349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMCPY) { 7978349cc55cSDimitry Andric auto &MF = *MI.getParent()->getParent(); 7979349cc55cSDimitry Andric const auto &TLI = *MF.getSubtarget().getTargetLowering(); 7980349cc55cSDimitry Andric bool OptSize = shouldLowerMemFuncForSize(MF); 7981349cc55cSDimitry Andric uint64_t Limit = TLI.getMaxStoresPerMemcpy(OptSize); 7982349cc55cSDimitry Andric return lowerMemcpy(MI, Dst, Src, KnownLen, Limit, DstAlign, SrcAlign, 7983349cc55cSDimitry Andric IsVolatile); 7984349cc55cSDimitry Andric } 7985349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMMOVE) 7986349cc55cSDimitry Andric return lowerMemmove(MI, Dst, Src, KnownLen, DstAlign, SrcAlign, IsVolatile); 7987349cc55cSDimitry Andric if (Opc == TargetOpcode::G_MEMSET) 7988349cc55cSDimitry Andric return lowerMemset(MI, Dst, Src, KnownLen, DstAlign, IsVolatile); 7989349cc55cSDimitry Andric return UnableToLegalize; 7990349cc55cSDimitry Andric } 7991