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 | 49ad36c | 2015-05-20 08:05:31 +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 | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 293 | isl::union_set WriteSet = give(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"; |
| 349 | // TODO: see if we need to add `this` as the id user pointer |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 350 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name.c_str(), nullptr); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 351 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 352 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 353 | isl::pw_aff PwAff = |
| 354 | 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] | 355 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 356 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 359 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 360 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 361 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 362 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 363 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 364 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 365 | if (CheckConsistency) { |
| 366 | for (int i = 0; i < SharedDims; i++) { |
| 367 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 368 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 369 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 370 | return false; |
| 371 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 372 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 373 | if (DimensionSizes.size() >= NewSizes.size()) |
| 374 | return true; |
| 375 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 376 | |
| 377 | DimensionSizes.clear(); |
| 378 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 379 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 380 | DimensionSizesPw.clear(); |
| 381 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 382 | if (!Expr) { |
| 383 | DimensionSizesPw.push_back(nullptr); |
| 384 | continue; |
| 385 | } |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 386 | isl::pw_aff Size = isl::manage(S.getPwAffOnly(Expr)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 387 | DimensionSizesPw.push_back(Size); |
| 388 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 389 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 392 | ScopArrayInfo::~ScopArrayInfo() {} |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 393 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 394 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 395 | |
| 396 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 397 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 400 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 401 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 402 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 403 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 404 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 405 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 406 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 407 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 408 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 409 | // If this is a Fortran array, then we can print the outermost dimension |
| 410 | // as a isl_pw_aff even though there is no SCEV information. |
| 411 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 412 | |
| 413 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 414 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 415 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 416 | u++; |
| 417 | } |
| 418 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 419 | OS << "["; |
| 420 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 421 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 422 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 423 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 424 | } else { |
| 425 | OS << *getDimensionSize(u); |
| 426 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 427 | |
| 428 | OS << "]"; |
| 429 | } |
| 430 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 431 | OS << ";"; |
| 432 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 433 | if (BasePtrOriginSAI) |
| 434 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 435 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 436 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 440 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 441 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 442 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 443 | return getFromId(Id); |
| 444 | } |
| 445 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 446 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 447 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 448 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 449 | return SAI; |
| 450 | } |
| 451 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 452 | void MemoryAccess::wrapConstantDimensions() { |
| 453 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 454 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 455 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 456 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 457 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 458 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 459 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 460 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 461 | |
| 462 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 463 | for (int i = DimsArray - 1; i > 0; i--) { |
| 464 | auto *DimSize = SAI->getDimensionSize(i); |
| 465 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 466 | |
| 467 | // This transformation is not applicable to dimensions with dynamic size. |
| 468 | if (!DimSizeCst) |
| 469 | continue; |
| 470 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 471 | // This transformation is not applicable to dimensions of size zero. |
| 472 | if (DimSize->isZero()) |
| 473 | continue; |
| 474 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 475 | isl::val DimSizeVal = |
| 476 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 477 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 478 | isl::aff PrevVar = |
| 479 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 480 | |
| 481 | // Compute: index % size |
| 482 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 483 | isl::aff Modulo = Var.mod_val(DimSizeVal); |
| 484 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 485 | |
| 486 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 487 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 488 | Divide = Divide.floor(); |
| 489 | Divide = Divide.add(PrevVar); |
| 490 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 491 | |
| 492 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 493 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 494 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 498 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 499 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 500 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 501 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 504 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 505 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 506 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 507 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 508 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 509 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 510 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 511 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 512 | auto DimsMissing = DimsArray - DimsAccess; |
| 513 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 514 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 515 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 516 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 517 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 518 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 519 | isl::map Map = isl::map::from_domain_and_range( |
| 520 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 521 | |
| 522 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 523 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 524 | |
| 525 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 526 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 527 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 528 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 529 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 530 | // For the non delinearized arrays, divide the access function of the last |
| 531 | // subscript by the size of the elements in the array. |
| 532 | // |
| 533 | // A stride one array access in C expressed as A[i] is expressed in |
| 534 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 535 | // two subsequent values of 'i' index two values that are stored next to |
| 536 | // each other in memory. By this division we make this characteristic |
| 537 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 538 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 539 | // that divides the offsets of all accesses to this base pointer. |
| 540 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 541 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 542 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 545 | // We currently do this only if we added at least one dimension, which means |
| 546 | // some dimension's indices have not been specified, an indicator that some |
| 547 | // index values have been added together. |
| 548 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 549 | // expressions more complicated. |
| 550 | if (DimsMissing) |
| 551 | wrapConstantDimensions(); |
| 552 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 553 | if (!isAffine()) |
| 554 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 555 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 556 | // Introduce multi-element accesses in case the type loaded by this memory |
| 557 | // access is larger than the canonical element type of the array. |
| 558 | // |
| 559 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 560 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 561 | if (ElemBytes > ArrayElemSize) { |
| 562 | assert(ElemBytes % ArrayElemSize == 0 && |
| 563 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 564 | isl::map Map = isl::map::from_domain_and_range( |
| 565 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 566 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 567 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 568 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 569 | isl::constraint C; |
| 570 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 571 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 572 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 573 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 574 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 575 | C = isl::constraint::alloc_inequality(LS); |
| 576 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 577 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 578 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 579 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 580 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 581 | C = isl::constraint::alloc_inequality(LS); |
| 582 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 583 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 584 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 585 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 586 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 587 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 590 | const std::string |
| 591 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 592 | switch (RT) { |
| 593 | case MemoryAccess::RT_NONE: |
| 594 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 595 | "access which isn't a reduction"); |
| 596 | case MemoryAccess::RT_ADD: |
| 597 | return "+"; |
| 598 | case MemoryAccess::RT_MUL: |
| 599 | return "*"; |
| 600 | case MemoryAccess::RT_BOR: |
| 601 | return "|"; |
| 602 | case MemoryAccess::RT_BXOR: |
| 603 | return "^"; |
| 604 | case MemoryAccess::RT_BAND: |
| 605 | return "&"; |
| 606 | } |
| 607 | llvm_unreachable("Unknown reduction type"); |
| 608 | return ""; |
| 609 | } |
| 610 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 611 | /// Return the reduction type for a given binary operator. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 612 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 613 | const Instruction *Load) { |
| 614 | if (!BinOp) |
| 615 | return MemoryAccess::RT_NONE; |
| 616 | switch (BinOp->getOpcode()) { |
| 617 | case Instruction::FAdd: |
| 618 | if (!BinOp->hasUnsafeAlgebra()) |
| 619 | return MemoryAccess::RT_NONE; |
| 620 | // Fall through |
| 621 | case Instruction::Add: |
| 622 | return MemoryAccess::RT_ADD; |
| 623 | case Instruction::Or: |
| 624 | return MemoryAccess::RT_BOR; |
| 625 | case Instruction::Xor: |
| 626 | return MemoryAccess::RT_BXOR; |
| 627 | case Instruction::And: |
| 628 | return MemoryAccess::RT_BAND; |
| 629 | case Instruction::FMul: |
| 630 | if (!BinOp->hasUnsafeAlgebra()) |
| 631 | return MemoryAccess::RT_NONE; |
| 632 | // Fall through |
| 633 | case Instruction::Mul: |
| 634 | if (DisableMultiplicativeReductions) |
| 635 | return MemoryAccess::RT_NONE; |
| 636 | return MemoryAccess::RT_MUL; |
| 637 | default: |
| 638 | return MemoryAccess::RT_NONE; |
| 639 | } |
| 640 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 641 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 642 | MemoryAccess::~MemoryAccess() {} |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 643 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 644 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 645 | isl::id ArrayId = getArrayId(); |
| 646 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 647 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 648 | return SAI; |
| 649 | } |
| 650 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 651 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 652 | isl::id ArrayId = getLatestArrayId(); |
| 653 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 654 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 655 | return SAI; |
| 656 | } |
| 657 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 658 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 659 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 662 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 663 | if (!hasNewAccessRelation()) |
| 664 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 665 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 668 | isl::map MemoryAccess::getAddressFunction() const { |
| 669 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 672 | isl::pw_multi_aff |
| 673 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 674 | isl::map Schedule, ScheduledAccRel; |
| 675 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 676 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 677 | UDomain = isl::manage(getStatement()->getDomain()); |
| 678 | USchedule = USchedule.intersect_domain(UDomain); |
| 679 | Schedule = isl::map::from_union_map(USchedule); |
| 680 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 681 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 684 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 685 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 688 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 689 | return stringFromIslObj(AccessRelation.get()); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 692 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 693 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 696 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 697 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 700 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 701 | return stringFromIslObj(NewAccessRelation.get()); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 704 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 705 | return isl::manage(getAccessRelation().get()).to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 708 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 709 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
| 710 | Space = Space.align_params(isl::manage(Statement->getDomainSpace())); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 711 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 712 | return isl::basic_map::from_domain_and_range( |
| 713 | isl::basic_set::universe(isl::manage(Statement->getDomainSpace())), |
| 714 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 717 | // Formalize no out-of-bound access assumption |
| 718 | // |
| 719 | // When delinearizing array accesses we optimistically assume that the |
| 720 | // delinearized accesses do not access out of bound locations (the subscript |
| 721 | // expression of each array evaluates for each statement instance that is |
| 722 | // executed to a value that is larger than zero and strictly smaller than the |
| 723 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 724 | // dimension for which we do not need to assume any upper bound. At this point |
| 725 | // we formalize this assumption to ensure that at code generation time the |
| 726 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 727 | // |
| 728 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 729 | // first build the set of data locations that are not within array bounds. We |
| 730 | // then apply the reverse access relation to obtain the set of iterations that |
| 731 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 732 | // that are actually executed by intersecting them with the domain of the |
| 733 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 734 | // parameters that may cause statement instances to be executed that may |
| 735 | // possibly yield out of bound memory accesses. The complement of these |
| 736 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 737 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 738 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 739 | if (PollyIgnoreInbounds) |
| 740 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 741 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 742 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 743 | isl::set Outside = isl::set::empty(Space); |
| 744 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 745 | isl::local_space LS(Space); |
| 746 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 747 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 748 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 749 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 750 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 751 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 752 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 753 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 754 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 755 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 758 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 759 | Outside = Outside.intersect(give(Statement->getDomain())); |
| 760 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 761 | |
| 762 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 763 | // Doing so increases the set of parameter combinations that are assumed to |
| 764 | // not appear. This is always save, but may make the resulting run-time check |
| 765 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 766 | Outside = Outside.remove_divs(); |
| 767 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 768 | const auto &Loc = getAccessInstruction() |
| 769 | ? getAccessInstruction()->getDebugLoc() |
| 770 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 771 | if (!PollyPreciseInbounds) |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 772 | Outside = Outside.gist_params(give(Statement->getDomain()).params()); |
| 773 | Statement->getParent()->recordAssumption(INBOUNDS, Outside.release(), Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 774 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 777 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 778 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 779 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 780 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 781 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 782 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 783 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 784 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 785 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 786 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 787 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 788 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 789 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 790 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 791 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 792 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 793 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 794 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 795 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 796 | LengthMap = LengthMap.sum(SubscriptMap); |
| 797 | AccessRelation = |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 798 | LengthMap.set_tuple_id(isl::dim::in, give(getStatement()->getDomainId())); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 801 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 802 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 803 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 804 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 805 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 806 | return; |
| 807 | |
| 808 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 809 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 810 | return; |
| 811 | |
| 812 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 813 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 814 | return; |
| 815 | |
| 816 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 817 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 818 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 819 | |
| 820 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 821 | if (Range.isFullSet()) |
| 822 | return; |
| 823 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 824 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 825 | return; |
| 826 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 827 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 828 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 829 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 830 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 831 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 832 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 833 | |
| 834 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 835 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 836 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 837 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 838 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 839 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 840 | isl::set AccessRange = Relation.range(); |
| 841 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 842 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 843 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 846 | void MemoryAccess::foldAccessRelation() { |
| 847 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 848 | return; |
| 849 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 850 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 851 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 852 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 853 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 854 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 855 | isl::space Space; |
| 856 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 857 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 858 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 859 | isl::space SpaceSize = DimSize.get_space(); |
| 860 | isl::id ParamId = |
| 861 | give(isl_space_get_dim_id(SpaceSize.get(), isl_dim_param, 0)); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 862 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 863 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 864 | Space = Space.range().map_from_set(); |
| 865 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 866 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 867 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 868 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 869 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 870 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 871 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 872 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 873 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 874 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 875 | for (int j = 0; j < Size; ++j) |
| 876 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 877 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 878 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 879 | isl::local_space LS(Space); |
| 880 | isl::constraint C; |
| 881 | C = isl::constraint::alloc_equality(LS); |
| 882 | C = C.set_constant_si(-1); |
| 883 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 884 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 885 | MapTwo = MapTwo.add_constraint(C); |
| 886 | C = isl::constraint::alloc_equality(LS); |
| 887 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 888 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 889 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 890 | MapTwo = MapTwo.add_constraint(C); |
| 891 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 892 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 893 | MapOne = MapOne.unite(MapTwo); |
| 894 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 895 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 896 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 897 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 898 | isl::space Space = give(Statement->getDomainSpace()); |
| 899 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 900 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 901 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
| 902 | NewAccessRelation = |
| 903 | NewAccessRelation.gist_domain(give(Statement->getDomain())); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 904 | |
| 905 | // Access dimension folding might in certain cases increase the number of |
| 906 | // disjuncts in the memory access, which can possibly complicate the generated |
| 907 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 908 | if (!PollyPreciseFoldAccesses && |
| 909 | isl_map_n_basic_map(NewAccessRelation.get()) > |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 910 | isl_map_n_basic_map(AccessRelation.get())) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 911 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 912 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 913 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 916 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 917 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 918 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 919 | if (Size == 1) |
| 920 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 921 | |
| 922 | // Only one factor needs to be divisible. |
| 923 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 924 | for (auto *FactorExpr : MulExpr->operands()) |
| 925 | if (isDivisible(FactorExpr, Size, SE)) |
| 926 | return true; |
| 927 | return false; |
| 928 | } |
| 929 | |
| 930 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 931 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 932 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 933 | for (auto *OpExpr : NAryExpr->operands()) |
| 934 | if (!isDivisible(OpExpr, Size, SE)) |
| 935 | return false; |
| 936 | return true; |
| 937 | } |
| 938 | |
| 939 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 940 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 941 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 942 | return MulSCEV == Expr; |
| 943 | } |
| 944 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 945 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 946 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 947 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 948 | // Initialize the invalid domain which describes all iterations for which the |
| 949 | // access relation is not modeled correctly. |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 950 | isl::set StmtInvalidDomain = isl::manage(getStatement()->getInvalidDomain()); |
| 951 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 952 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 953 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 954 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 955 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 956 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 957 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 958 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 959 | return; |
| 960 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 961 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 962 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 963 | // We overapproximate non-affine accesses with a possible access to the |
| 964 | // whole array. For read accesses it does not make a difference, if an |
| 965 | // access must or may happen. However, for write accesses it is important to |
| 966 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 967 | if (AccessRelation.is_null()) |
| 968 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 969 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 970 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 974 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 975 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 976 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 977 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 978 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 979 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 980 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 983 | Space = isl::manage(Statement->getDomainSpace()); |
| 984 | AccessRelation = AccessRelation.set_tuple_id( |
| 985 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 986 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 987 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 988 | AccessRelation = |
| 989 | AccessRelation.gist_domain(isl::manage(Statement->getDomain())); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 990 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 991 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 992 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 993 | AccessType AccType, Value *BaseAddress, |
| 994 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 995 | ArrayRef<const SCEV *> Subscripts, |
| 996 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 997 | MemoryKind Kind) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 998 | : Kind(Kind), AccType(AccType), RedType(RT_NONE), Statement(Stmt), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 999 | InvalidDomain(nullptr), BaseAddr(BaseAddress), ElementType(ElementType), |
| 1000 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 1001 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1002 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1003 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1004 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1005 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1006 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1007 | std::string IdName = Stmt->getBaseName() + Access; |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1008 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1009 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1010 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1011 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1012 | : Kind(MemoryKind::Array), AccType(AccType), RedType(RT_NONE), |
| 1013 | Statement(Stmt), InvalidDomain(nullptr), AccessInstruction(nullptr), |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1014 | IsAffine(true), AccessRelation(nullptr), NewAccessRelation(AccRel), |
| 1015 | FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 1016 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1017 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1018 | Sizes.push_back(nullptr); |
| 1019 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1020 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1021 | ElementType = SAI->getElementType(); |
| 1022 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1023 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1024 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1025 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1026 | std::string IdName = Stmt->getBaseName() + Access; |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1027 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1030 | void MemoryAccess::realignParams() { |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1031 | isl::set Ctx = isl::manage(Statement->getParent()->getContext()); |
| 1032 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 1033 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1036 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1037 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1038 | } |
| 1039 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1040 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1041 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1042 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1043 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1044 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1045 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1046 | else |
| 1047 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1048 | return OS; |
| 1049 | } |
| 1050 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1051 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1052 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1053 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1054 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1055 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1056 | OS.indent(12) << "ReadAccess :=\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 MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1059 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1060 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1061 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1062 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1065 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1066 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (FAD) { |
| 1069 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1070 | OS << "] "; |
| 1071 | }; |
| 1072 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1073 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1074 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1075 | if (hasNewAccessRelation()) |
| 1076 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1079 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1080 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1081 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1082 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1083 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1084 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1085 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1086 | isl::set StmtDom = isl::manage(getStatement()->getDomain()); |
| 1087 | StmtDom = StmtDom.reset_tuple_id(); |
| 1088 | isl::set NewInvalidDom = StmtDom.intersect(isl::manage(PWAC.second)); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1089 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1090 | return isl::manage(PWAC.first); |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1093 | // Create a map in the size of the provided set domain, that maps from the |
| 1094 | // one element of the provided set domain to another element of the provided |
| 1095 | // set domain. |
| 1096 | // The mapping is limited to all points that are equal in all but the last |
| 1097 | // dimension and for which the last dimension of the input is strict smaller |
| 1098 | // than the last dimension of the output. |
| 1099 | // |
| 1100 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1101 | // |
| 1102 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1103 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1104 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1105 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1106 | isl::space Space = SetDomain.map_from_set(); |
| 1107 | isl::map Map = isl::map::universe(Space); |
| 1108 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Set all but the last dimension to be equal for the input and output |
| 1111 | // |
| 1112 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1113 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1114 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1115 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Set the last dimension of the input to be strict smaller than the |
| 1118 | // last dimension of the output. |
| 1119 | // |
| 1120 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1121 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1122 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1125 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1126 | isl::map AccessRelation = getAccessRelation(); |
| 1127 | isl::space Space = Schedule.get_space().range(); |
| 1128 | isl::map NextScatt = getEqualAndLarger(Space); |
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 | Schedule = Schedule.reverse(); |
| 1131 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1132 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1133 | NextScatt = NextScatt.apply_range(Schedule); |
| 1134 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1135 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1136 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1137 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1138 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1139 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1142 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1143 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1144 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1145 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1146 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1147 | StrideX = isl::set::universe(Stride.get_space()); |
| 1148 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1149 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1150 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1151 | StrideWidth); |
| 1152 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1153 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1154 | return IsStrideX; |
| 1155 | } |
| 1156 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1157 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1158 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1161 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1162 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1165 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1166 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1169 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1170 | assert(NewAccess); |
| 1171 | |
| 1172 | #ifndef NDEBUG |
| 1173 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1174 | isl::space NewSpace = NewAccess.get_space(); |
| 1175 | isl::space NewDomainSpace = NewSpace.domain(); |
| 1176 | isl::space OriginalDomainSpace = |
| 1177 | isl::manage(getStatement()->getDomainSpace()); |
| 1178 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1179 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1180 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1181 | // subdomain only. |
| 1182 | if (isRead()) { |
| 1183 | // Check whether there is an access for every statement instance. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1184 | isl::set StmtDomain = isl::manage(getStatement()->getDomain()); |
| 1185 | StmtDomain = StmtDomain.intersect_params( |
| 1186 | isl::manage(getStatement()->getParent()->getContext())); |
| 1187 | isl::set NewDomain = NewAccess.domain(); |
| 1188 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1189 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1190 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1191 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1192 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1193 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1194 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1195 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1196 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1197 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1200 | InvariantEquivClassTy *EqClass = |
| 1201 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1202 | SAI->getBasePtr()); |
| 1203 | assert(EqClass && |
| 1204 | "Access functions to indirect arrays must have an invariant and " |
| 1205 | "hoisted base pointer"); |
| 1206 | } |
| 1207 | |
| 1208 | // Check whether access dimensions correspond to number of dimensions of the |
| 1209 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1210 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1211 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1212 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1213 | #endif |
| 1214 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame^] | 1215 | NewAccess = NewAccess.gist_domain(isl::manage(getStatement()->getDomain())); |
| 1216 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1217 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1218 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1219 | bool MemoryAccess::isLatestPartialAccess() const { |
| 1220 | isl::set StmtDom = give(getStatement()->getDomain()); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1221 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1222 | |
| 1223 | return isl_set_is_subset(StmtDom.keep(), AccDom.keep()) == isl_bool_false; |
| 1224 | } |
| 1225 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1226 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1227 | |
Johannes Doerfert | 3c6a99b | 2016-04-09 21:55:23 +0000 | [diff] [blame] | 1228 | __isl_give isl_map *ScopStmt::getSchedule() const { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1229 | isl_set *Domain = getDomain(); |
| 1230 | if (isl_set_is_empty(Domain)) { |
| 1231 | isl_set_free(Domain); |
| 1232 | return isl_map_from_aff( |
| 1233 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 1234 | } |
| 1235 | auto *Schedule = getParent()->getSchedule(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1236 | if (!Schedule) { |
| 1237 | isl_set_free(Domain); |
| 1238 | return nullptr; |
| 1239 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1240 | Schedule = isl_union_map_intersect_domain( |
| 1241 | Schedule, isl_union_set_from_set(isl_set_copy(Domain))); |
| 1242 | if (isl_union_map_is_empty(Schedule)) { |
| 1243 | isl_set_free(Domain); |
| 1244 | isl_union_map_free(Schedule); |
| 1245 | return isl_map_from_aff( |
| 1246 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 1247 | } |
| 1248 | auto *M = isl_map_from_union_map(Schedule); |
| 1249 | M = isl_map_coalesce(M); |
| 1250 | M = isl_map_gist_domain(M, Domain); |
| 1251 | M = isl_map_coalesce(M); |
| 1252 | return M; |
| 1253 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1254 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1255 | void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) { |
| 1256 | assert(isl_set_is_subset(NewDomain, Domain) && |
| 1257 | "New domain is not a subset of old domain!"); |
| 1258 | isl_set_free(Domain); |
| 1259 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1262 | void ScopStmt::buildAccessRelations() { |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 1263 | Scop &S = *getParent(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1264 | for (MemoryAccess *Access : MemAccs) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1265 | Type *ElementType = Access->getElementType(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1266 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1267 | MemoryKind Ty; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1268 | if (Access->isPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1269 | Ty = MemoryKind::PHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1270 | else if (Access->isExitPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1271 | Ty = MemoryKind::ExitPHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1272 | else if (Access->isValueKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1273 | Ty = MemoryKind::Value; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1274 | else |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1275 | Ty = MemoryKind::Array; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1276 | |
Tobias Grosser | 296fe2e | 2017-02-10 10:09:46 +0000 | [diff] [blame] | 1277 | auto *SAI = S.getOrCreateScopArrayInfo(Access->getOriginalBaseAddr(), |
| 1278 | ElementType, Access->Sizes, Ty); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1279 | Access->buildAccessRelation(SAI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1280 | S.addAccessData(Access); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1284 | void ScopStmt::addAccess(MemoryAccess *Access) { |
| 1285 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1286 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1287 | if (Access->isArrayKind()) { |
| 1288 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1289 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1290 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1291 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 1292 | assert(Parent.getStmtFor(AccessVal) == this); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1293 | assert(!ValueWrites.lookup(AccessVal)); |
| 1294 | |
| 1295 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1296 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1297 | Value *AccessVal = Access->getAccessValue(); |
| 1298 | assert(!ValueReads.lookup(AccessVal)); |
| 1299 | |
| 1300 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1301 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1302 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1303 | assert(!PHIWrites.lookup(PHI)); |
| 1304 | |
| 1305 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1306 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1307 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1308 | assert(!PHIReads.lookup(PHI)); |
| 1309 | |
| 1310 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1316 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1317 | for (MemoryAccess *MA : *this) |
| 1318 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1319 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 1320 | auto *Ctx = Parent.getContext(); |
| 1321 | InvalidDomain = isl_set_gist_params(InvalidDomain, isl_set_copy(Ctx)); |
| 1322 | Domain = isl_set_gist_params(Domain, Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1325 | /// 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] | 1326 | static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet, |
| 1327 | void *User) { |
| 1328 | isl_set **BoundedParts = static_cast<isl_set **>(User); |
| 1329 | if (isl_basic_set_is_bounded(BSet)) |
| 1330 | *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet)); |
| 1331 | else |
| 1332 | isl_basic_set_free(BSet); |
| 1333 | return isl_stat_ok; |
| 1334 | } |
| 1335 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1336 | /// Return the bounded parts of @p S. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1337 | static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) { |
| 1338 | isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S)); |
| 1339 | isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts); |
| 1340 | isl_set_free(S); |
| 1341 | return BoundedParts; |
| 1342 | } |
| 1343 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1344 | /// 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] | 1345 | /// |
| 1346 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1347 | /// both with regards to the dimension @p Dim. |
| 1348 | static std::pair<__isl_give isl_set *, __isl_give isl_set *> |
| 1349 | partitionSetParts(__isl_take isl_set *S, unsigned Dim) { |
| 1350 | |
| 1351 | 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] | 1352 | S = isl_set_lower_bound_si(S, isl_dim_set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1353 | |
| 1354 | unsigned NumDimsS = isl_set_n_dim(S); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1355 | isl_set *OnlyDimS = isl_set_copy(S); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1358 | assert(NumDimsS >= Dim + 1); |
| 1359 | OnlyDimS = |
| 1360 | isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1); |
| 1361 | |
| 1362 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1363 | // as we are not interested in them. |
| 1364 | OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim); |
| 1365 | for (unsigned u = 0; u < Dim; u++) { |
| 1366 | isl_constraint *C = isl_inequality_alloc( |
| 1367 | isl_local_space_from_space(isl_set_get_space(OnlyDimS))); |
| 1368 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1); |
| 1369 | C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1); |
| 1370 | OnlyDimS = isl_set_add_constraint(OnlyDimS, C); |
| 1371 | } |
| 1372 | |
| 1373 | // Collect all bounded parts of OnlyDimS. |
| 1374 | isl_set *BoundedParts = collectBoundedParts(OnlyDimS); |
| 1375 | |
| 1376 | // Create the dimensions greater than Dim again. |
| 1377 | BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1, |
| 1378 | NumDimsS - Dim - 1); |
| 1379 | |
| 1380 | // Remove the artificial upper bound parameters again. |
| 1381 | BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim); |
| 1382 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1383 | isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1384 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1385 | } |
| 1386 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1387 | /// Set the dimension Ids from @p From in @p To. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1388 | static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From, |
| 1389 | __isl_take isl_set *To) { |
| 1390 | for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) { |
| 1391 | isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u); |
| 1392 | To = isl_set_set_dim_id(To, isl_dim_set, u, DimId); |
| 1393 | } |
| 1394 | return To; |
| 1395 | } |
| 1396 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1397 | /// 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] | 1398 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1399 | __isl_take isl_pw_aff *L, |
| 1400 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1401 | switch (Pred) { |
| 1402 | case ICmpInst::ICMP_EQ: |
| 1403 | return isl_pw_aff_eq_set(L, R); |
| 1404 | case ICmpInst::ICMP_NE: |
| 1405 | return isl_pw_aff_ne_set(L, R); |
| 1406 | case ICmpInst::ICMP_SLT: |
| 1407 | return isl_pw_aff_lt_set(L, R); |
| 1408 | case ICmpInst::ICMP_SLE: |
| 1409 | return isl_pw_aff_le_set(L, R); |
| 1410 | case ICmpInst::ICMP_SGT: |
| 1411 | return isl_pw_aff_gt_set(L, R); |
| 1412 | case ICmpInst::ICMP_SGE: |
| 1413 | return isl_pw_aff_ge_set(L, R); |
| 1414 | case ICmpInst::ICMP_ULT: |
| 1415 | return isl_pw_aff_lt_set(L, R); |
| 1416 | case ICmpInst::ICMP_UGT: |
| 1417 | return isl_pw_aff_gt_set(L, R); |
| 1418 | case ICmpInst::ICMP_ULE: |
| 1419 | return isl_pw_aff_le_set(L, R); |
| 1420 | case ICmpInst::ICMP_UGE: |
| 1421 | return isl_pw_aff_ge_set(L, R); |
| 1422 | default: |
| 1423 | llvm_unreachable("Non integer predicate not supported"); |
| 1424 | } |
| 1425 | } |
| 1426 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1427 | /// 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] | 1428 | /// |
| 1429 | /// Helper function that will make sure the dimensions of the result have the |
| 1430 | /// same isl_id's as the @p Domain. |
| 1431 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
| 1432 | __isl_take isl_pw_aff *L, |
| 1433 | __isl_take isl_pw_aff *R, |
| 1434 | __isl_keep isl_set *Domain) { |
| 1435 | isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R); |
| 1436 | return setDimensionIds(Domain, ConsequenceCondSet); |
| 1437 | } |
| 1438 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1439 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1440 | /// |
| 1441 | /// @param S The Scop in which @p BB resides in. |
| 1442 | /// @param BB The BB for which isl representation is to be |
| 1443 | /// computed. |
| 1444 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1445 | /// @param E The SCEV that should be translated. |
| 1446 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1447 | /// |
| 1448 | /// Note that this function will also adjust the invalid context accordingly. |
| 1449 | |
| 1450 | __isl_give isl_pw_aff * |
| 1451 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1452 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1453 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1454 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1455 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(isl::manage(PWAC.second)); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1456 | return PWAC.first; |
| 1457 | } |
| 1458 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1459 | /// 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] | 1460 | /// |
| 1461 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1462 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1463 | /// have as many elements as @p SI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1464 | static bool |
| 1465 | buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1466 | __isl_keep isl_set *Domain, |
| 1467 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1468 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1469 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1470 | Value *Condition = getConditionFromTerminator(SI); |
| 1471 | assert(Condition && "No condition for switch"); |
| 1472 | |
| 1473 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1474 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1475 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1476 | |
| 1477 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1478 | ConditionSets.resize(NumSuccessors); |
| 1479 | for (auto &Case : SI->cases()) { |
| 1480 | unsigned Idx = Case.getSuccessorIndex(); |
| 1481 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1482 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1483 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1484 | isl_set *CaseConditionSet = |
| 1485 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain); |
| 1486 | ConditionSets[Idx] = isl_set_coalesce( |
| 1487 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1488 | } |
| 1489 | |
| 1490 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1491 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1492 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1493 | ConditionSetUnion = |
| 1494 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
| 1495 | ConditionSets[0] = setDimensionIds( |
| 1496 | Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion)); |
| 1497 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1498 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1499 | |
| 1500 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1503 | /// Build condition sets for unsigned ICmpInst(s). |
| 1504 | /// Special handling is required for unsigned operands to ensure that if |
| 1505 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1506 | /// it should wrap around. |
| 1507 | /// |
| 1508 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1509 | /// between TestVal and UpperBound, i.e, |
| 1510 | /// TestVal < UpperBound OR TestVal <= UpperBound |
| 1511 | static __isl_give isl_set * |
| 1512 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1513 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1514 | const SCEV *SCEV_UpperBound, |
| 1515 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1516 | bool IsStrictUpperBound) { |
| 1517 | |
| 1518 | // Do not take NonNeg assumption on TestVal |
| 1519 | // as it might have MSB (Sign bit) set. |
| 1520 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1521 | // Take NonNeg assumption on UpperBound. |
| 1522 | isl_pw_aff *UpperBound = |
| 1523 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1524 | |
| 1525 | // 0 <= TestVal |
| 1526 | isl_set *First = |
| 1527 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1528 | isl_pw_aff_get_domain_space(TestVal))), |
| 1529 | isl_pw_aff_copy(TestVal)); |
| 1530 | |
| 1531 | isl_set *Second; |
| 1532 | if (IsStrictUpperBound) |
| 1533 | // TestVal < UpperBound |
| 1534 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1535 | else |
| 1536 | // TestVal <= UpperBound |
| 1537 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1538 | |
| 1539 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
| 1540 | ConsequenceCondSet = setDimensionIds(Domain, ConsequenceCondSet); |
| 1541 | return ConsequenceCondSet; |
| 1542 | } |
| 1543 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1544 | /// Build the conditions sets for the branch condition @p Condition in |
| 1545 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1546 | /// |
| 1547 | /// This will fill @p ConditionSets with the conditions under which control |
| 1548 | /// 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] | 1549 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1550 | /// context under which @p Condition is true/false will be returned as the |
| 1551 | /// new elements of @p ConditionSets. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1552 | static bool |
| 1553 | buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1554 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1555 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1556 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1557 | |
| 1558 | isl_set *ConsequenceCondSet = nullptr; |
| 1559 | if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
| 1560 | if (CCond->isZero()) |
| 1561 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1562 | else |
| 1563 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1564 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1565 | auto Opcode = BinOp->getOpcode(); |
| 1566 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1567 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1568 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1569 | InvalidDomainMap, ConditionSets) && |
| 1570 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1571 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1572 | if (!Valid) { |
| 1573 | while (!ConditionSets.empty()) |
| 1574 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1575 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1576 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1577 | |
| 1578 | isl_set_free(ConditionSets.pop_back_val()); |
| 1579 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1580 | isl_set_free(ConditionSets.pop_back_val()); |
| 1581 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1582 | |
| 1583 | if (Opcode == Instruction::And) |
| 1584 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1585 | else |
| 1586 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1587 | } else { |
| 1588 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1589 | assert(ICond && |
| 1590 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1591 | |
| 1592 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1593 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1594 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1595 | // to be set. The comparison is equal to a signed comparison under this |
| 1596 | // assumption. |
| 1597 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1598 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1599 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1600 | |
| 1601 | switch (ICond->getPredicate()) { |
| 1602 | case ICmpInst::ICMP_ULT: |
| 1603 | ConsequenceCondSet = |
| 1604 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1605 | RightOperand, InvalidDomainMap, true); |
| 1606 | break; |
| 1607 | case ICmpInst::ICMP_ULE: |
| 1608 | ConsequenceCondSet = |
| 1609 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1610 | RightOperand, InvalidDomainMap, false); |
| 1611 | break; |
| 1612 | case ICmpInst::ICMP_UGT: |
| 1613 | ConsequenceCondSet = |
| 1614 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1615 | LeftOperand, InvalidDomainMap, true); |
| 1616 | break; |
| 1617 | case ICmpInst::ICMP_UGE: |
| 1618 | ConsequenceCondSet = |
| 1619 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1620 | LeftOperand, InvalidDomainMap, false); |
| 1621 | break; |
| 1622 | default: |
| 1623 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1624 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
| 1625 | ConsequenceCondSet = |
| 1626 | buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain); |
| 1627 | break; |
| 1628 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1631 | // If no terminator was given we are only looking for parameter constraints |
| 1632 | // under which @p Condition is true/false. |
| 1633 | if (!TI) |
| 1634 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1635 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1636 | ConsequenceCondSet = isl_set_coalesce( |
| 1637 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1638 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1639 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1640 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1641 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1642 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1643 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1644 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1645 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1646 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1647 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1650 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1651 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1652 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1653 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1654 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1655 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | ConditionSets.push_back(ConsequenceCondSet); |
| 1659 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1660 | |
| 1661 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1664 | /// 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] | 1665 | /// |
| 1666 | /// This will fill @p ConditionSets with the conditions under which control |
| 1667 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1668 | /// have as many elements as @p TI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1669 | static bool |
| 1670 | buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1671 | __isl_keep isl_set *Domain, |
| 1672 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1673 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1676 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1677 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1678 | |
| 1679 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1680 | |
| 1681 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1682 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1683 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1686 | Value *Condition = getConditionFromTerminator(TI); |
| 1687 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1688 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1689 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1690 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1693 | void ScopStmt::buildDomain() { |
Michael Kruse | 526fcf5 | 2016-02-24 22:08:08 +0000 | [diff] [blame] | 1694 | isl_id *Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1695 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1696 | Domain = getParent()->getDomainConditions(this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1697 | Domain = isl_set_set_tuple_id(Domain, Id); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1700 | void ScopStmt::collectSurroundingLoops() { |
| 1701 | for (unsigned u = 0, e = isl_set_n_dim(Domain); u < e; u++) { |
| 1702 | isl_id *DimId = isl_set_get_dim_id(Domain, isl_dim_set, u); |
| 1703 | NestLoops.push_back(static_cast<Loop *>(isl_id_get_user(DimId))); |
| 1704 | isl_id_free(DimId); |
| 1705 | } |
| 1706 | } |
| 1707 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1708 | ScopStmt::ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1709 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(nullptr), |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1710 | R(&R), Build(nullptr), SurroundingLoop(SurroundingLoop) { |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1711 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1712 | BaseName = getIslCompatibleName( |
| 1713 | "Stmt", R.getNameStr(), parent.getNextStmtIdx(), "", UseInstructionNames); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1716 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop, |
| 1717 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1718 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1719 | R(nullptr), Build(nullptr), SurroundingLoop(SurroundingLoop), |
| 1720 | Instructions(Instructions) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1721 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1722 | BaseName = getIslCompatibleName("Stmt", &bb, parent.getNextStmtIdx(), "", |
| 1723 | UseInstructionNames); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1726 | ScopStmt::ScopStmt(Scop &parent, __isl_take isl_map *SourceRel, |
| 1727 | __isl_take isl_map *TargetRel, __isl_take isl_set *NewDomain) |
| 1728 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), BB(nullptr), |
| 1729 | R(nullptr), Build(nullptr) { |
| 1730 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1731 | std::to_string(parent.getCopyStmtsNum())); |
| 1732 | auto *Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
| 1733 | Domain = isl_set_set_tuple_id(Domain, isl_id_copy(Id)); |
| 1734 | TargetRel = isl_map_set_tuple_id(TargetRel, isl_dim_in, Id); |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1735 | auto *Access = new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, |
| 1736 | isl::manage(TargetRel)); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1737 | parent.addAccessFunction(Access); |
| 1738 | addAccess(Access); |
| 1739 | SourceRel = isl_map_set_tuple_id(SourceRel, isl_dim_in, isl_id_copy(Id)); |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1740 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, |
| 1741 | isl::manage(SourceRel)); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1742 | parent.addAccessFunction(Access); |
| 1743 | addAccess(Access); |
| 1744 | } |
| 1745 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 1746 | void ScopStmt::init(LoopInfo &LI) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1747 | assert(!Domain && "init must be called only once"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1748 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1749 | buildDomain(); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1750 | collectSurroundingLoops(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1751 | buildAccessRelations(); |
| 1752 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 1753 | if (DetectReductions) |
| 1754 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1757 | /// Collect loads which might form a reduction chain with @p StoreMA. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1758 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1759 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 1760 | /// two loads as operands. If the binary operand is commutative & associative, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1761 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 1762 | /// once, we have found a possible reduction chain. It starts at an operand |
| 1763 | /// load and includes the binary operator and @p StoreMA. |
| 1764 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1765 | /// 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] | 1766 | /// escape this block or into any other store except @p StoreMA. |
| 1767 | void ScopStmt::collectCandiateReductionLoads( |
| 1768 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 1769 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 1770 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | |
| 1773 | // Skip if there is not one binary operator between the load and the store |
| 1774 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1775 | if (!BinOp) |
| 1776 | return; |
| 1777 | |
| 1778 | // Skip if the binary operators has multiple uses |
| 1779 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1782 | // Skip if the opcode of the binary operator is not commutative/associative |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1783 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 1784 | return; |
| 1785 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1786 | // Skip if the binary operator is outside the current SCoP |
| 1787 | if (BinOp->getParent() != Store->getParent()) |
| 1788 | return; |
| 1789 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1790 | // Skip if it is a multiplicative reduction and we disabled them |
| 1791 | if (DisableMultiplicativeReductions && |
| 1792 | (BinOp->getOpcode() == Instruction::Mul || |
| 1793 | BinOp->getOpcode() == Instruction::FMul)) |
| 1794 | return; |
| 1795 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1796 | // Check the binary operator operands for a candidate load |
| 1797 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 1798 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 1799 | if (!PossibleLoad0 && !PossibleLoad1) |
| 1800 | return; |
| 1801 | |
| 1802 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 1803 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1804 | if (PossibleLoad0->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1805 | Loads.push_back(&getArrayAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1806 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1807 | if (PossibleLoad1->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1808 | Loads.push_back(&getArrayAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1811 | /// Check for reductions in this ScopStmt. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1812 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1813 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 1814 | /// like chains. For all candidates we check if they have the same base address |
| 1815 | /// and there are no other accesses which overlap with them. The base address |
| 1816 | /// check rules out impossible reductions candidates early. The overlap check, |
| 1817 | /// together with the "only one user" check in collectCandiateReductionLoads, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1818 | /// guarantees that none of the intermediate results will escape during |
| 1819 | /// execution of the loop nest. We basically check here that no other memory |
| 1820 | /// access can access the same memory as the potential reduction. |
| 1821 | void ScopStmt::checkForReductions() { |
| 1822 | SmallVector<MemoryAccess *, 2> Loads; |
| 1823 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 1824 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1825 | // First collect candidate load-store reduction chains by iterating over all |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1826 | // stores and collecting possible reduction loads. |
| 1827 | for (MemoryAccess *StoreMA : MemAccs) { |
| 1828 | if (StoreMA->isRead()) |
| 1829 | continue; |
| 1830 | |
| 1831 | Loads.clear(); |
| 1832 | collectCandiateReductionLoads(StoreMA, Loads); |
| 1833 | for (MemoryAccess *LoadMA : Loads) |
| 1834 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 1835 | } |
| 1836 | |
| 1837 | // Then check each possible candidate pair. |
| 1838 | for (const auto &CandidatePair : Candidates) { |
| 1839 | bool Valid = true; |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1840 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation().release(); |
| 1841 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation().release(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1842 | |
| 1843 | // Skip those with obviously unequal base addresses. |
| 1844 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 1845 | isl_map_free(LoadAccs); |
| 1846 | isl_map_free(StoreAccs); |
| 1847 | continue; |
| 1848 | } |
| 1849 | |
| 1850 | // And check if the remaining for overlap with other memory accesses. |
| 1851 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
| 1852 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain()); |
| 1853 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 1854 | |
| 1855 | for (MemoryAccess *MA : MemAccs) { |
| 1856 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1857 | continue; |
| 1858 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1859 | isl_map *AccRel = isl_map_intersect_domain( |
| 1860 | MA->getAccessRelation().release(), getDomain()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1861 | isl_set *Accs = isl_map_range(AccRel); |
| 1862 | |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1863 | if (isl_set_has_equal_space(AllAccs, Accs)) { |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1864 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 1865 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 1866 | isl_set_free(OverlapAccs); |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1867 | } else { |
| 1868 | isl_set_free(Accs); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | isl_set_free(AllAccs); |
| 1873 | if (!Valid) |
| 1874 | continue; |
| 1875 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1876 | const LoadInst *Load = |
| 1877 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1878 | MemoryAccess::ReductionType RT = |
| 1879 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1880 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1881 | // If no overlapping access was found we mark the load and store as |
| 1882 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1883 | CandidatePair.first->markAsReductionLike(RT); |
| 1884 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1885 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1888 | std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1889 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1890 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1891 | auto *S = getSchedule(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1892 | if (!S) |
| 1893 | return ""; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1894 | auto Str = stringFromIslObj(S); |
| 1895 | isl_map_free(S); |
| 1896 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1899 | void ScopStmt::setInvalidDomain(__isl_take isl_set *ID) { |
| 1900 | isl_set_free(InvalidDomain); |
| 1901 | InvalidDomain = ID; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1904 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1905 | if (isBlockStmt()) |
| 1906 | return getBasicBlock(); |
| 1907 | return getRegion()->getEntry(); |
| 1908 | } |
| 1909 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1910 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1911 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1912 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1913 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1914 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1915 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1918 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1919 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1920 | __isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1921 | |
Tobias Grosser | 6e6c7e0 | 2015-03-30 12:22:39 +0000 | [diff] [blame] | 1922 | __isl_give isl_space *ScopStmt::getDomainSpace() const { |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1923 | return isl_set_get_space(Domain); |
| 1924 | } |
| 1925 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1926 | __isl_give isl_id *ScopStmt::getDomainId() const { |
| 1927 | return isl_set_get_tuple_id(Domain); |
| 1928 | } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1929 | |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1930 | ScopStmt::~ScopStmt() { |
| 1931 | isl_set_free(Domain); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1932 | isl_set_free(InvalidDomain); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1933 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1934 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1935 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1936 | OS << "Instructions {\n"; |
| 1937 | |
| 1938 | for (Instruction *Inst : Instructions) |
| 1939 | OS.indent(16) << *Inst << "\n"; |
| 1940 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1941 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1944 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1945 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1946 | OS.indent(12) << "Domain :=\n"; |
| 1947 | |
| 1948 | if (Domain) { |
| 1949 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1950 | } else |
| 1951 | OS.indent(16) << "n/a\n"; |
| 1952 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1953 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1954 | |
| 1955 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1956 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1957 | } else |
| 1958 | OS.indent(16) << "n/a\n"; |
| 1959 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1960 | for (MemoryAccess *Access : MemAccs) |
| 1961 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1962 | |
Michael Kruse | eca86ce | 2017-07-26 22:01:33 +0000 | [diff] [blame] | 1963 | if (PrintInstructions && isBlockStmt()) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1964 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1967 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1968 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1969 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1970 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1971 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1972 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1973 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1974 | (void)Found; |
| 1975 | assert(Found && "Expected access data not found"); |
| 1976 | } |
| 1977 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1978 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1979 | (void)Found; |
| 1980 | assert(Found && "Expected access data not found"); |
| 1981 | } |
| 1982 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1983 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1984 | (void)Found; |
| 1985 | assert(Found && "Expected access data not found"); |
| 1986 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1987 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1988 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1989 | (void)Found; |
| 1990 | assert(Found && "Expected access data not found"); |
| 1991 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1994 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1995 | // Remove the memory accesses from this statement together with all scalar |
| 1996 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1997 | // instruction, hence would not be removed by this function. However, it is |
| 1998 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1999 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 2000 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 2001 | auto Predicate = [&](MemoryAccess *Acc) { |
| 2002 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 2003 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2004 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2005 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2006 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2007 | Parent.removeAccessData(MA); |
| 2008 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2009 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 2010 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 2011 | MemAccs.end()); |
| 2012 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2015 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA) { |
| 2016 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 2017 | assert(MAIt != MemAccs.end()); |
| 2018 | MemAccs.erase(MAIt); |
| 2019 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2020 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2021 | Parent.removeAccessData(MA); |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2022 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2023 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 2024 | if (It != InstructionToAccess.end()) { |
| 2025 | It->second.remove(MA); |
| 2026 | if (It->second.empty()) |
| 2027 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 2028 | } |
| 2029 | } |
| 2030 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 2031 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 2032 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 2033 | if (Access) |
| 2034 | return Access; |
| 2035 | |
| 2036 | ScopArrayInfo *SAI = |
| 2037 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 2038 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 2039 | true, {}, {}, V, MemoryKind::Value); |
| 2040 | Parent.addAccessFunction(Access); |
| 2041 | Access->buildAccessRelation(SAI); |
| 2042 | addAccess(Access); |
| 2043 | Parent.addAccessData(Access); |
| 2044 | return Access; |
| 2045 | } |
| 2046 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2047 | raw_ostream &polly::operator<<(raw_ostream &O, const ScopStmt &S) { |
| 2048 | S.print(O, PollyPrintInstructions); |
| 2049 | return O; |
| 2050 | } |
| 2051 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2052 | //===----------------------------------------------------------------------===// |
| 2053 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2054 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 2055 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 2056 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 2057 | isl_set_free(Context); |
| 2058 | Context = NewContext; |
| 2059 | } |
| 2060 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2061 | namespace { |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2062 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2063 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2064 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2065 | ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2066 | |
| 2067 | public: |
| 2068 | SCEVSensitiveParameterRewriter(ValueToValueMap &VMap, ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2069 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2070 | |
| 2071 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
| 2072 | ValueToValueMap &VMap) { |
| 2073 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 2074 | return SSPR.visit(E); |
| 2075 | } |
| 2076 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2077 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 2078 | auto *Start = visit(E->getStart()); |
| 2079 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 2080 | visit(E->getStepRecurrence(SE)), |
| 2081 | E->getLoop(), SCEV::FlagAnyWrap); |
| 2082 | return SE.getAddExpr(Start, AddRec); |
| 2083 | } |
| 2084 | |
| 2085 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 2086 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 2087 | return SE.getUnknown(NewValue); |
| 2088 | return E; |
| 2089 | } |
| 2090 | }; |
| 2091 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2092 | /// Check whether we should remap a SCEV expression. |
| 2093 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
| 2094 | ValueToValueMap &VMap; |
| 2095 | bool FoundInside = false; |
| 2096 | Scop *S; |
| 2097 | |
| 2098 | public: |
| 2099 | SCEVFindInsideScop(ValueToValueMap &VMap, ScalarEvolution &SE, Scop *S) |
| 2100 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 2101 | |
| 2102 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
| 2103 | ValueToValueMap &VMap, Scop *S) { |
| 2104 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 2105 | SFIS.visitAll(E); |
| 2106 | return SFIS.FoundInside; |
| 2107 | } |
| 2108 | |
| 2109 | bool follow(const SCEV *E) { |
| 2110 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 2111 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 2112 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 2113 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 2114 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 2115 | } |
| 2116 | return !FoundInside; |
| 2117 | } |
| 2118 | bool isDone() { return FoundInside; } |
| 2119 | }; |
| 2120 | } // namespace |
| 2121 | |
| 2122 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) { |
| 2123 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 2124 | // doesn't like addition between an AddRec and an expression that |
| 2125 | // doesn't have a dominance relationship with it.) |
| 2126 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 2127 | return E; |
| 2128 | |
| 2129 | // Rewrite SCEV. |
| 2130 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2133 | // This table of function names is used to translate parameter names in more |
| 2134 | // human-readable names. This makes it easier to interpret Polly analysis |
| 2135 | // results. |
| 2136 | StringMap<std::string> KnownNames = { |
| 2137 | {"_Z13get_global_idj", "global_id"}, |
| 2138 | {"_Z12get_local_idj", "local_id"}, |
| 2139 | {"_Z15get_global_sizej", "global_size"}, |
| 2140 | {"_Z14get_local_sizej", "local_size"}, |
| 2141 | {"_Z12get_work_dimv", "work_dim"}, |
| 2142 | {"_Z17get_global_offsetj", "global_offset"}, |
| 2143 | {"_Z12get_group_idj", "group_id"}, |
| 2144 | {"_Z14get_num_groupsj", "num_groups"}, |
| 2145 | }; |
| 2146 | |
| 2147 | static std::string getCallParamName(CallInst *Call) { |
| 2148 | std::string Result; |
| 2149 | raw_string_ostream OS(Result); |
| 2150 | std::string Name = Call->getCalledFunction()->getName(); |
| 2151 | |
| 2152 | auto Iterator = KnownNames.find(Name); |
| 2153 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 2154 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2155 | OS << Name; |
| 2156 | for (auto &Operand : Call->arg_operands()) { |
| 2157 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 2158 | OS << "_" << Op->getValue(); |
| 2159 | } |
| 2160 | OS.flush(); |
| 2161 | return Result; |
| 2162 | } |
| 2163 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2164 | void Scop::createParameterId(const SCEV *Parameter) { |
| 2165 | assert(Parameters.count(Parameter)); |
| 2166 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2167 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2168 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2169 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2170 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 2171 | Value *Val = ValueParameter->getValue(); |
| 2172 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2173 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2174 | if (Call && isConstCall(Call)) { |
| 2175 | ParameterName = getCallParamName(Call); |
| 2176 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2177 | // If this parameter references a specific Value and this value has a name |
| 2178 | // we use this name as it is likely to be unique and more useful than just |
| 2179 | // a number. |
| 2180 | if (Val->hasName()) |
| 2181 | ParameterName = Val->getName(); |
| 2182 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 2183 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 2184 | if (LoadOrigin->hasName()) { |
| 2185 | ParameterName += "_loaded_from_"; |
| 2186 | ParameterName += |
| 2187 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 2188 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2191 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2192 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 2193 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 2194 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2195 | auto *Id = isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 2196 | const_cast<void *>((const void *)Parameter)); |
| 2197 | ParameterIds[Parameter] = Id; |
| 2198 | } |
| 2199 | |
| 2200 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2201 | for (const SCEV *Parameter : NewParameters) { |
| 2202 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2203 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2204 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2205 | |
| 2206 | if (Parameters.insert(Parameter)) |
| 2207 | createParameterId(Parameter); |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) { |
| 2212 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2213 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2214 | return isl_id_copy(ParameterIds.lookup(Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2215 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2216 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 2217 | __isl_give isl_set * |
| 2218 | Scop::addNonEmptyDomainConstraints(__isl_take isl_set *C) const { |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2219 | isl_set *DomainContext = isl_union_set_params(getDomains()); |
| 2220 | return isl_set_intersect_params(C, DomainContext); |
| 2221 | } |
| 2222 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2223 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2224 | return DT.dominates(BB, getEntry()); |
| 2225 | } |
| 2226 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2227 | void Scop::addUserAssumptions( |
| 2228 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2229 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2230 | for (auto &Assumption : AC.assumptions()) { |
| 2231 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2232 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2233 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2234 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2235 | bool InScop = contains(CI); |
| 2236 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2237 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2238 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2239 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2240 | auto *Val = CI->getArgOperand(0); |
| 2241 | ParameterSetTy DetectedParams; |
| 2242 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2243 | ORE.emit( |
| 2244 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 2245 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2246 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2247 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2248 | |
| 2249 | // Collect all newly introduced parameters. |
| 2250 | ParameterSetTy NewParams; |
| 2251 | for (auto *Param : DetectedParams) { |
| 2252 | Param = extractConstantFactor(Param, *SE).second; |
| 2253 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2254 | if (Parameters.count(Param)) |
| 2255 | continue; |
| 2256 | NewParams.insert(Param); |
| 2257 | } |
| 2258 | |
| 2259 | SmallVector<isl_set *, 2> ConditionSets; |
| 2260 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2261 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
| 2262 | auto *Dom = InScop ? DomainMap[BB].copy() : isl_set_copy(Context); |
| 2263 | assert(Dom && "Cannot propagate a nullptr."); |
| 2264 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 2265 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2266 | isl_set_free(Dom); |
| 2267 | |
| 2268 | if (!Valid) |
| 2269 | continue; |
| 2270 | |
| 2271 | isl_set *AssumptionCtx = nullptr; |
| 2272 | if (InScop) { |
| 2273 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2274 | isl_set_free(ConditionSets[0]); |
| 2275 | } else { |
| 2276 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2277 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2278 | } |
| 2279 | |
| 2280 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2281 | if (!NewParams.empty()) { |
| 2282 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2283 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2284 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2285 | isl_id_free(Id); |
| 2286 | |
| 2287 | if (!NewParams.count(Param)) |
| 2288 | continue; |
| 2289 | |
| 2290 | AssumptionCtx = |
| 2291 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2292 | } |
| 2293 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2294 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2295 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2296 | Context = isl_set_intersect(Context, AssumptionCtx); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2300 | void Scop::addUserContext() { |
| 2301 | if (UserContextStr.empty()) |
| 2302 | return; |
| 2303 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2304 | isl_set *UserContext = |
| 2305 | isl_set_read_from_str(getIslCtx(), UserContextStr.c_str()); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2306 | isl_space *Space = getParamSpace(); |
| 2307 | if (isl_space_dim(Space, isl_dim_param) != |
| 2308 | isl_set_dim(UserContext, isl_dim_param)) { |
| 2309 | auto SpaceStr = isl_space_to_str(Space); |
| 2310 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2311 | << "number of dimensions than the computed context. Due to this " |
| 2312 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2313 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2314 | free(SpaceStr); |
| 2315 | isl_set_free(UserContext); |
| 2316 | isl_space_free(Space); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
| 2320 | 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] | 2321 | auto *NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 2322 | auto *NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2323 | |
| 2324 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 2325 | auto SpaceStr = isl_space_to_str(Space); |
| 2326 | errs() << "Error: the name of dimension " << i |
| 2327 | << " provided in -polly-context " |
| 2328 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2329 | << "context is '" << NameContext |
| 2330 | << "'. Due to this name mismatch, " |
| 2331 | << "the -polly-context option is ignored. Please provide " |
| 2332 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2333 | free(SpaceStr); |
| 2334 | isl_set_free(UserContext); |
| 2335 | isl_space_free(Space); |
| 2336 | return; |
| 2337 | } |
| 2338 | |
| 2339 | UserContext = |
| 2340 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 2341 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 2342 | } |
| 2343 | |
| 2344 | Context = isl_set_intersect(Context, UserContext); |
| 2345 | isl_space_free(Space); |
| 2346 | } |
| 2347 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2348 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2349 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2350 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2351 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2352 | for (LoadInst *LInst : RIL) { |
| 2353 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2354 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2355 | Type *Ty = LInst->getType(); |
| 2356 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2357 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2358 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2359 | continue; |
| 2360 | } |
| 2361 | |
| 2362 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2363 | InvariantEquivClasses.emplace_back( |
| 2364 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2368 | void Scop::buildContext() { |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2369 | isl_space *Space = isl_space_params_alloc(getIslCtx(), 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2370 | Context = isl_set_universe(isl_space_copy(Space)); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2371 | InvalidContext = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2372 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2375 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2376 | unsigned PDim = 0; |
| 2377 | for (auto *Parameter : Parameters) { |
| 2378 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 2379 | Context = |
| 2380 | addRangeBoundsToSet(give(Context), SRange, PDim++, isl::dim::param) |
| 2381 | .release(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2385 | // We use the outermost dimension to generate GPU transfers for Fortran arrays |
| 2386 | // even when the array bounds are not known statically. To do so, we need the |
| 2387 | // outermost dimension information. We add this into the context so that the |
| 2388 | // outermost dimension is available during codegen. |
| 2389 | // We currently do not care about dimensions other than the outermost |
| 2390 | // dimension since it doesn't affect transfers. |
| 2391 | static isl_set *addFortranArrayOutermostDimParams(__isl_give isl_set *Context, |
| 2392 | Scop::array_range Arrays) { |
| 2393 | |
| 2394 | std::vector<isl_id *> OutermostSizeIds; |
| 2395 | for (auto Array : Arrays) { |
| 2396 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2397 | // for its outermost dimension. Fortran arrays will have this since the |
| 2398 | // outermost dimension size can be picked up from their runtime description. |
| 2399 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2400 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 2401 | isl_pw_aff *PwAff = Array->getDimensionSizePw(0).release(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2402 | if (!PwAff) |
| 2403 | continue; |
| 2404 | |
| 2405 | isl_id *Id = isl_pw_aff_get_dim_id(PwAff, isl_dim_param, 0); |
| 2406 | isl_pw_aff_free(PwAff); |
| 2407 | assert(Id && "Invalid Id for PwAff expression in Fortran array"); |
| 2408 | OutermostSizeIds.push_back(Id); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | const int NumTrueParams = isl_set_dim(Context, isl_dim_param); |
| 2413 | Context = isl_set_add_dims(Context, isl_dim_param, OutermostSizeIds.size()); |
| 2414 | |
| 2415 | for (size_t i = 0; i < OutermostSizeIds.size(); i++) { |
| 2416 | Context = isl_set_set_dim_id(Context, isl_dim_param, NumTrueParams + i, |
| 2417 | OutermostSizeIds[i]); |
| 2418 | Context = |
| 2419 | isl_set_lower_bound_si(Context, isl_dim_param, NumTrueParams + i, 0); |
| 2420 | } |
| 2421 | |
| 2422 | return Context; |
| 2423 | } |
| 2424 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2425 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2426 | if (PollyIgnoreParamBounds) |
| 2427 | return; |
| 2428 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2429 | // Add all parameters into a common model. |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2430 | isl_space *Space = isl_space_params_alloc(getIslCtx(), ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2431 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2432 | unsigned PDim = 0; |
| 2433 | for (const auto *Parameter : Parameters) { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2434 | isl_id *id = getIdForParam(Parameter); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2435 | Space = isl_space_set_dim_id(Space, isl_dim_param, PDim++, id); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | // Align the parameters of all data structures to the model. |
| 2439 | Context = isl_set_align_params(Context, Space); |
| 2440 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2441 | // Add the outermost dimension of the Fortran arrays into the Context. |
| 2442 | // See the description of the function for more information. |
| 2443 | Context = addFortranArrayOutermostDimParams(Context, arrays()); |
| 2444 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2445 | // As all parameters are known add bounds to them. |
| 2446 | addParameterBounds(); |
| 2447 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2448 | for (ScopStmt &Stmt : *this) |
| 2449 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2450 | // Simplify the schedule according to the context too. |
| 2451 | Schedule = isl_schedule_gist_domain_params(Schedule, getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2454 | static __isl_give isl_set * |
| 2455 | simplifyAssumptionContext(__isl_take isl_set *AssumptionContext, |
| 2456 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2457 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2458 | // simplify the context with the constraints that are needed for anything to |
| 2459 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2460 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2461 | // domains, thus we cannot use the remaining domain to simplify the |
| 2462 | // assumptions. |
| 2463 | if (!S.hasErrorBlock()) { |
| 2464 | isl_set *DomainParameters = isl_union_set_params(S.getDomains()); |
| 2465 | AssumptionContext = |
| 2466 | isl_set_gist_params(AssumptionContext, DomainParameters); |
| 2467 | } |
| 2468 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2469 | AssumptionContext = isl_set_gist_params(AssumptionContext, S.getContext()); |
| 2470 | return AssumptionContext; |
| 2471 | } |
| 2472 | |
| 2473 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2474 | // The parameter constraints of the iteration domains give us a set of |
| 2475 | // constraints that need to hold for all cases where at least a single |
| 2476 | // statement iteration is executed in the whole scop. We now simplify the |
| 2477 | // assumed context under the assumption that such constraints hold and at |
| 2478 | // least a single statement iteration is executed. For cases where no |
| 2479 | // statement instances are executed, the assumptions we have taken about |
| 2480 | // the executed code do not matter and can be changed. |
| 2481 | // |
| 2482 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2483 | // the set of statement instances that are executed. Otherwise we |
| 2484 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2485 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2486 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2487 | // performed. In such a case, modifying the run-time conditions and |
| 2488 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2489 | // to not be executed. |
| 2490 | // |
| 2491 | // Example: |
| 2492 | // |
| 2493 | // When delinearizing the following code: |
| 2494 | // |
| 2495 | // for (long i = 0; i < 100; i++) |
| 2496 | // for (long j = 0; j < m; j++) |
| 2497 | // A[i+p][j] = 1.0; |
| 2498 | // |
| 2499 | // 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] | 2500 | // 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] | 2501 | // 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] | 2502 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2503 | InvalidContext = isl_set_align_params(InvalidContext, getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2506 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2507 | static isl::stat |
| 2508 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2509 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2510 | isl::pw_aff LastDimAff; |
| 2511 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2512 | unsigned Pos; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2513 | isl::ctx Ctx = Set.get_ctx(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2514 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2515 | Set = Set.remove_divs(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2516 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2517 | if (isl_set_n_basic_set(Set.get()) >= MaxDisjunctsInDomain) |
| 2518 | return isl::stat::error; |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2519 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2520 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2521 | // lexmax computation will take too long if this number is high. |
| 2522 | // |
| 2523 | // Experiments with a simple test case using an i7 4800MQ: |
| 2524 | // |
| 2525 | // #Parameters involved | Time (in sec) |
| 2526 | // 6 | 0.01 |
| 2527 | // 7 | 0.04 |
| 2528 | // 8 | 0.12 |
| 2529 | // 9 | 0.40 |
| 2530 | // 10 | 1.54 |
| 2531 | // 11 | 6.78 |
| 2532 | // 12 | 30.38 |
| 2533 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2534 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2535 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2536 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2537 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2538 | InvolvedParams++; |
| 2539 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2540 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2541 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 2544 | if (isl_set_n_basic_set(Set.get()) > RunTimeChecksMaxAccessDisjuncts) |
| 2545 | return isl::stat::error; |
| 2546 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2547 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2548 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2549 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2550 | if (isl_ctx_last_error(Ctx.get()) == isl_error_quota) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2551 | return isl::stat::error; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2552 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2553 | MinPMA = MinPMA.coalesce(); |
| 2554 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2555 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2556 | // Adjust the last dimension of the maximal access by one as we want to |
| 2557 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2558 | // we test during code generation might now point after the end of the |
| 2559 | // allocated array but we will never dereference it anyway. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2560 | assert(MaxPMA.dim(isl::dim::out) && "Assumed at least one output dimension"); |
| 2561 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2562 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2563 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2564 | OneAff = OneAff.add_constant_si(1); |
| 2565 | LastDimAff = LastDimAff.add(OneAff); |
| 2566 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2567 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2568 | MinMaxAccesses.push_back(std::make_pair(MinPMA.copy(), MaxPMA.copy())); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2569 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2570 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2573 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
| 2574 | isl_set *Domain = MA->getStatement()->getDomain(); |
| 2575 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2576 | return isl_set_reset_tuple_id(Domain); |
| 2577 | } |
| 2578 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2579 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2580 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2581 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2582 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2583 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2584 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2585 | isl::union_set Domains = give(S.getDomains()); |
| 2586 | isl::union_map Accesses = isl::union_map::empty(give(S.getParamSpace())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2587 | |
| 2588 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 2589 | Accesses = Accesses.add_map(give(MA->getAccessRelation().release())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2590 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2591 | Accesses = Accesses.intersect_domain(Domains); |
| 2592 | isl::union_set Locations = Accesses.range(); |
| 2593 | Locations = Locations.coalesce(); |
| 2594 | Locations = Locations.detect_equalities(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2595 | |
| 2596 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2597 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2598 | }; |
| 2599 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2602 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2603 | /// |
| 2604 | ///{ |
| 2605 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2606 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2607 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2608 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2609 | : RN->getNodeAs<BasicBlock>(); |
| 2610 | } |
| 2611 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2612 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2613 | static inline BasicBlock * |
| 2614 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2615 | if (RN->isSubRegion()) { |
| 2616 | assert(idx == 0); |
| 2617 | return RN->getNodeAs<Region>()->getExit(); |
| 2618 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2619 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2622 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2623 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2624 | if (!RN->isSubRegion()) { |
| 2625 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2626 | Loop *L = LI.getLoopFor(BB); |
| 2627 | |
| 2628 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2629 | // they are not part of an actual loop in the control flow graph. |
| 2630 | // Nevertheless, we handle certain unreachable statements that are common |
| 2631 | // when modeling run-time bounds checks as being part of the loop to be |
| 2632 | // able to model them and to later eliminate the run-time bounds checks. |
| 2633 | // |
| 2634 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2635 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2636 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2637 | // allows us to model the following code. |
| 2638 | // |
| 2639 | // for (i = 0; i < N; i++) { |
| 2640 | // if (i > 1024) |
| 2641 | // abort(); <- this abort might be translated to an |
| 2642 | // unreachable |
| 2643 | // |
| 2644 | // A[i] = ... |
| 2645 | // } |
| 2646 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2647 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2648 | return L; |
| 2649 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2650 | |
| 2651 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2652 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2653 | while (L && NonAffineSubRegion->contains(L)) |
| 2654 | L = L->getParentLoop(); |
| 2655 | return L; |
| 2656 | } |
| 2657 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2658 | /// Get the number of blocks in @p L. |
| 2659 | /// |
| 2660 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2661 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2662 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2663 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2664 | /// scop and represent run-time conditions which we want to model and |
| 2665 | /// subsequently speculate away. |
| 2666 | /// |
| 2667 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2668 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2669 | unsigned NumBlocks = L->getNumBlocks(); |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2670 | SmallVector<llvm::BasicBlock *, 4> ExitBlocks; |
| 2671 | L->getExitBlocks(ExitBlocks); |
| 2672 | |
| 2673 | for (auto ExitBlock : ExitBlocks) { |
| 2674 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2675 | NumBlocks++; |
| 2676 | } |
| 2677 | return NumBlocks; |
| 2678 | } |
| 2679 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2680 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2681 | if (!RN->isSubRegion()) |
| 2682 | return 1; |
| 2683 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2684 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2685 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2688 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2689 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2690 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2691 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2692 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2693 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2694 | return true; |
| 2695 | return false; |
| 2696 | } |
| 2697 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2698 | ///} |
| 2699 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2700 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 2701 | unsigned Dim, Loop *L) { |
Michael Kruse | 88a2256 | 2016-03-29 07:50:52 +0000 | [diff] [blame] | 2702 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2703 | isl_id *DimId = |
| 2704 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 2705 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 2706 | } |
| 2707 | |
Johannes Doerfert | fff283d | 2016-04-19 14:48:22 +0000 | [diff] [blame] | 2708 | __isl_give isl_set *Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2709 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Johannes Doerfert | fff283d | 2016-04-19 14:48:22 +0000 | [diff] [blame] | 2712 | __isl_give isl_set *Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2713 | auto DIt = DomainMap.find(BB); |
| 2714 | if (DIt != DomainMap.end()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2715 | return DIt->getSecond().copy(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2716 | |
| 2717 | auto &RI = *R.getRegionInfo(); |
| 2718 | auto *BBR = RI.getRegionFor(BB); |
| 2719 | while (BBR->getEntry() == BB) |
| 2720 | BBR = BBR->getParent(); |
| 2721 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2724 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2725 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2726 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2727 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2728 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2729 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2730 | int LD = getRelativeLoopDepth(L); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2731 | 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] | 2732 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2733 | while (LD-- >= 0) { |
| 2734 | S = addDomainDimId(S, LD + 1, L); |
| 2735 | L = L->getParentLoop(); |
| 2736 | } |
| 2737 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2738 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2739 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2740 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2741 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2742 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2743 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2744 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2745 | return false; |
| 2746 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2747 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2748 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2751 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2752 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2753 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2754 | // will cause problems when building up a ScopStmt for them. |
| 2755 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2756 | // 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] | 2757 | // can themselves not be constructed properly. To this end we will replace |
| 2758 | // the domains of error blocks and those only reachable via error blocks |
| 2759 | // 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] | 2760 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2761 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2762 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2763 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2764 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2765 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2768 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2769 | /// to be compatible to domains constructed for loop @p NewL. |
| 2770 | /// |
| 2771 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2772 | /// edge from @p OldL to @p NewL. |
| 2773 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2774 | __isl_take isl_set *Dom, |
| 2775 | Loop *OldL, Loop *NewL) { |
| 2776 | |
| 2777 | // If the loops are the same there is nothing to do. |
| 2778 | if (NewL == OldL) |
| 2779 | return Dom; |
| 2780 | |
| 2781 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2782 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2783 | // If both loops are non-affine loops there is nothing to do. |
| 2784 | if (OldDepth == -1 && NewDepth == -1) |
| 2785 | return Dom; |
| 2786 | |
| 2787 | // Distinguish three cases: |
| 2788 | // 1) The depth is the same but the loops are not. |
| 2789 | // => One loop was left one was entered. |
| 2790 | // 2) The depth increased from OldL to NewL. |
| 2791 | // => One loop was entered, none was left. |
| 2792 | // 3) The depth decreased from OldL to NewL. |
| 2793 | // => Loops were left were difference of the depths defines how many. |
| 2794 | if (OldDepth == NewDepth) { |
| 2795 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2796 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2797 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2798 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2799 | } else if (OldDepth < NewDepth) { |
| 2800 | assert(OldDepth + 1 == NewDepth); |
| 2801 | auto &R = S.getRegion(); |
| 2802 | (void)R; |
| 2803 | assert(NewL->getParentLoop() == OldL || |
| 2804 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2805 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2806 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2807 | } else { |
| 2808 | assert(OldDepth > NewDepth); |
| 2809 | int Diff = OldDepth - NewDepth; |
| 2810 | int NumDim = isl_set_n_dim(Dom); |
| 2811 | assert(NumDim >= Diff); |
| 2812 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2813 | } |
| 2814 | |
| 2815 | return Dom; |
| 2816 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2817 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2818 | bool Scop::propagateInvalidStmtDomains( |
| 2819 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2820 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2821 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2822 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2823 | for (auto *RN : RTraversal) { |
| 2824 | |
| 2825 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2826 | // subregions. |
| 2827 | if (RN->isSubRegion()) { |
| 2828 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2829 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2830 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2831 | continue; |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2836 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2837 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2838 | assert(Domain && "Cannot propagate a nullptr"); |
| 2839 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2840 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2841 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2842 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2843 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2844 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2845 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2846 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2847 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2848 | isl::set DomPar = Domain.params(); |
| 2849 | recordAssumption(ERRORBLOCK, DomPar.release(), |
| 2850 | BB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2851 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2854 | if (InvalidDomain.is_empty()) { |
| 2855 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2856 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2859 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2860 | auto *TI = BB->getTerminator(); |
| 2861 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2862 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2863 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2864 | |
| 2865 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2866 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2867 | continue; |
| 2868 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2869 | // Skip backedges. |
| 2870 | if (DT.dominates(SuccBB, BB)) |
| 2871 | continue; |
| 2872 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2873 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2874 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2875 | auto *AdjustedInvalidDomain = adjustDomainDimensions( |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2876 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2877 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2878 | auto *SuccInvalidDomain = InvalidDomainMap[SuccBB].copy(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2879 | SuccInvalidDomain = |
| 2880 | isl_set_union(SuccInvalidDomain, AdjustedInvalidDomain); |
| 2881 | SuccInvalidDomain = isl_set_coalesce(SuccInvalidDomain); |
| 2882 | unsigned NumConjucts = isl_set_n_basic_set(SuccInvalidDomain); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2883 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2884 | InvalidDomainMap[SuccBB] = isl::manage(SuccInvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2885 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2886 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2887 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2888 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2889 | continue; |
| 2890 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2891 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2892 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2893 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2894 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2895 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2896 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2897 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2898 | |
| 2899 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2902 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2903 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2904 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2905 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2906 | |
| 2907 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2908 | // domain to the exit block of the region. Otherwise we are done. |
| 2909 | auto *RI = R.getRegionInfo(); |
| 2910 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2911 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2912 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2913 | return; |
| 2914 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2915 | // 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] | 2916 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2917 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2918 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2919 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2920 | BBLoop->getLoopLatches(LatchBBs); |
| 2921 | for (auto *LatchBB : LatchBBs) |
| 2922 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2923 | return; |
| 2924 | L = L->getParentLoop(); |
| 2925 | } |
| 2926 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2927 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2928 | assert(Domain && "Cannot propagate a nullptr"); |
| 2929 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2930 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2931 | |
| 2932 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2933 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2934 | isl::set AdjustedDomain = isl::manage( |
| 2935 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2936 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2937 | |
| 2938 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2939 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2940 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2941 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2942 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2943 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2944 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2945 | FinishedExitBlocks.insert(ExitBB); |
| 2946 | } |
| 2947 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2948 | bool Scop::buildDomainsWithBranchConstraints( |
| 2949 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2950 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2951 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2952 | // To create the domain for each block in R we iterate over all blocks and |
| 2953 | // subregions in R and propagate the conditions under which the current region |
| 2954 | // element is executed. To this end we iterate in reverse post order over R as |
| 2955 | // it ensures that we first visit all predecessors of a region node (either a |
| 2956 | // basic block or a subregion) before we visit the region node itself. |
| 2957 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2958 | // there we propagate the current domain to all successors, however we add the |
| 2959 | // condition that the successor is actually executed next. |
| 2960 | // As we are only interested in non-loop carried constraints here we can |
| 2961 | // simply skip loop back edges. |
| 2962 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2963 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2964 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2965 | for (auto *RN : RTraversal) { |
| 2966 | |
| 2967 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2968 | // subregions. |
| 2969 | if (RN->isSubRegion()) { |
| 2970 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2971 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2972 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2973 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2974 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2975 | continue; |
| 2976 | } |
| 2977 | } |
| 2978 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2979 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2980 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2981 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2982 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2983 | TerminatorInst *TI = BB->getTerminator(); |
| 2984 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2985 | if (isa<UnreachableInst>(TI)) |
| 2986 | continue; |
| 2987 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2988 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2989 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2990 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2991 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2992 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2993 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2994 | // Propagate the domain from BB directly to blocks that have a superset |
| 2995 | // 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] | 2996 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2997 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2998 | |
| 2999 | // If all successors of BB have been set a domain through the propagation |
| 3000 | // above we do not need to build condition sets but can just skip this |
| 3001 | // block. However, it is important to note that this is a local property |
| 3002 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 3003 | // local variable. |
| 3004 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 3005 | return FinishedExitBlocks.count(SuccBB); |
| 3006 | }; |
| 3007 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 3008 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3009 | |
| 3010 | // Build the condition sets for the successor nodes of the current region |
| 3011 | // node. If it is a non-affine subregion we will always execute the single |
| 3012 | // exit node, hence the single entry node domain is the condition set. For |
| 3013 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3014 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3015 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3016 | ConditionSets.push_back(Domain.copy()); |
| 3017 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3018 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3019 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Now iterate over the successors and set their initial domain based on |
| 3022 | // their condition set. We skip back edges here and have to be careful when |
| 3023 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 3024 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3025 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3026 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3027 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3028 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3029 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3030 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3031 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3032 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3033 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3034 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 3035 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3036 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3037 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3038 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3039 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3040 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3041 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3042 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3043 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 3044 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3045 | CondSet = isl::manage( |
| 3046 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3047 | |
| 3048 | // Set the domain for the successor or merge it with an existing domain in |
| 3049 | // case there are multiple paths (without loop back edges) to the |
| 3050 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3051 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 3052 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3053 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3054 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3055 | } else { |
| 3056 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3057 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3058 | SuccDomain = CondSet; |
| 3059 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3060 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3061 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 3062 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 3063 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3064 | // In case this happens we will clean up and bail. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3065 | if (isl_set_n_basic_set(SuccDomain.get()) < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3066 | continue; |
| 3067 | |
| 3068 | invalidate(COMPLEXITY, DebugLoc()); |
| 3069 | while (++u < ConditionSets.size()) |
| 3070 | isl_set_free(ConditionSets[u]); |
| 3071 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3072 | } |
| 3073 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3074 | |
| 3075 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 3078 | __isl_give isl_set * |
| 3079 | Scop::getPredecessorDomainConstraints(BasicBlock *BB, |
| 3080 | __isl_keep isl_set *Domain, |
| 3081 | DominatorTree &DT, LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3082 | // If @p BB is the ScopEntry we are done |
| 3083 | if (R.getEntry() == BB) |
| 3084 | return isl_set_universe(isl_set_get_space(Domain)); |
| 3085 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3086 | // The region info of this function. |
| 3087 | auto &RI = *R.getRegionInfo(); |
| 3088 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3089 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3090 | |
| 3091 | // A domain to collect all predecessor domains, thus all conditions under |
| 3092 | // which the block is executed. To this end we start with the empty domain. |
| 3093 | isl_set *PredDom = isl_set_empty(isl_set_get_space(Domain)); |
| 3094 | |
| 3095 | // Set of regions of which the entry block domain has been propagated to BB. |
| 3096 | // all predecessors inside any of the regions can be skipped. |
| 3097 | SmallSet<Region *, 8> PropagatedRegions; |
| 3098 | |
| 3099 | for (auto *PredBB : predecessors(BB)) { |
| 3100 | // Skip backedges. |
| 3101 | if (DT.dominates(BB, PredBB)) |
| 3102 | continue; |
| 3103 | |
| 3104 | // If the predecessor is in a region we used for propagation we can skip it. |
| 3105 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 3106 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 3107 | PredBBInRegion)) { |
| 3108 | continue; |
| 3109 | } |
| 3110 | |
| 3111 | // Check if there is a valid region we can use for propagation, thus look |
| 3112 | // for a region that contains the predecessor and has @p BB as exit block. |
| 3113 | auto *PredR = RI.getRegionFor(PredBB); |
| 3114 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 3115 | PredR->getParent(); |
| 3116 | |
| 3117 | // If a valid region for propagation was found use the entry of that region |
| 3118 | // for propagation, otherwise the PredBB directly. |
| 3119 | if (PredR->getExit() == BB) { |
| 3120 | PredBB = PredR->getEntry(); |
| 3121 | PropagatedRegions.insert(PredR); |
| 3122 | } |
| 3123 | |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 3124 | auto *PredBBDom = getDomainConditions(PredBB); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3125 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 3126 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3127 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 3128 | |
| 3129 | PredDom = isl_set_union(PredDom, PredBBDom); |
| 3130 | } |
| 3131 | |
| 3132 | return PredDom; |
| 3133 | } |
| 3134 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3135 | bool Scop::propagateDomainConstraints( |
| 3136 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3137 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3138 | // Iterate over the region R and propagate the domain constrains from the |
| 3139 | // predecessors to the current node. In contrast to the |
| 3140 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 3141 | // information from the predecessors instead of pushing it to the successors. |
| 3142 | // Additionally, we assume the domains to be already present in the domain |
| 3143 | // map here. However, we iterate again in reverse post order so we know all |
| 3144 | // predecessors have been visited before a block or non-affine subregion is |
| 3145 | // visited. |
| 3146 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3147 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3148 | for (auto *RN : RTraversal) { |
| 3149 | |
| 3150 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 3151 | // subregions. |
| 3152 | if (RN->isSubRegion()) { |
| 3153 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3154 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3155 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3156 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3157 | continue; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3162 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 3163 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3164 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3165 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3166 | isl::set PredDom = |
| 3167 | isl::manage(getPredecessorDomainConstraints(BB, Domain.get(), DT, LI)); |
| 3168 | Domain = Domain.intersect(PredDom).coalesce(); |
| 3169 | Domain = Domain.align_params(isl::manage(getParamSpace())); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3170 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3171 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3172 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3173 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3174 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3175 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3176 | |
| 3177 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3180 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 3181 | /// |
| 3182 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 3183 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3184 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3185 | /// |
| 3186 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3187 | static __isl_give isl_map * |
| 3188 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 3189 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 3190 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
Tobias Grosser | f4fe34b | 2017-03-16 21:33:20 +0000 | [diff] [blame] | 3191 | 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] | 3192 | if (u != Dim) |
| 3193 | NextIterationMap = |
| 3194 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 3195 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 3196 | C = isl_constraint_set_constant_si(C, 1); |
| 3197 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 3198 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 3199 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 3200 | return NextIterationMap; |
| 3201 | } |
| 3202 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3203 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3204 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3205 | int LoopDepth = getRelativeLoopDepth(L); |
| 3206 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3207 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3208 | BasicBlock *HeaderBB = L->getHeader(); |
| 3209 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3210 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3211 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3212 | isl::map NextIterationMap = isl::manage( |
| 3213 | createNextIterationMap(HeaderBBDom.get_space().release(), LoopDepth)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3214 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3215 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3216 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3217 | SmallVector<llvm::BasicBlock *, 4> LatchBlocks; |
| 3218 | L->getLoopLatches(LatchBlocks); |
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 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3221 | |
| 3222 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3223 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3224 | if (!LatchBBDom) |
| 3225 | continue; |
| 3226 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3227 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3228 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3229 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 3230 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 3231 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 3232 | |
| 3233 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3234 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3235 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3236 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3237 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3238 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 3239 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3240 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3241 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3242 | // Free the non back edge condition set as we do not need it. |
| 3243 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3244 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3245 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3246 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3247 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3248 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3249 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3250 | BackedgeCondition = BackedgeCondition.project_out( |
| 3251 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3252 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3253 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3254 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3255 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3256 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3257 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3258 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3259 | isl::set UnionBackedgeConditionComplement = |
| 3260 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3261 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3262 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3263 | 0); |
| 3264 | UnionBackedgeConditionComplement = |
| 3265 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3266 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3267 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3268 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3269 | auto Parts = partitionSetParts(HeaderBBDom.copy(), LoopDepth); |
| 3270 | HeaderBBDom = isl::manage(Parts.second); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3271 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3272 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3273 | // the bounded assumptions to the context as they are already implied by the |
| 3274 | // <nsw> tag. |
| 3275 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 3276 | isl_set_free(Parts.first); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3277 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3280 | isl_set *UnboundedCtx = isl_set_params(Parts.first); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3281 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3282 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3283 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3286 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3287 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3288 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3289 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3290 | if (!PointerBaseInst) |
| 3291 | return nullptr; |
| 3292 | |
| 3293 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3294 | if (!BasePtrStmt) |
| 3295 | return nullptr; |
| 3296 | |
| 3297 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3298 | } |
| 3299 | |
| 3300 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3301 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3302 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3303 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3304 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3305 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3306 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3307 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3308 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3309 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3310 | |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3314 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3315 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3316 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3317 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3318 | if (buildAliasGroups(AA)) { |
| 3319 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3320 | // collect statistics so we do it here explicitly. |
| 3321 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3322 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3323 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3324 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3325 | |
| 3326 | // If a problem occurs while building the alias groups we need to delete |
| 3327 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3328 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3329 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3330 | |
| 3331 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3332 | << " could not be created as the number of parameters involved " |
| 3333 | "is too high. The SCoP will be " |
| 3334 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3335 | "the maximal number of parameters but be advised that the " |
| 3336 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3337 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3340 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3341 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3342 | AliasSetTracker AST(AA); |
| 3343 | |
| 3344 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3345 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3346 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3347 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3348 | isl_set *StmtDomain = Stmt.getDomain(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3349 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3350 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3351 | |
| 3352 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3353 | if (StmtDomainEmpty) |
| 3354 | continue; |
| 3355 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3356 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3357 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3358 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3359 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3360 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3361 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3362 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3363 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3364 | else |
| 3365 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3366 | AST.add(Acc); |
| 3367 | } |
| 3368 | } |
| 3369 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3370 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3371 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3372 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3373 | continue; |
| 3374 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3375 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3376 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3377 | if (AG.size() < 2) |
| 3378 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3379 | AliasGroups.push_back(std::move(AG)); |
| 3380 | } |
| 3381 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3382 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3383 | } |
| 3384 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3385 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3386 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3387 | AliasGroupTy NewAG; |
| 3388 | AliasGroupTy &AG = AliasGroups[u]; |
| 3389 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3390 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3391 | while (AGI != AG.end()) { |
| 3392 | MemoryAccess *MA = *AGI; |
| 3393 | isl_set *MADomain = getAccessDomain(MA); |
| 3394 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3395 | NewAG.push_back(MA); |
| 3396 | AGI = AG.erase(AGI); |
| 3397 | isl_set_free(MADomain); |
| 3398 | } else { |
| 3399 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3400 | AGI++; |
| 3401 | } |
| 3402 | } |
| 3403 | if (NewAG.size() > 1) |
| 3404 | AliasGroups.push_back(std::move(NewAG)); |
| 3405 | isl_set_free(AGDomain); |
| 3406 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3410 | // To create sound alias checks we perform the following steps: |
| 3411 | // o) We partition each group into read only and non read only accesses. |
| 3412 | // o) For each group with more than one base pointer we then compute minimal |
| 3413 | // and maximal accesses to each array of a group in read only and non |
| 3414 | // read only partitions separately. |
| 3415 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3416 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3417 | |
| 3418 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3419 | |
| 3420 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3421 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3422 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3423 | if (!hasFeasibleRuntimeContext()) |
| 3424 | return false; |
| 3425 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3426 | { |
| 3427 | IslMaxOperationsGuard MaxOpGuard(getIslCtx(), OptComputeOut); |
| 3428 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3429 | if (!Valid) |
| 3430 | return false; |
| 3431 | } |
| 3432 | if (isl_ctx_last_error(getIslCtx()) == isl_error_quota) { |
| 3433 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3434 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3435 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3436 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3437 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3438 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3441 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3442 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3443 | AliasGroupTy ReadOnlyAccesses; |
| 3444 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3445 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3446 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3447 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3448 | if (AliasGroup.size() < 2) |
| 3449 | return true; |
| 3450 | |
| 3451 | for (MemoryAccess *Access : AliasGroup) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3452 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias", |
| 3453 | Access->getAccessInstruction()) |
| 3454 | << "Possibly aliasing pointer, use restrict keyword."); |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3455 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3456 | if (HasWriteAccess.count(Array)) { |
| 3457 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3458 | ReadWriteAccesses.push_back(Access); |
| 3459 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3460 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3461 | ReadOnlyAccesses.push_back(Access); |
| 3462 | } |
| 3463 | } |
| 3464 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3465 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3466 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3467 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3468 | return true; |
| 3469 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3470 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3471 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3472 | return true; |
| 3473 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3474 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3475 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3476 | for (MemoryAccess *MA : AliasGroup) { |
| 3477 | if (!MA->isAffine()) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3478 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(), |
| 3479 | MA->getAccessInstruction()->getParent()); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3480 | return false; |
| 3481 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3485 | // their base pointers are available. |
| 3486 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3487 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3488 | addRequiredInvariantLoad( |
| 3489 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3490 | } |
| 3491 | |
| 3492 | MinMaxAliasGroups.emplace_back(); |
| 3493 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3494 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3495 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3496 | |
| 3497 | bool Valid; |
| 3498 | |
| 3499 | Valid = |
| 3500 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3501 | |
| 3502 | if (!Valid) |
| 3503 | return false; |
| 3504 | |
| 3505 | // Bail out if the number of values we need to compare is too large. |
| 3506 | // This is important as the number of comparisons grows quadratically with |
| 3507 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3508 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3509 | RunTimeChecksMaxArraysPerGroup) |
| 3510 | return false; |
| 3511 | |
| 3512 | Valid = |
| 3513 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3514 | |
| 3515 | if (!Valid) |
| 3516 | return false; |
| 3517 | |
| 3518 | return true; |
| 3519 | } |
| 3520 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3521 | /// 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] | 3522 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3523 | // Start with the smallest loop containing the entry and expand that |
| 3524 | // loop until it contains all blocks in the region. If there is a loop |
| 3525 | // containing all blocks in the region check if it is itself contained |
| 3526 | // and if so take the parent loop as it will be the smallest containing |
| 3527 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3528 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3529 | while (L) { |
| 3530 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3531 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3532 | AllContained &= L->contains(BB); |
| 3533 | if (AllContained) |
| 3534 | break; |
| 3535 | L = L->getParentLoop(); |
| 3536 | } |
| 3537 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3538 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3541 | int Scop::NextScopID = 0; |
| 3542 | |
| 3543 | std::string Scop::CurrentFunc = ""; |
| 3544 | |
| 3545 | int Scop::getNextID(std::string ParentFunc) { |
| 3546 | if (ParentFunc != CurrentFunc) { |
| 3547 | CurrentFunc = ParentFunc; |
| 3548 | NextScopID = 0; |
| 3549 | } |
| 3550 | return NextScopID++; |
| 3551 | } |
| 3552 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3553 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3554 | ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 35bdcaf | 2017-05-15 13:43:01 +0000 | [diff] [blame] | 3555 | : SE(&ScalarEvolution), R(R), name(R.getNameStr()), IsOptimized(false), |
Siddharth Bhat | 47c7237 | 2017-07-05 15:07:28 +0000 | [diff] [blame] | 3556 | HasSingleExitEdge(R.getExitingBlock()), HasErrorBlock(false), |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3557 | MaxLoopDepth(0), CopyStmtsNum(0), SkipScop(false), DC(DC), ORE(ORE), |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 3558 | IslCtx(isl_ctx_alloc(), isl_ctx_free), Context(nullptr), |
| 3559 | Affinator(this, LI), AssumedContext(nullptr), InvalidContext(nullptr), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3560 | Schedule(nullptr), |
| 3561 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3562 | if (IslOnErrorAbort) |
| 3563 | isl_options_set_on_error(getIslCtx(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3564 | buildContext(); |
| 3565 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3566 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3567 | void Scop::foldSizeConstantsToRight() { |
| 3568 | isl_union_set *Accessed = isl_union_map_range(getAccesses()); |
| 3569 | |
| 3570 | for (auto Array : arrays()) { |
| 3571 | if (Array->getNumberOfDimensions() <= 1) |
| 3572 | continue; |
| 3573 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3574 | isl_space *Space = Array->getSpace().release(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3575 | |
| 3576 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3577 | |
| 3578 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3579 | isl_space_free(Space); |
| 3580 | continue; |
| 3581 | } |
| 3582 | |
| 3583 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3584 | |
| 3585 | isl_map *Transform = |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3586 | isl_map_universe(isl_space_map_from_set(Array->getSpace().release())); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3587 | |
| 3588 | std::vector<int> Int; |
| 3589 | |
| 3590 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3591 | for (int i = 0; i < Dims; i++) { |
| 3592 | isl_set *DimOnly = |
| 3593 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3594 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3595 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3596 | |
| 3597 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3598 | |
| 3599 | if (i == Dims - 1) { |
| 3600 | Int.push_back(1); |
| 3601 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3602 | isl_basic_set_free(DimHull); |
| 3603 | continue; |
| 3604 | } |
| 3605 | |
| 3606 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3607 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3608 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3609 | isl_aff_free(Diff); |
| 3610 | |
| 3611 | int ValInt = 1; |
| 3612 | |
| 3613 | if (isl_val_is_int(Val)) |
| 3614 | ValInt = isl_val_get_num_si(Val); |
| 3615 | isl_val_free(Val); |
| 3616 | |
| 3617 | Int.push_back(ValInt); |
| 3618 | |
| 3619 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3620 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3621 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3622 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3623 | Transform = isl_map_add_constraint(Transform, C); |
| 3624 | isl_basic_set_free(DimHull); |
| 3625 | continue; |
| 3626 | } |
| 3627 | |
| 3628 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3629 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3630 | |
| 3631 | int ValInt = 1; |
| 3632 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3633 | ValInt = 0; |
| 3634 | } |
| 3635 | |
| 3636 | Int.push_back(ValInt); |
| 3637 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3638 | isl_basic_set_free(DimHull); |
| 3639 | isl_basic_set_free(ZeroSet); |
| 3640 | } |
| 3641 | |
| 3642 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3643 | |
| 3644 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3645 | isl_set_free(Elements); |
| 3646 | isl_set_free(MappedElements); |
| 3647 | isl_map_free(Transform); |
| 3648 | continue; |
| 3649 | } |
| 3650 | |
| 3651 | isl_set_free(MappedElements); |
| 3652 | |
| 3653 | bool CanFold = true; |
| 3654 | |
| 3655 | if (Int[0] <= 1) |
| 3656 | CanFold = false; |
| 3657 | |
| 3658 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3659 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3660 | if (Int[0] != Int[i] && Int[i]) |
| 3661 | CanFold = false; |
| 3662 | |
| 3663 | if (!CanFold) { |
| 3664 | isl_set_free(Elements); |
| 3665 | isl_map_free(Transform); |
| 3666 | continue; |
| 3667 | } |
| 3668 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3669 | for (auto &Access : AccessFunctions) |
| 3670 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3671 | Access->setAccessRelation(Access->getAccessRelation().apply_range( |
| 3672 | isl::manage(isl_map_copy(Transform)))); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3673 | |
| 3674 | isl_map_free(Transform); |
| 3675 | |
| 3676 | std::vector<const SCEV *> Sizes; |
| 3677 | for (unsigned i = 0; i < NumDims; i++) { |
| 3678 | auto Size = Array->getDimensionSize(i); |
| 3679 | |
| 3680 | if (i == NumDims - 1) |
| 3681 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3682 | Sizes.push_back(Size); |
| 3683 | } |
| 3684 | |
| 3685 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3686 | |
| 3687 | isl_set_free(Elements); |
| 3688 | } |
| 3689 | isl_union_set_free(Accessed); |
| 3690 | return; |
| 3691 | } |
| 3692 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3693 | void Scop::markFortranArrays() { |
| 3694 | for (ScopStmt &Stmt : Stmts) { |
| 3695 | for (MemoryAccess *MemAcc : Stmt) { |
| 3696 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3697 | if (!FAD) |
| 3698 | continue; |
| 3699 | |
| 3700 | // TODO: const_cast-ing to edit |
| 3701 | ScopArrayInfo *SAI = |
| 3702 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3703 | assert(SAI && "memory access into a Fortran array does not " |
| 3704 | "have an associated ScopArrayInfo"); |
| 3705 | SAI->applyAndSetFAD(FAD); |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3710 | void Scop::finalizeAccesses() { |
| 3711 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3712 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3713 | foldAccessRelations(); |
| 3714 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3715 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3718 | Scop::~Scop() { |
| 3719 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 3720 | isl_set_free(AssumedContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3721 | isl_set_free(InvalidContext); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3722 | isl_schedule_free(Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3723 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 3724 | for (auto &It : ParameterIds) |
| 3725 | isl_id_free(It.second); |
| 3726 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3727 | for (auto &AS : RecordedAssumptions) |
| 3728 | isl_set_free(AS.Set); |
| 3729 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3730 | // Free the alias groups |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3731 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3732 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3733 | isl_pw_multi_aff_free(MMA.first); |
| 3734 | isl_pw_multi_aff_free(MMA.second); |
| 3735 | } |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3736 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3737 | isl_pw_multi_aff_free(MMA.first); |
| 3738 | isl_pw_multi_aff_free(MMA.second); |
| 3739 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3740 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3741 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3742 | for (const auto &IAClass : InvariantEquivClasses) |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3743 | isl_set_free(IAClass.ExecutionContext); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3744 | |
| 3745 | // Explicitly release all Scop objects and the underlying isl objects before |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3746 | // we release the isl context. |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3747 | Stmts.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3748 | ScopArrayInfoSet.clear(); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3749 | ScopArrayInfoMap.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3750 | ScopArrayNameMap.clear(); |
Roman Gareev | e2ee79a | 2016-08-21 11:09:19 +0000 | [diff] [blame] | 3751 | AccessFunctions.clear(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3754 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3755 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3756 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3757 | for (ScopStmt &Stmt : *this) |
| 3758 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3759 | if (!Access->isArrayKind()) |
| 3760 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3761 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3762 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3763 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3764 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3765 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3766 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3767 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3768 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3769 | DivisibleSize /= 2; |
| 3770 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3771 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3774 | for (auto &Stmt : *this) |
| 3775 | for (auto &Access : Stmt) |
| 3776 | Access->updateDimensionality(); |
| 3777 | } |
| 3778 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3779 | void Scop::foldAccessRelations() { |
| 3780 | for (auto &Stmt : *this) |
| 3781 | for (auto &Access : Stmt) |
| 3782 | Access->foldAccessRelation(); |
| 3783 | } |
| 3784 | |
| 3785 | void Scop::assumeNoOutOfBounds() { |
| 3786 | for (auto &Stmt : *this) |
| 3787 | for (auto &Access : Stmt) |
| 3788 | Access->assumeNoOutOfBound(); |
| 3789 | } |
| 3790 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3791 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
| 3792 | if (Stmt.isRegionStmt()) |
| 3793 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) |
| 3794 | StmtMap.erase(BB); |
| 3795 | else |
| 3796 | StmtMap.erase(Stmt.getBasicBlock()); |
| 3797 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3798 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3799 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete) { |
| 3800 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3801 | if (!ShouldDelete(*StmtIt)) { |
| 3802 | StmtIt++; |
| 3803 | continue; |
| 3804 | } |
| 3805 | |
| 3806 | removeFromStmtMap(*StmtIt); |
| 3807 | StmtIt = Stmts.erase(StmtIt); |
| 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | void Scop::removeStmtNotInDomainMap() { |
| 3812 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Tobias Grosser | 199ec4a | 2017-07-19 16:31:10 +0000 | [diff] [blame] | 3813 | return !this->DomainMap.lookup(Stmt.getEntryBlock()); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3814 | }; |
| 3815 | removeStmts(ShouldDelete); |
| 3816 | } |
| 3817 | |
| 3818 | void Scop::simplifySCoP(bool AfterHoisting) { |
| 3819 | |
| 3820 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3821 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3822 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3823 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3824 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3825 | bool OnlyRead = true; |
| 3826 | for (MemoryAccess *MA : Stmt) { |
| 3827 | if (MA->isRead()) |
| 3828 | continue; |
| 3829 | |
| 3830 | OnlyRead = false; |
| 3831 | break; |
| 3832 | } |
| 3833 | |
| 3834 | RemoveStmt = OnlyRead; |
| 3835 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3836 | return RemoveStmt; |
| 3837 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3838 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3839 | removeStmts(ShouldDelete); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3842 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3843 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3844 | if (!LInst) |
| 3845 | return nullptr; |
| 3846 | |
| 3847 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3848 | LInst = cast<LoadInst>(Rep); |
| 3849 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3850 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3851 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3852 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3853 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3854 | continue; |
| 3855 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3856 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3857 | for (auto *MA : MAs) |
| 3858 | if (MA->getAccessInstruction() == Val) |
| 3859 | return &IAClass; |
| 3860 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3861 | |
| 3862 | return nullptr; |
| 3863 | } |
| 3864 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3865 | /// Check if @p MA can always be hoisted without execution context. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3866 | static bool canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3867 | bool MAInvalidCtxIsEmpty, |
| 3868 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3869 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3870 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
| 3871 | // TODO: We can provide more information for better but more expensive |
| 3872 | // results. |
| 3873 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3874 | LInst->getAlignment(), DL)) |
| 3875 | return false; |
| 3876 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3877 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3878 | // |
| 3879 | // TODO: This is probably to conservative. |
| 3880 | if (!NonHoistableCtxIsEmpty) |
| 3881 | return false; |
| 3882 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3883 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3884 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3885 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3886 | return true; |
| 3887 | |
| 3888 | // 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] | 3889 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3890 | // statement domain. |
| 3891 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3892 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3893 | return false; |
| 3894 | return true; |
| 3895 | } |
| 3896 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3897 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3898 | |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3899 | if (InvMAs.empty()) |
| 3900 | return; |
| 3901 | |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3902 | auto *StmtInvalidCtx = Stmt.getInvalidContext(); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3903 | bool StmtInvalidCtxIsEmpty = isl_set_is_empty(StmtInvalidCtx); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3904 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3905 | // Get the context under which the statement is executed but remove the error |
| 3906 | // context under which this statement is reached. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3907 | isl_set *DomainCtx = isl_set_params(Stmt.getDomain()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3908 | DomainCtx = isl_set_subtract(DomainCtx, StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3909 | |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 3910 | if (isl_set_n_basic_set(DomainCtx) >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3911 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3912 | invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3913 | isl_set_free(DomainCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3914 | for (auto &InvMA : InvMAs) |
| 3915 | isl_set_free(InvMA.NonHoistableCtx); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3916 | return; |
| 3917 | } |
| 3918 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3919 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3920 | // we could have cyclic dependences on the constraints under which the |
| 3921 | // hoisted loads are executed and we could not determine an order in which to |
| 3922 | // pre-load them. This happens because not only lower bounds are part of the |
| 3923 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3924 | for (auto &InvMA : InvMAs) { |
| 3925 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3926 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3927 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3928 | SetVector<Value *> Values; |
| 3929 | for (const SCEV *Parameter : Parameters) { |
| 3930 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3931 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3932 | if (!Values.count(AccInst)) |
| 3933 | continue; |
| 3934 | |
| 3935 | if (isl_id *ParamId = getIdForParam(Parameter)) { |
| 3936 | int Dim = isl_set_find_dim_by_id(DomainCtx, isl_dim_param, ParamId); |
Tobias Grosser | b58ed8d | 2017-03-17 09:02:53 +0000 | [diff] [blame] | 3937 | if (Dim >= 0) |
| 3938 | DomainCtx = isl_set_eliminate(DomainCtx, isl_dim_param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3939 | isl_id_free(ParamId); |
| 3940 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3941 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3942 | } |
| 3943 | } |
| 3944 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3945 | for (auto &InvMA : InvMAs) { |
| 3946 | auto *MA = InvMA.MA; |
| 3947 | auto *NHCtx = InvMA.NonHoistableCtx; |
| 3948 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3949 | // Check for another invariant access that accesses the same location as |
| 3950 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3951 | // class at the end of InvariantEquivClasses. |
| 3952 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3953 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3954 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3955 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 3956 | auto *MAInvalidCtx = MA->getInvalidContext().release(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3957 | bool NonHoistableCtxIsEmpty = isl_set_is_empty(NHCtx); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3958 | bool MAInvalidCtxIsEmpty = isl_set_is_empty(MAInvalidCtx); |
| 3959 | |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3960 | isl_set *MACtx; |
| 3961 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3962 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3963 | NonHoistableCtxIsEmpty)) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3964 | MACtx = isl_set_universe(isl_set_get_space(DomainCtx)); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3965 | isl_set_free(MAInvalidCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3966 | isl_set_free(NHCtx); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3967 | } else { |
| 3968 | MACtx = isl_set_copy(DomainCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3969 | MACtx = isl_set_subtract(MACtx, isl_set_union(MAInvalidCtx, NHCtx)); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3970 | MACtx = isl_set_gist_params(MACtx, getContext()); |
| 3971 | } |
| 3972 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3973 | bool Consolidated = false; |
| 3974 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3975 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3976 | continue; |
| 3977 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3978 | // If the pointer and the type is equal check if the access function wrt. |
| 3979 | // to the domain is equal too. It can happen that the domain fixes |
| 3980 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3981 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3982 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3983 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3984 | if (!MAs.empty()) { |
| 3985 | auto *LastMA = MAs.front(); |
| 3986 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 3987 | auto *AR = isl_map_range(MA->getAccessRelation().release()); |
| 3988 | auto *LastAR = isl_map_range(LastMA->getAccessRelation().release()); |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3989 | bool SameAR = isl_set_is_equal(AR, LastAR); |
| 3990 | isl_set_free(AR); |
| 3991 | isl_set_free(LastAR); |
| 3992 | |
| 3993 | if (!SameAR) |
| 3994 | continue; |
| 3995 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3996 | |
| 3997 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3998 | MAs.push_front(MA); |
| 3999 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 4000 | Consolidated = true; |
| 4001 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4002 | // Unify the execution context of the class and this statement. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4003 | isl_set *&IAClassDomainCtx = IAClass.ExecutionContext; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 4004 | if (IAClassDomainCtx) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 4005 | IAClassDomainCtx = |
| 4006 | isl_set_coalesce(isl_set_union(IAClassDomainCtx, MACtx)); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 4007 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 4008 | IAClassDomainCtx = MACtx; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4009 | break; |
| 4010 | } |
| 4011 | |
| 4012 | if (Consolidated) |
| 4013 | continue; |
| 4014 | |
| 4015 | // If we did not consolidate MA, thus did not find an equivalence class |
| 4016 | // for it, we create a new one. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4017 | InvariantEquivClasses.emplace_back( |
| 4018 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList{MA}, MACtx, Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | isl_set_free(DomainCtx); |
| 4022 | } |
| 4023 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 4024 | /// Check if an access range is too complex. |
| 4025 | /// |
| 4026 | /// An access range is too complex, if it contains either many disjuncts or |
| 4027 | /// very complex expressions. As a simple heuristic, we assume if a set to |
| 4028 | /// be too complex if the sum of existentially quantified dimensions and |
| 4029 | /// set dimensions is larger than a threshold. This reliably detects both |
| 4030 | /// sets with many disjuncts as well as sets with many divisions as they |
| 4031 | /// arise in h264. |
| 4032 | /// |
| 4033 | /// @param AccessRange The range to check for complexity. |
| 4034 | /// |
| 4035 | /// @returns True if the access range is too complex. |
| 4036 | static bool isAccessRangeTooComplex(isl::set AccessRange) { |
| 4037 | unsigned NumTotalDims = 0; |
| 4038 | |
| 4039 | auto CountDimensions = [&NumTotalDims](isl::basic_set BSet) -> isl::stat { |
| 4040 | NumTotalDims += BSet.dim(isl::dim::div); |
| 4041 | NumTotalDims += BSet.dim(isl::dim::set); |
| 4042 | return isl::stat::ok; |
| 4043 | }; |
| 4044 | |
| 4045 | AccessRange.foreach_basic_set(CountDimensions); |
| 4046 | |
| 4047 | if (NumTotalDims > MaxDimensionsInAccessRange) |
| 4048 | return true; |
| 4049 | |
| 4050 | return false; |
| 4051 | } |
| 4052 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4053 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4054 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 4055 | // zero iterators, are by construction invariant, though we |
| 4056 | // currently "hoist" them anyway. This is necessary because we allow |
| 4057 | // them to be treated as parameters (e.g., in conditions) and our code |
| 4058 | // generation would otherwise use the old value. |
| 4059 | |
| 4060 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 4061 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4062 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 4063 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 4064 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4065 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4066 | |
| 4067 | // Skip accesses that have an invariant base pointer which is defined but |
| 4068 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 4069 | // returns a pointer that is used as a base address. However, as we want |
| 4070 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 4071 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 4072 | // that has a base pointer origin has a base pointer that is loaded and |
| 4073 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 4074 | // no base pointer origin we check that the base pointer is defined |
| 4075 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4076 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 4077 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4078 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4079 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 4080 | isl::map AccessRelation = give(Access->getAccessRelation().release()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4081 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4082 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4083 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4084 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4085 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4086 | AccessRelation = AccessRelation.intersect_domain(give(Stmt.getDomain())); |
| 4087 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4088 | |
| 4089 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 4090 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 4091 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4092 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4093 | } else if (BB != LI->getParent()) { |
| 4094 | // Skip accesses in non-affine subregions as they might not be executed |
| 4095 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4096 | return nullptr; |
| 4097 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4098 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4099 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4100 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 4101 | if (isAccessRangeTooComplex(AccessRelation.range())) |
| 4102 | return nullptr; |
| 4103 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4104 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 4105 | isl::set WrittenCtx = Written.params(); |
| 4106 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4107 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4108 | if (!IsWritten) |
| 4109 | return WrittenCtx; |
| 4110 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4111 | WrittenCtx = WrittenCtx.remove_divs(); |
| 4112 | bool TooComplex = |
| 4113 | isl_set_n_basic_set(WrittenCtx.get()) >= MaxDisjunctsInDomain; |
| 4114 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4115 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4116 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4117 | addAssumption(INVARIANTLOAD, WrittenCtx.copy(), LI->getDebugLoc(), |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4118 | AS_RESTRICTION, LI->getParent()); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4119 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4122 | void Scop::verifyInvariantLoads() { |
| 4123 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4124 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4125 | assert(LI && contains(LI)); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4126 | ScopStmt *Stmt = getStmtFor(LI); |
Tobias Grosser | 949e8c6 | 2015-12-21 07:10:39 +0000 | [diff] [blame] | 4127 | if (Stmt && Stmt->getArrayAccessOrNULLFor(LI)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4128 | invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4129 | return; |
| 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4134 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4135 | if (!PollyInvariantLoadHoisting) |
| 4136 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4137 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4138 | isl::union_map Writes = give(getWrites()); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4139 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4140 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4141 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4142 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4143 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
| 4144 | InvariantAccesses.push_back({Access, NHCtx.release()}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4145 | |
| 4146 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 4147 | for (auto InvMA : InvariantAccesses) |
| 4148 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4149 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4150 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4153 | /// Find the canonical scop array info object for a set of invariant load |
| 4154 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 4155 | /// first load in the list of accesses which is used as base pointer of a |
| 4156 | /// scop array. |
| 4157 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 4158 | MemoryAccessList &Accesses) { |
| 4159 | for (MemoryAccess *Access : Accesses) { |
| 4160 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 4161 | Access->getAccessInstruction(), MemoryKind::Array); |
| 4162 | if (CanonicalArray) |
| 4163 | return CanonicalArray; |
| 4164 | } |
| 4165 | return nullptr; |
| 4166 | } |
| 4167 | |
| 4168 | /// Check if @p Array severs as base array in an invariant load. |
| 4169 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 4170 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 4171 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 4172 | if (Access2->getScopArrayInfo() == Array) |
| 4173 | return true; |
| 4174 | return false; |
| 4175 | } |
| 4176 | |
| 4177 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 4178 | /// with a reference to @p New. |
| 4179 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 4180 | const ScopArrayInfo *New) { |
| 4181 | for (ScopStmt &Stmt : *S) |
| 4182 | for (MemoryAccess *Access : Stmt) { |
| 4183 | if (Access->getLatestScopArrayInfo() != Old) |
| 4184 | continue; |
| 4185 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 4186 | isl::id Id = New->getBasePtrId(); |
| 4187 | isl::map Map = Access->getAccessRelation(); |
| 4188 | Map = Map.set_tuple_id(isl::dim::out, Id); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4189 | Access->setAccessRelation(Map); |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | void Scop::canonicalizeDynamicBasePtrs() { |
| 4194 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 4195 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 4196 | |
| 4197 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 4198 | findCanonicalArray(this, BasePtrAccesses); |
| 4199 | |
| 4200 | if (!CanonicalBasePtrSAI) |
| 4201 | continue; |
| 4202 | |
| 4203 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 4204 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 4205 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 4206 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 4207 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 4208 | continue; |
| 4209 | |
| 4210 | // we currently do not canonicalize arrays where some accesses are |
| 4211 | // hoisted as invariant loads. If we would, we need to update the access |
| 4212 | // function of the invariant loads as well. However, as this is not a |
| 4213 | // very common situation, we leave this for now to avoid further |
| 4214 | // complexity increases. |
| 4215 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 4216 | continue; |
| 4217 | |
| 4218 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4223 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 4224 | ArrayRef<const SCEV *> Sizes, |
| 4225 | MemoryKind Kind, |
| 4226 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4227 | assert((BasePtr || BaseName) && |
| 4228 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 4229 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 4230 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 4231 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4232 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4233 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 4234 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4235 | DL, this, BaseName)); |
| 4236 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4237 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4238 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 4239 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 4240 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4241 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4242 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4243 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4244 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4247 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 4248 | const std::string &BaseName, |
| 4249 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4250 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4251 | std::vector<const SCEV *> SCEVSizes; |
| 4252 | |
| 4253 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4254 | if (size) |
| 4255 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4256 | else |
| 4257 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4258 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4259 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4260 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4261 | return SAI; |
| 4262 | } |
| 4263 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4264 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4265 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4266 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4267 | return SAI; |
| 4268 | } |
| 4269 | |
| 4270 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4271 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4272 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4273 | return SAI; |
| 4274 | } |
| 4275 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 4276 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4277 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4278 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4279 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4280 | return stringFromIslObj(AssumedContext); |
| 4281 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4282 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4283 | std::string Scop::getInvalidContextStr() const { |
| 4284 | return stringFromIslObj(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4285 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4286 | |
| 4287 | std::string Scop::getNameStr() const { |
| 4288 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4289 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4290 | return EntryName + "---" + ExitName; |
| 4291 | } |
| 4292 | |
| 4293 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4294 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4295 | raw_string_ostream ExitStr(ExitName); |
| 4296 | raw_string_ostream EntryStr(EntryName); |
| 4297 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4298 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4299 | EntryStr.str(); |
| 4300 | |
| 4301 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4302 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4303 | ExitStr.str(); |
| 4304 | } else |
| 4305 | ExitName = "FunctionExit"; |
| 4306 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4307 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 4310 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4311 | __isl_give isl_space *Scop::getParamSpace() const { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 4312 | return isl_set_get_space(Context); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4315 | __isl_give isl_set *Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4316 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4317 | return isl_set_copy(AssumedContext); |
| 4318 | } |
| 4319 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4320 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4321 | if (PollyProcessUnprofitable) |
| 4322 | return true; |
| 4323 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4324 | if (isEmpty()) |
| 4325 | return false; |
| 4326 | |
| 4327 | unsigned OptimizableStmtsOrLoops = 0; |
| 4328 | for (auto &Stmt : *this) { |
| 4329 | if (Stmt.getNumIterators() == 0) |
| 4330 | continue; |
| 4331 | |
| 4332 | bool ContainsArrayAccs = false; |
| 4333 | bool ContainsScalarAccs = false; |
| 4334 | for (auto *MA : Stmt) { |
| 4335 | if (MA->isRead()) |
| 4336 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4337 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4338 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4341 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4342 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4343 | } |
| 4344 | |
| 4345 | return OptimizableStmtsOrLoops > 1; |
| 4346 | } |
| 4347 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4348 | bool Scop::hasFeasibleRuntimeContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4349 | auto *PositiveContext = getAssumedContext(); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4350 | auto *NegativeContext = getInvalidContext(); |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4351 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 4352 | bool IsFeasible = !(isl_set_is_empty(PositiveContext) || |
| 4353 | isl_set_is_subset(PositiveContext, NegativeContext)); |
| 4354 | isl_set_free(PositiveContext); |
| 4355 | if (!IsFeasible) { |
| 4356 | isl_set_free(NegativeContext); |
| 4357 | return false; |
| 4358 | } |
| 4359 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4360 | auto *DomainContext = isl_union_set_params(getDomains()); |
| 4361 | IsFeasible = !isl_set_is_subset(DomainContext, NegativeContext); |
Johannes Doerfert | fb72187 | 2016-04-12 17:54:29 +0000 | [diff] [blame] | 4362 | IsFeasible &= !isl_set_is_subset(Context, NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4363 | isl_set_free(NegativeContext); |
| 4364 | isl_set_free(DomainContext); |
| 4365 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4366 | return IsFeasible; |
| 4367 | } |
| 4368 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4369 | static std::string toString(AssumptionKind Kind) { |
| 4370 | switch (Kind) { |
| 4371 | case ALIASING: |
| 4372 | return "No-aliasing"; |
| 4373 | case INBOUNDS: |
| 4374 | return "Inbounds"; |
| 4375 | case WRAPPING: |
| 4376 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4377 | case UNSIGNED: |
| 4378 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4379 | case COMPLEXITY: |
| 4380 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4381 | case PROFITABLE: |
| 4382 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4383 | case ERRORBLOCK: |
| 4384 | return "No-error"; |
| 4385 | case INFINITELOOP: |
| 4386 | return "Finite loop"; |
| 4387 | case INVARIANTLOAD: |
| 4388 | return "Invariant load"; |
| 4389 | case DELINEARIZATION: |
| 4390 | return "Delinearization"; |
| 4391 | } |
| 4392 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4393 | } |
| 4394 | |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4395 | bool Scop::isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign) { |
| 4396 | if (Sign == AS_ASSUMPTION) { |
| 4397 | if (isl_set_is_subset(Context, Set)) |
| 4398 | return false; |
| 4399 | |
| 4400 | if (isl_set_is_subset(AssumedContext, Set)) |
| 4401 | return false; |
| 4402 | } else { |
| 4403 | if (isl_set_is_disjoint(Set, Context)) |
| 4404 | return false; |
| 4405 | |
| 4406 | if (isl_set_is_subset(Set, InvalidContext)) |
| 4407 | return false; |
| 4408 | } |
| 4409 | return true; |
| 4410 | } |
| 4411 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4412 | bool Scop::trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4413 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4414 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4415 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4416 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4417 | // Do never emit trivial assumptions as they only clutter the output. |
| 4418 | if (!PollyRemarksMinimal) { |
| 4419 | isl_set *Univ = nullptr; |
| 4420 | if (Sign == AS_ASSUMPTION) |
| 4421 | Univ = isl_set_universe(isl_set_get_space(Set)); |
| 4422 | |
| 4423 | bool IsTrivial = (Sign == AS_RESTRICTION && isl_set_is_empty(Set)) || |
| 4424 | (Sign == AS_ASSUMPTION && isl_set_is_equal(Univ, Set)); |
| 4425 | isl_set_free(Univ); |
| 4426 | |
| 4427 | if (IsTrivial) |
| 4428 | return false; |
| 4429 | } |
| 4430 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4431 | switch (Kind) { |
| 4432 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4433 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4434 | break; |
| 4435 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4436 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4437 | break; |
| 4438 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4439 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4440 | break; |
| 4441 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4442 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4443 | break; |
| 4444 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4445 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4446 | break; |
| 4447 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4448 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4449 | break; |
| 4450 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4451 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4452 | break; |
| 4453 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4454 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4455 | break; |
| 4456 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4457 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4460 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4461 | break; |
| 4462 | } |
| 4463 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4464 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
| 4465 | std::string Msg = toString(Kind) + Suffix + stringFromIslObj(Set); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4466 | if (BB) |
| 4467 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 4468 | << Msg); |
| 4469 | else |
| 4470 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 4471 | R.getEntry()) |
| 4472 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4473 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | void Scop::addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4477 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4478 | // Simplify the assumptions/restrictions first. |
| 4479 | Set = isl_set_gist_params(Set, getContext()); |
| 4480 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4481 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4482 | isl_set_free(Set); |
| 4483 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4486 | if (Sign == AS_ASSUMPTION) { |
| 4487 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
| 4488 | AssumedContext = isl_set_coalesce(AssumedContext); |
| 4489 | } else { |
| 4490 | InvalidContext = isl_set_union(InvalidContext, Set); |
| 4491 | InvalidContext = isl_set_coalesce(InvalidContext); |
| 4492 | } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4495 | void Scop::recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4496 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4497 | assert((isl_set_is_params(Set) || BB) && |
| 4498 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4499 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | void Scop::addRecordedAssumptions() { |
| 4503 | while (!RecordedAssumptions.empty()) { |
| 4504 | const Assumption &AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4505 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4506 | if (!AS.BB) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4507 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign, nullptr /* BasicBlock */); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4508 | continue; |
| 4509 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4510 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4511 | // If the domain was deleted the assumptions are void. |
| 4512 | isl_set *Dom = getDomainConditions(AS.BB); |
| 4513 | if (!Dom) { |
| 4514 | isl_set_free(AS.Set); |
| 4515 | continue; |
| 4516 | } |
| 4517 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4518 | // If a basic block was given use its domain to simplify the assumption. |
| 4519 | // In case of restrictions we know they only have to hold on the domain, |
| 4520 | // thus we can intersect them with the domain of the block. However, for |
| 4521 | // assumptions the domain has to imply them, thus: |
| 4522 | // _ _____ |
| 4523 | // Dom => S <==> A v B <==> A - B |
| 4524 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4525 | // 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] | 4526 | // assumption. |
| 4527 | isl_set *S = AS.Set; |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4528 | if (AS.Sign == AS_RESTRICTION) |
| 4529 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4530 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4531 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4532 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4533 | addAssumption(AS.Kind, S, AS.Loc, AS_RESTRICTION, AS.BB); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4537 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
| 4538 | addAssumption(Kind, isl_set_empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4541 | __isl_give isl_set *Scop::getInvalidContext() const { |
| 4542 | return isl_set_copy(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4545 | void Scop::printContext(raw_ostream &OS) const { |
| 4546 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4547 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4548 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4549 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4550 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4551 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4552 | OS.indent(4) << "Invalid Context:\n"; |
| 4553 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4554 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4555 | unsigned Dim = 0; |
| 4556 | for (const SCEV *Parameter : Parameters) |
| 4557 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4560 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4561 | int noOfGroups = 0; |
| 4562 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4563 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4564 | noOfGroups += 1; |
| 4565 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4566 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4569 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4570 | if (MinMaxAliasGroups.empty()) { |
| 4571 | OS.indent(8) << "n/a\n"; |
| 4572 | return; |
| 4573 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4574 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4575 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4576 | |
| 4577 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4578 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4579 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4580 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4581 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4582 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4583 | } |
| 4584 | OS << " ]]\n"; |
| 4585 | } |
| 4586 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4587 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4588 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4589 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4590 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4591 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4592 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4593 | } |
| 4594 | OS << " ]]\n"; |
| 4595 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4596 | } |
| 4597 | } |
| 4598 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4599 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4600 | OS << "Statements {\n"; |
| 4601 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4602 | for (const ScopStmt &Stmt : *this) { |
| 4603 | OS.indent(4); |
| 4604 | Stmt.print(OS, PrintInstructions); |
| 4605 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4606 | |
| 4607 | OS.indent(4) << "}\n"; |
| 4608 | } |
| 4609 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4610 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4611 | OS << "Arrays {\n"; |
| 4612 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4613 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4614 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4615 | |
| 4616 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4617 | |
| 4618 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4619 | |
| 4620 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4621 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4622 | |
| 4623 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4626 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4627 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4628 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4629 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4630 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4631 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4632 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4633 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4634 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4635 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4636 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4637 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4638 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4639 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4640 | } |
| 4641 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4642 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4643 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4644 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4645 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4648 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 4649 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4650 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4651 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 4652 | isl_ctx *Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4653 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4654 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4655 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4656 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4657 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4658 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4659 | // 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] | 4660 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4661 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4662 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4663 | // TODO: We could use a heuristic and either use: |
| 4664 | // SCEVAffinator::takeNonNegativeAssumption |
| 4665 | // or |
| 4666 | // SCEVAffinator::interpretAsUnsigned |
| 4667 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4668 | if (NonNegative) |
| 4669 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4670 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4671 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4672 | |
| 4673 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4674 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4675 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4678 | __isl_give isl_union_set *Scop::getDomains() const { |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4679 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx(), 0); |
| 4680 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4681 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4682 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4683 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4684 | |
| 4685 | return Domain; |
| 4686 | } |
| 4687 | |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4688 | __isl_give isl_pw_aff *Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
| 4689 | PWACtx PWAC = getPwAff(E, BB); |
| 4690 | isl_set_free(PWAC.second); |
| 4691 | return PWAC.first; |
| 4692 | } |
| 4693 | |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4694 | __isl_give isl_union_map * |
| 4695 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
| 4696 | isl_union_map *Accesses = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4697 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4698 | for (ScopStmt &Stmt : *this) { |
| 4699 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4700 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4701 | continue; |
| 4702 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4703 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 4704 | isl_map *AccessDomain = MA->getAccessRelation().release(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4705 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4706 | Accesses = isl_union_map_add_map(Accesses, AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4707 | } |
| 4708 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 4709 | |
| 4710 | return isl_union_map_coalesce(Accesses); |
| 4711 | |
| 4712 | for (auto X : this->getInvariantAccesses()) |
| 4713 | for (auto A : X.InvariantAccesses) { |
| 4714 | if (!Predicate(*A)) |
| 4715 | continue; |
| 4716 | Accesses = |
| 4717 | isl_union_map_add_map(Accesses, A->getAccessRelation().release()); |
| 4718 | } |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4719 | return isl_union_map_coalesce(Accesses); |
| 4720 | } |
| 4721 | |
| 4722 | __isl_give isl_union_map *Scop::getMustWrites() { |
| 4723 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | __isl_give isl_union_map *Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4727 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4730 | __isl_give isl_union_map *Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4731 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4735 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4738 | __isl_give isl_union_map *Scop::getAccesses() { |
| 4739 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4740 | } |
| 4741 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4742 | // Check whether @p Node is an extension node. |
| 4743 | // |
| 4744 | // @return true if @p Node is an extension node. |
| 4745 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4746 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4747 | return isl_bool_error; |
| 4748 | else |
| 4749 | return isl_bool_true; |
| 4750 | } |
| 4751 | |
| 4752 | bool Scop::containsExtensionNode(__isl_keep isl_schedule *Schedule) { |
| 4753 | return isl_schedule_foreach_schedule_node_top_down(Schedule, isNotExtNode, |
| 4754 | nullptr) == isl_stat_error; |
| 4755 | } |
| 4756 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4757 | __isl_give isl_union_map *Scop::getSchedule() const { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4758 | auto *Tree = getScheduleTree(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4759 | if (containsExtensionNode(Tree)) { |
| 4760 | isl_schedule_free(Tree); |
| 4761 | return nullptr; |
| 4762 | } |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4763 | auto *S = isl_schedule_get_map(Tree); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4764 | isl_schedule_free(Tree); |
| 4765 | return S; |
| 4766 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4767 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4768 | __isl_give isl_schedule *Scop::getScheduleTree() const { |
| 4769 | return isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 4770 | getDomains()); |
| 4771 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4772 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4773 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
| 4774 | auto *S = isl_schedule_from_domain(getDomains()); |
| 4775 | S = isl_schedule_insert_partial_schedule( |
| 4776 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 4777 | isl_schedule_free(Schedule); |
| 4778 | Schedule = S; |
| 4779 | } |
| 4780 | |
| 4781 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 4782 | isl_schedule_free(Schedule); |
| 4783 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 4787 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4788 | for (ScopStmt &Stmt : *this) { |
| 4789 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4790 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 4791 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 4792 | |
| 4793 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 4794 | isl_union_set_free(StmtDomain); |
| 4795 | isl_union_set_free(NewStmtDomain); |
| 4796 | continue; |
| 4797 | } |
| 4798 | |
| 4799 | Changed = true; |
| 4800 | |
| 4801 | isl_union_set_free(StmtDomain); |
| 4802 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 4803 | |
| 4804 | if (isl_union_set_is_empty(NewStmtDomain)) { |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4805 | Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace())); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4806 | isl_union_set_free(NewStmtDomain); |
| 4807 | } else |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4808 | Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4809 | } |
| 4810 | isl_union_set_free(Domain); |
| 4811 | return Changed; |
| 4812 | } |
| 4813 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4814 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4815 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4816 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4817 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4818 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4819 | // # Example: |
| 4820 | // |
| 4821 | // Domain: { A[i,j]; B[i,j,k] } |
| 4822 | // N: 1 |
| 4823 | // |
| 4824 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4825 | // |
| 4826 | // @param USet A union set describing the elements for which to generate a |
| 4827 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4828 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4829 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4830 | 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] | 4831 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4832 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4833 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4834 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4835 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4836 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4837 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4838 | int Dim = S.dim(isl::dim::set); |
| 4839 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4840 | N, Dim - N); |
| 4841 | if (N > 1) |
| 4842 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4843 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4844 | Result = Result.add_pw_multi_aff(PMA); |
| 4845 | return isl::stat::ok; |
| 4846 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4847 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4848 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4849 | (void)Res; |
| 4850 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4851 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4852 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4853 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4856 | void Scop::addScopStmt(BasicBlock *BB, Loop *SurroundingLoop, |
| 4857 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4858 | assert(BB && "Unexpected nullptr!"); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4859 | Stmts.emplace_back(*this, *BB, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4860 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4861 | StmtMap[BB].push_back(Stmt); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4864 | void Scop::addScopStmt(Region *R, Loop *SurroundingLoop) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4865 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4866 | Stmts.emplace_back(*this, *R, SurroundingLoop); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4867 | auto *Stmt = &Stmts.back(); |
| 4868 | for (BasicBlock *BB : R->blocks()) |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4869 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4872 | ScopStmt *Scop::addScopStmt(__isl_take isl_map *SourceRel, |
| 4873 | __isl_take isl_map *TargetRel, |
| 4874 | __isl_take isl_set *Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4875 | #ifndef NDEBUG |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4876 | isl_set *SourceDomain = isl_map_domain(isl_map_copy(SourceRel)); |
| 4877 | isl_set *TargetDomain = isl_map_domain(isl_map_copy(TargetRel)); |
| 4878 | assert(isl_set_is_subset(Domain, TargetDomain) && |
| 4879 | "Target access not defined for complete statement domain"); |
| 4880 | assert(isl_set_is_subset(Domain, SourceDomain) && |
| 4881 | "Source access not defined for complete statement domain"); |
| 4882 | isl_set_free(SourceDomain); |
| 4883 | isl_set_free(TargetDomain); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4884 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4885 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4886 | CopyStmtsNum++; |
| 4887 | return &(Stmts.back()); |
| 4888 | } |
| 4889 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4890 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4891 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4892 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4893 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4894 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4895 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4898 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4899 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4900 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4901 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4902 | /// |
| 4903 | /// Example: |
| 4904 | /// bb1(0) |
| 4905 | /// / \. |
| 4906 | /// bb2(1) bb3(2) |
| 4907 | /// \ / \. |
| 4908 | /// bb4(3) bb5(4) |
| 4909 | /// \ / |
| 4910 | /// bb6(5) |
| 4911 | /// |
| 4912 | /// Including loops requires additional processing. Whenever a loop header is |
| 4913 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4914 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4915 | /// this loop and complete the sequential schedule at this loop-level before |
| 4916 | /// processing about any other nodes. To implement this |
| 4917 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4918 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4919 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4920 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4921 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4922 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4923 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4924 | |
| 4925 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4926 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4927 | std::deque<RegionNode *> DelayList; |
| 4928 | bool LastRNWaiting = false; |
| 4929 | |
| 4930 | // Iterate over the region @p R in reverse post-order but queue |
| 4931 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4932 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4933 | // that we queued the last sub-region/block from the reverse post-order |
| 4934 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4935 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4936 | // the next queued sub-region/blocks. |
| 4937 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4938 | RegionNode *RN; |
| 4939 | |
| 4940 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.size() == 0) { |
| 4941 | RN = WorkList.front(); |
| 4942 | WorkList.pop_front(); |
| 4943 | LastRNWaiting = false; |
| 4944 | } else { |
| 4945 | RN = DelayList.front(); |
| 4946 | DelayList.pop_front(); |
| 4947 | } |
| 4948 | |
| 4949 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4950 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4951 | L = OuterScopLoop; |
| 4952 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4953 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4954 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4955 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4956 | LastRNWaiting = true; |
| 4957 | DelayList.push_back(RN); |
| 4958 | continue; |
| 4959 | } |
| 4960 | LoopStack.push_back({L, nullptr, 0}); |
| 4961 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4962 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | return; |
| 4966 | } |
| 4967 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4968 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4969 | |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4970 | if (RN->isSubRegion()) { |
| 4971 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4972 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4973 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4974 | return; |
| 4975 | } |
| 4976 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4977 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4978 | auto &LoopData = LoopStack.back(); |
| 4979 | LoopData.NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4980 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4981 | for (auto *Stmt : getStmtListFor(RN)) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4982 | auto *UDomain = isl_union_set_from_set(Stmt->getDomain()); |
| 4983 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4984 | LoopData.Schedule = combineInSequence(LoopData.Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4987 | // Check if we just processed the last node in this loop. If we did, finalize |
| 4988 | // the loop by: |
| 4989 | // |
| 4990 | // - adding new schedule dimensions |
| 4991 | // - folding the resulting schedule into the parent loop schedule |
| 4992 | // - dropping the loop schedule from the LoopStack. |
| 4993 | // |
| 4994 | // Then continue to check surrounding loops, which might also have been |
| 4995 | // completed by this node. |
| 4996 | while (LoopData.L && |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 4997 | LoopData.NumBlocksProcessed == getNumBlocksInLoop(LoopData.L)) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4998 | auto *Schedule = LoopData.Schedule; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4999 | auto NumBlocksProcessed = LoopData.NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 5000 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5001 | LoopStack.pop_back(); |
| 5002 | auto &NextLoopData = LoopStack.back(); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 5003 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5004 | if (Schedule) { |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 5005 | isl::union_set Domain = give(isl_schedule_get_domain(Schedule)); |
| 5006 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, LoopStack.size()); |
| 5007 | Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA.release()); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5008 | NextLoopData.Schedule = |
| 5009 | combineInSequence(NextLoopData.Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5010 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 5011 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5012 | NextLoopData.NumBlocksProcessed += NumBlocksProcessed; |
| 5013 | LoopData = NextLoopData; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 5014 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5017 | ScopStmt *Scop::getStmtFor(BasicBlock *BB) const { |
Tobias Grosser | 57411e3 | 2015-05-27 06:51:34 +0000 | [diff] [blame] | 5018 | auto StmtMapIt = StmtMap.find(BB); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 5019 | if (StmtMapIt == StmtMap.end()) |
| 5020 | return nullptr; |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 5021 | assert(StmtMapIt->second.size() == 1); |
| 5022 | return StmtMapIt->second.front(); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 5025 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 5026 | auto StmtMapIt = StmtMap.find(BB); |
| 5027 | if (StmtMapIt == StmtMap.end()) |
| 5028 | return {}; |
| 5029 | assert(StmtMapIt->second.size() == 1 && |
| 5030 | "Each statement corresponds to exactly one BB."); |
| 5031 | return StmtMapIt->second; |
| 5032 | } |
| 5033 | |
| 5034 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 5035 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
| 5036 | if (StmtList.size() > 0) |
| 5037 | return StmtList.back(); |
| 5038 | return nullptr; |
| 5039 | } |
| 5040 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5041 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5042 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5043 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 5044 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5047 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 5048 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5051 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5052 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5053 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5054 | // outermostLoopInRegion always returns nullptr for top level regions |
| 5055 | if (R.isTopLevelRegion()) { |
| 5056 | // LoopInfo's depths start at 1, we start at 0 |
| 5057 | return L->getLoopDepth() - 1; |
| 5058 | } else { |
| 5059 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 5060 | assert(OuterLoop); |
| 5061 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 5062 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 5065 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 5066 | for (auto &SAI : arrays()) { |
| 5067 | if (SAI->getName() == BaseName) |
| 5068 | return SAI; |
| 5069 | } |
| 5070 | return nullptr; |
| 5071 | } |
| 5072 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 5073 | void Scop::addAccessData(MemoryAccess *Access) { |
| 5074 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 5075 | assert(SAI && "can only use after access relations have been constructed"); |
| 5076 | |
| 5077 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 5078 | ValueUseAccs[SAI].push_back(Access); |
| 5079 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 5080 | PHIIncomingAccs[SAI].push_back(Access); |
| 5081 | } |
| 5082 | |
| 5083 | void Scop::removeAccessData(MemoryAccess *Access) { |
| 5084 | if (Access->isOriginalValueKind() && Access->isRead()) { |
| 5085 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
| 5086 | std::remove(Uses.begin(), Uses.end(), Access); |
| 5087 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 5088 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
| 5089 | std::remove(Incomings.begin(), Incomings.end(), Access); |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 5094 | assert(SAI->isValueKind()); |
| 5095 | |
| 5096 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 5097 | if (!Val) |
| 5098 | return nullptr; |
| 5099 | |
| 5100 | ScopStmt *Stmt = getStmtFor(Val); |
| 5101 | if (!Stmt) |
| 5102 | return nullptr; |
| 5103 | |
| 5104 | return Stmt->lookupValueWriteOf(Val); |
| 5105 | } |
| 5106 | |
| 5107 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 5108 | assert(SAI->isValueKind()); |
| 5109 | auto It = ValueUseAccs.find(SAI); |
| 5110 | if (It == ValueUseAccs.end()) |
| 5111 | return {}; |
| 5112 | return It->second; |
| 5113 | } |
| 5114 | |
| 5115 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 5116 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5117 | |
| 5118 | if (SAI->isExitPHIKind()) |
| 5119 | return nullptr; |
| 5120 | |
| 5121 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
| 5122 | ScopStmt *Stmt = getStmtFor(PHI); |
| 5123 | assert(Stmt && "PHINode must be within the SCoP"); |
| 5124 | |
| 5125 | return Stmt->lookupPHIReadOf(PHI); |
| 5126 | } |
| 5127 | |
| 5128 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 5129 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5130 | auto It = PHIIncomingAccs.find(SAI); |
| 5131 | if (It == PHIIncomingAccs.end()) |
| 5132 | return {}; |
| 5133 | return It->second; |
| 5134 | } |
| 5135 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 5136 | bool Scop::isEscaping(Instruction *Inst) { |
| 5137 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 5138 | "values defined inside the SCoP"); |
| 5139 | |
| 5140 | for (Use &Use : Inst->uses()) { |
| 5141 | BasicBlock *UserBB = getUseBlock(Use); |
| 5142 | if (!contains(UserBB)) |
| 5143 | return true; |
| 5144 | |
| 5145 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 5146 | // move to a new basic block such that its incoming blocks are not in the |
| 5147 | // SCoP anymore. |
| 5148 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 5149 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 5150 | return true; |
| 5151 | } |
| 5152 | return false; |
| 5153 | } |
| 5154 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5155 | raw_ostream &polly::operator<<(raw_ostream &O, const Scop &scop) { |
| 5156 | scop.print(O, PollyPrintInstructions); |
| 5157 | return O; |
| 5158 | } |
| 5159 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5160 | //===----------------------------------------------------------------------===// |
| 5161 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5162 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5163 | AU.addRequired<RegionInfoPass>(); |
| 5164 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5165 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5166 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5167 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5168 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5169 | AU.setPreservesAll(); |
| 5170 | } |
| 5171 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5172 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats) { |
| 5173 | NumLoopsInScop += Stats.NumLoops; |
| 5174 | MaxNumLoopsInScop = |
| 5175 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 5176 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5177 | if (Stats.MaxDepth == 1) |
| 5178 | NumScopsDepthOne++; |
| 5179 | else if (Stats.MaxDepth == 2) |
| 5180 | NumScopsDepthTwo++; |
| 5181 | else if (Stats.MaxDepth == 3) |
| 5182 | NumScopsDepthThree++; |
| 5183 | else if (Stats.MaxDepth == 4) |
| 5184 | NumScopsDepthFour++; |
| 5185 | else if (Stats.MaxDepth == 5) |
| 5186 | NumScopsDepthFive++; |
| 5187 | else |
| 5188 | NumScopsDepthLarger++; |
| 5189 | } |
| 5190 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5191 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5192 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5193 | |
| 5194 | if (!SD.isMaxRegionInScop(*R)) |
| 5195 | return false; |
| 5196 | |
| 5197 | Function *F = R->getEntry()->getParent(); |
| 5198 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5199 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5200 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5201 | auto const &DL = F->getParent()->getDataLayout(); |
| 5202 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5203 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5204 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5205 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5206 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5207 | |
| 5208 | if (S) { |
| 5209 | ScopDetection::LoopStats Stats = |
| 5210 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5211 | updateLoopCountStatistic(Stats); |
| 5212 | } |
| 5213 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5214 | return false; |
| 5215 | } |
| 5216 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5217 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5218 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5219 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5220 | else |
| 5221 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5222 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5223 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5224 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5225 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5226 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5227 | |
| 5228 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5229 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5230 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5231 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5232 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5233 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5234 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5235 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5236 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5237 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5238 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5239 | "Polly - Create polyhedral description of Scops", false, |
| 5240 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5241 | |
| 5242 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5243 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5244 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
| 5245 | AssumptionCache &AC) { |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5246 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5247 | /// function. |
| 5248 | for (auto &It : SD) { |
| 5249 | Region *R = const_cast<Region *>(It); |
| 5250 | if (!SD.isMaxRegionInScop(*R)) |
| 5251 | continue; |
| 5252 | |
| 5253 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
| 5254 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5255 | if (!S) |
| 5256 | continue; |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 5257 | ScopDetection::LoopStats Stats = |
| 5258 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5259 | updateLoopCountStatistic(Stats); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5260 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5261 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5262 | (void)Inserted; |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | AnalysisKey ScopInfoAnalysis::Key; |
| 5267 | |
| 5268 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5269 | FunctionAnalysisManager &FAM) { |
| 5270 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5271 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5272 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5273 | auto &AA = FAM.getResult<AAManager>(F); |
| 5274 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5275 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5276 | auto &DL = F.getParent()->getDataLayout(); |
| 5277 | return {DL, SD, SE, LI, AA, DT, AC}; |
| 5278 | } |
| 5279 | |
| 5280 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5281 | FunctionAnalysisManager &FAM) { |
| 5282 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
| 5283 | for (auto &It : SI) { |
| 5284 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5285 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5286 | else |
| 5287 | Stream << "Invalid Scop!\n"; |
| 5288 | } |
| 5289 | return PreservedAnalyses::all(); |
| 5290 | } |
| 5291 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5292 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5293 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5294 | AU.addRequired<RegionInfoPass>(); |
| 5295 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5296 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5297 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5298 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5299 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5300 | AU.setPreservesAll(); |
| 5301 | } |
| 5302 | |
| 5303 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5304 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5305 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5306 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5307 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5308 | auto const &DL = F.getParent()->getDataLayout(); |
| 5309 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5310 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5311 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5312 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5313 | return false; |
| 5314 | } |
| 5315 | |
| 5316 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5317 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5318 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5319 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5320 | else |
| 5321 | OS << "Invalid Scop!\n"; |
| 5322 | } |
| 5323 | } |
| 5324 | |
| 5325 | char ScopInfoWrapperPass::ID = 0; |
| 5326 | |
| 5327 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5328 | return new ScopInfoWrapperPass(); |
| 5329 | } |
| 5330 | |
| 5331 | INITIALIZE_PASS_BEGIN( |
| 5332 | ScopInfoWrapperPass, "polly-function-scops", |
| 5333 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5334 | false); |
| 5335 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5336 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5337 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5338 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5339 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5340 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5341 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5342 | INITIALIZE_PASS_END( |
| 5343 | ScopInfoWrapperPass, "polly-function-scops", |
| 5344 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5345 | false) |