1 //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "PPCInstrInfo.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"
28 #define DEBUG_TYPE "ppc-tls-dynamic-call"
31 void initializePPCTLSDynamicCallPass(PassRegistry&);
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 {
42 PPCTLSDynamicCall() : MachineFunctionPass(ID) {
43 initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
46 const PPCTargetMachine *TM;
47 const PPCInstrInfo *TII;
50 bool processBlock(MachineBasicBlock &MBB) {
52 bool Is64Bit = TM->getSubtargetImpl()->isPPC64();
54 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
58 if (MI->getOpcode() != PPC::GETtlsADDR &&
59 MI->getOpcode() != PPC::GETtlsldADDR &&
60 MI->getOpcode() != PPC::GETtlsADDR32 &&
61 MI->getOpcode() != PPC::GETtlsldADDR32)
64 DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << *MI;);
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;
71 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
73 MI->getOperand(0).setReg(GPR3);
74 MI->getOperand(1).setReg(GPR3);
75 BuildMI(MBB, ++I, DL, TII->get(TargetOpcode::COPY), OutReg)
85 bool runOnMachineFunction(MachineFunction &MF) override {
86 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
87 TII = TM->getSubtargetImpl()->getInstrInfo();
91 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
92 MachineBasicBlock &B = *I++;
100 void getAnalysisUsage(AnalysisUsage &AU) const override {
101 MachineFunctionPass::getAnalysisUsage(AU);
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)
111 char PPCTLSDynamicCall::ID = 0;
113 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }