1// RUN: mlir-opt -arith-unsigned-when-equivalent %s | FileCheck %s
2
3// CHECK-LABEL func @not_with_maybe_overflow
4// CHECK: arith.divsi
5// CHECK: arith.ceildivsi
6// CHECK: arith.floordivsi
7// CHECK: arith.remsi
8// CHECK: arith.minsi
9// CHECK: arith.maxsi
10// CHECK: arith.extsi
11// CHECK: arith.cmpi sle
12// CHECK: arith.cmpi slt
13// CHECK: arith.cmpi sge
14// CHECK: arith.cmpi sgt
15func.func @not_with_maybe_overflow(%arg0 : i32) {
16    %ci32_smax = arith.constant 0x7fffffff : i32
17    %c1 = arith.constant 1 : i32
18    %c4 = arith.constant 4 : i32
19    %0 = arith.minui %arg0, %ci32_smax : i32
20    %1 = arith.addi %0, %c1 : i32
21    %2 = arith.divsi %1, %c4 : i32
22    %3 = arith.ceildivsi %1, %c4 : i32
23    %4 = arith.floordivsi %1, %c4 : i32
24    %5 = arith.remsi %1, %c4 : i32
25    %6 = arith.minsi %1, %c4 : i32
26    %7 = arith.maxsi %1, %c4 : i32
27    %8 = arith.extsi %1 : i32 to i64
28    %9 = arith.cmpi sle, %1, %c4 : i32
29    %10 = arith.cmpi slt, %1, %c4 : i32
30    %11 = arith.cmpi sge, %1, %c4 : i32
31    %12 = arith.cmpi sgt, %1, %c4 : i32
32    func.return
33}
34
35// CHECK-LABEL func @yes_with_no_overflow
36// CHECK: arith.divui
37// CHECK: arith.ceildivui
38// CHECK: arith.divui
39// CHECK: arith.remui
40// CHECK: arith.minui
41// CHECK: arith.maxui
42// CHECK: arith.extui
43// CHECK: arith.cmpi ule
44// CHECK: arith.cmpi ult
45// CHECK: arith.cmpi uge
46// CHECK: arith.cmpi ugt
47func.func @yes_with_no_overflow(%arg0 : i32) {
48    %ci32_almost_smax = arith.constant 0x7ffffffe : i32
49    %c1 = arith.constant 1 : i32
50    %c4 = arith.constant 4 : i32
51    %0 = arith.minui %arg0, %ci32_almost_smax : i32
52    %1 = arith.addi %0, %c1 : i32
53    %2 = arith.divsi %1, %c4 : i32
54    %3 = arith.ceildivsi %1, %c4 : i32
55    %4 = arith.floordivsi %1, %c4 : i32
56    %5 = arith.remsi %1, %c4 : i32
57    %6 = arith.minsi %1, %c4 : i32
58    %7 = arith.maxsi %1, %c4 : i32
59    %8 = arith.extsi %1 : i32 to i64
60    %9 = arith.cmpi sle, %1, %c4 : i32
61    %10 = arith.cmpi slt, %1, %c4 : i32
62    %11 = arith.cmpi sge, %1, %c4 : i32
63    %12 = arith.cmpi sgt, %1, %c4 : i32
64    func.return
65}
66
67// CHECK-LABEL: func @preserves_structure
68// CHECK: scf.for %[[arg1:.*]] =
69// CHECK: %[[v:.*]] = arith.remui %[[arg1]]
70// CHECK: %[[w:.*]] = arith.addi %[[v]], %[[v]]
71// CHECK: %[[test:.*]] = arith.cmpi ule, %[[w]]
72// CHECK: scf.if %[[test]]
73// CHECK: memref.store %[[w]]
74func.func @preserves_structure(%arg0 : memref<8xindex>) {
75    %c0 = arith.constant 0 : index
76    %c1 = arith.constant 1 : index
77    %c4 = arith.constant 4 : index
78    %c8 = arith.constant 8 : index
79    scf.for %arg1 = %c0 to %c8 step %c1 {
80        %v = arith.remsi %arg1, %c4 : index
81        %w = arith.addi %v, %v : index
82        %test = arith.cmpi sle, %w, %c4 : index
83        scf.if %test {
84            memref.store %w, %arg0[%arg1] : memref<8xindex>
85        }
86    }
87    func.return
88}
89