[flang-commits] [clang] [flang] Revert "[flang] Integrate the option -flang-experimental-integer-overflow into -fno-wrapv" (PR #113901)
Yusuke MINATO via flang-commits
flang-commits at lists.llvm.org
Mon Oct 28 05:48:35 PDT 2024
https://github.com/yus3710-fj created https://github.com/llvm/llvm-project/pull/113901
Reverts llvm/llvm-project#110063 due to the performance regression on 503.bwaves_r in SPEC2017.
>From 2f315eb090ec08e193acbc59127b958efa93fd35 Mon Sep 17 00:00:00 2001
From: Yusuke MINATO <minato.yusuke at fujitsu.com>
Date: Mon, 28 Oct 2024 21:45:39 +0900
Subject: [PATCH] =?UTF-8?q?Revert=20"[flang]=20Integrate=20the=20option=20?=
=?UTF-8?q?-flang-experimental-integer-overflow=20int=E2=80=A6"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This reverts commit 96bb375f5cedcfcc5dcd96296ba54ff933b39d4d.
---
clang/include/clang/Driver/Options.td | 4 +
clang/lib/Driver/ToolChains/Flang.cpp | 1 +
flang/include/flang/Lower/LoweringOptions.def | 5 +
.../flang/Optimizer/Transforms/Passes.h | 4 +-
.../flang/Optimizer/Transforms/Passes.td | 2 +-
flang/include/flang/Tools/CrossToolHelpers.h | 2 +-
flang/lib/Frontend/CompilerInvocation.cpp | 6 +
flang/lib/Frontend/FrontendActions.cpp | 4 +-
flang/lib/Lower/Bridge.cpp | 2 +-
flang/lib/Lower/IO.cpp | 2 +-
flang/lib/Optimizer/Passes/Pipelines.cpp | 6 +-
.../Transforms/ControlFlowConverter.cpp | 8 +-
flang/test/Driver/frontend-forwarding.f90 | 2 +
.../Fir/convert-to-llvm-openmp-and-fir.fir | 10 +-
flang/test/Fir/loop01.fir | 230 +++++++++++++++++-
flang/test/Fir/loop02.fir | 4 +-
flang/test/Lower/HLFIR/goto-do-body.f90 | 4 +-
.../OpenMP/parallel-private-clause-fixes.f90 | 4 +-
...oop-reduction-allocatable-array-minmax.f90 | 4 +-
flang/test/Lower/OpenMP/wsloop-variable.f90 | 4 +-
flang/test/Lower/array-character.f90 | 2 +-
.../test/Lower/array-derived-assignments.f90 | 2 +-
flang/test/Lower/array-derived.f90 | 2 +-
.../array-elemental-calls-char-byval.f90 | 2 +-
.../test/Lower/array-elemental-calls-char.f90 | 2 +-
.../Lower/array-expression-assumed-size.f90 | 2 +-
flang/test/Lower/array-expression-slice-1.f90 | 2 +-
flang/test/Lower/array-substring.f90 | 46 +++-
flang/test/Lower/array-temp.f90 | 2 +-
flang/test/Lower/components.f90 | 2 +-
flang/test/Lower/do_loop.f90 | 73 ++++--
flang/test/Lower/do_loop_unstructured.f90 | 203 +++++++++++++++-
flang/test/Lower/goto-do-body.f90 | 4 +-
flang/test/Lower/host-associated.f90 | 2 +-
flang/test/Lower/infinite_loop.f90 | 41 +++-
flang/test/Lower/io-implied-do-fixes.f90 | 49 +++-
flang/test/Lower/loops2.f90 | 2 +-
flang/test/Lower/mixed_loops.f90 | 6 +-
flang/test/Lower/vector-subscript-io.f90 | 34 +--
flang/tools/bbc/bbc.cpp | 9 +-
40 files changed, 676 insertions(+), 119 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5df6ddd5e6a0c5..75b4607b1b2391 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6787,6 +6787,10 @@ def flang_deprecated_no_hlfir : Flag<["-"], "flang-deprecated-no-hlfir">,
Flags<[HelpHidden]>, Visibility<[FlangOption, FC1Option]>,
HelpText<"Do not use HLFIR lowering (deprecated)">;
+def flang_experimental_integer_overflow : Flag<["-"], "flang-experimental-integer-overflow">,
+ Flags<[HelpHidden]>, Visibility<[FlangOption, FC1Option]>,
+ HelpText<"Add nsw flag to internal operations such as do-variable increment (experimental)">;
+
//===----------------------------------------------------------------------===//
// FLangOption + CoreOption + NoXarchOption
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 366cadc2e54775..a9d2b7a4dc48f9 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -148,6 +148,7 @@ void Flang::addCodegenOptions(const ArgList &Args,
Args.addAllArgs(CmdArgs, {options::OPT_flang_experimental_hlfir,
options::OPT_flang_deprecated_no_hlfir,
+ options::OPT_flang_experimental_integer_overflow,
options::OPT_fno_ppc_native_vec_elem_order,
options::OPT_fppc_native_vec_elem_order});
}
diff --git a/flang/include/flang/Lower/LoweringOptions.def b/flang/include/flang/Lower/LoweringOptions.def
index 0b22e54b648e94..231de533fbd30a 100644
--- a/flang/include/flang/Lower/LoweringOptions.def
+++ b/flang/include/flang/Lower/LoweringOptions.def
@@ -38,5 +38,10 @@ ENUM_LOWERINGOPT(Underscoring, unsigned, 1, 1)
/// (i.e. wraps around as two's complement). Off by default.
ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
+/// If true, add nsw flags to loop variable increments.
+/// Off by default.
+/// TODO: integrate this option with the above
+ENUM_LOWERINGOPT(NSWOnLoopVarInc, unsigned, 1, 0)
+
#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.h b/flang/include/flang/Optimizer/Transforms/Passes.h
index e1453cfa374bfc..e8f0a8444a31a1 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.h
+++ b/flang/include/flang/Optimizer/Transforms/Passes.h
@@ -66,7 +66,7 @@ namespace fir {
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
std::unique_ptr<mlir::Pass>
createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
-std::unique_ptr<mlir::Pass> createCFGConversionPassWithoutNSW();
+std::unique_ptr<mlir::Pass> createCFGConversionPassWithNSW();
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
std::unique_ptr<mlir::Pass>
@@ -83,7 +83,7 @@ createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr);
void populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
bool forceLoopToExecuteOnce = false,
- bool setNSW = true);
+ bool setNSW = false);
// declarative passes
#define GEN_PASS_REGISTRATION
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index 54b43adbfc6bbf..a41f0f348f27a6 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -153,7 +153,7 @@ def CFGConversion : Pass<"cfg-conversion"> {
/*default=*/"false",
"force the body of a loop to execute at least once">,
Option<"setNSW", "set-nsw", "bool",
- /*default=*/"true",
+ /*default=*/"false",
"set nsw on loop variable increment">
];
}
diff --git a/flang/include/flang/Tools/CrossToolHelpers.h b/flang/include/flang/Tools/CrossToolHelpers.h
index 1626970600eff2..df4b21ada058fe 100644
--- a/flang/include/flang/Tools/CrossToolHelpers.h
+++ b/flang/include/flang/Tools/CrossToolHelpers.h
@@ -122,7 +122,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
bool NoSignedZerosFPMath =
false; ///< Set no-signed-zeros-fp-math attribute for functions.
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
- bool NSWOnLoopVarInc = true; ///< Add nsw flag to loop variable increments.
+ bool NSWOnLoopVarInc = false; ///< Add nsw flag to loop variable increments.
};
struct OffloadModuleOpts {
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 5da5236af2b0e1..94d3d115417877 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1350,6 +1350,12 @@ bool CompilerInvocation::createFromArgs(
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
}
+ // -flang-experimental-integer-overflow
+ if (args.hasArg(
+ clang::driver::options::OPT_flang_experimental_integer_overflow)) {
+ invoc.loweringOpts.setNSWOnLoopVarInc(true);
+ }
+
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
// -Rpass-analysis. This will be used later when processing and outputting the
// remarks generated by LLVM in ExecuteCompilerInvocation.cpp.
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 3ea242315484fd..f2e460fc53a67f 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -828,8 +828,8 @@ void CodeGenAction::generateLLVMIR() {
config.VScaleMax = vsr->second;
}
- if (ci.getInvocation().getLoweringOpts().getIntegerWrapAround())
- config.NSWOnLoopVarInc = false;
+ if (ci.getInvocation().getLoweringOpts().getNSWOnLoopVarInc())
+ config.NSWOnLoopVarInc = true;
// Create the pass pipeline
fir::createMLIRToLLVMPassPipeline(pm, config, getCurrentFile());
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index a3bd1ace11da21..877fe122265dd0 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2271,7 +2271,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
assert(!incrementLoopNestInfo.empty() && "empty loop nest");
mlir::Location loc = toLocation();
mlir::arith::IntegerOverflowFlags flags{};
- if (!getLoweringOptions().getIntegerWrapAround())
+ if (getLoweringOptions().getNSWOnLoopVarInc())
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get(
builder->getContext(), flags);
diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp
index b534c81a605a90..1894b0cfd1bec2 100644
--- a/flang/lib/Lower/IO.cpp
+++ b/flang/lib/Lower/IO.cpp
@@ -929,7 +929,7 @@ static void genIoLoop(Fortran::lower::AbstractConverter &converter,
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::Location loc = converter.getCurrentLocation();
mlir::arith::IntegerOverflowFlags flags{};
- if (!converter.getLoweringOptions().getIntegerWrapAround())
+ if (converter.getLoweringOptions().getNSWOnLoopVarInc())
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
auto iofAttr =
mlir::arith::IntegerOverflowFlagsAttr::get(builder.getContext(), flags);
diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index 091b7c4c164e40..3c139f7e93405c 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -35,11 +35,11 @@ void addCanonicalizerPassWithoutRegionSimplification(mlir::OpPassManager &pm) {
void addCfgConversionPass(mlir::PassManager &pm,
const MLIRToLLVMPassPipelineConfig &config) {
if (config.NSWOnLoopVarInc)
+ addNestedPassToAllTopLevelOperationsConditionally(
+ pm, disableCfgConversion, fir::createCFGConversionPassWithNSW);
+ else
addNestedPassToAllTopLevelOperationsConditionally(pm, disableCfgConversion,
fir::createCFGConversion);
- else
- addNestedPassToAllTopLevelOperationsConditionally(
- pm, disableCfgConversion, fir::createCFGConversionPassWithoutNSW);
}
void addAVC(mlir::PassManager &pm, const llvm::OptimizationLevel &optLevel) {
diff --git a/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp b/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
index 411bf7f364a602..3b79d6d311b71c 100644
--- a/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
+++ b/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
@@ -332,6 +332,8 @@ class CfgConversion : public fir::impl::CFGConversionBase<CfgConversion> {
public:
using CFGConversionBase<CfgConversion>::CFGConversionBase;
+ CfgConversion(bool setNSW) { this->setNSW = setNSW; }
+
void runOnOperation() override {
auto *context = &this->getContext();
mlir::RewritePatternSet patterns(context);
@@ -364,8 +366,6 @@ void fir::populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
patterns.getContext(), forceLoopToExecuteOnce, setNSW);
}
-std::unique_ptr<mlir::Pass> fir::createCFGConversionPassWithoutNSW() {
- fir::CFGConversionOptions options;
- options.setNSW = false;
- return fir::createCFGConversion(options);
+std::unique_ptr<mlir::Pass> fir::createCFGConversionPassWithNSW() {
+ return std::make_unique<CfgConversion>(true);
}
diff --git a/flang/test/Driver/frontend-forwarding.f90 b/flang/test/Driver/frontend-forwarding.f90
index 55a74ccf40467b..ff2d6609521464 100644
--- a/flang/test/Driver/frontend-forwarding.f90
+++ b/flang/test/Driver/frontend-forwarding.f90
@@ -20,6 +20,7 @@
! RUN: -fversion-loops-for-stride \
! RUN: -flang-experimental-hlfir \
! RUN: -flang-deprecated-no-hlfir \
+! RUN: -flang-experimental-integer-overflow \
! RUN: -fno-ppc-native-vector-element-order \
! RUN: -fppc-native-vector-element-order \
! RUN: -mllvm -print-before-all \
@@ -51,6 +52,7 @@
! CHECK: "-fversion-loops-for-stride"
! CHECK: "-flang-experimental-hlfir"
! CHECK: "-flang-deprecated-no-hlfir"
+! CHECK: "-flang-experimental-integer-overflow"
! CHECK: "-fno-ppc-native-vector-element-order"
! CHECK: "-fppc-native-vector-element-order"
! CHECK: "-Rpass"
diff --git a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
index 00f8e6e6cc9a6b..335877e7c9a872 100644
--- a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
+++ b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
@@ -358,10 +358,10 @@ func.func @_QPopenmp_target_data_region() {
%9 = arith.subi %8, %c1_i64 : i64
%10 = fir.coordinate_of %0, %9 : (!fir.ref<!fir.array<1024xi32>>, i64) -> !fir.ref<i32>
fir.store %6 to %10 : !fir.ref<i32>
- %11 = arith.addi %arg0, %c1 overflow<nsw> : index
+ %11 = arith.addi %arg0, %c1 : index
%12 = fir.convert %c1 : (index) -> i32
%13 = fir.load %1 : !fir.ref<i32>
- %14 = arith.addi %13, %12 overflow<nsw> : i32
+ %14 = arith.addi %13, %12 : i32
fir.result %11, %14 : index, i32
}
fir.store %5#1 to %1 : !fir.ref<i32>
@@ -404,11 +404,11 @@ func.func @_QPopenmp_target_data_region() {
// CHECK: %[[VAL_21:.*]] = llvm.sub %[[VAL_19]], %[[VAL_20]] : i64
// CHECK: %[[VAL_22:.*]] = llvm.getelementptr %[[VAL_1]][0, %[[VAL_21]]] : (!llvm.ptr, i64) -> !llvm.ptr
// CHECK: llvm.store %[[VAL_17]], %[[VAL_22]] : i32, !llvm.ptr
-// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
+// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
// CHECK: %[[VAL_24:.*]] = llvm.trunc %[[VAL_8]] : i64 to i32
// CHECK: %[[VAL_25:.*]] = llvm.load %[[VAL_3]] : !llvm.ptr -> i32
-// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] overflow<nsw> : i32
-// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
+// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] : i32
+// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
// CHECK: %[[VAL_28:.*]] = llvm.mlir.constant(1 : index) : i64
// CHECK: %[[VAL_29:.*]] = llvm.sub %[[VAL_14]], %[[VAL_28]] : i64
// CHECK: llvm.br ^bb1(%[[VAL_27]], %[[VAL_26]], %[[VAL_29]] : i64, i32, i64)
diff --git a/flang/test/Fir/loop01.fir b/flang/test/Fir/loop01.fir
index 30d10b9bbdb979..c1cbb522c378c0 100644
--- a/flang/test/Fir/loop01.fir
+++ b/flang/test/Fir/loop01.fir
@@ -1,7 +1,5 @@
// RUN: fir-opt --split-input-file --cfg-conversion %s | FileCheck %s
-// RUN: fir-opt --split-input-file --cfg-conversion="set-nsw=false" %s | FileCheck %s --check-prefix=NO-NSW
-
-// NO-NSW-NOT: overflow<nsw>
+// RUN: fir-opt --split-input-file --cfg-conversion="set-nsw=true" %s | FileCheck %s --check-prefix=NSW
func.func @x(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
fir.do_loop %iv = %lb to %ub step %step unordered {
@@ -37,7 +35,7 @@ func.func private @f2() -> i1
// CHECK: fir.store %[[VAL_12]] to %[[VAL_4]] : !fir.ref<index>
// CHECK: br ^bb5
// CHECK: ^bb5:
-// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_8]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_8]], %[[VAL_2]] : index
// CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
// CHECK: br ^bb1(%[[VAL_13]], %[[VAL_15]] : index, index)
@@ -46,6 +44,34 @@ func.func private @f2() -> i1
// CHECK: }
// CHECK: func private @f2() -> i1
+// NSW: func @x(%[[VAL_0:.*]]: index, %[[VAL_1:.*]]: index, %[[VAL_2:.*]]: index, %[[VAL_3:.*]]: i1, %[[VAL_4:.*]]: !fir.ref<index>) {
+// NSW: %[[VAL_5:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
+// NSW: %[[VAL_6:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] : index
+// NSW: %[[VAL_7:.*]] = arith.divsi %[[VAL_6]], %[[VAL_2]] : index
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_7]] : index, index)
+// NSW: ^bb1(%[[VAL_8:.*]]: index, %[[VAL_9:.*]]: index):
+// NSW: %[[VAL_10:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
+// NSW: cond_br %[[VAL_11]], ^bb2, ^bb6
+// NSW: ^bb2:
+// NSW: cond_br %[[VAL_3]], ^bb3, ^bb4
+// NSW: ^bb3:
+// NSW: fir.store %[[VAL_8]] to %[[VAL_4]] : !fir.ref<index>
+// NSW: br ^bb5
+// NSW: ^bb4:
+// NSW: %[[VAL_12:.*]] = arith.constant 0 : index
+// NSW: fir.store %[[VAL_12]] to %[[VAL_4]] : !fir.ref<index>
+// NSW: br ^bb5
+// NSW: ^bb5:
+// NSW: %[[VAL_13:.*]] = arith.addi %[[VAL_8]], %[[VAL_2]] overflow<nsw> : index
+// NSW: %[[VAL_14:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
+// NSW: br ^bb1(%[[VAL_13]], %[[VAL_15]] : index, index)
+// NSW: ^bb6:
+// NSW: return
+// NSW: }
+// NSW: func private @f2() -> i1
+
// -----
func.func @x2(%lo : index, %up : index, %ok : i1) {
@@ -75,13 +101,36 @@ func.func private @f3(i16)
// CHECK: cond_br %[[VAL_14]], ^bb2, ^bb3
// CHECK: ^bb2:
// CHECK: %[[VAL_15:.*]] = fir.call @f2() : () -> i1
-// CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_4]], %[[VAL_3]] overflow<nsw> : index
+// CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_4]], %[[VAL_3]] : index
// CHECK: br ^bb1(%[[VAL_16]], %[[VAL_15]] : index, i1)
// CHECK: ^bb3:
// CHECK: return
// CHECK: }
// CHECK: func private @f3(i16)
+// NSW: func @x2(%[[VAL_0:.*]]: index, %[[VAL_1:.*]]: index, %[[VAL_2:.*]]: i1) {
+// NSW: %[[VAL_3:.*]] = arith.constant 1 : index
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_2]] : index, i1)
+// NSW: ^bb1(%[[VAL_4:.*]]: index, %[[VAL_5:.*]]: i1):
+// NSW: %[[VAL_6:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_7:.*]] = arith.cmpi slt, %[[VAL_6]], %[[VAL_3]] : index
+// NSW: %[[VAL_8:.*]] = arith.cmpi sle, %[[VAL_4]], %[[VAL_1]] : index
+// NSW: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_3]], %[[VAL_6]] : index
+// NSW: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_4]] : index
+// NSW: %[[VAL_11:.*]] = arith.andi %[[VAL_7]], %[[VAL_8]] : i1
+// NSW: %[[VAL_12:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
+// NSW: %[[VAL_13:.*]] = arith.ori %[[VAL_11]], %[[VAL_12]] : i1
+// NSW: %[[VAL_14:.*]] = arith.andi %[[VAL_5]], %[[VAL_13]] : i1
+// NSW: cond_br %[[VAL_14]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_15:.*]] = fir.call @f2() : () -> i1
+// NSW: %[[VAL_16:.*]] = arith.addi %[[VAL_4]], %[[VAL_3]] overflow<nsw> : index
+// NSW: br ^bb1(%[[VAL_16]], %[[VAL_15]] : index, i1)
+// NSW: ^bb3:
+// NSW: return
+// NSW: }
+// NSW: func private @f3(i16)
+
// -----
// do_loop with an extra loop-carried value
@@ -110,7 +159,7 @@ func.func @x3(%lo : index, %up : index) -> i1 {
// CHECK: cond_br %[[VAL_11]], ^bb2, ^bb3
// CHECK: ^bb2:
// CHECK: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
-// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] : index
// CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
// CHECK: br ^bb1(%[[VAL_13]], %[[VAL_12]], %[[VAL_15]] : index, i1, index)
@@ -118,6 +167,29 @@ func.func @x3(%lo : index, %up : index) -> i1 {
// CHECK: return %[[VAL_8]] : i1
// CHECK: }
+// NSW-LABEL: func @x3(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> i1 {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.constant true
+// NSW: %[[VAL_4:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
+// NSW: %[[VAL_5:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] : index
+// NSW: %[[VAL_6:.*]] = arith.divsi %[[VAL_5]], %[[VAL_2]] : index
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_6]] : index, i1, index)
+// NSW: ^bb1(%[[VAL_7:.*]]: index, %[[VAL_8:.*]]: i1, %[[VAL_9:.*]]: index):
+// NSW: %[[VAL_10:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
+// NSW: cond_br %[[VAL_11]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
+// NSW: %[[VAL_13:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
+// NSW: %[[VAL_14:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
+// NSW: br ^bb1(%[[VAL_13]], %[[VAL_12]], %[[VAL_15]] : index, i1, index)
+// NSW: ^bb3:
+// NSW: return %[[VAL_8]] : i1
+// NSW: }
+
// -----
// iterate_while with an extra loop-carried value
@@ -155,7 +227,7 @@ func.func private @f4(i32) -> i1
// CHECK: cond_br %[[VAL_16]], ^bb2, ^bb3
// CHECK: ^bb2:
// CHECK: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
-// CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] : index
// CHECK: br ^bb1(%[[VAL_18]], %[[VAL_6]], %[[VAL_17]] : index, i1, i1)
// CHECK: ^bb3:
// CHECK: %[[VAL_19:.*]] = arith.andi %[[VAL_6]], %[[VAL_7]] : i1
@@ -163,6 +235,34 @@ func.func private @f4(i32) -> i1
// CHECK: }
// CHECK: func private @f4(i32) -> i1
+// NSW-LABEL: func @y3(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> i1 {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.constant true
+// NSW: %[[VAL_4:.*]] = fir.call @f2() : () -> i1
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_4]] : index, i1, i1)
+// NSW: ^bb1(%[[VAL_5:.*]]: index, %[[VAL_6:.*]]: i1, %[[VAL_7:.*]]: i1):
+// NSW: %[[VAL_8:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_8]], %[[VAL_2]] : index
+// NSW: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_5]], %[[VAL_1]] : index
+// NSW: %[[VAL_11:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_8]] : index
+// NSW: %[[VAL_12:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_5]] : index
+// NSW: %[[VAL_13:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
+// NSW: %[[VAL_14:.*]] = arith.andi %[[VAL_11]], %[[VAL_12]] : i1
+// NSW: %[[VAL_15:.*]] = arith.ori %[[VAL_13]], %[[VAL_14]] : i1
+// NSW: %[[VAL_16:.*]] = arith.andi %[[VAL_6]], %[[VAL_15]] : i1
+// NSW: cond_br %[[VAL_16]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
+// NSW: %[[VAL_18:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
+// NSW: br ^bb1(%[[VAL_18]], %[[VAL_6]], %[[VAL_17]] : index, i1, i1)
+// NSW: ^bb3:
+// NSW: %[[VAL_19:.*]] = arith.andi %[[VAL_6]], %[[VAL_7]] : i1
+// NSW: return %[[VAL_19]] : i1
+// NSW: }
+// NSW: func private @f4(i32) -> i1
+
// -----
// do_loop that returns the final value of the induction
@@ -191,7 +291,7 @@ func.func @x4(%lo : index, %up : index) -> index {
// CHECK: ^bb2:
// CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_6]] : (index) -> i32
// CHECK: %[[VAL_11:.*]] = fir.call @f4(%[[VAL_10]]) : (i32) -> i1
-// CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] : index
// CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_14:.*]] = arith.subi %[[VAL_7]], %[[VAL_13]] : index
// CHECK: br ^bb1(%[[VAL_12]], %[[VAL_14]] : index, index)
@@ -199,6 +299,29 @@ func.func @x4(%lo : index, %up : index) -> index {
// CHECK: return %[[VAL_6]] : index
// CHECK: }
+// NSW-LABEL: func @x4(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> index {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
+// NSW: %[[VAL_4:.*]] = arith.addi %[[VAL_3]], %[[VAL_2]] : index
+// NSW: %[[VAL_5:.*]] = arith.divsi %[[VAL_4]], %[[VAL_2]] : index
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_5]] : index, index)
+// NSW: ^bb1(%[[VAL_6:.*]]: index, %[[VAL_7:.*]]: index):
+// NSW: %[[VAL_8:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_9:.*]] = arith.cmpi sgt, %[[VAL_7]], %[[VAL_8]] : index
+// NSW: cond_br %[[VAL_9]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_10:.*]] = fir.convert %[[VAL_6]] : (index) -> i32
+// NSW: %[[VAL_11:.*]] = fir.call @f4(%[[VAL_10]]) : (i32) -> i1
+// NSW: %[[VAL_12:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] overflow<nsw> : index
+// NSW: %[[VAL_13:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_14:.*]] = arith.subi %[[VAL_7]], %[[VAL_13]] : index
+// NSW: br ^bb1(%[[VAL_12]], %[[VAL_14]] : index, index)
+// NSW: ^bb3:
+// NSW: return %[[VAL_6]] : index
+// NSW: }
+
// -----
// iterate_while that returns the final value of both inductions
@@ -233,12 +356,38 @@ func.func @y4(%lo : index, %up : index) -> index {
// CHECK: ^bb2:
// CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_4]] : (index) -> i32
// CHECK: %[[VAL_16:.*]] = fir.call @f4(%[[VAL_15]]) : (i32) -> i1
-// CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] : index
// CHECK: br ^bb1(%[[VAL_17]], %[[VAL_16]] : index, i1)
// CHECK: ^bb3:
// CHECK: return %[[VAL_4]] : index
// CHECK: }
+// NSW-LABEL: func @y4(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> index {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.constant true
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_3]] : index, i1)
+// NSW: ^bb1(%[[VAL_4:.*]]: index, %[[VAL_5:.*]]: i1):
+// NSW: %[[VAL_6:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_7:.*]] = arith.cmpi slt, %[[VAL_6]], %[[VAL_2]] : index
+// NSW: %[[VAL_8:.*]] = arith.cmpi sle, %[[VAL_4]], %[[VAL_1]] : index
+// NSW: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_6]] : index
+// NSW: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_4]] : index
+// NSW: %[[VAL_11:.*]] = arith.andi %[[VAL_7]], %[[VAL_8]] : i1
+// NSW: %[[VAL_12:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
+// NSW: %[[VAL_13:.*]] = arith.ori %[[VAL_11]], %[[VAL_12]] : i1
+// NSW: %[[VAL_14:.*]] = arith.andi %[[VAL_5]], %[[VAL_13]] : i1
+// NSW: cond_br %[[VAL_14]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_15:.*]] = fir.convert %[[VAL_4]] : (index) -> i32
+// NSW: %[[VAL_16:.*]] = fir.call @f4(%[[VAL_15]]) : (i32) -> i1
+// NSW: %[[VAL_17:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] overflow<nsw> : index
+// NSW: br ^bb1(%[[VAL_17]], %[[VAL_16]] : index, i1)
+// NSW: ^bb3:
+// NSW: return %[[VAL_4]] : index
+// NSW: }
+
// -----
// do_loop that returns the final induction value
@@ -271,7 +420,7 @@ func.func @x5(%lo : index, %up : index) -> index {
// CHECK: ^bb2:
// CHECK: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
// CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i1) -> i16
-// CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] : index
// CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_16:.*]] = arith.subi %[[VAL_9]], %[[VAL_15]] : index
// CHECK: br ^bb1(%[[VAL_14]], %[[VAL_13]], %[[VAL_16]] : index, i16, index)
@@ -280,6 +429,31 @@ func.func @x5(%lo : index, %up : index) -> index {
// CHECK: return %[[VAL_7]] : index
// CHECK: }
+// NSW-LABEL: func @x5(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> index {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.constant 42 : i16
+// NSW: %[[VAL_4:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
+// NSW: %[[VAL_5:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] : index
+// NSW: %[[VAL_6:.*]] = arith.divsi %[[VAL_5]], %[[VAL_2]] : index
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_6]] : index, i16, index)
+// NSW: ^bb1(%[[VAL_7:.*]]: index, %[[VAL_8:.*]]: i16, %[[VAL_9:.*]]: index):
+// NSW: %[[VAL_10:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
+// NSW: cond_br %[[VAL_11]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
+// NSW: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i1) -> i16
+// NSW: %[[VAL_14:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
+// NSW: %[[VAL_15:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_16:.*]] = arith.subi %[[VAL_9]], %[[VAL_15]] : index
+// NSW: br ^bb1(%[[VAL_14]], %[[VAL_13]], %[[VAL_16]] : index, i16, index)
+// NSW: ^bb3:
+// NSW: fir.call @f3(%[[VAL_8]]) : (i16) -> ()
+// NSW: return %[[VAL_7]] : index
+// NSW: }
+
// -----
// iterate_while that returns the both induction values
@@ -322,7 +496,7 @@ func.func @y5(%lo : index, %up : index) -> index {
// CHECK: ^bb2:
// CHECK: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
// CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i1) -> i16
-// CHECK: %[[VAL_19:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_19:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] : index
// CHECK: br ^bb1(%[[VAL_19]], %[[VAL_17]], %[[VAL_18]] : index, i1, i16)
// CHECK: ^bb3:
// CHECK: cond_br %[[VAL_6]], ^bb4, ^bb5
@@ -334,3 +508,37 @@ func.func @y5(%lo : index, %up : index) -> index {
// CHECK: fir.call @f3(%[[VAL_7]]) : (i16) -> ()
// CHECK: return %[[VAL_5]] : index
// CHECK: }
+
+// NSW-LABEL: func @y5(
+// NSW-SAME: %[[VAL_0:.*]]: index,
+// NSW-SAME: %[[VAL_1:.*]]: index) -> index {
+// NSW: %[[VAL_2:.*]] = arith.constant 1 : index
+// NSW: %[[VAL_3:.*]] = arith.constant 42 : i16
+// NSW: %[[VAL_4:.*]] = arith.constant true
+// NSW: br ^bb1(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]] : index, i1, i16)
+// NSW: ^bb1(%[[VAL_5:.*]]: index, %[[VAL_6:.*]]: i1, %[[VAL_7:.*]]: i16):
+// NSW: %[[VAL_8:.*]] = arith.constant 0 : index
+// NSW: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_8]], %[[VAL_2]] : index
+// NSW: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_5]], %[[VAL_1]] : index
+// NSW: %[[VAL_11:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_8]] : index
+// NSW: %[[VAL_12:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_5]] : index
+// NSW: %[[VAL_13:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
+// NSW: %[[VAL_14:.*]] = arith.andi %[[VAL_11]], %[[VAL_12]] : i1
+// NSW: %[[VAL_15:.*]] = arith.ori %[[VAL_13]], %[[VAL_14]] : i1
+// NSW: %[[VAL_16:.*]] = arith.andi %[[VAL_6]], %[[VAL_15]] : i1
+// NSW: cond_br %[[VAL_16]], ^bb2, ^bb3
+// NSW: ^bb2:
+// NSW: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
+// NSW: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i1) -> i16
+// NSW: %[[VAL_19:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
+// NSW: br ^bb1(%[[VAL_19]], %[[VAL_17]], %[[VAL_18]] : index, i1, i16)
+// NSW: ^bb3:
+// NSW: cond_br %[[VAL_6]], ^bb4, ^bb5
+// NSW: ^bb4:
+// NSW: %[[VAL_20:.*]] = arith.constant 0 : i32
+// NSW: %[[VAL_21:.*]] = fir.call @f4(%[[VAL_20]]) : (i32) -> i1
+// NSW: br ^bb5
+// NSW: ^bb5:
+// NSW: fir.call @f3(%[[VAL_7]]) : (i16) -> ()
+// NSW: return %[[VAL_5]] : index
+// NSW: }
diff --git a/flang/test/Fir/loop02.fir b/flang/test/Fir/loop02.fir
index fb209a9dfeb426..50948e0e7aa6b5 100644
--- a/flang/test/Fir/loop02.fir
+++ b/flang/test/Fir/loop02.fir
@@ -31,7 +31,7 @@ func.func private @y(%addr : !fir.ref<index>)
// CHECK: cond_br %[[VAL_13]], ^bb2, ^bb3
// CHECK: ^bb2:
// CHECK: fir.call @y(%[[VAL_0]]) : (!fir.ref<index>) -> ()
-// CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_10]], %[[VAL_2]] overflow<nsw> : index
+// CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_10]], %[[VAL_2]] : index
// CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_16:.*]] = arith.subi %[[VAL_11]], %[[VAL_15]] : index
// CHECK: br ^bb1(%[[VAL_14]], %[[VAL_16]] : index, index)
@@ -54,7 +54,7 @@ func.func private @y(%addr : !fir.ref<index>)
// NOOPT: cond_br %[[VAL_9]], ^bb2, ^bb3
// NOOPT: ^bb2:
// NOOPT: fir.call @y(%[[VAL_0]]) : (!fir.ref<index>) -> ()
-// NOOPT: %[[VAL_10:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] overflow<nsw> : index
+// NOOPT: %[[VAL_10:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] : index
// NOOPT: %[[VAL_11:.*]] = arith.constant 1 : index
// NOOPT: %[[VAL_12:.*]] = arith.subi %[[VAL_7]], %[[VAL_11]] : index
// NOOPT: br ^bb1(%[[VAL_10]], %[[VAL_12]] : index, index)
diff --git a/flang/test/Lower/HLFIR/goto-do-body.f90 b/flang/test/Lower/HLFIR/goto-do-body.f90
index 5f5b09ccb8f7dc..383b839e591e33 100644
--- a/flang/test/Lower/HLFIR/goto-do-body.f90
+++ b/flang/test/Lower/HLFIR/goto-do-body.f90
@@ -40,7 +40,7 @@ subroutine sub1()
! CHECK: %[[TMP5:.*]] = arith.subi %[[TMP4]], %[[C1]] : i32
! CHECK: fir.store %[[TMP5]] to %[[TRIP]] : !fir.ref<i32>
! CHECK: %[[TMP6:.*]] = fir.load %[[I]]#1 : !fir.ref<i32>
-! CHECK: %[[TMP7:.*]] = arith.addi %[[TMP6]], %[[C1]] overflow<nsw> : i32
+! CHECK: %[[TMP7:.*]] = arith.addi %[[TMP6]], %[[C1]] : i32
! CHECK: fir.store %[[TMP7]] to %[[I]]#1 : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
end do
@@ -104,7 +104,7 @@ subroutine sub2()
! CHECK: fir.store %[[TMP9]] to %[[TRIP]] : !fir.ref<i32>
! CHECK: %[[TMP10:.*]] = fir.load %[[I]]#1 : !fir.ref<i32>
! CHECK: %[[STEP_VAL:.*]] = fir.load %[[STEP_VAR]] : !fir.ref<i32>
-! CHECK: %[[TMP11:.*]] = arith.addi %[[TMP10]], %[[STEP_VAL]] overflow<nsw> : i32
+! CHECK: %[[TMP11:.*]] = arith.addi %[[TMP10]], %[[STEP_VAL]] : i32
! CHECK: fir.store %[[TMP11]] to %[[I]]#1 : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
end do
diff --git a/flang/test/Lower/OpenMP/parallel-private-clause-fixes.f90 b/flang/test/Lower/OpenMP/parallel-private-clause-fixes.f90
index 99323e69113bcc..5e76e8ff1663bf 100644
--- a/flang/test/Lower/OpenMP/parallel-private-clause-fixes.f90
+++ b/flang/test/Lower/OpenMP/parallel-private-clause-fixes.f90
@@ -82,10 +82,10 @@
! CHECK: %[[VAL_15:.*]] = fir.load %[[PRIV_J_DECL]]#0 : !fir.ref<i32>
! CHECK: %[[VAL_16:.*]] = arith.addi %[[LOAD]], %[[VAL_15]] : i32
! CHECK: hlfir.assign %[[VAL_16]] to %[[PRIV_X_DECL]]#0 : i32, !fir.ref<i32>
-! CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_13]], %[[VAL_11]] overflow<nsw> : index
+! CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_13]], %[[VAL_11]] : index
! CHECK: %[[STEPCAST:.*]] = fir.convert %[[VAL_11]] : (index) -> i32
! CHECK: %[[IVLOAD:.*]] = fir.load %[[PRIV_J_DECL]]#1 : !fir.ref<i32>
-! CHECK: %[[IVINC:.*]] = arith.addi %[[IVLOAD]], %[[STEPCAST]] overflow<nsw> :
+! CHECK: %[[IVINC:.*]] = arith.addi %[[IVLOAD]], %[[STEPCAST]]
! CHECK: fir.result %[[VAL_17]], %[[IVINC]] : index, i32
! CHECK: }
! CHECK: fir.store %[[VAL_12]]#1 to %[[PRIV_J_DECL]]#1 : !fir.ref<i32>
diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-allocatable-array-minmax.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-allocatable-array-minmax.f90
index ce45d09d77a22a..a49eba69ff38cc 100644
--- a/flang/test/Lower/OpenMP/wsloop-reduction-allocatable-array-minmax.f90
+++ b/flang/test/Lower/OpenMP/wsloop-reduction-allocatable-array-minmax.f90
@@ -206,10 +206,10 @@ program reduce15
! CHECK: %[[VAL_48:.*]] = fir.convert %[[VAL_47]] : (i32) -> i64
! CHECK: %[[VAL_49:.*]] = hlfir.designate %[[VAL_46]] (%[[VAL_48]]) : (!fir.box<!fir.heap<!fir.array<?xi32>>>, i64) -> !fir.ref<i32>
! CHECK: hlfir.assign %[[VAL_45]] to %[[VAL_49]] : i32, !fir.ref<i32>
-! CHECK: %[[VAL_50:.*]] = arith.addi %[[VAL_43]], %[[VAL_40]] overflow<nsw> : index
+! CHECK: %[[VAL_50:.*]] = arith.addi %[[VAL_43]], %[[VAL_40]] : index
! CHECK: %[[VAL_51:.*]] = fir.convert %[[VAL_40]] : (index) -> i32
! CHECK: %[[VAL_52:.*]] = fir.load %[[VAL_3]]#1 : !fir.ref<i32>
-! CHECK: %[[VAL_53:.*]] = arith.addi %[[VAL_52]], %[[VAL_51]] overflow<nsw> : i32
+! CHECK: %[[VAL_53:.*]] = arith.addi %[[VAL_52]], %[[VAL_51]] : i32
! CHECK: fir.result %[[VAL_50]], %[[VAL_53]] : index, i32
! CHECK: }
! CHECK: fir.store %[[VAL_54:.*]]#1 to %[[VAL_3]]#1 : !fir.ref<i32>
diff --git a/flang/test/Lower/OpenMP/wsloop-variable.f90 b/flang/test/Lower/OpenMP/wsloop-variable.f90
index cc77ce754d97e9..8d235c10fa1d60 100644
--- a/flang/test/Lower/OpenMP/wsloop-variable.f90
+++ b/flang/test/Lower/OpenMP/wsloop-variable.f90
@@ -150,10 +150,10 @@ subroutine wsloop_variable_sub
!CHECK: %[[VAL_42:.*]] = arith.addi %[[VAL_40]], %[[VAL_41]] : i64
!CHECK: %[[VAL_43:.*]] = fir.convert %[[VAL_42]] : (i64) -> f32
!CHECK: hlfir.assign %[[VAL_43]] to %[[VAL_21]]#0 : f32, !fir.ref<f32>
-!CHECK: %[[VAL_44:.*]] = arith.addi %[[VAL_37]], %[[VAL_34]] overflow<nsw> : index
+!CHECK: %[[VAL_44:.*]] = arith.addi %[[VAL_37]], %[[VAL_34]] : index
!CHECK: %[[VAL_45:.*]] = fir.convert %[[VAL_34]] : (index) -> i64
!CHECK: %[[VAL_46:.*]] = fir.load %[[VAL_17]]#1 : !fir.ref<i64>
-!CHECK: %[[VAL_47:.*]] = arith.addi %[[VAL_46]], %[[VAL_45]] overflow<nsw> : i64
+!CHECK: %[[VAL_47:.*]] = arith.addi %[[VAL_46]], %[[VAL_45]] : i64
!CHECK: fir.result %[[VAL_44]], %[[VAL_47]] : index, i64
!CHECK: }
!CHECK: fir.store %[[VAL_48:.*]]#1 to %[[VAL_17]]#1 : !fir.ref<i64>
diff --git a/flang/test/Lower/array-character.f90 b/flang/test/Lower/array-character.f90
index 53adc5c02958c3..c93ef4be30823c 100644
--- a/flang/test/Lower/array-character.f90
+++ b/flang/test/Lower/array-character.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -hlfir=false -fwrapv %s -o - | fir-opt --canonicalize --cse | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | fir-opt --canonicalize --cse | FileCheck %s
! CHECK-LABEL: func @_QPissue(
! CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>{{.*}}, %[[VAL_1:.*]]: !fir.boxchar<1>{{.*}}) {
diff --git a/flang/test/Lower/array-derived-assignments.f90 b/flang/test/Lower/array-derived-assignments.f90
index f4e51271d59368..71e61f651302a3 100644
--- a/flang/test/Lower/array-derived-assignments.f90
+++ b/flang/test/Lower/array-derived-assignments.f90
@@ -1,5 +1,5 @@
! Test derived type assignment lowering inside array expression
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck %s
module array_derived_assign
type simple_copy
diff --git a/flang/test/Lower/array-derived.f90 b/flang/test/Lower/array-derived.f90
index a0c55f5d88255f..b5eb7621c90f10 100644
--- a/flang/test/Lower/array-derived.f90
+++ b/flang/test/Lower/array-derived.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck %s
module cs
type r
diff --git a/flang/test/Lower/array-elemental-calls-char-byval.f90 b/flang/test/Lower/array-elemental-calls-char-byval.f90
index 682191fc789562..c321614e7fc5b5 100644
--- a/flang/test/Lower/array-elemental-calls-char-byval.f90
+++ b/flang/test/Lower/array-elemental-calls-char-byval.f90
@@ -1,6 +1,6 @@
! Test lowering of elemental calls with character argument
! with the VALUE attribute.
-! RUN: bbc -hlfir=false -fwrapv -o - %s | FileCheck %s
+! RUN: bbc -hlfir=false -o - %s | FileCheck %s
module char_elem_byval
diff --git a/flang/test/Lower/array-elemental-calls-char.f90 b/flang/test/Lower/array-elemental-calls-char.f90
index 00e2f8e8f9c127..603cc677805fc9 100644
--- a/flang/test/Lower/array-elemental-calls-char.f90
+++ b/flang/test/Lower/array-elemental-calls-char.f90
@@ -1,6 +1,6 @@
! Test lowering of elemental calls with character argument
! without the VALUE attribute.
-! RUN: bbc -hlfir=false -fwrapv -o - %s | FileCheck %s
+! RUN: bbc -hlfir=false -o - %s | FileCheck %s
module char_elem
diff --git a/flang/test/Lower/array-expression-assumed-size.f90 b/flang/test/Lower/array-expression-assumed-size.f90
index 2fbf315aff1148..ae35da951538b8 100644
--- a/flang/test/Lower/array-expression-assumed-size.f90
+++ b/flang/test/Lower/array-expression-assumed-size.f90
@@ -1,5 +1,5 @@
! RUN: bbc --emit-fir -hlfir=false %s -o - | FileCheck %s
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck --check-prefix=PostOpt %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck --check-prefix=PostOpt %s
subroutine assumed_size_test(a)
diff --git a/flang/test/Lower/array-expression-slice-1.f90 b/flang/test/Lower/array-expression-slice-1.f90
index b597814bc0d9f1..15245090243290 100644
--- a/flang/test/Lower/array-expression-slice-1.f90
+++ b/flang/test/Lower/array-expression-slice-1.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -hlfir=false -fwrapv -o - --outline-intrinsics %s | FileCheck %s
+! RUN: bbc -hlfir=false -o - --outline-intrinsics %s | FileCheck %s
! CHECK-LABEL: func @_QQmain() attributes {fir.bindc_name = "p"} {
! CHECK-DAG: %[[VAL_0:.*]] = arith.constant 10 : index
diff --git a/flang/test/Lower/array-substring.f90 b/flang/test/Lower/array-substring.f90
index 02101039120e9f..2e283997e3e003 100644
--- a/flang/test/Lower/array-substring.f90
+++ b/flang/test/Lower/array-substring.f90
@@ -1,7 +1,5 @@
! RUN: bbc -hlfir=false %s -o - | FileCheck %s
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s --check-prefix=NO-NSW
-
-! NO-NSW-NOT: overflow<nsw>
+! RUN: bbc -hlfir=false -integer-overflow %s -o - | FileCheck %s --check-prefix=NSW
! CHECK-LABEL: func @_QPtest(
! CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>{{.*}}) -> !fir.array<1x!fir.logical<4>> {
@@ -34,9 +32,8 @@
! CHECK: %[[VAL_26:.*]] = fir.convert %[[VAL_25]] : (i1) -> !fir.logical<4>
! CHECK: %[[VAL_27:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_9]]) %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.logical<4>>>, !fir.shape<1>, index) -> !fir.ref<!fir.logical<4>>
! CHECK: fir.store %[[VAL_26]] to %[[VAL_27]] : !fir.ref<!fir.logical<4>>
-! CHECK: %[[VAL_15_NSW:.*]] = arith.addi %[[VAL_12]], %[[VAL_1]] overflow<nsw> : index
! CHECK: %[[VAL_28:.*]] = arith.subi %[[VAL_13]], %[[VAL_1]] : index
-! CHECK: br ^bb1(%[[VAL_15_NSW]], %[[VAL_28]] : index, index)
+! CHECK: br ^bb1(%[[VAL_15]], %[[VAL_28]] : index, index)
! CHECK: ^bb3:
! CHECK: %[[VAL_29:.*]] = fir.load %[[VAL_8]] : !fir.ref<!fir.array<1x!fir.logical<4>>>
! CHECK: return %[[VAL_29]] : !fir.array<1x!fir.logical<4>>
@@ -49,3 +46,42 @@ function test(C)
test = C(1:1)(1:8) == (/'ABCDabcd'/)
end function test
+
+! NSW-LABEL: func @_QPtest(
+! NSW-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>{{.*}}) -> !fir.array<1x!fir.logical<4>> {
+! NSW-DAG: %[[VAL_1:.*]] = arith.constant 1 : index
+! NSW-DAG: %[[VAL_2:.*]] = arith.constant 0 : index
+! NSW-DAG: %[[VAL_3:.*]] = arith.constant 0 : i32
+! NSW-DAG: %[[VAL_4:.*]] = arith.constant 8 : index
+! NSW: %[[VAL_6:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! NSW: %[[VAL_7:.*]] = fir.convert %[[VAL_6]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<1x!fir.char<1,12>>>
+! NSW: %[[VAL_8:.*]] = fir.alloca !fir.array<1x!fir.logical<4>> {bindc_name = "test", uniq_name = "_QFtestEtest"}
+! NSW: %[[VAL_9:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
+! NSW: %[[VAL_10:.*]] = fir.slice %[[VAL_1]], %[[VAL_1]], %[[VAL_1]] : (index, index, index) -> !fir.slice<1>
+! NSW: %[[VAL_11:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.array<1x!fir.char<1,8>>>
+! NSW: br ^bb1(%[[VAL_2]], %[[VAL_1]] : index, index)
+! NSW: ^bb1(%[[VAL_12:.*]]: index, %[[VAL_13:.*]]: index):
+! NSW: %[[VAL_14:.*]] = arith.cmpi sgt, %[[VAL_13]], %[[VAL_2]] : index
+! NSW: cond_br %[[VAL_14]], ^bb2, ^bb3
+! NSW: ^bb2:
+! NSW: %[[VAL_15:.*]] = arith.addi %[[VAL_12]], %[[VAL_1]] : index
+! NSW: %[[VAL_16:.*]] = fir.array_coor %[[VAL_7]](%[[VAL_9]]) {{\[}}%[[VAL_10]]] %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.char<1,12>>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<!fir.char<1,12>>
+! NSW: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (!fir.ref<!fir.char<1,12>>) -> !fir.ref<!fir.array<12x!fir.char<1>>>
+! NSW: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_17]], %[[VAL_2]] : (!fir.ref<!fir.array<12x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+! NSW: %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+! NSW: %[[VAL_20:.*]] = fir.array_coor %[[VAL_11]](%[[VAL_9]]) %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.char<1,8>>>, !fir.shape<1>, index) -> !fir.ref<!fir.char<1,8>>
+! NSW: %[[VAL_21:.*]] = fir.convert %[[VAL_19]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+! NSW: %[[VAL_22:.*]] = fir.convert %[[VAL_20]] : (!fir.ref<!fir.char<1,8>>) -> !fir.ref<i8>
+! NSW: %[[VAL_23:.*]] = fir.convert %[[VAL_4]] : (index) -> i64
+! NSW: %[[VAL_24:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_21]], %[[VAL_22]], %[[VAL_23]], %[[VAL_23]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+! NSW: %[[VAL_25:.*]] = arith.cmpi eq, %[[VAL_24]], %[[VAL_3]] : i32
+! NSW: %[[VAL_26:.*]] = fir.convert %[[VAL_25]] : (i1) -> !fir.logical<4>
+! NSW: %[[VAL_27:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_9]]) %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.logical<4>>>, !fir.shape<1>, index) -> !fir.ref<!fir.logical<4>>
+! NSW: fir.store %[[VAL_26]] to %[[VAL_27]] : !fir.ref<!fir.logical<4>>
+! NSW: %[[VAL_15_NSW:.*]] = arith.addi %[[VAL_12]], %[[VAL_1]] overflow<nsw> : index
+! NSW: %[[VAL_28:.*]] = arith.subi %[[VAL_13]], %[[VAL_1]] : index
+! NSW: br ^bb1(%[[VAL_15_NSW]], %[[VAL_28]] : index, index)
+! NSW: ^bb3:
+! NSW: %[[VAL_29:.*]] = fir.load %[[VAL_8]] : !fir.ref<!fir.array<1x!fir.logical<4>>>
+! NSW: return %[[VAL_29]] : !fir.array<1x!fir.logical<4>>
+! NSW: }
diff --git a/flang/test/Lower/array-temp.f90 b/flang/test/Lower/array-temp.f90
index 718aef84a4e853..10c5ee91d44bda 100644
--- a/flang/test/Lower/array-temp.f90
+++ b/flang/test/Lower/array-temp.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck %s
! CHECK-LABEL: func @_QPss1()
subroutine ss1
diff --git a/flang/test/Lower/components.f90 b/flang/test/Lower/components.f90
index 28e836c5d10456..e1582a8a31e0d4 100644
--- a/flang/test/Lower/components.f90
+++ b/flang/test/Lower/components.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck %s
module components_test
type t1
diff --git a/flang/test/Lower/do_loop.f90 b/flang/test/Lower/do_loop.f90
index 5d8343b8d68a45..a46e6c947391b7 100644
--- a/flang/test/Lower/do_loop.f90
+++ b/flang/test/Lower/do_loop.f90
@@ -1,17 +1,17 @@
! RUN: bbc --use-desc-for-alloc=false -emit-fir -hlfir=false -o - %s | FileCheck %s
! RUN: %flang_fc1 -mllvm --use-desc-for-alloc=false -emit-fir -flang-deprecated-no-hlfir -o - %s | FileCheck %s
-! RUN: %flang_fc1 -mllvm --use-desc-for-alloc=false -emit-fir -flang-deprecated-no-hlfir -fwrapv -o - %s | FileCheck %s --check-prefix=NO-NSW
+! RUN: %flang_fc1 -mllvm --use-desc-for-alloc=false -emit-fir -flang-deprecated-no-hlfir -flang-experimental-integer-overflow -o - %s | FileCheck %s --check-prefix=NSW
! Simple tests for structured ordered loops with loop-control.
! Tests the structure of the loop, storage to index variable and return and
! storage of the final value of the index variable.
-! NO-NSW-NOT: overflow<nsw>
-
! Test a simple loop with the final value of the index variable read outside the loop
! CHECK-LABEL: simple_loop
+! NSW-LABEL: simple_loop
subroutine simple_loop
! CHECK: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_loopEi"}
+ ! NSW: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_loopEi"}
integer :: i
! CHECK: %[[C1:.*]] = arith.constant 1 : i32
@@ -21,14 +21,18 @@ subroutine simple_loop
! CHECK: %[[C1:.*]] = arith.constant 1 : index
! CHECK: %[[LB:.*]] = fir.convert %[[C1_CVT]] : (index) -> i32
! CHECK: %[[LI_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+ ! NSW: %[[LI_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[C1_CVT]] to %[[C5_CVT]] step %[[C1]]
! CHECK-SAME: iter_args(%[[IV:.*]] = %[[LB]]) -> (index, i32) {
do i=1,5
! CHECK: fir.store %[[IV]] to %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[C1]] overflow<nsw> : index
+ ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[C1]] : index
+ ! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[C1:.*]] overflow<nsw> : index
! CHECK: %[[STEPCAST:.*]] = fir.convert %[[C1]] : (index) -> i32
! CHECK: %[[IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[IVINC:.*]] = arith.addi %[[IVLOAD]], %[[STEPCAST]] overflow<nsw> : i32
+ ! NSW: %[[IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
+ ! CHECK: %[[IVINC:.*]] = arith.addi %[[IVLOAD]], %[[STEPCAST]] : i32
+ ! NSW: %[[IVINC:.*]] = arith.addi %[[IVLOAD]], %[[STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LI_NEXT]], %[[IVINC]] : index, i32
! CHECK: }
end do
@@ -40,11 +44,14 @@ subroutine simple_loop
! Test a 2-nested loop with a body composed of a reduction. Values are read from a 2d array.
! CHECK-LABEL: nested_loop
+! NSW-LABEL: nested_loop
subroutine nested_loop
! CHECK: %[[ARR_REF:.*]] = fir.alloca !fir.array<5x5xi32> {bindc_name = "arr", uniq_name = "_QFnested_loopEarr"}
! CHECK: %[[ASUM_REF:.*]] = fir.alloca i32 {bindc_name = "asum", uniq_name = "_QFnested_loopEasum"}
! CHECK: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFnested_loopEi"}
+ ! NSW: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFnested_loopEi"}
! CHECK: %[[J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFnested_loopEj"}
+ ! NSW: %[[J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFnested_loopEj"}
integer :: asum, arr(5,5)
integer :: i, j
asum = 0
@@ -55,6 +62,7 @@ subroutine nested_loop
! CHECK: %[[ST_I:.*]] = arith.constant 1 : index
! CHECK: %[[I_LB:.*]] = fir.convert %[[S_I_CVT]] : (index) -> i32
! CHECK: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+ ! NSW: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[S_I_CVT]] to %[[E_I_CVT]] step %[[ST_I]]
! CHECK-SAME: iter_args(%[[I_IV:.*]] = %[[I_LB]]) -> (index, i32) {
do i=1,5
@@ -66,6 +74,7 @@ subroutine nested_loop
! CHECK: %[[ST_J:.*]] = arith.constant 1 : index
! CHECK: %[[J_LB:.*]] = fir.convert %[[S_J_CVT]] : (index) -> i32
! CHECK: %[[J_RES:.*]]:2 = fir.do_loop %[[LJ:[^ ]*]] =
+ ! NSW: %[[J_RES:.*]]:2 = fir.do_loop %[[LJ:[^ ]*]] =
! CHECK-SAME: %[[S_J_CVT]] to %[[E_J_CVT]] step %[[ST_J]]
! CHECK-SAME: iter_args(%[[J_IV:.*]] = %[[J_LB]]) -> (index, i32) {
do j=1,5
@@ -84,18 +93,24 @@ subroutine nested_loop
! CHECK: %[[ASUM_NEW:.*]] = arith.addi %[[ASUM]], %[[ARR_VAL]] : i32
! CHECK: fir.store %[[ASUM_NEW]] to %[[ASUM_REF]] : !fir.ref<i32>
asum = asum + arr(i,j)
- ! CHECK: %[[LJ_NEXT:.*]] = arith.addi %[[LJ]], %[[ST_J]] overflow<nsw> : index
+ ! CHECK: %[[LJ_NEXT:.*]] = arith.addi %[[LJ]], %[[ST_J]] : index
+ ! NSW: %[[LJ_NEXT:.*]] = arith.addi %[[LJ]], %[[ST_J:.*]] overflow<nsw> : index
! CHECK: %[[J_STEPCAST:.*]] = fir.convert %[[ST_J]] : (index) -> i32
! CHECK: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
- ! CHECK: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] overflow<nsw> : i32
+ ! NSW: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
+ ! CHECK: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] : i32
+ ! NSW: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LJ_NEXT]], %[[J_IVINC]] : index, i32
! CHECK: }
end do
! CHECK: fir.store %[[J_RES]]#1 to %[[J_REF]] : !fir.ref<i32>
- ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_I]] overflow<nsw> : index
+ ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_I]] : index
+ ! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_I:.*]] overflow<nsw> : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[ST_I]] : (index) -> i32
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i32
+ ! NSW: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
+ ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i32
+ ! NSW: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LI_NEXT]], %[[I_IVINC]] : index, i32
! CHECK: }
end do
@@ -104,9 +119,11 @@ subroutine nested_loop
! Test a downcounting loop
! CHECK-LABEL: down_counting_loop
+! NSW-LABEL: down_counting_loop
subroutine down_counting_loop()
integer :: i
! CHECK: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFdown_counting_loopEi"}
+ ! NSW: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFdown_counting_loopEi"}
! CHECK: %[[C5:.*]] = arith.constant 5 : i32
! CHECK: %[[C5_CVT:.*]] = fir.convert %[[C5]] : (i32) -> index
@@ -116,14 +133,18 @@ subroutine down_counting_loop()
! CHECK: %[[CMINUS1_STEP_CVT:.*]] = fir.convert %[[CMINUS1]] : (i32) -> index
! CHECK: %[[I_LB:.*]] = fir.convert %[[C5_CVT]] : (index) -> i32
! CHECK: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+ ! NSW: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[C5_CVT]] to %[[C1_CVT]] step %[[CMINUS1_STEP_CVT]]
! CHECK-SAME: iter_args(%[[I_IV:.*]] = %[[I_LB]]) -> (index, i32) {
do i=5,1,-1
! CHECK: fir.store %[[I_IV]] to %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[CMINUS1_STEP_CVT]] overflow<nsw> : index
+ ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[CMINUS1_STEP_CVT]] : index
+ ! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[CMINUS1_STEP_CVT:.*]] overflow<nsw> : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[CMINUS1_STEP_CVT]] : (index) -> i32
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i32
+ ! NSW: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
+ ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i32
+ ! NSW: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LI_NEXT]], %[[I_IVINC]] : index, i32
! CHECK: }
end do
@@ -132,6 +153,7 @@ subroutine down_counting_loop()
! Test a general loop with a variable step
! CHECK-LABEL: loop_with_variable_step
+! NSW-LABEL: loop_with_variable_step
! CHECK-SAME: (%[[S_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "s"}, %[[E_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "e"}, %[[ST_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "st"}) {
subroutine loop_with_variable_step(s,e,st)
integer :: s, e, st
@@ -144,14 +166,18 @@ subroutine loop_with_variable_step(s,e,st)
! CHECK: %[[ST_CVT:.*]] = fir.convert %[[ST]] : (i32) -> index
! CHECK: %[[I_LB:.*]] = fir.convert %[[S_CVT]] : (index) -> i32
! CHECK: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+ ! NSW: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[S_CVT]] to %[[E_CVT]] step %[[ST_CVT]]
! CHECK-SAME: iter_args(%[[I_IV:.*]] = %[[I_LB]]) -> (index, i32) {
do i=s,e,st
! CHECK: fir.store %[[I_IV]] to %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] overflow<nsw> : index
+ ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] : index
+ ! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT:.*]] overflow<nsw> : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[ST_CVT]] : (index) -> i32
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i32
+ ! NSW: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
+ ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i32
+ ! NSW: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LI_NEXT]], %[[I_IVINC]] : index, i32
! CHECK: }
end do
@@ -160,11 +186,13 @@ subroutine loop_with_variable_step(s,e,st)
! Test usage of pointer variables as index, start, end and step variables
! CHECK-LABEL: loop_with_pointer_variables
+! NSW-LABEL: loop_with_pointer_variables
! CHECK-SAME: (%[[S_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "s", fir.target}, %[[E_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "e", fir.target}, %[[ST_REF:.*]]: !fir.ref<i32> {fir.bindc_name = "st", fir.target}) {
subroutine loop_with_pointer_variables(s,e,st)
! CHECK: %[[E_PTR_REF:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFloop_with_pointer_variablesEeptr.addr"}
! CHECK: %[[I_REF:.*]] = fir.alloca i32 {bindc_name = "i", fir.target, uniq_name = "_QFloop_with_pointer_variablesEi"}
! CHECK: %[[I_PTR_REF:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFloop_with_pointer_variablesEiptr.addr"}
+! NSW: %[[I_PTR_REF:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFloop_with_pointer_variablesEiptr.addr"}
! CHECK: %[[S_PTR_REF:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFloop_with_pointer_variablesEsptr.addr"}
! CHECK: %[[ST_PTR_REF:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFloop_with_pointer_variablesEstptr.addr"}
integer, target :: i
@@ -185,6 +213,7 @@ subroutine loop_with_pointer_variables(s,e,st)
stptr => st
! CHECK: %[[I_PTR:.*]] = fir.load %[[I_PTR_REF]] : !fir.ref<!fir.ptr<i32>>
+! NSW: %[[I_PTR:.*]] = fir.load %[[I_PTR_REF]] : !fir.ref<!fir.ptr<i32>>
! CHECK: %[[S_PTR:.*]] = fir.load %[[S_PTR_REF]] : !fir.ref<!fir.ptr<i32>>
! CHECK: %[[S:.*]] = fir.load %[[S_PTR]] : !fir.ptr<i32>
! CHECK: %[[S_CVT:.*]] = fir.convert %[[S]] : (i32) -> index
@@ -196,14 +225,18 @@ subroutine loop_with_pointer_variables(s,e,st)
! CHECK: %[[ST_CVT:.*]] = fir.convert %[[ST]] : (i32) -> index
! CHECK: %[[I_LB:.*]] = fir.convert %[[S_CVT]] : (index) -> i32
! CHECK: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+! NSW: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[S_CVT]] to %[[E_CVT]] step %[[ST_CVT]]
! CHECK-SAME: iter_args(%[[I_IV:.*]] = %[[I_LB]]) -> (index, i32) {
do iptr=sptr,eptr,stptr
! CHECK: fir.store %[[I_IV]] to %[[I_PTR]] : !fir.ptr<i32>
-! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] overflow<nsw> : index
+! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] : index
+! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT:.*]] overflow<nsw> : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[ST_CVT]] : (index) -> i32
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_PTR]] : !fir.ptr<i32>
-! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i32
+! NSW: %[[I_IVLOAD:.*]] = fir.load %[[I_PTR]] : !fir.ptr<i32>
+! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i32
+! NSW: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST:.*]] overflow<nsw> : i32
! CHECK: fir.result %[[LI_NEXT]], %[[I_IVINC]] : index, i32
end do
! CHECK: }
@@ -212,9 +245,11 @@ subroutine loop_with_pointer_variables(s,e,st)
! Test usage of non-default integer kind for loop control and loop index variable
! CHECK-LABEL: loop_with_non_default_integer
+! NSW-LABEL: loop_with_non_default_integer
! CHECK-SAME: (%[[S_REF:.*]]: !fir.ref<i64> {fir.bindc_name = "s"}, %[[E_REF:.*]]: !fir.ref<i64> {fir.bindc_name = "e"}, %[[ST_REF:.*]]: !fir.ref<i64> {fir.bindc_name = "st"}) {
subroutine loop_with_non_default_integer(s,e,st)
! CHECK: %[[I_REF:.*]] = fir.alloca i64 {bindc_name = "i", uniq_name = "_QFloop_with_non_default_integerEi"}
+ ! NSW: %[[I_REF:.*]] = fir.alloca i64 {bindc_name = "i", uniq_name = "_QFloop_with_non_default_integerEi"}
integer(kind=8):: i
! CHECK: %[[S:.*]] = fir.load %[[S_REF]] : !fir.ref<i64>
! CHECK: %[[S_CVT:.*]] = fir.convert %[[S]] : (i64) -> index
@@ -226,14 +261,18 @@ subroutine loop_with_non_default_integer(s,e,st)
! CHECK: %[[I_LB:.*]] = fir.convert %[[S_CVT]] : (index) -> i64
! CHECK: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
+ ! NSW: %[[I_RES:.*]]:2 = fir.do_loop %[[LI:[^ ]*]] =
! CHECK-SAME: %[[S_CVT]] to %[[E_CVT]] step %[[ST_CVT]]
! CHECK-SAME: iter_args(%[[I_IV:.*]] = %[[I_LB]]) -> (index, i64) {
do i=s,e,st
! CHECK: fir.store %[[I_IV]] to %[[I_REF]] : !fir.ref<i64>
- ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] overflow<nsw> : index
+ ! CHECK: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT]] : index
+ ! NSW: %[[LI_NEXT:.*]] = arith.addi %[[LI]], %[[ST_CVT:.*]] overflow<nsw> : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[ST_CVT]] : (index) -> i64
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i64>
- ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i64
+ ! NSW: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i64>
+ ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i64
+ ! NSW: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST:.*]] overflow<nsw> : i64
! CHECK: fir.result %[[LI_NEXT]], %[[I_IVINC]] : index, i64
end do
! CHECK: }
diff --git a/flang/test/Lower/do_loop_unstructured.f90 b/flang/test/Lower/do_loop_unstructured.f90
index d8890b2d0926ea..e1a669e09c9a89 100644
--- a/flang/test/Lower/do_loop_unstructured.f90
+++ b/flang/test/Lower/do_loop_unstructured.f90
@@ -1,11 +1,9 @@
! RUN: bbc -emit-fir -hlfir=false -o - %s | FileCheck %s
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -o - %s | FileCheck %s
-! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fwrapv -o - %s | FileCheck %s --check-prefix=NO-NSW
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -flang-experimental-integer-overflow -o - %s | FileCheck %s --check-prefix=NSW
! Tests for unstructured loops.
-! NO-NSW-NOT: overflow<nsw>
-
! Test a simple unstructured loop. Test for the existence of,
! -> The initialization of the trip-count and loop-variable
! -> The branch to the body or the exit inside the header
@@ -41,12 +39,42 @@ subroutine simple_unstructured()
! CHECK: fir.store %[[TRIP_VAR_NEXT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR:.*]] = fir.load %[[LOOP_VAR_REF]] : !fir.ref<i32>
! CHECK: %[[STEP_ONE_2:.*]] = arith.constant 1 : i32
-! CHECK: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_ONE_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_ONE_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_NEXT]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
! CHECK: ^[[EXIT]]:
! CHECK: return
+! NSW-LABEL: simple_unstructured
+! NSW: %[[TRIP_VAR_REF:.*]] = fir.alloca i32
+! NSW: %[[LOOP_VAR_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_unstructuredEi"}
+! NSW: %[[ONE:.*]] = arith.constant 1 : i32
+! NSW: %[[HUNDRED:.*]] = arith.constant 100 : i32
+! NSW: %[[STEP_ONE:.*]] = arith.constant 1 : i32
+! NSW: %[[TMP1:.*]] = arith.subi %[[HUNDRED]], %[[ONE]] : i32
+! NSW: %[[TMP2:.*]] = arith.addi %[[TMP1]], %[[STEP_ONE]] : i32
+! NSW: %[[TRIP_COUNT:.*]] = arith.divsi %[[TMP2]], %[[STEP_ONE]] : i32
+! NSW: fir.store %[[TRIP_COUNT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[ONE]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER:.*]]
+! NSW: ^[[HEADER]]:
+! NSW: %[[TRIP_VAR:.*]] = fir.load %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO:.*]] = arith.constant 0 : i32
+! NSW: %[[COND:.*]] = arith.cmpi sgt, %[[TRIP_VAR]], %[[ZERO]] : i32
+! NSW: cf.cond_br %[[COND]], ^[[BODY:.*]], ^[[EXIT:.*]]
+! NSW: ^[[BODY]]:
+! NSW: %[[TRIP_VAR:.*]] = fir.load %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[ONE_1:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_NEXT:.*]] = arith.subi %[[TRIP_VAR]], %[[ONE_1]] : i32
+! NSW: fir.store %[[TRIP_VAR_NEXT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR:.*]] = fir.load %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[STEP_ONE_2:.*]] = arith.constant 1 : i32
+! NSW: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_ONE_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_NEXT]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER]]
+! NSW: ^[[EXIT]]:
+! NSW: return
+
! Test an unstructured loop with a step. Mostly similar to the previous one.
! Only difference is a non-unit step.
subroutine simple_unstructured_with_step()
@@ -80,12 +108,42 @@ subroutine simple_unstructured_with_step()
! CHECK: fir.store %[[TRIP_VAR_NEXT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR:.*]] = fir.load %[[LOOP_VAR_REF]] : !fir.ref<i32>
! CHECK: %[[STEP_2:.*]] = arith.constant 2 : i32
-! CHECK: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_NEXT]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
! CHECK: ^[[EXIT]]:
! CHECK: return
+! NSW-LABEL: simple_unstructured_with_step
+! NSW: %[[TRIP_VAR_REF:.*]] = fir.alloca i32
+! NSW: %[[LOOP_VAR_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_unstructured_with_stepEi"}
+! NSW: %[[ONE:.*]] = arith.constant 1 : i32
+! NSW: %[[HUNDRED:.*]] = arith.constant 100 : i32
+! NSW: %[[STEP:.*]] = arith.constant 2 : i32
+! NSW: %[[TMP1:.*]] = arith.subi %[[HUNDRED]], %[[ONE]] : i32
+! NSW: %[[TMP2:.*]] = arith.addi %[[TMP1]], %[[STEP]] : i32
+! NSW: %[[TRIP_COUNT:.*]] = arith.divsi %[[TMP2]], %[[STEP]] : i32
+! NSW: fir.store %[[TRIP_COUNT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[ONE]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER:.*]]
+! NSW: ^[[HEADER]]:
+! NSW: %[[TRIP_VAR:.*]] = fir.load %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO:.*]] = arith.constant 0 : i32
+! NSW: %[[COND:.*]] = arith.cmpi sgt, %[[TRIP_VAR]], %[[ZERO]] : i32
+! NSW: cf.cond_br %[[COND]], ^[[BODY:.*]], ^[[EXIT:.*]]
+! NSW: ^[[BODY]]:
+! NSW: %[[TRIP_VAR:.*]] = fir.load %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[ONE_1:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_NEXT:.*]] = arith.subi %[[TRIP_VAR]], %[[ONE_1]] : i32
+! NSW: fir.store %[[TRIP_VAR_NEXT]] to %[[TRIP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR:.*]] = fir.load %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: %[[STEP_2:.*]] = arith.constant 2 : i32
+! NSW: %[[LOOP_VAR_NEXT:.*]] = arith.addi %[[LOOP_VAR]], %[[STEP_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_NEXT]] to %[[LOOP_VAR_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER]]
+! NSW: ^[[EXIT]]:
+! NSW: return
+
! Test a three nested unstructured loop. Three nesting is the basic case where
! we have loops that are neither innermost or outermost.
subroutine nested_unstructured()
@@ -157,7 +215,7 @@ subroutine nested_unstructured()
! CHECK: fir.store %[[TRIP_VAR_K_NEXT]] to %[[TRIP_VAR_K_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR_K:.*]] = fir.load %[[LOOP_VAR_K_REF]] : !fir.ref<i32>
! CHECK: %[[K_STEP_2:.*]] = arith.constant 1 : i32
-! CHECK: %[[LOOP_VAR_K_NEXT:.*]] = arith.addi %[[LOOP_VAR_K]], %[[K_STEP_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_K_NEXT:.*]] = arith.addi %[[LOOP_VAR_K]], %[[K_STEP_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_K_NEXT]] to %[[LOOP_VAR_K_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER_K]]
! CHECK: ^[[EXIT_K]]:
@@ -167,7 +225,7 @@ subroutine nested_unstructured()
! CHECK: fir.store %[[TRIP_VAR_J_NEXT]] to %[[TRIP_VAR_J_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR_J:.*]] = fir.load %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
! CHECK: %[[J_STEP_2:.*]] = arith.constant 1 : i32
-! CHECK: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %[[J_STEP_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %[[J_STEP_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_J_NEXT]] to %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER_J]]
! CHECK: ^[[EXIT_J]]:
@@ -177,12 +235,96 @@ subroutine nested_unstructured()
! CHECK: fir.store %[[TRIP_VAR_I_NEXT]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR_I:.*]] = fir.load %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: %[[I_STEP_2:.*]] = arith.constant 1 : i32
-! CHECK: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_I_NEXT]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER_I]]
! CHECK: ^[[EXIT_I]]:
! CHECK: return
+! NSW-LABEL: nested_unstructured
+! NSW: %[[TRIP_VAR_K_REF:.*]] = fir.alloca i32
+! NSW: %[[TRIP_VAR_J_REF:.*]] = fir.alloca i32
+! NSW: %[[TRIP_VAR_I_REF:.*]] = fir.alloca i32
+! NSW: %[[LOOP_VAR_I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFnested_unstructuredEi"}
+! NSW: %[[LOOP_VAR_J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFnested_unstructuredEj"}
+! NSW: %[[LOOP_VAR_K_REF:.*]] = fir.alloca i32 {bindc_name = "k", uniq_name = "_QFnested_unstructuredEk"}
+! NSW: %[[I_START:.*]] = arith.constant 1 : i32
+! NSW: %[[I_END:.*]] = arith.constant 100 : i32
+! NSW: %[[I_STEP:.*]] = arith.constant 1 : i32
+! NSW: %[[TMP1:.*]] = arith.subi %[[I_END]], %[[I_START]] : i32
+! NSW: %[[TMP2:.*]] = arith.addi %[[TMP1]], %[[I_STEP]] : i32
+! NSW: %[[TRIP_COUNT_I:.*]] = arith.divsi %[[TMP2]], %[[I_STEP]] : i32
+! NSW: fir.store %[[TRIP_COUNT_I]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[I_START]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_I:.*]]
+! NSW: ^[[HEADER_I]]:
+! NSW: %[[TRIP_VAR_I:.*]] = fir.load %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO_1:.*]] = arith.constant 0 : i32
+! NSW: %[[COND_I:.*]] = arith.cmpi sgt, %[[TRIP_VAR_I]], %[[ZERO_1]] : i32
+! NSW: cf.cond_br %[[COND_I]], ^[[BODY_I:.*]], ^[[EXIT_I:.*]]
+! NSW: ^[[BODY_I]]:
+! NSW: %[[J_START:.*]] = arith.constant 1 : i32
+! NSW: %[[J_END:.*]] = arith.constant 200 : i32
+! NSW: %[[J_STEP:.*]] = arith.constant 1 : i32
+! NSW: %[[TMP3:.*]] = arith.subi %[[J_END]], %[[J_START]] : i32
+! NSW: %[[TMP4:.*]] = arith.addi %[[TMP3]], %[[J_STEP]] : i32
+! NSW: %[[TRIP_COUNT_J:.*]] = arith.divsi %[[TMP4]], %[[J_STEP]] : i32
+! NSW: fir.store %[[TRIP_COUNT_J]] to %[[TRIP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[J_START]] to %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_J:.*]]
+! NSW: ^[[HEADER_J]]:
+! NSW: %[[TRIP_VAR_J:.*]] = fir.load %[[TRIP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO_2:.*]] = arith.constant 0 : i32
+! NSW: %[[COND_J:.*]] = arith.cmpi sgt, %[[TRIP_VAR_J]], %[[ZERO_2]] : i32
+! NSW: cf.cond_br %[[COND_J]], ^[[BODY_J:.*]], ^[[EXIT_J:.*]]
+! NSW: ^[[BODY_J]]:
+! NSW: %[[K_START:.*]] = arith.constant 1 : i32
+! NSW: %[[K_END:.*]] = arith.constant 300 : i32
+! NSW: %[[K_STEP:.*]] = arith.constant 1 : i32
+! NSW: %[[TMP3:.*]] = arith.subi %[[K_END]], %[[K_START]] : i32
+! NSW: %[[TMP4:.*]] = arith.addi %[[TMP3]], %[[K_STEP]] : i32
+! NSW: %[[TRIP_COUNT_K:.*]] = arith.divsi %[[TMP4]], %[[K_STEP]] : i32
+! NSW: fir.store %[[TRIP_COUNT_K]] to %[[TRIP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[K_START]] to %[[LOOP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_K:.*]]
+! NSW: ^[[HEADER_K]]:
+! NSW: %[[TRIP_VAR_K:.*]] = fir.load %[[TRIP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO_2:.*]] = arith.constant 0 : i32
+! NSW: %[[COND_K:.*]] = arith.cmpi sgt, %[[TRIP_VAR_K]], %[[ZERO_2]] : i32
+! NSW: cf.cond_br %[[COND_K]], ^[[BODY_K:.*]], ^[[EXIT_K:.*]]
+! NSW: ^[[BODY_K]]:
+! NSW: %[[TRIP_VAR_K:.*]] = fir.load %[[TRIP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: %[[ONE_1:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_K_NEXT:.*]] = arith.subi %[[TRIP_VAR_K]], %[[ONE_1]] : i32
+! NSW: fir.store %[[TRIP_VAR_K_NEXT]] to %[[TRIP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR_K:.*]] = fir.load %[[LOOP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: %[[K_STEP_2:.*]] = arith.constant 1 : i32
+! NSW: %[[LOOP_VAR_K_NEXT:.*]] = arith.addi %[[LOOP_VAR_K]], %[[K_STEP_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_K_NEXT]] to %[[LOOP_VAR_K_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_K]]
+! NSW: ^[[EXIT_K]]:
+! NSW: %[[TRIP_VAR_J:.*]] = fir.load %[[TRIP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[ONE_1:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_J_NEXT:.*]] = arith.subi %[[TRIP_VAR_J]], %[[ONE_1]] : i32
+! NSW: fir.store %[[TRIP_VAR_J_NEXT]] to %[[TRIP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR_J:.*]] = fir.load %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[J_STEP_2:.*]] = arith.constant 1 : i32
+! NSW: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %[[J_STEP_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_J_NEXT]] to %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_J]]
+! NSW: ^[[EXIT_J]]:
+! NSW: %[[TRIP_VAR_I:.*]] = fir.load %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[ONE_1:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_I_NEXT:.*]] = arith.subi %[[TRIP_VAR_I]], %[[ONE_1]] : i32
+! NSW: fir.store %[[TRIP_VAR_I_NEXT]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR_I:.*]] = fir.load %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[I_STEP_2:.*]] = arith.constant 1 : i32
+! NSW: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_I_NEXT]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER_I]]
+! NSW: ^[[EXIT_I]]:
+! NSW: return
+
! Test the existence of a structured loop inside an unstructured loop.
! Only minimal checks are inserted for the structured loop.
subroutine nested_structured_in_unstructured()
@@ -217,9 +359,9 @@ subroutine nested_structured_in_unstructured()
! CHECK-SAME: %{{.*}} to %{{.*}} step %[[ST:[^ ]*]]
! CHECK-SAME: iter_args(%[[J_IV:.*]] = %{{.*}}) -> (index, i32) {
! CHECK: fir.store %[[J_IV]] to %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
-! CHECK: %[[J_INDEX_NEXT:.*]] = arith.addi %[[J_INDEX]], %[[ST]] overflow<nsw> : index
+! CHECK: %[[J_INDEX_NEXT:.*]] = arith.addi %[[J_INDEX]], %[[ST]] : index
! CHECK: %[[LOOP_VAR_J:.*]] = fir.load %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
-! CHECK: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %{{[^ ]*}} overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %{{[^ ]*}} : i32
! CHECK: }
! CHECK: %[[TRIP_VAR_I:.*]] = fir.load %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: %[[C1_3:.*]] = arith.constant 1 : i32
@@ -227,8 +369,47 @@ subroutine nested_structured_in_unstructured()
! CHECK: fir.store %[[TRIP_VAR_I_NEXT]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: %[[LOOP_VAR_I:.*]] = fir.load %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: %[[I_STEP_2:.*]] = arith.constant 1 : i32
-! CHECK: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] overflow<nsw> : i32
+! CHECK: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] : i32
! CHECK: fir.store %[[LOOP_VAR_I_NEXT]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
! CHECK: ^[[EXIT]]:
! CHECK: return
+
+! NSW-LABEL: nested_structured_in_unstructured
+! NSW: %[[TRIP_VAR_I_REF:.*]] = fir.alloca i32
+! NSW: %[[LOOP_VAR_I_REF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFnested_structured_in_unstructuredEi"}
+! NSW: %[[LOOP_VAR_J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFnested_structured_in_unstructuredEj"}
+! NSW: %[[I_START:.*]] = arith.constant 1 : i32
+! NSW: %[[I_END:.*]] = arith.constant 100 : i32
+! NSW: %[[I_STEP:.*]] = arith.constant 1 : i32
+! NSW: %[[TMP1:.*]] = arith.subi %[[I_END]], %[[I_START]] : i32
+! NSW: %[[TMP2:.*]] = arith.addi %[[TMP1]], %[[I_STEP]] : i32
+! NSW: %[[TRIP_COUNT:.*]] = arith.divsi %[[TMP2]], %[[I_STEP]] : i32
+! NSW: fir.store %[[TRIP_COUNT]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: fir.store %[[I_START]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER:.*]]
+! NSW: ^[[HEADER]]:
+! NSW: %[[TRIP_VAR:.*]] = fir.load %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[ZERO:.*]] = arith.constant 0 : i32
+! NSW: %[[COND:.*]] = arith.cmpi sgt, %[[TRIP_VAR]], %[[ZERO]] : i32
+! NSW: cf.cond_br %[[COND]], ^[[BODY:.*]], ^[[EXIT:.*]]
+! NSW: ^[[BODY]]:
+! NSW: %{{.*}} = fir.do_loop %[[J_INDEX:[^ ]*]] =
+! NSW-SAME: %{{.*}} to %{{.*}} step %[[ST:[^ ]*]]
+! NSW-SAME: iter_args(%[[J_IV:.*]] = %{{.*}}) -> (index, i32) {
+! NSW: fir.store %[[J_IV]] to %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[J_INDEX_NEXT:.*]] = arith.addi %[[J_INDEX]], %[[ST]] overflow<nsw> : index
+! NSW: %[[LOOP_VAR_J:.*]] = fir.load %[[LOOP_VAR_J_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR_J_NEXT:.*]] = arith.addi %[[LOOP_VAR_J]], %{{[^ ]*}} overflow<nsw> : i32
+! NSW: }
+! NSW: %[[TRIP_VAR_I:.*]] = fir.load %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[C1_3:.*]] = arith.constant 1 : i32
+! NSW: %[[TRIP_VAR_I_NEXT:.*]] = arith.subi %[[TRIP_VAR_I]], %[[C1_3]] : i32
+! NSW: fir.store %[[TRIP_VAR_I_NEXT]] to %[[TRIP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[LOOP_VAR_I:.*]] = fir.load %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: %[[I_STEP_2:.*]] = arith.constant 1 : i32
+! NSW: %[[LOOP_VAR_I_NEXT:.*]] = arith.addi %[[LOOP_VAR_I]], %[[I_STEP_2]] overflow<nsw> : i32
+! NSW: fir.store %[[LOOP_VAR_I_NEXT]] to %[[LOOP_VAR_I_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[HEADER]]
+! NSW: ^[[EXIT]]:
+! NSW: return
diff --git a/flang/test/Lower/goto-do-body.f90 b/flang/test/Lower/goto-do-body.f90
index 89e4a7a64a87ba..910e55f1839fd2 100644
--- a/flang/test/Lower/goto-do-body.f90
+++ b/flang/test/Lower/goto-do-body.f90
@@ -48,7 +48,7 @@ subroutine sub1()
! CHECK: fir.store %[[TMP8]] to %[[TRIP]] : !fir.ref<i32>
! CHECK: %[[TMP9:.*]] = fir.load %[[I]] : !fir.ref<i32>
! CHECK: %[[C1_4:.*]] = arith.constant 1 : i32
-! CHECK: %[[TMP10:.*]] = arith.addi %[[TMP9]], %[[C1_4]] overflow<nsw> : i32
+! CHECK: %[[TMP10:.*]] = arith.addi %[[TMP9]], %[[C1_4]] : i32
! CHECK: fir.store %[[TMP10]] to %[[I]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
end do
@@ -115,7 +115,7 @@ subroutine sub2()
! CHECK: fir.store %[[TMP10]] to %[[TRIP]] : !fir.ref<i32>
! CHECK: %[[TMP11:.*]] = fir.load %[[I]] : !fir.ref<i32>
! CHECK: %[[STEP_VAL:.*]] = fir.load %[[STEP_VAR]] : !fir.ref<i32>
-! CHECK: %[[TMP12:.*]] = arith.addi %[[TMP11]], %[[STEP_VAL]] overflow<nsw> : i32
+! CHECK: %[[TMP12:.*]] = arith.addi %[[TMP11]], %[[STEP_VAL]] : i32
! CHECK: fir.store %[[TMP12]] to %[[I]] : !fir.ref<i32>
! CHECK: cf.br ^[[HEADER]]
end do
diff --git a/flang/test/Lower/host-associated.f90 b/flang/test/Lower/host-associated.f90
index 33acdff1bb74cf..9b4269df7bfcb6 100644
--- a/flang/test/Lower/host-associated.f90
+++ b/flang/test/Lower/host-associated.f90
@@ -1,5 +1,5 @@
! Test internal procedure host association lowering.
-! RUN: bbc -hlfir=false -fwrapv %s -o - | FileCheck %s
+! RUN: bbc -hlfir=false %s -o - | FileCheck %s
! -----------------------------------------------------------------------------
! Test non character intrinsic scalars
diff --git a/flang/test/Lower/infinite_loop.f90 b/flang/test/Lower/infinite_loop.f90
index de0bee779c5b62..6942dda8d7a23a 100644
--- a/flang/test/Lower/infinite_loop.f90
+++ b/flang/test/Lower/infinite_loop.f90
@@ -1,11 +1,9 @@
! RUN: bbc -emit-fir -hlfir=false -o - %s | FileCheck %s
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -o - %s | FileCheck %s
-! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fwrapv -o - %s | FileCheck %s --check-prefix=NO-NSW
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -flang-experimental-integer-overflow -o - %s | FileCheck %s --check-prefix=NSW
! Tests for infinite loop.
-! NO-NSW-NOT: overflow<nsw>
-
subroutine empty_infinite()
do
end do
@@ -98,10 +96,10 @@ subroutine structured_loop_in_infinite(i)
! CHECK-SAME: %[[C1_INDEX]] to %[[C10_INDEX]] step %[[C1_1]]
! CHECK-SAME: iter_args(%[[J_IV:.*]] = %[[J_LB]]) -> (index, i32) {
! CHECK: fir.store %[[J_IV]] to %[[J_REF]] : !fir.ref<i32>
-! CHECK: %[[J_NEXT:.*]] = arith.addi %[[J]], %[[C1_1]] overflow<nsw> : index
+! CHECK: %[[J_NEXT:.*]] = arith.addi %[[J]], %[[C1_1]] : index
! CHECK: %[[J_STEPCAST:.*]] = fir.convert %[[C1_1]] : (index) -> i32
! CHECK: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
-! CHECK: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] overflow<nsw> : i32
+! CHECK: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] : i32
! CHECK: fir.result %[[J_NEXT]], %[[J_IVINC]] : index, i32
! CHECK: }
! CHECK: fir.store %[[J_FINAL]]#1 to %[[J_REF]] : !fir.ref<i32>
@@ -109,6 +107,39 @@ subroutine structured_loop_in_infinite(i)
! CHECK: ^[[RETURN]]:
! CHECK: return
+! NSW-LABEL: structured_loop_in_infinite
+! NSW-SAME: %[[I_REF:.*]]: !fir.ref<i32>
+! NSW: %[[J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFstructured_loop_in_infiniteEj"}
+! NSW: cf.br ^[[BODY1:.*]]
+! NSW: ^[[BODY1]]:
+! NSW: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
+! NSW: %[[C100:.*]] = arith.constant 100 : i32
+! NSW: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C100]] : i32
+! NSW: cf.cond_br %[[COND]], ^[[EXIT:.*]], ^[[BODY2:.*]]
+! NSW: ^[[EXIT]]:
+! NSW: cf.br ^[[RETURN:.*]]
+! NSW: ^[[BODY2:.*]]:
+! NSW: %[[C1:.*]] = arith.constant 1 : i32
+! NSW: %[[C1_INDEX:.*]] = fir.convert %[[C1]] : (i32) -> index
+! NSW: %[[C10:.*]] = arith.constant 10 : i32
+! NSW: %[[C10_INDEX:.*]] = fir.convert %[[C10]] : (i32) -> index
+! NSW: %[[C1_1:.*]] = arith.constant 1 : index
+! NSW: %[[J_LB:.*]] = fir.convert %[[C1_INDEX]] : (index) -> i32
+! NSW: %[[J_FINAL:.*]]:2 = fir.do_loop %[[J:[^ ]*]] =
+! NSW-SAME: %[[C1_INDEX]] to %[[C10_INDEX]] step %[[C1_1]]
+! NSW-SAME: iter_args(%[[J_IV:.*]] = %[[J_LB]]) -> (index, i32) {
+! NSW: fir.store %[[J_IV]] to %[[J_REF]] : !fir.ref<i32>
+! NSW: %[[J_NEXT:.*]] = arith.addi %[[J]], %[[C1_1]] overflow<nsw> : index
+! NSW: %[[J_STEPCAST:.*]] = fir.convert %[[C1_1]] : (index) -> i32
+! NSW: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
+! NSW: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] overflow<nsw> : i32
+! NSW: fir.result %[[J_NEXT]], %[[J_IVINC]] : index, i32
+! NSW: }
+! NSW: fir.store %[[J_FINAL]]#1 to %[[J_REF]] : !fir.ref<i32>
+! NSW: cf.br ^[[BODY1]]
+! NSW: ^[[RETURN]]:
+! NSW: return
+
subroutine empty_infinite_in_while(i)
integer :: i
do while (i .gt. 50)
diff --git a/flang/test/Lower/io-implied-do-fixes.f90 b/flang/test/Lower/io-implied-do-fixes.f90
index cd4fd43e051946..a6c115fa80ded1 100644
--- a/flang/test/Lower/io-implied-do-fixes.f90
+++ b/flang/test/Lower/io-implied-do-fixes.f90
@@ -1,20 +1,30 @@
! RUN: bbc --use-desc-for-alloc=false -emit-fir -hlfir=false %s -o - | FileCheck %s
-! RUN: bbc --use-desc-for-alloc=false -emit-fir -hlfir=false -fwrapv %s -o - | FileCheck %s --check-prefix=NO-NSW
+! RUN: bbc --use-desc-for-alloc=false -emit-fir -hlfir=false -integer-overflow %s -o - | FileCheck %s --check-prefix=NSW
! UNSUPPORTED: system-windows
-! NO-NSW-NOT: overflow<nsw>
-
! CHECK-LABEL: func @_QPido1
! CHECK: %[[J_REF_ADDR:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFido1Eiptr.addr"}
! CHECK: %[[J_ADDR:.*]] = fir.load %[[J_REF_ADDR]] : !fir.ref<!fir.ptr<i32>>
! CHECK: %[[J_VAL_FINAL:.*]] = fir.do_loop %[[J_VAL:.*]] = %{{.*}} to %{{.*}} step %{{.*}} -> index {
! CHECK: %[[J_VAL_CVT1:.*]] = fir.convert %[[J_VAL]] : (index) -> i32
! CHECK: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.ptr<i32>
-! CHECK: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! CHECK: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} : index
! CHECK: fir.result %[[J_VAL_NEXT]] : index
! CHECK: }
! CHECK: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]] : (index) -> i32
! CHECK: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.ptr<i32>
+
+! NSW-LABEL: func @_QPido1
+! NSW: %[[J_REF_ADDR:.*]] = fir.alloca !fir.ptr<i32> {uniq_name = "_QFido1Eiptr.addr"}
+! NSW: %[[J_ADDR:.*]] = fir.load %[[J_REF_ADDR]] : !fir.ref<!fir.ptr<i32>>
+! NSW: %[[J_VAL_FINAL:.*]] = fir.do_loop %[[J_VAL:.*]] = %{{.*}} to %{{.*}} step %{{.*}} -> index {
+! NSW: %[[J_VAL_CVT1:.*]] = fir.convert %[[J_VAL]] : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.ptr<i32>
+! NSW: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! NSW: fir.result %[[J_VAL_NEXT]] : index
+! NSW: }
+! NSW: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]] : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.ptr<i32>
subroutine ido1
integer, pointer :: iptr
integer, target :: itgt
@@ -28,11 +38,23 @@ subroutine ido1
! CHECK: %[[J_VAL_FINAL:.*]] = fir.do_loop %[[J_VAL:.*]] = %{{.*}} to %{{.*}} step %{{.*}} -> index {
! CHECK: %[[J_VAL_CVT1:.*]] = fir.convert %[[J_VAL]] : (index) -> i32
! CHECK: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.heap<i32>
-! CHECK: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! CHECK: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} : index
! CHECK: fir.result %[[J_VAL_NEXT]] : index
! CHECK: }
! CHECK: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]] : (index) -> i32
! CHECK: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.heap<i32>
+
+! NSW-LABEL: func @_QPido2
+! NSW: %[[J_REF_ADDR:.*]] = fir.alloca !fir.heap<i32> {uniq_name = "_QFido2Eiptr.addr"}
+! NSW: %[[J_ADDR:.*]] = fir.load %[[J_REF_ADDR]] : !fir.ref<!fir.heap<i32>>
+! NSW: %[[J_VAL_FINAL:.*]] = fir.do_loop %[[J_VAL:.*]] = %{{.*}} to %{{.*}} step %{{.*}} -> index {
+! NSW: %[[J_VAL_CVT1:.*]] = fir.convert %[[J_VAL]] : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.heap<i32>
+! NSW: %[[J_VAL_NEXT:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! NSW: fir.result %[[J_VAL_NEXT]] : index
+! NSW: }
+! NSW: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]] : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.heap<i32>
subroutine ido2
integer, allocatable :: iptr
allocate(iptr)
@@ -47,12 +69,27 @@ subroutine ido2
! CHECK: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.heap<i32>
! CHECK: %[[RES:.*]] = fir.if %[[OK]] -> (i1) {
! CHECK: }
-! CHECK: %[[J_VAL_INC:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! CHECK: %[[J_VAL_INC:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} : index
! CHECK: %[[J_VAL_NEXT:.*]] = arith.select %[[RES]], %[[J_VAL_INC]], %[[J_VAL]] : index
! CHECK: fir.result %[[J_VAL_NEXT]], %[[RES]] : index, i1
! CHECK: }
! CHECK: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]]#0 : (index) -> i32
! CHECK: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.heap<i32
+
+! NSW-LABEL: func @_QPido3
+! NSW: %[[J_REF_ADDR:.*]] = fir.alloca !fir.heap<i32> {uniq_name = "_QFido3Ej.addr"}
+! NSW: %[[J_ADDR:.*]] = fir.load %[[J_REF_ADDR]] : !fir.ref<!fir.heap<i32>>
+! NSW: %[[J_VAL_FINAL:.*]]:2 = fir.iterate_while (%[[J_VAL:.*]] = %{{.*}} to %{{.*}} step %{{.*}}) and (%[[OK:.*]] = {{.*}}) -> (index, i1) {
+! NSW: %[[J_VAL_CVT1:.*]] = fir.convert %[[J_VAL]] : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT1]] to %[[J_ADDR]] : !fir.heap<i32>
+! NSW: %[[RES:.*]] = fir.if %[[OK]] -> (i1) {
+! NSW: }
+! NSW: %[[J_VAL_INC:.*]] = arith.addi %[[J_VAL]], %{{[^ ]*}} overflow<nsw> : index
+! NSW: %[[J_VAL_NEXT:.*]] = arith.select %[[RES]], %[[J_VAL_INC]], %[[J_VAL]] : index
+! NSW: fir.result %[[J_VAL_NEXT]], %[[RES]] : index, i1
+! NSW: }
+! NSW: %[[J_VAL_CVT2:.*]] = fir.convert %[[J_VAL_FINAL]]#0 : (index) -> i32
+! NSW: fir.store %[[J_VAL_CVT2]] to %[[J_ADDR]] : !fir.heap<i32
subroutine ido3
integer, allocatable :: j
allocate(j)
diff --git a/flang/test/Lower/loops2.f90 b/flang/test/Lower/loops2.f90
index 60a6bf6c531197..0a587234a991b6 100644
--- a/flang/test/Lower/loops2.f90
+++ b/flang/test/Lower/loops2.f90
@@ -107,7 +107,7 @@ subroutine test_pointer_unstructured_loop()
! CHECK: ^bb4:
! CHECK: %[[VAL_20:.*]] = fir.load %[[VAL_3]] : !fir.ptr<i32>
! CHECK: %[[VAL_21:.*]] = arith.constant 1 : i32
-! CHECK: %[[VAL_22:.*]] = arith.addi %[[VAL_20]], %[[VAL_21]] overflow<nsw> : i32
+! CHECK: %[[VAL_22:.*]] = arith.addi %[[VAL_20]], %[[VAL_21]] : i32
! CHECK: fir.store %[[VAL_22]] to %[[VAL_3]] : !fir.ptr<i32>
! CHECK: br ^bb1
! CHECK: ^bb5:
diff --git a/flang/test/Lower/mixed_loops.f90 b/flang/test/Lower/mixed_loops.f90
index 991fd7aa82bb95..1aa0225129bed6 100644
--- a/flang/test/Lower/mixed_loops.f90
+++ b/flang/test/Lower/mixed_loops.f90
@@ -53,7 +53,7 @@ subroutine while_inside_do_loop
! CHECK: fir.store %[[TDEC]] to %[[T_REF]]
! CHECK: %[[I3:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
! CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
- ! CHECK: %[[IINC:.*]] = arith.addi %[[I3]], %[[C1_2]] overflow<nsw> : i32
+ ! CHECK: %[[IINC:.*]] = arith.addi %[[I3]], %[[C1_2]] : i32
! CHECK: fir.store %[[IINC]] to %[[I_REF]] : !fir.ref<i32>
! CHECK: br ^[[HDR1]]
end do
@@ -100,10 +100,10 @@ subroutine do_inside_while_loop
! CHECK-DAG: %[[C2:.*]] = arith.constant 2 : i32
! CHECK: %[[JINC:.*]] = arith.muli %[[C2]], %[[J2]] : i32
! CHECK: fir.store %[[JINC]] to %[[J_REF]] : !fir.ref<i32>
- ! CHECK: %[[IINC:.*]] = arith.addi %[[IDX]], %[[C1]] overflow<nsw> : index
+ ! CHECK: %[[IINC:.*]] = arith.addi %[[IDX]], %[[C1]] : index
! CHECK: %[[I_STEPCAST:.*]] = fir.convert %[[C1]] : (index) -> i32
! CHECK: %[[I_IVLOAD:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
- ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] overflow<nsw> : i32
+ ! CHECK: %[[I_IVINC:.*]] = arith.addi %[[I_IVLOAD]], %[[I_STEPCAST]] : i32
! CHECK: fir.result %[[IINC]], %[[I_IVINC]] : index, i32
do i=8,13
j=j*2
diff --git a/flang/test/Lower/vector-subscript-io.f90 b/flang/test/Lower/vector-subscript-io.f90
index 372130fd099074..129e3ee1206c09 100644
--- a/flang/test/Lower/vector-subscript-io.f90
+++ b/flang/test/Lower/vector-subscript-io.f90
@@ -30,7 +30,7 @@ subroutine simple(x, y)
! CHECK: %[[VAL_19:.*]] = fir.array_coor %[[VAL_20]](%[[VAL_10]]) {{\[}}%[[VAL_11]]] %[[VAL_18]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<i32>
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_19]] : (!fir.ref<i32>) -> !fir.ref<i64>
! CHECK: %[[VAL_22:.*]] = fir.call @_FortranAioInputInteger(%[[VAL_9]], %[[VAL_21]], %[[VAL_3]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i64>, i32) -> i1
-! CHECK: %[[VAL_23:.*]] = arith.addi %[[VAL_12]], %[[VAL_6]] overflow<nsw> : index
+! CHECK: %[[VAL_23:.*]] = arith.addi %[[VAL_12]], %[[VAL_6]] : index
! CHECK: %[[VAL_24:.*]] = arith.subi %[[VAL_13]], %[[VAL_6]] : index
! CHECK: cf.br ^bb1(%[[VAL_23]], %[[VAL_24]] : index, index)
! CHECK: ^bb3:
@@ -79,7 +79,7 @@ integer function get_substcript()
! CHECK: %[[VAL_49:.*]] = fir.convert %[[VAL_48]] : (i32) -> index
! CHECK: %[[VAL_50:.*]] = fir.array_coor %[[VAL_51]] {{\[}}%[[VAL_42]]] %[[VAL_46]], %[[VAL_49]] : (!fir.box<!fir.array<?x?xf32>>, !fir.slice<2>, index, index) -> !fir.ref<f32>
! CHECK: %[[VAL_52:.*]] = fir.call @_FortranAioInputReal32(%[[VAL_34]], %[[VAL_50]]) {{.*}}: (!fir.ref<i8>, !fir.ref<f32>) -> i1
-! CHECK: %[[VAL_53:.*]] = arith.addi %[[VAL_43]], %[[VAL_30]] overflow<nsw> : index
+! CHECK: %[[VAL_53:.*]] = arith.addi %[[VAL_43]], %[[VAL_30]] : index
! CHECK: %[[VAL_54:.*]] = arith.subi %[[VAL_44]], %[[VAL_30]] : index
! CHECK: cf.br ^bb1(%[[VAL_53]], %[[VAL_54]] : index, index)
! CHECK: ^bb3:
@@ -122,7 +122,7 @@ subroutine with_assumed_shapes(x, y)
! CHECK: %[[VAL_77:.*]] = fir.array_coor %[[VAL_78]] {{\[}}%[[VAL_70]]] %[[VAL_76]] : (!fir.box<!fir.array<?xi32>>, !fir.slice<1>, index) -> !fir.ref<i32>
! CHECK: %[[VAL_79:.*]] = fir.convert %[[VAL_77]] : (!fir.ref<i32>) -> !fir.ref<i64>
! CHECK: %[[VAL_80:.*]] = fir.call @_FortranAioInputInteger(%[[VAL_67]], %[[VAL_79]], %[[VAL_62]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i64>, i32) -> i1
-! CHECK: %[[VAL_81:.*]] = arith.addi %[[VAL_71]], %[[VAL_64]] overflow<nsw> : index
+! CHECK: %[[VAL_81:.*]] = arith.addi %[[VAL_71]], %[[VAL_64]] : index
! CHECK: %[[VAL_82:.*]] = arith.subi %[[VAL_72]], %[[VAL_64]] : index
! CHECK: cf.br ^bb1(%[[VAL_81]], %[[VAL_82]] : index, index)
! CHECK: ^bb3:
@@ -162,7 +162,7 @@ subroutine lower_bounds(x, y)
! CHECK: %[[VAL_107:.*]] = fir.array_coor %[[VAL_108]](%[[VAL_97]]) {{\[}}%[[VAL_99]]] %[[VAL_91]], %[[VAL_106]] : (!fir.ref<!fir.array<4x6xi32>>, !fir.shapeshift<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
! CHECK: %[[VAL_109:.*]] = fir.convert %[[VAL_107]] : (!fir.ref<i32>) -> !fir.ref<i64>
! CHECK: %[[VAL_110:.*]] = fir.call @_FortranAioInputInteger(%[[VAL_96]], %[[VAL_109]], %[[VAL_90]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i64>, i32) -> i1
-! CHECK: %[[VAL_111:.*]] = arith.addi %[[VAL_100]], %[[VAL_93]] overflow<nsw> : index
+! CHECK: %[[VAL_111:.*]] = arith.addi %[[VAL_100]], %[[VAL_93]] : index
! CHECK: %[[VAL_112:.*]] = arith.subi %[[VAL_101]], %[[VAL_93]] : index
! CHECK: cf.br ^bb1(%[[VAL_111]], %[[VAL_112]] : index, index)
! CHECK: ^bb3:
@@ -202,11 +202,11 @@ subroutine two_vectors(x, y1, y2)
! CHECK: %[[VAL_138:.*]] = fir.convert %[[VAL_137]] : (i32) -> index
! CHECK: %[[VAL_139:.*]] = fir.array_coor %[[VAL_140]](%[[VAL_123]]) {{\[}}%[[VAL_124]]] %[[VAL_134]], %[[VAL_138]] : (!fir.ref<!fir.array<4x4xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
! CHECK: %[[VAL_141:.*]] = fir.call @_FortranAioInputReal32(%[[VAL_122]], %[[VAL_139]]) {{.*}}: (!fir.ref<i8>, !fir.ref<f32>) -> i1
-! CHECK: %[[VAL_142:.*]] = arith.addi %[[VAL_128]], %[[VAL_119]] overflow<nsw> : index
+! CHECK: %[[VAL_142:.*]] = arith.addi %[[VAL_128]], %[[VAL_119]] : index
! CHECK: %[[VAL_143:.*]] = arith.subi %[[VAL_129]], %[[VAL_119]] : index
! CHECK: cf.br ^bb2(%[[VAL_142]], %[[VAL_143]] : index, index)
! CHECK: ^bb4:
-! CHECK: %[[VAL_144:.*]] = arith.addi %[[VAL_125]], %[[VAL_119]] overflow<nsw> : index
+! CHECK: %[[VAL_144:.*]] = arith.addi %[[VAL_125]], %[[VAL_119]] : index
! CHECK: %[[VAL_145:.*]] = arith.subi %[[VAL_126]], %[[VAL_119]] : index
! CHECK: cf.br ^bb1(%[[VAL_144]], %[[VAL_145]] : index, index)
! CHECK: ^bb5:
@@ -245,11 +245,11 @@ subroutine triplets_and_vector(x, y)
! CHECK: %[[VAL_169:.*]] = fir.array_coor %[[VAL_170]](%[[VAL_157]]) {{\[}}%[[VAL_158]]] %[[VAL_162]], %[[VAL_168]] : (!fir.ref<!fir.array<4x4xcomplex<f32>>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<complex<f32>>
! CHECK: %[[VAL_171:.*]] = fir.convert %[[VAL_169]] : (!fir.ref<complex<f32>>) -> !fir.ref<f32>
! CHECK: %[[VAL_172:.*]] = fir.call @_FortranAioInputComplex32(%[[VAL_156]], %[[VAL_171]]) {{.*}}: (!fir.ref<i8>, !fir.ref<f32>) -> i1
-! CHECK: %[[VAL_173:.*]] = arith.addi %[[VAL_162]], %[[VAL_153]] overflow<nsw> : index
+! CHECK: %[[VAL_173:.*]] = arith.addi %[[VAL_162]], %[[VAL_153]] : index
! CHECK: %[[VAL_174:.*]] = arith.subi %[[VAL_163]], %[[VAL_153]] : index
! CHECK: cf.br ^bb2(%[[VAL_173]], %[[VAL_174]] : index, index)
! CHECK: ^bb4:
-! CHECK: %[[VAL_175:.*]] = arith.addi %[[VAL_159]], %[[VAL_153]] overflow<nsw> : index
+! CHECK: %[[VAL_175:.*]] = arith.addi %[[VAL_159]], %[[VAL_153]] : index
! CHECK: %[[VAL_176:.*]] = arith.subi %[[VAL_160]], %[[VAL_153]] : index
! CHECK: cf.br ^bb1(%[[VAL_175]], %[[VAL_176]] : index, index)
! CHECK: ^bb5:
@@ -287,7 +287,7 @@ subroutine simple_char(x, y)
! CHECK: %[[VAL_200:.*]] = fir.convert %[[VAL_199]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
! CHECK: %[[VAL_201:.*]] = fir.convert %[[VAL_184]]#1 : (index) -> i64
! CHECK: %[[VAL_202:.*]] = fir.call @_FortranAioInputAscii(%[[VAL_189]], %[[VAL_200]], %[[VAL_201]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
-! CHECK: %[[VAL_203:.*]] = arith.addi %[[VAL_192]], %[[VAL_183]] overflow<nsw> : index
+! CHECK: %[[VAL_203:.*]] = arith.addi %[[VAL_192]], %[[VAL_183]] : index
! CHECK: %[[VAL_204:.*]] = arith.subi %[[VAL_193]], %[[VAL_183]] : index
! CHECK: cf.br ^bb1(%[[VAL_203]], %[[VAL_204]] : index, index)
! CHECK: ^bb3:
@@ -333,7 +333,7 @@ subroutine substring(x, y, i, j)
! CHECK: %[[VAL_238:.*]] = fir.convert %[[VAL_233]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
! CHECK: %[[VAL_239:.*]] = fir.convert %[[VAL_237]] : (index) -> i64
! CHECK: %[[VAL_240:.*]] = fir.call @_FortranAioInputAscii(%[[VAL_213]], %[[VAL_238]], %[[VAL_239]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
-! CHECK: %[[VAL_241:.*]] = arith.addi %[[VAL_221]], %[[VAL_210]] overflow<nsw> : index
+! CHECK: %[[VAL_241:.*]] = arith.addi %[[VAL_221]], %[[VAL_210]] : index
! CHECK: %[[VAL_242:.*]] = arith.subi %[[VAL_222]], %[[VAL_210]] : index
! CHECK: cf.br ^bb1(%[[VAL_241]], %[[VAL_242]] : index, index)
! CHECK: ^bb3:
@@ -366,7 +366,7 @@ subroutine complex_part(z, y)
! CHECK: %[[VAL_260:.*]] = fir.convert %[[VAL_259]] : (i32) -> index
! CHECK: %[[VAL_261:.*]] = fir.array_coor %[[VAL_262]] {{\[}}%[[VAL_254]]] %[[VAL_260]] : (!fir.box<!fir.array<?xcomplex<f32>>>, !fir.slice<1>, index) -> !fir.ref<f32>
! CHECK: %[[VAL_263:.*]] = fir.call @_FortranAioInputReal32(%[[VAL_251]], %[[VAL_261]]) {{.*}}: (!fir.ref<i8>, !fir.ref<f32>) -> i1
-! CHECK: %[[VAL_264:.*]] = arith.addi %[[VAL_255]], %[[VAL_248]] overflow<nsw> : index
+! CHECK: %[[VAL_264:.*]] = arith.addi %[[VAL_255]], %[[VAL_248]] : index
! CHECK: %[[VAL_265:.*]] = arith.subi %[[VAL_256]], %[[VAL_248]] : index
! CHECK: cf.br ^bb1(%[[VAL_264]], %[[VAL_265]] : index, index)
! CHECK: ^bb3:
@@ -414,7 +414,7 @@ subroutine simple_derived(x, y)
! CHECK: %[[VAL_288:.*]] = fir.embox %[[VAL_286]] : (!fir.ref<!fir.type<_QMderived_typesTt{i:i32,c:!fir.char<1,2>}>>) -> !fir.box<!fir.type<_QMderived_typesTt{i:i32,c:!fir.char<1,2>}>>
! CHECK: %[[VAL_289:.*]] = fir.convert %[[VAL_288]] : (!fir.box<!fir.type<_QMderived_typesTt{i:i32,c:!fir.char<1,2>}>>) -> !fir.box<none>
! CHECK: %[[VAL_290:.*]] = fir.call @_FortranAioInputDerivedType(%[[VAL_276]], %[[VAL_289]], {{.*}}) {{.*}}: (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1
-! CHECK: %[[VAL_291:.*]] = arith.addi %[[VAL_279]], %[[VAL_273]] overflow<nsw> : index
+! CHECK: %[[VAL_291:.*]] = arith.addi %[[VAL_279]], %[[VAL_273]] : index
! CHECK: %[[VAL_292:.*]] = arith.subi %[[VAL_280]], %[[VAL_273]] : index
! CHECK: cf.br ^bb1(%[[VAL_291]], %[[VAL_292]] : index, index)
! CHECK: ^bb3:
@@ -463,11 +463,11 @@ subroutine with_path(b, i)
! CHECK: %[[VAL_325:.*]] = fir.array_coor %[[VAL_326:.*]](%[[VAL_313]]) {{\[}}%[[VAL_315]]] %[[VAL_301]], %[[VAL_324]], %[[VAL_316]] : (!fir.box<!fir.array<?x?x?x!fir.type<_QMderived_typesTt2{a:!fir.array<5x5x!fir.type<_QMderived_typesTt{i:i32,c:!fir.char<1,2>}>>}>>>, !fir.shift<3>, !fir.slice<3>, index, index, index) -> !fir.ref<i32>
! CHECK: %[[VAL_327:.*]] = fir.convert %[[VAL_325]] : (!fir.ref<i32>) -> !fir.ref<i64>
! CHECK: %[[VAL_328:.*]] = fir.call @_FortranAioInputInteger(%[[VAL_308]], %[[VAL_327]], %[[VAL_302]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i64>, i32) -> i1
-! CHECK: %[[VAL_329:.*]] = arith.addi %[[VAL_319]], %[[VAL_305]] overflow<nsw> : index
+! CHECK: %[[VAL_329:.*]] = arith.addi %[[VAL_319]], %[[VAL_305]] : index
! CHECK: %[[VAL_330:.*]] = arith.subi %[[VAL_320]], %[[VAL_305]] : index
! CHECK: cf.br ^bb2(%[[VAL_329]], %[[VAL_330]] : index, index)
! CHECK: ^bb4:
-! CHECK: %[[VAL_331:.*]] = arith.addi %[[VAL_316]], %[[VAL_305]] overflow<nsw> : index
+! CHECK: %[[VAL_331:.*]] = arith.addi %[[VAL_316]], %[[VAL_305]] : index
! CHECK: %[[VAL_332:.*]] = arith.subi %[[VAL_317]], %[[VAL_305]] : index
! CHECK: cf.br ^bb1(%[[VAL_331]], %[[VAL_332]] : index, index)
! CHECK: ^bb5:
@@ -505,7 +505,7 @@ subroutine simple_iostat(x, y, j, stat)
! CHECK: %[[VAL_355:.*]] = fir.convert %[[VAL_354]] : (i32) -> index
! CHECK: %[[VAL_356:.*]] = fir.array_coor %[[VAL_357]] {{\[}}%[[VAL_347]]] %[[VAL_355]] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>, index) -> !fir.ref<f32>
! CHECK: %[[VAL_358:.*]] = fir.call @_FortranAioInputReal32(%[[VAL_343]], %[[VAL_356]]) {{.*}}: (!fir.ref<i8>, !fir.ref<f32>) -> i1
-! CHECK: %[[VAL_359:.*]] = arith.addi %[[VAL_349]], %[[VAL_338]] overflow<nsw> : index
+! CHECK: %[[VAL_359:.*]] = arith.addi %[[VAL_349]], %[[VAL_338]] : index
! CHECK: cf.br ^bb1(%[[VAL_359]], %[[VAL_358]] : index, i1)
! CHECK: ^bb3:
! CHECK: cf.cond_br %[[VAL_350]], ^bb4, ^bb5
@@ -568,10 +568,10 @@ subroutine iostat_in_io_loop(k, j, stat)
! CHECK: %[[VAL_399:.*]] = fir.array_coor %[[VAL_400]](%[[VAL_387]]) {{\[}}%[[VAL_389]]] %[[VAL_394]], %[[VAL_398]] : (!fir.ref<!fir.array<3x5xi32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
! CHECK: %[[VAL_401:.*]] = fir.convert %[[VAL_399]] : (!fir.ref<i32>) -> !fir.ref<i64>
! CHECK: %[[VAL_402:.*]] = fir.call @_FortranAioInputInteger(%[[VAL_378]], %[[VAL_401]], %[[VAL_374]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i64>, i32) -> i1
-! CHECK: %[[VAL_403:.*]] = arith.addi %[[VAL_390]], %[[VAL_371]] overflow<nsw> : index
+! CHECK: %[[VAL_403:.*]] = arith.addi %[[VAL_390]], %[[VAL_371]] : index
! CHECK: cf.br ^bb4(%[[VAL_403]], %[[VAL_402]] : index, i1)
! CHECK: ^bb6(%[[VAL_404:.*]]: i1):
-! CHECK: %[[VAL_405:.*]] = arith.addi %[[VAL_380]], %[[VAL_371]] overflow<nsw> : index
+! CHECK: %[[VAL_405:.*]] = arith.addi %[[VAL_380]], %[[VAL_371]] : index
! CHECK: cf.br ^bb1(%[[VAL_405]], %[[VAL_404]] : index, i1)
! CHECK: ^bb7:
! CHECK: %[[VAL_406:.*]] = fir.convert %[[VAL_380]] : (index) -> i32
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index af38bf272b02b1..fe5e36f704c76c 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -234,6 +234,12 @@ static llvm::cl::opt<bool> integerWrapAround(
llvm::cl::desc("Treat signed integer overflow as two's complement"),
llvm::cl::init(false));
+// TODO: integrate this option with the above
+static llvm::cl::opt<bool>
+ setNSW("integer-overflow",
+ llvm::cl::desc("add nsw flag to internal operations"),
+ llvm::cl::init(false));
+
#define FLANG_EXCLUDE_CODEGEN
#include "flang/Optimizer/Passes/CommandLineOpts.h"
#include "flang/Optimizer/Passes/Pipelines.h"
@@ -375,6 +381,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder);
loweringOptions.setLowerToHighLevelFIR(useHLFIR || emitHLFIR);
loweringOptions.setIntegerWrapAround(integerWrapAround);
+ loweringOptions.setNSWOnLoopVarInc(setNSW);
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {};
Fortran::frontend::TargetOptions targetOpts;
Fortran::frontend::CodeGenOptions cgOpts;
@@ -460,7 +467,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
// Add O2 optimizer pass pipeline.
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
- config.NSWOnLoopVarInc = !integerWrapAround;
+ config.NSWOnLoopVarInc = setNSW;
fir::registerDefaultInlinerPass(config);
fir::createDefaultFIROptimizerPassPipeline(pm, config);
}
More information about the flang-commits
mailing list