Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AreFlagsSetToZeroCmp to not consider unsupported formats #85714

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/coreclr/jit/emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,19 @@ class emitter
IS_INFO isInfo = emitGetSchedInfo(idInsFmt());
return (isInfo & (IS_AM_RW | IS_AM_WR)) != 0;
}

bool idHasMem() const
{
return idHasMemGen() || idHasMemStk() || idHasMemAdr();
}
bool idHasMemRead() const
{
return idHasMemGenRead() || idHasMemStkRead() || idHasMemAdrRead();
}
bool idHasMemWrite() const
{
return idHasMemGenWrite() || idHasMemStkWrite() || idHasMemAdrWrite();
}
#endif // defined(TARGET_XARCH)
#ifdef TARGET_ARMARCH
insOpts idInsOpt() const
Expand Down
24 changes: 20 additions & 4 deletions src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,21 +887,30 @@ bool emitter::AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, GenCondition
return false;
}

// Only consider if safe
//
if (!emitCanPeepholeLastIns())
{
// Don't consider if not safe
return false;
}

instrDesc* id = emitLastIns;
instruction lastIns = id->idIns();

if (!id->idHasReg1() || (id->idReg1() != reg))
if (!id->idIsReg1Write() || (id->idReg1() != reg))
{
// Don't consider instructions which didn't write a register
return false;
}

if (id->idHasMemWrite() || id->idIsReg2Write())
{
// Don't consider instructions which also wrote a mem location or second register
return false;
}

assert(!id->idIsReg3Write());
assert(!id->idIsReg4Write());

// Certain instruction like and, or and xor modifies exactly same flags
// as "test" instruction.
// They reset OF and CF to 0 and modifies SF, ZF and PF.
Expand Down Expand Up @@ -956,8 +965,15 @@ bool emitter::AreFlagsSetForSignJumpOpt(regNumber reg, emitAttr opSize, GenCondi
instruction lastIns = id->idIns();
insFormat fmt = id->idInsFmt();

if (!id->idHasReg1() || (id->idReg1() != reg))
if (!id->idIsReg1Write() || (id->idReg1() != reg))
{
// Don't consider instructions which didn't write a register
return false;
}

if (id->idHasMemWrite() || id->idIsReg2Write())
{
// Don't consider instructions which also wrote a mem location or second register
return false;
}
Comment on lines +968 to 978
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, the previous switch was:

    switch (fmt)
    {
        case IF_RWR_CNS:
        case IF_RRW_CNS:
        case IF_RRW_SHF:
        case IF_RWR_RRD:
        case IF_RRW_RRD:
        case IF_RWR_MRD:
        case IF_RWR_SRD:
        case IF_RRW_SRD:
        case IF_RWR_ARD:
        case IF_RRW_ARD:
        case IF_RWR:
        case IF_RRD:
        case IF_RRW:
            break;
        default:
            return false;
    }


Expand Down