Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +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" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 24 | #include "polly/ScopDetection.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 25 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 26 | #include "polly/Support/ISLOStream.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 27 | #include "polly/Support/SCEVAffinator.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 28 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "polly/Support/ScopHelper.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 30 | #include "llvm/ADT/APInt.h" |
| 31 | #include "llvm/ADT/ArrayRef.h" |
| 32 | #include "llvm/ADT/DenseMap.h" |
| 33 | #include "llvm/ADT/DenseSet.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/PostOrderIterator.h" |
| 35 | #include "llvm/ADT/STLExtras.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 37 | #include "llvm/ADT/SmallPtrSet.h" |
| 38 | #include "llvm/ADT/SmallSet.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 42 | #include "llvm/ADT/StringMap.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 44 | #include "llvm/Analysis/AliasSetTracker.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/LoopInfo.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 48 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
| 49 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/RegionIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 51 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 53 | #include "llvm/IR/Argument.h" |
| 54 | #include "llvm/IR/BasicBlock.h" |
| 55 | #include "llvm/IR/CFG.h" |
| 56 | #include "llvm/IR/ConstantRange.h" |
| 57 | #include "llvm/IR/Constants.h" |
| 58 | #include "llvm/IR/DataLayout.h" |
| 59 | #include "llvm/IR/DebugLoc.h" |
| 60 | #include "llvm/IR/DerivedTypes.h" |
Johannes Doerfert | 48fe86f | 2015-11-12 02:32:32 +0000 | [diff] [blame] | 61 | #include "llvm/IR/DiagnosticInfo.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 62 | #include "llvm/IR/Dominators.h" |
| 63 | #include "llvm/IR/Function.h" |
| 64 | #include "llvm/IR/InstrTypes.h" |
| 65 | #include "llvm/IR/Instruction.h" |
| 66 | #include "llvm/IR/Instructions.h" |
| 67 | #include "llvm/IR/IntrinsicInst.h" |
| 68 | #include "llvm/IR/Module.h" |
| 69 | #include "llvm/IR/PassManager.h" |
| 70 | #include "llvm/IR/Type.h" |
| 71 | #include "llvm/IR/Use.h" |
| 72 | #include "llvm/IR/User.h" |
| 73 | #include "llvm/IR/Value.h" |
| 74 | #include "llvm/Pass.h" |
| 75 | #include "llvm/Support/Casting.h" |
| 76 | #include "llvm/Support/CommandLine.h" |
| 77 | #include "llvm/Support/Compiler.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 78 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 79 | #include "llvm/Support/ErrorHandling.h" |
| 80 | #include "llvm/Support/MathExtras.h" |
| 81 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 82 | #include "isl/aff.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 83 | #include "isl/constraint.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 84 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 85 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 86 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 87 | #include "isl/printer.h" |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 88 | #include "isl/schedule.h" |
| 89 | #include "isl/schedule_node.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 90 | #include "isl/set.h" |
| 91 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 92 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 93 | #include "isl/val.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 94 | #include <algorithm> |
| 95 | #include <cassert> |
| 96 | #include <cstdlib> |
| 97 | #include <cstring> |
| 98 | #include <deque> |
| 99 | #include <iterator> |
| 100 | #include <memory> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 101 | #include <string> |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 102 | #include <tuple> |
| 103 | #include <utility> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 104 | #include <vector> |
| 105 | |
| 106 | using namespace llvm; |
| 107 | using namespace polly; |
| 108 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 109 | #define DEBUG_TYPE "polly-scops" |
| 110 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 111 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 112 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 113 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 114 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 115 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 116 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 117 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 118 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 119 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 120 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 121 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 122 | "Number of delinearization assumptions taken."); |
| 123 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 124 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
| 125 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 126 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 127 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 128 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 129 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 130 | STATISTIC(NumScopsDepthLarger, |
| 131 | "Number of scops with maximal loop depth 6 and larger"); |
| 132 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 133 | |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 134 | // The maximal number of basic sets we allow during domain construction to |
| 135 | // be created. More complex scops will result in very high compile time and |
| 136 | // are also unlikely to result in good code |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 137 | static int const MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 138 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 139 | // The number of disjunct in the context after which we stop to add more |
| 140 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 141 | // number of disjunct when adding non-convex sets to the context. |
| 142 | static int const MaxDisjunctsInContext = 4; |
| 143 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 144 | // The maximal number of dimensions we allow during invariant load construction. |
| 145 | // More complex access ranges will result in very high compile time and are also |
| 146 | // unlikely to result in good code. This value is very high and should only |
| 147 | // trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006). |
| 148 | static int const MaxDimensionsInAccessRange = 9; |
| 149 | |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 150 | static cl::opt<int> |
| 151 | OptComputeOut("polly-analysis-computeout", |
| 152 | cl::desc("Bound the scop analysis by a maximal amount of " |
| 153 | "computational steps (0 means no bound)"), |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 154 | cl::Hidden, cl::init(800000), cl::ZeroOrMore, |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 155 | cl::cat(PollyCategory)); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 156 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 157 | static cl::opt<bool> PollyRemarksMinimal( |
| 158 | "polly-remarks-minimal", |
| 159 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 160 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 161 | |
Johannes Doerfert | 9e7b17b | 2014-08-18 00:40:13 +0000 | [diff] [blame] | 162 | // Multiplicative reductions can be disabled separately as these kind of |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 163 | // operations can overflow easily. Additive reductions and bit operations |
| 164 | // are in contrast pretty stable. |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 165 | static cl::opt<bool> DisableMultiplicativeReductions( |
| 166 | "polly-disable-multiplicative-reductions", |
| 167 | cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore, |
| 168 | cl::init(false), cl::cat(PollyCategory)); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 169 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 170 | static cl::opt<int> RunTimeChecksMaxAccessDisjuncts( |
| 171 | "polly-rtc-max-array-disjuncts", |
| 172 | cl::desc("The maximal number of disjunts allowed in memory accesses to " |
| 173 | "to build RTCs."), |
| 174 | cl::Hidden, cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 175 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 176 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 177 | "polly-rtc-max-parameters", |
| 178 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 179 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 180 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 181 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 182 | "polly-rtc-max-arrays-per-group", |
| 183 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 184 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 185 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 186 | static cl::opt<std::string> UserContextStr( |
| 187 | "polly-context", cl::value_desc("isl parameter set"), |
| 188 | cl::desc("Provide additional constraints on the context parameters"), |
| 189 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 190 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 191 | static cl::opt<bool> DetectReductions("polly-detect-reductions", |
| 192 | cl::desc("Detect and exploit reductions"), |
| 193 | cl::Hidden, cl::ZeroOrMore, |
| 194 | cl::init(true), cl::cat(PollyCategory)); |
| 195 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 196 | static cl::opt<bool> |
| 197 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 198 | cl::desc("Abort if an isl error is encountered"), |
| 199 | cl::init(true), cl::cat(PollyCategory)); |
| 200 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 201 | static cl::opt<bool> PollyPreciseInbounds( |
| 202 | "polly-precise-inbounds", |
| 203 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 204 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 205 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 206 | static cl::opt<bool> |
| 207 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 208 | cl::desc("Do not take inbounds assumptions at all"), |
| 209 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 210 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 211 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 212 | "polly-ignore-parameter-bounds", |
| 213 | cl::desc( |
| 214 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 215 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 216 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 217 | static cl::opt<bool> PollyAllowDereferenceOfAllFunctionParams( |
| 218 | "polly-allow-dereference-of-all-function-parameters", |
| 219 | cl::desc( |
| 220 | "Treat all parameters to functions that are pointers as dereferencible." |
| 221 | " This is useful for invariant load hoisting, since we can generate" |
| 222 | " less runtime checks. This is only valid if all pointers to functions" |
| 223 | " are always initialized, so that Polly can choose to hoist" |
| 224 | " their loads. "), |
| 225 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 226 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 227 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 228 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 229 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 230 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 231 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 232 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 233 | bool polly::UseInstructionNames; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 234 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 235 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 236 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 237 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 238 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 239 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 240 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 241 | static cl::opt<bool> PollyPrintInstructions( |
| 242 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 243 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 244 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 245 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 246 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 247 | // Create a sequence of two schedules. Either argument may be null and is |
| 248 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 249 | // empty. |
| 250 | static __isl_give isl_schedule * |
| 251 | combineInSequence(__isl_take isl_schedule *Prev, |
| 252 | __isl_take isl_schedule *Succ) { |
| 253 | if (!Prev) |
| 254 | return Succ; |
| 255 | if (!Succ) |
| 256 | return Prev; |
| 257 | |
| 258 | return isl_schedule_sequence(Prev, Succ); |
| 259 | } |
| 260 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 261 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 262 | int dim, isl::dim type) { |
| 263 | isl::val V; |
| 264 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 265 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 266 | // The upper and lower bound for a parameter value is derived either from |
| 267 | // the data type of the parameter or from the - possibly more restrictive - |
| 268 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 269 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 270 | S = S.lower_bound_val(type, dim, V); |
| 271 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 272 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 273 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 274 | if (Range.isFullSet()) |
| 275 | return S; |
| 276 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 277 | if (isl_set_n_basic_set(S.get()) > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 278 | return S; |
| 279 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 280 | // In case of signed wrapping, we can refine the set of valid values by |
| 281 | // excluding the part not covered by the wrapping range. |
| 282 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 283 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 284 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 285 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 286 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 287 | V = V.sub_ui(1); |
| 288 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 289 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 290 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 291 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 292 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 295 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 296 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 297 | if (!BasePtrLI) |
| 298 | return nullptr; |
| 299 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 300 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 301 | return nullptr; |
| 302 | |
| 303 | ScalarEvolution &SE = *S->getSE(); |
| 304 | |
| 305 | auto *OriginBaseSCEV = |
| 306 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 307 | if (!OriginBaseSCEV) |
| 308 | return nullptr; |
| 309 | |
| 310 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 311 | if (!OriginBaseSCEVUnknown) |
| 312 | return nullptr; |
| 313 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 314 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 315 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Tobias Grosser | 27db02b | 2017-08-06 17:25:05 +0000 | [diff] [blame] | 318 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 319 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 320 | const DataLayout &DL, Scop *S, |
| 321 | const char *BaseName) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 322 | : BasePtr(BasePtr), ElementType(ElementType), Kind(Kind), DL(DL), S(*S) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 323 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 324 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 325 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 326 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 327 | UseInstructionNames); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 328 | Id = isl::id::alloc(Ctx, BasePtrName, this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 329 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 330 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 331 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 332 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 333 | BasePtrOriginSAI = nullptr; |
| 334 | return; |
| 335 | } |
| 336 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 337 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 338 | if (BasePtrOriginSAI) |
| 339 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 342 | ScopArrayInfo::~ScopArrayInfo() = default; |
| 343 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 344 | isl::space ScopArrayInfo::getSpace() const { |
| 345 | auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); |
| 346 | Space = Space.set_tuple_id(isl::dim::set, Id); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 347 | return Space; |
| 348 | } |
| 349 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 350 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 351 | isl::union_set WriteSet = S.getWrites().range(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 352 | isl::space Space = getSpace(); |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 353 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 354 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 355 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 358 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 359 | if (Array->getElementType() != getElementType()) |
| 360 | return false; |
| 361 | |
| 362 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 363 | return false; |
| 364 | |
| 365 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 366 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 367 | return false; |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 372 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 373 | if (NewElementType == ElementType) |
| 374 | return; |
| 375 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 376 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 377 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 378 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 379 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 380 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 381 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 382 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 383 | ElementType = NewElementType; |
| 384 | } else { |
| 385 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 386 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 387 | } |
| 388 | } |
| 389 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 390 | /// Make the ScopArrayInfo model a Fortran Array |
| 391 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 392 | assert(FAD && "got invalid Fortran array descriptor"); |
| 393 | if (this->FAD) { |
| 394 | assert(this->FAD == FAD && |
| 395 | "receiving different array descriptors for same array"); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 400 | assert(!this->FAD); |
| 401 | this->FAD = FAD; |
| 402 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 403 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 404 | |
| 405 | std::string param_name = getName(); |
| 406 | param_name += "_fortranarr_size"; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 407 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name, this); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 408 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 409 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 410 | isl::pw_aff PwAff = |
| 411 | 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] | 412 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 413 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 416 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 417 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 418 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 419 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 420 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 421 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 422 | if (CheckConsistency) { |
| 423 | for (int i = 0; i < SharedDims; i++) { |
| 424 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 425 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 426 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 427 | return false; |
| 428 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 429 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 430 | if (DimensionSizes.size() >= NewSizes.size()) |
| 431 | return true; |
| 432 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 433 | |
| 434 | DimensionSizes.clear(); |
| 435 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 436 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 437 | DimensionSizesPw.clear(); |
| 438 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 439 | if (!Expr) { |
| 440 | DimensionSizesPw.push_back(nullptr); |
| 441 | continue; |
| 442 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 443 | isl::pw_aff Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 444 | DimensionSizesPw.push_back(Size); |
| 445 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 446 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 449 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 450 | |
| 451 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 452 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 455 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 456 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 457 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 458 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 459 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 460 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 461 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 462 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 463 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 464 | // If this is a Fortran array, then we can print the outermost dimension |
| 465 | // as a isl_pw_aff even though there is no SCEV information. |
| 466 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 467 | |
| 468 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 469 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 470 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 471 | u++; |
| 472 | } |
| 473 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 474 | OS << "["; |
| 475 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 476 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 477 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 478 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 479 | } else { |
| 480 | OS << *getDimensionSize(u); |
| 481 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 482 | |
| 483 | OS << "]"; |
| 484 | } |
| 485 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 486 | OS << ";"; |
| 487 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 488 | if (BasePtrOriginSAI) |
| 489 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 490 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 491 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 495 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 496 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 497 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 498 | return getFromId(Id); |
| 499 | } |
| 500 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 501 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 502 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 503 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 504 | return SAI; |
| 505 | } |
| 506 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 507 | void MemoryAccess::wrapConstantDimensions() { |
| 508 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 509 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 510 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 511 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 512 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 513 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 514 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 515 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 516 | |
| 517 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 518 | for (int i = DimsArray - 1; i > 0; i--) { |
| 519 | auto *DimSize = SAI->getDimensionSize(i); |
| 520 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 521 | |
| 522 | // This transformation is not applicable to dimensions with dynamic size. |
| 523 | if (!DimSizeCst) |
| 524 | continue; |
| 525 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 526 | // This transformation is not applicable to dimensions of size zero. |
| 527 | if (DimSize->isZero()) |
| 528 | continue; |
| 529 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 530 | isl::val DimSizeVal = |
| 531 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 532 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 533 | isl::aff PrevVar = |
| 534 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 535 | |
| 536 | // Compute: index % size |
| 537 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | cb0224a | 2017-08-06 15:56:45 +0000 | [diff] [blame] | 538 | isl::aff Modulo = Var.mod(DimSizeVal); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 539 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 540 | |
| 541 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 542 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 543 | Divide = Divide.floor(); |
| 544 | Divide = Divide.add(PrevVar); |
| 545 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 546 | |
| 547 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 548 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 549 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 553 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 554 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 555 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 556 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 559 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 560 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 561 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 562 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 563 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 564 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 565 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 566 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 567 | auto DimsMissing = DimsArray - DimsAccess; |
| 568 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 569 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 570 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 571 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 572 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 573 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 574 | isl::map Map = isl::map::from_domain_and_range( |
| 575 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 576 | |
| 577 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 578 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 579 | |
| 580 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 581 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 582 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 583 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 584 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 585 | // For the non delinearized arrays, divide the access function of the last |
| 586 | // subscript by the size of the elements in the array. |
| 587 | // |
| 588 | // A stride one array access in C expressed as A[i] is expressed in |
| 589 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 590 | // two subsequent values of 'i' index two values that are stored next to |
| 591 | // each other in memory. By this division we make this characteristic |
| 592 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 593 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 594 | // that divides the offsets of all accesses to this base pointer. |
| 595 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 596 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 597 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 600 | // We currently do this only if we added at least one dimension, which means |
| 601 | // some dimension's indices have not been specified, an indicator that some |
| 602 | // index values have been added together. |
| 603 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 604 | // expressions more complicated. |
| 605 | if (DimsMissing) |
| 606 | wrapConstantDimensions(); |
| 607 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 608 | if (!isAffine()) |
| 609 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 610 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 611 | // Introduce multi-element accesses in case the type loaded by this memory |
| 612 | // access is larger than the canonical element type of the array. |
| 613 | // |
| 614 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 615 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 616 | if (ElemBytes > ArrayElemSize) { |
| 617 | assert(ElemBytes % ArrayElemSize == 0 && |
| 618 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 619 | isl::map Map = isl::map::from_domain_and_range( |
| 620 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 621 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 622 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 623 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 624 | isl::constraint C; |
| 625 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 626 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 627 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 628 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 629 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 630 | C = isl::constraint::alloc_inequality(LS); |
| 631 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 632 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 633 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 634 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 635 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 636 | C = isl::constraint::alloc_inequality(LS); |
| 637 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 638 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 639 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 640 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 641 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 642 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 645 | const std::string |
| 646 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 647 | switch (RT) { |
| 648 | case MemoryAccess::RT_NONE: |
| 649 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 650 | "access which isn't a reduction"); |
| 651 | case MemoryAccess::RT_ADD: |
| 652 | return "+"; |
| 653 | case MemoryAccess::RT_MUL: |
| 654 | return "*"; |
| 655 | case MemoryAccess::RT_BOR: |
| 656 | return "|"; |
| 657 | case MemoryAccess::RT_BXOR: |
| 658 | return "^"; |
| 659 | case MemoryAccess::RT_BAND: |
| 660 | return "&"; |
| 661 | } |
| 662 | llvm_unreachable("Unknown reduction type"); |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 665 | /// Return the reduction type for a given binary operator. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 666 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 667 | const Instruction *Load) { |
| 668 | if (!BinOp) |
| 669 | return MemoryAccess::RT_NONE; |
| 670 | switch (BinOp->getOpcode()) { |
| 671 | case Instruction::FAdd: |
| 672 | if (!BinOp->hasUnsafeAlgebra()) |
| 673 | return MemoryAccess::RT_NONE; |
| 674 | // Fall through |
| 675 | case Instruction::Add: |
| 676 | return MemoryAccess::RT_ADD; |
| 677 | case Instruction::Or: |
| 678 | return MemoryAccess::RT_BOR; |
| 679 | case Instruction::Xor: |
| 680 | return MemoryAccess::RT_BXOR; |
| 681 | case Instruction::And: |
| 682 | return MemoryAccess::RT_BAND; |
| 683 | case Instruction::FMul: |
| 684 | if (!BinOp->hasUnsafeAlgebra()) |
| 685 | return MemoryAccess::RT_NONE; |
| 686 | // Fall through |
| 687 | case Instruction::Mul: |
| 688 | if (DisableMultiplicativeReductions) |
| 689 | return MemoryAccess::RT_NONE; |
| 690 | return MemoryAccess::RT_MUL; |
| 691 | default: |
| 692 | return MemoryAccess::RT_NONE; |
| 693 | } |
| 694 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 695 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 696 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 697 | isl::id ArrayId = getArrayId(); |
| 698 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 699 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 700 | return SAI; |
| 701 | } |
| 702 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 703 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 704 | isl::id ArrayId = getLatestArrayId(); |
| 705 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 706 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 707 | return SAI; |
| 708 | } |
| 709 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 710 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 711 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 714 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 715 | if (!hasNewAccessRelation()) |
| 716 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 717 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 720 | isl::map MemoryAccess::getAddressFunction() const { |
| 721 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 724 | isl::pw_multi_aff |
| 725 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 726 | isl::map Schedule, ScheduledAccRel; |
| 727 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 728 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 729 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 730 | USchedule = USchedule.intersect_domain(UDomain); |
| 731 | Schedule = isl::map::from_union_map(USchedule); |
| 732 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 733 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 736 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 737 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 740 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 741 | return stringFromIslObj(AccessRelation.get()); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 744 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 745 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 748 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 749 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 752 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 753 | return stringFromIslObj(NewAccessRelation.get()); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 756 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 757 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 760 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 761 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 762 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 763 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 764 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 765 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 766 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 769 | // Formalize no out-of-bound access assumption |
| 770 | // |
| 771 | // When delinearizing array accesses we optimistically assume that the |
| 772 | // delinearized accesses do not access out of bound locations (the subscript |
| 773 | // expression of each array evaluates for each statement instance that is |
| 774 | // executed to a value that is larger than zero and strictly smaller than the |
| 775 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 776 | // dimension for which we do not need to assume any upper bound. At this point |
| 777 | // we formalize this assumption to ensure that at code generation time the |
| 778 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 779 | // |
| 780 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 781 | // first build the set of data locations that are not within array bounds. We |
| 782 | // then apply the reverse access relation to obtain the set of iterations that |
| 783 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 784 | // that are actually executed by intersecting them with the domain of the |
| 785 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 786 | // parameters that may cause statement instances to be executed that may |
| 787 | // possibly yield out of bound memory accesses. The complement of these |
| 788 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 789 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 790 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 791 | if (PollyIgnoreInbounds) |
| 792 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 793 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 794 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 795 | isl::set Outside = isl::set::empty(Space); |
| 796 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 797 | isl::local_space LS(Space); |
| 798 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 799 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 800 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 801 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 802 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 803 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 804 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 805 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 806 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 807 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 810 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 811 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 812 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 813 | |
| 814 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 815 | // Doing so increases the set of parameter combinations that are assumed to |
| 816 | // not appear. This is always save, but may make the resulting run-time check |
| 817 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 818 | Outside = Outside.remove_divs(); |
| 819 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 820 | const auto &Loc = getAccessInstruction() |
| 821 | ? getAccessInstruction()->getDebugLoc() |
| 822 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 823 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 824 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 825 | Statement->getParent()->recordAssumption(INBOUNDS, Outside.release(), Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 826 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 829 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 830 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 831 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 832 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 833 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 834 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 835 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 836 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 837 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 838 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 839 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 840 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 841 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 842 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 843 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 844 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 845 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 846 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 847 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 848 | LengthMap = LengthMap.sum(SubscriptMap); |
| 849 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 850 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 853 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 854 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 855 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 856 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 857 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 858 | return; |
| 859 | |
| 860 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 861 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 862 | return; |
| 863 | |
| 864 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 865 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 866 | return; |
| 867 | |
| 868 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 869 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 870 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 871 | |
| 872 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 873 | if (Range.isFullSet()) |
| 874 | return; |
| 875 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 876 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 877 | return; |
| 878 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 879 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 880 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 881 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 882 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 883 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 884 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 885 | |
| 886 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 887 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 888 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 889 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 890 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 891 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 892 | isl::set AccessRange = Relation.range(); |
| 893 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 894 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 895 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 898 | void MemoryAccess::foldAccessRelation() { |
| 899 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 900 | return; |
| 901 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 902 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 903 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 904 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 905 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 906 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 907 | isl::space Space; |
| 908 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 909 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 910 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 911 | isl::space SpaceSize = DimSize.get_space(); |
| 912 | isl::id ParamId = |
| 913 | give(isl_space_get_dim_id(SpaceSize.get(), isl_dim_param, 0)); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 914 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 915 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 916 | Space = Space.range().map_from_set(); |
| 917 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 918 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 919 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 920 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 921 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 922 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 923 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 924 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 925 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 926 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 927 | for (int j = 0; j < Size; ++j) |
| 928 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 929 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 930 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 931 | isl::local_space LS(Space); |
| 932 | isl::constraint C; |
| 933 | C = isl::constraint::alloc_equality(LS); |
| 934 | C = C.set_constant_si(-1); |
| 935 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 936 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 937 | MapTwo = MapTwo.add_constraint(C); |
| 938 | C = isl::constraint::alloc_equality(LS); |
| 939 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 940 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 941 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 942 | MapTwo = MapTwo.add_constraint(C); |
| 943 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 944 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 945 | MapOne = MapOne.unite(MapTwo); |
| 946 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 947 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 948 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 949 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 950 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 951 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 952 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 953 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 954 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 955 | |
| 956 | // Access dimension folding might in certain cases increase the number of |
| 957 | // disjuncts in the memory access, which can possibly complicate the generated |
| 958 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 959 | if (!PollyPreciseFoldAccesses && |
| 960 | isl_map_n_basic_map(NewAccessRelation.get()) > |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 961 | isl_map_n_basic_map(AccessRelation.get())) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 962 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 963 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 964 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 967 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 968 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 969 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 970 | if (Size == 1) |
| 971 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 972 | |
| 973 | // Only one factor needs to be divisible. |
| 974 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 975 | for (auto *FactorExpr : MulExpr->operands()) |
| 976 | if (isDivisible(FactorExpr, Size, SE)) |
| 977 | return true; |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 982 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 983 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 984 | for (auto *OpExpr : NAryExpr->operands()) |
| 985 | if (!isDivisible(OpExpr, Size, SE)) |
| 986 | return false; |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 991 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 992 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 993 | return MulSCEV == Expr; |
| 994 | } |
| 995 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 996 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 997 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 998 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 999 | // Initialize the invalid domain which describes all iterations for which the |
| 1000 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1001 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1002 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1003 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1004 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1005 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 1006 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 1007 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 1008 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1009 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1012 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 1013 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1014 | // We overapproximate non-affine accesses with a possible access to the |
| 1015 | // whole array. For read accesses it does not make a difference, if an |
| 1016 | // access must or may happen. However, for write accesses it is important to |
| 1017 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1018 | if (AccessRelation.is_null()) |
| 1019 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1020 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1021 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
| 1024 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1025 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 1026 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 1027 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1028 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1029 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 1030 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 1031 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1034 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1035 | AccessRelation = AccessRelation.set_tuple_id( |
| 1036 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 1037 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 1038 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1039 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1040 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 1041 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1042 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1043 | AccessType AccType, Value *BaseAddress, |
| 1044 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1045 | ArrayRef<const SCEV *> Subscripts, |
| 1046 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 1047 | MemoryKind Kind) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1048 | : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(nullptr), |
| 1049 | BaseAddr(BaseAddress), ElementType(ElementType), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1050 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 1051 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1052 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1053 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1054 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1055 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1056 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1057 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1058 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1059 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1060 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1061 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1062 | : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), |
| 1063 | InvalidDomain(nullptr), AccessRelation(nullptr), |
| 1064 | NewAccessRelation(AccRel), FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 1065 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1066 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1067 | Sizes.push_back(nullptr); |
| 1068 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1069 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1070 | ElementType = SAI->getElementType(); |
| 1071 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1072 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1073 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1074 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1075 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1076 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1079 | MemoryAccess::~MemoryAccess() = default; |
| 1080 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1081 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1082 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1083 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 1084 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1087 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1088 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1089 | } |
| 1090 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1091 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1092 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1093 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1094 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1095 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1096 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1097 | else |
| 1098 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1099 | return OS; |
| 1100 | } |
| 1101 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1102 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1103 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1104 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1105 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1106 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1107 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1108 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1109 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1110 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1111 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1112 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1113 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1114 | break; |
| 1115 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1116 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1117 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (FAD) { |
| 1120 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1121 | OS << "] "; |
| 1122 | }; |
| 1123 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1124 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1125 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1126 | if (hasNewAccessRelation()) |
| 1127 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1130 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1131 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1132 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1133 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1134 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1135 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1136 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1137 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1138 | StmtDom = StmtDom.reset_tuple_id(); |
| 1139 | isl::set NewInvalidDom = StmtDom.intersect(isl::manage(PWAC.second)); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1140 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1141 | return isl::manage(PWAC.first); |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1144 | // Create a map in the size of the provided set domain, that maps from the |
| 1145 | // one element of the provided set domain to another element of the provided |
| 1146 | // set domain. |
| 1147 | // The mapping is limited to all points that are equal in all but the last |
| 1148 | // dimension and for which the last dimension of the input is strict smaller |
| 1149 | // than the last dimension of the output. |
| 1150 | // |
| 1151 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1152 | // |
| 1153 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1154 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1155 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1156 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1157 | isl::space Space = SetDomain.map_from_set(); |
| 1158 | isl::map Map = isl::map::universe(Space); |
| 1159 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Set all but the last dimension to be equal for the input and output |
| 1162 | // |
| 1163 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1164 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1165 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1166 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Set the last dimension of the input to be strict smaller than the |
| 1169 | // last dimension of the output. |
| 1170 | // |
| 1171 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1172 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1173 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1176 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1177 | isl::map AccessRelation = getAccessRelation(); |
| 1178 | isl::space Space = Schedule.get_space().range(); |
| 1179 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1180 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1181 | Schedule = Schedule.reverse(); |
| 1182 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1183 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1184 | NextScatt = NextScatt.apply_range(Schedule); |
| 1185 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1186 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1187 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1188 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1189 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1190 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1193 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1194 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1195 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1196 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1197 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1198 | StrideX = isl::set::universe(Stride.get_space()); |
| 1199 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1200 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1201 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1202 | StrideWidth); |
| 1203 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1204 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1205 | return IsStrideX; |
| 1206 | } |
| 1207 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1208 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1209 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1212 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1213 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1216 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1217 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1220 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1221 | assert(NewAccess); |
| 1222 | |
| 1223 | #ifndef NDEBUG |
| 1224 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1225 | isl::space NewSpace = NewAccess.get_space(); |
| 1226 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1227 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1228 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1229 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1230 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1231 | // subdomain only. |
| 1232 | if (isRead()) { |
| 1233 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1234 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1235 | StmtDomain = |
| 1236 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1237 | isl::set NewDomain = NewAccess.domain(); |
| 1238 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1239 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1240 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1241 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1242 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1243 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1244 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1245 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1246 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1247 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1250 | InvariantEquivClassTy *EqClass = |
| 1251 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1252 | SAI->getBasePtr()); |
| 1253 | assert(EqClass && |
| 1254 | "Access functions to indirect arrays must have an invariant and " |
| 1255 | "hoisted base pointer"); |
| 1256 | } |
| 1257 | |
| 1258 | // Check whether access dimensions correspond to number of dimensions of the |
| 1259 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1260 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1261 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1262 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1265 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1266 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1267 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1268 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1269 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1270 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1271 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1272 | |
| 1273 | return isl_set_is_subset(StmtDom.keep(), AccDom.keep()) == isl_bool_false; |
| 1274 | } |
| 1275 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1276 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1277 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1278 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1279 | isl::set Domain = getDomain(); |
| 1280 | if (Domain.is_empty()) |
| 1281 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1282 | auto Schedule = getParent()->getSchedule(); |
| 1283 | if (!Schedule) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1284 | return nullptr; |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1285 | Schedule = Schedule.intersect_domain(isl::union_set(Domain)); |
| 1286 | if (Schedule.is_empty()) |
| 1287 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1288 | isl::map M = M.from_union_map(Schedule); |
| 1289 | M = M.coalesce(); |
| 1290 | M = M.gist_domain(Domain); |
| 1291 | M = M.coalesce(); |
| 1292 | return M; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1293 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1294 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1295 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1296 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1297 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1298 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1301 | void ScopStmt::buildAccessRelations() { |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 1302 | Scop &S = *getParent(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1303 | for (MemoryAccess *Access : MemAccs) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1304 | Type *ElementType = Access->getElementType(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1305 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1306 | MemoryKind Ty; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1307 | if (Access->isPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1308 | Ty = MemoryKind::PHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1309 | else if (Access->isExitPHIKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1310 | Ty = MemoryKind::ExitPHI; |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1311 | else if (Access->isValueKind()) |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1312 | Ty = MemoryKind::Value; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1313 | else |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1314 | Ty = MemoryKind::Array; |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1315 | |
Tobias Grosser | 296fe2e | 2017-02-10 10:09:46 +0000 | [diff] [blame] | 1316 | auto *SAI = S.getOrCreateScopArrayInfo(Access->getOriginalBaseAddr(), |
| 1317 | ElementType, Access->Sizes, Ty); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1318 | Access->buildAccessRelation(SAI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1319 | S.addAccessData(Access); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1323 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 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 | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1331 | assert(!ValueWrites.lookup(AccessVal)); |
| 1332 | |
| 1333 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1334 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1335 | Value *AccessVal = Access->getAccessValue(); |
| 1336 | assert(!ValueReads.lookup(AccessVal)); |
| 1337 | |
| 1338 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1339 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1340 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1341 | assert(!PHIWrites.lookup(PHI)); |
| 1342 | |
| 1343 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1344 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1345 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1346 | assert(!PHIReads.lookup(PHI)); |
| 1347 | |
| 1348 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1351 | if (Prepend) { |
| 1352 | MemAccs.insert(MemAccs.begin(), Access); |
| 1353 | return; |
| 1354 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1355 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1358 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1359 | for (MemoryAccess *MA : *this) |
| 1360 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1361 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1362 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1363 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1364 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1367 | /// 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] | 1368 | static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet, |
| 1369 | void *User) { |
| 1370 | isl_set **BoundedParts = static_cast<isl_set **>(User); |
| 1371 | if (isl_basic_set_is_bounded(BSet)) |
| 1372 | *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet)); |
| 1373 | else |
| 1374 | isl_basic_set_free(BSet); |
| 1375 | return isl_stat_ok; |
| 1376 | } |
| 1377 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1378 | /// Return the bounded parts of @p S. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1379 | static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) { |
| 1380 | isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S)); |
| 1381 | isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts); |
| 1382 | isl_set_free(S); |
| 1383 | return BoundedParts; |
| 1384 | } |
| 1385 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1386 | /// 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] | 1387 | /// |
| 1388 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1389 | /// both with regards to the dimension @p Dim. |
| 1390 | static std::pair<__isl_give isl_set *, __isl_give isl_set *> |
| 1391 | partitionSetParts(__isl_take isl_set *S, unsigned Dim) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1392 | 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] | 1393 | S = isl_set_lower_bound_si(S, isl_dim_set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1394 | |
| 1395 | unsigned NumDimsS = isl_set_n_dim(S); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1396 | isl_set *OnlyDimS = isl_set_copy(S); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1399 | assert(NumDimsS >= Dim + 1); |
| 1400 | OnlyDimS = |
| 1401 | isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1); |
| 1402 | |
| 1403 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1404 | // as we are not interested in them. |
| 1405 | OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim); |
| 1406 | for (unsigned u = 0; u < Dim; u++) { |
| 1407 | isl_constraint *C = isl_inequality_alloc( |
| 1408 | isl_local_space_from_space(isl_set_get_space(OnlyDimS))); |
| 1409 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1); |
| 1410 | C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1); |
| 1411 | OnlyDimS = isl_set_add_constraint(OnlyDimS, C); |
| 1412 | } |
| 1413 | |
| 1414 | // Collect all bounded parts of OnlyDimS. |
| 1415 | isl_set *BoundedParts = collectBoundedParts(OnlyDimS); |
| 1416 | |
| 1417 | // Create the dimensions greater than Dim again. |
| 1418 | BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1, |
| 1419 | NumDimsS - Dim - 1); |
| 1420 | |
| 1421 | // Remove the artificial upper bound parameters again. |
| 1422 | BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim); |
| 1423 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1424 | isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1425 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1426 | } |
| 1427 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1428 | /// Set the dimension Ids from @p From in @p To. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1429 | static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From, |
| 1430 | __isl_take isl_set *To) { |
| 1431 | for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) { |
| 1432 | isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u); |
| 1433 | To = isl_set_set_dim_id(To, isl_dim_set, u, DimId); |
| 1434 | } |
| 1435 | return To; |
| 1436 | } |
| 1437 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1438 | /// 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] | 1439 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1440 | __isl_take isl_pw_aff *L, |
| 1441 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1442 | switch (Pred) { |
| 1443 | case ICmpInst::ICMP_EQ: |
| 1444 | return isl_pw_aff_eq_set(L, R); |
| 1445 | case ICmpInst::ICMP_NE: |
| 1446 | return isl_pw_aff_ne_set(L, R); |
| 1447 | case ICmpInst::ICMP_SLT: |
| 1448 | return isl_pw_aff_lt_set(L, R); |
| 1449 | case ICmpInst::ICMP_SLE: |
| 1450 | return isl_pw_aff_le_set(L, R); |
| 1451 | case ICmpInst::ICMP_SGT: |
| 1452 | return isl_pw_aff_gt_set(L, R); |
| 1453 | case ICmpInst::ICMP_SGE: |
| 1454 | return isl_pw_aff_ge_set(L, R); |
| 1455 | case ICmpInst::ICMP_ULT: |
| 1456 | return isl_pw_aff_lt_set(L, R); |
| 1457 | case ICmpInst::ICMP_UGT: |
| 1458 | return isl_pw_aff_gt_set(L, R); |
| 1459 | case ICmpInst::ICMP_ULE: |
| 1460 | return isl_pw_aff_le_set(L, R); |
| 1461 | case ICmpInst::ICMP_UGE: |
| 1462 | return isl_pw_aff_ge_set(L, R); |
| 1463 | default: |
| 1464 | llvm_unreachable("Non integer predicate not supported"); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1468 | /// 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] | 1469 | /// |
| 1470 | /// Helper function that will make sure the dimensions of the result have the |
| 1471 | /// same isl_id's as the @p Domain. |
| 1472 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
| 1473 | __isl_take isl_pw_aff *L, |
| 1474 | __isl_take isl_pw_aff *R, |
| 1475 | __isl_keep isl_set *Domain) { |
| 1476 | isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R); |
| 1477 | return setDimensionIds(Domain, ConsequenceCondSet); |
| 1478 | } |
| 1479 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1480 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1481 | /// |
| 1482 | /// @param S The Scop in which @p BB resides in. |
| 1483 | /// @param BB The BB for which isl representation is to be |
| 1484 | /// computed. |
| 1485 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1486 | /// @param E The SCEV that should be translated. |
| 1487 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1488 | /// |
| 1489 | /// Note that this function will also adjust the invalid context accordingly. |
| 1490 | |
| 1491 | __isl_give isl_pw_aff * |
| 1492 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1493 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1494 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1495 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1496 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(isl::manage(PWAC.second)); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1497 | return PWAC.first; |
| 1498 | } |
| 1499 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1500 | /// 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] | 1501 | /// |
| 1502 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1503 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1504 | /// have as many elements as @p SI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1505 | static bool |
| 1506 | buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1507 | __isl_keep isl_set *Domain, |
| 1508 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1509 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1510 | Value *Condition = getConditionFromTerminator(SI); |
| 1511 | assert(Condition && "No condition for switch"); |
| 1512 | |
| 1513 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1514 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1515 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1516 | |
| 1517 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1518 | ConditionSets.resize(NumSuccessors); |
| 1519 | for (auto &Case : SI->cases()) { |
| 1520 | unsigned Idx = Case.getSuccessorIndex(); |
| 1521 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1522 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1523 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1524 | isl_set *CaseConditionSet = |
| 1525 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain); |
| 1526 | ConditionSets[Idx] = isl_set_coalesce( |
| 1527 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1528 | } |
| 1529 | |
| 1530 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1531 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1532 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1533 | ConditionSetUnion = |
| 1534 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
| 1535 | ConditionSets[0] = setDimensionIds( |
| 1536 | Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion)); |
| 1537 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1538 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1539 | |
| 1540 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1543 | /// Build condition sets for unsigned ICmpInst(s). |
| 1544 | /// Special handling is required for unsigned operands to ensure that if |
| 1545 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1546 | /// it should wrap around. |
| 1547 | /// |
| 1548 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1549 | /// between TestVal and UpperBound, i.e, |
| 1550 | /// TestVal < UpperBound OR TestVal <= UpperBound |
| 1551 | static __isl_give isl_set * |
| 1552 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1553 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1554 | const SCEV *SCEV_UpperBound, |
| 1555 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1556 | bool IsStrictUpperBound) { |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1557 | // Do not take NonNeg assumption on TestVal |
| 1558 | // as it might have MSB (Sign bit) set. |
| 1559 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1560 | // Take NonNeg assumption on UpperBound. |
| 1561 | isl_pw_aff *UpperBound = |
| 1562 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1563 | |
| 1564 | // 0 <= TestVal |
| 1565 | isl_set *First = |
| 1566 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1567 | isl_pw_aff_get_domain_space(TestVal))), |
| 1568 | isl_pw_aff_copy(TestVal)); |
| 1569 | |
| 1570 | isl_set *Second; |
| 1571 | if (IsStrictUpperBound) |
| 1572 | // TestVal < UpperBound |
| 1573 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1574 | else |
| 1575 | // TestVal <= UpperBound |
| 1576 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1577 | |
| 1578 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
| 1579 | ConsequenceCondSet = setDimensionIds(Domain, ConsequenceCondSet); |
| 1580 | return ConsequenceCondSet; |
| 1581 | } |
| 1582 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1583 | /// Build the conditions sets for the branch condition @p Condition in |
| 1584 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1585 | /// |
| 1586 | /// This will fill @p ConditionSets with the conditions under which control |
| 1587 | /// 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] | 1588 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1589 | /// context under which @p Condition is true/false will be returned as the |
| 1590 | /// new elements of @p ConditionSets. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1591 | static bool |
| 1592 | buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1593 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1594 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1595 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1596 | isl_set *ConsequenceCondSet = nullptr; |
| 1597 | if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
| 1598 | if (CCond->isZero()) |
| 1599 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1600 | else |
| 1601 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1602 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1603 | auto Opcode = BinOp->getOpcode(); |
| 1604 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1605 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1606 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1607 | InvalidDomainMap, ConditionSets) && |
| 1608 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1609 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1610 | if (!Valid) { |
| 1611 | while (!ConditionSets.empty()) |
| 1612 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1613 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1614 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1615 | |
| 1616 | isl_set_free(ConditionSets.pop_back_val()); |
| 1617 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1618 | isl_set_free(ConditionSets.pop_back_val()); |
| 1619 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1620 | |
| 1621 | if (Opcode == Instruction::And) |
| 1622 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1623 | else |
| 1624 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1625 | } else { |
| 1626 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1627 | assert(ICond && |
| 1628 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1629 | |
| 1630 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1631 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1632 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1633 | // to be set. The comparison is equal to a signed comparison under this |
| 1634 | // assumption. |
| 1635 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1636 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1637 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1638 | |
| 1639 | switch (ICond->getPredicate()) { |
| 1640 | case ICmpInst::ICMP_ULT: |
| 1641 | ConsequenceCondSet = |
| 1642 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1643 | RightOperand, InvalidDomainMap, true); |
| 1644 | break; |
| 1645 | case ICmpInst::ICMP_ULE: |
| 1646 | ConsequenceCondSet = |
| 1647 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1648 | RightOperand, InvalidDomainMap, false); |
| 1649 | break; |
| 1650 | case ICmpInst::ICMP_UGT: |
| 1651 | ConsequenceCondSet = |
| 1652 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1653 | LeftOperand, InvalidDomainMap, true); |
| 1654 | break; |
| 1655 | case ICmpInst::ICMP_UGE: |
| 1656 | ConsequenceCondSet = |
| 1657 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1658 | LeftOperand, InvalidDomainMap, false); |
| 1659 | break; |
| 1660 | default: |
| 1661 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1662 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
| 1663 | ConsequenceCondSet = |
| 1664 | buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain); |
| 1665 | break; |
| 1666 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1669 | // If no terminator was given we are only looking for parameter constraints |
| 1670 | // under which @p Condition is true/false. |
| 1671 | if (!TI) |
| 1672 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1673 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1674 | ConsequenceCondSet = isl_set_coalesce( |
| 1675 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1676 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1677 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1678 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1679 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1680 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1681 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1682 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1683 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1684 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1685 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1688 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1689 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1690 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1691 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1692 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1693 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | ConditionSets.push_back(ConsequenceCondSet); |
| 1697 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1698 | |
| 1699 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1702 | /// 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] | 1703 | /// |
| 1704 | /// This will fill @p ConditionSets with the conditions under which control |
| 1705 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1706 | /// have as many elements as @p TI has successors. |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1707 | static bool |
| 1708 | buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1709 | __isl_keep isl_set *Domain, |
| 1710 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1711 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1712 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1713 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1714 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1715 | |
| 1716 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1717 | |
| 1718 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1719 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1720 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1723 | Value *Condition = getConditionFromTerminator(TI); |
| 1724 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1725 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1726 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1727 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1730 | void ScopStmt::buildDomain() { |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1731 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 1732 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1733 | Domain = getParent()->getDomainConditions(this); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1734 | Domain = Domain.set_tuple_id(Id); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1737 | void ScopStmt::collectSurroundingLoops() { |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1738 | for (unsigned u = 0, e = Domain.dim(isl::dim::set); u < e; u++) { |
| 1739 | isl::id DimId = Domain.get_dim_id(isl::dim::set, u); |
| 1740 | NestLoops.push_back(static_cast<Loop *>(DimId.get_user())); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 1744 | ScopStmt::ScopStmt(Scop &parent, Region &R, Loop *SurroundingLoop) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1745 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), R(&R), |
| 1746 | Build(nullptr), SurroundingLoop(SurroundingLoop) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1747 | BaseName = getIslCompatibleName( |
| 1748 | "Stmt", R.getNameStr(), parent.getNextStmtIdx(), "", UseInstructionNames); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1751 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, Loop *SurroundingLoop, |
| 1752 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1753 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1754 | Build(nullptr), SurroundingLoop(SurroundingLoop), |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1755 | Instructions(Instructions) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1756 | BaseName = getIslCompatibleName("Stmt", &bb, parent.getNextStmtIdx(), "", |
| 1757 | UseInstructionNames); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1760 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1761 | isl::set NewDomain) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1762 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), |
| 1763 | Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1764 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1765 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1766 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1767 | Domain = Domain.set_tuple_id(Id); |
| 1768 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1769 | auto *Access = |
| 1770 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1771 | parent.addAccessFunction(Access); |
| 1772 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1773 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1774 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1775 | parent.addAccessFunction(Access); |
| 1776 | addAccess(Access); |
| 1777 | } |
| 1778 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1779 | ScopStmt::~ScopStmt() = default; |
| 1780 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 1781 | void ScopStmt::init(LoopInfo &LI) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1782 | assert(!Domain && "init must be called only once"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1783 | |
Johannes Doerfert | 32ae76e | 2015-09-10 13:12:02 +0000 | [diff] [blame] | 1784 | buildDomain(); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1785 | collectSurroundingLoops(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1786 | buildAccessRelations(); |
| 1787 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame] | 1788 | if (DetectReductions) |
| 1789 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1792 | /// Collect loads which might form a reduction chain with @p StoreMA. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1793 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1794 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 1795 | /// two loads as operands. If the binary operand is commutative & associative, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1796 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 1797 | /// once, we have found a possible reduction chain. It starts at an operand |
| 1798 | /// load and includes the binary operator and @p StoreMA. |
| 1799 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1800 | /// 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] | 1801 | /// escape this block or into any other store except @p StoreMA. |
| 1802 | void ScopStmt::collectCandiateReductionLoads( |
| 1803 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 1804 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 1805 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1806 | return; |
| 1807 | |
| 1808 | // Skip if there is not one binary operator between the load and the store |
| 1809 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1810 | if (!BinOp) |
| 1811 | return; |
| 1812 | |
| 1813 | // Skip if the binary operators has multiple uses |
| 1814 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1815 | return; |
| 1816 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1817 | // Skip if the opcode of the binary operator is not commutative/associative |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1818 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 1819 | return; |
| 1820 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1821 | // Skip if the binary operator is outside the current SCoP |
| 1822 | if (BinOp->getParent() != Store->getParent()) |
| 1823 | return; |
| 1824 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 1825 | // Skip if it is a multiplicative reduction and we disabled them |
| 1826 | if (DisableMultiplicativeReductions && |
| 1827 | (BinOp->getOpcode() == Instruction::Mul || |
| 1828 | BinOp->getOpcode() == Instruction::FMul)) |
| 1829 | return; |
| 1830 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1831 | // Check the binary operator operands for a candidate load |
| 1832 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 1833 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 1834 | if (!PossibleLoad0 && !PossibleLoad1) |
| 1835 | return; |
| 1836 | |
| 1837 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 1838 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1839 | if (PossibleLoad0->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1840 | Loads.push_back(&getArrayAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1841 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 1842 | if (PossibleLoad1->getParent() == Store->getParent()) |
Tobias Grosser | 35ec5fb | 2015-12-15 23:50:04 +0000 | [diff] [blame] | 1843 | Loads.push_back(&getArrayAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1846 | /// Check for reductions in this ScopStmt. |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1847 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1848 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 1849 | /// like chains. For all candidates we check if they have the same base address |
| 1850 | /// and there are no other accesses which overlap with them. The base address |
| 1851 | /// check rules out impossible reductions candidates early. The overlap check, |
| 1852 | /// together with the "only one user" check in collectCandiateReductionLoads, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1853 | /// guarantees that none of the intermediate results will escape during |
| 1854 | /// execution of the loop nest. We basically check here that no other memory |
| 1855 | /// access can access the same memory as the potential reduction. |
| 1856 | void ScopStmt::checkForReductions() { |
| 1857 | SmallVector<MemoryAccess *, 2> Loads; |
| 1858 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 1859 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1860 | // First collect candidate load-store reduction chains by iterating over all |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1861 | // stores and collecting possible reduction loads. |
| 1862 | for (MemoryAccess *StoreMA : MemAccs) { |
| 1863 | if (StoreMA->isRead()) |
| 1864 | continue; |
| 1865 | |
| 1866 | Loads.clear(); |
| 1867 | collectCandiateReductionLoads(StoreMA, Loads); |
| 1868 | for (MemoryAccess *LoadMA : Loads) |
| 1869 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 1870 | } |
| 1871 | |
| 1872 | // Then check each possible candidate pair. |
| 1873 | for (const auto &CandidatePair : Candidates) { |
| 1874 | bool Valid = true; |
Tobias Grosser | b841753 | 2017-08-15 03:45:55 +0000 | [diff] [blame] | 1875 | isl::map LoadAccs = CandidatePair.first->getAccessRelation(); |
| 1876 | isl::map StoreAccs = CandidatePair.second->getAccessRelation(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Skip those with obviously unequal base addresses. |
Tobias Grosser | b841753 | 2017-08-15 03:45:55 +0000 | [diff] [blame] | 1879 | if (!LoadAccs.has_equal_space(StoreAccs)) { |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1880 | continue; |
| 1881 | } |
| 1882 | |
| 1883 | // And check if the remaining for overlap with other memory accesses. |
Tobias Grosser | b841753 | 2017-08-15 03:45:55 +0000 | [diff] [blame] | 1884 | isl::map AllAccsRel = LoadAccs.unite(StoreAccs); |
| 1885 | AllAccsRel = AllAccsRel.intersect_domain(getDomain()); |
| 1886 | isl::set AllAccs = AllAccsRel.range(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1887 | |
| 1888 | for (MemoryAccess *MA : MemAccs) { |
| 1889 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1890 | continue; |
| 1891 | |
Tobias Grosser | b841753 | 2017-08-15 03:45:55 +0000 | [diff] [blame] | 1892 | isl::map AccRel = MA->getAccessRelation().intersect_domain(getDomain()); |
| 1893 | isl::set Accs = AccRel.range(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1894 | |
Tobias Grosser | b841753 | 2017-08-15 03:45:55 +0000 | [diff] [blame] | 1895 | if (AllAccs.has_equal_space(Accs)) { |
| 1896 | isl::set OverlapAccs = Accs.intersect(AllAccs); |
| 1897 | Valid = Valid && OverlapAccs.is_empty(); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1901 | if (!Valid) |
| 1902 | continue; |
| 1903 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1904 | const LoadInst *Load = |
| 1905 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1906 | MemoryAccess::ReductionType RT = |
| 1907 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1908 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1909 | // If no overlapping access was found we mark the load and store as |
| 1910 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1911 | CandidatePair.first->markAsReductionLike(RT); |
| 1912 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1913 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1916 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1917 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1918 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1919 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1920 | if (!S) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 1921 | return {}; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1922 | auto Str = stringFromIslObj(S); |
| 1923 | isl_map_free(S); |
| 1924 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1927 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1928 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1929 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1930 | if (isBlockStmt()) |
| 1931 | return getBasicBlock(); |
| 1932 | return getRegion()->getEntry(); |
| 1933 | } |
| 1934 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1935 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1936 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1937 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1938 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1939 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1940 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1943 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1944 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1945 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1946 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1947 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1948 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1949 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1950 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1951 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1952 | OS << "Instructions {\n"; |
| 1953 | |
| 1954 | for (Instruction *Inst : Instructions) |
| 1955 | OS.indent(16) << *Inst << "\n"; |
| 1956 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1957 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1960 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1961 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1962 | OS.indent(12) << "Domain :=\n"; |
| 1963 | |
| 1964 | if (Domain) { |
| 1965 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1966 | } else |
| 1967 | OS.indent(16) << "n/a\n"; |
| 1968 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1969 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1970 | |
| 1971 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1972 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1973 | } else |
| 1974 | OS.indent(16) << "n/a\n"; |
| 1975 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1976 | for (MemoryAccess *Access : MemAccs) |
| 1977 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1978 | |
Michael Kruse | eca86ce | 2017-07-26 22:01:33 +0000 | [diff] [blame] | 1979 | if (PrintInstructions && isBlockStmt()) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1980 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1983 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1984 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1985 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1986 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1987 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1988 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1989 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1990 | (void)Found; |
| 1991 | assert(Found && "Expected access data not found"); |
| 1992 | } |
| 1993 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1994 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1995 | (void)Found; |
| 1996 | assert(Found && "Expected access data not found"); |
| 1997 | } |
| 1998 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1999 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 2000 | (void)Found; |
| 2001 | assert(Found && "Expected access data not found"); |
| 2002 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 2003 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 2004 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 2005 | (void)Found; |
| 2006 | assert(Found && "Expected access data not found"); |
| 2007 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 2010 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 2011 | // Remove the memory accesses from this statement together with all scalar |
| 2012 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 2013 | // instruction, hence would not be removed by this function. However, it is |
| 2014 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 2015 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 2016 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 2017 | auto Predicate = [&](MemoryAccess *Acc) { |
| 2018 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 2019 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2020 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2021 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2022 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2023 | Parent.removeAccessData(MA); |
| 2024 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2025 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 2026 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 2027 | MemAccs.end()); |
| 2028 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2031 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA) { |
| 2032 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 2033 | assert(MAIt != MemAccs.end()); |
| 2034 | MemAccs.erase(MAIt); |
| 2035 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2036 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2037 | Parent.removeAccessData(MA); |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 2038 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 2039 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 2040 | if (It != InstructionToAccess.end()) { |
| 2041 | It->second.remove(MA); |
| 2042 | if (It->second.empty()) |
| 2043 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 2044 | } |
| 2045 | } |
| 2046 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 2047 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 2048 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 2049 | if (Access) |
| 2050 | return Access; |
| 2051 | |
| 2052 | ScopArrayInfo *SAI = |
| 2053 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 2054 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 2055 | true, {}, {}, V, MemoryKind::Value); |
| 2056 | Parent.addAccessFunction(Access); |
| 2057 | Access->buildAccessRelation(SAI); |
| 2058 | addAccess(Access); |
| 2059 | Parent.addAccessData(Access); |
| 2060 | return Access; |
| 2061 | } |
| 2062 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2063 | raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { |
| 2064 | S.print(OS, PollyPrintInstructions); |
| 2065 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2068 | //===----------------------------------------------------------------------===// |
| 2069 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2070 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 2071 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 2072 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 2073 | isl_set_free(Context); |
| 2074 | Context = NewContext; |
| 2075 | } |
| 2076 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2077 | namespace { |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2078 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2079 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2080 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2081 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2082 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2083 | |
| 2084 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2085 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 2086 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 2087 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2088 | |
| 2089 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2090 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2091 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 2092 | return SSPR.visit(E); |
| 2093 | } |
| 2094 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 2095 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 2096 | auto *Start = visit(E->getStart()); |
| 2097 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 2098 | visit(E->getStepRecurrence(SE)), |
| 2099 | E->getLoop(), SCEV::FlagAnyWrap); |
| 2100 | return SE.getAddExpr(Start, AddRec); |
| 2101 | } |
| 2102 | |
| 2103 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 2104 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 2105 | return SE.getUnknown(NewValue); |
| 2106 | return E; |
| 2107 | } |
| 2108 | }; |
| 2109 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2110 | /// Check whether we should remap a SCEV expression. |
| 2111 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2112 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2113 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2114 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2115 | |
| 2116 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2117 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 2118 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2119 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 2120 | |
| 2121 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2122 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2123 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 2124 | SFIS.visitAll(E); |
| 2125 | return SFIS.FoundInside; |
| 2126 | } |
| 2127 | |
| 2128 | bool follow(const SCEV *E) { |
| 2129 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 2130 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 2131 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 2132 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 2133 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 2134 | } |
| 2135 | return !FoundInside; |
| 2136 | } |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2137 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2138 | bool isDone() { return FoundInside; } |
| 2139 | }; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2140 | |
| 2141 | } // end anonymous namespace |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2142 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2143 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 2144 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 2145 | // doesn't like addition between an AddRec and an expression that |
| 2146 | // doesn't have a dominance relationship with it.) |
| 2147 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 2148 | return E; |
| 2149 | |
| 2150 | // Rewrite SCEV. |
| 2151 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2154 | // This table of function names is used to translate parameter names in more |
| 2155 | // human-readable names. This makes it easier to interpret Polly analysis |
| 2156 | // results. |
| 2157 | StringMap<std::string> KnownNames = { |
| 2158 | {"_Z13get_global_idj", "global_id"}, |
| 2159 | {"_Z12get_local_idj", "local_id"}, |
| 2160 | {"_Z15get_global_sizej", "global_size"}, |
| 2161 | {"_Z14get_local_sizej", "local_size"}, |
| 2162 | {"_Z12get_work_dimv", "work_dim"}, |
| 2163 | {"_Z17get_global_offsetj", "global_offset"}, |
| 2164 | {"_Z12get_group_idj", "group_id"}, |
| 2165 | {"_Z14get_num_groupsj", "num_groups"}, |
| 2166 | }; |
| 2167 | |
| 2168 | static std::string getCallParamName(CallInst *Call) { |
| 2169 | std::string Result; |
| 2170 | raw_string_ostream OS(Result); |
| 2171 | std::string Name = Call->getCalledFunction()->getName(); |
| 2172 | |
| 2173 | auto Iterator = KnownNames.find(Name); |
| 2174 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 2175 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2176 | OS << Name; |
| 2177 | for (auto &Operand : Call->arg_operands()) { |
| 2178 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 2179 | OS << "_" << Op->getValue(); |
| 2180 | } |
| 2181 | OS.flush(); |
| 2182 | return Result; |
| 2183 | } |
| 2184 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2185 | void Scop::createParameterId(const SCEV *Parameter) { |
| 2186 | assert(Parameters.count(Parameter)); |
| 2187 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2188 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2189 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2190 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2191 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 2192 | Value *Val = ValueParameter->getValue(); |
| 2193 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2194 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2195 | if (Call && isConstCall(Call)) { |
| 2196 | ParameterName = getCallParamName(Call); |
| 2197 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2198 | // If this parameter references a specific Value and this value has a name |
| 2199 | // we use this name as it is likely to be unique and more useful than just |
| 2200 | // a number. |
| 2201 | if (Val->hasName()) |
| 2202 | ParameterName = Val->getName(); |
| 2203 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 2204 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 2205 | if (LoadOrigin->hasName()) { |
| 2206 | ParameterName += "_loaded_from_"; |
| 2207 | ParameterName += |
| 2208 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 2209 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2210 | } |
| 2211 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2212 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2213 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 2214 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 2215 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2216 | isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2217 | const_cast<void *>((const void *)Parameter)); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2218 | ParameterIds[Parameter] = Id; |
| 2219 | } |
| 2220 | |
| 2221 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2222 | for (const SCEV *Parameter : NewParameters) { |
| 2223 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2224 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2225 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2226 | |
| 2227 | if (Parameters.insert(Parameter)) |
| 2228 | createParameterId(Parameter); |
| 2229 | } |
| 2230 | } |
| 2231 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 2232 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2233 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2234 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2235 | return ParameterIds.lookup(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2236 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2237 | |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2238 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2239 | isl_set *DomainContext = isl_union_set_params(getDomains().release()); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2240 | return isl::manage(isl_set_intersect_params(C.release(), DomainContext)); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2243 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2244 | return DT.dominates(BB, getEntry()); |
| 2245 | } |
| 2246 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2247 | void Scop::addUserAssumptions( |
| 2248 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2249 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2250 | for (auto &Assumption : AC.assumptions()) { |
| 2251 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2252 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2253 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2254 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2255 | bool InScop = contains(CI); |
| 2256 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2257 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2258 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2259 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2260 | auto *Val = CI->getArgOperand(0); |
| 2261 | ParameterSetTy DetectedParams; |
| 2262 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2263 | ORE.emit( |
| 2264 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 2265 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2266 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2267 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2268 | |
| 2269 | // Collect all newly introduced parameters. |
| 2270 | ParameterSetTy NewParams; |
| 2271 | for (auto *Param : DetectedParams) { |
| 2272 | Param = extractConstantFactor(Param, *SE).second; |
| 2273 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2274 | if (Parameters.count(Param)) |
| 2275 | continue; |
| 2276 | NewParams.insert(Param); |
| 2277 | } |
| 2278 | |
| 2279 | SmallVector<isl_set *, 2> ConditionSets; |
| 2280 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2281 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
| 2282 | auto *Dom = InScop ? DomainMap[BB].copy() : isl_set_copy(Context); |
| 2283 | assert(Dom && "Cannot propagate a nullptr."); |
| 2284 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 2285 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2286 | isl_set_free(Dom); |
| 2287 | |
| 2288 | if (!Valid) |
| 2289 | continue; |
| 2290 | |
| 2291 | isl_set *AssumptionCtx = nullptr; |
| 2292 | if (InScop) { |
| 2293 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2294 | isl_set_free(ConditionSets[0]); |
| 2295 | } else { |
| 2296 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2297 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2298 | } |
| 2299 | |
| 2300 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2301 | if (!NewParams.empty()) { |
| 2302 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2303 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2304 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2305 | isl_id_free(Id); |
| 2306 | |
| 2307 | if (!NewParams.count(Param)) |
| 2308 | continue; |
| 2309 | |
| 2310 | AssumptionCtx = |
| 2311 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2312 | } |
| 2313 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2314 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2315 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2316 | Context = isl_set_intersect(Context, AssumptionCtx); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2320 | void Scop::addUserContext() { |
| 2321 | if (UserContextStr.empty()) |
| 2322 | return; |
| 2323 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2324 | isl_set *UserContext = |
| 2325 | isl_set_read_from_str(getIslCtx(), UserContextStr.c_str()); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2326 | isl_space *Space = getParamSpace().release(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2327 | if (isl_space_dim(Space, isl_dim_param) != |
| 2328 | isl_set_dim(UserContext, isl_dim_param)) { |
| 2329 | auto SpaceStr = isl_space_to_str(Space); |
| 2330 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2331 | << "number of dimensions than the computed context. Due to this " |
| 2332 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2333 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2334 | free(SpaceStr); |
| 2335 | isl_set_free(UserContext); |
| 2336 | isl_space_free(Space); |
| 2337 | return; |
| 2338 | } |
| 2339 | |
| 2340 | 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] | 2341 | auto *NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 2342 | auto *NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2343 | |
| 2344 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 2345 | auto SpaceStr = isl_space_to_str(Space); |
| 2346 | errs() << "Error: the name of dimension " << i |
| 2347 | << " provided in -polly-context " |
| 2348 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2349 | << "context is '" << NameContext |
| 2350 | << "'. Due to this name mismatch, " |
| 2351 | << "the -polly-context option is ignored. Please provide " |
| 2352 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2353 | free(SpaceStr); |
| 2354 | isl_set_free(UserContext); |
| 2355 | isl_space_free(Space); |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | UserContext = |
| 2360 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 2361 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 2362 | } |
| 2363 | |
| 2364 | Context = isl_set_intersect(Context, UserContext); |
| 2365 | isl_space_free(Space); |
| 2366 | } |
| 2367 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2368 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2369 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2370 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2371 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2372 | for (LoadInst *LInst : RIL) { |
| 2373 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2374 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2375 | Type *Ty = LInst->getType(); |
| 2376 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2377 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2378 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2379 | continue; |
| 2380 | } |
| 2381 | |
| 2382 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2383 | InvariantEquivClasses.emplace_back( |
| 2384 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2385 | } |
| 2386 | } |
| 2387 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2388 | void Scop::buildContext() { |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2389 | isl_space *Space = isl_space_params_alloc(getIslCtx(), 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2390 | Context = isl_set_universe(isl_space_copy(Space)); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2391 | InvalidContext = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2392 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2395 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2396 | unsigned PDim = 0; |
| 2397 | for (auto *Parameter : Parameters) { |
| 2398 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 2399 | Context = |
| 2400 | addRangeBoundsToSet(give(Context), SRange, PDim++, isl::dim::param) |
| 2401 | .release(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2405 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 2406 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2407 | for (auto Array : Arrays) { |
| 2408 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2409 | // for its outermost dimension. Fortran arrays will have this since the |
| 2410 | // outermost dimension size can be picked up from their runtime description. |
| 2411 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2412 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2413 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2414 | if (!PwAff) |
| 2415 | continue; |
| 2416 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2417 | isl::id Id = |
| 2418 | isl::manage(isl_pw_aff_get_dim_id(PwAff.get(), isl_dim_param, 0)); |
| 2419 | assert(!Id.is_null() && |
| 2420 | "Invalid Id for PwAff expression in Fortran array"); |
| 2421 | Id.dump(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2422 | OutermostSizeIds.push_back(Id); |
| 2423 | } |
| 2424 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2425 | return OutermostSizeIds; |
| 2426 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2427 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2428 | // The FORTRAN array size parameters are known to be non-negative. |
| 2429 | static isl_set *boundFortranArrayParams(__isl_give isl_set *Context, |
| 2430 | Scop::array_range Arrays) { |
| 2431 | std::vector<isl::id> OutermostSizeIds; |
| 2432 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2433 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2434 | for (isl::id Id : OutermostSizeIds) { |
| 2435 | int dim = isl_set_find_dim_by_id(Context, isl_dim_param, Id.get()); |
| 2436 | Context = isl_set_lower_bound_si(Context, isl_dim_param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | return Context; |
| 2440 | } |
| 2441 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2442 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2443 | if (PollyIgnoreParamBounds) |
| 2444 | return; |
| 2445 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2446 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2447 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Align the parameters of all data structures to the model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2450 | Context = isl_set_align_params(Context, Space.copy()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2451 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2452 | // Bound the size of the fortran array dimensions. |
| 2453 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2454 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2455 | // As all parameters are known add bounds to them. |
| 2456 | addParameterBounds(); |
| 2457 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2458 | for (ScopStmt &Stmt : *this) |
| 2459 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2460 | // Simplify the schedule according to the context too. |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 2461 | Schedule = isl_schedule_gist_domain_params(Schedule, getContext().release()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2464 | static __isl_give isl_set * |
| 2465 | simplifyAssumptionContext(__isl_take isl_set *AssumptionContext, |
| 2466 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2467 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2468 | // simplify the context with the constraints that are needed for anything to |
| 2469 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2470 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2471 | // domains, thus we cannot use the remaining domain to simplify the |
| 2472 | // assumptions. |
| 2473 | if (!S.hasErrorBlock()) { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2474 | isl_set *DomainParameters = isl_union_set_params(S.getDomains().release()); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2475 | AssumptionContext = |
| 2476 | isl_set_gist_params(AssumptionContext, DomainParameters); |
| 2477 | } |
| 2478 | |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2479 | AssumptionContext = |
| 2480 | isl_set_gist_params(AssumptionContext, S.getContext().release()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2481 | return AssumptionContext; |
| 2482 | } |
| 2483 | |
| 2484 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2485 | // The parameter constraints of the iteration domains give us a set of |
| 2486 | // constraints that need to hold for all cases where at least a single |
| 2487 | // statement iteration is executed in the whole scop. We now simplify the |
| 2488 | // assumed context under the assumption that such constraints hold and at |
| 2489 | // least a single statement iteration is executed. For cases where no |
| 2490 | // statement instances are executed, the assumptions we have taken about |
| 2491 | // the executed code do not matter and can be changed. |
| 2492 | // |
| 2493 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2494 | // the set of statement instances that are executed. Otherwise we |
| 2495 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2496 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2497 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2498 | // performed. In such a case, modifying the run-time conditions and |
| 2499 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2500 | // to not be executed. |
| 2501 | // |
| 2502 | // Example: |
| 2503 | // |
| 2504 | // When delinearizing the following code: |
| 2505 | // |
| 2506 | // for (long i = 0; i < 100; i++) |
| 2507 | // for (long j = 0; j < m; j++) |
| 2508 | // A[i+p][j] = 1.0; |
| 2509 | // |
| 2510 | // 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] | 2511 | // 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] | 2512 | // 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] | 2513 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2514 | InvalidContext = |
| 2515 | isl_set_align_params(InvalidContext, getParamSpace().release()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2518 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2519 | static isl::stat |
| 2520 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2521 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2522 | isl::pw_aff LastDimAff; |
| 2523 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2524 | unsigned Pos; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2525 | isl::ctx Ctx = Set.get_ctx(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2526 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2527 | Set = Set.remove_divs(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2528 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2529 | if (isl_set_n_basic_set(Set.get()) >= MaxDisjunctsInDomain) |
| 2530 | return isl::stat::error; |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2531 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2532 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2533 | // lexmax computation will take too long if this number is high. |
| 2534 | // |
| 2535 | // Experiments with a simple test case using an i7 4800MQ: |
| 2536 | // |
| 2537 | // #Parameters involved | Time (in sec) |
| 2538 | // 6 | 0.01 |
| 2539 | // 7 | 0.04 |
| 2540 | // 8 | 0.12 |
| 2541 | // 9 | 0.40 |
| 2542 | // 10 | 1.54 |
| 2543 | // 11 | 6.78 |
| 2544 | // 12 | 30.38 |
| 2545 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2546 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2547 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2548 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2549 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2550 | InvolvedParams++; |
| 2551 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2552 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2553 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 2556 | if (isl_set_n_basic_set(Set.get()) > RunTimeChecksMaxAccessDisjuncts) |
| 2557 | return isl::stat::error; |
| 2558 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2559 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2560 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2561 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2562 | if (isl_ctx_last_error(Ctx.get()) == isl_error_quota) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2563 | return isl::stat::error; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2564 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2565 | MinPMA = MinPMA.coalesce(); |
| 2566 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2567 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2568 | // Adjust the last dimension of the maximal access by one as we want to |
| 2569 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2570 | // we test during code generation might now point after the end of the |
| 2571 | // allocated array but we will never dereference it anyway. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2572 | assert(MaxPMA.dim(isl::dim::out) && "Assumed at least one output dimension"); |
| 2573 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2574 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2575 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2576 | OneAff = OneAff.add_constant_si(1); |
| 2577 | LastDimAff = LastDimAff.add(OneAff); |
| 2578 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2579 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2580 | MinMaxAccesses.push_back(std::make_pair(MinPMA.copy(), MaxPMA.copy())); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2581 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2582 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2585 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2586 | isl_set *Domain = MA->getStatement()->getDomain().release(); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2587 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2588 | return isl_set_reset_tuple_id(Domain); |
| 2589 | } |
| 2590 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2591 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2592 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2593 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2594 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2595 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2596 | isl::union_set Domains = S.getDomains(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2597 | isl::union_map Accesses = isl::union_map::empty(S.getParamSpace()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2598 | |
| 2599 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 2600 | Accesses = Accesses.add_map(give(MA->getAccessRelation().release())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2601 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2602 | Accesses = Accesses.intersect_domain(Domains); |
| 2603 | isl::union_set Locations = Accesses.range(); |
| 2604 | Locations = Locations.coalesce(); |
| 2605 | Locations = Locations.detect_equalities(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2606 | |
| 2607 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2608 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2609 | }; |
| 2610 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2613 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2614 | /// |
| 2615 | ///{ |
| 2616 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2617 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2618 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2619 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2620 | : RN->getNodeAs<BasicBlock>(); |
| 2621 | } |
| 2622 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2623 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2624 | static inline BasicBlock * |
| 2625 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2626 | if (RN->isSubRegion()) { |
| 2627 | assert(idx == 0); |
| 2628 | return RN->getNodeAs<Region>()->getExit(); |
| 2629 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2630 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2633 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2634 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2635 | if (!RN->isSubRegion()) { |
| 2636 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2637 | Loop *L = LI.getLoopFor(BB); |
| 2638 | |
| 2639 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2640 | // they are not part of an actual loop in the control flow graph. |
| 2641 | // Nevertheless, we handle certain unreachable statements that are common |
| 2642 | // when modeling run-time bounds checks as being part of the loop to be |
| 2643 | // able to model them and to later eliminate the run-time bounds checks. |
| 2644 | // |
| 2645 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2646 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2647 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2648 | // allows us to model the following code. |
| 2649 | // |
| 2650 | // for (i = 0; i < N; i++) { |
| 2651 | // if (i > 1024) |
| 2652 | // abort(); <- this abort might be translated to an |
| 2653 | // unreachable |
| 2654 | // |
| 2655 | // A[i] = ... |
| 2656 | // } |
| 2657 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2658 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2659 | return L; |
| 2660 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2661 | |
| 2662 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2663 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2664 | while (L && NonAffineSubRegion->contains(L)) |
| 2665 | L = L->getParentLoop(); |
| 2666 | return L; |
| 2667 | } |
| 2668 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2669 | /// Get the number of blocks in @p L. |
| 2670 | /// |
| 2671 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2672 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2673 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2674 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2675 | /// scop and represent run-time conditions which we want to model and |
| 2676 | /// subsequently speculate away. |
| 2677 | /// |
| 2678 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2679 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2680 | unsigned NumBlocks = L->getNumBlocks(); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 2681 | SmallVector<BasicBlock *, 4> ExitBlocks; |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2682 | L->getExitBlocks(ExitBlocks); |
| 2683 | |
| 2684 | for (auto ExitBlock : ExitBlocks) { |
| 2685 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2686 | NumBlocks++; |
| 2687 | } |
| 2688 | return NumBlocks; |
| 2689 | } |
| 2690 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2691 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2692 | if (!RN->isSubRegion()) |
| 2693 | return 1; |
| 2694 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2695 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2696 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2699 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2700 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2701 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2702 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2703 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2704 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2705 | return true; |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2709 | ///} |
| 2710 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2711 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 2712 | unsigned Dim, Loop *L) { |
Michael Kruse | 88a2256 | 2016-03-29 07:50:52 +0000 | [diff] [blame] | 2713 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2714 | isl_id *DimId = |
| 2715 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 2716 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 2717 | } |
| 2718 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2719 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2720 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2723 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2724 | auto DIt = DomainMap.find(BB); |
| 2725 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2726 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2727 | |
| 2728 | auto &RI = *R.getRegionInfo(); |
| 2729 | auto *BBR = RI.getRegionFor(BB); |
| 2730 | while (BBR->getEntry() == BB) |
| 2731 | BBR = BBR->getParent(); |
| 2732 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2735 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2736 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2737 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2738 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2739 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2740 | int LD = getRelativeLoopDepth(L); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2741 | 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] | 2742 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2743 | while (LD-- >= 0) { |
| 2744 | S = addDomainDimId(S, LD + 1, L); |
| 2745 | L = L->getParentLoop(); |
| 2746 | } |
| 2747 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2748 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2749 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2750 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2751 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2752 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2753 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2754 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2755 | return false; |
| 2756 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2757 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2758 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2761 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2762 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2763 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2764 | // will cause problems when building up a ScopStmt for them. |
| 2765 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2766 | // 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] | 2767 | // can themselves not be constructed properly. To this end we will replace |
| 2768 | // the domains of error blocks and those only reachable via error blocks |
| 2769 | // 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] | 2770 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2771 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2772 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2773 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2774 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2775 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2778 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2779 | /// to be compatible to domains constructed for loop @p NewL. |
| 2780 | /// |
| 2781 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2782 | /// edge from @p OldL to @p NewL. |
| 2783 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2784 | __isl_take isl_set *Dom, |
| 2785 | Loop *OldL, Loop *NewL) { |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2786 | // If the loops are the same there is nothing to do. |
| 2787 | if (NewL == OldL) |
| 2788 | return Dom; |
| 2789 | |
| 2790 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2791 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2792 | // If both loops are non-affine loops there is nothing to do. |
| 2793 | if (OldDepth == -1 && NewDepth == -1) |
| 2794 | return Dom; |
| 2795 | |
| 2796 | // Distinguish three cases: |
| 2797 | // 1) The depth is the same but the loops are not. |
| 2798 | // => One loop was left one was entered. |
| 2799 | // 2) The depth increased from OldL to NewL. |
| 2800 | // => One loop was entered, none was left. |
| 2801 | // 3) The depth decreased from OldL to NewL. |
| 2802 | // => Loops were left were difference of the depths defines how many. |
| 2803 | if (OldDepth == NewDepth) { |
| 2804 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2805 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2806 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2807 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2808 | } else if (OldDepth < NewDepth) { |
| 2809 | assert(OldDepth + 1 == NewDepth); |
| 2810 | auto &R = S.getRegion(); |
| 2811 | (void)R; |
| 2812 | assert(NewL->getParentLoop() == OldL || |
| 2813 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2814 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2815 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2816 | } else { |
| 2817 | assert(OldDepth > NewDepth); |
| 2818 | int Diff = OldDepth - NewDepth; |
| 2819 | int NumDim = isl_set_n_dim(Dom); |
| 2820 | assert(NumDim >= Diff); |
| 2821 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2822 | } |
| 2823 | |
| 2824 | return Dom; |
| 2825 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2826 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2827 | bool Scop::propagateInvalidStmtDomains( |
| 2828 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2829 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2830 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2831 | for (auto *RN : RTraversal) { |
| 2832 | |
| 2833 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2834 | // subregions. |
| 2835 | if (RN->isSubRegion()) { |
| 2836 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2837 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2838 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2839 | continue; |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2844 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2845 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2846 | assert(Domain && "Cannot propagate a nullptr"); |
| 2847 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2848 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2849 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2850 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2851 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2852 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2853 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2854 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2855 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2856 | isl::set DomPar = Domain.params(); |
| 2857 | recordAssumption(ERRORBLOCK, DomPar.release(), |
| 2858 | BB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2859 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2862 | if (InvalidDomain.is_empty()) { |
| 2863 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2864 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2867 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2868 | auto *TI = BB->getTerminator(); |
| 2869 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2870 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2871 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2872 | |
| 2873 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2874 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2875 | continue; |
| 2876 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2877 | // Skip backedges. |
| 2878 | if (DT.dominates(SuccBB, BB)) |
| 2879 | continue; |
| 2880 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2881 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2882 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2883 | auto *AdjustedInvalidDomain = adjustDomainDimensions( |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2884 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2885 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2886 | auto *SuccInvalidDomain = InvalidDomainMap[SuccBB].copy(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2887 | SuccInvalidDomain = |
| 2888 | isl_set_union(SuccInvalidDomain, AdjustedInvalidDomain); |
| 2889 | SuccInvalidDomain = isl_set_coalesce(SuccInvalidDomain); |
| 2890 | unsigned NumConjucts = isl_set_n_basic_set(SuccInvalidDomain); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2891 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2892 | InvalidDomainMap[SuccBB] = isl::manage(SuccInvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2893 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2894 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2895 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2896 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2897 | continue; |
| 2898 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2899 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2900 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2901 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2902 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2903 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2904 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2905 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2906 | |
| 2907 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2910 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2911 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2912 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2913 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2914 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2915 | // domain to the exit block of the region. Otherwise we are done. |
| 2916 | auto *RI = R.getRegionInfo(); |
| 2917 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2918 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2919 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2920 | return; |
| 2921 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2922 | // 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] | 2923 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2924 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2925 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2926 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2927 | BBLoop->getLoopLatches(LatchBBs); |
| 2928 | for (auto *LatchBB : LatchBBs) |
| 2929 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2930 | return; |
| 2931 | L = L->getParentLoop(); |
| 2932 | } |
| 2933 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2934 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2935 | assert(Domain && "Cannot propagate a nullptr"); |
| 2936 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2937 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2938 | |
| 2939 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2940 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2941 | isl::set AdjustedDomain = isl::manage( |
| 2942 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2943 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2944 | |
| 2945 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2946 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2947 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2948 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2949 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2950 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2951 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2952 | FinishedExitBlocks.insert(ExitBB); |
| 2953 | } |
| 2954 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2955 | bool Scop::buildDomainsWithBranchConstraints( |
| 2956 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2957 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2958 | // To create the domain for each block in R we iterate over all blocks and |
| 2959 | // subregions in R and propagate the conditions under which the current region |
| 2960 | // element is executed. To this end we iterate in reverse post order over R as |
| 2961 | // it ensures that we first visit all predecessors of a region node (either a |
| 2962 | // basic block or a subregion) before we visit the region node itself. |
| 2963 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2964 | // there we propagate the current domain to all successors, however we add the |
| 2965 | // condition that the successor is actually executed next. |
| 2966 | // As we are only interested in non-loop carried constraints here we can |
| 2967 | // simply skip loop back edges. |
| 2968 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2969 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2970 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2971 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2972 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2973 | // subregions. |
| 2974 | if (RN->isSubRegion()) { |
| 2975 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2976 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2977 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2978 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2979 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2980 | continue; |
| 2981 | } |
| 2982 | } |
| 2983 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2984 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2985 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2986 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2987 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2988 | TerminatorInst *TI = BB->getTerminator(); |
| 2989 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2990 | if (isa<UnreachableInst>(TI)) |
| 2991 | continue; |
| 2992 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2993 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2994 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2995 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2996 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2997 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2998 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2999 | // Propagate the domain from BB directly to blocks that have a superset |
| 3000 | // 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] | 3001 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 3002 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3003 | |
| 3004 | // If all successors of BB have been set a domain through the propagation |
| 3005 | // above we do not need to build condition sets but can just skip this |
| 3006 | // block. However, it is important to note that this is a local property |
| 3007 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 3008 | // local variable. |
| 3009 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 3010 | return FinishedExitBlocks.count(SuccBB); |
| 3011 | }; |
| 3012 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 3013 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3014 | |
| 3015 | // Build the condition sets for the successor nodes of the current region |
| 3016 | // node. If it is a non-affine subregion we will always execute the single |
| 3017 | // exit node, hence the single entry node domain is the condition set. For |
| 3018 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3019 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3020 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3021 | ConditionSets.push_back(Domain.copy()); |
| 3022 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3023 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3024 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3025 | |
| 3026 | // Now iterate over the successors and set their initial domain based on |
| 3027 | // their condition set. We skip back edges here and have to be careful when |
| 3028 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 3029 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3030 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3031 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3032 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3033 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3034 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3035 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3036 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3037 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 3038 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3039 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 3040 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3041 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3042 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3043 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3044 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3045 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3046 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3047 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3048 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 3049 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3050 | CondSet = isl::manage( |
| 3051 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3052 | |
| 3053 | // Set the domain for the successor or merge it with an existing domain in |
| 3054 | // case there are multiple paths (without loop back edges) to the |
| 3055 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3056 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 3057 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3058 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3059 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3060 | } else { |
| 3061 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3062 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 3063 | SuccDomain = CondSet; |
| 3064 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3065 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3066 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 3067 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 3068 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3069 | // In case this happens we will clean up and bail. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3070 | if (isl_set_n_basic_set(SuccDomain.get()) < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3071 | continue; |
| 3072 | |
| 3073 | invalidate(COMPLEXITY, DebugLoc()); |
| 3074 | while (++u < ConditionSets.size()) |
| 3075 | isl_set_free(ConditionSets[u]); |
| 3076 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3077 | } |
| 3078 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 3079 | |
| 3080 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3083 | isl::set Scop::getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain, |
| 3084 | DominatorTree &DT, |
| 3085 | LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3086 | // If @p BB is the ScopEntry we are done |
| 3087 | if (R.getEntry() == BB) |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3088 | return isl::set::universe(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3089 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3090 | // The region info of this function. |
| 3091 | auto &RI = *R.getRegionInfo(); |
| 3092 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3093 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3094 | |
| 3095 | // A domain to collect all predecessor domains, thus all conditions under |
| 3096 | // which the block is executed. To this end we start with the empty domain. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3097 | isl::set PredDom = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Set of regions of which the entry block domain has been propagated to BB. |
| 3100 | // all predecessors inside any of the regions can be skipped. |
| 3101 | SmallSet<Region *, 8> PropagatedRegions; |
| 3102 | |
| 3103 | for (auto *PredBB : predecessors(BB)) { |
| 3104 | // Skip backedges. |
| 3105 | if (DT.dominates(BB, PredBB)) |
| 3106 | continue; |
| 3107 | |
| 3108 | // If the predecessor is in a region we used for propagation we can skip it. |
| 3109 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 3110 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 3111 | PredBBInRegion)) { |
| 3112 | continue; |
| 3113 | } |
| 3114 | |
| 3115 | // Check if there is a valid region we can use for propagation, thus look |
| 3116 | // for a region that contains the predecessor and has @p BB as exit block. |
| 3117 | auto *PredR = RI.getRegionFor(PredBB); |
| 3118 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 3119 | PredR->getParent(); |
| 3120 | |
| 3121 | // If a valid region for propagation was found use the entry of that region |
| 3122 | // for propagation, otherwise the PredBB directly. |
| 3123 | if (PredR->getExit() == BB) { |
| 3124 | PredBB = PredR->getEntry(); |
| 3125 | PropagatedRegions.insert(PredR); |
| 3126 | } |
| 3127 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 3128 | auto *PredBBDom = getDomainConditions(PredBB).release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3129 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 3130 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3131 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 3132 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3133 | PredDom = PredDom.unite(isl::manage(PredBBDom)); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | return PredDom; |
| 3137 | } |
| 3138 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3139 | bool Scop::propagateDomainConstraints( |
| 3140 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3141 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3142 | // Iterate over the region R and propagate the domain constrains from the |
| 3143 | // predecessors to the current node. In contrast to the |
| 3144 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 3145 | // information from the predecessors instead of pushing it to the successors. |
| 3146 | // Additionally, we assume the domains to be already present in the domain |
| 3147 | // map here. However, we iterate again in reverse post order so we know all |
| 3148 | // predecessors have been visited before a block or non-affine subregion is |
| 3149 | // visited. |
| 3150 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3151 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3152 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3153 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 3154 | // subregions. |
| 3155 | if (RN->isSubRegion()) { |
| 3156 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3157 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3158 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3159 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3160 | continue; |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3165 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 3166 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3167 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3168 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 3169 | isl::set PredDom = getPredecessorDomainConstraints(BB, Domain, DT, LI); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3170 | Domain = Domain.intersect(PredDom).coalesce(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 3171 | Domain = Domain.align_params(getParamSpace()); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 3172 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 3173 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3174 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3175 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3176 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3177 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3178 | |
| 3179 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3182 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 3183 | /// |
| 3184 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 3185 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3186 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3187 | /// |
| 3188 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3189 | static __isl_give isl_map * |
| 3190 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 3191 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 3192 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
Tobias Grosser | f4fe34b | 2017-03-16 21:33:20 +0000 | [diff] [blame] | 3193 | 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] | 3194 | if (u != Dim) |
| 3195 | NextIterationMap = |
| 3196 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 3197 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 3198 | C = isl_constraint_set_constant_si(C, 1); |
| 3199 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 3200 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 3201 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 3202 | return NextIterationMap; |
| 3203 | } |
| 3204 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3205 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3206 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3207 | int LoopDepth = getRelativeLoopDepth(L); |
| 3208 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3209 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3210 | BasicBlock *HeaderBB = L->getHeader(); |
| 3211 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3212 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3213 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3214 | isl::map NextIterationMap = isl::manage( |
| 3215 | createNextIterationMap(HeaderBBDom.get_space().release(), LoopDepth)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3216 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3217 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3218 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 3219 | SmallVector<BasicBlock *, 4> LatchBlocks; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3220 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3221 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3222 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3223 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3224 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3225 | if (!LatchBBDom) |
| 3226 | continue; |
| 3227 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3228 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3229 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3230 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 3231 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 3232 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 3233 | |
| 3234 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3235 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3236 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3237 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3238 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3239 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 3240 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3241 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3242 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3243 | // Free the non back edge condition set as we do not need it. |
| 3244 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3245 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3246 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3247 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3248 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3249 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3250 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3251 | BackedgeCondition = BackedgeCondition.project_out( |
| 3252 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3253 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3254 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3255 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3256 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3257 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3258 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3259 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3260 | isl::set UnionBackedgeConditionComplement = |
| 3261 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3262 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3263 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3264 | 0); |
| 3265 | UnionBackedgeConditionComplement = |
| 3266 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3267 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3268 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3269 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3270 | auto Parts = partitionSetParts(HeaderBBDom.copy(), LoopDepth); |
| 3271 | HeaderBBDom = isl::manage(Parts.second); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3272 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3273 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3274 | // the bounded assumptions to the context as they are already implied by the |
| 3275 | // <nsw> tag. |
| 3276 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 3277 | isl_set_free(Parts.first); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3278 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3281 | isl_set *UnboundedCtx = isl_set_params(Parts.first); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3282 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3283 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3284 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3287 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3288 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3289 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3290 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3291 | if (!PointerBaseInst) |
| 3292 | return nullptr; |
| 3293 | |
| 3294 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3295 | if (!BasePtrStmt) |
| 3296 | return nullptr; |
| 3297 | |
| 3298 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3299 | } |
| 3300 | |
| 3301 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3302 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3303 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3304 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3305 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3306 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3307 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3308 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3309 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3310 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3311 | |
| 3312 | return false; |
| 3313 | } |
| 3314 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3315 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3316 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3317 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3318 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3319 | if (buildAliasGroups(AA)) { |
| 3320 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3321 | // collect statistics so we do it here explicitly. |
| 3322 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3323 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3324 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3325 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3326 | |
| 3327 | // If a problem occurs while building the alias groups we need to delete |
| 3328 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3329 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3330 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3331 | |
| 3332 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3333 | << " could not be created as the number of parameters involved " |
| 3334 | "is too high. The SCoP will be " |
| 3335 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3336 | "the maximal number of parameters but be advised that the " |
| 3337 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3338 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3341 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3342 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3343 | AliasSetTracker AST(AA); |
| 3344 | |
| 3345 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3346 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3347 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3348 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3349 | isl_set *StmtDomain = Stmt.getDomain().release(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3350 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3351 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3352 | |
| 3353 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3354 | if (StmtDomainEmpty) |
| 3355 | continue; |
| 3356 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3357 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3358 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3359 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3360 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3361 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3362 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3363 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3364 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3365 | else |
| 3366 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3367 | AST.add(Acc); |
| 3368 | } |
| 3369 | } |
| 3370 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3371 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3372 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3373 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3374 | continue; |
| 3375 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3376 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3377 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3378 | if (AG.size() < 2) |
| 3379 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3380 | AliasGroups.push_back(std::move(AG)); |
| 3381 | } |
| 3382 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3383 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3384 | } |
| 3385 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3386 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3387 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3388 | AliasGroupTy NewAG; |
| 3389 | AliasGroupTy &AG = AliasGroups[u]; |
| 3390 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3391 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3392 | while (AGI != AG.end()) { |
| 3393 | MemoryAccess *MA = *AGI; |
| 3394 | isl_set *MADomain = getAccessDomain(MA); |
| 3395 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3396 | NewAG.push_back(MA); |
| 3397 | AGI = AG.erase(AGI); |
| 3398 | isl_set_free(MADomain); |
| 3399 | } else { |
| 3400 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3401 | AGI++; |
| 3402 | } |
| 3403 | } |
| 3404 | if (NewAG.size() > 1) |
| 3405 | AliasGroups.push_back(std::move(NewAG)); |
| 3406 | isl_set_free(AGDomain); |
| 3407 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3411 | // To create sound alias checks we perform the following steps: |
| 3412 | // o) We partition each group into read only and non read only accesses. |
| 3413 | // o) For each group with more than one base pointer we then compute minimal |
| 3414 | // and maximal accesses to each array of a group in read only and non |
| 3415 | // read only partitions separately. |
| 3416 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3417 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3418 | |
| 3419 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3420 | |
| 3421 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3422 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3423 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3424 | if (!hasFeasibleRuntimeContext()) |
| 3425 | return false; |
| 3426 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3427 | { |
| 3428 | IslMaxOperationsGuard MaxOpGuard(getIslCtx(), OptComputeOut); |
| 3429 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3430 | if (!Valid) |
| 3431 | return false; |
| 3432 | } |
| 3433 | if (isl_ctx_last_error(getIslCtx()) == isl_error_quota) { |
| 3434 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3435 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3436 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3437 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3438 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3439 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3442 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3443 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3444 | AliasGroupTy ReadOnlyAccesses; |
| 3445 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3446 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3447 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3448 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3449 | if (AliasGroup.size() < 2) |
| 3450 | return true; |
| 3451 | |
| 3452 | for (MemoryAccess *Access : AliasGroup) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3453 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias", |
| 3454 | Access->getAccessInstruction()) |
| 3455 | << "Possibly aliasing pointer, use restrict keyword."); |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3456 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3457 | if (HasWriteAccess.count(Array)) { |
| 3458 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3459 | ReadWriteAccesses.push_back(Access); |
| 3460 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3461 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3462 | ReadOnlyAccesses.push_back(Access); |
| 3463 | } |
| 3464 | } |
| 3465 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3466 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3467 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3468 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3469 | return true; |
| 3470 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3471 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3472 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3473 | return true; |
| 3474 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3475 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3476 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3477 | for (MemoryAccess *MA : AliasGroup) { |
| 3478 | if (!MA->isAffine()) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3479 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(), |
| 3480 | MA->getAccessInstruction()->getParent()); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3481 | return false; |
| 3482 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3486 | // their base pointers are available. |
| 3487 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3488 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3489 | addRequiredInvariantLoad( |
| 3490 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3491 | } |
| 3492 | |
| 3493 | MinMaxAliasGroups.emplace_back(); |
| 3494 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3495 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3496 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3497 | |
| 3498 | bool Valid; |
| 3499 | |
| 3500 | Valid = |
| 3501 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3502 | |
| 3503 | if (!Valid) |
| 3504 | return false; |
| 3505 | |
| 3506 | // Bail out if the number of values we need to compare is too large. |
| 3507 | // This is important as the number of comparisons grows quadratically with |
| 3508 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3509 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3510 | RunTimeChecksMaxArraysPerGroup) |
| 3511 | return false; |
| 3512 | |
| 3513 | Valid = |
| 3514 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3515 | |
| 3516 | if (!Valid) |
| 3517 | return false; |
| 3518 | |
| 3519 | return true; |
| 3520 | } |
| 3521 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3522 | /// 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] | 3523 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3524 | // Start with the smallest loop containing the entry and expand that |
| 3525 | // loop until it contains all blocks in the region. If there is a loop |
| 3526 | // containing all blocks in the region check if it is itself contained |
| 3527 | // and if so take the parent loop as it will be the smallest containing |
| 3528 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3529 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3530 | while (L) { |
| 3531 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3532 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3533 | AllContained &= L->contains(BB); |
| 3534 | if (AllContained) |
| 3535 | break; |
| 3536 | L = L->getParentLoop(); |
| 3537 | } |
| 3538 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3539 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3542 | int Scop::NextScopID = 0; |
| 3543 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 3544 | std::string Scop::CurrentFunc; |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3545 | |
| 3546 | int Scop::getNextID(std::string ParentFunc) { |
| 3547 | if (ParentFunc != CurrentFunc) { |
| 3548 | CurrentFunc = ParentFunc; |
| 3549 | NextScopID = 0; |
| 3550 | } |
| 3551 | return NextScopID++; |
| 3552 | } |
| 3553 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3554 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3555 | ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 3556 | : SE(&ScalarEvolution), R(R), name(R.getNameStr()), |
| 3557 | HasSingleExitEdge(R.getExitingBlock()), DC(DC), ORE(ORE), |
| 3558 | IslCtx(isl_ctx_alloc(), isl_ctx_free), Affinator(this, LI), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3559 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3560 | if (IslOnErrorAbort) |
| 3561 | isl_options_set_on_error(getIslCtx(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3562 | buildContext(); |
| 3563 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3564 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 3565 | Scop::~Scop() { |
| 3566 | isl_set_free(Context); |
| 3567 | isl_set_free(AssumedContext); |
| 3568 | isl_set_free(InvalidContext); |
| 3569 | isl_schedule_free(Schedule); |
| 3570 | |
| 3571 | ParameterIds.clear(); |
| 3572 | |
| 3573 | for (auto &AS : RecordedAssumptions) |
| 3574 | isl_set_free(AS.Set); |
| 3575 | |
| 3576 | // Free the alias groups |
| 3577 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
| 3578 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
| 3579 | isl_pw_multi_aff_free(MMA.first); |
| 3580 | isl_pw_multi_aff_free(MMA.second); |
| 3581 | } |
| 3582 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
| 3583 | isl_pw_multi_aff_free(MMA.first); |
| 3584 | isl_pw_multi_aff_free(MMA.second); |
| 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | for (const auto &IAClass : InvariantEquivClasses) |
| 3589 | isl_set_free(IAClass.ExecutionContext); |
| 3590 | |
| 3591 | // Explicitly release all Scop objects and the underlying isl objects before |
| 3592 | // we release the isl context. |
| 3593 | Stmts.clear(); |
| 3594 | ScopArrayInfoSet.clear(); |
| 3595 | ScopArrayInfoMap.clear(); |
| 3596 | ScopArrayNameMap.clear(); |
| 3597 | AccessFunctions.clear(); |
| 3598 | } |
| 3599 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3600 | void Scop::foldSizeConstantsToRight() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3601 | isl_union_set *Accessed = isl_union_map_range(getAccesses().release()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3602 | |
| 3603 | for (auto Array : arrays()) { |
| 3604 | if (Array->getNumberOfDimensions() <= 1) |
| 3605 | continue; |
| 3606 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3607 | isl_space *Space = Array->getSpace().release(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3608 | |
| 3609 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3610 | |
| 3611 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3612 | isl_space_free(Space); |
| 3613 | continue; |
| 3614 | } |
| 3615 | |
| 3616 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3617 | |
| 3618 | isl_map *Transform = |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3619 | isl_map_universe(isl_space_map_from_set(Array->getSpace().release())); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3620 | |
| 3621 | std::vector<int> Int; |
| 3622 | |
| 3623 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3624 | for (int i = 0; i < Dims; i++) { |
| 3625 | isl_set *DimOnly = |
| 3626 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3627 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3628 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3629 | |
| 3630 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3631 | |
| 3632 | if (i == Dims - 1) { |
| 3633 | Int.push_back(1); |
| 3634 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3635 | isl_basic_set_free(DimHull); |
| 3636 | continue; |
| 3637 | } |
| 3638 | |
| 3639 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3640 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3641 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3642 | isl_aff_free(Diff); |
| 3643 | |
| 3644 | int ValInt = 1; |
| 3645 | |
| 3646 | if (isl_val_is_int(Val)) |
| 3647 | ValInt = isl_val_get_num_si(Val); |
| 3648 | isl_val_free(Val); |
| 3649 | |
| 3650 | Int.push_back(ValInt); |
| 3651 | |
| 3652 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3653 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3654 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3655 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3656 | Transform = isl_map_add_constraint(Transform, C); |
| 3657 | isl_basic_set_free(DimHull); |
| 3658 | continue; |
| 3659 | } |
| 3660 | |
| 3661 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3662 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3663 | |
| 3664 | int ValInt = 1; |
| 3665 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3666 | ValInt = 0; |
| 3667 | } |
| 3668 | |
| 3669 | Int.push_back(ValInt); |
| 3670 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3671 | isl_basic_set_free(DimHull); |
| 3672 | isl_basic_set_free(ZeroSet); |
| 3673 | } |
| 3674 | |
| 3675 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3676 | |
| 3677 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3678 | isl_set_free(Elements); |
| 3679 | isl_set_free(MappedElements); |
| 3680 | isl_map_free(Transform); |
| 3681 | continue; |
| 3682 | } |
| 3683 | |
| 3684 | isl_set_free(MappedElements); |
| 3685 | |
| 3686 | bool CanFold = true; |
| 3687 | |
| 3688 | if (Int[0] <= 1) |
| 3689 | CanFold = false; |
| 3690 | |
| 3691 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3692 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3693 | if (Int[0] != Int[i] && Int[i]) |
| 3694 | CanFold = false; |
| 3695 | |
| 3696 | if (!CanFold) { |
| 3697 | isl_set_free(Elements); |
| 3698 | isl_map_free(Transform); |
| 3699 | continue; |
| 3700 | } |
| 3701 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3702 | for (auto &Access : AccessFunctions) |
| 3703 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3704 | Access->setAccessRelation(Access->getAccessRelation().apply_range( |
| 3705 | isl::manage(isl_map_copy(Transform)))); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3706 | |
| 3707 | isl_map_free(Transform); |
| 3708 | |
| 3709 | std::vector<const SCEV *> Sizes; |
| 3710 | for (unsigned i = 0; i < NumDims; i++) { |
| 3711 | auto Size = Array->getDimensionSize(i); |
| 3712 | |
| 3713 | if (i == NumDims - 1) |
| 3714 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3715 | Sizes.push_back(Size); |
| 3716 | } |
| 3717 | |
| 3718 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3719 | |
| 3720 | isl_set_free(Elements); |
| 3721 | } |
| 3722 | isl_union_set_free(Accessed); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3725 | void Scop::markFortranArrays() { |
| 3726 | for (ScopStmt &Stmt : Stmts) { |
| 3727 | for (MemoryAccess *MemAcc : Stmt) { |
| 3728 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3729 | if (!FAD) |
| 3730 | continue; |
| 3731 | |
| 3732 | // TODO: const_cast-ing to edit |
| 3733 | ScopArrayInfo *SAI = |
| 3734 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3735 | assert(SAI && "memory access into a Fortran array does not " |
| 3736 | "have an associated ScopArrayInfo"); |
| 3737 | SAI->applyAndSetFAD(FAD); |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3742 | void Scop::finalizeAccesses() { |
| 3743 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3744 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3745 | foldAccessRelations(); |
| 3746 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3747 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3750 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3751 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3752 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3753 | for (ScopStmt &Stmt : *this) |
| 3754 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3755 | if (!Access->isArrayKind()) |
| 3756 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3757 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3758 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3759 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3760 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3761 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3762 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3763 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3764 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3765 | DivisibleSize /= 2; |
| 3766 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3767 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3770 | for (auto &Stmt : *this) |
| 3771 | for (auto &Access : Stmt) |
| 3772 | Access->updateDimensionality(); |
| 3773 | } |
| 3774 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3775 | void Scop::foldAccessRelations() { |
| 3776 | for (auto &Stmt : *this) |
| 3777 | for (auto &Access : Stmt) |
| 3778 | Access->foldAccessRelation(); |
| 3779 | } |
| 3780 | |
| 3781 | void Scop::assumeNoOutOfBounds() { |
| 3782 | for (auto &Stmt : *this) |
| 3783 | for (auto &Access : Stmt) |
| 3784 | Access->assumeNoOutOfBound(); |
| 3785 | } |
| 3786 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3787 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
| 3788 | if (Stmt.isRegionStmt()) |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3789 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3790 | StmtMap.erase(BB); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3791 | for (Instruction &Inst : *BB) |
| 3792 | InstStmtMap.erase(&Inst); |
| 3793 | } |
| 3794 | else { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3795 | StmtMap.erase(Stmt.getBasicBlock()); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3796 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3797 | InstStmtMap.erase(Inst); |
| 3798 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3799 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3800 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3801 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete) { |
| 3802 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3803 | if (!ShouldDelete(*StmtIt)) { |
| 3804 | StmtIt++; |
| 3805 | continue; |
| 3806 | } |
| 3807 | |
| 3808 | removeFromStmtMap(*StmtIt); |
| 3809 | StmtIt = Stmts.erase(StmtIt); |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | void Scop::removeStmtNotInDomainMap() { |
| 3814 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Tobias Grosser | 199ec4a | 2017-07-19 16:31:10 +0000 | [diff] [blame] | 3815 | return !this->DomainMap.lookup(Stmt.getEntryBlock()); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3816 | }; |
| 3817 | removeStmts(ShouldDelete); |
| 3818 | } |
| 3819 | |
| 3820 | void Scop::simplifySCoP(bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3821 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3822 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3823 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3824 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3825 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3826 | bool OnlyRead = true; |
| 3827 | for (MemoryAccess *MA : Stmt) { |
| 3828 | if (MA->isRead()) |
| 3829 | continue; |
| 3830 | |
| 3831 | OnlyRead = false; |
| 3832 | break; |
| 3833 | } |
| 3834 | |
| 3835 | RemoveStmt = OnlyRead; |
| 3836 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3837 | return RemoveStmt; |
| 3838 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3839 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3840 | removeStmts(ShouldDelete); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3843 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3844 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3845 | if (!LInst) |
| 3846 | return nullptr; |
| 3847 | |
| 3848 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3849 | LInst = cast<LoadInst>(Rep); |
| 3850 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3851 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3852 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3853 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3854 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3855 | continue; |
| 3856 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3857 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3858 | for (auto *MA : MAs) |
| 3859 | if (MA->getAccessInstruction() == Val) |
| 3860 | return &IAClass; |
| 3861 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3862 | |
| 3863 | return nullptr; |
| 3864 | } |
| 3865 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3866 | bool isAParameter(llvm::Value *maybeParam, const Function &F) { |
| 3867 | for (const llvm::Argument &Arg : F.args()) |
| 3868 | if (&Arg == maybeParam) |
| 3869 | return true; |
| 3870 | |
| 3871 | return false; |
| 3872 | }; |
| 3873 | |
Tobias Grosser | 305d316 | 2017-08-07 00:10:11 +0000 | [diff] [blame] | 3874 | bool Scop::canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
| 3875 | bool MAInvalidCtxIsEmpty, |
| 3876 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3877 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3878 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3879 | if (PollyAllowDereferenceOfAllFunctionParams && |
| 3880 | isAParameter(LInst->getPointerOperand(), getFunction())) |
| 3881 | return true; |
| 3882 | |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3883 | // TODO: We can provide more information for better but more expensive |
| 3884 | // results. |
| 3885 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3886 | LInst->getAlignment(), DL)) |
| 3887 | return false; |
| 3888 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3889 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3890 | // |
Siddharth Bhat | 83fe6b5 | 2017-08-08 12:26:32 +0000 | [diff] [blame] | 3891 | // TODO: This is probably too conservative. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3892 | if (!NonHoistableCtxIsEmpty) |
| 3893 | return false; |
| 3894 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3895 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3896 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3897 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3898 | return true; |
| 3899 | |
| 3900 | // 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] | 3901 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3902 | // statement domain. |
| 3903 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3904 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3905 | return false; |
| 3906 | return true; |
| 3907 | } |
| 3908 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3909 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3910 | if (InvMAs.empty()) |
| 3911 | return; |
| 3912 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 3913 | isl::set StmtInvalidCtx = Stmt.getInvalidContext(); |
| 3914 | bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.is_empty(); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3915 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3916 | // Get the context under which the statement is executed but remove the error |
| 3917 | // context under which this statement is reached. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3918 | isl::set DomainCtx = Stmt.getDomain().params(); |
| 3919 | DomainCtx = DomainCtx.subtract(StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3920 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3921 | if (isl_set_n_basic_set(DomainCtx.get()) >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3922 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3923 | invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3924 | return; |
| 3925 | } |
| 3926 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3927 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3928 | // we could have cyclic dependences on the constraints under which the |
| 3929 | // hoisted loads are executed and we could not determine an order in which to |
| 3930 | // pre-load them. This happens because not only lower bounds are part of the |
| 3931 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3932 | for (auto &InvMA : InvMAs) { |
| 3933 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3934 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3935 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3936 | SetVector<Value *> Values; |
| 3937 | for (const SCEV *Parameter : Parameters) { |
| 3938 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3939 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3940 | if (!Values.count(AccInst)) |
| 3941 | continue; |
| 3942 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3943 | if (isl::id ParamId = getIdForParam(Parameter)) { |
| 3944 | int Dim = DomainCtx.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | b58ed8d | 2017-03-17 09:02:53 +0000 | [diff] [blame] | 3945 | if (Dim >= 0) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3946 | DomainCtx = DomainCtx.eliminate(isl::dim::param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3947 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3948 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3949 | } |
| 3950 | } |
| 3951 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3952 | for (auto &InvMA : InvMAs) { |
| 3953 | auto *MA = InvMA.MA; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3954 | isl::set NHCtx = InvMA.NonHoistableCtx; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3955 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3956 | // Check for another invariant access that accesses the same location as |
| 3957 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3958 | // class at the end of InvariantEquivClasses. |
| 3959 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3960 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3961 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3962 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3963 | isl::set MAInvalidCtx = MA->getInvalidContext(); |
| 3964 | bool NonHoistableCtxIsEmpty = NHCtx.is_empty(); |
| 3965 | bool MAInvalidCtxIsEmpty = MAInvalidCtx.is_empty(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3966 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3967 | isl::set MACtx; |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3968 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3969 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3970 | NonHoistableCtxIsEmpty)) { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3971 | MACtx = isl::set::universe(DomainCtx.get_space()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3972 | } else { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3973 | MACtx = DomainCtx; |
| 3974 | MACtx = MACtx.subtract(MAInvalidCtx.unite(NHCtx)); |
| 3975 | MACtx = MACtx.gist_params(getContext()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3978 | bool Consolidated = false; |
| 3979 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3980 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3981 | continue; |
| 3982 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3983 | // If the pointer and the type is equal check if the access function wrt. |
| 3984 | // to the domain is equal too. It can happen that the domain fixes |
| 3985 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3986 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3987 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3988 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3989 | if (!MAs.empty()) { |
| 3990 | auto *LastMA = MAs.front(); |
| 3991 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3992 | isl::set AR = MA->getAccessRelation().range(); |
| 3993 | isl::set LastAR = LastMA->getAccessRelation().range(); |
| 3994 | bool SameAR = AR.is_equal(LastAR); |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3995 | |
| 3996 | if (!SameAR) |
| 3997 | continue; |
| 3998 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3999 | |
| 4000 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4001 | MAs.push_front(MA); |
| 4002 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 4003 | Consolidated = true; |
| 4004 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4005 | // Unify the execution context of the class and this statement. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 4006 | isl::set IAClassDomainCtx = isl::manage(IAClass.ExecutionContext); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 4007 | if (IAClassDomainCtx) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 4008 | IAClassDomainCtx = IAClassDomainCtx.unite(MACtx).coalesce(); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 4009 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 4010 | IAClassDomainCtx = MACtx; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 4011 | IAClass.ExecutionContext = IAClassDomainCtx.release(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4012 | break; |
| 4013 | } |
| 4014 | |
| 4015 | if (Consolidated) |
| 4016 | continue; |
| 4017 | |
| 4018 | // If we did not consolidate MA, thus did not find an equivalence class |
| 4019 | // for it, we create a new one. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 4020 | InvariantEquivClasses.emplace_back(InvariantEquivClassTy{ |
| 4021 | PointerSCEV, MemoryAccessList{MA}, MACtx.release(), Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4022 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 4025 | /// Check if an access range is too complex. |
| 4026 | /// |
| 4027 | /// An access range is too complex, if it contains either many disjuncts or |
| 4028 | /// very complex expressions. As a simple heuristic, we assume if a set to |
| 4029 | /// be too complex if the sum of existentially quantified dimensions and |
| 4030 | /// set dimensions is larger than a threshold. This reliably detects both |
| 4031 | /// sets with many disjuncts as well as sets with many divisions as they |
| 4032 | /// arise in h264. |
| 4033 | /// |
| 4034 | /// @param AccessRange The range to check for complexity. |
| 4035 | /// |
| 4036 | /// @returns True if the access range is too complex. |
| 4037 | static bool isAccessRangeTooComplex(isl::set AccessRange) { |
| 4038 | unsigned NumTotalDims = 0; |
| 4039 | |
| 4040 | auto CountDimensions = [&NumTotalDims](isl::basic_set BSet) -> isl::stat { |
| 4041 | NumTotalDims += BSet.dim(isl::dim::div); |
| 4042 | NumTotalDims += BSet.dim(isl::dim::set); |
| 4043 | return isl::stat::ok; |
| 4044 | }; |
| 4045 | |
| 4046 | AccessRange.foreach_basic_set(CountDimensions); |
| 4047 | |
| 4048 | if (NumTotalDims > MaxDimensionsInAccessRange) |
| 4049 | return true; |
| 4050 | |
| 4051 | return false; |
| 4052 | } |
| 4053 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4054 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4055 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 4056 | // zero iterators, are by construction invariant, though we |
| 4057 | // currently "hoist" them anyway. This is necessary because we allow |
| 4058 | // them to be treated as parameters (e.g., in conditions) and our code |
| 4059 | // generation would otherwise use the old value. |
| 4060 | |
| 4061 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 4062 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4063 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 4064 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 4065 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4066 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4067 | |
| 4068 | // Skip accesses that have an invariant base pointer which is defined but |
| 4069 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 4070 | // returns a pointer that is used as a base address. However, as we want |
| 4071 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 4072 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 4073 | // that has a base pointer origin has a base pointer that is loaded and |
| 4074 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 4075 | // no base pointer origin we check that the base pointer is defined |
| 4076 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4077 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 4078 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4079 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4080 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 4081 | isl::map AccessRelation = give(Access->getAccessRelation().release()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4082 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4083 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4084 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4085 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4086 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4087 | AccessRelation = AccessRelation.intersect_domain(Stmt.getDomain()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4088 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4089 | |
| 4090 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 4091 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 4092 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4093 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4094 | } else if (BB != LI->getParent()) { |
| 4095 | // Skip accesses in non-affine subregions as they might not be executed |
| 4096 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4097 | return nullptr; |
| 4098 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4099 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 4100 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4101 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 4102 | if (isAccessRangeTooComplex(AccessRelation.range())) |
| 4103 | return nullptr; |
| 4104 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4105 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 4106 | isl::set WrittenCtx = Written.params(); |
| 4107 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4108 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4109 | if (!IsWritten) |
| 4110 | return WrittenCtx; |
| 4111 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4112 | WrittenCtx = WrittenCtx.remove_divs(); |
| 4113 | bool TooComplex = |
| 4114 | isl_set_n_basic_set(WrittenCtx.get()) >= MaxDisjunctsInDomain; |
| 4115 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4116 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4117 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4118 | addAssumption(INVARIANTLOAD, WrittenCtx.copy(), LI->getDebugLoc(), |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4119 | AS_RESTRICTION, LI->getParent()); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4120 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4123 | void Scop::verifyInvariantLoads() { |
| 4124 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4125 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4126 | assert(LI && contains(LI)); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4127 | // If there exists a statement in the scop which has a memory access for |
| 4128 | // @p LI, then mark this scop as infeasible for optimization. |
| 4129 | for (ScopStmt &Stmt : Stmts) |
| 4130 | if (Stmt.getArrayAccessOrNULLFor(LI)) { |
| 4131 | invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent()); |
| 4132 | return; |
| 4133 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4137 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4138 | if (!PollyInvariantLoadHoisting) |
| 4139 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4140 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4141 | isl::union_map Writes = getWrites(); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4142 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 4143 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4144 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4145 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 4146 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
Tobias Grosser | d16f927 | 2017-08-06 17:25:14 +0000 | [diff] [blame] | 4147 | InvariantAccesses.push_back({Access, NHCtx}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4148 | |
| 4149 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 4150 | for (auto InvMA : InvariantAccesses) |
| 4151 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 4152 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4153 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4156 | /// Find the canonical scop array info object for a set of invariant load |
| 4157 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 4158 | /// first load in the list of accesses which is used as base pointer of a |
| 4159 | /// scop array. |
| 4160 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 4161 | MemoryAccessList &Accesses) { |
| 4162 | for (MemoryAccess *Access : Accesses) { |
| 4163 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 4164 | Access->getAccessInstruction(), MemoryKind::Array); |
| 4165 | if (CanonicalArray) |
| 4166 | return CanonicalArray; |
| 4167 | } |
| 4168 | return nullptr; |
| 4169 | } |
| 4170 | |
| 4171 | /// Check if @p Array severs as base array in an invariant load. |
| 4172 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 4173 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 4174 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 4175 | if (Access2->getScopArrayInfo() == Array) |
| 4176 | return true; |
| 4177 | return false; |
| 4178 | } |
| 4179 | |
| 4180 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 4181 | /// with a reference to @p New. |
| 4182 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 4183 | const ScopArrayInfo *New) { |
| 4184 | for (ScopStmt &Stmt : *S) |
| 4185 | for (MemoryAccess *Access : Stmt) { |
| 4186 | if (Access->getLatestScopArrayInfo() != Old) |
| 4187 | continue; |
| 4188 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 4189 | isl::id Id = New->getBasePtrId(); |
| 4190 | isl::map Map = Access->getAccessRelation(); |
| 4191 | Map = Map.set_tuple_id(isl::dim::out, Id); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4192 | Access->setAccessRelation(Map); |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | void Scop::canonicalizeDynamicBasePtrs() { |
| 4197 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 4198 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 4199 | |
| 4200 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 4201 | findCanonicalArray(this, BasePtrAccesses); |
| 4202 | |
| 4203 | if (!CanonicalBasePtrSAI) |
| 4204 | continue; |
| 4205 | |
| 4206 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 4207 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 4208 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 4209 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 4210 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 4211 | continue; |
| 4212 | |
| 4213 | // we currently do not canonicalize arrays where some accesses are |
| 4214 | // hoisted as invariant loads. If we would, we need to update the access |
| 4215 | // function of the invariant loads as well. However, as this is not a |
| 4216 | // very common situation, we leave this for now to avoid further |
| 4217 | // complexity increases. |
| 4218 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 4219 | continue; |
| 4220 | |
| 4221 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4226 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 4227 | ArrayRef<const SCEV *> Sizes, |
| 4228 | MemoryKind Kind, |
| 4229 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4230 | assert((BasePtr || BaseName) && |
| 4231 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 4232 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 4233 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 4234 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4235 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4236 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 4237 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4238 | DL, this, BaseName)); |
| 4239 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4240 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4241 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 4242 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 4243 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4244 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4245 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4246 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4247 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4250 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 4251 | const std::string &BaseName, |
| 4252 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4253 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4254 | std::vector<const SCEV *> SCEVSizes; |
| 4255 | |
| 4256 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4257 | if (size) |
| 4258 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4259 | else |
| 4260 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4261 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4262 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4263 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4264 | return SAI; |
| 4265 | } |
| 4266 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4267 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4268 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4269 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4270 | return SAI; |
| 4271 | } |
| 4272 | |
| 4273 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4274 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4275 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4276 | return SAI; |
| 4277 | } |
| 4278 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4279 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4280 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4281 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4282 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4283 | return stringFromIslObj(AssumedContext); |
| 4284 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4285 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4286 | std::string Scop::getInvalidContextStr() const { |
| 4287 | return stringFromIslObj(InvalidContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4288 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4289 | |
| 4290 | std::string Scop::getNameStr() const { |
| 4291 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4292 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4293 | return EntryName + "---" + ExitName; |
| 4294 | } |
| 4295 | |
| 4296 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4297 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4298 | raw_string_ostream ExitStr(ExitName); |
| 4299 | raw_string_ostream EntryStr(EntryName); |
| 4300 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4301 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4302 | EntryStr.str(); |
| 4303 | |
| 4304 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4305 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4306 | ExitStr.str(); |
| 4307 | } else |
| 4308 | ExitName = "FunctionExit"; |
| 4309 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4310 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4313 | isl::set Scop::getContext() const { return isl::manage(isl_set_copy(Context)); } |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4314 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4315 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4316 | isl::space Scop::getFullParamSpace() const { |
| 4317 | std::vector<isl::id> FortranIDs; |
| 4318 | FortranIDs = getFortranArrayIds(arrays()); |
| 4319 | |
| 4320 | isl::space Space = isl::space::params_alloc( |
| 4321 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 4322 | |
| 4323 | unsigned PDim = 0; |
| 4324 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 4325 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4326 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4327 | } |
| 4328 | |
| 4329 | for (isl::id Id : FortranIDs) |
| 4330 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4331 | |
| 4332 | return Space; |
| 4333 | } |
| 4334 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4335 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4336 | assert(AssumedContext && "Assumed context not yet built"); |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4337 | return isl::manage(isl_set_copy(AssumedContext)); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4340 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4341 | if (PollyProcessUnprofitable) |
| 4342 | return true; |
| 4343 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4344 | if (isEmpty()) |
| 4345 | return false; |
| 4346 | |
| 4347 | unsigned OptimizableStmtsOrLoops = 0; |
| 4348 | for (auto &Stmt : *this) { |
| 4349 | if (Stmt.getNumIterators() == 0) |
| 4350 | continue; |
| 4351 | |
| 4352 | bool ContainsArrayAccs = false; |
| 4353 | bool ContainsScalarAccs = false; |
| 4354 | for (auto *MA : Stmt) { |
| 4355 | if (MA->isRead()) |
| 4356 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4357 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4358 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4361 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4362 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4363 | } |
| 4364 | |
| 4365 | return OptimizableStmtsOrLoops > 1; |
| 4366 | } |
| 4367 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4368 | bool Scop::hasFeasibleRuntimeContext() const { |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4369 | auto *PositiveContext = getAssumedContext().release(); |
Tobias Grosser | 04ec2eb | 2017-08-06 21:42:16 +0000 | [diff] [blame] | 4370 | auto *NegativeContext = getInvalidContext().release(); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 4371 | PositiveContext = |
| 4372 | addNonEmptyDomainConstraints(isl::manage(PositiveContext)).release(); |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4373 | bool IsFeasible = !(isl_set_is_empty(PositiveContext) || |
| 4374 | isl_set_is_subset(PositiveContext, NegativeContext)); |
| 4375 | isl_set_free(PositiveContext); |
| 4376 | if (!IsFeasible) { |
| 4377 | isl_set_free(NegativeContext); |
| 4378 | return false; |
| 4379 | } |
| 4380 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4381 | auto *DomainContext = isl_union_set_params(getDomains().release()); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4382 | IsFeasible = !isl_set_is_subset(DomainContext, NegativeContext); |
Johannes Doerfert | fb72187 | 2016-04-12 17:54:29 +0000 | [diff] [blame] | 4383 | IsFeasible &= !isl_set_is_subset(Context, NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4384 | isl_set_free(NegativeContext); |
| 4385 | isl_set_free(DomainContext); |
| 4386 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4387 | return IsFeasible; |
| 4388 | } |
| 4389 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4390 | static std::string toString(AssumptionKind Kind) { |
| 4391 | switch (Kind) { |
| 4392 | case ALIASING: |
| 4393 | return "No-aliasing"; |
| 4394 | case INBOUNDS: |
| 4395 | return "Inbounds"; |
| 4396 | case WRAPPING: |
| 4397 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4398 | case UNSIGNED: |
| 4399 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4400 | case COMPLEXITY: |
| 4401 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4402 | case PROFITABLE: |
| 4403 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4404 | case ERRORBLOCK: |
| 4405 | return "No-error"; |
| 4406 | case INFINITELOOP: |
| 4407 | return "Finite loop"; |
| 4408 | case INVARIANTLOAD: |
| 4409 | return "Invariant load"; |
| 4410 | case DELINEARIZATION: |
| 4411 | return "Delinearization"; |
| 4412 | } |
| 4413 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4414 | } |
| 4415 | |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4416 | bool Scop::isEffectiveAssumption(__isl_keep isl_set *Set, AssumptionSign Sign) { |
| 4417 | if (Sign == AS_ASSUMPTION) { |
| 4418 | if (isl_set_is_subset(Context, Set)) |
| 4419 | return false; |
| 4420 | |
| 4421 | if (isl_set_is_subset(AssumedContext, Set)) |
| 4422 | return false; |
| 4423 | } else { |
| 4424 | if (isl_set_is_disjoint(Set, Context)) |
| 4425 | return false; |
| 4426 | |
| 4427 | if (isl_set_is_subset(Set, InvalidContext)) |
| 4428 | return false; |
| 4429 | } |
| 4430 | return true; |
| 4431 | } |
| 4432 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4433 | bool Scop::trackAssumption(AssumptionKind Kind, __isl_keep isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4434 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4435 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4436 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4437 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4438 | // Do never emit trivial assumptions as they only clutter the output. |
| 4439 | if (!PollyRemarksMinimal) { |
| 4440 | isl_set *Univ = nullptr; |
| 4441 | if (Sign == AS_ASSUMPTION) |
| 4442 | Univ = isl_set_universe(isl_set_get_space(Set)); |
| 4443 | |
| 4444 | bool IsTrivial = (Sign == AS_RESTRICTION && isl_set_is_empty(Set)) || |
| 4445 | (Sign == AS_ASSUMPTION && isl_set_is_equal(Univ, Set)); |
| 4446 | isl_set_free(Univ); |
| 4447 | |
| 4448 | if (IsTrivial) |
| 4449 | return false; |
| 4450 | } |
| 4451 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4452 | switch (Kind) { |
| 4453 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4454 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4455 | break; |
| 4456 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4457 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4460 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4461 | break; |
| 4462 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4463 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4464 | break; |
| 4465 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4466 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4467 | break; |
| 4468 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4469 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4470 | break; |
| 4471 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4472 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4473 | break; |
| 4474 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4475 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4476 | break; |
| 4477 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4478 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4479 | break; |
| 4480 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4481 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4482 | break; |
| 4483 | } |
| 4484 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4485 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
| 4486 | std::string Msg = toString(Kind) + Suffix + stringFromIslObj(Set); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4487 | if (BB) |
| 4488 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 4489 | << Msg); |
| 4490 | else |
| 4491 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 4492 | R.getEntry()) |
| 4493 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4494 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | void Scop::addAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4498 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4499 | // Simplify the assumptions/restrictions first. |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4500 | Set = isl_set_gist_params(Set, getContext().release()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4501 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4502 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4503 | isl_set_free(Set); |
| 4504 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4507 | if (Sign == AS_ASSUMPTION) { |
| 4508 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
| 4509 | AssumedContext = isl_set_coalesce(AssumedContext); |
| 4510 | } else { |
| 4511 | InvalidContext = isl_set_union(InvalidContext, Set); |
| 4512 | InvalidContext = isl_set_coalesce(InvalidContext); |
| 4513 | } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4516 | void Scop::recordAssumption(AssumptionKind Kind, __isl_take isl_set *Set, |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4517 | DebugLoc Loc, AssumptionSign Sign, BasicBlock *BB) { |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4518 | assert((isl_set_is_params(Set) || BB) && |
| 4519 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4520 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | void Scop::addRecordedAssumptions() { |
| 4524 | while (!RecordedAssumptions.empty()) { |
| 4525 | const Assumption &AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4526 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4527 | if (!AS.BB) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4528 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign, nullptr /* BasicBlock */); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4529 | continue; |
| 4530 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4531 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4532 | // If the domain was deleted the assumptions are void. |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4533 | isl_set *Dom = getDomainConditions(AS.BB).release(); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4534 | if (!Dom) { |
| 4535 | isl_set_free(AS.Set); |
| 4536 | continue; |
| 4537 | } |
| 4538 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4539 | // If a basic block was given use its domain to simplify the assumption. |
| 4540 | // In case of restrictions we know they only have to hold on the domain, |
| 4541 | // thus we can intersect them with the domain of the block. However, for |
| 4542 | // assumptions the domain has to imply them, thus: |
| 4543 | // _ _____ |
| 4544 | // Dom => S <==> A v B <==> A - B |
| 4545 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4546 | // 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] | 4547 | // assumption. |
| 4548 | isl_set *S = AS.Set; |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4549 | if (AS.Sign == AS_RESTRICTION) |
| 4550 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4551 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4552 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4553 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4554 | addAssumption(AS.Kind, S, AS.Loc, AS_RESTRICTION, AS.BB); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4558 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4559 | addAssumption(Kind, isl_set_empty(getParamSpace().release()), Loc, |
| 4560 | AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Tobias Grosser | 04ec2eb | 2017-08-06 21:42:16 +0000 | [diff] [blame] | 4563 | isl::set Scop::getInvalidContext() const { |
| 4564 | return isl::manage(isl_set_copy(InvalidContext)); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4567 | void Scop::printContext(raw_ostream &OS) const { |
| 4568 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4569 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4570 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4571 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4572 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4573 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4574 | OS.indent(4) << "Invalid Context:\n"; |
| 4575 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4576 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4577 | unsigned Dim = 0; |
| 4578 | for (const SCEV *Parameter : Parameters) |
| 4579 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4582 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4583 | int noOfGroups = 0; |
| 4584 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4585 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4586 | noOfGroups += 1; |
| 4587 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4588 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4591 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4592 | if (MinMaxAliasGroups.empty()) { |
| 4593 | OS.indent(8) << "n/a\n"; |
| 4594 | return; |
| 4595 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4596 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4597 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4598 | |
| 4599 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4600 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4601 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4602 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4603 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4604 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4605 | } |
| 4606 | OS << " ]]\n"; |
| 4607 | } |
| 4608 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4609 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4610 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4611 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4612 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4613 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4614 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4615 | } |
| 4616 | OS << " ]]\n"; |
| 4617 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4621 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4622 | OS << "Statements {\n"; |
| 4623 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4624 | for (const ScopStmt &Stmt : *this) { |
| 4625 | OS.indent(4); |
| 4626 | Stmt.print(OS, PrintInstructions); |
| 4627 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4628 | |
| 4629 | OS.indent(4) << "}\n"; |
| 4630 | } |
| 4631 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4632 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4633 | OS << "Arrays {\n"; |
| 4634 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4635 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4636 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4637 | |
| 4638 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4639 | |
| 4640 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4641 | |
| 4642 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4643 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4644 | |
| 4645 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4648 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4649 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4650 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4651 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4652 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4653 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4654 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4655 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4656 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4657 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4658 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4659 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4660 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4661 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4662 | } |
| 4663 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4664 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4665 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4666 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4667 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4670 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 4671 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4672 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4673 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 4674 | isl_ctx *Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4675 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4676 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4677 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4678 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4679 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4680 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4681 | // 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] | 4682 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4683 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4684 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4685 | // TODO: We could use a heuristic and either use: |
| 4686 | // SCEVAffinator::takeNonNegativeAssumption |
| 4687 | // or |
| 4688 | // SCEVAffinator::interpretAsUnsigned |
| 4689 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4690 | if (NonNegative) |
| 4691 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4692 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4693 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4694 | |
| 4695 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4696 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4697 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4700 | isl::union_set Scop::getDomains() const { |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4701 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx(), 0); |
| 4702 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4703 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4704 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4705 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4706 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4707 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4710 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4711 | PWACtx PWAC = getPwAff(E, BB); |
| 4712 | isl_set_free(PWAC.second); |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4713 | return isl::manage(PWAC.first); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4716 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4717 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4718 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4719 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4720 | for (ScopStmt &Stmt : *this) { |
| 4721 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4722 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4723 | continue; |
| 4724 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4725 | isl::set Domain = Stmt.getDomain(); |
| 4726 | isl::map AccessDomain = MA->getAccessRelation(); |
| 4727 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 4728 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 4731 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4732 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4735 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4736 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4739 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4740 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4743 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4744 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4747 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4748 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4751 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4752 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4753 | } |
| 4754 | |
Tobias Grosser | fa03cb7 | 2017-08-17 22:04:53 +0000 | [diff] [blame] | 4755 | isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { |
| 4756 | return getAccessesOfType( |
| 4757 | [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); |
| 4758 | } |
| 4759 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4760 | // Check whether @p Node is an extension node. |
| 4761 | // |
| 4762 | // @return true if @p Node is an extension node. |
| 4763 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4764 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4765 | return isl_bool_error; |
| 4766 | else |
| 4767 | return isl_bool_true; |
| 4768 | } |
| 4769 | |
| 4770 | bool Scop::containsExtensionNode(__isl_keep isl_schedule *Schedule) { |
| 4771 | return isl_schedule_foreach_schedule_node_top_down(Schedule, isNotExtNode, |
| 4772 | nullptr) == isl_stat_error; |
| 4773 | } |
| 4774 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4775 | isl::union_map Scop::getSchedule() const { |
| 4776 | auto *Tree = getScheduleTree().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4777 | if (containsExtensionNode(Tree)) { |
| 4778 | isl_schedule_free(Tree); |
| 4779 | return nullptr; |
| 4780 | } |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 4781 | auto *S = isl_schedule_get_map(Tree); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4782 | isl_schedule_free(Tree); |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4783 | return isl::manage(S); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4784 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4785 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4786 | isl::schedule Scop::getScheduleTree() const { |
| 4787 | return isl::manage(isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 4788 | getDomains().release())); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4789 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4790 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4791 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4792 | auto *S = isl_schedule_from_domain(getDomains().release()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4793 | S = isl_schedule_insert_partial_schedule( |
| 4794 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 4795 | isl_schedule_free(Schedule); |
| 4796 | Schedule = S; |
| 4797 | } |
| 4798 | |
| 4799 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 4800 | isl_schedule_free(Schedule); |
| 4801 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4804 | bool Scop::restrictDomains(isl::union_set Domain) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4805 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4806 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4807 | isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); |
| 4808 | isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4809 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4810 | if (StmtDomain.is_subset(NewStmtDomain)) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4811 | continue; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4812 | |
| 4813 | Changed = true; |
| 4814 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4815 | NewStmtDomain = NewStmtDomain.coalesce(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4816 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4817 | if (NewStmtDomain.is_empty()) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4818 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4819 | else |
| 4820 | Stmt.restrictDomain(isl::set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4821 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4822 | return Changed; |
| 4823 | } |
| 4824 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4825 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4826 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4827 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4828 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4829 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4830 | // # Example: |
| 4831 | // |
| 4832 | // Domain: { A[i,j]; B[i,j,k] } |
| 4833 | // N: 1 |
| 4834 | // |
| 4835 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4836 | // |
| 4837 | // @param USet A union set describing the elements for which to generate a |
| 4838 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4839 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4840 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4841 | 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] | 4842 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4843 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4844 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4845 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4846 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4847 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4848 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4849 | int Dim = S.dim(isl::dim::set); |
| 4850 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4851 | N, Dim - N); |
| 4852 | if (N > 1) |
| 4853 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4854 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4855 | Result = Result.add_pw_multi_aff(PMA); |
| 4856 | return isl::stat::ok; |
| 4857 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4858 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4859 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4860 | (void)Res; |
| 4861 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4862 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4863 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4864 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4867 | void Scop::addScopStmt(BasicBlock *BB, Loop *SurroundingLoop, |
| 4868 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4869 | assert(BB && "Unexpected nullptr!"); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 4870 | Stmts.emplace_back(*this, *BB, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4871 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4872 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4873 | for (Instruction *Inst : Instructions) { |
| 4874 | assert(!InstStmtMap.count(Inst) && |
| 4875 | "Unexpected statement corresponding to the instruction."); |
| 4876 | InstStmtMap[Inst] = Stmt; |
| 4877 | } |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4880 | void Scop::addScopStmt(Region *R, Loop *SurroundingLoop) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4881 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | 5545407 | 2017-03-15 22:16:43 +0000 | [diff] [blame] | 4882 | Stmts.emplace_back(*this, *R, SurroundingLoop); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4883 | auto *Stmt = &Stmts.back(); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4884 | for (BasicBlock *BB : R->blocks()) { |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4885 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4886 | for (Instruction &Inst : *BB) { |
| 4887 | assert(!InstStmtMap.count(&Inst) && |
| 4888 | "Unexpected statement corresponding to the instruction."); |
| 4889 | InstStmtMap[&Inst] = Stmt; |
| 4890 | } |
| 4891 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4894 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 4895 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4896 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4897 | isl::set SourceDomain = SourceRel.domain(); |
| 4898 | isl::set TargetDomain = TargetRel.domain(); |
| 4899 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4900 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4901 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4902 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4903 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4904 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4905 | CopyStmtsNum++; |
| 4906 | return &(Stmts.back()); |
| 4907 | } |
| 4908 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4909 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4910 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4911 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4912 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4913 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4914 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4917 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4918 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4919 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4920 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4921 | /// |
| 4922 | /// Example: |
| 4923 | /// bb1(0) |
| 4924 | /// / \. |
| 4925 | /// bb2(1) bb3(2) |
| 4926 | /// \ / \. |
| 4927 | /// bb4(3) bb5(4) |
| 4928 | /// \ / |
| 4929 | /// bb6(5) |
| 4930 | /// |
| 4931 | /// Including loops requires additional processing. Whenever a loop header is |
| 4932 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4933 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4934 | /// this loop and complete the sequential schedule at this loop-level before |
| 4935 | /// processing about any other nodes. To implement this |
| 4936 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4937 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4938 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4939 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4940 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4941 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4942 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4943 | |
| 4944 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4945 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4946 | std::deque<RegionNode *> DelayList; |
| 4947 | bool LastRNWaiting = false; |
| 4948 | |
| 4949 | // Iterate over the region @p R in reverse post-order but queue |
| 4950 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4951 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4952 | // that we queued the last sub-region/block from the reverse post-order |
| 4953 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4954 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4955 | // the next queued sub-region/blocks. |
| 4956 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4957 | RegionNode *RN; |
| 4958 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 4959 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4960 | RN = WorkList.front(); |
| 4961 | WorkList.pop_front(); |
| 4962 | LastRNWaiting = false; |
| 4963 | } else { |
| 4964 | RN = DelayList.front(); |
| 4965 | DelayList.pop_front(); |
| 4966 | } |
| 4967 | |
| 4968 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4969 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4970 | L = OuterScopLoop; |
| 4971 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4972 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4973 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4974 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4975 | LastRNWaiting = true; |
| 4976 | DelayList.push_back(RN); |
| 4977 | continue; |
| 4978 | } |
| 4979 | LoopStack.push_back({L, nullptr, 0}); |
| 4980 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4981 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4982 | } |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4985 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4986 | if (RN->isSubRegion()) { |
| 4987 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4988 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4989 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4990 | return; |
| 4991 | } |
| 4992 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4993 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4994 | auto &LoopData = LoopStack.back(); |
| 4995 | LoopData.NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4996 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4997 | for (auto *Stmt : getStmtListFor(RN)) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4998 | auto *UDomain = isl_union_set_from_set(Stmt->getDomain().release()); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4999 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5000 | LoopData.Schedule = combineInSequence(LoopData.Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5003 | // Check if we just processed the last node in this loop. If we did, finalize |
| 5004 | // the loop by: |
| 5005 | // |
| 5006 | // - adding new schedule dimensions |
| 5007 | // - folding the resulting schedule into the parent loop schedule |
| 5008 | // - dropping the loop schedule from the LoopStack. |
| 5009 | // |
| 5010 | // Then continue to check surrounding loops, which might also have been |
| 5011 | // completed by this node. |
| 5012 | while (LoopData.L && |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 5013 | LoopData.NumBlocksProcessed == getNumBlocksInLoop(LoopData.L)) { |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 5014 | auto *Schedule = LoopData.Schedule; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5015 | auto NumBlocksProcessed = LoopData.NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 5016 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5017 | LoopStack.pop_back(); |
| 5018 | auto &NextLoopData = LoopStack.back(); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 5019 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5020 | if (Schedule) { |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 5021 | isl::union_set Domain = give(isl_schedule_get_domain(Schedule)); |
| 5022 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, LoopStack.size()); |
| 5023 | Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA.release()); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5024 | NextLoopData.Schedule = |
| 5025 | combineInSequence(NextLoopData.Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5026 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 5027 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 5028 | NextLoopData.NumBlocksProcessed += NumBlocksProcessed; |
| 5029 | LoopData = NextLoopData; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 5030 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 5033 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 5034 | auto StmtMapIt = StmtMap.find(BB); |
| 5035 | if (StmtMapIt == StmtMap.end()) |
| 5036 | return {}; |
| 5037 | assert(StmtMapIt->second.size() == 1 && |
| 5038 | "Each statement corresponds to exactly one BB."); |
| 5039 | return StmtMapIt->second; |
| 5040 | } |
| 5041 | |
| 5042 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 5043 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 5044 | if (!StmtList.empty()) |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 5045 | return StmtList.back(); |
| 5046 | return nullptr; |
| 5047 | } |
| 5048 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5049 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5050 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5051 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 5052 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 5055 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 5056 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5059 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5060 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5061 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 5062 | // outermostLoopInRegion always returns nullptr for top level regions |
| 5063 | if (R.isTopLevelRegion()) { |
| 5064 | // LoopInfo's depths start at 1, we start at 0 |
| 5065 | return L->getLoopDepth() - 1; |
| 5066 | } else { |
| 5067 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 5068 | assert(OuterLoop); |
| 5069 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 5070 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 5073 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 5074 | for (auto &SAI : arrays()) { |
| 5075 | if (SAI->getName() == BaseName) |
| 5076 | return SAI; |
| 5077 | } |
| 5078 | return nullptr; |
| 5079 | } |
| 5080 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 5081 | void Scop::addAccessData(MemoryAccess *Access) { |
| 5082 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 5083 | assert(SAI && "can only use after access relations have been constructed"); |
| 5084 | |
| 5085 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 5086 | ValueUseAccs[SAI].push_back(Access); |
| 5087 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 5088 | PHIIncomingAccs[SAI].push_back(Access); |
| 5089 | } |
| 5090 | |
| 5091 | void Scop::removeAccessData(MemoryAccess *Access) { |
| 5092 | if (Access->isOriginalValueKind() && Access->isRead()) { |
| 5093 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
| 5094 | std::remove(Uses.begin(), Uses.end(), Access); |
| 5095 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 5096 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
| 5097 | std::remove(Incomings.begin(), Incomings.end(), Access); |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 5102 | assert(SAI->isValueKind()); |
| 5103 | |
| 5104 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 5105 | if (!Val) |
| 5106 | return nullptr; |
| 5107 | |
| 5108 | ScopStmt *Stmt = getStmtFor(Val); |
| 5109 | if (!Stmt) |
| 5110 | return nullptr; |
| 5111 | |
| 5112 | return Stmt->lookupValueWriteOf(Val); |
| 5113 | } |
| 5114 | |
| 5115 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 5116 | assert(SAI->isValueKind()); |
| 5117 | auto It = ValueUseAccs.find(SAI); |
| 5118 | if (It == ValueUseAccs.end()) |
| 5119 | return {}; |
| 5120 | return It->second; |
| 5121 | } |
| 5122 | |
| 5123 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 5124 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5125 | |
| 5126 | if (SAI->isExitPHIKind()) |
| 5127 | return nullptr; |
| 5128 | |
| 5129 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
| 5130 | ScopStmt *Stmt = getStmtFor(PHI); |
| 5131 | assert(Stmt && "PHINode must be within the SCoP"); |
| 5132 | |
| 5133 | return Stmt->lookupPHIReadOf(PHI); |
| 5134 | } |
| 5135 | |
| 5136 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 5137 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 5138 | auto It = PHIIncomingAccs.find(SAI); |
| 5139 | if (It == PHIIncomingAccs.end()) |
| 5140 | return {}; |
| 5141 | return It->second; |
| 5142 | } |
| 5143 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 5144 | bool Scop::isEscaping(Instruction *Inst) { |
| 5145 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 5146 | "values defined inside the SCoP"); |
| 5147 | |
| 5148 | for (Use &Use : Inst->uses()) { |
| 5149 | BasicBlock *UserBB = getUseBlock(Use); |
| 5150 | if (!contains(UserBB)) |
| 5151 | return true; |
| 5152 | |
| 5153 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 5154 | // move to a new basic block such that its incoming blocks are not in the |
| 5155 | // SCoP anymore. |
| 5156 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 5157 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 5158 | return true; |
| 5159 | } |
| 5160 | return false; |
| 5161 | } |
| 5162 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame^] | 5163 | raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { |
| 5164 | scop.print(OS, PollyPrintInstructions); |
| 5165 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5168 | //===----------------------------------------------------------------------===// |
| 5169 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5170 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5171 | AU.addRequired<RegionInfoPass>(); |
| 5172 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5173 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5174 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5175 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5176 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5177 | AU.setPreservesAll(); |
| 5178 | } |
| 5179 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5180 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats) { |
| 5181 | NumLoopsInScop += Stats.NumLoops; |
| 5182 | MaxNumLoopsInScop = |
| 5183 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 5184 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5185 | if (Stats.MaxDepth == 1) |
| 5186 | NumScopsDepthOne++; |
| 5187 | else if (Stats.MaxDepth == 2) |
| 5188 | NumScopsDepthTwo++; |
| 5189 | else if (Stats.MaxDepth == 3) |
| 5190 | NumScopsDepthThree++; |
| 5191 | else if (Stats.MaxDepth == 4) |
| 5192 | NumScopsDepthFour++; |
| 5193 | else if (Stats.MaxDepth == 5) |
| 5194 | NumScopsDepthFive++; |
| 5195 | else |
| 5196 | NumScopsDepthLarger++; |
| 5197 | } |
| 5198 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5199 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5200 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5201 | |
| 5202 | if (!SD.isMaxRegionInScop(*R)) |
| 5203 | return false; |
| 5204 | |
| 5205 | Function *F = R->getEntry()->getParent(); |
| 5206 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5207 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5208 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5209 | auto const &DL = F->getParent()->getDataLayout(); |
| 5210 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5211 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5212 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5213 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5214 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5215 | |
| 5216 | if (S) { |
| 5217 | ScopDetection::LoopStats Stats = |
| 5218 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5219 | updateLoopCountStatistic(Stats); |
| 5220 | } |
| 5221 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5222 | return false; |
| 5223 | } |
| 5224 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5225 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5226 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5227 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5228 | else |
| 5229 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5230 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5231 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5232 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5233 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5234 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5235 | |
| 5236 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5237 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5238 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5239 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5240 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5241 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5242 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5243 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5244 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5245 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5246 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5247 | "Polly - Create polyhedral description of Scops", false, |
| 5248 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5249 | |
| 5250 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5251 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5252 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 5253 | AssumptionCache &AC) |
| 5254 | : DL(DL), SD(SD), SE(SE), LI(LI), AA(AA), DT(DT), AC(AC) { |
| 5255 | recompute(); |
| 5256 | } |
| 5257 | |
| 5258 | void ScopInfo::recompute() { |
| 5259 | RegionToScopMap.clear(); |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5260 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5261 | /// function. |
| 5262 | for (auto &It : SD) { |
| 5263 | Region *R = const_cast<Region *>(It); |
| 5264 | if (!SD.isMaxRegionInScop(*R)) |
| 5265 | continue; |
| 5266 | |
| 5267 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE); |
| 5268 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5269 | if (!S) |
| 5270 | continue; |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 5271 | ScopDetection::LoopStats Stats = |
| 5272 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
| 5273 | updateLoopCountStatistic(Stats); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5274 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5275 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5276 | (void)Inserted; |
| 5277 | } |
| 5278 | } |
| 5279 | |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 5280 | bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 5281 | FunctionAnalysisManager::Invalidator &Inv) { |
| 5282 | // Check whether the analysis, all analyses on functions have been preserved |
| 5283 | // or anything we're holding references to is being invalidated |
| 5284 | auto PAC = PA.getChecker<ScopInfoAnalysis>(); |
| 5285 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 5286 | Inv.invalidate<ScopAnalysis>(F, PA) || |
| 5287 | Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || |
| 5288 | Inv.invalidate<LoopAnalysis>(F, PA) || |
| 5289 | Inv.invalidate<AAManager>(F, PA) || |
| 5290 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 5291 | Inv.invalidate<AssumptionAnalysis>(F, PA); |
| 5292 | } |
| 5293 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5294 | AnalysisKey ScopInfoAnalysis::Key; |
| 5295 | |
| 5296 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5297 | FunctionAnalysisManager &FAM) { |
| 5298 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5299 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5300 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5301 | auto &AA = FAM.getResult<AAManager>(F); |
| 5302 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5303 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5304 | auto &DL = F.getParent()->getDataLayout(); |
| 5305 | return {DL, SD, SE, LI, AA, DT, AC}; |
| 5306 | } |
| 5307 | |
| 5308 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5309 | FunctionAnalysisManager &FAM) { |
| 5310 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 5311 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 5312 | // order here to keep the output persistent |
| 5313 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5314 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5315 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5316 | else |
| 5317 | Stream << "Invalid Scop!\n"; |
| 5318 | } |
| 5319 | return PreservedAnalyses::all(); |
| 5320 | } |
| 5321 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5322 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5323 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5324 | AU.addRequired<RegionInfoPass>(); |
| 5325 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5326 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5327 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5328 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5329 | AU.addRequired<AssumptionCacheTracker>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5330 | AU.setPreservesAll(); |
| 5331 | } |
| 5332 | |
| 5333 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5334 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5335 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5336 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5337 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5338 | auto const &DL = F.getParent()->getDataLayout(); |
| 5339 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5340 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5341 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5342 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5343 | return false; |
| 5344 | } |
| 5345 | |
| 5346 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5347 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5348 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5349 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5350 | else |
| 5351 | OS << "Invalid Scop!\n"; |
| 5352 | } |
| 5353 | } |
| 5354 | |
| 5355 | char ScopInfoWrapperPass::ID = 0; |
| 5356 | |
| 5357 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5358 | return new ScopInfoWrapperPass(); |
| 5359 | } |
| 5360 | |
| 5361 | INITIALIZE_PASS_BEGIN( |
| 5362 | ScopInfoWrapperPass, "polly-function-scops", |
| 5363 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5364 | false); |
| 5365 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5366 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5367 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5368 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5369 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5370 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5371 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5372 | INITIALIZE_PASS_END( |
| 5373 | ScopInfoWrapperPass, "polly-function-scops", |
| 5374 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5375 | false) |