Skip to content

Commit 99dd33b

Browse files
XARCH: Remove redudant tests for GT_LT/GT_GE relops. (#61152)
* XARCH: Remove redudant tests for GT_LT/GT_GE relops. We can now optimize cases such as `(x + y < 0)` or `for (int x = v; x >= 0; x--)` using the flag tracking logic during the emit stage. Notably, cases that would generate... ``` add reg0, reg1 test reg0, reg0 jge LABEL ``` now transform to ``` add reg0, reg1 jns LABEL ``` This transform is valid for signed GE and signed LT only. * Add a few asserts related to flag reuse optimizations.
1 parent c737141 commit 99dd33b

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

src/coreclr/jit/codegenlinear.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,16 @@ void CodeGen::genCodeForJumpTrue(GenTreeOp* jtrue)
26312631

26322632
condition = GenCondition(GenCondition::P);
26332633
}
2634+
2635+
if (relop->MarkedForSignJumpOpt())
2636+
{
2637+
// If relop was previously marked for a signed jump check optimization because of SF flag
2638+
// reuse, replace jge/jl with jns/js.
2639+
2640+
assert(relop->OperGet() == GT_LT || relop->OperGet() == GT_GE);
2641+
condition = (relop->OperGet() == GT_LT) ? GenCondition(GenCondition::S) : GenCondition(GenCondition::NS);
2642+
}
2643+
26342644
#endif
26352645

26362646
inst_JCC(condition, compiler->compCurBB->bbJumpDest);

src/coreclr/jit/codegenxarch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6227,11 +6227,18 @@ void CodeGen::genCompareInt(GenTree* treeNode)
62276227
assert(genTypeSize(type) <= genTypeSize(TYP_I_IMPL));
62286228
// TYP_UINT and TYP_ULONG should not appear here, only small types can be unsigned
62296229
assert(!varTypeIsUnsigned(type) || varTypeIsSmall(type));
6230+
// Sign jump optimization should only be set the following check
6231+
assert((tree->gtFlags & GTF_RELOP_SJUMP_OPT) == 0);
62306232

62316233
if (canReuseFlags && emit->AreFlagsSetToZeroCmp(op1->GetRegNum(), emitTypeSize(type), tree->OperGet()))
62326234
{
62336235
JITDUMP("Not emitting compare due to flags being already set\n");
62346236
}
6237+
else if (canReuseFlags && emit->AreFlagsSetForSignJumpOpt(op1->GetRegNum(), emitTypeSize(type), tree))
6238+
{
6239+
JITDUMP("Not emitting compare due to sign being already set, follow up instr will transform jump\n");
6240+
tree->gtFlags |= GTF_RELOP_SJUMP_OPT;
6241+
}
62356242
else
62366243
{
62376244
emit->emitInsBinary(ins, emitTypeSize(type), op1, op2);

src/coreclr/jit/emitxarch.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ bool emitter::DoesWriteZeroFlag(instruction ins)
164164
return (CodeGenInterface::instInfo[ins] & Writes_ZF) != 0;
165165
}
166166

167+
//------------------------------------------------------------------------
168+
// DoesWriteSignFlag: check if the instruction writes the
169+
// SF flag.
170+
//
171+
// Arguments:
172+
// ins - instruction to test
173+
//
174+
// Return Value:
175+
// true if instruction writes the SF flag, false otherwise.
176+
//
177+
bool emitter::DoesWriteSignFlag(instruction ins)
178+
{
179+
return (CodeGenInterface::instInfo[ins] & Writes_SF) != 0;
180+
}
181+
167182
//------------------------------------------------------------------------
168183
// DoesResetOverflowAndCarryFlags: check if the instruction resets the
169184
// OF and CF flag to 0.
@@ -338,6 +353,11 @@ bool emitter::AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, genTreeOps tr
338353
{
339354
assert(reg != REG_NA);
340355

356+
if (!emitComp->opts.OptimizationEnabled())
357+
{
358+
return false;
359+
}
360+
341361
// Don't look back across IG boundaries (possible control flow)
342362
if (emitCurIGinsCnt == 0 && ((emitCurIG->igFlags & IGF_EXTEND) == 0))
343363
{
@@ -393,6 +413,79 @@ bool emitter::AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, genTreeOps tr
393413
return false;
394414
}
395415

416+
//------------------------------------------------------------------------
417+
// AreFlagsSetToForSignJumpOpt: checks if the previous instruction set the SF if the tree
418+
// node qualifies for a jg/jle to jns/js optimization
419+
//
420+
// Arguments:
421+
// reg - register of interest
422+
// opSize - size of register
423+
// relop - relational tree node
424+
//
425+
// Return Value:
426+
// true if the tree node qualifies for the jg/jle to jns/js optimization
427+
// false if not, or if we can't safely determine
428+
//
429+
// Notes:
430+
// Currently only looks back one instruction.
431+
bool emitter::AreFlagsSetForSignJumpOpt(regNumber reg, emitAttr opSize, GenTree* relop)
432+
{
433+
assert(reg != REG_NA);
434+
435+
if (!emitComp->opts.OptimizationEnabled())
436+
{
437+
return false;
438+
}
439+
440+
// Don't look back across IG boundaries (possible control flow)
441+
if (emitCurIGinsCnt == 0 && ((emitCurIG->igFlags & IGF_EXTEND) == 0))
442+
{
443+
return false;
444+
}
445+
446+
instrDesc* id = emitLastIns;
447+
instruction lastIns = id->idIns();
448+
insFormat fmt = id->idInsFmt();
449+
450+
// make sure op1 is a reg
451+
switch (fmt)
452+
{
453+
case IF_RWR_CNS:
454+
case IF_RRW_CNS:
455+
case IF_RRW_SHF:
456+
case IF_RWR_RRD:
457+
case IF_RRW_RRD:
458+
case IF_RWR_MRD:
459+
case IF_RWR_SRD:
460+
case IF_RRW_SRD:
461+
case IF_RWR_ARD:
462+
case IF_RRW_ARD:
463+
case IF_RWR:
464+
case IF_RRD:
465+
case IF_RRW:
466+
break;
467+
default:
468+
return false;
469+
}
470+
471+
if (id->idReg1() != reg)
472+
{
473+
return false;
474+
}
475+
476+
// If we have a GT_GE/GT_LT which generates an jge/jl, and the previous instruction
477+
// sets the SF, we can omit a test instruction and check for jns/js.
478+
if ((relop->OperGet() == GT_GE || relop->OperGet() == GT_LT) && !GenCondition::FromRelop(relop).IsUnsigned())
479+
{
480+
if (DoesWriteSignFlag(lastIns) && IsFlagsAlwaysModified(id))
481+
{
482+
return id->idOpSize() == opSize;
483+
}
484+
}
485+
486+
return false;
487+
}
488+
396489
//------------------------------------------------------------------------
397490
// IsDstSrcImmAvxInstruction: Checks if the instruction has a "reg, reg/mem, imm" or
398491
// "reg/mem, reg, imm" form for the legacy, VEX, and EVEX

src/coreclr/jit/emitxarch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static bool IsJmpInstruction(instruction ins);
116116
bool AreUpper32BitsZero(regNumber reg);
117117

118118
bool AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, genTreeOps treeOps);
119+
bool AreFlagsSetForSignJumpOpt(regNumber reg, emitAttr opSize, GenTree* tree);
119120

120121
bool hasRexPrefix(code_t code)
121122
{
@@ -190,6 +191,7 @@ void SetContains256bitAVX(bool value)
190191
bool IsDstDstSrcAVXInstruction(instruction ins);
191192
bool IsDstSrcSrcAVXInstruction(instruction ins);
192193
bool DoesWriteZeroFlag(instruction ins);
194+
bool DoesWriteSignFlag(instruction ins);
193195
bool DoesResetOverflowAndCarryFlags(instruction ins);
194196
bool IsFlagsAlwaysModified(instrDesc* id);
195197

src/coreclr/jit/gentree.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ enum GenTreeFlags : unsigned int
531531
GTF_RELOP_QMARK = 0x20000000, // GT_<relop> -- the node is the condition for ?:
532532
GTF_RELOP_ZTT = 0x08000000, // GT_<relop> -- Loop test cloned for converting while-loops into do-while
533533
// with explicit "loop test" in the header block.
534+
GTF_RELOP_SJUMP_OPT = 0x04000000, // GT_<relop> -- Swap signed jl/jge with js/jns during emitter, reuses flags
535+
// from previous instruction.
534536

535537
GTF_JCMP_EQ = 0x80000000, // GTF_JCMP_EQ -- Branch on equal rather than not equal
536538
GTF_JCMP_TST = 0x40000000, // GTF_JCMP_TST -- Use bit test instruction rather than compare against zero instruction
@@ -3028,6 +3030,13 @@ struct GenTreeOp : public GenTreeUnOp
30283030
{
30293031
}
30303032
#endif
3033+
3034+
// True if this relop is marked for a transform during the emitter
3035+
// phase, e.g., jge => jns
3036+
bool MarkedForSignJumpOpt() const
3037+
{
3038+
return (gtFlags & GTF_RELOP_SJUMP_OPT) != 0;
3039+
}
30313040
};
30323041

30333042
struct GenTreeVal : public GenTree

0 commit comments

Comments
 (0)