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)
62 DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << *MI;);
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;
69 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
71 MI->getOperand(0).setReg(GPR3);
72 MI->getOperand(1).setReg(GPR3);
73 BuildMI(MBB, ++I, DL, TII->get(TargetOpcode::COPY), OutReg)
83 bool runOnMachineFunction(MachineFunction &MF) override {
84 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
85 TII = TM->getSubtargetImpl()->getInstrInfo();
89 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
90 MachineBasicBlock &B = *I++;
98 void getAnalysisUsage(AnalysisUsage &AU) const override {
99 MachineFunctionPass::getAnalysisUsage(AU);
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)
109 char PPCTLSDynamicCall::ID = 0;
111 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }