Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/AArch64/AArch64CleanupLocalDynamicTLSPass.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- AArch64CleanupLocalDynamicTLSPass.cpp ---------------------*- C++ -*-=//
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
// Local-dynamic access to thread-local variables proceeds in three stages.
10
//
11
// 1. The offset of this Module's thread-local area from TPIDR_EL0 is calculated
12
//    in much the same way as a general-dynamic TLS-descriptor access against
13
//    the special symbol _TLS_MODULE_BASE.
14
// 2. The variable's offset from _TLS_MODULE_BASE_ is calculated using
15
//    instructions with "dtprel" modifiers.
16
// 3. These two are added, together with TPIDR_EL0, to obtain the variable's
17
//    true address.
18
//
19
// This is only better than general-dynamic access to the variable if two or
20
// more of the first stage TLS-descriptor calculations can be combined. This
21
// pass looks through a function and performs such combinations.
22
//
23
//===----------------------------------------------------------------------===//
24
#include "AArch64.h"
25
#include "AArch64InstrInfo.h"
26
#include "AArch64MachineFunctionInfo.h"
27
#include "llvm/CodeGen/MachineDominators.h"
28
#include "llvm/CodeGen/MachineFunction.h"
29
#include "llvm/CodeGen/MachineFunctionPass.h"
30
#include "llvm/CodeGen/MachineInstrBuilder.h"
31
#include "llvm/CodeGen/MachineRegisterInfo.h"
32
using namespace llvm;
33
34
13.8k
#define TLSCLEANUP_PASS_NAME "AArch64 Local Dynamic TLS Access Clean-up"
35
36
namespace {
37
struct LDTLSCleanup : public MachineFunctionPass {
38
  static char ID;
39
997
  LDTLSCleanup() : MachineFunctionPass(ID) {
40
997
    initializeLDTLSCleanupPass(*PassRegistry::getPassRegistry());
41
997
  }
42
43
13.7k
  bool runOnMachineFunction(MachineFunction &MF) override {
44
13.7k
    if (skipFunction(MF.getFunction()))
45
7
      return false;
46
13.7k
47
13.7k
    AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
48
13.7k
    if (AFI->getNumLocalDynamicTLSAccesses() < 2) {
49
13.7k
      // No point folding accesses if there isn't at least two.
50
13.7k
      return false;
51
13.7k
    }
52
3
53
3
    MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
54
3
    return VisitNode(DT->getRootNode(), 0);
55
3
  }
56
57
  // Visit the dominator subtree rooted at Node in pre-order.
58
  // If TLSBaseAddrReg is non-null, then use that to replace any
59
  // TLS_base_addr instructions. Otherwise, create the register
60
  // when the first such instruction is seen, and then use it
61
  // as we encounter more instructions.
62
3
  bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
63
3
    MachineBasicBlock *BB = Node->getBlock();
64
3
    bool Changed = false;
65
3
66
3
    // Traverse the current block.
67
45
    for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
68
42
         ++I) {
69
42
      switch (I->getOpcode()) {
70
42
      case AArch64::TLSDESC_CALLSEQ:
71
6
        // Make sure it's a local dynamic access.
72
6
        if (!I->getOperand(0).isSymbol() ||
73
6
            strcmp(I->getOperand(0).getSymbolName(), "_TLS_MODULE_BASE_"))
74
0
          break;
75
6
76
6
        if (TLSBaseAddrReg)
77
3
          I = replaceTLSBaseAddrCall(*I, TLSBaseAddrReg);
78
3
        else
79
3
          I = setRegister(*I, &TLSBaseAddrReg);
80
6
        Changed = true;
81
6
        break;
82
36
      default:
83
36
        break;
84
42
      }
85
42
    }
86
3
87
3
    // Visit the children of this block in the dominator tree.
88
3
    for (MachineDomTreeNode *N : *Node) {
89
0
      Changed |= VisitNode(N, TLSBaseAddrReg);
90
0
    }
91
3
92
3
    return Changed;
93
3
  }
94
95
  // Replace the TLS_base_addr instruction I with a copy from
96
  // TLSBaseAddrReg, returning the new instruction.
97
  MachineInstr *replaceTLSBaseAddrCall(MachineInstr &I,
98
3
                                       unsigned TLSBaseAddrReg) {
99
3
    MachineFunction *MF = I.getParent()->getParent();
100
3
    const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
101
3
102
3
    // Insert a Copy from TLSBaseAddrReg to x0, which is where the rest of the
103
3
    // code sequence assumes the address will be.
104
3
    MachineInstr *Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
105
3
                                 TII->get(TargetOpcode::COPY), AArch64::X0)
106
3
                             .addReg(TLSBaseAddrReg);
107
3
108
3
    // Erase the TLS_base_addr instruction.
109
3
    I.eraseFromParent();
110
3
111
3
    return Copy;
112
3
  }
113
114
  // Create a virtual register in *TLSBaseAddrReg, and populate it by
115
  // inserting a copy instruction after I. Returns the new instruction.
116
3
  MachineInstr *setRegister(MachineInstr &I, unsigned *TLSBaseAddrReg) {
117
3
    MachineFunction *MF = I.getParent()->getParent();
118
3
    const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
119
3
120
3
    // Create a virtual register for the TLS base address.
121
3
    MachineRegisterInfo &RegInfo = MF->getRegInfo();
122
3
    *TLSBaseAddrReg = RegInfo.createVirtualRegister(&AArch64::GPR64RegClass);
123
3
124
3
    // Insert a copy from X0 to TLSBaseAddrReg for later.
125
3
    MachineInstr *Copy =
126
3
        BuildMI(*I.getParent(), ++I.getIterator(), I.getDebugLoc(),
127
3
                TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
128
3
            .addReg(AArch64::X0);
129
3
130
3
    return Copy;
131
3
  }
132
133
13.8k
  StringRef getPassName() const override { return TLSCLEANUP_PASS_NAME; }
134
135
991
  void getAnalysisUsage(AnalysisUsage &AU) const override {
136
991
    AU.setPreservesCFG();
137
991
    AU.addRequired<MachineDominatorTree>();
138
991
    MachineFunctionPass::getAnalysisUsage(AU);
139
991
  }
140
};
141
}
142
143
INITIALIZE_PASS(LDTLSCleanup, "aarch64-local-dynamic-tls-cleanup",
144
                TLSCLEANUP_PASS_NAME, false, false)
145
146
char LDTLSCleanup::ID = 0;
147
997
FunctionPass *llvm::createAArch64CleanupLocalDynamicTLSPass() {
148
997
  return new LDTLSCleanup();
149
997
}