RFR: 8353551: C2: Constant folding for ReverseBytes nodes [v2]

Vladimir Ivanov vlivanov at openjdk.org
Tue Apr 8 20:00:13 UTC 2025


On Tue, 8 Apr 2025 08:12:09 GMT, Hannes Greule <hgreule at openjdk.org> wrote:

>> I didn’t intend to advocate for waiting for that PR. `try_cast` is tiny and could be added independently. I have just been looking for things that could generally help with unifying TypeInt/TypeLong implementations.
>
> @j3graham I mainly want to avoid conflicts or duplicated solutions. I meant that after #17508 this code can be further generalized anyway, allowing to use the `try_cast` function then. This can obviously happen in a separate PR, if this one is integrated before.
> 
> @iwanowww I'm not sure if that's possible without more duplication. We need to choose the correct byteswap implementation depending on the node's type. Please let me know if I'm missing something.

It does have some duplication, but simply in a different place. I wouldn't say there's more of it:

static const Type* reverse_bytes(int op, const Type* con) {
  switch (op) {
    case Op_ReverseBytesS:  return TypeInt::make(byteswap(checked_cast<jshort>(con->is_int()->get_con())));
    case Op_ReverseBytesUS: return TypeInt::make(byteswap(checked_cast<jchar> (con->is_int()->get_con())));
    case Op_ReverseBytesI:  return TypeInt::make(byteswap(con->is_int()->get_con()));
    case Op_ReverseBytesL:  return TypeLong::make(byteswap(con->is_long()->get_con()));

    default: ShouldNotReachHere();
  }
}

const Type* ReverseBytesNode::Value(PhaseGVN* phase) const {
  const Type* type = phase->type(in(1));
  if (type == Type::TOP) {
    return Type::TOP;
  }
  if (type->singleton()) {
    return reverse_bytes(Opcode(), type);
  }
  return bottom_type();
}


At some point, we should consider folding `ReverseBytes*Node` specializations into a single one (`ReverseBytesNode`) parameterized by element type (as was done for `ReverseBytesV` and other vector nodes). After it is done, there won't be a convenient way to piggyback on virtual calls to hook specialized versions forcing us to do explicit dispatch anyway.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24382#discussion_r2033930617


More information about the hotspot-compiler-dev mailing list