[llvm] AMDGPU/GlobalISel: Fold (trunc (lshr x, 16)) to unmerge high half (PR #217633)

Krzysztof Drewniak via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 20 11:12:43 PDT 2026


================
@@ -216,6 +216,34 @@ def combine_or_s64_s32 : GICombineRule<
          (G_OR $or, $x_lo, $y),
          (G_MERGE_VALUES $dst, $or, $x_hi))>;
 
+// (trunc (lshr i32:$src, 16)) -> (unmerge_hi i16:$src)
+// Reads the high half in place instead of shifting it down into a fresh
+// 32-bit register; with true16 the unmerge is just a hi16 subregister.
+class trunc_lshr16_unmerge<ValueType ty, dag applyPat> : GICombineRule<
+  (defs root:$dst),
+  (match (G_CONSTANT i32:$c16, 16),
+         (G_LSHR i32:$shr, $src, $c16),
+         (G_TRUNC ty:$dst, $shr),
+         [{ return Helper.isLegalOrBeforeLegalizer(
+                {TargetOpcode::G_UNMERGE_VALUES,
+                 {LLT::integer(16), LLT::integer(32)}}); }]),
+  applyPat>;
+
+defvar hi_int = (apply (G_UNMERGE_VALUES i16:$lo, $dst, $src));
+
+// Float halves are unmerged in the integer domain and bitcast back.
+defvar hi_float = (apply (G_UNMERGE_VALUES i16:$lo, i16:$hi, $src),
+                         (G_BITCAST $dst, $hi));
+
+def trunc_lshr16_unmerge_i16  : trunc_lshr16_unmerge<i16,  hi_int>;
----------------
krzysz00 wrote:

Can't we iterate over all 16-bit types?

(I'm wondering about v2i8 and friends, though that might be a separate PR)

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


More information about the llvm-commits mailing list