LLVM  3.7.0
PPCTLSDynamicCall.cpp
Go to the documentation of this file.
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 expands ADDItls{ld,gd}LADDR[32] machine instructions into
11 // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of
12 // which define GPR3. A copy is added from GPR3 to the target virtual
13 // register of the original instruction. The GETtlsADDR[32] is really
14 // a call instruction, so its target register is constrained to be GPR3.
15 // This is not true of ADDItls[gd]L[32], but there is a legacy linker
16 // optimization bug that requires the target register of the addi of
17 // a local- or general-dynamic TLS access sequence to be GPR3.
18 //
19 // This is done in a late pass so that TLS variable accesses can be
20 // fully commoned by MachineCSE.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #include "PPCInstrInfo.h"
25 #include "PPC.h"
26 #include "PPCInstrBuilder.h"
27 #include "PPCTargetMachine.h"
31 #include "llvm/Support/Debug.h"
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "ppc-tls-dynamic-call"
37 
38 namespace llvm {
40 }
41 
42 namespace {
43  struct PPCTLSDynamicCall : public MachineFunctionPass {
44  static char ID;
45  PPCTLSDynamicCall() : MachineFunctionPass(ID) {
47  }
48 
49  const PPCInstrInfo *TII;
50  LiveIntervals *LIS;
51 
52 protected:
53  bool processBlock(MachineBasicBlock &MBB) {
54  bool Changed = false;
55  bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
56 
57  for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
58  I != IE;) {
59  MachineInstr *MI = I;
60 
61  if (MI->getOpcode() != PPC::ADDItlsgdLADDR &&
62  MI->getOpcode() != PPC::ADDItlsldLADDR &&
63  MI->getOpcode() != PPC::ADDItlsgdLADDR32 &&
64  MI->getOpcode() != PPC::ADDItlsldLADDR32) {
65  ++I;
66  continue;
67  }
68 
69  DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << *MI;);
70 
71  unsigned OutReg = MI->getOperand(0).getReg();
72  unsigned InReg = MI->getOperand(1).getReg();
73  DebugLoc DL = MI->getDebugLoc();
74  unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
75  unsigned Opc1, Opc2;
76  SmallVector<unsigned, 4> OrigRegs;
77  OrigRegs.push_back(OutReg);
78  OrigRegs.push_back(InReg);
79  OrigRegs.push_back(GPR3);
80 
81  switch (MI->getOpcode()) {
82  default:
83  llvm_unreachable("Opcode inconsistency error");
84  case PPC::ADDItlsgdLADDR:
85  Opc1 = PPC::ADDItlsgdL;
86  Opc2 = PPC::GETtlsADDR;
87  break;
88  case PPC::ADDItlsldLADDR:
89  Opc1 = PPC::ADDItlsldL;
90  Opc2 = PPC::GETtlsldADDR;
91  break;
92  case PPC::ADDItlsgdLADDR32:
93  Opc1 = PPC::ADDItlsgdL32;
94  Opc2 = PPC::GETtlsADDR32;
95  break;
96  case PPC::ADDItlsldLADDR32:
97  Opc1 = PPC::ADDItlsldL32;
98  Opc2 = PPC::GETtlsldADDR32;
99  break;
100  }
101 
102  // Expand into two ops built prior to the existing instruction.
103  MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3)
104  .addReg(InReg);
105  Addi->addOperand(MI->getOperand(2));
106 
107  // The ADDItls* instruction is the first instruction in the
108  // repair range.
110  --First;
111 
112  MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
113  .addReg(GPR3));
114  Call->addOperand(MI->getOperand(3));
115 
116  BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
117  .addReg(GPR3);
118 
119  // The COPY is the last instruction in the repair range.
121  --Last;
122 
123  // Move past the original instruction and remove it.
124  ++I;
125  MI->removeFromParent();
126 
127  // Repair the live intervals.
128  LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
129  Changed = true;
130  }
131 
132  return Changed;
133  }
134 
135 public:
136  bool runOnMachineFunction(MachineFunction &MF) override {
137  TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
138  LIS = &getAnalysis<LiveIntervals>();
139 
140  bool Changed = false;
141 
142  for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
143  MachineBasicBlock &B = *I++;
144  if (processBlock(B))
145  Changed = true;
146  }
147 
148  return Changed;
149  }
150 
151  void getAnalysisUsage(AnalysisUsage &AU) const override {
154  AU.addRequired<SlotIndexes>();
157  }
158  };
159 }
160 
161 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
162  "PowerPC TLS Dynamic Call Fixup", false, false)
165 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
166  "PowerPC TLS Dynamic Call Fixup", false, false)
167 
168 char PPCTLSDynamicCall::ID = 0;
170 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
void push_back(const T &Elt)
Definition: SmallVector.h:222
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializePPCTLSDynamicCallPass(PassRegistry &)
INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,"PowerPC TLS Dynamic Call Fixup", false, false) INITIALIZE_PASS_END(PPCTLSDynamicCall
A debug info location.
Definition: DebugLoc.h:34
FunctionPass * createPPCTLSDynamicCallPass()
COPY - Target-independent register copy.
Definition: TargetOpcodes.h:86
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
SlotIndexes pass.
Definition: SlotIndexes.h:334
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:267
PowerPC TLS Dynamic Call false
bundle_iterator< MachineInstr, instr_iterator > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
PowerPC TLS Dynamic Call Fixup
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
#define DEBUG_TYPE
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:238
Representation of each machine instruction.
Definition: MachineInstr.h:51
#define I(x, y, z)
Definition: MD5.cpp:54
MachineInstr * removeFromParent()
Unlink 'this' from the containing basic block, and return it without deleting it. ...
unsigned getReg() const
getReg - Returns the register number.
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:92
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41