10b57cec5SDimitry Andric //===- R600ExpandSpecialInstrs.cpp - Expand special instructions ----------===// 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 100b57cec5SDimitry Andric /// Vector, Reduction, and Cube instructions need to fill the entire instruction 110b57cec5SDimitry Andric /// group to work correctly. This pass expands these individual instructions 120b57cec5SDimitry Andric /// into several instructions that will completely fill the instruction group. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "AMDGPU.h" 170b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 18*e8d8bef9SDimitry Andric #include "R600Defines.h" 19*e8d8bef9SDimitry Andric #include "R600Subtarget.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric #define DEBUG_TYPE "r600-expand-special-instrs" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace { 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric class R600ExpandSpecialInstrsPass : public MachineFunctionPass { 280b57cec5SDimitry Andric private: 290b57cec5SDimitry Andric const R600InstrInfo *TII = nullptr; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric void SetFlagInNewMI(MachineInstr *NewMI, const MachineInstr *OldMI, 320b57cec5SDimitry Andric unsigned Op); 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric public: 350b57cec5SDimitry Andric static char ID; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric R600ExpandSpecialInstrsPass() : MachineFunctionPass(ID) {} 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric StringRef getPassName() const override { 420b57cec5SDimitry Andric return "R600 Expand special instructions pass"; 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric }; 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric } // end anonymous namespace 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(R600ExpandSpecialInstrsPass, DEBUG_TYPE, 490b57cec5SDimitry Andric "R600 Expand Special Instrs", false, false) 500b57cec5SDimitry Andric INITIALIZE_PASS_END(R600ExpandSpecialInstrsPass, DEBUG_TYPE, 510b57cec5SDimitry Andric "R600ExpandSpecialInstrs", false, false) 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric char R600ExpandSpecialInstrsPass::ID = 0; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric char &llvm::R600ExpandSpecialInstrsPassID = R600ExpandSpecialInstrsPass::ID; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric FunctionPass *llvm::createR600ExpandSpecialInstrsPass() { 580b57cec5SDimitry Andric return new R600ExpandSpecialInstrsPass(); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric void R600ExpandSpecialInstrsPass::SetFlagInNewMI(MachineInstr *NewMI, 620b57cec5SDimitry Andric const MachineInstr *OldMI, unsigned Op) { 630b57cec5SDimitry Andric int OpIdx = TII->getOperandIdx(*OldMI, Op); 640b57cec5SDimitry Andric if (OpIdx > -1) { 650b57cec5SDimitry Andric uint64_t Val = OldMI->getOperand(OpIdx).getImm(); 660b57cec5SDimitry Andric TII->setImmOperand(*NewMI, Op, Val); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric bool R600ExpandSpecialInstrsPass::runOnMachineFunction(MachineFunction &MF) { 710b57cec5SDimitry Andric const R600Subtarget &ST = MF.getSubtarget<R600Subtarget>(); 720b57cec5SDimitry Andric TII = ST.getInstrInfo(); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric const R600RegisterInfo &TRI = TII->getRegisterInfo(); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); 770b57cec5SDimitry Andric BB != BB_E; ++BB) { 780b57cec5SDimitry Andric MachineBasicBlock &MBB = *BB; 790b57cec5SDimitry Andric MachineBasicBlock::iterator I = MBB.begin(); 800b57cec5SDimitry Andric while (I != MBB.end()) { 810b57cec5SDimitry Andric MachineInstr &MI = *I; 820b57cec5SDimitry Andric I = std::next(I); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // Expand LDS_*_RET instructions 850b57cec5SDimitry Andric if (TII->isLDSRetInstr(MI.getOpcode())) { 860b57cec5SDimitry Andric int DstIdx = TII->getOperandIdx(MI.getOpcode(), R600::OpName::dst); 870b57cec5SDimitry Andric assert(DstIdx != -1); 880b57cec5SDimitry Andric MachineOperand &DstOp = MI.getOperand(DstIdx); 890b57cec5SDimitry Andric MachineInstr *Mov = TII->buildMovInstr(&MBB, I, 900b57cec5SDimitry Andric DstOp.getReg(), R600::OQAP); 910b57cec5SDimitry Andric DstOp.setReg(R600::OQAP); 920b57cec5SDimitry Andric int LDSPredSelIdx = TII->getOperandIdx(MI.getOpcode(), 930b57cec5SDimitry Andric R600::OpName::pred_sel); 940b57cec5SDimitry Andric int MovPredSelIdx = TII->getOperandIdx(Mov->getOpcode(), 950b57cec5SDimitry Andric R600::OpName::pred_sel); 960b57cec5SDimitry Andric // Copy the pred_sel bit 970b57cec5SDimitry Andric Mov->getOperand(MovPredSelIdx).setReg( 980b57cec5SDimitry Andric MI.getOperand(LDSPredSelIdx).getReg()); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric switch (MI.getOpcode()) { 1020b57cec5SDimitry Andric default: break; 1030b57cec5SDimitry Andric // Expand PRED_X to one of the PRED_SET instructions. 1040b57cec5SDimitry Andric case R600::PRED_X: { 1050b57cec5SDimitry Andric uint64_t Flags = MI.getOperand(3).getImm(); 1060b57cec5SDimitry Andric // The native opcode used by PRED_X is stored as an immediate in the 1070b57cec5SDimitry Andric // third operand. 1080b57cec5SDimitry Andric MachineInstr *PredSet = TII->buildDefaultInstruction(MBB, I, 1090b57cec5SDimitry Andric MI.getOperand(2).getImm(), // opcode 1100b57cec5SDimitry Andric MI.getOperand(0).getReg(), // dst 1110b57cec5SDimitry Andric MI.getOperand(1).getReg(), // src0 1120b57cec5SDimitry Andric R600::ZERO); // src1 1130b57cec5SDimitry Andric TII->addFlag(*PredSet, 0, MO_FLAG_MASK); 1140b57cec5SDimitry Andric if (Flags & MO_FLAG_PUSH) { 1150b57cec5SDimitry Andric TII->setImmOperand(*PredSet, R600::OpName::update_exec_mask, 1); 1160b57cec5SDimitry Andric } else { 1170b57cec5SDimitry Andric TII->setImmOperand(*PredSet, R600::OpName::update_pred, 1); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric MI.eraseFromParent(); 1200b57cec5SDimitry Andric continue; 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric case R600::DOT_4: { 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric const R600RegisterInfo &TRI = TII->getRegisterInfo(); 1250b57cec5SDimitry Andric 1268bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 1270b57cec5SDimitry Andric unsigned DstBase = TRI.getEncodingValue(DstReg) & HW_REG_MASK; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric for (unsigned Chan = 0; Chan < 4; ++Chan) { 1300b57cec5SDimitry Andric bool Mask = (Chan != TRI.getHWRegChan(DstReg)); 1310b57cec5SDimitry Andric unsigned SubDstReg = 1320b57cec5SDimitry Andric R600::R600_TReg32RegClass.getRegister((DstBase * 4) + Chan); 1330b57cec5SDimitry Andric MachineInstr *BMI = 1340b57cec5SDimitry Andric TII->buildSlotOfVectorInstruction(MBB, &MI, Chan, SubDstReg); 1350b57cec5SDimitry Andric if (Chan > 0) { 1360b57cec5SDimitry Andric BMI->bundleWithPred(); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric if (Mask) { 1390b57cec5SDimitry Andric TII->addFlag(*BMI, 0, MO_FLAG_MASK); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric if (Chan != 3) 1420b57cec5SDimitry Andric TII->addFlag(*BMI, 0, MO_FLAG_NOT_LAST); 1430b57cec5SDimitry Andric unsigned Opcode = BMI->getOpcode(); 1440b57cec5SDimitry Andric // While not strictly necessary from hw point of view, we force 1450b57cec5SDimitry Andric // all src operands of a dot4 inst to belong to the same slot. 1468bcb0991SDimitry Andric Register Src0 = 1478bcb0991SDimitry Andric BMI->getOperand(TII->getOperandIdx(Opcode, R600::OpName::src0)) 1480b57cec5SDimitry Andric .getReg(); 1498bcb0991SDimitry Andric Register Src1 = 1508bcb0991SDimitry Andric BMI->getOperand(TII->getOperandIdx(Opcode, R600::OpName::src1)) 1510b57cec5SDimitry Andric .getReg(); 1520b57cec5SDimitry Andric (void) Src0; 1530b57cec5SDimitry Andric (void) Src1; 1540b57cec5SDimitry Andric if ((TRI.getEncodingValue(Src0) & 0xff) < 127 && 1550b57cec5SDimitry Andric (TRI.getEncodingValue(Src1) & 0xff) < 127) 1560b57cec5SDimitry Andric assert(TRI.getHWRegChan(Src0) == TRI.getHWRegChan(Src1)); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric MI.eraseFromParent(); 1590b57cec5SDimitry Andric continue; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric bool IsReduction = TII->isReductionOp(MI.getOpcode()); 1640b57cec5SDimitry Andric bool IsVector = TII->isVector(MI); 1650b57cec5SDimitry Andric bool IsCube = TII->isCubeOp(MI.getOpcode()); 1660b57cec5SDimitry Andric if (!IsReduction && !IsVector && !IsCube) { 1670b57cec5SDimitry Andric continue; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // Expand the instruction 1710b57cec5SDimitry Andric // 1720b57cec5SDimitry Andric // Reduction instructions: 1730b57cec5SDimitry Andric // T0_X = DP4 T1_XYZW, T2_XYZW 1740b57cec5SDimitry Andric // becomes: 1750b57cec5SDimitry Andric // TO_X = DP4 T1_X, T2_X 1760b57cec5SDimitry Andric // TO_Y (write masked) = DP4 T1_Y, T2_Y 1770b57cec5SDimitry Andric // TO_Z (write masked) = DP4 T1_Z, T2_Z 1780b57cec5SDimitry Andric // TO_W (write masked) = DP4 T1_W, T2_W 1790b57cec5SDimitry Andric // 1800b57cec5SDimitry Andric // Vector instructions: 1810b57cec5SDimitry Andric // T0_X = MULLO_INT T1_X, T2_X 1820b57cec5SDimitry Andric // becomes: 1830b57cec5SDimitry Andric // T0_X = MULLO_INT T1_X, T2_X 1840b57cec5SDimitry Andric // T0_Y (write masked) = MULLO_INT T1_X, T2_X 1850b57cec5SDimitry Andric // T0_Z (write masked) = MULLO_INT T1_X, T2_X 1860b57cec5SDimitry Andric // T0_W (write masked) = MULLO_INT T1_X, T2_X 1870b57cec5SDimitry Andric // 1880b57cec5SDimitry Andric // Cube instructions: 1890b57cec5SDimitry Andric // T0_XYZW = CUBE T1_XYZW 1900b57cec5SDimitry Andric // becomes: 1910b57cec5SDimitry Andric // TO_X = CUBE T1_Z, T1_Y 1920b57cec5SDimitry Andric // T0_Y = CUBE T1_Z, T1_X 1930b57cec5SDimitry Andric // T0_Z = CUBE T1_X, T1_Z 1940b57cec5SDimitry Andric // T0_W = CUBE T1_Y, T1_Z 1950b57cec5SDimitry Andric for (unsigned Chan = 0; Chan < 4; Chan++) { 1968bcb0991SDimitry Andric Register DstReg = 1978bcb0991SDimitry Andric MI.getOperand(TII->getOperandIdx(MI, R600::OpName::dst)).getReg(); 1988bcb0991SDimitry Andric Register Src0 = 1998bcb0991SDimitry Andric MI.getOperand(TII->getOperandIdx(MI, R600::OpName::src0)).getReg(); 2000b57cec5SDimitry Andric unsigned Src1 = 0; 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // Determine the correct source registers 2030b57cec5SDimitry Andric if (!IsCube) { 2040b57cec5SDimitry Andric int Src1Idx = TII->getOperandIdx(MI, R600::OpName::src1); 2050b57cec5SDimitry Andric if (Src1Idx != -1) { 2060b57cec5SDimitry Andric Src1 = MI.getOperand(Src1Idx).getReg(); 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric if (IsReduction) { 2105ffd83dbSDimitry Andric unsigned SubRegIndex = R600RegisterInfo::getSubRegFromChannel(Chan); 2110b57cec5SDimitry Andric Src0 = TRI.getSubReg(Src0, SubRegIndex); 2120b57cec5SDimitry Andric Src1 = TRI.getSubReg(Src1, SubRegIndex); 2130b57cec5SDimitry Andric } else if (IsCube) { 2140b57cec5SDimitry Andric static const int CubeSrcSwz[] = {2, 2, 0, 1}; 2155ffd83dbSDimitry Andric unsigned SubRegIndex0 = R600RegisterInfo::getSubRegFromChannel(CubeSrcSwz[Chan]); 2165ffd83dbSDimitry Andric unsigned SubRegIndex1 = R600RegisterInfo::getSubRegFromChannel(CubeSrcSwz[3 - Chan]); 2170b57cec5SDimitry Andric Src1 = TRI.getSubReg(Src0, SubRegIndex1); 2180b57cec5SDimitry Andric Src0 = TRI.getSubReg(Src0, SubRegIndex0); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Determine the correct destination registers; 2220b57cec5SDimitry Andric bool Mask = false; 2230b57cec5SDimitry Andric bool NotLast = true; 2240b57cec5SDimitry Andric if (IsCube) { 2255ffd83dbSDimitry Andric unsigned SubRegIndex = R600RegisterInfo::getSubRegFromChannel(Chan); 2260b57cec5SDimitry Andric DstReg = TRI.getSubReg(DstReg, SubRegIndex); 2270b57cec5SDimitry Andric } else { 2280b57cec5SDimitry Andric // Mask the write if the original instruction does not write to 2290b57cec5SDimitry Andric // the current Channel. 2300b57cec5SDimitry Andric Mask = (Chan != TRI.getHWRegChan(DstReg)); 2310b57cec5SDimitry Andric unsigned DstBase = TRI.getEncodingValue(DstReg) & HW_REG_MASK; 2320b57cec5SDimitry Andric DstReg = R600::R600_TReg32RegClass.getRegister((DstBase * 4) + Chan); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric // Set the IsLast bit 2360b57cec5SDimitry Andric NotLast = (Chan != 3 ); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric // Add the new instruction 2390b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 2400b57cec5SDimitry Andric switch (Opcode) { 2410b57cec5SDimitry Andric case R600::CUBE_r600_pseudo: 2420b57cec5SDimitry Andric Opcode = R600::CUBE_r600_real; 2430b57cec5SDimitry Andric break; 2440b57cec5SDimitry Andric case R600::CUBE_eg_pseudo: 2450b57cec5SDimitry Andric Opcode = R600::CUBE_eg_real; 2460b57cec5SDimitry Andric break; 2470b57cec5SDimitry Andric default: 2480b57cec5SDimitry Andric break; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric MachineInstr *NewMI = 2520b57cec5SDimitry Andric TII->buildDefaultInstruction(MBB, I, Opcode, DstReg, Src0, Src1); 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric if (Chan != 0) 2550b57cec5SDimitry Andric NewMI->bundleWithPred(); 2560b57cec5SDimitry Andric if (Mask) { 2570b57cec5SDimitry Andric TII->addFlag(*NewMI, 0, MO_FLAG_MASK); 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric if (NotLast) { 2600b57cec5SDimitry Andric TII->addFlag(*NewMI, 0, MO_FLAG_NOT_LAST); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::clamp); 2630b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::literal); 2640b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::src0_abs); 2650b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::src1_abs); 2660b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::src0_neg); 2670b57cec5SDimitry Andric SetFlagInNewMI(NewMI, &MI, R600::OpName::src1_neg); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric MI.eraseFromParent(); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric return false; 2730b57cec5SDimitry Andric } 274