Index: include/llvm/CodeGen/TargetInstrInfo.h =================================================================== --- include/llvm/CodeGen/TargetInstrInfo.h +++ include/llvm/CodeGen/TargetInstrInfo.h @@ -503,6 +503,16 @@ RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const; + /// \returns true if it's safe insert an instruction that would clobber the \p + /// Reg physical register. Note the result may be conservative. If it cannot + /// definitely determine the safety after visiting \p MaxInstructions + /// instructions in each direction it assumes it's not safe. + bool isSafeToClobberReg(MachineBasicBlock &MBB, + MachineBasicBlock::iterator I, + const TargetRegisterInfo &TRI, + unsigned Reg, + unsigned MaxInstructions) const; + /// Return true if two machine instructions would produce identical values. /// By default, this is only true when the two instructions /// are deemed identical except for defs. If this function is called when the Index: lib/CodeGen/TargetInstrInfo.cpp =================================================================== --- lib/CodeGen/TargetInstrInfo.cpp +++ lib/CodeGen/TargetInstrInfo.cpp @@ -1229,3 +1229,103 @@ InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm(); return true; } + +bool TargetInstrInfo::isSafeToClobberReg(MachineBasicBlock &MBB, + MachineBasicBlock::iterator I, + const TargetRegisterInfo &TRI, + const unsigned Reg, + unsigned MaxInstructions) const { + assert(TargetRegisterInfo::isPhysicalRegister(Reg) && + "this is only for physical registers"); + + MachineBasicBlock::iterator E = MBB.end(); + + // For compile time consideration, if we are not able to determine the + // safety after visiting 4 instructions in each direction, we will assume + // it's not safe. + MachineBasicBlock::iterator Iter = I; + for (unsigned i = 0; Iter != E && i < MaxInstructions; ++i) { + bool SeenDef = false; + for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) { + MachineOperand &MO = Iter->getOperand(j); + if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) + SeenDef = true; + if (!MO.isReg()) + continue; + if (MO.getReg() == Reg) { + if (MO.isUse()) + return false; + SeenDef = true; + } + } + + if (SeenDef) { + // This instruction defines Reg, no need to look any further. + return true; + } + + ++Iter; + // Skip over debug instructions. + while (Iter != E && Iter->isDebugInstr()) + ++Iter; + } + + // It is safe to clobber Reg at the end of a block of no successor has it + // live in. + if (Iter == E) { + for (MachineBasicBlock *S : MBB.successors()) { + for (MCSubRegIterator SubReg(Reg, &TRI, /*IncludeSelf*/true); + SubReg.isValid(); ++SubReg) { + if (S->isLiveIn(*SubReg)) + return false; + } + } + + return true; + } + + MachineBasicBlock::iterator B = MBB.begin(); + Iter = I; + for (unsigned i = 0; i < MaxInstructions; ++i) { + // If we make it to the beginning of the block, it's safe to clobber + // Reg iff Reg is not live-in. + if (Iter == B) { + for (MCSubRegIterator SubReg(Reg, &TRI, /*IncludeSelf*/true); + SubReg.isValid(); ++SubReg) { + if (MBB.isLiveIn(Reg)) + return false; + } + + return true; + } + + --Iter; + // Skip over debug instructions. + while (Iter != B && Iter->isDebugInstr()) + --Iter; + + bool SawKill = false; + for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) { + MachineOperand &MO = Iter->getOperand(j); + // A register mask may clobber Reg, but we should still look for a + // live Reg def. + if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) + SawKill = true; + if (MO.isReg() && MO.getReg() == Reg) { + if (MO.isDef()) + return MO.isDead(); + if (MO.isKill()) + SawKill = true; + } + } + + if (SawKill) { + // This instruction kills Reg and doesn't redefine it, so + // there's no need to look further. + return true; + } + } + + // Conservative answer. + return false; +} Index: lib/Target/X86/X86InstrInfo.h =================================================================== --- lib/Target/X86/X86InstrInfo.h +++ lib/Target/X86/X86InstrInfo.h @@ -456,7 +456,9 @@ /// conservative. If it cannot definitely determine the safety after visiting /// a few instructions in each direction it assumes it's not safe. bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB, - MachineBasicBlock::iterator I) const; + MachineBasicBlock::iterator I) const { + return isSafeToClobberReg(MBB, I, RI, X86::EFLAGS, 4); + } /// True if MI has a condition code def, e.g. EFLAGS, that is /// not marked dead. Index: lib/Target/X86/X86InstrInfo.cpp =================================================================== --- lib/Target/X86/X86InstrInfo.cpp +++ lib/Target/X86/X86InstrInfo.cpp @@ -579,83 +579,6 @@ return true; } -bool X86InstrInfo::isSafeToClobberEFLAGS(MachineBasicBlock &MBB, - MachineBasicBlock::iterator I) const { - MachineBasicBlock::iterator E = MBB.end(); - - // For compile time consideration, if we are not able to determine the - // safety after visiting 4 instructions in each direction, we will assume - // it's not safe. - MachineBasicBlock::iterator Iter = I; - for (unsigned i = 0; Iter != E && i < 4; ++i) { - bool SeenDef = false; - for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) { - MachineOperand &MO = Iter->getOperand(j); - if (MO.isRegMask() && MO.clobbersPhysReg(X86::EFLAGS)) - SeenDef = true; - if (!MO.isReg()) - continue; - if (MO.getReg() == X86::EFLAGS) { - if (MO.isUse()) - return false; - SeenDef = true; - } - } - - if (SeenDef) - // This instruction defines EFLAGS, no need to look any further. - return true; - ++Iter; - // Skip over debug instructions. - while (Iter != E && Iter->isDebugInstr()) - ++Iter; - } - - // It is safe to clobber EFLAGS at the end of a block of no successor has it - // live in. - if (Iter == E) { - for (MachineBasicBlock *S : MBB.successors()) - if (S->isLiveIn(X86::EFLAGS)) - return false; - return true; - } - - MachineBasicBlock::iterator B = MBB.begin(); - Iter = I; - for (unsigned i = 0; i < 4; ++i) { - // If we make it to the beginning of the block, it's safe to clobber - // EFLAGS iff EFLAGS is not live-in. - if (Iter == B) - return !MBB.isLiveIn(X86::EFLAGS); - - --Iter; - // Skip over debug instructions. - while (Iter != B && Iter->isDebugInstr()) - --Iter; - - bool SawKill = false; - for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) { - MachineOperand &MO = Iter->getOperand(j); - // A register mask may clobber EFLAGS, but we should still look for a - // live EFLAGS def. - if (MO.isRegMask() && MO.clobbersPhysReg(X86::EFLAGS)) - SawKill = true; - if (MO.isReg() && MO.getReg() == X86::EFLAGS) { - if (MO.isDef()) return MO.isDead(); - if (MO.isKill()) SawKill = true; - } - } - - if (SawKill) - // This instruction kills EFLAGS and doesn't redefine it, so - // there's no need to look further. - return true; - } - - // Conservative answer. - return false; -} - void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg, unsigned SubIdx,