43#define DEBUG_TYPE "optimize-mips-pic-call"
47 cl::desc(
"Load target address from GOT"),
57using CntRegP = std::pair<unsigned, unsigned>;
68 bool isVisited()
const;
69 void preVisit(ScopedHTType &ScopedHT);
74 ScopedHTType::ScopeTy *HTScope;
79 OptimizePICCall() : MachineFunctionPass(ID) {}
81 StringRef getPassName()
const override {
return "Mips OptimizePICCall"; }
83 bool runOnMachineFunction(MachineFunction &
F)
override;
85 void getAnalysisUsage(AnalysisUsage &AU)
const override {
92 bool visitNode(MBBInfo &
MBBI);
99 bool isCallViaRegister(MachineInstr &
MI,
unsigned &
Reg,
113 ScopedHTType ScopedHT;
120char OptimizePICCall::ID = 0;
124 if (
MI.getNumOperands() == 0)
139 assert(
TRI.legalclasstypes_end(*RC) -
TRI.legalclasstypes_begin(*RC) == 1);
140 return *
TRI.legalclasstypes_begin(*RC);
153 Register SrcReg =
I->getOperand(0).getReg();
154 unsigned DstReg =
getRegTy(SrcReg, MF) == MVT::i32 ? Mips::T9 : Mips::T9_64;
155 BuildMI(*
MBB,
I,
I->getDebugLoc(),
TII.get(TargetOpcode::COPY), DstReg)
157 I->getOperand(0).setReg(DstReg);
167 unsigned Reg = Ty == MVT::i32 ? Mips::GP : Mips::GP_64;
169 for (
unsigned I = 0;
I <
MI.getNumOperands(); ++
I) {
184bool MBBInfo::isVisited()
const {
return HTScope; }
186void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
187 HTScope =
new ScopedHTType::ScopeTy(ScopedHT);
190void MBBInfo::postVisit() {
195bool OptimizePICCall::runOnMachineFunction(MachineFunction &
F) {
196 if (
F.getSubtarget<MipsSubtarget>().inMips16Mode())
200 MachineDominatorTree *MDT =
201 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
206 while (!WorkList.empty()) {
211 if (
MBBI.isVisited()) {
218 MBBI.preVisit(ScopedHT);
221 WorkList.append(
Node->begin(),
Node->end());
227bool OptimizePICCall::visitNode(MBBInfo &
MBBI) {
229 MachineBasicBlock *
MBB =
MBBI.getNode()->getBlock();
237 if (!isCallViaRegister(*
I,
Reg, Entry))
241 unsigned N = getCount(Entry);
257 incCntAndSetReg(Entry,
Reg);
265bool OptimizePICCall::isCallViaRegister(MachineInstr &
MI,
unsigned &
Reg,
279 MachineRegisterInfo &
MRI =
MI.getParent()->getParent()->getRegInfo();
302unsigned OptimizePICCall::getCount(
ValueType Entry) {
303 return ScopedHT.lookup(Entry).first;
306unsigned OptimizePICCall::getReg(
ValueType Entry) {
307 unsigned Reg = ScopedHT.lookup(Entry).second;
312void OptimizePICCall::incCntAndSetReg(
ValueType Entry,
unsigned Reg) {
313 CntRegP
P = ScopedHT.lookup(Entry);
314 ScopedHT.insert(Entry, std::make_pair(
P.first + 1,
Reg));
319 return new OptimizePICCall();
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static cl::opt< bool > LoadTargetFromGOT("mips-load-target-from-got", cl::init(true), cl::desc("Load target address from GOT"), cl::Hidden)
static void setCallTargetReg(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
Do the following transformation:
static void eraseGPOpnd(MachineInstr &MI)
Search MI's operands for register GP and erase it.
static MachineOperand * getCallTargetRegOpnd(MachineInstr &MI)
Return the first MachineOperand of MI if it is a used virtual register.
static cl::opt< bool > EraseGPOpnd("mips-erase-gp-opnd", cl::init(true), cl::desc("Erase GP Operand"), cl::Hidden)
static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF)
Return type of register Reg.
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file defines the SmallVector class.
AnalysisUsage & addRequired()
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
FunctionPass class - This class is used to implement most global optimizations.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
unsigned getNumOperands() const
Retuns the total number of operands.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
unsigned getTargetFlags() const
Register getReg() const
getReg - Returns the register number.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
NodeAddr< NodeBase * > Node
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createMipsOptimizePICCallPass()
Return an OptimizeCall object.
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
An information struct used to provide DenseMap with the various necessary components for a given valu...