summaryrefslogtreecommitdiff
path: root/lib/Target/R600/SIFixSGPRLiveness.cpp
blob: 0fecd7a28eb7becc92e1f0d458b0606578ad9535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===-- SIFixSGPRLiveness.cpp - SGPR liveness adjustment ------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// \file
///
/// SGPRs are not affected by control flow. This pass adjusts SGPR liveness in
/// so that the register allocator can still correctly allocate them.
//
//===----------------------------------------------------------------------===//

#include "AMDGPU.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachinePostDominators.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"

using namespace llvm;

namespace {

class SIFixSGPRLiveness : public MachineFunctionPass {
private:
  static char ID;

  const TargetInstrInfo *TII;
  MachineRegisterInfo *MRI;
  MachineDominatorTree *MD;
  MachinePostDominatorTree *MPD;

  bool isSGPR(const TargetRegisterClass *RegClass) {
    return RegClass == &AMDGPU::SReg_1RegClass ||
           RegClass == &AMDGPU::SReg_32RegClass ||
           RegClass == &AMDGPU::SReg_64RegClass ||
           RegClass == &AMDGPU::SReg_128RegClass ||
           RegClass == &AMDGPU::SReg_256RegClass;
  }

  void addKill(MachineBasicBlock::iterator I, unsigned Reg);
  MachineBasicBlock *handleUses(unsigned VirtReg, MachineBasicBlock *Begin);
  void handlePreds(MachineBasicBlock *Begin, MachineBasicBlock *End,
                   unsigned VirtReg);

  bool handleVirtReg(unsigned VirtReg);

public:
  SIFixSGPRLiveness(TargetMachine &tm);

  virtual bool runOnMachineFunction(MachineFunction &MF);

  virtual const char *getPassName() const {
    return "SI fix SGPR liveness pass";
  }

  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
};

} // end anonymous namespace

char SIFixSGPRLiveness::ID = 0;

SIFixSGPRLiveness::SIFixSGPRLiveness(TargetMachine &tm):
  MachineFunctionPass(ID),
  TII(tm.getInstrInfo()) {
  initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
}

void SIFixSGPRLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
  AU.addRequired<MachineDominatorTree>();
  AU.addRequired<MachinePostDominatorTree>();
  AU.setPreservesCFG();
  MachineFunctionPass::getAnalysisUsage(AU);
}

void SIFixSGPRLiveness::addKill(MachineBasicBlock::iterator I, unsigned Reg) {
  MachineBasicBlock *MBB = I->getParent();

  BuildMI(*MBB, I, DebugLoc(), TII->get(TargetOpcode::KILL)).addReg(Reg);
}

// Find the common post dominator of all uses
MachineBasicBlock *SIFixSGPRLiveness::handleUses(unsigned VirtReg,
                                                 MachineBasicBlock *Begin) {
  MachineBasicBlock *LastUse = Begin, *End = Begin;
  bool EndUsesReg = true;

  MachineRegisterInfo::use_iterator i, e;
  for (i = MRI->use_begin(VirtReg), e = MRI->use_end(); i != e; ++i) {
    MachineBasicBlock *MBB = i->getParent();
    if (LastUse == MBB)
      continue;

    LastUse = MBB;
    MBB = MPD->findNearestCommonDominator(End, MBB);

    if (MBB == LastUse)
      EndUsesReg = true;
    else if (MBB != End)
      EndUsesReg = false;

    End = MBB;
  }

  return EndUsesReg ? Begin : End;
}

// Handles predecessors separately, only add KILLs to dominated ones
void SIFixSGPRLiveness::handlePreds(MachineBasicBlock *Begin,
                                    MachineBasicBlock *End,
                                    unsigned VirtReg) {
  MachineBasicBlock::pred_iterator i, e;
  for (i = End->pred_begin(), e = End->pred_end(); i != e; ++i) {

    if (MD->dominates(End, *i))
      continue; // ignore loops

    if (MD->dominates(*i, Begin))
      continue; // too far up, abort search

    if (MD->dominates(Begin, *i)) {
      // found end of livetime
      addKill((*i)->getFirstTerminator(), VirtReg);
      continue;
    }

    handlePreds(Begin, *i, VirtReg);
  }
}

bool SIFixSGPRLiveness::handleVirtReg(unsigned VirtReg) {

  MachineInstr *Def = MRI->getVRegDef(VirtReg);
  if (!Def || MRI->use_empty(VirtReg))
    return false; // No definition or not used

  MachineBasicBlock *Begin = Def->getParent();
  MachineBasicBlock *End = handleUses(VirtReg, Begin);
  if (Begin == End)
    return false; // Defined and only used in the same block

  if (MD->dominates(Begin, End)) {
    // Lifetime dominate the end node, just kill it here
    addKill(End->getFirstNonPHI(), VirtReg);
  } else {
    // only some predecessors are dominate, handle them separately
    handlePreds(Begin, End, VirtReg);
  }
  return true;
}

bool SIFixSGPRLiveness::runOnMachineFunction(MachineFunction &MF) {
  bool Changes = false;

  MRI = &MF.getRegInfo();
  MD = &getAnalysis<MachineDominatorTree>();
  MPD = &getAnalysis<MachinePostDominatorTree>();

  for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
    unsigned VirtReg = TargetRegisterInfo::index2VirtReg(i);

    const TargetRegisterClass *RegClass = MRI->getRegClass(VirtReg);
    if (!isSGPR(RegClass))
      continue;

    Changes |= handleVirtReg(VirtReg);
  }

  return Changes;
}

FunctionPass *llvm::createSIFixSGPRLivenessPass(TargetMachine &tm) {
  return new SIFixSGPRLiveness(tm);
}