[llvm] [LICM] Preserve nsw when reassociating integer adds (PR #221210)
Andrey Grabezhnoy via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 06:46:19 PDT 2026
================
@@ -429,6 +429,152 @@ loop:
br label %loop
}
+; Preserve NSW for constant invariant operands and enable hoistAdd().
+define void @add_nsw_constant_operands(i32 %start) {
+; CHECK-LABEL: @add_nsw_constant_operands(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[CONDITION:%.*]] = icmp sgt i32 [[IV]], -13
+; CHECK-NEXT: call void @llvm.assume(i1 [[CONDITION]])
+; CHECK-NEXT: [[NEXT]] = add i32 [[IV]], 1
+; CHECK-NEXT: br label [[LOOP]]
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i32 [ %start, %entry ], [ %next, %loop ]
+ %add1 = add nsw i32 %iv, 2
+ %add2 = add nsw i32 %add1, 4
+ %condition = icmp sgt i32 %add2, -7
+ call void @llvm.assume(i1 %condition)
+ %next = add i32 %iv, 1
+ br label %loop
+}
+
+; Preserve NSW when KnownBits proves nonconstant invariant operands safe.
+define void @add_nsw_nonconstant_operands(i32 %start, i16 %c1, i16 %c2) {
+; CHECK-LABEL: @add_nsw_nonconstant_operands(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[C1_EXT:%.*]] = zext i16 [[C1:%.*]] to i32
+; CHECK-NEXT: [[C2_EXT:%.*]] = zext i16 [[C2:%.*]] to i32
+; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nsw i32 [[C1_EXT]], [[C2_EXT]]
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[ADD2_REASS:%.*]] = add nsw i32 [[IV]], [[INVARIANT_OP]]
+; CHECK-NEXT: call void @use(i32 [[ADD2_REASS]])
+; CHECK-NEXT: [[NEXT]] = add i32 [[IV]], 1
+; CHECK-NEXT: br label [[LOOP]]
+;
+entry:
+ %c1.ext = zext i16 %c1 to i32
+ %c2.ext = zext i16 %c2 to i32
+ br label %loop
+
+loop:
+ %iv = phi i32 [ %start, %entry ], [ %next, %loop ]
+ %add1 = add nsw i32 %iv, %c1.ext
+ %add2 = add nsw i32 %add1, %c2.ext
+ call void @use(i32 %add2)
+ %next = add i32 %iv, 1
+ br label %loop
+}
+
+; Preserve NSW for vector invariant operands.
+define void @add_nsw_vector_operands(<2 x i32> %start, <2 x i16> %c1, <2 x i16> %c2) {
+; CHECK-LABEL: @add_nsw_vector_operands(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[C1_EXT:%.*]] = zext <2 x i16> [[C1:%.*]] to <2 x i32>
+; CHECK-NEXT: [[C2_EXT:%.*]] = zext <2 x i16> [[C2:%.*]] to <2 x i32>
+; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nsw <2 x i32> [[C1_EXT]], [[C2_EXT]]
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[IV:%.*]] = phi <2 x i32> [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[ADD2_REASS:%.*]] = add nsw <2 x i32> [[IV]], [[INVARIANT_OP]]
+; CHECK-NEXT: call void @use(<2 x i32> [[ADD2_REASS]])
+; CHECK-NEXT: [[NEXT]] = add <2 x i32> [[IV]], splat (i32 1)
+; CHECK-NEXT: br label [[LOOP]]
+;
+entry:
+ %c1.ext = zext <2 x i16> %c1 to <2 x i32>
+ %c2.ext = zext <2 x i16> %c2 to <2 x i32>
+ br label %loop
+
+loop:
+ %iv = phi <2 x i32> [ %start, %entry ], [ %next, %loop ]
+ %add1 = add nsw <2 x i32> %iv, %c1.ext
+ %add2 = add nsw <2 x i32> %add1, %c2.ext
+ call void @use(<2 x i32> %add2)
----------------
agrabezh wrote:
Done.
My original usage was based on the common pattern already used in this file,
but I agree that matching declarations make the new tests clearer.
I added separate scalar and vector opaque calls and updated all the new tests.
https://github.com/llvm/llvm-project/pull/221210
More information about the llvm-commits
mailing list