7d3a6a457bce17bbd7f6980f47e62c3475200378
[oota-llvm.git] / lib / Target / R600 / SIShrinkInstructions.cpp
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"
13 #include "SIInstrInfo.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetMachine.h"
23
24 #define DEBUG_TYPE "si-shrink-instructions"
25
26 STATISTIC(NumInstructionsShrunk,
27           "Number of 64-bit instruction reduced to 32-bit.");
28 STATISTIC(NumLiteralConstantsFolded,
29           "Number of literal constants folded into 32-bit instructions.");
30
31 namespace llvm {
32   void initializeSIShrinkInstructionsPass(PassRegistry&);
33 }
34
35 using namespace llvm;
36
37 namespace {
38
39 class SIShrinkInstructions : public MachineFunctionPass {
40 public:
41   static char ID;
42
43 public:
44   SIShrinkInstructions() : MachineFunctionPass(ID) {
45   }
46
47   virtual bool runOnMachineFunction(MachineFunction &MF) override;
48
49   virtual const char *getPassName() const override {
50     return "SI Shrink Instructions";
51   }
52
53   virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
54     AU.setPreservesCFG();
55     MachineFunctionPass::getAnalysisUsage(AU);
56   }
57 };
58
59 } // End anonymous namespace.
60
61 INITIALIZE_PASS_BEGIN(SIShrinkInstructions, DEBUG_TYPE,
62                       "SI Lower il Copies", false, false)
63 INITIALIZE_PASS_END(SIShrinkInstructions, DEBUG_TYPE,
64                     "SI Lower il Copies", false, false)
65
66 char SIShrinkInstructions::ID = 0;
67
68 FunctionPass *llvm::createSIShrinkInstructionsPass() {
69   return new SIShrinkInstructions();
70 }
71
72 static bool isVGPR(const MachineOperand *MO, const SIRegisterInfo &TRI,
73                    const MachineRegisterInfo &MRI) {
74   if (!MO->isReg())
75     return false;
76
77   if (TargetRegisterInfo::isVirtualRegister(MO->getReg()))
78     return TRI.hasVGPRs(MRI.getRegClass(MO->getReg()));
79
80   return TRI.hasVGPRs(TRI.getPhysRegClass(MO->getReg()));
81 }
82
83 static bool canShrink(MachineInstr &MI, const SIInstrInfo *TII,
84                       const SIRegisterInfo &TRI,
85                       const MachineRegisterInfo &MRI) {
86
87   const MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
88   // Can't shrink instruction with three operands.
89   if (Src2)
90     return false;
91
92   const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
93   const MachineOperand *Src1Mod =
94       TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
95
96   if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0)))
97     return false;
98
99   // We don't need to check src0, all input types are legal, so just make
100   // sure src0 isn't using any modifiers.
101   const MachineOperand *Src0Mod =
102       TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
103   if (Src0Mod && Src0Mod->getImm() != 0)
104     return false;
105
106   // Check output modifiers
107   const MachineOperand *Omod = TII->getNamedOperand(MI, AMDGPU::OpName::omod);
108   if (Omod && Omod->getImm() != 0)
109     return false;
110
111   const MachineOperand *Clamp = TII->getNamedOperand(MI, AMDGPU::OpName::clamp);
112   return !Clamp || Clamp->getImm() == 0;
113 }
114
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.
133   if (Src0->isImm() && TII->isLiteralConstant(*Src0))
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()) {
155         const APFloat &APF = MovSrc.getFPImm()->getValueAPF();
156         if (&APF.getSemantics() == &APFloat::IEEEsingle) {
157           MRI.removeRegOperandFromUseList(Src0);
158           Src0->ChangeToImmediate(APF.bitcastToAPInt().getZExtValue());
159           ConstantFolded = true;
160         }
161       }
162       if (ConstantFolded) {
163         if (MRI.use_empty(Reg))
164           Def->eraseFromParent();
165         ++NumLiteralConstantsFolded;
166         return;
167       }
168     }
169   }
170
171   // We have failed to fold src0, so commute the instruction and try again.
172   if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(&MI))
173     foldImmediates(MI, TII, MRI, false);
174
175 }
176
177 bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
178   MachineRegisterInfo &MRI = MF.getRegInfo();
179   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
180       MF.getTarget().getInstrInfo());
181   const SIRegisterInfo &TRI = TII->getRegisterInfo();
182   std::vector<unsigned> I1Defs;
183
184   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
185                                                   BI != BE; ++BI) {
186
187     MachineBasicBlock &MBB = *BI;
188     MachineBasicBlock::iterator I, Next;
189     for (I = MBB.begin(); I != MBB.end(); I = Next) {
190       Next = std::next(I);
191       MachineInstr &MI = *I;
192
193       if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
194         continue;
195
196       if (!canShrink(MI, TII, TRI, MRI)) {
197         // Try commtuing the instruction and see if that enables us to shrink
198         // it.
199         if (!MI.isCommutable() || !TII->commuteInstruction(&MI) ||
200             !canShrink(MI, TII, TRI, MRI))
201           continue;
202       }
203
204       int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
205
206       // Op32 could be -1 here if we started with an instruction that had a
207       // a 32-bit encoding and then commuted it to an instruction that did not.
208       if (Op32 == -1)
209         continue;
210
211       if (TII->isVOPC(Op32)) {
212         unsigned DstReg = MI.getOperand(0).getReg();
213         if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
214           // VOPC instructions can only write to the VCC register.  We can't
215           // force them to use VCC here, because the register allocator
216           // has trouble with sequences like this, which cause the allocator
217           // to run out of registes if vreg0 and vreg1 belong to the VCCReg
218           // register class:
219           // vreg0 = VOPC;
220           // vreg1 = VOPC;
221           // S_AND_B64 vreg0, vreg1
222           //
223           // So, instead of forcing the instruction to write to VCC, we provide a
224           // hint to the register allocator to use VCC and then we
225           // we will run this pass again after RA and shrink it if it outpus to
226           // VCC.
227           MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
228           continue;
229         }
230         if (DstReg != AMDGPU::VCC)
231           continue;
232       }
233
234       // We can shrink this instruction
235       DEBUG(dbgs() << "Shrinking "; MI.dump(); dbgs() << '\n';);
236
237       MachineInstrBuilder Inst32 =
238           BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
239
240       // dst
241       Inst32.addOperand(MI.getOperand(0));
242
243       Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
244
245       const MachineOperand *Src1 =
246           TII->getNamedOperand(MI, AMDGPU::OpName::src1);
247       if (Src1)
248         Inst32.addOperand(*Src1);
249
250       ++NumInstructionsShrunk;
251       MI.eraseFromParent();
252
253       foldImmediates(*Inst32, TII, MRI);
254       DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
255
256
257     }
258   }
259   return false;
260 }