summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/R600/SIFoldOperands.cpp
blob: 761e8665e3a5a32a2f170cd3d8e3305bef4125d1 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===-- SIFoldOperands.cpp - Fold operands --- ----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
/// \file
//===----------------------------------------------------------------------===//
//

#include "AMDGPU.h"
#include "AMDGPUSubtarget.h"
#include "SIInstrInfo.h"
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetMachine.h"

#define DEBUG_TYPE "si-fold-operands"
using namespace llvm;

namespace {

class SIFoldOperands : public MachineFunctionPass {
public:
  static char ID;

public:
  SIFoldOperands() : MachineFunctionPass(ID) {
    initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
  }

  bool runOnMachineFunction(MachineFunction &MF) override;

  const char *getPassName() const override {
    return "SI Fold Operands";
  }

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

} // End anonymous namespace.

INITIALIZE_PASS_BEGIN(SIFoldOperands, DEBUG_TYPE,
                      "SI Fold Operands", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
INITIALIZE_PASS_END(SIFoldOperands, DEBUG_TYPE,
                    "SI Fold Operands", false, false)

char SIFoldOperands::ID = 0;

char &llvm::SIFoldOperandsID = SIFoldOperands::ID;

FunctionPass *llvm::createSIFoldOperandsPass() {
  return new SIFoldOperands();
}

static bool isSafeToFold(unsigned Opcode) {
  switch(Opcode) {
  case AMDGPU::V_MOV_B32_e32:
  case AMDGPU::V_MOV_B32_e64:
  case AMDGPU::S_MOV_B32:
  case AMDGPU::S_MOV_B64:
  case AMDGPU::COPY:
    return true;
  default:
    return false;
  }
}

static bool updateOperand(MachineInstr *MI, unsigned OpNo,
                          const MachineOperand &New,
                          const TargetRegisterInfo &TRI) {
  MachineOperand &Old = MI->getOperand(OpNo);
  assert(Old.isReg());

  if (New.isImm()) {
    Old.ChangeToImmediate(New.getImm());
    return true;
  }

  if (New.isFPImm()) {
    Old.ChangeToFPImmediate(New.getFPImm());
    return true;
  }

  if (New.isReg())  {
    if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
        TargetRegisterInfo::isVirtualRegister(New.getReg())) {
      Old.substVirtReg(New.getReg(), New.getSubReg(), TRI);
      return true;
    }
  }

  // FIXME: Handle physical registers.

  return false;
}

bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
  MachineRegisterInfo &MRI = MF.getRegInfo();
  const SIInstrInfo *TII =
      static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
  const SIRegisterInfo &TRI = TII->getRegisterInfo();

  for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
                                                  BI != BE; ++BI) {

    MachineBasicBlock &MBB = *BI;
    MachineBasicBlock::iterator I, Next;
    for (I = MBB.begin(); I != MBB.end(); I = Next) {
      Next = std::next(I);
      MachineInstr &MI = *I;

      if (!isSafeToFold(MI.getOpcode()))
        continue;

      MachineOperand &OpToFold = MI.getOperand(1);

      // FIXME: Fold operands with subregs.
      if (OpToFold.isReg() &&
          (!TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()) ||
           OpToFold.getSubReg()))
        continue;

      std::vector<std::pair<MachineInstr *, unsigned>> FoldList;
      for (MachineRegisterInfo::use_iterator
           Use = MRI.use_begin(MI.getOperand(0).getReg()), E = MRI.use_end();
           Use != E; ++Use) {

        MachineInstr *UseMI = Use->getParent();
        const MachineOperand &UseOp = UseMI->getOperand(Use.getOperandNo());

        // FIXME: Fold operands with subregs.
        if (UseOp.isReg() && UseOp.getSubReg()) {
          continue;
        }

        // In order to fold immediates into copies, we need to change the
        // copy to a MOV.
        if ((OpToFold.isImm() || OpToFold.isFPImm()) &&
             UseMI->getOpcode() == AMDGPU::COPY) {
          const TargetRegisterClass *TRC =
              MRI.getRegClass(UseMI->getOperand(0).getReg());

          if (TRC->getSize() == 4) {
            if (TRI.isSGPRClass(TRC))
              UseMI->setDesc(TII->get(AMDGPU::S_MOV_B32));
            else
              UseMI->setDesc(TII->get(AMDGPU::V_MOV_B32_e32));
          } else if (TRC->getSize() == 8 && TRI.isSGPRClass(TRC)) {
            UseMI->setDesc(TII->get(AMDGPU::S_MOV_B64));
          } else {
            continue;
          }
        }

        const MCInstrDesc &UseDesc = UseMI->getDesc();

        // Don't fold into target independent nodes.  Target independent opcodes
        // don't have defined register classes.
        if (UseDesc.isVariadic() ||
            UseDesc.OpInfo[Use.getOperandNo()].RegClass == -1)
          continue;

        // Normal substitution
        if (TII->isOperandLegal(UseMI, Use.getOperandNo(), &OpToFold)) {
          FoldList.push_back(std::make_pair(UseMI, Use.getOperandNo()));
          continue;
        }

        // FIXME: We could commute the instruction to create more opportunites
        // for folding.  This will only be useful if we have 32-bit instructions.

        // FIXME: We could try to change the instruction from 64-bit to 32-bit
        // to enable more folding opportunites.  The shrink operands pass
        // already does this.
      }

      for (std::pair<MachineInstr *, unsigned> Fold : FoldList) {
        if (updateOperand(Fold.first, Fold.second, OpToFold, TRI)) {
          // Clear kill flags.
          if (OpToFold.isReg())
            OpToFold.setIsKill(false);
          DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
                Fold.second << " of " << *Fold.first << '\n');
        }
      }
    }
  }
  return false;
}
OpenPOWER on IntegriCloud