1 //===--------------------- R600MergeVectorRegisters.cpp -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This pass merges inputs of swizzeable instructions into vector sharing
12 /// common data and/or have enough undef subreg using swizzle abilities.
14 /// For instance let's consider the following pseudo code :
15 /// vreg5<def> = REG_SEQ vreg1, sub0, vreg2, sub1, vreg3, sub2, undef, sub3
17 /// vreg7<def> = REG_SEQ vreg1, sub0, vreg3, sub1, undef, sub2, vreg4, sub3
18 /// (swizzable Inst) vreg7, SwizzleMask : sub0, sub1, sub2, sub3
21 /// vreg5<def> = REG_SEQ vreg1, sub0, vreg2, sub1, vreg3, sub2, undef, sub3
23 /// vreg7<def> = INSERT_SUBREG vreg4, sub3
24 /// (swizzable Inst) vreg7, SwizzleMask : sub0, sub2, sub1, sub3
26 /// This allow regalloc to reduce register pressure for vector registers and
27 /// to reduce MOV count.
28 //===----------------------------------------------------------------------===//
31 #include "AMDGPUSubtarget.h"
32 #include "R600InstrInfo.h"
33 #include "llvm/CodeGen/DFAPacketizer.h"
34 #include "llvm/CodeGen/MachineDominators.h"
35 #include "llvm/CodeGen/MachineFunctionPass.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineLoopInfo.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/Passes.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
45 #define DEBUG_TYPE "vec-merger"
50 isImplicitlyDef(MachineRegisterInfo &MRI, unsigned Reg) {
51 for (MachineRegisterInfo::def_instr_iterator It = MRI.def_instr_begin(Reg),
52 E = MRI.def_instr_end(); It != E; ++It) {
53 return (*It).isImplicitDef();
55 if (MRI.isReserved(Reg)) {
58 llvm_unreachable("Reg without a def");
65 DenseMap<unsigned, unsigned> RegToChan;
66 std::vector<unsigned> UndefReg;
67 RegSeqInfo(MachineRegisterInfo &MRI, MachineInstr *MI) : Instr(MI) {
68 assert(MI->getOpcode() == AMDGPU::REG_SEQUENCE);
69 for (unsigned i = 1, e = Instr->getNumOperands(); i < e; i+=2) {
70 MachineOperand &MO = Instr->getOperand(i);
71 unsigned Chan = Instr->getOperand(i + 1).getImm();
72 if (isImplicitlyDef(MRI, MO.getReg()))
73 UndefReg.push_back(Chan);
75 RegToChan[MO.getReg()] = Chan;
80 bool operator==(const RegSeqInfo &RSI) const {
81 return RSI.Instr == Instr;
85 class R600VectorRegMerger : public MachineFunctionPass {
87 MachineRegisterInfo *MRI;
88 const R600InstrInfo *TII;
89 bool canSwizzle(const MachineInstr &) const;
90 bool areAllUsesSwizzeable(unsigned Reg) const;
91 void SwizzleInput(MachineInstr &,
92 const std::vector<std::pair<unsigned, unsigned> > &) const;
93 bool tryMergeVector(const RegSeqInfo *, RegSeqInfo *,
94 std::vector<std::pair<unsigned, unsigned> > &Remap) const;
95 bool tryMergeUsingCommonSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
96 std::vector<std::pair<unsigned, unsigned> > &RemapChan);
97 bool tryMergeUsingFreeSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
98 std::vector<std::pair<unsigned, unsigned> > &RemapChan);
99 MachineInstr *RebuildVector(RegSeqInfo *MI,
100 const RegSeqInfo *BaseVec,
101 const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const;
102 void RemoveMI(MachineInstr *);
103 void trackRSI(const RegSeqInfo &RSI);
105 typedef DenseMap<unsigned, std::vector<MachineInstr *> > InstructionSetMap;
106 DenseMap<MachineInstr *, RegSeqInfo> PreviousRegSeq;
107 InstructionSetMap PreviousRegSeqByReg;
108 InstructionSetMap PreviousRegSeqByUndefCount;
111 R600VectorRegMerger(TargetMachine &tm) : MachineFunctionPass(ID),
114 void getAnalysisUsage(AnalysisUsage &AU) const override {
115 AU.setPreservesCFG();
116 AU.addRequired<MachineDominatorTree>();
117 AU.addPreserved<MachineDominatorTree>();
118 AU.addRequired<MachineLoopInfo>();
119 AU.addPreserved<MachineLoopInfo>();
120 MachineFunctionPass::getAnalysisUsage(AU);
123 const char *getPassName() const override {
124 return "R600 Vector Registers Merge Pass";
127 bool runOnMachineFunction(MachineFunction &Fn) override;
130 char R600VectorRegMerger::ID = 0;
132 bool R600VectorRegMerger::canSwizzle(const MachineInstr &MI)
134 if (TII->get(MI.getOpcode()).TSFlags & R600_InstFlag::TEX_INST)
136 switch (MI.getOpcode()) {
137 case AMDGPU::R600_ExportSwz:
138 case AMDGPU::EG_ExportSwz:
145 bool R600VectorRegMerger::tryMergeVector(const RegSeqInfo *Untouched,
146 RegSeqInfo *ToMerge, std::vector< std::pair<unsigned, unsigned> > &Remap)
148 unsigned CurrentUndexIdx = 0;
149 for (DenseMap<unsigned, unsigned>::iterator It = ToMerge->RegToChan.begin(),
150 E = ToMerge->RegToChan.end(); It != E; ++It) {
151 DenseMap<unsigned, unsigned>::const_iterator PosInUntouched =
152 Untouched->RegToChan.find((*It).first);
153 if (PosInUntouched != Untouched->RegToChan.end()) {
154 Remap.push_back(std::pair<unsigned, unsigned>
155 ((*It).second, (*PosInUntouched).second));
158 if (CurrentUndexIdx >= Untouched->UndefReg.size())
160 Remap.push_back(std::pair<unsigned, unsigned>
161 ((*It).second, Untouched->UndefReg[CurrentUndexIdx++]));
168 unsigned getReassignedChan(
169 const std::vector<std::pair<unsigned, unsigned> > &RemapChan,
171 for (unsigned j = 0, je = RemapChan.size(); j < je; j++) {
172 if (RemapChan[j].first == Chan)
173 return RemapChan[j].second;
175 llvm_unreachable("Chan wasn't reassigned");
178 MachineInstr *R600VectorRegMerger::RebuildVector(
179 RegSeqInfo *RSI, const RegSeqInfo *BaseRSI,
180 const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const {
181 unsigned Reg = RSI->Instr->getOperand(0).getReg();
182 MachineBasicBlock::iterator Pos = RSI->Instr;
183 MachineBasicBlock &MBB = *Pos->getParent();
184 DebugLoc DL = Pos->getDebugLoc();
186 unsigned SrcVec = BaseRSI->Instr->getOperand(0).getReg();
187 DenseMap<unsigned, unsigned> UpdatedRegToChan = BaseRSI->RegToChan;
188 std::vector<unsigned> UpdatedUndef = BaseRSI->UndefReg;
189 for (DenseMap<unsigned, unsigned>::iterator It = RSI->RegToChan.begin(),
190 E = RSI->RegToChan.end(); It != E; ++It) {
191 unsigned DstReg = MRI->createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
192 unsigned SubReg = (*It).first;
193 unsigned Swizzle = (*It).second;
194 unsigned Chan = getReassignedChan(RemapChan, Swizzle);
196 MachineInstr *Tmp = BuildMI(MBB, Pos, DL, TII->get(AMDGPU::INSERT_SUBREG),
201 UpdatedRegToChan[SubReg] = Chan;
202 std::vector<unsigned>::iterator ChanPos =
203 std::find(UpdatedUndef.begin(), UpdatedUndef.end(), Chan);
204 if (ChanPos != UpdatedUndef.end())
205 UpdatedUndef.erase(ChanPos);
206 assert(std::find(UpdatedUndef.begin(), UpdatedUndef.end(), Chan) ==
207 UpdatedUndef.end() &&
208 "UpdatedUndef shouldn't contain Chan more than once!");
209 DEBUG(dbgs() << " ->"; Tmp->dump(););
213 Pos = BuildMI(MBB, Pos, DL, TII->get(AMDGPU::COPY), Reg)
215 DEBUG(dbgs() << " ->"; Pos->dump(););
217 DEBUG(dbgs() << " Updating Swizzle:\n");
218 for (MachineRegisterInfo::use_instr_iterator It = MRI->use_instr_begin(Reg),
219 E = MRI->use_instr_end(); It != E; ++It) {
220 DEBUG(dbgs() << " ";(*It).dump(); dbgs() << " ->");
221 SwizzleInput(*It, RemapChan);
224 RSI->Instr->eraseFromParent();
228 RSI->RegToChan = UpdatedRegToChan;
229 RSI->UndefReg = UpdatedUndef;
234 void R600VectorRegMerger::RemoveMI(MachineInstr *MI) {
235 for (InstructionSetMap::iterator It = PreviousRegSeqByReg.begin(),
236 E = PreviousRegSeqByReg.end(); It != E; ++It) {
237 std::vector<MachineInstr *> &MIs = (*It).second;
238 MIs.erase(std::find(MIs.begin(), MIs.end(), MI), MIs.end());
240 for (InstructionSetMap::iterator It = PreviousRegSeqByUndefCount.begin(),
241 E = PreviousRegSeqByUndefCount.end(); It != E; ++It) {
242 std::vector<MachineInstr *> &MIs = (*It).second;
243 MIs.erase(std::find(MIs.begin(), MIs.end(), MI), MIs.end());
247 void R600VectorRegMerger::SwizzleInput(MachineInstr &MI,
248 const std::vector<std::pair<unsigned, unsigned> > &RemapChan) const {
250 if (TII->get(MI.getOpcode()).TSFlags & R600_InstFlag::TEX_INST)
254 for (unsigned i = 0; i < 4; i++) {
255 unsigned Swizzle = MI.getOperand(i + Offset).getImm() + 1;
256 for (unsigned j = 0, e = RemapChan.size(); j < e; j++) {
257 if (RemapChan[j].first == Swizzle) {
258 MI.getOperand(i + Offset).setImm(RemapChan[j].second - 1);
265 bool R600VectorRegMerger::areAllUsesSwizzeable(unsigned Reg) const {
266 for (MachineRegisterInfo::use_instr_iterator It = MRI->use_instr_begin(Reg),
267 E = MRI->use_instr_end(); It != E; ++It) {
268 if (!canSwizzle(*It))
274 bool R600VectorRegMerger::tryMergeUsingCommonSlot(RegSeqInfo &RSI,
275 RegSeqInfo &CompatibleRSI,
276 std::vector<std::pair<unsigned, unsigned> > &RemapChan) {
277 for (MachineInstr::mop_iterator MOp = RSI.Instr->operands_begin(),
278 MOE = RSI.Instr->operands_end(); MOp != MOE; ++MOp) {
281 if (PreviousRegSeqByReg[MOp->getReg()].empty())
283 for (MachineInstr *MI : PreviousRegSeqByReg[MOp->getReg()]) {
284 CompatibleRSI = PreviousRegSeq[MI];
285 if (RSI == CompatibleRSI)
287 if (tryMergeVector(&CompatibleRSI, &RSI, RemapChan))
294 bool R600VectorRegMerger::tryMergeUsingFreeSlot(RegSeqInfo &RSI,
295 RegSeqInfo &CompatibleRSI,
296 std::vector<std::pair<unsigned, unsigned> > &RemapChan) {
297 unsigned NeededUndefs = 4 - RSI.UndefReg.size();
298 if (PreviousRegSeqByUndefCount[NeededUndefs].empty())
300 std::vector<MachineInstr *> &MIs =
301 PreviousRegSeqByUndefCount[NeededUndefs];
302 CompatibleRSI = PreviousRegSeq[MIs.back()];
303 tryMergeVector(&CompatibleRSI, &RSI, RemapChan);
307 void R600VectorRegMerger::trackRSI(const RegSeqInfo &RSI) {
308 for (DenseMap<unsigned, unsigned>::const_iterator
309 It = RSI.RegToChan.begin(), E = RSI.RegToChan.end(); It != E; ++It) {
310 PreviousRegSeqByReg[(*It).first].push_back(RSI.Instr);
312 PreviousRegSeqByUndefCount[RSI.UndefReg.size()].push_back(RSI.Instr);
313 PreviousRegSeq[RSI.Instr] = RSI;
316 bool R600VectorRegMerger::runOnMachineFunction(MachineFunction &Fn) {
317 TII = static_cast<const R600InstrInfo *>(Fn.getSubtarget().getInstrInfo());
318 MRI = &(Fn.getRegInfo());
319 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
320 MBB != MBBe; ++MBB) {
321 MachineBasicBlock *MB = &*MBB;
322 PreviousRegSeq.clear();
323 PreviousRegSeqByReg.clear();
324 PreviousRegSeqByUndefCount.clear();
326 for (MachineBasicBlock::iterator MII = MB->begin(), MIIE = MB->end();
327 MII != MIIE; ++MII) {
328 MachineInstr *MI = MII;
329 if (MI->getOpcode() != AMDGPU::REG_SEQUENCE) {
330 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TEX_INST) {
331 unsigned Reg = MI->getOperand(1).getReg();
332 for (MachineRegisterInfo::def_instr_iterator
333 It = MRI->def_instr_begin(Reg), E = MRI->def_instr_end();
342 RegSeqInfo RSI(*MRI, MI);
344 // All uses of MI are swizzeable ?
345 unsigned Reg = MI->getOperand(0).getReg();
346 if (!areAllUsesSwizzeable(Reg))
349 DEBUG (dbgs() << "Trying to optimize ";
353 RegSeqInfo CandidateRSI;
354 std::vector<std::pair<unsigned, unsigned> > RemapChan;
355 DEBUG(dbgs() << "Using common slots...\n";);
356 if (tryMergeUsingCommonSlot(RSI, CandidateRSI, RemapChan)) {
357 // Remove CandidateRSI mapping
358 RemoveMI(CandidateRSI.Instr);
359 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
363 DEBUG(dbgs() << "Using free slots...\n";);
365 if (tryMergeUsingFreeSlot(RSI, CandidateRSI, RemapChan)) {
366 RemoveMI(CandidateRSI.Instr);
367 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
380 llvm::FunctionPass *llvm::createR600VectorRegMerger(TargetMachine &tm) {
381 return new R600VectorRegMerger(tm);