summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp
blob: b0e1dfa52a4de16defc62861590ac04282671cd6 (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
//===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass fixes up GETtls[ld]ADDR[32] machine instructions so that
// they read and write GPR3.  These are really call instructions, so
// must use the calling convention registers.  This is done in a late
// pass so that TLS variable accesses can be fully commoned.
//
//===----------------------------------------------------------------------===//

#include "PPCInstrInfo.h"
#include "PPC.h"
#include "PPCInstrBuilder.h"
#include "PPCTargetMachine.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

#define DEBUG_TYPE "ppc-tls-dynamic-call"

namespace llvm {
  void initializePPCTLSDynamicCallPass(PassRegistry&);
}

namespace {
  // PPCTLSDynamicCall pass - Add copies to and from GPR3 around
  // GETtls[ld]ADDR[32] machine instructions.  These instructions
  // are actually call instructions, so the register choice is
  // constrained.  We delay introducing these copies as late as
  // possible so that TLS variable accesses can be fully commoned.
  struct PPCTLSDynamicCall : public MachineFunctionPass {
    static char ID;
    PPCTLSDynamicCall() : MachineFunctionPass(ID) {
      initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
    }

    const PPCTargetMachine *TM;
    const PPCInstrInfo *TII;

protected:
    bool processBlock(MachineBasicBlock &MBB) {
      bool Changed = false;
      bool Is64Bit = TM->getSubtargetImpl()->isPPC64();

      for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
           I != IE; ++I) {
        MachineInstr *MI = I;

        if (MI->getOpcode() != PPC::GETtlsADDR &&
            MI->getOpcode() != PPC::GETtlsldADDR)
          continue;

        DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << *MI;);

        unsigned OutReg = MI->getOperand(0).getReg();
        unsigned InReg  = MI->getOperand(1).getReg();
        DebugLoc DL = MI->getDebugLoc();
        unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;

        BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
          .addReg(InReg);
        MI->getOperand(0).setReg(GPR3);
        MI->getOperand(1).setReg(GPR3);
        BuildMI(MBB, ++I, DL, TII->get(TargetOpcode::COPY), OutReg)
          .addReg(GPR3);

        Changed = true;
      }

      return Changed;
    }

public:
    bool runOnMachineFunction(MachineFunction &MF) override {
      TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
      TII = TM->getSubtargetImpl()->getInstrInfo();

      bool Changed = false;

      for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
        MachineBasicBlock &B = *I++;
        if (processBlock(B))
          Changed = true;
      }

      return Changed;
    }

    void getAnalysisUsage(AnalysisUsage &AU) const override {
      MachineFunctionPass::getAnalysisUsage(AU);
    }
  };
}

INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
                      "PowerPC TLS Dynamic Call Fixup", false, false)
INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
                    "PowerPC TLS Dynamic Call Fixup", false, false)

char PPCTLSDynamicCall::ID = 0;
FunctionPass*
llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
OpenPOWER on IntegriCloud