Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Mips/MipsOptimizePICCall.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MipsOptimizePICCall.cpp - Optimize PIC Calls -----------------------===//
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
// This pass eliminates unnecessary instructions that set up $gp and replace
10
// instructions that load target function addresses with copy instructions.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/MipsBaseInfo.h"
15
#include "Mips.h"
16
#include "MipsRegisterInfo.h"
17
#include "MipsSubtarget.h"
18
#include "llvm/ADT/PointerUnion.h"
19
#include "llvm/ADT/ScopedHashTable.h"
20
#include "llvm/ADT/SmallVector.h"
21
#include "llvm/CodeGen/MachineBasicBlock.h"
22
#include "llvm/CodeGen/MachineDominators.h"
23
#include "llvm/CodeGen/MachineFunction.h"
24
#include "llvm/CodeGen/MachineFunctionPass.h"
25
#include "llvm/CodeGen/MachineInstr.h"
26
#include "llvm/CodeGen/MachineInstrBuilder.h"
27
#include "llvm/CodeGen/MachineOperand.h"
28
#include "llvm/CodeGen/MachineRegisterInfo.h"
29
#include "llvm/CodeGen/TargetInstrInfo.h"
30
#include "llvm/CodeGen/TargetOpcodes.h"
31
#include "llvm/CodeGen/TargetRegisterInfo.h"
32
#include "llvm/CodeGen/TargetSubtargetInfo.h"
33
#include "llvm/Support/Allocator.h"
34
#include "llvm/Support/CommandLine.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include "llvm/Support/MachineValueType.h"
37
#include "llvm/Support/RecyclingAllocator.h"
38
#include <cassert>
39
#include <utility>
40
#include <vector>
41
42
using namespace llvm;
43
44
#define DEBUG_TYPE "optimize-mips-pic-call"
45
46
static cl::opt<bool> LoadTargetFromGOT("mips-load-target-from-got",
47
                                       cl::init(true),
48
                                       cl::desc("Load target address from GOT"),
49
                                       cl::Hidden);
50
51
static cl::opt<bool> EraseGPOpnd("mips-erase-gp-opnd",
52
                                 cl::init(true), cl::desc("Erase GP Operand"),
53
                                 cl::Hidden);
54
55
namespace {
56
57
using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>;
58
using CntRegP = std::pair<unsigned, unsigned>;
59
using AllocatorTy = RecyclingAllocator<BumpPtrAllocator,
60
                                       ScopedHashTableVal<ValueType, CntRegP>>;
61
using ScopedHTType = ScopedHashTable<ValueType, CntRegP,
62
                                     DenseMapInfo<ValueType>, AllocatorTy>;
63
64
class MBBInfo {
65
public:
66
  MBBInfo(MachineDomTreeNode *N);
67
68
  const MachineDomTreeNode *getNode() const;
69
  bool isVisited() const;
70
  void preVisit(ScopedHTType &ScopedHT);
71
  void postVisit();
72
73
private:
74
  MachineDomTreeNode *Node;
75
  ScopedHTType::ScopeTy *HTScope;
76
};
77
78
class OptimizePICCall : public MachineFunctionPass {
79
public:
80
2.09k
  OptimizePICCall() : MachineFunctionPass(ID) {}
81
82
15.2k
  StringRef getPassName() const override { return "Mips OptimizePICCall"; }
83
84
  bool runOnMachineFunction(MachineFunction &F) override;
85
86
2.01k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
87
2.01k
    AU.addRequired<MachineDominatorTree>();
88
2.01k
    MachineFunctionPass::getAnalysisUsage(AU);
89
2.01k
  }
90
91
private:
92
  /// Visit MBB.
93
  bool visitNode(MBBInfo &MBBI);
94
95
  /// Test if MI jumps to a function via a register.
96
  ///
97
  /// Also, return the virtual register containing the target function's address
98
  /// and the underlying object in Reg and Val respectively, if the function's
99
  /// address can be resolved lazily.
100
  bool isCallViaRegister(MachineInstr &MI, unsigned &Reg,
101
                         ValueType &Val) const;
102
103
  /// Return the number of instructions that dominate the current
104
  /// instruction and load the function address from object Entry.
105
  unsigned getCount(ValueType Entry);
106
107
  /// Return the destination virtual register of the last instruction
108
  /// that loads from object Entry.
109
  unsigned getReg(ValueType Entry);
110
111
  /// Update ScopedHT.
112
  void incCntAndSetReg(ValueType Entry, unsigned Reg);
113
114
  ScopedHTType ScopedHT;
115
116
  static char ID;
117
};
118
119
} // end of anonymous namespace
120
121
char OptimizePICCall::ID = 0;
122
123
/// Return the first MachineOperand of MI if it is a used virtual register.
124
2.23k
static MachineOperand *getCallTargetRegOpnd(MachineInstr &MI) {
125
2.23k
  if (MI.getNumOperands() == 0)
126
0
    return nullptr;
127
2.23k
128
2.23k
  MachineOperand &MO = MI.getOperand(0);
129
2.23k
130
2.23k
  if (!MO.isReg() || 
!MO.isUse()1.46k
||
131
2.23k
      
!TargetRegisterInfo::isVirtualRegister(MO.getReg())1.40k
)
132
827
    return nullptr;
133
1.40k
134
1.40k
  return &MO;
135
1.40k
}
136
137
/// Return type of register Reg.
138
1.44k
static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF) {
139
1.44k
  const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
140
1.44k
  const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
141
1.44k
  assert(TRI.legalclasstypes_end(*RC) - TRI.legalclasstypes_begin(*RC) == 1);
142
1.44k
  return *TRI.legalclasstypes_begin(*RC);
143
1.44k
}
144
145
/// Do the following transformation:
146
///
147
/// jalr $vreg
148
/// =>
149
/// copy $t9, $vreg
150
/// jalr $t9
151
static void setCallTargetReg(MachineBasicBlock *MBB,
152
1.40k
                             MachineBasicBlock::iterator I) {
153
1.40k
  MachineFunction &MF = *MBB->getParent();
154
1.40k
  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
155
1.40k
  unsigned SrcReg = I->getOperand(0).getReg();
156
1.40k
  unsigned DstReg = getRegTy(SrcReg, MF) == MVT::i32 ? 
Mips::T9753
:
Mips::T9_64651
;
157
1.40k
  BuildMI(*MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), DstReg)
158
1.40k
      .addReg(SrcReg);
159
1.40k
  I->getOperand(0).setReg(DstReg);
160
1.40k
}
161
162
/// Search MI's operands for register GP and erase it.
163
44
static void eraseGPOpnd(MachineInstr &MI) {
164
44
  if (!EraseGPOpnd)
165
0
    return;
166
44
167
44
  MachineFunction &MF = *MI.getParent()->getParent();
168
44
  MVT::SimpleValueType Ty = getRegTy(MI.getOperand(0).getReg(), MF);
169
44
  unsigned Reg = Ty == MVT::i32 ? 
Mips::GP40
:
Mips::GP_644
;
170
44
171
257
  for (unsigned I = 0; I < MI.getNumOperands(); 
++I213
) {
172
257
    MachineOperand &MO = MI.getOperand(I);
173
257
    if (MO.isReg() && 
MO.getReg() == Reg175
) {
174
44
      MI.RemoveOperand(I);
175
44
      return;
176
44
    }
177
257
  }
178
44
179
44
  
llvm_unreachable0
(nullptr);
180
44
}
181
182
14.7k
MBBInfo::MBBInfo(MachineDomTreeNode *N) : Node(N), HTScope(nullptr) {}
183
184
29.5k
const MachineDomTreeNode *MBBInfo::getNode() const { return Node; }
185
186
29.5k
bool MBBInfo::isVisited() const { return HTScope; }
187
188
14.7k
void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
189
14.7k
  HTScope = new ScopedHTType::ScopeTy(ScopedHT);
190
14.7k
}
191
192
14.7k
void MBBInfo::postVisit() {
193
14.7k
  delete HTScope;
194
14.7k
}
195
196
// OptimizePICCall methods.
197
13.2k
bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
198
13.2k
  if (static_cast<const MipsSubtarget &>(F.getSubtarget()).inMips16Mode())
199
380
    return false;
200
12.8k
201
12.8k
  // Do a pre-order traversal of the dominator tree.
202
12.8k
  MachineDominatorTree *MDT = &getAnalysis<MachineDominatorTree>();
203
12.8k
  bool Changed = false;
204
12.8k
205
12.8k
  SmallVector<MBBInfo, 8> WorkList(1, MBBInfo(MDT->getRootNode()));
206
12.8k
207
42.4k
  while (!WorkList.empty()) {
208
29.5k
    MBBInfo &MBBI = WorkList.back();
209
29.5k
210
29.5k
    // If this MBB has already been visited, destroy the scope for the MBB and
211
29.5k
    // pop it from the work list.
212
29.5k
    if (MBBI.isVisited()) {
213
14.7k
      MBBI.postVisit();
214
14.7k
      WorkList.pop_back();
215
14.7k
      continue;
216
14.7k
    }
217
14.7k
218
14.7k
    // Visit the MBB and add its children to the work list.
219
14.7k
    MBBI.preVisit(ScopedHT);
220
14.7k
    Changed |= visitNode(MBBI);
221
14.7k
    const MachineDomTreeNode *Node = MBBI.getNode();
222
14.7k
    const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
223
14.7k
    WorkList.append(Children.begin(), Children.end());
224
14.7k
  }
225
12.8k
226
12.8k
  return Changed;
227
12.8k
}
228
229
14.7k
bool OptimizePICCall::visitNode(MBBInfo &MBBI) {
230
14.7k
  bool Changed = false;
231
14.7k
  MachineBasicBlock *MBB = MBBI.getNode()->getBlock();
232
14.7k
233
146k
  for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
234
132k
       ++I) {
235
132k
    unsigned Reg;
236
132k
    ValueType Entry;
237
132k
238
132k
    // Skip instructions that are not call instructions via registers.
239
132k
    if (!isCallViaRegister(*I, Reg, Entry))
240
130k
      continue;
241
1.40k
242
1.40k
    Changed = true;
243
1.40k
    unsigned N = getCount(Entry);
244
1.40k
245
1.40k
    if (N != 0) {
246
44
      // If a function has been called more than twice, we do not have to emit a
247
44
      // load instruction to get the function address from the GOT, but can
248
44
      // instead reuse the address that has been loaded before.
249
44
      if (N >= 2 && 
!LoadTargetFromGOT16
)
250
2
        getCallTargetRegOpnd(*I)->setReg(getReg(Entry));
251
44
252
44
      // Erase the $gp operand if this isn't the first time a function has
253
44
      // been called. $gp needs to be set up only if the function call can go
254
44
      // through a lazy binding stub.
255
44
      eraseGPOpnd(*I);
256
44
    }
257
1.40k
258
1.40k
    if (Entry)
259
1.10k
      incCntAndSetReg(Entry, Reg);
260
1.40k
261
1.40k
    setCallTargetReg(MBB, I);
262
1.40k
  }
263
14.7k
264
14.7k
  return Changed;
265
14.7k
}
266
267
bool OptimizePICCall::isCallViaRegister(MachineInstr &MI, unsigned &Reg,
268
132k
                                        ValueType &Val) const {
269
132k
  if (!MI.isCall())
270
129k
    return false;
271
2.23k
272
2.23k
  MachineOperand *MO = getCallTargetRegOpnd(MI);
273
2.23k
274
2.23k
  // Return if MI is not a function call via a register.
275
2.23k
  if (!MO)
276
827
    return false;
277
1.40k
278
1.40k
  // Get the instruction that loads the function address from the GOT.
279
1.40k
  Reg = MO->getReg();
280
1.40k
  Val = nullptr;
281
1.40k
  MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
282
1.40k
  MachineInstr *DefMI = MRI.getVRegDef(Reg);
283
1.40k
284
1.40k
  assert(DefMI);
285
1.40k
286
1.40k
  // See if DefMI is an instruction that loads from a GOT entry that holds the
287
1.40k
  // address of a lazy binding stub.
288
1.40k
  if (!DefMI->mayLoad() || 
DefMI->getNumOperands() < 31.11k
)
289
292
    return true;
290
1.11k
291
1.11k
  unsigned Flags = DefMI->getOperand(2).getTargetFlags();
292
1.11k
293
1.11k
  if (Flags != MipsII::MO_GOT_CALL && 
Flags != MipsII::MO_CALL_LO1618
)
294
10
    return true;
295
1.10k
296
1.10k
  // Return the underlying object for the GOT entry in Val.
297
1.10k
  assert(DefMI->hasOneMemOperand());
298
1.10k
  Val = (*DefMI->memoperands_begin())->getValue();
299
1.10k
  if (!Val)
300
1.10k
    Val = (*DefMI->memoperands_begin())->getPseudoValue();
301
1.10k
  return true;
302
1.10k
}
303
304
1.40k
unsigned OptimizePICCall::getCount(ValueType Entry) {
305
1.40k
  return ScopedHT.lookup(Entry).first;
306
1.40k
}
307
308
2
unsigned OptimizePICCall::getReg(ValueType Entry) {
309
2
  unsigned Reg = ScopedHT.lookup(Entry).second;
310
2
  assert(Reg);
311
2
  return Reg;
312
2
}
313
314
1.10k
void OptimizePICCall::incCntAndSetReg(ValueType Entry, unsigned Reg) {
315
1.10k
  CntRegP P = ScopedHT.lookup(Entry);
316
1.10k
  ScopedHT.insert(Entry, std::make_pair(P.first + 1, Reg));
317
1.10k
}
318
319
/// Return an OptimizeCall object.
320
2.09k
FunctionPass *llvm::createMipsOptimizePICCallPass() {
321
2.09k
  return new OptimizePICCall();
322
2.09k
}