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,
116 unsigned regB, unsigned regBIdx, unsigned Dist);
117
118 bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
119 MachineBasicBlock::iterator &nmi,
120 MachineFunction::iterator &mbbi,
121 unsigned SrcIdx, unsigned DstIdx,
122 unsigned Dist);
123
124 void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
--- 600 unchanged lines hidden (view full) ---
725 }
726 }
727
728 Processed.insert(MI);
729}
730
731/// isSafeToDelete - If the specified instruction does not produce any side
732/// effects and all of its defs are dead, then it's safe to delete.
733static bool isSafeToDelete(MachineInstr *MI, unsigned Reg,
734 const TargetInstrInfo *TII,
735 SmallVector<unsigned, 4> &Kills) {
736 const TargetInstrDesc &TID = MI->getDesc();
737 if (TID.mayStore() || TID.isCall())
738 return false;
739 if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
740 return false;
741
742 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
743 MachineOperand &MO = MI->getOperand(i);
744 if (!MO.isReg())
745 continue;
746 if (MO.isDef() && !MO.isDead())
747 return false;
748 if (MO.isUse() && MO.getReg() != Reg && MO.isKill())
749 Kills.push_back(MO.getReg());
750 }
751
752 return true;
753}
754
755/// canUpdateDeletedKills - Check if all the registers listed in Kills are
756/// killed by instructions in MBB preceding the current instruction at
757/// position Dist. If so, return true and record information about the
758/// preceding kills in NewKills.
759bool TwoAddressInstructionPass::
--- 18 unchanged lines hidden (view full) ---
778}
779
780/// DeleteUnusedInstr - If an instruction with a tied register operand can
781/// be safely deleted, just delete it.
782bool
783TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
784 MachineBasicBlock::iterator &nmi,
785 MachineFunction::iterator &mbbi,
786 unsigned regB, unsigned regBIdx,
787 unsigned Dist) {
788 // Check if the instruction has no side effects and if all its defs are dead.
789 SmallVector<unsigned, 4> Kills;
790 if (!isSafeToDelete(mi, regB, TII, Kills))
791 return false;
792
793 // If this instruction kills some virtual registers, we need to
794 // update the kill information. If it's not possible to do so,
795 // then bail out.
796 SmallVector<NewKill, 4> NewKills;
797 if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
798 return false;
--- 6 unchanged lines hidden (view full) ---
805 NewKills.pop_back();
806 if (LV->removeVirtualRegisterKilled(Kill, mi)) {
807 if (isDead)
808 LV->addVirtualRegisterDead(Kill, NewKill);
809 else
810 LV->addVirtualRegisterKilled(Kill, NewKill);
811 }
812 }
813
814 // If regB was marked as a kill, update its Kills list.
815 if (mi->getOperand(regBIdx).isKill())
816 LV->removeVirtualRegisterKilled(regB, mi);
817 }
818
819 mbbi->erase(mi); // Nuke the old inst.
820 mi = nmi;
821 return true;
822}
823
824/// TryInstructionTransform - For the case where an instruction has a single
--- 12 unchanged lines hidden (view full) ---
837
838 assert(TargetRegisterInfo::isVirtualRegister(regB) &&
839 "cannot make instruction into two-address form");
840
841 // If regA is dead and the instruction can be deleted, just delete
842 // it so it doesn't clobber regB.
843 bool regBKilled = isKilled(*mi, regB, MRI, TII);
844 if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
845 DeleteUnusedInstr(mi, nmi, mbbi, regB, SrcIdx, Dist)) {
846 ++NumDeletes;
847 return true; // Done with this instruction.
848 }
849
850 // Check if it is profitable to commute the operands.
851 unsigned SrcOp1, SrcOp2;
852 unsigned regC = 0;
853 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,
116 unsigned regB, unsigned regBIdx, unsigned Dist);
117
118 bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
119 MachineBasicBlock::iterator &nmi,
120 MachineFunction::iterator &mbbi,
121 unsigned SrcIdx, unsigned DstIdx,
122 unsigned Dist);
123
124 void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
--- 600 unchanged lines hidden (view full) ---
725 }
726 }
727
728 Processed.insert(MI);
729}
730
731/// isSafeToDelete - If the specified instruction does not produce any side
732/// effects and all of its defs are dead, then it's safe to delete.
733static bool isSafeToDelete(MachineInstr *MI, unsigned Reg,
734 const TargetInstrInfo *TII,
735 SmallVector<unsigned, 4> &Kills) {
736 const TargetInstrDesc &TID = MI->getDesc();
737 if (TID.mayStore() || TID.isCall())
738 return false;
739 if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
740 return false;
741
742 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
743 MachineOperand &MO = MI->getOperand(i);
744 if (!MO.isReg())
745 continue;
746 if (MO.isDef() && !MO.isDead())
747 return false;
748 if (MO.isUse() && MO.getReg() != Reg && MO.isKill())
749 Kills.push_back(MO.getReg());
750 }
751
752 return true;
753}
754
755/// canUpdateDeletedKills - Check if all the registers listed in Kills are
756/// killed by instructions in MBB preceding the current instruction at
757/// position Dist. If so, return true and record information about the
758/// preceding kills in NewKills.
759bool TwoAddressInstructionPass::
--- 18 unchanged lines hidden (view full) ---
778}
779
780/// DeleteUnusedInstr - If an instruction with a tied register operand can
781/// be safely deleted, just delete it.
782bool
783TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
784 MachineBasicBlock::iterator &nmi,
785 MachineFunction::iterator &mbbi,
786 unsigned regB, unsigned regBIdx,
787 unsigned Dist) {
788 // Check if the instruction has no side effects and if all its defs are dead.
789 SmallVector<unsigned, 4> Kills;
790 if (!isSafeToDelete(mi, regB, TII, Kills))
791 return false;
792
793 // If this instruction kills some virtual registers, we need to
794 // update the kill information. If it's not possible to do so,
795 // then bail out.
796 SmallVector<NewKill, 4> NewKills;
797 if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
798 return false;
--- 6 unchanged lines hidden (view full) ---
805 NewKills.pop_back();
806 if (LV->removeVirtualRegisterKilled(Kill, mi)) {
807 if (isDead)
808 LV->addVirtualRegisterDead(Kill, NewKill);
809 else
810 LV->addVirtualRegisterKilled(Kill, NewKill);
811 }
812 }
813
814 // If regB was marked as a kill, update its Kills list.
815 if (mi->getOperand(regBIdx).isKill())
816 LV->removeVirtualRegisterKilled(regB, mi);
817 }
818
819 mbbi->erase(mi); // Nuke the old inst.
820 mi = nmi;
821 return true;
822}
823
824/// TryInstructionTransform - For the case where an instruction has a single
--- 12 unchanged lines hidden (view full) ---
837
838 assert(TargetRegisterInfo::isVirtualRegister(regB) &&
839 "cannot make instruction into two-address form");
840
841 // If regA is dead and the instruction can be deleted, just delete
842 // it so it doesn't clobber regB.
843 bool regBKilled = isKilled(*mi, regB, MRI, TII);
844 if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
845 DeleteUnusedInstr(mi, nmi, mbbi, regB, SrcIdx, Dist)) {
846 ++NumDeletes;
847 return true; // Done with this instruction.
848 }
849
850 // Check if it is profitable to commute the operands.
851 unsigned SrcOp1, SrcOp2;
852 unsigned regC = 0;
853 unsigned regCIdx = ~0U;
--- 263 unchanged lines hidden ---