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