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