https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122021
--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Pan Li <[email protected]>: https://gcc.gnu.org/g:7a5da9ab53a8c4b6ffe03ef569b3ffe0ef096878 commit r16-4021-g7a5da9ab53a8c4b6ffe03ef569b3ffe0ef096878 Author: Pan Li <[email protected]> Date: Mon Sep 22 14:31:41 2025 +0800 Widen-Mul: Fix mis-compile for build_and_insert_cast refinement [PR122021] The previous refinement in build_and_insert_cast will convert 2 cast into one, aka: uint16_t _3; From: int16_t _4 = (uint16_t)_3; // no-extend int32_t _5 = (int32_t)_4 // sign-extend 16 => 32 To: int32_t _5 = (int32_t)_3; // zero-extend 16 => 32 That will have a problem for sign-extend, the highest bits may be all 1s but will be loss after convert to zero-extend. Thus, there will be more cases if the convert has different types. Case 1 as above and Case 2, 3, and 4 as following. Case 2: int16_t _3; From: uint32_t _4 = (uint32_t)_3; // zero-extend 16 => 32 uint64_t _5 = (uint64_t)_4; // zero-extend 32 => 64 To: uint64_t _5 = (uint32_t)_3; // zero-extend 16 => 64 Case 3: uint8_t _3; From: uint16_t _4 = (uint16_t)_3; // zero-extend 8 => 16 int32_t _5 = (int32_t)_4; // zero-extend 16 => 32 To: int32_t _5 = (int32_t)_3; // zero-extend 8 => 32 Case 4: int8_t _3; From: int16_t _4 = (int16_t)_3; // sign-extend 8 => 16 uint32_t _5 = (uint32_t)_4; // zero-extend 16 => 32 To: uint32_t _5 = (uint32_t)_3; // zero-extend 8 => 32 Then, we can see, there will be mis-compile if and only if there is a cast from small to big size with sign extend. Thus, restrict the check and stop prop if there is sign extend cast. The below test suites are passed for this patch: 1. The rv64gcv fully regression tests. 2. The x86 bootstrap tests. 3. The x86 fully regression tests. PR middle-end/122021 gcc/ChangeLog: * tree-ssa-math-opts.cc (build_and_insert_cast): Add sign-extend check before prop. gcc/testsuite/ChangeLog: * gcc.target/i386/pr122021-0.c: New test. Signed-off-by: Pan Li <[email protected]>
