1*9880d681SAndroid Build Coastguard Worker //===--------------------- R600MergeVectorRegisters.cpp -------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// This pass merges inputs of swizzeable instructions into vector sharing
12*9880d681SAndroid Build Coastguard Worker /// common data and/or have enough undef subreg using swizzle abilities.
13*9880d681SAndroid Build Coastguard Worker ///
14*9880d681SAndroid Build Coastguard Worker /// For instance let's consider the following pseudo code :
15*9880d681SAndroid Build Coastguard Worker /// vreg5<def> = REG_SEQ vreg1, sub0, vreg2, sub1, vreg3, sub2, undef, sub3
16*9880d681SAndroid Build Coastguard Worker /// ...
17*9880d681SAndroid Build Coastguard Worker /// vreg7<def> = REG_SEQ vreg1, sub0, vreg3, sub1, undef, sub2, vreg4, sub3
18*9880d681SAndroid Build Coastguard Worker /// (swizzable Inst) vreg7, SwizzleMask : sub0, sub1, sub2, sub3
19*9880d681SAndroid Build Coastguard Worker ///
20*9880d681SAndroid Build Coastguard Worker /// is turned into :
21*9880d681SAndroid Build Coastguard Worker /// vreg5<def> = REG_SEQ vreg1, sub0, vreg2, sub1, vreg3, sub2, undef, sub3
22*9880d681SAndroid Build Coastguard Worker /// ...
23*9880d681SAndroid Build Coastguard Worker /// vreg7<def> = INSERT_SUBREG vreg4, sub3
24*9880d681SAndroid Build Coastguard Worker /// (swizzable Inst) vreg7, SwizzleMask : sub0, sub2, sub1, sub3
25*9880d681SAndroid Build Coastguard Worker ///
26*9880d681SAndroid Build Coastguard Worker /// This allow regalloc to reduce register pressure for vector registers and
27*9880d681SAndroid Build Coastguard Worker /// to reduce MOV count.
28*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker #include "AMDGPU.h"
31*9880d681SAndroid Build Coastguard Worker #include "AMDGPUSubtarget.h"
32*9880d681SAndroid Build Coastguard Worker #include "R600Defines.h"
33*9880d681SAndroid Build Coastguard Worker #include "R600InstrInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/DFAPacketizer.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker using namespace llvm;
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "vec-merger"
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker namespace {
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker static bool
isImplicitlyDef(MachineRegisterInfo & MRI,unsigned Reg)51*9880d681SAndroid Build Coastguard Worker isImplicitlyDef(MachineRegisterInfo &MRI, unsigned Reg) {
52*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::def_instr_iterator It = MRI.def_instr_begin(Reg),
53*9880d681SAndroid Build Coastguard Worker E = MRI.def_instr_end(); It != E; ++It) {
54*9880d681SAndroid Build Coastguard Worker return (*It).isImplicitDef();
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker if (MRI.isReserved(Reg)) {
57*9880d681SAndroid Build Coastguard Worker return false;
58*9880d681SAndroid Build Coastguard Worker }
59*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Reg without a def");
60*9880d681SAndroid Build Coastguard Worker return false;
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker class RegSeqInfo {
64*9880d681SAndroid Build Coastguard Worker public:
65*9880d681SAndroid Build Coastguard Worker MachineInstr *Instr;
66*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, unsigned> RegToChan;
67*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> UndefReg;
RegSeqInfo(MachineRegisterInfo & MRI,MachineInstr * MI)68*9880d681SAndroid Build Coastguard Worker RegSeqInfo(MachineRegisterInfo &MRI, MachineInstr *MI) : Instr(MI) {
69*9880d681SAndroid Build Coastguard Worker assert(MI->getOpcode() == AMDGPU::REG_SEQUENCE);
70*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Instr->getNumOperands(); i < e; i+=2) {
71*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = Instr->getOperand(i);
72*9880d681SAndroid Build Coastguard Worker unsigned Chan = Instr->getOperand(i + 1).getImm();
73*9880d681SAndroid Build Coastguard Worker if (isImplicitlyDef(MRI, MO.getReg()))
74*9880d681SAndroid Build Coastguard Worker UndefReg.push_back(Chan);
75*9880d681SAndroid Build Coastguard Worker else
76*9880d681SAndroid Build Coastguard Worker RegToChan[MO.getReg()] = Chan;
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker }
RegSeqInfo()79*9880d681SAndroid Build Coastguard Worker RegSeqInfo() {}
80*9880d681SAndroid Build Coastguard Worker
operator ==(const RegSeqInfo & RSI) const81*9880d681SAndroid Build Coastguard Worker bool operator==(const RegSeqInfo &RSI) const {
82*9880d681SAndroid Build Coastguard Worker return RSI.Instr == Instr;
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker };
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker class R600VectorRegMerger : public MachineFunctionPass {
87*9880d681SAndroid Build Coastguard Worker private:
88*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI;
89*9880d681SAndroid Build Coastguard Worker const R600InstrInfo *TII;
90*9880d681SAndroid Build Coastguard Worker bool canSwizzle(const MachineInstr &) const;
91*9880d681SAndroid Build Coastguard Worker bool areAllUsesSwizzeable(unsigned Reg) const;
92*9880d681SAndroid Build Coastguard Worker void SwizzleInput(MachineInstr &,
93*9880d681SAndroid Build Coastguard Worker const std::vector<std::pair<unsigned, unsigned> > &) const;
94*9880d681SAndroid Build Coastguard Worker bool tryMergeVector(const RegSeqInfo *, RegSeqInfo *,
95*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > &Remap) const;
96*9880d681SAndroid Build Coastguard Worker bool tryMergeUsingCommonSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
97*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > &RemapChan);
98*9880d681SAndroid Build Coastguard Worker bool tryMergeUsingFreeSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
99*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > &RemapChan);
100*9880d681SAndroid Build Coastguard Worker MachineInstr *RebuildVector(RegSeqInfo *MI,
101*9880d681SAndroid Build Coastguard Worker const RegSeqInfo *BaseVec,
102*9880d681SAndroid Build Coastguard Worker const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const;
103*9880d681SAndroid Build Coastguard Worker void RemoveMI(MachineInstr *);
104*9880d681SAndroid Build Coastguard Worker void trackRSI(const RegSeqInfo &RSI);
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker typedef DenseMap<unsigned, std::vector<MachineInstr *> > InstructionSetMap;
107*9880d681SAndroid Build Coastguard Worker DenseMap<MachineInstr *, RegSeqInfo> PreviousRegSeq;
108*9880d681SAndroid Build Coastguard Worker InstructionSetMap PreviousRegSeqByReg;
109*9880d681SAndroid Build Coastguard Worker InstructionSetMap PreviousRegSeqByUndefCount;
110*9880d681SAndroid Build Coastguard Worker public:
111*9880d681SAndroid Build Coastguard Worker static char ID;
R600VectorRegMerger(TargetMachine & tm)112*9880d681SAndroid Build Coastguard Worker R600VectorRegMerger(TargetMachine &tm) : MachineFunctionPass(ID),
113*9880d681SAndroid Build Coastguard Worker TII(nullptr) { }
114*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const115*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
116*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
117*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
118*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
119*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineLoopInfo>();
120*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineLoopInfo>();
121*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker
getPassName() const124*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
125*9880d681SAndroid Build Coastguard Worker return "R600 Vector Registers Merge Pass";
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &Fn) override;
129*9880d681SAndroid Build Coastguard Worker };
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker char R600VectorRegMerger::ID = 0;
132*9880d681SAndroid Build Coastguard Worker
canSwizzle(const MachineInstr & MI) const133*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::canSwizzle(const MachineInstr &MI)
134*9880d681SAndroid Build Coastguard Worker const {
135*9880d681SAndroid Build Coastguard Worker if (TII->get(MI.getOpcode()).TSFlags & R600_InstFlag::TEX_INST)
136*9880d681SAndroid Build Coastguard Worker return true;
137*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
138*9880d681SAndroid Build Coastguard Worker case AMDGPU::R600_ExportSwz:
139*9880d681SAndroid Build Coastguard Worker case AMDGPU::EG_ExportSwz:
140*9880d681SAndroid Build Coastguard Worker return true;
141*9880d681SAndroid Build Coastguard Worker default:
142*9880d681SAndroid Build Coastguard Worker return false;
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker
tryMergeVector(const RegSeqInfo * Untouched,RegSeqInfo * ToMerge,std::vector<std::pair<unsigned,unsigned>> & Remap) const146*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::tryMergeVector(const RegSeqInfo *Untouched,
147*9880d681SAndroid Build Coastguard Worker RegSeqInfo *ToMerge, std::vector< std::pair<unsigned, unsigned> > &Remap)
148*9880d681SAndroid Build Coastguard Worker const {
149*9880d681SAndroid Build Coastguard Worker unsigned CurrentUndexIdx = 0;
150*9880d681SAndroid Build Coastguard Worker for (DenseMap<unsigned, unsigned>::iterator It = ToMerge->RegToChan.begin(),
151*9880d681SAndroid Build Coastguard Worker E = ToMerge->RegToChan.end(); It != E; ++It) {
152*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, unsigned>::const_iterator PosInUntouched =
153*9880d681SAndroid Build Coastguard Worker Untouched->RegToChan.find((*It).first);
154*9880d681SAndroid Build Coastguard Worker if (PosInUntouched != Untouched->RegToChan.end()) {
155*9880d681SAndroid Build Coastguard Worker Remap.push_back(std::pair<unsigned, unsigned>
156*9880d681SAndroid Build Coastguard Worker ((*It).second, (*PosInUntouched).second));
157*9880d681SAndroid Build Coastguard Worker continue;
158*9880d681SAndroid Build Coastguard Worker }
159*9880d681SAndroid Build Coastguard Worker if (CurrentUndexIdx >= Untouched->UndefReg.size())
160*9880d681SAndroid Build Coastguard Worker return false;
161*9880d681SAndroid Build Coastguard Worker Remap.push_back(std::pair<unsigned, unsigned>
162*9880d681SAndroid Build Coastguard Worker ((*It).second, Untouched->UndefReg[CurrentUndexIdx++]));
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker return true;
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker static
getReassignedChan(const std::vector<std::pair<unsigned,unsigned>> & RemapChan,unsigned Chan)169*9880d681SAndroid Build Coastguard Worker unsigned getReassignedChan(
170*9880d681SAndroid Build Coastguard Worker const std::vector<std::pair<unsigned, unsigned> > &RemapChan,
171*9880d681SAndroid Build Coastguard Worker unsigned Chan) {
172*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, je = RemapChan.size(); j < je; j++) {
173*9880d681SAndroid Build Coastguard Worker if (RemapChan[j].first == Chan)
174*9880d681SAndroid Build Coastguard Worker return RemapChan[j].second;
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Chan wasn't reassigned");
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker
RebuildVector(RegSeqInfo * RSI,const RegSeqInfo * BaseRSI,const std::vector<std::pair<unsigned,unsigned>> & RemapChan) const179*9880d681SAndroid Build Coastguard Worker MachineInstr *R600VectorRegMerger::RebuildVector(
180*9880d681SAndroid Build Coastguard Worker RegSeqInfo *RSI, const RegSeqInfo *BaseRSI,
181*9880d681SAndroid Build Coastguard Worker const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const {
182*9880d681SAndroid Build Coastguard Worker unsigned Reg = RSI->Instr->getOperand(0).getReg();
183*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator Pos = RSI->Instr;
184*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = *Pos->getParent();
185*9880d681SAndroid Build Coastguard Worker DebugLoc DL = Pos->getDebugLoc();
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker unsigned SrcVec = BaseRSI->Instr->getOperand(0).getReg();
188*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, unsigned> UpdatedRegToChan = BaseRSI->RegToChan;
189*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> UpdatedUndef = BaseRSI->UndefReg;
190*9880d681SAndroid Build Coastguard Worker for (DenseMap<unsigned, unsigned>::iterator It = RSI->RegToChan.begin(),
191*9880d681SAndroid Build Coastguard Worker E = RSI->RegToChan.end(); It != E; ++It) {
192*9880d681SAndroid Build Coastguard Worker unsigned DstReg = MRI->createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
193*9880d681SAndroid Build Coastguard Worker unsigned SubReg = (*It).first;
194*9880d681SAndroid Build Coastguard Worker unsigned Swizzle = (*It).second;
195*9880d681SAndroid Build Coastguard Worker unsigned Chan = getReassignedChan(RemapChan, Swizzle);
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker MachineInstr *Tmp = BuildMI(MBB, Pos, DL, TII->get(AMDGPU::INSERT_SUBREG),
198*9880d681SAndroid Build Coastguard Worker DstReg)
199*9880d681SAndroid Build Coastguard Worker .addReg(SrcVec)
200*9880d681SAndroid Build Coastguard Worker .addReg(SubReg)
201*9880d681SAndroid Build Coastguard Worker .addImm(Chan);
202*9880d681SAndroid Build Coastguard Worker UpdatedRegToChan[SubReg] = Chan;
203*9880d681SAndroid Build Coastguard Worker std::vector<unsigned>::iterator ChanPos =
204*9880d681SAndroid Build Coastguard Worker std::find(UpdatedUndef.begin(), UpdatedUndef.end(), Chan);
205*9880d681SAndroid Build Coastguard Worker if (ChanPos != UpdatedUndef.end())
206*9880d681SAndroid Build Coastguard Worker UpdatedUndef.erase(ChanPos);
207*9880d681SAndroid Build Coastguard Worker assert(std::find(UpdatedUndef.begin(), UpdatedUndef.end(), Chan) ==
208*9880d681SAndroid Build Coastguard Worker UpdatedUndef.end() &&
209*9880d681SAndroid Build Coastguard Worker "UpdatedUndef shouldn't contain Chan more than once!");
210*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ->"; Tmp->dump(););
211*9880d681SAndroid Build Coastguard Worker (void)Tmp;
212*9880d681SAndroid Build Coastguard Worker SrcVec = DstReg;
213*9880d681SAndroid Build Coastguard Worker }
214*9880d681SAndroid Build Coastguard Worker MachineInstr *NewMI =
215*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, Pos, DL, TII->get(AMDGPU::COPY), Reg).addReg(SrcVec);
216*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ->"; NewMI->dump(););
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Updating Swizzle:\n");
219*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::use_instr_iterator It = MRI->use_instr_begin(Reg),
220*9880d681SAndroid Build Coastguard Worker E = MRI->use_instr_end(); It != E; ++It) {
221*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ";(*It).dump(); dbgs() << " ->");
222*9880d681SAndroid Build Coastguard Worker SwizzleInput(*It, RemapChan);
223*9880d681SAndroid Build Coastguard Worker DEBUG((*It).dump());
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker RSI->Instr->eraseFromParent();
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker // Update RSI
228*9880d681SAndroid Build Coastguard Worker RSI->Instr = NewMI;
229*9880d681SAndroid Build Coastguard Worker RSI->RegToChan = UpdatedRegToChan;
230*9880d681SAndroid Build Coastguard Worker RSI->UndefReg = UpdatedUndef;
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker return NewMI;
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
RemoveMI(MachineInstr * MI)235*9880d681SAndroid Build Coastguard Worker void R600VectorRegMerger::RemoveMI(MachineInstr *MI) {
236*9880d681SAndroid Build Coastguard Worker for (InstructionSetMap::iterator It = PreviousRegSeqByReg.begin(),
237*9880d681SAndroid Build Coastguard Worker E = PreviousRegSeqByReg.end(); It != E; ++It) {
238*9880d681SAndroid Build Coastguard Worker std::vector<MachineInstr *> &MIs = (*It).second;
239*9880d681SAndroid Build Coastguard Worker MIs.erase(std::find(MIs.begin(), MIs.end(), MI), MIs.end());
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker for (InstructionSetMap::iterator It = PreviousRegSeqByUndefCount.begin(),
242*9880d681SAndroid Build Coastguard Worker E = PreviousRegSeqByUndefCount.end(); It != E; ++It) {
243*9880d681SAndroid Build Coastguard Worker std::vector<MachineInstr *> &MIs = (*It).second;
244*9880d681SAndroid Build Coastguard Worker MIs.erase(std::find(MIs.begin(), MIs.end(), MI), MIs.end());
245*9880d681SAndroid Build Coastguard Worker }
246*9880d681SAndroid Build Coastguard Worker }
247*9880d681SAndroid Build Coastguard Worker
SwizzleInput(MachineInstr & MI,const std::vector<std::pair<unsigned,unsigned>> & RemapChan) const248*9880d681SAndroid Build Coastguard Worker void R600VectorRegMerger::SwizzleInput(MachineInstr &MI,
249*9880d681SAndroid Build Coastguard Worker const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const {
250*9880d681SAndroid Build Coastguard Worker unsigned Offset;
251*9880d681SAndroid Build Coastguard Worker if (TII->get(MI.getOpcode()).TSFlags & R600_InstFlag::TEX_INST)
252*9880d681SAndroid Build Coastguard Worker Offset = 2;
253*9880d681SAndroid Build Coastguard Worker else
254*9880d681SAndroid Build Coastguard Worker Offset = 3;
255*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < 4; i++) {
256*9880d681SAndroid Build Coastguard Worker unsigned Swizzle = MI.getOperand(i + Offset).getImm() + 1;
257*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, e = RemapChan.size(); j < e; j++) {
258*9880d681SAndroid Build Coastguard Worker if (RemapChan[j].first == Swizzle) {
259*9880d681SAndroid Build Coastguard Worker MI.getOperand(i + Offset).setImm(RemapChan[j].second - 1);
260*9880d681SAndroid Build Coastguard Worker break;
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker }
263*9880d681SAndroid Build Coastguard Worker }
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker
areAllUsesSwizzeable(unsigned Reg) const266*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::areAllUsesSwizzeable(unsigned Reg) const {
267*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::use_instr_iterator It = MRI->use_instr_begin(Reg),
268*9880d681SAndroid Build Coastguard Worker E = MRI->use_instr_end(); It != E; ++It) {
269*9880d681SAndroid Build Coastguard Worker if (!canSwizzle(*It))
270*9880d681SAndroid Build Coastguard Worker return false;
271*9880d681SAndroid Build Coastguard Worker }
272*9880d681SAndroid Build Coastguard Worker return true;
273*9880d681SAndroid Build Coastguard Worker }
274*9880d681SAndroid Build Coastguard Worker
tryMergeUsingCommonSlot(RegSeqInfo & RSI,RegSeqInfo & CompatibleRSI,std::vector<std::pair<unsigned,unsigned>> & RemapChan)275*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::tryMergeUsingCommonSlot(RegSeqInfo &RSI,
276*9880d681SAndroid Build Coastguard Worker RegSeqInfo &CompatibleRSI,
277*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > &RemapChan) {
278*9880d681SAndroid Build Coastguard Worker for (MachineInstr::mop_iterator MOp = RSI.Instr->operands_begin(),
279*9880d681SAndroid Build Coastguard Worker MOE = RSI.Instr->operands_end(); MOp != MOE; ++MOp) {
280*9880d681SAndroid Build Coastguard Worker if (!MOp->isReg())
281*9880d681SAndroid Build Coastguard Worker continue;
282*9880d681SAndroid Build Coastguard Worker if (PreviousRegSeqByReg[MOp->getReg()].empty())
283*9880d681SAndroid Build Coastguard Worker continue;
284*9880d681SAndroid Build Coastguard Worker for (MachineInstr *MI : PreviousRegSeqByReg[MOp->getReg()]) {
285*9880d681SAndroid Build Coastguard Worker CompatibleRSI = PreviousRegSeq[MI];
286*9880d681SAndroid Build Coastguard Worker if (RSI == CompatibleRSI)
287*9880d681SAndroid Build Coastguard Worker continue;
288*9880d681SAndroid Build Coastguard Worker if (tryMergeVector(&CompatibleRSI, &RSI, RemapChan))
289*9880d681SAndroid Build Coastguard Worker return true;
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker return false;
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
tryMergeUsingFreeSlot(RegSeqInfo & RSI,RegSeqInfo & CompatibleRSI,std::vector<std::pair<unsigned,unsigned>> & RemapChan)295*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::tryMergeUsingFreeSlot(RegSeqInfo &RSI,
296*9880d681SAndroid Build Coastguard Worker RegSeqInfo &CompatibleRSI,
297*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > &RemapChan) {
298*9880d681SAndroid Build Coastguard Worker unsigned NeededUndefs = 4 - RSI.UndefReg.size();
299*9880d681SAndroid Build Coastguard Worker if (PreviousRegSeqByUndefCount[NeededUndefs].empty())
300*9880d681SAndroid Build Coastguard Worker return false;
301*9880d681SAndroid Build Coastguard Worker std::vector<MachineInstr *> &MIs =
302*9880d681SAndroid Build Coastguard Worker PreviousRegSeqByUndefCount[NeededUndefs];
303*9880d681SAndroid Build Coastguard Worker CompatibleRSI = PreviousRegSeq[MIs.back()];
304*9880d681SAndroid Build Coastguard Worker tryMergeVector(&CompatibleRSI, &RSI, RemapChan);
305*9880d681SAndroid Build Coastguard Worker return true;
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker
trackRSI(const RegSeqInfo & RSI)308*9880d681SAndroid Build Coastguard Worker void R600VectorRegMerger::trackRSI(const RegSeqInfo &RSI) {
309*9880d681SAndroid Build Coastguard Worker for (DenseMap<unsigned, unsigned>::const_iterator
310*9880d681SAndroid Build Coastguard Worker It = RSI.RegToChan.begin(), E = RSI.RegToChan.end(); It != E; ++It) {
311*9880d681SAndroid Build Coastguard Worker PreviousRegSeqByReg[(*It).first].push_back(RSI.Instr);
312*9880d681SAndroid Build Coastguard Worker }
313*9880d681SAndroid Build Coastguard Worker PreviousRegSeqByUndefCount[RSI.UndefReg.size()].push_back(RSI.Instr);
314*9880d681SAndroid Build Coastguard Worker PreviousRegSeq[RSI.Instr] = RSI;
315*9880d681SAndroid Build Coastguard Worker }
316*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & Fn)317*9880d681SAndroid Build Coastguard Worker bool R600VectorRegMerger::runOnMachineFunction(MachineFunction &Fn) {
318*9880d681SAndroid Build Coastguard Worker if (skipFunction(*Fn.getFunction()))
319*9880d681SAndroid Build Coastguard Worker return false;
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard Worker const R600Subtarget &ST = Fn.getSubtarget<R600Subtarget>();
322*9880d681SAndroid Build Coastguard Worker TII = ST.getInstrInfo();
323*9880d681SAndroid Build Coastguard Worker MRI = &Fn.getRegInfo();
324*9880d681SAndroid Build Coastguard Worker
325*9880d681SAndroid Build Coastguard Worker for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
326*9880d681SAndroid Build Coastguard Worker MBB != MBBe; ++MBB) {
327*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MB = &*MBB;
328*9880d681SAndroid Build Coastguard Worker PreviousRegSeq.clear();
329*9880d681SAndroid Build Coastguard Worker PreviousRegSeqByReg.clear();
330*9880d681SAndroid Build Coastguard Worker PreviousRegSeqByUndefCount.clear();
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator MII = MB->begin(), MIIE = MB->end();
333*9880d681SAndroid Build Coastguard Worker MII != MIIE; ++MII) {
334*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *MII;
335*9880d681SAndroid Build Coastguard Worker if (MI.getOpcode() != AMDGPU::REG_SEQUENCE) {
336*9880d681SAndroid Build Coastguard Worker if (TII->get(MI.getOpcode()).TSFlags & R600_InstFlag::TEX_INST) {
337*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI.getOperand(1).getReg();
338*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::def_instr_iterator
339*9880d681SAndroid Build Coastguard Worker It = MRI->def_instr_begin(Reg), E = MRI->def_instr_end();
340*9880d681SAndroid Build Coastguard Worker It != E; ++It) {
341*9880d681SAndroid Build Coastguard Worker RemoveMI(&(*It));
342*9880d681SAndroid Build Coastguard Worker }
343*9880d681SAndroid Build Coastguard Worker }
344*9880d681SAndroid Build Coastguard Worker continue;
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker RegSeqInfo RSI(*MRI, &MI);
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker // All uses of MI are swizzeable ?
350*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI.getOperand(0).getReg();
351*9880d681SAndroid Build Coastguard Worker if (!areAllUsesSwizzeable(Reg))
352*9880d681SAndroid Build Coastguard Worker continue;
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker DEBUG({
355*9880d681SAndroid Build Coastguard Worker dbgs() << "Trying to optimize ";
356*9880d681SAndroid Build Coastguard Worker MI.dump();
357*9880d681SAndroid Build Coastguard Worker });
358*9880d681SAndroid Build Coastguard Worker
359*9880d681SAndroid Build Coastguard Worker RegSeqInfo CandidateRSI;
360*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<unsigned, unsigned> > RemapChan;
361*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Using common slots...\n";);
362*9880d681SAndroid Build Coastguard Worker if (tryMergeUsingCommonSlot(RSI, CandidateRSI, RemapChan)) {
363*9880d681SAndroid Build Coastguard Worker // Remove CandidateRSI mapping
364*9880d681SAndroid Build Coastguard Worker RemoveMI(CandidateRSI.Instr);
365*9880d681SAndroid Build Coastguard Worker MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
366*9880d681SAndroid Build Coastguard Worker trackRSI(RSI);
367*9880d681SAndroid Build Coastguard Worker continue;
368*9880d681SAndroid Build Coastguard Worker }
369*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Using free slots...\n";);
370*9880d681SAndroid Build Coastguard Worker RemapChan.clear();
371*9880d681SAndroid Build Coastguard Worker if (tryMergeUsingFreeSlot(RSI, CandidateRSI, RemapChan)) {
372*9880d681SAndroid Build Coastguard Worker RemoveMI(CandidateRSI.Instr);
373*9880d681SAndroid Build Coastguard Worker MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
374*9880d681SAndroid Build Coastguard Worker trackRSI(RSI);
375*9880d681SAndroid Build Coastguard Worker continue;
376*9880d681SAndroid Build Coastguard Worker }
377*9880d681SAndroid Build Coastguard Worker //Failed to merge
378*9880d681SAndroid Build Coastguard Worker trackRSI(RSI);
379*9880d681SAndroid Build Coastguard Worker }
380*9880d681SAndroid Build Coastguard Worker }
381*9880d681SAndroid Build Coastguard Worker return false;
382*9880d681SAndroid Build Coastguard Worker }
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker
createR600VectorRegMerger(TargetMachine & tm)386*9880d681SAndroid Build Coastguard Worker llvm::FunctionPass *llvm::createR600VectorRegMerger(TargetMachine &tm) {
387*9880d681SAndroid Build Coastguard Worker return new R600VectorRegMerger(tm);
388*9880d681SAndroid Build Coastguard Worker }
389