From 3f62940561760fe5dcc8675853be57ee4ac8069a Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 3 Sep 2008 15:56:16 +0000 Subject: [PATCH] Fix addRegisterDead and addRegisterKilled to be more thorough when searching for redundant subregister dead/kill bits. Previously it was common to see instructions marked like this: "RET %EAX, %AX" With this change, addRegisterKilled continues scanning after finding the %EAX operand, so it proceeds to discover the redundant %AX kill and eliminates it, producing this: "RET %EAX" This currently has no effect on the generated code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55698 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/MachineInstr.cpp | 53 +++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/CodeGen/MachineInstr.cpp b/lib/CodeGen/MachineInstr.cpp index ce5c5235b82..9fbae684799 100644 --- a/lib/CodeGen/MachineInstr.cpp +++ b/lib/CodeGen/MachineInstr.cpp @@ -800,6 +800,7 @@ bool MachineInstr::addRegisterKilled(unsigned IncomingReg, bool AddIfNotFound) { bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); + bool Found = false; SmallVector DeadOps; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { MachineOperand &MO = getOperand(i); @@ -810,11 +811,15 @@ bool MachineInstr::addRegisterKilled(unsigned IncomingReg, continue; if (Reg == IncomingReg) { - MO.setIsKill(); - return true; - } - if (hasAliases && MO.isKill() && - TargetRegisterInfo::isPhysicalRegister(Reg)) { + if (!Found) { + if (MO.isKill()) + // The register is already marked kill. + return true; + MO.setIsKill(); + Found = true; + } + } else if (hasAliases && MO.isKill() && + TargetRegisterInfo::isPhysicalRegister(Reg)) { // A super-register kill already exists. if (RegInfo->isSuperRegister(IncomingReg, Reg)) return true; @@ -835,14 +840,14 @@ bool MachineInstr::addRegisterKilled(unsigned IncomingReg, // If not found, this means an alias of one of the operands is killed. Add a // new implicit operand if required. - if (AddIfNotFound) { + if (!Found && AddIfNotFound) { addOperand(MachineOperand::CreateReg(IncomingReg, false /*IsDef*/, true /*IsImp*/, true /*IsKill*/)); return true; } - return false; + return Found; } bool MachineInstr::addRegisterDead(unsigned IncomingReg, @@ -850,18 +855,26 @@ bool MachineInstr::addRegisterDead(unsigned IncomingReg, bool AddIfNotFound) { bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); + bool Found = false; SmallVector DeadOps; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { MachineOperand &MO = getOperand(i); if (!MO.isRegister() || !MO.isDef()) continue; unsigned Reg = MO.getReg(); + if (!Reg) + continue; + if (Reg == IncomingReg) { - MO.setIsDead(); - return true; - } - if (hasAliases && MO.isDead() && - TargetRegisterInfo::isPhysicalRegister(Reg)) { + if (!Found) { + if (MO.isDead()) + // The register is already marked dead. + return true; + MO.setIsDead(); + Found = true; + } + } else if (hasAliases && MO.isDead() && + TargetRegisterInfo::isPhysicalRegister(Reg)) { // There exists a super-register that's marked dead. if (RegInfo->isSuperRegister(IncomingReg, Reg)) return true; @@ -882,13 +895,15 @@ bool MachineInstr::addRegisterDead(unsigned IncomingReg, DeadOps.pop_back(); } - // If not found, this means an alias of one of the operand is dead. Add a - // new implicit operand. - if (AddIfNotFound) { - addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/, - true/*IsImp*/,false/*IsKill*/, - true/*IsDead*/)); + // If not found, this means an alias of one of the operands is dead. Add a + // new implicit operand if required. + if (!Found && AddIfNotFound) { + addOperand(MachineOperand::CreateReg(IncomingReg, + true /*IsDef*/, + true /*IsImp*/, + false /*IsKill*/, + true /*IsDead*/)); return true; } - return false; + return Found; }