[llvm] [DAG] Add TRUNCATE_SSAT_S/U and TRUNCATE_USAT_U to canCreateUndefOrPoison (#152143) (PR #168809)

Jerry Dang via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 21 06:58:24 PST 2025


================
@@ -0,0 +1,64 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx2 | FileCheck %s
+
+; Test that freeze is eliminated for saturation truncate patterns.
+; The freeze elimination happens at the IR level due to the IntrNoCreateUndefOrPoison
+; attribute on the llvm.smax/smin/umin intrinsics. At the SelectionDAG level,
+; TRUNCATE_SSAT_S/U and TRUNCATE_USAT_U operations are also marked in
+; canCreateUndefOrPoison() to ensure consistency and enable potential future
+; optimizations. This test validates the end-to-end behavior that no freeze
+; instruction appears in the output.
----------------
kuroyukiasuna wrote:

```
> llvm-project % grep -r "TRUNCATE_SSAT\|TRUNCATE_USAT" llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | grep -i "known\|sign"
> llvm-project % grep -A 10 "computeKnownBits" llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | grep -i truncate
    // Implicitly truncate the bits to match the official semantics of
      // BUILD_VECTOR can implicitly truncate sources, we must handle this.
  case ISD::TRUNCATE: {
```
I searched for value tracking, seems there's no computeKnownBits or ComputeNumSignBits cases for TRUNCATE_SSAT_S/U or TRUNCATE_USAT_U, only regular TRUNCATE has value tracking implemented.

If we file another issue I can also tackle that in a follow-up PR, or I can have both in this PR. Will need some time (a few days) to ramp up & read the code.

https://github.com/llvm/llvm-project/pull/168809


More information about the llvm-commits mailing list