Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 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 | |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 49 | const PPCInstrInfo *TII; |
| 50 | LiveIntervals *LIS; |
| 51 | |
| 52 | protected: |
| 53 | bool processBlock(MachineBasicBlock &MBB) { |
| 54 | bool Changed = false; |
Eric Christopher | 1df0c51 | 2015-02-20 18:44:15 +0000 | [diff] [blame] | 55 | bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64(); |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 56 | |
| 57 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
Hal Finkel | 82e1fc5 | 2015-05-21 23:45:49 +0000 | [diff] [blame] | 58 | I != IE;) { |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 59 | MachineInstr *MI = I; |
| 60 | |
| 61 | if (MI->getOpcode() != PPC::ADDItlsgdLADDR && |
| 62 | MI->getOpcode() != PPC::ADDItlsldLADDR && |
| 63 | MI->getOpcode() != PPC::ADDItlsgdLADDR32 && |
Hal Finkel | 82e1fc5 | 2015-05-21 23:45:49 +0000 | [diff] [blame] | 64 | MI->getOpcode() != PPC::ADDItlsldLADDR32) { |
| 65 | ++I; |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 66 | continue; |
Hal Finkel | 82e1fc5 | 2015-05-21 23:45:49 +0000 | [diff] [blame] | 67 | } |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 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; |
Benjamin Kramer | 3bc1edf | 2016-07-02 11:41:39 +0000 | [diff] [blame] | 76 | const unsigned OrigRegs[] = {OutReg, InReg, GPR3}; |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 77 | |
| 78 | switch (MI->getOpcode()) { |
| 79 | default: |
| 80 | llvm_unreachable("Opcode inconsistency error"); |
| 81 | case PPC::ADDItlsgdLADDR: |
| 82 | Opc1 = PPC::ADDItlsgdL; |
| 83 | Opc2 = PPC::GETtlsADDR; |
| 84 | break; |
| 85 | case PPC::ADDItlsldLADDR: |
| 86 | Opc1 = PPC::ADDItlsldL; |
| 87 | Opc2 = PPC::GETtlsldADDR; |
| 88 | break; |
| 89 | case PPC::ADDItlsgdLADDR32: |
| 90 | Opc1 = PPC::ADDItlsgdL32; |
| 91 | Opc2 = PPC::GETtlsADDR32; |
| 92 | break; |
| 93 | case PPC::ADDItlsldLADDR32: |
| 94 | Opc1 = PPC::ADDItlsldL32; |
| 95 | Opc2 = PPC::GETtlsldADDR32; |
| 96 | break; |
| 97 | } |
| 98 | |
Kyle Butt | bfcff38 | 2016-01-08 02:06:19 +0000 | [diff] [blame] | 99 | // Don't really need to save data to the stack - the clobbered |
| 100 | // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr) |
| 101 | // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR). |
| 102 | BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0); |
| 103 | |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 104 | // Expand into two ops built prior to the existing instruction. |
| 105 | MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3) |
| 106 | .addReg(InReg); |
| 107 | Addi->addOperand(MI->getOperand(2)); |
| 108 | |
| 109 | // The ADDItls* instruction is the first instruction in the |
| 110 | // repair range. |
| 111 | MachineBasicBlock::iterator First = I; |
| 112 | --First; |
| 113 | |
| 114 | MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3) |
| 115 | .addReg(GPR3)); |
| 116 | Call->addOperand(MI->getOperand(3)); |
| 117 | |
Kyle Butt | bfcff38 | 2016-01-08 02:06:19 +0000 | [diff] [blame] | 118 | BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0); |
| 119 | |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 120 | BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg) |
| 121 | .addReg(GPR3); |
| 122 | |
| 123 | // The COPY is the last instruction in the repair range. |
| 124 | MachineBasicBlock::iterator Last = I; |
| 125 | --Last; |
| 126 | |
| 127 | // Move past the original instruction and remove it. |
| 128 | ++I; |
| 129 | MI->removeFromParent(); |
| 130 | |
| 131 | // Repair the live intervals. |
| 132 | LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs); |
| 133 | Changed = true; |
| 134 | } |
| 135 | |
| 136 | return Changed; |
| 137 | } |
| 138 | |
| 139 | public: |
| 140 | bool runOnMachineFunction(MachineFunction &MF) override { |
Eric Christopher | 1df0c51 | 2015-02-20 18:44:15 +0000 | [diff] [blame] | 141 | TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 142 | LIS = &getAnalysis<LiveIntervals>(); |
| 143 | |
| 144 | bool Changed = false; |
| 145 | |
| 146 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 147 | MachineBasicBlock &B = *I++; |
| 148 | if (processBlock(B)) |
| 149 | Changed = true; |
| 150 | } |
| 151 | |
| 152 | return Changed; |
| 153 | } |
| 154 | |
| 155 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 156 | AU.addRequired<LiveIntervals>(); |
| 157 | AU.addPreserved<LiveIntervals>(); |
| 158 | AU.addRequired<SlotIndexes>(); |
| 159 | AU.addPreserved<SlotIndexes>(); |
| 160 | MachineFunctionPass::getAnalysisUsage(AU); |
| 161 | } |
| 162 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 163 | } |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 164 | |
| 165 | INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE, |
| 166 | "PowerPC TLS Dynamic Call Fixup", false, false) |
| 167 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 168 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 169 | INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE, |
| 170 | "PowerPC TLS Dynamic Call Fixup", false, false) |
| 171 | |
| 172 | char PPCTLSDynamicCall::ID = 0; |
| 173 | FunctionPass* |
| 174 | llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); } |