14be39288SPhilip Reames //===-- RISCVInstructionSelector.cpp -----------------------------*- C++ -*-==// 24be39288SPhilip Reames // 34be39288SPhilip Reames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44be39288SPhilip Reames // See https://llvm.org/LICENSE.txt for license information. 54be39288SPhilip Reames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64be39288SPhilip Reames // 74be39288SPhilip Reames //===----------------------------------------------------------------------===// 84be39288SPhilip Reames /// \file 94be39288SPhilip Reames /// This file implements the targeting of the InstructionSelector class for 1029463612SCraig Topper /// RISC-V. 114be39288SPhilip Reames /// \todo This should be generated by TableGen. 124be39288SPhilip Reames //===----------------------------------------------------------------------===// 134be39288SPhilip Reames 14c07062a2SNitin John Raj #include "MCTargetDesc/RISCVMatInt.h" 154be39288SPhilip Reames #include "RISCVRegisterBankInfo.h" 164be39288SPhilip Reames #include "RISCVSubtarget.h" 174be39288SPhilip Reames #include "RISCVTargetMachine.h" 181fe7d9c7Spvanhout #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h" 19c1f43384SMikhail Gudim #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h" 2086d6b379SNitin John Raj #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" 214be39288SPhilip Reames #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 225f5faf40SMin-Yih Hsu #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" 23c07062a2SNitin John Raj #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 2420f544d0SCraig Topper #include "llvm/CodeGen/MachineJumpTableInfo.h" 254be39288SPhilip Reames #include "llvm/IR/IntrinsicsRISCV.h" 264be39288SPhilip Reames #include "llvm/Support/Debug.h" 274be39288SPhilip Reames 284be39288SPhilip Reames #define DEBUG_TYPE "riscv-isel" 294be39288SPhilip Reames 304be39288SPhilip Reames using namespace llvm; 314458ba8cSMichael Maitland using namespace MIPatternMatch; 324be39288SPhilip Reames 334be39288SPhilip Reames #define GET_GLOBALISEL_PREDICATE_BITSET 344be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 354be39288SPhilip Reames #undef GET_GLOBALISEL_PREDICATE_BITSET 364be39288SPhilip Reames 374be39288SPhilip Reames namespace { 384be39288SPhilip Reames 394be39288SPhilip Reames class RISCVInstructionSelector : public InstructionSelector { 404be39288SPhilip Reames public: 414be39288SPhilip Reames RISCVInstructionSelector(const RISCVTargetMachine &TM, 424be39288SPhilip Reames const RISCVSubtarget &STI, 434be39288SPhilip Reames const RISCVRegisterBankInfo &RBI); 444be39288SPhilip Reames 45c07062a2SNitin John Raj bool select(MachineInstr &MI) override; 46a3cc4b61SMichael Maitland 47a3cc4b61SMichael Maitland void setupMF(MachineFunction &MF, GISelKnownBits *KB, 48a3cc4b61SMichael Maitland CodeGenCoverage *CoverageInfo, ProfileSummaryInfo *PSI, 49a3cc4b61SMichael Maitland BlockFrequencyInfo *BFI) override { 50a3cc4b61SMichael Maitland InstructionSelector::setupMF(MF, KB, CoverageInfo, PSI, BFI); 51a3cc4b61SMichael Maitland MRI = &MF.getRegInfo(); 52a3cc4b61SMichael Maitland } 53a3cc4b61SMichael Maitland 544be39288SPhilip Reames static const char *getName() { return DEBUG_TYPE; } 554be39288SPhilip Reames 564be39288SPhilip Reames private: 57c07062a2SNitin John Raj const TargetRegisterClass * 5898eb28b6SCraig Topper getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB) const; 59c07062a2SNitin John Raj 60a3cc4b61SMichael Maitland bool isRegInGprb(Register Reg) const; 61a3cc4b61SMichael Maitland bool isRegInFprb(Register Reg) const; 62dbb9043dSMichael Maitland 637f9a50ffSNitin John Raj // tblgen-erated 'select' implementation, used as the initial selector for 647f9a50ffSNitin John Raj // the patterns that don't require complex C++. 654be39288SPhilip Reames bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const; 667f9a50ffSNitin John Raj 6745a29bd5SNitin John Raj // A lowering phase that runs before any selection attempts. 6845a29bd5SNitin John Raj // Returns true if the instruction was modified. 69a3cc4b61SMichael Maitland void preISelLower(MachineInstr &MI, MachineIRBuilder &MIB); 7045a29bd5SNitin John Raj 71a3cc4b61SMichael Maitland bool replacePtrWithInt(MachineOperand &Op, MachineIRBuilder &MIB); 7245a29bd5SNitin John Raj 737f9a50ffSNitin John Raj // Custom selection methods 74a3cc4b61SMichael Maitland bool selectCopy(MachineInstr &MI) const; 75a3cc4b61SMichael Maitland bool selectImplicitDef(MachineInstr &MI, MachineIRBuilder &MIB) const; 764fe0d35fSCraig Topper bool materializeImm(Register Reg, int64_t Imm, MachineIRBuilder &MIB) const; 77a3cc4b61SMichael Maitland bool selectAddr(MachineInstr &MI, MachineIRBuilder &MIB, bool IsLocal = true, 78fe3c4218SCraig Topper bool IsExternWeak = false) const; 79a3cc4b61SMichael Maitland bool selectSelect(MachineInstr &MI, MachineIRBuilder &MIB) const; 80a3cc4b61SMichael Maitland bool selectFPCompare(MachineInstr &MI, MachineIRBuilder &MIB) const; 81ffcc5c77SCraig Topper void emitFence(AtomicOrdering FenceOrdering, SyncScope::ID FenceSSID, 82ffcc5c77SCraig Topper MachineIRBuilder &MIB) const; 83a3cc4b61SMichael Maitland bool selectUnmergeValues(MachineInstr &MI, MachineIRBuilder &MIB) const; 844be39288SPhilip Reames 8537b10af6SCraig Topper ComplexRendererFns selectShiftMask(MachineOperand &Root, 8637b10af6SCraig Topper unsigned ShiftWidth) const; 8737b10af6SCraig Topper ComplexRendererFns selectShiftMaskXLen(MachineOperand &Root) const { 8837b10af6SCraig Topper return selectShiftMask(Root, STI.getXLen()); 8937b10af6SCraig Topper } 9037b10af6SCraig Topper ComplexRendererFns selectShiftMask32(MachineOperand &Root) const { 9137b10af6SCraig Topper return selectShiftMask(Root, 32); 9237b10af6SCraig Topper } 9386d6b379SNitin John Raj ComplexRendererFns selectAddrRegImm(MachineOperand &Root) const; 94ac182deeSCraig Topper 95ae9d0623SCraig Topper ComplexRendererFns selectSExtBits(MachineOperand &Root, unsigned Bits) const; 96ae9d0623SCraig Topper template <unsigned Bits> 97ae9d0623SCraig Topper ComplexRendererFns selectSExtBits(MachineOperand &Root) const { 98ae9d0623SCraig Topper return selectSExtBits(Root, Bits); 99ae9d0623SCraig Topper } 100ae9d0623SCraig Topper 1015dc8d611SCraig Topper ComplexRendererFns selectZExtBits(MachineOperand &Root, unsigned Bits) const; 1025dc8d611SCraig Topper template <unsigned Bits> 1035dc8d611SCraig Topper ComplexRendererFns selectZExtBits(MachineOperand &Root) const { 1045dc8d611SCraig Topper return selectZExtBits(Root, Bits); 1055dc8d611SCraig Topper } 1065dc8d611SCraig Topper 1075f5faf40SMin-Yih Hsu ComplexRendererFns selectSHXADDOp(MachineOperand &Root, unsigned ShAmt) const; 1085f5faf40SMin-Yih Hsu template <unsigned ShAmt> 1095f5faf40SMin-Yih Hsu ComplexRendererFns selectSHXADDOp(MachineOperand &Root) const { 1105f5faf40SMin-Yih Hsu return selectSHXADDOp(Root, ShAmt); 1115f5faf40SMin-Yih Hsu } 1125f5faf40SMin-Yih Hsu 113cdcaef87SMin-Yih Hsu ComplexRendererFns selectSHXADD_UWOp(MachineOperand &Root, 114cdcaef87SMin-Yih Hsu unsigned ShAmt) const; 115cdcaef87SMin-Yih Hsu template <unsigned ShAmt> 116cdcaef87SMin-Yih Hsu ComplexRendererFns selectSHXADD_UWOp(MachineOperand &Root) const { 117cdcaef87SMin-Yih Hsu return selectSHXADD_UWOp(Root, ShAmt); 118cdcaef87SMin-Yih Hsu } 119cdcaef87SMin-Yih Hsu 120f873fc3aSMichael Maitland ComplexRendererFns renderVLOp(MachineOperand &Root) const; 121f873fc3aSMichael Maitland 122ac182deeSCraig Topper // Custom renderers for tablegen 123ac182deeSCraig Topper void renderNegImm(MachineInstrBuilder &MIB, const MachineInstr &MI, 124ac182deeSCraig Topper int OpIdx) const; 125d605d9d7SCraig Topper void renderImmSubFromXLen(MachineInstrBuilder &MIB, const MachineInstr &MI, 126d605d9d7SCraig Topper int OpIdx) const; 127d605d9d7SCraig Topper void renderImmSubFrom32(MachineInstrBuilder &MIB, const MachineInstr &MI, 128d605d9d7SCraig Topper int OpIdx) const; 12986d6b379SNitin John Raj void renderImmPlus1(MachineInstrBuilder &MIB, const MachineInstr &MI, 13086d6b379SNitin John Raj int OpIdx) const; 1319d1a3fddSCraig Topper void renderImm(MachineInstrBuilder &MIB, const MachineInstr &MI, 1329d1a3fddSCraig Topper int OpIdx) const; 133ce393bedSSergei Barannikov void renderFrameIndex(MachineInstrBuilder &MIB, const MachineInstr &MI, 134ce393bedSSergei Barannikov int OpIdx) const; 135ac182deeSCraig Topper 136cdcaef87SMin-Yih Hsu void renderTrailingZeros(MachineInstrBuilder &MIB, const MachineInstr &MI, 137cdcaef87SMin-Yih Hsu int OpIdx) const; 138d78fe84dSCraig Topper void renderXLenSubTrailingOnes(MachineInstrBuilder &MIB, 139d78fe84dSCraig Topper const MachineInstr &MI, int OpIdx) const; 140cdcaef87SMin-Yih Hsu 1416ab8401fSLuke Quinn void renderAddiPairImmLarge(MachineInstrBuilder &MIB, const MachineInstr &MI, 1426ab8401fSLuke Quinn int OpIdx) const; 1436ab8401fSLuke Quinn void renderAddiPairImmSmall(MachineInstrBuilder &MIB, const MachineInstr &MI, 1446ab8401fSLuke Quinn int OpIdx) const; 1456ab8401fSLuke Quinn 1464be39288SPhilip Reames const RISCVSubtarget &STI; 1474be39288SPhilip Reames const RISCVInstrInfo &TII; 1484be39288SPhilip Reames const RISCVRegisterInfo &TRI; 1494be39288SPhilip Reames const RISCVRegisterBankInfo &RBI; 15004dd2ac0SMichael Maitland const RISCVTargetMachine &TM; 1514be39288SPhilip Reames 152a3cc4b61SMichael Maitland MachineRegisterInfo *MRI = nullptr; 153a3cc4b61SMichael Maitland 1544be39288SPhilip Reames // FIXME: This is necessary because DAGISel uses "Subtarget->" and GlobalISel 1554be39288SPhilip Reames // uses "STI." in the code generated by TableGen. We need to unify the name of 1564be39288SPhilip Reames // Subtarget variable. 1574be39288SPhilip Reames const RISCVSubtarget *Subtarget = &STI; 1584be39288SPhilip Reames 1594be39288SPhilip Reames #define GET_GLOBALISEL_PREDICATES_DECL 1604be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 1614be39288SPhilip Reames #undef GET_GLOBALISEL_PREDICATES_DECL 1624be39288SPhilip Reames 1634be39288SPhilip Reames #define GET_GLOBALISEL_TEMPORARIES_DECL 1644be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 1654be39288SPhilip Reames #undef GET_GLOBALISEL_TEMPORARIES_DECL 1664be39288SPhilip Reames }; 1674be39288SPhilip Reames 1684be39288SPhilip Reames } // end anonymous namespace 1694be39288SPhilip Reames 1704be39288SPhilip Reames #define GET_GLOBALISEL_IMPL 1714be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 1724be39288SPhilip Reames #undef GET_GLOBALISEL_IMPL 1734be39288SPhilip Reames 1744be39288SPhilip Reames RISCVInstructionSelector::RISCVInstructionSelector( 1754be39288SPhilip Reames const RISCVTargetMachine &TM, const RISCVSubtarget &STI, 1764be39288SPhilip Reames const RISCVRegisterBankInfo &RBI) 1774be39288SPhilip Reames : STI(STI), TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI), 17804dd2ac0SMichael Maitland TM(TM), 1794be39288SPhilip Reames 1804be39288SPhilip Reames #define GET_GLOBALISEL_PREDICATES_INIT 1814be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 1824be39288SPhilip Reames #undef GET_GLOBALISEL_PREDICATES_INIT 1834be39288SPhilip Reames #define GET_GLOBALISEL_TEMPORARIES_INIT 1844be39288SPhilip Reames #include "RISCVGenGlobalISel.inc" 1854be39288SPhilip Reames #undef GET_GLOBALISEL_TEMPORARIES_INIT 1864be39288SPhilip Reames { 1874be39288SPhilip Reames } 1884be39288SPhilip Reames 189ac182deeSCraig Topper InstructionSelector::ComplexRendererFns 19037b10af6SCraig Topper RISCVInstructionSelector::selectShiftMask(MachineOperand &Root, 19137b10af6SCraig Topper unsigned ShiftWidth) const { 192c1f43384SMikhail Gudim if (!Root.isReg()) 193c1f43384SMikhail Gudim return std::nullopt; 194c1f43384SMikhail Gudim 195c1f43384SMikhail Gudim using namespace llvm::MIPatternMatch; 196c1f43384SMikhail Gudim 19737b10af6SCraig Topper Register ShAmtReg = Root.getReg(); 198c1f43384SMikhail Gudim // Peek through zext. 199c1f43384SMikhail Gudim Register ZExtSrcReg; 20037b10af6SCraig Topper if (mi_match(ShAmtReg, *MRI, m_GZExt(m_Reg(ZExtSrcReg)))) 201c1f43384SMikhail Gudim ShAmtReg = ZExtSrcReg; 202c1f43384SMikhail Gudim 203c1f43384SMikhail Gudim APInt AndMask; 204c1f43384SMikhail Gudim Register AndSrcReg; 205ed3a60c7SHongbin Jin // Try to combine the following pattern (applicable to other shift 206ed3a60c7SHongbin Jin // instructions as well as 32-bit ones): 207ed3a60c7SHongbin Jin // 208ed3a60c7SHongbin Jin // %4:gprb(s64) = G_AND %3, %2 209ed3a60c7SHongbin Jin // %5:gprb(s64) = G_LSHR %1, %4(s64) 210ed3a60c7SHongbin Jin // 211ed3a60c7SHongbin Jin // According to RISC-V's ISA manual, SLL, SRL, and SRA ignore other bits than 212ed3a60c7SHongbin Jin // the lowest log2(XLEN) bits of register rs2. As for the above pattern, if 213ed3a60c7SHongbin Jin // the lowest log2(XLEN) bits of register rd and rs2 of G_AND are the same, 214ed3a60c7SHongbin Jin // then it can be eliminated. Given register rs1 or rs2 holding a constant 215ed3a60c7SHongbin Jin // (the and mask), there are two cases G_AND can be erased: 216ed3a60c7SHongbin Jin // 217ed3a60c7SHongbin Jin // 1. the lowest log2(XLEN) bits of the and mask are all set 218ed3a60c7SHongbin Jin // 2. the bits of the register being masked are already unset (zero set) 219a3cc4b61SMichael Maitland if (mi_match(ShAmtReg, *MRI, m_GAnd(m_Reg(AndSrcReg), m_ICst(AndMask)))) { 220c1f43384SMikhail Gudim APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1); 221c1f43384SMikhail Gudim if (ShMask.isSubsetOf(AndMask)) { 222c1f43384SMikhail Gudim ShAmtReg = AndSrcReg; 223c1f43384SMikhail Gudim } else { 224c1f43384SMikhail Gudim // SimplifyDemandedBits may have optimized the mask so try restoring any 225c1f43384SMikhail Gudim // bits that are known zero. 226ed3a60c7SHongbin Jin KnownBits Known = KB->getKnownBits(AndSrcReg); 227c1f43384SMikhail Gudim if (ShMask.isSubsetOf(AndMask | Known.Zero)) 228c1f43384SMikhail Gudim ShAmtReg = AndSrcReg; 229c1f43384SMikhail Gudim } 230c1f43384SMikhail Gudim } 231c1f43384SMikhail Gudim 232c1f43384SMikhail Gudim APInt Imm; 233c1f43384SMikhail Gudim Register Reg; 234a3cc4b61SMichael Maitland if (mi_match(ShAmtReg, *MRI, m_GAdd(m_Reg(Reg), m_ICst(Imm)))) { 235c1f43384SMikhail Gudim if (Imm != 0 && Imm.urem(ShiftWidth) == 0) 236c1f43384SMikhail Gudim // If we are shifting by X+N where N == 0 mod Size, then just shift by X 237c1f43384SMikhail Gudim // to avoid the ADD. 238c1f43384SMikhail Gudim ShAmtReg = Reg; 239a3cc4b61SMichael Maitland } else if (mi_match(ShAmtReg, *MRI, m_GSub(m_ICst(Imm), m_Reg(Reg)))) { 240c1f43384SMikhail Gudim if (Imm != 0 && Imm.urem(ShiftWidth) == 0) { 241c1f43384SMikhail Gudim // If we are shifting by N-X where N == 0 mod Size, then just shift by -X 242c1f43384SMikhail Gudim // to generate a NEG instead of a SUB of a constant. 243a3cc4b61SMichael Maitland ShAmtReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 244c1f43384SMikhail Gudim unsigned NegOpc = Subtarget->is64Bit() ? RISCV::SUBW : RISCV::SUB; 245c1f43384SMikhail Gudim return {{[=](MachineInstrBuilder &MIB) { 246c1f43384SMikhail Gudim MachineIRBuilder(*MIB.getInstr()) 247c1f43384SMikhail Gudim .buildInstr(NegOpc, {ShAmtReg}, {Register(RISCV::X0), Reg}); 248c1f43384SMikhail Gudim MIB.addReg(ShAmtReg); 249c1f43384SMikhail Gudim }}}; 250c1f43384SMikhail Gudim } 251c1f43384SMikhail Gudim if (Imm.urem(ShiftWidth) == ShiftWidth - 1) { 252c1f43384SMikhail Gudim // If we are shifting by N-X where N == -1 mod Size, then just shift by ~X 253c1f43384SMikhail Gudim // to generate a NOT instead of a SUB of a constant. 254a3cc4b61SMichael Maitland ShAmtReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 255c1f43384SMikhail Gudim return {{[=](MachineInstrBuilder &MIB) { 256c1f43384SMikhail Gudim MachineIRBuilder(*MIB.getInstr()) 257c1f43384SMikhail Gudim .buildInstr(RISCV::XORI, {ShAmtReg}, {Reg}) 258c1f43384SMikhail Gudim .addImm(-1); 259c1f43384SMikhail Gudim MIB.addReg(ShAmtReg); 260c1f43384SMikhail Gudim }}}; 261c1f43384SMikhail Gudim } 262c1f43384SMikhail Gudim } 263c1f43384SMikhail Gudim 264c1f43384SMikhail Gudim return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(ShAmtReg); }}}; 265ac182deeSCraig Topper } 266ac182deeSCraig Topper 26786d6b379SNitin John Raj InstructionSelector::ComplexRendererFns 268ae9d0623SCraig Topper RISCVInstructionSelector::selectSExtBits(MachineOperand &Root, 269ae9d0623SCraig Topper unsigned Bits) const { 270ae9d0623SCraig Topper if (!Root.isReg()) 271ae9d0623SCraig Topper return std::nullopt; 272ae9d0623SCraig Topper Register RootReg = Root.getReg(); 273ae9d0623SCraig Topper MachineInstr *RootDef = MRI->getVRegDef(RootReg); 274ae9d0623SCraig Topper 275ae9d0623SCraig Topper if (RootDef->getOpcode() == TargetOpcode::G_SEXT_INREG && 276ae9d0623SCraig Topper RootDef->getOperand(2).getImm() == Bits) { 277ae9d0623SCraig Topper return { 278ae9d0623SCraig Topper {[=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); }}}; 279ae9d0623SCraig Topper } 280ae9d0623SCraig Topper 281ae9d0623SCraig Topper unsigned Size = MRI->getType(RootReg).getScalarSizeInBits(); 282ae9d0623SCraig Topper if ((Size - KB->computeNumSignBits(RootReg)) < Bits) 283ae9d0623SCraig Topper return {{[=](MachineInstrBuilder &MIB) { MIB.add(Root); }}}; 284ae9d0623SCraig Topper 285ae9d0623SCraig Topper return std::nullopt; 286ae9d0623SCraig Topper } 287ae9d0623SCraig Topper 288ae9d0623SCraig Topper InstructionSelector::ComplexRendererFns 2895dc8d611SCraig Topper RISCVInstructionSelector::selectZExtBits(MachineOperand &Root, 2905dc8d611SCraig Topper unsigned Bits) const { 2915dc8d611SCraig Topper if (!Root.isReg()) 2925dc8d611SCraig Topper return std::nullopt; 2935dc8d611SCraig Topper Register RootReg = Root.getReg(); 2945dc8d611SCraig Topper 2955dc8d611SCraig Topper Register RegX; 2965dc8d611SCraig Topper uint64_t Mask = maskTrailingOnes<uint64_t>(Bits); 2975dc8d611SCraig Topper if (mi_match(RootReg, *MRI, m_GAnd(m_Reg(RegX), m_SpecificICst(Mask)))) { 2985dc8d611SCraig Topper return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(RegX); }}}; 2995dc8d611SCraig Topper } 3005dc8d611SCraig Topper 301694719a4SCraig Topper if (mi_match(RootReg, *MRI, m_GZExt(m_Reg(RegX))) && 302694719a4SCraig Topper MRI->getType(RegX).getScalarSizeInBits() == Bits) 303694719a4SCraig Topper return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(RegX); }}}; 304694719a4SCraig Topper 305da032b79SCraig Topper unsigned Size = MRI->getType(RootReg).getScalarSizeInBits(); 306da032b79SCraig Topper if (KB->maskedValueIsZero(RootReg, APInt::getBitsSetFrom(Size, Bits))) 307da032b79SCraig Topper return {{[=](MachineInstrBuilder &MIB) { MIB.add(Root); }}}; 3085dc8d611SCraig Topper 3095dc8d611SCraig Topper return std::nullopt; 3105dc8d611SCraig Topper } 3115dc8d611SCraig Topper 3125dc8d611SCraig Topper InstructionSelector::ComplexRendererFns 3135f5faf40SMin-Yih Hsu RISCVInstructionSelector::selectSHXADDOp(MachineOperand &Root, 3145f5faf40SMin-Yih Hsu unsigned ShAmt) const { 3155f5faf40SMin-Yih Hsu using namespace llvm::MIPatternMatch; 3165f5faf40SMin-Yih Hsu 3175f5faf40SMin-Yih Hsu if (!Root.isReg()) 3185f5faf40SMin-Yih Hsu return std::nullopt; 3195f5faf40SMin-Yih Hsu Register RootReg = Root.getReg(); 3205f5faf40SMin-Yih Hsu 3215f5faf40SMin-Yih Hsu const unsigned XLen = STI.getXLen(); 3225f5faf40SMin-Yih Hsu APInt Mask, C2; 3235f5faf40SMin-Yih Hsu Register RegY; 3245f5faf40SMin-Yih Hsu std::optional<bool> LeftShift; 3255f5faf40SMin-Yih Hsu // (and (shl y, c2), mask) 326a3cc4b61SMichael Maitland if (mi_match(RootReg, *MRI, 3275f5faf40SMin-Yih Hsu m_GAnd(m_GShl(m_Reg(RegY), m_ICst(C2)), m_ICst(Mask)))) 3285f5faf40SMin-Yih Hsu LeftShift = true; 3295f5faf40SMin-Yih Hsu // (and (lshr y, c2), mask) 330a3cc4b61SMichael Maitland else if (mi_match(RootReg, *MRI, 3315f5faf40SMin-Yih Hsu m_GAnd(m_GLShr(m_Reg(RegY), m_ICst(C2)), m_ICst(Mask)))) 3325f5faf40SMin-Yih Hsu LeftShift = false; 3335f5faf40SMin-Yih Hsu 3345f5faf40SMin-Yih Hsu if (LeftShift.has_value()) { 3355f5faf40SMin-Yih Hsu if (*LeftShift) 3365f5faf40SMin-Yih Hsu Mask &= maskTrailingZeros<uint64_t>(C2.getLimitedValue()); 3375f5faf40SMin-Yih Hsu else 3385f5faf40SMin-Yih Hsu Mask &= maskTrailingOnes<uint64_t>(XLen - C2.getLimitedValue()); 3395f5faf40SMin-Yih Hsu 3405f5faf40SMin-Yih Hsu if (Mask.isShiftedMask()) { 3415f5faf40SMin-Yih Hsu unsigned Leading = XLen - Mask.getActiveBits(); 3425f5faf40SMin-Yih Hsu unsigned Trailing = Mask.countr_zero(); 3435f5faf40SMin-Yih Hsu // Given (and (shl y, c2), mask) in which mask has no leading zeros and 3445f5faf40SMin-Yih Hsu // c3 trailing zeros. We can use an SRLI by c3 - c2 followed by a SHXADD. 3455f5faf40SMin-Yih Hsu if (*LeftShift && Leading == 0 && C2.ult(Trailing) && Trailing == ShAmt) { 346a3cc4b61SMichael Maitland Register DstReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 3475f5faf40SMin-Yih Hsu return {{[=](MachineInstrBuilder &MIB) { 3485f5faf40SMin-Yih Hsu MachineIRBuilder(*MIB.getInstr()) 3495f5faf40SMin-Yih Hsu .buildInstr(RISCV::SRLI, {DstReg}, {RegY}) 3505f5faf40SMin-Yih Hsu .addImm(Trailing - C2.getLimitedValue()); 3515f5faf40SMin-Yih Hsu MIB.addReg(DstReg); 3525f5faf40SMin-Yih Hsu }}}; 3535f5faf40SMin-Yih Hsu } 3545f5faf40SMin-Yih Hsu 3555f5faf40SMin-Yih Hsu // Given (and (lshr y, c2), mask) in which mask has c2 leading zeros and 3565f5faf40SMin-Yih Hsu // c3 trailing zeros. We can use an SRLI by c2 + c3 followed by a SHXADD. 3575f5faf40SMin-Yih Hsu if (!*LeftShift && Leading == C2 && Trailing == ShAmt) { 358a3cc4b61SMichael Maitland Register DstReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 3595f5faf40SMin-Yih Hsu return {{[=](MachineInstrBuilder &MIB) { 3605f5faf40SMin-Yih Hsu MachineIRBuilder(*MIB.getInstr()) 3615f5faf40SMin-Yih Hsu .buildInstr(RISCV::SRLI, {DstReg}, {RegY}) 3625f5faf40SMin-Yih Hsu .addImm(Leading + Trailing); 3635f5faf40SMin-Yih Hsu MIB.addReg(DstReg); 3645f5faf40SMin-Yih Hsu }}}; 3655f5faf40SMin-Yih Hsu } 3665f5faf40SMin-Yih Hsu } 3675f5faf40SMin-Yih Hsu } 3685f5faf40SMin-Yih Hsu 3695f5faf40SMin-Yih Hsu LeftShift.reset(); 3705f5faf40SMin-Yih Hsu 3715f5faf40SMin-Yih Hsu // (shl (and y, mask), c2) 372a3cc4b61SMichael Maitland if (mi_match(RootReg, *MRI, 3735f5faf40SMin-Yih Hsu m_GShl(m_OneNonDBGUse(m_GAnd(m_Reg(RegY), m_ICst(Mask))), 3745f5faf40SMin-Yih Hsu m_ICst(C2)))) 3755f5faf40SMin-Yih Hsu LeftShift = true; 3765f5faf40SMin-Yih Hsu // (lshr (and y, mask), c2) 377a3cc4b61SMichael Maitland else if (mi_match(RootReg, *MRI, 3785f5faf40SMin-Yih Hsu m_GLShr(m_OneNonDBGUse(m_GAnd(m_Reg(RegY), m_ICst(Mask))), 3795f5faf40SMin-Yih Hsu m_ICst(C2)))) 3805f5faf40SMin-Yih Hsu LeftShift = false; 3815f5faf40SMin-Yih Hsu 3825f5faf40SMin-Yih Hsu if (LeftShift.has_value() && Mask.isShiftedMask()) { 3835f5faf40SMin-Yih Hsu unsigned Leading = XLen - Mask.getActiveBits(); 3845f5faf40SMin-Yih Hsu unsigned Trailing = Mask.countr_zero(); 3855f5faf40SMin-Yih Hsu 3865f5faf40SMin-Yih Hsu // Given (shl (and y, mask), c2) in which mask has 32 leading zeros and 3875f5faf40SMin-Yih Hsu // c3 trailing zeros. If c1 + c3 == ShAmt, we can emit SRLIW + SHXADD. 3885f5faf40SMin-Yih Hsu bool Cond = *LeftShift && Leading == 32 && Trailing > 0 && 3895f5faf40SMin-Yih Hsu (Trailing + C2.getLimitedValue()) == ShAmt; 3905f5faf40SMin-Yih Hsu if (!Cond) 3915f5faf40SMin-Yih Hsu // Given (lshr (and y, mask), c2) in which mask has 32 leading zeros and 3925f5faf40SMin-Yih Hsu // c3 trailing zeros. If c3 - c1 == ShAmt, we can emit SRLIW + SHXADD. 3935f5faf40SMin-Yih Hsu Cond = !*LeftShift && Leading == 32 && C2.ult(Trailing) && 3945f5faf40SMin-Yih Hsu (Trailing - C2.getLimitedValue()) == ShAmt; 3955f5faf40SMin-Yih Hsu 3965f5faf40SMin-Yih Hsu if (Cond) { 397a3cc4b61SMichael Maitland Register DstReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 3985f5faf40SMin-Yih Hsu return {{[=](MachineInstrBuilder &MIB) { 3995f5faf40SMin-Yih Hsu MachineIRBuilder(*MIB.getInstr()) 4005f5faf40SMin-Yih Hsu .buildInstr(RISCV::SRLIW, {DstReg}, {RegY}) 4015f5faf40SMin-Yih Hsu .addImm(Trailing); 4025f5faf40SMin-Yih Hsu MIB.addReg(DstReg); 4035f5faf40SMin-Yih Hsu }}}; 4045f5faf40SMin-Yih Hsu } 4055f5faf40SMin-Yih Hsu } 4065f5faf40SMin-Yih Hsu 4075f5faf40SMin-Yih Hsu return std::nullopt; 4085f5faf40SMin-Yih Hsu } 4095f5faf40SMin-Yih Hsu 4105f5faf40SMin-Yih Hsu InstructionSelector::ComplexRendererFns 411cdcaef87SMin-Yih Hsu RISCVInstructionSelector::selectSHXADD_UWOp(MachineOperand &Root, 412cdcaef87SMin-Yih Hsu unsigned ShAmt) const { 413cdcaef87SMin-Yih Hsu using namespace llvm::MIPatternMatch; 414cdcaef87SMin-Yih Hsu 415cdcaef87SMin-Yih Hsu if (!Root.isReg()) 416cdcaef87SMin-Yih Hsu return std::nullopt; 417cdcaef87SMin-Yih Hsu Register RootReg = Root.getReg(); 418cdcaef87SMin-Yih Hsu 419cdcaef87SMin-Yih Hsu // Given (and (shl x, c2), mask) in which mask is a shifted mask with 420cdcaef87SMin-Yih Hsu // 32 - ShAmt leading zeros and c2 trailing zeros. We can use SLLI by 421cdcaef87SMin-Yih Hsu // c2 - ShAmt followed by SHXADD_UW with ShAmt for x amount. 422cdcaef87SMin-Yih Hsu APInt Mask, C2; 423cdcaef87SMin-Yih Hsu Register RegX; 424cdcaef87SMin-Yih Hsu if (mi_match( 425a3cc4b61SMichael Maitland RootReg, *MRI, 426cdcaef87SMin-Yih Hsu m_OneNonDBGUse(m_GAnd(m_OneNonDBGUse(m_GShl(m_Reg(RegX), m_ICst(C2))), 427cdcaef87SMin-Yih Hsu m_ICst(Mask))))) { 428cdcaef87SMin-Yih Hsu Mask &= maskTrailingZeros<uint64_t>(C2.getLimitedValue()); 429cdcaef87SMin-Yih Hsu 430cdcaef87SMin-Yih Hsu if (Mask.isShiftedMask()) { 431cdcaef87SMin-Yih Hsu unsigned Leading = Mask.countl_zero(); 432cdcaef87SMin-Yih Hsu unsigned Trailing = Mask.countr_zero(); 433cdcaef87SMin-Yih Hsu if (Leading == 32 - ShAmt && C2 == Trailing && Trailing > ShAmt) { 434a3cc4b61SMichael Maitland Register DstReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 435cdcaef87SMin-Yih Hsu return {{[=](MachineInstrBuilder &MIB) { 436cdcaef87SMin-Yih Hsu MachineIRBuilder(*MIB.getInstr()) 437cdcaef87SMin-Yih Hsu .buildInstr(RISCV::SLLI, {DstReg}, {RegX}) 438cdcaef87SMin-Yih Hsu .addImm(C2.getLimitedValue() - ShAmt); 439cdcaef87SMin-Yih Hsu MIB.addReg(DstReg); 440cdcaef87SMin-Yih Hsu }}}; 441cdcaef87SMin-Yih Hsu } 442cdcaef87SMin-Yih Hsu } 443cdcaef87SMin-Yih Hsu } 444cdcaef87SMin-Yih Hsu 445cdcaef87SMin-Yih Hsu return std::nullopt; 446cdcaef87SMin-Yih Hsu } 447cdcaef87SMin-Yih Hsu 448cdcaef87SMin-Yih Hsu InstructionSelector::ComplexRendererFns 449f873fc3aSMichael Maitland RISCVInstructionSelector::renderVLOp(MachineOperand &Root) const { 450f873fc3aSMichael Maitland assert(Root.isReg() && "Expected operand to be a Register"); 451f873fc3aSMichael Maitland MachineInstr *RootDef = MRI->getVRegDef(Root.getReg()); 452f873fc3aSMichael Maitland 453f873fc3aSMichael Maitland if (RootDef->getOpcode() == TargetOpcode::G_CONSTANT) { 454f873fc3aSMichael Maitland auto C = RootDef->getOperand(1).getCImm(); 455f873fc3aSMichael Maitland if (C->getValue().isAllOnes()) 456f873fc3aSMichael Maitland // If the operand is a G_CONSTANT with value of all ones it is larger than 457f873fc3aSMichael Maitland // VLMAX. We convert it to an immediate with value VLMaxSentinel. This is 458f873fc3aSMichael Maitland // recognized specially by the vsetvli insertion pass. 459f873fc3aSMichael Maitland return {{[=](MachineInstrBuilder &MIB) { 460f873fc3aSMichael Maitland MIB.addImm(RISCV::VLMaxSentinel); 461f873fc3aSMichael Maitland }}}; 462f873fc3aSMichael Maitland 463f873fc3aSMichael Maitland if (isUInt<5>(C->getZExtValue())) { 464f873fc3aSMichael Maitland uint64_t ZExtC = C->getZExtValue(); 465f873fc3aSMichael Maitland return {{[=](MachineInstrBuilder &MIB) { MIB.addImm(ZExtC); }}}; 466f873fc3aSMichael Maitland } 467f873fc3aSMichael Maitland } 468f873fc3aSMichael Maitland return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(Root.getReg()); }}}; 469f873fc3aSMichael Maitland } 470f873fc3aSMichael Maitland 471f873fc3aSMichael Maitland InstructionSelector::ComplexRendererFns 47286d6b379SNitin John Raj RISCVInstructionSelector::selectAddrRegImm(MachineOperand &Root) const { 473f995afe7SCraig Topper if (!Root.isReg()) 474f995afe7SCraig Topper return std::nullopt; 475f995afe7SCraig Topper 476a3cc4b61SMichael Maitland MachineInstr *RootDef = MRI->getVRegDef(Root.getReg()); 477f995afe7SCraig Topper if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) { 478f995afe7SCraig Topper return {{ 479f995afe7SCraig Topper [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); }, 480f995afe7SCraig Topper [=](MachineInstrBuilder &MIB) { MIB.addImm(0); }, 481f995afe7SCraig Topper }}; 482f995afe7SCraig Topper } 483f995afe7SCraig Topper 484a3cc4b61SMichael Maitland if (isBaseWithConstantOffset(Root, *MRI)) { 48575a9ed42SCraig Topper MachineOperand &LHS = RootDef->getOperand(1); 48675a9ed42SCraig Topper MachineOperand &RHS = RootDef->getOperand(2); 487a3cc4b61SMichael Maitland MachineInstr *LHSDef = MRI->getVRegDef(LHS.getReg()); 488a3cc4b61SMichael Maitland MachineInstr *RHSDef = MRI->getVRegDef(RHS.getReg()); 48975a9ed42SCraig Topper 49075a9ed42SCraig Topper int64_t RHSC = RHSDef->getOperand(1).getCImm()->getSExtValue(); 49175a9ed42SCraig Topper if (isInt<12>(RHSC)) { 49275a9ed42SCraig Topper if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) 49375a9ed42SCraig Topper return {{ 49475a9ed42SCraig Topper [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); }, 49575a9ed42SCraig Topper [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); }, 49675a9ed42SCraig Topper }}; 49775a9ed42SCraig Topper 49875a9ed42SCraig Topper return {{[=](MachineInstrBuilder &MIB) { MIB.add(LHS); }, 49975a9ed42SCraig Topper [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); }}}; 50075a9ed42SCraig Topper } 50175a9ed42SCraig Topper } 50275a9ed42SCraig Topper 50386d6b379SNitin John Raj // TODO: Need to get the immediate from a G_PTR_ADD. Should this be done in 50486d6b379SNitin John Raj // the combiner? 50586d6b379SNitin John Raj return {{[=](MachineInstrBuilder &MIB) { MIB.addReg(Root.getReg()); }, 50686d6b379SNitin John Raj [=](MachineInstrBuilder &MIB) { MIB.addImm(0); }}}; 50786d6b379SNitin John Raj } 50886d6b379SNitin John Raj 509e0e0891dSCraig Topper /// Returns the RISCVCC::CondCode that corresponds to the CmpInst::Predicate CC. 510e0e0891dSCraig Topper /// CC Must be an ICMP Predicate. 511e0e0891dSCraig Topper static RISCVCC::CondCode getRISCVCCFromICmp(CmpInst::Predicate CC) { 512e0e0891dSCraig Topper switch (CC) { 513e0e0891dSCraig Topper default: 514e0e0891dSCraig Topper llvm_unreachable("Expected ICMP CmpInst::Predicate."); 515e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_EQ: 516e0e0891dSCraig Topper return RISCVCC::COND_EQ; 517e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_NE: 518e0e0891dSCraig Topper return RISCVCC::COND_NE; 519e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_ULT: 520e0e0891dSCraig Topper return RISCVCC::COND_LTU; 521e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SLT: 522e0e0891dSCraig Topper return RISCVCC::COND_LT; 523e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_UGE: 524e0e0891dSCraig Topper return RISCVCC::COND_GEU; 525e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SGE: 526e0e0891dSCraig Topper return RISCVCC::COND_GE; 527e0e0891dSCraig Topper } 528e0e0891dSCraig Topper } 529e0e0891dSCraig Topper 530a3cc4b61SMichael Maitland static void getOperandsForBranch(Register CondReg, RISCVCC::CondCode &CC, 531a3cc4b61SMichael Maitland Register &LHS, Register &RHS, 532a3cc4b61SMichael Maitland MachineRegisterInfo &MRI) { 533e0e0891dSCraig Topper // Try to fold an ICmp. If that fails, use a NE compare with X0. 534e0e0891dSCraig Topper CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; 535e0e0891dSCraig Topper if (!mi_match(CondReg, MRI, m_GICmp(m_Pred(Pred), m_Reg(LHS), m_Reg(RHS)))) { 536e0e0891dSCraig Topper LHS = CondReg; 537e0e0891dSCraig Topper RHS = RISCV::X0; 538e0e0891dSCraig Topper CC = RISCVCC::COND_NE; 539e0e0891dSCraig Topper return; 540e0e0891dSCraig Topper } 541e0e0891dSCraig Topper 542e0e0891dSCraig Topper // We found an ICmp, do some canonicalizations. 543e0e0891dSCraig Topper 544e0e0891dSCraig Topper // Adjust comparisons to use comparison with 0 if possible. 545e0e0891dSCraig Topper if (auto Constant = getIConstantVRegSExtVal(RHS, MRI)) { 546e0e0891dSCraig Topper switch (Pred) { 547e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SGT: 548e0e0891dSCraig Topper // Convert X > -1 to X >= 0 549e0e0891dSCraig Topper if (*Constant == -1) { 550e0e0891dSCraig Topper CC = RISCVCC::COND_GE; 551e0e0891dSCraig Topper RHS = RISCV::X0; 552e0e0891dSCraig Topper return; 553e0e0891dSCraig Topper } 554e0e0891dSCraig Topper break; 555e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SLT: 556e0e0891dSCraig Topper // Convert X < 1 to 0 >= X 557e0e0891dSCraig Topper if (*Constant == 1) { 558e0e0891dSCraig Topper CC = RISCVCC::COND_GE; 559e0e0891dSCraig Topper RHS = LHS; 560e0e0891dSCraig Topper LHS = RISCV::X0; 561e0e0891dSCraig Topper return; 562e0e0891dSCraig Topper } 563e0e0891dSCraig Topper break; 564e0e0891dSCraig Topper default: 565e0e0891dSCraig Topper break; 566e0e0891dSCraig Topper } 567e0e0891dSCraig Topper } 568e0e0891dSCraig Topper 569e0e0891dSCraig Topper switch (Pred) { 570e0e0891dSCraig Topper default: 571e0e0891dSCraig Topper llvm_unreachable("Expected ICMP CmpInst::Predicate."); 572e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_EQ: 573e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_NE: 574e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_ULT: 575e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SLT: 576e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_UGE: 577e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SGE: 578e0e0891dSCraig Topper // These CCs are supported directly by RISC-V branches. 579e0e0891dSCraig Topper break; 580e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SGT: 581e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_SLE: 582e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_UGT: 583e0e0891dSCraig Topper case CmpInst::Predicate::ICMP_ULE: 584e0e0891dSCraig Topper // These CCs are not supported directly by RISC-V branches, but changing the 585e0e0891dSCraig Topper // direction of the CC and swapping LHS and RHS are. 586e0e0891dSCraig Topper Pred = CmpInst::getSwappedPredicate(Pred); 587e0e0891dSCraig Topper std::swap(LHS, RHS); 588e0e0891dSCraig Topper break; 589e0e0891dSCraig Topper } 590e0e0891dSCraig Topper 591e0e0891dSCraig Topper CC = getRISCVCCFromICmp(Pred); 592e0e0891dSCraig Topper } 593e0e0891dSCraig Topper 594c07062a2SNitin John Raj bool RISCVInstructionSelector::select(MachineInstr &MI) { 595ac182deeSCraig Topper MachineIRBuilder MIB(MI); 5964be39288SPhilip Reames 597a3cc4b61SMichael Maitland preISelLower(MI, MIB); 59845a29bd5SNitin John Raj const unsigned Opc = MI.getOpcode(); 59945a29bd5SNitin John Raj 6007c3c8a12SMin-Yih Hsu if (!MI.isPreISelOpcode() || Opc == TargetOpcode::G_PHI) { 60198eb28b6SCraig Topper if (Opc == TargetOpcode::PHI || Opc == TargetOpcode::G_PHI) { 60298eb28b6SCraig Topper const Register DefReg = MI.getOperand(0).getReg(); 603a3cc4b61SMichael Maitland const LLT DefTy = MRI->getType(DefReg); 60498eb28b6SCraig Topper 60598eb28b6SCraig Topper const RegClassOrRegBank &RegClassOrBank = 606a3cc4b61SMichael Maitland MRI->getRegClassOrRegBank(DefReg); 60798eb28b6SCraig Topper 60898eb28b6SCraig Topper const TargetRegisterClass *DefRC = 60910b80ff0SKazu Hirata dyn_cast<const TargetRegisterClass *>(RegClassOrBank); 61098eb28b6SCraig Topper if (!DefRC) { 61198eb28b6SCraig Topper if (!DefTy.isValid()) { 61298eb28b6SCraig Topper LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n"); 61398eb28b6SCraig Topper return false; 61498eb28b6SCraig Topper } 61598eb28b6SCraig Topper 61610b80ff0SKazu Hirata const RegisterBank &RB = *cast<const RegisterBank *>(RegClassOrBank); 61798eb28b6SCraig Topper DefRC = getRegClassForTypeOnBank(DefTy, RB); 61898eb28b6SCraig Topper if (!DefRC) { 61998eb28b6SCraig Topper LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n"); 62098eb28b6SCraig Topper return false; 62198eb28b6SCraig Topper } 62298eb28b6SCraig Topper } 62398eb28b6SCraig Topper 62498eb28b6SCraig Topper MI.setDesc(TII.get(TargetOpcode::PHI)); 625a3cc4b61SMichael Maitland return RBI.constrainGenericRegister(DefReg, *DefRC, *MRI); 62698eb28b6SCraig Topper } 62798eb28b6SCraig Topper 6284be39288SPhilip Reames // Certain non-generic instructions also need some special handling. 629c07062a2SNitin John Raj if (MI.isCopy()) 630a3cc4b61SMichael Maitland return selectCopy(MI); 631c07062a2SNitin John Raj 6324be39288SPhilip Reames return true; 6334be39288SPhilip Reames } 6344be39288SPhilip Reames 635c07062a2SNitin John Raj if (selectImpl(MI, *CoverageInfo)) 6364be39288SPhilip Reames return true; 6374be39288SPhilip Reames 638c07062a2SNitin John Raj switch (Opc) { 639c07062a2SNitin John Raj case TargetOpcode::G_ANYEXT: 64045a29bd5SNitin John Raj case TargetOpcode::G_PTRTOINT: 641cfdafc1eSCraig Topper case TargetOpcode::G_INTTOPTR: 642ac182deeSCraig Topper case TargetOpcode::G_TRUNC: 64376748119SYingwei Zheng case TargetOpcode::G_FREEZE: 644a3cc4b61SMichael Maitland return selectCopy(MI); 6454fe0d35fSCraig Topper case TargetOpcode::G_CONSTANT: { 6464fe0d35fSCraig Topper Register DstReg = MI.getOperand(0).getReg(); 6474fe0d35fSCraig Topper int64_t Imm = MI.getOperand(1).getCImm()->getSExtValue(); 6484fe0d35fSCraig Topper 6494fe0d35fSCraig Topper if (!materializeImm(DstReg, Imm, MIB)) 6504fe0d35fSCraig Topper return false; 6514fe0d35fSCraig Topper 6524fe0d35fSCraig Topper MI.eraseFromParent(); 6534fe0d35fSCraig Topper return true; 6544fe0d35fSCraig Topper } 655422ffc52SCraig Topper case TargetOpcode::G_FCONSTANT: { 656422ffc52SCraig Topper // TODO: Use constant pool for complext constants. 657422ffc52SCraig Topper // TODO: Optimize +0.0 to use fcvt.d.w for s64 on rv32. 658422ffc52SCraig Topper Register DstReg = MI.getOperand(0).getReg(); 659422ffc52SCraig Topper const APFloat &FPimm = MI.getOperand(1).getFPImm()->getValueAPF(); 660422ffc52SCraig Topper APInt Imm = FPimm.bitcastToAPInt(); 661a3cc4b61SMichael Maitland unsigned Size = MRI->getType(DstReg).getSizeInBits(); 662dff6871cSCraig Topper if (Size == 16 || Size == 32 || (Size == 64 && Subtarget->is64Bit())) { 663a3cc4b61SMichael Maitland Register GPRReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 664422ffc52SCraig Topper if (!materializeImm(GPRReg, Imm.getSExtValue(), MIB)) 665422ffc52SCraig Topper return false; 666422ffc52SCraig Topper 667dff6871cSCraig Topper unsigned Opcode = Size == 64 ? RISCV::FMV_D_X 668dff6871cSCraig Topper : Size == 32 ? RISCV::FMV_W_X 669dff6871cSCraig Topper : RISCV::FMV_H_X; 670422ffc52SCraig Topper auto FMV = MIB.buildInstr(Opcode, {DstReg}, {GPRReg}); 671422ffc52SCraig Topper if (!FMV.constrainAllUses(TII, TRI, RBI)) 672422ffc52SCraig Topper return false; 673422ffc52SCraig Topper } else { 674422ffc52SCraig Topper assert(Size == 64 && !Subtarget->is64Bit() && 675422ffc52SCraig Topper "Unexpected size or subtarget"); 676422ffc52SCraig Topper // Split into two pieces and build through the stack. 677a3cc4b61SMichael Maitland Register GPRRegHigh = MRI->createVirtualRegister(&RISCV::GPRRegClass); 678a3cc4b61SMichael Maitland Register GPRRegLow = MRI->createVirtualRegister(&RISCV::GPRRegClass); 679422ffc52SCraig Topper if (!materializeImm(GPRRegHigh, Imm.extractBits(32, 32).getSExtValue(), 680422ffc52SCraig Topper MIB)) 681422ffc52SCraig Topper return false; 682422ffc52SCraig Topper if (!materializeImm(GPRRegLow, Imm.trunc(32).getSExtValue(), MIB)) 683422ffc52SCraig Topper return false; 684422ffc52SCraig Topper MachineInstrBuilder PairF64 = MIB.buildInstr( 685422ffc52SCraig Topper RISCV::BuildPairF64Pseudo, {DstReg}, {GPRRegLow, GPRRegHigh}); 686422ffc52SCraig Topper if (!PairF64.constrainAllUses(TII, TRI, RBI)) 687422ffc52SCraig Topper return false; 688422ffc52SCraig Topper } 689422ffc52SCraig Topper 690422ffc52SCraig Topper MI.eraseFromParent(); 691422ffc52SCraig Topper return true; 692422ffc52SCraig Topper } 693fe3c4218SCraig Topper case TargetOpcode::G_GLOBAL_VALUE: { 694fe3c4218SCraig Topper auto *GV = MI.getOperand(1).getGlobal(); 695fe3c4218SCraig Topper if (GV->isThreadLocal()) { 696fe3c4218SCraig Topper // TODO: implement this case. 697fe3c4218SCraig Topper return false; 698fe3c4218SCraig Topper } 699fe3c4218SCraig Topper 700a3cc4b61SMichael Maitland return selectAddr(MI, MIB, GV->isDSOLocal(), GV->hasExternalWeakLinkage()); 701fe3c4218SCraig Topper } 70220f544d0SCraig Topper case TargetOpcode::G_JUMP_TABLE: 703f866fde5SCraig Topper case TargetOpcode::G_CONSTANT_POOL: 704fe3c4218SCraig Topper return selectAddr(MI, MIB, MRI); 70598eb28b6SCraig Topper case TargetOpcode::G_BRCOND: { 706e0e0891dSCraig Topper Register LHS, RHS; 707e0e0891dSCraig Topper RISCVCC::CondCode CC; 708a3cc4b61SMichael Maitland getOperandsForBranch(MI.getOperand(0).getReg(), CC, LHS, RHS, *MRI); 709e0e0891dSCraig Topper 710e0e0891dSCraig Topper auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(CC), {}, {LHS, RHS}) 71198eb28b6SCraig Topper .addMBB(MI.getOperand(1).getMBB()); 71298eb28b6SCraig Topper MI.eraseFromParent(); 71398eb28b6SCraig Topper return constrainSelectedInstRegOperands(*Bcc, TII, TRI, RBI); 71498eb28b6SCraig Topper } 715179a2e04SCraig Topper case TargetOpcode::G_BRINDIRECT: 716179a2e04SCraig Topper MI.setDesc(TII.get(RISCV::PseudoBRIND)); 717179a2e04SCraig Topper MI.addOperand(MachineOperand::CreateImm(0)); 718179a2e04SCraig Topper return constrainSelectedInstRegOperands(MI, TII, TRI, RBI); 7197f9a50ffSNitin John Raj case TargetOpcode::G_SELECT: 720a3cc4b61SMichael Maitland return selectSelect(MI, MIB); 721fdbff881SCraig Topper case TargetOpcode::G_FCMP: 722a3cc4b61SMichael Maitland return selectFPCompare(MI, MIB); 723ffcc5c77SCraig Topper case TargetOpcode::G_FENCE: { 724ffcc5c77SCraig Topper AtomicOrdering FenceOrdering = 725ffcc5c77SCraig Topper static_cast<AtomicOrdering>(MI.getOperand(0).getImm()); 726ffcc5c77SCraig Topper SyncScope::ID FenceSSID = 727ffcc5c77SCraig Topper static_cast<SyncScope::ID>(MI.getOperand(1).getImm()); 728ffcc5c77SCraig Topper emitFence(FenceOrdering, FenceSSID, MIB); 729ffcc5c77SCraig Topper MI.eraseFromParent(); 730ffcc5c77SCraig Topper return true; 731ffcc5c77SCraig Topper } 7326976dac0SMichael Maitland case TargetOpcode::G_IMPLICIT_DEF: 733a3cc4b61SMichael Maitland return selectImplicitDef(MI, MIB); 734dbb9043dSMichael Maitland case TargetOpcode::G_UNMERGE_VALUES: 735a3cc4b61SMichael Maitland return selectUnmergeValues(MI, MIB); 736c07062a2SNitin John Raj default: 737c07062a2SNitin John Raj return false; 738c07062a2SNitin John Raj } 739c07062a2SNitin John Raj } 740c07062a2SNitin John Raj 741dbb9043dSMichael Maitland bool RISCVInstructionSelector::selectUnmergeValues( 742a3cc4b61SMichael Maitland MachineInstr &MI, MachineIRBuilder &MIB) const { 743dbb9043dSMichael Maitland assert(MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES); 744dbb9043dSMichael Maitland 745*785b16adSCraig Topper if (!Subtarget->hasStdExtZfa()) 746*785b16adSCraig Topper return false; 747*785b16adSCraig Topper 748dbb9043dSMichael Maitland // Split F64 Src into two s32 parts 749dbb9043dSMichael Maitland if (MI.getNumOperands() != 3) 750dbb9043dSMichael Maitland return false; 751dbb9043dSMichael Maitland Register Src = MI.getOperand(2).getReg(); 752dbb9043dSMichael Maitland Register Lo = MI.getOperand(0).getReg(); 753dbb9043dSMichael Maitland Register Hi = MI.getOperand(1).getReg(); 754a3cc4b61SMichael Maitland if (!isRegInFprb(Src) || !isRegInGprb(Lo) || !isRegInGprb(Hi)) 755dbb9043dSMichael Maitland return false; 756*785b16adSCraig Topper 757*785b16adSCraig Topper MachineInstr *ExtractLo = MIB.buildInstr(RISCV::FMV_X_W_FPR64, {Lo}, {Src}); 758*785b16adSCraig Topper if (!constrainSelectedInstRegOperands(*ExtractLo, TII, TRI, RBI)) 759*785b16adSCraig Topper return false; 760*785b16adSCraig Topper 761*785b16adSCraig Topper MachineInstr *ExtractHi = MIB.buildInstr(RISCV::FMVH_X_D, {Hi}, {Src}); 762*785b16adSCraig Topper if (!constrainSelectedInstRegOperands(*ExtractHi, TII, TRI, RBI)) 763*785b16adSCraig Topper return false; 764*785b16adSCraig Topper 765*785b16adSCraig Topper MI.eraseFromParent(); 766*785b16adSCraig Topper return true; 767dbb9043dSMichael Maitland } 768dbb9043dSMichael Maitland 76945a29bd5SNitin John Raj bool RISCVInstructionSelector::replacePtrWithInt(MachineOperand &Op, 770a3cc4b61SMichael Maitland MachineIRBuilder &MIB) { 77145a29bd5SNitin John Raj Register PtrReg = Op.getReg(); 772a3cc4b61SMichael Maitland assert(MRI->getType(PtrReg).isPointer() && "Operand is not a pointer!"); 77345a29bd5SNitin John Raj 77456b99f05SCraig Topper const LLT sXLen = LLT::scalar(STI.getXLen()); 77556b99f05SCraig Topper auto PtrToInt = MIB.buildPtrToInt(sXLen, PtrReg); 776a3cc4b61SMichael Maitland MRI->setRegBank(PtrToInt.getReg(0), RBI.getRegBank(RISCV::GPRBRegBankID)); 77745a29bd5SNitin John Raj Op.setReg(PtrToInt.getReg(0)); 77845a29bd5SNitin John Raj return select(*PtrToInt); 77945a29bd5SNitin John Raj } 78045a29bd5SNitin John Raj 78145a29bd5SNitin John Raj void RISCVInstructionSelector::preISelLower(MachineInstr &MI, 782a3cc4b61SMichael Maitland MachineIRBuilder &MIB) { 78345a29bd5SNitin John Raj switch (MI.getOpcode()) { 78445a29bd5SNitin John Raj case TargetOpcode::G_PTR_ADD: { 78545a29bd5SNitin John Raj Register DstReg = MI.getOperand(0).getReg(); 78656b99f05SCraig Topper const LLT sXLen = LLT::scalar(STI.getXLen()); 78745a29bd5SNitin John Raj 788a3cc4b61SMichael Maitland replacePtrWithInt(MI.getOperand(1), MIB); 78945a29bd5SNitin John Raj MI.setDesc(TII.get(TargetOpcode::G_ADD)); 790a3cc4b61SMichael Maitland MRI->setType(DstReg, sXLen); 79145a29bd5SNitin John Raj break; 79245a29bd5SNitin John Raj } 793a6f72785SMichael Maitland case TargetOpcode::G_PTRMASK: { 794a6f72785SMichael Maitland Register DstReg = MI.getOperand(0).getReg(); 795a6f72785SMichael Maitland const LLT sXLen = LLT::scalar(STI.getXLen()); 796a3cc4b61SMichael Maitland replacePtrWithInt(MI.getOperand(1), MIB); 797a6f72785SMichael Maitland MI.setDesc(TII.get(TargetOpcode::G_AND)); 798a3cc4b61SMichael Maitland MRI->setType(DstReg, sXLen); 799989c437dSMichael Maitland break; 800a6f72785SMichael Maitland } 80145a29bd5SNitin John Raj } 80245a29bd5SNitin John Raj } 80345a29bd5SNitin John Raj 804ac182deeSCraig Topper void RISCVInstructionSelector::renderNegImm(MachineInstrBuilder &MIB, 805ac182deeSCraig Topper const MachineInstr &MI, 806ac182deeSCraig Topper int OpIdx) const { 807ac182deeSCraig Topper assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 808ac182deeSCraig Topper "Expected G_CONSTANT"); 809ac182deeSCraig Topper int64_t CstVal = MI.getOperand(1).getCImm()->getSExtValue(); 810ac182deeSCraig Topper MIB.addImm(-CstVal); 811ac182deeSCraig Topper } 812ac182deeSCraig Topper 813d605d9d7SCraig Topper void RISCVInstructionSelector::renderImmSubFromXLen(MachineInstrBuilder &MIB, 814d605d9d7SCraig Topper const MachineInstr &MI, 815d605d9d7SCraig Topper int OpIdx) const { 816d605d9d7SCraig Topper assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 817d605d9d7SCraig Topper "Expected G_CONSTANT"); 818d605d9d7SCraig Topper uint64_t CstVal = MI.getOperand(1).getCImm()->getZExtValue(); 819d605d9d7SCraig Topper MIB.addImm(STI.getXLen() - CstVal); 820d605d9d7SCraig Topper } 821d605d9d7SCraig Topper 822d605d9d7SCraig Topper void RISCVInstructionSelector::renderImmSubFrom32(MachineInstrBuilder &MIB, 823d605d9d7SCraig Topper const MachineInstr &MI, 824d605d9d7SCraig Topper int OpIdx) const { 825d605d9d7SCraig Topper assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 826d605d9d7SCraig Topper "Expected G_CONSTANT"); 827d605d9d7SCraig Topper uint64_t CstVal = MI.getOperand(1).getCImm()->getZExtValue(); 828d605d9d7SCraig Topper MIB.addImm(32 - CstVal); 829d605d9d7SCraig Topper } 830d605d9d7SCraig Topper 83186d6b379SNitin John Raj void RISCVInstructionSelector::renderImmPlus1(MachineInstrBuilder &MIB, 83286d6b379SNitin John Raj const MachineInstr &MI, 83386d6b379SNitin John Raj int OpIdx) const { 83486d6b379SNitin John Raj assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 83586d6b379SNitin John Raj "Expected G_CONSTANT"); 83686d6b379SNitin John Raj int64_t CstVal = MI.getOperand(1).getCImm()->getSExtValue(); 83786d6b379SNitin John Raj MIB.addImm(CstVal + 1); 83886d6b379SNitin John Raj } 83986d6b379SNitin John Raj 8409d1a3fddSCraig Topper void RISCVInstructionSelector::renderImm(MachineInstrBuilder &MIB, 8419d1a3fddSCraig Topper const MachineInstr &MI, 8429d1a3fddSCraig Topper int OpIdx) const { 8439d1a3fddSCraig Topper assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 8449d1a3fddSCraig Topper "Expected G_CONSTANT"); 8459d1a3fddSCraig Topper int64_t CstVal = MI.getOperand(1).getCImm()->getSExtValue(); 8469d1a3fddSCraig Topper MIB.addImm(CstVal); 8479d1a3fddSCraig Topper } 8489d1a3fddSCraig Topper 849ce393bedSSergei Barannikov void RISCVInstructionSelector::renderFrameIndex(MachineInstrBuilder &MIB, 850ce393bedSSergei Barannikov const MachineInstr &MI, 851ce393bedSSergei Barannikov int OpIdx) const { 852ce393bedSSergei Barannikov assert(MI.getOpcode() == TargetOpcode::G_FRAME_INDEX && OpIdx == -1 && 853ce393bedSSergei Barannikov "Expected G_FRAME_INDEX"); 854ce393bedSSergei Barannikov MIB.add(MI.getOperand(1)); 855ce393bedSSergei Barannikov } 856ce393bedSSergei Barannikov 857cdcaef87SMin-Yih Hsu void RISCVInstructionSelector::renderTrailingZeros(MachineInstrBuilder &MIB, 858cdcaef87SMin-Yih Hsu const MachineInstr &MI, 859cdcaef87SMin-Yih Hsu int OpIdx) const { 860cdcaef87SMin-Yih Hsu assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 861cdcaef87SMin-Yih Hsu "Expected G_CONSTANT"); 862cdcaef87SMin-Yih Hsu uint64_t C = MI.getOperand(1).getCImm()->getZExtValue(); 863cdcaef87SMin-Yih Hsu MIB.addImm(llvm::countr_zero(C)); 864cdcaef87SMin-Yih Hsu } 865cdcaef87SMin-Yih Hsu 866d78fe84dSCraig Topper void RISCVInstructionSelector::renderXLenSubTrailingOnes( 867d78fe84dSCraig Topper MachineInstrBuilder &MIB, const MachineInstr &MI, int OpIdx) const { 868d78fe84dSCraig Topper assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 869d78fe84dSCraig Topper "Expected G_CONSTANT"); 870d78fe84dSCraig Topper uint64_t C = MI.getOperand(1).getCImm()->getZExtValue(); 871d78fe84dSCraig Topper MIB.addImm(Subtarget->getXLen() - llvm::countr_one(C)); 872d78fe84dSCraig Topper } 873d78fe84dSCraig Topper 8746ab8401fSLuke Quinn void RISCVInstructionSelector::renderAddiPairImmSmall(MachineInstrBuilder &MIB, 8756ab8401fSLuke Quinn const MachineInstr &MI, 8766ab8401fSLuke Quinn int OpIdx) const { 8776ab8401fSLuke Quinn assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 8786ab8401fSLuke Quinn "Expected G_CONSTANT"); 8796ab8401fSLuke Quinn int64_t Imm = MI.getOperand(1).getCImm()->getSExtValue(); 8806ab8401fSLuke Quinn int64_t Adj = Imm < 0 ? -2048 : 2047; 8816ab8401fSLuke Quinn MIB.addImm(Imm - Adj); 8826ab8401fSLuke Quinn } 8836ab8401fSLuke Quinn 8846ab8401fSLuke Quinn void RISCVInstructionSelector::renderAddiPairImmLarge(MachineInstrBuilder &MIB, 8856ab8401fSLuke Quinn const MachineInstr &MI, 8866ab8401fSLuke Quinn int OpIdx) const { 8876ab8401fSLuke Quinn assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && OpIdx == -1 && 8886ab8401fSLuke Quinn "Expected G_CONSTANT"); 8896ab8401fSLuke Quinn int64_t Imm = MI.getOperand(1).getCImm()->getSExtValue() < 0 ? -2048 : 2047; 8906ab8401fSLuke Quinn MIB.addImm(Imm); 8916ab8401fSLuke Quinn } 8926ab8401fSLuke Quinn 893c07062a2SNitin John Raj const TargetRegisterClass *RISCVInstructionSelector::getRegClassForTypeOnBank( 89498eb28b6SCraig Topper LLT Ty, const RegisterBank &RB) const { 8958c53cfd3SCraig Topper if (RB.getID() == RISCV::GPRBRegBankID) { 896ac182deeSCraig Topper if (Ty.getSizeInBits() <= 32 || (STI.is64Bit() && Ty.getSizeInBits() == 64)) 897c07062a2SNitin John Raj return &RISCV::GPRRegClass; 898c07062a2SNitin John Raj } 899c07062a2SNitin John Raj 9008c53cfd3SCraig Topper if (RB.getID() == RISCV::FPRBRegBankID) { 901643e4718SYingwei Zheng if (Ty.getSizeInBits() == 16) 902643e4718SYingwei Zheng return &RISCV::FPR16RegClass; 903716c0220SCraig Topper if (Ty.getSizeInBits() == 32) 904716c0220SCraig Topper return &RISCV::FPR32RegClass; 905716c0220SCraig Topper if (Ty.getSizeInBits() == 64) 906716c0220SCraig Topper return &RISCV::FPR64RegClass; 907716c0220SCraig Topper } 908716c0220SCraig Topper 90910c2d5ffSJiahan Xie if (RB.getID() == RISCV::VRBRegBankID) { 91010c2d5ffSJiahan Xie if (Ty.getSizeInBits().getKnownMinValue() <= 64) 91110c2d5ffSJiahan Xie return &RISCV::VRRegClass; 91210c2d5ffSJiahan Xie 91310c2d5ffSJiahan Xie if (Ty.getSizeInBits().getKnownMinValue() == 128) 91410c2d5ffSJiahan Xie return &RISCV::VRM2RegClass; 91510c2d5ffSJiahan Xie 91610c2d5ffSJiahan Xie if (Ty.getSizeInBits().getKnownMinValue() == 256) 91710c2d5ffSJiahan Xie return &RISCV::VRM4RegClass; 91810c2d5ffSJiahan Xie 91910c2d5ffSJiahan Xie if (Ty.getSizeInBits().getKnownMinValue() == 512) 92010c2d5ffSJiahan Xie return &RISCV::VRM8RegClass; 92110c2d5ffSJiahan Xie } 92210c2d5ffSJiahan Xie 923c07062a2SNitin John Raj return nullptr; 924c07062a2SNitin John Raj } 925c07062a2SNitin John Raj 926a3cc4b61SMichael Maitland bool RISCVInstructionSelector::isRegInGprb(Register Reg) const { 927a3cc4b61SMichael Maitland return RBI.getRegBank(Reg, *MRI, TRI)->getID() == RISCV::GPRBRegBankID; 928dbb9043dSMichael Maitland } 929dbb9043dSMichael Maitland 930a3cc4b61SMichael Maitland bool RISCVInstructionSelector::isRegInFprb(Register Reg) const { 931a3cc4b61SMichael Maitland return RBI.getRegBank(Reg, *MRI, TRI)->getID() == RISCV::FPRBRegBankID; 932dbb9043dSMichael Maitland } 933dbb9043dSMichael Maitland 934a3cc4b61SMichael Maitland bool RISCVInstructionSelector::selectCopy(MachineInstr &MI) const { 935c07062a2SNitin John Raj Register DstReg = MI.getOperand(0).getReg(); 936c07062a2SNitin John Raj 9379d488569SCraig Topper if (DstReg.isPhysical()) 938c07062a2SNitin John Raj return true; 939c07062a2SNitin John Raj 940c07062a2SNitin John Raj const TargetRegisterClass *DstRC = getRegClassForTypeOnBank( 941a3cc4b61SMichael Maitland MRI->getType(DstReg), *RBI.getRegBank(DstReg, *MRI, TRI)); 942c07062a2SNitin John Raj assert(DstRC && 943c07062a2SNitin John Raj "Register class not available for LLT, register bank combination"); 944c07062a2SNitin John Raj 9459d488569SCraig Topper // No need to constrain SrcReg. It will get constrained when 9469d488569SCraig Topper // we hit another of its uses or its defs. 9479d488569SCraig Topper // Copies do not have constraints. 948a3cc4b61SMichael Maitland if (!RBI.constrainGenericRegister(DstReg, *DstRC, *MRI)) { 949c07062a2SNitin John Raj LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(MI.getOpcode()) 950c07062a2SNitin John Raj << " operand\n"); 951c07062a2SNitin John Raj return false; 952c07062a2SNitin John Raj } 953c07062a2SNitin John Raj 9549d488569SCraig Topper MI.setDesc(TII.get(RISCV::COPY)); 955c07062a2SNitin John Raj return true; 956c07062a2SNitin John Raj } 957c07062a2SNitin John Raj 958a3cc4b61SMichael Maitland bool RISCVInstructionSelector::selectImplicitDef(MachineInstr &MI, 959a3cc4b61SMichael Maitland MachineIRBuilder &MIB) const { 9606976dac0SMichael Maitland assert(MI.getOpcode() == TargetOpcode::G_IMPLICIT_DEF); 9616976dac0SMichael Maitland 9626976dac0SMichael Maitland const Register DstReg = MI.getOperand(0).getReg(); 9636976dac0SMichael Maitland const TargetRegisterClass *DstRC = getRegClassForTypeOnBank( 964a3cc4b61SMichael Maitland MRI->getType(DstReg), *RBI.getRegBank(DstReg, *MRI, TRI)); 9656976dac0SMichael Maitland 9666976dac0SMichael Maitland assert(DstRC && 9676976dac0SMichael Maitland "Register class not available for LLT, register bank combination"); 9686976dac0SMichael Maitland 969a3cc4b61SMichael Maitland if (!RBI.constrainGenericRegister(DstReg, *DstRC, *MRI)) { 9706976dac0SMichael Maitland LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(MI.getOpcode()) 9716976dac0SMichael Maitland << " operand\n"); 9726976dac0SMichael Maitland } 9736976dac0SMichael Maitland MI.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF)); 9746976dac0SMichael Maitland return true; 9756976dac0SMichael Maitland } 9766976dac0SMichael Maitland 9774fe0d35fSCraig Topper bool RISCVInstructionSelector::materializeImm(Register DstReg, int64_t Imm, 9784fe0d35fSCraig Topper MachineIRBuilder &MIB) const { 979972df2ceSCraig Topper if (Imm == 0) { 9804fe0d35fSCraig Topper MIB.buildCopy(DstReg, Register(RISCV::X0)); 981a3cc4b61SMichael Maitland RBI.constrainGenericRegister(DstReg, RISCV::GPRRegClass, *MRI); 982972df2ceSCraig Topper return true; 983972df2ceSCraig Topper } 984972df2ceSCraig Topper 985e179b125SWang Pengcheng RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Imm, *Subtarget); 986c07062a2SNitin John Raj unsigned NumInsts = Seq.size(); 987c07062a2SNitin John Raj Register SrcReg = RISCV::X0; 988c07062a2SNitin John Raj 989c07062a2SNitin John Raj for (unsigned i = 0; i < NumInsts; i++) { 9904fe0d35fSCraig Topper Register TmpReg = i < NumInsts - 1 991a3cc4b61SMichael Maitland ? MRI->createVirtualRegister(&RISCV::GPRRegClass) 9924fe0d35fSCraig Topper : DstReg; 993c07062a2SNitin John Raj const RISCVMatInt::Inst &I = Seq[i]; 994c07062a2SNitin John Raj MachineInstr *Result; 995c07062a2SNitin John Raj 996c07062a2SNitin John Raj switch (I.getOpndKind()) { 997c07062a2SNitin John Raj case RISCVMatInt::Imm: 998c07062a2SNitin John Raj // clang-format off 999d0f6825dSCraig Topper Result = MIB.buildInstr(I.getOpcode(), {TmpReg}, {}) 1000c07062a2SNitin John Raj .addImm(I.getImm()); 1001c07062a2SNitin John Raj // clang-format on 1002c07062a2SNitin John Raj break; 1003c07062a2SNitin John Raj case RISCVMatInt::RegX0: 1004d0f6825dSCraig Topper Result = MIB.buildInstr(I.getOpcode(), {TmpReg}, 1005d0f6825dSCraig Topper {SrcReg, Register(RISCV::X0)}); 1006c07062a2SNitin John Raj break; 1007c07062a2SNitin John Raj case RISCVMatInt::RegReg: 1008d0f6825dSCraig Topper Result = MIB.buildInstr(I.getOpcode(), {TmpReg}, {SrcReg, SrcReg}); 1009c07062a2SNitin John Raj break; 1010c07062a2SNitin John Raj case RISCVMatInt::RegImm: 1011d0f6825dSCraig Topper Result = 1012d0f6825dSCraig Topper MIB.buildInstr(I.getOpcode(), {TmpReg}, {SrcReg}).addImm(I.getImm()); 1013c07062a2SNitin John Raj break; 1014c07062a2SNitin John Raj } 1015c07062a2SNitin John Raj 1016c07062a2SNitin John Raj if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI)) 1017c07062a2SNitin John Raj return false; 1018c07062a2SNitin John Raj 10194fe0d35fSCraig Topper SrcReg = TmpReg; 1020c07062a2SNitin John Raj } 1021c07062a2SNitin John Raj 1022c07062a2SNitin John Raj return true; 10234be39288SPhilip Reames } 10244be39288SPhilip Reames 1025fe3c4218SCraig Topper bool RISCVInstructionSelector::selectAddr(MachineInstr &MI, 1026a3cc4b61SMichael Maitland MachineIRBuilder &MIB, bool IsLocal, 1027fe3c4218SCraig Topper bool IsExternWeak) const { 1028fe3c4218SCraig Topper assert((MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE || 1029f866fde5SCraig Topper MI.getOpcode() == TargetOpcode::G_JUMP_TABLE || 1030f866fde5SCraig Topper MI.getOpcode() == TargetOpcode::G_CONSTANT_POOL) && 1031fe3c4218SCraig Topper "Unexpected opcode"); 103204dd2ac0SMichael Maitland 1033fe3c4218SCraig Topper const MachineOperand &DispMO = MI.getOperand(1); 103404dd2ac0SMichael Maitland 103504dd2ac0SMichael Maitland Register DefReg = MI.getOperand(0).getReg(); 1036a3cc4b61SMichael Maitland const LLT DefTy = MRI->getType(DefReg); 103704dd2ac0SMichael Maitland 103804dd2ac0SMichael Maitland // When HWASAN is used and tagging of global variables is enabled 103904dd2ac0SMichael Maitland // they should be accessed via the GOT, since the tagged address of a global 104004dd2ac0SMichael Maitland // is incompatible with existing code models. This also applies to non-pic 104104dd2ac0SMichael Maitland // mode. 104204dd2ac0SMichael Maitland if (TM.isPositionIndependent() || Subtarget->allowTaggedGlobals()) { 1043fe3c4218SCraig Topper if (IsLocal && !Subtarget->allowTaggedGlobals()) { 104404dd2ac0SMichael Maitland // Use PC-relative addressing to access the symbol. This generates the 104504dd2ac0SMichael Maitland // pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym)) 104604dd2ac0SMichael Maitland // %pcrel_lo(auipc)). 10474a2db23eSCraig Topper MI.setDesc(TII.get(RISCV::PseudoLLA)); 10484a2db23eSCraig Topper return constrainSelectedInstRegOperands(MI, TII, TRI, RBI); 10494a2db23eSCraig Topper } 10504a2db23eSCraig Topper 105104dd2ac0SMichael Maitland // Use PC-relative addressing to access the GOT for this symbol, then 105204dd2ac0SMichael Maitland // load the address from the GOT. This generates the pattern (PseudoLGA 105304dd2ac0SMichael Maitland // sym), which expands to (ld (addi (auipc %got_pcrel_hi(sym)) 105404dd2ac0SMichael Maitland // %pcrel_lo(auipc))). 105504dd2ac0SMichael Maitland MachineFunction &MF = *MI.getParent()->getParent(); 105604dd2ac0SMichael Maitland MachineMemOperand *MemOp = MF.getMachineMemOperand( 105704dd2ac0SMichael Maitland MachinePointerInfo::getGOT(MF), 105804dd2ac0SMichael Maitland MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 105904dd2ac0SMichael Maitland MachineMemOperand::MOInvariant, 106004dd2ac0SMichael Maitland DefTy, Align(DefTy.getSizeInBits() / 8)); 106104dd2ac0SMichael Maitland 10624a2db23eSCraig Topper auto Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {}) 1063fe3c4218SCraig Topper .addDisp(DispMO, 0) 106404dd2ac0SMichael Maitland .addMemOperand(MemOp); 106504dd2ac0SMichael Maitland 106604dd2ac0SMichael Maitland if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI)) 106704dd2ac0SMichael Maitland return false; 106804dd2ac0SMichael Maitland 106904dd2ac0SMichael Maitland MI.eraseFromParent(); 107004dd2ac0SMichael Maitland return true; 107104dd2ac0SMichael Maitland } 107204dd2ac0SMichael Maitland 107304dd2ac0SMichael Maitland switch (TM.getCodeModel()) { 107404dd2ac0SMichael Maitland default: { 107504dd2ac0SMichael Maitland reportGISelFailure(const_cast<MachineFunction &>(*MF), *TPC, *MORE, 107604dd2ac0SMichael Maitland getName(), "Unsupported code model for lowering", MI); 107704dd2ac0SMichael Maitland return false; 107804dd2ac0SMichael Maitland } 107904dd2ac0SMichael Maitland case CodeModel::Small: { 108004dd2ac0SMichael Maitland // Must lie within a single 2 GiB address range and must lie between 108104dd2ac0SMichael Maitland // absolute addresses -2 GiB and +2 GiB. This generates the pattern (addi 108204dd2ac0SMichael Maitland // (lui %hi(sym)) %lo(sym)). 1083a3cc4b61SMichael Maitland Register AddrHiDest = MRI->createVirtualRegister(&RISCV::GPRRegClass); 1084d0f6825dSCraig Topper MachineInstr *AddrHi = MIB.buildInstr(RISCV::LUI, {AddrHiDest}, {}) 1085fe3c4218SCraig Topper .addDisp(DispMO, 0, RISCVII::MO_HI); 108604dd2ac0SMichael Maitland 108704dd2ac0SMichael Maitland if (!constrainSelectedInstRegOperands(*AddrHi, TII, TRI, RBI)) 108804dd2ac0SMichael Maitland return false; 108904dd2ac0SMichael Maitland 10904a2db23eSCraig Topper auto Result = MIB.buildInstr(RISCV::ADDI, {DefReg}, {AddrHiDest}) 1091fe3c4218SCraig Topper .addDisp(DispMO, 0, RISCVII::MO_LO); 109204dd2ac0SMichael Maitland 109304dd2ac0SMichael Maitland if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI)) 109404dd2ac0SMichael Maitland return false; 109504dd2ac0SMichael Maitland 109604dd2ac0SMichael Maitland MI.eraseFromParent(); 109704dd2ac0SMichael Maitland return true; 109804dd2ac0SMichael Maitland } 1099fe3c4218SCraig Topper case CodeModel::Medium: 110004dd2ac0SMichael Maitland // Emit LGA/LLA instead of the sequence it expands to because the pcrel_lo 110104dd2ac0SMichael Maitland // relocation needs to reference a label that points to the auipc 110204dd2ac0SMichael Maitland // instruction itself, not the global. This cannot be done inside the 110304dd2ac0SMichael Maitland // instruction selector. 1104fe3c4218SCraig Topper if (IsExternWeak) { 110504dd2ac0SMichael Maitland // An extern weak symbol may be undefined, i.e. have value 0, which may 110604dd2ac0SMichael Maitland // not be within 2GiB of PC, so use GOT-indirect addressing to access the 110704dd2ac0SMichael Maitland // symbol. This generates the pattern (PseudoLGA sym), which expands to 110804dd2ac0SMichael Maitland // (ld (addi (auipc %got_pcrel_hi(sym)) %pcrel_lo(auipc))). 110904dd2ac0SMichael Maitland MachineFunction &MF = *MI.getParent()->getParent(); 111004dd2ac0SMichael Maitland MachineMemOperand *MemOp = MF.getMachineMemOperand( 111104dd2ac0SMichael Maitland MachinePointerInfo::getGOT(MF), 111204dd2ac0SMichael Maitland MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 111304dd2ac0SMichael Maitland MachineMemOperand::MOInvariant, 111404dd2ac0SMichael Maitland DefTy, Align(DefTy.getSizeInBits() / 8)); 111504dd2ac0SMichael Maitland 11164a2db23eSCraig Topper auto Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {}) 1117fe3c4218SCraig Topper .addDisp(DispMO, 0) 111804dd2ac0SMichael Maitland .addMemOperand(MemOp); 111920f544d0SCraig Topper 112020f544d0SCraig Topper if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI)) 112120f544d0SCraig Topper return false; 112220f544d0SCraig Topper 112320f544d0SCraig Topper MI.eraseFromParent(); 112420f544d0SCraig Topper return true; 112520f544d0SCraig Topper } 112620f544d0SCraig Topper 11274a2db23eSCraig Topper // Generate a sequence for accessing addresses within any 2GiB range 11284a2db23eSCraig Topper // within the address space. This generates the pattern (PseudoLLA sym), 11294a2db23eSCraig Topper // which expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)). 11304a2db23eSCraig Topper MI.setDesc(TII.get(RISCV::PseudoLLA)); 11314a2db23eSCraig Topper return constrainSelectedInstRegOperands(MI, TII, TRI, RBI); 11324a2db23eSCraig Topper } 11334a2db23eSCraig Topper 113420f544d0SCraig Topper return false; 113520f544d0SCraig Topper } 113620f544d0SCraig Topper 11377f9a50ffSNitin John Raj bool RISCVInstructionSelector::selectSelect(MachineInstr &MI, 1138a3cc4b61SMichael Maitland MachineIRBuilder &MIB) const { 1139b8014b58SCraig Topper auto &SelectMI = cast<GSelect>(MI); 11404458ba8cSMichael Maitland 11414c3206c5SCraig Topper Register LHS, RHS; 11424c3206c5SCraig Topper RISCVCC::CondCode CC; 1143a3cc4b61SMichael Maitland getOperandsForBranch(SelectMI.getCondReg(), CC, LHS, RHS, *MRI); 1144b8014b58SCraig Topper 11455f31dbd1SCraig Topper Register DstReg = SelectMI.getReg(0); 11465f31dbd1SCraig Topper 11475f31dbd1SCraig Topper unsigned Opc = RISCV::Select_GPR_Using_CC_GPR; 1148a3cc4b61SMichael Maitland if (RBI.getRegBank(DstReg, *MRI, TRI)->getID() == RISCV::FPRBRegBankID) { 1149a3cc4b61SMichael Maitland unsigned Size = MRI->getType(DstReg).getSizeInBits(); 11505f31dbd1SCraig Topper Opc = Size == 32 ? RISCV::Select_FPR32_Using_CC_GPR 11515f31dbd1SCraig Topper : RISCV::Select_FPR64_Using_CC_GPR; 11525f31dbd1SCraig Topper } 11535f31dbd1SCraig Topper 11545f31dbd1SCraig Topper MachineInstr *Result = MIB.buildInstr(Opc) 11555f31dbd1SCraig Topper .addDef(DstReg) 1156b8014b58SCraig Topper .addReg(LHS) 1157b8014b58SCraig Topper .addReg(RHS) 1158b8014b58SCraig Topper .addImm(CC) 1159b8014b58SCraig Topper .addReg(SelectMI.getTrueReg()) 1160b8014b58SCraig Topper .addReg(SelectMI.getFalseReg()); 11617f9a50ffSNitin John Raj MI.eraseFromParent(); 11627f9a50ffSNitin John Raj return constrainSelectedInstRegOperands(*Result, TII, TRI, RBI); 11637f9a50ffSNitin John Raj } 11647f9a50ffSNitin John Raj 1165fdbff881SCraig Topper // Convert an FCMP predicate to one of the supported F or D instructions. 1166fdbff881SCraig Topper static unsigned getFCmpOpcode(CmpInst::Predicate Pred, unsigned Size) { 1167acd6cb85SCraig Topper assert((Size == 16 || Size == 32 || Size == 64) && "Unsupported size"); 1168fdbff881SCraig Topper switch (Pred) { 1169fdbff881SCraig Topper default: 1170fdbff881SCraig Topper llvm_unreachable("Unsupported predicate"); 1171fdbff881SCraig Topper case CmpInst::FCMP_OLT: 1172acd6cb85SCraig Topper return Size == 16 ? RISCV::FLT_H : Size == 32 ? RISCV::FLT_S : RISCV::FLT_D; 1173fdbff881SCraig Topper case CmpInst::FCMP_OLE: 1174acd6cb85SCraig Topper return Size == 16 ? RISCV::FLE_H : Size == 32 ? RISCV::FLE_S : RISCV::FLE_D; 1175fdbff881SCraig Topper case CmpInst::FCMP_OEQ: 1176acd6cb85SCraig Topper return Size == 16 ? RISCV::FEQ_H : Size == 32 ? RISCV::FEQ_S : RISCV::FEQ_D; 1177fdbff881SCraig Topper } 1178fdbff881SCraig Topper } 1179fdbff881SCraig Topper 1180fdbff881SCraig Topper // Try legalizing an FCMP by swapping or inverting the predicate to one that 1181fdbff881SCraig Topper // is supported. 1182fdbff881SCraig Topper static bool legalizeFCmpPredicate(Register &LHS, Register &RHS, 1183fdbff881SCraig Topper CmpInst::Predicate &Pred, bool &NeedInvert) { 1184fdbff881SCraig Topper auto isLegalFCmpPredicate = [](CmpInst::Predicate Pred) { 1185fdbff881SCraig Topper return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE || 1186fdbff881SCraig Topper Pred == CmpInst::FCMP_OEQ; 1187fdbff881SCraig Topper }; 1188fdbff881SCraig Topper 1189fdbff881SCraig Topper assert(!isLegalFCmpPredicate(Pred) && "Predicate already legal?"); 1190fdbff881SCraig Topper 1191fdbff881SCraig Topper CmpInst::Predicate InvPred = CmpInst::getSwappedPredicate(Pred); 1192fdbff881SCraig Topper if (isLegalFCmpPredicate(InvPred)) { 1193fdbff881SCraig Topper Pred = InvPred; 1194fdbff881SCraig Topper std::swap(LHS, RHS); 1195fdbff881SCraig Topper return true; 1196fdbff881SCraig Topper } 1197fdbff881SCraig Topper 1198fdbff881SCraig Topper InvPred = CmpInst::getInversePredicate(Pred); 1199fdbff881SCraig Topper NeedInvert = true; 1200fdbff881SCraig Topper if (isLegalFCmpPredicate(InvPred)) { 1201fdbff881SCraig Topper Pred = InvPred; 1202fdbff881SCraig Topper return true; 1203fdbff881SCraig Topper } 1204fdbff881SCraig Topper InvPred = CmpInst::getSwappedPredicate(InvPred); 1205fdbff881SCraig Topper if (isLegalFCmpPredicate(InvPred)) { 1206fdbff881SCraig Topper Pred = InvPred; 1207fdbff881SCraig Topper std::swap(LHS, RHS); 1208fdbff881SCraig Topper return true; 1209fdbff881SCraig Topper } 1210fdbff881SCraig Topper 1211fdbff881SCraig Topper return false; 1212fdbff881SCraig Topper } 1213fdbff881SCraig Topper 1214fdbff881SCraig Topper // Emit a sequence of instructions to compare LHS and RHS using Pred. Return 1215fdbff881SCraig Topper // the result in DstReg. 1216fdbff881SCraig Topper // FIXME: Maybe we should expand this earlier. 1217fdbff881SCraig Topper bool RISCVInstructionSelector::selectFPCompare(MachineInstr &MI, 1218a3cc4b61SMichael Maitland MachineIRBuilder &MIB) const { 1219fdbff881SCraig Topper auto &CmpMI = cast<GFCmp>(MI); 1220fdbff881SCraig Topper CmpInst::Predicate Pred = CmpMI.getCond(); 1221fdbff881SCraig Topper 1222fdbff881SCraig Topper Register DstReg = CmpMI.getReg(0); 1223fdbff881SCraig Topper Register LHS = CmpMI.getLHSReg(); 1224fdbff881SCraig Topper Register RHS = CmpMI.getRHSReg(); 1225fdbff881SCraig Topper 1226a3cc4b61SMichael Maitland unsigned Size = MRI->getType(LHS).getSizeInBits(); 1227acd6cb85SCraig Topper assert((Size == 16 || Size == 32 || Size == 64) && "Unexpected size"); 1228fdbff881SCraig Topper 1229fdbff881SCraig Topper Register TmpReg = DstReg; 1230fdbff881SCraig Topper 1231fdbff881SCraig Topper bool NeedInvert = false; 1232fdbff881SCraig Topper // First try swapping operands or inverting. 1233fdbff881SCraig Topper if (legalizeFCmpPredicate(LHS, RHS, Pred, NeedInvert)) { 1234fdbff881SCraig Topper if (NeedInvert) 1235a3cc4b61SMichael Maitland TmpReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 1236fdbff881SCraig Topper auto Cmp = MIB.buildInstr(getFCmpOpcode(Pred, Size), {TmpReg}, {LHS, RHS}); 1237fdbff881SCraig Topper if (!Cmp.constrainAllUses(TII, TRI, RBI)) 1238fdbff881SCraig Topper return false; 1239fdbff881SCraig Topper } else if (Pred == CmpInst::FCMP_ONE || Pred == CmpInst::FCMP_UEQ) { 1240fdbff881SCraig Topper // fcmp one LHS, RHS => (OR (FLT LHS, RHS), (FLT RHS, LHS)) 1241fdbff881SCraig Topper NeedInvert = Pred == CmpInst::FCMP_UEQ; 1242fdbff881SCraig Topper auto Cmp1 = MIB.buildInstr(getFCmpOpcode(CmpInst::FCMP_OLT, Size), 1243fdbff881SCraig Topper {&RISCV::GPRRegClass}, {LHS, RHS}); 1244fdbff881SCraig Topper if (!Cmp1.constrainAllUses(TII, TRI, RBI)) 1245fdbff881SCraig Topper return false; 1246fdbff881SCraig Topper auto Cmp2 = MIB.buildInstr(getFCmpOpcode(CmpInst::FCMP_OLT, Size), 1247fdbff881SCraig Topper {&RISCV::GPRRegClass}, {RHS, LHS}); 1248fdbff881SCraig Topper if (!Cmp2.constrainAllUses(TII, TRI, RBI)) 1249fdbff881SCraig Topper return false; 1250fdbff881SCraig Topper if (NeedInvert) 1251a3cc4b61SMichael Maitland TmpReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 1252fdbff881SCraig Topper auto Or = 1253fdbff881SCraig Topper MIB.buildInstr(RISCV::OR, {TmpReg}, {Cmp1.getReg(0), Cmp2.getReg(0)}); 1254fdbff881SCraig Topper if (!Or.constrainAllUses(TII, TRI, RBI)) 1255fdbff881SCraig Topper return false; 1256fdbff881SCraig Topper } else if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) { 1257fdbff881SCraig Topper // fcmp ord LHS, RHS => (AND (FEQ LHS, LHS), (FEQ RHS, RHS)) 1258fdbff881SCraig Topper // FIXME: If LHS and RHS are the same we can use a single FEQ. 1259fdbff881SCraig Topper NeedInvert = Pred == CmpInst::FCMP_UNO; 1260fdbff881SCraig Topper auto Cmp1 = MIB.buildInstr(getFCmpOpcode(CmpInst::FCMP_OEQ, Size), 1261fdbff881SCraig Topper {&RISCV::GPRRegClass}, {LHS, LHS}); 1262fdbff881SCraig Topper if (!Cmp1.constrainAllUses(TII, TRI, RBI)) 1263fdbff881SCraig Topper return false; 1264fdbff881SCraig Topper auto Cmp2 = MIB.buildInstr(getFCmpOpcode(CmpInst::FCMP_OEQ, Size), 1265fdbff881SCraig Topper {&RISCV::GPRRegClass}, {RHS, RHS}); 1266fdbff881SCraig Topper if (!Cmp2.constrainAllUses(TII, TRI, RBI)) 1267fdbff881SCraig Topper return false; 1268fdbff881SCraig Topper if (NeedInvert) 1269a3cc4b61SMichael Maitland TmpReg = MRI->createVirtualRegister(&RISCV::GPRRegClass); 1270fdbff881SCraig Topper auto And = 1271fdbff881SCraig Topper MIB.buildInstr(RISCV::AND, {TmpReg}, {Cmp1.getReg(0), Cmp2.getReg(0)}); 1272fdbff881SCraig Topper if (!And.constrainAllUses(TII, TRI, RBI)) 1273fdbff881SCraig Topper return false; 1274fdbff881SCraig Topper } else 1275fdbff881SCraig Topper llvm_unreachable("Unhandled predicate"); 1276fdbff881SCraig Topper 1277fdbff881SCraig Topper // Emit an XORI to invert the result if needed. 1278fdbff881SCraig Topper if (NeedInvert) { 1279fdbff881SCraig Topper auto Xor = MIB.buildInstr(RISCV::XORI, {DstReg}, {TmpReg}).addImm(1); 1280fdbff881SCraig Topper if (!Xor.constrainAllUses(TII, TRI, RBI)) 1281fdbff881SCraig Topper return false; 1282fdbff881SCraig Topper } 1283fdbff881SCraig Topper 1284fdbff881SCraig Topper MI.eraseFromParent(); 1285fdbff881SCraig Topper return true; 1286fdbff881SCraig Topper } 1287fdbff881SCraig Topper 1288ffcc5c77SCraig Topper void RISCVInstructionSelector::emitFence(AtomicOrdering FenceOrdering, 1289ffcc5c77SCraig Topper SyncScope::ID FenceSSID, 1290ffcc5c77SCraig Topper MachineIRBuilder &MIB) const { 1291ffcc5c77SCraig Topper if (STI.hasStdExtZtso()) { 1292ffcc5c77SCraig Topper // The only fence that needs an instruction is a sequentially-consistent 1293ffcc5c77SCraig Topper // cross-thread fence. 1294ffcc5c77SCraig Topper if (FenceOrdering == AtomicOrdering::SequentiallyConsistent && 1295ffcc5c77SCraig Topper FenceSSID == SyncScope::System) { 1296ffcc5c77SCraig Topper // fence rw, rw 1297ffcc5c77SCraig Topper MIB.buildInstr(RISCV::FENCE, {}, {}) 1298ffcc5c77SCraig Topper .addImm(RISCVFenceField::R | RISCVFenceField::W) 1299ffcc5c77SCraig Topper .addImm(RISCVFenceField::R | RISCVFenceField::W); 1300ffcc5c77SCraig Topper return; 1301ffcc5c77SCraig Topper } 1302ffcc5c77SCraig Topper 1303ffcc5c77SCraig Topper // MEMBARRIER is a compiler barrier; it codegens to a no-op. 1304ffcc5c77SCraig Topper MIB.buildInstr(TargetOpcode::MEMBARRIER, {}, {}); 1305ffcc5c77SCraig Topper return; 1306ffcc5c77SCraig Topper } 1307ffcc5c77SCraig Topper 1308ffcc5c77SCraig Topper // singlethread fences only synchronize with signal handlers on the same 1309ffcc5c77SCraig Topper // thread and thus only need to preserve instruction order, not actually 1310ffcc5c77SCraig Topper // enforce memory ordering. 1311ffcc5c77SCraig Topper if (FenceSSID == SyncScope::SingleThread) { 1312ffcc5c77SCraig Topper MIB.buildInstr(TargetOpcode::MEMBARRIER, {}, {}); 1313ffcc5c77SCraig Topper return; 1314ffcc5c77SCraig Topper } 1315ffcc5c77SCraig Topper 1316ffcc5c77SCraig Topper // Refer to Table A.6 in the version 2.3 draft of the RISC-V Instruction Set 1317ffcc5c77SCraig Topper // Manual: Volume I. 1318ffcc5c77SCraig Topper unsigned Pred, Succ; 1319ffcc5c77SCraig Topper switch (FenceOrdering) { 1320ffcc5c77SCraig Topper default: 1321ffcc5c77SCraig Topper llvm_unreachable("Unexpected ordering"); 1322ffcc5c77SCraig Topper case AtomicOrdering::AcquireRelease: 1323ffcc5c77SCraig Topper // fence acq_rel -> fence.tso 1324ffcc5c77SCraig Topper MIB.buildInstr(RISCV::FENCE_TSO, {}, {}); 1325ffcc5c77SCraig Topper return; 1326ffcc5c77SCraig Topper case AtomicOrdering::Acquire: 1327ffcc5c77SCraig Topper // fence acquire -> fence r, rw 1328ffcc5c77SCraig Topper Pred = RISCVFenceField::R; 1329ffcc5c77SCraig Topper Succ = RISCVFenceField::R | RISCVFenceField::W; 1330ffcc5c77SCraig Topper break; 1331ffcc5c77SCraig Topper case AtomicOrdering::Release: 1332ffcc5c77SCraig Topper // fence release -> fence rw, w 1333ffcc5c77SCraig Topper Pred = RISCVFenceField::R | RISCVFenceField::W; 1334ffcc5c77SCraig Topper Succ = RISCVFenceField::W; 1335ffcc5c77SCraig Topper break; 1336ffcc5c77SCraig Topper case AtomicOrdering::SequentiallyConsistent: 1337ffcc5c77SCraig Topper // fence seq_cst -> fence rw, rw 1338ffcc5c77SCraig Topper Pred = RISCVFenceField::R | RISCVFenceField::W; 1339ffcc5c77SCraig Topper Succ = RISCVFenceField::R | RISCVFenceField::W; 1340ffcc5c77SCraig Topper break; 1341ffcc5c77SCraig Topper } 1342ffcc5c77SCraig Topper MIB.buildInstr(RISCV::FENCE, {}, {}).addImm(Pred).addImm(Succ); 1343ffcc5c77SCraig Topper } 1344ffcc5c77SCraig Topper 13454be39288SPhilip Reames namespace llvm { 13464be39288SPhilip Reames InstructionSelector * 13474be39288SPhilip Reames createRISCVInstructionSelector(const RISCVTargetMachine &TM, 13489b95d08eSMichael Maitland const RISCVSubtarget &Subtarget, 13499b95d08eSMichael Maitland const RISCVRegisterBankInfo &RBI) { 13504be39288SPhilip Reames return new RISCVInstructionSelector(TM, Subtarget, RBI); 13514be39288SPhilip Reames } 13524be39288SPhilip Reames } // end namespace llvm 1353