Skip to content

Commit

Permalink
Fold "FrozenObjectHandle(REF) + CNS" to a byref constant (#85888)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored May 9, 2023
1 parent 8c96a18 commit e338820
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6247,6 +6247,22 @@ GenTree* Lowering::LowerAdd(GenTreeOp* node)
return next;
}

// Fold ADD(CNS1, CNS2). We mainly target a very specific pattern - byref ADD(frozen_handle, cns_offset)
// We could do this folding earlier, but that is not trivial as we'll have to introduce a way to restore
// the original object from a byref constant for optimizations.
if (comp->opts.OptimizationEnabled() && op1->IsCnsIntOrI() && op2->IsCnsIntOrI() && !node->gtOverflow() &&
(op1->IsIconHandle(GTF_ICON_OBJ_HDL) || op2->IsIconHandle(GTF_ICON_OBJ_HDL)) &&
!op1->AsIntCon()->ImmedValNeedsReloc(comp) && !op2->AsIntCon()->ImmedValNeedsReloc(comp))
{
assert(node->TypeIs(TYP_I_IMPL, TYP_BYREF));

// TODO-CQ: we should allow this for AOT too. For that we need to guarantee that the new constant
// will be lowered as the original handle with offset in a reloc.
BlockRange().Remove(op1);
BlockRange().Remove(op2);
node->BashToConst(op1->AsIntCon()->IconValue() + op2->AsIntCon()->IconValue(), node->TypeGet());
}

#ifdef TARGET_XARCH
if (BlockRange().TryGetUse(node, &use))
{
Expand Down

0 comments on commit e338820

Please sign in to comment.