blob: fef39781174e743596a38b53adf7529a4435225b [file] [log] [blame]
Tom Stellard1aaad692014-07-21 16:55:33 +00001//===-- 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 Christopherd9134482014-08-04 21:25:23 +000013#include "AMDGPUSubtarget.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000014#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 Stellard6407e1e2014-08-01 00:32:33 +000019#include "llvm/IR/Constants.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000020#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
27STATISTIC(NumInstructionsShrunk,
28 "Number of 64-bit instruction reduced to 32-bit.");
Tom Stellard6407e1e2014-08-01 00:32:33 +000029STATISTIC(NumLiteralConstantsFolded,
30 "Number of literal constants folded into 32-bit instructions.");
Tom Stellard1aaad692014-07-21 16:55:33 +000031
32namespace llvm {
33 void initializeSIShrinkInstructionsPass(PassRegistry&);
34}
35
36using namespace llvm;
37
38namespace {
39
40class SIShrinkInstructions : public MachineFunctionPass {
41public:
42 static char ID;
43
44public:
45 SIShrinkInstructions() : MachineFunctionPass(ID) {
46 }
47
Craig Topperfd38cbe2014-08-30 16:48:34 +000048 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard1aaad692014-07-21 16:55:33 +000049
Craig Topperfd38cbe2014-08-30 16:48:34 +000050 const char *getPassName() const override {
Tom Stellard1aaad692014-07-21 16:55:33 +000051 return "SI Shrink Instructions";
52 }
53
Craig Topperfd38cbe2014-08-30 16:48:34 +000054 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard1aaad692014-07-21 16:55:33 +000055 AU.setPreservesCFG();
56 MachineFunctionPass::getAnalysisUsage(AU);
57 }
58};
59
60} // End anonymous namespace.
61
62INITIALIZE_PASS_BEGIN(SIShrinkInstructions, DEBUG_TYPE,
63 "SI Lower il Copies", false, false)
64INITIALIZE_PASS_END(SIShrinkInstructions, DEBUG_TYPE,
65 "SI Lower il Copies", false, false)
66
67char SIShrinkInstructions::ID = 0;
68
69FunctionPass *llvm::createSIShrinkInstructionsPass() {
70 return new SIShrinkInstructions();
71}
72
73static 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
84static 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 Stellardb4a313a2014-08-01 00:32:39 +000097 if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0)))
Tom Stellard1aaad692014-07-21 16:55:33 +000098 return false;
99
100 // We don't need to check src0, all input types are legal, so just make
101 // sure src0 isn't using any modifiers.
102 const MachineOperand *Src0Mod =
103 TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
104 if (Src0Mod && Src0Mod->getImm() != 0)
105 return false;
106
107 // Check output modifiers
108 const MachineOperand *Omod = TII->getNamedOperand(MI, AMDGPU::OpName::omod);
109 if (Omod && Omod->getImm() != 0)
110 return false;
111
112 const MachineOperand *Clamp = TII->getNamedOperand(MI, AMDGPU::OpName::clamp);
113 return !Clamp || Clamp->getImm() == 0;
114}
115
Tom Stellard6407e1e2014-08-01 00:32:33 +0000116/// \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.
120static 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 } else if (MovSrc.isFPImm()) {
Matt Arsenaultffc5d5b2014-10-17 18:00:41 +0000156 const ConstantFP *CFP = MovSrc.getFPImm();
157 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
158 Src0->ChangeToFPImmediate(CFP);
Tom Stellard6407e1e2014-08-01 00:32:33 +0000159 ConstantFolded = true;
160 }
161 }
162 if (ConstantFolded) {
Tom Stellard6407e1e2014-08-01 00:32:33 +0000163 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
Tom Stellard1aaad692014-07-21 16:55:33 +0000177bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
178 MachineRegisterInfo &MRI = MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000179 const SIInstrInfo *TII =
180 static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
Tom Stellard1aaad692014-07-21 16:55:33 +0000181 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
Tom Stellard86d12eb2014-08-01 00:32:28 +0000193 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard1aaad692014-07-21 16:55:33 +0000194 continue;
195
196 if (!canShrink(MI, TII, TRI, MRI)) {
Matt Arsenault66524032014-09-16 18:00:23 +0000197 // Try commuting the instruction and see if that enables us to shrink
Tom Stellard1aaad692014-07-21 16:55:33 +0000198 // it.
199 if (!MI.isCommutable() || !TII->commuteInstruction(&MI) ||
200 !canShrink(MI, TII, TRI, MRI))
201 continue;
202 }
203
Tom Stellard86d12eb2014-08-01 00:32:28 +0000204 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
Tom Stellard1aaad692014-07-21 16:55:33 +0000211 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
Matt Arsenault5d26d042014-09-13 19:58:27 +0000215 // force them to use VCC here, because the register allocator has
216 // trouble with sequences like this, which cause the allocator to run
217 // out of registers if vreg0 and vreg1 belong to the VCCReg register
218 // class:
Tom Stellard1aaad692014-07-21 16:55:33 +0000219 // vreg0 = VOPC;
220 // vreg1 = VOPC;
221 // S_AND_B64 vreg0, vreg1
222 //
Matt Arsenaulta9627ae2014-09-21 17:27:32 +0000223 // So, instead of forcing the instruction to write to VCC, we provide
224 // a hint to the register allocator to use VCC and then we we will run
225 // this pass again after RA and shrink it if it outputs to VCC.
Tom Stellard1aaad692014-07-21 16:55:33 +0000226 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
227 continue;
228 }
229 if (DstReg != AMDGPU::VCC)
230 continue;
231 }
232
233 // We can shrink this instruction
Tom Stellard6407e1e2014-08-01 00:32:33 +0000234 DEBUG(dbgs() << "Shrinking "; MI.dump(); dbgs() << '\n';);
Tom Stellard1aaad692014-07-21 16:55:33 +0000235
Tom Stellard6407e1e2014-08-01 00:32:33 +0000236 MachineInstrBuilder Inst32 =
Tom Stellard1aaad692014-07-21 16:55:33 +0000237 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
238
239 // dst
Tom Stellard6407e1e2014-08-01 00:32:33 +0000240 Inst32.addOperand(MI.getOperand(0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000241
Tom Stellard6407e1e2014-08-01 00:32:33 +0000242 Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000243
244 const MachineOperand *Src1 =
245 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
246 if (Src1)
Tom Stellard6407e1e2014-08-01 00:32:33 +0000247 Inst32.addOperand(*Src1);
Tom Stellard1aaad692014-07-21 16:55:33 +0000248
Tom Stellard1aaad692014-07-21 16:55:33 +0000249 ++NumInstructionsShrunk;
250 MI.eraseFromParent();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000251
252 foldImmediates(*Inst32, TII, MRI);
253 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
254
255
Tom Stellard1aaad692014-07-21 16:55:33 +0000256 }
257 }
258 return false;
259}