Michael Kruse | 2133cb9 | 2016-06-28 01:37:20 +0000 | [diff] [blame] | 1 | //===--------- ScopInfo.cpp ----------------------------------------------===// |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Create a polyhedral description for a static control flow region. |
| 11 | // |
| 12 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 13 | // detection derived from their LLVM-IR code. |
| 14 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 15 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 20 | #include "polly/ScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 21 | #include "polly/LinkAllPasses.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 22 | #include "polly/Options.h" |
Michael Kruse | 73fa33b | 2016-06-28 01:37:28 +0000 | [diff] [blame] | 23 | #include "polly/ScopBuilder.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 24 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 25 | #include "polly/Support/ISLOStream.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 26 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 27 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DepthFirstIterator.h" |
Tobias Grosser | f4c24b2 | 2015-04-05 13:11:54 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/MapVector.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/PostOrderIterator.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/AliasAnalysis.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/LoopInfo.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopIterator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/RegionIterator.h" |
| 41 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Johannes Doerfert | 48fe86f | 2015-11-12 02:32:32 +0000 | [diff] [blame] | 42 | #include "llvm/IR/DiagnosticInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 44 | #include "isl/aff.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 45 | #include "isl/constraint.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 46 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 47 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 48 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 49 | #include "isl/printer.h" |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 50 | #include "isl/schedule.h" |
| 51 | #include "isl/schedule_node.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 52 | #include "isl/set.h" |
| 53 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 54 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 55 | #include "isl/val.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 56 | #include <sstream> |
| 57 | #include <string> |
| 58 | #include <vector> |
| 59 | |
| 60 | using namespace llvm; |
| 61 | using namespace polly; |
| 62 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 63 | #define DEBUG_TYPE "polly-scops" |
| 64 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 65 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 66 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 67 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 68 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 69 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 70 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 71 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 72 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 73 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 74 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 75 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 76 | "Number of delinearization assumptions taken."); |
| 77 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 78 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
| 79 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 80 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 81 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 82 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 83 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 84 | STATISTIC(NumScopsDepthLarger, |
| 85 | "Number of scops with maximal loop depth 6 and larger"); |
| 86 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 87 | |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 88 | // The maximal number of basic sets we allow during domain construction to |
| 89 | // be created. More complex scops will result in very high compile time and |
| 90 | // are also unlikely to result in good code |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 91 | static int const MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 92 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 93 | // The number of disjunct in the context after which we stop to add more |
| 94 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 95 | // number of disjunct when adding non-convex sets to the context. |
| 96 | static int const MaxDisjunctsInContext = 4; |
| 97 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 98 | // The maximal number of dimensions we allow during invariant load construction. |
| 99 | // More complex access ranges will result in very high compile time and are also |
| 100 | // unlikely to result in good code. This value is very high and should only |
| 101 | // trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006). |
| 102 | static int const MaxDimensionsInAccessRange = 9; |
| 103 | |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 104 | static cl::opt<int> |
| 105 | OptComputeOut("polly-analysis-computeout", |
| 106 | cl::desc("Bound the scop analysis by a maximal amount of " |
| 107 | "computational steps (0 means no bound)"), |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 108 | cl::Hidden, cl::init(800000), cl::ZeroOrMore, |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 109 | cl::cat(PollyCategory)); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 110 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 111 | static cl::opt<bool> PollyRemarksMinimal( |
| 112 | "polly-remarks-minimal", |
| 113 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 114 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 115 | |
Johannes Doerfert | 9e7b17b | 2014-08-18 00:40:13 +0000 | [diff] [blame] | 116 | // Multiplicative reductions can be disabled separately as these kind of |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 117 | // operations can overflow easily. Additive reductions and bit operations |
| 118 | // are in contrast pretty stable. |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 119 | static cl::opt<bool> DisableMultiplicativeReductions( |
| 120 | "polly-disable-multiplicative-reductions", |
| 121 | cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore, |
| 122 | cl::init(false), cl::cat(PollyCategory)); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 123 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 124 | static cl::opt<int> RunTimeChecksMaxAccessDisjuncts( |
| 125 | "polly-rtc-max-array-disjuncts", |
| 126 | cl::desc("The maximal number of disjunts allowed in memory accesses to " |
| 127 | "to build RTCs."), |
| 128 | cl::Hidden, cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 129 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 130 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 131 | "polly-rtc-max-parameters", |
| 132 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 133 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 134 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 135 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 136 | "polly-rtc-max-arrays-per-group", |
| 137 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 138 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 139 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 140 | static cl::opt<std::string> UserContextStr( |
| 141 | "polly-context", cl::value_desc("isl parameter set"), |
| 142 | cl::desc("Provide additional constraints on the context parameters"), |
| 143 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 144 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 145 | static cl::opt<bool> DetectReductions("polly-detect-reductions", |
| 146 | cl::desc("Detect and exploit reductions"), |
| 147 | cl::Hidden, cl::ZeroOrMore, |
| 148 | cl::init(true), cl::cat(PollyCategory)); |
| 149 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 150 | static cl::opt<bool> |
| 151 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 152 | cl::desc("Abort if an isl error is encountered"), |
| 153 | cl::init(true), cl::cat(PollyCategory)); |
| 154 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 155 | static cl::opt<bool> PollyPreciseInbounds( |
| 156 | "polly-precise-inbounds", |
| 157 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 158 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 159 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 160 | static cl::opt<bool> |
| 161 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 162 | cl::desc("Do not take inbounds assumptions at all"), |
| 163 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 164 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 165 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 166 | "polly-ignore-parameter-bounds", |
| 167 | cl::desc( |
| 168 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 169 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 170 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 171 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 172 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 173 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 174 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 175 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 176 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 177 | bool polly::UseInstructionNames; |
| 178 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 179 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 180 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 181 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 182 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 183 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 184 | static cl::opt<bool> PollyPrintInstructions( |
| 185 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 186 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 187 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 189 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 190 | // Create a sequence of two schedules. Either argument may be null and is |
| 191 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 192 | // empty. |
| 193 | static __isl_give isl_schedule * |
| 194 | combineInSequence(__isl_take isl_schedule *Prev, |
| 195 | __isl_take isl_schedule *Succ) { |
| 196 | if (!Prev) |
| 197 | return Succ; |
| 198 | if (!Succ) |
| 199 | return Prev; |
| 200 | |
| 201 | return isl_schedule_sequence(Prev, Succ); |
| 202 | } |
| 203 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 204 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 205 | int dim, isl::dim type) { |
| 206 | isl::val V; |
| 207 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 208 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 209 | // The upper and lower bound for a parameter value is derived either from |
| 210 | // the data type of the parameter or from the - possibly more restrictive - |
| 211 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 212 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 213 | S = S.lower_bound_val(type, dim, V); |
| 214 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 215 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 216 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 217 | if (Range.isFullSet()) |
| 218 | return S; |
| 219 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 220 | if (isl_set_n_basic_set(S.get()) > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 221 | return S; |
| 222 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 223 | // In case of signed wrapping, we can refine the set of valid values by |
| 224 | // excluding the part not covered by the wrapping range. |
| 225 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 226 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 227 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 228 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 229 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 230 | V = V.sub_ui(1); |
| 231 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 232 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 233 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 234 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 235 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 238 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 239 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 240 | if (!BasePtrLI) |
| 241 | return nullptr; |
| 242 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 243 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 244 | return nullptr; |
| 245 | |
| 246 | ScalarEvolution &SE = *S->getSE(); |
| 247 | |
| 248 | auto *OriginBaseSCEV = |
| 249 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 250 | if (!OriginBaseSCEV) |
| 251 | return nullptr; |
| 252 | |
| 253 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 254 | if (!OriginBaseSCEVUnknown) |
| 255 | return nullptr; |
| 256 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 257 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 258 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Tobias Grosser | 27db02b | 2017-08-06 17:25:05 +0000 | [diff] [blame] | 261 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 262 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 263 | const DataLayout &DL, Scop *S, |
| 264 | const char *BaseName) |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 265 | : BasePtr(BasePtr), ElementType(ElementType), IsOnHeap(false), Kind(Kind), |
| 266 | DL(DL), S(*S), FAD(nullptr) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 267 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 268 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 269 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 270 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 271 | UseInstructionNames); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 272 | Id = isl::id::alloc(Ctx, BasePtrName.c_str(), this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 273 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 274 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 275 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 276 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 277 | BasePtrOriginSAI = nullptr; |
| 278 | return; |
| 279 | } |
| 280 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 281 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 282 | if (BasePtrOriginSAI) |
| 283 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 286 | isl::space ScopArrayInfo::getSpace() const { |
| 287 | auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); |
| 288 | Space = Space.set_tuple_id(isl::dim::set, Id); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 289 | return Space; |
| 290 | } |
| 291 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 292 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 293 | isl::union_set WriteSet = S.getWrites().range(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 294 | isl::space Space = getSpace(); |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 295 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 296 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 297 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 300 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 301 | if (Array->getElementType() != getElementType()) |
| 302 | return false; |
| 303 | |
| 304 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 305 | return false; |
| 306 | |
| 307 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 308 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 309 | return false; |
| 310 | |
| 311 | return true; |
| 312 | } |
| 313 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 314 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 315 | if (NewElementType == ElementType) |
| 316 | return; |
| 317 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 318 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 319 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 320 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 321 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 322 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 323 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 324 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 325 | ElementType = NewElementType; |
| 326 | } else { |
| 327 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 328 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 329 | } |
| 330 | } |
| 331 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 332 | /// Make the ScopArrayInfo model a Fortran Array |
| 333 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 334 | assert(FAD && "got invalid Fortran array descriptor"); |
| 335 | if (this->FAD) { |
| 336 | assert(this->FAD == FAD && |
| 337 | "receiving different array descriptors for same array"); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 342 | assert(!this->FAD); |
| 343 | this->FAD = FAD; |
| 344 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 345 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 346 | |
| 347 | std::string param_name = getName(); |
| 348 | param_name += "_fortranarr_size"; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 349 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name.c_str(), this); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 350 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 351 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 352 | isl::pw_aff PwAff = |
| 353 | isl::aff::var_on_domain(isl::local_space(Space), isl::dim::param, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 354 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 355 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 358 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 359 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 360 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 361 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 362 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 363 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 364 | if (CheckConsistency) { |
| 365 | for (int i = 0; i < SharedDims; i++) { |
| 366 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 367 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 368 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 369 | return false; |
| 370 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 371 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 372 | if (DimensionSizes.size() >= NewSizes.size()) |
| 373 | return true; |
| 374 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 375 | |
| 376 | DimensionSizes.clear(); |
| 377 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 378 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 379 | DimensionSizesPw.clear(); |
| 380 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 381 | if (!Expr) { |
| 382 | DimensionSizesPw.push_back(nullptr); |
| 383 | continue; |
| 384 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 385 | isl::pw_aff Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 386 | DimensionSizesPw.push_back(Size); |
| 387 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 388 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 391 | ScopArrayInfo::~ScopArrayInfo() {} |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 392 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 393 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 394 | |
| 395 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 396 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 399 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 400 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 401 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 402 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 403 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 404 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 405 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 406 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 407 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 408 | // If this is a Fortran array, then we can print the outermost dimension |
| 409 | // as a isl_pw_aff even though there is no SCEV information. |
| 410 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 411 | |
| 412 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 413 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 414 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 415 | u++; |
| 416 | } |
| 417 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 418 | OS << "["; |
| 419 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 420 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 421 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 422 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 423 | } else { |
| 424 | OS << *getDimensionSize(u); |
| 425 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 426 | |
| 427 | OS << "]"; |
| 428 | } |
| 429 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 430 | OS << ";"; |
| 431 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 432 | if (BasePtrOriginSAI) |
| 433 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 434 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 435 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 439 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 440 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 441 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 442 | return getFromId(Id); |
| 443 | } |
| 444 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 445 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 446 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 447 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 448 | return SAI; |
| 449 | } |
| 450 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 451 | void MemoryAccess::wrapConstantDimensions() { |
| 452 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 453 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 454 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 455 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 456 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 457 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 458 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 459 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 460 | |
| 461 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 462 | for (int i = DimsArray - 1; i > 0; i--) { |
| 463 | auto *DimSize = SAI->getDimensionSize(i); |
| 464 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 465 | |
| 466 | // This transformation is not applicable to dimensions with dynamic size. |
| 467 | if (!DimSizeCst) |
| 468 | continue; |
| 469 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 470 | // This transformation is not applicable to dimensions of size zero. |
| 471 | if (DimSize->isZero()) |
| 472 | continue; |
| 473 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 474 | isl::val DimSizeVal = |
| 475 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 476 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 477 | isl::aff PrevVar = |
| 478 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 479 | |
| 480 | // Compute: index % size |
| 481 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | cb0224a | 2017-08-06 15:56:45 +0000 | [diff] [blame] | 482 | isl::aff Modulo = Var.mod(DimSizeVal); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 483 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 484 | |
| 485 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 486 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 487 | Divide = Divide.floor(); |
| 488 | Divide = Divide.add(PrevVar); |
| 489 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 490 | |
| 491 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 492 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 493 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 497 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 498 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 499 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 500 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 503 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 504 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 505 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 506 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 507 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 508 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 509 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 510 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 511 | auto DimsMissing = DimsArray - DimsAccess; |
| 512 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 513 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 514 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 515 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 516 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 517 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 518 | isl::map Map = isl::map::from_domain_and_range( |
| 519 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 520 | |
| 521 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 522 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 523 | |
| 524 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 525 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 526 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 527 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 528 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 529 | // For the non delinearized arrays, divide the access function of the last |
| 530 | // subscript by the size of the elements in the array. |
| 531 | // |
| 532 | // A stride one array access in C expressed as A[i] is expressed in |
| 533 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 534 | // two subsequent values of 'i' index two values that are stored next to |
| 535 | // each other in memory. By this division we make this characteristic |
| 536 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 537 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 538 | // that divides the offsets of all accesses to this base pointer. |
| 539 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 540 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 541 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 544 | // We currently do this only if we added at least one dimension, which means |
| 545 | // some dimension's indices have not been specified, an indicator that some |
| 546 | // index values have been added together. |
| 547 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 548 | // expressions more complicated. |
| 549 | if (DimsMissing) |
| 550 | wrapConstantDimensions(); |
| 551 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 552 | if (!isAffine()) |
| 553 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 554 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 555 | // Introduce multi-element accesses in case the type loaded by this memory |
| 556 | // access is larger than the canonical element type of the array. |
| 557 | // |
| 558 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 559 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 560 | if (ElemBytes > ArrayElemSize) { |
| 561 | assert(ElemBytes % ArrayElemSize == 0 && |
| 562 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 563 | isl::map Map = isl::map::from_domain_and_range( |
| 564 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 565 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 566 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 567 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 568 | isl::constraint C; |
| 569 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 570 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 571 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 572 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 573 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 574 | C = isl::constraint::alloc_inequality(LS); |
| 575 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 576 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 577 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 578 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 579 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 580 | C = isl::constraint::alloc_inequality(LS); |
| 581 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 582 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 583 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 584 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 585 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 586 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 589 | const std::string |
| 590 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 591 | switch (RT) { |
| 592 | case MemoryAccess::RT_NONE: |
| 593 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 594 | "access which isn't a reduction"); |
| 595 | case MemoryAccess::RT_ADD: |
| 596 | return "+"; |
| 597 | case MemoryAccess::RT_MUL: |
| 598 | return "*"; |
| 599 | case MemoryAccess::RT_BOR: |
| 600 | return "|"; |
| 601 | case MemoryAccess::RT_BXOR: |
| 602 | return "^"; |
| 603 | case MemoryAccess::RT_BAND: |
| 604 | return "&"; |
| 605 | } |
| 606 | llvm_unreachable("Unknown reduction type"); |
| 607 | return ""; |
| 608 | } |
| 609 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 610 | /// Return the reduction type for a given binary operator. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 611 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 612 | const Instruction *Load) { |
| 613 | if (!BinOp) |
| 614 | return MemoryAccess::RT_NONE; |
| 615 | switch (BinOp->getOpcode()) { |
| 616 | case Instruction::FAdd: |
| 617 | if (!BinOp->hasUnsafeAlgebra()) |
| 618 | return MemoryAccess::RT_NONE; |
| 619 | // Fall through |
| 620 | case Instruction::Add: |
| 621 | return MemoryAccess::RT_ADD; |
| 622 | case Instruction::Or: |
| 623 | return MemoryAccess::RT_BOR; |
| 624 | case Instruction::Xor: |
| 625 | return MemoryAccess::RT_BXOR; |
| 626 | case Instruction::And: |
| 627 | return MemoryAccess::RT_BAND; |
| 628 | case Instruction::FMul: |
| 629 | if (!BinOp->hasUnsafeAlgebra()) |
| 630 | return MemoryAccess::RT_NONE; |
| 631 | // Fall through |
| 632 | case Instruction::Mul: |
| 633 | if (DisableMultiplicativeReductions) |
| 634 | return MemoryAccess::RT_NONE; |
| 635 | return MemoryAccess::RT_MUL; |
| 636 | default: |
| 637 | return MemoryAccess::RT_NONE; |
| 638 | } |
| 639 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 640 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 641 | MemoryAccess::~MemoryAccess() {} |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 642 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 643 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 644 | isl::id ArrayId = getArrayId(); |
| 645 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 646 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 647 | return SAI; |
| 648 | } |
| 649 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 650 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 651 | isl::id ArrayId = getLatestArrayId(); |
| 652 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 653 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 654 | return SAI; |
| 655 | } |
| 656 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 657 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 658 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 661 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 662 | if (!hasNewAccessRelation()) |
| 663 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 664 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 667 | isl::map MemoryAccess::getAddressFunction() const { |
| 668 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 671 | isl::pw_multi_aff |
| 672 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 673 | isl::map Schedule, ScheduledAccRel; |
| 674 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 675 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 676 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 677 | USchedule = USchedule.intersect_domain(UDomain); |
| 678 | Schedule = isl::map::from_union_map(USchedule); |
| 679 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 680 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 683 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 684 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 687 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 688 | return stringFromIslObj(AccessRelation.get()); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 691 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 692 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 695 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 696 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 699 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 700 | return stringFromIslObj(NewAccessRelation.get()); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 703 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 704 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 707 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 708 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 709 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 710 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 711 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 712 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 713 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 716 | // Formalize no out-of-bound access assumption |
| 717 | // |
| 718 | // When delinearizing array accesses we optimistically assume that the |
| 719 | // delinearized accesses do not access out of bound locations (the subscript |
| 720 | // expression of each array evaluates for each statement instance that is |
| 721 | // executed to a value that is larger than zero and strictly smaller than the |
| 722 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 723 | // dimension for which we do not need to assume any upper bound. At this point |
| 724 | // we formalize this assumption to ensure that at code generation time the |
| 725 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 726 | // |
| 727 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 728 | // first build the set of data locations that are not within array bounds. We |
| 729 | // then apply the reverse access relation to obtain the set of iterations that |
| 730 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 731 | // that are actually executed by intersecting them with the domain of the |
| 732 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 733 | // parameters that may cause statement instances to be executed that may |
| 734 | // possibly yield out of bound memory accesses. The complement of these |
| 735 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 736 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 737 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 738 | if (PollyIgnoreInbounds) |
| 739 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 740 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 741 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 742 | isl::set Outside = isl::set::empty(Space); |
| 743 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 744 | isl::local_space LS(Space); |
| 745 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 746 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 747 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 748 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 749 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 750 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 751 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 752 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 753 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 754 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 757 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 758 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 759 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 760 | |
| 761 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 762 | // Doing so increases the set of parameter combinations that are assumed to |
| 763 | // not appear. This is always save, but may make the resulting run-time check |
| 764 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 765 | Outside = Outside.remove_divs(); |
| 766 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 767 | const auto &Loc = getAccessInstruction() |
| 768 | ? getAccessInstruction()->getDebugLoc() |
| 769 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 770 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 771 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 772 | Statement->getParent()->recordAssumption(INBOUNDS, Outside.release(), Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 773 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 776 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 777 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 778 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 779 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 780 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 781 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 782 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 783 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 784 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 785 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 786 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 787 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 788 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 789 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 790 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 791 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 792 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 793 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 794 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 795 | LengthMap = LengthMap.sum(SubscriptMap); |
| 796 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 797 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 800 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 801 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 802 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 803 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 804 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 805 | return; |
| 806 | |
| 807 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 808 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 809 | return; |
| 810 | |
| 811 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 812 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 813 | return; |
| 814 | |
| 815 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 816 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 817 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 818 | |
| 819 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 820 | if (Range.isFullSet()) |
| 821 | return; |
| 822 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 823 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 824 | return; |
| 825 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 826 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 827 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 828 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 829 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 830 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 831 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 832 | |
| 833 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 834 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 835 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 836 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 837 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 838 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 839 | isl::set AccessRange = Relation.range(); |
| 840 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 841 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 842 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 845 | void MemoryAccess::foldAccessRelation() { |
| 846 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 847 | return; |
| 848 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 849 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 850 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 851 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 852 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 853 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 854 | isl::space Space; |
| 855 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 856 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 857 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 858 | isl::space SpaceSize = DimSize.get_space(); |
| 859 | isl::id ParamId = |
| 860 | give(isl_space_get_dim_id(SpaceSize.get(), isl_dim_param, 0)); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 861 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 862 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 863 | Space = Space.range().map_from_set(); |
| 864 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 865 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 866 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 867 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 868 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 869 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 870 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 871 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 872 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 873 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 874 | for (int j = 0; j < Size; ++j) |
| 875 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 876 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 877 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 878 | isl::local_space LS(Space); |
| 879 | isl::constraint C; |
| 880 | C = isl::constraint::alloc_equality(LS); |
| 881 | C = C.set_constant_si(-1); |
| 882 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 883 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 884 | MapTwo = MapTwo.add_constraint(C); |
| 885 | C = isl::constraint::alloc_equality(LS); |
| 886 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 887 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 888 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 889 | MapTwo = MapTwo.add_constraint(C); |
| 890 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 891 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 892 | MapOne = MapOne.unite(MapTwo); |
| 893 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 894 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 895 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 896 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 897 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 898 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 899 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 900 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 901 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 902 | |
| 903 | // Access dimension folding might in certain cases increase the number of |
| 904 | // disjuncts in the memory access, which can possibly complicate the generated |
| 905 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 906 | if (!PollyPreciseFoldAccesses && |
| 907 | isl_map_n_basic_map(NewAccessRelation.get()) > |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 908 | isl_map_n_basic_map(AccessRelation.get())) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 909 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 910 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 911 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 914 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 915 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 916 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 917 | if (Size == 1) |
| 918 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 919 | |
| 920 | // Only one factor needs to be divisible. |
| 921 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 922 | for (auto *FactorExpr : MulExpr->operands()) |
| 923 | if (isDivisible(FactorExpr, Size, SE)) |
| 924 | return true; |
| 925 | return false; |
| 926 | } |
| 927 | |
| 928 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 929 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 930 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 931 | for (auto *OpExpr : NAryExpr->operands()) |
| 932 | if (!isDivisible(OpExpr, Size, SE)) |
| 933 | return false; |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 938 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 939 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 940 | return MulSCEV == Expr; |
| 941 | } |
| 942 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 943 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 944 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 945 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 946 | // Initialize the invalid domain which describes all iterations for which the |
| 947 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 948 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 949 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 950 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 951 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 952 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 953 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 954 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 955 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 956 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 957 | return; |
| 958 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 959 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 960 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 961 | // We overapproximate non-affine accesses with a possible access to the |
| 962 | // whole array. For read accesses it does not make a difference, if an |
| 963 | // access must or may happen. However, for write accesses it is important to |
| 964 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 965 | if (AccessRelation.is_null()) |
| 966 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 967 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 968 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 969 | return; |
| 970 | } |
| 971 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 972 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 973 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 974 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 975 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 976 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 977 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 978 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 981 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 982 | AccessRelation = AccessRelation.set_tuple_id( |
| 983 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 984 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 985 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 986 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 987 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 988 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 989 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 990 | AccessType AccType, Value *BaseAddress, |
| 991 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 992 | ArrayRef<const SCEV *> Subscripts, |
| 993 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 994 | MemoryKind Kind) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 995 | : Kind(Kind), AccType(AccType), RedType(RT_NONE), Statement(Stmt), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 996 | InvalidDomain(nullptr), BaseAddr(BaseAddress), ElementType(ElementType), |
| 997 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 998 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 999 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1000 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1001 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1002 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1003 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1004 | std::string IdName = Stmt->getBaseName() + Access; |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1005 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1006 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1007 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1008 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1009 | : Kind(MemoryKind::Array), AccType(AccType), RedType(RT_NONE), |
| 1010 | Statement(Stmt), InvalidDomain(nullptr), AccessInstruction(nullptr), |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1011 | IsAffine(true), AccessRelation(nullptr), NewAccessRelation(AccRel), |
| 1012 | FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 1013 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1014 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1015 | Sizes.push_back(nullptr); |
| 1016 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1017 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1018 | ElementType = SAI->getElementType(); |
| 1019 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1020 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1021 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1022 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1023 | std::string IdName = Stmt->getBaseName() + Access; |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1024 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1027 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1028 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1029 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 1030 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1033 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1034 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1035 | } |
| 1036 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1037 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1038 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1039 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1040 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1041 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1042 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1043 | else |
| 1044 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1045 | return OS; |
| 1046 | } |
| 1047 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1048 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1049 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1050 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1051 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1052 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1053 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1054 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1055 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1056 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1057 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1058 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1059 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1062 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1063 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (FAD) { |
| 1066 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1067 | OS << "] "; |
| 1068 | }; |
| 1069 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1070 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1071 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1072 | if (hasNewAccessRelation()) |
| 1073 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1076 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1077 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1078 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1079 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1080 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1081 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1082 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1083 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1084 | StmtDom = StmtDom.reset_tuple_id(); |
| 1085 | isl::set NewInvalidDom = StmtDom.intersect(isl::manage(PWAC.second)); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1086 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1087 | return isl::manage(PWAC.first); |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1090 | // Create a map in the size of the provided set domain, that maps from the |
| 1091 | // one element of the provided set domain to another element of the provided |
| 1092 | // set domain. |
| 1093 | // The mapping is limited to all points that are equal in all but the last |
| 1094 | // dimension and for which the last dimension of the input is strict smaller |
| 1095 | // than the last dimension of the output. |
| 1096 | // |
| 1097 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1098 | // |
| 1099 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1100 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1101 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1102 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1103 | isl::space Space = SetDomain.map_from_set(); |
| 1104 | isl::map Map = isl::map::universe(Space); |
| 1105 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Set all but the last dimension to be equal for the input and output |
| 1108 | // |
| 1109 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1110 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1111 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1112 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1113 | |
| 1114 | // Set the last dimension of the input to be strict smaller than the |
| 1115 | // last dimension of the output. |
| 1116 | // |
| 1117 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1118 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1119 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1122 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1123 | isl::map AccessRelation = getAccessRelation(); |
| 1124 | isl::space Space = Schedule.get_space().range(); |
| 1125 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1126 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1127 | Schedule = Schedule.reverse(); |
| 1128 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1129 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1130 | NextScatt = NextScatt.apply_range(Schedule); |
| 1131 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1132 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1133 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1134 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1135 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1136 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1139 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1140 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1141 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1142 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1143 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1144 | StrideX = isl::set::universe(Stride.get_space()); |
| 1145 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1146 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1147 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1148 | StrideWidth); |
| 1149 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1150 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1151 | return IsStrideX; |
| 1152 | } |
| 1153 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1154 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1155 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1158 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1159 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1162 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1163 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1166 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1167 | assert(NewAccess); |
| 1168 | |
| 1169 | #ifndef NDEBUG |
| 1170 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1171 | isl::space NewSpace = NewAccess.get_space(); |
| 1172 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1173 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1174 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1175 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1176 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1177 | // subdomain only. |
| 1178 | if (isRead()) { |
| 1179 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1180 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1181 | StmtDomain = |
| 1182 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1183 | isl::set NewDomain = NewAccess.domain(); |
| 1184 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1185 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1186 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1187 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1188 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1189 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1190 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1191 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1192 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1193 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1194 | |
| 1195 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1196 | InvariantEquivClassTy *EqClass = |
| 1197 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1198 | SAI->getBasePtr()); |
| 1199 | assert(EqClass && |
| 1200 | "Access functions to indirect arrays must have an invariant and " |
| 1201 | "hoisted base pointer"); |
| 1202 | } |
| 1203 | |
| 1204 | // Check whether access dimensions correspond to number of dimensions of the |
| 1205 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1206 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1207 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1208 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1209 | #endif |
| 1210 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1211 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1212 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1213 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1214 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1215 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1216 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1217 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1218 | |
| 1219 | return isl_set_is_subset(StmtDom.keep(), AccDom.keep()) == isl_bool_false; |
| 1220 | } |
| 1221 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1222 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1223 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1224 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1225 | isl_set *Domain = getDomain().release(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1226 | if (isl_set_is_empty(Domain)) { |
| 1227 | isl_set_free(Domain); |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1228 | return isl::manage(isl_map_from_aff(isl_aff_zero_on_domain( |
| 1229 | isl_local_space_from_space(getDomainSpace().release())))); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1230 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1231 | auto *Schedule = getParent()->getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1232 | if (!Schedule) { |
| 1233 | isl_set_free(Domain); |
| 1234 | return nullptr; |
| 1235 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1236 | Schedule = isl_union_map_intersect_domain( |
| 1237 | Schedule, isl_union_set_from_set(isl_set_copy(Domain))); |
| 1238 | if (isl_union_map_is_empty(Schedule)) { |
| 1239 | isl_set_free(Domain); |
| 1240 | isl_union_map_free(Schedule); |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1241 | return isl::manage(isl_map_from_aff(isl_aff_zero_on_domain( |
| 1242 | isl_local_space_from_space(getDomainSpace().release())))); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1243 | } |
| 1244 | auto *M = isl_map_from_union_map(Schedule); |
| 1245 | M = isl_map_coalesce(M); |
| 1246 | M = isl_map_gist_domain(M, Domain); |
| 1247 | M = isl_map_coalesce(M); |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1248 | return isl::manage(M); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1249 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1250 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1251 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1252 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1253 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1254 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1257 | void ScopStmt::buildAccessRelations() { |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 1258 | Scop &S = *getParent(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1259 | for (MemoryAccess *Access : MemAccs) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1260 | Type *ElementType = Access->getElementType(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1261 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1262 | MemoryKind Ty; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1263 | if (Access->isPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1264 | Ty = MemoryKind::PHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1265 | else if (Access->isExitPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1266 | Ty = MemoryKind::ExitPHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1267 | else if (Access->isValueKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1268 | Ty = MemoryKind::Value; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1269 | else |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1270 | Ty = MemoryKind::Array; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1271 | |
Tobias Grosser | 296fe2e | 2017-02-10 10:09:46 +0000 | [diff] [blame] | 1272 | auto *SAI = S.getOrCreateScopArrayInfo(Access->getOriginalBaseAddr(), |
| 1273 | ElementType, Access->Sizes, Ty); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1274 | Access->buildAccessRelation(SAI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1275 | S.addAccessData(Access); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1279 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1280 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1281 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1282 | if (Access->isArrayKind()) { |
| 1283 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1284 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1285 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1286 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 1287 | assert(Parent.getStmtFor(AccessVal) == this); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1288 | assert(!ValueWrites.lookup(AccessVal)); |
| 1289 | |
| 1290 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1291 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1292 | Value *AccessVal = Access->getAccessValue(); |
| 1293 | assert(!ValueReads.lookup(AccessVal)); |
| 1294 | |
| 1295 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1296 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1297 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1298 | assert(!PHIWrites.lookup(PHI)); |
| 1299 | |
| 1300 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1301 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1302 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1303 | assert(!PHIReads.lookup(PHI)); |
| 1304 | |
| 1305 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1308 | if (Prepend) { |
| 1309 | MemAccs.insert(MemAccs.begin(), Access); |
| 1310 | return; |
| 1311 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1312 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1315 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1316 | for (MemoryAccess *MA : *this) |
| 1317 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1318 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1319 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1320 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1321 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1324 | /// Add @p BSet to the set @p User if @p BSet is bounded. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1325 | static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet, |
| 1326 | void *User) { |
| 1327 | isl_set **BoundedParts = static_cast<isl_set **>(User); |
| 1328 | if (isl_basic_set_is_bounded(BSet)) |
| 1329 | *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet)); |
| 1330 | else |
| 1331 | isl_basic_set_free(BSet); |
| 1332 | return isl_stat_ok; |
| 1333 | } |
| 1334 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1335 | /// Return the bounded parts of @p S. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1336 | static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) { |
| 1337 | isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S)); |
| 1338 | isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts); |
| 1339 | isl_set_free(S); |
| 1340 | return BoundedParts; |
| 1341 | } |
| 1342 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1343 | /// Compute the (un)bounded parts of @p S wrt. to dimension @p Dim. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1344 | /// |
| 1345 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1346 | /// both with regards to the dimension @p Dim. |
| 1347 | static std::pair<__isl_give isl_set *, __isl_give isl_set *> |
| 1348 | partitionSetParts(__isl_take isl_set *S, unsigned Dim) { |
| 1349 | |
| 1350 | for (unsigned u = 0, e = isl_set_n_dim(S); u < e; u++) |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1351 | S = isl_set_lower_bound_si(S, isl_dim_set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1352 | |
| 1353 | unsigned NumDimsS = isl_set_n_dim(S); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1354 | isl_set *OnlyDimS = isl_set_copy(S); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1357 | assert(NumDimsS >= Dim + 1); |
| 1358 | OnlyDimS = |
| 1359 | isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1); |
| 1360 | |
| 1361 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1362 | // as we are not interested in them. |
| 1363 | OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim); |
| 1364 | for (unsigned u = 0; u < Dim; u++) { |
| 1365 | isl_constraint *C = isl_inequality_alloc( |
| 1366 | isl_local_space_from_space(isl_set_get_space(OnlyDimS))); |
| 1367 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1); |
| 1368 | C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1); |
| 1369 | OnlyDimS = isl_set_add_constraint(OnlyDimS, C); |
| 1370 | } |
| 1371 | |
| 1372 | // Collect all bounded parts of OnlyDimS. |
| 1373 | isl_set *BoundedParts = collectBoundedParts(OnlyDimS); |
| 1374 | |
| 1375 | // Create the dimensions greater than Dim again. |
| 1376 | BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1, |
| 1377 | NumDimsS - Dim - 1); |
| 1378 | |
| 1379 | // Remove the artificial upper bound parameters again. |
| 1380 | BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim); |
| 1381 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1382 | isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1383 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1384 | } |
| 1385 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1386 | /// Set the dimension Ids from @p From in @p To. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1387 | static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From, |
| 1388 | __isl_take isl_set *To) { |
| 1389 | for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) { |
| 1390 | isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u); |
| 1391 | To = isl_set_set_dim_id(To, isl_dim_set, u, DimId); |
| 1392 | } |
| 1393 | return To; |
| 1394 | } |
| 1395 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1396 | /// Create the conditions under which @p L @p Pred @p R is true. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1397 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1398 | __isl_take isl_pw_aff *L, |
| 1399 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1400 | switch (Pred) { |
| 1401 | case ICmpInst::ICMP_EQ: |
| 1402 | return isl_pw_aff_eq_set(L, R); |
| 1403 | case ICmpInst::ICMP_NE: |
| 1404 | return isl_pw_aff_ne_set(L, R); |
| 1405 | case ICmpInst::ICMP_SLT: |
| 1406 | return isl_pw_aff_lt_set(L, R); |
| 1407 | case ICmpInst::ICMP_SLE: |
| 1408 | return isl_pw_aff_le_set(L, R); |
| 1409 | case ICmpInst::ICMP_SGT: |
| 1410 | return isl_pw_aff_gt_set(L, R); |
| 1411 | case ICmpInst::ICMP_SGE: |
| 1412 | return isl_pw_aff_ge_set(L, R); |
| 1413 | case ICmpInst::ICMP_ULT: |
| 1414 | return isl_pw_aff_lt_set(L, R); |
| 1415 | case ICmpInst::ICMP_UGT: |
| 1416 | return isl_pw_aff_gt_set(L, R); |
| 1417 | case ICmpInst::ICMP_ULE: |
| 1418 | return isl_pw_aff_le_set(L, R); |
| 1419 | case ICmpInst::ICMP_UGE: |
| 1420 | return isl_pw_aff_ge_set(L, R); |
| 1421 | default: |
| 1422 | llvm_unreachable("Non integer predicate not supported"); |
| 1423 | } |
| 1424 | } |
| 1425 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1426 | /// Create the conditions under which @p L @p Pred @p R is true. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1427 | /// |
| 1428 | /// Helper function that will make sure the dimensions of the result have the |
| 1429 | /// same isl_id's as the @p Domain. |
| 1430 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
| 1431 | __isl_take isl_pw_aff *L, |
| 1432 | __isl_take isl_pw_aff *R, |
| 1433 | __isl_keep isl_set *Domain) { |
| 1434 | isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R); |
| 1435 | return setDimensionIds(Domain, ConsequenceCondSet); |
| 1436 | } |
| 1437 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1438 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1439 | /// |
| 1440 | /// @param S The Scop in which @p BB resides in. |
| 1441 | /// @param BB The BB for which isl representation is to be |
| 1442 | /// computed. |
| 1443 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1444 | /// @param E The SCEV that should be translated. |
| 1445 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1446 | /// |
| 1447 | /// Note that this function will also adjust the invalid context accordingly. |
| 1448 | |
| 1449 | __isl_give isl_pw_aff * |
| 1450 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1451 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1452 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1453 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1454 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(isl::manage(PWAC.second)); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1455 | return PWAC.first; |
| 1456 | } |
| 1457 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1458 | /// Build the conditions sets for the switch @p SI in the @p Domain. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1459 | /// |
| 1460 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1461 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1462 | /// have as many elements as @p SI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1463 | static bool |
| 1464 | buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1465 | __isl_keep isl_set *Domain, |
| 1466 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1467 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1468 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1469 | Value *Condition = getConditionFromTerminator(SI); |
| 1470 | assert(Condition && "No condition for switch"); |
| 1471 | |
| 1472 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1473 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1474 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1475 | |
| 1476 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1477 | ConditionSets.resize(NumSuccessors); |
| 1478 | for (auto &Case : SI->cases()) { |
| 1479 | unsigned Idx = Case.getSuccessorIndex(); |
| 1480 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1481 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1482 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1483 | isl_set *CaseConditionSet = |
| 1484 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain); |
| 1485 | ConditionSets[Idx] = isl_set_coalesce( |
| 1486 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1487 | } |
| 1488 | |
| 1489 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1490 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1491 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1492 | ConditionSetUnion = |
| 1493 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
| 1494 | ConditionSets[0] = setDimensionIds( |
| 1495 | Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion)); |
| 1496 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1497 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1498 | |
| 1499 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1502 | /// Build condition sets for unsigned ICmpInst(s). |
| 1503 | /// Special handling is required for unsigned operands to ensure that if |
| 1504 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1505 | /// it should wrap around. |
| 1506 | /// |
| 1507 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1508 | /// between TestVal and UpperBound, i.e, |
| 1509 | /// TestVal < UpperBound OR TestVal <= UpperBound |
| 1510 | static __isl_give isl_set * |
| 1511 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1512 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1513 | const SCEV *SCEV_UpperBound, |
| 1514 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1515 | bool IsStrictUpperBound) { |
| 1516 | |
| 1517 | // Do not take NonNeg assumption on TestVal |
| 1518 | // as it might have MSB (Sign bit) set. |
| 1519 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1520 | // Take NonNeg assumption on UpperBound. |
| 1521 | isl_pw_aff *UpperBound = |
| 1522 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1523 | |
| 1524 | // 0 <= TestVal |
| 1525 | isl_set *First = |
| 1526 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1527 | isl_pw_aff_get_domain_space(TestVal))), |
| 1528 | isl_pw_aff_copy(TestVal)); |
| 1529 | |
| 1530 | isl_set *Second; |
| 1531 | if (IsStrictUpperBound) |
| 1532 | // TestVal < UpperBound |
| 1533 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1534 | else |
| 1535 | // TestVal <= UpperBound |
| 1536 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1537 | |
| 1538 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
| 1539 | ConsequenceCondSet = setDimensionIds(Domain, ConsequenceCondSet); |
| 1540 | return ConsequenceCondSet; |
| 1541 | } |
| 1542 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1543 | /// Build the conditions sets for the branch condition @p Condition in |
| 1544 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1545 | /// |
| 1546 | /// This will fill @p ConditionSets with the conditions under which control |
| 1547 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1548 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1549 | /// context under which @p Condition is true/false will be returned as the |
| 1550 | /// new elements of @p ConditionSets. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1551 | static bool |
| 1552 | buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1553 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1554 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1555 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1556 | |
| 1557 | isl_set *ConsequenceCondSet = nullptr; |
| 1558 | if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
| 1559 | if (CCond->isZero()) |
| 1560 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1561 | else |
| 1562 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1563 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1564 | auto Opcode = BinOp->getOpcode(); |
| 1565 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1566 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1567 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1568 | InvalidDomainMap, ConditionSets) && |
| 1569 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1570 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1571 | if (!Valid) { |
| 1572 | while (!ConditionSets.empty()) |
| 1573 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1574 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1575 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1576 | |
| 1577 | isl_set_free(ConditionSets.pop_back_val()); |
| 1578 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1579 | isl_set_free(ConditionSets.pop_back_val()); |
| 1580 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1581 | |
| 1582 | if (Opcode == Instruction::And) |
| 1583 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1584 | else |
| 1585 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1586 | } else { |
| 1587 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1588 | assert(ICond && |
| 1589 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1590 | |
| 1591 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1592 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1593 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1594 | // to be set. The comparison is equal to a signed comparison under this |
| 1595 | // assumption. |
| 1596 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1597 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1598 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1599 | |
| 1600 | switch (ICond->getPredicate()) { |
| 1601 | case ICmpInst::ICMP_ULT: |
| 1602 | ConsequenceCondSet = |
| 1603 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1604 | RightOperand, InvalidDomainMap, true); |
| 1605 | break; |
| 1606 | case ICmpInst::ICMP_ULE: |
| 1607 | ConsequenceCondSet = |
| 1608 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1609 | RightOperand, InvalidDomainMap, false); |
| 1610 | break; |
| 1611 | case ICmpInst::ICMP_UGT: |
| 1612 | ConsequenceCondSet = |
| 1613 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1614 | LeftOperand, InvalidDomainMap, true); |
| 1615 | break; |
| 1616 | case ICmpInst::ICMP_UGE: |
| 1617 | ConsequenceCondSet = |
| 1618 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1619 | LeftOperand, InvalidDomainMap, false); |
| 1620 | break; |
| 1621 | default: |
| 1622 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1623 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
| 1624 | ConsequenceCondSet = |
| 1625 | buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain); |
| 1626 | break; |
| 1627 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1630 | // If no terminator was given we are only looking for parameter constraints |
| 1631 | // under which @p Condition is true/false. |
| 1632 | if (!TI) |
| 1633 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1634 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1635 | ConsequenceCondSet = isl_set_coalesce( |
| 1636 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1637 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1638 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1639 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1640 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1641 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1642 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1643 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1644 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1645 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1646 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1649 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1650 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1651 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1652 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1653 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1654 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | ConditionSets.push_back(ConsequenceCondSet); |
| 1658 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1659 | |
| 1660 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1663 | /// Build the conditions sets for the terminator @p TI in the @p Domain. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1664 | /// |
| 1665 | /// This will fill @p ConditionSets with the conditions under which control |
| 1666 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1667 | /// have as many elements as @p TI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1668 | static bool |
| 1669 | buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1670 | __isl_keep isl_set *Domain, |
| 1671 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1672 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1673 | |
| 1674 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1675 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1676 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1677 | |
| 1678 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1679 | |
| 1680 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1681 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1682 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1685 | Value *Condition = getConditionFromTerminator(TI); |
| 1686 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1687 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1688 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1689 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1692 | void ScopStmt::buildDomain() { |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1693 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1694 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1695 | Domain = getParent()->getDomainConditions(this); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1696 | Domain = Domain.set_tuple_id(Id); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1699 | void ScopStmt::collectSurroundingLoops() { |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1700 | for (unsigned u = 0, e = Domain.dim(isl::dim::set); u < e; u++) { |
| 1701 | isl::id DimId = Domain.get_dim_id(isl::dim::set, u); |
| 1702 | NestLoops.push_back(static_cast<Loop *>(DimId.get_user())); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1706 | ScopStmt::ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1707 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(nullptr), |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1708 | R(&R), Build(nullptr), SurroundingLoop(SurroundingLoop) { |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1709 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1710 | BaseName = getIslCompatibleName( |
| 1711 | "Stmt", R.getNameStr(), parent.getNextStmtIdx(), "", UseInstructionNames); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1714 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop, |
| 1715 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1716 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1717 | R(nullptr), Build(nullptr), SurroundingLoop(SurroundingLoop), |
| 1718 | Instructions(Instructions) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1719 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1720 | BaseName = getIslCompatibleName("Stmt", &bb, parent.getNextStmtIdx(), "", |
| 1721 | UseInstructionNames); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1724 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1725 | isl::set NewDomain) |
| 1726 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), BB(nullptr), |
| 1727 | R(nullptr), Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1728 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1729 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1730 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1731 | Domain = Domain.set_tuple_id(Id); |
| 1732 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1733 | auto *Access = |
| 1734 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1735 | parent.addAccessFunction(Access); |
| 1736 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1737 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1738 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1739 | parent.addAccessFunction(Access); |
| 1740 | addAccess(Access); |
| 1741 | } |
| 1742 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 1743 | void ScopStmt::init(LoopInfo &LI) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1744 | assert(!Domain && "init must be called only once"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1745 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1746 | buildDomain(); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1747 | collectSurroundingLoops(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1748 | buildAccessRelations(); |
| 1749 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 1750 | if (DetectReductions) |
| 1751 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1754 | /// Collect loads which might form a reduction chain with @p StoreMA. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1755 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1756 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 1757 | /// two loads as operands. If the binary operand is commutative & associative, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1758 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 1759 | /// once, we have found a possible reduction chain. It starts at an operand |
| 1760 | /// load and includes the binary operator and @p StoreMA. |
| 1761 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1762 | /// Note: We allow only one use to ensure the load and binary operator cannot |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1763 | /// escape this block or into any other store except @p StoreMA. |
| 1764 | void ScopStmt::collectCandiateReductionLoads( |
| 1765 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 1766 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 1767 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1768 | return; |
| 1769 | |
| 1770 | // Skip if there is not one binary operator between the load and the store |
| 1771 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1772 | if (!BinOp) |
| 1773 | return; |
| 1774 | |
| 1775 | // Skip if the binary operators has multiple uses |
| 1776 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1777 | return; |
| 1778 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1779 | // Skip if the opcode of the binary operator is not commutative/associative |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1780 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 1781 | return; |
| 1782 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1783 | // Skip if the binary operator is outside the current SCoP |
| 1784 | if (BinOp->getParent() != Store->getParent()) |
| 1785 | return; |
| 1786 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1787 | // Skip if it is a multiplicative reduction and we disabled them |
| 1788 | if (DisableMultiplicativeReductions && |
| 1789 | (BinOp->getOpcode() == Instruction::Mul || |
| 1790 | BinOp->getOpcode() == Instruction::FMul)) |
| 1791 | return; |
| 1792 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1793 | // Check the binary operator operands for a candidate load |
| 1794 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 1795 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 1796 | if (!PossibleLoad0 && !PossibleLoad1) |
| 1797 | return; |
| 1798 | |
| 1799 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 1800 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1801 | if (PossibleLoad0->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1802 | Loads.push_back(&getArrayAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1803 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1804 | if (PossibleLoad1->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1805 | Loads.push_back(&getArrayAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1808 | /// Check for reductions in this ScopStmt. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1809 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1810 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 1811 | /// like chains. For all candidates we check if they have the same base address |
| 1812 | /// and there are no other accesses which overlap with them. The base address |
| 1813 | /// check rules out impossible reductions candidates early. The overlap check, |
| 1814 | /// together with the "only one user" check in collectCandiateReductionLoads, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1815 | /// guarantees that none of the intermediate results will escape during |
| 1816 | /// execution of the loop nest. We basically check here that no other memory |
| 1817 | /// access can access the same memory as the potential reduction. |
| 1818 | void ScopStmt::checkForReductions() { |
| 1819 | SmallVector<MemoryAccess *, 2> Loads; |
| 1820 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 1821 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1822 | // First collect candidate load-store reduction chains by iterating over all |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1823 | // stores and collecting possible reduction loads. |
| 1824 | for (MemoryAccess *StoreMA : MemAccs) { |
| 1825 | if (StoreMA->isRead()) |
| 1826 | continue; |
| 1827 | |
| 1828 | Loads.clear(); |
| 1829 | collectCandiateReductionLoads(StoreMA, Loads); |
| 1830 | for (MemoryAccess *LoadMA : Loads) |
| 1831 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 1832 | } |
| 1833 | |
| 1834 | // Then check each possible candidate pair. |
| 1835 | for (const auto &CandidatePair : Candidates) { |
| 1836 | bool Valid = true; |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1837 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation().release(); |
| 1838 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation().release(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1839 | |
| 1840 | // Skip those with obviously unequal base addresses. |
| 1841 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 1842 | isl_map_free(LoadAccs); |
| 1843 | isl_map_free(StoreAccs); |
| 1844 | continue; |
| 1845 | } |
| 1846 | |
| 1847 | // And check if the remaining for overlap with other memory accesses. |
| 1848 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1849 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain().release()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1850 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 1851 | |
| 1852 | for (MemoryAccess *MA : MemAccs) { |
| 1853 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1854 | continue; |
| 1855 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1856 | isl_map *AccRel = isl_map_intersect_domain( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1857 | MA->getAccessRelation().release(), getDomain().release()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1858 | isl_set *Accs = isl_map_range(AccRel); |
| 1859 | |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1860 | if (isl_set_has_equal_space(AllAccs, Accs)) { |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1861 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 1862 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 1863 | isl_set_free(OverlapAccs); |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1864 | } else { |
| 1865 | isl_set_free(Accs); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | isl_set_free(AllAccs); |
| 1870 | if (!Valid) |
| 1871 | continue; |
| 1872 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1873 | const LoadInst *Load = |
| 1874 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1875 | MemoryAccess::ReductionType RT = |
| 1876 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1877 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1878 | // If no overlapping access was found we mark the load and store as |
| 1879 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1880 | CandidatePair.first->markAsReductionLike(RT); |
| 1881 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1882 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1885 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1886 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1887 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1888 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1889 | if (!S) |
| 1890 | return ""; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1891 | auto Str = stringFromIslObj(S); |
| 1892 | isl_map_free(S); |
| 1893 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1896 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1897 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1898 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1899 | if (isBlockStmt()) |
| 1900 | return getBasicBlock(); |
| 1901 | return getRegion()->getEntry(); |
| 1902 | } |
| 1903 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1904 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1905 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1906 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1907 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1908 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1909 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1912 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1913 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1914 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1915 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1916 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1917 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1918 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1919 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1920 | ScopStmt::~ScopStmt() {} |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1921 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1922 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1923 | OS << "Instructions {\n"; |
| 1924 | |
| 1925 | for (Instruction *Inst : Instructions) |
| 1926 | OS.indent(16) << *Inst << "\n"; |
| 1927 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1928 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1931 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1932 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1933 | OS.indent(12) << "Domain :=\n"; |
| 1934 | |
| 1935 | if (Domain) { |
| 1936 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1937 | } else |
| 1938 | OS.indent(16) << "n/a\n"; |
| 1939 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1940 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1941 | |
| 1942 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1943 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1944 | } else |
| 1945 | OS.indent(16) << "n/a\n"; |
| 1946 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1947 | for (MemoryAccess *Access : MemAccs) |
| 1948 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1949 | |
Michael Kruse | eca86ce | 2017-07-26 22:01:33 +0000 | [diff] [blame] | 1950 | if (PrintInstructions && isBlockStmt()) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1951 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1954 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1955 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1956 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1957 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1958 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1959 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1960 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1961 | (void)Found; |
| 1962 | assert(Found && "Expected access data not found"); |
| 1963 | } |
| 1964 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1965 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1966 | (void)Found; |
| 1967 | assert(Found && "Expected access data not found"); |
| 1968 | } |
| 1969 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1970 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1971 | (void)Found; |
| 1972 | assert(Found && "Expected access data not found"); |
| 1973 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1974 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1975 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1976 | (void)Found; |
| 1977 | assert(Found && "Expected access data not found"); |
| 1978 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1981 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1982 | // Remove the memory accesses from this statement together with all scalar |
| 1983 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1984 | // instruction, hence would not be removed by this function. However, it is |
| 1985 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1986 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1987 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1988 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1989 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1990 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1991 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1992 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1993 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1994 | Parent.removeAccessData(MA); |
| 1995 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1996 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1997 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1998 | MemAccs.end()); |
| 1999 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2002 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA) { |
| 2003 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 2004 | assert(MAIt != MemAccs.end()); |
| 2005 | MemAccs.erase(MAIt); |
| 2006 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2007 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2008 | Parent.removeAccessData(MA); |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2009 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2010 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 2011 | if (It != InstructionToAccess.end()) { |
| 2012 | It->second.remove(MA); |
| 2013 | if (It->second.empty()) |
| 2014 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 2015 | } |
| 2016 | } |
| 2017 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 2018 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 2019 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 2020 | if (Access) |
| 2021 | return Access; |
| 2022 | |
| 2023 | ScopArrayInfo *SAI = |
| 2024 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 2025 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 2026 | true, {}, {}, V, MemoryKind::Value); |
| 2027 | Parent.addAccessFunction(Access); |
| 2028 | Access->buildAccessRelation(SAI); |
| 2029 | addAccess(Access); |
| 2030 | Parent.addAccessData(Access); |
| 2031 | return Access; |
| 2032 | } |
| 2033 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2034 | raw_ostream &polly::operator<<(raw_ostream &O, const ScopStmt &S) { |
| 2035 | S.print(O, PollyPrintInstructions); |
| 2036 | return O; |
| 2037 | } |
| 2038 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2039 | //===----------------------------------------------------------------------===// |
| 2040 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2041 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 2042 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 2043 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 2044 | isl_set_free(Context); |
| 2045 | Context = NewContext; |
| 2046 | } |
| 2047 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2048 | namespace { |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2049 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2050 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2051 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2052 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2053 | |
| 2054 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2055 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 2056 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2057 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2058 | |
| 2059 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2060 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2061 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 2062 | return SSPR.visit(E); |
| 2063 | } |
| 2064 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2065 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 2066 | auto *Start = visit(E->getStart()); |
| 2067 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 2068 | visit(E->getStepRecurrence(SE)), |
| 2069 | E->getLoop(), SCEV::FlagAnyWrap); |
| 2070 | return SE.getAddExpr(Start, AddRec); |
| 2071 | } |
| 2072 | |
| 2073 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 2074 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 2075 | return SE.getUnknown(NewValue); |
| 2076 | return E; |
| 2077 | } |
| 2078 | }; |
| 2079 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2080 | /// Check whether we should remap a SCEV expression. |
| 2081 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2082 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2083 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2084 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2085 | |
| 2086 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2087 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 2088 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2089 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 2090 | |
| 2091 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2092 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2093 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 2094 | SFIS.visitAll(E); |
| 2095 | return SFIS.FoundInside; |
| 2096 | } |
| 2097 | |
| 2098 | bool follow(const SCEV *E) { |
| 2099 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 2100 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 2101 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 2102 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 2103 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 2104 | } |
| 2105 | return !FoundInside; |
| 2106 | } |
| 2107 | bool isDone() { return FoundInside; } |
| 2108 | }; |
| 2109 | } // namespace |
| 2110 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2111 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2112 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 2113 | // doesn't like addition between an AddRec and an expression that |
| 2114 | // doesn't have a dominance relationship with it.) |
| 2115 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 2116 | return E; |
| 2117 | |
| 2118 | // Rewrite SCEV. |
| 2119 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2122 | // This table of function names is used to translate parameter names in more |
| 2123 | // human-readable names. This makes it easier to interpret Polly analysis |
| 2124 | // results. |
| 2125 | StringMap<std::string> KnownNames = { |
| 2126 | {"_Z13get_global_idj", "global_id"}, |
| 2127 | {"_Z12get_local_idj", "local_id"}, |
| 2128 | {"_Z15get_global_sizej", "global_size"}, |
| 2129 | {"_Z14get_local_sizej", "local_size"}, |
| 2130 | {"_Z12get_work_dimv", "work_dim"}, |
| 2131 | {"_Z17get_global_offsetj", "global_offset"}, |
| 2132 | {"_Z12get_group_idj", "group_id"}, |
| 2133 | {"_Z14get_num_groupsj", "num_groups"}, |
| 2134 | }; |
| 2135 | |
| 2136 | static std::string getCallParamName(CallInst *Call) { |
| 2137 | std::string Result; |
| 2138 | raw_string_ostream OS(Result); |
| 2139 | std::string Name = Call->getCalledFunction()->getName(); |
| 2140 | |
| 2141 | auto Iterator = KnownNames.find(Name); |
| 2142 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 2143 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2144 | OS << Name; |
| 2145 | for (auto &Operand : Call->arg_operands()) { |
| 2146 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 2147 | OS << "_" << Op->getValue(); |
| 2148 | } |
| 2149 | OS.flush(); |
| 2150 | return Result; |
| 2151 | } |
| 2152 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2153 | void Scop::createParameterId(const SCEV *Parameter) { |
| 2154 | assert(Parameters.count(Parameter)); |
| 2155 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2156 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2157 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2158 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2159 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 2160 | Value *Val = ValueParameter->getValue(); |
| 2161 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2162 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2163 | if (Call && isConstCall(Call)) { |
| 2164 | ParameterName = getCallParamName(Call); |
| 2165 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2166 | // If this parameter references a specific Value and this value has a name |
| 2167 | // we use this name as it is likely to be unique and more useful than just |
| 2168 | // a number. |
| 2169 | if (Val->hasName()) |
| 2170 | ParameterName = Val->getName(); |
| 2171 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 2172 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 2173 | if (LoadOrigin->hasName()) { |
| 2174 | ParameterName += "_loaded_from_"; |
| 2175 | ParameterName += |
| 2176 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 2177 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2178 | } |
| 2179 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2180 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2181 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 2182 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 2183 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2184 | auto *Id = isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 2185 | const_cast<void *>((const void *)Parameter)); |
| 2186 | ParameterIds[Parameter] = Id; |
| 2187 | } |
| 2188 | |
| 2189 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2190 | for (const SCEV *Parameter : NewParameters) { |
| 2191 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2192 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2193 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2194 | |
| 2195 | if (Parameters.insert(Parameter)) |
| 2196 | createParameterId(Parameter); |
| 2197 | } |
| 2198 | } |
| 2199 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 2200 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2201 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2202 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 2203 | return isl::manage(isl_id_copy(ParameterIds.lookup(Parameter))); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2204 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2205 | |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2206 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2207 | isl_set *DomainContext = isl_union_set_params(getDomains().release()); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2208 | return isl::manage(isl_set_intersect_params(C.release(), DomainContext)); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2211 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2212 | return DT.dominates(BB, getEntry()); |
| 2213 | } |
| 2214 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2215 | void Scop::addUserAssumptions( |
| 2216 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2217 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2218 | for (auto &Assumption : AC.assumptions()) { |
| 2219 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2220 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2221 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2222 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2223 | bool InScop = contains(CI); |
| 2224 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2225 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2226 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2227 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2228 | auto *Val = CI->getArgOperand(0); |
| 2229 | ParameterSetTy DetectedParams; |
| 2230 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2231 | ORE.emit( |
| 2232 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 2233 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2234 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2235 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Collect all newly introduced parameters. |
| 2238 | ParameterSetTy NewParams; |
| 2239 | for (auto *Param : DetectedParams) { |
| 2240 | Param = extractConstantFactor(Param, *SE).second; |
| 2241 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2242 | if (Parameters.count(Param)) |
| 2243 | continue; |
| 2244 | NewParams.insert(Param); |
| 2245 | } |
| 2246 | |
| 2247 | SmallVector<isl_set *, 2> ConditionSets; |
| 2248 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2249 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
| 2250 | auto *Dom = InScop ? DomainMap[BB].copy() : isl_set_copy(Context); |
| 2251 | assert(Dom && "Cannot propagate a nullptr."); |
| 2252 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 2253 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2254 | isl_set_free(Dom); |
| 2255 | |
| 2256 | if (!Valid) |
| 2257 | continue; |
| 2258 | |
| 2259 | isl_set *AssumptionCtx = nullptr; |
| 2260 | if (InScop) { |
| 2261 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2262 | isl_set_free(ConditionSets[0]); |
| 2263 | } else { |
| 2264 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2265 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2266 | } |
| 2267 | |
| 2268 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2269 | if (!NewParams.empty()) { |
| 2270 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2271 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2272 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2273 | isl_id_free(Id); |
| 2274 | |
| 2275 | if (!NewParams.count(Param)) |
| 2276 | continue; |
| 2277 | |
| 2278 | AssumptionCtx = |
| 2279 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2280 | } |
| 2281 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2282 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2283 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2284 | Context = isl_set_intersect(Context, AssumptionCtx); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2288 | void Scop::addUserContext() { |
| 2289 | if (UserContextStr.empty()) |
| 2290 | return; |
| 2291 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2292 | isl_set *UserContext = |
| 2293 | isl_set_read_from_str(getIslCtx(), UserContextStr.c_str()); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2294 | isl_space *Space = getParamSpace().release(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2295 | if (isl_space_dim(Space, isl_dim_param) != |
| 2296 | isl_set_dim(UserContext, isl_dim_param)) { |
| 2297 | auto SpaceStr = isl_space_to_str(Space); |
| 2298 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2299 | << "number of dimensions than the computed context. Due to this " |
| 2300 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2301 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2302 | free(SpaceStr); |
| 2303 | isl_set_free(UserContext); |
| 2304 | isl_space_free(Space); |
| 2305 | return; |
| 2306 | } |
| 2307 | |
| 2308 | for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 2309 | auto *NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 2310 | auto *NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2311 | |
| 2312 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 2313 | auto SpaceStr = isl_space_to_str(Space); |
| 2314 | errs() << "Error: the name of dimension " << i |
| 2315 | << " provided in -polly-context " |
| 2316 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2317 | << "context is '" << NameContext |
| 2318 | << "'. Due to this name mismatch, " |
| 2319 | << "the -polly-context option is ignored. Please provide " |
| 2320 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2321 | free(SpaceStr); |
| 2322 | isl_set_free(UserContext); |
| 2323 | isl_space_free(Space); |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | UserContext = |
| 2328 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 2329 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 2330 | } |
| 2331 | |
| 2332 | Context = isl_set_intersect(Context, UserContext); |
| 2333 | isl_space_free(Space); |
| 2334 | } |
| 2335 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2336 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2337 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2338 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2339 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2340 | for (LoadInst *LInst : RIL) { |
| 2341 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2342 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2343 | Type *Ty = LInst->getType(); |
| 2344 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2345 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2346 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2347 | continue; |
| 2348 | } |
| 2349 | |
| 2350 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2351 | InvariantEquivClasses.emplace_back( |
| 2352 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2356 | void Scop::buildContext() { |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2357 | isl_space *Space = isl_space_params_alloc(getIslCtx(), 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2358 | Context = isl_set_universe(isl_space_copy(Space)); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2359 | InvalidContext = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2360 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2363 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2364 | unsigned PDim = 0; |
| 2365 | for (auto *Parameter : Parameters) { |
| 2366 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 2367 | Context = |
| 2368 | addRangeBoundsToSet(give(Context), SRange, PDim++, isl::dim::param) |
| 2369 | .release(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2373 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 2374 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2375 | for (auto Array : Arrays) { |
| 2376 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2377 | // for its outermost dimension. Fortran arrays will have this since the |
| 2378 | // outermost dimension size can be picked up from their runtime description. |
| 2379 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2380 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2381 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2382 | if (!PwAff) |
| 2383 | continue; |
| 2384 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2385 | isl::id Id = |
| 2386 | isl::manage(isl_pw_aff_get_dim_id(PwAff.get(), isl_dim_param, 0)); |
| 2387 | assert(!Id.is_null() && |
| 2388 | "Invalid Id for PwAff expression in Fortran array"); |
| 2389 | Id.dump(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2390 | OutermostSizeIds.push_back(Id); |
| 2391 | } |
| 2392 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2393 | return OutermostSizeIds; |
| 2394 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2395 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2396 | // The FORTRAN array size parameters are known to be non-negative. |
| 2397 | static isl_set *boundFortranArrayParams(__isl_give isl_set *Context, |
| 2398 | Scop::array_range Arrays) { |
| 2399 | std::vector<isl::id> OutermostSizeIds; |
| 2400 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2401 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2402 | for (isl::id Id : OutermostSizeIds) { |
| 2403 | int dim = isl_set_find_dim_by_id(Context, isl_dim_param, Id.get()); |
| 2404 | Context = isl_set_lower_bound_si(Context, isl_dim_param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | return Context; |
| 2408 | } |
| 2409 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2410 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2411 | if (PollyIgnoreParamBounds) |
| 2412 | return; |
| 2413 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2414 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2415 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2416 | |
| 2417 | // Align the parameters of all data structures to the model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2418 | Context = isl_set_align_params(Context, Space.copy()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2419 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2420 | // Bound the size of the fortran array dimensions. |
| 2421 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2422 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2423 | // As all parameters are known add bounds to them. |
| 2424 | addParameterBounds(); |
| 2425 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2426 | for (ScopStmt &Stmt : *this) |
| 2427 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2428 | // Simplify the schedule according to the context too. |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 2429 | Schedule = isl_schedule_gist_domain_params(Schedule, getContext().release()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2432 | static __isl_give isl_set * |
| 2433 | simplifyAssumptionContext(__isl_take isl_set *AssumptionContext, |
| 2434 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2435 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2436 | // simplify the context with the constraints that are needed for anything to |
| 2437 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2438 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2439 | // domains, thus we cannot use the remaining domain to simplify the |
| 2440 | // assumptions. |
| 2441 | if (!S.hasErrorBlock()) { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2442 | isl_set *DomainParameters = isl_union_set_params(S.getDomains().release()); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2443 | AssumptionContext = |
| 2444 | isl_set_gist_params(AssumptionContext, DomainParameters); |
| 2445 | } |
| 2446 | |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2447 | AssumptionContext = |
| 2448 | isl_set_gist_params(AssumptionContext, S.getContext().release()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2449 | return AssumptionContext; |
| 2450 | } |
| 2451 | |
| 2452 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2453 | // The parameter constraints of the iteration domains give us a set of |
| 2454 | // constraints that need to hold for all cases where at least a single |
| 2455 | // statement iteration is executed in the whole scop. We now simplify the |
| 2456 | // assumed context under the assumption that such constraints hold and at |
| 2457 | // least a single statement iteration is executed. For cases where no |
| 2458 | // statement instances are executed, the assumptions we have taken about |
| 2459 | // the executed code do not matter and can be changed. |
| 2460 | // |
| 2461 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2462 | // the set of statement instances that are executed. Otherwise we |
| 2463 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2464 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2465 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2466 | // performed. In such a case, modifying the run-time conditions and |
| 2467 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2468 | // to not be executed. |
| 2469 | // |
| 2470 | // Example: |
| 2471 | // |
| 2472 | // When delinearizing the following code: |
| 2473 | // |
| 2474 | // for (long i = 0; i < 100; i++) |
| 2475 | // for (long j = 0; j < m; j++) |
| 2476 | // A[i+p][j] = 1.0; |
| 2477 | // |
| 2478 | // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2479 | // otherwise we would access out of bound data. Now, knowing that code is |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2480 | // only executed for the case m >= 0, it is sufficient to assume p >= 0. |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2481 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2482 | InvalidContext = |
| 2483 | isl_set_align_params(InvalidContext, getParamSpace().release()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2486 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2487 | static isl::stat |
| 2488 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2489 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2490 | isl::pw_aff LastDimAff; |
| 2491 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2492 | unsigned Pos; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2493 | isl::ctx Ctx = Set.get_ctx(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2494 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2495 | Set = Set.remove_divs(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2496 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2497 | if (isl_set_n_basic_set(Set.get()) >= MaxDisjunctsInDomain) |
| 2498 | return isl::stat::error; |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2499 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2500 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2501 | // lexmax computation will take too long if this number is high. |
| 2502 | // |
| 2503 | // Experiments with a simple test case using an i7 4800MQ: |
| 2504 | // |
| 2505 | // #Parameters involved | Time (in sec) |
| 2506 | // 6 | 0.01 |
| 2507 | // 7 | 0.04 |
| 2508 | // 8 | 0.12 |
| 2509 | // 9 | 0.40 |
| 2510 | // 10 | 1.54 |
| 2511 | // 11 | 6.78 |
| 2512 | // 12 | 30.38 |
| 2513 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2514 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2515 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2516 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2517 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2518 | InvolvedParams++; |
| 2519 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2520 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2521 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 2524 | if (isl_set_n_basic_set(Set.get()) > RunTimeChecksMaxAccessDisjuncts) |
| 2525 | return isl::stat::error; |
| 2526 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2527 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2528 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2529 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2530 | if (isl_ctx_last_error(Ctx.get()) == isl_error_quota) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2531 | return isl::stat::error; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2532 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2533 | MinPMA = MinPMA.coalesce(); |
| 2534 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2535 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2536 | // Adjust the last dimension of the maximal access by one as we want to |
| 2537 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2538 | // we test during code generation might now point after the end of the |
| 2539 | // allocated array but we will never dereference it anyway. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2540 | assert(MaxPMA.dim(isl::dim::out) && "Assumed at least one output dimension"); |
| 2541 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2542 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2543 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2544 | OneAff = OneAff.add_constant_si(1); |
| 2545 | LastDimAff = LastDimAff.add(OneAff); |
| 2546 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2547 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2548 | MinMaxAccesses.push_back(std::make_pair(MinPMA.copy(), MaxPMA.copy())); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2549 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2550 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2553 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2554 | isl_set *Domain = MA->getStatement()->getDomain().release(); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2555 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2556 | return isl_set_reset_tuple_id(Domain); |
| 2557 | } |
| 2558 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2559 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2560 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2561 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2562 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2563 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2564 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2565 | isl::union_set Domains = S.getDomains(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2566 | isl::union_map Accesses = isl::union_map::empty(S.getParamSpace()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2567 | |
| 2568 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 2569 | Accesses = Accesses.add_map(give(MA->getAccessRelation().release())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2570 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2571 | Accesses = Accesses.intersect_domain(Domains); |
| 2572 | isl::union_set Locations = Accesses.range(); |
| 2573 | Locations = Locations.coalesce(); |
| 2574 | Locations = Locations.detect_equalities(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2575 | |
| 2576 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2577 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2578 | }; |
| 2579 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2582 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2583 | /// |
| 2584 | ///{ |
| 2585 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2586 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2587 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2588 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2589 | : RN->getNodeAs<BasicBlock>(); |
| 2590 | } |
| 2591 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2592 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2593 | static inline BasicBlock * |
| 2594 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2595 | if (RN->isSubRegion()) { |
| 2596 | assert(idx == 0); |
| 2597 | return RN->getNodeAs<Region>()->getExit(); |
| 2598 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2599 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2602 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2603 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2604 | if (!RN->isSubRegion()) { |
| 2605 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2606 | Loop *L = LI.getLoopFor(BB); |
| 2607 | |
| 2608 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2609 | // they are not part of an actual loop in the control flow graph. |
| 2610 | // Nevertheless, we handle certain unreachable statements that are common |
| 2611 | // when modeling run-time bounds checks as being part of the loop to be |
| 2612 | // able to model them and to later eliminate the run-time bounds checks. |
| 2613 | // |
| 2614 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2615 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2616 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2617 | // allows us to model the following code. |
| 2618 | // |
| 2619 | // for (i = 0; i < N; i++) { |
| 2620 | // if (i > 1024) |
| 2621 | // abort(); <- this abort might be translated to an |
| 2622 | // unreachable |
| 2623 | // |
| 2624 | // A[i] = ... |
| 2625 | // } |
| 2626 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2627 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2628 | return L; |
| 2629 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2630 | |
| 2631 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2632 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2633 | while (L && NonAffineSubRegion->contains(L)) |
| 2634 | L = L->getParentLoop(); |
| 2635 | return L; |
| 2636 | } |
| 2637 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2638 | /// Get the number of blocks in @p L. |
| 2639 | /// |
| 2640 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2641 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2642 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2643 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2644 | /// scop and represent run-time conditions which we want to model and |
| 2645 | /// subsequently speculate away. |
| 2646 | /// |
| 2647 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2648 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2649 | unsigned NumBlocks = L->getNumBlocks(); |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2650 | SmallVector<llvm::BasicBlock *, 4> ExitBlocks; |
| 2651 | L->getExitBlocks(ExitBlocks); |
| 2652 | |
| 2653 | for (auto ExitBlock : ExitBlocks) { |
| 2654 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2655 | NumBlocks++; |
| 2656 | } |
| 2657 | return NumBlocks; |
| 2658 | } |
| 2659 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2660 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2661 | if (!RN->isSubRegion()) |
| 2662 | return 1; |
| 2663 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2664 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2665 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2668 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2669 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2670 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2671 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2672 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2673 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2674 | return true; |
| 2675 | return false; |
| 2676 | } |
| 2677 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2678 | ///} |
| 2679 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2680 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 2681 | unsigned Dim, Loop *L) { |
Michael Kruse | 88a2256 | 2016-03-29 07:50:52 +0000 | [diff] [blame] | 2682 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2683 | isl_id *DimId = |
| 2684 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 2685 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 2686 | } |
| 2687 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2688 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2689 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2692 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2693 | auto DIt = DomainMap.find(BB); |
| 2694 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2695 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2696 | |
| 2697 | auto &RI = *R.getRegionInfo(); |
| 2698 | auto *BBR = RI.getRegionFor(BB); |
| 2699 | while (BBR->getEntry() == BB) |
| 2700 | BBR = BBR->getParent(); |
| 2701 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2704 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2705 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2706 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2707 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2708 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2709 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2710 | int LD = getRelativeLoopDepth(L); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2711 | auto *S = isl_set_universe(isl_space_set_alloc(getIslCtx(), 0, LD + 1)); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2712 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2713 | while (LD-- >= 0) { |
| 2714 | S = addDomainDimId(S, LD + 1, L); |
| 2715 | L = L->getParentLoop(); |
| 2716 | } |
| 2717 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2718 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2719 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2720 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2721 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2722 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2723 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2724 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2725 | return false; |
| 2726 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2727 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2728 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2731 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2732 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2733 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2734 | // will cause problems when building up a ScopStmt for them. |
| 2735 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2736 | // instructions in the error block which, if the error block is not modeled, |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2737 | // can themselves not be constructed properly. To this end we will replace |
| 2738 | // the domains of error blocks and those only reachable via error blocks |
| 2739 | // with an empty set. Additionally, we will record for each block under which |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2740 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2741 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2742 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2743 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2744 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2745 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2748 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2749 | /// to be compatible to domains constructed for loop @p NewL. |
| 2750 | /// |
| 2751 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2752 | /// edge from @p OldL to @p NewL. |
| 2753 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2754 | __isl_take isl_set *Dom, |
| 2755 | Loop *OldL, Loop *NewL) { |
| 2756 | |
| 2757 | // If the loops are the same there is nothing to do. |
| 2758 | if (NewL == OldL) |
| 2759 | return Dom; |
| 2760 | |
| 2761 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2762 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2763 | // If both loops are non-affine loops there is nothing to do. |
| 2764 | if (OldDepth == -1 && NewDepth == -1) |
| 2765 | return Dom; |
| 2766 | |
| 2767 | // Distinguish three cases: |
| 2768 | // 1) The depth is the same but the loops are not. |
| 2769 | // => One loop was left one was entered. |
| 2770 | // 2) The depth increased from OldL to NewL. |
| 2771 | // => One loop was entered, none was left. |
| 2772 | // 3) The depth decreased from OldL to NewL. |
| 2773 | // => Loops were left were difference of the depths defines how many. |
| 2774 | if (OldDepth == NewDepth) { |
| 2775 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2776 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2777 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2778 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2779 | } else if (OldDepth < NewDepth) { |
| 2780 | assert(OldDepth + 1 == NewDepth); |
| 2781 | auto &R = S.getRegion(); |
| 2782 | (void)R; |
| 2783 | assert(NewL->getParentLoop() == OldL || |
| 2784 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2785 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2786 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2787 | } else { |
| 2788 | assert(OldDepth > NewDepth); |
| 2789 | int Diff = OldDepth - NewDepth; |
| 2790 | int NumDim = isl_set_n_dim(Dom); |
| 2791 | assert(NumDim >= Diff); |
| 2792 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2793 | } |
| 2794 | |
| 2795 | return Dom; |
| 2796 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2797 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2798 | bool Scop::propagateInvalidStmtDomains( |
| 2799 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2800 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2801 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2802 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2803 | for (auto *RN : RTraversal) { |
| 2804 | |
| 2805 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2806 | // subregions. |
| 2807 | if (RN->isSubRegion()) { |
| 2808 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2809 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2810 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2811 | continue; |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2816 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2817 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2818 | assert(Domain && "Cannot propagate a nullptr"); |
| 2819 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2820 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2821 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2822 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2823 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2824 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2825 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2826 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2827 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2828 | isl::set DomPar = Domain.params(); |
| 2829 | recordAssumption(ERRORBLOCK, DomPar.release(), |
| 2830 | BB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2831 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2834 | if (InvalidDomain.is_empty()) { |
| 2835 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2836 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2839 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2840 | auto *TI = BB->getTerminator(); |
| 2841 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2842 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2843 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2844 | |
| 2845 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2846 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2847 | continue; |
| 2848 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2849 | // Skip backedges. |
| 2850 | if (DT.dominates(SuccBB, BB)) |
| 2851 | continue; |
| 2852 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2853 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2854 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2855 | auto *AdjustedInvalidDomain = adjustDomainDimensions( |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2856 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2857 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2858 | auto *SuccInvalidDomain = InvalidDomainMap[SuccBB].copy(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2859 | SuccInvalidDomain = |
| 2860 | isl_set_union(SuccInvalidDomain, AdjustedInvalidDomain); |
| 2861 | SuccInvalidDomain = isl_set_coalesce(SuccInvalidDomain); |
| 2862 | unsigned NumConjucts = isl_set_n_basic_set(SuccInvalidDomain); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2863 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2864 | InvalidDomainMap[SuccBB] = isl::manage(SuccInvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2865 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2866 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2867 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2868 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2869 | continue; |
| 2870 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2871 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2872 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2873 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2874 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2875 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2876 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2877 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2878 | |
| 2879 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2882 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2883 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2884 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2885 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2886 | |
| 2887 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2888 | // domain to the exit block of the region. Otherwise we are done. |
| 2889 | auto *RI = R.getRegionInfo(); |
| 2890 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2891 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2892 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2893 | return; |
| 2894 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2895 | // Do not propagate the domain if there is a loop backedge inside the region |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2896 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2897 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2898 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2899 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2900 | BBLoop->getLoopLatches(LatchBBs); |
| 2901 | for (auto *LatchBB : LatchBBs) |
| 2902 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2903 | return; |
| 2904 | L = L->getParentLoop(); |
| 2905 | } |
| 2906 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2907 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2908 | assert(Domain && "Cannot propagate a nullptr"); |
| 2909 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2910 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2911 | |
| 2912 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2913 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2914 | isl::set AdjustedDomain = isl::manage( |
| 2915 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2916 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2917 | |
| 2918 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2919 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2920 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2921 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2922 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2923 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2924 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2925 | FinishedExitBlocks.insert(ExitBB); |
| 2926 | } |
| 2927 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2928 | bool Scop::buildDomainsWithBranchConstraints( |
| 2929 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2930 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2931 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2932 | // To create the domain for each block in R we iterate over all blocks and |
| 2933 | // subregions in R and propagate the conditions under which the current region |
| 2934 | // element is executed. To this end we iterate in reverse post order over R as |
| 2935 | // it ensures that we first visit all predecessors of a region node (either a |
| 2936 | // basic block or a subregion) before we visit the region node itself. |
| 2937 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2938 | // there we propagate the current domain to all successors, however we add the |
| 2939 | // condition that the successor is actually executed next. |
| 2940 | // As we are only interested in non-loop carried constraints here we can |
| 2941 | // simply skip loop back edges. |
| 2942 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2943 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2944 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2945 | for (auto *RN : RTraversal) { |
| 2946 | |
| 2947 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2948 | // subregions. |
| 2949 | if (RN->isSubRegion()) { |
| 2950 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2951 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2952 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2953 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2954 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2955 | continue; |
| 2956 | } |
| 2957 | } |
| 2958 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2959 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2960 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2961 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2962 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2963 | TerminatorInst *TI = BB->getTerminator(); |
| 2964 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2965 | if (isa<UnreachableInst>(TI)) |
| 2966 | continue; |
| 2967 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2968 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2969 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2970 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2971 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2972 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2973 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2974 | // Propagate the domain from BB directly to blocks that have a superset |
| 2975 | // domain, at the moment only region exit nodes of regions that start in BB. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2976 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2977 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2978 | |
| 2979 | // If all successors of BB have been set a domain through the propagation |
| 2980 | // above we do not need to build condition sets but can just skip this |
| 2981 | // block. However, it is important to note that this is a local property |
| 2982 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 2983 | // local variable. |
| 2984 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 2985 | return FinishedExitBlocks.count(SuccBB); |
| 2986 | }; |
| 2987 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 2988 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Build the condition sets for the successor nodes of the current region |
| 2991 | // node. If it is a non-affine subregion we will always execute the single |
| 2992 | // exit node, hence the single entry node domain is the condition set. For |
| 2993 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2994 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2995 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2996 | ConditionSets.push_back(Domain.copy()); |
| 2997 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2998 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2999 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Now iterate over the successors and set their initial domain based on |
| 3002 | // their condition set. We skip back edges here and have to be careful when |
| 3003 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 3004 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3005 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3006 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3007 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3008 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3009 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3010 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3011 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3012 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3013 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3014 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 3015 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3016 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3017 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3018 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3019 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3020 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3021 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3022 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3023 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 3024 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3025 | CondSet = isl::manage( |
| 3026 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3027 | |
| 3028 | // Set the domain for the successor or merge it with an existing domain in |
| 3029 | // case there are multiple paths (without loop back edges) to the |
| 3030 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3031 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 3032 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3033 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3034 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3035 | } else { |
| 3036 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3037 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3038 | SuccDomain = CondSet; |
| 3039 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3040 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3041 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 3042 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 3043 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3044 | // In case this happens we will clean up and bail. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3045 | if (isl_set_n_basic_set(SuccDomain.get()) < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3046 | continue; |
| 3047 | |
| 3048 | invalidate(COMPLEXITY, DebugLoc()); |
| 3049 | while (++u < ConditionSets.size()) |
| 3050 | isl_set_free(ConditionSets[u]); |
| 3051 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3054 | |
| 3055 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3058 | isl::set Scop::getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain, |
| 3059 | DominatorTree &DT, |
| 3060 | LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3061 | // If @p BB is the ScopEntry we are done |
| 3062 | if (R.getEntry() == BB) |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3063 | return isl::set::universe(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3064 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3065 | // The region info of this function. |
| 3066 | auto &RI = *R.getRegionInfo(); |
| 3067 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3068 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3069 | |
| 3070 | // A domain to collect all predecessor domains, thus all conditions under |
| 3071 | // which the block is executed. To this end we start with the empty domain. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3072 | isl::set PredDom = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3073 | |
| 3074 | // Set of regions of which the entry block domain has been propagated to BB. |
| 3075 | // all predecessors inside any of the regions can be skipped. |
| 3076 | SmallSet<Region *, 8> PropagatedRegions; |
| 3077 | |
| 3078 | for (auto *PredBB : predecessors(BB)) { |
| 3079 | // Skip backedges. |
| 3080 | if (DT.dominates(BB, PredBB)) |
| 3081 | continue; |
| 3082 | |
| 3083 | // If the predecessor is in a region we used for propagation we can skip it. |
| 3084 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 3085 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 3086 | PredBBInRegion)) { |
| 3087 | continue; |
| 3088 | } |
| 3089 | |
| 3090 | // Check if there is a valid region we can use for propagation, thus look |
| 3091 | // for a region that contains the predecessor and has @p BB as exit block. |
| 3092 | auto *PredR = RI.getRegionFor(PredBB); |
| 3093 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 3094 | PredR->getParent(); |
| 3095 | |
| 3096 | // If a valid region for propagation was found use the entry of that region |
| 3097 | // for propagation, otherwise the PredBB directly. |
| 3098 | if (PredR->getExit() == BB) { |
| 3099 | PredBB = PredR->getEntry(); |
| 3100 | PropagatedRegions.insert(PredR); |
| 3101 | } |
| 3102 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 3103 | auto *PredBBDom = getDomainConditions(PredBB).release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3104 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 3105 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3106 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 3107 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3108 | PredDom = PredDom.unite(isl::manage(PredBBDom)); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | return PredDom; |
| 3112 | } |
| 3113 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3114 | bool Scop::propagateDomainConstraints( |
| 3115 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3116 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3117 | // Iterate over the region R and propagate the domain constrains from the |
| 3118 | // predecessors to the current node. In contrast to the |
| 3119 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 3120 | // information from the predecessors instead of pushing it to the successors. |
| 3121 | // Additionally, we assume the domains to be already present in the domain |
| 3122 | // map here. However, we iterate again in reverse post order so we know all |
| 3123 | // predecessors have been visited before a block or non-affine subregion is |
| 3124 | // visited. |
| 3125 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3126 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3127 | for (auto *RN : RTraversal) { |
| 3128 | |
| 3129 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 3130 | // subregions. |
| 3131 | if (RN->isSubRegion()) { |
| 3132 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3133 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3134 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3135 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3136 | continue; |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3141 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 3142 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3143 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3144 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3145 | isl::set PredDom = getPredecessorDomainConstraints(BB, Domain, DT, LI); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3146 | Domain = Domain.intersect(PredDom).coalesce(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 3147 | Domain = Domain.align_params(getParamSpace()); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3148 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3149 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3150 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3151 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3152 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3153 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3154 | |
| 3155 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3158 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 3159 | /// |
| 3160 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 3161 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3162 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3163 | /// |
| 3164 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3165 | static __isl_give isl_map * |
| 3166 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 3167 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 3168 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
Tobias Grosser | f4fe34b | 2017-03-16 21:33:20 +0000 | [diff] [blame] | 3169 | for (unsigned u = 0; u < isl_map_dim(NextIterationMap, isl_dim_in); u++) |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3170 | if (u != Dim) |
| 3171 | NextIterationMap = |
| 3172 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 3173 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 3174 | C = isl_constraint_set_constant_si(C, 1); |
| 3175 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 3176 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 3177 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 3178 | return NextIterationMap; |
| 3179 | } |
| 3180 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3181 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3182 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3183 | int LoopDepth = getRelativeLoopDepth(L); |
| 3184 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3185 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3186 | BasicBlock *HeaderBB = L->getHeader(); |
| 3187 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3188 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3189 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3190 | isl::map NextIterationMap = isl::manage( |
| 3191 | createNextIterationMap(HeaderBBDom.get_space().release(), LoopDepth)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3192 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3193 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3194 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3195 | SmallVector<llvm::BasicBlock *, 4> LatchBlocks; |
| 3196 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3197 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3198 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3199 | |
| 3200 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3201 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3202 | if (!LatchBBDom) |
| 3203 | continue; |
| 3204 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3205 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3206 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3207 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 3208 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 3209 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 3210 | |
| 3211 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3212 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3213 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3214 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3215 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3216 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 3217 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3218 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3219 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3220 | // Free the non back edge condition set as we do not need it. |
| 3221 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3222 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3223 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3224 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3225 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3226 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3227 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3228 | BackedgeCondition = BackedgeCondition.project_out( |
| 3229 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3230 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3231 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3232 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3233 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3234 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3235 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3236 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3237 | isl::set UnionBackedgeConditionComplement = |
| 3238 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3239 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3240 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3241 | 0); |
| 3242 | UnionBackedgeConditionComplement = |
| 3243 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3244 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3245 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3246 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3247 | auto Parts = partitionSetParts(HeaderBBDom.copy(), LoopDepth); |
| 3248 | HeaderBBDom = isl::manage(Parts.second); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3249 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3250 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3251 | // the bounded assumptions to the context as they are already implied by the |
| 3252 | // <nsw> tag. |
| 3253 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 3254 | isl_set_free(Parts.first); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3255 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3258 | isl_set *UnboundedCtx = isl_set_params(Parts.first); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3259 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3260 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3261 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3264 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3265 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3266 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3267 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3268 | if (!PointerBaseInst) |
| 3269 | return nullptr; |
| 3270 | |
| 3271 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3272 | if (!BasePtrStmt) |
| 3273 | return nullptr; |
| 3274 | |
| 3275 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3276 | } |
| 3277 | |
| 3278 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3279 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3280 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3281 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3282 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3283 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3284 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3285 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3286 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3287 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3288 | |
| 3289 | return false; |
| 3290 | } |
| 3291 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3292 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3293 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3294 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3295 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3296 | if (buildAliasGroups(AA)) { |
| 3297 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3298 | // collect statistics so we do it here explicitly. |
| 3299 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3300 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3301 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3302 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3303 | |
| 3304 | // If a problem occurs while building the alias groups we need to delete |
| 3305 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3306 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3307 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3308 | |
| 3309 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3310 | << " could not be created as the number of parameters involved " |
| 3311 | "is too high. The SCoP will be " |
| 3312 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3313 | "the maximal number of parameters but be advised that the " |
| 3314 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3315 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3318 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3319 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3320 | AliasSetTracker AST(AA); |
| 3321 | |
| 3322 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3323 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3324 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3325 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3326 | isl_set *StmtDomain = Stmt.getDomain().release(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3327 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3328 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3329 | |
| 3330 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3331 | if (StmtDomainEmpty) |
| 3332 | continue; |
| 3333 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3334 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3335 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3336 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3337 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3338 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3339 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3340 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3341 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3342 | else |
| 3343 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3344 | AST.add(Acc); |
| 3345 | } |
| 3346 | } |
| 3347 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3348 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3349 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3350 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3351 | continue; |
| 3352 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3353 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3354 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3355 | if (AG.size() < 2) |
| 3356 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3357 | AliasGroups.push_back(std::move(AG)); |
| 3358 | } |
| 3359 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3360 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3361 | } |
| 3362 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3363 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3364 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3365 | AliasGroupTy NewAG; |
| 3366 | AliasGroupTy &AG = AliasGroups[u]; |
| 3367 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3368 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3369 | while (AGI != AG.end()) { |
| 3370 | MemoryAccess *MA = *AGI; |
| 3371 | isl_set *MADomain = getAccessDomain(MA); |
| 3372 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3373 | NewAG.push_back(MA); |
| 3374 | AGI = AG.erase(AGI); |
| 3375 | isl_set_free(MADomain); |
| 3376 | } else { |
| 3377 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3378 | AGI++; |
| 3379 | } |
| 3380 | } |
| 3381 | if (NewAG.size() > 1) |
| 3382 | AliasGroups.push_back(std::move(NewAG)); |
| 3383 | isl_set_free(AGDomain); |
| 3384 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3388 | // To create sound alias checks we perform the following steps: |
| 3389 | // o) We partition each group into read only and non read only accesses. |
| 3390 | // o) For each group with more than one base pointer we then compute minimal |
| 3391 | // and maximal accesses to each array of a group in read only and non |
| 3392 | // read only partitions separately. |
| 3393 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3394 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3395 | |
| 3396 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3397 | |
| 3398 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3399 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3400 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3401 | if (!hasFeasibleRuntimeContext()) |
| 3402 | return false; |
| 3403 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3404 | { |
| 3405 | IslMaxOperationsGuard MaxOpGuard(getIslCtx(), OptComputeOut); |
| 3406 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3407 | if (!Valid) |
| 3408 | return false; |
| 3409 | } |
| 3410 | if (isl_ctx_last_error(getIslCtx()) == isl_error_quota) { |
| 3411 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3412 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3413 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3414 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3415 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3416 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3419 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3420 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3421 | AliasGroupTy ReadOnlyAccesses; |
| 3422 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3423 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3424 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3425 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3426 | if (AliasGroup.size() < 2) |
| 3427 | return true; |
| 3428 | |
| 3429 | for (MemoryAccess *Access : AliasGroup) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3430 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias", |
| 3431 | Access->getAccessInstruction()) |
| 3432 | << "Possibly aliasing pointer, use restrict keyword."); |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3433 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3434 | if (HasWriteAccess.count(Array)) { |
| 3435 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3436 | ReadWriteAccesses.push_back(Access); |
| 3437 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3438 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3439 | ReadOnlyAccesses.push_back(Access); |
| 3440 | } |
| 3441 | } |
| 3442 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3443 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3444 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3445 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3446 | return true; |
| 3447 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3448 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3449 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3450 | return true; |
| 3451 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3452 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3453 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3454 | for (MemoryAccess *MA : AliasGroup) { |
| 3455 | if (!MA->isAffine()) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3456 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(), |
| 3457 | MA->getAccessInstruction()->getParent()); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3458 | return false; |
| 3459 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3463 | // their base pointers are available. |
| 3464 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3465 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3466 | addRequiredInvariantLoad( |
| 3467 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3468 | } |
| 3469 | |
| 3470 | MinMaxAliasGroups.emplace_back(); |
| 3471 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3472 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3473 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3474 | |
| 3475 | bool Valid; |
| 3476 | |
| 3477 | Valid = |
| 3478 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3479 | |
| 3480 | if (!Valid) |
| 3481 | return false; |
| 3482 | |
| 3483 | // Bail out if the number of values we need to compare is too large. |
| 3484 | // This is important as the number of comparisons grows quadratically with |
| 3485 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3486 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3487 | RunTimeChecksMaxArraysPerGroup) |
| 3488 | return false; |
| 3489 | |
| 3490 | Valid = |
| 3491 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3492 | |
| 3493 | if (!Valid) |
| 3494 | return false; |
| 3495 | |
| 3496 | return true; |
| 3497 | } |
| 3498 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3499 | /// Get the smallest loop that contains @p S but is not in @p S. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3500 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3501 | // Start with the smallest loop containing the entry and expand that |
| 3502 | // loop until it contains all blocks in the region. If there is a loop |
| 3503 | // containing all blocks in the region check if it is itself contained |
| 3504 | // and if so take the parent loop as it will be the smallest containing |
| 3505 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3506 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3507 | while (L) { |
| 3508 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3509 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3510 | AllContained &= L->contains(BB); |
| 3511 | if (AllContained) |
| 3512 | break; |
| 3513 | L = L->getParentLoop(); |
| 3514 | } |
| 3515 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3516 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3519 | int Scop::NextScopID = 0; |
| 3520 | |
| 3521 | std::string Scop::CurrentFunc = ""; |
| 3522 | |
| 3523 | int Scop::getNextID(std::string ParentFunc) { |
| 3524 | if (ParentFunc != CurrentFunc) { |
| 3525 | CurrentFunc = ParentFunc; |
| 3526 | NextScopID = 0; |
| 3527 | } |
| 3528 | return NextScopID++; |
| 3529 | } |
| 3530 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3531 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3532 | ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 35bdcaf | 2017-05-15 13:43:01 +0000 | [diff] [blame] | 3533 | : SE(&ScalarEvolution), R(R), name(R.getNameStr()), IsOptimized(false), |
Siddharth Bhat | 47c7237 | 2017-07-05 15:07:28 +0000 | [diff] [blame] | 3534 | HasSingleExitEdge(R.getExitingBlock()), HasErrorBlock(false), |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3535 | MaxLoopDepth(0), CopyStmtsNum(0), SkipScop(false), DC(DC), ORE(ORE), |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 3536 | IslCtx(isl_ctx_alloc(), isl_ctx_free), Context(nullptr), |
| 3537 | Affinator(this, LI), AssumedContext(nullptr), InvalidContext(nullptr), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3538 | Schedule(nullptr), |
| 3539 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3540 | if (IslOnErrorAbort) |
| 3541 | isl_options_set_on_error(getIslCtx(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3542 | buildContext(); |
| 3543 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3544 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3545 | void Scop::foldSizeConstantsToRight() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3546 | isl_union_set *Accessed = isl_union_map_range(getAccesses().release()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3547 | |
| 3548 | for (auto Array : arrays()) { |
| 3549 | if (Array->getNumberOfDimensions() <= 1) |
| 3550 | continue; |
| 3551 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3552 | isl_space *Space = Array->getSpace().release(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3553 | |
| 3554 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3555 | |
| 3556 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3557 | isl_space_free(Space); |
| 3558 | continue; |
| 3559 | } |
| 3560 | |
| 3561 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3562 | |
| 3563 | isl_map *Transform = |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3564 | isl_map_universe(isl_space_map_from_set(Array->getSpace().release())); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3565 | |
| 3566 | std::vector<int> Int; |
| 3567 | |
| 3568 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3569 | for (int i = 0; i < Dims; i++) { |
| 3570 | isl_set *DimOnly = |
| 3571 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3572 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3573 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3574 | |
| 3575 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3576 | |
| 3577 | if (i == Dims - 1) { |
| 3578 | Int.push_back(1); |
| 3579 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3580 | isl_basic_set_free(DimHull); |
| 3581 | continue; |
| 3582 | } |
| 3583 | |
| 3584 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3585 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3586 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3587 | isl_aff_free(Diff); |
| 3588 | |
| 3589 | int ValInt = 1; |
| 3590 | |
| 3591 | if (isl_val_is_int(Val)) |
| 3592 | ValInt = isl_val_get_num_si(Val); |
| 3593 | isl_val_free(Val); |
| 3594 | |
| 3595 | Int.push_back(ValInt); |
| 3596 | |
| 3597 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3598 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3599 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3600 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3601 | Transform = isl_map_add_constraint(Transform, C); |
| 3602 | isl_basic_set_free(DimHull); |
| 3603 | continue; |
| 3604 | } |
| 3605 | |
| 3606 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3607 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3608 | |
| 3609 | int ValInt = 1; |
| 3610 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3611 | ValInt = 0; |
| 3612 | } |
| 3613 | |
| 3614 | Int.push_back(ValInt); |
| 3615 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3616 | isl_basic_set_free(DimHull); |
| 3617 | isl_basic_set_free(ZeroSet); |
| 3618 | } |
| 3619 | |
| 3620 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3621 | |
| 3622 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3623 | isl_set_free(Elements); |
| 3624 | isl_set_free(MappedElements); |
| 3625 | isl_map_free(Transform); |
| 3626 | continue; |
| 3627 | } |
| 3628 | |
| 3629 | isl_set_free(MappedElements); |
| 3630 | |
| 3631 | bool CanFold = true; |
| 3632 | |
| 3633 | if (Int[0] <= 1) |
| 3634 | CanFold = false; |
| 3635 | |
| 3636 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3637 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3638 | if (Int[0] != Int[i] && Int[i]) |
| 3639 | CanFold = false; |
| 3640 | |
| 3641 | if (!CanFold) { |
| 3642 | isl_set_free(Elements); |
| 3643 | isl_map_free(Transform); |
| 3644 | continue; |
| 3645 | } |
| 3646 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3647 | for (auto &Access : AccessFunctions) |
| 3648 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3649 | Access->setAccessRelation(Access->getAccessRelation().apply_range( |
| 3650 | isl::manage(isl_map_copy(Transform)))); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3651 | |
| 3652 | isl_map_free(Transform); |
| 3653 | |
| 3654 | std::vector<const SCEV *> Sizes; |
| 3655 | for (unsigned i = 0; i < NumDims; i++) { |
| 3656 | auto Size = Array->getDimensionSize(i); |
| 3657 | |
| 3658 | if (i == NumDims - 1) |
| 3659 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3660 | Sizes.push_back(Size); |
| 3661 | } |
| 3662 | |
| 3663 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3664 | |
| 3665 | isl_set_free(Elements); |
| 3666 | } |
| 3667 | isl_union_set_free(Accessed); |
| 3668 | return; |
| 3669 | } |
| 3670 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3671 | void Scop::markFortranArrays() { |
| 3672 | for (ScopStmt &Stmt : Stmts) { |
| 3673 | for (MemoryAccess *MemAcc : Stmt) { |
| 3674 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3675 | if (!FAD) |
| 3676 | continue; |
| 3677 | |
| 3678 | // TODO: const_cast-ing to edit |
| 3679 | ScopArrayInfo *SAI = |
| 3680 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3681 | assert(SAI && "memory access into a Fortran array does not " |
| 3682 | "have an associated ScopArrayInfo"); |
| 3683 | SAI->applyAndSetFAD(FAD); |
| 3684 | } |
| 3685 | } |
| 3686 | } |
| 3687 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3688 | void Scop::finalizeAccesses() { |
| 3689 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3690 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3691 | foldAccessRelations(); |
| 3692 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3693 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3696 | Scop::~Scop() { |
| 3697 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 3698 | isl_set_free(AssumedContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3699 | isl_set_free(InvalidContext); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3700 | isl_schedule_free(Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3701 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 3702 | for (auto &It : ParameterIds) |
| 3703 | isl_id_free(It.second); |
| 3704 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3705 | for (auto &AS : RecordedAssumptions) |
| 3706 | isl_set_free(AS.Set); |
| 3707 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3708 | // Free the alias groups |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3709 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3710 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3711 | isl_pw_multi_aff_free(MMA.first); |
| 3712 | isl_pw_multi_aff_free(MMA.second); |
| 3713 | } |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3714 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3715 | isl_pw_multi_aff_free(MMA.first); |
| 3716 | isl_pw_multi_aff_free(MMA.second); |
| 3717 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3718 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3719 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3720 | for (const auto &IAClass : InvariantEquivClasses) |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3721 | isl_set_free(IAClass.ExecutionContext); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3722 | |
| 3723 | // Explicitly release all Scop objects and the underlying isl objects before |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3724 | // we release the isl context. |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3725 | Stmts.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3726 | ScopArrayInfoSet.clear(); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3727 | ScopArrayInfoMap.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3728 | ScopArrayNameMap.clear(); |
Roman Gareev | e2ee79a | 2016-08-21 11:09:19 +0000 | [diff] [blame] | 3729 | AccessFunctions.clear(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3732 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3733 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3734 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3735 | for (ScopStmt &Stmt : *this) |
| 3736 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3737 | if (!Access->isArrayKind()) |
| 3738 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3739 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3740 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3741 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3742 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3743 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3744 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3745 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3746 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3747 | DivisibleSize /= 2; |
| 3748 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3749 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3752 | for (auto &Stmt : *this) |
| 3753 | for (auto &Access : Stmt) |
| 3754 | Access->updateDimensionality(); |
| 3755 | } |
| 3756 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3757 | void Scop::foldAccessRelations() { |
| 3758 | for (auto &Stmt : *this) |
| 3759 | for (auto &Access : Stmt) |
| 3760 | Access->foldAccessRelation(); |
| 3761 | } |
| 3762 | |
| 3763 | void Scop::assumeNoOutOfBounds() { |
| 3764 | for (auto &Stmt : *this) |
| 3765 | for (auto &Access : Stmt) |
| 3766 | Access->assumeNoOutOfBound(); |
| 3767 | } |
| 3768 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3769 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
| 3770 | if (Stmt.isRegionStmt()) |
| 3771 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) |
| 3772 | StmtMap.erase(BB); |
| 3773 | else |
| 3774 | StmtMap.erase(Stmt.getBasicBlock()); |
| 3775 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3776 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3777 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete) { |
| 3778 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3779 | if (!ShouldDelete(*StmtIt)) { |
| 3780 | StmtIt++; |
| 3781 | continue; |
| 3782 | } |
| 3783 | |
| 3784 | removeFromStmtMap(*StmtIt); |
| 3785 | StmtIt = Stmts.erase(StmtIt); |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | void Scop::removeStmtNotInDomainMap() { |
| 3790 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Tobias Grosser | 199ec4a | 2017-07-19 16:31:10 +0000 | [diff] [blame] | 3791 | return !this->DomainMap.lookup(Stmt.getEntryBlock()); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3792 | }; |
| 3793 | removeStmts(ShouldDelete); |
| 3794 | } |
| 3795 | |
| 3796 | void Scop::simplifySCoP(bool AfterHoisting) { |
| 3797 | |
| 3798 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3799 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3800 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3801 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3802 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3803 | bool OnlyRead = true; |
| 3804 | for (MemoryAccess *MA : Stmt) { |
| 3805 | if (MA->isRead()) |
| 3806 | continue; |
| 3807 | |
| 3808 | OnlyRead = false; |
| 3809 | break; |
| 3810 | } |
| 3811 | |
| 3812 | RemoveStmt = OnlyRead; |
| 3813 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3814 | return RemoveStmt; |
| 3815 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3816 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3817 | removeStmts(ShouldDelete); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3820 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3821 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3822 | if (!LInst) |
| 3823 | return nullptr; |
| 3824 | |
| 3825 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3826 | LInst = cast<LoadInst>(Rep); |
| 3827 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3828 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3829 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3830 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3831 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3832 | continue; |
| 3833 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3834 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3835 | for (auto *MA : MAs) |
| 3836 | if (MA->getAccessInstruction() == Val) |
| 3837 | return &IAClass; |
| 3838 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3839 | |
| 3840 | return nullptr; |
| 3841 | } |
| 3842 | |
Tobias Grosser | 305d316 | 2017-08-07 00:10:11 +0000 | [diff] [blame] | 3843 | bool Scop::canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
| 3844 | bool MAInvalidCtxIsEmpty, |
| 3845 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3846 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3847 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
| 3848 | // TODO: We can provide more information for better but more expensive |
| 3849 | // results. |
| 3850 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3851 | LInst->getAlignment(), DL)) |
| 3852 | return false; |
| 3853 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3854 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3855 | // |
Siddharth Bhat | 83fe6b5 | 2017-08-08 12:26:32 +0000 | [diff] [blame^] | 3856 | // TODO: This is probably too conservative. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3857 | if (!NonHoistableCtxIsEmpty) |
| 3858 | return false; |
| 3859 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3860 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3861 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3862 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3863 | return true; |
| 3864 | |
| 3865 | // Even if the statement is not modeled precisely we can hoist the load if it |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3866 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3867 | // statement domain. |
| 3868 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3869 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3870 | return false; |
| 3871 | return true; |
| 3872 | } |
| 3873 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3874 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3875 | |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3876 | if (InvMAs.empty()) |
| 3877 | return; |
| 3878 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 3879 | isl::set StmtInvalidCtx = Stmt.getInvalidContext(); |
| 3880 | bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.is_empty(); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3881 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3882 | // Get the context under which the statement is executed but remove the error |
| 3883 | // context under which this statement is reached. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3884 | isl::set DomainCtx = Stmt.getDomain().params(); |
| 3885 | DomainCtx = DomainCtx.subtract(StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3886 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3887 | if (isl_set_n_basic_set(DomainCtx.get()) >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3888 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3889 | invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3890 | return; |
| 3891 | } |
| 3892 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3893 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3894 | // we could have cyclic dependences on the constraints under which the |
| 3895 | // hoisted loads are executed and we could not determine an order in which to |
| 3896 | // pre-load them. This happens because not only lower bounds are part of the |
| 3897 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3898 | for (auto &InvMA : InvMAs) { |
| 3899 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3900 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3901 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3902 | SetVector<Value *> Values; |
| 3903 | for (const SCEV *Parameter : Parameters) { |
| 3904 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3905 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3906 | if (!Values.count(AccInst)) |
| 3907 | continue; |
| 3908 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3909 | if (isl::id ParamId = getIdForParam(Parameter)) { |
| 3910 | int Dim = DomainCtx.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | b58ed8d | 2017-03-17 09:02:53 +0000 | [diff] [blame] | 3911 | if (Dim >= 0) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3912 | DomainCtx = DomainCtx.eliminate(isl::dim::param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3913 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3914 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3915 | } |
| 3916 | } |
| 3917 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3918 | for (auto &InvMA : InvMAs) { |
| 3919 | auto *MA = InvMA.MA; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3920 | isl::set NHCtx = InvMA.NonHoistableCtx; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3921 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3922 | // Check for another invariant access that accesses the same location as |
| 3923 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3924 | // class at the end of InvariantEquivClasses. |
| 3925 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3926 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3927 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3928 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3929 | isl::set MAInvalidCtx = MA->getInvalidContext(); |
| 3930 | bool NonHoistableCtxIsEmpty = NHCtx.is_empty(); |
| 3931 | bool MAInvalidCtxIsEmpty = MAInvalidCtx.is_empty(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3932 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3933 | isl::set MACtx; |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3934 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3935 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3936 | NonHoistableCtxIsEmpty)) { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3937 | MACtx = isl::set::universe(DomainCtx.get_space()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3938 | } else { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3939 | MACtx = DomainCtx; |
| 3940 | MACtx = MACtx.subtract(MAInvalidCtx.unite(NHCtx)); |
| 3941 | MACtx = MACtx.gist_params(getContext()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3944 | bool Consolidated = false; |
| 3945 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3946 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3947 | continue; |
| 3948 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3949 | // If the pointer and the type is equal check if the access function wrt. |
| 3950 | // to the domain is equal too. It can happen that the domain fixes |
| 3951 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3952 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3953 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3954 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3955 | if (!MAs.empty()) { |
| 3956 | auto *LastMA = MAs.front(); |
| 3957 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3958 | isl::set AR = MA->getAccessRelation().range(); |
| 3959 | isl::set LastAR = LastMA->getAccessRelation().range(); |
| 3960 | bool SameAR = AR.is_equal(LastAR); |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3961 | |
| 3962 | if (!SameAR) |
| 3963 | continue; |
| 3964 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3965 | |
| 3966 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3967 | MAs.push_front(MA); |
| 3968 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3969 | Consolidated = true; |
| 3970 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3971 | // Unify the execution context of the class and this statement. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3972 | isl::set IAClassDomainCtx = isl::manage(IAClass.ExecutionContext); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3973 | if (IAClassDomainCtx) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3974 | IAClassDomainCtx = IAClassDomainCtx.unite(MACtx).coalesce(); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3975 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3976 | IAClassDomainCtx = MACtx; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3977 | IAClass.ExecutionContext = IAClassDomainCtx.release(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | if (Consolidated) |
| 3982 | continue; |
| 3983 | |
| 3984 | // If we did not consolidate MA, thus did not find an equivalence class |
| 3985 | // for it, we create a new one. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3986 | InvariantEquivClasses.emplace_back(InvariantEquivClassTy{ |
| 3987 | PointerSCEV, MemoryAccessList{MA}, MACtx.release(), Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3988 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 3991 | /// Check if an access range is too complex. |
| 3992 | /// |
| 3993 | /// An access range is too complex, if it contains either many disjuncts or |
| 3994 | /// very complex expressions. As a simple heuristic, we assume if a set to |
| 3995 | /// be too complex if the sum of existentially quantified dimensions and |
| 3996 | /// set dimensions is larger than a threshold. This reliably detects both |
| 3997 | /// sets with many disjuncts as well as sets with many divisions as they |
| 3998 | /// arise in h264. |
| 3999 | /// |
| 4000 | /// @param AccessRange The range to check for complexity. |
| 4001 | /// |
| 4002 | /// @returns True if the access range is too complex. |
| 4003 | static bool isAccessRangeTooComplex(isl::set AccessRange) { |
| 4004 | unsigned NumTotalDims = 0; |
| 4005 | |
| 4006 | auto CountDimensions = [&NumTotalDims](isl::basic_set BSet) -> isl::stat { |
| 4007 | NumTotalDims += BSet.dim(isl::dim::div); |
| 4008 | NumTotalDims += BSet.dim(isl::dim::set); |
| 4009 | return isl::stat::ok; |
| 4010 | }; |
| 4011 | |
| 4012 | AccessRange.foreach_basic_set(CountDimensions); |
| 4013 | |
| 4014 | if (NumTotalDims > MaxDimensionsInAccessRange) |
| 4015 | return true; |
| 4016 | |
| 4017 | return false; |
| 4018 | } |
| 4019 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4020 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4021 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 4022 | // zero iterators, are by construction invariant, though we |
| 4023 | // currently "hoist" them anyway. This is necessary because we allow |
| 4024 | // them to be treated as parameters (e.g., in conditions) and our code |
| 4025 | // generation would otherwise use the old value. |
| 4026 | |
| 4027 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 4028 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4029 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 4030 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 4031 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4032 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4033 | |
| 4034 | // Skip accesses that have an invariant base pointer which is defined but |
| 4035 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 4036 | // returns a pointer that is used as a base address. However, as we want |
| 4037 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 4038 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 4039 | // that has a base pointer origin has a base pointer that is loaded and |
| 4040 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 4041 | // no base pointer origin we check that the base pointer is defined |
| 4042 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4043 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 4044 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4045 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4046 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 4047 | isl::map AccessRelation = give(Access->getAccessRelation().release()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4048 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4049 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4050 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4051 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4052 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4053 | AccessRelation = AccessRelation.intersect_domain(Stmt.getDomain()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4054 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4055 | |
| 4056 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 4057 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 4058 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4059 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4060 | } else if (BB != LI->getParent()) { |
| 4061 | // Skip accesses in non-affine subregions as they might not be executed |
| 4062 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4063 | return nullptr; |
| 4064 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4065 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4066 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4067 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 4068 | if (isAccessRangeTooComplex(AccessRelation.range())) |
| 4069 | return nullptr; |
| 4070 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4071 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 4072 | isl::set WrittenCtx = Written.params(); |
| 4073 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4074 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4075 | if (!IsWritten) |
| 4076 | return WrittenCtx; |
| 4077 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4078 | WrittenCtx = WrittenCtx.remove_divs(); |
| 4079 | bool TooComplex = |
| 4080 | isl_set_n_basic_set(WrittenCtx.get()) >= MaxDisjunctsInDomain; |
| 4081 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4082 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4083 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4084 | addAssumption(INVARIANTLOAD, WrittenCtx.copy(), LI->getDebugLoc(), |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4085 | AS_RESTRICTION, LI->getParent()); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4086 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4089 | void Scop::verifyInvariantLoads() { |
| 4090 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4091 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4092 | assert(LI && contains(LI)); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4093 | ScopStmt *Stmt = getStmtFor(LI); |
Tobias Grosser | 949e8c6 | 2015-12-21 07:10:39 +0000 | [diff] [blame] | 4094 | if (Stmt && Stmt->getArrayAccessOrNULLFor(LI)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4095 | invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4096 | return; |
| 4097 | } |
| 4098 | } |
| 4099 | } |
| 4100 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4101 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4102 | if (!PollyInvariantLoadHoisting) |
| 4103 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4104 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4105 | isl::union_map Writes = getWrites(); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4106 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4107 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4108 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4109 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4110 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
Tobias Grosser | d16f927 | 2017-08-06 17:25:14 +0000 | [diff] [blame] | 4111 | InvariantAccesses.push_back({Access, NHCtx}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4112 | |
| 4113 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 4114 | for (auto InvMA : InvariantAccesses) |
| 4115 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4116 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4117 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4120 | /// Find the canonical scop array info object for a set of invariant load |
| 4121 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 4122 | /// first load in the list of accesses which is used as base pointer of a |
| 4123 | /// scop array. |
| 4124 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 4125 | MemoryAccessList &Accesses) { |
| 4126 | for (MemoryAccess *Access : Accesses) { |
| 4127 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 4128 | Access->getAccessInstruction(), MemoryKind::Array); |
| 4129 | if (CanonicalArray) |
| 4130 | return CanonicalArray; |
| 4131 | } |
| 4132 | return nullptr; |
| 4133 | } |
| 4134 | |
| 4135 | /// Check if @p Array severs as base array in an invariant load. |
| 4136 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 4137 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 4138 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 4139 | if (Access2->getScopArrayInfo() == Array) |
| 4140 | return true; |
| 4141 | return false; |
| 4142 | } |
| 4143 | |
| 4144 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 4145 | /// with a reference to @p New. |
| 4146 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 4147 | const ScopArrayInfo *New) { |
| 4148 | for (ScopStmt &Stmt : *S) |
| 4149 | for (MemoryAccess *Access : Stmt) { |
| 4150 | if (Access->getLatestScopArrayInfo() != Old) |
| 4151 | continue; |
| 4152 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 4153 | isl::id Id = New->getBasePtrId(); |
| 4154 | isl::map Map = Access->getAccessRelation(); |
| 4155 | Map = Map.set_tuple_id(isl::dim::out, Id); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4156 | Access->setAccessRelation(Map); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | void Scop::canonicalizeDynamicBasePtrs() { |
| 4161 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 4162 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 4163 | |
| 4164 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 4165 | findCanonicalArray(this, BasePtrAccesses); |
| 4166 | |
| 4167 | if (!CanonicalBasePtrSAI) |
| 4168 | continue; |
| 4169 | |
| 4170 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 4171 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 4172 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 4173 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 4174 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 4175 | continue; |
| 4176 | |
| 4177 | // we currently do not canonicalize arrays where some accesses are |
| 4178 | // hoisted as invariant loads. If we would, we need to update the access |
| 4179 | // function of the invariant loads as well. However, as this is not a |
| 4180 | // very common situation, we leave this for now to avoid further |
| 4181 | // complexity increases. |
| 4182 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 4183 | continue; |
| 4184 | |
| 4185 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 4186 | } |
| 4187 | } |
| 4188 | } |
| 4189 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4190 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 4191 | ArrayRef<const SCEV *> Sizes, |
| 4192 | MemoryKind Kind, |
| 4193 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4194 | assert((BasePtr || BaseName) && |
| 4195 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 4196 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 4197 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 4198 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4199 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4200 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 4201 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4202 | DL, this, BaseName)); |
| 4203 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4204 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4205 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 4206 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 4207 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4208 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4209 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4210 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4211 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4214 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 4215 | const std::string &BaseName, |
| 4216 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4217 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4218 | std::vector<const SCEV *> SCEVSizes; |
| 4219 | |
| 4220 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4221 | if (size) |
| 4222 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4223 | else |
| 4224 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4225 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4226 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4227 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4228 | return SAI; |
| 4229 | } |
| 4230 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4231 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4232 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4233 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4234 | return SAI; |
| 4235 | } |
| 4236 | |
| 4237 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4238 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4239 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4240 | return SAI; |
| 4241 | } |
| 4242 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4243 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4244 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4245 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4246 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4247 | return stringFromIslObj(AssumedContext); |
| 4248 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4249 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4250 | std::string Scop::getInvalidContextStr() const { |
| 4251 | return stringFromIslObj(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4252 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4253 | |
| 4254 | std::string Scop::getNameStr() const { |
| 4255 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4256 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4257 | return EntryName + "---" + ExitName; |
| 4258 | } |
| 4259 | |
| 4260 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4261 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4262 | raw_string_ostream ExitStr(ExitName); |
| 4263 | raw_string_ostream EntryStr(EntryName); |
| 4264 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4265 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4266 | EntryStr.str(); |
| 4267 | |
| 4268 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4269 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4270 | ExitStr.str(); |
| 4271 | } else |
| 4272 | ExitName = "FunctionExit"; |
| 4273 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4274 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4277 | isl::set Scop::getContext() const { return isl::manage(isl_set_copy(Context)); } |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4278 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4279 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4280 | isl::space Scop::getFullParamSpace() const { |
| 4281 | std::vector<isl::id> FortranIDs; |
| 4282 | FortranIDs = getFortranArrayIds(arrays()); |
| 4283 | |
| 4284 | isl::space Space = isl::space::params_alloc( |
| 4285 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 4286 | |
| 4287 | unsigned PDim = 0; |
| 4288 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 4289 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4290 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4291 | } |
| 4292 | |
| 4293 | for (isl::id Id : FortranIDs) |
| 4294 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4295 | |
| 4296 | return Space; |
| 4297 | } |
| 4298 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4299 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4300 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4301 | return isl::manage(isl_set_copy(AssumedContext)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4304 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4305 | if (PollyProcessUnprofitable) |
| 4306 | return true; |
| 4307 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4308 | if (isEmpty()) |
| 4309 | return false; |
| 4310 | |
| 4311 | unsigned OptimizableStmtsOrLoops = 0; |
| 4312 | for (auto &Stmt : *this) { |
| 4313 | if (Stmt.getNumIterators() == 0) |
| 4314 | continue; |
| 4315 | |
| 4316 | bool ContainsArrayAccs = false; |
| 4317 | bool ContainsScalarAccs = false; |
| 4318 | for (auto *MA : Stmt) { |
| 4319 | if (MA->isRead()) |
| 4320 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4321 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4322 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4325 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4326 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4327 | } |
| 4328 | |
| 4329 | return OptimizableStmtsOrLoops > 1; |
| 4330 | } |
| 4331 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4332 | bool Scop::hasFeasibleRuntimeContext() const { |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4333 | auto *PositiveContext = getAssumedContext().release(); |
Tobias Grosser | 04ec2eb | 2017-08-06 21:42:16 +0000 | [diff] [blame] | 4334 | auto *NegativeContext = getInvalidContext().release(); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 4335 | PositiveContext = |
| 4336 | addNonEmptyDomainConstraints(isl::manage(PositiveContext)).release(); |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4337 | bool IsFeasible = !(isl_set_is_empty(PositiveContext) || |
| 4338 | isl_set_is_subset(PositiveContext, NegativeContext)); |
| 4339 | isl_set_free(PositiveContext); |
| 4340 | if (!IsFeasible) { |
| 4341 | isl_set_free(NegativeContext); |
| 4342 | return false; |
| 4343 | } |
| 4344 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4345 | auto *DomainContext = isl_union_set_params(getDomains().release()); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4346 | IsFeasible = !isl_set_is_subset(DomainContext, NegativeContext); |
Johannes Doerfert | fb72187 | 2016-04-12 17:54:29 +0000 | [diff] [blame] | 4347 | IsFeasible &= !isl_set_is_subset(Context, NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4348 | isl_set_free(NegativeContext); |
| 4349 | isl_set_free(DomainContext); |
| 4350 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4351 | return IsFeasible; |
| 4352 | } |
| 4353 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4354 | static std::string toString(AssumptionKind Kind) { |
| 4355 | switch (Kind) { |
| 4356 | case ALIASING: |
| 4357 | return "No-aliasing"; |
| 4358 | case INBOUNDS: |
| 4359 | return "Inbounds"; |
| 4360 | case WRAPPING: |
| 4361 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4362 | case UNSIGNED: |
| 4363 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4364 | case COMPLEXITY: |
| 4365 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4366 | case PROFITABLE: |
| 4367 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4368 | case ERRORBLOCK: |
| 4369 | return "No-error"; |
| 4370 | case INFINITELOOP: |
| 4371 | return "Finite loop"; |
| 4372 | case INVARIANTLOAD: |
| 4373 | return "Invariant load"; |
| 4374 | case DELINEARIZATION: |
| 4375 | return "Delinearization"; |
| 4376 | } |
| 4377 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4378 | } |
| 4379 | |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4380 | bool Scop::isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign) { |
| 4381 | if (Sign == AS_ASSUMPTION) { |
| 4382 | if (isl_set_is_subset(Context, Set)) |
| 4383 | return false; |
| 4384 | |
| 4385 | if (isl_set_is_subset(AssumedContext, Set)) |
| 4386 | return false; |
| 4387 | } else { |
| 4388 | if (isl_set_is_disjoint(Set, Context)) |
| 4389 | return false; |
| 4390 | |
| 4391 | if (isl_set_is_subset(Set, InvalidContext)) |
| 4392 | return false; |
| 4393 | } |
| 4394 | return true; |
| 4395 | } |
| 4396 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4397 | bool Scop::trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4398 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4399 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4400 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4401 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4402 | // Do never emit trivial assumptions as they only clutter the output. |
| 4403 | if (!PollyRemarksMinimal) { |
| 4404 | isl_set *Univ = nullptr; |
| 4405 | if (Sign == AS_ASSUMPTION) |
| 4406 | Univ = isl_set_universe(isl_set_get_space(Set)); |
| 4407 | |
| 4408 | bool IsTrivial = (Sign == AS_RESTRICTION && isl_set_is_empty(Set)) || |
| 4409 | (Sign == AS_ASSUMPTION && isl_set_is_equal(Univ, Set)); |
| 4410 | isl_set_free(Univ); |
| 4411 | |
| 4412 | if (IsTrivial) |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4416 | switch (Kind) { |
| 4417 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4418 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4419 | break; |
| 4420 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4421 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4422 | break; |
| 4423 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4424 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4425 | break; |
| 4426 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4427 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4428 | break; |
| 4429 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4430 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4431 | break; |
| 4432 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4433 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4434 | break; |
| 4435 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4436 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4437 | break; |
| 4438 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4439 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4440 | break; |
| 4441 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4442 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4443 | break; |
| 4444 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4445 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4446 | break; |
| 4447 | } |
| 4448 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4449 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
| 4450 | std::string Msg = toString(Kind) + Suffix + stringFromIslObj(Set); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4451 | if (BB) |
| 4452 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 4453 | << Msg); |
| 4454 | else |
| 4455 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 4456 | R.getEntry()) |
| 4457 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4458 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | void Scop::addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4462 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4463 | // Simplify the assumptions/restrictions first. |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4464 | Set = isl_set_gist_params(Set, getContext().release()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4465 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4466 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4467 | isl_set_free(Set); |
| 4468 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4471 | if (Sign == AS_ASSUMPTION) { |
| 4472 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
| 4473 | AssumedContext = isl_set_coalesce(AssumedContext); |
| 4474 | } else { |
| 4475 | InvalidContext = isl_set_union(InvalidContext, Set); |
| 4476 | InvalidContext = isl_set_coalesce(InvalidContext); |
| 4477 | } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4480 | void Scop::recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4481 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4482 | assert((isl_set_is_params(Set) || BB) && |
| 4483 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4484 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | void Scop::addRecordedAssumptions() { |
| 4488 | while (!RecordedAssumptions.empty()) { |
| 4489 | const Assumption &AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4490 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4491 | if (!AS.BB) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4492 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign, nullptr /* BasicBlock */); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4493 | continue; |
| 4494 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4495 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4496 | // If the domain was deleted the assumptions are void. |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4497 | isl_set *Dom = getDomainConditions(AS.BB).release(); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4498 | if (!Dom) { |
| 4499 | isl_set_free(AS.Set); |
| 4500 | continue; |
| 4501 | } |
| 4502 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4503 | // If a basic block was given use its domain to simplify the assumption. |
| 4504 | // In case of restrictions we know they only have to hold on the domain, |
| 4505 | // thus we can intersect them with the domain of the block. However, for |
| 4506 | // assumptions the domain has to imply them, thus: |
| 4507 | // _ _____ |
| 4508 | // Dom => S <==> A v B <==> A - B |
| 4509 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4510 | // To avoid the complement we will register A - B as a restriction not an |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4511 | // assumption. |
| 4512 | isl_set *S = AS.Set; |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4513 | if (AS.Sign == AS_RESTRICTION) |
| 4514 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4515 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4516 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4517 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4518 | addAssumption(AS.Kind, S, AS.Loc, AS_RESTRICTION, AS.BB); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4519 | } |
| 4520 | } |
| 4521 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4522 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4523 | addAssumption(Kind, isl_set_empty(getParamSpace().release()), Loc, |
| 4524 | AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Tobias Grosser | 04ec2eb | 2017-08-06 21:42:16 +0000 | [diff] [blame] | 4527 | isl::set Scop::getInvalidContext() const { |
| 4528 | return isl::manage(isl_set_copy(InvalidContext)); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4531 | void Scop::printContext(raw_ostream &OS) const { |
| 4532 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4533 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4534 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4535 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4536 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4537 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4538 | OS.indent(4) << "Invalid Context:\n"; |
| 4539 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4540 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4541 | unsigned Dim = 0; |
| 4542 | for (const SCEV *Parameter : Parameters) |
| 4543 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4546 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4547 | int noOfGroups = 0; |
| 4548 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4549 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4550 | noOfGroups += 1; |
| 4551 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4552 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4555 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4556 | if (MinMaxAliasGroups.empty()) { |
| 4557 | OS.indent(8) << "n/a\n"; |
| 4558 | return; |
| 4559 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4560 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4561 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4562 | |
| 4563 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4564 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4565 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4566 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4567 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4568 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4569 | } |
| 4570 | OS << " ]]\n"; |
| 4571 | } |
| 4572 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4573 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4574 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4575 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4576 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4577 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4578 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4579 | } |
| 4580 | OS << " ]]\n"; |
| 4581 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4585 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4586 | OS << "Statements {\n"; |
| 4587 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4588 | for (const ScopStmt &Stmt : *this) { |
| 4589 | OS.indent(4); |
| 4590 | Stmt.print(OS, PrintInstructions); |
| 4591 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4592 | |
| 4593 | OS.indent(4) << "}\n"; |
| 4594 | } |
| 4595 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4596 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4597 | OS << "Arrays {\n"; |
| 4598 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4599 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4600 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4601 | |
| 4602 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4603 | |
| 4604 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4605 | |
| 4606 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4607 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4608 | |
| 4609 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4612 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4613 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4614 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4615 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4616 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4617 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4618 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4619 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4620 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4621 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4622 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4623 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4624 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4625 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4626 | } |
| 4627 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4628 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4629 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4630 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4631 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4634 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 4635 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4636 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4637 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 4638 | isl_ctx *Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4639 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4640 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4641 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4642 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4643 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4644 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4645 | // the SCoP and return a dummy value. This way we do not need to add error |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4646 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4647 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4648 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4649 | // TODO: We could use a heuristic and either use: |
| 4650 | // SCEVAffinator::takeNonNegativeAssumption |
| 4651 | // or |
| 4652 | // SCEVAffinator::interpretAsUnsigned |
| 4653 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4654 | if (NonNegative) |
| 4655 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4656 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4657 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4658 | |
| 4659 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4660 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4661 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4664 | isl::union_set Scop::getDomains() const { |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4665 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx(), 0); |
| 4666 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4667 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4668 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4669 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4670 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4671 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4674 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4675 | PWACtx PWAC = getPwAff(E, BB); |
| 4676 | isl_set_free(PWAC.second); |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4677 | return isl::manage(PWAC.first); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4680 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4681 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4682 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4683 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4684 | for (ScopStmt &Stmt : *this) { |
| 4685 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4686 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4687 | continue; |
| 4688 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4689 | isl::set Domain = Stmt.getDomain(); |
| 4690 | isl::map AccessDomain = MA->getAccessRelation(); |
| 4691 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 4692 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4693 | } |
| 4694 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 4695 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4696 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4699 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4700 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4703 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4704 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4707 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4708 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4711 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4712 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4715 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4716 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4717 | } |
| 4718 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4719 | // Check whether @p Node is an extension node. |
| 4720 | // |
| 4721 | // @return true if @p Node is an extension node. |
| 4722 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4723 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4724 | return isl_bool_error; |
| 4725 | else |
| 4726 | return isl_bool_true; |
| 4727 | } |
| 4728 | |
| 4729 | bool Scop::containsExtensionNode(__isl_keep isl_schedule *Schedule) { |
| 4730 | return isl_schedule_foreach_schedule_node_top_down(Schedule, isNotExtNode, |
| 4731 | nullptr) == isl_stat_error; |
| 4732 | } |
| 4733 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4734 | isl::union_map Scop::getSchedule() const { |
| 4735 | auto *Tree = getScheduleTree().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4736 | if (containsExtensionNode(Tree)) { |
| 4737 | isl_schedule_free(Tree); |
| 4738 | return nullptr; |
| 4739 | } |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4740 | auto *S = isl_schedule_get_map(Tree); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4741 | isl_schedule_free(Tree); |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4742 | return isl::manage(S); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4743 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4744 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4745 | isl::schedule Scop::getScheduleTree() const { |
| 4746 | return isl::manage(isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 4747 | getDomains().release())); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4748 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4749 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4750 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4751 | auto *S = isl_schedule_from_domain(getDomains().release()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4752 | S = isl_schedule_insert_partial_schedule( |
| 4753 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 4754 | isl_schedule_free(Schedule); |
| 4755 | Schedule = S; |
| 4756 | } |
| 4757 | |
| 4758 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 4759 | isl_schedule_free(Schedule); |
| 4760 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 4764 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4765 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4766 | isl_union_set *StmtDomain = |
| 4767 | isl_union_set_from_set(Stmt.getDomain().release()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4768 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 4769 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 4770 | |
| 4771 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 4772 | isl_union_set_free(StmtDomain); |
| 4773 | isl_union_set_free(NewStmtDomain); |
| 4774 | continue; |
| 4775 | } |
| 4776 | |
| 4777 | Changed = true; |
| 4778 | |
| 4779 | isl_union_set_free(StmtDomain); |
| 4780 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 4781 | |
| 4782 | if (isl_union_set_is_empty(NewStmtDomain)) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4783 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4784 | isl_union_set_free(NewStmtDomain); |
| 4785 | } else |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 4786 | Stmt.restrictDomain(isl::manage(isl_set_from_union_set(NewStmtDomain))); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4787 | } |
| 4788 | isl_union_set_free(Domain); |
| 4789 | return Changed; |
| 4790 | } |
| 4791 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4792 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4793 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4794 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4795 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4796 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4797 | // # Example: |
| 4798 | // |
| 4799 | // Domain: { A[i,j]; B[i,j,k] } |
| 4800 | // N: 1 |
| 4801 | // |
| 4802 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4803 | // |
| 4804 | // @param USet A union set describing the elements for which to generate a |
| 4805 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4806 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4807 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4808 | static isl::multi_union_pw_aff mapToDimension(isl::union_set USet, int N) { |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4809 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4810 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4811 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4812 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4813 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4814 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4815 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4816 | int Dim = S.dim(isl::dim::set); |
| 4817 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4818 | N, Dim - N); |
| 4819 | if (N > 1) |
| 4820 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4821 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4822 | Result = Result.add_pw_multi_aff(PMA); |
| 4823 | return isl::stat::ok; |
| 4824 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4825 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4826 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4827 | (void)Res; |
| 4828 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4829 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4830 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4831 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4834 | void Scop::addScopStmt(BasicBlock *BB, Loop *SurroundingLoop, |
| 4835 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4836 | assert(BB && "Unexpected nullptr!"); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4837 | Stmts.emplace_back(*this, *BB, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4838 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4839 | StmtMap[BB].push_back(Stmt); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4842 | void Scop::addScopStmt(Region *R, Loop *SurroundingLoop) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4843 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4844 | Stmts.emplace_back(*this, *R, SurroundingLoop); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4845 | auto *Stmt = &Stmts.back(); |
| 4846 | for (BasicBlock *BB : R->blocks()) |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4847 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4850 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 4851 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4852 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4853 | isl::set SourceDomain = SourceRel.domain(); |
| 4854 | isl::set TargetDomain = TargetRel.domain(); |
| 4855 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4856 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4857 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4858 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4859 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4860 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4861 | CopyStmtsNum++; |
| 4862 | return &(Stmts.back()); |
| 4863 | } |
| 4864 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4865 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4866 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4867 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4868 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4869 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4870 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4873 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4874 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4875 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4876 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4877 | /// |
| 4878 | /// Example: |
| 4879 | /// bb1(0) |
| 4880 | /// / \. |
| 4881 | /// bb2(1) bb3(2) |
| 4882 | /// \ / \. |
| 4883 | /// bb4(3) bb5(4) |
| 4884 | /// \ / |
| 4885 | /// bb6(5) |
| 4886 | /// |
| 4887 | /// Including loops requires additional processing. Whenever a loop header is |
| 4888 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4889 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4890 | /// this loop and complete the sequential schedule at this loop-level before |
| 4891 | /// processing about any other nodes. To implement this |
| 4892 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4893 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4894 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4895 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4896 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4897 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4898 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4899 | |
| 4900 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4901 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4902 | std::deque<RegionNode *> DelayList; |
| 4903 | bool LastRNWaiting = false; |
| 4904 | |
| 4905 | // Iterate over the region @p R in reverse post-order but queue |
| 4906 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4907 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4908 | // that we queued the last sub-region/block from the reverse post-order |
| 4909 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4910 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4911 | // the next queued sub-region/blocks. |
| 4912 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4913 | RegionNode *RN; |
| 4914 | |
| 4915 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.size() == 0) { |
| 4916 | RN = WorkList.front(); |
| 4917 | WorkList.pop_front(); |
| 4918 | LastRNWaiting = false; |
| 4919 | } else { |
| 4920 | RN = DelayList.front(); |
| 4921 | DelayList.pop_front(); |
| 4922 | } |
| 4923 | |
| 4924 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4925 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4926 | L = OuterScopLoop; |
| 4927 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4928 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4929 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4930 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4931 | LastRNWaiting = true; |
| 4932 | DelayList.push_back(RN); |
| 4933 | continue; |
| 4934 | } |
| 4935 | LoopStack.push_back({L, nullptr, 0}); |
| 4936 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4937 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | return; |
| 4941 | } |
| 4942 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4943 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4944 | |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4945 | if (RN->isSubRegion()) { |
| 4946 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4947 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4948 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4949 | return; |
| 4950 | } |
| 4951 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4952 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4953 | auto &LoopData = LoopStack.back(); |
| 4954 | LoopData.NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4955 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4956 | for (auto *Stmt : getStmtListFor(RN)) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4957 | auto *UDomain = isl_union_set_from_set(Stmt->getDomain().release()); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4958 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4959 | LoopData.Schedule = combineInSequence(LoopData.Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4962 | // Check if we just processed the last node in this loop. If we did, finalize |
| 4963 | // the loop by: |
| 4964 | // |
| 4965 | // - adding new schedule dimensions |
| 4966 | // - folding the resulting schedule into the parent loop schedule |
| 4967 | // - dropping the loop schedule from the LoopStack. |
| 4968 | // |
| 4969 | // Then continue to check surrounding loops, which might also have been |
| 4970 | // completed by this node. |
| 4971 | while (LoopData.L && |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 4972 | LoopData.NumBlocksProcessed == getNumBlocksInLoop(LoopData.L)) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4973 | auto *Schedule = LoopData.Schedule; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4974 | auto NumBlocksProcessed = LoopData.NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4975 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4976 | LoopStack.pop_back(); |
| 4977 | auto &NextLoopData = LoopStack.back(); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4978 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4979 | if (Schedule) { |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4980 | isl::union_set Domain = give(isl_schedule_get_domain(Schedule)); |
| 4981 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, LoopStack.size()); |
| 4982 | Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA.release()); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4983 | NextLoopData.Schedule = |
| 4984 | combineInSequence(NextLoopData.Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4985 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4986 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4987 | NextLoopData.NumBlocksProcessed += NumBlocksProcessed; |
| 4988 | LoopData = NextLoopData; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4989 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4992 | ScopStmt *Scop::getStmtFor(BasicBlock *BB) const { |
Tobias Grosser | 57411e3 | 2015-05-27 06:51:34 +0000 | [diff] [blame] | 4993 | auto StmtMapIt = StmtMap.find(BB); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 4994 | if (StmtMapIt == StmtMap.end()) |
| 4995 | return nullptr; |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4996 | assert(StmtMapIt->second.size() == 1); |
| 4997 | return StmtMapIt->second.front(); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 5000 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 5001 | auto StmtMapIt = StmtMap.find(BB); |
| 5002 | if (StmtMapIt == StmtMap.end()) |
| 5003 | return {}; |
| 5004 | assert(StmtMapIt->second.size() == 1 && |
| 5005 | "Each statement corresponds to exactly one BB."); |
| 5006 | return StmtMapIt->second; |
| 5007 | } |
| 5008 | |
| 5009 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 5010 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
| 5011 | if (StmtList.size() > 0) |
| 5012 | return StmtList.back(); |
| 5013 | return nullptr; |
| 5014 | } |
| 5015 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5016 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5017 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5018 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 5019 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5022 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 5023 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5026 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5027 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5028 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5029 | // outermostLoopInRegion always returns nullptr for top level regions |
| 5030 | if (R.isTopLevelRegion()) { |
| 5031 | // LoopInfo's depths start at 1, we start at 0 |
| 5032 | return L->getLoopDepth() - 1; |
| 5033 | } else { |
| 5034 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 5035 | assert(OuterLoop); |
| 5036 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 5037 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 5040 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 5041 | for (auto &SAI : arrays()) { |
| 5042 | if (SAI->getName() == BaseName) |
| 5043 | return SAI; |
| 5044 | } |
| 5045 | return nullptr; |
| 5046 | } |
| 5047 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 5048 | void Scop::addAccessData(MemoryAccess *Access) { |
| 5049 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 5050 | assert(SAI && "can only use after access relations have been constructed"); |
| 5051 | |
| 5052 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 5053 | ValueUseAccs[SAI].push_back(Access); |
| 5054 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 5055 | PHIIncomingAccs[SAI].push_back(Access); |
| 5056 | } |
| 5057 | |
| 5058 | void Scop::removeAccessData(MemoryAccess *Access) { |
| 5059 | if (Access->isOriginalValueKind() && Access->isRead()) { |
| 5060 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
| 5061 | std::remove(Uses.begin(), Uses.end(), Access); |
| 5062 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 5063 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
| 5064 | std::remove(Incomings.begin(), Incomings.end(), Access); |
| 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 5069 | assert(SAI->isValueKind()); |
| 5070 | |
| 5071 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 5072 | if (!Val) |
| 5073 | return nullptr; |
| 5074 | |
| 5075 | ScopStmt *Stmt = getStmtFor(Val); |
| 5076 | if (!Stmt) |
| 5077 | return nullptr; |
| 5078 | |
| 5079 | return Stmt->lookupValueWriteOf(Val); |
| 5080 | } |
| 5081 | |
| 5082 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 5083 | assert(SAI->isValueKind()); |
| 5084 | auto It = ValueUseAccs.find(SAI); |
| 5085 | if (It == ValueUseAccs.end()) |
| 5086 | return {}; |
| 5087 | return It->second; |
| 5088 | } |
| 5089 | |
| 5090 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 5091 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5092 | |
| 5093 | if (SAI->isExitPHIKind()) |
| 5094 | return nullptr; |
| 5095 | |
| 5096 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
| 5097 | ScopStmt *Stmt = getStmtFor(PHI); |
| 5098 | assert(Stmt && "PHINode must be within the SCoP"); |
| 5099 | |
| 5100 | return Stmt->lookupPHIReadOf(PHI); |
| 5101 | } |
| 5102 | |
| 5103 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 5104 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5105 | auto It = PHIIncomingAccs.find(SAI); |
| 5106 | if (It == PHIIncomingAccs.end()) |
| 5107 | return {}; |
| 5108 | return It->second; |
| 5109 | } |
| 5110 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 5111 | bool Scop::isEscaping(Instruction *Inst) { |
| 5112 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 5113 | "values defined inside the SCoP"); |
| 5114 | |
| 5115 | for (Use &Use : Inst->uses()) { |
| 5116 | BasicBlock *UserBB = getUseBlock(Use); |
| 5117 | if (!contains(UserBB)) |
| 5118 | return true; |
| 5119 | |
| 5120 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 5121 | // move to a new basic block such that its incoming blocks are not in the |
| 5122 | // SCoP anymore. |
| 5123 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 5124 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 5125 | return true; |
| 5126 | } |
| 5127 | return false; |
| 5128 | } |
| 5129 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5130 | raw_ostream &polly::operator<<(raw_ostream &O, const Scop &scop) { |
| 5131 | scop.print(O, PollyPrintInstructions); |
| 5132 | return O; |
| 5133 | } |
| 5134 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5135 | //===----------------------------------------------------------------------===// |
| 5136 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5137 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5138 | AU.addRequired<RegionInfoPass>(); |
| 5139 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5140 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5141 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5142 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5143 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5144 | AU.setPreservesAll(); |
| 5145 | } |
| 5146 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5147 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats) { |
| 5148 | NumLoopsInScop += Stats.NumLoops; |
| 5149 | MaxNumLoopsInScop = |
| 5150 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 5151 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5152 | if (Stats.MaxDepth == 1) |
| 5153 | NumScopsDepthOne++; |
| 5154 | else if (Stats.MaxDepth == 2) |
| 5155 | NumScopsDepthTwo++; |
| 5156 | else if (Stats.MaxDepth == 3) |
| 5157 | NumScopsDepthThree++; |
| 5158 | else if (Stats.MaxDepth == 4) |
| 5159 | NumScopsDepthFour++; |
| 5160 | else if (Stats.MaxDepth == 5) |
| 5161 | NumScopsDepthFive++; |
| 5162 | else |
| 5163 | NumScopsDepthLarger++; |
| 5164 | } |
| 5165 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5166 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5167 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5168 | |
| 5169 | if (!SD.isMaxRegionInScop(*R)) |
| 5170 | return false; |
| 5171 | |
| 5172 | Function *F = R->getEntry()->getParent(); |
| 5173 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5174 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5175 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5176 | auto const &DL = F->getParent()->getDataLayout(); |
| 5177 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5178 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5179 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5180 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5181 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5182 | |
| 5183 | if (S) { |
| 5184 | ScopDetection::LoopStats Stats = |
| 5185 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5186 | updateLoopCountStatistic(Stats); |
| 5187 | } |
| 5188 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5189 | return false; |
| 5190 | } |
| 5191 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5192 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5193 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5194 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5195 | else |
| 5196 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5197 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5198 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5199 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5200 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5201 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5202 | |
| 5203 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5204 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5205 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5206 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5207 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5208 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5209 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5210 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5211 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5212 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5213 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5214 | "Polly - Create polyhedral description of Scops", false, |
| 5215 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5216 | |
| 5217 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5218 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5219 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
| 5220 | AssumptionCache &AC) { |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5221 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5222 | /// function. |
| 5223 | for (auto &It : SD) { |
| 5224 | Region *R = const_cast<Region *>(It); |
| 5225 | if (!SD.isMaxRegionInScop(*R)) |
| 5226 | continue; |
| 5227 | |
| 5228 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
| 5229 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5230 | if (!S) |
| 5231 | continue; |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 5232 | ScopDetection::LoopStats Stats = |
| 5233 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5234 | updateLoopCountStatistic(Stats); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5235 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5236 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5237 | (void)Inserted; |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | AnalysisKey ScopInfoAnalysis::Key; |
| 5242 | |
| 5243 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5244 | FunctionAnalysisManager &FAM) { |
| 5245 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5246 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5247 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5248 | auto &AA = FAM.getResult<AAManager>(F); |
| 5249 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5250 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5251 | auto &DL = F.getParent()->getDataLayout(); |
| 5252 | return {DL, SD, SE, LI, AA, DT, AC}; |
| 5253 | } |
| 5254 | |
| 5255 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5256 | FunctionAnalysisManager &FAM) { |
| 5257 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 5258 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 5259 | // order here to keep the output persistent |
| 5260 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5261 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5262 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5263 | else |
| 5264 | Stream << "Invalid Scop!\n"; |
| 5265 | } |
| 5266 | return PreservedAnalyses::all(); |
| 5267 | } |
| 5268 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5269 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5270 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5271 | AU.addRequired<RegionInfoPass>(); |
| 5272 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5273 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5274 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5275 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5276 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5277 | AU.setPreservesAll(); |
| 5278 | } |
| 5279 | |
| 5280 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5281 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5282 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5283 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5284 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5285 | auto const &DL = F.getParent()->getDataLayout(); |
| 5286 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5287 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5288 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5289 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5290 | return false; |
| 5291 | } |
| 5292 | |
| 5293 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5294 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5295 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5296 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5297 | else |
| 5298 | OS << "Invalid Scop!\n"; |
| 5299 | } |
| 5300 | } |
| 5301 | |
| 5302 | char ScopInfoWrapperPass::ID = 0; |
| 5303 | |
| 5304 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5305 | return new ScopInfoWrapperPass(); |
| 5306 | } |
| 5307 | |
| 5308 | INITIALIZE_PASS_BEGIN( |
| 5309 | ScopInfoWrapperPass, "polly-function-scops", |
| 5310 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5311 | false); |
| 5312 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5313 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5314 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5315 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5316 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5317 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5318 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5319 | INITIALIZE_PASS_END( |
| 5320 | ScopInfoWrapperPass, "polly-function-scops", |
| 5321 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5322 | false) |