Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Hexagon/RDFCopy.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- RDFCopy.cpp --------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// RDF-based copy propagation.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "RDFCopy.h"
14
#include "RDFGraph.h"
15
#include "RDFLiveness.h"
16
#include "RDFRegisters.h"
17
#include "llvm/CodeGen/MachineDominators.h"
18
#include "llvm/CodeGen/MachineInstr.h"
19
#include "llvm/CodeGen/MachineOperand.h"
20
#include "llvm/CodeGen/MachineRegisterInfo.h"
21
#include "llvm/CodeGen/TargetOpcodes.h"
22
#include "llvm/CodeGen/TargetRegisterInfo.h"
23
#include "llvm/MC/MCRegisterInfo.h"
24
#include "llvm/Support/CommandLine.h"
25
#include "llvm/Support/Debug.h"
26
#include "llvm/Support/ErrorHandling.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <cassert>
29
#include <cstdint>
30
#include <utility>
31
32
using namespace llvm;
33
using namespace rdf;
34
35
#ifndef NDEBUG
36
static cl::opt<unsigned> CpLimit("rdf-cp-limit", cl::init(0), cl::Hidden);
37
static unsigned CpCount = 0;
38
#endif
39
40
28.6k
bool CopyPropagation::interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) {
41
28.6k
  unsigned Opc = MI->getOpcode();
42
28.6k
  switch (Opc) {
43
28.6k
    case TargetOpcode::COPY: {
44
1.76k
      const MachineOperand &Dst = MI->getOperand(0);
45
1.76k
      const MachineOperand &Src = MI->getOperand(1);
46
1.76k
      RegisterRef DstR = DFG.makeRegRef(Dst.getReg(), Dst.getSubReg());
47
1.76k
      RegisterRef SrcR = DFG.makeRegRef(Src.getReg(), Src.getSubReg());
48
1.76k
      assert(TargetRegisterInfo::isPhysicalRegister(DstR.Reg));
49
1.76k
      assert(TargetRegisterInfo::isPhysicalRegister(SrcR.Reg));
50
1.76k
      const TargetRegisterInfo &TRI = DFG.getTRI();
51
1.76k
      if (TRI.getMinimalPhysRegClass(DstR.Reg) !=
52
1.76k
          TRI.getMinimalPhysRegClass(SrcR.Reg))
53
846
        return false;
54
919
      EM.insert(std::make_pair(DstR, SrcR));
55
919
      return true;
56
919
    }
57
919
    case TargetOpcode::REG_SEQUENCE:
58
0
      llvm_unreachable("Unexpected REG_SEQUENCE");
59
26.8k
  }
60
26.8k
  return false;
61
26.8k
}
62
63
920
void CopyPropagation::recordCopy(NodeAddr<StmtNode*> SA, EqualityMap &EM) {
64
920
  CopyMap.insert(std::make_pair(SA.Id, EM));
65
920
  Copies.push_back(SA.Id);
66
920
}
67
68
5.54k
bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
69
5.54k
  bool Changed = false;
70
5.54k
  NodeAddr<BlockNode*> BA = DFG.findBlock(B);
71
5.54k
72
41.3k
  for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
73
41.3k
    if (DFG.IsCode<NodeAttrs::Stmt>(IA)) {
74
29.5k
      NodeAddr<StmtNode*> SA = IA;
75
29.5k
      EqualityMap EM;
76
29.5k
      if (interpretAsCopy(SA.Addr->getCode(), EM))
77
920
        recordCopy(SA, EM);
78
29.5k
    }
79
41.3k
  }
80
5.54k
81
5.54k
  MachineDomTreeNode *N = MDT.getNode(B);
82
5.54k
  for (auto I : *N)
83
2.19k
    Changed |= scanBlock(I->getBlock());
84
5.54k
85
5.54k
  return Changed;
86
5.54k
}
87
88
NodeId CopyPropagation::getLocalReachingDef(RegisterRef RefRR,
89
1.22k
      NodeAddr<InstrNode*> IA) {
90
1.22k
  NodeAddr<RefNode*> RA = L.getNearestAliasedRef(RefRR, IA);
91
1.22k
  if (RA.Id != 0) {
92
1.22k
    if (RA.Addr->getKind() == NodeAttrs::Def)
93
991
      return RA.Id;
94
230
    assert(RA.Addr->getKind() == NodeAttrs::Use);
95
230
    if (NodeId RD = RA.Addr->getReachingDef())
96
229
      return RD;
97
2
  }
98
2
  return 0;
99
2
}
100
101
3.34k
bool CopyPropagation::run() {
102
3.34k
  scanBlock(&DFG.getMF().front());
103
3.34k
104
3.34k
  if (trace()) {
105
0
    dbgs() << "Copies:\n";
106
0
    for (NodeId I : Copies) {
107
0
      dbgs() << "Instr: " << *DFG.addr<StmtNode*>(I).Addr->getCode();
108
0
      dbgs() << "   eq: {";
109
0
      for (auto J : CopyMap[I])
110
0
        dbgs() << ' ' << Print<RegisterRef>(J.first, DFG) << '='
111
0
               << Print<RegisterRef>(J.second, DFG);
112
0
      dbgs() << " }\n";
113
0
    }
114
0
  }
115
3.34k
116
3.34k
  bool Changed = false;
117
#ifndef NDEBUG
118
  bool HasLimit = CpLimit.getNumOccurrences() > 0;
119
#endif
120
121
3.34k
  auto MinPhysReg = [this] (RegisterRef RR) -> unsigned {
122
3
    const TargetRegisterInfo &TRI = DFG.getTRI();
123
3
    const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
124
3
    if ((RC.LaneMask & RR.Mask) == RC.LaneMask)
125
3
      return RR.Reg;
126
0
    for (MCSubRegIndexIterator S(RR.Reg, &TRI); S.isValid(); ++S)
127
0
      if (RR.Mask == TRI.getSubRegIndexLaneMask(S.getSubRegIndex()))
128
0
        return S.getSubReg();
129
0
    llvm_unreachable("Should have found a register");
130
0
    return 0;
131
0
  };
132
3.34k
133
3.34k
  for (NodeId C : Copies) {
134
#ifndef NDEBUG
135
    if (HasLimit && CpCount >= CpLimit)
136
      break;
137
#endif
138
    auto SA = DFG.addr<InstrNode*>(C);
139
920
    auto FS = CopyMap.find(SA.Id);
140
920
    if (FS == CopyMap.end())
141
0
      continue;
142
920
143
920
    EqualityMap &EM = FS->second;
144
972
    for (NodeAddr<DefNode*> DA : SA.Addr->members_if(DFG.IsDef, DFG)) {
145
972
      RegisterRef DR = DA.Addr->getRegRef(DFG);
146
972
      auto FR = EM.find(DR);
147
972
      if (FR == EM.end())
148
1
        continue;
149
971
      RegisterRef SR = FR->second;
150
971
      if (DR == SR)
151
0
        continue;
152
971
153
971
      NodeId AtCopy = getLocalReachingDef(SR, SA);
154
971
155
2.13k
      for (NodeId N = DA.Addr->getReachedUse(), NextN; N; 
N = NextN1.16k
) {
156
1.16k
        auto UA = DFG.addr<UseNode*>(N);
157
1.16k
        NextN = UA.Addr->getSibling();
158
1.16k
        uint16_t F = UA.Addr->getFlags();
159
1.16k
        if ((F & NodeAttrs::PhiRef) || 
(F & NodeAttrs::Fixed)821
)
160
830
          continue;
161
332
        if (UA.Addr->getRegRef(DFG) != DR)
162
81
          continue;
163
251
164
251
        NodeAddr<InstrNode*> IA = UA.Addr->getOwner(DFG);
165
251
        assert(DFG.IsCode<NodeAttrs::Stmt>(IA));
166
251
        NodeId AtUse = getLocalReachingDef(SR, IA);
167
251
        if (AtCopy != AtUse)
168
181
          continue;
169
70
170
70
        MachineOperand &Op = UA.Addr->getOp();
171
70
        if (Op.isTied())
172
67
          continue;
173
3
        if (trace()) {
174
0
          dbgs() << "Can replace " << Print<RegisterRef>(DR, DFG)
175
0
                 << " with " << Print<RegisterRef>(SR, DFG) << " in "
176
0
                 << *NodeAddr<StmtNode*>(IA).Addr->getCode();
177
0
        }
178
3
179
3
        unsigned NewReg = MinPhysReg(SR);
180
3
        Op.setReg(NewReg);
181
3
        Op.setSubReg(0);
182
3
        DFG.unlinkUse(UA, false);
183
3
        if (AtCopy != 0) {
184
2
          UA.Addr->linkToDef(UA.Id, DFG.addr<DefNode*>(AtCopy));
185
2
        } else {
186
1
          UA.Addr->setReachingDef(0);
187
1
          UA.Addr->setSibling(0);
188
1
        }
189
3
190
3
        Changed = true;
191
  #ifndef NDEBUG
192
        if (HasLimit && CpCount >= CpLimit)
193
          break;
194
        CpCount++;
195
  #endif
196
197
3
        auto FC = CopyMap.find(IA.Id);
198
3
        if (FC != CopyMap.end()) {
199
0
          // Update the EM map in the copy's entry.
200
0
          auto &M = FC->second;
201
0
          for (auto &J : M) {
202
0
            if (J.second != DR)
203
0
              continue;
204
0
            J.second = SR;
205
0
            break;
206
0
          }
207
0
        }
208
3
      } // for (N in reached-uses)
209
971
    } // for (DA in defs)
210
920
  } // for (C in Copies)
211
3.34k
212
3.34k
  return Changed;
213
3.34k
}