summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/radeon/AMDILMachinePeephole.cpp
blob: 5cb988785e24963cc871778beff540dc6e66a6d5 (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
//===-- AMDILMachinePeephole.cpp - AMDIL Machine Peephole Pass -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//==-----------------------------------------------------------------------===//


#include "AMDIL.h"
#include "AMDILInstrInfo.h"
#include "AMDILSubtarget.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetMachine.h"

using namespace llvm;
namespace
{
  class AMDILMachinePeephole : public MachineFunctionPass
  {
    public:
      static char ID;
      AMDILMachinePeephole(TargetMachine &tm AMDIL_OPT_LEVEL_DECL);
      //virtual ~AMDILMachinePeephole();
      virtual const char*
        getPassName() const;
      virtual bool
        runOnMachineFunction(MachineFunction &MF);
    private:
      void insertFence(MachineBasicBlock::iterator &MIB);
      TargetMachine &TM;
      bool mDebug;
  }; // AMDILMachinePeephole
  char AMDILMachinePeephole::ID = 0;
} // anonymous namespace

namespace llvm
{
  FunctionPass*
    createAMDILMachinePeephole(TargetMachine &tm AMDIL_OPT_LEVEL_DECL)
    {
      return new AMDILMachinePeephole(tm AMDIL_OPT_LEVEL_VAR);
    }
} // llvm namespace

AMDILMachinePeephole::AMDILMachinePeephole(TargetMachine &tm AMDIL_OPT_LEVEL_DECL)
  : MachineFunctionPass(ID), TM(tm)
{
  mDebug = false;
}

bool
AMDILMachinePeephole::runOnMachineFunction(MachineFunction &MF)
{
  bool Changed = false;
  const AMDILSubtarget *STM = &TM.getSubtarget<AMDILSubtarget>();
  const AMDILInstrInfo * AMDILII =
                         static_cast<const AMDILInstrInfo *>(TM.getInstrInfo());
  for (MachineFunction::iterator MBB = MF.begin(), MBE = MF.end();
      MBB != MBE; ++MBB) {
    MachineBasicBlock *mb = MBB;
    for (MachineBasicBlock::iterator MIB = mb->begin(), MIE = mb->end();
        MIB != MIE; ++MIB) {
      MachineInstr *mi = MIB;
      const char * name;
      name = TM.getInstrInfo()->getName(mi->getOpcode());
      switch (mi->getOpcode()) {
        default:
          if (AMDILII->isAtomicInst(mi)) {
            // If we don't support the hardware accellerated address spaces,
            // then the atomic needs to be transformed to the global atomic.
            if (strstr(name, "_L_")
                && STM->device()->usesSoftware(AMDILDeviceInfo::LocalMem)) {
              BuildMI(*mb, MIB, mi->getDebugLoc(), 
                  TM.getInstrInfo()->get(AMDIL::ADD_i32), AMDIL::R1011)
                .addReg(mi->getOperand(1).getReg())
                .addReg(AMDIL::T2);
              mi->getOperand(1).setReg(AMDIL::R1011);
              mi->setDesc(
                  TM.getInstrInfo()->get(
                    (mi->getOpcode() - AMDIL::ATOM_L_ADD) + AMDIL::ATOM_G_ADD));
            } else if (strstr(name, "_R_")
                && STM->device()->usesSoftware(AMDILDeviceInfo::RegionMem)) {
              assert(!"Software region memory is not supported!");
              mi->setDesc(
                  TM.getInstrInfo()->get(
                    (mi->getOpcode() - AMDIL::ATOM_R_ADD) + AMDIL::ATOM_G_ADD));
            }
          } else if ((AMDILII->isLoadInst(mi) || AMDILII->isStoreInst(mi))
                     && AMDILII->isVolatileInst(mi)) {
            insertFence(MIB);
          }
          continue;
          break;
        case AMDIL::USHR_i16:
        case AMDIL::USHR_v2i16:
        case AMDIL::USHR_v4i16:
        case AMDIL::USHRVEC_i16:
        case AMDIL::USHRVEC_v2i16:
        case AMDIL::USHRVEC_v4i16:
          if (TM.getSubtarget<AMDILSubtarget>()
              .device()->usesSoftware(AMDILDeviceInfo::ShortOps)) {
            unsigned lReg = MF.getRegInfo()
              .createVirtualRegister(&AMDIL::GPRI32RegClass);
            unsigned Reg = MF.getRegInfo()
              .createVirtualRegister(&AMDIL::GPRV4I32RegClass);
            BuildMI(*mb, MIB, mi->getDebugLoc(),
                TM.getInstrInfo()->get(AMDIL::LOADCONST_i32),
                lReg).addImm(0xFFFF);
            BuildMI(*mb, MIB, mi->getDebugLoc(),
                TM.getInstrInfo()->get(AMDIL::BINARY_AND_v4i32),
                Reg)
              .addReg(mi->getOperand(1).getReg())
              .addReg(lReg);
            mi->getOperand(1).setReg(Reg);
          }
          break;
        case AMDIL::USHR_i8:
        case AMDIL::USHR_v2i8:
        case AMDIL::USHR_v4i8:
        case AMDIL::USHRVEC_i8:
        case AMDIL::USHRVEC_v2i8:
        case AMDIL::USHRVEC_v4i8:
          if (TM.getSubtarget<AMDILSubtarget>()
              .device()->usesSoftware(AMDILDeviceInfo::ByteOps)) {
            unsigned lReg = MF.getRegInfo()
              .createVirtualRegister(&AMDIL::GPRI32RegClass);
            unsigned Reg = MF.getRegInfo()
              .createVirtualRegister(&AMDIL::GPRV4I32RegClass);
            BuildMI(*mb, MIB, mi->getDebugLoc(),
                TM.getInstrInfo()->get(AMDIL::LOADCONST_i32),
                lReg).addImm(0xFF);
            BuildMI(*mb, MIB, mi->getDebugLoc(),
                TM.getInstrInfo()->get(AMDIL::BINARY_AND_v4i32),
                Reg)
              .addReg(mi->getOperand(1).getReg())
              .addReg(lReg);
            mi->getOperand(1).setReg(Reg);
          }
          break;
      }
    }
  }
  return Changed;
}

const char*
AMDILMachinePeephole::getPassName() const
{
  return "AMDIL Generic Machine Peephole Optimization Pass";
}

void
AMDILMachinePeephole::insertFence(MachineBasicBlock::iterator &MIB)
{
  MachineInstr *MI = MIB;
  MachineInstr *fence = BuildMI(*(MI->getParent()->getParent()),
        MI->getDebugLoc(),
        TM.getInstrInfo()->get(AMDIL::FENCE)).addReg(1);

  MI->getParent()->insert(MIB, fence);
  fence = BuildMI(*(MI->getParent()->getParent()),
        MI->getDebugLoc(),
        TM.getInstrInfo()->get(AMDIL::FENCE)).addReg(1);
  MIB = MI->getParent()->insertAfter(MIB, fence);
}