1//===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
--- 98 unchanged lines hidden (view full) ---
107 unsigned RegB, unsigned Dist);
108
109 typedef std::pair<std::pair<unsigned, bool>, MachineInstr*> NewKill;
110 bool canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
111 SmallVector<NewKill, 4> &NewKills,
112 MachineBasicBlock *MBB, unsigned Dist);
113 bool DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
114 MachineBasicBlock::iterator &nmi,
115 MachineFunction::iterator &mbbi, unsigned Dist);
116
117 bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
118 MachineBasicBlock::iterator &nmi,
119 MachineFunction::iterator &mbbi,
120 unsigned SrcIdx, unsigned DstIdx,
121 unsigned Dist);
122
123 void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
--- 600 unchanged lines hidden (view full) ---
724 }
725 }
726
727 Processed.insert(MI);
728}
729
730/// isSafeToDelete - If the specified instruction does not produce any side
731/// effects and all of its defs are dead, then it's safe to delete.
732static bool isSafeToDelete(MachineInstr *MI,
733 const TargetInstrInfo *TII,
734 SmallVector<unsigned, 4> &Kills) {
735 const TargetInstrDesc &TID = MI->getDesc();
736 if (TID.mayStore() || TID.isCall())
737 return false;
738 if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
739 return false;
740
741 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
742 MachineOperand &MO = MI->getOperand(i);
743 if (!MO.isReg())
744 continue;
745 if (MO.isDef() && !MO.isDead())
746 return false;
747 if (MO.isUse() && MO.isKill())
748 Kills.push_back(MO.getReg());
749 }
750 return true;
751}
752
753/// canUpdateDeletedKills - Check if all the registers listed in Kills are
754/// killed by instructions in MBB preceding the current instruction at
755/// position Dist. If so, return true and record information about the
756/// preceding kills in NewKills.
757bool TwoAddressInstructionPass::
--- 18 unchanged lines hidden (view full) ---
776}
777
778/// DeleteUnusedInstr - If an instruction with a tied register operand can
779/// be safely deleted, just delete it.
780bool
781TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
782 MachineBasicBlock::iterator &nmi,
783 MachineFunction::iterator &mbbi,
784 unsigned Dist) {
785 // Check if the instruction has no side effects and if all its defs are dead.
786 SmallVector<unsigned, 4> Kills;
787 if (!isSafeToDelete(mi, TII, Kills))
788 return false;
789
790 // If this instruction kills some virtual registers, we need to
791 // update the kill information. If it's not possible to do so,
792 // then bail out.
793 SmallVector<NewKill, 4> NewKills;
794 if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
795 return false;
--- 6 unchanged lines hidden (view full) ---
802 NewKills.pop_back();
803 if (LV->removeVirtualRegisterKilled(Kill, mi)) {
804 if (isDead)
805 LV->addVirtualRegisterDead(Kill, NewKill);
806 else
807 LV->addVirtualRegisterKilled(Kill, NewKill);
808 }
809 }
810 }
811
812 mbbi->erase(mi); // Nuke the old inst.
813 mi = nmi;
814 return true;
815}
816
817/// TryInstructionTransform - For the case where an instruction has a single
--- 12 unchanged lines hidden (view full) ---
830
831 assert(TargetRegisterInfo::isVirtualRegister(regB) &&
832 "cannot make instruction into two-address form");
833
834 // If regA is dead and the instruction can be deleted, just delete
835 // it so it doesn't clobber regB.
836 bool regBKilled = isKilled(*mi, regB, MRI, TII);
837 if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
838 DeleteUnusedInstr(mi, nmi, mbbi, Dist)) {
839 ++NumDeletes;
840 return true; // Done with this instruction.
841 }
842
843 // Check if it is profitable to commute the operands.
844 unsigned SrcOp1, SrcOp2;
845 unsigned regC = 0;
846 unsigned regCIdx = ~0U;
--- 263 unchanged lines hidden ---
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
--- 98 unchanged lines hidden (view full) ---
107 unsigned RegB, unsigned Dist);
108
109 typedef std::pair<std::pair<unsigned, bool>, MachineInstr*> NewKill;
110 bool canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
111 SmallVector<NewKill, 4> &NewKills,
112 MachineBasicBlock *MBB, unsigned Dist);
113 bool DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
114 MachineBasicBlock::iterator &nmi,
115 MachineFunction::iterator &mbbi, unsigned Dist);
116
117 bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
118 MachineBasicBlock::iterator &nmi,
119 MachineFunction::iterator &mbbi,
120 unsigned SrcIdx, unsigned DstIdx,
121 unsigned Dist);
122
123 void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
--- 600 unchanged lines hidden (view full) ---
724 }
725 }
726
727 Processed.insert(MI);
728}
729
730/// isSafeToDelete - If the specified instruction does not produce any side
731/// effects and all of its defs are dead, then it's safe to delete.
732static bool isSafeToDelete(MachineInstr *MI,
733 const TargetInstrInfo *TII,
734 SmallVector<unsigned, 4> &Kills) {
735 const TargetInstrDesc &TID = MI->getDesc();
736 if (TID.mayStore() || TID.isCall())
737 return false;
738 if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
739 return false;
740
741 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
742 MachineOperand &MO = MI->getOperand(i);
743 if (!MO.isReg())
744 continue;
745 if (MO.isDef() && !MO.isDead())
746 return false;
747 if (MO.isUse() && MO.isKill())
748 Kills.push_back(MO.getReg());
749 }
750 return true;
751}
752
753/// canUpdateDeletedKills - Check if all the registers listed in Kills are
754/// killed by instructions in MBB preceding the current instruction at
755/// position Dist. If so, return true and record information about the
756/// preceding kills in NewKills.
757bool TwoAddressInstructionPass::
--- 18 unchanged lines hidden (view full) ---
776}
777
778/// DeleteUnusedInstr - If an instruction with a tied register operand can
779/// be safely deleted, just delete it.
780bool
781TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
782 MachineBasicBlock::iterator &nmi,
783 MachineFunction::iterator &mbbi,
784 unsigned Dist) {
785 // Check if the instruction has no side effects and if all its defs are dead.
786 SmallVector<unsigned, 4> Kills;
787 if (!isSafeToDelete(mi, TII, Kills))
788 return false;
789
790 // If this instruction kills some virtual registers, we need to
791 // update the kill information. If it's not possible to do so,
792 // then bail out.
793 SmallVector<NewKill, 4> NewKills;
794 if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
795 return false;
--- 6 unchanged lines hidden (view full) ---
802 NewKills.pop_back();
803 if (LV->removeVirtualRegisterKilled(Kill, mi)) {
804 if (isDead)
805 LV->addVirtualRegisterDead(Kill, NewKill);
806 else
807 LV->addVirtualRegisterKilled(Kill, NewKill);
808 }
809 }
810 }
811
812 mbbi->erase(mi); // Nuke the old inst.
813 mi = nmi;
814 return true;
815}
816
817/// TryInstructionTransform - For the case where an instruction has a single
--- 12 unchanged lines hidden (view full) ---
830
831 assert(TargetRegisterInfo::isVirtualRegister(regB) &&
832 "cannot make instruction into two-address form");
833
834 // If regA is dead and the instruction can be deleted, just delete
835 // it so it doesn't clobber regB.
836 bool regBKilled = isKilled(*mi, regB, MRI, TII);
837 if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
838 DeleteUnusedInstr(mi, nmi, mbbi, Dist)) {
839 ++NumDeletes;
840 return true; // Done with this instruction.
841 }
842
843 // Check if it is profitable to commute the operands.
844 unsigned SrcOp1, SrcOp2;
845 unsigned regC = 0;
846 unsigned regCIdx = ~0U;
--- 263 unchanged lines hidden ---