24#define DEBUG_TYPE "CopyHoist"
39 StringRef getPassName()
const override {
return "Hexagon Copy Hoisting"; }
41 void getAnalysisUsage(AnalysisUsage &AU)
const override {
51 bool runOnMachineFunction(MachineFunction &Fn)
override;
52 void collectCopyInst();
53 void addMItoCopyList(MachineInstr *
MI);
54 bool analyzeCopy(MachineBasicBlock *BB);
55 bool isSafetoMove(MachineInstr *CandMI);
56 void moveCopyInstr(MachineBasicBlock *DestBB,
57 std::pair<Register, Register>
Key, MachineInstr *
MI);
60 MachineRegisterInfo *MRI;
61 std::vector<DenseMap<std::pair<Register, Register>, MachineInstr *>>
67char HexagonCopyHoisting::ID = 0;
94 if (BB->pred_size() != 1)
96 auto &BBCopyInst = CopyMIList[BB->getNumber()];
97 if (BBCopyInst.size() > 0)
98 Changed |= analyzeCopy(*BB->pred_begin());
103 LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
114void HexagonCopyHoisting::collectCopyInst() {
115 for (MachineBasicBlock &BB : *MFN) {
117 auto &BBCopyInst = CopyMIList[BB.getNumber()];
121 for (MachineInstr &
MI : BB) {
122 if (
MI.getOpcode() == TargetOpcode::COPY)
123 addMItoCopyList(&
MI);
125 LLVM_DEBUG(
dbgs() <<
"\tNumber of copies: " << BBCopyInst.size() <<
"\n");
129void HexagonCopyHoisting::addMItoCopyList(MachineInstr *
MI) {
130 unsigned BBNum =
MI->getParent()->getNumber();
131 auto &BBCopyInst = CopyMIList[BBNum];
135 if (!Register::isVirtualRegister(DstReg) ||
136 !Register::isVirtualRegister(SrcReg) ||
137 MRI->getRegClass(DstReg) != &Hexagon::IntRegsRegClass ||
138 MRI->getRegClass(SrcReg) != &Hexagon::IntRegsRegClass)
141 BBCopyInst.insert(std::pair(std::pair(SrcReg, DstReg),
MI));
144 for (
auto II : BBCopyInst) {
145 MachineInstr *TempMI =
II.getSecond();
156bool HexagonCopyHoisting::analyzeCopy(MachineBasicBlock *BB) {
162 for (MachineBasicBlock *SB : BB->
successors()) {
163 if (SB->pred_size() != 1 || SB->isEHPad() || SB->hasAddressTaken())
168 auto &BBCopyInst1 = CopyMIList[SBB1->
getNumber()];
170 for (
auto II : BBCopyInst1) {
171 std::pair<Register, Register>
Key =
II.getFirst();
172 MachineInstr *
MI =
II.getSecond();
173 bool IsSafetoMove =
true;
174 for (MachineBasicBlock *SuccBB : BB->
successors()) {
175 auto &SuccBBCopyInst = CopyMIList[SuccBB->getNumber()];
176 auto It = SuccBBCopyInst.find(
Key);
177 if (It == SuccBBCopyInst.end()) {
179 IsSafetoMove =
false;
184 MachineInstr *SuccMI = It->second;
185 if (!isSafetoMove(SuccMI)) {
186 IsSafetoMove =
false;
195 moveCopyInstr(BB,
Key,
MI);
202 auto &BBCopyInst = CopyMIList[BB->
getNumber()];
203 for (
auto II : BBCopyInst) {
204 MachineInstr *TempMI =
II.getSecond();
211bool HexagonCopyHoisting::isSafetoMove(MachineInstr *CandMI) {
218 MachineBasicBlock *BB = CandMI->
getParent();
221 for (MII = BB->
begin(), MIE = CandMI; MII != MIE; ++MII) {
222 MachineInstr *OtherMI = &*MII;
223 for (
const MachineOperand &Mo : OtherMI->
operands())
224 if (Mo.isReg() && Mo.getReg() == DefR)
228 for (MII = BB->
begin(), MIE = CandMI; MII != MIE; ++MII) {
229 MachineInstr *OtherMI = &*MII;
230 for (
const MachineOperand &Mo : OtherMI->
operands())
231 if (Mo.isReg() && Mo.isDef() && Mo.getReg() == UseR)
237void HexagonCopyHoisting::moveCopyInstr(MachineBasicBlock *DestBB,
238 std::pair<Register, Register>
Key,
247 auto &BBCopyInst = CopyMIList[SuccBB->getNumber()];
248 MachineInstr *SuccMI = BBCopyInst[
Key];
250 BBCopyInst.erase(
Key);
259 "Hexagon move phi copy",
false,
false)
262 return new HexagonCopyHoisting();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
static cl::opt< std::string > CPHoistFn("cphoistfn", cl::Hidden, cl::desc(""), cl::init(""))
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
FunctionPass class - This class is used to implement most global optimizations.
SlotIndexes * getSlotIndexes() const
void reanalyze(MachineFunction &MF)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
succ_iterator succ_begin()
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned succ_size() const
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock * getParent() const
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
FunctionPass * createHexagonCopyHoisting()
iterator_range< po_iterator< T > > post_order(const T &G)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
char & HexagonCopyHoistingID