10b57cec5SDimitry Andric //===- MipsOptimizePICCall.cpp - Optimize PIC Calls -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass eliminates unnecessary instructions that set up $gp and replace
100b57cec5SDimitry Andric // instructions that load target function addresses with copy instructions.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "MCTargetDesc/MipsBaseInfo.h"
150b57cec5SDimitry Andric #include "Mips.h"
160b57cec5SDimitry Andric #include "MipsRegisterInfo.h"
170b57cec5SDimitry Andric #include "MipsSubtarget.h"
180b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h"
190b57cec5SDimitry Andric #include "llvm/ADT/ScopedHashTable.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
33*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h"
340b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
350b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include "llvm/Support/RecyclingAllocator.h"
380b57cec5SDimitry Andric #include <cassert>
390b57cec5SDimitry Andric #include <utility>
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric #define DEBUG_TYPE "optimize-mips-pic-call"
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric static cl::opt<bool> LoadTargetFromGOT("mips-load-target-from-got",
460b57cec5SDimitry Andric cl::init(true),
470b57cec5SDimitry Andric cl::desc("Load target address from GOT"),
480b57cec5SDimitry Andric cl::Hidden);
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric static cl::opt<bool> EraseGPOpnd("mips-erase-gp-opnd",
510b57cec5SDimitry Andric cl::init(true), cl::desc("Erase GP Operand"),
520b57cec5SDimitry Andric cl::Hidden);
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric namespace {
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>;
570b57cec5SDimitry Andric using CntRegP = std::pair<unsigned, unsigned>;
580b57cec5SDimitry Andric using AllocatorTy = RecyclingAllocator<BumpPtrAllocator,
590b57cec5SDimitry Andric ScopedHashTableVal<ValueType, CntRegP>>;
600b57cec5SDimitry Andric using ScopedHTType = ScopedHashTable<ValueType, CntRegP,
610b57cec5SDimitry Andric DenseMapInfo<ValueType>, AllocatorTy>;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric class MBBInfo {
640b57cec5SDimitry Andric public:
650b57cec5SDimitry Andric MBBInfo(MachineDomTreeNode *N);
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric const MachineDomTreeNode *getNode() const;
680b57cec5SDimitry Andric bool isVisited() const;
690b57cec5SDimitry Andric void preVisit(ScopedHTType &ScopedHT);
700b57cec5SDimitry Andric void postVisit();
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric private:
730b57cec5SDimitry Andric MachineDomTreeNode *Node;
740b57cec5SDimitry Andric ScopedHTType::ScopeTy *HTScope;
750b57cec5SDimitry Andric };
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric class OptimizePICCall : public MachineFunctionPass {
780b57cec5SDimitry Andric public:
OptimizePICCall()790b57cec5SDimitry Andric OptimizePICCall() : MachineFunctionPass(ID) {}
800b57cec5SDimitry Andric
getPassName() const810b57cec5SDimitry Andric StringRef getPassName() const override { return "Mips OptimizePICCall"; }
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &F) override;
840b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const850b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
86*0fca6ea1SDimitry Andric AU.addRequired<MachineDominatorTreeWrapperPass>();
870b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric private:
910b57cec5SDimitry Andric /// Visit MBB.
920b57cec5SDimitry Andric bool visitNode(MBBInfo &MBBI);
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric /// Test if MI jumps to a function via a register.
950b57cec5SDimitry Andric ///
960b57cec5SDimitry Andric /// Also, return the virtual register containing the target function's address
970b57cec5SDimitry Andric /// and the underlying object in Reg and Val respectively, if the function's
980b57cec5SDimitry Andric /// address can be resolved lazily.
990b57cec5SDimitry Andric bool isCallViaRegister(MachineInstr &MI, unsigned &Reg,
1000b57cec5SDimitry Andric ValueType &Val) const;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric /// Return the number of instructions that dominate the current
1030b57cec5SDimitry Andric /// instruction and load the function address from object Entry.
1040b57cec5SDimitry Andric unsigned getCount(ValueType Entry);
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric /// Return the destination virtual register of the last instruction
1070b57cec5SDimitry Andric /// that loads from object Entry.
1080b57cec5SDimitry Andric unsigned getReg(ValueType Entry);
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric /// Update ScopedHT.
1110b57cec5SDimitry Andric void incCntAndSetReg(ValueType Entry, unsigned Reg);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric ScopedHTType ScopedHT;
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric static char ID;
1160b57cec5SDimitry Andric };
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric } // end of anonymous namespace
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric char OptimizePICCall::ID = 0;
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric /// Return the first MachineOperand of MI if it is a used virtual register.
getCallTargetRegOpnd(MachineInstr & MI)1230b57cec5SDimitry Andric static MachineOperand *getCallTargetRegOpnd(MachineInstr &MI) {
1240b57cec5SDimitry Andric if (MI.getNumOperands() == 0)
1250b57cec5SDimitry Andric return nullptr;
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(0);
1280b57cec5SDimitry Andric
129bdd1243dSDimitry Andric if (!MO.isReg() || !MO.isUse() || !MO.getReg().isVirtual())
1300b57cec5SDimitry Andric return nullptr;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric return &MO;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric /// Return type of register Reg.
getRegTy(unsigned Reg,MachineFunction & MF)1360b57cec5SDimitry Andric static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF) {
1370b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1380b57cec5SDimitry Andric const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
1390b57cec5SDimitry Andric assert(TRI.legalclasstypes_end(*RC) - TRI.legalclasstypes_begin(*RC) == 1);
1400b57cec5SDimitry Andric return *TRI.legalclasstypes_begin(*RC);
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric /// Do the following transformation:
1440b57cec5SDimitry Andric ///
1450b57cec5SDimitry Andric /// jalr $vreg
1460b57cec5SDimitry Andric /// =>
1470b57cec5SDimitry Andric /// copy $t9, $vreg
1480b57cec5SDimitry Andric /// jalr $t9
setCallTargetReg(MachineBasicBlock * MBB,MachineBasicBlock::iterator I)1490b57cec5SDimitry Andric static void setCallTargetReg(MachineBasicBlock *MBB,
1500b57cec5SDimitry Andric MachineBasicBlock::iterator I) {
1510b57cec5SDimitry Andric MachineFunction &MF = *MBB->getParent();
1520b57cec5SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1538bcb0991SDimitry Andric Register SrcReg = I->getOperand(0).getReg();
1540b57cec5SDimitry Andric unsigned DstReg = getRegTy(SrcReg, MF) == MVT::i32 ? Mips::T9 : Mips::T9_64;
1550b57cec5SDimitry Andric BuildMI(*MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), DstReg)
1560b57cec5SDimitry Andric .addReg(SrcReg);
1570b57cec5SDimitry Andric I->getOperand(0).setReg(DstReg);
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric /// Search MI's operands for register GP and erase it.
eraseGPOpnd(MachineInstr & MI)1610b57cec5SDimitry Andric static void eraseGPOpnd(MachineInstr &MI) {
1620b57cec5SDimitry Andric if (!EraseGPOpnd)
1630b57cec5SDimitry Andric return;
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric MachineFunction &MF = *MI.getParent()->getParent();
1660b57cec5SDimitry Andric MVT::SimpleValueType Ty = getRegTy(MI.getOperand(0).getReg(), MF);
1670b57cec5SDimitry Andric unsigned Reg = Ty == MVT::i32 ? Mips::GP : Mips::GP_64;
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
1700b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(I);
1710b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == Reg) {
17281ad6265SDimitry Andric MI.removeOperand(I);
1730b57cec5SDimitry Andric return;
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric llvm_unreachable(nullptr);
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric
MBBInfo(MachineDomTreeNode * N)1800b57cec5SDimitry Andric MBBInfo::MBBInfo(MachineDomTreeNode *N) : Node(N), HTScope(nullptr) {}
1810b57cec5SDimitry Andric
getNode() const1820b57cec5SDimitry Andric const MachineDomTreeNode *MBBInfo::getNode() const { return Node; }
1830b57cec5SDimitry Andric
isVisited() const1840b57cec5SDimitry Andric bool MBBInfo::isVisited() const { return HTScope; }
1850b57cec5SDimitry Andric
preVisit(ScopedHTType & ScopedHT)1860b57cec5SDimitry Andric void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
1870b57cec5SDimitry Andric HTScope = new ScopedHTType::ScopeTy(ScopedHT);
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric
postVisit()1900b57cec5SDimitry Andric void MBBInfo::postVisit() {
1910b57cec5SDimitry Andric delete HTScope;
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric // OptimizePICCall methods.
runOnMachineFunction(MachineFunction & F)1950b57cec5SDimitry Andric bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
19681ad6265SDimitry Andric if (F.getSubtarget<MipsSubtarget>().inMips16Mode())
1970b57cec5SDimitry Andric return false;
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric // Do a pre-order traversal of the dominator tree.
200*0fca6ea1SDimitry Andric MachineDominatorTree *MDT =
201*0fca6ea1SDimitry Andric &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2020b57cec5SDimitry Andric bool Changed = false;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric SmallVector<MBBInfo, 8> WorkList(1, MBBInfo(MDT->getRootNode()));
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric while (!WorkList.empty()) {
2070b57cec5SDimitry Andric MBBInfo &MBBI = WorkList.back();
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric // If this MBB has already been visited, destroy the scope for the MBB and
2100b57cec5SDimitry Andric // pop it from the work list.
2110b57cec5SDimitry Andric if (MBBI.isVisited()) {
2120b57cec5SDimitry Andric MBBI.postVisit();
2130b57cec5SDimitry Andric WorkList.pop_back();
2140b57cec5SDimitry Andric continue;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric // Visit the MBB and add its children to the work list.
2180b57cec5SDimitry Andric MBBI.preVisit(ScopedHT);
2190b57cec5SDimitry Andric Changed |= visitNode(MBBI);
2200b57cec5SDimitry Andric const MachineDomTreeNode *Node = MBBI.getNode();
2215ffd83dbSDimitry Andric WorkList.append(Node->begin(), Node->end());
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andric return Changed;
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
visitNode(MBBInfo & MBBI)2270b57cec5SDimitry Andric bool OptimizePICCall::visitNode(MBBInfo &MBBI) {
2280b57cec5SDimitry Andric bool Changed = false;
2290b57cec5SDimitry Andric MachineBasicBlock *MBB = MBBI.getNode()->getBlock();
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
2320b57cec5SDimitry Andric ++I) {
2330b57cec5SDimitry Andric unsigned Reg;
2340b57cec5SDimitry Andric ValueType Entry;
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andric // Skip instructions that are not call instructions via registers.
2370b57cec5SDimitry Andric if (!isCallViaRegister(*I, Reg, Entry))
2380b57cec5SDimitry Andric continue;
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric Changed = true;
2410b57cec5SDimitry Andric unsigned N = getCount(Entry);
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric if (N != 0) {
2440b57cec5SDimitry Andric // If a function has been called more than twice, we do not have to emit a
2450b57cec5SDimitry Andric // load instruction to get the function address from the GOT, but can
2460b57cec5SDimitry Andric // instead reuse the address that has been loaded before.
2470b57cec5SDimitry Andric if (N >= 2 && !LoadTargetFromGOT)
2480b57cec5SDimitry Andric getCallTargetRegOpnd(*I)->setReg(getReg(Entry));
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andric // Erase the $gp operand if this isn't the first time a function has
2510b57cec5SDimitry Andric // been called. $gp needs to be set up only if the function call can go
2520b57cec5SDimitry Andric // through a lazy binding stub.
2530b57cec5SDimitry Andric eraseGPOpnd(*I);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric if (Entry)
2570b57cec5SDimitry Andric incCntAndSetReg(Entry, Reg);
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric setCallTargetReg(MBB, I);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andric return Changed;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric
isCallViaRegister(MachineInstr & MI,unsigned & Reg,ValueType & Val) const2650b57cec5SDimitry Andric bool OptimizePICCall::isCallViaRegister(MachineInstr &MI, unsigned &Reg,
2660b57cec5SDimitry Andric ValueType &Val) const {
2670b57cec5SDimitry Andric if (!MI.isCall())
2680b57cec5SDimitry Andric return false;
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric MachineOperand *MO = getCallTargetRegOpnd(MI);
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric // Return if MI is not a function call via a register.
2730b57cec5SDimitry Andric if (!MO)
2740b57cec5SDimitry Andric return false;
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric // Get the instruction that loads the function address from the GOT.
2770b57cec5SDimitry Andric Reg = MO->getReg();
2780b57cec5SDimitry Andric Val = nullptr;
2790b57cec5SDimitry Andric MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2800b57cec5SDimitry Andric MachineInstr *DefMI = MRI.getVRegDef(Reg);
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andric assert(DefMI);
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // See if DefMI is an instruction that loads from a GOT entry that holds the
2850b57cec5SDimitry Andric // address of a lazy binding stub.
2860b57cec5SDimitry Andric if (!DefMI->mayLoad() || DefMI->getNumOperands() < 3)
2870b57cec5SDimitry Andric return true;
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric unsigned Flags = DefMI->getOperand(2).getTargetFlags();
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andric if (Flags != MipsII::MO_GOT_CALL && Flags != MipsII::MO_CALL_LO16)
2920b57cec5SDimitry Andric return true;
2930b57cec5SDimitry Andric
2940b57cec5SDimitry Andric // Return the underlying object for the GOT entry in Val.
2950b57cec5SDimitry Andric assert(DefMI->hasOneMemOperand());
2960b57cec5SDimitry Andric Val = (*DefMI->memoperands_begin())->getValue();
2970b57cec5SDimitry Andric if (!Val)
2980b57cec5SDimitry Andric Val = (*DefMI->memoperands_begin())->getPseudoValue();
2990b57cec5SDimitry Andric return true;
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric
getCount(ValueType Entry)3020b57cec5SDimitry Andric unsigned OptimizePICCall::getCount(ValueType Entry) {
3030b57cec5SDimitry Andric return ScopedHT.lookup(Entry).first;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric
getReg(ValueType Entry)3060b57cec5SDimitry Andric unsigned OptimizePICCall::getReg(ValueType Entry) {
3070b57cec5SDimitry Andric unsigned Reg = ScopedHT.lookup(Entry).second;
3080b57cec5SDimitry Andric assert(Reg);
3090b57cec5SDimitry Andric return Reg;
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric
incCntAndSetReg(ValueType Entry,unsigned Reg)3120b57cec5SDimitry Andric void OptimizePICCall::incCntAndSetReg(ValueType Entry, unsigned Reg) {
3130b57cec5SDimitry Andric CntRegP P = ScopedHT.lookup(Entry);
3140b57cec5SDimitry Andric ScopedHT.insert(Entry, std::make_pair(P.first + 1, Reg));
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric /// Return an OptimizeCall object.
createMipsOptimizePICCallPass()3180b57cec5SDimitry Andric FunctionPass *llvm::createMipsOptimizePICCallPass() {
3190b57cec5SDimitry Andric return new OptimizePICCall();
3200b57cec5SDimitry Andric }
321