[llvm] [CVP] Implement type narrowing for LShr (PR #119577)

via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 05:52:54 PST 2024


================
@@ -0,0 +1,114 @@
+; RUN: opt < %s -passes="correlated-propagation,instcombine" -S | FileCheck %s
+
+; The tests below are the same as in lshr.ll
+; Here we test whether the CorrelatedValuePropagation pass 
+; composed with InstCombinePass produces the expected optimizations.
+
+; CHECK-LABEL: @trunc_test1
+; CHECK-NEXT: [[A1:%.*]] = lshr i32 [[A:%.*]], 16
+; CHECK-NEXT: [[CARG:%.*]] = trunc nuw i32 [[A1]] to i16
+; CHECK-NEXT: [[CSHIFT:%.*]] = trunc i32 [[B:%.*]] to i16
+; CHECK-NEXT: [[C1:%.*]] = lshr i16 [[CARG]], [[CSHIFT]]
+; CHECK-NEXT: ret i16 [[C1]]
+
+define i16 @trunc_test1(i32 %a, i32 %b) {
----------------
adam-bzowski wrote:

1. The original motivation is a subtle difference between the division and right shift. Due to the lack of narrowing for LShr, UDivs may produce a better code, here's an example:

https://godbolt.org/z/7EPnj9a8d

With the proposed patch the two functions generate identical (better) code.

2. The incorrect transformation was fixed by checking if the maximal shift is smaller than the new bit width.

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


More information about the llvm-commits mailing list