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