[llvm] [LoopInterchange] Extract statically bounded outer epilogues (PR #224196)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 22:39:48 PDT 2026
https://github.com/MattPD created https://github.com/llvm/llvm-project/pull/224196
LoopInterchange requires a tightly nested pair, so an outer loop whose body ends with a small epilogue after the inner loop is rejected even when that epilogue could be moved into its own loop and the remaining nest interchanged. The motivating shape is a column reduction followed by a diagonal update of the same array:
```
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j)
check += A[j][i]; // column-strided reduction
A[i][i] = f(A[i][i]); // per-outer-iteration epilogue
}
```
This patch adds that transformation behind a new option, `-loop-interchange-outer-epilogue-fission`, which is off by default. The pass discovers a single-path outer-loop epilogue, proves from the raw nest-to-epilogue dependences that moving it after the nest is legal, and runs the existing legality and profitability checks on the nest without the epilogue. After those checks succeed, the pass splits the epilogue into a sibling loop and interchanges the remaining nest.
A dependence whose direction DependenceAnalysis cannot determine is accepted only through an exact byte-offset proof, which bounds the outer trip count by the row stride recovered from the address arithmetic. Only plans whose bound is proved at compile time are applied. A plan that needs a runtime bound is declined with a missed-optimization remark and left to a follow-up. The checksum nest in SPEC CPU2000 171.swim is such a case. Moving its epilogue out and interchanging the nest would turn a 10,680-byte inner stride into unit stride, but the bound is a runtime value in the IR generated for the benchmark, so this patch leaves it unchanged.
Assisted-by: Claude Opus 5, GPT-5.6 Sol, GPT-6 Astra, Claude Fable 5.1.
>From 49312405ed7e8ae2e410417abd7eafc93ddc54fb Mon Sep 17 00:00:00 2001
From: "Matt P. Dziubinski" <matt-p.dziubinski at hpe.com>
Date: Tue, 15 Sep 2026 20:58:33 -0500
Subject: [PATCH 1/3] [LoopInterchange] Add IR-free numeric helpers and unit
tests (NFC)
A later change lets LoopInterchange distribute an outer-loop epilogue
out of an imperfect nest before interchanging it. Its legality proof is
plain integer and APInt arithmetic. The interesting failures sit at
numeric boundaries: the magnitude of INT64_MIN does not fit an int64_t,
truncating a wide APInt to a narrower operand's width before a
comparison discards its high bits, and a byte-range computation can
overflow past the end of a known object.
Those boundaries cannot be reached from a lit test. An IR input that
would exercise them is rejected earlier by dependence analysis, or it
cannot be written at all without undefined behavior. So the helpers live
in a private header and are covered directly by unit tests. No behavior
changes here.
Assisted-by: Claude Opus 5, GPT-5.6 Sol, GPT-6 Astra, Claude Fable 5.1.
---
.../Transforms/Scalar/LoopInterchangeUtils.h | 91 +++++++++
.../Transforms/Scalar/CMakeLists.txt | 1 +
.../Transforms/Scalar/LoopInterchangeTest.cpp | 175 ++++++++++++++++++
3 files changed, 267 insertions(+)
create mode 100644 llvm/lib/Transforms/Scalar/LoopInterchangeUtils.h
create mode 100644 llvm/unittests/Transforms/Scalar/LoopInterchangeTest.cpp
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchangeUtils.h b/llvm/lib/Transforms/Scalar/LoopInterchangeUtils.h
new file mode 100644
index 00000000000000..40bdd6cecc1a28
--- /dev/null
+++ b/llvm/lib/Transforms/Scalar/LoopInterchangeUtils.h
@@ -0,0 +1,91 @@
+//===- LoopInterchangeUtils.h - Numeric helpers for LoopInterchange -------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Pure, IR-free numeric helpers for the planned LoopInterchange outer-epilogue
+// fission preparation and its unit tests. Nothing here inspects or mutates a
+// Loop, BasicBlock, or Instruction; every function operates only on integers
+// and APInts so its boundary behaviour (INT64_MIN, wide zero-extension, and
+// fixed byte-range/object containment overflow) can be exercised directly from
+// a unit test without constructing IR that would reject earlier or introduce
+// undefined behaviour. A later phase includes this private lib header from the
+// pass; this phase does not expose a public API.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TRANSFORMS_SCALAR_LOOPINTERCHANGEUTILS_H
+#define LLVM_LIB_TRANSFORMS_SCALAR_LOOPINTERCHANGEUTILS_H
+
+#include "llvm/ADT/APInt.h"
+#include "llvm/Support/MathExtras.h"
+#include <algorithm>
+#include <cstdint>
+#include <limits>
+#include <optional>
+#include <utility>
+
+namespace llvm {
+namespace loop_interchange_utils {
+
+/// Absolute value of \p Value as an unsigned 64-bit integer. Byte row-stride
+/// (W) recovery applies this to the already element-normalized inner
+/// coefficient. The later proof represents W as a positive int64_t and
+/// compares coefficients against both W and -W, so reject a magnitude that
+/// does not fit that domain.
+inline std::optional<uint64_t> checkedAbsToUnsigned(int64_t Value) {
+ uint64_t Magnitude = AbsoluteValue(Value);
+ if (Magnitude > static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return std::nullopt;
+ return Magnitude;
+}
+
+/// Zero-extend two unsigned APInts to a common bit width so an unsigned
+/// comparison never loses high bits. The widened values preserve every set bit
+/// of both inputs (an i129 operand stays i129); nothing is truncated toward the
+/// narrower width.
+inline std::pair<APInt, APInt> zeroExtendToCommonWidth(const APInt &A,
+ const APInt &B) {
+ unsigned Width = std::max(A.getBitWidth(), B.getBitWidth());
+ return {A.zext(Width), B.zext(Width)};
+}
+
+/// True iff \p A u<= \p B, compared as unsigned values after lossless
+/// zero-extension to a common width. A value that only fits in the wider type
+/// keeps its high bits, so a wide trip count is never silently truncated into
+/// comparing small against a narrow bound.
+inline bool unsignedLEWithZeroExtend(const APInt &A, const APInt &B) {
+ std::pair<APInt, APInt> Widened = zeroExtendToCommonWidth(A, B);
+ return Widened.first.ule(Widened.second);
+}
+
+/// Fixed byte-range / known-object containment check for the planned proof.
+/// Given a closed byte range of element start offsets
+/// [\p FirstByte, \p LastByteStart] and a fixed \p ElementSize, verify the
+/// whole accessed range
+/// [\p FirstByte, \p LastByteStart + ElementSize - 1] stays inside
+/// [0, \p ObjectSize). Rejects a negative start, a reversed range, an element
+/// size of zero, an element size that cannot be represented as a signed byte
+/// offset, signed overflow while adding ElementSize - 1 to the last element
+/// start, and a final byte at or past ObjectSize.
+inline bool isFixedByteRangeWithinObject(int64_t FirstByte,
+ int64_t LastByteStart,
+ uint64_t ElementSize,
+ uint64_t ObjectSize) {
+ if (FirstByte < 0 || LastByteStart < FirstByte || ElementSize == 0 ||
+ ElementSize > static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return false;
+ int64_t LastByte;
+ if (AddOverflow(LastByteStart, static_cast<int64_t>(ElementSize) - 1,
+ LastByte))
+ return false;
+ return LastByte >= 0 && static_cast<uint64_t>(LastByte) < ObjectSize;
+}
+
+} // namespace loop_interchange_utils
+} // namespace llvm
+
+#endif // LLVM_LIB_TRANSFORMS_SCALAR_LOOPINTERCHANGEUTILS_H
diff --git a/llvm/unittests/Transforms/Scalar/CMakeLists.txt b/llvm/unittests/Transforms/Scalar/CMakeLists.txt
index eaa2c4eb27a1c8..491060808650e3 100644
--- a/llvm/unittests/Transforms/Scalar/CMakeLists.txt
+++ b/llvm/unittests/Transforms/Scalar/CMakeLists.txt
@@ -10,6 +10,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(ScalarTests
LICMTest.cpp
+ LoopInterchangeTest.cpp
LoopPassManagerTest.cpp
)
diff --git a/llvm/unittests/Transforms/Scalar/LoopInterchangeTest.cpp b/llvm/unittests/Transforms/Scalar/LoopInterchangeTest.cpp
new file mode 100644
index 00000000000000..e7c9918e529aac
--- /dev/null
+++ b/llvm/unittests/Transforms/Scalar/LoopInterchangeTest.cpp
@@ -0,0 +1,175 @@
+//===- LoopInterchangeTest.cpp - LoopInterchange helper unit tests --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Direct coverage of the numeric helpers behind LoopInterchange's
+// outer-epilogue fission. These boundary cases (normalized INT64_MIN, fixed
+// byte-range/object containment overflow, and lossless wide zero-extension)
+// cannot be reached from IR without the pass rejecting earlier or the input
+// having undefined behaviour.
+//
+//===----------------------------------------------------------------------===//
+
+#include "../lib/Transforms/Scalar/LoopInterchangeUtils.h"
+#include "llvm/ADT/APInt.h"
+#include "gtest/gtest.h"
+#include <limits>
+
+using namespace llvm;
+using namespace llvm::loop_interchange_utils;
+
+namespace {
+
+TEST(LoopInterchangeUtilsTest, CheckedAbsRejectsInt64Min) {
+ // INT64_MIN has magnitude 2^63, which does not fit the positive int64_t row
+ // stride domain required by the later coefficient comparisons.
+ EXPECT_FALSE(
+ checkedAbsToUnsigned(std::numeric_limits<int64_t>::min()).has_value());
+
+ // Ordinary magnitudes, including zero and both extreme representable values.
+ auto Zero = checkedAbsToUnsigned(0);
+ ASSERT_TRUE(Zero.has_value());
+ EXPECT_EQ(*Zero, 0u);
+
+ auto Pos = checkedAbsToUnsigned(1335);
+ ASSERT_TRUE(Pos.has_value());
+ EXPECT_EQ(*Pos, 1335u);
+
+ auto Neg = checkedAbsToUnsigned(-1335);
+ ASSERT_TRUE(Neg.has_value());
+ EXPECT_EQ(*Neg, 1335u);
+
+ auto Max = checkedAbsToUnsigned(std::numeric_limits<int64_t>::max());
+ ASSERT_TRUE(Max.has_value());
+ EXPECT_EQ(*Max, 9223372036854775807ULL);
+
+ auto NegMax = checkedAbsToUnsigned(-std::numeric_limits<int64_t>::max());
+ ASSERT_TRUE(NegMax.has_value());
+ EXPECT_EQ(*NegMax, 9223372036854775807ULL);
+}
+
+TEST(LoopInterchangeUtilsTest, FixedByteRangeWithinObject) {
+ // A [4 x [4 x double]] object is 128 bytes; element start offsets run
+ // 0, 8, ..., 120 and each element occupies 8 bytes.
+ constexpr uint64_t ElemSize = 8;
+ constexpr uint64_t ObjSize = 128;
+
+ // In-bounds range: last element starts at byte 120, last byte 127 < 128.
+ EXPECT_TRUE(isFixedByteRangeWithinObject(0, 120, ElemSize, ObjSize));
+
+ // Exact upper boundary: last byte == ObjectSize - 1 is still inside.
+ EXPECT_TRUE(isFixedByteRangeWithinObject(120, 120, ElemSize, ObjSize));
+
+ // One byte outside: last byte == ObjectSize is rejected.
+ EXPECT_FALSE(isFixedByteRangeWithinObject(0, 121, ElemSize, ObjSize));
+ EXPECT_FALSE(isFixedByteRangeWithinObject(121, 121, ElemSize, ObjSize));
+
+ // A negative element start is rejected (out of object, not wrapped).
+ EXPECT_FALSE(isFixedByteRangeWithinObject(-8, 120, ElemSize, ObjSize));
+
+ // A reversed range (last start before first) is rejected.
+ EXPECT_FALSE(isFixedByteRangeWithinObject(120, 0, ElemSize, ObjSize));
+
+ // A zero-sized element occupies no valid byte range.
+ EXPECT_FALSE(isFixedByteRangeWithinObject(0, 8, 0, ObjSize));
+
+ // Adding ElementSize - 1 to the last element start must not wrap a huge
+ // positive last start into a small in-bounds last byte.
+ EXPECT_FALSE(isFixedByteRangeWithinObject(
+ std::numeric_limits<int64_t>::max(), std::numeric_limits<int64_t>::max(),
+ ElemSize, std::numeric_limits<uint64_t>::max()));
+
+ // An element size too large to be a signed byte offset is rejected.
+ EXPECT_FALSE(isFixedByteRangeWithinObject(
+ 0, 0, static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1,
+ ObjSize));
+}
+
+TEST(LoopInterchangeUtilsTest, ZeroExtendUnsignedCompareIsLossless) {
+ // Ordinary same-width unsigned comparisons.
+ EXPECT_TRUE(unsignedLEWithZeroExtend(APInt(64, 1335), APInt(64, 1335)));
+ EXPECT_TRUE(unsignedLEWithZeroExtend(APInt(64, 1334), APInt(64, 1335)));
+ EXPECT_FALSE(unsignedLEWithZeroExtend(APInt(64, 1336), APInt(64, 1335)));
+
+ // A wide i129 trip whose value needs bit 64 (2^64) must be compared at full
+ // width, never truncated toward the i64 bound's width.
+ APInt WideTrip = APInt::getOneBitSet(129, 64); // 2^64
+ APInt NarrowW(64, 1335);
+ std::pair<APInt, APInt> Widened = zeroExtendToCommonWidth(WideTrip, NarrowW);
+ EXPECT_EQ(Widened.first.getBitWidth(), 129u);
+ EXPECT_EQ(Widened.second.getBitWidth(), 129u);
+ // High bits are preserved exactly: the widened trip is still 2^64 and the
+ // widened bound is still 1335 (no truncation in either direction).
+ EXPECT_EQ(Widened.first, APInt::getOneBitSet(129, 64));
+ EXPECT_EQ(Widened.second, APInt(129, 1335));
+ // 2^64 is not u<= 1335, so a wide out-of-range trip is correctly rejected
+ // instead of silently comparing small.
+ EXPECT_FALSE(unsignedLEWithZeroExtend(WideTrip, NarrowW));
+
+ // Other direction: a small i64 trip is u<= a huge i129 bound.
+ APInt HugeW = APInt::getOneBitSet(129, 100); // 2^100
+ EXPECT_TRUE(unsignedLEWithZeroExtend(APInt(64, 4), HugeW));
+ EXPECT_TRUE(unsignedLEWithZeroExtend(HugeW, HugeW));
+ EXPECT_FALSE(unsignedLEWithZeroExtend(HugeW, APInt(64, 4)));
+
+ // Bit 63 is a value bit for this unsigned comparison. This distinguishes
+ // zero extension from sign extension and ule from sle.
+ APInt HighBit64 = APInt::getOneBitSet(64, 63);
+ EXPECT_FALSE(unsignedLEWithZeroExtend(HighBit64, NarrowW));
+ std::pair<APInt, APInt> HighBitWidened =
+ zeroExtendToCommonWidth(HighBit64, APInt(129, 0));
+ EXPECT_EQ(HighBitWidened.first, APInt::getOneBitSet(129, 63));
+ EXPECT_EQ(HighBitWidened.second, APInt(129, 0));
+}
+
+TEST(LoopInterchangeUtilsTest, ByteOffsetProofDiscriminatesHigh32) {
+ // The bound_offset_high32 test case in outer-epilogue-fission-bounds.ll uses
+ // the fixed epilogue offsets 0x10 and 0x100000010, whose low 32 bits agree
+ // while their high 32 bits differ. These checks cover the full-width
+ // arithmetic of the helpers.
+ constexpr int64_t Lo = 0x10; // 16
+ constexpr int64_t Hi = 0x100000010; // 4294967312, low 32 bits == 0x10
+
+ // The two offsets are distinct 64-bit values whose 32-bit truncations
+ // collide.
+ EXPECT_NE(Lo, Hi);
+ EXPECT_EQ(static_cast<uint32_t>(Lo), static_cast<uint32_t>(Hi));
+
+ // checkedAbsToUnsigned recovers a row stride from a byte coefficient. It
+ // preserves the whole 64-bit magnitude, so the two offsets stay distinct.
+ auto AbsLo = checkedAbsToUnsigned(Lo);
+ auto AbsHi = checkedAbsToUnsigned(Hi);
+ ASSERT_TRUE(AbsLo.has_value());
+ ASSERT_TRUE(AbsHi.has_value());
+ EXPECT_EQ(*AbsLo, 0x10ULL);
+ EXPECT_EQ(*AbsHi, 0x100000010ULL);
+ EXPECT_NE(*AbsLo, *AbsHi);
+
+ // The containment check takes full int64_t offsets. Both 8-byte accesses fit
+ // the test case's 0x100000018-byte object (Hi is exactly its last
+ // element)...
+ constexpr uint64_t ElemSize = 8;
+ constexpr uint64_t FixtureObj = 0x100000018;
+ constexpr uint64_t ShrunkObj = 0x100000010;
+ EXPECT_TRUE(isFixedByteRangeWithinObject(Lo, Lo, ElemSize, FixtureObj));
+ EXPECT_TRUE(isFixedByteRangeWithinObject(Hi, Hi, ElemSize, FixtureObj));
+ // ...but shrink the object to 0x100000010 bytes and only the true low offset
+ // fits. A 32-bit-truncating implementation would read Hi as 0x10 and wrongly
+ // report it in range here.
+ EXPECT_TRUE(isFixedByteRangeWithinObject(Lo, Lo, ElemSize, ShrunkObj));
+ EXPECT_FALSE(isFixedByteRangeWithinObject(Hi, Hi, ElemSize, ShrunkObj));
+
+ // The shared APInt comparison helper orders the two offsets at full width:
+ // 0x10 u<= 0x100000010 but not the reverse. A truncation to 32 bits would
+ // make both directions trivially true.
+ APInt LoAP(64, static_cast<uint64_t>(Lo));
+ APInt HiAP(64, static_cast<uint64_t>(Hi));
+ EXPECT_TRUE(unsignedLEWithZeroExtend(LoAP, HiAP));
+ EXPECT_FALSE(unsignedLEWithZeroExtend(HiAP, LoAP));
+}
+
+} // namespace
>From c389f39a190bd6e494b6bc4136649968d41d3572 Mon Sep 17 00:00:00 2001
From: "Matt P. Dziubinski" <matt-p.dziubinski at hpe.com>
Date: Tue, 15 Sep 2026 20:58:34 -0500
Subject: [PATCH 2/3] [LoopInterchange] Prepare outer-epilogue fission plans
LoopInterchange requires a tightly nested pair, so an outer loop whose
body ends with a small epilogue after the inner loop is rejected even
when the epilogue could be moved into its own loop and the remaining
nest interchanged. A column reduction followed by a diagonal update of
the same array is the motivating shape.
This commit adds the analysis for that distribution behind a new option,
-loop-interchange-outer-epilogue-fission, which is off by default, and
applies nothing. The pass discovers a single-path epilogue, proves from
the raw nest-to-epilogue dependences that moving it after the nest is
legal, and runs the existing legality and profitability checks on the
nest with the epilogue hidden. A dependence with an unknown direction is
accepted only through an exact byte-offset proof, which bounds the outer
trip count by the row stride recovered from the address arithmetic. That
bound is classified as proved at compile time or as needing a runtime
check.
The proof needs the full chain of enclosing loops, while the existing
checks work on the selected subnest, so preparation keeps one dependence
matrix for each. A retained nest that performs a convergent operation is
rejected, because distribution would run it under a different set of
threads.
The -loop-interchange-print-prepared-plan option, also off by default,
reports each preparation decision and its reason.
Assisted-by: Claude Opus 5, GPT-5.6 Sol, GPT-6 Astra, Claude Fable 5.1.
---
.../lib/Transforms/Scalar/LoopInterchange.cpp | 2371 +++++++++++++++--
.../outer-epilogue-fission-bounds.ll | 1432 ++++++++++
...r-epilogue-fission-deep-operand-chain.test | 210 ++
.../outer-epilogue-fission-rejections.ll | 2282 ++++++++++++++++
.../outer-epilogue-fission-routing.ll | 1404 ++++++++++
.../outer-epilogue-fission-runtime.ll | 273 ++
.../LoopInterchange/outer-epilogue-fission.ll | 827 ++++++
7 files changed, 8563 insertions(+), 236 deletions(-)
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-deep-operand-chain.test
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 48a5ad97871b63..51725eb0b72130 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -13,7 +13,12 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar/LoopInterchange.h"
+#include "LoopInterchangeUtils.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
@@ -24,17 +29,24 @@
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopNestAnalysis.h"
#include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/GetElementPtrTypeIterator.h"
+#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Operator.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
@@ -42,12 +54,19 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
+#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
+#include <algorithm>
#include <cassert>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <optional>
#include <utility>
#include <vector>
@@ -89,6 +108,11 @@ enum class RuleTy {
Ignore
};
+enum class DependenceColumns {
+ SelectedSubnest,
+ AbsoluteAncestors,
+};
+
} // end anonymous namespace
// Minimum loop depth supported.
@@ -124,6 +148,21 @@ static cl::opt<bool> EnableReduction2Memory(
"loop-interchange-reduction-to-mem", cl::init(false), cl::Hidden,
cl::desc("Support for the inner-loop reduction pattern."));
+static cl::opt<bool> EnableOuterEpilogueFission(
+ "loop-interchange-outer-epilogue-fission", cl::init(false), cl::Hidden,
+ cl::desc("Prepare a provably independent outer-loop epilogue for "
+ "distribution before interchange"));
+
+static cl::opt<unsigned int> MaxOuterEpilogueFissionCandidates(
+ "loop-interchange-max-outer-epilogue-fission-candidates", cl::init(10),
+ cl::Hidden,
+ cl::desc("Maximum number of eligible outer-epilogue fission candidates "
+ "prepared"));
+
+static cl::opt<bool> PrintPreparedEpiloguePlans(
+ "loop-interchange-print-prepared-plan", cl::init(false), cl::Hidden,
+ cl::desc("Print deterministic outer-epilogue preparation decisions"));
+
#ifndef NDEBUG
static bool noDuplicateRulesAndIgnore(ArrayRef<RuleTy> Rules) {
SmallSet<RuleTy, 4> Set;
@@ -167,10 +206,65 @@ static bool inThisOrder(const Instruction *Src, const Instruction *Dst) {
}
#endif
-static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
- Loop *L, DependenceInfo *DI,
- ScalarEvolution *SE,
- OptimizationRemarkEmitter *ORE) {
+namespace {
+
+/// A borrowed view of the instructions omitted by virtual extraction.
+/// Ordinary interchange uses the empty view.
+class ExtractedEpilogueView {
+ const SmallPtrSetImpl<Instruction *> *Excluded = nullptr;
+
+public:
+ ExtractedEpilogueView() = default;
+ explicit ExtractedEpilogueView(const SmallPtrSetImpl<Instruction *> *Excluded)
+ : Excluded(Excluded) {}
+
+ bool excludes(const Instruction *I) const {
+ return Excluded && Excluded->contains(const_cast<Instruction *>(I));
+ }
+
+ explicit operator bool() const { return Excluded != nullptr; }
+
+ const SmallPtrSetImpl<Instruction *> *getExcludedInstructions() const {
+ return Excluded;
+ }
+};
+
+/// Follow the ordinary empty-block walk while treating the extracted slice as
+/// absent. Forwarding PHIs remain in the nest.
+static const BasicBlock &
+skipVirtuallyEmptyBlockUntil(const BasicBlock *From, const BasicBlock *End,
+ ExtractedEpilogueView View) {
+ if (!View)
+ return LoopNest::skipEmptyBlockUntil(From, End);
+
+ assert(From && End && "expected valid path endpoints");
+ if (From == End || !From->getUniqueSuccessor())
+ return *From;
+
+ auto IsVirtuallyEmpty = [View](const BasicBlock *BB) {
+ return all_of(*BB, [View](const Instruction &I) {
+ return I.isTerminator() || View.excludes(&I);
+ });
+ };
+
+ SmallPtrSet<const BasicBlock *, 4> Visited;
+ const BasicBlock *BB = From->getUniqueSuccessor();
+ const BasicBlock *PredBB = From;
+ while (BB && BB != End && IsVirtuallyEmpty(BB) && !Visited.contains(BB)) {
+ Visited.insert(BB);
+ PredBB = BB;
+ BB = BB->getUniqueSuccessor();
+ }
+ return BB == End ? *End : *PredBB;
+}
+
+} // end anonymous namespace
+
+static bool populateDependencyMatrix(
+ CharMatrix &DepMatrix, unsigned Level, Loop *L, DependenceInfo *DI,
+ ScalarEvolution *SE, OptimizationRemarkEmitter *ORE,
+ DependenceColumns Columns = DependenceColumns::SelectedSubnest,
+ ExtractedEpilogueView View = {}) {
using ValueVector = SmallVector<Value *, 16>;
ValueVector MemInstr;
@@ -180,6 +274,8 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
for (BasicBlock *BB : L->blocks()) {
// Scan the BB and collect legal loads and stores.
for (Instruction &I : *BB) {
+ if (View.excludes(&I))
+ continue;
NumInsts++;
if (auto *Ld = dyn_cast<LoadInst>(&I)) {
if (!Ld->isSimple())
@@ -204,12 +300,15 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
LLVM_DEBUG(dbgs() << "Found " << NumMemInstr
<< " Loads and Stores to analyze\n");
if (MaxMemInstrRatio * NumInsts < NumMemInstr * NumMemInstr) {
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLoop",
- L->getStartLoc(), L->getHeader())
- << "Number of loads/stores exceeded, the supported maximum can be "
- "increased with option -loop-interchange-max-mem-instr-ratio.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLoop",
+ L->getStartLoc(), L->getHeader())
+ << "Number of loads/stores exceeded, the supported maximum can "
+ "be "
+ "increased with option "
+ "-loop-interchange-max-mem-instr-ratio.";
+ });
return false;
}
ValueVector::iterator I, IE, J, JE;
@@ -261,29 +360,36 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
// If the Dependence object doesn't have any information, fill the
// dependency vector with '*'.
+ unsigned AbsoluteDepth = L->getLoopDepth() + Level - 1;
if (D->isConfused()) {
assert(Dep.empty() && "Expected empty dependency vector");
- Dep.assign(L->getLoopDepth() + Level - 1, '*');
+ Dep.assign(AbsoluteDepth, '*');
}
- while (Dep.size() < L->getLoopDepth() + Level - 1) {
+ // Absolute mode cannot represent levels below the selected inner loop.
+ if (Columns == DependenceColumns::AbsoluteAncestors &&
+ Dep.size() > AbsoluteDepth)
+ return false;
+
+ while (Dep.size() < AbsoluteDepth) {
Dep.push_back('I');
}
// Dependence analysis reports levels for the full enclosing loop nest.
// Keep only the suffix that corresponds to the selected perfect
// subnest.
- if (Dep.size() > Level)
+ if (Columns == DependenceColumns::SelectedSubnest && Dep.size() > Level)
Dep.erase(Dep.begin(), Dep.end() - Level);
// If all the elements of any direction vector have only '*', legality
// can't be proven. Exit early to save compile time.
if (all_of(Dep, equal_to('*'))) {
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
- L->getStartLoc(), L->getHeader())
- << "All loops have dependencies in all directions.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
+ L->getStartLoc(), L->getHeader())
+ << "All loops have dependencies in all directions.";
+ });
return false;
}
@@ -391,6 +497,16 @@ static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix,
return true;
}
+/// The absolute ancestor prefix must decide a row forward or leave it entirely
+/// equal/independent before the selected outer dimension.
+static bool hasDecisiveOrEqualAncestorPrefix(const CharMatrix &DepMatrix,
+ unsigned OuterLoopId) {
+ for (const std::vector<char> &Row : DepMatrix)
+ if (isLexicographicallyPositive(Row, 0, OuterLoopId) == false)
+ return false;
+ return true;
+}
+
static void populateWorklist(Loop &L, LoopVector &LoopList) {
LLVM_DEBUG(dbgs() << "Calling populateWorklist on Func: "
<< L.getHeader()->getParent()->getName() << " Loop: %"
@@ -414,11 +530,15 @@ static void populateWorklist(Loop &L, LoopVector &LoopList) {
LoopList.push_back(CurrentLoop);
}
+static bool hasSupportedLoopDepth(ArrayRef<Loop *> LoopList) {
+ unsigned LoopNestDepth = LoopList.size();
+ return LoopNestDepth >= MinLoopNestDepth && LoopNestDepth <= MaxLoopNestDepth;
+}
+
static bool hasSupportedLoopDepth(ArrayRef<Loop *> LoopList,
OptimizationRemarkEmitter &ORE) {
- unsigned LoopNestDepth = LoopList.size();
- if (LoopNestDepth < MinLoopNestDepth || LoopNestDepth > MaxLoopNestDepth) {
- LLVM_DEBUG(dbgs() << "Unsupported depth of loop nest " << LoopNestDepth
+ if (!hasSupportedLoopDepth(LoopList)) {
+ LLVM_DEBUG(dbgs() << "Unsupported depth of loop nest " << LoopList.size()
<< ", the supported range is [" << MinLoopNestDepth
<< ", " << MaxLoopNestDepth << "].\n");
Loop *OuterLoop = LoopList.front();
@@ -461,8 +581,13 @@ namespace {
class LoopInterchangeLegality {
public:
LoopInterchangeLegality(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
- OptimizationRemarkEmitter *ORE, DominatorTree *DT)
- : OuterLoop(Outer), InnerLoop(Inner), SE(SE), DT(DT), ORE(ORE) {}
+ OptimizationRemarkEmitter *ORE, DominatorTree *DT,
+ ExtractedEpilogueView View = {})
+ : OuterLoop(Outer), InnerLoop(Inner), SE(SE), DT(DT), ORE(ORE),
+ HasExtractedEpilogueView(bool(View)) {
+ if (const auto *Excluded = View.getExcludedInstructions())
+ ExcludedEpilogueInstructions.insert(Excluded->begin(), Excluded->end());
+ }
/// Check if the loops can be interchanged.
bool canInterchangeLoops(unsigned InnerLoopId, unsigned OuterLoopId,
@@ -488,6 +613,17 @@ class LoopInterchangeLegality {
ArrayRef<Instruction *> getHasNoInfInsts() const { return HasNoInfInsts; }
+ bool isPreparedFor(
+ Loop *Outer, Loop *Inner,
+ const SmallPtrSetImpl<Instruction *> &ExcludedInstructions) const {
+ return OuterLoop == Outer && InnerLoop == Inner &&
+ HasExtractedEpilogueView &&
+ ExcludedEpilogueInstructions.size() == ExcludedInstructions.size() &&
+ all_of(ExcludedInstructions, [&](Instruction *I) {
+ return ExcludedEpilogueInstructions.contains(I);
+ });
+ }
+
/// Record reductions in the inner loop. Currently supported reductions:
/// - initialized from a constant.
/// - reduction PHI node has only one user.
@@ -513,6 +649,15 @@ class LoopInterchangeLegality {
private:
bool tightlyNested(Loop *Outer, Loop *Inner);
bool containsUnsafeInstructions(BasicBlock *BB, Instruction *Skip);
+ bool isVirtuallyExtracted(const Instruction *I) const {
+ return HasExtractedEpilogueView &&
+ ExcludedEpilogueInstructions.contains(const_cast<Instruction *>(I));
+ }
+ ExtractedEpilogueView getExtractedEpilogueView() {
+ if (!HasExtractedEpilogueView)
+ return {};
+ return ExtractedEpilogueView(&ExcludedEpilogueInstructions);
+ }
/// Traverse all PHI nodes in the header of each loop in the loop nest
/// starting from \p OuterLoop, and perform the following checks:
@@ -548,6 +693,10 @@ class LoopInterchangeLegality {
/// Interface to emit optimization remarks.
OptimizationRemarkEmitter *ORE;
+ /// Own the exclusion set so moving a prepared plan cannot invalidate it.
+ SmallPtrSet<Instruction *, 32> ExcludedEpilogueInstructions;
+ bool HasExtractedEpilogueView = false;
+
/// Set of reduction PHIs taking part of a reduction across the inner and
/// outer loop.
SmallPtrSet<PHINode *, 4> OuterInnerReductions;
@@ -564,98 +713,1742 @@ class LoopInterchangeLegality {
/// flags must be dropped when interchanging the loops.
SmallVector<Instruction *, 4> HasNoInfInsts;
- /// Vector of reductions in the inner loop.
- SmallVector<InnerReduction, 8> InnerReductions;
-};
+ /// Vector of reductions in the inner loop.
+ SmallVector<InnerReduction, 8> InnerReductions;
+};
+
+/// Manages information utilized by the profitability check for cache. The main
+/// purpose of this class is to delay the computation of CacheCost until it is
+/// actually needed.
+class CacheCostManager {
+ Loop *OutermostLoop;
+ LoopStandardAnalysisResults *AR;
+ DependenceInfo *DI;
+
+ /// CacheCost for \ref OutermostLoop. Once it is computed, it is cached. Note
+ /// that the result can be nullptr.
+ std::optional<std::unique_ptr<CacheCost>> CC;
+
+ /// Maps each loop to an index representing the optimal position within the
+ /// loop-nest, as determined by the cache cost analysis.
+ DenseMap<const Loop *, unsigned> CostMap;
+
+ void computeIfUnitinialized();
+
+public:
+ CacheCostManager(Loop *OutermostLoop, LoopStandardAnalysisResults *AR,
+ DependenceInfo *DI)
+ : OutermostLoop(OutermostLoop), AR(AR), DI(DI) {}
+ CacheCost *getCacheCost();
+ const DenseMap<const Loop *, unsigned> &getCostMap();
+};
+
+/// LoopInterchangeProfitability checks if it is profitable to interchange the
+/// loop.
+class LoopInterchangeProfitability {
+public:
+ LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
+ OptimizationRemarkEmitter *ORE)
+ : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {}
+
+ /// Check if the loop interchange is profitable.
+ bool isProfitable(const Loop *InnerLoop, const Loop *OuterLoop,
+ unsigned InnerLoopId, unsigned OuterLoopId,
+ CharMatrix &DepMatrix, CacheCostManager &CCM);
+
+private:
+ int getInstrOrderCost();
+ std::optional<bool> isProfitablePerLoopCacheAnalysis(
+ const DenseMap<const Loop *, unsigned> &CostMap, CacheCost *CC);
+ std::optional<bool> isProfitablePerInstrOrderCost();
+ std::optional<bool> isProfitableForVectorization(unsigned InnerLoopId,
+ unsigned OuterLoopId,
+ CharMatrix &DepMatrix);
+ Loop *OuterLoop;
+ Loop *InnerLoop;
+
+ /// Scev analysis.
+ ScalarEvolution *SE;
+
+ /// Interface to emit optimization remarks.
+ OptimizationRemarkEmitter *ORE;
+};
+
+/// LoopInterchangeTransform interchanges the loop.
+class LoopInterchangeTransform {
+public:
+ LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
+ LoopInfo *LI, DominatorTree *DT,
+ const LoopInterchangeLegality &LIL)
+ : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT), LIL(LIL) {}
+
+ /// Interchange OuterLoop and InnerLoop.
+ void transform(ArrayRef<Instruction *> DropNoWrapInsts,
+ ArrayRef<Instruction *> DropNoInfInsts);
+ void reduction2Memory();
+ void restructureLoops(Loop *NewInner, Loop *NewOuter,
+ BasicBlock *OrigInnerPreHeader,
+ BasicBlock *OrigOuterPreHeader);
+ void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop);
+
+private:
+ void adjustLoopBranches();
+
+ Loop *OuterLoop;
+ Loop *InnerLoop;
+
+ /// Scev analysis.
+ ScalarEvolution *SE;
+
+ LoopInfo *LI;
+ DominatorTree *DT;
+
+ const LoopInterchangeLegality &LIL;
+};
+
+struct PreparedOuterIVControl {
+ PHINode *Induction = nullptr;
+ Value *InitialValue = nullptr;
+ Instruction *NextValue = nullptr;
+ ICmpInst *LatchCompare = nullptr;
+ CondBrInst *LatchBranch = nullptr;
+ SmallVector<Instruction *, 4> LatchInstructions;
+};
+
+/// The closed post-inner slice is recorded in path/program order. Forwarding
+/// PHIs remain with the nest.
+struct DistributableOuterEpilogue {
+ Loop *Outer = nullptr;
+ Loop *Inner = nullptr;
+ SmallVector<BasicBlock *, 4> Path;
+ SmallVector<Instruction *, 16> Instructions;
+ SmallPtrSet<Instruction *, 32> InstructionSet;
+ SmallVector<Instruction *, 8> MemoryInstructions;
+ SmallVector<Instruction *, 8> OuterIVDerivedInstructions;
+ PreparedOuterIVControl OuterControl;
+};
+
+/// Dependence objects are query temporaries. Retain only the proof summary
+/// and the ID of any bound requirements that discharge it.
+struct PreparedCrossPartitionDependence {
+ bool UsedByteOffsetProof = false;
+ unsigned RequirementId = 0;
+};
+
+enum class PreparedBoundOutcome { StaticBound, RuntimeBound };
+enum class BoundRequirementKind { ModularOuterSpan, ObjectContainment };
+
+/// One requirement: trip(DomainLoop) u<= Limit.
+struct PreparedBoundRequirement {
+ Loop *DomainLoop = nullptr;
+ APInt Limit;
+ BoundRequirementKind Kind = BoundRequirementKind::ModularOuterSpan;
+ unsigned RequirementId = 0;
+ const SCEV *ExactTrip = nullptr;
+ bool Runtime = false;
+};
+
+struct PreparedTripBound {
+ PreparedBoundOutcome Outcome = PreparedBoundOutcome::StaticBound;
+ SmallVector<PreparedBoundRequirement, 2> Requirements;
+ const SCEV *RuntimeExactTrip = nullptr;
+ APInt Wmin;
+};
+
+/// All state needed to validate one collected-chain tail without mutating IR.
+struct PreparedInterchangePlan {
+ DistributableOuterEpilogue Epilogue;
+ SmallVector<Loop *, 8> RoutingChain;
+ SmallVector<Loop *, 8> AbsoluteAncestors;
+ unsigned AbsoluteOuterLoopId = 0;
+ unsigned AbsoluteInnerLoopId = 0;
+ unsigned RoutingOuterLoopId = 0;
+ unsigned RoutingInnerLoopId = 0;
+ SmallVector<Instruction *, 16> NestMemoryInstructions;
+ SmallVector<PreparedCrossPartitionDependence, 8> CrossDependences;
+ PreparedTripBound TripBound;
+ CharMatrix FissionContextMatrix;
+ CharMatrix RoutingMatrix;
+ Loop *FissionContextScanRoot = nullptr;
+ Loop *RoutingScanRoot = nullptr;
+ unsigned FissionContextLevel = 0;
+ unsigned RoutingLevel = 0;
+ DependenceColumns FissionContextColumns =
+ DependenceColumns::AbsoluteAncestors;
+ DependenceColumns RoutingColumns = DependenceColumns::SelectedSubnest;
+ SmallVector<Instruction *, 4> DropNoWrap;
+ SmallVector<Instruction *, 4> DropNoInf;
+ std::unique_ptr<LoopInterchangeLegality> Legality;
+ bool FissionLegal = false;
+ bool FissionContextMatrixComplete = false;
+ bool FissionContextLegal = false;
+ bool RoutingMatrixComplete = false;
+ bool InterchangeLegal = false;
+ bool Profitable = false;
+};
+
+static void debugPreparationReject(Loop *Outer, Loop *Inner, StringRef Reason) {
+ LLVM_DEBUG(dbgs() << "loop-interchange: outer-epilogue preparation rejected "
+ "candidate in function '"
+ << Outer->getHeader()->getParent()->getName()
+ << "': Outer '" << Outer->getName() << "', Inner '"
+ << Inner->getName() << "': " << Reason << "\n");
+ if (PrintPreparedEpiloguePlans)
+ errs() << "loop-interchange: rejected function="
+ << Outer->getHeader()->getParent()->getName()
+ << " outer=" << Outer->getName() << " inner=" << Inner->getName()
+ << " reason=" << Reason << "\n";
+}
+
+/// Emit a feature-specific remark only after discovering a material epilogue.
+/// Speculative calls to ordinary legality and profitability use a null emitter.
+static void rejectPreparedEpilogue(OptimizationRemarkEmitter *ORE, Loop *Outer,
+ Loop *Inner, StringRef Reason) {
+ debugPreparationReject(Outer, Inner, Reason);
+ if (!ORE)
+ return;
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "OuterEpilogueNotDistributed",
+ Outer->getStartLoc(), Outer->getHeader())
+ << "did not distribute the discovered outer-loop epilogue: "
+ << Reason;
+ });
+}
+
+static bool isSupportedEpilogueValueInstruction(const Instruction &I) {
+ return isa<BinaryOperator, UnaryOperator, CastInst, CmpInst, SelectInst,
+ GetElementPtrInst>(I);
+}
+
+static bool hasSupportedEpilogueMetadata(const Instruction &I) {
+ SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
+ I.getAllMetadataOtherThanDebugLoc(Metadata);
+ return all_of(Metadata, [](const auto &Entry) {
+ switch (Entry.first) {
+ case LLVMContext::MD_tbaa:
+ case LLVMContext::MD_fpmath:
+ case LLVMContext::MD_range:
+ case LLVMContext::MD_tbaa_struct:
+ case LLVMContext::MD_invariant_load:
+ case LLVMContext::MD_alias_scope:
+ case LLVMContext::MD_noalias:
+ case LLVMContext::MD_nontemporal:
+ case LLVMContext::MD_nonnull:
+ case LLVMContext::MD_dereferenceable:
+ case LLVMContext::MD_dereferenceable_or_null:
+ case LLVMContext::MD_prof:
+ case LLVMContext::MD_align:
+ case LLVMContext::MD_noundef:
+ case LLVMContext::MD_noalias_addrspace:
+ case LLVMContext::MD_nofpclass:
+ case LLVMContext::MD_mem_cache_hint:
+ return true;
+ default:
+ return false;
+ }
+ });
+}
+
+static bool isSupportedEpilogueInstruction(const Instruction &I) {
+ if (!hasSupportedEpilogueMetadata(I))
+ return false;
+ if (const auto *Load = dyn_cast<LoadInst>(&I))
+ return Load->isSimple();
+ if (const auto *Store = dyn_cast<StoreInst>(&I))
+ return Store->isSimple();
+ return isSupportedEpilogueValueInstruction(I) && !I.mayReadFromMemory() &&
+ !I.mayHaveSideEffects();
+}
+
+enum class EpilogueOperandClass {
+ Invalid,
+ Invariant,
+ OuterIVDerived,
+};
+
+/// Classify the non-E operand closure and record supported loop-local
+/// definitions in operand-before-user order.
+class EpilogueOperandClassifier {
+ Loop *Outer;
+ Loop *Inner;
+ PHINode *OuterIV;
+ const SmallPtrSetImpl<Instruction *> &EpilogueInstructions;
+ SmallVectorImpl<Instruction *> &Rematerialize;
+ DenseMap<Value *, EpilogueOperandClass> Cache;
+ SmallPtrSet<Value *, 8> Visiting;
+ SmallPtrSet<Instruction *, 8> Recorded;
+
+public:
+ EpilogueOperandClassifier(
+ Loop *Outer, Loop *Inner, PHINode *OuterIV,
+ const SmallPtrSetImpl<Instruction *> &EpilogueInstructions,
+ SmallVectorImpl<Instruction *> &Rematerialize)
+ : Outer(Outer), Inner(Inner), OuterIV(OuterIV),
+ EpilogueInstructions(EpilogueInstructions),
+ Rematerialize(Rematerialize) {}
+
+ EpilogueOperandClass classify(Value *V) {
+ SmallVector<Frame, 16> Stack;
+ if (std::optional<EpilogueOperandClass> Resolved = enter(V, Stack))
+ return *Resolved;
+
+ while (true) {
+ // Index the top rather than hold a reference: entering an operand can
+ // push and reallocate.
+ size_t Top = Stack.size() - 1;
+ if (Stack[Top].Result != EpilogueOperandClass::Invalid &&
+ Stack[Top].NextOperand < Stack[Top].I->getNumOperands()) {
+ Value *Operand = Stack[Top].I->getOperand(Stack[Top].NextOperand++);
+ // A pushed operand becomes the top and finalizes first, so operands
+ // are visited in order and the first Invalid one stops the rest.
+ if (std::optional<EpilogueOperandClass> Resolved =
+ enter(Operand, Stack))
+ merge(Stack[Top].Result, *Resolved);
+ continue;
+ }
+
+ Frame F = Stack.pop_back_val();
+ Visiting.erase(F.I);
+
+ // Even mathematically invariant loop-local definitions need independent
+ // clones. The originals remain with the nest.
+ if (F.Result != EpilogueOperandClass::Invalid &&
+ Recorded.insert(F.I).second)
+ Rematerialize.push_back(F.I);
+ Cache[F.I] = F.Result;
+
+ if (Stack.empty())
+ return F.Result;
+ merge(Stack.back().Result, F.Result);
+ }
+ }
+
+private:
+ /// One suspended operand walk. The heap holds the traversal, so call-stack
+ /// use is independent of operand-chain length.
+ struct Frame {
+ Instruction *I;
+ /// Index of the next operand to enter.
+ unsigned NextOperand;
+ /// Accumulated class: Invariant, then OuterIVDerived, or Invalid.
+ EpilogueOperandClass Result;
+ };
+
+ /// Resolve a leaf, a cached value, or a structurally invalid value. Otherwise
+ /// push a frame and report that the caller must run it.
+ std::optional<EpilogueOperandClass> enter(Value *V,
+ SmallVectorImpl<Frame> &Stack) {
+ if (V == OuterIV)
+ return EpilogueOperandClass::OuterIVDerived;
+ // Retention is decided by loop invariance alone; in particular, do not
+ // follow an enclosing-loop PHI through its recurrence.
+ if (Outer->isLoopInvariant(V))
+ return EpilogueOperandClass::Invariant;
+
+ auto CacheIt = Cache.find(V);
+ if (CacheIt != Cache.end())
+ return CacheIt->second;
+
+ auto *I = dyn_cast<Instruction>(V);
+ if (!I || EpilogueInstructions.contains(I) ||
+ Inner->contains(I->getParent()) || isa<PHINode>(I) ||
+ !isSupportedEpilogueValueInstruction(*I) ||
+ !hasSupportedEpilogueMetadata(*I) || I->mayReadFromMemory() ||
+ I->mayHaveSideEffects() || !Visiting.insert(V).second) {
+ Cache[V] = EpilogueOperandClass::Invalid;
+ return EpilogueOperandClass::Invalid;
+ }
+
+ Stack.push_back({I, 0, EpilogueOperandClass::Invariant});
+ return std::nullopt;
+ }
+
+ static void merge(EpilogueOperandClass &Acc, EpilogueOperandClass C) {
+ if (C == EpilogueOperandClass::Invalid)
+ Acc = EpilogueOperandClass::Invalid;
+ else if (C == EpilogueOperandClass::OuterIVDerived &&
+ Acc != EpilogueOperandClass::Invalid)
+ Acc = EpilogueOperandClass::OuterIVDerived;
+ }
+};
+
+static std::optional<DistributableOuterEpilogue>
+discoverDistributableOuterEpilogue(Loop *Outer, Loop *Inner,
+ ScalarEvolution *SE, DominatorTree *DT) {
+ auto Reject =
+ [&](StringRef Reason) -> std::optional<DistributableOuterEpilogue> {
+ debugPreparationReject(Outer, Inner, Reason);
+ return std::nullopt;
+ };
+
+ if (Outer->getSubLoops().size() != 1 ||
+ Outer->getSubLoops().front() != Inner ||
+ Inner->getParentLoop() != Outer || !Inner->isInnermost())
+ return Reject("not a direct single-child/leaf-inner pair");
+
+ SmallVector<Loop *, 2> Pair = {Outer, Inner};
+ if (!Outer->isLoopSimplifyForm() || !Inner->isLoopSimplifyForm() ||
+ !Outer->hasDedicatedExits() || !Inner->hasDedicatedExits() ||
+ !Outer->isLCSSAForm(*DT) || !Inner->isLCSSAForm(*DT) ||
+ !isComputableLoopNest(SE, Pair))
+ return Reject("pair is not canonical LoopSimplify/LCSSA with computable "
+ "single exits");
+
+ BasicBlock *OuterLatch = Outer->getLoopLatch();
+ BasicBlock *InnerLatch = Inner->getLoopLatch();
+ BasicBlock *InnerExit = Inner->getUniqueExitBlock();
+ if (!OuterLatch || !InnerLatch || !InnerExit ||
+ Outer->getExitingBlock() != OuterLatch ||
+ Inner->getExitingBlock() != InnerLatch ||
+ !isa<CondBrInst>(OuterLatch->getTerminator()) ||
+ !isa<CondBrInst>(InnerLatch->getTerminator()))
+ return Reject("latch/exit control is not the supported canonical form");
+
+ const SCEV *InnerBTC = SE->getBackedgeTakenCount(Inner);
+ if (isa<SCEVCouldNotCompute>(InnerBTC) ||
+ !SE->isLoopInvariant(InnerBTC, Outer))
+ return Reject("child range is not rectangular with respect to the outer "
+ "loop");
+
+ if (Outer->getLoopID() || Inner->getLoopID())
+ return Reject("loop carries metadata without a fission policy");
+
+ PHINode *OuterIV = Outer->getInductionVariable(*SE);
+ std::optional<Loop::LoopBounds> Bounds = Outer->getBounds(*SE);
+ auto *OuterLatchBranch = dyn_cast<CondBrInst>(OuterLatch->getTerminator());
+ ICmpInst *OuterLatchCompare = Outer->getLatchCmpInst();
+ if (!OuterIV || !Bounds || !OuterLatchBranch || !OuterLatchCompare ||
+ !Bounds->getStepValue())
+ return Reject("outer IV/latch control cannot be mapped exactly");
+
+ Value *InitialValue =
+ OuterIV->getIncomingValueForBlock(Outer->getLoopPreheader());
+ auto *NextValue =
+ dyn_cast<Instruction>(OuterIV->getIncomingValueForBlock(OuterLatch));
+ if (!InitialValue || !NextValue || NextValue->getParent() != OuterLatch ||
+ OuterLatchCompare->getParent() != OuterLatch)
+ return Reject("outer IV initial/update mapping is incomplete");
+
+ SmallPtrSet<Value *, 4> MappedOuterControl = {OuterIV, NextValue,
+ OuterLatchCompare};
+ for (Instruction *Control :
+ {NextValue, static_cast<Instruction *>(OuterLatchCompare)})
+ for (Value *Operand : Control->operands())
+ if (!MappedOuterControl.contains(Operand) &&
+ !Outer->isLoopInvariant(Operand))
+ return Reject(
+ "outer latch control depends on an unsupported loop-local value");
+ if (!hasSupportedEpilogueMetadata(*OuterIV) ||
+ !hasSupportedEpilogueMetadata(*NextValue) ||
+ !hasSupportedEpilogueMetadata(*OuterLatchCompare) ||
+ !hasSupportedEpilogueMetadata(*OuterLatchBranch))
+ return Reject("outer IV/latch control carries unsupported metadata");
+
+ DistributableOuterEpilogue Epilogue;
+ Epilogue.Outer = Outer;
+ Epilogue.Inner = Inner;
+ Epilogue.OuterControl.Induction = OuterIV;
+ Epilogue.OuterControl.InitialValue = InitialValue;
+ Epilogue.OuterControl.NextValue = NextValue;
+ Epilogue.OuterControl.LatchCompare = OuterLatchCompare;
+ Epilogue.OuterControl.LatchBranch = OuterLatchBranch;
+
+ SmallPtrSet<BasicBlock *, 8> Visited;
+ BasicBlock *Previous = InnerLatch;
+ BasicBlock *Current = InnerExit;
+ while (Current != OuterLatch) {
+ if (!Visited.insert(Current).second || !Outer->contains(Current) ||
+ Inner->contains(Current) || !DT->dominates(InnerExit, Current))
+ return Reject("exit-to-latch region is not one dominated acyclic path");
+ if (Current != InnerExit && Current->hasAddressTaken())
+ return Reject(
+ "post-inner path block cannot be collapsed after extraction");
+ if (Current->getUniquePredecessor() != Previous)
+ return Reject("exit-to-latch path block does not have its expected "
+ "unique predecessor");
+
+ auto *Branch = dyn_cast<UncondBrInst>(Current->getTerminator());
+ if (!Branch || Current->getUniqueSuccessor() != Branch->getSuccessor(0))
+ return Reject("exit-to-latch path contains conditional or non-straight-"
+ "line control");
+ if (!hasSupportedEpilogueMetadata(*Branch))
+ return Reject("exit-to-latch control carries unsupported metadata");
+
+ for (PHINode &Phi : Current->phis())
+ if (Phi.getNumIncomingValues() != 1 ||
+ Phi.getIncomingBlock(0) != Previous ||
+ !hasSupportedEpilogueMetadata(Phi))
+ return Reject("post-inner forwarding PHI is not single-input or "
+ "carries unsupported metadata");
+
+ Epilogue.Path.push_back(Current);
+ Previous = Current;
+ Current = Branch->getSuccessor(0);
+ }
+
+ if (OuterLatch->getUniquePredecessor() != Previous)
+ return Reject(
+ "the post-inner path does not uniquely enter the outer latch");
+
+ for (PHINode &Phi : OuterLatch->phis())
+ if (Phi.getNumIncomingValues() != 1 ||
+ Phi.getIncomingBlock(0) != Previous ||
+ !hasSupportedEpilogueMetadata(Phi))
+ return Reject("outer-latch forwarding PHI is not single-input or carries "
+ "unsupported metadata");
+
+ for (BasicBlock *BB : Epilogue.Path)
+ for (Instruction &I : *BB) {
+ if (isa<PHINode>(I) || I.isTerminator())
+ continue;
+ if (!isSupportedEpilogueInstruction(I))
+ return Reject("epilogue contains an unsupported operation or metadata");
+ Epilogue.Instructions.push_back(&I);
+ Epilogue.InstructionSet.insert(&I);
+ if (isa<LoadInst, StoreInst>(I))
+ Epilogue.MemoryInstructions.push_back(&I);
+ }
+
+ bool SawOuterControl = false;
+ for (Instruction &I : *OuterLatch) {
+ if (isa<PHINode>(I) || I.isTerminator())
+ continue;
+ if (&I == NextValue || &I == OuterLatchCompare) {
+ SawOuterControl = true;
+ Epilogue.OuterControl.LatchInstructions.push_back(&I);
+ continue;
+ }
+ if (SawOuterControl)
+ return Reject("outer latch has material epilogue instructions after its "
+ "IV/update/compare control slice");
+ if (!isSupportedEpilogueInstruction(I))
+ return Reject("epilogue contains an unsupported operation or metadata");
+ Epilogue.Instructions.push_back(&I);
+ Epilogue.InstructionSet.insert(&I);
+ if (isa<LoadInst, StoreInst>(I))
+ Epilogue.MemoryInstructions.push_back(&I);
+ }
+ if (Epilogue.OuterControl.LatchInstructions.size() != 2 ||
+ Epilogue.OuterControl.LatchInstructions[0] != NextValue ||
+ Epilogue.OuterControl.LatchInstructions[1] != OuterLatchCompare)
+ return Reject("outer latch does not end in the exact IV-update/compare "
+ "control order");
+
+ if (Epilogue.Instructions.empty() || Epilogue.MemoryInstructions.empty())
+ return Reject("post-inner region has no material epilogue memory slice");
+
+ EpilogueOperandClassifier Classifier(Outer, Inner, OuterIV,
+ Epilogue.InstructionSet,
+ Epilogue.OuterIVDerivedInstructions);
+ for (Instruction *I : Epilogue.Instructions)
+ for (Value *Operand : I->operands()) {
+ if (auto *OperandI = dyn_cast<Instruction>(Operand);
+ OperandI && Epilogue.InstructionSet.contains(OperandI))
+ continue;
+ if (Classifier.classify(Operand) == EpilogueOperandClass::Invalid)
+ return Reject("epilogue consumes an inner/reduction or unsupported "
+ "external SSA value");
+ }
+
+ for (Instruction *I : Epilogue.Instructions)
+ for (User *U : I->users()) {
+ if (auto *UserI = dyn_cast<Instruction>(U);
+ UserI && Epilogue.InstructionSet.contains(UserI))
+ continue;
+ if (U->isDroppable())
+ continue;
+ return Reject("epilogue-defined SSA value escapes the closed slice");
+ }
+
+ LLVM_DEBUG(dbgs() << "loop-interchange: discovered a closed outer-loop "
+ "epilogue in function '"
+ << Outer->getHeader()->getParent()->getName() << "' with "
+ << Epilogue.Path.size() << " path block(s), "
+ << Epilogue.Instructions.size() << " instruction(s), and "
+ << Epilogue.OuterIVDerivedInstructions.size()
+ << " rematerialized loop-local definition(s).\n");
+ return std::optional<DistributableOuterEpilogue>(std::move(Epilogue));
+}
+
+static bool
+collectAndCheckPreparationMemory(const DistributableOuterEpilogue &Epilogue,
+ SmallVectorImpl<Instruction *> &NestMemory) {
+ uint64_t NumInstructions = 0;
+ uint64_t NumMemory = 0;
+
+ // Preserve E's discovered path order rather than replacing it with LoopInfo
+ // block order. Validation replays that same ordered slice.
+ for (BasicBlock *BB : Epilogue.Outer->blocks())
+ for (Instruction &I : *BB) {
+ ++NumInstructions;
+ if (!isa<LoadInst, StoreInst>(I))
+ continue;
+ ++NumMemory;
+ if (!Epilogue.InstructionSet.contains(&I))
+ NestMemory.push_back(&I);
+ }
+
+ bool LeftOverflow = false;
+ bool RightOverflow = false;
+ uint64_t Allowed = SaturatingMultiply<uint64_t>(
+ static_cast<uint64_t>(MaxMemInstrRatio), NumInstructions, &LeftOverflow);
+ uint64_t Required =
+ SaturatingMultiply<uint64_t>(NumMemory, NumMemory, &RightOverflow);
+ LLVM_DEBUG(dbgs() << "loop-interchange: outer-epilogue preparation memory "
+ "budget: "
+ << NumMemory << " loads/stores over " << NumInstructions
+ << " instructions.\n");
+ if (LeftOverflow || RightOverflow || Allowed < Required)
+ return false;
+
+ auto IsSimpleMemory = [](Instruction *I) {
+ if (auto *Load = dyn_cast<LoadInst>(I))
+ return Load->isSimple();
+ return cast<StoreInst>(I)->isSimple();
+ };
+ return all_of(NestMemory, IsSimpleMemory) &&
+ all_of(Epilogue.MemoryInstructions, IsSimpleMemory);
+}
+
+/// Reject a retained nest that performs a convergent operation. Distribution
+/// moves E out of the nest, so the retained region would execute under a
+/// different set of threads. Convergence is a CallBase property, and every
+/// convergence-token producer is itself a convergent call inside this region,
+/// so scanning the retained blocks for convergent calls is complete.
+static bool
+hasConvergentOperationInRetainedNest(const DistributableOuterEpilogue &Epi) {
+ for (BasicBlock *BB : Epi.Outer->blocks())
+ for (Instruction &I : *BB) {
+ if (Epi.InstructionSet.contains(&I))
+ continue;
+ if (const auto *Call = dyn_cast<CallBase>(&I);
+ Call && Call->isConvergent())
+ return true;
+ }
+ return false;
+}
+
+struct AffineArrayIndex {
+ int64_t Constant = 0;
+ int64_t OuterCoefficient = 0;
+ int64_t InnerCoefficient = 0;
+
+ bool add(const AffineArrayIndex &Other) {
+ int64_t NewConstant;
+ int64_t NewOuter;
+ int64_t NewInner;
+ if (AddOverflow(Constant, Other.Constant, NewConstant) ||
+ AddOverflow(OuterCoefficient, Other.OuterCoefficient, NewOuter) ||
+ AddOverflow(InnerCoefficient, Other.InnerCoefficient, NewInner))
+ return false;
+ Constant = NewConstant;
+ OuterCoefficient = NewOuter;
+ InnerCoefficient = NewInner;
+ return true;
+ }
+
+ bool multiply(int64_t Factor) {
+ int64_t NewConstant;
+ int64_t NewOuter;
+ int64_t NewInner;
+ if (MulOverflow(Constant, Factor, NewConstant) ||
+ MulOverflow(OuterCoefficient, Factor, NewOuter) ||
+ MulOverflow(InnerCoefficient, Factor, NewInner))
+ return false;
+ Constant = NewConstant;
+ OuterCoefficient = NewOuter;
+ InnerCoefficient = NewInner;
+ return true;
+ }
+
+ bool operator==(const AffineArrayIndex &Other) const {
+ return Constant == Other.Constant &&
+ OuterCoefficient == Other.OuterCoefficient &&
+ InnerCoefficient == Other.InnerCoefficient;
+ }
+};
+
+/// The accessible byte extent proves storage containment, not row stride.
+struct FlattenedByteAccess {
+ Value *Base = nullptr;
+ Type *ElementType = nullptr;
+ unsigned AddressSpace = 0;
+ uint64_t ElementSize = 0;
+ uint64_t KnownAccessibleBytes = 0;
+ AffineArrayIndex ByteOffset;
+};
+
+static std::optional<int64_t> getSignedSCEVConstant(const SCEV *S) {
+ const auto *C = dyn_cast<SCEVConstant>(S);
+ if (!C)
+ return std::nullopt;
+ return C->getAPInt().trySExtValue();
+}
+
+/// Decompose an exact signed-i64 affine expression over the selected outer and
+/// inner iteration numbers.
+static std::optional<AffineArrayIndex>
+decomposeAffineByteOffset(const SCEV *S, Loop *Outer, Loop *Inner,
+ ScalarEvolution &SE) {
+ if (!S->getType()->isIntegerTy(64))
+ return std::nullopt;
+ if (std::optional<int64_t> C = getSignedSCEVConstant(S))
+ return AffineArrayIndex{*C, 0, 0};
+
+ if (const auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
+ if (!AR->isAffine() || (AR->getLoop() != Outer && AR->getLoop() != Inner))
+ return std::nullopt;
+ std::optional<AffineArrayIndex> Start =
+ decomposeAffineByteOffset(AR->getStart(), Outer, Inner, SE);
+ std::optional<AffineArrayIndex> Step =
+ decomposeAffineByteOffset(AR->getStepRecurrence(SE), Outer, Inner, SE);
+ if (!Start || !Step || Step->OuterCoefficient != 0 ||
+ Step->InnerCoefficient != 0)
+ return std::nullopt;
+ AffineArrayIndex IterationTerm;
+ if (AR->getLoop() == Outer)
+ IterationTerm.OuterCoefficient = Step->Constant;
+ else
+ IterationTerm.InnerCoefficient = Step->Constant;
+ if (!Start->add(IterationTerm))
+ return std::nullopt;
+ return Start;
+ }
+
+ if (const auto *Add = dyn_cast<SCEVAddExpr>(S)) {
+ AffineArrayIndex Result;
+ for (const SCEV *Operand : Add->operands()) {
+ std::optional<AffineArrayIndex> Part =
+ decomposeAffineByteOffset(Operand, Outer, Inner, SE);
+ if (!Part || !Result.add(*Part))
+ return std::nullopt;
+ }
+ return Result;
+ }
+
+ if (const auto *Mul = dyn_cast<SCEVMulExpr>(S)) {
+ int64_t ConstantFactor = 1;
+ std::optional<AffineArrayIndex> VaryingFactor;
+ for (const SCEV *Operand : Mul->operands()) {
+ if (std::optional<int64_t> C = getSignedSCEVConstant(Operand)) {
+ int64_t Product;
+ if (MulOverflow(ConstantFactor, *C, Product))
+ return std::nullopt;
+ ConstantFactor = Product;
+ continue;
+ }
+ if (VaryingFactor)
+ return std::nullopt;
+ VaryingFactor = decomposeAffineByteOffset(Operand, Outer, Inner, SE);
+ if (!VaryingFactor)
+ return std::nullopt;
+ }
+ if (!VaryingFactor)
+ return AffineArrayIndex{ConstantFactor, 0, 0};
+ if (!VaryingFactor->multiply(ConstantFactor))
+ return std::nullopt;
+ return VaryingFactor;
+ }
+
+ return std::nullopt;
+}
+
+static std::optional<uint64_t> getKnownAccessibleBytes(Value *Base,
+ const DataLayout &DL) {
+ if (auto *GV = dyn_cast<GlobalVariable>(Base)) {
+ if (!GV->getValueType()->isSized() || GV->hasExternalWeakLinkage())
+ return std::nullopt;
+ uint64_t DeclaredMinimumBytes = GV->getGlobalSize(DL);
+ if (DeclaredMinimumBytes == 0)
+ return std::nullopt;
+ return DeclaredMinimumBytes;
+ }
+
+ ObjectSizeOpts Opts;
+ Opts.NullIsUnknownSize = true;
+ std::optional<TypeSize> Size =
+ getBaseObjectSize(Base, DL, /*TLI=*/nullptr, Opts);
+ if (Size && !Size->isScalable() && Size->getFixedValue() != 0)
+ return Size->getFixedValue();
+
+ bool CanBeNull = true;
+ uint64_t DereferenceableBytes = Base->getPointerDereferenceableBytes(
+ DL, CanBeNull, /*CanBeFreed=*/nullptr);
+ if (DereferenceableBytes == 0 || CanBeNull)
+ return std::nullopt;
+ return DereferenceableBytes;
+}
+
+/// Verify that collectOffset's modulo-2^64 result represents the exact
+/// mathematical byte contribution of this GEP.
+static bool collectExactGEPByteOffset(
+ GEPOperator &GEP, const DataLayout &DL,
+ SmallMapVector<Value *, int64_t, 4> &ExactVariableOffsets,
+ int64_t &ExactConstantOffset) {
+ SmallMapVector<Value *, APInt, 4> CollectedVariableOffsets;
+ APInt CollectedConstantOffset(64, 0);
+ if (!GEP.collectOffset(DL, 64, CollectedVariableOffsets,
+ CollectedConstantOffset))
+ return false;
+
+ ExactConstantOffset = 0;
+ for (gep_type_iterator GTI = gep_type_begin(&GEP), GTE = gep_type_end(&GEP);
+ GTI != GTE; ++GTI) {
+ Value *Index = GTI.getOperand();
+ if (StructType *Struct = GTI.getStructTypeOrNull()) {
+ auto *ConstantIndex = dyn_cast<ConstantInt>(Index);
+ if (!ConstantIndex)
+ return false;
+ uint64_t FieldOffset = DL.getStructLayout(Struct)->getElementOffset(
+ ConstantIndex->getZExtValue());
+ if (FieldOffset >
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return false;
+ int64_t NewConstant;
+ if (AddOverflow(ExactConstantOffset, static_cast<int64_t>(FieldOffset),
+ NewConstant))
+ return false;
+ ExactConstantOffset = NewConstant;
+ continue;
+ }
+
+ TypeSize StrideSize = GTI.getSequentialElementStride(DL);
+ if (StrideSize.isScalable() ||
+ StrideSize.getFixedValue() >
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return false;
+ int64_t Stride = static_cast<int64_t>(StrideSize.getFixedValue());
+ if (auto *ConstantIndex = dyn_cast<ConstantInt>(Index)) {
+ std::optional<int64_t> SignedIndex =
+ ConstantIndex->getValue().trySExtValue();
+ int64_t Term;
+ int64_t NewConstant;
+ if (!SignedIndex || MulOverflow(*SignedIndex, Stride, Term) ||
+ AddOverflow(ExactConstantOffset, Term, NewConstant))
+ return false;
+ ExactConstantOffset = NewConstant;
+ continue;
+ }
+
+ if (Stride == 0)
+ continue;
+ auto It = ExactVariableOffsets.insert({Index, 0}).first;
+ int64_t NewScale;
+ if (AddOverflow(It->second, Stride, NewScale))
+ return false;
+ It->second = NewScale;
+ }
+
+ std::optional<int64_t> CollectedConstant =
+ CollectedConstantOffset.trySExtValue();
+ if (!CollectedConstant || *CollectedConstant != ExactConstantOffset ||
+ CollectedVariableOffsets.size() != ExactVariableOffsets.size())
+ return false;
+ for (const auto &[Variable, ExactScale] : ExactVariableOffsets) {
+ auto It = CollectedVariableOffsets.find(Variable);
+ if (It == CollectedVariableOffsets.end())
+ return false;
+ std::optional<int64_t> CollectedScale = It->second.trySExtValue();
+ if (!CollectedScale || *CollectedScale != ExactScale)
+ return false;
+ }
+ return true;
+}
+
+/// Parse instruction and constant-expression GEPs into one exact byte offset.
+static std::optional<FlattenedByteAccess>
+parseFlattenedByteAccess(Instruction *MemoryInstruction, Loop *Outer,
+ Loop *Inner, ScalarEvolution &SE) {
+ Value *Pointer = getLoadStorePointerOperand(MemoryInstruction);
+ if (!Pointer || !Pointer->getType()->isPointerTy())
+ return std::nullopt;
+
+ const DataLayout &DL = MemoryInstruction->getFunction()->getDataLayout();
+ unsigned AddressSpace = getLoadStoreAddressSpace(MemoryInstruction);
+ if (DL.getIndexTypeSizeInBits(Pointer->getType()) != 64)
+ return std::nullopt;
+
+ Value *OriginalPointer = Pointer;
+ AffineArrayIndex ByteOffset;
+ unsigned GEPCount = 0;
+ while (auto *GEP = dyn_cast<GEPOperator>(Pointer)) {
+ if (++GEPCount > 16 || GEP->getPointerAddressSpace() != AddressSpace ||
+ DL.getIndexTypeSizeInBits(GEP->getPointerOperand()->getType()) != 64)
+ return std::nullopt;
+
+ SmallMapVector<Value *, int64_t, 4> VariableOffsets;
+ int64_t ConstantOffset;
+ if (!collectExactGEPByteOffset(*GEP, DL, VariableOffsets, ConstantOffset) ||
+ !ByteOffset.add(AffineArrayIndex{ConstantOffset, 0, 0}))
+ return std::nullopt;
+
+ for (const auto &[Variable, Scale] : VariableOffsets) {
+ std::optional<AffineArrayIndex> Part =
+ decomposeAffineByteOffset(SE.getSCEV(Variable), Outer, Inner, SE);
+ if (!Part || !Part->multiply(Scale) || !ByteOffset.add(*Part))
+ return std::nullopt;
+ }
+ Pointer = GEP->getPointerOperand();
+ }
+
+ if (GEPCount == 0 || !Pointer->getType()->isPointerTy() ||
+ Pointer->getType()->getPointerAddressSpace() != AddressSpace ||
+ !Outer->isLoopInvariant(Pointer) ||
+ getUnderlyingObject(OriginalPointer, /*MaxLookup=*/16) != Pointer)
+ return std::nullopt;
+
+ Type *ElementType = getLoadStoreType(MemoryInstruction);
+ if (!ElementType->isSized())
+ return std::nullopt;
+ TypeSize StoreSize = DL.getTypeStoreSize(ElementType);
+ TypeSize AllocSize = DL.getTypeAllocSize(ElementType);
+ if (StoreSize.isScalable() || AllocSize.isScalable() ||
+ StoreSize.getFixedValue() == 0 ||
+ StoreSize.getFixedValue() != AllocSize.getFixedValue())
+ return std::nullopt;
+
+ std::optional<uint64_t> AccessibleBytes =
+ getKnownAccessibleBytes(Pointer, DL);
+ if (!AccessibleBytes || *AccessibleBytes == 0)
+ return std::nullopt;
+
+ FlattenedByteAccess Access;
+ Access.Base = Pointer;
+ Access.ElementType = ElementType;
+ Access.AddressSpace = AddressSpace;
+ Access.ElementSize = AllocSize.getFixedValue();
+ Access.KnownAccessibleBytes = *AccessibleBytes;
+ Access.ByteOffset = ByteOffset;
+ return Access;
+}
+
+static std::optional<std::pair<int64_t, int64_t>>
+getAffineIndexRange(const AffineArrayIndex &Index, uint64_t OuterTripCount,
+ uint64_t InnerTripCount) {
+ if (OuterTripCount == 0 || InnerTripCount == 0 ||
+ OuterTripCount >
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) ||
+ InnerTripCount >
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return std::nullopt;
+
+ int64_t Minimum = Index.Constant;
+ int64_t Maximum = Index.Constant;
+ auto Accumulate = [&](int64_t Coefficient, uint64_t TripCount) {
+ int64_t LastIteration = static_cast<int64_t>(TripCount - 1);
+ int64_t Term;
+ if (MulOverflow(Coefficient, LastIteration, Term))
+ return false;
+ if (Coefficient < 0)
+ return !AddOverflow(Minimum, Term, Minimum);
+ return !AddOverflow(Maximum, Term, Maximum);
+ };
+ if (!Accumulate(Index.OuterCoefficient, OuterTripCount) ||
+ !Accumulate(Index.InnerCoefficient, InnerTripCount))
+ return std::nullopt;
+ return std::pair<int64_t, int64_t>(Minimum, Maximum);
+}
+
+static bool isByteAccessRangeWithinObject(const FlattenedByteAccess &Access,
+ uint64_t OuterTripCount,
+ uint64_t InnerTripCount) {
+ std::optional<std::pair<int64_t, int64_t>> Range =
+ getAffineIndexRange(Access.ByteOffset, OuterTripCount, InnerTripCount);
+ if (!Range)
+ return false;
+ return loop_interchange_utils::isFixedByteRangeWithinObject(
+ Range->first, Range->second, Access.ElementSize,
+ Access.KnownAccessibleBytes);
+}
+
+/// Divide raw byte coefficients by element size exactly once.
+static std::optional<AffineArrayIndex>
+normalizeByteOffset(const FlattenedByteAccess &Access) {
+ if (Access.ElementSize == 0 ||
+ Access.ElementSize >
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return std::nullopt;
+ int64_t ElementSize = static_cast<int64_t>(Access.ElementSize);
+ const AffineArrayIndex &Bytes = Access.ByteOffset;
+ if (Bytes.Constant % ElementSize != 0 ||
+ Bytes.OuterCoefficient % ElementSize != 0 ||
+ Bytes.InnerCoefficient % ElementSize != 0)
+ return std::nullopt;
+ return AffineArrayIndex{Bytes.Constant / ElementSize,
+ Bytes.OuterCoefficient / ElementSize,
+ Bytes.InnerCoefficient / ElementSize};
+}
+
+static int64_t positiveModulo(int64_t Value, int64_t Modulus) {
+ assert(Modulus > 0 && "expected a positive modulus");
+ int64_t Result = Value % Modulus;
+ return Result < 0 ? Result + Modulus : Result;
+}
+
+/// Equal addresses imply equal outer iterations modulo W. With outer span at
+/// most W and unit outer coefficient in N, they imply the same outer iteration.
+/// Storage containment is an independent requirement.
+static bool proveSameOuterIterationByByteOffset(
+ Instruction *NestInstruction, Instruction *EpilogueInstruction, Loop *Outer,
+ Loop *Inner, ScalarEvolution &SE, unsigned RequirementId,
+ SmallVectorImpl<PreparedBoundRequirement> &Requirements) {
+ std::optional<FlattenedByteAccess> Nest =
+ parseFlattenedByteAccess(NestInstruction, Outer, Inner, SE);
+ std::optional<FlattenedByteAccess> Epilogue =
+ parseFlattenedByteAccess(EpilogueInstruction, Outer, Inner, SE);
+ if (!Nest || !Epilogue || Nest->Base != Epilogue->Base ||
+ Nest->ElementType != Epilogue->ElementType ||
+ Nest->AddressSpace != Epilogue->AddressSpace ||
+ Nest->ElementSize != Epilogue->ElementSize)
+ return false;
+
+ std::optional<AffineArrayIndex> NestElements = normalizeByteOffset(*Nest);
+ std::optional<AffineArrayIndex> EpilogueElements =
+ normalizeByteOffset(*Epilogue);
+ if (!NestElements || !EpilogueElements)
+ return false;
+ std::optional<uint64_t> AbsInner =
+ loop_interchange_utils::checkedAbsToUnsigned(
+ NestElements->InnerCoefficient);
+ if (!AbsInner || *AbsInner == 0)
+ return false;
+ uint64_t Width = *AbsInner;
+ if (Width > static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
+ return false;
+ int64_t SignedWidth = static_cast<int64_t>(Width);
+
+ if ((NestElements->OuterCoefficient != 1 &&
+ NestElements->OuterCoefficient != -1) ||
+ (NestElements->InnerCoefficient != SignedWidth &&
+ NestElements->InnerCoefficient != -SignedWidth) ||
+ EpilogueElements->InnerCoefficient != 0)
+ return false;
+
+ if (positiveModulo(EpilogueElements->OuterCoefficient, SignedWidth) !=
+ positiveModulo(NestElements->OuterCoefficient, SignedWidth) ||
+ positiveModulo(EpilogueElements->Constant, SignedWidth) !=
+ positiveModulo(NestElements->Constant, SignedWidth))
+ return false;
+
+ if (!isByteAccessRangeWithinObject(*Nest, Width, Width) ||
+ !isByteAccessRangeWithinObject(*Epilogue, Width, Width))
+ return false;
+
+ Requirements.push_back({Outer, APInt(64, Width),
+ BoundRequirementKind::ModularOuterSpan, RequirementId,
+ nullptr, false});
+ Requirements.push_back({Inner, APInt(64, Width),
+ BoundRequirementKind::ObjectContainment,
+ RequirementId, nullptr, false});
+
+ LLVM_DEBUG(
+ dbgs() << "loop-interchange: flattened byte-offset proof accepted an N/E "
+ "pair:\n"
+ << " N: " << *NestInstruction << "\n"
+ << " E: " << *EpilogueInstruction << "\n"
+ << " width = " << Width
+ << ", element bytes = " << Nest->ElementSize
+ << ", bounded modular equality implies equal outer iteration.\n");
+ return true;
+}
+
+/// Query raw N-to-E dependences independently of both normalized matrices.
+static bool proveNestToEpilogueDependences(
+ ArrayRef<Instruction *> NestMemory, ArrayRef<Instruction *> EpilogueMemory,
+ Loop *Outer, Loop *Inner, unsigned AbsoluteOuterLoopId, DependenceInfo *DI,
+ ScalarEvolution *SE,
+ SmallVectorImpl<PreparedCrossPartitionDependence> &Prepared,
+ SmallVectorImpl<PreparedBoundRequirement> &Requirements) {
+ const unsigned OuterLevel = AbsoluteOuterLoopId + 1;
+ unsigned NextRequirementId = 0;
+ for (Instruction *NestI : NestMemory)
+ for (Instruction *EpilogueI : EpilogueMemory) {
+ if (isa<LoadInst>(NestI) && isa<LoadInst>(EpilogueI))
+ continue;
+
+ std::unique_ptr<Dependence> D =
+ DI->depends(NestI, EpilogueI, /*UnderRuntimeAssumptions=*/false);
+ if (!D)
+ continue;
+
+ // Normalization can reverse the endpoints and cannot establish this
+ // raw N-then-E ordering proof.
+ PreparedCrossPartitionDependence Summary;
+ if (!D->isOrdered() || D->isConfused() ||
+ !D->getRuntimeAssumptions().isAlwaysTrue() || D->getSrc() != NestI ||
+ D->getDst() != EpilogueI || D->isDirectionNegative() ||
+ D->getLevels() < OuterLevel) {
+ LLVM_DEBUG(
+ dbgs() << "loop-interchange: rejected an unknown, reversed, "
+ "confused, or assumption-bearing N/E dependence.\n");
+ return false;
+ }
+
+ bool DecisiveForward = false;
+ bool NeedsByteOffsetProof = false;
+ for (unsigned Level = 1; Level <= OuterLevel; ++Level) {
+ unsigned Direction = D->getDirection(Level);
+ if (Level < OuterLevel) {
+ if (Direction == Dependence::DVEntry::LT) {
+ DecisiveForward = true;
+ break;
+ }
+ if (Direction == Dependence::DVEntry::GT ||
+ Direction == Dependence::DVEntry::GE)
+ return false;
+ // EQ, ALL, LE, and NE preserve ancestor iteration order.
+ if (Direction != Dependence::DVEntry::EQ &&
+ Direction != Dependence::DVEntry::ALL &&
+ Direction != Dependence::DVEntry::LE &&
+ Direction != Dependence::DVEntry::NE)
+ return false;
+ continue;
+ }
+
+ if (Direction == Dependence::DVEntry::EQ)
+ continue;
+ if (Direction == Dependence::DVEntry::LT) {
+ DecisiveForward = true;
+ break;
+ }
+ if (Direction == Dependence::DVEntry::ALL &&
+ D->getLevels() == OuterLevel) {
+ NeedsByteOffsetProof = true;
+ break;
+ }
+ return false;
+ }
+
+ if (!DecisiveForward && !NeedsByteOffsetProof &&
+ D->getLevels() != OuterLevel)
+ return false;
+ if (NeedsByteOffsetProof) {
+ Summary.RequirementId = NextRequirementId;
+ if (!proveSameOuterIterationByByteOffset(NestI, EpilogueI, Outer, Inner,
+ *SE, NextRequirementId,
+ Requirements))
+ return false;
+ Summary.UsedByteOffsetProof = true;
+ ++NextRequirementId;
+ }
+ Prepared.push_back(Summary);
+ }
+ return true;
+}
+
+/// Convert every backedge count to a trip count before comparison. Unresolved
+/// requirements may share only the selected outer's exact trip.
+static std::optional<PreparedTripBound> resolvePreparedTripBound(
+ SmallVectorImpl<PreparedBoundRequirement> &Requirements,
+ Loop *SelectedOuter, ScalarEvolution &SE, const TargetTransformInfo *TTI,
+ Instruction *CheckPoint, const char *&RejectReason) {
+ PreparedTripBound Bound;
+ LLVMContext &Ctx = SE.getContext();
+ SCEVExpander Expander(SE, "loop-interchange-bound");
+
+ const SCEV *RuntimeTrip = nullptr;
+ const SCEV *SelectedOuterExactTrip = nullptr;
+ bool HasRuntime = false;
+ APInt Wmin;
+
+ auto MatchWidth =
+ [&](const SCEV *Trip,
+ const APInt &W) -> std::pair<const SCEV *, const SCEV *> {
+ unsigned TripWidth = Trip->getType()->getIntegerBitWidth();
+ unsigned CommonWidth = std::max(TripWidth, W.getBitWidth());
+ const SCEV *WideTrip =
+ CommonWidth > TripWidth
+ ? SE.getZeroExtendExpr(Trip, Type::getIntNTy(Ctx, CommonWidth))
+ : Trip;
+ return {WideTrip, SE.getConstant(W.zext(CommonWidth))};
+ };
+
+ for (PreparedBoundRequirement &Req : Requirements) {
+ Loop *L = Req.DomainLoop;
+ const APInt &W = Req.Limit;
+
+ const SCEV *MaxExit = SE.getConstantMaxBackedgeTakenCount(L);
+ if (!isa<SCEVCouldNotCompute>(MaxExit)) {
+ const SCEV *MaxTrip = SE.getTripCountFromExitCount(MaxExit);
+ if (const auto *C = dyn_cast<SCEVConstant>(MaxTrip))
+ if (loop_interchange_utils::unsignedLEWithZeroExtend(C->getAPInt(), W))
+ continue;
+ }
+
+ const SCEV *ExactExit = SE.getBackedgeTakenCount(L);
+ if (isa<SCEVCouldNotCompute>(ExactExit)) {
+ RejectReason = "exact-backedge-not-computable";
+ return std::nullopt;
+ }
+ const SCEV *ExactTrip = SE.getTripCountFromExitCount(ExactExit);
+ Req.ExactTrip = ExactTrip;
+ auto [WideTrip, WideW] = MatchWidth(ExactTrip, W);
+
+ if (SE.isKnownPredicateAt(ICmpInst::ICMP_ULE, WideTrip, WideW, CheckPoint))
+ continue;
+ if (SE.isKnownPredicateAt(ICmpInst::ICMP_UGT, WideTrip, WideW,
+ CheckPoint)) {
+ RejectReason = "known-unsafe-trip-exceeds-bound";
+ return std::nullopt;
+ }
+
+ if (!SE.isLoopInvariant(ExactTrip, SelectedOuter) ||
+ !SE.isAvailableAtLoopEntry(ExactTrip, SelectedOuter)) {
+ RejectReason = "runtime-trip-not-available-at-preheader";
+ return std::nullopt;
+ }
+ if (!Expander.isSafeToExpandAt(ExactTrip, CheckPoint)) {
+ RejectReason = "runtime-trip-unsafe-to-expand";
+ return std::nullopt;
+ }
+ if (!TTI || Expander.isHighCostExpansion({ExactTrip}, L, /*Budget=*/8, TTI,
+ CheckPoint)) {
+ RejectReason = "runtime-trip-expansion-too-costly";
+ return std::nullopt;
+ }
+ Req.Runtime = true;
+
+ if (!SelectedOuterExactTrip) {
+ if (L == SelectedOuter) {
+ SelectedOuterExactTrip = ExactTrip;
+ } else {
+ const SCEV *SelectedOuterExit = SE.getBackedgeTakenCount(SelectedOuter);
+ if (isa<SCEVCouldNotCompute>(SelectedOuterExit)) {
+ RejectReason = "selected-outer-exact-backedge-not-computable";
+ return std::nullopt;
+ }
+ SelectedOuterExactTrip =
+ SE.getTripCountFromExitCount(SelectedOuterExit);
+ }
+ }
+ if (ExactTrip != SelectedOuterExactTrip) {
+ RejectReason = Req.Kind == BoundRequirementKind::ObjectContainment
+ ? "second-distinct-runtime-trip-object-containment"
+ : "second-distinct-runtime-trip-modular-outer-span";
+ return std::nullopt;
+ }
+
+ if (HasRuntime) {
+ if (RuntimeTrip != ExactTrip) {
+ RejectReason = Req.Kind == BoundRequirementKind::ObjectContainment
+ ? "second-distinct-runtime-trip-object-containment"
+ : "second-distinct-runtime-trip-modular-outer-span";
+ return std::nullopt;
+ }
+ Wmin = APIntOps::umin(Wmin, W);
+ } else {
+ HasRuntime = true;
+ RuntimeTrip = SelectedOuterExactTrip;
+ Wmin = W;
+ }
+ }
+
+ Bound.Requirements.assign(Requirements.begin(), Requirements.end());
+ if (HasRuntime) {
+ Bound.Outcome = PreparedBoundOutcome::RuntimeBound;
+ Bound.RuntimeExactTrip = RuntimeTrip;
+ Bound.Wmin = Wmin;
+ }
+ return Bound;
+}
+
+/// Dependence levels are absolute, including ancestors outside the current
+/// LoopNest root.
+static std::optional<SmallVector<Loop *, 8>>
+buildCandidateAncestorChain(Loop *Outer, Loop *Inner) {
+ SmallVector<Loop *, 8> InnerFirst;
+ for (Loop *L = Inner; L; L = L->getParentLoop())
+ InnerFirst.push_back(L);
+
+ SmallVector<Loop *, 8> Ancestors(InnerFirst.rbegin(), InnerFirst.rend());
+ if (Ancestors.size() < 2 || Ancestors.back() != Inner ||
+ Ancestors[Ancestors.size() - 2] != Outer)
+ return std::nullopt;
+ return Ancestors;
+}
+
+/// Replay plan consistency without analysis queries or IR mutation.
+static bool validatePreparedPlan(const PreparedInterchangePlan &Plan) {
+ const DistributableOuterEpilogue &Epi = Plan.Epilogue;
+ if (!Epi.Outer || !Epi.Inner || !Plan.FissionLegal ||
+ !Plan.FissionContextMatrixComplete || !Plan.FissionContextLegal ||
+ !Plan.RoutingMatrixComplete || !Plan.InterchangeLegal ||
+ !Plan.Profitable || !Plan.Legality)
+ return false;
+
+ if (Plan.AbsoluteAncestors.size() < 2 ||
+ Plan.AbsoluteAncestors.size() != Epi.Inner->getLoopDepth() ||
+ Plan.AbsoluteInnerLoopId != Plan.AbsoluteAncestors.size() - 1 ||
+ Plan.AbsoluteOuterLoopId + 1 != Plan.AbsoluteInnerLoopId ||
+ Plan.AbsoluteOuterLoopId != Epi.Outer->getLoopDepth() - 1 ||
+ Plan.AbsoluteInnerLoopId != Epi.Inner->getLoopDepth() - 1 ||
+ Plan.AbsoluteAncestors[Plan.AbsoluteOuterLoopId] != Epi.Outer ||
+ Plan.AbsoluteAncestors[Plan.AbsoluteInnerLoopId] != Epi.Inner)
+ return false;
+ if (!Plan.AbsoluteAncestors.front() ||
+ Plan.AbsoluteAncestors.front()->getParentLoop())
+ return false;
+ for (auto [Index, L] : enumerate(Plan.AbsoluteAncestors))
+ if (!L || L->getLoopDepth() != Index + 1 ||
+ (Index != 0 && L->getParentLoop() != Plan.AbsoluteAncestors[Index - 1]))
+ return false;
+
+ if (Plan.RoutingChain.size() < 2 ||
+ Plan.RoutingInnerLoopId != Plan.RoutingChain.size() - 1 ||
+ Plan.RoutingOuterLoopId + 1 != Plan.RoutingInnerLoopId ||
+ Plan.RoutingChain[Plan.RoutingOuterLoopId] != Epi.Outer ||
+ Plan.RoutingChain[Plan.RoutingInnerLoopId] != Epi.Inner)
+ return false;
+ for (unsigned I = 1; I < Plan.RoutingChain.size(); ++I)
+ if (!Plan.RoutingChain[I] ||
+ Plan.RoutingChain[I]->getParentLoop() != Plan.RoutingChain[I - 1])
+ return false;
+
+ if (Plan.RoutingChain.size() > Plan.AbsoluteAncestors.size())
+ return false;
+ unsigned SuffixStart =
+ Plan.AbsoluteAncestors.size() - Plan.RoutingChain.size();
+ for (unsigned I = 0; I < Plan.RoutingChain.size(); ++I)
+ if (Plan.RoutingChain[I] != Plan.AbsoluteAncestors[SuffixStart + I])
+ return false;
+
+ if (Epi.Outer->getSubLoops().size() != 1 ||
+ Epi.Outer->getSubLoops().front() != Epi.Inner ||
+ Epi.Inner->getParentLoop() != Epi.Outer || !Epi.Inner->isInnermost())
+ return false;
+
+ if (Plan.FissionContextScanRoot != Epi.Outer ||
+ Plan.FissionContextLevel != 2 ||
+ Plan.FissionContextColumns != DependenceColumns::AbsoluteAncestors ||
+ Plan.RoutingScanRoot != Plan.RoutingChain.front() ||
+ Plan.RoutingLevel != Plan.RoutingChain.size() ||
+ Plan.RoutingColumns != DependenceColumns::SelectedSubnest)
+ return false;
+
+ auto HasWidth = [](const CharMatrix &Matrix, unsigned Width) {
+ return all_of(Matrix, [Width](const std::vector<char> &Row) {
+ return Row.size() == Width + 1 &&
+ (Row.back() == '<' || Row.back() == '*');
+ });
+ };
+ CharMatrix ValidatedRoutingMatrix = Plan.RoutingMatrix;
+ if (!HasWidth(Plan.FissionContextMatrix, Plan.AbsoluteAncestors.size()) ||
+ !HasWidth(Plan.RoutingMatrix, Plan.RoutingChain.size()) ||
+ !hasDecisiveOrEqualAncestorPrefix(Plan.FissionContextMatrix,
+ Plan.AbsoluteOuterLoopId) ||
+ !isLegalToInterChangeLoops(ValidatedRoutingMatrix,
+ Plan.RoutingInnerLoopId,
+ Plan.RoutingOuterLoopId))
+ return false;
+
+ if (Epi.Instructions.empty() || Epi.MemoryInstructions.empty() ||
+ Epi.InstructionSet.size() != Epi.Instructions.size())
+ return false;
+ for (Instruction *I : Epi.Instructions)
+ if (!I || !Epi.InstructionSet.contains(I) ||
+ !isSupportedEpilogueInstruction(*I))
+ return false;
+
+ BasicBlock *OuterLatch = Epi.Outer->getLoopLatch();
+ BasicBlock *InnerLatch = Epi.Inner->getLoopLatch();
+ BasicBlock *Current = Epi.Inner->getUniqueExitBlock();
+ BasicBlock *OuterPreheader = Epi.Outer->getLoopPreheader();
+ BasicBlock *Previous = InnerLatch;
+ if (!OuterLatch || !InnerLatch || !Current || !OuterPreheader)
+ return false;
+ for (BasicBlock *BB : Epi.Path) {
+ if (!BB || BB != Current || BB == OuterLatch ||
+ BB->getUniquePredecessor() != Previous)
+ return false;
+ auto *Branch = dyn_cast<UncondBrInst>(BB->getTerminator());
+ if (!Branch || BB->getUniqueSuccessor() != Branch->getSuccessor(0))
+ return false;
+ Previous = BB;
+ Current = Branch->getSuccessor(0);
+ }
+ if (Current != OuterLatch || OuterLatch->getUniquePredecessor() != Previous)
+ return false;
+
+ SmallVector<Instruction *, 16> ExpectedInstructions;
+ SmallVector<Instruction *, 8> ExpectedMemory;
+ for (BasicBlock *BB : Epi.Path)
+ for (Instruction &I : *BB) {
+ if (isa<PHINode>(I) || I.isTerminator())
+ continue;
+ ExpectedInstructions.push_back(&I);
+ if (isa<LoadInst, StoreInst>(I))
+ ExpectedMemory.push_back(&I);
+ }
+ for (Instruction &I : *OuterLatch) {
+ if (&I == Epi.OuterControl.NextValue || &I == Epi.OuterControl.LatchCompare)
+ break;
+ if (isa<PHINode>(I) || I.isTerminator())
+ continue;
+ ExpectedInstructions.push_back(&I);
+ if (isa<LoadInst, StoreInst>(I))
+ ExpectedMemory.push_back(&I);
+ }
+ if (ExpectedInstructions != Epi.Instructions ||
+ ExpectedMemory != Epi.MemoryInstructions)
+ return false;
+
+ for (Instruction *I : Epi.Instructions)
+ for (User *U : I->users()) {
+ if (auto *UserI = dyn_cast<Instruction>(U);
+ UserI && Epi.InstructionSet.contains(UserI))
+ continue;
+ if (!U->isDroppable())
+ return false;
+ }
+
+ SmallVector<Instruction *, 8> ExpectedRematerialized;
+ EpilogueOperandClassifier Classifier(
+ Epi.Outer, Epi.Inner, Epi.OuterControl.Induction, Epi.InstructionSet,
+ ExpectedRematerialized);
+ for (Instruction *I : Epi.Instructions)
+ for (Value *Operand : I->operands()) {
+ if (auto *OperandI = dyn_cast<Instruction>(Operand);
+ OperandI && Epi.InstructionSet.contains(OperandI))
+ continue;
+ if (Classifier.classify(Operand) == EpilogueOperandClass::Invalid)
+ return false;
+ }
+ if (ExpectedRematerialized != Epi.OuterIVDerivedInstructions)
+ return false;
+
+ for (Instruction *I : Epi.MemoryInstructions)
+ if (!I || !Epi.InstructionSet.contains(I) || !isa<LoadInst, StoreInst>(I))
+ return false;
+ // Preparation proved this same Outer-minus-E region admissible, so replay
+ // that one shared predicate here rather than trusting the earlier result.
+ if (hasConvergentOperationInRetainedNest(Epi))
+ return false;
+
+ SmallVector<Instruction *, 16> ExpectedNestMemory;
+ for (BasicBlock *BB : Epi.Outer->blocks())
+ for (Instruction &I : *BB)
+ if (isa<LoadInst, StoreInst>(I) && !Epi.InstructionSet.contains(&I))
+ ExpectedNestMemory.push_back(&I);
+ if (ExpectedNestMemory != Plan.NestMemoryInstructions)
+ return false;
+ for (Instruction *I : Plan.NestMemoryInstructions)
+ if (!I || Epi.InstructionSet.contains(I) ||
+ !Epi.Outer->contains(I->getParent()) || !isa<LoadInst, StoreInst>(I))
+ return false;
-/// Manages information utilized by the profitability check for cache. The main
-/// purpose of this class is to delay the computation of CacheCost until it is
-/// actually needed.
-class CacheCostManager {
- Loop *OutermostLoop;
- LoopStandardAnalysisResults *AR;
- DependenceInfo *DI;
+ const PreparedOuterIVControl &Control = Epi.OuterControl;
+ if (!Control.Induction || !Control.InitialValue || !Control.NextValue ||
+ !Control.LatchCompare || !Control.LatchBranch ||
+ Control.LatchInstructions.size() != 2 ||
+ Control.LatchInstructions[0] != Control.NextValue ||
+ Control.LatchInstructions[1] != Control.LatchCompare ||
+ Control.Induction->getParent() != Epi.Outer->getHeader() ||
+ Control.NextValue->getParent() != OuterLatch ||
+ Control.LatchCompare->getParent() != OuterLatch ||
+ Control.LatchBranch != OuterLatch->getTerminator() ||
+ Control.InitialValue !=
+ Control.Induction->getIncomingValueForBlock(OuterPreheader) ||
+ Control.NextValue !=
+ Control.Induction->getIncomingValueForBlock(OuterLatch))
+ return false;
- /// CacheCost for \ref OutermostLoop. Once it is computed, it is cached. Note
- /// that the result can be nullptr.
- std::optional<std::unique_ptr<CacheCost>> CC;
+ SmallSet<unsigned, 8> RequirementIds;
+ for (const PreparedCrossPartitionDependence &Dep : Plan.CrossDependences) {
+ if (!Dep.UsedByteOffsetProof)
+ continue;
+ if (!RequirementIds.insert(Dep.RequirementId).second)
+ return false;
+ bool HasOuter = false;
+ bool HasContainment = false;
+ unsigned MatchingRequirements = 0;
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements) {
+ if (Req.RequirementId != Dep.RequirementId)
+ continue;
+ ++MatchingRequirements;
+ HasOuter |= Req.Kind == BoundRequirementKind::ModularOuterSpan;
+ HasContainment |= Req.Kind == BoundRequirementKind::ObjectContainment;
+ }
+ if (MatchingRequirements != 2 || !HasOuter || !HasContainment)
+ return false;
+ }
- /// Maps each loop to an index representing the optimal position within the
- /// loop-nest, as determined by the cache cost analysis.
- DenseMap<const Loop *, unsigned> CostMap;
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements) {
+ if (!Req.DomainLoop || Req.Limit.isZero() || Req.Limit.getBitWidth() != 64)
+ return false;
+ if (Req.Kind == BoundRequirementKind::ModularOuterSpan) {
+ if (Req.DomainLoop != Epi.Outer)
+ return false;
+ } else if (Req.DomainLoop != Epi.Inner) {
+ return false;
+ }
+ if (none_of(Plan.CrossDependences,
+ [&](const PreparedCrossPartitionDependence &Dep) {
+ return Dep.UsedByteOffsetProof &&
+ Dep.RequirementId == Req.RequirementId;
+ }))
+ return false;
+ }
- void computeIfUnitinialized();
+ if (Plan.TripBound.Outcome == PreparedBoundOutcome::RuntimeBound) {
+ if (!Plan.TripBound.RuntimeExactTrip || Plan.TripBound.Wmin.isZero())
+ return false;
+ bool SawRuntime = false;
+ APInt ExpectedWmin;
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements) {
+ if (!Req.Runtime)
+ continue;
+ if (!Req.ExactTrip || Req.ExactTrip != Plan.TripBound.RuntimeExactTrip)
+ return false;
+ if (SawRuntime)
+ ExpectedWmin = APIntOps::umin(ExpectedWmin, Req.Limit);
+ else
+ ExpectedWmin = Req.Limit;
+ SawRuntime = true;
+ }
+ if (!SawRuntime || ExpectedWmin != Plan.TripBound.Wmin)
+ return false;
+ } else {
+ if (Plan.TripBound.RuntimeExactTrip ||
+ any_of(Plan.TripBound.Requirements,
+ [](const PreparedBoundRequirement &Req) { return Req.Runtime; }))
+ return false;
+ }
-public:
- CacheCostManager(Loop *OutermostLoop, LoopStandardAnalysisResults *AR,
- DependenceInfo *DI)
- : OutermostLoop(OutermostLoop), AR(AR), DI(DI) {}
- CacheCost *getCacheCost();
- const DenseMap<const Loop *, unsigned> &getCostMap();
-};
+ ArrayRef<Instruction *> LegalDropNoWrap =
+ Plan.Legality->getHasNoWrapReductions();
+ ArrayRef<Instruction *> LegalDropNoInf = Plan.Legality->getHasNoInfInsts();
+ if (!Plan.Legality->isPreparedFor(Epi.Outer, Epi.Inner, Epi.InstructionSet) ||
+ Plan.DropNoWrap.size() != LegalDropNoWrap.size() ||
+ Plan.DropNoInf.size() != LegalDropNoInf.size() ||
+ !std::equal(Plan.DropNoWrap.begin(), Plan.DropNoWrap.end(),
+ LegalDropNoWrap.begin()) ||
+ !std::equal(Plan.DropNoInf.begin(), Plan.DropNoInf.end(),
+ LegalDropNoInf.begin()))
+ return false;
-/// LoopInterchangeProfitability checks if it is profitable to interchange the
-/// loop.
-class LoopInterchangeProfitability {
-public:
- LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
- OptimizationRemarkEmitter *ORE)
- : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {}
+ return true;
+}
- /// Check if the loop interchange is profitable.
- bool isProfitable(const Loop *InnerLoop, const Loop *OuterLoop,
- unsigned InnerLoopId, unsigned OuterLoopId,
- CharMatrix &DepMatrix, CacheCostManager &CCM);
+static void printPreparedPlanDiagnostic(const PreparedInterchangePlan &Plan) {
+ if (!PrintPreparedEpiloguePlans)
+ return;
+ const DistributableOuterEpilogue &Epi = Plan.Epilogue;
+ Loop *Outer = Epi.Outer;
+ Loop *Inner = Epi.Inner;
+ assert(Plan.Legality && "a printed plan carries interchange legality");
+ unsigned ByteOffsetProofs = count_if(
+ Plan.CrossDependences, [](const PreparedCrossPartitionDependence &Dep) {
+ return Dep.UsedByteOffsetProof;
+ });
-private:
- int getInstrOrderCost();
- std::optional<bool> isProfitablePerLoopCacheAnalysis(
- const DenseMap<const Loop *, unsigned> &CostMap, CacheCost *CC);
- std::optional<bool> isProfitablePerInstrOrderCost();
- std::optional<bool> isProfitableForVectorization(unsigned InnerLoopId,
- unsigned OuterLoopId,
- CharMatrix &DepMatrix);
- Loop *OuterLoop;
- Loop *InnerLoop;
+ bool Static = Plan.TripBound.Outcome == PreparedBoundOutcome::StaticBound;
+ APInt MaxLimit(64, 0);
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements)
+ if (Req.Limit.ugt(MaxLimit))
+ MaxLimit = Req.Limit;
+
+ errs() << "loop-interchange: prepared function="
+ << Outer->getHeader()->getParent()->getName()
+ << " outer=" << Outer->getName() << " inner=" << Inner->getName()
+ << " path-blocks=" << Epi.Path.size()
+ << " epilogue-insts=" << Epi.Instructions.size()
+ << " rematerialized=" << Epi.OuterIVDerivedInstructions.size()
+ << " absolute-depth=" << Plan.AbsoluteAncestors.size()
+ << " routing-depth=" << Plan.RoutingChain.size()
+ << " fission-rows=" << Plan.FissionContextMatrix.size()
+ << " routing-rows=" << Plan.RoutingMatrix.size()
+ << " cross-deps=" << Plan.CrossDependences.size()
+ << " reductions=" << Plan.Legality->getOuterInnerReductions().size()
+ << " byte-offset-proofs=" << ByteOffsetProofs
+ << " requirements=" << Plan.TripBound.Requirements.size()
+ << " bound=" << (Static ? "static-bound" : "runtime-bound");
+ if (Static && Plan.TripBound.Requirements.empty())
+ errs() << ", no-bound-requirement";
+ else if (Static)
+ errs() << " W=" << MaxLimit.getZExtValue();
+ else
+ errs() << " Wmin=" << Plan.TripBound.Wmin.getZExtValue();
+ errs() << " requirement-ids=";
+ bool First = true;
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements) {
+ if (!First)
+ errs() << ",";
+ First = false;
+ errs() << Req.RequirementId << ":"
+ << (Req.Kind == BoundRequirementKind::ModularOuterSpan
+ ? "modular-outer-span"
+ : "object-containment")
+ << "/" << (Req.Runtime ? "runtime" : "static") << "/"
+ << (Req.Runtime ? "runtime"
+ : Req.ExactTrip ? "by-context-exact"
+ : "by-constant-max");
+ }
+ errs() << "\n";
+}
- /// Scev analysis.
- ScalarEvolution *SE;
+static std::optional<PreparedInterchangePlan>
+prepareInterchangeWithOuterEpilogue(ArrayRef<Loop *> RoutingChain,
+ ScalarEvolution *SE, LoopInfo *LI,
+ DependenceInfo *DI, DominatorTree *DT,
+ LoopStandardAnalysisResults *AR,
+ OptimizationRemarkEmitter *ORE) {
+ (void)LI;
+ if (RoutingChain.size() < 2)
+ return std::nullopt;
+ Loop *Outer = RoutingChain[RoutingChain.size() - 2];
+ Loop *Inner = RoutingChain.back();
- /// Interface to emit optimization remarks.
- OptimizationRemarkEmitter *ORE;
-};
+ std::optional<DistributableOuterEpilogue> Discovered =
+ discoverDistributableOuterEpilogue(Outer, Inner, SE, DT);
+ if (!Discovered)
+ return std::nullopt;
-/// LoopInterchangeTransform interchanges the loop.
-class LoopInterchangeTransform {
-public:
- LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
- LoopInfo *LI, DominatorTree *DT,
- const LoopInterchangeLegality &LIL)
- : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT), LIL(LIL) {}
+ std::optional<SmallVector<Loop *, 8>> AbsoluteAncestors =
+ buildCandidateAncestorChain(Outer, Inner);
+ if (!AbsoluteAncestors) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "true ancestor-chain context is unavailable");
+ return std::nullopt;
+ }
- /// Interchange OuterLoop and InnerLoop.
- void transform(ArrayRef<Instruction *> DropNoWrapInsts,
- ArrayRef<Instruction *> DropNoInfInsts);
- void reduction2Memory();
- void restructureLoops(Loop *NewInner, Loop *NewOuter,
- BasicBlock *OrigInnerPreHeader,
- BasicBlock *OrigOuterPreHeader);
- void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop);
+ PreparedInterchangePlan Plan;
+ Plan.Epilogue = std::move(*Discovered);
+ Plan.RoutingChain.assign(RoutingChain.begin(), RoutingChain.end());
+ Plan.AbsoluteAncestors = std::move(*AbsoluteAncestors);
+ Plan.AbsoluteInnerLoopId =
+ static_cast<unsigned>(Plan.AbsoluteAncestors.size() - 1);
+ Plan.AbsoluteOuterLoopId = Plan.AbsoluteInnerLoopId - 1;
+ Plan.RoutingInnerLoopId = static_cast<unsigned>(Plan.RoutingChain.size() - 1);
+ Plan.RoutingOuterLoopId = Plan.RoutingInnerLoopId - 1;
+
+ if (!collectAndCheckPreparationMemory(Plan.Epilogue,
+ Plan.NestMemoryInstructions)) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "union N/E memory budget or simple-access check "
+ "failed");
+ return std::nullopt;
+ }
-private:
- void adjustLoopBranches();
+ if (hasConvergentOperationInRetainedNest(Plan.Epilogue)) {
+ rejectPreparedEpilogue(
+ ORE, Outer, Inner,
+ "retained nest contains an unsupported convergent operation");
+ return std::nullopt;
+ }
- Loop *OuterLoop;
- Loop *InnerLoop;
+ SmallVector<PreparedBoundRequirement, 4> Requirements;
+ if (!proveNestToEpilogueDependences(Plan.NestMemoryInstructions,
+ Plan.Epilogue.MemoryInstructions, Outer,
+ Inner, Plan.AbsoluteOuterLoopId, DI, SE,
+ Plan.CrossDependences, Requirements)) {
+ rejectPreparedEpilogue(
+ ORE, Outer, Inner,
+ "cross-partition dependence is not statically proved N-to-E");
+ return std::nullopt;
+ }
+ Plan.FissionLegal = true;
- /// Scev analysis.
- ScalarEvolution *SE;
+ BasicBlock *Preheader = Outer->getLoopPreheader();
+ Instruction *CheckPoint = Preheader ? Preheader->getTerminator() : nullptr;
+ if (!CheckPoint) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "selected outer loop has no preheader terminator");
+ return std::nullopt;
+ }
+ const char *BoundRejectReason = "array-bound-requirement-unresolved";
+ std::optional<PreparedTripBound> TripBound = resolvePreparedTripBound(
+ Requirements, Outer, *SE, &AR->TTI, CheckPoint, BoundRejectReason);
+ if (!TripBound) {
+ rejectPreparedEpilogue(ORE, Outer, Inner, BoundRejectReason);
+ return std::nullopt;
+ }
+ Plan.TripBound = std::move(*TripBound);
+
+ ExtractedEpilogueView View(&Plan.Epilogue.InstructionSet);
+
+ // Fission uses only the selected Outer's memory and absolute ancestry.
+ Plan.FissionContextScanRoot = Outer;
+ Plan.FissionContextLevel = 2;
+ Plan.FissionContextColumns = DependenceColumns::AbsoluteAncestors;
+ if (!populateDependencyMatrix(
+ Plan.FissionContextMatrix, Plan.FissionContextLevel,
+ Plan.FissionContextScanRoot, DI, SE,
+ /*ORE=*/nullptr, Plan.FissionContextColumns, View)) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "fission-context dependence matrix is unavailable");
+ return std::nullopt;
+ }
+ Plan.FissionContextMatrixComplete = true;
+ if (!hasDecisiveOrEqualAncestorPrefix(Plan.FissionContextMatrix,
+ Plan.AbsoluteOuterLoopId)) {
+ rejectPreparedEpilogue(
+ ORE, Outer, Inner,
+ "virtual extracted nest has unknown surrounding dependence context");
+ return std::nullopt;
+ }
+ Plan.FissionContextLegal = true;
+
+ // Ordinary legality and profitability need the collected chain's row domain
+ // and suffix projection, not a projection of the fission matrix.
+ Plan.RoutingScanRoot = Plan.RoutingChain.front();
+ Plan.RoutingLevel = static_cast<unsigned>(Plan.RoutingChain.size());
+ Plan.RoutingColumns = DependenceColumns::SelectedSubnest;
+ if (!populateDependencyMatrix(Plan.RoutingMatrix, Plan.RoutingLevel,
+ Plan.RoutingScanRoot, DI, SE,
+ /*ORE=*/nullptr, Plan.RoutingColumns, View)) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "routing dependence matrix is unavailable");
+ return std::nullopt;
+ }
+ Plan.RoutingMatrixComplete = true;
+
+ Plan.Legality = std::make_unique<LoopInterchangeLegality>(
+ Outer, Inner, SE, /*ORE=*/nullptr, DT, View);
+ if (!Plan.Legality->canInterchangeLoops(Plan.RoutingInnerLoopId,
+ Plan.RoutingOuterLoopId,
+ Plan.RoutingMatrix)) {
+ rejectPreparedEpilogue(
+ ORE, Outer, Inner,
+ "virtual extracted nest failed existing interchange legality");
+ return std::nullopt;
+ }
+ Plan.InterchangeLegal = true;
+
+ CacheCostManager CCM(Plan.RoutingChain.front(), AR, DI);
+ LoopInterchangeProfitability Profitability(Outer, Inner, SE, /*ORE=*/nullptr);
+ if (!Profitability.isProfitable(Inner, Outer, Plan.RoutingInnerLoopId,
+ Plan.RoutingOuterLoopId, Plan.RoutingMatrix,
+ CCM)) {
+ rejectPreparedEpilogue(
+ ORE, Outer, Inner,
+ "virtual extracted nest failed existing default profitability");
+ return std::nullopt;
+ }
+ Plan.Profitable = true;
- LoopInfo *LI;
- DominatorTree *DT;
+ ArrayRef<Instruction *> DropNoWrap = Plan.Legality->getHasNoWrapReductions();
+ Plan.DropNoWrap.append(DropNoWrap.begin(), DropNoWrap.end());
+ ArrayRef<Instruction *> DropNoInf = Plan.Legality->getHasNoInfInsts();
+ Plan.DropNoInf.append(DropNoInf.begin(), DropNoInf.end());
- const LoopInterchangeLegality &LIL;
-};
+ if (!validatePreparedPlan(Plan)) {
+ rejectPreparedEpilogue(ORE, Outer, Inner,
+ "prepared plan failed pure re-validation");
+ return std::nullopt;
+ }
+
+ LLVM_DEBUG(dbgs() << "loop-interchange: prepared a complete outer-epilogue "
+ "fission plan in function '"
+ << Outer->getHeader()->getParent()->getName()
+ << "': Outer '" << Outer->getName() << "', Inner '"
+ << Inner->getName() << "', absolute columns "
+ << Plan.AbsoluteOuterLoopId << "/"
+ << Plan.AbsoluteInnerLoopId << ", routing columns "
+ << Plan.RoutingOuterLoopId << "/" << Plan.RoutingInnerLoopId
+ << ", " << Plan.CrossDependences.size()
+ << " cross dependence(s).\n");
+ return std::optional<PreparedInterchangePlan>(std::move(Plan));
+}
struct LoopInterchange {
ScalarEvolution *SE = nullptr;
@@ -719,12 +2512,90 @@ struct LoopInterchange {
return LoopLists;
}
+ bool isFissionEligibleChain(ArrayRef<Loop *> Chain) {
+ if (Chain.size() < 2 || !hasSupportedLoopDepth(Chain) ||
+ !isComputableLoopNest(SE, Chain))
+ return false;
+
+ Loop *Outer = Chain[Chain.size() - 2];
+ Loop *Inner = Chain.back();
+ Loop *Pair[] = {Outer, Inner};
+ if (!hasSupportedLoopDepth(ArrayRef<Loop *>(Pair)))
+ return false;
+
+ return Outer->getSubLoops().size() == 1 &&
+ Outer->getSubLoops().front() == Inner &&
+ Inner->getParentLoop() == Outer && Inner->isInnermost();
+ }
+
+ /// Ineligible chains consume no slot. Rejections and runtime bounds consume
+ /// one attempt each, but neither suppresses a later static plan.
+ std::optional<PreparedInterchangePlan>
+ tryPrepareOuterEpilogueFission(ArrayRef<SmallVector<Loop *, 8>> LoopLists) {
+ const unsigned Budget = MaxOuterEpilogueFissionCandidates;
+ if (Budget == 0)
+ return std::nullopt;
+
+ unsigned Attempts = 0;
+ std::optional<PreparedInterchangePlan> FirstRuntime;
+ for (const SmallVector<Loop *, 8> &CollectedChain : LoopLists) {
+ ArrayRef<Loop *> LoopList = CollectedChain;
+ if (!isFissionEligibleChain(LoopList))
+ continue;
+ if (Attempts == Budget)
+ break;
+ ++Attempts;
+
+ std::optional<PreparedInterchangePlan> Plan =
+ prepareInterchangeWithOuterEpilogue(LoopList, SE, LI, DI, DT, AR,
+ ORE);
+ if (!Plan)
+ continue;
+ if (Plan->TripBound.Outcome == PreparedBoundOutcome::StaticBound) {
+ if (!validatePreparedPlan(*Plan)) {
+ rejectPreparedEpilogue(
+ ORE, Plan->Epilogue.Outer, Plan->Epilogue.Inner,
+ "prepared plan failed pure re-validation before selection");
+ continue;
+ }
+ printPreparedPlanDiagnostic(*Plan);
+ return Plan;
+ }
+ if (!FirstRuntime)
+ FirstRuntime = std::move(Plan);
+ }
+
+ if (FirstRuntime) {
+ if (!validatePreparedPlan(*FirstRuntime)) {
+ rejectPreparedEpilogue(
+ ORE, FirstRuntime->Epilogue.Outer, FirstRuntime->Epilogue.Inner,
+ "prepared plan failed pure re-validation before selection");
+ return std::nullopt;
+ }
+ printPreparedPlanDiagnostic(*FirstRuntime);
+ rejectPreparedEpilogue(
+ ORE, FirstRuntime->Epilogue.Outer, FirstRuntime->Epilogue.Inner,
+ "recovered selected-outer array bound is a runtime value; safe "
+ "distribution requires a runtime bound check");
+ }
+ return FirstRuntime;
+ }
+
bool run(LoopNest &LN) {
SmallVector<SmallVector<Loop *, 8>, 4> LoopLists = collectPerfectNests(LN);
if (LoopLists.empty()) {
LLVM_DEBUG(dbgs() << "No Valid candidates for loop interchange.\n");
return false;
}
+
+ if (EnableOuterEpilogueFission && !LN.getParent()->hasOptSize()) {
+ std::optional<PreparedInterchangePlan> Prepared =
+ tryPrepareOuterEpilogueFission(LoopLists);
+ // Preparation is analysis-only. Destroy every plan before ordinary
+ // routing can mutate the collected chains.
+ (void)Prepared;
+ }
+
bool Changed = false;
for (SmallVector<Loop *, 8> &LoopList : LoopLists) {
// Ensure minimum depth of the loop nest to do the interchange.
@@ -864,8 +2735,8 @@ struct LoopInterchange {
bool LoopInterchangeLegality::containsUnsafeInstructions(BasicBlock *BB,
Instruction *Skip) {
- return any_of(*BB, [Skip](const Instruction &I) {
- if (&I == Skip)
+ return any_of(*BB, [this, Skip](const Instruction &I) {
+ if (&I == Skip || isVirtuallyExtracted(&I))
return false;
return I.mayHaveSideEffects() || I.mayReadFromMemory();
});
@@ -987,8 +2858,8 @@ bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) {
BasicBlock *InnerLoopExit = InnerLoop->getExitBlock();
// Ensure the inner loop exit block flows to the outer loop latch possibly
// through empty blocks.
- const BasicBlock &SuccInner =
- LoopNest::skipEmptyBlockUntil(InnerLoopExit, OuterLoopLatch);
+ const BasicBlock &SuccInner = skipVirtuallyEmptyBlockUntil(
+ InnerLoopExit, OuterLoopLatch, getExtractedEpilogueView());
if (&SuccInner != OuterLoopLatch) {
LLVM_DEBUG(dbgs() << "Inner loop exit block " << *InnerLoopExit
<< " does not lead to the outer loop latch.\n";);
@@ -1243,11 +3114,12 @@ bool LoopInterchangeLegality::isInnerReduction(
// the innermost two loops.
if (!L->isInnermost()) {
LLVM_DEBUG(dbgs() << "Only supported when the loop is the innermost.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
- L->getStartLoc(), L->getHeader())
- << "Only supported when the loop is the innermost.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
+ L->getStartLoc(), L->getHeader())
+ << "Only supported when the loop is the innermost.";
+ });
return false;
}
@@ -1262,12 +3134,13 @@ bool LoopInterchangeLegality::isInnerReduction(
LLVM_DEBUG(
dbgs()
<< "Only supported for the reduction with a constant initial value.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
- L->getStartLoc(), L->getHeader())
- << "Only supported for the reduction with a constant initial "
- "value.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
+ L->getStartLoc(), L->getHeader())
+ << "Only supported for the reduction with a constant initial "
+ "value.";
+ });
return false;
}
@@ -1311,12 +3184,13 @@ bool LoopInterchangeLegality::isInnerReduction(
if (!Lcssa->hasOneUser()) {
LLVM_DEBUG(dbgs() << "Only supported when the reduction is used once in "
"the outer loop.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
- L->getStartLoc(), L->getHeader())
- << "Only supported when the reduction is used once in the outer "
- "loop.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
+ L->getStartLoc(), L->getHeader())
+ << "Only supported when the reduction is used once in the outer "
+ "loop.";
+ });
return false;
}
@@ -1337,12 +3211,13 @@ bool LoopInterchangeLegality::isInnerReduction(
if (!DT->dominates(dyn_cast<Instruction>(MemRef), L->getHeader())) {
LLVM_DEBUG(dbgs() << "Only supported when memory reference dominate "
"the inner loop.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
- L->getStartLoc(), L->getHeader())
- << "Only supported when memory reference dominate the inner "
- "loop.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
+ L->getStartLoc(), L->getHeader())
+ << "Only supported when memory reference dominate the inner "
+ "loop.";
+ });
return false;
}
@@ -1417,13 +3292,15 @@ bool LoopInterchangeLegality::checkInductionsAndReductions(Loop *OuterLoop) {
LLVM_DEBUG(
dbgs()
<< "Failed to recognize PHI as an induction or reduction.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIOuter",
- OuterLoop->getStartLoc(),
- OuterLoop->getHeader())
- << "Only outer loops with induction or reduction PHI nodes "
- "can be interchanged currently.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIOuter",
+ OuterLoop->getStartLoc(),
+ OuterLoop->getHeader())
+ << "Only outer loops with induction or reduction PHI "
+ "nodes "
+ "can be interchanged currently.";
+ });
return false;
}
@@ -1437,13 +3314,15 @@ bool LoopInterchangeLegality::checkInductionsAndReductions(Loop *OuterLoop) {
LLVM_DEBUG(dbgs() << "Found a reduction in the inner loop: \n"
<< PHI << '\n');
} else {
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIInner",
- CurLoop->getStartLoc(),
- CurLoop->getHeader())
- << "Only inner loops with induction or reduction PHI nodes "
- "can be interchanged currently.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIInner",
+ CurLoop->getStartLoc(),
+ CurLoop->getHeader())
+ << "Only inner loops with induction or reduction PHI "
+ "nodes "
+ "can be interchanged currently.";
+ });
return false;
}
}
@@ -1452,12 +3331,13 @@ bool LoopInterchangeLegality::checkInductionsAndReductions(Loop *OuterLoop) {
// For now we only support at most one reduction.
if (InnerReductions.size() > 1) {
LLVM_DEBUG(dbgs() << "Only supports at most one reduction.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerReduction",
- CurLoop->getStartLoc(),
- CurLoop->getHeader())
- << "Only supports at most one reduction.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(
+ DEBUG_TYPE, "UnsupportedInnerReduction",
+ CurLoop->getStartLoc(), CurLoop->getHeader())
+ << "Only supports at most one reduction.";
+ });
return false;
}
}
@@ -1479,25 +3359,27 @@ bool LoopInterchangeLegality::currentLimitations() {
LLVM_DEBUG(
dbgs() << "Loops where the latch is not the exiting block are not"
<< " supported currently.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "ExitingNotLatch",
- OuterLoop->getStartLoc(),
- OuterLoop->getHeader())
- << "Loops where the latch is not the exiting block cannot be"
- " interchange currently.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "ExitingNotLatch",
+ OuterLoop->getStartLoc(),
+ OuterLoop->getHeader())
+ << "Loops where the latch is not the exiting block cannot be"
+ " interchange currently.";
+ });
return true;
}
// TODO: Triangular loops are not handled for now.
if (!isLoopStructureUnderstood()) {
LLVM_DEBUG(dbgs() << "Loop structure not understood by pass\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedStructureInner",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Inner loop structure not understood currently.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedStructureInner",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Inner loop structure not understood currently.";
+ });
return true;
}
@@ -1511,11 +3393,13 @@ bool LoopInterchangeLegality::currentLimitations() {
if (isa<IndirectBrInst>(Pred->getTerminator())) {
LLVM_DEBUG(
dbgs() << "Indirect branch found in the loop predecessor.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "IndirectBranchPreheader",
- L->getStartLoc(), L->getHeader())
- << "Indirect branch found in the loop predecessor.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE,
+ "IndirectBranchPreheader",
+ L->getStartLoc(), L->getHeader())
+ << "Indirect branch found in the loop predecessor.";
+ });
return true;
}
}
@@ -1651,17 +3535,20 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
LLVM_DEBUG(dbgs() << "Failed interchange InnerLoopId = " << InnerLoopId
<< " and OuterLoopId = " << OuterLoopId
<< " due to dependence\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Cannot interchange loops due to dependences.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Cannot interchange loops due to dependences.";
+ });
return false;
}
// Check if outer and inner loop contain legal instructions only.
for (auto *BB : OuterLoop->blocks())
for (Instruction &I : *BB) {
+ if (isVirtuallyExtracted(&I))
+ continue;
// Loads and stores are checked separately, so we can skip them here.
if (isa<LoadInst, StoreInst, PseudoProbeInst>(&I))
continue;
@@ -1674,12 +3561,13 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
LLVM_DEBUG(
dbgs()
<< "Loops contain instructions that cannot be safely interchanged\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsafeInst",
- I.getDebugLoc(), I.getParent())
- << "Cannot interchange loops due to instruction that is "
- "potentially unsafe to interchange.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsafeInst",
+ I.getDebugLoc(), I.getParent())
+ << "Cannot interchange loops due to instruction that is "
+ "potentially unsafe to interchange.";
+ });
return false;
}
@@ -1692,13 +3580,15 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
if (!areInnerLoopLatchPHIsSupported(InnerLoop, InnerLoopInductions)) {
LLVM_DEBUG(dbgs() << "Found unsupported PHI nodes in inner loop latch.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerLatchPHI",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Cannot interchange loops because unsupported PHI nodes found "
- "in inner loop latch.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedInnerLatchPHI",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Cannot interchange loops because unsupported PHI nodes "
+ "found "
+ "in inner loop latch.";
+ });
return false;
}
@@ -1707,13 +3597,14 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
Freeze = findFreezeInInnerLatchCloneSet(InnerLoop, InnerLoopInductions);
if (Freeze) {
LLVM_DEBUG(dbgs() << "Interchange would re-nest or duplicate freeze\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsafeInst",
- Freeze->getDebugLoc(),
- Freeze->getParent())
- << "Cannot interchange loops because re-nesting or duplicating "
- "freeze may change its sampling behavior.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsafeInst",
+ Freeze->getDebugLoc(),
+ Freeze->getParent())
+ << "Cannot interchange loops because re-nesting or duplicating "
+ "freeze may change its sampling behavior.";
+ });
return false;
}
@@ -1727,13 +3618,14 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
// Check if the loops are tightly nested.
if (!tightlyNested(OuterLoop, InnerLoop)) {
LLVM_DEBUG(dbgs() << "Loops not tightly nested\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "NotTightlyNested",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Cannot interchange loops because they are not tightly "
- "nested.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "NotTightlyNested",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Cannot interchange loops because they are not tightly "
+ "nested.";
+ });
return false;
}
@@ -1748,23 +3640,25 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
if (!areInnerLoopExitPHIsSupported(OuterLoop, InnerLoop, OuterInnerReductions,
LcssaReduction)) {
LLVM_DEBUG(dbgs() << "Found unsupported PHI nodes in inner loop exit.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedExitPHI",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Found unsupported PHI node in loop exit.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedExitPHI",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Found unsupported PHI node in loop exit.";
+ });
return false;
}
if (!areOuterLoopExitPHIsSupported(OuterLoop, InnerLoop)) {
LLVM_DEBUG(dbgs() << "Found unsupported PHI nodes in outer loop exit.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedExitPHI",
- OuterLoop->getStartLoc(),
- OuterLoop->getHeader())
- << "Found unsupported PHI node in loop exit.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedExitPHI",
+ OuterLoop->getStartLoc(),
+ OuterLoop->getHeader())
+ << "Found unsupported PHI node in loop exit.";
+ });
return false;
}
@@ -1772,13 +3666,14 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
[](PHINode &PHI) { return PHI.getNumIncomingValues() != 1; })) {
LLVM_DEBUG(dbgs() << "Only outer loop latch PHI nodes with one incoming "
"value are supported.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLatchPHI",
- OuterLoop->getStartLoc(),
- OuterLoop->getHeader())
- << "Only outer loop latch PHI nodes with one incoming value are "
- "supported.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLatchPHI",
+ OuterLoop->getStartLoc(),
+ OuterLoop->getHeader())
+ << "Only outer loop latch PHI nodes with one incoming value are "
+ "supported.";
+ });
return false;
}
@@ -1792,13 +3687,15 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
for (PHINode &PHI : OuterLoop->getLoopLatch()->phis())
if (any_of(PHI.users(), [](const User *U) { return !isa<PHINode>(U); })) {
LLVM_DEBUG(dbgs() << "Outer loop latch PHI has a non-PHI user.\n");
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLatchPHI",
- OuterLoop->getStartLoc(),
- OuterLoop->getHeader())
- << "Cannot interchange loops because an outer loop latch PHI "
- "node has a non-PHI user.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLatchPHI",
+ OuterLoop->getStartLoc(),
+ OuterLoop->getHeader())
+ << "Cannot interchange loops because an outer loop latch "
+ "PHI "
+ "node has a non-PHI user.";
+ });
return false;
}
@@ -2066,22 +3963,24 @@ bool LoopInterchangeProfitability::isProfitable(
}
if (!shouldInterchange.has_value()) {
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Insufficient information to calculate the cost of loop for "
- "interchange.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Insufficient information to calculate the cost of loop for "
+ "interchange.";
+ });
return false;
} else if (!shouldInterchange.value()) {
- ORE->emit([&]() {
- return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable",
- InnerLoop->getStartLoc(),
- InnerLoop->getHeader())
- << "Interchanging loops is not considered to improve cache "
- "locality nor vectorization.";
- });
+ if (ORE)
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable",
+ InnerLoop->getStartLoc(),
+ InnerLoop->getHeader())
+ << "Interchanging loops is not considered to improve cache "
+ "locality nor vectorization.";
+ });
return false;
}
return true;
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
new file mode 100644
index 00000000000000..08feb5b31a8224
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
@@ -0,0 +1,1432 @@
+; NOTE: Do not autogenerate
+;
+; Trip-count, byte-offset, and storage boundaries of the static bound proof:
+; constant trips at, below, and above the row extent, a contextual exact trip
+; behind a truncation, an enclosing AddRec trip, and the storage kinds an
+; object-containment requirement may or may not read.
+;
+; DEFINE: %{outer_backedges} = \
+; DEFINE: --implicit-check-not='{{^Loop %outer.header: (backedge-taken|constant max backedge-taken) count is }}'
+;
+; With the option off the pass leaves this module unchanged.
+; RUN: opt -S -passes=no-op-loopnest %s -o %t.noop
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %s -o %t.out
+; RUN: diff -u %t.noop %t.out
+;
+; The three contextual test cases must keep the outer backedge-taken counts
+; their names promise: a truncation for the two twins, an enclosing AddRec for
+; the third.
+; RUN: llvm-extract -S --recursive --keep-const-init --func=bound_context_exact_trunc %s -o %t.exact-trunc.ll
+; RUN: llvm-extract -S --recursive --keep-const-init --func=bound_context_nofact_trunc %s -o %t.nofact-trunc.ll
+; RUN: llvm-extract -S --recursive --keep-const-init --func=bound_context_exact_addrec %s -o %t.exact-addrec.ll
+; RUN: opt -passes='print<scalar-evolution>' -disable-output %t.exact-trunc.ll 2>&1 | FileCheck %s --check-prefix=EXACT-TRUNC-SCEV %{outer_backedges}
+; RUN: opt -passes='print<scalar-evolution>' -disable-output %t.nofact-trunc.ll 2>&1 | FileCheck %s --check-prefix=NOFACT-TRUNC-SCEV %{outer_backedges}
+; RUN: opt -passes='print<scalar-evolution>' -disable-output %t.exact-addrec.ll 2>&1 | FileCheck %s --check-prefix=ADDREC-SCEV %{outer_backedges}
+;
+; The profitability policy is fixed so that the small synthetic kernels are
+; judged by instruction order rather than by the cache model.
+; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
+; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
+; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+;
+; Preparation is analysis-only: a complete plan is printed and then discarded,
+; so the IR is unchanged with the option on.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
+; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
+; RUN: diff -u %t.out %t.prep
+;
+; Each printed plan has passed both dependence matrices, legality,
+; profitability, and validation. W is recovered from the element-normalized
+; byte stride, not from the storage size, and each generalized N/E pair has
+; its own modular and containment requirements.
+; PREP-LABEL: loop-interchange: prepared function=bound_typed_canonical{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[TYPED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[TYPED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_same_trip_wmin{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=8 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=2 reductions=2
+; PREP-SAME: byte-offset-proofs=2 requirements=4 bound=runtime-bound Wmin=4
+; PREP-SAME: requirement-ids=[[#WMIN_ID:]]:modular-outer-span/runtime/runtime
+; PREP-SAME: ,[[#WMIN_ID]]:object-containment/static/by-constant-max,[[#WMIN_ID+1]]:modular-outer-span/runtime/runtime,[[#WMIN_ID+1]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: loop-interchange: rejected function=bound_same_trip_wmin outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_distinct_runtime{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=second-distinct-runtime-trip-object-containment{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_outer_static_inner_runtime{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=second-distinct-runtime-trip-object-containment{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_byte_canonical{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[BYTE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BYTE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_byte_nondivisible{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+;
+; The high-offset store is independent of N; it needs no byte-offset bound
+; proof. Do not reinterpret a high-32-bit offset as the low address.
+; PREP-LABEL: loop-interchange: prepared function=bound_offset_high32{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+;
+; optsize and minsize functions skip preparation rather than reject a
+; discovered candidate, so they print nothing. bound_const_eq is the same shape
+; without the size attribute.
+; PREP-LABEL: loop-interchange: prepared function=bound_storage_defined_global{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[DEFINED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[DEFINED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_storage_external_global{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[EXTERNAL_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[EXTERNAL_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_storage_replaceable_global{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[REPLACEABLE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[REPLACEABLE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_storage_missing{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_storage_undersized{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_storage_nullable{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_storage_weak{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_runtime_canonical{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=1335
+; PREP-SAME: requirement-ids=[[RUNTIME_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[RUNTIME_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: loop-interchange: rejected function=bound_runtime_canonical outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+;
+; W-1 and W trips are safe. W+1 and W+2 must not be accepted by comparing
+; backedges directly to W. The i8 wraparound case below has 256 trips, not zero.
+; PREP-LABEL: loop-interchange: prepared function=bound_const_below{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[BELOW_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BELOW_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_const_eq{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[CONST_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[CONST_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_const_above1{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=known-unsafe-trip-exceeds-bound{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_const_above2{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=known-unsafe-trip-exceeds-bound{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_dominating_guard{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[GUARD_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[GUARD_ID]]:object-containment/static/by-constant-max{{$}}
+;
+; This body discharges by constant maximum despite its name. The truncation
+; twins below exercise the contextual exact-trip strategy.
+; PREP-LABEL: loop-interchange: prepared function=bound_max_too_broad{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[MAX_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[MAX_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_assume{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[ASSUME_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[ASSUME_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_scunknown_range{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[RANGE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[RANGE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_ineffective_min_metadata{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+; PREP-SAME: requirement-ids=[[MIN_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[MIN_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: loop-interchange: rejected function=bound_ineffective_min_metadata outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_nofact{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+; PREP-SAME: requirement-ids=[[NOFACT_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[NOFACT_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: loop-interchange: rejected function=bound_nofact outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; PREP-LABEL: loop-interchange: prepared function=bound_widen_i32{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=1
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[WIDEN_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[WIDEN_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LABEL: loop-interchange: rejected function=bound_overflow{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=known-unsafe-trip-exceeds-bound{{$}}
+;
+; Each pair has its own requirement ID. The modular row uses the exact trip
+; in context, while the containment row independently uses a constant maximum.
+; PREP-LABEL: loop-interchange: prepared function=bound_context_exact_trunc{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[TRUNC_ID:[0-9]+]]:modular-outer-span/static/by-context-exact,[[TRUNC_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=bound_context_nofact_trunc{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+; PREP-SAME: requirement-ids=[[NOFACT_TRUNC_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[NOFACT_TRUNC_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: loop-interchange: rejected function=bound_context_nofact_trunc outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=bound_context_exact_addrec{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=3 routing-depth=3 fission-rows=0 routing-rows=1 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[ADDREC_ID:[0-9]+]]:modular-outer-span/static/by-context-exact,[[ADDREC_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+;
+; EXACT-TRUNC-SCEV-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'bound_context_exact_trunc':
+; EXACT-TRUNC-SCEV: {{^}}Determining loop execution counts for: @bound_context_exact_trunc{{$}}
+; EXACT-TRUNC-SCEV: {{^}}Loop %outer.header: backedge-taken count is (zext i3 (trunc i64 %n to i3) to i64){{$}}
+; EXACT-TRUNC-SCEV-NEXT: {{^}}Loop %outer.header: constant max backedge-taken count is i64 7{{$}}
+; NOFACT-TRUNC-SCEV-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'bound_context_nofact_trunc':
+; NOFACT-TRUNC-SCEV: {{^}}Determining loop execution counts for: @bound_context_nofact_trunc{{$}}
+; NOFACT-TRUNC-SCEV: {{^}}Loop %outer.header: backedge-taken count is (zext i3 (trunc i64 %n to i3) to i64){{$}}
+; NOFACT-TRUNC-SCEV-NEXT: {{^}}Loop %outer.header: constant max backedge-taken count is i64 7{{$}}
+; ADDREC-SCEV-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'bound_context_exact_addrec':
+; ADDREC-SCEV: {{^}}Determining loop execution counts for: @bound_context_exact_addrec{{$}}
+; ADDREC-SCEV: {{^}}Loop %outer.header: backedge-taken count is {0,+,1}<nuw><nsw><%top.header>{{$}}
+; ADDREC-SCEV-NEXT: {{^}}Loop %outer.header: constant max backedge-taken count is i64 999999{{$}}
+;
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @bound_typed_canonical(ptr noalias dereferenceable(14257800) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [1335 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [1335 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_same_trip_wmin(ptr noalias dereferenceable(128) %A,
+ ptr noalias dereferenceable(288) %B,
+ ptr noalias %R, i64 %n) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %acol = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ %bcol = getelementptr inbounds [6 x double], ptr %B, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x double], ptr %acol, i64 %j, i64 0
+ %av = load double, ptr %aidx, align 8
+ %bidx = getelementptr inbounds [6 x double], ptr %bcol, i64 %j, i64 0
+ %bv = load double, ptr %bidx, align 8
+ %sum = fadd reassoc double %chk.j, %av
+ %chk.next.j = fadd reassoc double %sum, %bv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %adiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %adv = load double, ptr %adiag, align 8
+ %adn = fmul double %adv, 1.500000e+00
+ store double %adn, ptr %adiag, align 8
+ %bdiag = getelementptr inbounds [6 x double], ptr %B, i64 %i, i64 %i
+ %bdv = load double, ptr %bdiag, align 8
+ %bdn = fmul double %bdv, 1.500000e+00
+ store double %bdn, ptr %bdiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at distinct_runtime_obj = internal global [1024 x double] zeroinitializer, align 8
+
+define void @bound_distinct_runtime(ptr noalias %R, i64 %n, i64 %m) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %row = mul i64 %j, 4
+ %off = add i64 %row, %i
+ %idx = getelementptr inbounds double, ptr @distinct_runtime_obj, i64 %off
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, %m
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %drow = mul i64 %i, 4
+ %doff = add i64 %drow, %i
+ %ddiag = getelementptr inbounds double, ptr @distinct_runtime_obj, i64 %doff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_outer_static_inner_runtime(ptr noalias %R, i64 %m) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %row = mul i64 %j, 4
+ %off = add i64 %row, %i
+ %idx = getelementptr inbounds double, ptr @distinct_runtime_obj, i64 %off
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, %m
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %drow = mul i64 %i, 4
+ %doff = add i64 %drow, %i
+ %ddiag = getelementptr inbounds double, ptr @distinct_runtime_obj, i64 %doff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at byte_canonical_obj = common global [14268552 x i8] zeroinitializer, align 8
+
+define void @bound_byte_canonical(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr inbounds i8, ptr @byte_canonical_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 10680
+ %idx = getelementptr inbounds i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 10688
+ %ddiag = getelementptr inbounds i8, ptr @byte_canonical_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at byte_nondiv_obj = internal global [256 x i8] zeroinitializer, align 8
+
+define void @bound_byte_nondivisible(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr inbounds i8, ptr @byte_nondiv_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 52
+ %idx = getelementptr inbounds i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 4
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 60
+ %ddiag = getelementptr inbounds i8, ptr @byte_nondiv_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 4
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 4
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at offset_high32_obj = external global [4294967320 x i8], align 8
+
+define void @bound_offset_high32(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr @offset_high32_obj, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %lo = getelementptr inbounds i8, ptr @offset_high32_obj, i64 16
+ %hi = getelementptr inbounds i8, ptr @offset_high32_obj, i64 4294967312
+ %lv = load double, ptr %lo, align 8
+ %hn = fmul double %lv, 1.500000e+00
+ store double %hn, ptr %hi, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_optsize_canonical(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) optsize {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_minsize_canonical(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) minsize {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at storage_defined_obj = internal global [16 x double] zeroinitializer, align 8
+ at storage_external_obj = external global [16 x double], align 8
+ at storage_replaceable_obj = weak global [16 x double] zeroinitializer, align 8
+ at storage_weak_obj = extern_weak global [16 x double], align 8
+
+define void @bound_storage_defined_global(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr @storage_defined_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr @storage_defined_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_external_global(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr @storage_external_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr @storage_external_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_replaceable_global(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr @storage_replaceable_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr @storage_replaceable_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_missing(ptr noalias %A, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr %A, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr %A, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_undersized(ptr noalias dereferenceable(127) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr %A, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr %A, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_nullable(
+ ptr noalias dereferenceable_or_null(128) %A, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr %A, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr %A, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_storage_weak(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %coloff = mul i64 %i, 8
+ %col = getelementptr i8, ptr @storage_weak_obj, i64 %coloff
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %rowoff = mul i64 %j, 32
+ %idx = getelementptr i8, ptr %col, i64 %rowoff
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %diagoff = mul i64 %i, 40
+ %ddiag = getelementptr i8, ptr @storage_weak_obj, i64 %diagoff
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_runtime_canonical(ptr noalias dereferenceable(14257800) %A,
+ ptr noalias %R, i64 %n) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [1335 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [1335 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_const_below(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 3
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_const_eq(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_const_above1(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 5
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_const_above2(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 6
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_dominating_guard(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R, i64 %n) {
+entry:
+ %lo = icmp uge i64 %n, 1
+ %hi = icmp ule i64 %n, 4
+ %ok = and i1 %lo, %hi
+ br i1 %ok, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
+
+define void @bound_max_too_broad(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R, i64 %n) {
+entry:
+ %m7 = and i64 %n, 7
+ %nn = add i64 %m7, 1
+ %hi = icmp ule i64 %nn, 4
+ br i1 %hi, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %nn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
+
+define void @bound_assume(ptr noalias dereferenceable(128) %A, ptr noalias %R,
+ i64 %n) {
+entry:
+ %lo = icmp uge i64 %n, 1
+ call void @llvm.assume(i1 %lo)
+ %hi = icmp ule i64 %n, 4
+ call void @llvm.assume(i1 %hi)
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_scunknown_range(ptr noalias dereferenceable(128) %A,
+ ptr noalias %R, ptr %np) {
+entry:
+ %n = load i64, ptr %np, align 8, !range !1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_ineffective_min_metadata(
+ ptr noalias dereferenceable(128) %A, ptr noalias %R, i64 %m, i64 %k) {
+entry:
+ %mn = call i64 @llvm.smin.i64(i64 %m, i64 %k), !range !0
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %mn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @bound_nofact(ptr noalias dereferenceable(128) %A, ptr noalias %R,
+ i64 %n) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+ at widen_obj = internal global [4096 x double] zeroinitializer, align 8
+
+define void @bound_widen_i32(ptr noalias %R, i32 %n) {
+entry:
+ %lo = icmp uge i32 %n, 1
+ %hi = icmp ule i32 %n, 4
+ %ok = and i1 %lo, %hi
+ br i1 %ok, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i32 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %i64 = zext i32 %i to i64
+ %col = getelementptr inbounds [4 x double], ptr @widen_obj, i64 0, i64 %i64
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr @widen_obj, i64 %i64, i64 %i64
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i32 %i, 1
+ %i.ec = icmp eq i32 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
+
+ at overflow_obj = internal global [2048 x double] zeroinitializer, align 8
+
+define void @bound_overflow(ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i8 = phi i8 [ 0, %entry ], [ %i8.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %i = zext i8 %i8 to i64
+ %col = getelementptr inbounds [4 x double], ptr @overflow_obj, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr @overflow_obj, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i8.next = add i8 %i8, 1
+ %i.ec = icmp eq i8 %i8.next, 0
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+declare void @llvm.assume(i1)
+declare i64 @llvm.smin.i64(i64, i64)
+
+!0 = !{i64 0, i64 5}
+!1 = !{i64 1, i64 5}
+
+define void @bound_context_exact_trunc(ptr noalias dereferenceable(128) %A, ptr noalias %R, i64 %n) {
+entry:
+ %m7 = and i64 %n, 7
+ %nn = add nuw nsw i64 %m7, 1
+ %nn32 = trunc i64 %nn to i32
+ %hi = icmp ule i32 %nn32, 4
+ br i1 %hi, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %nn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
+define void @bound_context_nofact_trunc(ptr noalias dereferenceable(128) %A, ptr noalias %R, i64 %n) {
+entry:
+ %m7 = and i64 %n, 7
+ %nn = add nuw nsw i64 %m7, 1
+ %nn32 = trunc i64 %nn to i32
+ %hi = icmp ne i32 %nn32, 12345
+ br i1 %hi, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %nn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
+
+; The enclosing AddRec supplies the selected outer trip. Its guard limits
+; entry to the imperfect pair without changing the enclosing loop's span.
+define void @bound_context_exact_addrec(ptr noalias dereferenceable(128) %A, ptr noalias %R) {
+entry:
+ br label %top.header
+top.header:
+ %t = phi i64 [ 1, %entry ], [ %t.next, %top.latch ]
+ %hi = icmp ule i64 %t, 4
+ br i1 %hi, label %outer.ph, label %top.latch
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %t
+ br i1 %i.ec, label %outer.exit, label %outer.header
+outer.exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %top.latch
+top.latch:
+ %t.next = add nuw nsw i64 %t, 1
+ %t.ec = icmp eq i64 %t.next, 1000001
+ br i1 %t.ec, label %done, label %top.header
+done:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-deep-operand-chain.test b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-deep-operand-chain.test
new file mode 100644
index 00000000000000..79f4336ed63c53
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-deep-operand-chain.test
@@ -0,0 +1,210 @@
+# NOTE: Do not autogenerate
+#
+# A deep outer-IV operand chain. The epilogue operand classifier walks the
+# chain iteratively, so a chain of 300000 additions must not exhaust the
+# stack. split-file provides a generator, the K=3 module that generator must
+# reproduce, and a runner that sets the child stack to exactly 8 MiB before
+# exec'ing opt, so a recursion depth that outgrows that stack fails here
+# rather than depending on the host's stack limit. The generated shape is the
+# bound_context_nofact_trunc test case plus K dependent additions in the outer
+# header, where %c{K-1} feeds the epilogue's uitofp feeding the stored fmul, so
+# the chain reaches no address and no loop condition. The candidate stays
+# RuntimeBound, so the feature-on module must equal the feature-off module.
+#
+# REQUIRES: system-linux
+#
+# RUN: rm -rf %t && split-file %s %t
+# RUN: %python %t/generate-runtime-chain.py 3 > %t/k3.ll
+# RUN: diff -u %t/deep-k3.ll %t/k3.ll
+# RUN: %python %t/generate-runtime-chain.py 300000 > %t/k300000.ll
+# RUN: %python %t/bounded-stack.py opt -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -disable-output %t/k3.ll
+# RUN: %python %t/bounded-stack.py opt -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -disable-output %t/k300000.ll
+# Feature-on preparation at both K reports K rematerialized definitions; the
+# plan is then declined as RuntimeBound.
+# RUN: %python %t/bounded-stack.py opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -loop-interchange-outer-epilogue-fission=false %t/k3.ll -o %t/k3.off.ll
+# RUN: %python %t/bounded-stack.py opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan %t/k3.ll -o %t/k3.on.ll 2> %t/k3.on.stderr
+# RUN: diff -u %t/k3.off.ll %t/k3.on.ll
+# RUN: FileCheck %s --check-prefix=K3 --input-file=%t/k3.on.stderr --implicit-check-not='loop-interchange:'
+# RUN: FileCheck %s --check-prefix=NO-MARKER --input-file=%t/k3.on.ll --implicit-check-not='.lver' --implicit-check-not='!alias.scope' --implicit-check-not='!noalias'
+# RUN: %python %t/bounded-stack.py opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -loop-interchange-outer-epilogue-fission=false %t/k300000.ll -o %t/k300000.off.ll
+# RUN: %python %t/bounded-stack.py opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan %t/k300000.ll -o %t/k300000.on.ll 2> %t/k300000.on.stderr
+# RUN: diff -u %t/k300000.off.ll %t/k300000.on.ll
+# RUN: FileCheck %s --check-prefix=K300000 --input-file=%t/k300000.on.stderr --implicit-check-not='loop-interchange:'
+# RUN: FileCheck %s --check-prefix=NO-MARKER --input-file=%t/k300000.on.ll --implicit-check-not='.lver' --implicit-check-not='!alias.scope' --implicit-check-not='!noalias'
+# RUN: %if asserts %{ %python %t/bounded-stack.py opt -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=instorder,vectorize -verify-each -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan -debug-only=loop-interchange -disable-output %t/k3.ll 2>&1 | FileCheck %s --check-prefix=K3-DEBUG %}
+#
+# K3-LABEL: loop-interchange: prepared function=deep_runtime_nofact{{ }}
+# K3-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=3
+# K3-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+# K3-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+# K3-SAME: requirement-ids=[[K3_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[K3_ID]]:object-containment/static/by-constant-max{{$}}
+# K3-NEXT: loop-interchange: rejected function=deep_runtime_nofact{{ }}
+# K3-SAME: outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+#
+# K300000-LABEL: loop-interchange: prepared function=deep_runtime_nofact{{ }}
+# K300000-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=300000
+# K300000-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+# K300000-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+# K300000-SAME: requirement-ids=[[K300000_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[K300000_ID]]:object-containment/static/by-constant-max{{$}}
+# K300000-NEXT: loop-interchange: rejected function=deep_runtime_nofact{{ }}
+# K300000-SAME: outer=outer.header inner=inner.header reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+#
+# The discarded plan leaves no guard, no fallback loop, no versioning marker,
+# and no alias metadata in the module at either K.
+# NO-MARKER: {{^}}define void @deep_runtime_nofact({{.*$}}
+#
+# K3-DEBUG: loop-interchange: discovered a closed outer-loop epilogue in function 'deep_runtime_nofact' with 1 path block(s), 5 instruction(s), and 3 rematerialized loop-local definition(s).
+#
+#--- generate-runtime-chain.py
+#!/usr/bin/env python3
+import argparse
+
+
+def positive_int(value):
+ result = int(value)
+ if result < 1:
+ raise argparse.ArgumentTypeError("chain length must be positive")
+ return result
+
+
+parser = argparse.ArgumentParser()
+parser.add_argument("length", type=positive_int)
+args = parser.parse_args()
+
+print('target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"')
+print("""
+define void @deep_runtime_nofact(ptr noalias dereferenceable(128) %A, ptr noalias %R, i64 %n) {
+entry:
+ %m7 = and i64 %n, 7
+ %nn = add nuw nsw i64 %m7, 1
+ %nn32 = trunc i64 %nn to i32
+ %hi = icmp ne i32 %nn32, 12345
+ br i1 %hi, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]""")
+for index in range(args.length):
+ operand = "%i" if index == 0 else f"%c{index - 1}"
+ print(f" %c{index} = add i64 {operand}, 1")
+print(f""" %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %deep.fp = uitofp i64 %c{args.length - 1} to double
+ %dnew = fmul double %dval, %deep.fp
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %nn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}}""")
+#--- bounded-stack.py
+#!/usr/bin/env python3
+"""Exec a tool under an 8 MiB child stack with core dumps disabled.
+
+Both the soft and the hard stack limit are set, so nothing downstream can
+raise the limit back. Every step is confirmed by re-reading the limit, and any
+step that cannot be taken fails the run with a message instead of silently
+measuring a different stack.
+"""
+import os
+import resource
+import sys
+
+STACK_BYTES = 8 * 1024 * 1024
+
+
+def fail(message):
+ sys.stderr.write("bounded-stack: " + message + "\n")
+ sys.exit(2)
+
+
+def set_limit(which, name, limits):
+ try:
+ resource.setrlimit(which, limits)
+ except (ValueError, OSError) as error:
+ fail("cannot set %s to %s: %s" % (name, limits, error))
+ actual = resource.getrlimit(which)
+ if tuple(actual) != limits:
+ fail("%s reads back as %s, expected %s" % (name, tuple(actual), limits))
+
+
+if len(sys.argv) < 2:
+ fail("usage: bounded-stack.py TOOL [ARGUMENT ...]")
+
+set_limit(resource.RLIMIT_STACK, "RLIMIT_STACK", (STACK_BYTES, STACK_BYTES))
+set_limit(resource.RLIMIT_CORE, "RLIMIT_CORE", (0, 0))
+
+try:
+ os.execvp(sys.argv[1], sys.argv[1:])
+except OSError as error:
+ fail("cannot exec %s: %s" % (sys.argv[1], error))
+fail("exec of %s returned" % sys.argv[1])
+#--- deep-k3.ll
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @deep_runtime_nofact(ptr noalias dereferenceable(128) %A, ptr noalias %R, i64 %n) {
+entry:
+ %m7 = and i64 %n, 7
+ %nn = add nuw nsw i64 %m7, 1
+ %nn32 = trunc i64 %nn to i32
+ %hi = icmp ne i32 %nn32, 12345
+ br i1 %hi, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %outer.ph ], [ %chk.next, %outer.latch ]
+ %c0 = add i64 %i, 1
+ %c1 = add i64 %c0, 1
+ %c2 = add i64 %c1, 1
+ %col = getelementptr inbounds [4 x double], ptr %A, i64 0, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %idx = getelementptr inbounds [4 x double], ptr %col, i64 %j, i64 0
+ %v = load double, ptr %idx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %v
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x double], ptr %A, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %deep.fp = uitofp i64 %c2 to double
+ %dnew = fmul double %dval, %deep.fp
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %nn
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ br label %done
+done:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
new file mode 100644
index 00000000000000..65a5b5ebcb9499
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
@@ -0,0 +1,2282 @@
+; NOTE: Do not autogenerate
+;
+; Rejections: dependence, operand-chain, legality, and structure negatives,
+; including unsupported epilogue data and control roles. Two positive controls
+; separate N/N aliasing and the matrix memory budget from the negatives.
+;
+; With the option off the pass leaves this module unchanged.
+; RUN: opt -S -passes=no-op-loopnest %s -o %t.noop
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %s -o %t.out
+; RUN: diff -u %t.noop %t.out
+;
+; The profitability policy is fixed so that the small synthetic kernels are
+; judged by instruction order rather than by the cache model. The positive
+; N/E, closed-slice, reassociated-FP, and pure-data controls live in
+; outer-epilogue-fission.ll.
+; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
+; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
+; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+;
+; Discovery rejects the four convergence negatives, so no later proof stage
+; may report them.
+; DEFINE: %{convergence_owner} = \
+; DEFINE: --implicit-check-not='cross-partition' \
+; DEFINE: --implicit-check-not='fission-context' \
+; DEFINE: --implicit-check-not='routing dependence' \
+; DEFINE: --implicit-check-not='interchange legality'
+;
+; The three selected-outer negatives stop at the N-to-E proof, before either
+; speculative matrix is built.
+; DEFINE: %{selected_owner} = \
+; DEFINE: --implicit-check-not='fission-context' \
+; DEFINE: --implicit-check-not='routing dependence'
+;
+; Preparation is analysis-only: a complete plan is printed and then discarded,
+; so the IR is unchanged with the option on.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
+; RUN: FileCheck %s --check-prefixes=PREP,DISCOVERY,MATRIX-READY --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
+; RUN: diff -u %t.out %t.prep
+;
+; At ratio zero, discovery still reports its own failures; only candidates
+; that finish discovery reach the shared budget rejection. At ratio one the
+; later dependence and legality paths run again, but matrix_memory_ratio now
+; fails the matrix memory limit computed after the epilogue is excluded.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 -loop-interchange-outer-epilogue-fission=false %s -o %t.ratio0.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 %{prepare} %{remarks} %s -o %t.ratio0.on 2> %t.ratio0.stderr
+; RUN: diff -u %t.ratio0.off %t.ratio0.on
+; RUN: FileCheck %s --check-prefixes=MEMZERO,DISCOVERY --input-file=%t.ratio0.stderr --implicit-check-not='loop-interchange:'
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=1 -loop-interchange-outer-epilogue-fission=false %s -o %t.ratio1.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=1 %{prepare} %{remarks} %s -o %t.ratio1.on 2> %t.ratio1.stderr
+; RUN: diff -u %t.ratio1.off %t.ratio1.on
+; RUN: FileCheck %s --check-prefixes=PREP,DISCOVERY,MATRIX-LIMIT --input-file=%t.ratio1.stderr --implicit-check-not='loop-interchange:'
+;
+; These whole-module traces reach the two matrix guards. The same N/N alias
+; rejection and noalias control precede the ratio-sensitive function in both
+; traces, and ordinary routing starts only after the feature has decided.
+; RUN: %if asserts %{ opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -loop-interchange-max-mem-instr-ratio=1 -debug-only=loop-interchange -verify-each %s -o %t.matrix1.debug 2> %t.matrix1.debug.stderr %}
+; RUN: %if asserts %{ FileCheck %s --check-prefixes=MATRIX-TRACE,MATRIX-TRACE-LIMIT --input-file=%t.matrix1.debug.stderr %}
+; RUN: %if asserts %{ diff -u %t.ratio1.on %t.matrix1.debug %}
+; RUN: %if asserts %{ opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -verify-each %s -o %t.matrix4.debug 2> %t.matrix4.debug.stderr %}
+; RUN: %if asserts %{ FileCheck %s --check-prefixes=MATRIX-TRACE,MATRIX-TRACE-READY --input-file=%t.matrix4.debug.stderr %}
+; RUN: %if asserts %{ diff -u %t.prep %t.matrix4.debug %}
+;
+; The raw dependence directions below are those between N and E, before the
+; ordinary path normalizes them. LE and NE pass the raw N/E check and both
+; matrices, then fail tight nesting. GE fails the raw direction check before
+; either matrix is built.
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_le %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=LE-PREP --implicit-check-not='loop-interchange: prepared function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_ge %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=GE-PREP --implicit-check-not='loop-interchange: prepared function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_ne %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=NE-PREP --implicit-check-not='loop-interchange: prepared function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=n_convergent_controlled_in_nest %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=CONV-NEST %{convergence_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=n_convergent_uncontrolled_in_outer_header %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=CONV-HEADER %{convergence_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=n_convergent_uncontrolled_in_inner_body %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=CONV-BODY %{convergence_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=n_convergent_uncontrolled_in_inner_ph %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=CONV-PH %{convergence_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_selected_le %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SEL-LE %{selected_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_selected_ge %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SEL-GE %{selected_owner} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=direction_selected_ne %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SEL-NE %{selected_owner} %}
+;
+; Each negative reports its first rejection; the implicit exclusion rejects
+; any other feature line.
+; PREP-LABEL: loop-interchange: rejected function=e_to_n_flow{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_to_n_flow{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; PREP-LABEL: loop-interchange: rejected function=e_to_n_anti{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_to_n_anti{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=e_to_n_output{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_to_n_output{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_consumes_reduction{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue consumes an inner/reduction or unsupported external SSA value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_consumes_inner_iv{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue consumes an inner/reduction or unsupported external SSA value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_escapes{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue-defined SSA value escapes the closed slice{{$}}
+; PREP-LABEL: loop-interchange: rejected function=exact_recurrence{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=exact_recurrence{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=partly_exact_recurrence{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=partly_exact_recurrence{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=call_in_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=convergent_call_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=deopt_call_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=constrained_fp_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; PREP-LABEL: loop-interchange: rejected function=unknown_alias{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=unknown_alias{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=non_affine_address{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=non_affine_address{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=volatile_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=atomic_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=fence_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue contains an unsupported operation or metadata{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=conditional_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=exit-to-latch path contains conditional or non-straight-line control{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=multiple_epilogues{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=exit-to-latch path contains conditional or non-straight-line control{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=address_taken_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=post-inner path block cannot be collapsed after extraction{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=triangular_bounds{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=child range is not rectangular with respect to the outer loop{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=data_dependent_bounds{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=child range is not rectangular with respect to the outer loop{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=loop_local_outer_bound{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=outer latch control depends on an unsupported loop-local value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=noncanonical_latch{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=latch/exit control is not the supported canonical form{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=unsupported_metadata{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=loop carries metadata without a fission policy{{$}}
+; PREP-LABEL: loop-interchange: rejected function=outer_header_freeze{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=outer_header_freeze{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=invariant_address_n_to_e_flow{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=invariant_address_n_to_e_flow{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=invariant_address_n_to_e_output{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=invariant_address_n_to_e_output{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=direction_le{{ }}
+; PREP-SAME: outer=le.outer inner=le.inner reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_le{{ }}
+; MEMZERO-SAME: outer=le.outer inner=le.inner reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=direction_ge{{ }}
+; PREP-SAME: outer=ge.outer inner=ge.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_ge{{ }}
+; MEMZERO-SAME: outer=ge.outer inner=ge.inner reason=union N/E memory budget or simple-access check failed{{$}}
+; PREP-LABEL: loop-interchange: rejected function=direction_ne{{ }}
+; PREP-SAME: outer=ne.outer inner=ne.inner reason=virtual extracted nest failed existing interchange legality{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_ne{{ }}
+; MEMZERO-SAME: outer=ne.outer inner=ne.inner reason=union N/E memory budget or simple-access check failed{{$}}
+;
+; These four roles and e_consumes_inner_iv above fail in discovery. Even
+; loop-invariant loop-local latch control is outside the supported control
+; partition. Their pure-data twins in outer-epilogue-fission.ll are positive.
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_data_from_shared_preheader_freeze{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue consumes an inner/reduction or unsupported external SSA value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_data_from_inner_preheader_load{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue consumes an inner/reduction or unsupported external SSA value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_control_from_shared_preheader{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=outer latch control depends on an unsupported loop-local value{{$}}
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_control_from_inner_preheader{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=outer latch control depends on an unsupported loop-local value{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; The invalid leaf records a, b, c and then fails at k1, so invalidity
+; propagates after partial recording. Discovery rejects it, and no run prints
+; a prepared line for it.
+; DISCOVERY-LABEL: loop-interchange: rejected function=e_operand_closure_invalid_leaf{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=epilogue consumes an inner/reduction or unsupported external SSA value{{$}}
+;
+; The four convergence negatives place a convergent call in each region the
+; scan covers. Discovery rejects each one, so no later proof stage reports
+; them. The controls with a plain callee are in outer-epilogue-fission.ll.
+; PREP-LABEL: loop-interchange: rejected function=n_convergent_controlled_in_nest{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_outer_header{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_body{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_ph{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+;
+; The three selected-outer directions are the generalized raw classes at the
+; selected outer loop itself: raw vectors [<=|<], [=>|<], and [<>].
+; PREP-LABEL: loop-interchange: rejected function=direction_selected_le{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: rejected function=direction_selected_ge{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: rejected function=direction_selected_ne{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+;
+; The budget check precedes the convergence scan, so at ratio zero a
+; convergence negative reports the budget failure instead.
+; MEMZERO-LABEL: loop-interchange: rejected function=n_convergent_controlled_in_nest{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_outer_header{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_body{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_ph{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_selected_le{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_selected_ge{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=direction_selected_ne{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+;
+; Both N/E pairs are independent, but the uncertain N/N pair fails the first
+; matrix. Adding noalias to A alone gives a complete plan with no requirement.
+; PREP-LABEL: loop-interchange: rejected function=unknown_alias_in_nest{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=fission-context dependence matrix is unavailable{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: fission-context dependence matrix is unavailable
+; PREP-NEXT: remark: <unknown>:0:0: All loops have dependencies in all directions.
+; MEMZERO-LABEL: loop-interchange: rejected function=unknown_alias_in_nest{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; PREP-LABEL: loop-interchange: prepared function=noalias_in_nest{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MEMZERO-LABEL: loop-interchange: rejected function=noalias_in_nest{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+;
+; N and E together have 9 memory instructions among 92. Excluding the
+; 66-instruction E slice leaves 8 among 26, which ratio 1 rejects (8 > 26/8)
+; and the default ratio accepts.
+; MATRIX-READY-LABEL: loop-interchange: prepared function=matrix_memory_ratio{{ }}
+; MATRIX-READY-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=66 rematerialized=0
+; MATRIX-READY-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; MATRIX-READY-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; MATRIX-READY-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MATRIX-LIMIT-LABEL: loop-interchange: rejected function=matrix_memory_ratio{{ }}
+; MATRIX-LIMIT-SAME: outer=outer.header inner=inner.header reason=fission-context dependence matrix is unavailable{{$}}
+; MATRIX-LIMIT-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: fission-context dependence matrix is unavailable
+; MATRIX-LIMIT-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MEMZERO-LABEL: loop-interchange: rejected function=matrix_memory_ratio{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; The reproducer of https://github.com/llvm/llvm-project/issues/47457 fails
+; the straight-line exit-to-latch check in discovery, so it reports the same
+; reason under every memory ratio and prints no prepared line.
+; DISCOVERY-LABEL: loop-interchange: rejected function=issue47457_guarded_epilogue{{ }}
+; DISCOVERY-SAME: outer=outer.header inner=inner.header reason=exit-to-latch path contains conditional or non-straight-line control{{$}}
+;
+; The speculative matrix calls must not emit the ordinary missed remarks.
+; -NEXT ties the failing matrix to the feature rejection before the ordinary
+; route emits its own miss.
+; MATRIX-TRACE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'unknown_alias_in_nest' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-TRACE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 16 instructions.
+; MATRIX-TRACE-NEXT: Found 2 Loads and Stores to analyze
+; MATRIX-TRACE-NEXT: Found anti dependency between Src and Dst
+; MATRIX-TRACE-NEXT: Src: %v = load i64, ptr %ap, align 8
+; MATRIX-TRACE-NEXT: Dst: store i64 %v, ptr %bp, align 8
+; MATRIX-TRACE-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'unknown_alias_in_nest': Outer 'outer.header', Inner 'inner.header': fission-context dependence matrix is unavailable
+; MATRIX-TRACE-NEXT: loop-interchange: rejected function=unknown_alias_in_nest outer=outer.header inner=inner.header reason=fission-context dependence matrix is unavailable
+; MATRIX-TRACE-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: fission-context dependence matrix is unavailable
+; MATRIX-TRACE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; MATRIX-TRACE: remark: <unknown>:0:0: All loops have dependencies in all directions.
+; MATRIX-TRACE-NEXT: Populating dependency matrix failed
+; MATRIX-TRACE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'noalias_in_nest' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-TRACE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 16 instructions.
+; MATRIX-TRACE-NEXT: Found 2 Loads and Stores to analyze
+; MATRIX-TRACE-NEXT: Found 2 Loads and Stores to analyze
+; MATRIX-TRACE-NEXT: Checking if loops 'outer.header' and 'inner.header' are tightly nested
+; MATRIX-TRACE-NEXT: Checking instructions in Loop header and Loop latch
+; MATRIX-TRACE-NEXT: Loops are perfectly nested
+; MATRIX-TRACE-NEXT: Cost = -2
+; MATRIX-TRACE-NEXT: loop-interchange: prepared a complete outer-epilogue fission plan in function 'noalias_in_nest': Outer 'outer.header', Inner 'inner.header', absolute columns 0/1, routing columns 0/1, 0 cross dependence(s).
+; MATRIX-TRACE-NEXT: loop-interchange: prepared function=noalias_in_nest{{ }}
+; MATRIX-TRACE-SAME: rematerialized=0{{ }}
+; MATRIX-TRACE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; MATRIX-TRACE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; MATRIX-TRACE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'matrix_memory_ratio' with 1 path block(s), 66 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-TRACE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 9 loads/stores over 92 instructions.
+; MATRIX-TRACE-NEXT: Found 8 Loads and Stores to analyze
+; MATRIX-TRACE-LIMIT-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'matrix_memory_ratio': Outer 'outer.header', Inner 'inner.header': fission-context dependence matrix is unavailable
+; MATRIX-TRACE-LIMIT-NEXT: loop-interchange: rejected function=matrix_memory_ratio outer=outer.header inner=inner.header reason=fission-context dependence matrix is unavailable
+; MATRIX-TRACE-LIMIT-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: fission-context dependence matrix is unavailable
+; MATRIX-TRACE-READY-NEXT: Found anti dependency between Src and Dst
+; MATRIX-TRACE-READY: Src: %dv = load i64, ptr %dp, align 8
+; MATRIX-TRACE-READY-NEXT: Dst: store i64 %dn, ptr %dp, align 8
+; MATRIX-TRACE-READY-NEXT: Found 8 Loads and Stores to analyze
+; MATRIX-TRACE-READY: loop-interchange: prepared a complete outer-epilogue fission plan in function 'matrix_memory_ratio': Outer 'outer.header', Inner 'inner.header', absolute columns 0/1, routing columns 0/1, 0 cross dependence(s).
+; MATRIX-TRACE-READY-NEXT: loop-interchange: prepared function=matrix_memory_ratio{{ }}
+; MATRIX-TRACE-READY-SAME: rematerialized=0{{ }}
+; MATRIX-TRACE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; MATRIX-TRACE: Found 9 Loads and Stores to analyze
+; MATRIX-TRACE: Dependency matrix before interchange:
+; MATRIX-TRACE-NEXT: {{^}}= ={{ *$}}
+; MATRIX-TRACE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; MATRIX-TRACE: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MATRIX-TRACE-NEXT: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+;
+; LE-PREP-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_le' with 1 path block(s), 8 instruction(s), and 0 rematerialized loop-local definition(s).
+; LE-PREP-NEXT: loop-interchange: outer-epilogue preparation memory budget: 4 loads/stores over 23 instructions.
+; LE-PREP-NEXT: Found 2 Loads and Stores to analyze
+; LE-PREP-NEXT: Found 2 Loads and Stores to analyze
+; LE-PREP-NEXT: Checking if loops 'le.outer' and 'le.inner' are tightly nested
+; LE-PREP-NEXT: Checking instructions in Loop header and Loop latch
+; LE-PREP-NEXT: Loops not tightly nested
+; LE-PREP-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_le': Outer 'le.outer', Inner 'le.inner': virtual extracted nest failed existing interchange legality
+; LE-PREP-NEXT: loop-interchange: rejected function=direction_le outer=le.outer inner=le.inner reason=virtual extracted nest failed existing interchange legality
+; LE-PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: virtual extracted nest failed existing interchange legality
+; LE-PREP-NEXT: Processing LoopList of size = 3 containing the following loops:
+; LE-PREP: Dependency matrix before interchange:
+; LE-PREP-NEXT: {{^}}* = I{{ *$}}
+; LE-PREP-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
+; LE-PREP: Cannot prove legality, not interchanging loops 'le.root' and 'le.outer'
+;
+; GE-PREP-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_ge' with 1 path block(s), 7 instruction(s), and 0 rematerialized loop-local definition(s).
+; GE-PREP-NEXT: loop-interchange: outer-epilogue preparation memory budget: 4 loads/stores over 22 instructions.
+; GE-PREP-NEXT: loop-interchange: rejected an unknown, reversed, confused, or assumption-bearing N/E dependence.
+; GE-PREP-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_ge': Outer 'ge.outer', Inner 'ge.inner': cross-partition dependence is not statically proved N-to-E
+; GE-PREP-NEXT: loop-interchange: rejected function=direction_ge outer=ge.outer inner=ge.inner reason=cross-partition dependence is not statically proved N-to-E
+; GE-PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; GE-PREP-NEXT: Processing LoopList of size = 3 containing the following loops:
+; GE-PREP: Negative dependence vector normalized.
+; GE-PREP: Dependency matrix before interchange:
+; GE-PREP-NEXT: {{^}}* = I{{ *$}}
+; GE-PREP-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
+; GE-PREP: Cannot prove legality, not interchanging loops 'ge.root' and 'ge.outer'
+;
+; NE-PREP-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_ne' with 1 path block(s), 7 instruction(s), and 0 rematerialized loop-local definition(s).
+; NE-PREP-NEXT: loop-interchange: outer-epilogue preparation memory budget: 4 loads/stores over 21 instructions.
+; NE-PREP-NEXT: Found 2 Loads and Stores to analyze
+; NE-PREP-NEXT: Found 2 Loads and Stores to analyze
+; NE-PREP-NEXT: Checking if loops 'ne.outer' and 'ne.inner' are tightly nested
+; NE-PREP-NEXT: Checking instructions in Loop header and Loop latch
+; NE-PREP-NEXT: Loops not tightly nested
+; NE-PREP-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_ne': Outer 'ne.outer', Inner 'ne.inner': virtual extracted nest failed existing interchange legality
+; NE-PREP-NEXT: loop-interchange: rejected function=direction_ne outer=ne.outer inner=ne.inner reason=virtual extracted nest failed existing interchange legality
+; NE-PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: virtual extracted nest failed existing interchange legality
+; NE-PREP-NEXT: Processing LoopList of size = 3 containing the following loops:
+; NE-PREP: Dependency matrix before interchange:
+; NE-PREP-NEXT: {{^}}* = I{{ *$}}
+; NE-PREP-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
+; NE-PREP: Cannot prove legality, not interchanging loops 'ne.root' and 'ne.outer'
+; Each convergence negative shows discovery, then the memory budget, then the
+; convergence rejection with nothing between them. The implicit exclusions
+; fail any scan that reaches a later proof stage.
+; CONV-NEST-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'n_convergent_controlled_in_nest' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; CONV-NEST-NEXT: loop-interchange: outer-epilogue preparation memory budget: 2 loads/stores over 17 instructions.
+; CONV-NEST-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'n_convergent_controlled_in_nest': Outer 'outer.header', Inner 'inner.header': retained nest contains an unsupported convergent operation
+; CONV-NEST-NEXT: loop-interchange: rejected function=n_convergent_controlled_in_nest outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation
+; CONV-NEST-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; CONV-NEST-NEXT: Processing LoopList of size = 2 containing the following loops:
+; CONV-NEST: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; CONV-HEADER-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'n_convergent_uncontrolled_in_outer_header' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; CONV-HEADER-NEXT: loop-interchange: outer-epilogue preparation memory budget: 2 loads/stores over 16 instructions.
+; CONV-HEADER-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'n_convergent_uncontrolled_in_outer_header': Outer 'outer.header', Inner 'inner.header': retained nest contains an unsupported convergent operation
+; CONV-HEADER-NEXT: loop-interchange: rejected function=n_convergent_uncontrolled_in_outer_header outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation
+; CONV-HEADER-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; CONV-HEADER-NEXT: Processing LoopList of size = 2 containing the following loops:
+; CONV-HEADER: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; CONV-BODY-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'n_convergent_uncontrolled_in_inner_body' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; CONV-BODY-NEXT: loop-interchange: outer-epilogue preparation memory budget: 2 loads/stores over 16 instructions.
+; CONV-BODY-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'n_convergent_uncontrolled_in_inner_body': Outer 'outer.header', Inner 'inner.header': retained nest contains an unsupported convergent operation
+; CONV-BODY-NEXT: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_body outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation
+; CONV-BODY-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; CONV-BODY-NEXT: Processing LoopList of size = 2 containing the following loops:
+; CONV-BODY: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; CONV-PH-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'n_convergent_uncontrolled_in_inner_ph' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; CONV-PH-NEXT: loop-interchange: outer-epilogue preparation memory budget: 2 loads/stores over 16 instructions.
+; CONV-PH-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'n_convergent_uncontrolled_in_inner_ph': Outer 'outer.header', Inner 'inner.header': retained nest contains an unsupported convergent operation
+; CONV-PH-NEXT: loop-interchange: rejected function=n_convergent_uncontrolled_in_inner_ph outer=outer.header inner=inner.header reason=retained nest contains an unsupported convergent operation
+; CONV-PH-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
+; CONV-PH-NEXT: Processing LoopList of size = 2 containing the following loops:
+; CONV-PH: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+;
+; The selected-outer negatives stop at the N-to-E proof. GE additionally
+; reports the reversed/unknown N/E dependence that the proof refused.
+; SEL-LE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_selected_le' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
+; SEL-LE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 19 instructions.
+; SEL-LE-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_selected_le': Outer 'outer.header', Inner 'inner.header': cross-partition dependence is not statically proved N-to-E
+; SEL-LE-NEXT: loop-interchange: rejected function=direction_selected_le outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E
+; SEL-LE-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; SEL-LE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SEL-LE: Dependency matrix before interchange:
+; SEL-LE-NEXT: {{^}}* I{{ *$}}
+; SEL-LE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; SEL-LE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; SEL-GE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_selected_ge' with 1 path block(s), 3 instruction(s), and 0 rematerialized loop-local definition(s).
+; SEL-GE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 18 instructions.
+; SEL-GE-NEXT: loop-interchange: rejected an unknown, reversed, confused, or assumption-bearing N/E dependence.
+; SEL-GE-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_selected_ge': Outer 'outer.header', Inner 'inner.header': cross-partition dependence is not statically proved N-to-E
+; SEL-GE-NEXT: loop-interchange: rejected function=direction_selected_ge outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E
+; SEL-GE-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; SEL-GE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SEL-GE: Dependency matrix before interchange:
+; SEL-GE-NEXT: {{^}}* I{{ *$}}
+; SEL-GE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; SEL-GE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; SEL-NE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_selected_ne' with 1 path block(s), 3 instruction(s), and 0 rematerialized loop-local definition(s).
+; SEL-NE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 17 instructions.
+; SEL-NE-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'direction_selected_ne': Outer 'outer.header', Inner 'inner.header': cross-partition dependence is not statically proved N-to-E
+; SEL-NE-NEXT: loop-interchange: rejected function=direction_selected_ne outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E
+; SEL-NE-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; SEL-NE-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SEL-NE: Dependency matrix before interchange:
+; SEL-NE-NEXT: {{^}}* I{{ *$}}
+; SEL-NE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; SEL-NE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+ at address_taken_epilogue_block =
+ constant ptr blockaddress(@address_taken_epilogue, %epilogue.second)
+
+define void @e_to_n_flow(ptr noalias %A, ptr noalias %S, ptr noalias %B,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %sv = load double, ptr %si, align 8
+ %bidx = getelementptr inbounds [1335 x double], ptr %B, i64 %j, i64 %i
+ store double %sv, ptr %bidx, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %i1 = add i64 %i, 1
+ %si1 = getelementptr inbounds double, ptr %S, i64 %i1
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %si1, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_to_n_anti(ptr noalias %A, ptr noalias %S, ptr noalias %B,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ store double %av, ptr %si, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %i1 = add i64 %i, 1
+ %si1 = getelementptr inbounds double, ptr %S, i64 %i1
+ %sr = load double, ptr %si1, align 8
+ %bi = getelementptr inbounds double, ptr %B, i64 %i
+ store double %sr, ptr %bi, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_to_n_output(ptr noalias %A, ptr noalias %S, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ store double %av, ptr %si, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %i1 = add i64 %i, 1
+ %si1 = getelementptr inbounds double, ptr %S, i64 %i1
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %si1, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_consumes_reduction(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %scaled = fmul double %chk.next, %chk.next
+ store double %scaled, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_consumes_inner_iv(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %j.exit = phi i64 [ %j.next, %inner.header ]
+ %j.value = uitofp i64 %j.exit to double
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ store double %j.value, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_escapes(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ %esc = phi double [ %dnew, %outer.latch ]
+ %r1 = getelementptr inbounds double, ptr %R, i64 1
+ store double %chk.res, ptr %R, align 8
+ store double %esc, ptr %r1, align 8
+ ret void
+}
+
+define void @exact_recurrence(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @partly_exact_recurrence(ptr noalias %A, ptr noalias %B,
+ ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chkA.i = phi double [ 0.000000e+00, %entry ], [ %chkA.next, %outer.latch ]
+ %chkB.i = phi double [ 0.000000e+00, %entry ], [ %chkB.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chkA.j = phi double [ %chkA.i, %outer.header ], [ %chkA.next.j, %inner.header ]
+ %chkB.j = phi double [ %chkB.i, %outer.header ], [ %chkB.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chkA.next.j = fadd reassoc double %chkA.j, %av
+ %bidx = getelementptr inbounds [1335 x double], ptr %B, i64 %j, i64 %i
+ %bv = load double, ptr %bidx, align 8
+ %chkB.next.j = fadd double %chkB.j, %bv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chkA.next = phi double [ %chkA.next.j, %inner.header ]
+ %chkB.next = phi double [ %chkB.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chkA.res = phi double [ %chkA.next, %outer.latch ]
+ %chkB.res = phi double [ %chkB.next, %outer.latch ]
+ %r1 = getelementptr inbounds double, ptr %R, i64 1
+ store double %chkA.res, ptr %R, align 8
+ store double %chkB.res, ptr %r1, align 8
+ ret void
+}
+
+declare void @sink(double)
+declare void @barrier() #0
+declare double @llvm.experimental.constrained.fadd.f64(double, double, metadata, metadata)
+
+define void @call_in_epilogue(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ call void @sink(double %dval)
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @convergent_call_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ call void @barrier() #0
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @deopt_call_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ call void @sink(double %dval) [ "deopt"(i32 0) ]
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @constrained_fp_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) #1 {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = call double @llvm.experimental.constrained.fadd.f64(double %dval, double 1.000000e+00, metadata !"round.dynamic", metadata !"fpexcept.strict") #1
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @unknown_alias(ptr %X, ptr %Y, ptr %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %xidx = getelementptr inbounds [1335 x double], ptr %X, i64 %j, i64 %i
+ %xv = load double, ptr %xidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %xv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ydiag = getelementptr inbounds [1335 x double], ptr %Y, i64 %i, i64 %i
+ %yv = load double, ptr %ydiag, align 8
+ %yn = fmul double %yv, 1.500000e+00
+ store double %yn, ptr %ydiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @non_affine_address(ptr noalias %A,
+ ptr noalias dereferenceable(14257800) %D,
+ ptr noalias %IdxV, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %didx = getelementptr inbounds [1335 x double], ptr %D, i64 %j, i64 %i
+ %dv = load double, ptr %didx, align 8
+ %pair = fadd reassoc double %av, %dv
+ %chk.next.j = fadd reassoc double %chk.j, %pair
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ivp = getelementptr inbounds i64, ptr %IdxV, i64 %i
+ %dynidx = load i64, ptr %ivp, align 8
+ %ddyn = getelementptr inbounds double, ptr %D, i64 %dynidx
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %ddyn, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @volatile_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load volatile double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store volatile double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @atomic_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load atomic double, ptr %ddiag monotonic, align 8
+ %dnew = fadd double %dval, 1.000000e+00
+ store atomic double %dnew, ptr %ddiag monotonic, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @fence_epilogue(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ fence seq_cst
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @conditional_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %inner.exit, label %inner.header
+inner.exit:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %odd = and i64 %i, 1
+ %do = icmp eq i64 %odd, 0
+ br i1 %do, label %do.epilogue, label %outer.latch
+do.epilogue:
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @multiple_epilogues(ptr noalias %A, ptr noalias %D, ptr noalias %E,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue.select, label %inner.header
+epilogue.select:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %odd = and i64 %i, 1
+ %choose.first = icmp eq i64 %odd, 0
+ br i1 %choose.first, label %epilogue1, label %epilogue2
+epilogue1:
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+epilogue2:
+ %ediag = getelementptr inbounds [1335 x double], ptr %E, i64 %i, i64 %i
+ %eval = load double, ptr %ediag, align 8
+ %enew = fmul double %eval, 2.000000e+00
+ store double %enew, ptr %ediag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @address_taken_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %E, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue.first, label %inner.header
+epilogue.first:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ store double 1.000000e+00, ptr %dp, align 8
+ br label %epilogue.second
+epilogue.second:
+ %ep = getelementptr inbounds double, ptr %E, i64 %i
+ store double 2.000000e+00, ptr %ep, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @triangular_bounds(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ %i, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1334
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @data_dependent_bounds(ptr noalias %A, ptr noalias %Bounds,
+ ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %boundp = getelementptr inbounds i64, ptr %Bounds, i64 %i
+ %bound = load i64, ptr %boundp, align 8
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, %bound
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ %dv = load double, ptr %dp, align 8
+ %dn = fadd double %dv, 1.000000e+00
+ store double %dn, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @loop_local_outer_bound(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %limit = sub nsw i64 8, %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ store double 1.000000e+00, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %continue = icmp slt i64 %i.next, %limit
+ br i1 %continue, label %outer.header, label %exit
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @noncanonical_latch(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.latch ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.latch ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.ec = icmp eq i64 %j, 1334
+ br i1 %j.ec, label %epilogue, label %inner.latch
+inner.latch:
+ %j.next = add i64 %j, 1
+ br label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @unsupported_metadata(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header, !llvm.loop !0
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @outer_header_freeze(ptr noalias %A, ptr noalias %D,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %choice = freeze i1 poison
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %selected = select i1 %choice, double %av, double 0.000000e+00
+ %chk.next.j = fadd reassoc double %chk.j, %selected
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ %dv = load double, ptr %dp, align 8
+ %dn = fmul double %dv, 1.500000e+00
+ store double %dn, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @invariant_address_n_to_e_flow(
+ ptr noalias %A, ptr noalias %S, ptr noalias %B, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ store double %av, ptr %si, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %sval = load double, ptr %si, align 8
+ %bdbl = fadd reassoc double %sval, %sval
+ %bi = getelementptr inbounds double, ptr %B, i64 %i
+ store double %bdbl, ptr %bi, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @invariant_address_n_to_e_output(
+ ptr noalias %A, ptr noalias %S, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ store double %av, ptr %si, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %si, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+; Generalized raw directions at the selected outer loop. Each function is a
+; valid depth-three candidate chain. N stores from the selected outer header,
+; while the post-inner epilogue E reads the same base. A leading-zero typed GEP keeps
+; the selected-outer IV and root-IV expression in separate subscripts. Both
+; addresses use the same selected-outer IV, so that component is equality.
+; Their distinct root-IV expressions produce the generalized ancestor
+; component.
+define void @direction_le(ptr noalias %A, ptr noalias %Scratch,
+ ptr noalias %R) {
+entry:
+ br label %le.root
+le.root:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %le.root.latch ]
+ br label %le.outer
+le.outer:
+ %i = phi i64 [ 0, %le.root ], [ %i.next, %le.latch ]
+ %n.root = add i64 %k, 10
+ %n.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %n.root
+ store i64 %i, ptr %n.ptr, align 8
+ br label %le.inner
+le.inner:
+ %j = phi i64 [ 0, %le.outer ], [ %j.next, %le.inner ]
+ %scratch.ptr = getelementptr [4 x [4 x i64]], ptr %Scratch, i64 %k, i64 %j, i64 %i
+ store i64 %j, ptr %scratch.ptr, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %le.epilogue, label %le.inner
+le.epilogue:
+ %e.twice = shl i64 %k, 1
+ %e.root = or disjoint i64 %e.twice, 1
+ %e.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %e.root
+ %e.value = load i64, ptr %e.ptr, align 8
+ %r.row = mul i64 %k, 4
+ %r.index = add i64 %r.row, %i
+ %r.ptr = getelementptr i64, ptr %R, i64 %r.index
+ store i64 %e.value, ptr %r.ptr, align 8
+ br label %le.latch
+le.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %le.root.latch, label %le.outer
+le.root.latch:
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 10
+ br i1 %k.done, label %exit, label %le.root
+exit:
+ ret void
+}
+
+define void @direction_ge(ptr noalias %A, ptr noalias %Scratch,
+ ptr noalias %R) {
+entry:
+ br label %ge.root
+ge.root:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %ge.root.latch ]
+ br label %ge.outer
+ge.outer:
+ %i = phi i64 [ 0, %ge.root ], [ %i.next, %ge.latch ]
+ %n.root = mul i64 %k, 6
+ %n.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %n.root
+ store i64 %i, ptr %n.ptr, align 8
+ br label %ge.inner
+ge.inner:
+ %j = phi i64 [ 0, %ge.outer ], [ %j.next, %ge.inner ]
+ %scratch.ptr = getelementptr [4 x [4 x i64]], ptr %Scratch, i64 %k, i64 %j, i64 %i
+ store i64 %j, ptr %scratch.ptr, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %ge.epilogue, label %ge.inner
+ge.epilogue:
+ %e.root = add i64 %k, 60
+ %e.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %e.root
+ %e.value = load i64, ptr %e.ptr, align 8
+ %r.row = mul i64 %k, 4
+ %r.index = add i64 %r.row, %i
+ %r.ptr = getelementptr i64, ptr %R, i64 %r.index
+ store i64 %e.value, ptr %r.ptr, align 8
+ br label %ge.latch
+ge.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %ge.root.latch, label %ge.outer
+ge.root.latch:
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 13
+ br i1 %k.done, label %exit, label %ge.root
+exit:
+ ret void
+}
+
+define void @direction_ne(ptr noalias %A, ptr noalias %Scratch,
+ ptr noalias %R) {
+entry:
+ br label %ne.root
+ne.root:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %ne.root.latch ]
+ br label %ne.outer
+ne.outer:
+ %i = phi i64 [ 0, %ne.root ], [ %i.next, %ne.latch ]
+ %n.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %k
+ store i64 %i, ptr %n.ptr, align 8
+ br label %ne.inner
+ne.inner:
+ %j = phi i64 [ 0, %ne.outer ], [ %j.next, %ne.inner ]
+ %scratch.ptr = getelementptr [4 x [4 x i64]], ptr %Scratch, i64 %k, i64 %j, i64 %i
+ store i64 %j, ptr %scratch.ptr, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %ne.epilogue, label %ne.inner
+ne.epilogue:
+ %e.root = sub i64 5, %k
+ %e.ptr = getelementptr [4 x [128 x i64]], ptr %A, i64 0, i64 %i, i64 %e.root
+ %e.value = load i64, ptr %e.ptr, align 8
+ %r.row = mul i64 %k, 4
+ %r.index = add i64 %r.row, %i
+ %r.ptr = getelementptr i64, ptr %R, i64 %r.index
+ store i64 %e.value, ptr %r.ptr, align 8
+ br label %ne.latch
+ne.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %ne.root.latch, label %ne.outer
+ne.root.latch:
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 4
+ br i1 %k.done, label %exit, label %ne.root
+exit:
+ ret void
+}
+
+attributes #0 = { convergent nounwind }
+attributes #1 = { strictfp }
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.mustprogress"}
+
+define void @e_data_from_shared_preheader_freeze(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ %bad = freeze i64 %i
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %bad, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+define void @e_data_from_inner_preheader_load(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+
+inner.ph:
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ %bad = load i64, ptr %L, align 8
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %bad, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+define void @e_control_from_shared_preheader(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ %limit.copy = add i64 3, 1
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %r.iv.2, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit.copy
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+define void @e_control_from_inner_preheader(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+
+inner.ph:
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ %limit.copy = add i64 3, 1
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %r.iv.2, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit.copy
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+declare token @llvm.experimental.convergence.entry()
+declare token @llvm.experimental.convergence.loop()
+declare i32 @fission_plain_seed(i64) memory(none) nounwind willreturn
+declare i32 @fission_convergent_seed(i64) convergent memory(none) nounwind willreturn
+
+; The invalid-leaf twin of e_operand_closure_dag: the same operand chain with
+; a loaded leaf, so the walk records part of the chain and then stops.
+define void @e_operand_closure_invalid_leaf(
+ ptr noalias %A, ptr noalias %D, ptr noalias %K, i64 %bias) {
+entry:
+ br label %outer.ph
+
+outer.ph:
+ %seed = freeze i64 %bias
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %a = add i64 %i, %seed
+ %b = mul i64 %a, 3
+ %c = add i64 %b, %b
+ %k1 = load i64, ptr %K, align 8
+ %k2 = mul i64 %k1, %k1
+ %d = add i64 %c, %k2
+ %e = sub i64 %d, %a
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ store i64 %j, ptr %ap, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %e, ptr %dp, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; The controlled convergent call sits inside the nest, whose outer header
+; also serves as the inner preheader.
+define void @n_convergent_controlled_in_nest(ptr noalias %A, ptr noalias %D) convergent {
+entry:
+ %entrytok = call token @llvm.experimental.convergence.entry()
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %outertok = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %entrytok) ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %innertok = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %outertok) ]
+ %v = call i32 @fission_convergent_seed(i64 %j) [ "convergencectrl"(token %innertok) ]
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ ret void
+}
+
+; The uncontrolled negatives isolate the outer header alone, a block inside
+; Inner, and a retained block outside Inner but inside Outer.
+define void @n_convergent_uncontrolled_in_outer_header(
+ ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %v = call i32 @fission_convergent_seed(i64 %i)
+ br label %inner.ph
+
+inner.ph:
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+define void @n_convergent_uncontrolled_in_inner_body(
+ ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+
+inner.ph:
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %v = call i32 @fission_convergent_seed(i64 %j)
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+define void @n_convergent_uncontrolled_in_inner_ph(
+ ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+
+inner.ph:
+ %v = call i32 @fission_convergent_seed(i64 %i)
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; Raw LE, GE, and NE between N and E at the selected outer loop.
+define void @direction_selected_le(ptr noalias %A, ptr noalias %B) {
+entry:
+ br label %outer.header
+
+outer.header: ; preds = %outer.latch, %entry
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header: ; preds = %inner.header, %outer.header
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %n.index = add i64 %i, 10
+ %np = getelementptr inbounds i64, ptr %A, i64 %n.index
+ %nv = load i64, ptr %np, align 8
+ %bp = getelementptr inbounds [32 x i64], ptr %B, i64 %j, i64 %i
+ store i64 %nv, ptr %bp, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue: ; preds = %inner.header
+ %e.twice = shl i64 %i, 1
+ %previous = or disjoint i64 %e.twice, 1
+ %ep = getelementptr inbounds i64, ptr %A, i64 %previous
+ store i64 0, ptr %ep, align 8
+ br label %outer.latch
+
+outer.latch: ; preds = %epilogue
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 10
+ br i1 %i.done, label %exit, label %outer.header
+
+exit: ; preds = %outer.latch
+ ret void
+}
+
+define void @direction_selected_ge(ptr noalias %A, ptr noalias %B) {
+entry:
+ br label %outer.header
+
+outer.header: ; preds = %outer.latch, %entry
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header: ; preds = %inner.header, %outer.header
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %n.index = mul i64 %i, 6
+ %np = getelementptr inbounds i64, ptr %A, i64 %n.index
+ %nv = load i64, ptr %np, align 8
+ %bp = getelementptr inbounds [32 x i64], ptr %B, i64 %j, i64 %i
+ store i64 %nv, ptr %bp, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue: ; preds = %inner.header
+ %previous = add i64 %i, 60
+ %ep = getelementptr inbounds i64, ptr %A, i64 %previous
+ store i64 0, ptr %ep, align 8
+ br label %outer.latch
+
+outer.latch: ; preds = %epilogue
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 13
+ br i1 %i.done, label %exit, label %outer.header
+
+exit: ; preds = %outer.latch
+ ret void
+}
+
+define void @direction_selected_ne(ptr noalias %A, ptr noalias %B) {
+entry:
+ br label %outer.header
+
+outer.header: ; preds = %outer.latch, %entry
+ %i = phi i64 [ 1, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header: ; preds = %inner.header, %outer.header
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %np = getelementptr inbounds i64, ptr %A, i64 %i
+ %nv = load i64, ptr %np, align 8
+ %bp = getelementptr inbounds [8 x i64], ptr %B, i64 %j, i64 %i
+ store i64 %nv, ptr %bp, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue: ; preds = %inner.header
+ %previous = sub nuw nsw i64 5, %i
+ %ep = getelementptr inbounds i64, ptr %A, i64 %previous
+ store i64 0, ptr %ep, align 8
+ br label %outer.latch
+
+outer.latch: ; preds = %epilogue
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 5
+ br i1 %i.done, label %exit, label %outer.header
+
+exit: ; preds = %outer.latch
+ ret void
+}
+
+; The alias pair. Only the unknown-alias nest loses every direction.
+define void @unknown_alias_in_nest(ptr %A, ptr %B, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [8 x i64], ptr %A, i64 %j, i64 %i
+ %bp = getelementptr [8 x i64], ptr %B, i64 %j, i64 %i
+ %v = load i64, ptr %ap, align 8
+ store i64 %v, ptr %bp, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %i, ptr %dp, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+define void @noalias_in_nest(ptr noalias %A, ptr %B, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [8 x i64], ptr %A, i64 %j, i64 %i
+ %bp = getelementptr [8 x i64], ptr %B, i64 %j, i64 %i
+ %v = load i64, ptr %ap, align 8
+ store i64 %v, ptr %bp, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %i, ptr %dp, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; A retained nest with four memory streams under a long scalar epilogue.
+define void @matrix_memory_ratio(ptr noalias %A, ptr noalias %B,
+ ptr noalias %C, ptr noalias %D,
+ ptr noalias %Out, i64 %seed) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [8 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %bp = getelementptr [8 x i64], ptr %B, i64 %j, i64 %i
+ %bv = load i64, ptr %bp, align 8
+ %bn = add i64 %bv, 1
+ store i64 %bn, ptr %bp, align 8
+ %cp = getelementptr [8 x i64], ptr %C, i64 %j, i64 %i
+ %cv = load i64, ptr %cp, align 8
+ %cn = add i64 %cv, 1
+ store i64 %cn, ptr %cp, align 8
+ %dp = getelementptr [8 x i64], ptr %D, i64 %j, i64 %i
+ %dv = load i64, ptr %dp, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %dp, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %v01 = add i64 %seed, %i
+ %v02 = add i64 %v01, 1
+ %v03 = add i64 %v02, 1
+ %v04 = add i64 %v03, 1
+ %v05 = add i64 %v04, 1
+ %v06 = add i64 %v05, 1
+ %v07 = add i64 %v06, 1
+ %v08 = add i64 %v07, 1
+ %v09 = add i64 %v08, 1
+ %v10 = add i64 %v09, 1
+ %v11 = add i64 %v10, 1
+ %v12 = add i64 %v11, 1
+ %v13 = add i64 %v12, 1
+ %v14 = add i64 %v13, 1
+ %v15 = add i64 %v14, 1
+ %v16 = add i64 %v15, 1
+ %v17 = add i64 %v16, 1
+ %v18 = add i64 %v17, 1
+ %v19 = add i64 %v18, 1
+ %v20 = add i64 %v19, 1
+ %v21 = add i64 %v20, 1
+ %v22 = add i64 %v21, 1
+ %v23 = add i64 %v22, 1
+ %v24 = add i64 %v23, 1
+ %v25 = add i64 %v24, 1
+ %v26 = add i64 %v25, 1
+ %v27 = add i64 %v26, 1
+ %v28 = add i64 %v27, 1
+ %v29 = add i64 %v28, 1
+ %v30 = add i64 %v29, 1
+ %v31 = add i64 %v30, 1
+ %v32 = add i64 %v31, 1
+ %v33 = add i64 %v32, 1
+ %v34 = add i64 %v33, 1
+ %v35 = add i64 %v34, 1
+ %v36 = add i64 %v35, 1
+ %v37 = add i64 %v36, 1
+ %v38 = add i64 %v37, 1
+ %v39 = add i64 %v38, 1
+ %v40 = add i64 %v39, 1
+ %v41 = add i64 %v40, 1
+ %v42 = add i64 %v41, 1
+ %v43 = add i64 %v42, 1
+ %v44 = add i64 %v43, 1
+ %v45 = add i64 %v44, 1
+ %v46 = add i64 %v45, 1
+ %v47 = add i64 %v46, 1
+ %v48 = add i64 %v47, 1
+ %v49 = add i64 %v48, 1
+ %v50 = add i64 %v49, 1
+ %v51 = add i64 %v50, 1
+ %v52 = add i64 %v51, 1
+ %v53 = add i64 %v52, 1
+ %v54 = add i64 %v53, 1
+ %v55 = add i64 %v54, 1
+ %v56 = add i64 %v55, 1
+ %v57 = add i64 %v56, 1
+ %v58 = add i64 %v57, 1
+ %v59 = add i64 %v58, 1
+ %v60 = add i64 %v59, 1
+ %v61 = add i64 %v60, 1
+ %v62 = add i64 %v61, 1
+ %v63 = add i64 %v62, 1
+ %v64 = add i64 %v63, 1
+ %outp = getelementptr i64, ptr %Out, i64 %i
+ store i64 %v64, ptr %outp, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; Loop-simplified transcription of the reproducer in
+; https://github.com/llvm/llvm-project/issues/47457 (Bugzilla 48113). The
+; epilogue increments an external global through a pointer and conditionally
+; loads through an external pointer, and the internal counter is
+; register-promoted into the outer induction variable.
+ at a = global i8 0, align 1
+ at d = global i8 0, align 1
+ at b = global [1 x [2 x i32]] zeroinitializer, align 4
+ at c = global [1 x [9 x i8]] zeroinitializer, align 1
+ at e = global ptr null, align 8
+ at f = internal global i32 0, align 4
+ at g = global i16 0, align 2
+
+define void @issue47457_guarded_epilogue() {
+entry:
+ %f.initial = load i32, ptr @f, align 4
+ %f.guard = icmp ult i32 %f.initial, 3
+ br i1 %f.guard, label %outer.ph, label %exit
+
+outer.ph:
+ br label %outer.header
+
+outer.header:
+ %f.iv = phi i32 [ %f.initial, %outer.ph ], [ %f.next, %outer.latch ]
+ %f.idx = zext i32 %f.iv to i64
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %cp = getelementptr inbounds [9 x i8], ptr @c, i64 %j, i64 %f.idx
+ %cv = load i8, ptr %cp, align 1
+ %cv.ext = sext i8 %cv to i32
+ %bp = getelementptr inbounds [2 x i32], ptr @b, i64 %j, i64 1
+ store i32 %cv.ext, ptr %bp, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 3
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %d.old = load i8, ptr @d, align 1
+ %d.new = add i8 %d.old, 1
+ store i8 %d.new, ptr @d, align 1
+ %a.val = load i8, ptr @a, align 1
+ %a.set = icmp ne i8 %a.val, 0
+ br i1 %a.set, label %if.then, label %outer.latch
+
+if.then:
+ %e.ptr = load ptr, ptr @e, align 8
+ %e.val = load i8, ptr %e.ptr, align 1
+ %e.ext = sext i8 %e.val to i16
+ store i16 %e.ext, ptr @g, align 2
+ br label %outer.latch
+
+outer.latch:
+ %f.next = add nuw nsw i32 %f.iv, 1
+ %f.done = icmp eq i32 %f.next, 3
+ br i1 %f.done, label %outer.exit, label %outer.header
+
+outer.exit:
+ %f.final = phi i32 [ %f.next, %outer.latch ]
+ store i32 %f.final, ptr @f, align 4
+ br label %exit
+
+exit:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
new file mode 100644
index 00000000000000..5f6f21cdcde1c8
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
@@ -0,0 +1,1404 @@
+; NOTE: Do not autogenerate
+;
+; Candidate routing: which loop pair of a function's collected chains the
+; feature prepares, in which order, under which candidate budget, nest-depth
+; limits, and profitability policy, and how the remaining pairs reach ordinary
+; interchange. The routing cases are numbered r1 to r16; each function's
+; comment states its case.
+;
+; With the option off the pass leaves these functions unchanged; the others
+; are interchanged by the ordinary path, so %t.default is the reference.
+; RUN: llvm-extract -S %s -o %t.unchanged.ll -func=r1_r2_length3_simple_ancestor -func=r3_length3_nonsimple_ancestor -func=r4_overdepth_tail_candidate -func=r4_uncomputable_ancestor -func=r6_runtime_then_static -func=r7_rejected_then_static -func=r8_ineligible_then_static -func=r10_r11_budget_slot_sequence -func=r12_two_static_collected_order -func=r13_explicit_cache_nested_chain -func=r14_strict_fp -func=r15_nested_scalar_all -func=top_level_middle_epilogue_order
+; RUN: opt -S -passes=no-op-loopnest %t.unchanged.ll -o %t.unchanged.noop
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %t.unchanged.ll -o %t.unchanged.out
+; RUN: diff -u %t.unchanged.noop %t.unchanged.out
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %s -o %t.default
+;
+; The profitability policy is fixed so that the small synthetic kernels are
+; judged by instruction order rather than by the cache model.
+; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
+; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
+; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+; DEFINE: %{prep_checks} = --implicit-check-not='loop-interchange:'
+;
+; Preparation is analysis-only: a complete plan is printed and then discarded,
+; so every function keeps its ordinary result. PREP-COMMON holds the decisions
+; every configuration makes; PREP-LATER those reachable with at least two
+; candidate slots; CHAIN3 those needing an eligible length-three chain; PREP
+; and BUDGET2 distinguish reaching the third attempt from stopping at two.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.prep.stderr %{prep_checks}
+; RUN: diff -u %t.default %t.prep
+;
+; A zero candidate budget does no feature work. One slot stops after a
+; rejection or a runtime plan but skips an ineligible chain for free. An
+; exhausted budget never suppresses ordinary routing.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=0 -loop-interchange-outer-epilogue-fission=false %{remarks} %s -o %t.budget0.off 2> %t.budget0.off.stderr
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=0 %{prepare} %{remarks} %s -o %t.budget0.on 2> %t.budget0.on.stderr
+; RUN: diff -u %t.default %t.budget0.off
+; RUN: diff -u %t.budget0.off %t.budget0.on
+; RUN: diff -u %t.budget0.off.stderr %t.budget0.on.stderr
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=1 -loop-interchange-outer-epilogue-fission=false %s -o %t.budget1.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=1 %{prepare} %{remarks} %s -o %t.budget1.on 2> %t.budget1.stderr
+; RUN: diff -u %t.default %t.budget1.off
+; RUN: diff -u %t.budget1.off %t.budget1.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,CHAIN3,BUDGET1 --input-file=%t.budget1.stderr %{prep_checks}
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=2 -loop-interchange-outer-epilogue-fission=false %s -o %t.budget2.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=2 %{prepare} %{remarks} %s -o %t.budget2.on 2> %t.budget2.stderr
+; RUN: diff -u %t.default %t.budget2.off
+; RUN: diff -u %t.budget2.off %t.budget2.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,BUDGET2 --input-file=%t.budget2.stderr %{prep_checks}
+;
+; Chain depth and logical pair depth are separate gates. At [2,2], r1 is
+; skipped while nested length-two chains still prepare. At [2,3], r1 prepares.
+; At [3,3], r1 reaches ordinary legality but its depth-two fission pair is
+; ineligible, so the feature-on and feature-off diagnostics agree.
+; Raising the maximum to eleven reaches r4's absolute-prefix rejection.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=2 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth22.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=2 %{prepare} %{remarks} %s -o %t.depth22.on 2> %t.depth22.stderr
+; RUN: diff -u %t.depth22.off %t.depth22.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,PREP --input-file=%t.depth22.stderr %{prep_checks}
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=3 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth23.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=3 %{prepare} %{remarks} %s -o %t.depth23.on 2> %t.depth23.stderr
+; RUN: diff -u %t.depth23.off %t.depth23.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.depth23.stderr %{prep_checks}
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=3 -loop-interchange-max-loop-nest-depth=3 -loop-interchange-outer-epilogue-fission=false %{remarks} %s -o %t.depth33.off 2> %t.depth33.off.stderr
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=3 -loop-interchange-max-loop-nest-depth=3 %{prepare} %{remarks} %s -o %t.depth33.on 2> %t.depth33.on.stderr
+; RUN: diff -u %t.depth33.off %t.depth33.on
+; RUN: diff -u %t.depth33.off.stderr %t.depth33.on.stderr
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=11 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth211.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=11 %{prepare} %{remarks} %s -o %t.depth211.on 2> %t.depth211.stderr
+; RUN: diff -u %t.depth211.off %t.depth211.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP,DEPTH11 --input-file=%t.depth211.stderr %{prep_checks}
+;
+; Cache uses the collected-chain root. r1 and the top-level middle pair remain
+; positive; the nested cache candidates abstain. The diagnostic's existing
+; "default profitability" wording is also used for an explicit cache policy.
+; Ignore bypasses cost, not strict-FP or dependence legality. It also makes
+; both ordinary r9 chains interchange; do not impose the default r9 shape.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=cache -loop-interchange-outer-epilogue-fission=false %s -o %t.cache.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=cache %{prepare} %{remarks} %s -o %t.cache.on 2> %t.cache.stderr
+; RUN: diff -u %t.cache.off %t.cache.on
+; RUN: FileCheck %s --check-prefixes=CHAIN3,CACHE --input-file=%t.cache.stderr %{prep_checks}
+; RUN: FileCheck %s --check-prefix=CACHE-IR --input-file=%t.cache.on
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=ignore -loop-interchange-outer-epilogue-fission=false %s -o %t.ignore.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=ignore %{prepare} %{remarks} %s -o %t.ignore.on 2> %t.ignore.stderr
+; RUN: diff -u %t.ignore.off %t.ignore.on
+; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.ignore.stderr %{prep_checks}
+; RUN: FileCheck %s --check-prefix=IGNORE --input-file=%t.ignore.stderr
+; RUN: FileCheck %s --check-prefix=IGNORE-IR --input-file=%t.ignore.on
+;
+; Assertions-only traces distinguish the two matrix domains (absolute
+; ancestry versus the collected chain) and place each failure before or after
+; the speculative matrix calls.
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r1_r2_length3_simple_ancestor %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=MATRIX3 %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r3_length3_nonsimple_ancestor %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=MATRIX-REJECT --implicit-check-not='loop-interchange: prepared function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r15_nested_scalar_all %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=MATRIX-NESTED %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r16_depth3_nonempty_fission_row %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=MATRIX-R16 %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r6_runtime_then_static %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SCAN-RUNTIME --implicit-check-not='loop-interchange: prepared a complete' --implicit-check-not='loop-interchange: rejected function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r12_two_static_collected_order %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SCAN-ORDER --implicit-check-not='loop-interchange: discovered a closed' --implicit-check-not='loop-interchange: prepared a complete' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r14_strict_fp %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=STRICT --implicit-check-not='loop-interchange: prepared function=' %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r4_uncomputable_ancestor %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=COMPUTE-SKIP %{prep_checks} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r4_overdepth_tail_candidate %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=DEPTH-SKIP %{prep_checks} %}
+; RUN: %if asserts %{ llvm-extract -S --recursive --keep-const-init --func=r8_ineligible_then_static %s -o - | opt -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -debug-only=loop-interchange -disable-output 2>&1 | FileCheck %s --check-prefix=SCAN-SKIP --implicit-check-not="Couldn't compute backedge count" %}
+;
+; Every feature line per function is listed; the implicit exclusion rejects
+; any other, including a second selected static plan or a runtime decline
+; when a later static plan exists.
+; CHAIN3-LABEL: loop-interchange: prepared function=r1_r2_length3_simple_ancestor{{ }}
+; CHAIN3-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; CHAIN3-SAME: absolute-depth=3 routing-depth=3 fission-rows=0 routing-rows=1 cross-deps=1 reductions=2
+; CHAIN3-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; CHAIN3-SAME: requirement-ids=[[R1_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R1_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-LABEL: loop-interchange: rejected function=r9_no_static_two_ordinary{{ }}
+; PREP-COMMON-SAME: outer=for.j.header inner=for.r.header reason=post-inner region has no material epilogue memory slice{{$}}
+; PREP-LATER-NEXT: loop-interchange: rejected function=r9_no_static_two_ordinary outer=for.k.header inner=for.l.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r9_no_static_two_ordinary{{ }}
+; CACHE-SAME: outer=for.j.header inner=for.r.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CACHE-NEXT: loop-interchange: rejected function=r9_no_static_two_ordinary outer=for.k.header inner=for.l.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CACHE-NEXT: remark: <unknown>:0:0: Insufficient information to calculate the cost of loop for interchange.
+; CACHE-NEXT: remark: <unknown>:0:0: Insufficient information to calculate the cost of loop for interchange.
+;
+; PREP-COMMON-LABEL: loop-interchange: rejected function=r10_r11_budget_slot_sequence{{ }}
+; PREP-COMMON-SAME: outer=reject.outer inner=reject.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; PREP-LABEL: loop-interchange: prepared function=r10_r11_budget_slot_sequence{{ }}
+; PREP-SAME: outer=static.outer inner=static.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[R10_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R10_ID]]:object-containment/static/by-constant-max{{$}}
+; BUDGET2-LABEL: loop-interchange: prepared function=r10_r11_budget_slot_sequence{{ }}
+; BUDGET2-SAME: outer=runtime.outer inner=runtime.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; BUDGET2-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; BUDGET2-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+; BUDGET2-SAME: requirement-ids=[[B2_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[B2_ID]]:object-containment/static/by-constant-max{{$}}
+; BUDGET2-NEXT: loop-interchange: rejected function=r10_r11_budget_slot_sequence outer=runtime.outer inner=runtime.inner reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; BUDGET2-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check
+; CACHE-LABEL: loop-interchange: rejected function=r10_r11_budget_slot_sequence{{ }}
+; CACHE-SAME: outer=reject.outer inner=reject.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; CACHE: loop-interchange: rejected function=r10_r11_budget_slot_sequence outer=runtime.outer inner=runtime.inner reason=virtual extracted nest failed existing default profitability{{$}}
+; CACHE: loop-interchange: rejected function=r10_r11_budget_slot_sequence outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; PREP-COMMON-LABEL: loop-interchange: prepared function=r12_two_static_collected_order{{ }}
+; PREP-COMMON-SAME: outer=first.outer inner=first.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-COMMON-SAME: requirement-ids=[[R12_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R12_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r12_two_static_collected_order{{ }}
+; CACHE-SAME: outer=first.outer inner=first.inner reason=virtual extracted nest failed existing default profitability{{$}}
+; CACHE: loop-interchange: rejected function=r12_two_static_collected_order outer=second.outer inner=second.inner reason=virtual extracted nest failed existing default profitability{{$}}
+; PREP-COMMON-LABEL: loop-interchange: prepared function=r13_explicit_cache_nested_chain{{ }}
+; PREP-COMMON-SAME: outer=candidate.outer inner=candidate.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-COMMON-SAME: requirement-ids=[[R13_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R13_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r13_explicit_cache_nested_chain{{ }}
+; CACHE-SAME: outer=candidate.outer inner=candidate.inner reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; PREP-COMMON-LABEL: loop-interchange: rejected function=r14_strict_fp{{ }}
+; PREP-COMMON-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: virtual extracted nest failed existing interchange legality
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Only outer loops with induction or reduction PHI nodes can be interchanged currently.
+; CACHE-LABEL: loop-interchange: rejected function=r14_strict_fp{{ }}
+; CACHE-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality{{$}}
+; PREP-COMMON-LABEL: loop-interchange: prepared function=r15_nested_scalar_all{{ }}
+; PREP-COMMON-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-COMMON-SAME: requirement-ids=[[R15_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R15_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r15_nested_scalar_all{{ }}
+; CACHE-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; unsafe_prefix_then_ordinary declines in discovery. Its all-unknown matrix
+; failure and later success belong to ordinary routing.
+; PREP-COMMON-LABEL: loop-interchange: rejected function=unsafe_prefix_then_ordinary{{ }}
+; PREP-COMMON-SAME: outer=loop.j2.header inner=loop.k2.header reason=post-inner region has no material epilogue memory slice{{$}}
+; PREP-LATER-NEXT: loop-interchange: rejected function=unsafe_prefix_then_ordinary outer=loop.x2.header inner=loop.y2.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=unsafe_prefix_then_ordinary{{ }}
+; CACHE-SAME: outer=loop.j2.header inner=loop.k2.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CACHE-NEXT: loop-interchange: rejected function=unsafe_prefix_then_ordinary outer=loop.x2.header inner=loop.y2.header reason=post-inner region has no material epilogue memory slice{{$}}
+; CHAIN3-LABEL: loop-interchange: rejected function=r3_length3_nonsimple_ancestor{{ }}
+; CHAIN3-SAME: outer=outer.header inner=inner.header reason=routing dependence matrix is unavailable{{$}}
+; CHAIN3-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: routing dependence matrix is unavailable
+; DEPTH11-LABEL: loop-interchange: rejected function=r4_overdepth_tail_candidate{{ }}
+; DEPTH11-SAME: outer=l9.header inner=l10.header reason=virtual extracted nest has unknown surrounding dependence context{{$}}
+; DEPTH11-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: virtual extracted nest has unknown surrounding dependence context
+;
+; r4_uncomputable_ancestor has no attempted pair; the extracted trace below
+; checks the chain-wide exclusion.
+; PREP-COMMON-LABEL: loop-interchange: prepared function=r5_first_static_then_later_ordinary{{ }}
+; PREP-COMMON-SAME: outer=first.outer inner=first.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-COMMON-SAME: requirement-ids=[[R5_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R5_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r5_first_static_then_later_ordinary{{ }}
+; CACHE-SAME: outer=first.outer inner=first.inner reason=virtual extracted nest failed existing default profitability{{$}}
+; CACHE: loop-interchange: rejected function=r5_first_static_then_later_ordinary outer=second.outer inner=second.inner reason=post-inner region has no material epilogue memory slice{{$}}
+;
+; PREP-LATER-LABEL: loop-interchange: prepared function=r6_runtime_then_static{{ }}
+; PREP-LATER-SAME: outer=static.outer inner=static.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-LATER-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-LATER-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-LATER-SAME: requirement-ids=[[R6_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R6_ID]]:object-containment/static/by-constant-max{{$}}
+; BUDGET1-LABEL: loop-interchange: prepared function=r6_runtime_then_static{{ }}
+; BUDGET1-SAME: outer=runtime.outer inner=runtime.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; BUDGET1-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; BUDGET1-SAME: byte-offset-proofs=1 requirements=2 bound=runtime-bound Wmin=4
+; BUDGET1-SAME: requirement-ids=[[B1_ID:[0-9]+]]:modular-outer-span/runtime/runtime,[[B1_ID]]:object-containment/static/by-constant-max{{$}}
+; BUDGET1-NEXT: loop-interchange: rejected function=r6_runtime_then_static outer=runtime.outer inner=runtime.inner reason=recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check{{$}}
+; BUDGET1-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: recovered selected-outer array bound is a runtime value; safe distribution requires a runtime bound check
+; CACHE-LABEL: loop-interchange: rejected function=r6_runtime_then_static{{ }}
+; CACHE-SAME: outer=runtime.outer inner=runtime.inner reason=virtual extracted nest failed existing default profitability{{$}}
+; CACHE: loop-interchange: rejected function=r6_runtime_then_static outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; PREP-COMMON-LABEL: loop-interchange: rejected function=r7_rejected_then_static{{ }}
+; PREP-COMMON-SAME: outer=reject.outer inner=reject.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
+; PREP-LATER-LABEL: loop-interchange: prepared function=r7_rejected_then_static{{ }}
+; PREP-LATER-SAME: outer=static.outer inner=static.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-LATER-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-LATER-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-LATER-SAME: requirement-ids=[[R7_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R7_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r7_rejected_then_static{{ }}
+; CACHE-SAME: outer=reject.outer inner=reject.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
+; CACHE: loop-interchange: rejected function=r7_rejected_then_static outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; PREP-COMMON-LABEL: loop-interchange: prepared function=r8_ineligible_then_static{{ }}
+; PREP-COMMON-SAME: outer=static.outer inner=static.inner path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-COMMON-SAME: requirement-ids=[[R8_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R8_ID]]:object-containment/static/by-constant-max{{$}}
+; CACHE-LABEL: loop-interchange: rejected function=r8_ineligible_then_static{{ }}
+; CACHE-SAME: outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
+;
+; PREP-COMMON-LABEL: loop-interchange: prepared function=top_level_middle_epilogue_order{{ }}
+; PREP-COMMON-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
+; PREP-COMMON-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; PREP-COMMON-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; CACHE-LABEL: loop-interchange: prepared function=top_level_middle_epilogue_order{{ }}
+; CACHE-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
+; CACHE-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; CACHE-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; CACHE-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; CACHE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; CACHE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; The depth-3 positive keeps both speculative matrices nonempty, so its
+; ancestor-prefix proof is not vacuous when it accepts. absolute-depth=3 and
+; routing-depth=3 give the two domains, and fission-rows=1 and routing-rows=1
+; give their sizes. MATRIX-R16 below checks the ancestor column index and the
+; complete printed dependence behind each row.
+; CHAIN3-LABEL: loop-interchange: prepared function=r16_depth3_nonempty_fission_row{{ }}
+; CHAIN3-SAME: outer=l2.header inner=l3.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; CHAIN3-SAME: absolute-depth=3 routing-depth=3 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; CHAIN3-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; CHAIN3-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; CHAIN3-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+;
+; IGNORE-LABEL: loop-interchange: rejected function=r9_no_static_two_ordinary{{ }}
+; IGNORE-SAME: outer=for.j.header inner=for.r.header reason=post-inner region has no material epilogue memory slice{{$}}
+; IGNORE-NEXT: loop-interchange: rejected function=r9_no_static_two_ordinary outer=for.k.header inner=for.l.header reason=post-inner region has no material epilogue memory slice{{$}}
+; IGNORE-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; IGNORE-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; IGNORE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; IGNORE-NEXT: loop-interchange: rejected function=r10_r11_budget_slot_sequence{{ }}
+;
+; Both ordinary interchanges are visible through their PHIs and branches.
+; IGNORE-IR-LABEL: define void @r9_no_static_two_ordinary(
+; IGNORE-IR: for.j.header:
+; IGNORE-IR-NEXT: %j = phi i64 [ %j.next, %for.j.latch ], [ 0, %[[J_PH:[^ ]+]] ]
+; IGNORE-IR: for.r.header:
+; IGNORE-IR-NEXT: %r = phi i64 [ %[[R_NEXT:[^ ,]+]], %[[R_LATCH:[^ ]+]] ], [ 0, %[[R_PH:[^ ]+]] ]
+; IGNORE-IR-NEXT: br label %[[J_PH]]
+; IGNORE-IR: %a.element = getelementptr [64 x i8], ptr %A, i64 %j, i64 %r
+; IGNORE-IR-NEXT: store i8 0, ptr %a.element, align 1
+; IGNORE-IR: [[R_LATCH]]:
+; IGNORE-IR-NEXT: %[[R_NEXT]] = add i64 %r, 1
+; IGNORE-IR: for.k.header:
+; IGNORE-IR-NEXT: %k = phi i64 [ %k.next, %for.k.latch ], [ 0, %[[K_PH:[^ ]+]] ]
+; IGNORE-IR: for.l.header:
+; IGNORE-IR-NEXT: %l = phi i64 [ %[[L_NEXT:[^ ,]+]], %[[L_LATCH:[^ ]+]] ], [ 0, %[[L_PH:[^ ]+]] ]
+; IGNORE-IR-NEXT: br label %[[K_PH]]
+; IGNORE-IR: %b.element = getelementptr [64 x i8], ptr %B, i64 %l, i64 %k
+; IGNORE-IR-NEXT: store i8 0, ptr %b.element, align 1
+; IGNORE-IR: [[L_LATCH]]:
+; IGNORE-IR-NEXT: %[[L_NEXT]] = add i64 %l, 1
+; IGNORE-IR: {{^}}}{{$}}
+;
+; MATRIX3-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r1_r2_length3_simple_ancestor' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX3-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 20 instructions.
+; MATRIX3-NEXT: loop-interchange: flattened byte-offset proof accepted an N/E pair:
+; MATRIX3-NEXT: N: %av = load i64, ptr %aidx, align 8
+; MATRIX3-NEXT: E: store i64 %diag.next, ptr %diag, align 8
+; MATRIX3-NEXT: width = 4, element bytes = 8, bounded modular equality implies equal outer iteration.
+; MATRIX3-NEXT: Found 1 Loads and Stores to analyze
+; MATRIX3-NEXT: Found 4 Loads and Stores to analyze
+; MATRIX3-NEXT: Found output dependency between Src and Dst
+; MATRIX3-NEXT: Src: store i64 %ancestor.value, ptr %AncestorOut, align 8
+; MATRIX3-NEXT: Dst: store i64 %ancestor.value, ptr %AncestorOut, align 8
+; MATRIX3: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r1_r2_length3_simple_ancestor': Outer 'outer.header', Inner 'inner.header', absolute columns 1/2, routing columns 1/2, 1 cross dependence(s).
+; MATRIX3-NEXT: loop-interchange: prepared function=r1_r2_length3_simple_ancestor{{ }}
+; MATRIX3-SAME: rematerialized=0{{ }}
+; MATRIX3: Processing LoopList of size = 3 containing the following loops:
+; MATRIX3: Found 6 Loads and Stores to analyze
+; MATRIX3: Dependency matrix before interchange:
+; MATRIX3-NEXT: {{^}}* I I{{ *$}}
+; MATRIX3-NEXT: {{^}}* * I{{ *$}}
+; MATRIX3-NEXT: {{^}}* = I{{ *$}}
+; MATRIX3-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
+; MATRIX3: Cannot prove legality, not interchanging loops 'root.header' and 'outer.header'
+;
+; MATRIX-REJECT-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r3_length3_nonsimple_ancestor' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-REJECT: width = 4, element bytes = 8, bounded modular equality implies equal outer iteration.
+; MATRIX-REJECT-NEXT: Found 1 Loads and Stores to analyze
+; MATRIX-REJECT-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'r3_length3_nonsimple_ancestor': Outer 'outer.header', Inner 'inner.header': routing dependence matrix is unavailable
+; MATRIX-REJECT-NEXT: loop-interchange: rejected function=r3_length3_nonsimple_ancestor outer=outer.header inner=inner.header reason=routing dependence matrix is unavailable
+; MATRIX-REJECT-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: routing dependence matrix is unavailable
+; MATRIX-REJECT-NEXT: Processing LoopList of size = 3 containing the following loops:
+; MATRIX-REJECT: Populating dependency matrix failed
+;
+; MATRIX-NESTED-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r15_nested_scalar_all' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-NESTED: loop-interchange: flattened byte-offset proof accepted an N/E pair:
+; MATRIX-NESTED-NEXT: N: %dread = load double, ptr %didx, align 8
+; MATRIX-NESTED-NEXT: E: store double %dn, ptr %ddiag, align 8
+; MATRIX-NESTED-NEXT: width = 1335, element bytes = 8, bounded modular equality implies equal outer iteration.
+; MATRIX-NESTED-NEXT: Found 1 Loads and Stores to analyze
+; MATRIX-NESTED-NEXT: Found 1 Loads and Stores to analyze
+; MATRIX-NESTED: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r15_nested_scalar_all': Outer 'outer.header', Inner 'inner.header', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
+; MATRIX-NESTED-NEXT: loop-interchange: prepared function=r15_nested_scalar_all{{ }}
+; MATRIX-NESTED-SAME: rematerialized=0{{ }}
+; MATRIX-NESTED: Processing LoopList of size = 2 containing the following loops:
+; MATRIX-NESTED: Dependency matrix before interchange:
+; MATRIX-NESTED-NEXT: {{^}}* I{{ *$}}
+; MATRIX-NESTED-NEXT: {{^}}= I{{ *$}}
+; MATRIX-NESTED-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; MATRIX-NESTED: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; The two speculative matrices carry independent domains. The plan summary
+; gives the column indices: absolute columns 1/2 puts the selected outer at
+; index 1, so index 0 is the ancestor column that the prefix proof reads, and
+; routing columns 1/2 indexes the collected chain. The printer reports sizes
+; only, so each row is bound here by the complete dependence that produced it.
+; The ordinary depth-3 matrix that follows preparation is anchored as whole
+; rows, and it is a third, independent domain.
+; MATRIX-R16-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r16_depth3_nonempty_fission_row' with 1 path block(s), 2 instruction(s), and 0 rematerialized loop-local definition(s).
+; MATRIX-R16-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 16 instructions.
+; MATRIX-R16-NEXT: Found 2 Loads and Stores to analyze
+; MATRIX-R16-NEXT: Found anti dependency between Src and Dst
+; MATRIX-R16-NEXT: Src: %v = load double, ptr %ap, align 8
+; MATRIX-R16-NEXT: Dst: store double %w, ptr %ap, align 8
+; MATRIX-R16-NEXT: Found 2 Loads and Stores to analyze
+; MATRIX-R16-NEXT: Found anti dependency between Src and Dst
+; MATRIX-R16-NEXT: Src: %v = load double, ptr %ap, align 8
+; MATRIX-R16-NEXT: Dst: store double %w, ptr %ap, align 8
+; MATRIX-R16-NEXT: Checking if loops 'l2.header' and 'l3.header' are tightly nested
+; MATRIX-R16-NEXT: Checking instructions in Loop header and Loop latch
+; MATRIX-R16-NEXT: Loops are perfectly nested
+; MATRIX-R16-NEXT: Cost = -1
+; MATRIX-R16-NEXT: {{^}}loop-interchange: prepared a complete outer-epilogue fission plan in function 'r16_depth3_nonempty_fission_row': Outer 'l2.header', Inner 'l3.header', absolute columns 1/2, routing columns 1/2, 0 cross dependence(s).{{$}}
+; MATRIX-R16-NEXT: loop-interchange: prepared function=r16_depth3_nonempty_fission_row{{ }}
+; MATRIX-R16-SAME: rematerialized=0{{ }}
+; MATRIX-R16-SAME: absolute-depth=3 routing-depth=3 fission-rows=1 routing-rows=1{{ }}
+; MATRIX-R16-NEXT: Processing LoopList of size = 3 containing the following loops:
+; MATRIX-R16: Dependency matrix before interchange:
+; MATRIX-R16-NEXT: {{^}}= = ={{ *$}}
+; MATRIX-R16-NEXT: {{^}}* = I{{ *$}}
+; MATRIX-R16-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
+; MATRIX-R16: Cannot prove legality, not interchanging loops 'l1.header' and 'l2.header'
+;
+; SCAN-RUNTIME-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r6_runtime_then_static'
+; SCAN-RUNTIME: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r6_runtime_then_static': Outer 'runtime.outer', Inner 'runtime.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
+; SCAN-RUNTIME: loop-interchange: discovered a closed outer-loop epilogue in function 'r6_runtime_then_static'
+; SCAN-RUNTIME: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r6_runtime_then_static': Outer 'static.outer', Inner 'static.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
+; SCAN-RUNTIME-NEXT: loop-interchange: prepared function=r6_runtime_then_static outer=static.outer inner=static.inner{{ }}
+; SCAN-RUNTIME-SAME: rematerialized=0{{ }}
+; SCAN-RUNTIME-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SCAN-RUNTIME: Cannot prove legality, not interchanging loops 'runtime.outer' and 'runtime.inner'
+; SCAN-RUNTIME: Cannot prove legality, not interchanging loops 'static.outer' and 'static.inner'
+;
+; SCAN-ORDER-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r12_two_static_collected_order'
+; SCAN-ORDER: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r12_two_static_collected_order': Outer 'first.outer', Inner 'first.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
+; SCAN-ORDER-NEXT: loop-interchange: prepared function=r12_two_static_collected_order outer=first.outer inner=first.inner{{ }}
+; SCAN-ORDER-SAME: rematerialized=0{{ }}
+; SCAN-ORDER-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SCAN-ORDER: Cannot prove legality, not interchanging loops 'first.outer' and 'first.inner'
+; SCAN-ORDER-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SCAN-ORDER: Cannot prove legality, not interchanging loops 'second.outer' and 'second.inner'
+;
+; STRICT-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r14_strict_fp' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
+; STRICT-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 20 instructions.
+; STRICT-NEXT: Found 1 Loads and Stores to analyze
+; STRICT-NEXT: Found 1 Loads and Stores to analyze
+; STRICT-NEXT: Failed to recognize PHI as an induction or reduction.
+; STRICT-NEXT: Failed to find inner loop inductions or found unsupported reductions.
+; STRICT-NEXT: loop-interchange: outer-epilogue preparation rejected candidate in function 'r14_strict_fp': Outer 'outer.header', Inner 'inner.header': virtual extracted nest failed existing interchange legality
+; STRICT-NEXT: loop-interchange: rejected function=r14_strict_fp outer=outer.header inner=inner.header reason=virtual extracted nest failed existing interchange legality
+; STRICT-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: virtual extracted nest failed existing interchange legality
+; STRICT-NEXT: Processing LoopList of size = 2 containing the following loops:
+; STRICT: remark: <unknown>:0:0: Only outer loops with induction or reduction PHI nodes can be interchanged currently.
+; STRICT: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+;
+; COMPUTE-SKIP: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; COMPUTE-SKIP-NEXT: Couldn't compute backedge count
+; COMPUTE-SKIP-NEXT: Couldn't compute backedge count
+; COMPUTE-SKIP-NEXT: Not valid loop candidate for interchange
+; DEPTH-SKIP: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; DEPTH-SKIP-NEXT: Unsupported depth of loop nest 11, the supported range is [2, 10].
+; DEPTH-SKIP-NEXT: remark: <unknown>:0:0: Unsupported depth of loop nest, the supported range is [2, 10].
+;
+; SCAN-SKIP: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
+; SCAN-SKIP-NEXT: Couldn't compute backedge count
+; SCAN-SKIP-NEXT: loop-interchange: discovered a closed outer-loop epilogue in function 'r8_ineligible_then_static'
+; SCAN-SKIP: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r8_ineligible_then_static': Outer 'static.outer', Inner 'static.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
+; SCAN-SKIP-NEXT: loop-interchange: prepared function=r8_ineligible_then_static outer=static.outer inner=static.inner{{ }}
+; SCAN-SKIP-SAME: rematerialized=0{{ }}
+; SCAN-SKIP-NEXT: Couldn't compute backedge count
+; SCAN-SKIP-NEXT: Not valid loop candidate for interchange
+; SCAN-SKIP-NEXT: Processing LoopList of size = 2 containing the following loops:
+; SCAN-SKIP: Cannot prove legality, not interchanging loops 'static.outer' and 'static.inner'
+;
+; CACHE-IR-LABEL: define void @r9_no_static_two_ordinary(
+; CACHE-IR: for.k.header:
+; CACHE-IR: br label %for.l.header
+; CACHE-IR: for.l.header:
+; CACHE-IR: %l = phi i64 [ 0, %for.k.header ], [ %l.next, %for.l.header ]
+; CACHE-IR: {{^}}}{{$}}
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @r1_r2_length3_simple_ancestor(
+ ptr noalias %AncestorIn, ptr noalias %AncestorOut,
+ ptr noalias dereferenceable(128) %A, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ %ancestor.value = load i64, ptr %AncestorIn, align 8
+ store i64 %ancestor.value, ptr %AncestorOut, align 8
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %diag.value = load i64, ptr %diag, align 8
+ %diag.next = add i64 %diag.value, 1
+ store i64 %diag.next, ptr %diag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %outer.header
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r9_no_static_two_ordinary(ptr noalias %A, ptr noalias %B) {
+entry:
+ br label %for.i.header
+for.i.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %for.i.latch ]
+ br label %for.j.header
+for.j.header:
+ %j = phi i64 [ 0, %for.i.header ], [ %j.next, %for.j.latch ]
+ br label %for.r.header
+for.r.header:
+ %r = phi i64 [ 0, %for.j.header ], [ %r.next, %for.r.header ]
+ %a.element = getelementptr [64 x i8], ptr %A, i64 %j, i64 %r
+ store i8 0, ptr %a.element
+ %r.next = add i64 %r, 1
+ %r.done = icmp eq i64 %r.next, 64
+ br i1 %r.done, label %for.j.latch, label %for.r.header
+for.j.latch:
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 64
+ br i1 %j.done, label %for.k.header, label %for.j.header
+for.k.header:
+ %k = phi i64 [ 0, %for.j.latch ], [ %k.next, %for.k.latch ]
+ br label %for.l.header
+for.l.header:
+ %l = phi i64 [ 0, %for.k.header ], [ %l.next, %for.l.header ]
+ %b.element = getelementptr [64 x i8], ptr %B, i64 %l, i64 %k
+ store i8 0, ptr %b.element
+ %l.next = add i64 %l, 1
+ %l.done = icmp eq i64 %l.next, 64
+ br i1 %l.done, label %for.k.latch, label %for.l.header
+for.k.latch:
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 64
+ br i1 %k.done, label %for.i.latch, label %for.k.header
+for.i.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 64
+ br i1 %i.done, label %exit, label %for.i.header
+exit:
+ ret void
+}
+
+define void @r10_r11_budget_slot_sequence(
+ ptr noalias %Limit, ptr noalias %B, ptr noalias %S,
+ ptr noalias %Scratch, ptr noalias dereferenceable(128) %RuntimeA,
+ ptr noalias dereferenceable(128) %StaticA, ptr noalias %R, i64 %n) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %ineligible.outer
+ineligible.outer:
+ %u = phi i64 [ 0, %root.header ], [ %u.next, %ineligible.outer.latch ]
+ br label %ineligible.inner
+ineligible.inner:
+ %v = phi i64 [ 0, %ineligible.outer ], [ %v.next, %ineligible.inner ]
+ %bidx = getelementptr inbounds [4 x i64], ptr %B, i64 %v, i64 %u
+ store i64 %u, ptr %bidx, align 8
+ %v.next = add i64 %v, 1
+ %v.done = icmp eq i64 %v.next, 4
+ br i1 %v.done, label %ineligible.outer.latch, label %ineligible.inner
+ineligible.outer.latch:
+ %limit = load volatile i64, ptr %Limit, align 8
+ %u.next = add i64 %u, 1
+ %u.continue = icmp ult i64 %u.next, %limit
+ br i1 %u.continue, label %ineligible.outer, label %reject.outer
+reject.outer:
+ %ri = phi i64 [ 0, %ineligible.outer.latch ], [ %ri.next, %reject.outer.latch ]
+ %si = getelementptr inbounds i64, ptr %S, i64 %ri
+ br label %reject.inner
+reject.inner:
+ %rj = phi i64 [ 0, %reject.outer ], [ %rj.next, %reject.inner ]
+ %sv = load i64, ptr %si, align 8
+ %scratch = getelementptr inbounds [4 x i64], ptr %Scratch, i64 %rj, i64 %ri
+ store i64 %sv, ptr %scratch, align 8
+ %rj.next = add i64 %rj, 1
+ %rj.done = icmp eq i64 %rj.next, 4
+ br i1 %rj.done, label %reject.epilogue, label %reject.inner
+reject.epilogue:
+ %ri.plus1 = add i64 %ri, 1
+ %si.plus1 = getelementptr inbounds i64, ptr %S, i64 %ri.plus1
+ store i64 %ri, ptr %si.plus1, align 8
+ br label %reject.outer.latch
+reject.outer.latch:
+ %ri.next = add i64 %ri, 1
+ %ri.done = icmp eq i64 %ri.next, 4
+ br i1 %ri.done, label %runtime.outer, label %reject.outer
+runtime.outer:
+ %ti = phi i64 [ 0, %reject.outer.latch ], [ %ti.next, %runtime.outer.latch ]
+ %tsum.i = phi i64 [ 0, %reject.outer.latch ], [ %tsum.next, %runtime.outer.latch ]
+ br label %runtime.inner
+runtime.inner:
+ %tj = phi i64 [ 0, %runtime.outer ], [ %tj.next, %runtime.inner ]
+ %tsum.j = phi i64 [ %tsum.i, %runtime.outer ], [ %tsum.next.j, %runtime.inner ]
+ %tidx = getelementptr inbounds [4 x i64], ptr %RuntimeA, i64 %tj, i64 %ti
+ %tv = load i64, ptr %tidx, align 8
+ %tsum.next.j = add i64 %tsum.j, %tv
+ %tj.next = add i64 %tj, 1
+ %tj.done = icmp eq i64 %tj.next, 4
+ br i1 %tj.done, label %runtime.epilogue, label %runtime.inner
+runtime.epilogue:
+ %tsum.next = phi i64 [ %tsum.next.j, %runtime.inner ]
+ %tdiag = getelementptr inbounds [4 x i64], ptr %RuntimeA, i64 %ti, i64 %ti
+ %tdv = load i64, ptr %tdiag, align 8
+ %tdn = add i64 %tdv, 1
+ store i64 %tdn, ptr %tdiag, align 8
+ br label %runtime.outer.latch
+runtime.outer.latch:
+ %ti.next = add i64 %ti, 1
+ %ti.done = icmp eq i64 %ti.next, %n
+ br i1 %ti.done, label %static.outer, label %runtime.outer
+static.outer:
+ %i = phi i64 [ 0, %runtime.outer.latch ], [ %i.next, %static.outer.latch ]
+ %sum.i = phi i64 [ %tsum.next, %runtime.outer.latch ], [ %sum.next, %static.outer.latch ]
+ br label %static.inner
+static.inner:
+ %j = phi i64 [ 0, %static.outer ], [ %j.next, %static.inner ]
+ %sum.j = phi i64 [ %sum.i, %static.outer ], [ %sum.next.j, %static.inner ]
+ %idx = getelementptr inbounds [4 x i64], ptr %StaticA, i64 %j, i64 %i
+ %value = load i64, ptr %idx, align 8
+ %sum.next.j = add i64 %sum.j, %value
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %static.epilogue, label %static.inner
+static.epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %static.inner ]
+ %diag = getelementptr inbounds [4 x i64], ptr %StaticA, i64 %i, i64 %i
+ %dv = load i64, ptr %diag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %diag, align 8
+ br label %static.outer.latch
+static.outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %static.outer
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %static.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r12_two_static_collected_order(
+ ptr noalias dereferenceable(128) %A0,
+ ptr noalias dereferenceable(128) %A1, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %first.outer
+first.outer:
+ %i0 = phi i64 [ 0, %root.header ], [ %i0.next, %first.outer.latch ]
+ %sum0.i = phi i64 [ 0, %root.header ], [ %sum0.next, %first.outer.latch ]
+ br label %first.inner
+first.inner:
+ %j0 = phi i64 [ 0, %first.outer ], [ %j0.next, %first.inner ]
+ %sum0.j = phi i64 [ %sum0.i, %first.outer ], [ %sum0.next.j, %first.inner ]
+ %idx0 = getelementptr inbounds [4 x i64], ptr %A0, i64 %j0, i64 %i0
+ %value0 = load i64, ptr %idx0, align 8
+ %sum0.next.j = add i64 %sum0.j, %value0
+ %j0.next = add i64 %j0, 1
+ %j0.done = icmp eq i64 %j0.next, 4
+ br i1 %j0.done, label %first.epilogue, label %first.inner
+first.epilogue:
+ %sum0.next = phi i64 [ %sum0.next.j, %first.inner ]
+ %diag0 = getelementptr inbounds [4 x i64], ptr %A0, i64 %i0, i64 %i0
+ %dv0 = load i64, ptr %diag0, align 8
+ %dn0 = add i64 %dv0, 11
+ store i64 %dn0, ptr %diag0, align 8
+ br label %first.outer.latch
+first.outer.latch:
+ %i0.next = add i64 %i0, 1
+ %i0.done = icmp eq i64 %i0.next, 4
+ br i1 %i0.done, label %second.outer, label %first.outer
+second.outer:
+ %i1 = phi i64 [ 0, %first.outer.latch ], [ %i1.next, %second.outer.latch ]
+ %sum1.i = phi i64 [ %sum0.next, %first.outer.latch ], [ %sum1.next, %second.outer.latch ]
+ br label %second.inner
+second.inner:
+ %j1 = phi i64 [ 0, %second.outer ], [ %j1.next, %second.inner ]
+ %sum1.j = phi i64 [ %sum1.i, %second.outer ], [ %sum1.next.j, %second.inner ]
+ %idx1 = getelementptr inbounds [4 x i64], ptr %A1, i64 %j1, i64 %i1
+ %value1 = load i64, ptr %idx1, align 8
+ %sum1.next.j = add i64 %sum1.j, %value1
+ %j1.next = add i64 %j1, 1
+ %j1.done = icmp eq i64 %j1.next, 4
+ br i1 %j1.done, label %second.epilogue, label %second.inner
+second.epilogue:
+ %sum1.next = phi i64 [ %sum1.next.j, %second.inner ]
+ %diag1 = getelementptr inbounds [4 x i64], ptr %A1, i64 %i1, i64 %i1
+ %dv1 = load i64, ptr %diag1, align 8
+ %dn1 = add i64 %dv1, 22
+ store i64 %dn1, ptr %diag1, align 8
+ br label %second.outer.latch
+second.outer.latch:
+ %i1.next = add i64 %i1, 1
+ %i1.done = icmp eq i64 %i1.next, 4
+ br i1 %i1.done, label %root.latch, label %second.outer
+root.latch:
+ %sum.result = phi i64 [ %sum1.next, %second.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r13_explicit_cache_nested_chain(
+ ptr noalias dereferenceable(128) %A, ptr noalias %B, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %candidate.outer
+candidate.outer:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %candidate.outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %candidate.outer.latch ]
+ br label %candidate.inner
+candidate.inner:
+ %j = phi i64 [ 0, %candidate.outer ], [ %j.next, %candidate.inner ]
+ %sum.j = phi i64 [ %sum.i, %candidate.outer ], [ %sum.next.j, %candidate.inner ]
+ %idx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %value = load i64, ptr %idx, align 8
+ %sum.next.j = add i64 %sum.j, %value
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %candidate.epilogue, label %candidate.inner
+candidate.epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %candidate.inner ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %dv = load i64, ptr %diag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %diag, align 8
+ br label %candidate.outer.latch
+candidate.outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %sibling.header, label %candidate.outer
+sibling.header:
+ %s = phi i64 [ 0, %candidate.outer.latch ], [ %s.next, %sibling.header ]
+ %bp = getelementptr inbounds i64, ptr %B, i64 %s
+ store i64 %s, ptr %bp, align 8
+ %s.next = add i64 %s, 1
+ %s.done = icmp eq i64 %s.next, 4
+ br i1 %s.done, label %root.latch, label %sibling.header
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %sibling.header ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r14_strict_fp(ptr noalias %A, ptr noalias %D, ptr noalias %R) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @r15_nested_scalar_all(
+ ptr noalias dereferenceable(14257800) %D, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %root.header ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %didx = getelementptr inbounds [1335 x double], ptr %D, i64 %j, i64 %i
+ %dread = load double, ptr %didx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %dread
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dv = load double, ptr %ddiag, align 8
+ %dn = fadd double %dv, 1.000000e+00
+ store double %dn, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %outer.exit, label %outer.header
+outer.exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ %rk = getelementptr inbounds double, ptr %R, i64 %k
+ store double %chk.res, ptr %rk, align 8
+ br label %sibling.header
+sibling.header:
+ %s = phi i64 [ 0, %outer.exit ], [ %s.next, %sibling.header ]
+ %s.next = add i64 %s, 1
+ %s.ec = icmp eq i64 %s.next, 2
+ br i1 %s.ec, label %root.latch, label %sibling.header
+root.latch:
+ %k.next = add i64 %k, 1
+ %k.ec = icmp eq i64 %k.next, 8
+ br i1 %k.ec, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @unsafe_prefix_then_ordinary(ptr %A, ptr %C) {
+entry:
+ br label %loop.h.header
+loop.h.header:
+ %h = phi i64 [ 0, %entry ], [ %h.next, %loop.h.latch ]
+ br label %loop.i2.header
+loop.i2.header:
+ %i = phi i64 [ 0, %loop.h.header ], [ %i.next, %loop.i2.latch ]
+ br label %loop.j2.header
+loop.j2.header:
+ %j = phi i64 [ 0, %loop.i2.header ], [ %j.next, %loop.j2.latch ]
+ br label %loop.k2.header
+loop.k2.header:
+ %k = phi i64 [ 0, %loop.j2.header ], [ %k.next, %loop.k2.latch ]
+ %c.ptr = getelementptr double, ptr %C, i64 %k
+ %c.val = load double, ptr %c.ptr, align 8
+ %sum = fadd double %c.val, 1.000000e+00
+ %jrow = mul nuw nsw i64 %j, 8
+ %aidx = add nuw nsw i64 %jrow, %k
+ %a.ptr = getelementptr double, ptr %A, i64 %aidx
+ store double %sum, ptr %a.ptr, align 8
+ br label %loop.k2.latch
+loop.k2.latch:
+ %k.next = add nuw nsw i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 8
+ br i1 %k.done, label %loop.j2.latch, label %loop.k2.header
+loop.j2.latch:
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 8
+ br i1 %j.done, label %loop.x2.header, label %loop.j2.header
+loop.x2.header:
+ %x = phi i64 [ 0, %loop.j2.latch ], [ %x.next, %loop.x2.latch ]
+ br label %loop.y2.header
+loop.y2.header:
+ %y = phi i64 [ 0, %loop.x2.header ], [ %y.next, %loop.y2.latch ]
+ %row = mul nuw nsw i64 %y, 8
+ %idx = add nuw nsw i64 %row, %x
+ %axy.ptr = getelementptr double, ptr %A, i64 %idx
+ %old = load double, ptr %axy.ptr, align 8
+ %new = fadd double %old, 2.000000e+00
+ store double %new, ptr %axy.ptr, align 8
+ br label %loop.y2.latch
+loop.y2.latch:
+ %y.next = add nuw nsw i64 %y, 1
+ %y.done = icmp eq i64 %y.next, 8
+ br i1 %y.done, label %loop.x2.latch, label %loop.y2.header
+loop.x2.latch:
+ %x.next = add nuw nsw i64 %x, 1
+ %x.done = icmp eq i64 %x.next, 8
+ br i1 %x.done, label %loop.i2.latch, label %loop.x2.header
+loop.i2.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 8
+ br i1 %i.done, label %loop.h.latch, label %loop.i2.header
+loop.h.latch:
+ %h.next = add nuw nsw i64 %h, 1
+ %h.done = icmp eq i64 %h.next, 8
+ br i1 %h.done, label %exit, label %loop.h.header
+exit:
+ ret void
+}
+
+define void @r3_length3_nonsimple_ancestor(
+ ptr noalias %Ancestor, ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ %ancestor.value = load volatile i64, ptr %Ancestor, align 8
+ store volatile i64 %ancestor.value, ptr %Ancestor, align 8
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %diag.value = load i64, ptr %diag, align 8
+ %diag.next = add i64 %diag.value, 1
+ store i64 %diag.next, ptr %diag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %outer.header
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r4_overdepth_tail_candidate(ptr noalias %A, ptr noalias %E) {
+entry:
+ br label %l0.header
+l0.header:
+ %v0 = phi i64 [ 0, %entry ], [ %v0.next, %l0.latch ]
+ br label %l1.header
+l1.header:
+ %v1 = phi i64 [ 0, %l0.header ], [ %v1.next, %l1.latch ]
+ br label %l2.header
+l2.header:
+ %v2 = phi i64 [ 0, %l1.header ], [ %v2.next, %l2.latch ]
+ br label %l3.header
+l3.header:
+ %v3 = phi i64 [ 0, %l2.header ], [ %v3.next, %l3.latch ]
+ br label %l4.header
+l4.header:
+ %v4 = phi i64 [ 0, %l3.header ], [ %v4.next, %l4.latch ]
+ br label %l5.header
+l5.header:
+ %v5 = phi i64 [ 0, %l4.header ], [ %v5.next, %l5.latch ]
+ br label %l6.header
+l6.header:
+ %v6 = phi i64 [ 0, %l5.header ], [ %v6.next, %l6.latch ]
+ br label %l7.header
+l7.header:
+ %v7 = phi i64 [ 0, %l6.header ], [ %v7.next, %l7.latch ]
+ br label %l8.header
+l8.header:
+ %v8 = phi i64 [ 0, %l7.header ], [ %v8.next, %l8.latch ]
+ br label %l9.header
+l9.header:
+ %v9 = phi i64 [ 0, %l8.header ], [ %v9.next, %l9.latch ]
+ br label %l10.header
+l10.header:
+ %v10 = phi i64 [ 0, %l9.header ], [ %v10.next, %l10.header ]
+ %idx = getelementptr inbounds [4 x i64], ptr %A, i64 %v10, i64 %v9
+ %value = load i64, ptr %idx, align 8
+ store i64 %value, ptr %idx, align 8
+ %v10.next = add i64 %v10, 1
+ %v10.done = icmp eq i64 %v10.next, 4
+ br i1 %v10.done, label %l9.epilogue, label %l10.header
+l9.epilogue:
+ %ep = getelementptr inbounds i64, ptr %E, i64 %v9
+ store i64 %v9, ptr %ep, align 8
+ br label %l9.latch
+l9.latch:
+ %v9.next = add i64 %v9, 1
+ %v9.done = icmp eq i64 %v9.next, 4
+ br i1 %v9.done, label %l8.latch, label %l9.header
+l8.latch:
+ %v8.next = add i64 %v8, 1
+ %v8.done = icmp eq i64 %v8.next, 2
+ br i1 %v8.done, label %l7.latch, label %l8.header
+l7.latch:
+ %v7.next = add i64 %v7, 1
+ %v7.done = icmp eq i64 %v7.next, 2
+ br i1 %v7.done, label %l6.latch, label %l7.header
+l6.latch:
+ %v6.next = add i64 %v6, 1
+ %v6.done = icmp eq i64 %v6.next, 2
+ br i1 %v6.done, label %l5.latch, label %l6.header
+l5.latch:
+ %v5.next = add i64 %v5, 1
+ %v5.done = icmp eq i64 %v5.next, 2
+ br i1 %v5.done, label %l4.latch, label %l5.header
+l4.latch:
+ %v4.next = add i64 %v4, 1
+ %v4.done = icmp eq i64 %v4.next, 2
+ br i1 %v4.done, label %l3.latch, label %l4.header
+l3.latch:
+ %v3.next = add i64 %v3, 1
+ %v3.done = icmp eq i64 %v3.next, 2
+ br i1 %v3.done, label %l2.latch, label %l3.header
+l2.latch:
+ %v2.next = add i64 %v2, 1
+ %v2.done = icmp eq i64 %v2.next, 2
+ br i1 %v2.done, label %l1.latch, label %l2.header
+l1.latch:
+ %v1.next = add i64 %v1, 1
+ %v1.done = icmp eq i64 %v1.next, 2
+ br i1 %v1.done, label %l0.latch, label %l1.header
+l0.latch:
+ %v0.next = add i64 %v0, 1
+ %v0.done = icmp eq i64 %v0.next, 2
+ br i1 %v0.done, label %exit, label %l0.header
+exit:
+ ret void
+}
+
+define void @r4_uncomputable_ancestor(
+ ptr noalias %Limit, ptr noalias dereferenceable(128) %A,
+ ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %diag.value = load i64, ptr %diag, align 8
+ %diag.next = add i64 %diag.value, 1
+ store i64 %diag.next, ptr %diag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %outer.header
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %limit = load volatile i64, ptr %Limit, align 8
+ %k.next = add i64 %k, 1
+ %k.continue = icmp ult i64 %k.next, %limit
+ br i1 %k.continue, label %root.header, label %exit
+exit:
+ ret void
+}
+
+define void @r5_first_static_then_later_ordinary(
+ ptr noalias dereferenceable(128) %A, ptr noalias %B, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %first.outer
+first.outer:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %first.outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %first.outer.latch ]
+ br label %first.inner
+first.inner:
+ %j = phi i64 [ 0, %first.outer ], [ %j.next, %first.inner ]
+ %sum.j = phi i64 [ %sum.i, %first.outer ], [ %sum.next.j, %first.inner ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %first.epilogue, label %first.inner
+first.epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %first.inner ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %dv = load i64, ptr %diag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %diag, align 8
+ br label %first.outer.latch
+first.outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %second.outer, label %first.outer
+second.outer:
+ %x = phi i64 [ 0, %first.outer.latch ], [ %x.next, %second.outer.latch ]
+ br label %second.inner
+second.inner:
+ %y = phi i64 [ 0, %second.outer ], [ %y.next, %second.inner ]
+ %bidx = getelementptr inbounds [4 x i64], ptr %B, i64 %y, i64 %x
+ store i64 %x, ptr %bidx, align 8
+ %y.next = add i64 %y, 1
+ %y.done = icmp eq i64 %y.next, 4
+ br i1 %y.done, label %second.outer.latch, label %second.inner
+second.outer.latch:
+ %x.next = add i64 %x, 1
+ %x.done = icmp eq i64 %x.next, 4
+ br i1 %x.done, label %root.latch, label %second.outer
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %second.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r6_runtime_then_static(
+ ptr noalias dereferenceable(128) %A0,
+ ptr noalias dereferenceable(128) %A1, ptr noalias %R, i64 %n) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %runtime.outer
+runtime.outer:
+ %ri = phi i64 [ 0, %root.header ], [ %ri.next, %runtime.outer.latch ]
+ %rsum.i = phi i64 [ 0, %root.header ], [ %rsum.next, %runtime.outer.latch ]
+ br label %runtime.inner
+runtime.inner:
+ %rj = phi i64 [ 0, %runtime.outer ], [ %rj.next, %runtime.inner ]
+ %rsum.j = phi i64 [ %rsum.i, %runtime.outer ], [ %rsum.next.j, %runtime.inner ]
+ %ridx = getelementptr inbounds [4 x i64], ptr %A0, i64 %rj, i64 %ri
+ %rv = load i64, ptr %ridx, align 8
+ %rsum.next.j = add i64 %rsum.j, %rv
+ %rj.next = add i64 %rj, 1
+ %rj.done = icmp eq i64 %rj.next, 4
+ br i1 %rj.done, label %runtime.epilogue, label %runtime.inner
+runtime.epilogue:
+ %rsum.next = phi i64 [ %rsum.next.j, %runtime.inner ]
+ %rdiag = getelementptr inbounds [4 x i64], ptr %A0, i64 %ri, i64 %ri
+ %rdv = load i64, ptr %rdiag, align 8
+ %rdn = add i64 %rdv, 1
+ store i64 %rdn, ptr %rdiag, align 8
+ br label %runtime.outer.latch
+runtime.outer.latch:
+ %ri.next = add i64 %ri, 1
+ %ri.done = icmp eq i64 %ri.next, %n
+ br i1 %ri.done, label %static.outer, label %runtime.outer
+static.outer:
+ %si = phi i64 [ 0, %runtime.outer.latch ], [ %si.next, %static.outer.latch ]
+ %ssum.i = phi i64 [ %rsum.next, %runtime.outer.latch ], [ %ssum.next, %static.outer.latch ]
+ br label %static.inner
+static.inner:
+ %sj = phi i64 [ 0, %static.outer ], [ %sj.next, %static.inner ]
+ %ssum.j = phi i64 [ %ssum.i, %static.outer ], [ %ssum.next.j, %static.inner ]
+ %sidx = getelementptr inbounds [4 x i64], ptr %A1, i64 %sj, i64 %si
+ %sv = load i64, ptr %sidx, align 8
+ %ssum.next.j = add i64 %ssum.j, %sv
+ %sj.next = add i64 %sj, 1
+ %sj.done = icmp eq i64 %sj.next, 4
+ br i1 %sj.done, label %static.epilogue, label %static.inner
+static.epilogue:
+ %ssum.next = phi i64 [ %ssum.next.j, %static.inner ]
+ %sdiag = getelementptr inbounds [4 x i64], ptr %A1, i64 %si, i64 %si
+ %sdv = load i64, ptr %sdiag, align 8
+ %sdn = add i64 %sdv, 2
+ store i64 %sdn, ptr %sdiag, align 8
+ br label %static.outer.latch
+static.outer.latch:
+ %si.next = add i64 %si, 1
+ %si.done = icmp eq i64 %si.next, 4
+ br i1 %si.done, label %root.latch, label %static.outer
+root.latch:
+ %final.sum = phi i64 [ %ssum.next, %static.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %final.sum, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r7_rejected_then_static(
+ ptr noalias %S, ptr noalias %Scratch,
+ ptr noalias dereferenceable(128) %A, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %reject.outer
+reject.outer:
+ %ri = phi i64 [ 0, %root.header ], [ %ri.next, %reject.outer.latch ]
+ %si = getelementptr inbounds i64, ptr %S, i64 %ri
+ br label %reject.inner
+reject.inner:
+ %rj = phi i64 [ 0, %reject.outer ], [ %rj.next, %reject.inner ]
+ %sv = load i64, ptr %si, align 8
+ %scratch = getelementptr inbounds [4 x i64], ptr %Scratch, i64 %rj, i64 %ri
+ store i64 %sv, ptr %scratch, align 8
+ %rj.next = add i64 %rj, 1
+ %rj.done = icmp eq i64 %rj.next, 4
+ br i1 %rj.done, label %reject.epilogue, label %reject.inner
+reject.epilogue:
+ %ri.plus1 = add i64 %ri, 1
+ %si.plus1 = getelementptr inbounds i64, ptr %S, i64 %ri.plus1
+ store i64 %ri, ptr %si.plus1, align 8
+ br label %reject.outer.latch
+reject.outer.latch:
+ %ri.next = add i64 %ri, 1
+ %ri.done = icmp eq i64 %ri.next, 4
+ br i1 %ri.done, label %static.outer, label %reject.outer
+static.outer:
+ %i = phi i64 [ 0, %reject.outer.latch ], [ %i.next, %static.outer.latch ]
+ %sum.i = phi i64 [ 0, %reject.outer.latch ], [ %sum.next, %static.outer.latch ]
+ br label %static.inner
+static.inner:
+ %j = phi i64 [ 0, %static.outer ], [ %j.next, %static.inner ]
+ %sum.j = phi i64 [ %sum.i, %static.outer ], [ %sum.next.j, %static.inner ]
+ %idx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %v = load i64, ptr %idx, align 8
+ %sum.next.j = add i64 %sum.j, %v
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %static.epilogue, label %static.inner
+static.epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %static.inner ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %dv = load i64, ptr %diag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %diag, align 8
+ br label %static.outer.latch
+static.outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %static.outer
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %static.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @r8_ineligible_then_static(
+ ptr noalias %Limit, ptr noalias %B,
+ ptr noalias dereferenceable(128) %A, ptr noalias %R) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %ineligible.outer
+ineligible.outer:
+ %x = phi i64 [ 0, %root.header ], [ %x.next, %ineligible.outer.latch ]
+ br label %ineligible.inner
+ineligible.inner:
+ %y = phi i64 [ 0, %ineligible.outer ], [ %y.next, %ineligible.inner ]
+ %bidx = getelementptr inbounds [4 x i64], ptr %B, i64 %y, i64 %x
+ store i64 %x, ptr %bidx, align 8
+ %y.next = add i64 %y, 1
+ %y.done = icmp eq i64 %y.next, 4
+ br i1 %y.done, label %ineligible.outer.latch, label %ineligible.inner
+ineligible.outer.latch:
+ %limit = load volatile i64, ptr %Limit, align 8
+ %x.next = add i64 %x, 1
+ %x.continue = icmp ult i64 %x.next, %limit
+ br i1 %x.continue, label %ineligible.outer, label %static.outer
+static.outer:
+ %i = phi i64 [ 0, %ineligible.outer.latch ], [ %i.next, %static.outer.latch ]
+ %sum.i = phi i64 [ 0, %ineligible.outer.latch ], [ %sum.next, %static.outer.latch ]
+ br label %static.inner
+static.inner:
+ %j = phi i64 [ 0, %static.outer ], [ %j.next, %static.inner ]
+ %sum.j = phi i64 [ %sum.i, %static.outer ], [ %sum.next.j, %static.inner ]
+ %idx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %v = load i64, ptr %idx, align 8
+ %sum.next.j = add i64 %sum.j, %v
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %static.epilogue, label %static.inner
+static.epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %static.inner ]
+ %diag = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %i
+ %dv = load i64, ptr %diag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %diag, align 8
+ br label %static.outer.latch
+static.outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %root.latch, label %static.outer
+root.latch:
+ %sum.result = phi i64 [ %sum.next, %static.outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %R, i64 %k
+ store i64 %sum.result, ptr %rk, align 8
+ %k.next = add i64 %k, 1
+ %k.done = icmp eq i64 %k.next, 2
+ br i1 %k.done, label %exit, label %root.header
+exit:
+ ret void
+}
+
+; The entry guard skips the selected pair and both auxiliary siblings.
+; The pair retains the shared pure-data kernel. Each sibling runs once and
+; writes only its own disjoint auxiliary storage.
+define void @top_level_middle_epilogue_order(
+ ptr noalias %A, ptr noalias %D, ptr noalias %L,
+ i64 %bias, i1 %take, ptr noalias %Before, ptr noalias %After) {
+entry:
+ br i1 %take, label %before.ph, label %done
+before.ph:
+ br label %before.header
+before.header:
+ %before.iv = phi i64 [ 0, %before.ph ], [ %before.next, %before.header ]
+ store i64 101, ptr %Before, align 8
+ %before.next = add i64 %before.iv, 1
+ %before.done = icmp eq i64 %before.next, 1
+ br i1 %before.done, label %outer.ph, label %before.header
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %r.iv.2, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %after.ph
+after.ph:
+ br label %after.header
+after.header:
+ %after.iv = phi i64 [ 0, %after.ph ], [ %after.next, %after.header ]
+ store i64 202, ptr %After, align 8
+ %after.next = add i64 %after.iv, 1
+ %after.done = icmp eq i64 %after.next, 1
+ br i1 %after.done, label %done, label %after.header
+done:
+ ret void
+}
+
+; One accepted absolute-depth-3 candidate whose retained nest carries exactly
+; one row in each matrix, so the ancestor-prefix proof is not vacuous.
+define void @r16_depth3_nonempty_fission_row(ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %l1.header
+
+l1.header:
+ %a = phi i64 [ 0, %entry ], [ %a.next, %l1.latch ]
+ br label %l2.header
+
+l2.header:
+ %i = phi i64 [ 0, %l1.header ], [ %i.next, %l2.latch ]
+ br label %l3.header
+
+l3.header:
+ %j = phi i64 [ 0, %l2.header ], [ %j.next, %l3.header ]
+ %ap = getelementptr inbounds [4 x [4 x double]], ptr %A, i64 %a, i64 %j, i64 %i
+ %v = load double, ptr %ap, align 8
+ %w = fadd double %v, 1.000000e+00
+ store double %w, ptr %ap, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %l3.header
+
+epilogue:
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ store double 1.000000e+00, ptr %dp, align 8
+ br label %l2.latch
+
+l2.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %l1.latch, label %l2.header
+
+l1.latch:
+ %a.next = add nuw nsw i64 %a, 1
+ %a.done = icmp eq i64 %a.next, 4
+ br i1 %a.done, label %exit, label %l1.header
+
+exit:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
new file mode 100644
index 00000000000000..80d6a0dfb43074
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
@@ -0,0 +1,273 @@
+; NOTE: Do not autogenerate
+;
+; Kernels whose trip counts are runtime values. A runtime trip count is not by
+; itself a runtime bound: the disjoint-memory kernels need no bound requirement
+; and the same-array kernels discharge theirs from the constant array extent.
+;
+; With the option off the pass leaves this module unchanged.
+; RUN: opt -S -passes=no-op-loopnest %s -o %t.noop
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %s -o %t.out
+; RUN: diff -u %t.noop %t.out
+;
+; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
+; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
+; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+;
+; Preparation is analysis-only: a complete plan is printed and then discarded,
+; so the IR is unchanged with the option on.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
+; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
+; RUN: diff -u %t.out %t.prep
+;
+; Each printed plan has passed both dependence matrices, legality,
+; profitability, and validation. The ordinary miss that follows it closes the
+; decision.
+; PREP-LABEL: loop-interchange: prepared function=checksum_kernel_rect{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=8 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=checksum_kernel_latch{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=0 epilogue-insts=8 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=same_array_diag{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[SAME_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[SAME_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=nested_same_array_diag{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
+; PREP-SAME: requirement-ids=[[NESTED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[NESTED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+;
+; The guarded bypass of the inner loop is rejected at discovery.
+; PREP-LABEL: loop-interchange: rejected function=checksum_kernel_zerotrip{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header reason=exit-to-latch region is not one dominated acyclic path{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @checksum_kernel_rect(ptr noalias %A, i64 %n, ptr noalias %diag,
+ ptr noalias %epcount, ptr noalias %sumout) {
+entry:
+ %n.pos = icmp sgt i64 %n, 0
+ br i1 %n.pos, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %outer.ph ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %ii1 = add i64 %i, 1
+ %dp = getelementptr inbounds i64, ptr %diag, i64 %i
+ %dv = load i64, ptr %dp, align 8
+ %dn = add i64 %dv, %ii1
+ store i64 %dn, ptr %dp, align 8
+ %ecv = load i64, ptr %epcount, align 8
+ %ecn = add i64 %ecv, 1
+ store i64 %ecn, ptr %epcount, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %sum.res = phi i64 [ %sum.next, %outer.latch ]
+ store i64 %sum.res, ptr %sumout, align 8
+ br label %done
+done:
+ ret void
+}
+
+define void @checksum_kernel_latch(ptr noalias %A, i64 %n, ptr %diag,
+ ptr noalias %epcount, ptr %sumout) {
+entry:
+ %n.pos = icmp sgt i64 %n, 0
+ br i1 %n.pos, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %outer.ph ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %outer.latch, label %inner.header
+outer.latch:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %ii1 = add i64 %i, 1
+ %dp = getelementptr inbounds i64, ptr %diag, i64 %i
+ %dv = load i64, ptr %dp, align 8
+ %dn = add i64 %dv, %ii1
+ store i64 %dn, ptr %dp, align 8
+ %ecv = load i64, ptr %epcount, align 8
+ %ecn = add i64 %ecv, 1
+ store i64 %ecn, ptr %epcount, align 8
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %sum.res = phi i64 [ %sum.next, %outer.latch ]
+ store i64 %sum.res, ptr %sumout, align 8
+ br label %done
+done:
+ ret void
+}
+
+define void @same_array_diag(ptr noalias dereferenceable(128) %X,
+ ptr noalias %sumout) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %entry ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %xidx = getelementptr inbounds [4 x i64], ptr %X, i64 %j, i64 %i
+ %xv = load i64, ptr %xidx, align 8
+ %sum.next.j = add i64 %sum.j, %xv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x i64], ptr %X, i64 %i, i64 %i
+ %dv = load i64, ptr %ddiag, align 8
+ %dn = add i64 %dv, 100
+ store i64 %dn, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %sum.res = phi i64 [ %sum.next, %outer.latch ]
+ store i64 %sum.res, ptr %sumout, align 8
+ ret void
+}
+
+define void @nested_same_array_diag(ptr noalias dereferenceable(128) %X,
+ ptr noalias %rowsum) {
+entry:
+ br label %root.header
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %root.header ], [ %sum.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %outer.header ], [ %sum.next.j, %inner.header ]
+ %xidx = getelementptr inbounds [4 x i64], ptr %X, i64 %j, i64 %i
+ %xv = load i64, ptr %xidx, align 8
+ %sum.next.j = add i64 %sum.j, %xv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 4
+ br i1 %j.ec, label %epilogue, label %inner.header
+epilogue:
+ %sum.next = phi i64 [ %sum.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [4 x i64], ptr %X, i64 %i, i64 %i
+ %dv = load i64, ptr %ddiag, align 8
+ %dn = add i64 %dv, 1
+ store i64 %dn, ptr %ddiag, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 4
+ br i1 %i.ec, label %outer.exit, label %outer.header
+outer.exit:
+ %sum.res = phi i64 [ %sum.next, %outer.latch ]
+ %rk = getelementptr inbounds i64, ptr %rowsum, i64 %k
+ %rkv = load i64, ptr %rk, align 8
+ %rksum = add i64 %rkv, %sum.res
+ store i64 %rksum, ptr %rk, align 8
+ br label %sibling.header
+sibling.header:
+ %s = phi i64 [ 0, %outer.exit ], [ %s.next, %sibling.header ]
+ %s.next = add i64 %s, 1
+ %s.ec = icmp eq i64 %s.next, 2
+ br i1 %s.ec, label %root.latch, label %sibling.header
+root.latch:
+ %k.next = add i64 %k, 1
+ %k.ec = icmp eq i64 %k.next, 2
+ br i1 %k.ec, label %exit, label %root.header
+exit:
+ ret void
+}
+
+define void @checksum_kernel_zerotrip(ptr noalias %A, i64 %n, i64 %m,
+ ptr noalias %diag, ptr noalias %epcount,
+ ptr noalias %sumout) {
+entry:
+ %n.pos = icmp sgt i64 %n, 0
+ br i1 %n.pos, label %outer.ph, label %done
+outer.ph:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %sum.i = phi i64 [ 0, %outer.ph ], [ %sum.next, %outer.latch ]
+ %m.pos = icmp sgt i64 %m, 0
+ br i1 %m.pos, label %inner.ph, label %epilogue
+inner.ph:
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %sum.j = phi i64 [ %sum.i, %inner.ph ], [ %sum.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [4 x i64], ptr %A, i64 %i, i64 %j
+ %av = load i64, ptr %aidx, align 8
+ %sum.next.j = add i64 %sum.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, %m
+ br i1 %j.ec, label %inner.exit, label %inner.header
+inner.exit:
+ %sum.inner = phi i64 [ %sum.next.j, %inner.header ]
+ br label %epilogue
+epilogue:
+ %sum.next = phi i64 [ %sum.i, %outer.header ], [ %sum.inner, %inner.exit ]
+ %ii1 = add i64 %i, 1
+ %dp = getelementptr inbounds i64, ptr %diag, i64 %i
+ %dv = load i64, ptr %dp, align 8
+ %dn = add i64 %dv, %ii1
+ store i64 %dn, ptr %dp, align 8
+ %ecv = load i64, ptr %epcount, align 8
+ %ecn = add i64 %ecv, 1
+ store i64 %ecn, ptr %epcount, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, %n
+ br i1 %i.ec, label %exit, label %outer.header
+exit:
+ %sum.res = phi i64 [ %sum.next, %outer.latch ]
+ store i64 %sum.res, ptr %sumout, align 8
+ br label %done
+done:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
new file mode 100644
index 00000000000000..8d7e40b1b8391f
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
@@ -0,0 +1,827 @@
+; NOTE: Do not autogenerate
+;
+; Outer-loop epilogues with static bounds: the motivating checksum shape, the
+; dependence directions between the nest N and the epilogue E, and the operand
+; roles E may consume. Every positive reaches a complete plan; the PREP lines
+; are the printed plans.
+;
+; With the option off the pass leaves this module unchanged.
+; RUN: opt -S -passes=no-op-loopnest %s -o %t.noop
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %s -o %t.out
+; RUN: diff -u %t.noop %t.out
+;
+; The profitability policy is fixed so that the small synthetic kernels are
+; judged by instruction order rather than by the cache model.
+; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
+; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
+; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+;
+; Preparation is analysis-only: a complete plan is printed and then discarded,
+; so the IR is unchanged with the option on.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
+; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
+; RUN: diff -u %t.out %t.prep
+;
+; A zero memory-instruction ratio fails the shared N/E budget before any
+; dependence matrix is built.
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 -loop-interchange-outer-epilogue-fission=false %s -o %t.ratio0.off
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 %{prepare} %{remarks} %s -o %t.ratio0.on 2> %t.ratio0.stderr
+; RUN: diff -u %t.ratio0.off %t.ratio0.on
+; RUN: FileCheck %s --check-prefix=MEMZERO --input-file=%t.ratio0.stderr --implicit-check-not='loop-interchange:'
+;
+; Each printed plan has passed both dependence matrices, legality,
+; profitability, and validation. The ordinary miss that follows it closes the
+; decision.
+; PREP-LABEL: loop-interchange: prepared function=checksum_diagonal_epilogue{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=6
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[CHECKSUM_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[CHECKSUM_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=n_to_e_flow{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[FLOW_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[FLOW_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=n_to_e_anti{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=1
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=n_to_e_output{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=3 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[OUTPUT_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[OUTPUT_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=multi_block_epilogue{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=2 epilogue-insts=8 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=nested_diagonal_epilogue{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
+; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[NESTED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[NESTED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-LABEL: loop-interchange: prepared function=flattened_byte_gep_latch{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=0 epilogue-insts=6 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
+; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
+; PREP-SAME: requirement-ids=[[BYTE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BYTE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+;
+; The shared and distinct pure operand chains include the two loop-local
+; invariant definitions. The freeze outside the outer loop is retained rather
+; than rematerialized. Disjoint N/E memory needs no bound requirement.
+; PREP-LABEL: loop-interchange: prepared function=e_data_from_shared_preheader_pure{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=e_data_from_inner_preheader_pure{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=e_data_from_outer_preheader_invariant{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+;
+; The operand chain records each loop-local definition once, in post-order,
+; including the shared operand and the duplicated operand: a, b, c, k1, k2, d,
+; e.
+; PREP-LABEL: loop-interchange: prepared function=e_operand_closure_dag{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=7
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+;
+; The three convergence controls each reach a complete static plan. Two place
+; a plain call where the paired negative (in the rejections test) places a
+; convergent one; the third keeps its convergent call outside the outer loop.
+; PREP-LABEL: loop-interchange: prepared function=n_nonconvergent_call_control{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=n_nonconvergent_outer_header_control{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-LABEL: loop-interchange: prepared function=n_convergent_call_outside_outer{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+;
+; The raw N/E distance is +1, a decisive LT direction, so this plan needs
+; neither a byte-offset proof nor a bound requirement.
+; PREP-LABEL: loop-interchange: prepared function=raw_lt_n_to_e{{ }}
+; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=3 rematerialized=0
+; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=0
+; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+;
+; MEMZERO-LABEL: loop-interchange: rejected function=checksum_diagonal_epilogue{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; MEMZERO-LABEL: loop-interchange: rejected function=n_to_e_flow{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_to_e_anti{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_to_e_output{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=multi_block_epilogue{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=nested_diagonal_epilogue{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=flattened_byte_gep_latch{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_data_from_shared_preheader_pure{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_data_from_inner_preheader_pure{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=e_data_from_outer_preheader_invariant{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+; MEMZERO-LABEL: loop-interchange: rejected function=e_operand_closure_dag{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_nonconvergent_call_control{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_nonconvergent_outer_header_control{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=n_convergent_call_outside_outer{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-LABEL: loop-interchange: rejected function=raw_lt_n_to_e{{ }}
+; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
+; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
+; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
+;
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+ at byte_common = internal global [14268552 x i8] zeroinitializer, align 8
+
+; The motivating shape, in Fortran (column-major, so the first subscript is
+; the unit-stride one; every array has leading dimension 1335):
+;
+; do i = 1, 1335
+; do j = 1, 1335
+; pchk = pchk + p(i, j)
+; uchk = uchk + u(i, j)
+; vchk = vchk + v(i, j)
+; end do
+; u(i, i) = u(i, i) * 1.5d0
+; end do
+;
+; The three reductions hold the first subscript fixed and step the second, a
+; 10,680-byte stride between consecutive j. The update of u(i, i) after the
+; inner loop makes the nest imperfect. Distributing that update into its own
+; loop and interchanging the remaining nest makes i the inner subscript, so
+; the reads become unit stride.
+; The trip count equals the leading dimension, so the bound is static here.
+define void @checksum_diagonal_epilogue(
+ ptr noalias %PNEW, ptr noalias dereferenceable(14257800) %UNEW,
+ ptr noalias %VNEW, ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %pchk.i = phi double [ 0.000000e+00, %entry ], [ %pchk.next, %outer.latch ]
+ %uchk.i = phi double [ 0.000000e+00, %entry ], [ %uchk.next, %outer.latch ]
+ %vchk.i = phi double [ 0.000000e+00, %entry ], [ %vchk.next, %outer.latch ]
+ %pcol = getelementptr inbounds [1335 x double], ptr %PNEW, i64 0, i64 %i
+ %ucol = getelementptr inbounds [1335 x double], ptr %UNEW, i64 0, i64 %i
+ %vcol = getelementptr inbounds [1335 x double], ptr %VNEW, i64 0, i64 %i
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %pchk.j = phi double [ %pchk.i, %outer.header ], [ %pchk.next.j, %inner.header ]
+ %uchk.j = phi double [ %uchk.i, %outer.header ], [ %uchk.next.j, %inner.header ]
+ %vchk.j = phi double [ %vchk.i, %outer.header ], [ %vchk.next.j, %inner.header ]
+ %pidx = getelementptr inbounds [1335 x double], ptr %pcol, i64 %j, i64 0
+ %pv = load double, ptr %pidx, align 8
+ %pchk.next.j = fadd reassoc double %pchk.j, %pv
+ %uidx = getelementptr inbounds [1335 x double], ptr %ucol, i64 %j, i64 0
+ %uv = load double, ptr %uidx, align 8
+ %uchk.next.j = fadd reassoc double %uchk.j, %uv
+ %vidx = getelementptr inbounds [1335 x double], ptr %vcol, i64 %j, i64 0
+ %vv = load double, ptr %vidx, align 8
+ %vchk.next.j = fadd reassoc double %vchk.j, %vv
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+
+epilogue:
+ %pchk.next = phi double [ %pchk.next.j, %inner.header ]
+ %uchk.next = phi double [ %uchk.next.j, %inner.header ]
+ %vchk.next = phi double [ %vchk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %UNEW, i64 %i, i64 %i
+ %dval = load double, ptr %ddiag, align 8
+ %dnew = fmul double %dval, 1.500000e+00
+ store double %dnew, ptr %ddiag, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header, !prof !0
+
+exit:
+ %pchk.res = phi double [ %pchk.next, %outer.latch ]
+ %uchk.res = phi double [ %uchk.next, %outer.latch ]
+ %vchk.res = phi double [ %vchk.next, %outer.latch ]
+ %r1 = getelementptr inbounds double, ptr %R, i64 1
+ %r2 = getelementptr inbounds double, ptr %R, i64 2
+ store double %pchk.res, ptr %R, align 8
+ store double %uchk.res, ptr %r1, align 8
+ store double %vchk.res, ptr %r2, align 8
+ ret void
+}
+
+define void @n_to_e_flow(
+ ptr noalias %A, ptr noalias dereferenceable(14257800) %S,
+ ptr noalias %B, ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %sidx = getelementptr inbounds [1335 x double], ptr %S, i64 %j, i64 %i
+ store double %av, ptr %sidx, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %seidx = getelementptr inbounds [1335 x double], ptr %S, i64 1334, i64 %i
+ %sval = load double, ptr %seidx, align 8
+ %bdbl = fadd reassoc double %sval, %sval
+ %bi = getelementptr inbounds double, ptr %B, i64 %i
+ store double %bdbl, ptr %bi, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @n_to_e_anti(ptr noalias %A, ptr noalias %S, ptr noalias %B,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %si = getelementptr inbounds double, ptr %S, i64 %i
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %sread = load double, ptr %si, align 8
+ %bidx = getelementptr inbounds [1335 x double], ptr %B, i64 %j, i64 %i
+ store double %sread, ptr %bidx, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %si, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @n_to_e_output(
+ ptr noalias %A, ptr noalias dereferenceable(14257800) %S,
+ ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %sidx = getelementptr inbounds [1335 x double], ptr %S, i64 %j, i64 %i
+ store double %av, ptr %sidx, align 8
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %seidx = getelementptr inbounds [1335 x double], ptr %S, i64 1334, i64 %i
+ %ival = uitofp i64 %i to double
+ store double %ival, ptr %seidx, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @multi_block_epilogue(ptr noalias %A, ptr noalias %D,
+ ptr noalias %E, ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue.first, label %inner.header
+
+epilogue.first:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ %dv = load double, ptr %dp, align 8
+ %dn = fadd double %dv, 1.000000e+00
+ store double %dn, ptr %dp, align 8
+ br label %epilogue.second
+
+epilogue.second:
+ %ep = getelementptr inbounds double, ptr %E, i64 %i
+ %ev = load double, ptr %ep, align 8
+ %en = fadd double %ev, 2.000000e+00
+ store double %en, ptr %ep, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @nested_diagonal_epilogue(
+ ptr noalias dereferenceable(14257800) %D, ptr noalias %R) {
+entry:
+ br label %root.header
+
+root.header:
+ %k = phi i64 [ 0, %entry ], [ %k.next, %root.latch ]
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %root.header ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %root.header ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %didx = getelementptr inbounds [1335 x double], ptr %D, i64 %j, i64 %i
+ %dread = load double, ptr %didx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %dread
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue, label %inner.header
+
+epilogue:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %ddiag = getelementptr inbounds [1335 x double], ptr %D, i64 %i, i64 %i
+ %dv = load double, ptr %ddiag, align 8
+ %dn = fadd double %dv, 1.000000e+00
+ store double %dn, ptr %ddiag, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %outer.exit, label %outer.header
+
+outer.exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ %rk = getelementptr inbounds double, ptr %R, i64 %k
+ store double %chk.res, ptr %rk, align 8
+ br label %sibling.header
+
+sibling.header:
+ %s = phi i64 [ 0, %outer.exit ], [ %s.next, %sibling.header ]
+ %s.next = add i64 %s, 1
+ %s.ec = icmp eq i64 %s.next, 2
+ br i1 %s.ec, label %root.latch, label %sibling.header
+
+root.latch:
+ %k.next = add i64 %k, 1
+ %k.ec = icmp eq i64 %k.next, 8
+ br i1 %k.ec, label %exit, label %root.header
+
+exit:
+ ret void
+}
+
+define void @flattened_byte_gep_latch(ptr noalias %R) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 1, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 1, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %n.row = mul i64 %j, 1335
+ %n.column.bias = add i64 %i, -1336
+ %n.element = add i64 %n.row, %n.column.bias
+ %n.ptr = getelementptr [8 x i8], ptr getelementptr inbounds (i8, ptr @byte_common, i64 64), i64 %n.element
+ %n.val = load double, ptr %n.ptr, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %n.val
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j, 1335
+ br i1 %j.ec, label %outer.latch, label %inner.header
+
+outer.latch:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ %e.byte = mul i64 %i, 10688
+ %e.base = getelementptr i8, ptr getelementptr inbounds (i8, ptr @byte_common, i64 64), i64 %e.byte
+ %e.ptr = getelementptr i8, ptr %e.base, i64 -10688
+ %e.val = load double, ptr %e.ptr, align 8
+ %e.new = fmul double %e.val, 1.500000e+00
+ store double %e.new, ptr %e.ptr, align 8
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+define void @e_data_from_shared_preheader_pure(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %r.iv.2, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+define void @e_data_from_inner_preheader_pure(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = add i64 %bias, 11
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+
+inner.ph:
+ %r.iv.1 = add i64 %i, %seed
+ %r.invariant.1 = add i64 %bias, 17
+ %r.invariant.2 = mul i64 %r.invariant.1, 3
+ %r.iv.2 = add i64 %r.iv.1, %r.invariant.2
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %r.iv.2, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+; The retained operand: the invariant freeze stays in the block that becomes
+; the new outer preheader and E reads it directly rather than through a clone.
+; The epilogue's trip limit is the same retained value as the nest's.
+define void @e_data_from_outer_preheader_invariant(ptr noalias %A, ptr noalias %D,
+ ptr noalias %L, i64 %bias, i1 %take) {
+entry:
+ br i1 %take, label %outer.ph, label %done
+outer.ph:
+ %seed = freeze i64 %bias
+ %limit = add i64 3, 1
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ br label %inner.ph
+inner.ph:
+ %column = getelementptr i64, ptr %A, i64 %i
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %column, i64 %j
+ %av = load i64, ptr %ap, align 8
+ %an = add i64 %av, 1
+ store i64 %an, ptr %ap, align 8
+ %j.next = add i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %seed, ptr %dp, align 8
+ br label %outer.latch
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.done = icmp eq i64 %i.next, %limit
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ br label %done
+done:
+ ret void
+}
+
+
+; E's stored value roots an operand chain with a shared operand, a duplicated
+; operand, and two loop-invariant leaves.
+define void @e_operand_closure_dag(
+ ptr noalias %A, ptr noalias %D, ptr noalias %K, i64 %bias) {
+entry:
+ br label %outer.ph
+
+outer.ph:
+ %seed = freeze i64 %bias
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ %a = add i64 %i, %seed
+ %b = mul i64 %a, 3
+ %c = add i64 %b, %b
+ %k1 = add i64 %bias, 17
+ %k2 = mul i64 %k1, %k1
+ %d = add i64 %c, %k2
+ %e = sub i64 %d, %a
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i64], ptr %A, i64 %j, i64 %i
+ store i64 %j, ptr %ap, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i64, ptr %D, i64 %i
+ store i64 %e, ptr %dp, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; The non-convergent twin of n_convergent_controlled_in_nest (in the
+; rejections test): the same shape and call position with a plain callee.
+define void @n_nonconvergent_call_control(ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %outer.header
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %v = call i32 @fission_plain_seed(i64 %j)
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+exit:
+ ret void
+}
+
+; The non-convergent twin of n_convergent_uncontrolled_in_outer_header. Its
+; distinct inner preheader keeps the outer header out of that role.
+define void @n_nonconvergent_outer_header_control(
+ ptr noalias %A, ptr noalias %D) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %v = call i32 @fission_plain_seed(i64 %i)
+ br label %inner.ph
+
+inner.ph:
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %inner.ph ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; The only convergent call sits in the outer preheader, outside Outer, so the
+; retained nest holds no convergent operation.
+define void @n_convergent_call_outside_outer(
+ ptr noalias %A, ptr noalias %D, i64 %bias) {
+entry:
+ br label %outer.ph
+
+outer.ph:
+ %v = call i32 @fission_convergent_seed(i64 %bias)
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %outer.ph ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %ap = getelementptr [4 x i32], ptr %A, i64 %j, i64 %i
+ store i32 %v, ptr %ap, align 4
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %dp = getelementptr i32, ptr %D, i64 %i
+ store i32 7, ptr %dp, align 4
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 4
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; The raw less-than control for the selected-outer direction negatives.
+define void @raw_lt_n_to_e(ptr noalias %A, ptr noalias %B) {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 1, %entry ], [ %i.next, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %np = getelementptr inbounds i64, ptr %A, i64 %i
+ %nv = load i64, ptr %np, align 8
+ %bp = getelementptr inbounds [8 x i64], ptr %B, i64 %j, i64 %i
+ store i64 %nv, ptr %bp, align 8
+ %j.next = add nuw nsw i64 %j, 1
+ %j.done = icmp eq i64 %j.next, 4
+ br i1 %j.done, label %epilogue, label %inner.header
+
+epilogue:
+ %previous = sub nuw nsw i64 %i, 1
+ %ep = getelementptr inbounds i64, ptr %A, i64 %previous
+ store i64 0, ptr %ep, align 8
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add nuw nsw i64 %i, 1
+ %i.done = icmp eq i64 %i.next, 5
+ br i1 %i.done, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; Callees for the convergence controls. A call result never reaches E.
+declare i32 @fission_plain_seed(i64) memory(none) nounwind willreturn
+declare i32 @fission_convergent_seed(i64) convergent memory(none) nounwind willreturn
+
+!0 = !{!"branch_weights", i32 1, i32 100}
>From 1b611a12421b0c53f4b2809c513072562c9c6bc1 Mon Sep 17 00:00:00 2001
From: "Matt P. Dziubinski" <matt-p.dziubinski at hpe.com>
Date: Tue, 15 Sep 2026 20:58:35 -0500
Subject: [PATCH 3/3] [LoopInterchange] Extract statically bounded outer
epilogues
When the -loop-interchange-outer-epilogue-fission option, which is off
by default, is enabled, LoopInterchange applies a prepared plan whose
trip bound is proved at compile time. It distributes the outer-loop
epilogue into a sibling loop after the nest and interchanges the
remaining nest. A plan that needs a runtime check is declined with a
missed-optimization remark and left to a follow-up. The checksum nest in
SPEC CPU2000 171.swim is such a case, so it stays unchanged here even
though moving the epilogue out and interchanging the nest would turn its
10,680-byte inner stride into unit stride.
The pass validates each plan immediately before mutation so that a
failed validation leaves the IR unchanged. After mutation begins, no
helper can reject the plan and no legality or profitability analysis
runs. The transformation updates LoopInfo, invalidates SCEV, reforms
LCSSA, and moves debug records with their instructions.
Assisted-by: Claude Opus 5, GPT-5.6 Sol, GPT-6 Astra, Claude Fable 5.1.
---
.../lib/Transforms/Scalar/LoopInterchange.cpp | 495 ++++++++++++++-
.../outer-epilogue-fission-bounds.ll | 503 ++++++++++++++-
.../outer-epilogue-fission-debug.ll | 123 ++++
.../outer-epilogue-fission-rejections.ll | 252 +++++++-
.../outer-epilogue-fission-routing.ll | 587 ++++++++++++++++--
.../outer-epilogue-fission-runtime.ll | 180 +++++-
.../LoopInterchange/outer-epilogue-fission.ll | 521 +++++++++++++++-
7 files changed, 2550 insertions(+), 111 deletions(-)
create mode 100644 llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-debug.ll
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 51725eb0b72130..f17f066caf4a33 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -25,6 +25,7 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/DependenceAnalysis.h"
+#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/LoopCacheAnalysis.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopNestAnalysis.h"
@@ -58,6 +59,7 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
@@ -75,6 +77,9 @@ using namespace llvm;
#define DEBUG_TYPE "loop-interchange"
STATISTIC(LoopsInterchanged, "Number of loops interchanged");
+STATISTIC(OuterEpiloguesDistributed,
+ "Number of outer-loop epilogues distributed into their own loop "
+ "before interchange");
static cl::opt<int> LoopInterchangeCostThreshold(
"loop-interchange-threshold", cl::init(0), cl::Hidden,
@@ -2581,19 +2586,495 @@ struct LoopInterchange {
return FirstRuntime;
}
- bool run(LoopNest &LN) {
+ /// Materialize the prepared outer-loop epilogue as its own sibling loop and
+ /// perform the interchange decided during preparation. Only StaticBound
+ /// plans reach here, so no runtime guard or fallback clone is created. The
+ /// selected outer loop may be top-level or nested; steps 5 and 10 register
+ /// the new loop accordingly. The mutation invalidates the IR pointers
+ /// recorded in \p Plan, so the caller must not read the plan afterwards.
+ void applyPreparedInterchange(PreparedInterchangePlan &Plan, LPMUpdater &U) {
+ assert(Plan.TripBound.Outcome == PreparedBoundOutcome::StaticBound &&
+ "apply materializes only statically bounded plans; a runtime-bound "
+ "plan must be declined before apply");
+ assert(!Plan.TripBound.RuntimeExactTrip &&
+ "a static plan must not carry a runtime trip SCEV");
+ DistributableOuterEpilogue &Epi = Plan.Epilogue;
+ Loop *Outer = Epi.Outer;
+ Loop *Inner = Epi.Inner;
+ assert(Outer && Inner && "prepared plan must carry its candidate pair");
+ assert(Plan.FissionLegal && Plan.InterchangeLegal && Plan.Profitable &&
+ Plan.Legality && "apply requires a complete prepared plan");
+
+ // The interchange below replaces the selected outer loop in place, so
+ // capture its parent first. A null parent makes the epilogue a new
+ // top-level loop.
+ Loop *OuterParent = Outer->getParentLoop();
+
+ Function &F = *Outer->getHeader()->getParent();
+ LLVMContext &Ctx = F.getContext();
+
+ PreparedOuterIVControl &Ctrl = Epi.OuterControl;
+ PHINode *OuterIV = Ctrl.Induction;
+ BasicBlock *OuterExit = Outer->getExitBlock();
+ assert(OuterExit && "prepared outer loop must have a unique exit block");
+
+ // The epilogue latch mirrors the outer latch's branch orientation so both
+ // loops run the same trip count.
+ CondBrInst *OuterLatchBranch = Ctrl.LatchBranch;
+ assert(OuterLatchBranch &&
+ "outer latch control was proved conditional during preparation");
+ bool ExitIsTrueEdge = OuterLatchBranch->getSuccessor(0) == OuterExit;
+ assert((ExitIsTrueEdge || OuterLatchBranch->getSuccessor(1) == OuterExit) &&
+ "outer latch must branch to the unique exit");
+
+ // Capture the interchange remark anchor before the transform repurposes the
+ // inner loop's blocks.
+ DebugLoc InnerLoc = Inner->getStartLoc();
+ BasicBlock *InnerHeader = Inner->getHeader();
+
+ DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager);
+
+ // (1) Split the outer exit after its LCSSA PHIs. The continuation stays
+ // after the epilogue loop, preserving the original N, E, continuation
+ // order even when memory aliases.
+ BasicBlock::iterator ContinuationStart = OuterExit->getFirstNonPHIIt();
+ assert(ContinuationStart != OuterExit->end() &&
+ "outer exit must contain a terminator");
+ BasicBlock *ExitCont =
+ SplitBlock(OuterExit, ContinuationStart, &DTU, LI, /*MSSAU=*/nullptr,
+ OuterExit->getName() + ".cont");
+
+ // (2) Build the epilogue loop: a preheader, a header for the rematerialized
+ // outer-IV chain and the cloned E slice, and a latch for the cloned outer
+ // IV update and exit compare.
+ BasicBlock *EpiPreheader =
+ BasicBlock::Create(Ctx, "epilogue.preheader", &F, ExitCont);
+ BasicBlock *EpiHeader =
+ BasicBlock::Create(Ctx, "epilogue.header", &F, ExitCont);
+ BasicBlock *EpiLatch =
+ BasicBlock::Create(Ctx, "epilogue.latch", &F, ExitCont);
+
+ // A fresh outer induction variable for the epilogue loop.
+ PHINode *EpiIV =
+ PHINode::Create(OuterIV->getType(), 2, "epilogue.iv", EpiHeader);
+ EpiIV->setDebugLoc(OuterIV->getDebugLoc());
+
+ // Clone every instruction before remapping any of them, so debug records
+ // attached to earlier instructions can refer to later definitions.
+ ValueToValueMapTy VMap;
+ VMap[OuterIV] = EpiIV;
+ SmallPtrSet<Value *, 32> ExplicitlyMappedValues;
+ ExplicitlyMappedValues.insert(OuterIV);
+ if (const DebugLoc &DL = OuterIV->getDebugLoc())
+ mapAtomInstance(DL, VMap);
+
+ SmallVector<std::pair<Instruction *, Instruction *>, 32> ClonedInstructions;
+ auto CloneWithMap = [&](Instruction *Orig, BasicBlock *Into,
+ bool OriginalSurvives) -> Instruction * {
+ Instruction *Clone = Orig->clone();
+ if (Orig->hasName())
+ Clone->setName(Orig->getName() + ".epil");
+ Clone->insertInto(Into, Into->end());
+ VMap[Orig] = Clone;
+ ExplicitlyMappedValues.insert(Orig);
+ ClonedInstructions.emplace_back(Orig, Clone);
+ if (OriginalSurvives)
+ if (const DebugLoc &DL = Orig->getDebugLoc())
+ mapAtomInstance(DL, VMap);
+ return Clone;
+ };
+
+ // (2a) Rematerialize the outer-IV-derived chain (operands first), then
+ // clone the E slice in program order. Outer-header arithmetic used only by
+ // N is in neither list.
+ for (Instruction *Def : Epi.OuterIVDerivedInstructions)
+ CloneWithMap(Def, EpiHeader, /*OriginalSurvives=*/true);
+ for (Instruction *I : Epi.Instructions)
+ CloneWithMap(I, EpiHeader, /*OriginalSurvives=*/false);
+ UncondBrInst *EpiHeaderBranch = UncondBrInst::Create(EpiLatch, EpiHeader);
+ if (const DebugLoc &DL = OuterLatchBranch->getDebugLoc())
+ EpiHeaderBranch->setDebugLoc(DebugLoc(DL.get()->getWithoutAtom()));
+
+ // (2b) Clone the outer-IV update and exit compare into the latch.
+ for (Instruction *I : Ctrl.LatchInstructions)
+ CloneWithMap(I, EpiLatch, /*OriginalSurvives=*/true);
+ Value *EpiNext = VMap[Ctrl.NextValue];
+ Value *EpiCond = VMap[Ctrl.LatchCompare];
+ assert(EpiNext && EpiCond &&
+ "outer IV update/compare must have been remapped");
+ CondBrInst *EpiLatchBranch;
+ if (ExitIsTrueEdge)
+ EpiLatchBranch =
+ CondBrInst::Create(EpiCond, ExitCont, EpiHeader, EpiLatch);
+ else
+ EpiLatchBranch =
+ CondBrInst::Create(EpiCond, EpiHeader, ExitCont, EpiLatch);
+ EpiLatchBranch->setDebugLoc(OuterLatchBranch->getDebugLoc());
+ EpiLatchBranch->copyMetadata(*OuterLatchBranch, {LLVMContext::MD_prof});
+ VMap[OuterLatchBranch] = EpiLatchBranch;
+ ExplicitlyMappedValues.insert(OuterLatchBranch);
+ ClonedInstructions.emplace_back(OuterLatchBranch, EpiLatchBranch);
+ if (const DebugLoc &DL = OuterLatchBranch->getDebugLoc())
+ mapAtomInstance(DL, VMap);
+
+ // (2c) Preheader branch and induction closure.
+ UncondBrInst *EpiPreheaderBranch =
+ UncondBrInst::Create(EpiHeader, EpiPreheader);
+ if (const DebugLoc &DL = OuterLatchBranch->getDebugLoc())
+ EpiPreheaderBranch->setDebugLoc(DebugLoc(DL.get()->getWithoutAtom()));
+ EpiIV->addIncoming(Ctrl.InitialValue, EpiPreheader);
+ EpiIV->addIncoming(EpiNext, EpiLatch);
+
+ // Remap every clone now that the atom map is complete, so a source atom
+ // shared by N and E is never live in both loops.
+ RemapSourceAtom(EpiIV, VMap);
+ for (auto [Orig, Clone] : ClonedInstructions)
+ RemapInstruction(Clone, VMap,
+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
+
+ // Preparation proved that no E instruction uses the latch control.
+ for (Instruction &I :
+ make_range(EpiHeader->getFirstNonPHIIt(), EpiHeader->end()))
+ for (Value *Operand : I.operands())
+ assert((!isa<Instruction>(Operand) ||
+ cast<Instruction>(Operand)->getParent() != EpiLatch) &&
+ "epilogue body depends on later latch control");
+
+ // (3) Route the nest exit through the epilogue loop. It runs only after
+ // the nest exits, so it inherits any zero-trip guard.
+ OuterExit->getTerminator()->replaceSuccessorWith(ExitCont, EpiPreheader);
+
+ // (4) Register the epilogue loop's control edges with the dominator tree.
+ DTU.applyUpdatesPermissive({
+ {DominatorTree::Delete, OuterExit, ExitCont},
+ {DominatorTree::Insert, OuterExit, EpiPreheader},
+ {DominatorTree::Insert, EpiPreheader, EpiHeader},
+ {DominatorTree::Insert, EpiHeader, EpiLatch},
+ {DominatorTree::Insert, EpiLatch, EpiHeader},
+ {DominatorTree::Insert, EpiLatch, ExitCont},
+ });
+
+ // Debug records attach to source positions, not to the values they
+ // describe. Map each cloned instruction to its clone and each omitted path
+ // terminator to the clone of the first E instruction from a later block,
+ // or to the new header branch when there is none.
+ DenseMap<Instruction *, Instruction *> DebugAnchorMap;
+ for (auto [Orig, Clone] : ClonedInstructions)
+ DebugAnchorMap[Orig] = Clone;
+
+ DenseMap<BasicBlock *, unsigned> PathOrder;
+ for (auto [Index, BB] : enumerate(Epi.Path))
+ PathOrder[BB] = static_cast<unsigned>(Index);
+
+ // Compute the terminator mapping in one linear pass: record the earliest
+ // slice position per source block (the outer latch sorts last), then a
+ // backward suffix scan yields the earliest position among all later
+ // blocks for each path index.
+ const unsigned PathBlockCount = static_cast<unsigned>(Epi.Path.size());
+ BasicBlock *LatchBlock = Ctrl.NextValue->getParent();
+ constexpr size_t NoPosition = std::numeric_limits<size_t>::max();
+ SmallVector<Instruction *, 8> FirstOfOrder(PathBlockCount + 1, nullptr);
+ SmallVector<size_t, 8> FirstOfOrderPos(PathBlockCount + 1, NoPosition);
+ for (auto [Position, I] : enumerate(Epi.Instructions)) {
+ BasicBlock *SourceBB = I->getParent();
+ unsigned Order;
+ if (SourceBB == LatchBlock) {
+ Order = PathBlockCount;
+ } else {
+ auto It = PathOrder.find(SourceBB);
+ if (It == PathOrder.end())
+ continue;
+ Order = It->second;
+ }
+ if (Position < FirstOfOrderPos[Order]) {
+ FirstOfOrderPos[Order] = Position;
+ FirstOfOrder[Order] = I;
+ }
+ }
+ // EarliestFrom[Rank] is the earliest slice instruction whose order is at
+ // least Rank, so the terminator at path index I reads EarliestFrom[I + 1].
+ SmallVector<Instruction *, 8> EarliestFrom(PathBlockCount + 2, nullptr);
+ SmallVector<size_t, 8> EarliestFromPos(PathBlockCount + 2, NoPosition);
+ for (unsigned Rank = PathBlockCount + 1; Rank-- > 0;) {
+ EarliestFrom[Rank] = EarliestFrom[Rank + 1];
+ EarliestFromPos[Rank] = EarliestFromPos[Rank + 1];
+ if (FirstOfOrderPos[Rank] < EarliestFromPos[Rank]) {
+ EarliestFromPos[Rank] = FirstOfOrderPos[Rank];
+ EarliestFrom[Rank] = FirstOfOrder[Rank];
+ }
+ }
+
+ SmallPtrSet<Instruction *, 4> PathTerminators;
+ for (auto [Index, BB] : enumerate(Epi.Path)) {
+ Instruction *Source = EarliestFrom[Index + 1];
+ Instruction *Dest =
+ Source ? cast<Instruction>(VMap[Source]) : EpiHeaderBranch;
+ Instruction *Terminator = BB->getTerminator();
+ DebugAnchorMap[Terminator] = Dest;
+ PathTerminators.insert(Terminator);
+ }
+
+ // PHI records sit at the header's first insertion point. Give that anchor
+ // an E destination so the outer IV's record can follow the epilogue IV;
+ // reduction-PHI records fail the mapped-value filter below and stay in N.
+ Instruction *OuterHeaderDebugAnchor =
+ &*Outer->getHeader()->getFirstInsertionPt();
+ if (!DebugAnchorMap.contains(OuterHeaderDebugAnchor))
+ DebugAnchorMap[OuterHeaderDebugAnchor] =
+ &*EpiHeader->getFirstInsertionPt();
+
+ SmallVector<Instruction *, 32> SourceDebugAnchors;
+ SmallPtrSet<Instruction *, 32> SeenDebugAnchors;
+ auto AddDebugAnchor = [&](Instruction *I) {
+ if (SeenDebugAnchors.insert(I).second)
+ SourceDebugAnchors.push_back(I);
+ };
+ AddDebugAnchor(OuterHeaderDebugAnchor);
+ for (Instruction *I : Epi.OuterIVDerivedInstructions)
+ AddDebugAnchor(I);
+ for (BasicBlock *BB : Epi.Path) {
+ for (Instruction &I : *BB)
+ if (Epi.InstructionSet.contains(&I))
+ AddDebugAnchor(&I);
+ AddDebugAnchor(BB->getTerminator());
+ }
+ for (Instruction &I : *Ctrl.NextValue->getParent())
+ if (Epi.InstructionSet.contains(&I) ||
+ is_contained(Ctrl.LatchInstructions, &I) || &I == OuterLatchBranch)
+ AddDebugAnchor(&I);
+
+ auto IsAvailableInEpilogue = [&](Value *V) {
+ if (ExplicitlyMappedValues.contains(V))
+ return true;
+ if (!Outer->isLoopInvariant(V))
+ return false;
+ auto *I = dyn_cast<Instruction>(V);
+ return !I || DT->dominates(I->getParent(), EpiPreheader);
+ };
+ auto ClassifyLocation = [&](DbgVariableRecord &DVR, bool &HasMapped,
+ bool &HasTransplanted) {
+ bool AllAvailable = true;
+ auto ClassifyValue = [&](Value *V) {
+ if (!V)
+ return;
+ HasMapped |= ExplicitlyMappedValues.contains(V);
+ if (auto *I = dyn_cast<Instruction>(V))
+ HasTransplanted |= Epi.InstructionSet.contains(I);
+ AllAvailable &= IsAvailableInEpilogue(V);
+ };
+ for (Value *V : DVR.location_ops())
+ ClassifyValue(V);
+ return AllAvailable;
+ };
+
+ // Transfer only records whose whole location is available in E: duplicate
+ // those describing surviving values, move those describing moved E values.
+ // Assignment-tracking records take the salvage path in step 6.
+ for (Instruction *Source : SourceDebugAnchors) {
+ Instruction *Dest = DebugAnchorMap.lookup(Source);
+ assert(Dest && "every debug source position must have an E destination");
+ DbgMarker *DestMarker =
+ Dest->getParent()->createMarker(Dest->getIterator());
+ bool SourcePositionMoves = Epi.InstructionSet.contains(Source) ||
+ PathTerminators.contains(Source);
+ for (DbgRecord &DR : make_early_inc_range(Source->getDbgRecordRange())) {
+ bool EraseOriginal = false;
+ if (auto *DVR = dyn_cast<DbgVariableRecord>(&DR)) {
+ if (DVR->isDbgAssign())
+ continue;
+ bool HasMapped = false;
+ bool HasTransplanted = false;
+ bool AllAvailable =
+ ClassifyLocation(*DVR, HasMapped, HasTransplanted);
+ if (!HasMapped)
+ continue;
+ if (!AllAvailable) {
+ // A location mixing an E value with an outer-variant N value has
+ // no meaning in E; drop it rather than leave an erased operand.
+ if (HasTransplanted)
+ DR.eraseFromParent();
+ continue;
+ }
+ EraseOriginal = HasTransplanted;
+ } else {
+ // Labels identify a program point rather than an SSA value.
+ if (!SourcePositionMoves && !ExplicitlyMappedValues.contains(Source))
+ continue;
+ EraseOriginal = SourcePositionMoves;
+ }
+
+ DbgRecord *Clone = DR.clone();
+ DestMarker->insertDbgRecord(Clone, /*InsertAtHead=*/false);
+ RemapDbgRecord(F.getParent(), Clone, VMap,
+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
+ if (EraseOriginal)
+ DR.eraseFromParent();
+ }
+ }
+
+ // (5) Register the epilogue loop as the sibling following the selected
+ // outer loop. The two LoopInfo containers store siblings in opposite
+ // orders.
+ Loop *EpiLoop = LI->AllocateLoop();
+ if (!OuterParent) {
+ // TopLevelLoops is in reverse program order, so E precedes Outer there.
+ SmallVector<Loop *, 4> ExistingTopLevelLoops =
+ LI->takeChildrenIf(/*Parent=*/nullptr, [](Loop *) { return true; });
+ bool InsertedEpilogue = false;
+ for (Loop *L : ExistingTopLevelLoops) {
+ if (L == Outer) {
+ LI->addTopLevelLoop(EpiLoop);
+ InsertedEpilogue = true;
+ }
+ LI->addTopLevelLoop(L);
+ }
+ assert(InsertedEpilogue &&
+ "prepared top-level outer must be present in LoopInfo");
+ (void)InsertedEpilogue;
+ } else {
+ // SubLoops is in program order, so E follows Outer there. The
+ // interchange later replaces Outer in place (LI->replaceLoop), which
+ // keeps this order.
+ SmallVector<Loop *, 4> ExistingChildren =
+ LI->takeChildrenIf(OuterParent, [](Loop *) { return true; });
+ bool InsertedEpilogue = false;
+ for (Loop *L : ExistingChildren) {
+ OuterParent->addChildLoop(L);
+ if (L == Outer) {
+ OuterParent->addChildLoop(EpiLoop);
+ InsertedEpilogue = true;
+ }
+ }
+ assert(InsertedEpilogue &&
+ "prepared nested outer must be present in its parent's subloops");
+ (void)InsertedEpilogue;
+ // The epilogue's dedicated preheader lives inside the parent loop but
+ // outside E, so it belongs to the parent and its ancestors.
+ OuterParent->addBasicBlockToLoop(EpiPreheader, *LI);
+ }
+ // addBasicBlockToLoop also registers ancestors, so E's parent is set first.
+ EpiLoop->addBasicBlockToLoop(EpiHeader, *LI);
+ EpiLoop->addBasicBlockToLoop(EpiLatch, *LI);
+
+ // (6) Erase the original E slice in reverse order. Droppable uses (assumes)
+ // go first; the slice is otherwise closed.
+ for (Instruction *I : Epi.Instructions)
+ I->dropDroppableUses();
+ for (Instruction *I : reverse(Epi.Instructions)) {
+ // N-only records anchored here re-anchor onto the surviving path.
+ salvageDebugInfo(*I);
+ assert(I->use_empty() &&
+ "epilogue value still used after cloning; slice was not closed");
+ I->eraseFromParent();
+ }
+
+ // (6a) Merge the now-empty intermediate path blocks, keeping the first
+ // (the inner exit with its forwarding PHIs) and the outer latch. A lazy
+ // updater batches the dominator-tree updates, which an eager one would
+ // apply once per merge.
+ DomTreeUpdater LazyDTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
+ for (unsigned K = 1, PathSize = Epi.Path.size(); K < PathSize; ++K) {
+ bool Merged = MergeBlockIntoPredecessor(Epi.Path[K], &LazyDTU, LI);
+ (void)Merged;
+ assert(Merged && "straight-line epilogue path must collapse");
+ }
+ LazyDTU.flush();
+
+ // (7) Cached SCEV expressions and dispositions may still name the erased
+ // path blocks; drop them before the LCSSA formation and the interchange
+ // query SCEV.
+ SE->forgetLoop(Outer);
+ SE->forgetBlockAndLoopDispositions();
+
+ // The epilogue loop is closed (no live-outs); keep it in LCSSA defensively
+ // before the interchange updates analyses.
+ formLCSSARecursively(*EpiLoop, *DT, LI, SE);
+
+ // (8) Interchange with the stored legality and flag-drop decisions.
+ LoopInterchangeTransform LIT(Outer, Inner, SE, LI, DT, *Plan.Legality);
+ LIT.transform(Plan.DropNoWrap, Plan.DropNoInf);
+ llvm::formLCSSARecursively(*Outer, *DT, LI, SE);
+ // The parent gained a subloop and the interchange moved blocks between
+ // loops, so drop the parent's cached SCEV and the dispositions again.
+ if (OuterParent)
+ SE->forgetLoop(OuterParent);
+ SE->forgetBlockAndLoopDispositions();
+ LoopsInterchanged++;
+ OuterEpiloguesDistributed++;
+
+ // The remark names the largest accepted bound limit W, or says that the
+ // plan needed no bound requirement.
+ std::string BoundNote = "bound=static-bound, no-bound-requirement";
+ if (!Plan.TripBound.Requirements.empty()) {
+ APInt StaticW(64, 0);
+ for (const PreparedBoundRequirement &Req : Plan.TripBound.Requirements)
+ if (Req.Limit.ugt(StaticW))
+ StaticW = Req.Limit;
+ BoundNote =
+ ("bound=static-bound, W=" + Twine(StaticW.getZExtValue())).str();
+ }
+
+ // (9) Emit the interchange and distribution remarks.
+ ORE->emit([&]() {
+ return OptimizationRemark(DEBUG_TYPE, "Interchanged", InnerLoc,
+ InnerHeader)
+ << "Loop interchanged with enclosing loop.";
+ });
+ ORE->emit([&]() {
+ return OptimizationRemark(DEBUG_TYPE, "OuterEpilogueDistributed",
+ EpiLoop->getStartLoc(), EpiHeader)
+ << "Distributed a proven outer-loop epilogue into its own loop "
+ "before interchanging the reduction nest; "
+ << BoundNote << ".";
+ });
+
+ // (10) In loop-nest mode the updater accepts only new top-level siblings.
+ // A nested epilogue loop is found by the next loop-nest walk after run()
+ // reports the change.
+ if (!OuterParent)
+ U.addSiblingLoops({EpiLoop});
+
+ LLVM_DEBUG(
+ dbgs() << "loop-interchange: materialized outer-epilogue fission + "
+ "interchange, "
+ << (OuterParent ? "nested sibling" : "top-level sibling")
+ << ", in function '" << F.getName() << "'.\n");
+ }
+
+ bool run(LoopNest &LN, LPMUpdater &U) {
SmallVector<SmallVector<Loop *, 8>, 4> LoopLists = collectPerfectNests(LN);
if (LoopLists.empty()) {
LLVM_DEBUG(dbgs() << "No Valid candidates for loop interchange.\n");
return false;
}
+ // Outer-epilogue fission considers the same parent/leaf-child candidates
+ // as the ordinary path. A StaticBound plan is applied here: the proven
+ // epilogue becomes its own sibling loop and the remaining nest is
+ // interchanged, with no runtime guard or clone. A RuntimeBound plan needs
+ // a runtime bound check, so it is declined with a missed remark and the
+ // ordinary routing below runs unchanged. Size-optimized functions are
+ // skipped so their output never changes.
if (EnableOuterEpilogueFission && !LN.getParent()->hasOptSize()) {
- std::optional<PreparedInterchangePlan> Prepared =
- tryPrepareOuterEpilogueFission(LoopLists);
- // Preparation is analysis-only. Destroy every plan before ordinary
- // routing can mutate the collected chains.
- (void)Prepared;
+ if (std::optional<PreparedInterchangePlan> Prepared =
+ tryPrepareOuterEpilogueFission(LoopLists)) {
+ if (Prepared->TripBound.Outcome == PreparedBoundOutcome::StaticBound) {
+ // Nothing has mutated since preparation, so re-validation cannot
+ // fail; a failure asserts. A release build declines the plan with a
+ // stable reason and leaves the function unchanged.
+ bool StillValid = validatePreparedPlan(*Prepared);
+ assert(StillValid && "a prepared plan must stay valid until apply");
+ if (StillValid) {
+ applyPreparedInterchange(*Prepared, U);
+ return true;
+ }
+ rejectPreparedEpilogue(
+ ORE, Prepared->Epilogue.Outer, Prepared->Epilogue.Inner,
+ "prepared plan failed re-validation immediately before apply");
+ }
+ // An unapplied plan is destroyed here, before ordinary routing mutates
+ // the chains it points into.
+ }
}
bool Changed = false;
@@ -4635,7 +5116,7 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
});
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
- if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, &AR, &ORE).run(LN))
+ if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, &AR, &ORE).run(LN, U))
return PreservedAnalyses::all();
U.markLoopNestChanged(true);
return getLoopPassPreservedAnalyses();
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
index 08feb5b31a8224..90e1f740f730de 100644
--- a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-bounds.ll
@@ -28,22 +28,51 @@
; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+; DEFINE: %{applied} = \
+; DEFINE: --func=bound_typed_canonical --func=bound_byte_canonical \
+; DEFINE: --func=bound_offset_high32 --func=bound_storage_defined_global \
+; DEFINE: --func=bound_storage_external_global \
+; DEFINE: --func=bound_storage_replaceable_global \
+; DEFINE: --func=bound_const_below --func=bound_const_eq \
+; DEFINE: --func=bound_dominating_guard --func=bound_max_too_broad \
+; DEFINE: --func=bound_assume --func=bound_scunknown_range \
+; DEFINE: --func=bound_widen_i32 --func=bound_context_exact_trunc \
+; DEFINE: --func=bound_context_exact_addrec
+; DEFINE: %{no_versioning} = \
+; DEFINE: --implicit-check-not='.lver' \
+; DEFINE: --implicit-check-not='!alias.scope' \
+; DEFINE: --implicit-check-not='!noalias' \
+; DEFINE: --implicit-check-not='llvm.loop.interchange.runtime_versioned' \
+; DEFINE: --implicit-check-not='LoopVersioning'
;
-; Preparation is analysis-only: a complete plan is printed and then discarded,
-; so the IR is unchanged with the option on.
+; With the option on, every function in %{applied} is transformed and every
+; other function is unchanged.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
-; RUN: diff -u %t.out %t.prep
+; RUN: llvm-extract -S --delete %{applied} %t.out -o %t.out.rest
+; RUN: llvm-extract -S --delete %{applied} %t.prep -o %t.prep.rest
+; RUN: diff -u -I '^; ModuleID' %t.out.rest %t.prep.rest
+; RUN: FileCheck %s --check-prefix=APPLIED --input-file=%t.prep %{no_versioning} --implicit-check-not='{{^define }}'
+;
+; The updated LoopInfo must match a fresh rebuild, sibling order included, and
+; a second run of the pass must change nothing.
+; RUN: opt -passes='loop(loop-interchange),print<loops>' -cache-line-size=64 %{policy} -loop-interchange-outer-epilogue-fission -disable-output %s 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -passes='print<loops>' -disable-output %t.prep 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -S -passes='loop(loop-interchange,loop-interchange)' -cache-line-size=64 %{policy} %{prepare} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.twice
+; RUN: diff -u %t.prep %t.twice
;
; Each printed plan has passed both dependence matrices, legality,
-; profitability, and validation. W is recovered from the element-normalized
-; byte stride, not from the storage size, and each generalized N/E pair has
-; its own modular and containment requirements.
+; profitability, and validation. The two remarks after it come from the apply
+; step. W is recovered from the element-normalized byte stride, not from the
+; storage size, and each generalized N/E pair has its own modular and
+; containment requirements.
; PREP-LABEL: loop-interchange: prepared function=bound_typed_canonical{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[TYPED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[TYPED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: prepared function=bound_same_trip_wmin{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=8 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=2 reductions=2
@@ -60,6 +89,8 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[BYTE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BYTE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: rejected function=bound_byte_nondivisible{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
;
@@ -69,6 +100,8 @@
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; optsize and minsize functions skip preparation rather than reject a
; discovered candidate, so they print nothing. bound_const_eq is the same shape
@@ -78,16 +111,22 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[DEFINED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[DEFINED_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_storage_external_global{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[EXTERNAL_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[EXTERNAL_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_storage_replaceable_global{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[REPLACEABLE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[REPLACEABLE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: rejected function=bound_storage_missing{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
; PREP-LABEL: loop-interchange: rejected function=bound_storage_undersized{{ }}
@@ -110,11 +149,15 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[BELOW_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BELOW_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_const_eq{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[CONST_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[CONST_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: rejected function=bound_const_above1{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=known-unsafe-trip-exceeds-bound{{$}}
; PREP-LABEL: loop-interchange: rejected function=bound_const_above2{{ }}
@@ -124,6 +167,8 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[GUARD_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[GUARD_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
;
; This body discharges by constant maximum despite its name. The truncation
; twins below exercise the contextual exact-trip strategy.
@@ -132,16 +177,22 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[MAX_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[MAX_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_assume{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[ASSUME_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[ASSUME_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_scunknown_range{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[RANGE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[RANGE_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_ineffective_min_metadata{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
@@ -159,6 +210,8 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[WIDEN_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[WIDEN_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: rejected function=bound_overflow{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=known-unsafe-trip-exceeds-bound{{$}}
;
@@ -169,7 +222,8 @@
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[TRUNC_ID:[0-9]+]]:modular-outer-span/static/by-context-exact,[[TRUNC_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=bound_context_nofact_trunc{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
@@ -183,8 +237,8 @@
; PREP-SAME: absolute-depth=3 routing-depth=3 fission-rows=0 routing-rows=1 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[ADDREC_ID:[0-9]+]]:modular-outer-span/static/by-context-exact,[[ADDREC_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
;
; EXACT-TRUNC-SCEV-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'bound_context_exact_trunc':
; EXACT-TRUNC-SCEV: {{^}}Determining loop execution counts for: @bound_context_exact_trunc{{$}}
@@ -198,7 +252,438 @@
; ADDREC-SCEV: {{^}}Determining loop execution counts for: @bound_context_exact_addrec{{$}}
; ADDREC-SCEV: {{^}}Loop %outer.header: backedge-taken count is {0,+,1}<nuw><nsw><%top.header>{{$}}
; ADDREC-SCEV-NEXT: {{^}}Loop %outer.header: constant max backedge-taken count is i64 999999{{$}}
+
+; The transformed module. Every definition is listed, so the RUN line's
+; '{{^define }}' exclusion makes the list exhaustive. Each applied function
+; gains one sibling epilogue loop whose latch branches to the original
+; continuation, and its nest is interchanged. The emptied original epilogue
+; block, the epilogue loop, and the continuation are checked instruction by
+; instruction. Block labels are plain directives because the printed IR
+; separates blocks with a blank line. The other functions were already shown
+; unchanged by the llvm-extract --delete pair above and carry only their label.
+;
+; APPLIED-LABEL: define {{.*}}@bound_typed_canonical(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [1335 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_same_trip_wmin(
+; APPLIED-LABEL: define {{.*}}@bound_distinct_runtime(
+; APPLIED-LABEL: define {{.*}}@bound_outer_static_inner_runtime(
+; APPLIED-LABEL: define {{.*}}@bound_byte_canonical(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diagoff.epil = mul i64 %epilogue.iv, 10688
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds i8, ptr @byte_canonical_obj, i64 %diagoff.epil
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_byte_nondivisible(
+; APPLIED-LABEL: define {{.*}}@bound_offset_high32(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %lo.epil = getelementptr inbounds i8, ptr @offset_high32_obj, i64 16
+; APPLIED-NEXT: %hi.epil = getelementptr inbounds i8, ptr @offset_high32_obj, i64 4294967312
+; APPLIED-NEXT: %lv.epil = load double, ptr %lo.epil, align 8
+; APPLIED-NEXT: %hn.epil = fmul double %lv.epil, 1.500000e+00
+; APPLIED-NEXT: store double %hn.epil, ptr %hi.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_optsize_canonical(
+; APPLIED-LABEL: define {{.*}}@bound_minsize_canonical(
+; APPLIED-LABEL: define {{.*}}@bound_storage_defined_global(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diagoff.epil = mul i64 %epilogue.iv, 40
+; APPLIED-NEXT: %ddiag.epil = getelementptr i8, ptr @storage_defined_obj, i64 %diagoff.epil
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_storage_external_global(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diagoff.epil = mul i64 %epilogue.iv, 40
+; APPLIED-NEXT: %ddiag.epil = getelementptr i8, ptr @storage_external_obj, i64 %diagoff.epil
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_storage_replaceable_global(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diagoff.epil = mul i64 %epilogue.iv, 40
+; APPLIED-NEXT: %ddiag.epil = getelementptr i8, ptr @storage_replaceable_obj, i64 %diagoff.epil
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_storage_missing(
+; APPLIED-LABEL: define {{.*}}@bound_storage_undersized(
+; APPLIED-LABEL: define {{.*}}@bound_storage_nullable(
+; APPLIED-LABEL: define {{.*}}@bound_storage_weak(
+; APPLIED-LABEL: define {{.*}}@bound_runtime_canonical(
+; APPLIED-LABEL: define {{.*}}@bound_const_below(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 3
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_const_eq(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_const_above1(
+; APPLIED-LABEL: define {{.*}}@bound_const_above2(
+; APPLIED-LABEL: define {{.*}}@bound_dominating_guard(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@bound_max_too_broad(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %nn
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@bound_assume(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_scunknown_range(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@bound_ineffective_min_metadata(
+; APPLIED-LABEL: define {{.*}}@bound_nofact(
+; APPLIED-LABEL: define {{.*}}@bound_widen_i32(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i32 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %i64.epil = zext i32 %epilogue.iv to i64
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr @widen_obj, i64 %i64.epil, i64 %i64.epil
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i32 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i32 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@bound_overflow(
+; APPLIED-LABEL: define {{.*}}@bound_context_exact_trunc(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %nn
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@bound_context_nofact_trunc(
+; APPLIED-LABEL: define {{.*}}@bound_context_exact_addrec(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: outer.exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x double], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %t
+; APPLIED-NEXT: br i1 %i.ec.epil, label %outer.exit.cont, label %epilogue.header
+; APPLIED: outer.exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: br label %top.latch
+;
+; The same loop-info lines are checked against the loop manager's live
+; LoopInfo and against a fresh rebuild of the transformed module. The block
+; lists inside the nest are wildcarded because LoopInfo stores them in
+; discovery order, which an incremental update and a fresh walk reach
+; differently; the loop order, the depths, and the epilogue loop's two blocks
+; are exact.
;
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_typed_canonical':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_byte_canonical':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_offset_high32':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_storage_defined_global':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_storage_external_global':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_storage_replaceable_global':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_const_below':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_const_eq':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_dominating_guard':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_max_too_broad':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_assume':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_scunknown_range':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_widen_i32':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_context_exact_trunc':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'bound_context_exact_addrec':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %top.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-debug.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-debug.ll
new file mode 100644
index 00000000000000..3128c4b45aa096
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-debug.ll
@@ -0,0 +1,123 @@
+; NOTE: Do not autogenerate
+; Debug-record coverage for static outer-epilogue fission.
+;
+; N denotes the reduction nest and E the post-inner epilogue.
+;
+; Records attach to the instruction they precede, not to the SSA value they
+; describe. This multi-block positive therefore covers three nontrivial cases:
+; a use-before-def record for a later E value, a record for an E value attached
+; to an omitted path terminator, and a final E record attached to the last path
+; terminator. A record for the N forwarding PHI shares the first E anchor but
+; must remain on the reduction path.
+;
+; Outer-epilogue fission is opt-in, so this RUN requests it explicitly.
+; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 \
+; RUN: -loop-interchange-outer-epilogue-fission \
+; RUN: -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa \
+; RUN: -S 2>&1 | FileCheck %s \
+; RUN: --implicit-check-not='#dbg_value({{.*}} poison'
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @multi_block_debug(ptr noalias %A, ptr noalias %D,
+ ptr noalias %EArray, ptr noalias %R) !dbg !3 {
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+ %chk.i = phi double [ 0.000000e+00, %entry ], [ %chk.next, %outer.latch ]
+ %outer.variant = uitofp i64 %i to double
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.header ]
+ %chk.j = phi double [ %chk.i, %outer.header ], [ %chk.next.j, %inner.header ]
+ %aidx = getelementptr inbounds [1335 x double], ptr %A, i64 %j, i64 %i
+ %av = load double, ptr %aidx, align 8
+ %chk.next.j = fadd reassoc double %chk.j, %av
+ %j.next = add i64 %j, 1
+ %j.ec = icmp eq i64 %j.next, 1335
+ br i1 %j.ec, label %epilogue.first, label %inner.header
+
+epilogue.first:
+ %chk.next = phi double [ %chk.next.j, %inner.header ]
+ #dbg_value(double %chk.next, !7, !DIExpression(), !12)
+ #dbg_value(double %en, !8, !DIExpression(), !13)
+ #dbg_value(!DIArgList(double %en, double %outer.variant), !16, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), !18)
+ #dbg_value(double 1.000000e+00, !17, !DIExpression(), !19)
+ %dp = getelementptr inbounds double, ptr %D, i64 %i
+ %dv = load double, ptr %dp, align 8
+ %dn = fadd double %dv, 1.000000e+00
+ store double %dn, ptr %dp, align 8
+ #dbg_value(double %dn, !9, !DIExpression(), !14)
+ br label %epilogue.second
+
+epilogue.second:
+ %ep = getelementptr inbounds double, ptr %EArray, i64 %i
+ %ev = load double, ptr %ep, align 8
+ %en = fadd double %ev, 2.000000e+00
+ store double %en, ptr %ep, align 8
+ #dbg_value(double %en, !10, !DIExpression(), !15)
+ br label %outer.latch
+
+outer.latch:
+ %i.next = add i64 %i, 1
+ %i.ec = icmp eq i64 %i.next, 1335
+ br i1 %i.ec, label %exit, label %outer.header
+
+exit:
+ %chk.res = phi double [ %chk.next, %outer.latch ]
+ store double %chk.res, ptr %R, align 8
+ ret void
+}
+
+; The N forwarding-PHI location remains in the emptied connector. The three
+; E-side records are absent here, so no erased E value can poison this path.
+; CHECK-LABEL: define void @multi_block_debug(
+; CHECK: epilogue.first:
+; CHECK-NEXT: #dbg_value(double %chk.next, [[N_VAR:![0-9]+]],
+; CHECK-NEXT: #dbg_value(double 1.000000e+00, [[CONSTANT_VAR:![0-9]+]],
+; CHECK-NEXT: br label %outer.latch
+;
+; The use-before-def record is preserved before the first cloned instruction
+; and remapped to the later %en.epil definition. The first path-terminator
+; record lands at the next flattened block boundary, before %ep.epil. The final
+; path-terminator record lands immediately before the new E-body branch.
+; CHECK: epilogue.header:
+; CHECK: %epilogue.iv = phi i64
+; CHECK-NOT: #dbg_value(!DIArgList(
+; CHECK-NOT: #dbg_value(double 1.000000e+00,
+; CHECK-NEXT: #dbg_value(double %en.epil, [[FUTURE_E_VAR:![0-9]+]],
+; CHECK-NEXT: %dp.epil = getelementptr inbounds double, ptr %D, i64 %epilogue.iv
+; CHECK: store double %dn.epil, ptr %dp.epil, align 8
+; CHECK-NEXT: #dbg_value(double %dn.epil, [[FIRST_TERM_VAR:![0-9]+]],
+; CHECK-NEXT: %ep.epil = getelementptr inbounds double, ptr %EArray, i64 %epilogue.iv
+; CHECK: store double %en.epil, ptr %ep.epil, align 8
+; CHECK-NEXT: #dbg_value(double %en.epil, [[FINAL_TERM_VAR:![0-9]+]],
+; CHECK-NEXT: br label %epilogue.latch
+; CHECK-NOT: #dbg_value(double %chk.next,
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "loop-interchange test", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "outer-epilogue-fission-debug.c", directory: "/")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = distinct !DISubprogram(name: "multi_block_debug", scope: !1, file: !1, line: 1, type: !4, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !6)
+!4 = !DISubroutineType(types: !5)
+!5 = !{null}
+!6 = !{!7, !8, !9, !10, !16, !17}
+!7 = !DILocalVariable(name: "n_forward", scope: !3, file: !1, line: 2, type: !11)
+!8 = !DILocalVariable(name: "future_e", scope: !3, file: !1, line: 3, type: !11)
+!9 = !DILocalVariable(name: "first_term_e", scope: !3, file: !1, line: 4, type: !11)
+!10 = !DILocalVariable(name: "final_term_e", scope: !3, file: !1, line: 5, type: !11)
+!11 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
+!12 = !DILocation(line: 2, column: 1, scope: !3)
+!13 = !DILocation(line: 3, column: 1, scope: !3)
+!14 = !DILocation(line: 4, column: 1, scope: !3)
+!15 = !DILocation(line: 5, column: 1, scope: !3)
+!16 = !DILocalVariable(name: "mixed_outer_variant", scope: !3, file: !1, line: 6, type: !11)
+!17 = !DILocalVariable(name: "constant_only", scope: !3, file: !1, line: 7, type: !11)
+!18 = !DILocation(line: 6, column: 1, scope: !3)
+!19 = !DILocation(line: 7, column: 1, scope: !3)
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
index 65a5b5ebcb9499..46509df42fba24 100644
--- a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-rejections.ll
@@ -16,6 +16,14 @@
; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+; DEFINE: %{applied} = \
+; DEFINE: --func=noalias_in_nest --func=matrix_memory_ratio
+; DEFINE: %{no_versioning} = \
+; DEFINE: --implicit-check-not='.lver' \
+; DEFINE: --implicit-check-not='!alias.scope' \
+; DEFINE: --implicit-check-not='!noalias' \
+; DEFINE: --implicit-check-not='llvm.loop.interchange.runtime_versioned' \
+; DEFINE: --implicit-check-not='LoopVersioning'
;
; Discovery rejects the four convergence negatives, so no later proof stage
; may report them.
@@ -31,11 +39,21 @@
; DEFINE: --implicit-check-not='fission-context' \
; DEFINE: --implicit-check-not='routing dependence'
;
-; Preparation is analysis-only: a complete plan is printed and then discarded,
-; so the IR is unchanged with the option on.
+; With the option on, the two positive controls are transformed and every
+; other function is unchanged.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
; RUN: FileCheck %s --check-prefixes=PREP,DISCOVERY,MATRIX-READY --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
-; RUN: diff -u %t.out %t.prep
+; RUN: llvm-extract -S --delete %{applied} %t.out -o %t.out.rest
+; RUN: llvm-extract -S --delete %{applied} %t.prep -o %t.prep.rest
+; RUN: diff -u -I '^; ModuleID' %t.out.rest %t.prep.rest
+; RUN: FileCheck %s --check-prefix=APPLIED --input-file=%t.prep %{no_versioning} --implicit-check-not='{{^define }}'
+;
+; The updated LoopInfo must match a fresh rebuild, sibling order included, and
+; a second run of the pass must change nothing.
+; RUN: opt -passes='loop(loop-interchange),print<loops>' -cache-line-size=64 %{policy} -loop-interchange-outer-epilogue-fission -disable-output %s 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -passes='print<loops>' -disable-output %t.prep 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -S -passes='loop(loop-interchange,loop-interchange)' -cache-line-size=64 %{policy} %{prepare} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.twice
+; RUN: diff -u %t.prep %t.twice
;
; At ratio zero, discovery still reports its own failures; only candidates
; that finish discovery reach the shared budget rejection. At ratio one the
@@ -47,7 +65,10 @@
; RUN: FileCheck %s --check-prefixes=MEMZERO,DISCOVERY --input-file=%t.ratio0.stderr --implicit-check-not='loop-interchange:'
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=1 -loop-interchange-outer-epilogue-fission=false %s -o %t.ratio1.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=1 %{prepare} %{remarks} %s -o %t.ratio1.on 2> %t.ratio1.stderr
-; RUN: diff -u %t.ratio1.off %t.ratio1.on
+; RUN: llvm-extract -S --delete --func=noalias_in_nest %t.ratio1.off -o %t.ratio1.off.rest
+; RUN: llvm-extract -S --delete --func=noalias_in_nest %t.ratio1.on -o %t.ratio1.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.ratio1.off.rest %t.ratio1.on.rest
+; RUN: FileCheck %s --check-prefix=RATIO-LIMITED --input-file=%t.ratio1.on %{no_versioning}
; RUN: FileCheck %s --check-prefixes=PREP,DISCOVERY,MATRIX-LIMIT --input-file=%t.ratio1.stderr --implicit-check-not='loop-interchange:'
;
; These whole-module traces reach the two matrix guards. The same N/N alias
@@ -207,8 +228,9 @@
; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: retained nest contains an unsupported convergent operation
; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
;
-; The three selected-outer directions are the generalized raw classes at the
-; selected outer loop itself: raw vectors [<=|<], [=>|<], and [<>].
+; The three selected-outer directions own the generalized raw classes at the
+; selected outer itself. Their raw vectors [<=|<], [=>|<], and [<>] are bound
+; by the function-scoped DA rows above and stay unnormalized there.
; PREP-LABEL: loop-interchange: rejected function=direction_selected_le{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=cross-partition dependence is not statically proved N-to-E{{$}}
; PREP-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: cross-partition dependence is not statically proved N-to-E
@@ -253,7 +275,8 @@
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; MEMZERO-LABEL: loop-interchange: rejected function=noalias_in_nest{{ }}
; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
@@ -266,7 +289,8 @@
; MATRIX-READY-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=66 rematerialized=0
; MATRIX-READY-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; MATRIX-READY-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; MATRIX-READY-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MATRIX-READY-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX-READY-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; MATRIX-LIMIT-LABEL: loop-interchange: rejected function=matrix_memory_ratio{{ }}
; MATRIX-LIMIT-SAME: outer=outer.header inner=inner.header reason=fission-context dependence matrix is unavailable{{$}}
; MATRIX-LIMIT-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: fission-context dependence matrix is unavailable
@@ -307,8 +331,13 @@
; MATRIX-TRACE-NEXT: loop-interchange: prepared a complete outer-epilogue fission plan in function 'noalias_in_nest': Outer 'outer.header', Inner 'inner.header', absolute columns 0/1, routing columns 0/1, 0 cross dependence(s).
; MATRIX-TRACE-NEXT: loop-interchange: prepared function=noalias_in_nest{{ }}
; MATRIX-TRACE-SAME: rematerialized=0{{ }}
-; MATRIX-TRACE-NEXT: Processing LoopList of size = 2 containing the following loops:
-; MATRIX-TRACE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; MATRIX-TRACE-NEXT: Splitting the inner loop latch
+; MATRIX-TRACE-NEXT: splitting InnerLoopHeader done
+; MATRIX-TRACE-NEXT: adjustLoopBranches called
+; MATRIX-TRACE-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX-TRACE-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
+; MATRIX-TRACE-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, top-level sibling, in function 'noalias_in_nest'.
+; MATRIX-TRACE: No Valid candidates for loop interchange.
; MATRIX-TRACE-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'matrix_memory_ratio' with 1 path block(s), 66 instruction(s), and 0 rematerialized loop-local definition(s).
; MATRIX-TRACE-NEXT: loop-interchange: outer-epilogue preparation memory budget: 9 loads/stores over 92 instructions.
; MATRIX-TRACE-NEXT: Found 8 Loads and Stores to analyze
@@ -322,13 +351,23 @@
; MATRIX-TRACE-READY: loop-interchange: prepared a complete outer-epilogue fission plan in function 'matrix_memory_ratio': Outer 'outer.header', Inner 'inner.header', absolute columns 0/1, routing columns 0/1, 0 cross dependence(s).
; MATRIX-TRACE-READY-NEXT: loop-interchange: prepared function=matrix_memory_ratio{{ }}
; MATRIX-TRACE-READY-SAME: rematerialized=0{{ }}
-; MATRIX-TRACE-NEXT: Processing LoopList of size = 2 containing the following loops:
-; MATRIX-TRACE: Found 9 Loads and Stores to analyze
-; MATRIX-TRACE: Dependency matrix before interchange:
-; MATRIX-TRACE-NEXT: {{^}}= ={{ *$}}
-; MATRIX-TRACE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
-; MATRIX-TRACE: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
-; MATRIX-TRACE-NEXT: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; MATRIX-TRACE-READY-NEXT: Splitting the inner loop latch
+; MATRIX-TRACE-READY-NEXT: splitting InnerLoopHeader done
+; MATRIX-TRACE-READY-NEXT: adjustLoopBranches called
+; MATRIX-TRACE-READY-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX-TRACE-READY-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
+; MATRIX-TRACE-READY-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, top-level sibling, in function 'matrix_memory_ratio'.
+; MATRIX-TRACE-READY: No Valid candidates for loop interchange.
+;
+; Only the ratio-limited run still routes this function through ordinary
+; interchange, so the trailing legality trace belongs to that run alone.
+; MATRIX-TRACE-LIMIT: Processing LoopList of size = 2 containing the following loops:
+; MATRIX-TRACE-LIMIT: Found 9 Loads and Stores to analyze
+; MATRIX-TRACE-LIMIT: Dependency matrix before interchange:
+; MATRIX-TRACE-LIMIT-NEXT: {{^}}= ={{ *$}}
+; MATRIX-TRACE-LIMIT-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
+; MATRIX-TRACE-LIMIT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; MATRIX-TRACE-LIMIT-NEXT: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
;
; LE-PREP-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'direction_le' with 1 path block(s), 8 instruction(s), and 0 rematerialized loop-local definition(s).
; LE-PREP-NEXT: loop-interchange: outer-epilogue preparation memory budget: 4 loads/stores over 23 instructions.
@@ -439,6 +478,185 @@
; SEL-NE-NEXT: {{^}}* I{{ *$}}
; SEL-NE-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
; SEL-NE: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+;
+; The transformed module. Every definition is listed, so the RUN line's
+; '{{^define }}' exclusion makes the list exhaustive. Each applied function
+; gains one sibling epilogue loop whose latch branches to the original
+; continuation, and its nest is interchanged. The emptied original epilogue
+; block, the epilogue loop, and the continuation are checked instruction by
+; instruction. Block labels are plain directives because the printed IR
+; separates blocks with a blank line. The other functions were already shown
+; unchanged by the llvm-extract --delete pair above and carry only their label.
+;
+; APPLIED-LABEL: define {{.*}}@e_to_n_flow(
+; APPLIED-LABEL: define {{.*}}@e_to_n_anti(
+; APPLIED-LABEL: define {{.*}}@e_to_n_output(
+; APPLIED-LABEL: define {{.*}}@e_consumes_reduction(
+; APPLIED-LABEL: define {{.*}}@e_consumes_inner_iv(
+; APPLIED-LABEL: define {{.*}}@e_escapes(
+; APPLIED-LABEL: define {{.*}}@exact_recurrence(
+; APPLIED-LABEL: define {{.*}}@partly_exact_recurrence(
+; APPLIED-LABEL: define {{.*}}@call_in_epilogue(
+; APPLIED-LABEL: define {{.*}}@convergent_call_epilogue(
+; APPLIED-LABEL: define {{.*}}@deopt_call_epilogue(
+; APPLIED-LABEL: define {{.*}}@constrained_fp_epilogue(
+; APPLIED-LABEL: define {{.*}}@unknown_alias(
+; APPLIED-LABEL: define {{.*}}@non_affine_address(
+; APPLIED-LABEL: define {{.*}}@volatile_epilogue(
+; APPLIED-LABEL: define {{.*}}@atomic_epilogue(
+; APPLIED-LABEL: define {{.*}}@fence_epilogue(
+; APPLIED-LABEL: define {{.*}}@conditional_epilogue(
+; APPLIED-LABEL: define {{.*}}@multiple_epilogues(
+; APPLIED-LABEL: define {{.*}}@address_taken_epilogue(
+; APPLIED-LABEL: define {{.*}}@triangular_bounds(
+; APPLIED-LABEL: define {{.*}}@data_dependent_bounds(
+; APPLIED-LABEL: define {{.*}}@loop_local_outer_bound(
+; APPLIED-LABEL: define {{.*}}@noncanonical_latch(
+; APPLIED-LABEL: define {{.*}}@unsupported_metadata(
+; APPLIED-LABEL: define {{.*}}@outer_header_freeze(
+; APPLIED-LABEL: define {{.*}}@invariant_address_n_to_e_flow(
+; APPLIED-LABEL: define {{.*}}@invariant_address_n_to_e_output(
+; APPLIED-LABEL: define {{.*}}@direction_le(
+; APPLIED-LABEL: define {{.*}}@direction_ge(
+; APPLIED-LABEL: define {{.*}}@direction_ne(
+; APPLIED-LABEL: define {{.*}}@e_data_from_shared_preheader_freeze(
+; APPLIED-LABEL: define {{.*}}@e_data_from_inner_preheader_load(
+; APPLIED-LABEL: define {{.*}}@e_control_from_shared_preheader(
+; APPLIED-LABEL: define {{.*}}@e_control_from_inner_preheader(
+; APPLIED-LABEL: define {{.*}}@e_operand_closure_invalid_leaf(
+; APPLIED-LABEL: define {{.*}}@n_convergent_controlled_in_nest(
+; APPLIED-LABEL: define {{.*}}@n_convergent_uncontrolled_in_outer_header(
+; APPLIED-LABEL: define {{.*}}@n_convergent_uncontrolled_in_inner_body(
+; APPLIED-LABEL: define {{.*}}@n_convergent_uncontrolled_in_inner_ph(
+; APPLIED-LABEL: define {{.*}}@direction_selected_le(
+; APPLIED-LABEL: define {{.*}}@direction_selected_ge(
+; APPLIED-LABEL: define {{.*}}@direction_selected_ne(
+; APPLIED-LABEL: define {{.*}}@unknown_alias_in_nest(
+; APPLIED-LABEL: define {{.*}}@noalias_in_nest(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %epilogue.iv, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@matrix_memory_ratio(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %v01.epil = add i64 %seed, %epilogue.iv
+; APPLIED-NEXT: %v02.epil = add i64 %v01.epil, 1
+; APPLIED-NEXT: %v03.epil = add i64 %v02.epil, 1
+; APPLIED-NEXT: %v04.epil = add i64 %v03.epil, 1
+; APPLIED-NEXT: %v05.epil = add i64 %v04.epil, 1
+; APPLIED-NEXT: %v06.epil = add i64 %v05.epil, 1
+; APPLIED-NEXT: %v07.epil = add i64 %v06.epil, 1
+; APPLIED-NEXT: %v08.epil = add i64 %v07.epil, 1
+; APPLIED-NEXT: %v09.epil = add i64 %v08.epil, 1
+; APPLIED-NEXT: %v10.epil = add i64 %v09.epil, 1
+; APPLIED-NEXT: %v11.epil = add i64 %v10.epil, 1
+; APPLIED-NEXT: %v12.epil = add i64 %v11.epil, 1
+; APPLIED-NEXT: %v13.epil = add i64 %v12.epil, 1
+; APPLIED-NEXT: %v14.epil = add i64 %v13.epil, 1
+; APPLIED-NEXT: %v15.epil = add i64 %v14.epil, 1
+; APPLIED-NEXT: %v16.epil = add i64 %v15.epil, 1
+; APPLIED-NEXT: %v17.epil = add i64 %v16.epil, 1
+; APPLIED-NEXT: %v18.epil = add i64 %v17.epil, 1
+; APPLIED-NEXT: %v19.epil = add i64 %v18.epil, 1
+; APPLIED-NEXT: %v20.epil = add i64 %v19.epil, 1
+; APPLIED-NEXT: %v21.epil = add i64 %v20.epil, 1
+; APPLIED-NEXT: %v22.epil = add i64 %v21.epil, 1
+; APPLIED-NEXT: %v23.epil = add i64 %v22.epil, 1
+; APPLIED-NEXT: %v24.epil = add i64 %v23.epil, 1
+; APPLIED-NEXT: %v25.epil = add i64 %v24.epil, 1
+; APPLIED-NEXT: %v26.epil = add i64 %v25.epil, 1
+; APPLIED-NEXT: %v27.epil = add i64 %v26.epil, 1
+; APPLIED-NEXT: %v28.epil = add i64 %v27.epil, 1
+; APPLIED-NEXT: %v29.epil = add i64 %v28.epil, 1
+; APPLIED-NEXT: %v30.epil = add i64 %v29.epil, 1
+; APPLIED-NEXT: %v31.epil = add i64 %v30.epil, 1
+; APPLIED-NEXT: %v32.epil = add i64 %v31.epil, 1
+; APPLIED-NEXT: %v33.epil = add i64 %v32.epil, 1
+; APPLIED-NEXT: %v34.epil = add i64 %v33.epil, 1
+; APPLIED-NEXT: %v35.epil = add i64 %v34.epil, 1
+; APPLIED-NEXT: %v36.epil = add i64 %v35.epil, 1
+; APPLIED-NEXT: %v37.epil = add i64 %v36.epil, 1
+; APPLIED-NEXT: %v38.epil = add i64 %v37.epil, 1
+; APPLIED-NEXT: %v39.epil = add i64 %v38.epil, 1
+; APPLIED-NEXT: %v40.epil = add i64 %v39.epil, 1
+; APPLIED-NEXT: %v41.epil = add i64 %v40.epil, 1
+; APPLIED-NEXT: %v42.epil = add i64 %v41.epil, 1
+; APPLIED-NEXT: %v43.epil = add i64 %v42.epil, 1
+; APPLIED-NEXT: %v44.epil = add i64 %v43.epil, 1
+; APPLIED-NEXT: %v45.epil = add i64 %v44.epil, 1
+; APPLIED-NEXT: %v46.epil = add i64 %v45.epil, 1
+; APPLIED-NEXT: %v47.epil = add i64 %v46.epil, 1
+; APPLIED-NEXT: %v48.epil = add i64 %v47.epil, 1
+; APPLIED-NEXT: %v49.epil = add i64 %v48.epil, 1
+; APPLIED-NEXT: %v50.epil = add i64 %v49.epil, 1
+; APPLIED-NEXT: %v51.epil = add i64 %v50.epil, 1
+; APPLIED-NEXT: %v52.epil = add i64 %v51.epil, 1
+; APPLIED-NEXT: %v53.epil = add i64 %v52.epil, 1
+; APPLIED-NEXT: %v54.epil = add i64 %v53.epil, 1
+; APPLIED-NEXT: %v55.epil = add i64 %v54.epil, 1
+; APPLIED-NEXT: %v56.epil = add i64 %v55.epil, 1
+; APPLIED-NEXT: %v57.epil = add i64 %v56.epil, 1
+; APPLIED-NEXT: %v58.epil = add i64 %v57.epil, 1
+; APPLIED-NEXT: %v59.epil = add i64 %v58.epil, 1
+; APPLIED-NEXT: %v60.epil = add i64 %v59.epil, 1
+; APPLIED-NEXT: %v61.epil = add i64 %v60.epil, 1
+; APPLIED-NEXT: %v62.epil = add i64 %v61.epil, 1
+; APPLIED-NEXT: %v63.epil = add i64 %v62.epil, 1
+; APPLIED-NEXT: %v64.epil = add i64 %v63.epil, 1
+; APPLIED-NEXT: %outp.epil = getelementptr i64, ptr %Out, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %v64.epil, ptr %outp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@issue47457_guarded_epilogue(
+;
+; The same loop-info lines are checked against the loop manager's live
+; LoopInfo and against a fresh rebuild of the transformed module. The block
+; lists inside the nest are wildcarded because LoopInfo stores them in
+; discovery order, which an incremental update and a fresh walk reach
+; differently; the loop order, the depths, and the epilogue loop's two blocks
+; are exact.
+;
+; APPLY-LOOPS-LABEL: Loop info for function 'noalias_in_nest':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'matrix_memory_ratio':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+;
+; At ratio one the ratio-limited function never reaches a matrix, so it keeps
+; its shape while the noalias control is still distributed.
+; RATIO-LIMITED-LABEL: define {{.*}}@noalias_in_nest(
+; RATIO-LIMITED: epilogue.header:
+; RATIO-LIMITED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; RATIO-LIMITED-LABEL: define {{.*}}@matrix_memory_ratio(
+; RATIO-LIMITED-NOT: %epilogue.iv = phi
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
index 5f6f21cdcde1c8..60d0c1962d1bf0 100644
--- a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-routing.ll
@@ -20,15 +20,75 @@
; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
; DEFINE: %{prep_checks} = --implicit-check-not='loop-interchange:'
+; DEFINE: %{applied} = \
+; DEFINE: --func=r1_r2_length3_simple_ancestor \
+; DEFINE: --func=r10_r11_budget_slot_sequence \
+; DEFINE: --func=r12_two_static_collected_order \
+; DEFINE: --func=r13_explicit_cache_nested_chain \
+; DEFINE: --func=r15_nested_scalar_all \
+; DEFINE: --func=r5_first_static_then_later_ordinary \
+; DEFINE: --func=r6_runtime_then_static --func=r7_rejected_then_static \
+; DEFINE: --func=r8_ineligible_then_static \
+; DEFINE: --func=top_level_middle_epilogue_order \
+; DEFINE: --func=r16_depth3_nonempty_fission_row
+; DEFINE: %{applied_budget1} = \
+; DEFINE: --func=r1_r2_length3_simple_ancestor \
+; DEFINE: --func=r12_two_static_collected_order \
+; DEFINE: --func=r13_explicit_cache_nested_chain \
+; DEFINE: --func=r15_nested_scalar_all \
+; DEFINE: --func=r5_first_static_then_later_ordinary \
+; DEFINE: --func=r8_ineligible_then_static \
+; DEFINE: --func=top_level_middle_epilogue_order \
+; DEFINE: --func=r16_depth3_nonempty_fission_row
+; DEFINE: %{applied_budget2} = \
+; DEFINE: --func=r1_r2_length3_simple_ancestor \
+; DEFINE: --func=r12_two_static_collected_order \
+; DEFINE: --func=r13_explicit_cache_nested_chain \
+; DEFINE: --func=r15_nested_scalar_all \
+; DEFINE: --func=r5_first_static_then_later_ordinary \
+; DEFINE: --func=r6_runtime_then_static --func=r7_rejected_then_static \
+; DEFINE: --func=r8_ineligible_then_static \
+; DEFINE: --func=top_level_middle_epilogue_order \
+; DEFINE: --func=r16_depth3_nonempty_fission_row
+; DEFINE: %{applied_depth22} = \
+; DEFINE: --func=r10_r11_budget_slot_sequence \
+; DEFINE: --func=r12_two_static_collected_order \
+; DEFINE: --func=r13_explicit_cache_nested_chain \
+; DEFINE: --func=r15_nested_scalar_all \
+; DEFINE: --func=r5_first_static_then_later_ordinary \
+; DEFINE: --func=r6_runtime_then_static --func=r7_rejected_then_static \
+; DEFINE: --func=r8_ineligible_then_static \
+; DEFINE: --func=top_level_middle_epilogue_order
+; DEFINE: %{applied_cache} = \
+; DEFINE: --func=r1_r2_length3_simple_ancestor \
+; DEFINE: --func=top_level_middle_epilogue_order \
+; DEFINE: --func=r16_depth3_nonempty_fission_row
+; DEFINE: %{no_versioning} = \
+; DEFINE: --implicit-check-not='.lver' \
+; DEFINE: --implicit-check-not='!alias.scope' \
+; DEFINE: --implicit-check-not='!noalias' \
+; DEFINE: --implicit-check-not='llvm.loop.interchange.runtime_versioned' \
+; DEFINE: --implicit-check-not='LoopVersioning'
;
-; Preparation is analysis-only: a complete plan is printed and then discarded,
-; so every function keeps its ordinary result. PREP-COMMON holds the decisions
+; With the option on, every function in %{applied} is transformed and every
+; other function keeps its ordinary result. PREP-COMMON holds the decisions
; every configuration makes; PREP-LATER those reachable with at least two
; candidate slots; CHAIN3 those needing an eligible length-three chain; PREP
; and BUDGET2 distinguish reaching the third attempt from stopping at two.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.prep.stderr %{prep_checks}
-; RUN: diff -u %t.default %t.prep
+; RUN: llvm-extract -S --delete %{applied} %t.default -o %t.default.rest
+; RUN: llvm-extract -S --delete %{applied} %t.prep -o %t.prep.rest
+; RUN: diff -u -I '^; ModuleID' %t.default.rest %t.prep.rest
+; RUN: FileCheck %s --check-prefix=APPLIED --input-file=%t.prep %{no_versioning} --implicit-check-not='{{^define }}'
+;
+; The updated LoopInfo must match a fresh rebuild, sibling order included. A
+; second run of the pass reaches the next candidate of a two-candidate
+; function instead of distributing the first one again.
+; RUN: opt -passes='loop(loop-interchange),print<loops>' -cache-line-size=64 %{policy} -loop-interchange-outer-epilogue-fission -disable-output %s 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -passes='print<loops>' -disable-output %t.prep 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -S -passes='loop(loop-interchange,loop-interchange)' -cache-line-size=64 %{policy} %{prepare} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.twice
+; RUN: FileCheck %s --check-prefix=TWICE --input-file=%t.twice --implicit-check-not='%epilogue.iv{{[0-9]*}} = phi'
;
; A zero candidate budget does no feature work. One slot stops after a
; rejection or a runtime plan but skips an ineligible chain for free. An
@@ -41,12 +101,16 @@
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=1 -loop-interchange-outer-epilogue-fission=false %s -o %t.budget1.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=1 %{prepare} %{remarks} %s -o %t.budget1.on 2> %t.budget1.stderr
; RUN: diff -u %t.default %t.budget1.off
-; RUN: diff -u %t.budget1.off %t.budget1.on
+; RUN: llvm-extract -S --delete %{applied_budget1} %t.budget1.off -o %t.budget1.off.rest
+; RUN: llvm-extract -S --delete %{applied_budget1} %t.budget1.on -o %t.budget1.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.budget1.off.rest %t.budget1.on.rest
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,CHAIN3,BUDGET1 --input-file=%t.budget1.stderr %{prep_checks}
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=2 -loop-interchange-outer-epilogue-fission=false %s -o %t.budget2.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-outer-epilogue-fission-candidates=2 %{prepare} %{remarks} %s -o %t.budget2.on 2> %t.budget2.stderr
; RUN: diff -u %t.default %t.budget2.off
-; RUN: diff -u %t.budget2.off %t.budget2.on
+; RUN: llvm-extract -S --delete %{applied_budget2} %t.budget2.off -o %t.budget2.off.rest
+; RUN: llvm-extract -S --delete %{applied_budget2} %t.budget2.on -o %t.budget2.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.budget2.off.rest %t.budget2.on.rest
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,BUDGET2 --input-file=%t.budget2.stderr %{prep_checks}
;
; Chain depth and logical pair depth are separate gates. At [2,2], r1 is
@@ -56,11 +120,13 @@
; Raising the maximum to eleven reaches r4's absolute-prefix rejection.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=2 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth22.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=2 %{prepare} %{remarks} %s -o %t.depth22.on 2> %t.depth22.stderr
-; RUN: diff -u %t.depth22.off %t.depth22.on
+; RUN: llvm-extract -S --delete %{applied_depth22} %t.depth22.off -o %t.depth22.off.rest
+; RUN: llvm-extract -S --delete %{applied_depth22} %t.depth22.on -o %t.depth22.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.depth22.off.rest %t.depth22.on.rest
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,PREP --input-file=%t.depth22.stderr %{prep_checks}
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=3 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth23.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=3 %{prepare} %{remarks} %s -o %t.depth23.on 2> %t.depth23.stderr
-; RUN: diff -u %t.depth23.off %t.depth23.on
+; RUN: diff -u %t.prep %t.depth23.on
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.depth23.stderr %{prep_checks}
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=3 -loop-interchange-max-loop-nest-depth=3 -loop-interchange-outer-epilogue-fission=false %{remarks} %s -o %t.depth33.off 2> %t.depth33.off.stderr
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=3 -loop-interchange-max-loop-nest-depth=3 %{prepare} %{remarks} %s -o %t.depth33.on 2> %t.depth33.on.stderr
@@ -68,7 +134,7 @@
; RUN: diff -u %t.depth33.off.stderr %t.depth33.on.stderr
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=11 -loop-interchange-outer-epilogue-fission=false %s -o %t.depth211.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-min-loop-nest-depth=2 -loop-interchange-max-loop-nest-depth=11 %{prepare} %{remarks} %s -o %t.depth211.on 2> %t.depth211.stderr
-; RUN: diff -u %t.depth211.off %t.depth211.on
+; RUN: diff -u %t.prep %t.depth211.on
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP,DEPTH11 --input-file=%t.depth211.stderr %{prep_checks}
;
; Cache uses the collected-chain root. r1 and the top-level middle pair remain
@@ -78,12 +144,16 @@
; both ordinary r9 chains interchange; do not impose the default r9 shape.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=cache -loop-interchange-outer-epilogue-fission=false %s -o %t.cache.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=cache %{prepare} %{remarks} %s -o %t.cache.on 2> %t.cache.stderr
-; RUN: diff -u %t.cache.off %t.cache.on
+; RUN: llvm-extract -S --delete %{applied_cache} %t.cache.off -o %t.cache.off.rest
+; RUN: llvm-extract -S --delete %{applied_cache} %t.cache.on -o %t.cache.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.cache.off.rest %t.cache.on.rest
; RUN: FileCheck %s --check-prefixes=CHAIN3,CACHE --input-file=%t.cache.stderr %{prep_checks}
; RUN: FileCheck %s --check-prefix=CACHE-IR --input-file=%t.cache.on
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=ignore -loop-interchange-outer-epilogue-fission=false %s -o %t.ignore.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -loop-interchange-profitabilities=ignore %{prepare} %{remarks} %s -o %t.ignore.on 2> %t.ignore.stderr
-; RUN: diff -u %t.ignore.off %t.ignore.on
+; RUN: llvm-extract -S --delete %{applied} %t.ignore.off -o %t.ignore.off.rest
+; RUN: llvm-extract -S --delete %{applied} %t.ignore.on -o %t.ignore.on.rest
+; RUN: diff -u -I '^; ModuleID' %t.ignore.off.rest %t.ignore.on.rest
; RUN: FileCheck %s --check-prefixes=PREP-COMMON,PREP-LATER,CHAIN3,PREP --input-file=%t.ignore.stderr %{prep_checks}
; RUN: FileCheck %s --check-prefix=IGNORE --input-file=%t.ignore.stderr
; RUN: FileCheck %s --check-prefix=IGNORE-IR --input-file=%t.ignore.on
@@ -110,6 +180,8 @@
; CHAIN3-SAME: absolute-depth=3 routing-depth=3 fission-rows=0 routing-rows=1 cross-deps=1 reductions=2
; CHAIN3-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; CHAIN3-SAME: requirement-ids=[[R1_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R1_ID]]:object-containment/static/by-constant-max{{$}}
+; CHAIN3-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; CHAIN3-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-COMMON-LABEL: loop-interchange: rejected function=r9_no_static_two_ordinary{{ }}
; PREP-COMMON-SAME: outer=for.j.header inner=for.r.header reason=post-inner region has no material epilogue memory slice{{$}}
; PREP-LATER-NEXT: loop-interchange: rejected function=r9_no_static_two_ordinary outer=for.k.header inner=for.l.header reason=post-inner region has no material epilogue memory slice{{$}}
@@ -127,6 +199,8 @@
; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[R10_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R10_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; BUDGET2-LABEL: loop-interchange: prepared function=r10_r11_budget_slot_sequence{{ }}
; BUDGET2-SAME: outer=runtime.outer inner=runtime.inner path-blocks=1 epilogue-insts=4 rematerialized=0
; BUDGET2-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
@@ -144,6 +218,8 @@
; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-COMMON-SAME: requirement-ids=[[R12_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R12_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; CACHE-LABEL: loop-interchange: rejected function=r12_two_static_collected_order{{ }}
; CACHE-SAME: outer=first.outer inner=first.inner reason=virtual extracted nest failed existing default profitability{{$}}
; CACHE: loop-interchange: rejected function=r12_two_static_collected_order outer=second.outer inner=second.inner reason=virtual extracted nest failed existing default profitability{{$}}
@@ -152,6 +228,8 @@
; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-COMMON-SAME: requirement-ids=[[R13_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R13_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; CACHE-LABEL: loop-interchange: rejected function=r13_explicit_cache_nested_chain{{ }}
; CACHE-SAME: outer=candidate.outer inner=candidate.inner reason=virtual extracted nest failed existing default profitability{{$}}
;
@@ -166,6 +244,8 @@
; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-COMMON-SAME: requirement-ids=[[R15_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R15_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; CACHE-LABEL: loop-interchange: rejected function=r15_nested_scalar_all{{ }}
; CACHE-SAME: outer=outer.header inner=inner.header reason=virtual extracted nest failed existing default profitability{{$}}
;
@@ -191,6 +271,8 @@
; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-COMMON-SAME: requirement-ids=[[R5_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R5_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; CACHE-LABEL: loop-interchange: rejected function=r5_first_static_then_later_ordinary{{ }}
; CACHE-SAME: outer=first.outer inner=first.inner reason=virtual extracted nest failed existing default profitability{{$}}
; CACHE: loop-interchange: rejected function=r5_first_static_then_later_ordinary outer=second.outer inner=second.inner reason=post-inner region has no material epilogue memory slice{{$}}
@@ -200,6 +282,8 @@
; PREP-LATER-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-LATER-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-LATER-SAME: requirement-ids=[[R6_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R6_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LATER-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-LATER-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; BUDGET1-LABEL: loop-interchange: prepared function=r6_runtime_then_static{{ }}
; BUDGET1-SAME: outer=runtime.outer inner=runtime.inner path-blocks=1 epilogue-insts=4 rematerialized=0
; BUDGET1-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
@@ -219,6 +303,8 @@
; PREP-LATER-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-LATER-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-LATER-SAME: requirement-ids=[[R7_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R7_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-LATER-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-LATER-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; CACHE-LABEL: loop-interchange: rejected function=r7_rejected_then_static{{ }}
; CACHE-SAME: outer=reject.outer inner=reject.inner reason=cross-partition dependence is not statically proved N-to-E{{$}}
; CACHE: loop-interchange: rejected function=r7_rejected_then_static outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
@@ -228,6 +314,8 @@
; PREP-COMMON-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-COMMON-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-COMMON-SAME: requirement-ids=[[R8_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[R8_ID]]:object-containment/static/by-constant-max{{$}}
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; CACHE-LABEL: loop-interchange: rejected function=r8_ineligible_then_static{{ }}
; CACHE-SAME: outer=static.outer inner=static.inner reason=virtual extracted nest failed existing default profitability{{$}}
;
@@ -235,14 +323,18 @@
; PREP-COMMON-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
; PREP-COMMON-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; PREP-COMMON-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-COMMON-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
+; PREP-COMMON-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; PREP-COMMON-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; PREP-COMMON-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; CACHE-LABEL: loop-interchange: prepared function=top_level_middle_epilogue_order{{ }}
; CACHE-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
; CACHE-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; CACHE-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; CACHE-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; CACHE-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; CACHE-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
+; CACHE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; CACHE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; CACHE-NEXT: remark: <unknown>:0:0: Computed dependence info, invoking the transform.
; The depth-3 positive keeps both speculative matrices nonempty, so its
@@ -254,8 +346,8 @@
; CHAIN3-SAME: outer=l2.header inner=l3.header path-blocks=1 epilogue-insts=2 rematerialized=0
; CHAIN3-SAME: absolute-depth=3 routing-depth=3 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; CHAIN3-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; CHAIN3-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
-; CHAIN3-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; CHAIN3-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; CHAIN3-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; IGNORE-LABEL: loop-interchange: rejected function=r9_no_static_two_ordinary{{ }}
; IGNORE-SAME: outer=for.j.header inner=for.r.header reason=post-inner region has no material epilogue memory slice{{$}}
@@ -301,14 +393,16 @@
; MATRIX3: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r1_r2_length3_simple_ancestor': Outer 'outer.header', Inner 'inner.header', absolute columns 1/2, routing columns 1/2, 1 cross dependence(s).
; MATRIX3-NEXT: loop-interchange: prepared function=r1_r2_length3_simple_ancestor{{ }}
; MATRIX3-SAME: rematerialized=0{{ }}
-; MATRIX3: Processing LoopList of size = 3 containing the following loops:
-; MATRIX3: Found 6 Loads and Stores to analyze
-; MATRIX3: Dependency matrix before interchange:
-; MATRIX3-NEXT: {{^}}* I I{{ *$}}
-; MATRIX3-NEXT: {{^}}* * I{{ *$}}
-; MATRIX3-NEXT: {{^}}* = I{{ *$}}
-; MATRIX3-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
-; MATRIX3: Cannot prove legality, not interchanging loops 'root.header' and 'outer.header'
+; MATRIX3-NEXT: Splitting the inner loop latch
+; MATRIX3-NEXT: splitting InnerLoopHeader done
+; MATRIX3-NEXT: adjustLoopBranches called
+; MATRIX3-NEXT: Outer loop reduction PHIs:
+; MATRIX3-NEXT: %sum.i = phi i64 [ %sum.next, %outer.latch ], [ 0, %outer.header.preheader ]
+; MATRIX3-NEXT: Inner loop reduction PHIs:
+; MATRIX3-NEXT: %sum.j = phi i64 [ %sum.next.j, %inner.header.split ], [ %sum.i, %inner.header.preheader ]
+; MATRIX3-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX3-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
+; MATRIX3-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r1_r2_length3_simple_ancestor'.
;
; MATRIX-REJECT-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r3_length3_nonsimple_ancestor' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
; MATRIX-REJECT: width = 4, element bytes = 8, bounded modular equality implies equal outer iteration.
@@ -329,12 +423,16 @@
; MATRIX-NESTED: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r15_nested_scalar_all': Outer 'outer.header', Inner 'inner.header', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
; MATRIX-NESTED-NEXT: loop-interchange: prepared function=r15_nested_scalar_all{{ }}
; MATRIX-NESTED-SAME: rematerialized=0{{ }}
-; MATRIX-NESTED: Processing LoopList of size = 2 containing the following loops:
-; MATRIX-NESTED: Dependency matrix before interchange:
-; MATRIX-NESTED-NEXT: {{^}}* I{{ *$}}
-; MATRIX-NESTED-NEXT: {{^}}= I{{ *$}}
-; MATRIX-NESTED-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
-; MATRIX-NESTED: Cannot prove legality, not interchanging loops 'outer.header' and 'inner.header'
+; MATRIX-NESTED-NEXT: Splitting the inner loop latch
+; MATRIX-NESTED-NEXT: splitting InnerLoopHeader done
+; MATRIX-NESTED-NEXT: adjustLoopBranches called
+; MATRIX-NESTED-NEXT: Outer loop reduction PHIs:
+; MATRIX-NESTED-NEXT: %chk.i = phi double [ %chk.next, %outer.latch ], [ 0.000000e+00, %outer.header.preheader ]
+; MATRIX-NESTED-NEXT: Inner loop reduction PHIs:
+; MATRIX-NESTED-NEXT: %chk.j = phi double [ %chk.next.j, %inner.header.split ], [ %chk.i, %inner.header.preheader ]
+; MATRIX-NESTED-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX-NESTED-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
+; MATRIX-NESTED-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r15_nested_scalar_all'.
; The two speculative matrices carry independent domains. The plan summary
; gives the column indices: absolute columns 1/2 puts the selected outer at
; index 1, so index 0 is the ancestor column that the prefix proof reads, and
@@ -360,12 +458,12 @@
; MATRIX-R16-NEXT: loop-interchange: prepared function=r16_depth3_nonempty_fission_row{{ }}
; MATRIX-R16-SAME: rematerialized=0{{ }}
; MATRIX-R16-SAME: absolute-depth=3 routing-depth=3 fission-rows=1 routing-rows=1{{ }}
-; MATRIX-R16-NEXT: Processing LoopList of size = 3 containing the following loops:
-; MATRIX-R16: Dependency matrix before interchange:
-; MATRIX-R16-NEXT: {{^}}= = ={{ *$}}
-; MATRIX-R16-NEXT: {{^}}* = I{{ *$}}
-; MATRIX-R16-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1
-; MATRIX-R16: Cannot prove legality, not interchanging loops 'l1.header' and 'l2.header'
+; MATRIX-R16-NEXT: Splitting the inner loop latch
+; MATRIX-R16-NEXT: splitting InnerLoopHeader done
+; MATRIX-R16-NEXT: adjustLoopBranches called
+; MATRIX-R16-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; MATRIX-R16-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
+; MATRIX-R16-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r16_depth3_nonempty_fission_row'.
;
; SCAN-RUNTIME-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r6_runtime_then_static'
; SCAN-RUNTIME: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r6_runtime_then_static': Outer 'runtime.outer', Inner 'runtime.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
@@ -373,18 +471,31 @@
; SCAN-RUNTIME: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r6_runtime_then_static': Outer 'static.outer', Inner 'static.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
; SCAN-RUNTIME-NEXT: loop-interchange: prepared function=r6_runtime_then_static outer=static.outer inner=static.inner{{ }}
; SCAN-RUNTIME-SAME: rematerialized=0{{ }}
-; SCAN-RUNTIME-NEXT: Processing LoopList of size = 2 containing the following loops:
-; SCAN-RUNTIME: Cannot prove legality, not interchanging loops 'runtime.outer' and 'runtime.inner'
-; SCAN-RUNTIME: Cannot prove legality, not interchanging loops 'static.outer' and 'static.inner'
+; SCAN-RUNTIME-NEXT: Splitting the inner loop latch
+; SCAN-RUNTIME-NEXT: splitting InnerLoopHeader done
+; SCAN-RUNTIME-NEXT: adjustLoopBranches called
+; SCAN-RUNTIME-NEXT: Outer loop reduction PHIs:
+; SCAN-RUNTIME-NEXT: %ssum.i = phi i64 [ %ssum.next, %static.outer.latch ], [ %rsum.next.lcssa, %static.outer.preheader2 ]
+; SCAN-RUNTIME-NEXT: Inner loop reduction PHIs:
+; SCAN-RUNTIME-NEXT: %ssum.j = phi i64 [ %ssum.next.j, %static.inner.split ], [ %ssum.i, %static.inner.preheader ]
+; SCAN-RUNTIME-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; SCAN-RUNTIME-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
+; SCAN-RUNTIME-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r6_runtime_then_static'.
;
; SCAN-ORDER-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r12_two_static_collected_order'
; SCAN-ORDER: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r12_two_static_collected_order': Outer 'first.outer', Inner 'first.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
; SCAN-ORDER-NEXT: loop-interchange: prepared function=r12_two_static_collected_order outer=first.outer inner=first.inner{{ }}
; SCAN-ORDER-SAME: rematerialized=0{{ }}
-; SCAN-ORDER-NEXT: Processing LoopList of size = 2 containing the following loops:
-; SCAN-ORDER: Cannot prove legality, not interchanging loops 'first.outer' and 'first.inner'
-; SCAN-ORDER-NEXT: Processing LoopList of size = 2 containing the following loops:
-; SCAN-ORDER: Cannot prove legality, not interchanging loops 'second.outer' and 'second.inner'
+; SCAN-ORDER-NEXT: Splitting the inner loop latch
+; SCAN-ORDER-NEXT: splitting InnerLoopHeader done
+; SCAN-ORDER-NEXT: adjustLoopBranches called
+; SCAN-ORDER-NEXT: Outer loop reduction PHIs:
+; SCAN-ORDER-NEXT: %sum0.i = phi i64 [ %sum0.next, %first.outer.latch ], [ 0, %first.outer.preheader ]
+; SCAN-ORDER-NEXT: Inner loop reduction PHIs:
+; SCAN-ORDER-NEXT: %sum0.j = phi i64 [ %sum0.next.j, %first.inner.split ], [ %sum0.i, %first.inner.preheader ]
+; SCAN-ORDER-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; SCAN-ORDER-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
+; SCAN-ORDER-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r12_two_static_collected_order'.
;
; STRICT-LABEL: loop-interchange: discovered a closed outer-loop epilogue in function 'r14_strict_fp' with 1 path block(s), 4 instruction(s), and 0 rematerialized loop-local definition(s).
; STRICT-NEXT: loop-interchange: outer-epilogue preparation memory budget: 3 loads/stores over 20 instructions.
@@ -413,10 +524,16 @@
; SCAN-SKIP: loop-interchange: prepared a complete outer-epilogue fission plan in function 'r8_ineligible_then_static': Outer 'static.outer', Inner 'static.inner', absolute columns 1/2, routing columns 0/1, 1 cross dependence(s).
; SCAN-SKIP-NEXT: loop-interchange: prepared function=r8_ineligible_then_static outer=static.outer inner=static.inner{{ }}
; SCAN-SKIP-SAME: rematerialized=0{{ }}
-; SCAN-SKIP-NEXT: Couldn't compute backedge count
-; SCAN-SKIP-NEXT: Not valid loop candidate for interchange
-; SCAN-SKIP-NEXT: Processing LoopList of size = 2 containing the following loops:
-; SCAN-SKIP: Cannot prove legality, not interchanging loops 'static.outer' and 'static.inner'
+; SCAN-SKIP-NEXT: Splitting the inner loop latch
+; SCAN-SKIP-NEXT: splitting InnerLoopHeader done
+; SCAN-SKIP-NEXT: adjustLoopBranches called
+; SCAN-SKIP-NEXT: Outer loop reduction PHIs:
+; SCAN-SKIP-NEXT: %sum.i = phi i64 [ %sum.next, %static.outer.latch ], [ 0, %static.outer.preheader ]
+; SCAN-SKIP-NEXT: Inner loop reduction PHIs:
+; SCAN-SKIP-NEXT: %sum.j = phi i64 [ %sum.next.j, %static.inner.split ], [ %sum.i, %static.inner.preheader ]
+; SCAN-SKIP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; SCAN-SKIP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
+; SCAN-SKIP-NEXT: loop-interchange: materialized outer-epilogue fission + interchange, nested sibling, in function 'r8_ineligible_then_static'.
;
; CACHE-IR-LABEL: define void @r9_no_static_two_ordinary(
; CACHE-IR: for.k.header:
@@ -425,6 +542,386 @@
; CACHE-IR: %l = phi i64 [ 0, %for.k.header ], [ %l.next, %for.l.header ]
; CACHE-IR: {{^}}}{{$}}
+; The transformed module. Every definition is listed, so the RUN line's
+; '{{^define }}' exclusion makes the list exhaustive. Each applied function
+; gains one sibling epilogue loop whose latch branches to the original
+; continuation, and its nest is interchanged. The emptied original epilogue
+; block, the epilogue loop, and the continuation are checked instruction by
+; instruction. Block labels are plain directives because the printed IR
+; separates blocks with a blank line. The other functions were already shown
+; unchanged by the llvm-extract --delete pair above and carry only their label.
+;
+; APPLIED-LABEL: define {{.*}}@r1_r2_length3_simple_ancestor(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: root.latch:
+; APPLIED-NEXT: %sum.result = phi i64 [ %sum.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %diag.value.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %diag.next.epil = add i64 %diag.value.epil, 1
+; APPLIED-NEXT: store i64 %diag.next.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %root.latch.cont, label %epilogue.header
+; APPLIED: root.latch.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %R, i64 %k
+; APPLIED-NEXT: store i64 %sum.result, ptr %rk, align 8
+; APPLIED-NEXT: %k.next = add i64 %k, 1
+; APPLIED-NEXT: %k.done = icmp eq i64 %k.next, 2
+; APPLIED-NEXT: br i1 %k.done, label %exit, label %root.header
+; APPLIED-LABEL: define {{.*}}@r9_no_static_two_ordinary(
+; APPLIED-LABEL: define {{.*}}@r10_r11_budget_slot_sequence(
+; APPLIED: static.epilogue:
+; APPLIED-NEXT: br label %static.outer.latch
+; APPLIED: root.latch:
+; APPLIED-NEXT: %sum.result = phi i64 [ %sum.next, %static.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %StaticA, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %root.latch.cont, label %epilogue.header
+; APPLIED: root.latch.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %R, i64 %k
+; APPLIED-NEXT: store i64 %sum.result, ptr %rk, align 8
+; APPLIED-NEXT: %k.next = add i64 %k, 1
+; APPLIED-NEXT: %k.done = icmp eq i64 %k.next, 2
+; APPLIED-NEXT: br i1 %k.done, label %exit, label %root.header
+; APPLIED-LABEL: define {{.*}}@r12_two_static_collected_order(
+; APPLIED: first.epilogue:
+; APPLIED-NEXT: br label %first.outer.latch
+; APPLIED: second.outer.preheader:
+; APPLIED-NEXT: %sum0.next.lcssa = phi i64 [ %sum0.next, %first.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i0.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag0.epil = getelementptr inbounds [4 x i64], ptr %A0, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv0.epil = load i64, ptr %diag0.epil, align 8
+; APPLIED-NEXT: %dn0.epil = add i64 %dv0.epil, 11
+; APPLIED-NEXT: store i64 %dn0.epil, ptr %diag0.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i0.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i0.done.epil = icmp eq i64 %i0.next.epil, 4
+; APPLIED-NEXT: br i1 %i0.done.epil, label %second.outer.preheader.cont, label %epilogue.header
+; APPLIED: second.outer.preheader.cont:
+; APPLIED-NEXT: br label %second.outer
+; APPLIED-LABEL: define {{.*}}@r13_explicit_cache_nested_chain(
+; APPLIED: candidate.epilogue:
+; APPLIED-NEXT: br label %candidate.outer.latch
+; APPLIED: sibling.header.preheader:
+; APPLIED-NEXT: %sum.next.lcssa = phi i64 [ %sum.next, %candidate.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %sibling.header.preheader.cont, label %epilogue.header
+; APPLIED: sibling.header.preheader.cont:
+; APPLIED-NEXT: br label %sibling.header
+; APPLIED-LABEL: define {{.*}}@r14_strict_fp(
+; APPLIED-LABEL: define {{.*}}@r15_nested_scalar_all(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: outer.exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [1335 x double], ptr %D, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dn.epil = fadd double %dv.epil, 1.000000e+00
+; APPLIED-NEXT: store double %dn.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %outer.exit.cont, label %epilogue.header
+; APPLIED: outer.exit.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds double, ptr %R, i64 %k
+; APPLIED-NEXT: store double %chk.res, ptr %rk, align 8
+; APPLIED-NEXT: br label %sibling.header
+; APPLIED-LABEL: define {{.*}}@unsafe_prefix_then_ordinary(
+; APPLIED-LABEL: define {{.*}}@r3_length3_nonsimple_ancestor(
+; APPLIED-LABEL: define {{.*}}@r4_overdepth_tail_candidate(
+; APPLIED-LABEL: define {{.*}}@r4_uncomputable_ancestor(
+; APPLIED-LABEL: define {{.*}}@r5_first_static_then_later_ordinary(
+; APPLIED: first.epilogue:
+; APPLIED-NEXT: br label %first.outer.latch
+; APPLIED: second.outer.preheader:
+; APPLIED-NEXT: %sum.next.lcssa = phi i64 [ %sum.next, %first.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %second.outer.preheader.cont, label %epilogue.header
+; APPLIED: second.outer.preheader.cont:
+; APPLIED-NEXT: br label %second.outer
+; APPLIED-LABEL: define {{.*}}@r6_runtime_then_static(
+; APPLIED: static.epilogue:
+; APPLIED-NEXT: br label %static.outer.latch
+; APPLIED: root.latch:
+; APPLIED-NEXT: %final.sum = phi i64 [ %ssum.next, %static.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %si.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %sdiag.epil = getelementptr inbounds [4 x i64], ptr %A1, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %sdv.epil = load i64, ptr %sdiag.epil, align 8
+; APPLIED-NEXT: %sdn.epil = add i64 %sdv.epil, 2
+; APPLIED-NEXT: store i64 %sdn.epil, ptr %sdiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %si.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %si.done.epil = icmp eq i64 %si.next.epil, 4
+; APPLIED-NEXT: br i1 %si.done.epil, label %root.latch.cont, label %epilogue.header
+; APPLIED: root.latch.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %R, i64 %k
+; APPLIED-NEXT: store i64 %final.sum, ptr %rk, align 8
+; APPLIED-NEXT: %k.next = add i64 %k, 1
+; APPLIED-NEXT: %k.done = icmp eq i64 %k.next, 2
+; APPLIED-NEXT: br i1 %k.done, label %exit, label %root.header
+; APPLIED-LABEL: define {{.*}}@r7_rejected_then_static(
+; APPLIED: static.epilogue:
+; APPLIED-NEXT: br label %static.outer.latch
+; APPLIED: root.latch:
+; APPLIED-NEXT: %sum.result = phi i64 [ %sum.next, %static.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %root.latch.cont, label %epilogue.header
+; APPLIED: root.latch.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %R, i64 %k
+; APPLIED-NEXT: store i64 %sum.result, ptr %rk, align 8
+; APPLIED-NEXT: %k.next = add i64 %k, 1
+; APPLIED-NEXT: %k.done = icmp eq i64 %k.next, 2
+; APPLIED-NEXT: br i1 %k.done, label %exit, label %root.header
+; APPLIED-LABEL: define {{.*}}@r8_ineligible_then_static(
+; APPLIED: static.epilogue:
+; APPLIED-NEXT: br label %static.outer.latch
+; APPLIED: root.latch:
+; APPLIED-NEXT: %sum.result = phi i64 [ %sum.next, %static.inner.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %diag.epil = getelementptr inbounds [4 x i64], ptr %A, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %diag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %diag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %root.latch.cont, label %epilogue.header
+; APPLIED: root.latch.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %R, i64 %k
+; APPLIED-NEXT: store i64 %sum.result, ptr %rk, align 8
+; APPLIED-NEXT: %k.next = add i64 %k, 1
+; APPLIED-NEXT: %k.done = icmp eq i64 %k.next, 2
+; APPLIED-NEXT: br i1 %k.done, label %exit, label %root.header
+; APPLIED-LABEL: define {{.*}}@top_level_middle_epilogue_order(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %r.iv.1.epil = add i64 %epilogue.iv, %seed
+; APPLIED-NEXT: %r.invariant.1.epil = add i64 %bias, 17
+; APPLIED-NEXT: %r.invariant.2.epil = mul i64 %r.invariant.1.epil, 3
+; APPLIED-NEXT: %r.iv.2.epil = add i64 %r.iv.1.epil, %r.invariant.2.epil
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %r.iv.2.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, %limit
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: br label %after.ph
+; APPLIED-LABEL: define {{.*}}@r16_depth3_nonempty_fission_row(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %l2.latch
+; APPLIED: l1.latch:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr inbounds double, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store double 1.000000e+00, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %l1.latch.cont, label %epilogue.header
+; APPLIED: l1.latch.cont:
+; APPLIED-NEXT: %a.next = add nuw nsw i64 %a, 1
+; APPLIED-NEXT: %a.done = icmp eq i64 %a.next, 4
+; APPLIED-NEXT: br i1 %a.done, label %exit, label %l1.header
+;
+; The same loop-info lines are checked against the loop manager's live
+; LoopInfo and against a fresh rebuild of the transformed module. The block
+; lists inside the nest are wildcarded because LoopInfo stores them in
+; discovery order, which an incremental update and a fresh walk reach
+; differently; the loop order, the depths, and the epilogue loop's two blocks
+; are exact.
+;
+; APPLY-LOOPS-LABEL: Loop info for function 'r1_r2_length3_simple_ancestor':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r10_r11_budget_slot_sequence':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %ineligible.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %ineligible.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %reject.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %reject.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %runtime.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %runtime.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %static.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %static.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r12_two_static_collected_order':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %first.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %first.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %second.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %second.inner<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r13_explicit_cache_nested_chain':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %candidate.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %candidate.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %sibling.header<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r15_nested_scalar_all':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %sibling.header<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r5_first_static_then_later_ordinary':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %first.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %first.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %second.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %second.inner<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r6_runtime_then_static':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %runtime.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %runtime.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %static.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %static.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r7_rejected_then_static':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %reject.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %reject.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %static.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %static.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r8_ineligible_then_static':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %ineligible.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %ineligible.inner<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %static.inner<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %static.outer<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'top_level_middle_epilogue_order':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %after.header<header><latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %before.header<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'r16_depth3_nonempty_fission_row':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %l1.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %l3.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %l2.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+;
+; A second run offers the next candidate of a two-candidate function. Only
+; the second selection of r12_two_static_collected_order is a complete static
+; plan, so exactly one further epilogue appears and no distributed nest is
+; offered again. The RUN line excludes every other epilogue induction
+; variable.
+; TWICE-LABEL: define {{.*}}@r1_r2_length3_simple_ancestor(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r10_r11_budget_slot_sequence(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r12_two_static_collected_order(
+; TWICE: %epilogue.iv = phi i64
+; TWICE: %epilogue.iv5 = phi i64
+; TWICE-LABEL: define {{.*}}@r13_explicit_cache_nested_chain(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r15_nested_scalar_all(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r5_first_static_then_later_ordinary(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r6_runtime_then_static(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r7_rejected_then_static(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r8_ineligible_then_static(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@top_level_middle_epilogue_order(
+; TWICE: %epilogue.iv = phi i64
+; TWICE-LABEL: define {{.*}}@r16_depth3_nonempty_fission_row(
+; TWICE: %epilogue.iv = phi i64
+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
define void @r1_r2_length3_simple_ancestor(
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
index 80d6a0dfb43074..0f3efe70661f6c 100644
--- a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission-runtime.ll
@@ -12,44 +12,205 @@
; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+; DEFINE: %{applied} = \
+; DEFINE: --func=checksum_kernel_rect --func=checksum_kernel_latch \
+; DEFINE: --func=same_array_diag --func=nested_same_array_diag
+; DEFINE: %{no_versioning} = \
+; DEFINE: --implicit-check-not='.lver' \
+; DEFINE: --implicit-check-not='!alias.scope' \
+; DEFINE: --implicit-check-not='!noalias' \
+; DEFINE: --implicit-check-not='llvm.loop.interchange.runtime_versioned' \
+; DEFINE: --implicit-check-not='LoopVersioning'
;
-; Preparation is analysis-only: a complete plan is printed and then discarded,
-; so the IR is unchanged with the option on.
+; With the option on, every function in %{applied} is transformed and every
+; other function is unchanged.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
-; RUN: diff -u %t.out %t.prep
+; RUN: llvm-extract -S --delete %{applied} %t.out -o %t.out.rest
+; RUN: llvm-extract -S --delete %{applied} %t.prep -o %t.prep.rest
+; RUN: diff -u -I '^; ModuleID' %t.out.rest %t.prep.rest
+; RUN: FileCheck %s --check-prefix=APPLIED --input-file=%t.prep %{no_versioning} --implicit-check-not='{{^define }}'
+;
+; The updated LoopInfo must match a fresh rebuild, sibling order included, and
+; a second run of the pass must change nothing.
+; RUN: opt -passes='loop(loop-interchange),print<loops>' -cache-line-size=64 %{policy} -loop-interchange-outer-epilogue-fission -disable-output %s 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -passes='print<loops>' -disable-output %t.prep 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -S -passes='loop(loop-interchange,loop-interchange)' -cache-line-size=64 %{policy} %{prepare} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.twice
+; RUN: diff -u %t.prep %t.twice
;
; Each printed plan has passed both dependence matrices, legality,
-; profitability, and validation. The ordinary miss that follows it closes the
-; decision.
+; profitability, and validation. The two remarks after it come from the apply
+; step.
; PREP-LABEL: loop-interchange: prepared function=checksum_kernel_rect{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=8 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=checksum_kernel_latch{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=0 epilogue-insts=8 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=same_array_diag{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[SAME_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[SAME_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
; PREP-LABEL: loop-interchange: prepared function=nested_same_array_diag{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=4
; PREP-SAME: requirement-ids=[[NESTED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[NESTED_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=4.
;
; The guarded bypass of the inner loop is rejected at discovery.
; PREP-LABEL: loop-interchange: rejected function=checksum_kernel_zerotrip{{ }}
; PREP-SAME: outer=outer.header inner=inner.header reason=exit-to-latch region is not one dominated acyclic path{{$}}
; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; The transformed module. Every definition is listed, so the RUN line's
+; '{{^define }}' exclusion makes the list exhaustive. Each applied function
+; gains one sibling epilogue loop whose latch branches to the original
+; continuation, and its nest is interchanged. The emptied original epilogue
+; block, the epilogue loop, and the continuation are checked instruction by
+; instruction. Block labels are plain directives because the printed IR
+; separates blocks with a blank line. The other functions were already shown
+; unchanged by the llvm-extract --delete pair above and carry only their label.
+;
+; APPLIED-LABEL: define {{.*}}@checksum_kernel_rect(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %sum.res = phi i64 [ %sum.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ii1.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %dp.epil = getelementptr inbounds i64, ptr %diag, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %dp.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, %ii1.epil
+; APPLIED-NEXT: store i64 %dn.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: %ecv.epil = load i64, ptr %epcount, align 8
+; APPLIED-NEXT: %ecn.epil = add i64 %ecv.epil, 1
+; APPLIED-NEXT: store i64 %ecn.epil, ptr %epcount, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store i64 %sum.res, ptr %sumout, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@checksum_kernel_latch(
+; APPLIED: outer.latch:
+; APPLIED-NEXT: %i.next = add i64 %i, 1
+; APPLIED-NEXT: %i.ec = icmp eq i64 %i.next, %n
+; APPLIED-NEXT: br i1 %i.ec, label %inner.header.split, label %outer.header
+; APPLIED: exit:
+; APPLIED-NEXT: %sum.res = phi i64 [ %sum.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ii1.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %dp.epil = getelementptr inbounds i64, ptr %diag, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %dp.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, %ii1.epil
+; APPLIED-NEXT: store i64 %dn.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: %ecv.epil = load i64, ptr %epcount, align 8
+; APPLIED-NEXT: %ecn.epil = add i64 %ecv.epil, 1
+; APPLIED-NEXT: store i64 %ecn.epil, ptr %epcount, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, %n
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store i64 %sum.res, ptr %sumout, align 8
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@same_array_diag(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %sum.res = phi i64 [ %sum.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x i64], ptr %X, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 100
+; APPLIED-NEXT: store i64 %dn.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store i64 %sum.res, ptr %sumout, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@nested_same_array_diag(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: outer.exit:
+; APPLIED-NEXT: %sum.res = phi i64 [ %sum.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [4 x i64], ptr %X, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load i64, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dn.epil = add i64 %dv.epil, 1
+; APPLIED-NEXT: store i64 %dn.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.ec.epil, label %outer.exit.cont, label %epilogue.header
+; APPLIED: outer.exit.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds i64, ptr %rowsum, i64 %k
+; APPLIED-NEXT: %rkv = load i64, ptr %rk, align 8
+; APPLIED-NEXT: %rksum = add i64 %rkv, %sum.res
+; APPLIED-NEXT: store i64 %rksum, ptr %rk, align 8
+; APPLIED-NEXT: br label %sibling.header
+; APPLIED-LABEL: define {{.*}}@checksum_kernel_zerotrip(
+;
+; The same loop-info lines are checked against the loop manager's live
+; LoopInfo and against a fresh rebuild of the transformed module. The block
+; lists inside the nest are wildcarded because LoopInfo stores them in
+; discovery order, which an incremental update and a fresh walk reach
+; differently; the loop order, the depths, and the epilogue loop's two blocks
+; are exact.
+;
+; APPLY-LOOPS-LABEL: Loop info for function 'checksum_kernel_rect':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'checksum_kernel_latch':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'same_array_diag':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'nested_same_array_diag':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %sibling.header<header><latch><exiting>
+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@@ -271,3 +432,4 @@ exit:
done:
ret void
}
+
diff --git a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
index 8d7e40b1b8391f..c6585fcec3c391 100644
--- a/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
+++ b/llvm/test/Transforms/LoopInterchange/outer-epilogue-fission.ll
@@ -2,8 +2,8 @@
;
; Outer-loop epilogues with static bounds: the motivating checksum shape, the
; dependence directions between the nest N and the epilogue E, and the operand
-; roles E may consume. Every positive reaches a complete plan; the PREP lines
-; are the printed plans.
+; roles E may consume. Every positive is prepared and applied; the PREP lines
+; are the printed plans and the APPLIED lines the transformed IR.
;
; With the option off the pass leaves this module unchanged.
; RUN: opt -S -passes=no-op-loopnest %s -o %t.noop
@@ -15,63 +15,115 @@
; DEFINE: %{policy} = -loop-interchange-profitabilities=instorder,vectorize
; DEFINE: %{prepare} = -loop-interchange-outer-epilogue-fission -loop-interchange-print-prepared-plan
; DEFINE: %{remarks} = -pass-remarks=loop-interchange -pass-remarks-analysis=loop-interchange -pass-remarks-missed=loop-interchange
+; DEFINE: %{applied} = \
+; DEFINE: --func=checksum_diagonal_epilogue --func=n_to_e_flow \
+; DEFINE: --func=n_to_e_anti --func=n_to_e_output \
+; DEFINE: --func=multi_block_epilogue --func=nested_diagonal_epilogue \
+; DEFINE: --func=flattened_byte_gep_latch \
+; DEFINE: --func=e_data_from_shared_preheader_pure \
+; DEFINE: --func=e_data_from_inner_preheader_pure \
+; DEFINE: --func=e_data_from_outer_preheader_invariant \
+; DEFINE: --func=e_operand_closure_dag \
+; DEFINE: --func=n_nonconvergent_call_control \
+; DEFINE: --func=n_nonconvergent_outer_header_control \
+; DEFINE: --func=n_convergent_call_outside_outer --func=raw_lt_n_to_e
+; DEFINE: %{no_versioning} = \
+; DEFINE: --implicit-check-not='.lver' \
+; DEFINE: --implicit-check-not='!alias.scope' \
+; DEFINE: --implicit-check-not='!noalias' \
+; DEFINE: --implicit-check-not='llvm.loop.interchange.runtime_versioned' \
+; DEFINE: --implicit-check-not='LoopVersioning'
;
-; Preparation is analysis-only: a complete plan is printed and then discarded,
-; so the IR is unchanged with the option on.
+; With the option on, every function in %{applied} is transformed and every
+; other function is unchanged.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} %{prepare} %{remarks} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.prep 2> %t.prep.stderr
; RUN: FileCheck %s --check-prefix=PREP --input-file=%t.prep.stderr --implicit-check-not='loop-interchange:'
-; RUN: diff -u %t.out %t.prep
+; RUN: llvm-extract -S --delete %{applied} %t.out -o %t.out.rest
+; RUN: llvm-extract -S --delete %{applied} %t.prep -o %t.prep.rest
+; RUN: diff -u -I '^; ModuleID' %t.out.rest %t.prep.rest
+; RUN: FileCheck %s --check-prefix=APPLIED --input-file=%t.prep %{no_versioning} --implicit-check-not='{{^define }}'
;
; A zero memory-instruction ratio fails the shared N/E budget before any
-; dependence matrix is built.
+; dependence matrix is built, so nothing is transformed.
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 -loop-interchange-outer-epilogue-fission=false %s -o %t.ratio0.off
; RUN: opt -S -passes=loop-interchange -cache-line-size=64 %{policy} -loop-interchange-max-mem-instr-ratio=0 %{prepare} %{remarks} %s -o %t.ratio0.on 2> %t.ratio0.stderr
; RUN: diff -u %t.ratio0.off %t.ratio0.on
; RUN: FileCheck %s --check-prefix=MEMZERO --input-file=%t.ratio0.stderr --implicit-check-not='loop-interchange:'
;
+; The updated LoopInfo must match a fresh rebuild, sibling order included, and
+; a second run of the pass must change nothing.
+; RUN: opt -passes='loop(loop-interchange),print<loops>' -cache-line-size=64 %{policy} -loop-interchange-outer-epilogue-fission -disable-output %s 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -passes='print<loops>' -disable-output %t.prep 2>&1 | FileCheck %s --check-prefix=APPLY-LOOPS
+; RUN: opt -S -passes='loop(loop-interchange,loop-interchange)' -cache-line-size=64 %{policy} %{prepare} -verify-each -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa %s -o %t.twice
+; RUN: diff -u %t.prep %t.twice
+;
+; A SCEV cache primed before the transform must agree with a fresh computation
+; over the transformed function, for a top-level and for a nested candidate.
+; RUN: llvm-extract -func=checksum_diagonal_epilogue -S %s -o %t.one.ll
+; RUN: opt -disable-output -passes='print<scalar-evolution>' %t.one.ll 2> %t.one.before
+; RUN: opt -disable-output -cache-line-size=64 -verify-scev %{policy} -loop-interchange-outer-epilogue-fission -passes='print<scalar-evolution>,loop-interchange,print<scalar-evolution>' %t.one.ll 2> %t.one.primed
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -verify-scev %{policy} -loop-interchange-outer-epilogue-fission %t.one.ll -o %t.one.transformed
+; RUN: opt -disable-output -passes='print<scalar-evolution>' %t.one.transformed 2> %t.one.after
+; RUN: cat %t.one.before %t.one.after > %t.one.expected
+; RUN: diff -u %t.one.expected %t.one.primed
+; RUN: llvm-extract -func=nested_diagonal_epilogue -S %s -o %t.nested.ll
+; RUN: opt -disable-output -passes='print<scalar-evolution>' %t.nested.ll 2> %t.nested.before
+; RUN: opt -disable-output -cache-line-size=64 -verify-scev %{policy} -loop-interchange-outer-epilogue-fission -passes='print<scalar-evolution>,loop-interchange,print<scalar-evolution>' %t.nested.ll 2> %t.nested.primed
+; RUN: opt -S -passes=loop-interchange -cache-line-size=64 -verify-scev %{policy} -loop-interchange-outer-epilogue-fission %t.nested.ll -o %t.nested.transformed
+; RUN: opt -disable-output -passes='print<scalar-evolution>' %t.nested.transformed 2> %t.nested.after
+; RUN: cat %t.nested.before %t.nested.after > %t.nested.expected
+; RUN: diff -u %t.nested.expected %t.nested.primed
+;
; Each printed plan has passed both dependence matrices, legality,
-; profitability, and validation. The ordinary miss that follows it closes the
-; decision.
+; profitability, and validation. The two remarks after it come from the apply
+; step.
; PREP-LABEL: loop-interchange: prepared function=checksum_diagonal_epilogue{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=6
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[CHECKSUM_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[CHECKSUM_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: prepared function=n_to_e_flow{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=5 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[FLOW_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[FLOW_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: prepared function=n_to_e_anti{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=1
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=n_to_e_output{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=3 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[OUTPUT_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[OUTPUT_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: prepared function=multi_block_epilogue{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=2 epilogue-insts=8 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=2
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=nested_diagonal_epilogue{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=4 rematerialized=0
; PREP-SAME: absolute-depth=3 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[NESTED_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[NESTED_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
; PREP-LABEL: loop-interchange: prepared function=flattened_byte_gep_latch{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=0 epilogue-insts=6 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=2
; PREP-SAME: byte-offset-proofs=1 requirements=2 bound=static-bound W=1335
; PREP-SAME: requirement-ids=[[BYTE_ID:[0-9]+]]:modular-outer-span/static/by-constant-max,[[BYTE_ID]]:object-containment/static/by-constant-max{{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops due to dependences.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, W=1335.
;
; The shared and distinct pure operand chains include the two loop-local
; invariant definitions. The freeze outside the outer loop is retained rather
@@ -80,26 +132,30 @@
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=e_data_from_inner_preheader_pure{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=4
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=e_data_from_outer_preheader_invariant{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=1 routing-rows=1 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; The operand chain records each loop-local definition once, in post-order,
; including the shared operand and the duplicated operand: a, b, c, k1, k2, d,
-; e.
+; e. The APPLIED lines below check that order.
; PREP-LABEL: loop-interchange: prepared function=e_operand_closure_dag{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=7
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; The three convergence controls each reach a complete static plan. Two place
; a plain call where the paired negative (in the rejections test) places a
@@ -108,17 +164,20 @@
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=n_nonconvergent_outer_header_control{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
; PREP-LABEL: loop-interchange: prepared function=n_convergent_call_outside_outer{{ }}
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=2 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=0 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; The raw N/E distance is +1, a decisive LT direction, so this plan needs
; neither a byte-offset proof nor a bound requirement.
@@ -126,7 +185,8 @@
; PREP-SAME: outer=outer.header inner=inner.header path-blocks=1 epilogue-insts=3 rematerialized=0
; PREP-SAME: absolute-depth=2 routing-depth=2 fission-rows=0 routing-rows=0 cross-deps=1 reductions=0
; PREP-SAME: byte-offset-proofs=0 requirements=0 bound=static-bound, no-bound-requirement requirement-ids={{$}}
-; PREP-NEXT: remark: <unknown>:0:0: Cannot interchange loops because they are not tightly nested.
+; PREP-NEXT: remark: <unknown>:0:0: Loop interchanged with enclosing loop.
+; PREP-NEXT: remark: <unknown>:0:0: Distributed a proven outer-loop epilogue into its own loop before interchanging the reduction nest; bound=static-bound, no-bound-requirement.
;
; MEMZERO-LABEL: loop-interchange: rejected function=checksum_diagonal_epilogue{{ }}
; MEMZERO-SAME: outer=outer.header inner=inner.header reason=union N/E memory budget or simple-access check failed{{$}}
@@ -165,6 +225,419 @@
; MEMZERO-NEXT: remark: <unknown>:0:0: did not distribute the discovered outer-loop epilogue: union N/E memory budget or simple-access check failed
; MEMZERO-NEXT: remark: <unknown>:0:0: Number of loads/stores exceeded, the supported maximum can be increased with option -loop-interchange-max-mem-instr-ratio.
;
+; The transformed module. Every definition is listed, so the RUN line's
+; '{{^define }}' exclusion makes the list exhaustive. Each applied function
+; gains one sibling epilogue loop whose latch branches to the original
+; continuation, and its nest is interchanged. The emptied original epilogue
+; block, the epilogue loop, and the continuation are checked instruction by
+; instruction. Block labels are plain directives because the printed IR
+; separates blocks with a blank line. The other functions were already shown
+; unchanged by the llvm-extract --delete pair above and carry only their label.
+;
+; APPLIED-LABEL: define {{.*}}@checksum_diagonal_epilogue(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %pchk.res = phi double [ %pchk.next, %inner.header.split ]
+; APPLIED-NEXT: %uchk.res = phi double [ %uchk.next, %inner.header.split ]
+; APPLIED-NEXT: %vchk.res = phi double [ %vchk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [1335 x double], ptr %UNEW, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dval.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dnew.epil = fmul double %dval.epil, 1.500000e+00
+; APPLIED-NEXT: store double %dnew.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header, !prof !0
+; APPLIED: exit.cont:
+; APPLIED-NEXT: %r1 = getelementptr inbounds double, ptr %R, i64 1
+; APPLIED-NEXT: %r2 = getelementptr inbounds double, ptr %R, i64 2
+; APPLIED-NEXT: store double %pchk.res, ptr %R, align 8
+; APPLIED-NEXT: store double %uchk.res, ptr %r1, align 8
+; APPLIED-NEXT: store double %vchk.res, ptr %r2, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_to_e_flow(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %seidx.epil = getelementptr inbounds [1335 x double], ptr %S, i64 1334, i64 %epilogue.iv
+; APPLIED-NEXT: %sval.epil = load double, ptr %seidx.epil, align 8
+; APPLIED-NEXT: %bdbl.epil = fadd reassoc double %sval.epil, %sval.epil
+; APPLIED-NEXT: %bi.epil = getelementptr inbounds double, ptr %B, i64 %epilogue.iv
+; APPLIED-NEXT: store double %bdbl.epil, ptr %bi.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_to_e_anti(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %si.epil = getelementptr inbounds double, ptr %S, i64 %epilogue.iv
+; APPLIED-NEXT: %ival.epil = uitofp i64 %epilogue.iv to double
+; APPLIED-NEXT: store double %ival.epil, ptr %si.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_to_e_output(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %seidx.epil = getelementptr inbounds [1335 x double], ptr %S, i64 1334, i64 %epilogue.iv
+; APPLIED-NEXT: %ival.epil = uitofp i64 %epilogue.iv to double
+; APPLIED-NEXT: store double %ival.epil, ptr %seidx.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@multi_block_epilogue(
+; APPLIED: epilogue.first:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr inbounds double, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load double, ptr %dp.epil, align 8
+; APPLIED-NEXT: %dn.epil = fadd double %dv.epil, 1.000000e+00
+; APPLIED-NEXT: store double %dn.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: %ep.epil = getelementptr inbounds double, ptr %E, i64 %epilogue.iv
+; APPLIED-NEXT: %ev.epil = load double, ptr %ep.epil, align 8
+; APPLIED-NEXT: %en.epil = fadd double %ev.epil, 2.000000e+00
+; APPLIED-NEXT: store double %en.epil, ptr %ep.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@nested_diagonal_epilogue(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: outer.exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %ddiag.epil = getelementptr inbounds [1335 x double], ptr %D, i64 %epilogue.iv, i64 %epilogue.iv
+; APPLIED-NEXT: %dv.epil = load double, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: %dn.epil = fadd double %dv.epil, 1.000000e+00
+; APPLIED-NEXT: store double %dn.epil, ptr %ddiag.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %i.next.epil, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %outer.exit.cont, label %epilogue.header
+; APPLIED: outer.exit.cont:
+; APPLIED-NEXT: %rk = getelementptr inbounds double, ptr %R, i64 %k
+; APPLIED-NEXT: store double %chk.res, ptr %rk, align 8
+; APPLIED-NEXT: br label %sibling.header
+; APPLIED-LABEL: define {{.*}}@flattened_byte_gep_latch(
+; APPLIED: outer.latch:
+; APPLIED-NEXT: %i.next = add i64 %i, 1
+; APPLIED-NEXT: %i.ec = icmp eq i64 %i, 1335
+; APPLIED-NEXT: br i1 %i.ec, label %inner.header.split, label %outer.header
+; APPLIED: exit:
+; APPLIED-NEXT: %chk.res = phi double [ %chk.next, %inner.header.split ]
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 1, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %e.byte.epil = mul i64 %epilogue.iv, 10688
+; APPLIED-NEXT: %e.base.epil = getelementptr i8, ptr getelementptr inbounds (i8, ptr @byte_common, i64 64), i64 %e.byte.epil
+; APPLIED-NEXT: %e.ptr.epil = getelementptr i8, ptr %e.base.epil, i64 -10688
+; APPLIED-NEXT: %e.val.epil = load double, ptr %e.ptr.epil, align 8
+; APPLIED-NEXT: %e.new.epil = fmul double %e.val.epil, 1.500000e+00
+; APPLIED-NEXT: store double %e.new.epil, ptr %e.ptr.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.ec.epil = icmp eq i64 %epilogue.iv, 1335
+; APPLIED-NEXT: br i1 %i.ec.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: store double %chk.res, ptr %R, align 8
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@e_data_from_shared_preheader_pure(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %r.iv.1.epil = add i64 %epilogue.iv, %seed
+; APPLIED-NEXT: %r.invariant.1.epil = add i64 %bias, 17
+; APPLIED-NEXT: %r.invariant.2.epil = mul i64 %r.invariant.1.epil, 3
+; APPLIED-NEXT: %r.iv.2.epil = add i64 %r.iv.1.epil, %r.invariant.2.epil
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %r.iv.2.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, %limit
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@e_data_from_inner_preheader_pure(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %r.iv.1.epil = add i64 %epilogue.iv, %seed
+; APPLIED-NEXT: %r.invariant.1.epil = add i64 %bias, 17
+; APPLIED-NEXT: %r.invariant.2.epil = mul i64 %r.invariant.1.epil, 3
+; APPLIED-NEXT: %r.iv.2.epil = add i64 %r.iv.1.epil, %r.invariant.2.epil
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %r.iv.2.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, %limit
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@e_data_from_outer_preheader_invariant(
+; APPLIED: inner.ph:
+; APPLIED-NEXT: %seed = freeze i64 %bias
+; APPLIED-NEXT: %limit = add i64 3, 1
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %seed, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, %limit
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: br label %done
+; APPLIED-LABEL: define {{.*}}@e_operand_closure_dag(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %a.epil = add i64 %epilogue.iv, %seed
+; APPLIED-NEXT: %b.epil = mul i64 %a.epil, 3
+; APPLIED-NEXT: %c.epil = add i64 %b.epil, %b.epil
+; APPLIED-NEXT: %k1.epil = add i64 %bias, 17
+; APPLIED-NEXT: %k2.epil = mul i64 %k1.epil, %k1.epil
+; APPLIED-NEXT: %d.epil = add i64 %c.epil, %k2.epil
+; APPLIED-NEXT: %e.epil = sub i64 %d.epil, %a.epil
+; APPLIED-NEXT: %dp.epil = getelementptr i64, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i64 %e.epil, ptr %dp.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_nonconvergent_call_control(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr i32, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i32 7, ptr %dp.epil, align 4
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_nonconvergent_outer_header_control(
+; APPLIED: outer.header:
+; APPLIED: %v = call i32 @fission_plain_seed(i64 %i)
+; APPLIED-NOT: call i32 @fission_plain_seed
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr i32, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i32 7, ptr %dp.epil, align 4
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@n_convergent_call_outside_outer(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 0, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %dp.epil = getelementptr i32, ptr %D, i64 %epilogue.iv
+; APPLIED-NEXT: store i32 7, ptr %dp.epil, align 4
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 4
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; APPLIED-LABEL: define {{.*}}@raw_lt_n_to_e(
+; APPLIED: epilogue:
+; APPLIED-NEXT: br label %outer.latch
+; APPLIED: exit:
+; APPLIED-NEXT: br label %epilogue.preheader
+; APPLIED: epilogue.preheader:
+; APPLIED-NEXT: br label %epilogue.header
+; APPLIED: epilogue.header:
+; APPLIED-NEXT: %epilogue.iv = phi i64 [ 1, %epilogue.preheader ], [ %i.next.epil, %epilogue.latch ]
+; APPLIED-NEXT: %previous.epil = sub nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %ep.epil = getelementptr inbounds i64, ptr %A, i64 %previous.epil
+; APPLIED-NEXT: store i64 0, ptr %ep.epil, align 8
+; APPLIED-NEXT: br label %epilogue.latch
+; APPLIED: epilogue.latch:
+; APPLIED-NEXT: %i.next.epil = add nuw nsw i64 %epilogue.iv, 1
+; APPLIED-NEXT: %i.done.epil = icmp eq i64 %i.next.epil, 5
+; APPLIED-NEXT: br i1 %i.done.epil, label %exit.cont, label %epilogue.header
+; APPLIED: exit.cont:
+; APPLIED-NEXT: ret void
+; The same loop-info lines are checked against the loop manager's live
+; LoopInfo and against a fresh rebuild of the transformed module. The block
+; lists inside the nest are wildcarded because LoopInfo stores them in
+; discovery order, which an incremental update and a fresh walk reach
+; differently; the loop order, the depths, and the epilogue loop's two blocks
+; are exact.
+;
+; APPLY-LOOPS-LABEL: Loop info for function 'checksum_diagonal_epilogue':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_to_e_flow':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_to_e_anti':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_to_e_output':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'multi_block_epilogue':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'nested_diagonal_epilogue':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %root.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 3 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %sibling.header<header><latch><exiting>
+; APPLY-LOOPS-LABEL: Loop info for function 'flattened_byte_gep_latch':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'e_data_from_shared_preheader_pure':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'e_data_from_inner_preheader_pure':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'e_data_from_outer_preheader_invariant':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'e_operand_closure_dag':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_nonconvergent_call_control':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_nonconvergent_outer_header_control':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'n_convergent_call_outside_outer':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+; APPLY-LOOPS-LABEL: Loop info for function 'raw_lt_n_to_e':
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %epilogue.header<header>,%epilogue.latch<latch><exiting>
+; APPLY-LOOPS-NEXT: Loop at depth 1 containing: %inner.header<header>,{{.*}}
+; APPLY-LOOPS-NEXT: Loop at depth 2 containing: %outer.header<header>,{{.*}}
+;
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
More information about the llvm-commits
mailing list