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 | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 25 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 26 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DepthFirstIterator.h" |
Tobias Grosser | f4c24b2 | 2015-04-05 13:11:54 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/MapVector.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/PostOrderIterator.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/AliasAnalysis.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/LoopInfo.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/LoopIterator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/RegionIterator.h" |
| 40 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Johannes Doerfert | 48fe86f | 2015-11-12 02:32:32 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DiagnosticInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 43 | #include "isl/aff.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 44 | #include "isl/constraint.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 45 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 46 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 47 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 48 | #include "isl/printer.h" |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 49 | #include "isl/schedule.h" |
| 50 | #include "isl/schedule_node.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 51 | #include "isl/set.h" |
| 52 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 53 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 54 | #include "isl/val.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 55 | #include <sstream> |
| 56 | #include <string> |
| 57 | #include <vector> |
| 58 | |
| 59 | using namespace llvm; |
| 60 | using namespace polly; |
| 61 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 62 | #define DEBUG_TYPE "polly-scops" |
| 63 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 64 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 65 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 66 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 67 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 68 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 69 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 70 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 71 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 72 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 73 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 74 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 75 | "Number of delinearization assumptions taken."); |
| 76 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 77 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
| 78 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 79 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 80 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 81 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 82 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 83 | STATISTIC(NumScopsDepthLarger, |
| 84 | "Number of scops with maximal loop depth 6 and larger"); |
| 85 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 86 | |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 87 | // The maximal number of basic sets we allow during domain construction to |
| 88 | // be created. More complex scops will result in very high compile time and |
| 89 | // are also unlikely to result in good code |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 90 | static int const MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 91 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 92 | // The number of disjunct in the context after which we stop to add more |
| 93 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 94 | // number of disjunct when adding non-convex sets to the context. |
| 95 | static int const MaxDisjunctsInContext = 4; |
| 96 | |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 97 | static cl::opt<int> |
| 98 | OptComputeOut("polly-analysis-computeout", |
| 99 | cl::desc("Bound the scop analysis by a maximal amount of " |
| 100 | "computational steps (0 means no bound)"), |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 101 | cl::Hidden, cl::init(800000), cl::ZeroOrMore, |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 102 | cl::cat(PollyCategory)); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 103 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 104 | static cl::opt<bool> PollyRemarksMinimal( |
| 105 | "polly-remarks-minimal", |
| 106 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 107 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 108 | |
Johannes Doerfert | 9e7b17b | 2014-08-18 00:40:13 +0000 | [diff] [blame] | 109 | // Multiplicative reductions can be disabled separately as these kind of |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 110 | // operations can overflow easily. Additive reductions and bit operations |
| 111 | // are in contrast pretty stable. |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 112 | static cl::opt<bool> DisableMultiplicativeReductions( |
| 113 | "polly-disable-multiplicative-reductions", |
| 114 | cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore, |
| 115 | cl::init(false), cl::cat(PollyCategory)); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 116 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 117 | static cl::opt<int> RunTimeChecksMaxAccessDisjuncts( |
| 118 | "polly-rtc-max-array-disjuncts", |
| 119 | cl::desc("The maximal number of disjunts allowed in memory accesses to " |
| 120 | "to build RTCs."), |
| 121 | cl::Hidden, cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 122 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 123 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 124 | "polly-rtc-max-parameters", |
| 125 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 126 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 127 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 128 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 129 | "polly-rtc-max-arrays-per-group", |
| 130 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 131 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 132 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 133 | static cl::opt<std::string> UserContextStr( |
| 134 | "polly-context", cl::value_desc("isl parameter set"), |
| 135 | cl::desc("Provide additional constraints on the context parameters"), |
| 136 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 137 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 138 | static cl::opt<bool> DetectReductions("polly-detect-reductions", |
| 139 | cl::desc("Detect and exploit reductions"), |
| 140 | cl::Hidden, cl::ZeroOrMore, |
| 141 | cl::init(true), cl::cat(PollyCategory)); |
| 142 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 143 | static cl::opt<bool> |
| 144 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 145 | cl::desc("Abort if an isl error is encountered"), |
| 146 | cl::init(true), cl::cat(PollyCategory)); |
| 147 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 148 | static cl::opt<bool> PollyPreciseInbounds( |
| 149 | "polly-precise-inbounds", |
| 150 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 151 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 152 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 153 | static cl::opt<bool> |
| 154 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 155 | cl::desc("Do not take inbounds assumptions at all"), |
| 156 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 157 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 158 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 159 | "polly-ignore-parameter-bounds", |
| 160 | cl::desc( |
| 161 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 162 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 163 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 164 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 165 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 166 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 167 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 168 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 169 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 170 | bool polly::UseInstructionNames; |
| 171 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 172 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 173 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 174 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 175 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 176 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 177 | static cl::opt<bool> PollyPrintInstructions( |
| 178 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 179 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 180 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 181 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 182 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 183 | // Create a sequence of two schedules. Either argument may be null and is |
| 184 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 185 | // empty. |
| 186 | static __isl_give isl_schedule * |
| 187 | combineInSequence(__isl_take isl_schedule *Prev, |
| 188 | __isl_take isl_schedule *Succ) { |
| 189 | if (!Prev) |
| 190 | return Succ; |
| 191 | if (!Succ) |
| 192 | return Prev; |
| 193 | |
| 194 | return isl_schedule_sequence(Prev, Succ); |
| 195 | } |
| 196 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 197 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 198 | int dim, isl::dim type) { |
| 199 | isl::val V; |
| 200 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 201 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 202 | // The upper and lower bound for a parameter value is derived either from |
| 203 | // the data type of the parameter or from the - possibly more restrictive - |
| 204 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 205 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 206 | S = S.lower_bound_val(type, dim, V); |
| 207 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 208 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 209 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 210 | if (Range.isFullSet()) |
| 211 | return S; |
| 212 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 213 | if (isl_set_n_basic_set(S.get()) > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 214 | return S; |
| 215 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 216 | // In case of signed wrapping, we can refine the set of valid values by |
| 217 | // excluding the part not covered by the wrapping range. |
| 218 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 219 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 220 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 221 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 222 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 223 | V = V.sub_ui(1); |
| 224 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 225 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 226 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 227 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 228 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 231 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 232 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 233 | if (!BasePtrLI) |
| 234 | return nullptr; |
| 235 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 236 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 237 | return nullptr; |
| 238 | |
| 239 | ScalarEvolution &SE = *S->getSE(); |
| 240 | |
| 241 | auto *OriginBaseSCEV = |
| 242 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 243 | if (!OriginBaseSCEV) |
| 244 | return nullptr; |
| 245 | |
| 246 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 247 | if (!OriginBaseSCEVUnknown) |
| 248 | return nullptr; |
| 249 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 250 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 251 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 254 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 255 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 256 | const DataLayout &DL, Scop *S, |
| 257 | const char *BaseName) |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 258 | : BasePtr(BasePtr), ElementType(ElementType), IsOnHeap(false), Kind(Kind), |
| 259 | DL(DL), S(*S), FAD(nullptr) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 260 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 261 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 262 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 263 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 264 | UseInstructionNames); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 265 | Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 266 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 267 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 268 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 269 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 270 | BasePtrOriginSAI = nullptr; |
| 271 | return; |
| 272 | } |
| 273 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 274 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 275 | if (BasePtrOriginSAI) |
| 276 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 279 | __isl_give isl_space *ScopArrayInfo::getSpace() const { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 280 | auto *Space = |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 281 | isl_space_set_alloc(isl_id_get_ctx(Id), 0, getNumberOfDimensions()); |
| 282 | Space = isl_space_set_tuple_id(Space, isl_dim_set, isl_id_copy(Id)); |
| 283 | return Space; |
| 284 | } |
| 285 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 286 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 287 | isl::union_set WriteSet = give(S.getWrites()).range(); |
| 288 | isl::space Space = give(getSpace()); |
| 289 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 290 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 291 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 294 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 295 | if (Array->getElementType() != getElementType()) |
| 296 | return false; |
| 297 | |
| 298 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 299 | return false; |
| 300 | |
| 301 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 302 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 303 | return false; |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 308 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 309 | if (NewElementType == ElementType) |
| 310 | return; |
| 311 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 312 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 313 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 314 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 315 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 316 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 317 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 318 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 319 | ElementType = NewElementType; |
| 320 | } else { |
| 321 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 322 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 323 | } |
| 324 | } |
| 325 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 326 | /// Make the ScopArrayInfo model a Fortran Array |
| 327 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 328 | assert(FAD && "got invalid Fortran array descriptor"); |
| 329 | if (this->FAD) { |
| 330 | assert(this->FAD == FAD && |
| 331 | "receiving different array descriptors for same array"); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 336 | assert(!this->FAD); |
| 337 | this->FAD = FAD; |
| 338 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 339 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 340 | |
| 341 | std::string param_name = getName(); |
| 342 | param_name += "_fortranarr_size"; |
| 343 | // 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] | 344 | 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] | 345 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 346 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 347 | isl::pw_aff PwAff = |
| 348 | 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] | 349 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 350 | DimensionSizesPw[0] = PwAff.release(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 353 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 354 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 355 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 356 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 357 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 358 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 359 | if (CheckConsistency) { |
| 360 | for (int i = 0; i < SharedDims; i++) { |
| 361 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 362 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 363 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 364 | return false; |
| 365 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 366 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 367 | if (DimensionSizes.size() >= NewSizes.size()) |
| 368 | return true; |
| 369 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 370 | |
| 371 | DimensionSizes.clear(); |
| 372 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 373 | NewSizes.end()); |
| 374 | for (isl_pw_aff *Size : DimensionSizesPw) |
| 375 | isl_pw_aff_free(Size); |
| 376 | DimensionSizesPw.clear(); |
| 377 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 378 | if (!Expr) { |
| 379 | DimensionSizesPw.push_back(nullptr); |
| 380 | continue; |
| 381 | } |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 382 | isl_pw_aff *Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 383 | DimensionSizesPw.push_back(Size); |
| 384 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 385 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 388 | ScopArrayInfo::~ScopArrayInfo() { |
| 389 | isl_id_free(Id); |
| 390 | for (isl_pw_aff *Size : DimensionSizesPw) |
| 391 | isl_pw_aff_free(Size); |
| 392 | } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 393 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 394 | std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); } |
| 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 | |
Johannes Doerfert | 3c6a99b | 2016-04-09 21:55:23 +0000 | [diff] [blame] | 400 | __isl_give isl_id *ScopArrayInfo::getBasePtrId() const { |
| 401 | return isl_id_copy(Id); |
| 402 | } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 403 | |
| 404 | void ScopArrayInfo::dump() const { print(errs()); } |
| 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) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 422 | auto *Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 423 | OS << " " << Size << " "; |
| 424 | isl_pw_aff_free(Size); |
| 425 | } else { |
| 426 | OS << *getDimensionSize(u); |
| 427 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 428 | |
| 429 | OS << "]"; |
| 430 | } |
| 431 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 432 | OS << ";"; |
| 433 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 434 | if (BasePtrOriginSAI) |
| 435 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 436 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 437 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | const ScopArrayInfo * |
| 441 | ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) { |
| 442 | isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out); |
| 443 | assert(Id && "Output dimension didn't have an ID"); |
| 444 | return getFromId(Id); |
| 445 | } |
| 446 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 447 | const ScopArrayInfo *ScopArrayInfo::getFromId(__isl_take isl_id *Id) { |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 448 | void *User = isl_id_get_user(Id); |
| 449 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 450 | isl_id_free(Id); |
| 451 | return SAI; |
| 452 | } |
| 453 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 454 | void MemoryAccess::wrapConstantDimensions() { |
| 455 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 456 | isl::space ArraySpace = give(SAI->getSpace()); |
| 457 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 458 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 459 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 460 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 461 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 462 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 463 | |
| 464 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 465 | for (int i = DimsArray - 1; i > 0; i--) { |
| 466 | auto *DimSize = SAI->getDimensionSize(i); |
| 467 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 468 | |
| 469 | // This transformation is not applicable to dimensions with dynamic size. |
| 470 | if (!DimSizeCst) |
| 471 | continue; |
| 472 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 473 | // This transformation is not applicable to dimensions of size zero. |
| 474 | if (DimSize->isZero()) |
| 475 | continue; |
| 476 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 477 | isl::val DimSizeVal = |
| 478 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 479 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 480 | isl::aff PrevVar = |
| 481 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 482 | |
| 483 | // Compute: index % size |
| 484 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 485 | isl::aff Modulo = Var.mod_val(DimSizeVal); |
| 486 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 487 | |
| 488 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 489 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 490 | Divide = Divide.floor(); |
| 491 | Divide = Divide.add(PrevVar); |
| 492 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 493 | |
| 494 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 495 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 496 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 500 | isl::map Relation = give(AccessRelation); |
| 501 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 502 | Relation = Relation.detect_equalities(); |
| 503 | AccessRelation = Relation.release(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 506 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 507 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 508 | isl::space ArraySpace = give(SAI->getSpace()); |
| 509 | isl::space AccessSpace = give(isl_map_get_space(AccessRelation)).range(); |
| 510 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 511 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 512 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 513 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 514 | auto DimsMissing = DimsArray - DimsAccess; |
| 515 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 516 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 517 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 518 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 519 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 520 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 521 | isl::map Map = isl::map::from_domain_and_range( |
| 522 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 523 | |
| 524 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 525 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 526 | |
| 527 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 528 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 529 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 530 | AccessRelation = isl_map_apply_range(AccessRelation, Map.release()); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 531 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 532 | // For the non delinearized arrays, divide the access function of the last |
| 533 | // subscript by the size of the elements in the array. |
| 534 | // |
| 535 | // A stride one array access in C expressed as A[i] is expressed in |
| 536 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 537 | // two subsequent values of 'i' index two values that are stored next to |
| 538 | // each other in memory. By this division we make this characteristic |
| 539 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 540 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 541 | // that divides the offsets of all accesses to this base pointer. |
| 542 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 543 | isl::val V = isl::val(Ctx, ArrayElemSize); |
| 544 | AccessRelation = isl_map_floordiv_val(AccessRelation, V.release()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 547 | // We currently do this only if we added at least one dimension, which means |
| 548 | // some dimension's indices have not been specified, an indicator that some |
| 549 | // index values have been added together. |
| 550 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 551 | // expressions more complicated. |
| 552 | if (DimsMissing) |
| 553 | wrapConstantDimensions(); |
| 554 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 555 | if (!isAffine()) |
| 556 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 557 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 558 | // Introduce multi-element accesses in case the type loaded by this memory |
| 559 | // access is larger than the canonical element type of the array. |
| 560 | // |
| 561 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 562 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 563 | if (ElemBytes > ArrayElemSize) { |
| 564 | assert(ElemBytes % ArrayElemSize == 0 && |
| 565 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 566 | isl::map Map = isl::map::from_domain_and_range( |
| 567 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 568 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 569 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 570 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 571 | isl::constraint C; |
| 572 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 573 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 574 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 575 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 576 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 577 | C = isl::constraint::alloc_inequality(LS); |
| 578 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 579 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 580 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 581 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 582 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 583 | C = isl::constraint::alloc_inequality(LS); |
| 584 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 585 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 586 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 587 | Map = Map.add_constraint(C); |
| 588 | AccessRelation = isl_map_apply_range(AccessRelation, Map.release()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 589 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 592 | const std::string |
| 593 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 594 | switch (RT) { |
| 595 | case MemoryAccess::RT_NONE: |
| 596 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 597 | "access which isn't a reduction"); |
| 598 | case MemoryAccess::RT_ADD: |
| 599 | return "+"; |
| 600 | case MemoryAccess::RT_MUL: |
| 601 | return "*"; |
| 602 | case MemoryAccess::RT_BOR: |
| 603 | return "|"; |
| 604 | case MemoryAccess::RT_BXOR: |
| 605 | return "^"; |
| 606 | case MemoryAccess::RT_BAND: |
| 607 | return "&"; |
| 608 | } |
| 609 | llvm_unreachable("Unknown reduction type"); |
| 610 | return ""; |
| 611 | } |
| 612 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 613 | /// Return the reduction type for a given binary operator. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 614 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 615 | const Instruction *Load) { |
| 616 | if (!BinOp) |
| 617 | return MemoryAccess::RT_NONE; |
| 618 | switch (BinOp->getOpcode()) { |
| 619 | case Instruction::FAdd: |
| 620 | if (!BinOp->hasUnsafeAlgebra()) |
| 621 | return MemoryAccess::RT_NONE; |
| 622 | // Fall through |
| 623 | case Instruction::Add: |
| 624 | return MemoryAccess::RT_ADD; |
| 625 | case Instruction::Or: |
| 626 | return MemoryAccess::RT_BOR; |
| 627 | case Instruction::Xor: |
| 628 | return MemoryAccess::RT_BXOR; |
| 629 | case Instruction::And: |
| 630 | return MemoryAccess::RT_BAND; |
| 631 | case Instruction::FMul: |
| 632 | if (!BinOp->hasUnsafeAlgebra()) |
| 633 | return MemoryAccess::RT_NONE; |
| 634 | // Fall through |
| 635 | case Instruction::Mul: |
| 636 | if (DisableMultiplicativeReductions) |
| 637 | return MemoryAccess::RT_NONE; |
| 638 | return MemoryAccess::RT_MUL; |
| 639 | default: |
| 640 | return MemoryAccess::RT_NONE; |
| 641 | } |
| 642 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 643 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 644 | MemoryAccess::~MemoryAccess() { |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 645 | isl_id_free(Id); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 646 | isl_set_free(InvalidDomain); |
Tobias Grosser | 54a86e6 | 2011-08-18 06:31:46 +0000 | [diff] [blame] | 647 | isl_map_free(AccessRelation); |
Tobias Grosser | 166c422 | 2015-09-05 07:46:40 +0000 | [diff] [blame] | 648 | isl_map_free(NewAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 651 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 652 | isl_id *ArrayId = getArrayId(); |
| 653 | void *User = isl_id_get_user(ArrayId); |
| 654 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 655 | isl_id_free(ArrayId); |
| 656 | return SAI; |
| 657 | } |
| 658 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 659 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
| 660 | isl_id *ArrayId = getLatestArrayId(); |
| 661 | void *User = isl_id_get_user(ArrayId); |
| 662 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 663 | isl_id_free(ArrayId); |
| 664 | return SAI; |
| 665 | } |
| 666 | |
| 667 | __isl_give isl_id *MemoryAccess::getOriginalArrayId() const { |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 668 | return isl_map_get_tuple_id(AccessRelation, isl_dim_out); |
| 669 | } |
| 670 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 671 | __isl_give isl_id *MemoryAccess::getLatestArrayId() const { |
| 672 | if (!hasNewAccessRelation()) |
| 673 | return getOriginalArrayId(); |
| 674 | return isl_map_get_tuple_id(NewAccessRelation, isl_dim_out); |
| 675 | } |
| 676 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 677 | __isl_give isl_map *MemoryAccess::getAddressFunction() const { |
| 678 | return isl_map_lexmin(getAccessRelation()); |
| 679 | } |
| 680 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 681 | __isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation( |
| 682 | __isl_take isl_union_map *USchedule) const { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 683 | isl_map *Schedule, *ScheduledAccRel; |
| 684 | isl_union_set *UDomain; |
| 685 | |
| 686 | UDomain = isl_union_set_from_set(getStatement()->getDomain()); |
| 687 | USchedule = isl_union_map_intersect_domain(USchedule, UDomain); |
| 688 | Schedule = isl_map_from_union_map(USchedule); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 689 | ScheduledAccRel = isl_map_apply_domain(getAddressFunction(), Schedule); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 690 | return isl_pw_multi_aff_from_map(ScheduledAccRel); |
| 691 | } |
| 692 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 693 | __isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 694 | return isl_map_copy(AccessRelation); |
| 695 | } |
| 696 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 697 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 698 | return stringFromIslObj(AccessRelation); |
| 699 | } |
| 700 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 701 | __isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 702 | return isl_map_get_space(AccessRelation); |
| 703 | } |
| 704 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 705 | __isl_give isl_map *MemoryAccess::getNewAccessRelation() const { |
Tobias Grosser | 166c422 | 2015-09-05 07:46:40 +0000 | [diff] [blame] | 706 | return isl_map_copy(NewAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 709 | std::string MemoryAccess::getNewAccessRelationStr() const { |
| 710 | return stringFromIslObj(NewAccessRelation); |
| 711 | } |
| 712 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 713 | std::string MemoryAccess::getAccessRelationStr() const { |
| 714 | return isl::manage(getAccessRelation()).to_str(); |
| 715 | } |
| 716 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 717 | __isl_give isl_basic_map * |
| 718 | MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 719 | isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | ed29566 | 2012-09-11 13:50:21 +0000 | [diff] [blame] | 720 | Space = isl_space_align_params(Space, Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 721 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 722 | return isl_basic_map_from_domain_and_range( |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 723 | isl_basic_set_universe(Statement->getDomainSpace()), |
| 724 | isl_basic_set_universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 727 | // Formalize no out-of-bound access assumption |
| 728 | // |
| 729 | // When delinearizing array accesses we optimistically assume that the |
| 730 | // delinearized accesses do not access out of bound locations (the subscript |
| 731 | // expression of each array evaluates for each statement instance that is |
| 732 | // executed to a value that is larger than zero and strictly smaller than the |
| 733 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 734 | // dimension for which we do not need to assume any upper bound. At this point |
| 735 | // we formalize this assumption to ensure that at code generation time the |
| 736 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 737 | // |
| 738 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 739 | // first build the set of data locations that are not within array bounds. We |
| 740 | // then apply the reverse access relation to obtain the set of iterations that |
| 741 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 742 | // that are actually executed by intersecting them with the domain of the |
| 743 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 744 | // parameters that may cause statement instances to be executed that may |
| 745 | // possibly yield out of bound memory accesses. The complement of these |
| 746 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 747 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 748 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 749 | if (PollyIgnoreInbounds) |
| 750 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 751 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 752 | isl::space Space = give(getOriginalAccessRelationSpace()).range(); |
| 753 | isl::set Outside = isl::set::empty(Space); |
| 754 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 755 | isl::local_space LS(Space); |
| 756 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 757 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 758 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 759 | isl::set DimOutside = Var.lt_set(Zero); |
| 760 | isl::pw_aff SizeE = give(SAI->getDimensionSizePw(i)); |
| 761 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 762 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 763 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 764 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 765 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 768 | Outside = Outside.apply(give(getAccessRelation()).reverse()); |
| 769 | Outside = Outside.intersect(give(Statement->getDomain())); |
| 770 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 771 | |
| 772 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 773 | // Doing so increases the set of parameter combinations that are assumed to |
| 774 | // not appear. This is always save, but may make the resulting run-time check |
| 775 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 776 | Outside = Outside.remove_divs(); |
| 777 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 778 | const auto &Loc = getAccessInstruction() |
| 779 | ? getAccessInstruction()->getDebugLoc() |
| 780 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 781 | if (!PollyPreciseInbounds) |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 782 | Outside = Outside.gist_params(give(Statement->getDomain()).params()); |
| 783 | Statement->getParent()->recordAssumption(INBOUNDS, Outside.release(), Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 784 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 787 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 788 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 789 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 790 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 791 | isl::pw_aff SubscriptPWA = give(getPwAff(Subscripts[0])); |
| 792 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 793 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 794 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 795 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 796 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 797 | } else { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 798 | isl::pw_aff LengthPWA = give(getPwAff(Subscripts[1])); |
| 799 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 800 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 801 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 802 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 803 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 804 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 805 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 806 | LengthMap = LengthMap.sum(SubscriptMap); |
| 807 | AccessRelation = |
| 808 | LengthMap.set_tuple_id(isl::dim::in, give(getStatement()->getDomainId())) |
| 809 | .release(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 812 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 813 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 814 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 815 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 816 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 817 | return; |
| 818 | |
| 819 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 820 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 821 | return; |
| 822 | |
| 823 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 824 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 825 | return; |
| 826 | |
| 827 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 828 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 829 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 830 | |
| 831 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 832 | if (Range.isFullSet()) |
| 833 | return; |
| 834 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 835 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 836 | return; |
| 837 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 838 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 839 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 840 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 841 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 842 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 843 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 844 | |
| 845 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 846 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 847 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 848 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 849 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 850 | isl::map Relation = give(AccessRelation); |
| 851 | isl::set AccessRange = Relation.range(); |
| 852 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 853 | isl::dim::set); |
| 854 | AccessRelation = Relation.intersect_range(AccessRange).release(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 857 | void MemoryAccess::foldAccessRelation() { |
| 858 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 859 | return; |
| 860 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 861 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 862 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 863 | isl::map NewAccessRelation = give(isl_map_copy(AccessRelation)); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 864 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 865 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 866 | isl::space Space; |
| 867 | isl::map MapOne, MapTwo; |
| 868 | isl::pw_aff DimSize = give(getPwAff(Sizes[i + 1])); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 869 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 870 | isl::space SpaceSize = DimSize.get_space(); |
| 871 | isl::id ParamId = |
| 872 | give(isl_space_get_dim_id(SpaceSize.get(), isl_dim_param, 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 | Space = give(isl_map_copy(AccessRelation)).get_space(); |
| 875 | Space = Space.range().map_from_set(); |
| 876 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 877 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 878 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 879 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 880 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 881 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 882 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 883 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 884 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 885 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 886 | for (int j = 0; j < Size; ++j) |
| 887 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 888 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 889 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 890 | isl::local_space LS(Space); |
| 891 | isl::constraint C; |
| 892 | C = isl::constraint::alloc_equality(LS); |
| 893 | C = C.set_constant_si(-1); |
| 894 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 895 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 896 | MapTwo = MapTwo.add_constraint(C); |
| 897 | C = isl::constraint::alloc_equality(LS); |
| 898 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 899 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 900 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 901 | MapTwo = MapTwo.add_constraint(C); |
| 902 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 903 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 904 | MapOne = MapOne.unite(MapTwo); |
| 905 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 906 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 907 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 908 | isl::id BaseAddrId = give(getScopArrayInfo()->getBasePtrId()); |
| 909 | isl::space Space = give(Statement->getDomainSpace()); |
| 910 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 911 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 912 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
| 913 | NewAccessRelation = |
| 914 | NewAccessRelation.gist_domain(give(Statement->getDomain())); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 915 | |
| 916 | // Access dimension folding might in certain cases increase the number of |
| 917 | // disjuncts in the memory access, which can possibly complicate the generated |
| 918 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 919 | if (!PollyPreciseFoldAccesses && |
| 920 | isl_map_n_basic_map(NewAccessRelation.get()) > |
| 921 | isl_map_n_basic_map(AccessRelation)) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 922 | } else { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 923 | isl_map_free(AccessRelation); |
| 924 | AccessRelation = NewAccessRelation.release(); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 925 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 928 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 929 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 930 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 931 | if (Size == 1) |
| 932 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 933 | |
| 934 | // Only one factor needs to be divisible. |
| 935 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 936 | for (auto *FactorExpr : MulExpr->operands()) |
| 937 | if (isDivisible(FactorExpr, Size, SE)) |
| 938 | return true; |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 943 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 944 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 945 | for (auto *OpExpr : NAryExpr->operands()) |
| 946 | if (!isDivisible(OpExpr, Size, SE)) |
| 947 | return false; |
| 948 | return true; |
| 949 | } |
| 950 | |
| 951 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 952 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 953 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 954 | return MulSCEV == Expr; |
| 955 | } |
| 956 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 957 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 37c8ee7 | 2017-06-30 06:30:51 +0000 | [diff] [blame] | 958 | assert(!AccessRelation && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 959 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 960 | // Initialize the invalid domain which describes all iterations for which the |
| 961 | // access relation is not modeled correctly. |
Johannes Doerfert | a4dd8ef | 2016-04-25 13:36:23 +0000 | [diff] [blame] | 962 | auto *StmtInvalidDomain = getStatement()->getInvalidDomain(); |
| 963 | InvalidDomain = isl_set_empty(isl_set_get_space(StmtInvalidDomain)); |
| 964 | isl_set_free(StmtInvalidDomain); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 965 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 966 | isl_ctx *Ctx = isl_id_get_ctx(Id); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 967 | isl_id *BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 968 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 969 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 970 | buildMemIntrinsicAccessRelation(); |
| 971 | AccessRelation = |
| 972 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
| 973 | return; |
| 974 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 975 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 976 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 977 | // We overapproximate non-affine accesses with a possible access to the |
| 978 | // whole array. For read accesses it does not make a difference, if an |
| 979 | // access must or may happen. However, for write accesses it is important to |
| 980 | // differentiate between writes that must happen and writes that may happen. |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 981 | if (!AccessRelation) |
| 982 | AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement)); |
| 983 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 984 | AccessRelation = |
| 985 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 986 | return; |
| 987 | } |
| 988 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 989 | isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0); |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 990 | AccessRelation = isl_map_universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 991 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 992 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 993 | isl_pw_aff *Affine = getPwAff(Subscripts[i]); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 994 | isl_map *SubscriptMap = isl_map_from_pw_aff(Affine); |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 995 | AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 998 | Space = Statement->getDomainSpace(); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 999 | AccessRelation = isl_map_set_tuple_id( |
| 1000 | AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 1001 | AccessRelation = |
| 1002 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
| 1003 | |
Tobias Grosser | aa660a9 | 2015-03-30 00:07:50 +0000 | [diff] [blame] | 1004 | AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain()); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 1005 | isl_space_free(Space); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1006 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 1007 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1008 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1009 | AccessType AccType, Value *BaseAddress, |
| 1010 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1011 | ArrayRef<const SCEV *> Subscripts, |
| 1012 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 1013 | MemoryKind Kind) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1014 | : Kind(Kind), AccType(AccType), RedType(RT_NONE), Statement(Stmt), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1015 | InvalidDomain(nullptr), BaseAddr(BaseAddress), ElementType(ElementType), |
| 1016 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 1017 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1018 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1019 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1020 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1021 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1022 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1023 | std::string IdName = Stmt->getBaseName() + Access; |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1024 | Id = isl_id_alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
| 1025 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1026 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1027 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, |
| 1028 | __isl_take isl_map *AccRel) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1029 | : Kind(MemoryKind::Array), AccType(AccType), RedType(RT_NONE), |
| 1030 | Statement(Stmt), InvalidDomain(nullptr), AccessInstruction(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1031 | IsAffine(true), AccessRelation(nullptr), NewAccessRelation(AccRel), |
| 1032 | FAD(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1033 | auto *ArrayInfoId = isl_map_get_tuple_id(NewAccessRelation, isl_dim_out); |
| 1034 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1035 | Sizes.push_back(nullptr); |
| 1036 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1037 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1038 | ElementType = SAI->getElementType(); |
| 1039 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1040 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1041 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1042 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1043 | std::string IdName = Stmt->getBaseName() + Access; |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1044 | Id = isl_id_alloc(Stmt->getParent()->getIslCtx(), IdName.c_str(), this); |
| 1045 | } |
| 1046 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1047 | void MemoryAccess::realignParams() { |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 1048 | auto *Ctx = Statement->getParent()->getContext(); |
| 1049 | InvalidDomain = isl_set_gist_params(InvalidDomain, isl_set_copy(Ctx)); |
| 1050 | AccessRelation = isl_map_gist_params(AccessRelation, Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1053 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1054 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1055 | } |
| 1056 | |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1057 | __isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); } |
| 1058 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1059 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1060 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1061 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1062 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1063 | else |
| 1064 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1065 | return OS; |
| 1066 | } |
| 1067 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1068 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1069 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1070 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1071 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1072 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1073 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1074 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1075 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1076 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1077 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1078 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1079 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1082 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1083 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (FAD) { |
| 1086 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1087 | OS << "] "; |
| 1088 | }; |
| 1089 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1090 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1091 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1092 | if (hasNewAccessRelation()) |
| 1093 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1096 | void MemoryAccess::dump() const { print(errs()); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1097 | |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1098 | __isl_give isl_pw_aff *MemoryAccess::getPwAff(const SCEV *E) { |
| 1099 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1100 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | 5329277 | 2016-07-11 12:01:26 +0000 | [diff] [blame] | 1101 | isl_set *StmtDom = isl_set_reset_tuple_id(getStatement()->getDomain()); |
| 1102 | isl_set *NewInvalidDom = isl_set_intersect(StmtDom, PWAC.second); |
| 1103 | InvalidDomain = isl_set_union(InvalidDomain, NewInvalidDom); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1104 | return PWAC.first; |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1107 | // Create a map in the size of the provided set domain, that maps from the |
| 1108 | // one element of the provided set domain to another element of the provided |
| 1109 | // set domain. |
| 1110 | // The mapping is limited to all points that are equal in all but the last |
| 1111 | // dimension and for which the last dimension of the input is strict smaller |
| 1112 | // than the last dimension of the output. |
| 1113 | // |
| 1114 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1115 | // |
| 1116 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1117 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1118 | // |
Tobias Grosser | 2a526fe | 2016-09-08 11:18:56 +0000 | [diff] [blame] | 1119 | static isl_map *getEqualAndLarger(__isl_take isl_space *setDomain) { |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1120 | isl_space *Space = isl_space_map_from_set(setDomain); |
Tobias Grosser | 1b6ea57 | 2015-05-21 19:02:44 +0000 | [diff] [blame] | 1121 | isl_map *Map = isl_map_universe(Space); |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1122 | unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Set all but the last dimension to be equal for the input and output |
| 1125 | // |
| 1126 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1127 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1128 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1129 | Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Set the last dimension of the input to be strict smaller than the |
| 1132 | // last dimension of the output. |
| 1133 | // |
| 1134 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | 1b6ea57 | 2015-05-21 19:02:44 +0000 | [diff] [blame] | 1135 | Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out, |
| 1136 | lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1137 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1140 | __isl_give isl_set * |
| 1141 | MemoryAccess::getStride(__isl_take const isl_map *Schedule) const { |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1142 | isl_map *S = const_cast<isl_map *>(Schedule); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 1143 | isl_map *AccessRelation = getAccessRelation(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1144 | isl_space *Space = isl_space_range(isl_map_get_space(S)); |
| 1145 | isl_map *NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1146 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1147 | S = isl_map_reverse(S); |
| 1148 | NextScatt = isl_map_lexmin(NextScatt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1149 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1150 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S)); |
| 1151 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation)); |
| 1152 | NextScatt = isl_map_apply_domain(NextScatt, S); |
| 1153 | NextScatt = isl_map_apply_domain(NextScatt, AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1154 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1155 | isl_set *Deltas = isl_map_deltas(NextScatt); |
| 1156 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1159 | bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule, |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1160 | int StrideWidth) const { |
| 1161 | isl_set *Stride, *StrideX; |
| 1162 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1163 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1164 | Stride = getStride(Schedule); |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1165 | StrideX = isl_set_universe(isl_set_get_space(Stride)); |
Tobias Grosser | 01c8f5f | 2015-08-24 22:20:46 +0000 | [diff] [blame] | 1166 | for (unsigned i = 0; i < isl_set_dim(StrideX, isl_dim_set) - 1; i++) |
| 1167 | StrideX = isl_set_fix_si(StrideX, isl_dim_set, i, 0); |
| 1168 | StrideX = isl_set_fix_si(StrideX, isl_dim_set, |
| 1169 | isl_set_dim(StrideX, isl_dim_set) - 1, StrideWidth); |
Roman Gareev | f2bd72e | 2015-08-18 16:12:05 +0000 | [diff] [blame] | 1170 | IsStrideX = isl_set_is_subset(Stride, StrideX); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1171 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1172 | isl_set_free(StrideX); |
Tobias Grosser | dea9823 | 2012-01-17 20:34:27 +0000 | [diff] [blame] | 1173 | isl_set_free(Stride); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1174 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1175 | return IsStrideX; |
| 1176 | } |
| 1177 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 1178 | bool MemoryAccess::isStrideZero(__isl_take const isl_map *Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1179 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 1182 | bool MemoryAccess::isStrideOne(__isl_take const isl_map *Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1183 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1186 | void MemoryAccess::setAccessRelation(__isl_take isl_map *NewAccess) { |
| 1187 | isl_map_free(AccessRelation); |
| 1188 | AccessRelation = NewAccess; |
| 1189 | } |
| 1190 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 1191 | void MemoryAccess::setNewAccessRelation(__isl_take isl_map *NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1192 | assert(NewAccess); |
| 1193 | |
| 1194 | #ifndef NDEBUG |
| 1195 | // Check domain space compatibility. |
| 1196 | auto *NewSpace = isl_map_get_space(NewAccess); |
| 1197 | auto *NewDomainSpace = isl_space_domain(isl_space_copy(NewSpace)); |
| 1198 | auto *OriginalDomainSpace = getStatement()->getDomainSpace(); |
| 1199 | assert(isl_space_has_equal_tuples(OriginalDomainSpace, NewDomainSpace)); |
| 1200 | isl_space_free(NewDomainSpace); |
| 1201 | isl_space_free(OriginalDomainSpace); |
| 1202 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1203 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1204 | // subdomain only. |
| 1205 | if (isRead()) { |
| 1206 | // Check whether there is an access for every statement instance. |
| 1207 | auto *StmtDomain = getStatement()->getDomain(); |
| 1208 | StmtDomain = isl_set_intersect_params( |
| 1209 | StmtDomain, getStatement()->getParent()->getContext()); |
| 1210 | auto *NewDomain = isl_map_domain(isl_map_copy(NewAccess)); |
| 1211 | assert(isl_set_is_subset(StmtDomain, NewDomain) && |
| 1212 | "Partial READ accesses not supported"); |
| 1213 | isl_set_free(NewDomain); |
| 1214 | isl_set_free(StmtDomain); |
| 1215 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1216 | |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1217 | auto *NewAccessSpace = isl_space_range(NewSpace); |
| 1218 | assert(isl_space_has_tuple_id(NewAccessSpace, isl_dim_set) && |
| 1219 | "Must specify the array that is accessed"); |
| 1220 | auto *NewArrayId = isl_space_get_tuple_id(NewAccessSpace, isl_dim_set); |
| 1221 | auto *SAI = static_cast<ScopArrayInfo *>(isl_id_get_user(NewArrayId)); |
| 1222 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1225 | InvariantEquivClassTy *EqClass = |
| 1226 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1227 | SAI->getBasePtr()); |
| 1228 | assert(EqClass && |
| 1229 | "Access functions to indirect arrays must have an invariant and " |
| 1230 | "hoisted base pointer"); |
| 1231 | } |
| 1232 | |
| 1233 | // Check whether access dimensions correspond to number of dimensions of the |
| 1234 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1235 | auto Dims = SAI->getNumberOfDimensions(); |
| 1236 | assert(isl_space_dim(NewAccessSpace, isl_dim_set) == Dims && |
| 1237 | "Access dims must match array dims"); |
| 1238 | isl_space_free(NewAccessSpace); |
| 1239 | isl_id_free(NewArrayId); |
| 1240 | #endif |
| 1241 | |
Tobias Grosser | 166c422 | 2015-09-05 07:46:40 +0000 | [diff] [blame] | 1242 | isl_map_free(NewAccessRelation); |
| 1243 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1244 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1245 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1246 | bool MemoryAccess::isLatestPartialAccess() const { |
| 1247 | isl::set StmtDom = give(getStatement()->getDomain()); |
| 1248 | isl::set AccDom = give(isl_map_domain(getLatestAccessRelation())); |
| 1249 | |
| 1250 | return isl_set_is_subset(StmtDom.keep(), AccDom.keep()) == isl_bool_false; |
| 1251 | } |
| 1252 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1253 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1254 | |
Johannes Doerfert | 3c6a99b | 2016-04-09 21:55:23 +0000 | [diff] [blame] | 1255 | __isl_give isl_map *ScopStmt::getSchedule() const { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1256 | isl_set *Domain = getDomain(); |
| 1257 | if (isl_set_is_empty(Domain)) { |
| 1258 | isl_set_free(Domain); |
| 1259 | return isl_map_from_aff( |
| 1260 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 1261 | } |
| 1262 | auto *Schedule = getParent()->getSchedule(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1263 | if (!Schedule) { |
| 1264 | isl_set_free(Domain); |
| 1265 | return nullptr; |
| 1266 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1267 | Schedule = isl_union_map_intersect_domain( |
| 1268 | Schedule, isl_union_set_from_set(isl_set_copy(Domain))); |
| 1269 | if (isl_union_map_is_empty(Schedule)) { |
| 1270 | isl_set_free(Domain); |
| 1271 | isl_union_map_free(Schedule); |
| 1272 | return isl_map_from_aff( |
| 1273 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 1274 | } |
| 1275 | auto *M = isl_map_from_union_map(Schedule); |
| 1276 | M = isl_map_coalesce(M); |
| 1277 | M = isl_map_gist_domain(M, Domain); |
| 1278 | M = isl_map_coalesce(M); |
| 1279 | return M; |
| 1280 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1281 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1282 | void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) { |
| 1283 | assert(isl_set_is_subset(NewDomain, Domain) && |
| 1284 | "New domain is not a subset of old domain!"); |
| 1285 | isl_set_free(Domain); |
| 1286 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1289 | void ScopStmt::buildAccessRelations() { |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 1290 | Scop &S = *getParent(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1291 | for (MemoryAccess *Access : MemAccs) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1292 | Type *ElementType = Access->getElementType(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1293 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1294 | MemoryKind Ty; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1295 | if (Access->isPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1296 | Ty = MemoryKind::PHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1297 | else if (Access->isExitPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1298 | Ty = MemoryKind::ExitPHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1299 | else if (Access->isValueKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1300 | Ty = MemoryKind::Value; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1301 | else |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1302 | Ty = MemoryKind::Array; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1303 | |
Tobias Grosser | 296fe2e | 2017-02-10 10:09:46 +0000 | [diff] [blame] | 1304 | auto *SAI = S.getOrCreateScopArrayInfo(Access->getOriginalBaseAddr(), |
| 1305 | ElementType, Access->Sizes, Ty); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1306 | Access->buildAccessRelation(SAI); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
Michael Kruse | 4c27643 | 2017-05-11 22:56:46 +0000 | [diff] [blame] | 1310 | MemoryAccess *ScopStmt::lookupPHIReadOf(PHINode *PHI) const { |
| 1311 | for (auto *MA : *this) { |
| 1312 | if (!MA->isRead()) |
| 1313 | continue; |
| 1314 | if (!MA->isLatestAnyPHIKind()) |
| 1315 | continue; |
| 1316 | |
| 1317 | if (MA->getAccessInstruction() == PHI) |
| 1318 | return MA; |
| 1319 | } |
| 1320 | return nullptr; |
| 1321 | } |
| 1322 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1323 | void ScopStmt::addAccess(MemoryAccess *Access) { |
| 1324 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1325 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1326 | if (Access->isArrayKind()) { |
| 1327 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1328 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1329 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1330 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 1331 | assert(Parent.getStmtFor(AccessVal) == this); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1332 | assert(!ValueWrites.lookup(AccessVal)); |
| 1333 | |
| 1334 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1335 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1336 | Value *AccessVal = Access->getAccessValue(); |
| 1337 | assert(!ValueReads.lookup(AccessVal)); |
| 1338 | |
| 1339 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1340 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1341 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1342 | assert(!PHIWrites.lookup(PHI)); |
| 1343 | |
| 1344 | PHIWrites[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1350 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1351 | for (MemoryAccess *MA : *this) |
| 1352 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1353 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 1354 | auto *Ctx = Parent.getContext(); |
| 1355 | InvalidDomain = isl_set_gist_params(InvalidDomain, isl_set_copy(Ctx)); |
| 1356 | Domain = isl_set_gist_params(Domain, Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1359 | /// 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] | 1360 | static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet, |
| 1361 | void *User) { |
| 1362 | isl_set **BoundedParts = static_cast<isl_set **>(User); |
| 1363 | if (isl_basic_set_is_bounded(BSet)) |
| 1364 | *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet)); |
| 1365 | else |
| 1366 | isl_basic_set_free(BSet); |
| 1367 | return isl_stat_ok; |
| 1368 | } |
| 1369 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1370 | /// Return the bounded parts of @p S. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1371 | static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) { |
| 1372 | isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S)); |
| 1373 | isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts); |
| 1374 | isl_set_free(S); |
| 1375 | return BoundedParts; |
| 1376 | } |
| 1377 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1378 | /// 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] | 1379 | /// |
| 1380 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1381 | /// both with regards to the dimension @p Dim. |
| 1382 | static std::pair<__isl_give isl_set *, __isl_give isl_set *> |
| 1383 | partitionSetParts(__isl_take isl_set *S, unsigned Dim) { |
| 1384 | |
| 1385 | 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] | 1386 | S = isl_set_lower_bound_si(S, isl_dim_set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1387 | |
| 1388 | unsigned NumDimsS = isl_set_n_dim(S); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1389 | isl_set *OnlyDimS = isl_set_copy(S); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1392 | assert(NumDimsS >= Dim + 1); |
| 1393 | OnlyDimS = |
| 1394 | isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1); |
| 1395 | |
| 1396 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1397 | // as we are not interested in them. |
| 1398 | OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim); |
| 1399 | for (unsigned u = 0; u < Dim; u++) { |
| 1400 | isl_constraint *C = isl_inequality_alloc( |
| 1401 | isl_local_space_from_space(isl_set_get_space(OnlyDimS))); |
| 1402 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1); |
| 1403 | C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1); |
| 1404 | OnlyDimS = isl_set_add_constraint(OnlyDimS, C); |
| 1405 | } |
| 1406 | |
| 1407 | // Collect all bounded parts of OnlyDimS. |
| 1408 | isl_set *BoundedParts = collectBoundedParts(OnlyDimS); |
| 1409 | |
| 1410 | // Create the dimensions greater than Dim again. |
| 1411 | BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1, |
| 1412 | NumDimsS - Dim - 1); |
| 1413 | |
| 1414 | // Remove the artificial upper bound parameters again. |
| 1415 | BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim); |
| 1416 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1417 | isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1418 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1419 | } |
| 1420 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1421 | /// Set the dimension Ids from @p From in @p To. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1422 | static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From, |
| 1423 | __isl_take isl_set *To) { |
| 1424 | for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) { |
| 1425 | isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u); |
| 1426 | To = isl_set_set_dim_id(To, isl_dim_set, u, DimId); |
| 1427 | } |
| 1428 | return To; |
| 1429 | } |
| 1430 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1431 | /// 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] | 1432 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1433 | __isl_take isl_pw_aff *L, |
| 1434 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1435 | switch (Pred) { |
| 1436 | case ICmpInst::ICMP_EQ: |
| 1437 | return isl_pw_aff_eq_set(L, R); |
| 1438 | case ICmpInst::ICMP_NE: |
| 1439 | return isl_pw_aff_ne_set(L, R); |
| 1440 | case ICmpInst::ICMP_SLT: |
| 1441 | return isl_pw_aff_lt_set(L, R); |
| 1442 | case ICmpInst::ICMP_SLE: |
| 1443 | return isl_pw_aff_le_set(L, R); |
| 1444 | case ICmpInst::ICMP_SGT: |
| 1445 | return isl_pw_aff_gt_set(L, R); |
| 1446 | case ICmpInst::ICMP_SGE: |
| 1447 | return isl_pw_aff_ge_set(L, R); |
| 1448 | case ICmpInst::ICMP_ULT: |
| 1449 | return isl_pw_aff_lt_set(L, R); |
| 1450 | case ICmpInst::ICMP_UGT: |
| 1451 | return isl_pw_aff_gt_set(L, R); |
| 1452 | case ICmpInst::ICMP_ULE: |
| 1453 | return isl_pw_aff_le_set(L, R); |
| 1454 | case ICmpInst::ICMP_UGE: |
| 1455 | return isl_pw_aff_ge_set(L, R); |
| 1456 | default: |
| 1457 | llvm_unreachable("Non integer predicate not supported"); |
| 1458 | } |
| 1459 | } |
| 1460 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1461 | /// 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] | 1462 | /// |
| 1463 | /// Helper function that will make sure the dimensions of the result have the |
| 1464 | /// same isl_id's as the @p Domain. |
| 1465 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
| 1466 | __isl_take isl_pw_aff *L, |
| 1467 | __isl_take isl_pw_aff *R, |
| 1468 | __isl_keep isl_set *Domain) { |
| 1469 | isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R); |
| 1470 | return setDimensionIds(Domain, ConsequenceCondSet); |
| 1471 | } |
| 1472 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1473 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1474 | /// |
| 1475 | /// @param S The Scop in which @p BB resides in. |
| 1476 | /// @param BB The BB for which isl representation is to be |
| 1477 | /// computed. |
| 1478 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1479 | /// @param E The SCEV that should be translated. |
| 1480 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1481 | /// |
| 1482 | /// Note that this function will also adjust the invalid context accordingly. |
| 1483 | |
| 1484 | __isl_give isl_pw_aff * |
| 1485 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1486 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1487 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1488 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1489 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(isl::manage(PWAC.second)); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1490 | return PWAC.first; |
| 1491 | } |
| 1492 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1493 | /// 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] | 1494 | /// |
| 1495 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1496 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1497 | /// have as many elements as @p SI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1498 | static bool |
| 1499 | buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1500 | __isl_keep isl_set *Domain, |
| 1501 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1502 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1503 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1504 | Value *Condition = getConditionFromTerminator(SI); |
| 1505 | assert(Condition && "No condition for switch"); |
| 1506 | |
| 1507 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1508 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1509 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1510 | |
| 1511 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1512 | ConditionSets.resize(NumSuccessors); |
| 1513 | for (auto &Case : SI->cases()) { |
| 1514 | unsigned Idx = Case.getSuccessorIndex(); |
| 1515 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1516 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1517 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1518 | isl_set *CaseConditionSet = |
| 1519 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain); |
| 1520 | ConditionSets[Idx] = isl_set_coalesce( |
| 1521 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1522 | } |
| 1523 | |
| 1524 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1525 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1526 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1527 | ConditionSetUnion = |
| 1528 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
| 1529 | ConditionSets[0] = setDimensionIds( |
| 1530 | Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion)); |
| 1531 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1532 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1533 | |
| 1534 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1537 | /// Build the conditions sets for the branch condition @p Condition in |
| 1538 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1539 | /// |
| 1540 | /// This will fill @p ConditionSets with the conditions under which control |
| 1541 | /// 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] | 1542 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1543 | /// context under which @p Condition is true/false will be returned as the |
| 1544 | /// new elements of @p ConditionSets. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1545 | static bool |
| 1546 | buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1547 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1548 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1549 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1550 | |
| 1551 | isl_set *ConsequenceCondSet = nullptr; |
| 1552 | if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
| 1553 | if (CCond->isZero()) |
| 1554 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1555 | else |
| 1556 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1557 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1558 | auto Opcode = BinOp->getOpcode(); |
| 1559 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1560 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1561 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1562 | InvalidDomainMap, ConditionSets) && |
| 1563 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1564 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1565 | if (!Valid) { |
| 1566 | while (!ConditionSets.empty()) |
| 1567 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1568 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1569 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1570 | |
| 1571 | isl_set_free(ConditionSets.pop_back_val()); |
| 1572 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1573 | isl_set_free(ConditionSets.pop_back_val()); |
| 1574 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1575 | |
| 1576 | if (Opcode == Instruction::And) |
| 1577 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1578 | else |
| 1579 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1580 | } else { |
| 1581 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1582 | assert(ICond && |
| 1583 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1584 | |
| 1585 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1586 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1587 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1588 | // to be set. The comparison is equal to a signed comparison under this |
| 1589 | // assumption. |
| 1590 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1591 | LHS = getPwAff(S, BB, InvalidDomainMap, |
| 1592 | SE.getSCEVAtScope(ICond->getOperand(0), L), NonNeg); |
| 1593 | RHS = getPwAff(S, BB, InvalidDomainMap, |
| 1594 | SE.getSCEVAtScope(ICond->getOperand(1), L), NonNeg); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1595 | ConsequenceCondSet = |
| 1596 | buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain); |
| 1597 | } |
| 1598 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1599 | // If no terminator was given we are only looking for parameter constraints |
| 1600 | // under which @p Condition is true/false. |
| 1601 | if (!TI) |
| 1602 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1603 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1604 | ConsequenceCondSet = isl_set_coalesce( |
| 1605 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1606 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1607 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1608 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1609 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1610 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1611 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1612 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1613 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1614 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1615 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1618 | if (TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1619 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc()); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1620 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1621 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1622 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | ConditionSets.push_back(ConsequenceCondSet); |
| 1626 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1627 | |
| 1628 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1631 | /// 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] | 1632 | /// |
| 1633 | /// This will fill @p ConditionSets with the conditions under which control |
| 1634 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1635 | /// have as many elements as @p TI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1636 | static bool |
| 1637 | buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1638 | __isl_keep isl_set *Domain, |
| 1639 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1640 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1641 | |
| 1642 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1643 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1644 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1645 | |
| 1646 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1647 | |
| 1648 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1649 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1650 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1653 | Value *Condition = getConditionFromTerminator(TI); |
| 1654 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1655 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1656 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1657 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1660 | void ScopStmt::buildDomain() { |
Michael Kruse | 526fcf5 | 2016-02-24 22:08:08 +0000 | [diff] [blame] | 1661 | isl_id *Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1662 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1663 | Domain = getParent()->getDomainConditions(this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1664 | Domain = isl_set_set_tuple_id(Domain, Id); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1667 | void ScopStmt::collectSurroundingLoops() { |
| 1668 | for (unsigned u = 0, e = isl_set_n_dim(Domain); u < e; u++) { |
| 1669 | isl_id *DimId = isl_set_get_dim_id(Domain, isl_dim_set, u); |
| 1670 | NestLoops.push_back(static_cast<Loop *>(isl_id_get_user(DimId))); |
| 1671 | isl_id_free(DimId); |
| 1672 | } |
| 1673 | } |
| 1674 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1675 | ScopStmt::ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1676 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(nullptr), |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1677 | R(&R), Build(nullptr), SurroundingLoop(SurroundingLoop) { |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1678 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1679 | BaseName = getIslCompatibleName( |
| 1680 | "Stmt", R.getNameStr(), parent.getNextStmtIdx(), "", UseInstructionNames); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1683 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop, |
| 1684 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1685 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1686 | R(nullptr), Build(nullptr), SurroundingLoop(SurroundingLoop), |
| 1687 | Instructions(Instructions) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1688 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1689 | BaseName = getIslCompatibleName("Stmt", &bb, parent.getNextStmtIdx(), "", |
| 1690 | UseInstructionNames); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1693 | ScopStmt::ScopStmt(Scop &parent, __isl_take isl_map *SourceRel, |
| 1694 | __isl_take isl_map *TargetRel, __isl_take isl_set *NewDomain) |
| 1695 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), BB(nullptr), |
| 1696 | R(nullptr), Build(nullptr) { |
| 1697 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1698 | std::to_string(parent.getCopyStmtsNum())); |
| 1699 | auto *Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
| 1700 | Domain = isl_set_set_tuple_id(Domain, isl_id_copy(Id)); |
| 1701 | TargetRel = isl_map_set_tuple_id(TargetRel, isl_dim_in, Id); |
| 1702 | auto *Access = |
| 1703 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
| 1704 | parent.addAccessFunction(Access); |
| 1705 | addAccess(Access); |
| 1706 | SourceRel = isl_map_set_tuple_id(SourceRel, isl_dim_in, isl_id_copy(Id)); |
| 1707 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
| 1708 | parent.addAccessFunction(Access); |
| 1709 | addAccess(Access); |
| 1710 | } |
| 1711 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 1712 | void ScopStmt::init(LoopInfo &LI) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1713 | assert(!Domain && "init must be called only once"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1714 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1715 | buildDomain(); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1716 | collectSurroundingLoops(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1717 | buildAccessRelations(); |
| 1718 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 1719 | if (DetectReductions) |
| 1720 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1723 | /// Collect loads which might form a reduction chain with @p StoreMA. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1724 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1725 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 1726 | /// two loads as operands. If the binary operand is commutative & associative, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1727 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 1728 | /// once, we have found a possible reduction chain. It starts at an operand |
| 1729 | /// load and includes the binary operator and @p StoreMA. |
| 1730 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1731 | /// 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] | 1732 | /// escape this block or into any other store except @p StoreMA. |
| 1733 | void ScopStmt::collectCandiateReductionLoads( |
| 1734 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 1735 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 1736 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1737 | return; |
| 1738 | |
| 1739 | // Skip if there is not one binary operator between the load and the store |
| 1740 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1741 | if (!BinOp) |
| 1742 | return; |
| 1743 | |
| 1744 | // Skip if the binary operators has multiple uses |
| 1745 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1746 | return; |
| 1747 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1748 | // Skip if the opcode of the binary operator is not commutative/associative |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1749 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 1750 | return; |
| 1751 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1752 | // Skip if the binary operator is outside the current SCoP |
| 1753 | if (BinOp->getParent() != Store->getParent()) |
| 1754 | return; |
| 1755 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1756 | // Skip if it is a multiplicative reduction and we disabled them |
| 1757 | if (DisableMultiplicativeReductions && |
| 1758 | (BinOp->getOpcode() == Instruction::Mul || |
| 1759 | BinOp->getOpcode() == Instruction::FMul)) |
| 1760 | return; |
| 1761 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1762 | // Check the binary operator operands for a candidate load |
| 1763 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 1764 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 1765 | if (!PossibleLoad0 && !PossibleLoad1) |
| 1766 | return; |
| 1767 | |
| 1768 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 1769 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1770 | if (PossibleLoad0->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1771 | Loads.push_back(&getArrayAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1772 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1773 | if (PossibleLoad1->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1774 | Loads.push_back(&getArrayAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1777 | /// Check for reductions in this ScopStmt. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1778 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1779 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 1780 | /// like chains. For all candidates we check if they have the same base address |
| 1781 | /// and there are no other accesses which overlap with them. The base address |
| 1782 | /// check rules out impossible reductions candidates early. The overlap check, |
| 1783 | /// together with the "only one user" check in collectCandiateReductionLoads, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1784 | /// guarantees that none of the intermediate results will escape during |
| 1785 | /// execution of the loop nest. We basically check here that no other memory |
| 1786 | /// access can access the same memory as the potential reduction. |
| 1787 | void ScopStmt::checkForReductions() { |
| 1788 | SmallVector<MemoryAccess *, 2> Loads; |
| 1789 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 1790 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1791 | // First collect candidate load-store reduction chains by iterating over all |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1792 | // stores and collecting possible reduction loads. |
| 1793 | for (MemoryAccess *StoreMA : MemAccs) { |
| 1794 | if (StoreMA->isRead()) |
| 1795 | continue; |
| 1796 | |
| 1797 | Loads.clear(); |
| 1798 | collectCandiateReductionLoads(StoreMA, Loads); |
| 1799 | for (MemoryAccess *LoadMA : Loads) |
| 1800 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 1801 | } |
| 1802 | |
| 1803 | // Then check each possible candidate pair. |
| 1804 | for (const auto &CandidatePair : Candidates) { |
| 1805 | bool Valid = true; |
| 1806 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation(); |
| 1807 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation(); |
| 1808 | |
| 1809 | // Skip those with obviously unequal base addresses. |
| 1810 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 1811 | isl_map_free(LoadAccs); |
| 1812 | isl_map_free(StoreAccs); |
| 1813 | continue; |
| 1814 | } |
| 1815 | |
| 1816 | // And check if the remaining for overlap with other memory accesses. |
| 1817 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
| 1818 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain()); |
| 1819 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 1820 | |
| 1821 | for (MemoryAccess *MA : MemAccs) { |
| 1822 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1823 | continue; |
| 1824 | |
| 1825 | isl_map *AccRel = |
| 1826 | isl_map_intersect_domain(MA->getAccessRelation(), getDomain()); |
| 1827 | isl_set *Accs = isl_map_range(AccRel); |
| 1828 | |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1829 | if (isl_set_has_equal_space(AllAccs, Accs)) { |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1830 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 1831 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 1832 | isl_set_free(OverlapAccs); |
Tobias Grosser | 55a7af7 | 2016-09-08 14:08:07 +0000 | [diff] [blame] | 1833 | } else { |
| 1834 | isl_set_free(Accs); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | isl_set_free(AllAccs); |
| 1839 | if (!Valid) |
| 1840 | continue; |
| 1841 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1842 | const LoadInst *Load = |
| 1843 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1844 | MemoryAccess::ReductionType RT = |
| 1845 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1846 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1847 | // If no overlapping access was found we mark the load and store as |
| 1848 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1849 | CandidatePair.first->markAsReductionLike(RT); |
| 1850 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1851 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1854 | std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1855 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1856 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1857 | auto *S = getSchedule(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1858 | if (!S) |
| 1859 | return ""; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1860 | auto Str = stringFromIslObj(S); |
| 1861 | isl_map_free(S); |
| 1862 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1865 | void ScopStmt::setInvalidDomain(__isl_take isl_set *ID) { |
| 1866 | isl_set_free(InvalidDomain); |
| 1867 | InvalidDomain = ID; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1870 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1871 | if (isBlockStmt()) |
| 1872 | return getBasicBlock(); |
| 1873 | return getRegion()->getEntry(); |
| 1874 | } |
| 1875 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1876 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1877 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1878 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1879 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1880 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1881 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1884 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1885 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1886 | __isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1887 | |
Tobias Grosser | 6e6c7e0 | 2015-03-30 12:22:39 +0000 | [diff] [blame] | 1888 | __isl_give isl_space *ScopStmt::getDomainSpace() const { |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1889 | return isl_set_get_space(Domain); |
| 1890 | } |
| 1891 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1892 | __isl_give isl_id *ScopStmt::getDomainId() const { |
| 1893 | return isl_set_get_tuple_id(Domain); |
| 1894 | } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1895 | |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1896 | ScopStmt::~ScopStmt() { |
| 1897 | isl_set_free(Domain); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1898 | isl_set_free(InvalidDomain); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1899 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1900 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1901 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1902 | OS << "Instructions {\n"; |
| 1903 | |
| 1904 | for (Instruction *Inst : Instructions) |
| 1905 | OS.indent(16) << *Inst << "\n"; |
| 1906 | |
| 1907 | OS.indent(16) << "}\n"; |
| 1908 | } |
| 1909 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1910 | void ScopStmt::print(raw_ostream &OS) const { |
| 1911 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1912 | OS.indent(12) << "Domain :=\n"; |
| 1913 | |
| 1914 | if (Domain) { |
| 1915 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1916 | } else |
| 1917 | OS.indent(16) << "n/a\n"; |
| 1918 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1919 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1920 | |
| 1921 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1922 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1923 | } else |
| 1924 | OS.indent(16) << "n/a\n"; |
| 1925 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1926 | for (MemoryAccess *Access : MemAccs) |
| 1927 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (PollyPrintInstructions) |
| 1930 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | void ScopStmt::dump() const { print(dbgs()); } |
| 1934 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1935 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1936 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1937 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1938 | (void)Found; |
| 1939 | assert(Found && "Expected access data not found"); |
| 1940 | } |
| 1941 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1942 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1943 | (void)Found; |
| 1944 | assert(Found && "Expected access data not found"); |
| 1945 | } |
| 1946 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1947 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1948 | (void)Found; |
| 1949 | assert(Found && "Expected access data not found"); |
| 1950 | } |
| 1951 | } |
| 1952 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1953 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1954 | // Remove the memory accesses from this statement together with all scalar |
| 1955 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1956 | // instruction, hence would not be removed by this function. However, it is |
| 1957 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1958 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1959 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1960 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1961 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1962 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1963 | for (auto *MA : MemAccs) { |
| 1964 | if (Predicate(MA)) |
| 1965 | removeAccessData(MA); |
| 1966 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1967 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1968 | MemAccs.end()); |
| 1969 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1972 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA) { |
| 1973 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 1974 | assert(MAIt != MemAccs.end()); |
| 1975 | MemAccs.erase(MAIt); |
| 1976 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1977 | removeAccessData(MA); |
| 1978 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1979 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 1980 | if (It != InstructionToAccess.end()) { |
| 1981 | It->second.remove(MA); |
| 1982 | if (It->second.empty()) |
| 1983 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1984 | } |
| 1985 | } |
| 1986 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1987 | //===----------------------------------------------------------------------===// |
| 1988 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1989 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1990 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1991 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 1992 | isl_set_free(Context); |
| 1993 | Context = NewContext; |
| 1994 | } |
| 1995 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1996 | namespace { |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1997 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1998 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1999 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2000 | ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2001 | |
| 2002 | public: |
| 2003 | SCEVSensitiveParameterRewriter(ValueToValueMap &VMap, ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2004 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2005 | |
| 2006 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
| 2007 | ValueToValueMap &VMap) { |
| 2008 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 2009 | return SSPR.visit(E); |
| 2010 | } |
| 2011 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2012 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 2013 | auto *Start = visit(E->getStart()); |
| 2014 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 2015 | visit(E->getStepRecurrence(SE)), |
| 2016 | E->getLoop(), SCEV::FlagAnyWrap); |
| 2017 | return SE.getAddExpr(Start, AddRec); |
| 2018 | } |
| 2019 | |
| 2020 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 2021 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 2022 | return SE.getUnknown(NewValue); |
| 2023 | return E; |
| 2024 | } |
| 2025 | }; |
| 2026 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2027 | /// Check whether we should remap a SCEV expression. |
| 2028 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
| 2029 | ValueToValueMap &VMap; |
| 2030 | bool FoundInside = false; |
| 2031 | Scop *S; |
| 2032 | |
| 2033 | public: |
| 2034 | SCEVFindInsideScop(ValueToValueMap &VMap, ScalarEvolution &SE, Scop *S) |
| 2035 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 2036 | |
| 2037 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
| 2038 | ValueToValueMap &VMap, Scop *S) { |
| 2039 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 2040 | SFIS.visitAll(E); |
| 2041 | return SFIS.FoundInside; |
| 2042 | } |
| 2043 | |
| 2044 | bool follow(const SCEV *E) { |
| 2045 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 2046 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 2047 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 2048 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 2049 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 2050 | } |
| 2051 | return !FoundInside; |
| 2052 | } |
| 2053 | bool isDone() { return FoundInside; } |
| 2054 | }; |
| 2055 | } // namespace |
| 2056 | |
| 2057 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) { |
| 2058 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 2059 | // doesn't like addition between an AddRec and an expression that |
| 2060 | // doesn't have a dominance relationship with it.) |
| 2061 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 2062 | return E; |
| 2063 | |
| 2064 | // Rewrite SCEV. |
| 2065 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2068 | // This table of function names is used to translate parameter names in more |
| 2069 | // human-readable names. This makes it easier to interpret Polly analysis |
| 2070 | // results. |
| 2071 | StringMap<std::string> KnownNames = { |
| 2072 | {"_Z13get_global_idj", "global_id"}, |
| 2073 | {"_Z12get_local_idj", "local_id"}, |
| 2074 | {"_Z15get_global_sizej", "global_size"}, |
| 2075 | {"_Z14get_local_sizej", "local_size"}, |
| 2076 | {"_Z12get_work_dimv", "work_dim"}, |
| 2077 | {"_Z17get_global_offsetj", "global_offset"}, |
| 2078 | {"_Z12get_group_idj", "group_id"}, |
| 2079 | {"_Z14get_num_groupsj", "num_groups"}, |
| 2080 | }; |
| 2081 | |
| 2082 | static std::string getCallParamName(CallInst *Call) { |
| 2083 | std::string Result; |
| 2084 | raw_string_ostream OS(Result); |
| 2085 | std::string Name = Call->getCalledFunction()->getName(); |
| 2086 | |
| 2087 | auto Iterator = KnownNames.find(Name); |
| 2088 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 2089 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2090 | OS << Name; |
| 2091 | for (auto &Operand : Call->arg_operands()) { |
| 2092 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 2093 | OS << "_" << Op->getValue(); |
| 2094 | } |
| 2095 | OS.flush(); |
| 2096 | return Result; |
| 2097 | } |
| 2098 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2099 | void Scop::createParameterId(const SCEV *Parameter) { |
| 2100 | assert(Parameters.count(Parameter)); |
| 2101 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2102 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2103 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2104 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2105 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 2106 | Value *Val = ValueParameter->getValue(); |
| 2107 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2108 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2109 | if (Call && isConstCall(Call)) { |
| 2110 | ParameterName = getCallParamName(Call); |
| 2111 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2112 | // If this parameter references a specific Value and this value has a name |
| 2113 | // we use this name as it is likely to be unique and more useful than just |
| 2114 | // a number. |
| 2115 | if (Val->hasName()) |
| 2116 | ParameterName = Val->getName(); |
| 2117 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 2118 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 2119 | if (LoadOrigin->hasName()) { |
| 2120 | ParameterName += "_loaded_from_"; |
| 2121 | ParameterName += |
| 2122 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 2123 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2126 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2127 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 2128 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 2129 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2130 | auto *Id = isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 2131 | const_cast<void *>((const void *)Parameter)); |
| 2132 | ParameterIds[Parameter] = Id; |
| 2133 | } |
| 2134 | |
| 2135 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2136 | for (const SCEV *Parameter : NewParameters) { |
| 2137 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2138 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2139 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2140 | |
| 2141 | if (Parameters.insert(Parameter)) |
| 2142 | createParameterId(Parameter); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) { |
| 2147 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2148 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2149 | return isl_id_copy(ParameterIds.lookup(Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2150 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2151 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 2152 | __isl_give isl_set * |
| 2153 | Scop::addNonEmptyDomainConstraints(__isl_take isl_set *C) const { |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2154 | isl_set *DomainContext = isl_union_set_params(getDomains()); |
| 2155 | return isl_set_intersect_params(C, DomainContext); |
| 2156 | } |
| 2157 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2158 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2159 | return DT.dominates(BB, getEntry()); |
| 2160 | } |
| 2161 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2162 | void Scop::addUserAssumptions( |
| 2163 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2164 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 2165 | auto &F = getFunction(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2166 | for (auto &Assumption : AC.assumptions()) { |
| 2167 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2168 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2169 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2170 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2171 | bool InScop = contains(CI); |
| 2172 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2173 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2174 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2175 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2176 | auto *Val = CI->getArgOperand(0); |
| 2177 | ParameterSetTy DetectedParams; |
| 2178 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
| 2179 | emitOptimizationRemarkAnalysis(F.getContext(), DEBUG_TYPE, F, |
| 2180 | CI->getDebugLoc(), |
| 2181 | "Non-affine user assumption ignored."); |
| 2182 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2183 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2184 | |
| 2185 | // Collect all newly introduced parameters. |
| 2186 | ParameterSetTy NewParams; |
| 2187 | for (auto *Param : DetectedParams) { |
| 2188 | Param = extractConstantFactor(Param, *SE).second; |
| 2189 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2190 | if (Parameters.count(Param)) |
| 2191 | continue; |
| 2192 | NewParams.insert(Param); |
| 2193 | } |
| 2194 | |
| 2195 | SmallVector<isl_set *, 2> ConditionSets; |
| 2196 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
| 2197 | auto &Stmt = InScop ? *getStmtFor(CI->getParent()) : *Stmts.begin(); |
| 2198 | auto *Dom = InScop ? getDomainConditions(&Stmt) : isl_set_copy(Context); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2199 | bool Valid = buildConditionSets(*this, Stmt.getEntryBlock(), Val, TI, L, |
| 2200 | Dom, InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2201 | isl_set_free(Dom); |
| 2202 | |
| 2203 | if (!Valid) |
| 2204 | continue; |
| 2205 | |
| 2206 | isl_set *AssumptionCtx = nullptr; |
| 2207 | if (InScop) { |
| 2208 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2209 | isl_set_free(ConditionSets[0]); |
| 2210 | } else { |
| 2211 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2212 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2213 | } |
| 2214 | |
| 2215 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2216 | if (!NewParams.empty()) { |
| 2217 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2218 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2219 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2220 | isl_id_free(Id); |
| 2221 | |
| 2222 | if (!NewParams.count(Param)) |
| 2223 | continue; |
| 2224 | |
| 2225 | AssumptionCtx = |
| 2226 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | emitOptimizationRemarkAnalysis( |
| 2231 | F.getContext(), DEBUG_TYPE, F, CI->getDebugLoc(), |
| 2232 | "Use user assumption: " + stringFromIslObj(AssumptionCtx)); |
| 2233 | Context = isl_set_intersect(Context, AssumptionCtx); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2237 | void Scop::addUserContext() { |
| 2238 | if (UserContextStr.empty()) |
| 2239 | return; |
| 2240 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2241 | isl_set *UserContext = |
| 2242 | isl_set_read_from_str(getIslCtx(), UserContextStr.c_str()); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2243 | isl_space *Space = getParamSpace(); |
| 2244 | if (isl_space_dim(Space, isl_dim_param) != |
| 2245 | isl_set_dim(UserContext, isl_dim_param)) { |
| 2246 | auto SpaceStr = isl_space_to_str(Space); |
| 2247 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2248 | << "number of dimensions than the computed context. Due to this " |
| 2249 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2250 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2251 | free(SpaceStr); |
| 2252 | isl_set_free(UserContext); |
| 2253 | isl_space_free(Space); |
| 2254 | return; |
| 2255 | } |
| 2256 | |
| 2257 | 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] | 2258 | auto *NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 2259 | auto *NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 2262 | auto SpaceStr = isl_space_to_str(Space); |
| 2263 | errs() << "Error: the name of dimension " << i |
| 2264 | << " provided in -polly-context " |
| 2265 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2266 | << "context is '" << NameContext |
| 2267 | << "'. Due to this name mismatch, " |
| 2268 | << "the -polly-context option is ignored. Please provide " |
| 2269 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2270 | free(SpaceStr); |
| 2271 | isl_set_free(UserContext); |
| 2272 | isl_space_free(Space); |
| 2273 | return; |
| 2274 | } |
| 2275 | |
| 2276 | UserContext = |
| 2277 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 2278 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 2279 | } |
| 2280 | |
| 2281 | Context = isl_set_intersect(Context, UserContext); |
| 2282 | isl_space_free(Space); |
| 2283 | } |
| 2284 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2285 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2286 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2287 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2288 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2289 | for (LoadInst *LInst : RIL) { |
| 2290 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2291 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2292 | Type *Ty = LInst->getType(); |
| 2293 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2294 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2295 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2296 | continue; |
| 2297 | } |
| 2298 | |
| 2299 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2300 | InvariantEquivClasses.emplace_back( |
| 2301 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2305 | void Scop::buildContext() { |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2306 | isl_space *Space = isl_space_params_alloc(getIslCtx(), 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2307 | Context = isl_set_universe(isl_space_copy(Space)); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2308 | InvalidContext = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2309 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2312 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2313 | unsigned PDim = 0; |
| 2314 | for (auto *Parameter : Parameters) { |
| 2315 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 2316 | Context = |
| 2317 | addRangeBoundsToSet(give(Context), SRange, PDim++, isl::dim::param) |
| 2318 | .release(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2322 | // We use the outermost dimension to generate GPU transfers for Fortran arrays |
| 2323 | // even when the array bounds are not known statically. To do so, we need the |
| 2324 | // outermost dimension information. We add this into the context so that the |
| 2325 | // outermost dimension is available during codegen. |
| 2326 | // We currently do not care about dimensions other than the outermost |
| 2327 | // dimension since it doesn't affect transfers. |
| 2328 | static isl_set *addFortranArrayOutermostDimParams(__isl_give isl_set *Context, |
| 2329 | Scop::array_range Arrays) { |
| 2330 | |
| 2331 | std::vector<isl_id *> OutermostSizeIds; |
| 2332 | for (auto Array : Arrays) { |
| 2333 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2334 | // for its outermost dimension. Fortran arrays will have this since the |
| 2335 | // outermost dimension size can be picked up from their runtime description. |
| 2336 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2337 | if (Array->getNumberOfDimensions() > 0) { |
| 2338 | isl_pw_aff *PwAff = Array->getDimensionSizePw(0); |
| 2339 | if (!PwAff) |
| 2340 | continue; |
| 2341 | |
| 2342 | isl_id *Id = isl_pw_aff_get_dim_id(PwAff, isl_dim_param, 0); |
| 2343 | isl_pw_aff_free(PwAff); |
| 2344 | assert(Id && "Invalid Id for PwAff expression in Fortran array"); |
| 2345 | OutermostSizeIds.push_back(Id); |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | const int NumTrueParams = isl_set_dim(Context, isl_dim_param); |
| 2350 | Context = isl_set_add_dims(Context, isl_dim_param, OutermostSizeIds.size()); |
| 2351 | |
| 2352 | for (size_t i = 0; i < OutermostSizeIds.size(); i++) { |
| 2353 | Context = isl_set_set_dim_id(Context, isl_dim_param, NumTrueParams + i, |
| 2354 | OutermostSizeIds[i]); |
| 2355 | Context = |
| 2356 | isl_set_lower_bound_si(Context, isl_dim_param, NumTrueParams + i, 0); |
| 2357 | } |
| 2358 | |
| 2359 | return Context; |
| 2360 | } |
| 2361 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2362 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2363 | if (PollyIgnoreParamBounds) |
| 2364 | return; |
| 2365 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2366 | // Add all parameters into a common model. |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2367 | isl_space *Space = isl_space_params_alloc(getIslCtx(), ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2368 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2369 | unsigned PDim = 0; |
| 2370 | for (const auto *Parameter : Parameters) { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2371 | isl_id *id = getIdForParam(Parameter); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2372 | Space = isl_space_set_dim_id(Space, isl_dim_param, PDim++, id); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | // Align the parameters of all data structures to the model. |
| 2376 | Context = isl_set_align_params(Context, Space); |
| 2377 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2378 | // Add the outermost dimension of the Fortran arrays into the Context. |
| 2379 | // See the description of the function for more information. |
| 2380 | Context = addFortranArrayOutermostDimParams(Context, arrays()); |
| 2381 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2382 | // As all parameters are known add bounds to them. |
| 2383 | addParameterBounds(); |
| 2384 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2385 | for (ScopStmt &Stmt : *this) |
| 2386 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2387 | // Simplify the schedule according to the context too. |
| 2388 | Schedule = isl_schedule_gist_domain_params(Schedule, getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2391 | static __isl_give isl_set * |
| 2392 | simplifyAssumptionContext(__isl_take isl_set *AssumptionContext, |
| 2393 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2394 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2395 | // simplify the context with the constraints that are needed for anything to |
| 2396 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2397 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2398 | // domains, thus we cannot use the remaining domain to simplify the |
| 2399 | // assumptions. |
| 2400 | if (!S.hasErrorBlock()) { |
| 2401 | isl_set *DomainParameters = isl_union_set_params(S.getDomains()); |
| 2402 | AssumptionContext = |
| 2403 | isl_set_gist_params(AssumptionContext, DomainParameters); |
| 2404 | } |
| 2405 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2406 | AssumptionContext = isl_set_gist_params(AssumptionContext, S.getContext()); |
| 2407 | return AssumptionContext; |
| 2408 | } |
| 2409 | |
| 2410 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2411 | // The parameter constraints of the iteration domains give us a set of |
| 2412 | // constraints that need to hold for all cases where at least a single |
| 2413 | // statement iteration is executed in the whole scop. We now simplify the |
| 2414 | // assumed context under the assumption that such constraints hold and at |
| 2415 | // least a single statement iteration is executed. For cases where no |
| 2416 | // statement instances are executed, the assumptions we have taken about |
| 2417 | // the executed code do not matter and can be changed. |
| 2418 | // |
| 2419 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2420 | // the set of statement instances that are executed. Otherwise we |
| 2421 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2422 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2423 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2424 | // performed. In such a case, modifying the run-time conditions and |
| 2425 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2426 | // to not be executed. |
| 2427 | // |
| 2428 | // Example: |
| 2429 | // |
| 2430 | // When delinearizing the following code: |
| 2431 | // |
| 2432 | // for (long i = 0; i < 100; i++) |
| 2433 | // for (long j = 0; j < m; j++) |
| 2434 | // A[i+p][j] = 1.0; |
| 2435 | // |
| 2436 | // 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] | 2437 | // 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] | 2438 | // 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] | 2439 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2440 | InvalidContext = isl_set_align_params(InvalidContext, getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2443 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2444 | static isl::stat |
| 2445 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2446 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2447 | isl::pw_aff LastDimAff; |
| 2448 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2449 | unsigned Pos; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2450 | isl::ctx Ctx = Set.get_ctx(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2451 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2452 | Set = Set.remove_divs(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2453 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2454 | if (isl_set_n_basic_set(Set.get()) >= MaxDisjunctsInDomain) |
| 2455 | return isl::stat::error; |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2456 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2457 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2458 | // lexmax computation will take too long if this number is high. |
| 2459 | // |
| 2460 | // Experiments with a simple test case using an i7 4800MQ: |
| 2461 | // |
| 2462 | // #Parameters involved | Time (in sec) |
| 2463 | // 6 | 0.01 |
| 2464 | // 7 | 0.04 |
| 2465 | // 8 | 0.12 |
| 2466 | // 9 | 0.40 |
| 2467 | // 10 | 1.54 |
| 2468 | // 11 | 6.78 |
| 2469 | // 12 | 30.38 |
| 2470 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2471 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2472 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2473 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2474 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2475 | InvolvedParams++; |
| 2476 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2477 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2478 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 2481 | if (isl_set_n_basic_set(Set.get()) > RunTimeChecksMaxAccessDisjuncts) |
| 2482 | return isl::stat::error; |
| 2483 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2484 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2485 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2486 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2487 | if (isl_ctx_last_error(Ctx.get()) == isl_error_quota) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2488 | return isl::stat::error; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2489 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2490 | MinPMA = MinPMA.coalesce(); |
| 2491 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2492 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2493 | // Adjust the last dimension of the maximal access by one as we want to |
| 2494 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2495 | // we test during code generation might now point after the end of the |
| 2496 | // allocated array but we will never dereference it anyway. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2497 | assert(MaxPMA.dim(isl::dim::out) && "Assumed at least one output dimension"); |
| 2498 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2499 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2500 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2501 | OneAff = OneAff.add_constant_si(1); |
| 2502 | LastDimAff = LastDimAff.add(OneAff); |
| 2503 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2504 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2505 | MinMaxAccesses.push_back(std::make_pair(MinPMA.copy(), MaxPMA.copy())); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2506 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2507 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2510 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
| 2511 | isl_set *Domain = MA->getStatement()->getDomain(); |
| 2512 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2513 | return isl_set_reset_tuple_id(Domain); |
| 2514 | } |
| 2515 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2516 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2517 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2518 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2519 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2520 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2521 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2522 | isl::union_set Domains = give(S.getDomains()); |
| 2523 | isl::union_map Accesses = isl::union_map::empty(give(S.getParamSpace())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2524 | |
| 2525 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2526 | Accesses = Accesses.add_map(give(MA->getAccessRelation())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2527 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2528 | Accesses = Accesses.intersect_domain(Domains); |
| 2529 | isl::union_set Locations = Accesses.range(); |
| 2530 | Locations = Locations.coalesce(); |
| 2531 | Locations = Locations.detect_equalities(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2532 | |
| 2533 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2534 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2535 | }; |
| 2536 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2539 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2540 | /// |
| 2541 | ///{ |
| 2542 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2543 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2544 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2545 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2546 | : RN->getNodeAs<BasicBlock>(); |
| 2547 | } |
| 2548 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2549 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2550 | static inline BasicBlock * |
| 2551 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2552 | if (RN->isSubRegion()) { |
| 2553 | assert(idx == 0); |
| 2554 | return RN->getNodeAs<Region>()->getExit(); |
| 2555 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2556 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2559 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2560 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2561 | if (!RN->isSubRegion()) { |
| 2562 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2563 | Loop *L = LI.getLoopFor(BB); |
| 2564 | |
| 2565 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2566 | // they are not part of an actual loop in the control flow graph. |
| 2567 | // Nevertheless, we handle certain unreachable statements that are common |
| 2568 | // when modeling run-time bounds checks as being part of the loop to be |
| 2569 | // able to model them and to later eliminate the run-time bounds checks. |
| 2570 | // |
| 2571 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2572 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2573 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2574 | // allows us to model the following code. |
| 2575 | // |
| 2576 | // for (i = 0; i < N; i++) { |
| 2577 | // if (i > 1024) |
| 2578 | // abort(); <- this abort might be translated to an |
| 2579 | // unreachable |
| 2580 | // |
| 2581 | // A[i] = ... |
| 2582 | // } |
| 2583 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2584 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2585 | return L; |
| 2586 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2587 | |
| 2588 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2589 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2590 | while (L && NonAffineSubRegion->contains(L)) |
| 2591 | L = L->getParentLoop(); |
| 2592 | return L; |
| 2593 | } |
| 2594 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2595 | /// Get the number of blocks in @p L. |
| 2596 | /// |
| 2597 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2598 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2599 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2600 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2601 | /// scop and represent run-time conditions which we want to model and |
| 2602 | /// subsequently speculate away. |
| 2603 | /// |
| 2604 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2605 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2606 | unsigned NumBlocks = L->getNumBlocks(); |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2607 | SmallVector<llvm::BasicBlock *, 4> ExitBlocks; |
| 2608 | L->getExitBlocks(ExitBlocks); |
| 2609 | |
| 2610 | for (auto ExitBlock : ExitBlocks) { |
| 2611 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2612 | NumBlocks++; |
| 2613 | } |
| 2614 | return NumBlocks; |
| 2615 | } |
| 2616 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2617 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2618 | if (!RN->isSubRegion()) |
| 2619 | return 1; |
| 2620 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2621 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2622 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2625 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2626 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2627 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2628 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2629 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2630 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2631 | return true; |
| 2632 | return false; |
| 2633 | } |
| 2634 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2635 | ///} |
| 2636 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2637 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 2638 | unsigned Dim, Loop *L) { |
Michael Kruse | 88a2256 | 2016-03-29 07:50:52 +0000 | [diff] [blame] | 2639 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2640 | isl_id *DimId = |
| 2641 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 2642 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 2643 | } |
| 2644 | |
Johannes Doerfert | fff283d | 2016-04-19 14:48:22 +0000 | [diff] [blame] | 2645 | __isl_give isl_set *Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2646 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Johannes Doerfert | fff283d | 2016-04-19 14:48:22 +0000 | [diff] [blame] | 2649 | __isl_give isl_set *Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2650 | auto DIt = DomainMap.find(BB); |
| 2651 | if (DIt != DomainMap.end()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2652 | return DIt->getSecond().copy(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2653 | |
| 2654 | auto &RI = *R.getRegionInfo(); |
| 2655 | auto *BBR = RI.getRegionFor(BB); |
| 2656 | while (BBR->getEntry() == BB) |
| 2657 | BBR = BBR->getParent(); |
| 2658 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2661 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2662 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2663 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2664 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2665 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2666 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2667 | int LD = getRelativeLoopDepth(L); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2668 | 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] | 2669 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2670 | while (LD-- >= 0) { |
| 2671 | S = addDomainDimId(S, LD + 1, L); |
| 2672 | L = L->getParentLoop(); |
| 2673 | } |
| 2674 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2675 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2676 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2677 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2678 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2679 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2680 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2681 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2682 | return false; |
| 2683 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2684 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2685 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2686 | |
| 2687 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2688 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2689 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2690 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2691 | // will cause problems when building up a ScopStmt for them. |
| 2692 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2693 | // 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] | 2694 | // can themselves not be constructed properly. To this end we will replace |
| 2695 | // the domains of error blocks and those only reachable via error blocks |
| 2696 | // 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] | 2697 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2698 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2699 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2700 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2701 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2702 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2705 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2706 | /// to be compatible to domains constructed for loop @p NewL. |
| 2707 | /// |
| 2708 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2709 | /// edge from @p OldL to @p NewL. |
| 2710 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2711 | __isl_take isl_set *Dom, |
| 2712 | Loop *OldL, Loop *NewL) { |
| 2713 | |
| 2714 | // If the loops are the same there is nothing to do. |
| 2715 | if (NewL == OldL) |
| 2716 | return Dom; |
| 2717 | |
| 2718 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2719 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2720 | // If both loops are non-affine loops there is nothing to do. |
| 2721 | if (OldDepth == -1 && NewDepth == -1) |
| 2722 | return Dom; |
| 2723 | |
| 2724 | // Distinguish three cases: |
| 2725 | // 1) The depth is the same but the loops are not. |
| 2726 | // => One loop was left one was entered. |
| 2727 | // 2) The depth increased from OldL to NewL. |
| 2728 | // => One loop was entered, none was left. |
| 2729 | // 3) The depth decreased from OldL to NewL. |
| 2730 | // => Loops were left were difference of the depths defines how many. |
| 2731 | if (OldDepth == NewDepth) { |
| 2732 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2733 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2734 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2735 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2736 | } else if (OldDepth < NewDepth) { |
| 2737 | assert(OldDepth + 1 == NewDepth); |
| 2738 | auto &R = S.getRegion(); |
| 2739 | (void)R; |
| 2740 | assert(NewL->getParentLoop() == OldL || |
| 2741 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2742 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2743 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2744 | } else { |
| 2745 | assert(OldDepth > NewDepth); |
| 2746 | int Diff = OldDepth - NewDepth; |
| 2747 | int NumDim = isl_set_n_dim(Dom); |
| 2748 | assert(NumDim >= Diff); |
| 2749 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2750 | } |
| 2751 | |
| 2752 | return Dom; |
| 2753 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2754 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2755 | bool Scop::propagateInvalidStmtDomains( |
| 2756 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2757 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2758 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2759 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2760 | for (auto *RN : RTraversal) { |
| 2761 | |
| 2762 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2763 | // subregions. |
| 2764 | if (RN->isSubRegion()) { |
| 2765 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2766 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2767 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2768 | continue; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2773 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2774 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2775 | assert(Domain && "Cannot propagate a nullptr"); |
| 2776 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2777 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2778 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2779 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2780 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2781 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2782 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2783 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2784 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2785 | isl::set DomPar = Domain.params(); |
| 2786 | recordAssumption(ERRORBLOCK, DomPar.release(), |
| 2787 | BB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2788 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2791 | if (InvalidDomain.is_empty()) { |
| 2792 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2793 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2796 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2797 | auto *TI = BB->getTerminator(); |
| 2798 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2799 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2800 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2803 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2804 | continue; |
| 2805 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2806 | // Skip backedges. |
| 2807 | if (DT.dominates(SuccBB, BB)) |
| 2808 | continue; |
| 2809 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2810 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2811 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2812 | auto *AdjustedInvalidDomain = adjustDomainDimensions( |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2813 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2814 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2815 | auto *SuccInvalidDomain = InvalidDomainMap[SuccBB].copy(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2816 | SuccInvalidDomain = |
| 2817 | isl_set_union(SuccInvalidDomain, AdjustedInvalidDomain); |
| 2818 | SuccInvalidDomain = isl_set_coalesce(SuccInvalidDomain); |
| 2819 | unsigned NumConjucts = isl_set_n_basic_set(SuccInvalidDomain); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2820 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2821 | InvalidDomainMap[SuccBB] = isl::manage(SuccInvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2822 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2823 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2824 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2825 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2826 | continue; |
| 2827 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2828 | InvalidDomainMap.erase(BB); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2829 | invalidate(COMPLEXITY, TI->getDebugLoc()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2830 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2831 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2832 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2833 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2834 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2835 | |
| 2836 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2839 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2840 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2841 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2842 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2843 | |
| 2844 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2845 | // domain to the exit block of the region. Otherwise we are done. |
| 2846 | auto *RI = R.getRegionInfo(); |
| 2847 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2848 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2849 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2852 | // 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] | 2853 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2854 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2855 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2856 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2857 | BBLoop->getLoopLatches(LatchBBs); |
| 2858 | for (auto *LatchBB : LatchBBs) |
| 2859 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2860 | return; |
| 2861 | L = L->getParentLoop(); |
| 2862 | } |
| 2863 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2864 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2865 | assert(Domain && "Cannot propagate a nullptr"); |
| 2866 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2867 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2868 | |
| 2869 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2870 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2871 | isl::set AdjustedDomain = isl::manage( |
| 2872 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2873 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2874 | |
| 2875 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2876 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2877 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2878 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2879 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2880 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2881 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2882 | FinishedExitBlocks.insert(ExitBB); |
| 2883 | } |
| 2884 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2885 | bool Scop::buildDomainsWithBranchConstraints( |
| 2886 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2887 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2888 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2889 | // To create the domain for each block in R we iterate over all blocks and |
| 2890 | // subregions in R and propagate the conditions under which the current region |
| 2891 | // element is executed. To this end we iterate in reverse post order over R as |
| 2892 | // it ensures that we first visit all predecessors of a region node (either a |
| 2893 | // basic block or a subregion) before we visit the region node itself. |
| 2894 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2895 | // there we propagate the current domain to all successors, however we add the |
| 2896 | // condition that the successor is actually executed next. |
| 2897 | // As we are only interested in non-loop carried constraints here we can |
| 2898 | // simply skip loop back edges. |
| 2899 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2900 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2901 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2902 | for (auto *RN : RTraversal) { |
| 2903 | |
| 2904 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2905 | // subregions. |
| 2906 | if (RN->isSubRegion()) { |
| 2907 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2908 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2909 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2910 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2911 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2912 | continue; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2916 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2917 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2918 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2919 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2920 | TerminatorInst *TI = BB->getTerminator(); |
| 2921 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2922 | if (isa<UnreachableInst>(TI)) |
| 2923 | continue; |
| 2924 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2925 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2926 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2927 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2928 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2929 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2930 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2931 | // Propagate the domain from BB directly to blocks that have a superset |
| 2932 | // 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] | 2933 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2934 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2935 | |
| 2936 | // If all successors of BB have been set a domain through the propagation |
| 2937 | // above we do not need to build condition sets but can just skip this |
| 2938 | // block. However, it is important to note that this is a local property |
| 2939 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 2940 | // local variable. |
| 2941 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 2942 | return FinishedExitBlocks.count(SuccBB); |
| 2943 | }; |
| 2944 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 2945 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2946 | |
| 2947 | // Build the condition sets for the successor nodes of the current region |
| 2948 | // node. If it is a non-affine subregion we will always execute the single |
| 2949 | // exit node, hence the single entry node domain is the condition set. For |
| 2950 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2951 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2952 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2953 | ConditionSets.push_back(Domain.copy()); |
| 2954 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2955 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2956 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2957 | |
| 2958 | // Now iterate over the successors and set their initial domain based on |
| 2959 | // their condition set. We skip back edges here and have to be careful when |
| 2960 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 2961 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2962 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2963 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2964 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2965 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2966 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2967 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2968 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2969 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2970 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2971 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 2972 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2973 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2974 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2975 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2976 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2977 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2978 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2979 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2980 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2981 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2982 | CondSet = isl::manage( |
| 2983 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Set the domain for the successor or merge it with an existing domain in |
| 2986 | // case there are multiple paths (without loop back edges) to the |
| 2987 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2988 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 2989 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2990 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2991 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2992 | } else { |
| 2993 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2994 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2995 | SuccDomain = CondSet; |
| 2996 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2997 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 2998 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 2999 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 3000 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3001 | // In case this happens we will clean up and bail. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3002 | if (isl_set_n_basic_set(SuccDomain.get()) < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3003 | continue; |
| 3004 | |
| 3005 | invalidate(COMPLEXITY, DebugLoc()); |
| 3006 | while (++u < ConditionSets.size()) |
| 3007 | isl_set_free(ConditionSets[u]); |
| 3008 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3011 | |
| 3012 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Michael Kruse | d56b90a | 2016-09-01 09:03:27 +0000 | [diff] [blame] | 3015 | __isl_give isl_set * |
| 3016 | Scop::getPredecessorDomainConstraints(BasicBlock *BB, |
| 3017 | __isl_keep isl_set *Domain, |
| 3018 | DominatorTree &DT, LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3019 | // If @p BB is the ScopEntry we are done |
| 3020 | if (R.getEntry() == BB) |
| 3021 | return isl_set_universe(isl_set_get_space(Domain)); |
| 3022 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3023 | // The region info of this function. |
| 3024 | auto &RI = *R.getRegionInfo(); |
| 3025 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3026 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3027 | |
| 3028 | // A domain to collect all predecessor domains, thus all conditions under |
| 3029 | // which the block is executed. To this end we start with the empty domain. |
| 3030 | isl_set *PredDom = isl_set_empty(isl_set_get_space(Domain)); |
| 3031 | |
| 3032 | // Set of regions of which the entry block domain has been propagated to BB. |
| 3033 | // all predecessors inside any of the regions can be skipped. |
| 3034 | SmallSet<Region *, 8> PropagatedRegions; |
| 3035 | |
| 3036 | for (auto *PredBB : predecessors(BB)) { |
| 3037 | // Skip backedges. |
| 3038 | if (DT.dominates(BB, PredBB)) |
| 3039 | continue; |
| 3040 | |
| 3041 | // If the predecessor is in a region we used for propagation we can skip it. |
| 3042 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 3043 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 3044 | PredBBInRegion)) { |
| 3045 | continue; |
| 3046 | } |
| 3047 | |
| 3048 | // Check if there is a valid region we can use for propagation, thus look |
| 3049 | // for a region that contains the predecessor and has @p BB as exit block. |
| 3050 | auto *PredR = RI.getRegionFor(PredBB); |
| 3051 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 3052 | PredR->getParent(); |
| 3053 | |
| 3054 | // If a valid region for propagation was found use the entry of that region |
| 3055 | // for propagation, otherwise the PredBB directly. |
| 3056 | if (PredR->getExit() == BB) { |
| 3057 | PredBB = PredR->getEntry(); |
| 3058 | PropagatedRegions.insert(PredR); |
| 3059 | } |
| 3060 | |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 3061 | auto *PredBBDom = getDomainConditions(PredBB); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3062 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 3063 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3064 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 3065 | |
| 3066 | PredDom = isl_set_union(PredDom, PredBBDom); |
| 3067 | } |
| 3068 | |
| 3069 | return PredDom; |
| 3070 | } |
| 3071 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3072 | bool Scop::propagateDomainConstraints( |
| 3073 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3074 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3075 | // Iterate over the region R and propagate the domain constrains from the |
| 3076 | // predecessors to the current node. In contrast to the |
| 3077 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 3078 | // information from the predecessors instead of pushing it to the successors. |
| 3079 | // Additionally, we assume the domains to be already present in the domain |
| 3080 | // map here. However, we iterate again in reverse post order so we know all |
| 3081 | // predecessors have been visited before a block or non-affine subregion is |
| 3082 | // visited. |
| 3083 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3084 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3085 | for (auto *RN : RTraversal) { |
| 3086 | |
| 3087 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 3088 | // subregions. |
| 3089 | if (RN->isSubRegion()) { |
| 3090 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3091 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3092 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3093 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3094 | continue; |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3099 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 3100 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3101 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3102 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3103 | isl::set PredDom = |
| 3104 | isl::manage(getPredecessorDomainConstraints(BB, Domain.get(), DT, LI)); |
| 3105 | Domain = Domain.intersect(PredDom).coalesce(); |
| 3106 | Domain = Domain.align_params(isl::manage(getParamSpace())); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3107 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3108 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3109 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3110 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3111 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3112 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3113 | |
| 3114 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3117 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 3118 | /// |
| 3119 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 3120 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3121 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3122 | /// |
| 3123 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3124 | static __isl_give isl_map * |
| 3125 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 3126 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 3127 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
Tobias Grosser | f4fe34b | 2017-03-16 21:33:20 +0000 | [diff] [blame] | 3128 | 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] | 3129 | if (u != Dim) |
| 3130 | NextIterationMap = |
| 3131 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 3132 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 3133 | C = isl_constraint_set_constant_si(C, 1); |
| 3134 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 3135 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 3136 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 3137 | return NextIterationMap; |
| 3138 | } |
| 3139 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3140 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3141 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3142 | int LoopDepth = getRelativeLoopDepth(L); |
| 3143 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3144 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3145 | BasicBlock *HeaderBB = L->getHeader(); |
| 3146 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3147 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3148 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3149 | isl::map NextIterationMap = isl::manage( |
| 3150 | createNextIterationMap(HeaderBBDom.get_space().release(), LoopDepth)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3151 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3152 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3153 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3154 | SmallVector<llvm::BasicBlock *, 4> LatchBlocks; |
| 3155 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3156 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3157 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3158 | |
| 3159 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3160 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3161 | if (!LatchBBDom) |
| 3162 | continue; |
| 3163 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3164 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3165 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3166 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 3167 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 3168 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 3169 | |
| 3170 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3171 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3172 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3173 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3174 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3175 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 3176 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3177 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3178 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3179 | // Free the non back edge condition set as we do not need it. |
| 3180 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3181 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3182 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3183 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3184 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3185 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3186 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3187 | BackedgeCondition = BackedgeCondition.project_out( |
| 3188 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3189 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3190 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3191 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3192 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3193 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3194 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3195 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3196 | isl::set UnionBackedgeConditionComplement = |
| 3197 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3198 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3199 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3200 | 0); |
| 3201 | UnionBackedgeConditionComplement = |
| 3202 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3203 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3204 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3205 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame^] | 3206 | auto Parts = partitionSetParts(HeaderBBDom.copy(), LoopDepth); |
| 3207 | HeaderBBDom = isl::manage(Parts.second); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3208 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3209 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3210 | // the bounded assumptions to the context as they are already implied by the |
| 3211 | // <nsw> tag. |
| 3212 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 3213 | isl_set_free(Parts.first); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3214 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3217 | isl_set *UnboundedCtx = isl_set_params(Parts.first); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3218 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3219 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3220 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3223 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3224 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3225 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3226 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3227 | if (!PointerBaseInst) |
| 3228 | return nullptr; |
| 3229 | |
| 3230 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3231 | if (!BasePtrStmt) |
| 3232 | return nullptr; |
| 3233 | |
| 3234 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3235 | } |
| 3236 | |
| 3237 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3238 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3239 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3240 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3241 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3242 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3243 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3244 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3245 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3246 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3247 | |
| 3248 | return false; |
| 3249 | } |
| 3250 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3251 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3252 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3253 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3254 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3255 | if (buildAliasGroups(AA)) { |
| 3256 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3257 | // collect statistics so we do it here explicitly. |
| 3258 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3259 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3260 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3261 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3262 | |
| 3263 | // If a problem occurs while building the alias groups we need to delete |
| 3264 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3265 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3266 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3267 | |
| 3268 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3269 | << " could not be created as the number of parameters involved " |
| 3270 | "is too high. The SCoP will be " |
| 3271 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3272 | "the maximal number of parameters but be advised that the " |
| 3273 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3274 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3277 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3278 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3279 | AliasSetTracker AST(AA); |
| 3280 | |
| 3281 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3282 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3283 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3284 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3285 | isl_set *StmtDomain = Stmt.getDomain(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3286 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3287 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3288 | |
| 3289 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3290 | if (StmtDomainEmpty) |
| 3291 | continue; |
| 3292 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3293 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3294 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3295 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3296 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3297 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3298 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3299 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3300 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3301 | else |
| 3302 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3303 | AST.add(Acc); |
| 3304 | } |
| 3305 | } |
| 3306 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3307 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3308 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3309 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3310 | continue; |
| 3311 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3312 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3313 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3314 | if (AG.size() < 2) |
| 3315 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3316 | AliasGroups.push_back(std::move(AG)); |
| 3317 | } |
| 3318 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3319 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3320 | } |
| 3321 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3322 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3323 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3324 | AliasGroupTy NewAG; |
| 3325 | AliasGroupTy &AG = AliasGroups[u]; |
| 3326 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3327 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3328 | while (AGI != AG.end()) { |
| 3329 | MemoryAccess *MA = *AGI; |
| 3330 | isl_set *MADomain = getAccessDomain(MA); |
| 3331 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3332 | NewAG.push_back(MA); |
| 3333 | AGI = AG.erase(AGI); |
| 3334 | isl_set_free(MADomain); |
| 3335 | } else { |
| 3336 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3337 | AGI++; |
| 3338 | } |
| 3339 | } |
| 3340 | if (NewAG.size() > 1) |
| 3341 | AliasGroups.push_back(std::move(NewAG)); |
| 3342 | isl_set_free(AGDomain); |
| 3343 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3347 | // To create sound alias checks we perform the following steps: |
| 3348 | // o) We partition each group into read only and non read only accesses. |
| 3349 | // o) For each group with more than one base pointer we then compute minimal |
| 3350 | // and maximal accesses to each array of a group in read only and non |
| 3351 | // read only partitions separately. |
| 3352 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3353 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3354 | |
| 3355 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3356 | |
| 3357 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3358 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3359 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3360 | if (!hasFeasibleRuntimeContext()) |
| 3361 | return false; |
| 3362 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3363 | { |
| 3364 | IslMaxOperationsGuard MaxOpGuard(getIslCtx(), OptComputeOut); |
| 3365 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3366 | if (!Valid) |
| 3367 | return false; |
| 3368 | } |
| 3369 | if (isl_ctx_last_error(getIslCtx()) == isl_error_quota) { |
| 3370 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3371 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3372 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3373 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3374 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3375 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3378 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3379 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3380 | AliasGroupTy ReadOnlyAccesses; |
| 3381 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3382 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3383 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3384 | |
| 3385 | auto &F = getFunction(); |
| 3386 | |
| 3387 | if (AliasGroup.size() < 2) |
| 3388 | return true; |
| 3389 | |
| 3390 | for (MemoryAccess *Access : AliasGroup) { |
| 3391 | emitOptimizationRemarkAnalysis( |
| 3392 | F.getContext(), DEBUG_TYPE, F, |
| 3393 | Access->getAccessInstruction()->getDebugLoc(), |
| 3394 | "Possibly aliasing pointer, use restrict keyword."); |
| 3395 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3396 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3397 | if (HasWriteAccess.count(Array)) { |
| 3398 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3399 | ReadWriteAccesses.push_back(Access); |
| 3400 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3401 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3402 | ReadOnlyAccesses.push_back(Access); |
| 3403 | } |
| 3404 | } |
| 3405 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3406 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3407 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3408 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3409 | return true; |
| 3410 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3411 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3412 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3413 | return true; |
| 3414 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3415 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3416 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3417 | for (MemoryAccess *MA : AliasGroup) { |
| 3418 | if (!MA->isAffine()) { |
| 3419 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc()); |
| 3420 | return false; |
| 3421 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3425 | // their base pointers are available. |
| 3426 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3427 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3428 | addRequiredInvariantLoad( |
| 3429 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3430 | } |
| 3431 | |
| 3432 | MinMaxAliasGroups.emplace_back(); |
| 3433 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3434 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3435 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3436 | |
| 3437 | bool Valid; |
| 3438 | |
| 3439 | Valid = |
| 3440 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3441 | |
| 3442 | if (!Valid) |
| 3443 | return false; |
| 3444 | |
| 3445 | // Bail out if the number of values we need to compare is too large. |
| 3446 | // This is important as the number of comparisons grows quadratically with |
| 3447 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3448 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3449 | RunTimeChecksMaxArraysPerGroup) |
| 3450 | return false; |
| 3451 | |
| 3452 | Valid = |
| 3453 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3454 | |
| 3455 | if (!Valid) |
| 3456 | return false; |
| 3457 | |
| 3458 | return true; |
| 3459 | } |
| 3460 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3461 | /// 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] | 3462 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3463 | // Start with the smallest loop containing the entry and expand that |
| 3464 | // loop until it contains all blocks in the region. If there is a loop |
| 3465 | // containing all blocks in the region check if it is itself contained |
| 3466 | // and if so take the parent loop as it will be the smallest containing |
| 3467 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3468 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3469 | while (L) { |
| 3470 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3471 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3472 | AllContained &= L->contains(BB); |
| 3473 | if (AllContained) |
| 3474 | break; |
| 3475 | L = L->getParentLoop(); |
| 3476 | } |
| 3477 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3478 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3481 | int Scop::NextScopID = 0; |
| 3482 | |
| 3483 | std::string Scop::CurrentFunc = ""; |
| 3484 | |
| 3485 | int Scop::getNextID(std::string ParentFunc) { |
| 3486 | if (ParentFunc != CurrentFunc) { |
| 3487 | CurrentFunc = ParentFunc; |
| 3488 | NextScopID = 0; |
| 3489 | } |
| 3490 | return NextScopID++; |
| 3491 | } |
| 3492 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3493 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Johannes Doerfert | 1dafea4 | 2016-05-23 09:07:08 +0000 | [diff] [blame] | 3494 | ScopDetection::DetectionContext &DC) |
Philip Pfaffe | 35bdcaf | 2017-05-15 13:43:01 +0000 | [diff] [blame] | 3495 | : SE(&ScalarEvolution), R(R), name(R.getNameStr()), IsOptimized(false), |
Siddharth Bhat | 47c7237 | 2017-07-05 15:07:28 +0000 | [diff] [blame] | 3496 | HasSingleExitEdge(R.getExitingBlock()), HasErrorBlock(false), |
| 3497 | MaxLoopDepth(0), CopyStmtsNum(0), SkipScop(false), DC(DC), |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 3498 | IslCtx(isl_ctx_alloc(), isl_ctx_free), Context(nullptr), |
| 3499 | Affinator(this, LI), AssumedContext(nullptr), InvalidContext(nullptr), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3500 | Schedule(nullptr), |
| 3501 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3502 | if (IslOnErrorAbort) |
| 3503 | isl_options_set_on_error(getIslCtx(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3504 | buildContext(); |
| 3505 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3506 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3507 | void Scop::foldSizeConstantsToRight() { |
| 3508 | isl_union_set *Accessed = isl_union_map_range(getAccesses()); |
| 3509 | |
| 3510 | for (auto Array : arrays()) { |
| 3511 | if (Array->getNumberOfDimensions() <= 1) |
| 3512 | continue; |
| 3513 | |
| 3514 | isl_space *Space = Array->getSpace(); |
| 3515 | |
| 3516 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3517 | |
| 3518 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3519 | isl_space_free(Space); |
| 3520 | continue; |
| 3521 | } |
| 3522 | |
| 3523 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3524 | |
| 3525 | isl_map *Transform = |
| 3526 | isl_map_universe(isl_space_map_from_set(Array->getSpace())); |
| 3527 | |
| 3528 | std::vector<int> Int; |
| 3529 | |
| 3530 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3531 | for (int i = 0; i < Dims; i++) { |
| 3532 | isl_set *DimOnly = |
| 3533 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3534 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3535 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3536 | |
| 3537 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3538 | |
| 3539 | if (i == Dims - 1) { |
| 3540 | Int.push_back(1); |
| 3541 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3542 | isl_basic_set_free(DimHull); |
| 3543 | continue; |
| 3544 | } |
| 3545 | |
| 3546 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3547 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3548 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3549 | isl_aff_free(Diff); |
| 3550 | |
| 3551 | int ValInt = 1; |
| 3552 | |
| 3553 | if (isl_val_is_int(Val)) |
| 3554 | ValInt = isl_val_get_num_si(Val); |
| 3555 | isl_val_free(Val); |
| 3556 | |
| 3557 | Int.push_back(ValInt); |
| 3558 | |
| 3559 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3560 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3561 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3562 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3563 | Transform = isl_map_add_constraint(Transform, C); |
| 3564 | isl_basic_set_free(DimHull); |
| 3565 | continue; |
| 3566 | } |
| 3567 | |
| 3568 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3569 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3570 | |
| 3571 | int ValInt = 1; |
| 3572 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3573 | ValInt = 0; |
| 3574 | } |
| 3575 | |
| 3576 | Int.push_back(ValInt); |
| 3577 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3578 | isl_basic_set_free(DimHull); |
| 3579 | isl_basic_set_free(ZeroSet); |
| 3580 | } |
| 3581 | |
| 3582 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3583 | |
| 3584 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3585 | isl_set_free(Elements); |
| 3586 | isl_set_free(MappedElements); |
| 3587 | isl_map_free(Transform); |
| 3588 | continue; |
| 3589 | } |
| 3590 | |
| 3591 | isl_set_free(MappedElements); |
| 3592 | |
| 3593 | bool CanFold = true; |
| 3594 | |
| 3595 | if (Int[0] <= 1) |
| 3596 | CanFold = false; |
| 3597 | |
| 3598 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3599 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3600 | if (Int[0] != Int[i] && Int[i]) |
| 3601 | CanFold = false; |
| 3602 | |
| 3603 | if (!CanFold) { |
| 3604 | isl_set_free(Elements); |
| 3605 | isl_map_free(Transform); |
| 3606 | continue; |
| 3607 | } |
| 3608 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3609 | for (auto &Access : AccessFunctions) |
| 3610 | if (Access->getScopArrayInfo() == Array) |
| 3611 | Access->setAccessRelation(isl_map_apply_range( |
| 3612 | Access->getAccessRelation(), isl_map_copy(Transform))); |
| 3613 | |
| 3614 | isl_map_free(Transform); |
| 3615 | |
| 3616 | std::vector<const SCEV *> Sizes; |
| 3617 | for (unsigned i = 0; i < NumDims; i++) { |
| 3618 | auto Size = Array->getDimensionSize(i); |
| 3619 | |
| 3620 | if (i == NumDims - 1) |
| 3621 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3622 | Sizes.push_back(Size); |
| 3623 | } |
| 3624 | |
| 3625 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3626 | |
| 3627 | isl_set_free(Elements); |
| 3628 | } |
| 3629 | isl_union_set_free(Accessed); |
| 3630 | return; |
| 3631 | } |
| 3632 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3633 | void Scop::markFortranArrays() { |
| 3634 | for (ScopStmt &Stmt : Stmts) { |
| 3635 | for (MemoryAccess *MemAcc : Stmt) { |
| 3636 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3637 | if (!FAD) |
| 3638 | continue; |
| 3639 | |
| 3640 | // TODO: const_cast-ing to edit |
| 3641 | ScopArrayInfo *SAI = |
| 3642 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3643 | assert(SAI && "memory access into a Fortran array does not " |
| 3644 | "have an associated ScopArrayInfo"); |
| 3645 | SAI->applyAndSetFAD(FAD); |
| 3646 | } |
| 3647 | } |
| 3648 | } |
| 3649 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3650 | void Scop::finalizeAccesses() { |
| 3651 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3652 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3653 | foldAccessRelations(); |
| 3654 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3655 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3658 | Scop::~Scop() { |
| 3659 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 3660 | isl_set_free(AssumedContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3661 | isl_set_free(InvalidContext); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3662 | isl_schedule_free(Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3663 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 3664 | for (auto &It : ParameterIds) |
| 3665 | isl_id_free(It.second); |
| 3666 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3667 | for (auto &AS : RecordedAssumptions) |
| 3668 | isl_set_free(AS.Set); |
| 3669 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3670 | // Free the alias groups |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3671 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3672 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3673 | isl_pw_multi_aff_free(MMA.first); |
| 3674 | isl_pw_multi_aff_free(MMA.second); |
| 3675 | } |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3676 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3677 | isl_pw_multi_aff_free(MMA.first); |
| 3678 | isl_pw_multi_aff_free(MMA.second); |
| 3679 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3680 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3681 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3682 | for (const auto &IAClass : InvariantEquivClasses) |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3683 | isl_set_free(IAClass.ExecutionContext); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3684 | |
| 3685 | // Explicitly release all Scop objects and the underlying isl objects before |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3686 | // we release the isl context. |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3687 | Stmts.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3688 | ScopArrayInfoSet.clear(); |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 3689 | ScopArrayInfoMap.clear(); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3690 | ScopArrayNameMap.clear(); |
Roman Gareev | e2ee79a | 2016-08-21 11:09:19 +0000 | [diff] [blame] | 3691 | AccessFunctions.clear(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3694 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3695 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3696 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3697 | for (ScopStmt &Stmt : *this) |
| 3698 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3699 | if (!Access->isArrayKind()) |
| 3700 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3701 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3702 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3703 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3704 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3705 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3706 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3707 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3708 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3709 | DivisibleSize /= 2; |
| 3710 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3711 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3714 | for (auto &Stmt : *this) |
| 3715 | for (auto &Access : Stmt) |
| 3716 | Access->updateDimensionality(); |
| 3717 | } |
| 3718 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3719 | void Scop::foldAccessRelations() { |
| 3720 | for (auto &Stmt : *this) |
| 3721 | for (auto &Access : Stmt) |
| 3722 | Access->foldAccessRelation(); |
| 3723 | } |
| 3724 | |
| 3725 | void Scop::assumeNoOutOfBounds() { |
| 3726 | for (auto &Stmt : *this) |
| 3727 | for (auto &Access : Stmt) |
| 3728 | Access->assumeNoOutOfBound(); |
| 3729 | } |
| 3730 | |
Michael Kruse | 977d38b | 2016-07-22 17:31:17 +0000 | [diff] [blame] | 3731 | void Scop::simplifySCoP(bool AfterHoisting) { |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3732 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3733 | ScopStmt &Stmt = *StmtIt; |
| 3734 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3735 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3736 | if (!RemoveStmt) |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 3737 | RemoveStmt = !DomainMap[Stmt.getEntryBlock()]; |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3738 | |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3739 | // Remove read only statements only after invariant loop hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3740 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3741 | bool OnlyRead = true; |
| 3742 | for (MemoryAccess *MA : Stmt) { |
| 3743 | if (MA->isRead()) |
| 3744 | continue; |
| 3745 | |
| 3746 | OnlyRead = false; |
| 3747 | break; |
| 3748 | } |
| 3749 | |
| 3750 | RemoveStmt = OnlyRead; |
| 3751 | } |
| 3752 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3753 | if (!RemoveStmt) { |
| 3754 | StmtIt++; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3755 | continue; |
| 3756 | } |
| 3757 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3758 | // Remove the statement because it is unnecessary. |
| 3759 | if (Stmt.isRegionStmt()) |
| 3760 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) |
| 3761 | StmtMap.erase(BB); |
| 3762 | else |
| 3763 | StmtMap.erase(Stmt.getBasicBlock()); |
| 3764 | |
| 3765 | StmtIt = Stmts.erase(StmtIt); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3769 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3770 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3771 | if (!LInst) |
| 3772 | return nullptr; |
| 3773 | |
| 3774 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3775 | LInst = cast<LoadInst>(Rep); |
| 3776 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3777 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3778 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3779 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3780 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3781 | continue; |
| 3782 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3783 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3784 | for (auto *MA : MAs) |
| 3785 | if (MA->getAccessInstruction() == Val) |
| 3786 | return &IAClass; |
| 3787 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3788 | |
| 3789 | return nullptr; |
| 3790 | } |
| 3791 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3792 | /// Check if @p MA can always be hoisted without execution context. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3793 | static bool canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3794 | bool MAInvalidCtxIsEmpty, |
| 3795 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3796 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3797 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
| 3798 | // TODO: We can provide more information for better but more expensive |
| 3799 | // results. |
| 3800 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3801 | LInst->getAlignment(), DL)) |
| 3802 | return false; |
| 3803 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3804 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3805 | // |
| 3806 | // TODO: This is probably to conservative. |
| 3807 | if (!NonHoistableCtxIsEmpty) |
| 3808 | return false; |
| 3809 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3810 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3811 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3812 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3813 | return true; |
| 3814 | |
| 3815 | // 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] | 3816 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3817 | // statement domain. |
| 3818 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3819 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3820 | return false; |
| 3821 | return true; |
| 3822 | } |
| 3823 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3824 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3825 | |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3826 | if (InvMAs.empty()) |
| 3827 | return; |
| 3828 | |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3829 | auto *StmtInvalidCtx = Stmt.getInvalidContext(); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3830 | bool StmtInvalidCtxIsEmpty = isl_set_is_empty(StmtInvalidCtx); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3831 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3832 | // Get the context under which the statement is executed but remove the error |
| 3833 | // context under which this statement is reached. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3834 | isl_set *DomainCtx = isl_set_params(Stmt.getDomain()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3835 | DomainCtx = isl_set_subtract(DomainCtx, StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3836 | |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 3837 | if (isl_set_n_basic_set(DomainCtx) >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3838 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3839 | invalidate(COMPLEXITY, AccInst->getDebugLoc()); |
| 3840 | isl_set_free(DomainCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3841 | for (auto &InvMA : InvMAs) |
| 3842 | isl_set_free(InvMA.NonHoistableCtx); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3843 | return; |
| 3844 | } |
| 3845 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3846 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3847 | // we could have cyclic dependences on the constraints under which the |
| 3848 | // hoisted loads are executed and we could not determine an order in which to |
| 3849 | // pre-load them. This happens because not only lower bounds are part of the |
| 3850 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3851 | for (auto &InvMA : InvMAs) { |
| 3852 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3853 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3854 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3855 | SetVector<Value *> Values; |
| 3856 | for (const SCEV *Parameter : Parameters) { |
| 3857 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3858 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3859 | if (!Values.count(AccInst)) |
| 3860 | continue; |
| 3861 | |
| 3862 | if (isl_id *ParamId = getIdForParam(Parameter)) { |
| 3863 | 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] | 3864 | if (Dim >= 0) |
| 3865 | DomainCtx = isl_set_eliminate(DomainCtx, isl_dim_param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3866 | isl_id_free(ParamId); |
| 3867 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3868 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3872 | for (auto &InvMA : InvMAs) { |
| 3873 | auto *MA = InvMA.MA; |
| 3874 | auto *NHCtx = InvMA.NonHoistableCtx; |
| 3875 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3876 | // Check for another invariant access that accesses the same location as |
| 3877 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3878 | // class at the end of InvariantEquivClasses. |
| 3879 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3880 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3881 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3882 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3883 | auto *MAInvalidCtx = MA->getInvalidContext(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3884 | bool NonHoistableCtxIsEmpty = isl_set_is_empty(NHCtx); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3885 | bool MAInvalidCtxIsEmpty = isl_set_is_empty(MAInvalidCtx); |
| 3886 | |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3887 | isl_set *MACtx; |
| 3888 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3889 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3890 | NonHoistableCtxIsEmpty)) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3891 | MACtx = isl_set_universe(isl_set_get_space(DomainCtx)); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3892 | isl_set_free(MAInvalidCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3893 | isl_set_free(NHCtx); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3894 | } else { |
| 3895 | MACtx = isl_set_copy(DomainCtx); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3896 | MACtx = isl_set_subtract(MACtx, isl_set_union(MAInvalidCtx, NHCtx)); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3897 | MACtx = isl_set_gist_params(MACtx, getContext()); |
| 3898 | } |
| 3899 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3900 | bool Consolidated = false; |
| 3901 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3902 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3903 | continue; |
| 3904 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3905 | // If the pointer and the type is equal check if the access function wrt. |
| 3906 | // to the domain is equal too. It can happen that the domain fixes |
| 3907 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3908 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3909 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3910 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3911 | if (!MAs.empty()) { |
| 3912 | auto *LastMA = MAs.front(); |
| 3913 | |
| 3914 | auto *AR = isl_map_range(MA->getAccessRelation()); |
| 3915 | auto *LastAR = isl_map_range(LastMA->getAccessRelation()); |
| 3916 | bool SameAR = isl_set_is_equal(AR, LastAR); |
| 3917 | isl_set_free(AR); |
| 3918 | isl_set_free(LastAR); |
| 3919 | |
| 3920 | if (!SameAR) |
| 3921 | continue; |
| 3922 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3923 | |
| 3924 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3925 | MAs.push_front(MA); |
| 3926 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3927 | Consolidated = true; |
| 3928 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3929 | // Unify the execution context of the class and this statement. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3930 | isl_set *&IAClassDomainCtx = IAClass.ExecutionContext; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3931 | if (IAClassDomainCtx) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3932 | IAClassDomainCtx = |
| 3933 | isl_set_coalesce(isl_set_union(IAClassDomainCtx, MACtx)); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3934 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3935 | IAClassDomainCtx = MACtx; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3936 | break; |
| 3937 | } |
| 3938 | |
| 3939 | if (Consolidated) |
| 3940 | continue; |
| 3941 | |
| 3942 | // If we did not consolidate MA, thus did not find an equivalence class |
| 3943 | // for it, we create a new one. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3944 | InvariantEquivClasses.emplace_back( |
| 3945 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList{MA}, MACtx, Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | isl_set_free(DomainCtx); |
| 3949 | } |
| 3950 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3951 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3952 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 3953 | // zero iterators, are by construction invariant, though we |
| 3954 | // currently "hoist" them anyway. This is necessary because we allow |
| 3955 | // them to be treated as parameters (e.g., in conditions) and our code |
| 3956 | // generation would otherwise use the old value. |
| 3957 | |
| 3958 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 3959 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3960 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 3961 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 3962 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3963 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3964 | |
| 3965 | // Skip accesses that have an invariant base pointer which is defined but |
| 3966 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 3967 | // returns a pointer that is used as a base address. However, as we want |
| 3968 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 3969 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 3970 | // that has a base pointer origin has a base pointer that is loaded and |
| 3971 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 3972 | // no base pointer origin we check that the base pointer is defined |
| 3973 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3974 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3975 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3976 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3977 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3978 | isl::map AccessRelation = give(Access->getAccessRelation()); |
| 3979 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3980 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3981 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3982 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3983 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3984 | AccessRelation = AccessRelation.intersect_domain(give(Stmt.getDomain())); |
| 3985 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3986 | |
| 3987 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 3988 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 3989 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3990 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3991 | } else if (BB != LI->getParent()) { |
| 3992 | // Skip accesses in non-affine subregions as they might not be executed |
| 3993 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3994 | return nullptr; |
| 3995 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3996 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3997 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3998 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3999 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 4000 | isl::set WrittenCtx = Written.params(); |
| 4001 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4002 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4003 | if (!IsWritten) |
| 4004 | return WrittenCtx; |
| 4005 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4006 | WrittenCtx = WrittenCtx.remove_divs(); |
| 4007 | bool TooComplex = |
| 4008 | isl_set_n_basic_set(WrittenCtx.get()) >= MaxDisjunctsInDomain; |
| 4009 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4010 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4011 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4012 | addAssumption(INVARIANTLOAD, WrittenCtx.copy(), LI->getDebugLoc(), |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4013 | AS_RESTRICTION); |
| 4014 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4017 | void Scop::verifyInvariantLoads() { |
| 4018 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4019 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4020 | assert(LI && contains(LI)); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4021 | ScopStmt *Stmt = getStmtFor(LI); |
Tobias Grosser | 949e8c6 | 2015-12-21 07:10:39 +0000 | [diff] [blame] | 4022 | if (Stmt && Stmt->getArrayAccessOrNULLFor(LI)) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4023 | invalidate(INVARIANTLOAD, LI->getDebugLoc()); |
| 4024 | return; |
| 4025 | } |
| 4026 | } |
| 4027 | } |
| 4028 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4029 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4030 | if (!PollyInvariantLoadHoisting) |
| 4031 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4032 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4033 | isl::union_map Writes = give(getWrites()); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4034 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4035 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4036 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4037 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4038 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
| 4039 | InvariantAccesses.push_back({Access, NHCtx.release()}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4040 | |
| 4041 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 4042 | for (auto InvMA : InvariantAccesses) |
| 4043 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4044 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4045 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4048 | /// Find the canonical scop array info object for a set of invariant load |
| 4049 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 4050 | /// first load in the list of accesses which is used as base pointer of a |
| 4051 | /// scop array. |
| 4052 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 4053 | MemoryAccessList &Accesses) { |
| 4054 | for (MemoryAccess *Access : Accesses) { |
| 4055 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 4056 | Access->getAccessInstruction(), MemoryKind::Array); |
| 4057 | if (CanonicalArray) |
| 4058 | return CanonicalArray; |
| 4059 | } |
| 4060 | return nullptr; |
| 4061 | } |
| 4062 | |
| 4063 | /// Check if @p Array severs as base array in an invariant load. |
| 4064 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 4065 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 4066 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 4067 | if (Access2->getScopArrayInfo() == Array) |
| 4068 | return true; |
| 4069 | return false; |
| 4070 | } |
| 4071 | |
| 4072 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 4073 | /// with a reference to @p New. |
| 4074 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 4075 | const ScopArrayInfo *New) { |
| 4076 | for (ScopStmt &Stmt : *S) |
| 4077 | for (MemoryAccess *Access : Stmt) { |
| 4078 | if (Access->getLatestScopArrayInfo() != Old) |
| 4079 | continue; |
| 4080 | |
| 4081 | isl_id *Id = New->getBasePtrId(); |
| 4082 | isl_map *Map = Access->getAccessRelation(); |
| 4083 | Map = isl_map_set_tuple_id(Map, isl_dim_out, Id); |
| 4084 | Access->setAccessRelation(Map); |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | void Scop::canonicalizeDynamicBasePtrs() { |
| 4089 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 4090 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 4091 | |
| 4092 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 4093 | findCanonicalArray(this, BasePtrAccesses); |
| 4094 | |
| 4095 | if (!CanonicalBasePtrSAI) |
| 4096 | continue; |
| 4097 | |
| 4098 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 4099 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 4100 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 4101 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 4102 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 4103 | continue; |
| 4104 | |
| 4105 | // we currently do not canonicalize arrays where some accesses are |
| 4106 | // hoisted as invariant loads. If we would, we need to update the access |
| 4107 | // function of the invariant loads as well. However, as this is not a |
| 4108 | // very common situation, we leave this for now to avoid further |
| 4109 | // complexity increases. |
| 4110 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 4111 | continue; |
| 4112 | |
| 4113 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4118 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 4119 | ArrayRef<const SCEV *> Sizes, |
| 4120 | MemoryKind Kind, |
| 4121 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4122 | assert((BasePtr || BaseName) && |
| 4123 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 4124 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 4125 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 4126 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4127 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4128 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 4129 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4130 | DL, this, BaseName)); |
| 4131 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4132 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4133 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 4134 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 4135 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4136 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4137 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4138 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4139 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4142 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 4143 | const std::string &BaseName, |
| 4144 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4145 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4146 | std::vector<const SCEV *> SCEVSizes; |
| 4147 | |
| 4148 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4149 | if (size) |
| 4150 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4151 | else |
| 4152 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4153 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4154 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4155 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4156 | return SAI; |
| 4157 | } |
| 4158 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4159 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4160 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4161 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4162 | return SAI; |
| 4163 | } |
| 4164 | |
| 4165 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4166 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4167 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4168 | return SAI; |
| 4169 | } |
| 4170 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 4171 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4172 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4173 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4174 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4175 | return stringFromIslObj(AssumedContext); |
| 4176 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4177 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4178 | std::string Scop::getInvalidContextStr() const { |
| 4179 | return stringFromIslObj(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4180 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4181 | |
| 4182 | std::string Scop::getNameStr() const { |
| 4183 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4184 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4185 | return EntryName + "---" + ExitName; |
| 4186 | } |
| 4187 | |
| 4188 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4189 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4190 | raw_string_ostream ExitStr(ExitName); |
| 4191 | raw_string_ostream EntryStr(EntryName); |
| 4192 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4193 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4194 | EntryStr.str(); |
| 4195 | |
| 4196 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4197 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4198 | ExitStr.str(); |
| 4199 | } else |
| 4200 | ExitName = "FunctionExit"; |
| 4201 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4202 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 4205 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4206 | __isl_give isl_space *Scop::getParamSpace() const { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 4207 | return isl_set_get_space(Context); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4210 | __isl_give isl_set *Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4211 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4212 | return isl_set_copy(AssumedContext); |
| 4213 | } |
| 4214 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4215 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4216 | if (PollyProcessUnprofitable) |
| 4217 | return true; |
| 4218 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4219 | if (isEmpty()) |
| 4220 | return false; |
| 4221 | |
| 4222 | unsigned OptimizableStmtsOrLoops = 0; |
| 4223 | for (auto &Stmt : *this) { |
| 4224 | if (Stmt.getNumIterators() == 0) |
| 4225 | continue; |
| 4226 | |
| 4227 | bool ContainsArrayAccs = false; |
| 4228 | bool ContainsScalarAccs = false; |
| 4229 | for (auto *MA : Stmt) { |
| 4230 | if (MA->isRead()) |
| 4231 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4232 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4233 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4236 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4237 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4238 | } |
| 4239 | |
| 4240 | return OptimizableStmtsOrLoops > 1; |
| 4241 | } |
| 4242 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4243 | bool Scop::hasFeasibleRuntimeContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4244 | auto *PositiveContext = getAssumedContext(); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4245 | auto *NegativeContext = getInvalidContext(); |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4246 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 4247 | bool IsFeasible = !(isl_set_is_empty(PositiveContext) || |
| 4248 | isl_set_is_subset(PositiveContext, NegativeContext)); |
| 4249 | isl_set_free(PositiveContext); |
| 4250 | if (!IsFeasible) { |
| 4251 | isl_set_free(NegativeContext); |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4255 | auto *DomainContext = isl_union_set_params(getDomains()); |
| 4256 | IsFeasible = !isl_set_is_subset(DomainContext, NegativeContext); |
Johannes Doerfert | fb72187 | 2016-04-12 17:54:29 +0000 | [diff] [blame] | 4257 | IsFeasible &= !isl_set_is_subset(Context, NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4258 | isl_set_free(NegativeContext); |
| 4259 | isl_set_free(DomainContext); |
| 4260 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4261 | return IsFeasible; |
| 4262 | } |
| 4263 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4264 | static std::string toString(AssumptionKind Kind) { |
| 4265 | switch (Kind) { |
| 4266 | case ALIASING: |
| 4267 | return "No-aliasing"; |
| 4268 | case INBOUNDS: |
| 4269 | return "Inbounds"; |
| 4270 | case WRAPPING: |
| 4271 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4272 | case UNSIGNED: |
| 4273 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4274 | case COMPLEXITY: |
| 4275 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4276 | case PROFITABLE: |
| 4277 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4278 | case ERRORBLOCK: |
| 4279 | return "No-error"; |
| 4280 | case INFINITELOOP: |
| 4281 | return "Finite loop"; |
| 4282 | case INVARIANTLOAD: |
| 4283 | return "Invariant load"; |
| 4284 | case DELINEARIZATION: |
| 4285 | return "Delinearization"; |
| 4286 | } |
| 4287 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4288 | } |
| 4289 | |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4290 | bool Scop::isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign) { |
| 4291 | if (Sign == AS_ASSUMPTION) { |
| 4292 | if (isl_set_is_subset(Context, Set)) |
| 4293 | return false; |
| 4294 | |
| 4295 | if (isl_set_is_subset(AssumedContext, Set)) |
| 4296 | return false; |
| 4297 | } else { |
| 4298 | if (isl_set_is_disjoint(Set, Context)) |
| 4299 | return false; |
| 4300 | |
| 4301 | if (isl_set_is_subset(Set, InvalidContext)) |
| 4302 | return false; |
| 4303 | } |
| 4304 | return true; |
| 4305 | } |
| 4306 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4307 | bool Scop::trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set, |
| 4308 | DebugLoc Loc, AssumptionSign Sign) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4309 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4310 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4311 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4312 | // Do never emit trivial assumptions as they only clutter the output. |
| 4313 | if (!PollyRemarksMinimal) { |
| 4314 | isl_set *Univ = nullptr; |
| 4315 | if (Sign == AS_ASSUMPTION) |
| 4316 | Univ = isl_set_universe(isl_set_get_space(Set)); |
| 4317 | |
| 4318 | bool IsTrivial = (Sign == AS_RESTRICTION && isl_set_is_empty(Set)) || |
| 4319 | (Sign == AS_ASSUMPTION && isl_set_is_equal(Univ, Set)); |
| 4320 | isl_set_free(Univ); |
| 4321 | |
| 4322 | if (IsTrivial) |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4326 | switch (Kind) { |
| 4327 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4328 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4329 | break; |
| 4330 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4331 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4334 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4335 | break; |
| 4336 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4337 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4338 | break; |
| 4339 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4340 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4341 | break; |
| 4342 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4343 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4344 | break; |
| 4345 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4346 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4347 | break; |
| 4348 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4349 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4350 | break; |
| 4351 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4352 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4353 | break; |
| 4354 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4355 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4356 | break; |
| 4357 | } |
| 4358 | |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4359 | auto &F = getFunction(); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4360 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
| 4361 | std::string Msg = toString(Kind) + Suffix + stringFromIslObj(Set); |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4362 | emitOptimizationRemarkAnalysis(F.getContext(), DEBUG_TYPE, F, Loc, Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4363 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | void Scop::addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4367 | DebugLoc Loc, AssumptionSign Sign) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4368 | // Simplify the assumptions/restrictions first. |
| 4369 | Set = isl_set_gist_params(Set, getContext()); |
| 4370 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4371 | if (!trackAssumption(Kind, Set, Loc, Sign)) { |
| 4372 | isl_set_free(Set); |
| 4373 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4376 | if (Sign == AS_ASSUMPTION) { |
| 4377 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
| 4378 | AssumedContext = isl_set_coalesce(AssumedContext); |
| 4379 | } else { |
| 4380 | InvalidContext = isl_set_union(InvalidContext, Set); |
| 4381 | InvalidContext = isl_set_coalesce(InvalidContext); |
| 4382 | } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4385 | void Scop::recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4386 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4387 | assert((isl_set_is_params(Set) || BB) && |
| 4388 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4389 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | void Scop::addRecordedAssumptions() { |
| 4393 | while (!RecordedAssumptions.empty()) { |
| 4394 | const Assumption &AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4395 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4396 | if (!AS.BB) { |
| 4397 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign); |
| 4398 | continue; |
| 4399 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4400 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4401 | // If the domain was deleted the assumptions are void. |
| 4402 | isl_set *Dom = getDomainConditions(AS.BB); |
| 4403 | if (!Dom) { |
| 4404 | isl_set_free(AS.Set); |
| 4405 | continue; |
| 4406 | } |
| 4407 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4408 | // If a basic block was given use its domain to simplify the assumption. |
| 4409 | // In case of restrictions we know they only have to hold on the domain, |
| 4410 | // thus we can intersect them with the domain of the block. However, for |
| 4411 | // assumptions the domain has to imply them, thus: |
| 4412 | // _ _____ |
| 4413 | // Dom => S <==> A v B <==> A - B |
| 4414 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4415 | // 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] | 4416 | // assumption. |
| 4417 | isl_set *S = AS.Set; |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4418 | if (AS.Sign == AS_RESTRICTION) |
| 4419 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4420 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4421 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4422 | |
| 4423 | addAssumption(AS.Kind, S, AS.Loc, AS_RESTRICTION); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4424 | } |
| 4425 | } |
| 4426 | |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4427 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc) { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4428 | addAssumption(Kind, isl_set_empty(getParamSpace()), Loc, AS_ASSUMPTION); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4431 | __isl_give isl_set *Scop::getInvalidContext() const { |
| 4432 | return isl_set_copy(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4435 | void Scop::printContext(raw_ostream &OS) const { |
| 4436 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4437 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4438 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4439 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4440 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4441 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4442 | OS.indent(4) << "Invalid Context:\n"; |
| 4443 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4444 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4445 | unsigned Dim = 0; |
| 4446 | for (const SCEV *Parameter : Parameters) |
| 4447 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4450 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4451 | int noOfGroups = 0; |
| 4452 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4453 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4454 | noOfGroups += 1; |
| 4455 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4456 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4459 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4460 | if (MinMaxAliasGroups.empty()) { |
| 4461 | OS.indent(8) << "n/a\n"; |
| 4462 | return; |
| 4463 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4464 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4465 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4466 | |
| 4467 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4468 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4469 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4470 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4471 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4472 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4473 | } |
| 4474 | OS << " ]]\n"; |
| 4475 | } |
| 4476 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4477 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4478 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4479 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4480 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4481 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4482 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4483 | } |
| 4484 | OS << " ]]\n"; |
| 4485 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4486 | } |
| 4487 | } |
| 4488 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4489 | void Scop::printStatements(raw_ostream &OS) const { |
| 4490 | OS << "Statements {\n"; |
| 4491 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4492 | for (const ScopStmt &Stmt : *this) |
| 4493 | OS.indent(4) << Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4494 | |
| 4495 | OS.indent(4) << "}\n"; |
| 4496 | } |
| 4497 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4498 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4499 | OS << "Arrays {\n"; |
| 4500 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4501 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4502 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4503 | |
| 4504 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4505 | |
| 4506 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4507 | |
| 4508 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4509 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4510 | |
| 4511 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4514 | void Scop::print(raw_ostream &OS) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4515 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4516 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4517 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4518 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4519 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4520 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4521 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4522 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4523 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4524 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4525 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4526 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4527 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4528 | } |
| 4529 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4530 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4531 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4532 | printAliasAssumptions(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4533 | printStatements(OS.indent(4)); |
| 4534 | } |
| 4535 | |
| 4536 | void Scop::dump() const { print(dbgs()); } |
| 4537 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 4538 | isl_ctx *Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4539 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4540 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4541 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4542 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4543 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4544 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4545 | // 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] | 4546 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4547 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4548 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4549 | // TODO: We could use a heuristic and either use: |
| 4550 | // SCEVAffinator::takeNonNegativeAssumption |
| 4551 | // or |
| 4552 | // SCEVAffinator::interpretAsUnsigned |
| 4553 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4554 | if (NonNegative) |
| 4555 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4556 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4557 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4558 | |
| 4559 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
| 4560 | invalidate(COMPLEXITY, DL); |
| 4561 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4564 | __isl_give isl_union_set *Scop::getDomains() const { |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4565 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx(), 0); |
| 4566 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4567 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4568 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4569 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4570 | |
| 4571 | return Domain; |
| 4572 | } |
| 4573 | |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4574 | __isl_give isl_pw_aff *Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
| 4575 | PWACtx PWAC = getPwAff(E, BB); |
| 4576 | isl_set_free(PWAC.second); |
| 4577 | return PWAC.first; |
| 4578 | } |
| 4579 | |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4580 | __isl_give isl_union_map * |
| 4581 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
| 4582 | isl_union_map *Accesses = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4583 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4584 | for (ScopStmt &Stmt : *this) { |
| 4585 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4586 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4587 | continue; |
| 4588 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4589 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4590 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 4591 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4592 | Accesses = isl_union_map_add_map(Accesses, AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4595 | return isl_union_map_coalesce(Accesses); |
| 4596 | } |
| 4597 | |
| 4598 | __isl_give isl_union_map *Scop::getMustWrites() { |
| 4599 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | __isl_give isl_union_map *Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4603 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4606 | __isl_give isl_union_map *Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4607 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4611 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4614 | __isl_give isl_union_map *Scop::getAccesses() { |
| 4615 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4616 | } |
| 4617 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4618 | // Check whether @p Node is an extension node. |
| 4619 | // |
| 4620 | // @return true if @p Node is an extension node. |
| 4621 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4622 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4623 | return isl_bool_error; |
| 4624 | else |
| 4625 | return isl_bool_true; |
| 4626 | } |
| 4627 | |
| 4628 | bool Scop::containsExtensionNode(__isl_keep isl_schedule *Schedule) { |
| 4629 | return isl_schedule_foreach_schedule_node_top_down(Schedule, isNotExtNode, |
| 4630 | nullptr) == isl_stat_error; |
| 4631 | } |
| 4632 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4633 | __isl_give isl_union_map *Scop::getSchedule() const { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4634 | auto *Tree = getScheduleTree(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4635 | if (containsExtensionNode(Tree)) { |
| 4636 | isl_schedule_free(Tree); |
| 4637 | return nullptr; |
| 4638 | } |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4639 | auto *S = isl_schedule_get_map(Tree); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4640 | isl_schedule_free(Tree); |
| 4641 | return S; |
| 4642 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4643 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4644 | __isl_give isl_schedule *Scop::getScheduleTree() const { |
| 4645 | return isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 4646 | getDomains()); |
| 4647 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4648 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4649 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
| 4650 | auto *S = isl_schedule_from_domain(getDomains()); |
| 4651 | S = isl_schedule_insert_partial_schedule( |
| 4652 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 4653 | isl_schedule_free(Schedule); |
| 4654 | Schedule = S; |
| 4655 | } |
| 4656 | |
| 4657 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 4658 | isl_schedule_free(Schedule); |
| 4659 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 4663 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4664 | for (ScopStmt &Stmt : *this) { |
| 4665 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4666 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 4667 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 4668 | |
| 4669 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 4670 | isl_union_set_free(StmtDomain); |
| 4671 | isl_union_set_free(NewStmtDomain); |
| 4672 | continue; |
| 4673 | } |
| 4674 | |
| 4675 | Changed = true; |
| 4676 | |
| 4677 | isl_union_set_free(StmtDomain); |
| 4678 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 4679 | |
| 4680 | if (isl_union_set_is_empty(NewStmtDomain)) { |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4681 | Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace())); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4682 | isl_union_set_free(NewStmtDomain); |
| 4683 | } else |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4684 | Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4685 | } |
| 4686 | isl_union_set_free(Domain); |
| 4687 | return Changed; |
| 4688 | } |
| 4689 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4690 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4691 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4692 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4693 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4694 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4695 | // # Example: |
| 4696 | // |
| 4697 | // Domain: { A[i,j]; B[i,j,k] } |
| 4698 | // N: 1 |
| 4699 | // |
| 4700 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4701 | // |
| 4702 | // @param USet A union set describing the elements for which to generate a |
| 4703 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4704 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4705 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4706 | 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] | 4707 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4708 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4709 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4710 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4711 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4712 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4713 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4714 | int Dim = S.dim(isl::dim::set); |
| 4715 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4716 | N, Dim - N); |
| 4717 | if (N > 1) |
| 4718 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4719 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4720 | Result = Result.add_pw_multi_aff(PMA); |
| 4721 | return isl::stat::ok; |
| 4722 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4723 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4724 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4725 | (void)Res; |
| 4726 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4727 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4728 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4729 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4732 | void Scop::addScopStmt(BasicBlock *BB, Loop *SurroundingLoop, |
| 4733 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4734 | assert(BB && "Unexpected nullptr!"); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4735 | Stmts.emplace_back(*this, *BB, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4736 | auto *Stmt = &Stmts.back(); |
| 4737 | StmtMap[BB] = Stmt; |
| 4738 | } |
| 4739 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4740 | void Scop::addScopStmt(Region *R, Loop *SurroundingLoop) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4741 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4742 | Stmts.emplace_back(*this, *R, SurroundingLoop); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4743 | auto *Stmt = &Stmts.back(); |
| 4744 | for (BasicBlock *BB : R->blocks()) |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4745 | StmtMap[BB] = Stmt; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4748 | ScopStmt *Scop::addScopStmt(__isl_take isl_map *SourceRel, |
| 4749 | __isl_take isl_map *TargetRel, |
| 4750 | __isl_take isl_set *Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4751 | #ifndef NDEBUG |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4752 | isl_set *SourceDomain = isl_map_domain(isl_map_copy(SourceRel)); |
| 4753 | isl_set *TargetDomain = isl_map_domain(isl_map_copy(TargetRel)); |
| 4754 | assert(isl_set_is_subset(Domain, TargetDomain) && |
| 4755 | "Target access not defined for complete statement domain"); |
| 4756 | assert(isl_set_is_subset(Domain, SourceDomain) && |
| 4757 | "Source access not defined for complete statement domain"); |
| 4758 | isl_set_free(SourceDomain); |
| 4759 | isl_set_free(TargetDomain); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4760 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4761 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4762 | CopyStmtsNum++; |
| 4763 | return &(Stmts.back()); |
| 4764 | } |
| 4765 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4766 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4767 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4768 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4769 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4770 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4771 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4774 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4775 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4776 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4777 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4778 | /// |
| 4779 | /// Example: |
| 4780 | /// bb1(0) |
| 4781 | /// / \. |
| 4782 | /// bb2(1) bb3(2) |
| 4783 | /// \ / \. |
| 4784 | /// bb4(3) bb5(4) |
| 4785 | /// \ / |
| 4786 | /// bb6(5) |
| 4787 | /// |
| 4788 | /// Including loops requires additional processing. Whenever a loop header is |
| 4789 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4790 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4791 | /// this loop and complete the sequential schedule at this loop-level before |
| 4792 | /// processing about any other nodes. To implement this |
| 4793 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4794 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4795 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4796 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4797 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4798 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4799 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4800 | |
| 4801 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4802 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4803 | std::deque<RegionNode *> DelayList; |
| 4804 | bool LastRNWaiting = false; |
| 4805 | |
| 4806 | // Iterate over the region @p R in reverse post-order but queue |
| 4807 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4808 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4809 | // that we queued the last sub-region/block from the reverse post-order |
| 4810 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4811 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4812 | // the next queued sub-region/blocks. |
| 4813 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4814 | RegionNode *RN; |
| 4815 | |
| 4816 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.size() == 0) { |
| 4817 | RN = WorkList.front(); |
| 4818 | WorkList.pop_front(); |
| 4819 | LastRNWaiting = false; |
| 4820 | } else { |
| 4821 | RN = DelayList.front(); |
| 4822 | DelayList.pop_front(); |
| 4823 | } |
| 4824 | |
| 4825 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4826 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4827 | L = OuterScopLoop; |
| 4828 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4829 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4830 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4831 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4832 | LastRNWaiting = true; |
| 4833 | DelayList.push_back(RN); |
| 4834 | continue; |
| 4835 | } |
| 4836 | LoopStack.push_back({L, nullptr, 0}); |
| 4837 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4838 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | return; |
| 4842 | } |
| 4843 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4844 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4845 | |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4846 | if (RN->isSubRegion()) { |
| 4847 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4848 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4849 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4850 | return; |
| 4851 | } |
| 4852 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4853 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4854 | auto &LoopData = LoopStack.back(); |
| 4855 | LoopData.NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4856 | |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4857 | if (auto *Stmt = getStmtFor(RN)) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4858 | auto *UDomain = isl_union_set_from_set(Stmt->getDomain()); |
| 4859 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4860 | LoopData.Schedule = combineInSequence(LoopData.Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4863 | // Check if we just processed the last node in this loop. If we did, finalize |
| 4864 | // the loop by: |
| 4865 | // |
| 4866 | // - adding new schedule dimensions |
| 4867 | // - folding the resulting schedule into the parent loop schedule |
| 4868 | // - dropping the loop schedule from the LoopStack. |
| 4869 | // |
| 4870 | // Then continue to check surrounding loops, which might also have been |
| 4871 | // completed by this node. |
| 4872 | while (LoopData.L && |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 4873 | LoopData.NumBlocksProcessed == getNumBlocksInLoop(LoopData.L)) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4874 | auto *Schedule = LoopData.Schedule; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4875 | auto NumBlocksProcessed = LoopData.NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4876 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4877 | LoopStack.pop_back(); |
| 4878 | auto &NextLoopData = LoopStack.back(); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4879 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4880 | if (Schedule) { |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4881 | isl::union_set Domain = give(isl_schedule_get_domain(Schedule)); |
| 4882 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, LoopStack.size()); |
| 4883 | Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA.release()); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4884 | NextLoopData.Schedule = |
| 4885 | combineInSequence(NextLoopData.Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4886 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4887 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4888 | NextLoopData.NumBlocksProcessed += NumBlocksProcessed; |
| 4889 | LoopData = NextLoopData; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4890 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4893 | ScopStmt *Scop::getStmtFor(BasicBlock *BB) const { |
Tobias Grosser | 57411e3 | 2015-05-27 06:51:34 +0000 | [diff] [blame] | 4894 | auto StmtMapIt = StmtMap.find(BB); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 4895 | if (StmtMapIt == StmtMap.end()) |
| 4896 | return nullptr; |
| 4897 | return StmtMapIt->second; |
| 4898 | } |
| 4899 | |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4900 | ScopStmt *Scop::getStmtFor(RegionNode *RN) const { |
| 4901 | if (RN->isSubRegion()) |
| 4902 | return getStmtFor(RN->getNodeAs<Region>()); |
| 4903 | return getStmtFor(RN->getNodeAs<BasicBlock>()); |
| 4904 | } |
| 4905 | |
| 4906 | ScopStmt *Scop::getStmtFor(Region *R) const { |
| 4907 | ScopStmt *Stmt = getStmtFor(R->getEntry()); |
| 4908 | assert(!Stmt || Stmt->getRegion() == R); |
| 4909 | return Stmt; |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4912 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4913 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4914 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4915 | // outermostLoopInRegion always returns nullptr for top level regions |
| 4916 | if (R.isTopLevelRegion()) { |
| 4917 | // LoopInfo's depths start at 1, we start at 0 |
| 4918 | return L->getLoopDepth() - 1; |
| 4919 | } else { |
| 4920 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 4921 | assert(OuterLoop); |
| 4922 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 4923 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4926 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 4927 | for (auto &SAI : arrays()) { |
| 4928 | if (SAI->getName() == BaseName) |
| 4929 | return SAI; |
| 4930 | } |
| 4931 | return nullptr; |
| 4932 | } |
| 4933 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4934 | //===----------------------------------------------------------------------===// |
| 4935 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4936 | AU.addRequired<LoopInfoWrapperPass>(); |
| 4937 | AU.addRequired<RegionInfoPass>(); |
| 4938 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4939 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4940 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4941 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4942 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4943 | AU.setPreservesAll(); |
| 4944 | } |
| 4945 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4946 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats) { |
| 4947 | NumLoopsInScop += Stats.NumLoops; |
| 4948 | MaxNumLoopsInScop = |
| 4949 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 4950 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4951 | if (Stats.MaxDepth == 1) |
| 4952 | NumScopsDepthOne++; |
| 4953 | else if (Stats.MaxDepth == 2) |
| 4954 | NumScopsDepthTwo++; |
| 4955 | else if (Stats.MaxDepth == 3) |
| 4956 | NumScopsDepthThree++; |
| 4957 | else if (Stats.MaxDepth == 4) |
| 4958 | NumScopsDepthFour++; |
| 4959 | else if (Stats.MaxDepth == 5) |
| 4960 | NumScopsDepthFive++; |
| 4961 | else |
| 4962 | NumScopsDepthLarger++; |
| 4963 | } |
| 4964 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4965 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4966 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4967 | |
| 4968 | if (!SD.isMaxRegionInScop(*R)) |
| 4969 | return false; |
| 4970 | |
| 4971 | Function *F = R->getEntry()->getParent(); |
| 4972 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 4973 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 4974 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 4975 | auto const &DL = F->getParent()->getDataLayout(); |
| 4976 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4977 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4978 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4979 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 4980 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4981 | |
| 4982 | if (S) { |
| 4983 | ScopDetection::LoopStats Stats = |
| 4984 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 4985 | updateLoopCountStatistic(Stats); |
| 4986 | } |
| 4987 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4988 | return false; |
| 4989 | } |
| 4990 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4991 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 4992 | if (S) |
| 4993 | S->print(OS); |
| 4994 | else |
| 4995 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4996 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4997 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4998 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 4999 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5000 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5001 | |
| 5002 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5003 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5004 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5005 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5006 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5007 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5008 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5009 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5010 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5011 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5012 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5013 | "Polly - Create polyhedral description of Scops", false, |
| 5014 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5015 | |
| 5016 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5017 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5018 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
| 5019 | AssumptionCache &AC) { |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5020 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5021 | /// function. |
| 5022 | for (auto &It : SD) { |
| 5023 | Region *R = const_cast<Region *>(It); |
| 5024 | if (!SD.isMaxRegionInScop(*R)) |
| 5025 | continue; |
| 5026 | |
| 5027 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
| 5028 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5029 | if (!S) |
| 5030 | continue; |
| 5031 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5032 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5033 | (void)Inserted; |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | AnalysisKey ScopInfoAnalysis::Key; |
| 5038 | |
| 5039 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5040 | FunctionAnalysisManager &FAM) { |
| 5041 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5042 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5043 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5044 | auto &AA = FAM.getResult<AAManager>(F); |
| 5045 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5046 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5047 | auto &DL = F.getParent()->getDataLayout(); |
| 5048 | return {DL, SD, SE, LI, AA, DT, AC}; |
| 5049 | } |
| 5050 | |
| 5051 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5052 | FunctionAnalysisManager &FAM) { |
| 5053 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
| 5054 | for (auto &It : SI) { |
| 5055 | if (It.second) |
| 5056 | It.second->print(Stream); |
| 5057 | else |
| 5058 | Stream << "Invalid Scop!\n"; |
| 5059 | } |
| 5060 | return PreservedAnalyses::all(); |
| 5061 | } |
| 5062 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5063 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5064 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5065 | AU.addRequired<RegionInfoPass>(); |
| 5066 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5067 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5068 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5069 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5070 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5071 | AU.setPreservesAll(); |
| 5072 | } |
| 5073 | |
| 5074 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5075 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5076 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5077 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5078 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5079 | auto const &DL = F.getParent()->getDataLayout(); |
| 5080 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5081 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5082 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5083 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5084 | return false; |
| 5085 | } |
| 5086 | |
| 5087 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5088 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5089 | if (It.second) |
| 5090 | It.second->print(OS); |
| 5091 | else |
| 5092 | OS << "Invalid Scop!\n"; |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | char ScopInfoWrapperPass::ID = 0; |
| 5097 | |
| 5098 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5099 | return new ScopInfoWrapperPass(); |
| 5100 | } |
| 5101 | |
| 5102 | INITIALIZE_PASS_BEGIN( |
| 5103 | ScopInfoWrapperPass, "polly-function-scops", |
| 5104 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5105 | false); |
| 5106 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5107 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5108 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5109 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5110 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5111 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5112 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5113 | INITIALIZE_PASS_END( |
| 5114 | ScopInfoWrapperPass, "polly-function-scops", |
| 5115 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5116 | false) |