Skip to content
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
32 changes: 31 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4816,9 +4816,39 @@ bool Compiler::gtMarkAddrMode(GenTree* addr, int* pCostEx, int* pCostSz, var_typ
gtWalkOp(&op2, &op1, nullptr, true);
#endif // defined(TARGET_XARCH)

if ((mul > 1) && (op2 != nullptr) && op2->OperIs(GT_LSH, GT_MUL))
bool noCSE = (op2 != nullptr) && op2->OperIs(GT_LSH, GT_MUL);
#if defined(TARGET_RISCV64)
noCSE = noCSE && this->compOpportunisticallyDependsOn(InstructionSet_Zba);
#else
noCSE = noCSE && (mul > 1);
#endif // defined(TARGET_RISCV64)

if (noCSE)
{
op2->gtFlags |= GTF_ADDRMODE_NO_CSE;
#if defined(TARGET_RISCV64)
// RISC-V addressing mode follows the form: (base + index*scale) + offset.
// To emit sh1/2/3add.uw, GT_ADD + GT_LSH/MUL + GT_CAST(zero-extend) nodes are required (Zba extension).
// Disabling CSE for GT_CAST prevents breaking the pattern and ensures emitting sh1/2/3add.uw.
// Note that emitting sh1/2/3add instructions (without .uw) don't require a GT_CAST node.
//
// Example:
// ADD
// |- ADD
// | |- LCL_VAR (base)
// | |- LSH (or MUL) (index * scale)
// | |- GT_CAST (index, CSE must be disabled here to emit sh1/2/3add.uw)
// | |- OP1 (CSE/ConstCSE allowed here)
// | |- CNS_INT (scale)
// |- CNS_INT (offset)

GenTree* index = op2->gtGetOp1();
if ((index != nullptr) && index->OperIs(GT_CAST))
{
assert(index->TypeIs(TYP_I_IMPL));
index->gtFlags |= GTF_ADDRMODE_NO_CSE;
}
#endif // defined(TARGET_RISCV64)
}

// Finally, adjust the costs on the parenting COMMAs.
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ enum GenTreeFlags : unsigned

GTF_IND_FLAGS = GTF_IND_COPYABLE_FLAGS | GTF_IND_NONNULL | GTF_IND_TGT_NOT_HEAP | GTF_IND_TGT_HEAP | GTF_IND_INVARIANT | GTF_IND_ALLOW_NON_ATOMIC,

GTF_ADDRMODE_NO_CSE = 0x80000000, // GT_ADD/GT_MUL/GT_LSH -- Do not CSE this node only, forms complex
// addressing mode
GTF_ADDRMODE_NO_CSE = 0x80000000, // GT_ADD/GT_MUL/GT_LSH/GT_CAST -- Do not CSE this node only, forms complex
// addressing mode

GTF_MUL_64RSLT = 0x40000000, // GT_MUL -- produce 64-bit result

Expand Down Expand Up @@ -2202,7 +2202,7 @@ struct GenTree

bool IsPartOfAddressMode()
{
return OperIs(GT_ADD, GT_MUL, GT_LSH) && ((gtFlags & GTF_ADDRMODE_NO_CSE) != 0);
return OperIs(GT_ADD, GT_MUL, GT_LSH, GT_CAST) && ((gtFlags & GTF_ADDRMODE_NO_CSE) != 0);
}

void SetAllEffectsFlags(GenTree* source)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/optcse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,6 @@ bool CSE_HeuristicCommon::CanConsiderTree(GenTree* tree, bool isReturn)
case GT_NOT:
case GT_BSWAP:
case GT_BSWAP16:
case GT_CAST:
case GT_BITCAST:
break;

Expand All @@ -1943,9 +1942,10 @@ bool CSE_HeuristicCommon::CanConsiderTree(GenTree* tree, bool isReturn)
case GT_ROR:
break;

case GT_ADD: // Check for ADDRMODE flag on these Binary Operators
case GT_ADD: // Check for ADDRMODE flag on these operators
case GT_MUL:
case GT_LSH:
case GT_CAST:
if (tree->IsPartOfAddressMode())
{
return false;
Expand Down
Loading