Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SILowerLiteralConstants.cpp - Lower intrs using literal constants--===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief This pass performs the following transformation on instructions with |
| 12 | /// literal constants: |
| 13 | /// |
| 14 | /// %VGPR0 = V_MOV_IMM_I32 1 |
| 15 | /// |
| 16 | /// becomes: |
| 17 | /// |
| 18 | /// BUNDLE |
| 19 | /// * %VGPR = V_MOV_B32_32 SI_LITERAL_CONSTANT |
| 20 | /// * SI_LOAD_LITERAL 1 |
| 21 | /// |
| 22 | /// The resulting sequence matches exactly how the hardware handles immediate |
| 23 | /// operands, so this transformation greatly simplifies the code generator. |
| 24 | /// |
| 25 | /// Only the *_MOV_IMM_* support immediate operands at the moment, but when |
| 26 | /// support for immediate operands is added to other instructions, they |
| 27 | /// will be lowered here as well. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #include "AMDGPU.h" |
| 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 34 | #include "llvm/CodeGen/MachineInstrBundle.h" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | class SILowerLiteralConstantsPass : public MachineFunctionPass { |
| 41 | |
| 42 | private: |
| 43 | static char ID; |
| 44 | const TargetInstrInfo *TII; |
| 45 | |
| 46 | public: |
| 47 | SILowerLiteralConstantsPass(TargetMachine &tm) : |
| 48 | MachineFunctionPass(ID), TII(tm.getInstrInfo()) { } |
| 49 | |
| 50 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 51 | |
| 52 | const char *getPassName() const { |
| 53 | return "SI Lower literal constants pass"; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | } // End anonymous namespace |
| 58 | |
| 59 | char SILowerLiteralConstantsPass::ID = 0; |
| 60 | |
| 61 | FunctionPass *llvm::createSILowerLiteralConstantsPass(TargetMachine &tm) { |
| 62 | return new SILowerLiteralConstantsPass(tm); |
| 63 | } |
| 64 | |
| 65 | bool SILowerLiteralConstantsPass::runOnMachineFunction(MachineFunction &MF) { |
| 66 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 67 | BB != BB_E; ++BB) { |
| 68 | MachineBasicBlock &MBB = *BB; |
| 69 | for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I); |
| 70 | I != MBB.end(); I = Next) { |
| 71 | Next = llvm::next(I); |
| 72 | MachineInstr &MI = *I; |
| 73 | switch (MI.getOpcode()) { |
| 74 | default: break; |
| 75 | case AMDGPU::S_MOV_IMM_I32: |
| 76 | case AMDGPU::S_MOV_IMM_I64: |
| 77 | case AMDGPU::V_MOV_IMM_F32: |
| 78 | case AMDGPU::V_MOV_IMM_I32: { |
| 79 | unsigned MovOpcode; |
| 80 | unsigned LoadLiteralOpcode; |
| 81 | MachineOperand LiteralOp = MI.getOperand(1); |
| 82 | if (AMDGPU::VReg_32RegClass.contains(MI.getOperand(0).getReg())) { |
| 83 | MovOpcode = AMDGPU::V_MOV_B32_e32; |
| 84 | } else { |
| 85 | MovOpcode = AMDGPU::S_MOV_B32; |
| 86 | } |
| 87 | if (LiteralOp.isImm()) { |
| 88 | LoadLiteralOpcode = AMDGPU::SI_LOAD_LITERAL_I32; |
| 89 | } else { |
| 90 | LoadLiteralOpcode = AMDGPU::SI_LOAD_LITERAL_F32; |
| 91 | } |
Jakob Stoklund Olesen | 436eea9 | 2012-12-13 00:59:38 +0000 | [diff] [blame] | 92 | MIBundleBuilder Bundle(MBB, I); |
| 93 | Bundle |
| 94 | .append(BuildMI(MF, MBB.findDebugLoc(I), TII->get(MovOpcode), |
| 95 | MI.getOperand(0).getReg()) |
| 96 | .addReg(AMDGPU::SI_LITERAL_CONSTANT)) |
| 97 | .append(BuildMI(MF, MBB.findDebugLoc(I), |
| 98 | TII->get(LoadLiteralOpcode)) |
| 99 | .addOperand(MI.getOperand(1))); |
| 100 | llvm::finalizeBundle(MBB, Bundle.begin()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 101 | MI.eraseFromParent(); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |