Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 1 | //===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | /// The pass tries to use the 32-bit encoding for instructions when possible. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | |
| 12 | #include "AMDGPU.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 13 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 14 | #include "SIInstrInfo.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | |
| 25 | #define DEBUG_TYPE "si-shrink-instructions" |
| 26 | |
| 27 | STATISTIC(NumInstructionsShrunk, |
| 28 | "Number of 64-bit instruction reduced to 32-bit."); |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 29 | STATISTIC(NumLiteralConstantsFolded, |
| 30 | "Number of literal constants folded into 32-bit instructions."); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 31 | |
| 32 | namespace llvm { |
| 33 | void initializeSIShrinkInstructionsPass(PassRegistry&); |
| 34 | } |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | class SIShrinkInstructions : public MachineFunctionPass { |
| 41 | public: |
| 42 | static char ID; |
| 43 | |
| 44 | public: |
| 45 | SIShrinkInstructions() : MachineFunctionPass(ID) { |
| 46 | } |
| 47 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 48 | bool runOnMachineFunction(MachineFunction &MF) override; |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 49 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 50 | const char *getPassName() const override { |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 51 | return "SI Shrink Instructions"; |
| 52 | } |
| 53 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 54 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 55 | AU.setPreservesCFG(); |
| 56 | MachineFunctionPass::getAnalysisUsage(AU); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | } // End anonymous namespace. |
| 61 | |
| 62 | INITIALIZE_PASS_BEGIN(SIShrinkInstructions, DEBUG_TYPE, |
| 63 | "SI Lower il Copies", false, false) |
| 64 | INITIALIZE_PASS_END(SIShrinkInstructions, DEBUG_TYPE, |
| 65 | "SI Lower il Copies", false, false) |
| 66 | |
| 67 | char SIShrinkInstructions::ID = 0; |
| 68 | |
| 69 | FunctionPass *llvm::createSIShrinkInstructionsPass() { |
| 70 | return new SIShrinkInstructions(); |
| 71 | } |
| 72 | |
| 73 | static bool isVGPR(const MachineOperand *MO, const SIRegisterInfo &TRI, |
| 74 | const MachineRegisterInfo &MRI) { |
| 75 | if (!MO->isReg()) |
| 76 | return false; |
| 77 | |
| 78 | if (TargetRegisterInfo::isVirtualRegister(MO->getReg())) |
| 79 | return TRI.hasVGPRs(MRI.getRegClass(MO->getReg())); |
| 80 | |
| 81 | return TRI.hasVGPRs(TRI.getPhysRegClass(MO->getReg())); |
| 82 | } |
| 83 | |
| 84 | static bool canShrink(MachineInstr &MI, const SIInstrInfo *TII, |
| 85 | const SIRegisterInfo &TRI, |
| 86 | const MachineRegisterInfo &MRI) { |
| 87 | |
| 88 | const MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2); |
| 89 | // Can't shrink instruction with three operands. |
| 90 | if (Src2) |
| 91 | return false; |
| 92 | |
| 93 | const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1); |
| 94 | const MachineOperand *Src1Mod = |
| 95 | TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers); |
| 96 | |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 97 | if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0))) |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 98 | return false; |
| 99 | |
Matt Arsenault | 8943d24 | 2014-10-17 18:00:45 +0000 | [diff] [blame] | 100 | // We don't need to check src0, all input types are legal, so just make sure |
| 101 | // src0 isn't using any modifiers. |
| 102 | if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers)) |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 103 | return false; |
| 104 | |
| 105 | // Check output modifiers |
Matt Arsenault | 8943d24 | 2014-10-17 18:00:45 +0000 | [diff] [blame] | 106 | if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod)) |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 107 | return false; |
| 108 | |
Matt Arsenault | 8943d24 | 2014-10-17 18:00:45 +0000 | [diff] [blame] | 109 | if (TII->hasModifiersSet(MI, AMDGPU::OpName::clamp)) |
| 110 | return false; |
| 111 | |
| 112 | return true; |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 115 | /// \brief This function checks \p MI for operands defined by a move immediate |
| 116 | /// instruction and then folds the literal constant into the instruction if it |
| 117 | /// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instruction |
| 118 | /// and will only fold literal constants if we are still in SSA. |
| 119 | static void foldImmediates(MachineInstr &MI, const SIInstrInfo *TII, |
| 120 | MachineRegisterInfo &MRI, bool TryToCommute = true) { |
| 121 | |
| 122 | if (!MRI.isSSA()) |
| 123 | return; |
| 124 | |
| 125 | assert(TII->isVOP1(MI.getOpcode()) || TII->isVOP2(MI.getOpcode()) || |
| 126 | TII->isVOPC(MI.getOpcode())); |
| 127 | |
| 128 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 129 | MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0); |
| 130 | |
| 131 | // Only one literal constant is allowed per instruction, so if src0 is a |
| 132 | // literal constant then we can't do any folding. |
Matt Arsenault | e184482 | 2014-10-17 18:00:50 +0000 | [diff] [blame] | 133 | if ((Src0->isImm() || Src0->isFPImm()) && TII->isLiteralConstant(*Src0)) |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
| 136 | |
| 137 | // Literal constants and SGPRs can only be used in Src0, so if Src0 is an |
| 138 | // SGPR, we cannot commute the instruction, so we can't fold any literal |
| 139 | // constants. |
| 140 | if (Src0->isReg() && !isVGPR(Src0, TRI, MRI)) |
| 141 | return; |
| 142 | |
| 143 | // Try to fold Src0 |
| 144 | if (Src0->isReg()) { |
| 145 | unsigned Reg = Src0->getReg(); |
| 146 | MachineInstr *Def = MRI.getUniqueVRegDef(Reg); |
| 147 | if (Def && Def->isMoveImmediate()) { |
| 148 | MachineOperand &MovSrc = Def->getOperand(1); |
| 149 | bool ConstantFolded = false; |
| 150 | |
| 151 | if (MovSrc.isImm() && isUInt<32>(MovSrc.getImm())) { |
| 152 | Src0->ChangeToImmediate(MovSrc.getImm()); |
| 153 | ConstantFolded = true; |
| 154 | } else if (MovSrc.isFPImm()) { |
Matt Arsenault | ffc5d5b | 2014-10-17 18:00:41 +0000 | [diff] [blame] | 155 | const ConstantFP *CFP = MovSrc.getFPImm(); |
| 156 | if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) { |
| 157 | Src0->ChangeToFPImmediate(CFP); |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 158 | ConstantFolded = true; |
| 159 | } |
| 160 | } |
| 161 | if (ConstantFolded) { |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 162 | if (MRI.use_empty(Reg)) |
| 163 | Def->eraseFromParent(); |
| 164 | ++NumLiteralConstantsFolded; |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // We have failed to fold src0, so commute the instruction and try again. |
| 171 | if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(&MI)) |
| 172 | foldImmediates(MI, TII, MRI, false); |
| 173 | |
| 174 | } |
| 175 | |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 176 | bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) { |
| 177 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 178 | const SIInstrInfo *TII = |
| 179 | static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 180 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 181 | std::vector<unsigned> I1Defs; |
| 182 | |
| 183 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 184 | BI != BE; ++BI) { |
| 185 | |
| 186 | MachineBasicBlock &MBB = *BI; |
| 187 | MachineBasicBlock::iterator I, Next; |
| 188 | for (I = MBB.begin(); I != MBB.end(); I = Next) { |
| 189 | Next = std::next(I); |
| 190 | MachineInstr &MI = *I; |
| 191 | |
Tom Stellard | 86d12eb | 2014-08-01 00:32:28 +0000 | [diff] [blame] | 192 | if (!TII->hasVALU32BitEncoding(MI.getOpcode())) |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 193 | continue; |
| 194 | |
| 195 | if (!canShrink(MI, TII, TRI, MRI)) { |
Matt Arsenault | 6652403 | 2014-09-16 18:00:23 +0000 | [diff] [blame] | 196 | // Try commuting the instruction and see if that enables us to shrink |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 197 | // it. |
| 198 | if (!MI.isCommutable() || !TII->commuteInstruction(&MI) || |
| 199 | !canShrink(MI, TII, TRI, MRI)) |
| 200 | continue; |
| 201 | } |
| 202 | |
Tom Stellard | 86d12eb | 2014-08-01 00:32:28 +0000 | [diff] [blame] | 203 | int Op32 = AMDGPU::getVOPe32(MI.getOpcode()); |
| 204 | |
| 205 | // Op32 could be -1 here if we started with an instruction that had a |
| 206 | // a 32-bit encoding and then commuted it to an instruction that did not. |
| 207 | if (Op32 == -1) |
| 208 | continue; |
| 209 | |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 210 | if (TII->isVOPC(Op32)) { |
| 211 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 212 | if (TargetRegisterInfo::isVirtualRegister(DstReg)) { |
| 213 | // VOPC instructions can only write to the VCC register. We can't |
Matt Arsenault | 5d26d04 | 2014-09-13 19:58:27 +0000 | [diff] [blame] | 214 | // force them to use VCC here, because the register allocator has |
| 215 | // trouble with sequences like this, which cause the allocator to run |
| 216 | // out of registers if vreg0 and vreg1 belong to the VCCReg register |
| 217 | // class: |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 218 | // vreg0 = VOPC; |
| 219 | // vreg1 = VOPC; |
| 220 | // S_AND_B64 vreg0, vreg1 |
| 221 | // |
Matt Arsenault | a9627ae | 2014-09-21 17:27:32 +0000 | [diff] [blame] | 222 | // So, instead of forcing the instruction to write to VCC, we provide |
| 223 | // a hint to the register allocator to use VCC and then we we will run |
| 224 | // this pass again after RA and shrink it if it outputs to VCC. |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 225 | MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC); |
| 226 | continue; |
| 227 | } |
| 228 | if (DstReg != AMDGPU::VCC) |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | // We can shrink this instruction |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 233 | DEBUG(dbgs() << "Shrinking "; MI.dump(); dbgs() << '\n';); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 234 | |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 235 | MachineInstrBuilder Inst32 = |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 236 | BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32)); |
| 237 | |
| 238 | // dst |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 239 | Inst32.addOperand(MI.getOperand(0)); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 240 | |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 241 | Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0)); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 242 | |
| 243 | const MachineOperand *Src1 = |
| 244 | TII->getNamedOperand(MI, AMDGPU::OpName::src1); |
| 245 | if (Src1) |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 246 | Inst32.addOperand(*Src1); |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 247 | |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 248 | ++NumInstructionsShrunk; |
| 249 | MI.eraseFromParent(); |
Tom Stellard | 6407e1e | 2014-08-01 00:32:33 +0000 | [diff] [blame] | 250 | |
| 251 | foldImmediates(*Inst32, TII, MRI); |
| 252 | DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n'); |
| 253 | |
| 254 | |
Tom Stellard | 1aaad69 | 2014-07-21 16:55:33 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | return false; |
| 258 | } |