aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm/lib/Target/Hexagon/RDFCopy.cpp
blob: c547c7195075dc423a5602a219cead7087a25d47 (plain) (blame)
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
//===--- RDFCopy.cpp ------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Simplistic RDF-based copy propagation.

#include "RDFCopy.h"
#include "RDFGraph.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/Support/CommandLine.h"

#include <atomic>

#ifndef NDEBUG
static cl::opt<unsigned> CpLimit("rdf-cp-limit", cl::init(0), cl::Hidden);
static unsigned CpCount = 0;
#endif

using namespace llvm;
using namespace rdf;

void CopyPropagation::recordCopy(NodeAddr<StmtNode*> SA, MachineInstr *MI) {
  assert(MI->getOpcode() == TargetOpcode::COPY);
  const MachineOperand &Op0 = MI->getOperand(0), &Op1 = MI->getOperand(1);
  RegisterRef DstR = { Op0.getReg(), Op0.getSubReg() };
  RegisterRef SrcR = { Op1.getReg(), Op1.getSubReg() };
  auto FS = DefM.find(SrcR);
  if (FS == DefM.end() || FS->second.empty())
    return;
  Copies.push_back(SA.Id);
  RDefMap[SrcR][SA.Id] = FS->second.top()->Id;
  // Insert DstR into the map.
  RDefMap[DstR];
}


void CopyPropagation::updateMap(NodeAddr<InstrNode*> IA) {
  RegisterSet RRs;
  for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
    RRs.insert(RA.Addr->getRegRef());
  bool Common = false;
  for (auto &R : RDefMap) {
    if (!RRs.count(R.first))
      continue;
    Common = true;
    break;
  }
  if (!Common)
    return;

  for (auto &R : RDefMap) {
    if (!RRs.count(R.first))
      continue;
    auto F = DefM.find(R.first);
    if (F == DefM.end() || F->second.empty())
      continue;
    R.second[IA.Id] = F->second.top()->Id;
  }
}


bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
  bool Changed = false;
  auto BA = DFG.getFunc().Addr->findBlock(B, DFG);
  DFG.markBlock(BA.Id, DefM);

  for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
    if (DFG.IsCode<NodeAttrs::Stmt>(IA)) {
      NodeAddr<StmtNode*> SA = IA;
      MachineInstr *MI = SA.Addr->getCode();
      if (MI->isCopy())
        recordCopy(SA, MI);
    }

    updateMap(IA);
    DFG.pushDefs(IA, DefM);
  }

  MachineDomTreeNode *N = MDT.getNode(B);
  for (auto I : *N)
    Changed |= scanBlock(I->getBlock());

  DFG.releaseBlock(BA.Id, DefM);
  return Changed;
}


bool CopyPropagation::run() {
  scanBlock(&DFG.getMF().front());

  if (trace()) {
    dbgs() << "Copies:\n";
    for (auto I : Copies)
      dbgs() << *DFG.addr<StmtNode*>(I).Addr->getCode();
    dbgs() << "\nRDef map:\n";
    for (auto R : RDefMap) {
      dbgs() << Print<RegisterRef>(R.first, DFG) << " -> {";
      for (auto &M : R.second)
        dbgs() << ' ' << Print<NodeId>(M.first, DFG) << ':'
               << Print<NodeId>(M.second, DFG);
      dbgs() << " }\n";
    }
  }

  bool Changed = false;
  NodeSet Deleted;
#ifndef NDEBUG
  bool HasLimit = CpLimit.getNumOccurrences() > 0;
#endif

  for (auto I : Copies) {
#ifndef NDEBUG
    if (HasLimit && CpCount >= CpLimit)
      break;
#endif
    if (Deleted.count(I))
      continue;
    auto SA = DFG.addr<InstrNode*>(I);
    NodeList Ds = SA.Addr->members_if(DFG.IsDef, DFG);
    if (Ds.size() != 1)
      continue;
    NodeAddr<DefNode*> DA = Ds[0];
    RegisterRef DR0 = DA.Addr->getRegRef();
    NodeList Us = SA.Addr->members_if(DFG.IsUse, DFG);
    if (Us.size() != 1)
      continue;
    NodeAddr<UseNode*> UA0 = Us[0];
    RegisterRef UR0 = UA0.Addr->getRegRef();
    NodeId RD0 = UA0.Addr->getReachingDef();

    for (NodeId N = DA.Addr->getReachedUse(), NextN; N; N = NextN) {
      auto UA = DFG.addr<UseNode*>(N);
      NextN = UA.Addr->getSibling();
      uint16_t F = UA.Addr->getFlags();
      if ((F & NodeAttrs::PhiRef) || (F & NodeAttrs::Fixed))
        continue;
      if (UA.Addr->getRegRef() != DR0)
        continue;
      NodeAddr<InstrNode*> IA = UA.Addr->getOwner(DFG);
      assert(DFG.IsCode<NodeAttrs::Stmt>(IA));
      MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
      if (RDefMap[UR0][IA.Id] != RD0)
        continue;
      MachineOperand &Op = UA.Addr->getOp();
      if (Op.isTied())
        continue;
      if (trace()) {
        dbgs() << "can replace " << Print<RegisterRef>(DR0, DFG)
               << " with " << Print<RegisterRef>(UR0, DFG) << " in "
               << *NodeAddr<StmtNode*>(IA).Addr->getCode();
      }

      Op.setReg(UR0.Reg);
      Op.setSubReg(UR0.Sub);
      Changed = true;
#ifndef NDEBUG
      if (HasLimit && CpCount >= CpLimit)
        break;
      CpCount++;
#endif

      if (MI->isCopy()) {
        MachineOperand &Op0 = MI->getOperand(0), &Op1 = MI->getOperand(1);
        if (Op0.getReg() == Op1.getReg() && Op0.getSubReg() == Op1.getSubReg())
          MI->eraseFromParent();
        Deleted.insert(IA.Id);
      }
    }
  }

  return Changed;
}