[Orc] Add a Kaleidoscope/Orc tutorial demonstrating lazy-codegen.
[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             MI->getOpcode() != PPC::GETtlsADDR32 &&
61             MI->getOpcode() != PPC::GETtlsldADDR32)
62           continue;
63
64         DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << *MI;);
65
66         unsigned OutReg = MI->getOperand(0).getReg();
67         unsigned InReg  = MI->getOperand(1).getReg();
68         DebugLoc DL = MI->getDebugLoc();
69         unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
70
71         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
72           .addReg(InReg);
73         MI->getOperand(0).setReg(GPR3);
74         MI->getOperand(1).setReg(GPR3);
75         BuildMI(MBB, ++I, DL, TII->get(TargetOpcode::COPY), OutReg)
76           .addReg(GPR3);
77
78         Changed = true;
79       }
80
81       return Changed;
82     }
83
84 public:
85     bool runOnMachineFunction(MachineFunction &MF) override {
86       TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
87       TII = TM->getSubtargetImpl()->getInstrInfo();
88
89       bool Changed = false;
90
91       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
92         MachineBasicBlock &B = *I++;
93         if (processBlock(B))
94           Changed = true;
95       }
96
97       return Changed;
98     }
99
100     void getAnalysisUsage(AnalysisUsage &AU) const override {
101       MachineFunctionPass::getAnalysisUsage(AU);
102     }
103   };
104 }
105
106 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
107                       "PowerPC TLS Dynamic Call Fixup", false, false)
108 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
109                     "PowerPC TLS Dynamic Call Fixup", false, false)
110
111 char PPCTLSDynamicCall::ID = 0;
112 FunctionPass*
113 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }