Add the following 64-bit vector integer arithmetic instructions added in POWER8:
[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 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"
28 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "ppc-tls-dynamic-call"
37
38 namespace llvm {
39   void initializePPCTLSDynamicCallPass(PassRegistry&);
40 }
41
42 namespace {
43   struct PPCTLSDynamicCall : public MachineFunctionPass {
44     static char ID;
45     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
46       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
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; ++I) {
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           continue;
66
67         DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << *MI;);
68
69         unsigned OutReg = MI->getOperand(0).getReg();
70         unsigned InReg  = MI->getOperand(1).getReg();
71         DebugLoc DL = MI->getDebugLoc();
72         unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
73         unsigned Opc1, Opc2;
74         SmallVector<unsigned, 4> OrigRegs;
75         OrigRegs.push_back(OutReg);
76         OrigRegs.push_back(InReg);
77         OrigRegs.push_back(GPR3);
78
79         switch (MI->getOpcode()) {
80         default:
81           llvm_unreachable("Opcode inconsistency error");
82         case PPC::ADDItlsgdLADDR:
83           Opc1 = PPC::ADDItlsgdL;
84           Opc2 = PPC::GETtlsADDR;
85           break;
86         case PPC::ADDItlsldLADDR:
87           Opc1 = PPC::ADDItlsldL;
88           Opc2 = PPC::GETtlsldADDR;
89           break;
90         case PPC::ADDItlsgdLADDR32:
91           Opc1 = PPC::ADDItlsgdL32;
92           Opc2 = PPC::GETtlsADDR32;
93           break;
94         case PPC::ADDItlsldLADDR32:
95           Opc1 = PPC::ADDItlsldL32;
96           Opc2 = PPC::GETtlsldADDR32;
97           break;
98         }
99
100         // Expand into two ops built prior to the existing instruction.
101         MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3)
102           .addReg(InReg);
103         Addi->addOperand(MI->getOperand(2));
104
105         // The ADDItls* instruction is the first instruction in the
106         // repair range.
107         MachineBasicBlock::iterator First = I;
108         --First;
109
110         MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
111                               .addReg(GPR3));
112         Call->addOperand(MI->getOperand(3));
113
114         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
115           .addReg(GPR3);
116
117         // The COPY is the last instruction in the repair range.
118         MachineBasicBlock::iterator Last = I;
119         --Last;
120
121         // Move past the original instruction and remove it.
122         ++I;
123         MI->removeFromParent();
124
125         // Repair the live intervals.
126         LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
127         Changed = true;
128       }
129
130       return Changed;
131     }
132
133 public:
134     bool runOnMachineFunction(MachineFunction &MF) override {
135       TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
136       LIS = &getAnalysis<LiveIntervals>();
137
138       bool Changed = false;
139
140       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
141         MachineBasicBlock &B = *I++;
142         if (processBlock(B))
143           Changed = true;
144       }
145
146       return Changed;
147     }
148
149     void getAnalysisUsage(AnalysisUsage &AU) const override {
150       AU.addRequired<LiveIntervals>();
151       AU.addPreserved<LiveIntervals>();
152       AU.addRequired<SlotIndexes>();
153       AU.addPreserved<SlotIndexes>();
154       MachineFunctionPass::getAnalysisUsage(AU);
155     }
156   };
157 }
158
159 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
160                       "PowerPC TLS Dynamic Call Fixup", false, false)
161 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
162 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
163 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
164                     "PowerPC TLS Dynamic Call Fixup", false, false)
165
166 char PPCTLSDynamicCall::ID = 0;
167 FunctionPass*
168 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }