[PowerPC] Yet another approach to __tls_get_addr
[oota-llvm.git] / lib / Target / PowerPC / PPCTLSDynamicCall.cpp
1 //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass fixes up GETtls[ld]ADDR[32] machine instructions so that
11 // they read and write GPR3.  These are really call instructions, so
12 // must use the calling convention registers.  This is done in a late
13 // pass so that TLS variable accesses can be fully commoned.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "PPCInstrInfo.h"
18 #include "PPC.h"
19 #include "PPCInstrBuilder.h"
20 #include "PPCTargetMachine.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 #define DEBUG_TYPE "ppc-tls-dynamic-call"
29
30 namespace llvm {
31   void initializePPCTLSDynamicCallPass(PassRegistry&);
32 }
33
34 namespace {
35   // PPCTLSDynamicCall pass - Add copies to and from GPR3 around
36   // GETtls[ld]ADDR[32] machine instructions.  These instructions
37   // are actually call instructions, so the register choice is
38   // constrained.  We delay introducing these copies as late as
39   // possible so that TLS variable accesses can be fully commoned.
40   struct PPCTLSDynamicCall : public MachineFunctionPass {
41     static char ID;
42     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
43       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
44     }
45
46     const PPCTargetMachine *TM;
47     const PPCInstrInfo *TII;
48
49 protected:
50     bool processBlock(MachineBasicBlock &MBB) {
51       bool Changed = false;
52       bool Is64Bit = TM->getSubtargetImpl()->isPPC64();
53
54       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
55            I != IE; ++I) {
56         MachineInstr *MI = I;
57
58         if (MI->getOpcode() != PPC::GETtlsADDR &&
59             MI->getOpcode() != PPC::GETtlsldADDR)
60           continue;
61
62         DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << *MI;);
63
64         unsigned OutReg = MI->getOperand(0).getReg();
65         unsigned InReg  = MI->getOperand(1).getReg();
66         DebugLoc DL = MI->getDebugLoc();
67         unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
68
69         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
70           .addReg(InReg);
71         MI->getOperand(0).setReg(GPR3);
72         MI->getOperand(1).setReg(GPR3);
73         BuildMI(MBB, ++I, DL, TII->get(TargetOpcode::COPY), OutReg)
74           .addReg(GPR3);
75
76         Changed = true;
77       }
78
79       return Changed;
80     }
81
82 public:
83     bool runOnMachineFunction(MachineFunction &MF) override {
84       TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
85       TII = TM->getSubtargetImpl()->getInstrInfo();
86
87       bool Changed = false;
88
89       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
90         MachineBasicBlock &B = *I++;
91         if (processBlock(B))
92           Changed = true;
93       }
94
95       return Changed;
96     }
97
98     void getAnalysisUsage(AnalysisUsage &AU) const override {
99       MachineFunctionPass::getAnalysisUsage(AU);
100     }
101   };
102 }
103
104 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
105                       "PowerPC TLS Dynamic Call Fixup", false, false)
106 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
107                     "PowerPC TLS Dynamic Call Fixup", false, false)
108
109 char PPCTLSDynamicCall::ID = 0;
110 FunctionPass*
111 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }