702 lines · plain
1// RUN: mlir-opt %s -test-scf-for-utils --split-input-file | FileCheck %s2 3// CHECK-LABEL: func.func @trip_count_index_zero_to_zero(4func.func @trip_count_index_zero_to_zero(%a : i32, %b : i32) -> i32 {5 %c0 = arith.constant 0 : index6 %c1 = arith.constant 1 : index7 8 // CHECK: "test.trip-count" = 09 %r = scf.for %i = %c0 to %c0 step %c1 iter_args(%0 = %a) -> i32 {10 scf.yield %b : i3211 }12 return %r : i3213}14 15// -----16 17// CHECK-LABEL: func.func @trip_count_index_zero_to_zero_step_dyn(18func.func @trip_count_index_zero_to_zero_step_dyn(%a : i32, %b : i32, %step : index) -> i32 {19 %c0 = arith.constant 0 : index20 21 // CHECK: "test.trip-count" = 022 %r = scf.for %i = %c0 to %c0 step %step iter_args(%0 = %a) -> i32 {23 scf.yield %b : i3224 }25 return %r : i3226}27 28// -----29 30// CHECK-LABEL: func.func @trip_count_i32_zero_to_zero(31func.func @trip_count_i32_zero_to_zero(%a : i32, %b : i32) -> i32 {32 %c0 = arith.constant 0 : i3233 %c1 = arith.constant 1 : i3234 35 // CHECK: "test.trip-count" = 036 %r = scf.for %i = %c0 to %c0 step %c1 iter_args(%0 = %a) -> i32 : i32 {37 scf.yield %b : i3238 }39 return %r : i3240}41 42// -----43 44 45// CHECK-LABEL: func.func @trip_count_i32_zero_to_zero_step_dyn(46func.func @trip_count_i32_zero_to_zero_step_dyn(%a : i32, %b : i32, %step : i32) -> i32 {47 %c0 = arith.constant 0 : i3248 49 // CHECK: "test.trip-count" = 050 %r = scf.for %i = %c0 to %c0 step %step iter_args(%0 = %a) -> i32 : i32 {51 scf.yield %b : i3252 }53 return %r : i3254}55 56// -----57 58// CHECK-LABEL: func.func @trip_count_index_one_to_zero(59func.func @trip_count_index_one_to_zero(%a : i32, %b : i32) -> i32 {60 %c0 = arith.constant 0 : index61 %c1 = arith.constant 1 : index62 63 // Index type has a unknown bitwidth, we can't compute a loop tripcount64 // in theory because of overflow concerns.65 // CHECK: "test.trip-count" = 066 %r2 = scf.for %i = %c1 to %c0 step %c1 iter_args(%0 = %a) -> i32 {67 scf.yield %b : i3268 }69 return %r2 : i3270}71 72// -----73 74// CHECK-LABEL: func.func @trip_count_i32_one_to_zero(75func.func @trip_count_i32_one_to_zero(%a : i32, %b : i32) -> i32 {76 %c0 = arith.constant 0 : i3277 %c1 = arith.constant 1 : i3278 79 // CHECK: "test.trip-count" = 080 %r2 = scf.for %i = %c1 to %c0 step %c1 iter_args(%0 = %a) -> i32 : i32 {81 scf.yield %b : i3282 }83 return %r2 : i3284}85 86// -----87 88// CHECK-LABEL: func.func @trip_count_i32_one_to_zero_dyn_step(89func.func @trip_count_i32_one_to_zero_dyn_step(%a : i32, %b : i32, %step : i32) -> i32 {90 %c0 = arith.constant 0 : i3291 %c1 = arith.constant 1 : i3292 93 // CHECK: "test.trip-count" = 094 %r2 = scf.for %i = %c1 to %c0 step %step iter_args(%0 = %a) -> i32 : i32 {95 scf.yield %b : i3296 }97 return %r2 : i3298}99 100// -----101 102// CHECK-LABEL: func.func @trip_count_index_negative_step(103func.func @trip_count_index_negative_step(%a : i32, %b : i32) -> i32 {104 %c0 = arith.constant 0 : index105 %c1 = arith.constant 1 : index106 %c-1 = arith.constant -1 : index107 108 // Negative step is invalid, loop won't execute.109 // CHECK: "test.trip-count" = 0110 %r3 = scf.for %i = %c1 to %c0 step %c-1 iter_args(%0 = %a) -> i32 {111 scf.yield %b : i32112 }113 return %r3 : i32114}115 116// -----117 118// CHECK-LABEL: func.func @trip_count_i32_negative_step(119func.func @trip_count_i32_negative_step(%a : i32, %b : i32) -> i32 {120 %c0 = arith.constant 0 : i32121 %c1 = arith.constant 1 : i32122 %c-1 = arith.constant -1 : i32123 124 // Negative step is invalid, loop won't execute.125 // CHECK: "test.trip-count" = 0126 %r3 = scf.for %i = %c1 to %c0 step %c-1 iter_args(%0 = %a) -> i32 : i32 {127 scf.yield %b : i32128 }129 return %r3 : i32130}131 132// -----133 134// CHECK-LABEL: func.func @trip_count_index_negative_step_unsigned_loop(135func.func @trip_count_index_negative_step_unsigned_loop(%a : i32, %b : i32) -> i32 {136 %c0 = arith.constant 0 : index137 %c1 = arith.constant 1 : index138 %c-1 = arith.constant -1 : index139 140 // Negative step is invalid, loop won't execute.141 // CHECK: "test.trip-count" = 0142 %r3 = scf.for unsigned %i = %c1 to %c0 step %c-1 iter_args(%0 = %a) -> i32 {143 scf.yield %b : i32144 }145 return %r3 : i32146}147 148// -----149 150// CHECK-LABEL: func.func @trip_count_i32_negative_step_unsigned_loop(151func.func @trip_count_i32_negative_step_unsigned_loop(%a : i32, %b : i32) -> i32 {152 %c0 = arith.constant 0 : i32153 %c1 = arith.constant 1 : i32154 %c-1 = arith.constant -1 : i32155 156 // Negative step is invalid, loop won't execute.157 // CHECK: "test.trip-count" = 0158 %r3 = scf.for unsigned %i = %c1 to %c0 step %c-1 iter_args(%0 = %a) -> i32 : i32 {159 scf.yield %b : i32160 }161 return %r3 : i32162}163 164// -----165 166// CHECK-LABEL: func.func @trip_count_index_normal_loop(167func.func @trip_count_index_normal_loop(%a : i32, %b : i32) -> i32 {168 %c0 = arith.constant 0 : index169 %c2 = arith.constant 2 : index170 %c10 = arith.constant 10 : index171 172 // Index type has a unknown bitwidth, we can't compute a loop tripcount173 // in theory because of overflow concerns.174 // CHECK: "test.trip-count" = 5175 %r4 = scf.for %i = %c0 to %c10 step %c2 iter_args(%0 = %a) -> i32 {176 scf.yield %b : i32177 }178 return %r4 : i32179}180 181// -----182 183// CHECK-LABEL: func.func @trip_count_i32_normal_loop(184func.func @trip_count_i32_normal_loop(%a : i32, %b : i32) -> i32 {185 %c0 = arith.constant 0 : i32186 %c2 = arith.constant 2 : i32187 %c10 = arith.constant 10 : i32188 189 // Normal loop190 // CHECK: "test.trip-count" = 5191 %r4 = scf.for %i = %c0 to %c10 step %c2 iter_args(%0 = %a) -> i32 : i32 {192 scf.yield %b : i32193 }194 return %r4 : i32195}196 197// -----198 199// CHECK-LABEL: func.func @trip_count_index_signed_crossing_zero(200func.func @trip_count_index_signed_crossing_zero(%a : i32, %b : i32) -> i32 {201 %c-1 = arith.constant -1 : index202 %c1 = arith.constant 1 : index203 204 // Index type has a unknown bitwidth, we can't compute a loop tripcount205 // in theory because of overflow concerns.206 // CHECK: "test.trip-count" = 2207 %r5 = scf.for %i = %c-1 to %c1 step %c1 iter_args(%0 = %a) -> i32 {208 scf.yield %b : i32209 }210 return %r5 : i32211}212 213// -----214 215// CHECK-LABEL: func.func @trip_count_i32_signed_crossing_zero(216func.func @trip_count_i32_signed_crossing_zero(%a : i32, %b : i32) -> i32 {217 %c-1 = arith.constant -1 : i32218 %c1 = arith.constant 1 : i32219 220 // This loop execute with signed comparison, but not unsigned, because it is crossing 0.221 // CHECK: "test.trip-count" = 2222 %r5 = scf.for %i = %c-1 to %c1 step %c1 iter_args(%0 = %a) -> i32 : i32 {223 scf.yield %b : i32224 }225 return %r5 : i32226}227 228// -----229 230// CHECK-LABEL: func.func @trip_count_index_unsigned_crossing_zero(231func.func @trip_count_index_unsigned_crossing_zero(%a : i32, %b : i32) -> i32 {232 %c-1 = arith.constant -1 : index233 %c1 = arith.constant 1 : index234 235 // Index type has a unknown bitwidth, we can't compute a loop tripcount236 // in theory because of overflow concerns.237 // CHECK: "test.trip-count" = 0238 %r6 = scf.for unsigned %i = %c-1 to %c1 step %c1 iter_args(%0 = %a) -> i32 {239 scf.yield %b : i32240 }241 return %r6 : i32242}243 244// -----245 246// CHECK-LABEL: func.func @trip_count_i32_unsigned_crossing_zero(247func.func @trip_count_i32_unsigned_crossing_zero(%a : i32, %b : i32) -> i32 {248 %c-1 = arith.constant -1 : i32249 %c1 = arith.constant 1 : i32250 251 // This loop execute with signed comparison, but not unsigned, because it is crossing 0.252 // CHECK: "test.trip-count" = 0253 %r6 = scf.for unsigned %i = %c-1 to %c1 step %c1 iter_args(%0 = %a) -> i32 : i32 {254 scf.yield %b : i32255 }256 return %r6 : i32257}258 259// -----260 261// CHECK-LABEL: func.func @trip_count_i32_unsigned_crossing_zero_dyn_step(262func.func @trip_count_i32_unsigned_crossing_zero_dyn_step(%a : i32, %b : i32, %step : i32) -> i32 {263 %c-1 = arith.constant -1 : i32264 %c1 = arith.constant 1 : i32265 266 // This loop execute with signed comparison, but not unsigned, because it is crossing 0.267 // CHECK: "test.trip-count" = 0268 %r6 = scf.for unsigned %i = %c-1 to %c1 step %step iter_args(%0 = %a) -> i32 : i32 {269 scf.yield %b : i32270 }271 return %r6 : i32272}273 274// -----275 276// CHECK-LABEL: func.func @trip_count_index_negative_bounds_signed(277func.func @trip_count_index_negative_bounds_signed(%a : i32, %b : i32) -> i32 {278 %c-10 = arith.constant -10 : index279 %c-1 = arith.constant -1 : index280 %c2 = arith.constant 2 : index281 282 // Index type has a unknown bitwidth, we can't compute a loop tripcount283 // in theory because of overflow concerns.284 // CHECK: "test.trip-count" = 5285 %r7 = scf.for %i = %c-10 to %c-1 step %c2 iter_args(%0 = %a) -> i32 {286 scf.yield %b : i32287 }288 return %r7 : i32289}290 291// -----292 293// CHECK-LABEL: func.func @trip_count_i32_negative_bounds_signed(294func.func @trip_count_i32_negative_bounds_signed(%a : i32, %b : i32) -> i32 {295 %c-10 = arith.constant -10 : i32296 %c-1 = arith.constant -1 : i32297 %c2 = arith.constant 2 : i32298 299 // This loop execute with signed comparison, because both bounds are300 // negative and there is no crossing of 0 here.301 // CHECK: "test.trip-count" = 5302 %r7 = scf.for %i = %c-10 to %c-1 step %c2 iter_args(%0 = %a) -> i32 : i32 {303 scf.yield %b : i32304 }305 return %r7 : i32306}307 308// -----309 310// CHECK-LABEL: func.func @trip_count_index_negative_bounds_unsigned(311func.func @trip_count_index_negative_bounds_unsigned(%a : i32, %b : i32) -> i32 {312 %c-10 = arith.constant -10 : index313 %c-1 = arith.constant -1 : index314 %c2 = arith.constant 2 : index315 316 // Index type has a unknown bitwidth, we can't compute a loop tripcount317 // in theory because of overflow concerns.318 // CHECK: "test.trip-count" = 5319 %r8 = scf.for %i = %c-10 to %c-1 step %c2 iter_args(%0 = %a) -> i32 {320 scf.yield %b : i32321 }322 return %r8 : i32323}324 325// -----326 327// CHECK-LABEL: func.func @trip_count_i32_negative_bounds_unsigned(328func.func @trip_count_i32_negative_bounds_unsigned(%a : i32, %b : i32) -> i32 {329 %c-10 = arith.constant -10 : i32330 %c-1 = arith.constant -1 : i32331 %c2 = arith.constant 2 : i32332 333 // CHECK: "test.trip-count" = 5334 %r8 = scf.for %i = %c-10 to %c-1 step %c2 iter_args(%0 = %a) -> i32 : i32 {335 scf.yield %b : i32336 }337 return %r8 : i32338}339 340// -----341 342// CHECK-LABEL: func.func @trip_count_index_overflow_signed(343func.func @trip_count_index_overflow_signed(%a : i32, %b : i32) -> i32 {344 %c1 = arith.constant 1 : index345 %c_max = arith.constant 2147483647 : index // 2^31 - 1346 %c_min = arith.constant 2147483648 : index // -2^31347 348 // Index type has a unknown bitwidth, we can't compute a loop tripcount349 // in theory because of overflow concerns.350 // CHECK: "test.trip-count" = 1351 %r9 = scf.for %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 {352 scf.yield %b : i32353 }354 return %r9 : i32355}356 357// -----358 359// CHECK-LABEL: func.func @trip_count_i32_overflow_signed(360func.func @trip_count_i32_overflow_signed(%a : i32, %b : i32) -> i32 {361 %c1 = arith.constant 1 : i32362 %c_max = arith.constant 2147483647 : i32 // 2^31 - 1363 %c_min = arith.constant 2147483648 : i32 // -2^31364 365 // This loop crosses the 2^31 threshold, which would overflow a signed 32-bit integer.366 // CHECK: "test.trip-count" = 0367 %r9 = scf.for %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 : i32 {368 scf.yield %b : i32369 }370 return %r9 : i32371}372 373// -----374 375// CHECK-LABEL: func.func @trip_count_i32_overflow_signed_dyn_step(376func.func @trip_count_i32_overflow_signed_dyn_step(%a : i32, %b : i32, %step : i32) -> i32 {377 %c_max = arith.constant 2147483647 : i32 // 2^31 - 1378 %c_min = arith.constant 2147483648 : i32 // -2^31379 380 // This loop crosses the 2^31 threshold, which would overflow a signed 32-bit integer.381 // CHECK: "test.trip-count" = 0382 %r9 = scf.for %i = %c_max to %c_min step %step iter_args(%0 = %a) -> i32 : i32 {383 scf.yield %b : i32384 }385 return %r9 : i32386}387 388// -----389 390// CHECK-LABEL: func.func @trip_count_index_overflow_unsigned(391func.func @trip_count_index_overflow_unsigned(%a : i32, %b : i32) -> i32 {392 %c1 = arith.constant 1 : index393 %c_max = arith.constant 2147483647 : index // 2^31 - 1394 %c_min = arith.constant 2147483648 : index // -2^31395 396 // Index type has a unknown bitwidth, we can't compute a loop tripcount397 // in theory because of overflow concerns.398 // CHECK: "test.trip-count" = 1399 %r10 = scf.for unsigned %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 {400 scf.yield %b : i32401 }402 return %r10 : i32403}404 405// -----406 407// CHECK-LABEL: func.func @trip_count_i32_overflow_unsigned(408func.func @trip_count_i32_overflow_unsigned(%a : i32, %b : i32) -> i32 {409 %c1 = arith.constant 1 : i32410 %c_max = arith.constant 2147483647 : i32 // 2^31 - 1411 %c_min = arith.constant 2147483648 : i32 // -2^31412 413 // The same loop with unsigned comparison executes normally414 // CHECK: "test.trip-count" = 1415 %r10 = scf.for unsigned %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 : i32 {416 scf.yield %b : i32417 }418 return %r10 : i32419}420 421// -----422 423// CHECK-LABEL: func.func @trip_count_index_overflow_64bit_signed(424func.func @trip_count_index_overflow_64bit_signed(%a : i32, %b : i32) -> i32 {425 %c1 = arith.constant 1 : index426 %c_max = arith.constant 9223372036854775807 : index // 2^63 - 1427 %c_min = arith.constant -9223372036854775808 : index // -2^63428 429 // This loop crosses the 2^63 threshold, which would overflow a signed 64-bit integer.430 // Index type has a unknown bitwidth, we can't compute a loop tripcount.431 // CHECK: "test.trip-count" = 0432 %r11 = scf.for %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 {433 scf.yield %b : i32434 }435 return %r11 : i32436}437 438// -----439 440// CHECK-LABEL: func.func @trip_count_i64_overflow_64bit_signed(441func.func @trip_count_i64_overflow_64bit_signed(%a : i32, %b : i32) -> i32 {442 %c1 = arith.constant 1 : i64443 %c_max = arith.constant 9223372036854775807 : i64 // 2^63 - 1444 %c_min = arith.constant -9223372036854775808 : i64 // -2^63445 446 // This loop crosses the 2^63 threshold, which would overflow a signed 64-bit integer.447 // CHECK: "test.trip-count" = 0448 %r11 = scf.for %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 : i64 {449 scf.yield %b : i32450 }451 return %r11 : i32452}453 454// -----455 456// CHECK-LABEL: func.func @trip_count_index_overflow_64bit_unsigned(457func.func @trip_count_index_overflow_64bit_unsigned(%a : i32, %b : i32) -> i32 {458 %c1 = arith.constant 1 : index459 %c_max = arith.constant 9223372036854775807 : index // 2^63 - 1460 %c_min = arith.constant -9223372036854775808 : index // -2^63461 462 // Index type has a unknown bitwidth, we can't compute a loop tripcount463 // in theory because of overflow concerns.464 // CHECK: "test.trip-count" = 1465 %r12 = scf.for unsigned %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 {466 scf.yield %b : i32467 }468 return %r12 : i32469}470 471// -----472 473// CHECK-LABEL: func.func @trip_count_i32_overflow_64bit_unsigned(474func.func @trip_count_i32_overflow_64bit_unsigned(%a : i32, %b : i32) -> i32 {475 %c1 = arith.constant 1 : i64476 %c_max = arith.constant 9223372036854775807 : i64 // 2^63 - 1477 %c_min = arith.constant -9223372036854775808 : i64 // -2^63478 479 // The same loop with unsigned comparison executes normally480 // CHECK: "test.trip-count" = 1481 %r12 = scf.for unsigned %i = %c_max to %c_min step %c1 iter_args(%0 = %a) -> i32 : i64 {482 scf.yield %b : i32483 }484 return %r12 : i32485}486 487// -----488 489// CHECK-LABEL:func.func @trip_count_step_greater_than_iteration(490func.func @trip_count_step_greater_than_iteration() -> i32 {491 %c0_i32 = arith.constant 0 : i32492 %c4_i32 = arith.constant 4 : i32493 %c17_i32 = arith.constant 17 : i32494 %c16_i32 = arith.constant 16 : i32495 // CHECK: "test.trip-count" = 1496 %1 = scf.for %arg0 = %c16_i32 to %c17_i32 step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {497 scf.yield %arg0 : i32498 }499 return %1 : i32500}501 502 503// -----504 505// CHECK-LABEL:func.func @trip_count_arith_add(506func.func @trip_count_arith_add(%lb : i32) -> i32 {507 %c0_i32 = arith.constant 0 : i32508 %c4_i32 = arith.constant 4 : i32509 %c17_i32 = arith.constant 17 : i32510 %c16_i32 = arith.constant 16 : i32511 // Can't compute a trip-count in the absence of overflow flag.512 // CHECK: "test.trip-count" = "none"513 %ub = arith.addi %lb, %c16_i32 : i32514 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {515 scf.yield %arg0 : i32516 }517 return %1 : i32518}519 520// -----521 522// CHECK-LABEL:func.func @trip_count_arith_add_negative(523func.func @trip_count_arith_add_negative(%lb : i32) -> i32 {524 %c0_i32 = arith.constant 0 : i32525 %c4_i32 = arith.constant 4 : i32526 %c-16_i32 = arith.constant -16 : i32527 // Can't compute a trip-count in the absence of overflow flag.528 // CHECK: "test.trip-count" = "none"529 %ub = arith.addi %lb, %c-16_i32 : i32530 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {531 scf.yield %arg0 : i32532 }533 return %1 : i32534}535 536// -----537 538// CHECK-LABEL:func.func @trip_count_arith_add_nsw_loop_signed(539func.func @trip_count_arith_add_nsw_loop_signed(%lb : i32) -> i32 {540 %c0_i32 = arith.constant 0 : i32541 %c4_i32 = arith.constant 4 : i32542 %c16_i32 = arith.constant 16 : i32543 %ub = arith.addi %lb, %c16_i32 overflow<nsw> : i32544 // CHECK: "test.trip-count" = 4545 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {546 scf.yield %arg0 : i32547 }548 return %1 : i32549}550 551// -----552 553// CHECK-LABEL:func.func @trip_count_arith_add_negative_nsw_loop_signed(554func.func @trip_count_arith_add_negative_nsw_loop_signed(%lb : i32) -> i32 {555 %c0_i32 = arith.constant 0 : i32556 %c4_i32 = arith.constant 4 : i32557 %c-16_i32 = arith.constant -16 : i32558 %ub = arith.addi %lb, %c-16_i32 overflow<nsw> : i32559 // CHECK: "test.trip-count" = 0560 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {561 scf.yield %arg0 : i32562 }563 return %1 : i32564}565 566// -----567 568// CHECK-LABEL:func.func @trip_count_arith_add_negative_nsw_loop_signed_step_dyn(569func.func @trip_count_arith_add_negative_nsw_loop_signed_step_dyn(%lb : i32, %step : i32) -> i32 {570 %c0_i32 = arith.constant 0 : i32571 %c-16_i32 = arith.constant -16 : i32572 %ub = arith.addi %lb, %c-16_i32 overflow<nsw> : i32573 // CHECK: "test.trip-count" = 0574 %1 = scf.for %arg0 = %lb to %ub step %step iter_args(%arg1 = %c0_i32) -> (i32) : i32 {575 scf.yield %arg0 : i32576 }577 return %1 : i32578}579 580// -----581 582// CHECK-LABEL:func.func @trip_count_arith_add_nsw_loop_unsigned(583func.func @trip_count_arith_add_nsw_loop_unsigned(%lb : i32) -> i32 {584 %c0_i32 = arith.constant 0 : i32585 %c4_i32 = arith.constant 4 : i32586 %c16_i32 = arith.constant 16 : i32587 // Can't compute a trip-count when the overflow flag mismatches the loop comparison signess588 // CHECK: "test.trip-count" = "none"589 %ub = arith.addi %lb, %c16_i32 overflow<nsw> : i32590 %1 = scf.for unsigned %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {591 scf.yield %arg0 : i32592 }593 return %1 : i32594}595 596// -----597 598// CHECK-LABEL:func.func @trip_count_arith_add_negative_nsw_loop_unsigned(599func.func @trip_count_arith_add_negative_nsw_loop_unsigned(%lb : i32) -> i32 {600 %c0_i32 = arith.constant 0 : i32601 %c4_i32 = arith.constant 4 : i32602 %c-16_i32 = arith.constant -16 : i32603 // Can't compute a trip-count when the overflow flag mismatches the loop comparison signess604 // CHECK: "test.trip-count" = "none"605 %ub = arith.addi %lb, %c-16_i32 overflow<nsw> : i32606 %1 = scf.for unsigned %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {607 scf.yield %arg0 : i32608 }609 return %1 : i32610}611 612// -----613 614// CHECK-LABEL:func.func @trip_count_arith_add_nuw_loop_signed(615func.func @trip_count_arith_add_nuw_loop_signed(%lb : i32) -> i32 {616 %c0_i32 = arith.constant 0 : i32617 %c4_i32 = arith.constant 4 : i32618 %c16_i32 = arith.constant 16 : i32619 // Can't compute a trip-count when the overflow flag mismatches the loop comparison signess620 // CHECK: "test.trip-count" = "none"621 %ub = arith.addi %lb, %c16_i32 overflow<nuw> : i32622 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {623 scf.yield %arg0 : i32624 }625 return %1 : i32626}627 628// -----629 630// CHECK-LABEL:func.func @trip_count_arith_add_negative_nuw_loop_signed(631func.func @trip_count_arith_add_negative_nuw_loop_signed(%lb : i32) -> i32 {632 %c0_i32 = arith.constant 0 : i32633 %c4_i32 = arith.constant 4 : i32634 %c-16_i32 = arith.constant -16 : i32635 // Can't compute a trip-count when the overflow flag mismatches the loop comparison signess636 // CHECK: "test.trip-count" = "none"637 %ub = arith.addi %lb, %c-16_i32 overflow<nuw> : i32638 %1 = scf.for %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {639 scf.yield %arg0 : i32640 }641 return %1 : i32642}643 644// -----645 646// CHECK-LABEL:func.func @trip_count_arith_add_nuw_loop_unsigned(647func.func @trip_count_arith_add_nuw_loop_unsigned(%lb : i32) -> i32 {648 %c0_i32 = arith.constant 0 : i32649 %c4_i32 = arith.constant 4 : i32650 %c16_i32 = arith.constant 16 : i32651 // CHECK: "test.trip-count" = 4652 %ub = arith.addi %lb, %c16_i32 overflow<nuw> : i32653 %1 = scf.for unsigned %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {654 scf.yield %arg0 : i32655 }656 return %1 : i32657}658 659// -----660 661// CHECK-LABEL:func.func @trip_count_arith_add_negative_nuw_loop_unsigned(662func.func @trip_count_arith_add_negative_nuw_loop_unsigned(%lb : i32) -> i32 {663 %c0_i32 = arith.constant 0 : i32664 %c4_i32 = arith.constant 4 : i32665 %c-16_i32 = arith.constant -16 : i32666 // CHECK: "test.trip-count" = 0667 %ub = arith.addi %lb, %c-16_i32 overflow<nuw> : i32668 %1 = scf.for unsigned %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {669 scf.yield %arg0 : i32670 }671 return %1 : i32672}673 674// -----675 676// CHECK-LABEL:func.func @trip_count_arith_add_negative_nuw_loop_unsigned_step_dyn(677func.func @trip_count_arith_add_negative_nuw_loop_unsigned_step_dyn(%lb : i32, %step : i32) -> i32 {678 %c0_i32 = arith.constant 0 : i32679 %c-16_i32 = arith.constant -16 : i32680 // CHECK: "test.trip-count" = 0681 %ub = arith.addi %lb, %c-16_i32 overflow<nuw> : i32682 %1 = scf.for unsigned %arg0 = %lb to %ub step %step iter_args(%arg1 = %c0_i32) -> (i32) : i32 {683 scf.yield %arg0 : i32684 }685 return %1 : i32686}687 688// -----689 690// CHECK-LABEL:func.func @trip_count_arith_add_nuw_loop_unsigned_invalid(691func.func @trip_count_arith_add_nuw_loop_unsigned_invalid(%lb : i32, %other : i32) -> i32 {692 %c0_i32 = arith.constant 0 : i32693 %c4_i32 = arith.constant 4 : i32694 %c16_i32 = arith.constant 16 : i32695 // The addition here is not adding from %lb696 // CHECK: "test.trip-count" = "none"697 %ub = arith.addi %other, %c16_i32 overflow<nuw> : i32698 %1 = scf.for unsigned %arg0 = %lb to %ub step %c4_i32 iter_args(%arg1 = %c0_i32) -> (i32) : i32 {699 scf.yield %arg0 : i32700 }701 return %1 : i32702}