RFR: 8347459: C2: missing transformation for chain of shifts/multiplications by constants [v17]

Marc Chevalier duke at openjdk.org
Mon Mar 24 13:09:02 UTC 2025


> This collapses double shift lefts by constants in a single constant: (x << con1) << con2 => x << (con1 + con2). Care must be taken in the case con1 + con2 is bigger than the number of bits in the integer type. In this case, we must simplify to 0.
> 
> Moreover, the simplification logic of the sign extension trick had to be improved. For instance, we use `(x << 16) >> 16` to convert a 32 bits into a 16 bits integer, with sign extension. When storing this into a 16-bit field, this can be simplified into simple `x`. But in the case where `x` is itself a left-shift expression, say `y << 3`, this PR makes the IR looks like `(y << 19) >> 16` instead of the old `((y << 3) << 16) >> 16`. The former logic didn't handle the case where the left and the right shift have different magnitude. In this PR, I generalize this simplification to cases where the left shift has a larger magnitude than the right shift. This improvement was needed not to miss vectorization opportunities: without the simplification, we have a left shift and a right shift instead of a single left shift, which confuses the type inference.
> 
> This also works for multiplications by powers of 2 since they are already translated into shifts.
> 
> Thanks,
> Marc

Marc Chevalier has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 31 additional commits since the last revision:

 - Merge branch 'master' into fix/missing-transformation-for-chain-of-shifts-multiplications-by-constants
 - Rephrase comment
 - more checks
 - order
 - rephrase
 - correct
 - s
 - rephrased corner case
 - rephrase
 - char -> byte
 - ... and 21 more: https://git.openjdk.org/jdk/compare/50ec8d0a...cd0b0c09

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/23728/files
  - new: https://git.openjdk.org/jdk/pull/23728/files/124d9382..cd0b0c09

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=23728&range=16
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=23728&range=15-16

  Stats: 66044 lines in 1237 files changed: 32505 ins; 21388 del; 12151 mod
  Patch: https://git.openjdk.org/jdk/pull/23728.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23728/head:pull/23728

PR: https://git.openjdk.org/jdk/pull/23728


More information about the hotspot-compiler-dev mailing list