1*06f32e7eSjoerg //===-- PPCTOCRegDeps.cpp - Add Extra TOC Register Dependencies -----------===//
2*06f32e7eSjoerg //
3*06f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*06f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06f32e7eSjoerg //
7*06f32e7eSjoerg //===----------------------------------------------------------------------===//
8*06f32e7eSjoerg //
9*06f32e7eSjoerg // When resolving an address using the ELF ABI TOC pointer, two relocations are
10*06f32e7eSjoerg // generally required: one for the high part and one for the low part. Only
11*06f32e7eSjoerg // the high part generally explicitly depends on r2 (the TOC pointer). And, so,
12*06f32e7eSjoerg // we might produce code like this:
13*06f32e7eSjoerg //
14*06f32e7eSjoerg // .Ltmp526:
15*06f32e7eSjoerg // addis 3, 2, .LC12@toc@ha
16*06f32e7eSjoerg // .Ltmp1628:
17*06f32e7eSjoerg // std 2, 40(1)
18*06f32e7eSjoerg // ld 5, 0(27)
19*06f32e7eSjoerg // ld 2, 8(27)
20*06f32e7eSjoerg // ld 11, 16(27)
21*06f32e7eSjoerg // ld 3, .LC12@toc@l(3)
22*06f32e7eSjoerg // rldicl 4, 4, 0, 32
23*06f32e7eSjoerg // mtctr 5
24*06f32e7eSjoerg // bctrl
25*06f32e7eSjoerg // ld 2, 40(1)
26*06f32e7eSjoerg //
27*06f32e7eSjoerg // And there is nothing wrong with this code, as such, but there is a linker bug
28*06f32e7eSjoerg // in binutils (https://sourceware.org/bugzilla/show_bug.cgi?id=18414) that will
29*06f32e7eSjoerg // misoptimize this code sequence to this:
30*06f32e7eSjoerg // nop
31*06f32e7eSjoerg // std r2,40(r1)
32*06f32e7eSjoerg // ld r5,0(r27)
33*06f32e7eSjoerg // ld r2,8(r27)
34*06f32e7eSjoerg // ld r11,16(r27)
35*06f32e7eSjoerg // ld r3,-32472(r2)
36*06f32e7eSjoerg // clrldi r4,r4,32
37*06f32e7eSjoerg // mtctr r5
38*06f32e7eSjoerg // bctrl
39*06f32e7eSjoerg // ld r2,40(r1)
40*06f32e7eSjoerg // because the linker does not know (and does not check) that the value in r2
41*06f32e7eSjoerg // changed in between the instruction using the .LC12@toc@ha (TOC-relative)
42*06f32e7eSjoerg // relocation and the instruction using the .LC12@toc@l(3) relocation.
43*06f32e7eSjoerg // Because it finds these instructions using the relocations (and not by
44*06f32e7eSjoerg // scanning the instructions), it has been asserted that there is no good way
45*06f32e7eSjoerg // to detect the change of r2 in between. As a result, this bug may never be
46*06f32e7eSjoerg // fixed (i.e. it may become part of the definition of the ABI). GCC was
47*06f32e7eSjoerg // updated to add extra dependencies on r2 to instructions using the @toc@l
48*06f32e7eSjoerg // relocations to avoid this problem, and we'll do the same here.
49*06f32e7eSjoerg //
50*06f32e7eSjoerg // This is done as a separate pass because:
51*06f32e7eSjoerg // 1. These extra r2 dependencies are not really properties of the
52*06f32e7eSjoerg // instructions, but rather due to a linker bug, and maybe one day we'll be
53*06f32e7eSjoerg // able to get rid of them when targeting linkers without this bug (and,
54*06f32e7eSjoerg // thus, keeping the logic centralized here will make that
55*06f32e7eSjoerg // straightforward).
56*06f32e7eSjoerg // 2. There are ISel-level peephole optimizations that propagate the @toc@l
57*06f32e7eSjoerg // relocations to some user instructions, and so the exta dependencies do
58*06f32e7eSjoerg // not apply only to a fixed set of instructions (without undesirable
59*06f32e7eSjoerg // definition replication).
60*06f32e7eSjoerg //
61*06f32e7eSjoerg //===----------------------------------------------------------------------===//
62*06f32e7eSjoerg
63*06f32e7eSjoerg #include "MCTargetDesc/PPCPredicates.h"
64*06f32e7eSjoerg #include "PPC.h"
65*06f32e7eSjoerg #include "PPCInstrBuilder.h"
66*06f32e7eSjoerg #include "PPCInstrInfo.h"
67*06f32e7eSjoerg #include "PPCMachineFunctionInfo.h"
68*06f32e7eSjoerg #include "PPCTargetMachine.h"
69*06f32e7eSjoerg #include "llvm/ADT/STLExtras.h"
70*06f32e7eSjoerg #include "llvm/ADT/Statistic.h"
71*06f32e7eSjoerg #include "llvm/CodeGen/MachineFrameInfo.h"
72*06f32e7eSjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
73*06f32e7eSjoerg #include "llvm/CodeGen/MachineInstr.h"
74*06f32e7eSjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
75*06f32e7eSjoerg #include "llvm/MC/MCAsmInfo.h"
76*06f32e7eSjoerg #include "llvm/Support/Debug.h"
77*06f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
78*06f32e7eSjoerg #include "llvm/Support/TargetRegistry.h"
79*06f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
80*06f32e7eSjoerg
81*06f32e7eSjoerg using namespace llvm;
82*06f32e7eSjoerg
83*06f32e7eSjoerg #define DEBUG_TYPE "ppc-toc-reg-deps"
84*06f32e7eSjoerg
85*06f32e7eSjoerg namespace {
86*06f32e7eSjoerg // PPCTOCRegDeps pass - For simple functions without epilogue code, move
87*06f32e7eSjoerg // returns up, and create conditional returns, to avoid unnecessary
88*06f32e7eSjoerg // branch-to-blr sequences.
89*06f32e7eSjoerg struct PPCTOCRegDeps : public MachineFunctionPass {
90*06f32e7eSjoerg static char ID;
PPCTOCRegDeps__anon08fc2cc90111::PPCTOCRegDeps91*06f32e7eSjoerg PPCTOCRegDeps() : MachineFunctionPass(ID) {
92*06f32e7eSjoerg initializePPCTOCRegDepsPass(*PassRegistry::getPassRegistry());
93*06f32e7eSjoerg }
94*06f32e7eSjoerg
95*06f32e7eSjoerg protected:
hasTOCLoReloc__anon08fc2cc90111::PPCTOCRegDeps96*06f32e7eSjoerg bool hasTOCLoReloc(const MachineInstr &MI) {
97*06f32e7eSjoerg if (MI.getOpcode() == PPC::LDtocL ||
98*06f32e7eSjoerg MI.getOpcode() == PPC::ADDItocL ||
99*06f32e7eSjoerg MI.getOpcode() == PPC::LWZtocL)
100*06f32e7eSjoerg return true;
101*06f32e7eSjoerg
102*06f32e7eSjoerg for (const MachineOperand &MO : MI.operands()) {
103*06f32e7eSjoerg if ((MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) == PPCII::MO_TOC_LO)
104*06f32e7eSjoerg return true;
105*06f32e7eSjoerg }
106*06f32e7eSjoerg
107*06f32e7eSjoerg return false;
108*06f32e7eSjoerg }
109*06f32e7eSjoerg
processBlock__anon08fc2cc90111::PPCTOCRegDeps110*06f32e7eSjoerg bool processBlock(MachineBasicBlock &MBB) {
111*06f32e7eSjoerg bool Changed = false;
112*06f32e7eSjoerg
113*06f32e7eSjoerg const bool isPPC64 =
114*06f32e7eSjoerg MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
115*06f32e7eSjoerg const unsigned TOCReg = isPPC64 ? PPC::X2 : PPC::R2;
116*06f32e7eSjoerg
117*06f32e7eSjoerg for (auto &MI : MBB) {
118*06f32e7eSjoerg if (!hasTOCLoReloc(MI))
119*06f32e7eSjoerg continue;
120*06f32e7eSjoerg
121*06f32e7eSjoerg MI.addOperand(MachineOperand::CreateReg(TOCReg,
122*06f32e7eSjoerg false /*IsDef*/,
123*06f32e7eSjoerg true /*IsImp*/));
124*06f32e7eSjoerg Changed = true;
125*06f32e7eSjoerg }
126*06f32e7eSjoerg
127*06f32e7eSjoerg return Changed;
128*06f32e7eSjoerg }
129*06f32e7eSjoerg
130*06f32e7eSjoerg public:
runOnMachineFunction__anon08fc2cc90111::PPCTOCRegDeps131*06f32e7eSjoerg bool runOnMachineFunction(MachineFunction &MF) override {
132*06f32e7eSjoerg bool Changed = false;
133*06f32e7eSjoerg
134*06f32e7eSjoerg for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
135*06f32e7eSjoerg MachineBasicBlock &B = *I++;
136*06f32e7eSjoerg if (processBlock(B))
137*06f32e7eSjoerg Changed = true;
138*06f32e7eSjoerg }
139*06f32e7eSjoerg
140*06f32e7eSjoerg return Changed;
141*06f32e7eSjoerg }
142*06f32e7eSjoerg
getAnalysisUsage__anon08fc2cc90111::PPCTOCRegDeps143*06f32e7eSjoerg void getAnalysisUsage(AnalysisUsage &AU) const override {
144*06f32e7eSjoerg MachineFunctionPass::getAnalysisUsage(AU);
145*06f32e7eSjoerg }
146*06f32e7eSjoerg };
147*06f32e7eSjoerg }
148*06f32e7eSjoerg
149*06f32e7eSjoerg INITIALIZE_PASS(PPCTOCRegDeps, DEBUG_TYPE,
150*06f32e7eSjoerg "PowerPC TOC Register Dependencies", false, false)
151*06f32e7eSjoerg
152*06f32e7eSjoerg char PPCTOCRegDeps::ID = 0;
153*06f32e7eSjoerg FunctionPass*
createPPCTOCRegDepsPass()154*06f32e7eSjoerg llvm::createPPCTOCRegDepsPass() { return new PPCTOCRegDeps(); }
155*06f32e7eSjoerg
156