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" |
Adam Nemet | e0f1541 | 2017-10-09 23:49:08 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 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 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 124 | STATISTIC(NumScops, "Number of feasible SCoPs after ScopInfo"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 125 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 126 | STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after ScopInfo"); |
| 127 | STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after ScopInfo"); |
| 128 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 129 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 130 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 131 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 132 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 133 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 134 | STATISTIC(NumScopsDepthLarger, |
| 135 | "Number of scops with maximal loop depth 6 and larger"); |
| 136 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 137 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 138 | STATISTIC(NumValueWrites, "Number of scalar value writes after ScopInfo"); |
| 139 | STATISTIC( |
| 140 | NumValueWritesInLoops, |
| 141 | "Number of scalar value writes nested in affine loops after ScopInfo"); |
| 142 | STATISTIC(NumPHIWrites, "Number of scalar phi writes after ScopInfo"); |
| 143 | STATISTIC(NumPHIWritesInLoops, |
| 144 | "Number of scalar phi writes nested in affine loops after ScopInfo"); |
| 145 | STATISTIC(NumSingletonWrites, "Number of singleton writes after ScopInfo"); |
| 146 | STATISTIC(NumSingletonWritesInLoops, |
| 147 | "Number of singleton writes nested in affine loops after ScopInfo"); |
| 148 | |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 149 | // The maximal number of basic sets we allow during domain construction to |
| 150 | // be created. More complex scops will result in very high compile time and |
| 151 | // are also unlikely to result in good code |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 152 | static int const MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 153 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 154 | // The number of disjunct in the context after which we stop to add more |
| 155 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 156 | // number of disjunct when adding non-convex sets to the context. |
| 157 | static int const MaxDisjunctsInContext = 4; |
| 158 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 159 | // The maximal number of dimensions we allow during invariant load construction. |
| 160 | // More complex access ranges will result in very high compile time and are also |
| 161 | // unlikely to result in good code. This value is very high and should only |
| 162 | // trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006). |
| 163 | static int const MaxDimensionsInAccessRange = 9; |
| 164 | |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 165 | static cl::opt<int> |
| 166 | OptComputeOut("polly-analysis-computeout", |
| 167 | cl::desc("Bound the scop analysis by a maximal amount of " |
| 168 | "computational steps (0 means no bound)"), |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 169 | cl::Hidden, cl::init(800000), cl::ZeroOrMore, |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 170 | cl::cat(PollyCategory)); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 171 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 172 | static cl::opt<bool> PollyRemarksMinimal( |
| 173 | "polly-remarks-minimal", |
| 174 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 175 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 176 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 177 | static cl::opt<int> RunTimeChecksMaxAccessDisjuncts( |
| 178 | "polly-rtc-max-array-disjuncts", |
| 179 | cl::desc("The maximal number of disjunts allowed in memory accesses to " |
| 180 | "to build RTCs."), |
| 181 | cl::Hidden, cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 182 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 183 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 184 | "polly-rtc-max-parameters", |
| 185 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 186 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 187 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 188 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 189 | "polly-rtc-max-arrays-per-group", |
| 190 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 191 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 192 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 193 | static cl::opt<std::string> UserContextStr( |
| 194 | "polly-context", cl::value_desc("isl parameter set"), |
| 195 | cl::desc("Provide additional constraints on the context parameters"), |
| 196 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 197 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 198 | static cl::opt<bool> |
| 199 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 200 | cl::desc("Abort if an isl error is encountered"), |
| 201 | cl::init(true), cl::cat(PollyCategory)); |
| 202 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 203 | static cl::opt<bool> PollyPreciseInbounds( |
| 204 | "polly-precise-inbounds", |
| 205 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 206 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 207 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 208 | static cl::opt<bool> |
| 209 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 210 | cl::desc("Do not take inbounds assumptions at all"), |
| 211 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 212 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 213 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 214 | "polly-ignore-parameter-bounds", |
| 215 | cl::desc( |
| 216 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 217 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 218 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 219 | static cl::opt<bool> PollyAllowDereferenceOfAllFunctionParams( |
| 220 | "polly-allow-dereference-of-all-function-parameters", |
| 221 | cl::desc( |
| 222 | "Treat all parameters to functions that are pointers as dereferencible." |
| 223 | " This is useful for invariant load hoisting, since we can generate" |
| 224 | " less runtime checks. This is only valid if all pointers to functions" |
| 225 | " are always initialized, so that Polly can choose to hoist" |
| 226 | " their loads. "), |
| 227 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 228 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 229 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 230 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 231 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 232 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 233 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 234 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 235 | bool polly::UseInstructionNames; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 236 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 237 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 238 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 239 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 240 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 241 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 242 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 243 | static cl::opt<bool> PollyPrintInstructions( |
| 244 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 245 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 246 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 248 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 249 | // Create a sequence of two schedules. Either argument may be null and is |
| 250 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 251 | // empty. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 252 | static isl::schedule combineInSequence(isl::schedule Prev, isl::schedule Succ) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 253 | if (!Prev) |
| 254 | return Succ; |
| 255 | if (!Succ) |
| 256 | return Prev; |
| 257 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 258 | return Prev.sequence(Succ); |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 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 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 665 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 666 | isl::id ArrayId = getArrayId(); |
| 667 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 668 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 669 | return SAI; |
| 670 | } |
| 671 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 672 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 673 | isl::id ArrayId = getLatestArrayId(); |
| 674 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 675 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 676 | return SAI; |
| 677 | } |
| 678 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 679 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 680 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 683 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 684 | if (!hasNewAccessRelation()) |
| 685 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 686 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 689 | isl::map MemoryAccess::getAddressFunction() const { |
| 690 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 693 | isl::pw_multi_aff |
| 694 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 695 | isl::map Schedule, ScheduledAccRel; |
| 696 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 697 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 698 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 699 | USchedule = USchedule.intersect_domain(UDomain); |
| 700 | Schedule = isl::map::from_union_map(USchedule); |
| 701 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 702 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 705 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 706 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 709 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 710 | return AccessRelation.to_str(); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 713 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 714 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 717 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 718 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 721 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 722 | return NewAccessRelation.to_str(); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 725 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 726 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 729 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 730 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 731 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 732 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 733 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 734 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 735 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 738 | // Formalize no out-of-bound access assumption |
| 739 | // |
| 740 | // When delinearizing array accesses we optimistically assume that the |
| 741 | // delinearized accesses do not access out of bound locations (the subscript |
| 742 | // expression of each array evaluates for each statement instance that is |
| 743 | // executed to a value that is larger than zero and strictly smaller than the |
| 744 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 745 | // dimension for which we do not need to assume any upper bound. At this point |
| 746 | // we formalize this assumption to ensure that at code generation time the |
| 747 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 748 | // |
| 749 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 750 | // first build the set of data locations that are not within array bounds. We |
| 751 | // then apply the reverse access relation to obtain the set of iterations that |
| 752 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 753 | // that are actually executed by intersecting them with the domain of the |
| 754 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 755 | // parameters that may cause statement instances to be executed that may |
| 756 | // possibly yield out of bound memory accesses. The complement of these |
| 757 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 758 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 759 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 760 | if (PollyIgnoreInbounds) |
| 761 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 762 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 763 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 764 | isl::set Outside = isl::set::empty(Space); |
| 765 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 766 | isl::local_space LS(Space); |
| 767 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 768 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 769 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 770 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 771 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 772 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 773 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 774 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 775 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 776 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 779 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 780 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 781 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 782 | |
| 783 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 784 | // Doing so increases the set of parameter combinations that are assumed to |
| 785 | // not appear. This is always save, but may make the resulting run-time check |
| 786 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 787 | Outside = Outside.remove_divs(); |
| 788 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 789 | const auto &Loc = getAccessInstruction() |
| 790 | ? getAccessInstruction()->getDebugLoc() |
| 791 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 792 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 793 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 794 | Statement->getParent()->recordAssumption(INBOUNDS, Outside, Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 795 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 798 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 799 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 800 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 801 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 802 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 803 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 804 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 805 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 806 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 807 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 808 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 809 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 810 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 811 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 812 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 813 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 814 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 815 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 816 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 817 | LengthMap = LengthMap.sum(SubscriptMap); |
| 818 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 819 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 822 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 823 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 824 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 825 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 826 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 827 | return; |
| 828 | |
| 829 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 830 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 831 | return; |
| 832 | |
| 833 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 834 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 835 | return; |
| 836 | |
| 837 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 838 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 839 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 840 | |
| 841 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 842 | if (Range.isFullSet()) |
| 843 | return; |
| 844 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 845 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 846 | return; |
| 847 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 848 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 849 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 850 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 851 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 852 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 853 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 854 | |
| 855 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 856 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 857 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 858 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 859 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 860 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 861 | isl::set AccessRange = Relation.range(); |
| 862 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 863 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 864 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 867 | void MemoryAccess::foldAccessRelation() { |
| 868 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 869 | return; |
| 870 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 871 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 872 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 873 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 874 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 875 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 876 | isl::space Space; |
| 877 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 878 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 879 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 880 | isl::space SpaceSize = DimSize.get_space(); |
| 881 | isl::id ParamId = |
| 882 | give(isl_space_get_dim_id(SpaceSize.get(), isl_dim_param, 0)); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 883 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 884 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 885 | Space = Space.range().map_from_set(); |
| 886 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 887 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 888 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 889 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 890 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 891 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 892 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 893 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 894 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 895 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 896 | for (int j = 0; j < Size; ++j) |
| 897 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 898 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 899 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 900 | isl::local_space LS(Space); |
| 901 | isl::constraint C; |
| 902 | C = isl::constraint::alloc_equality(LS); |
| 903 | C = C.set_constant_si(-1); |
| 904 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 905 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 906 | MapTwo = MapTwo.add_constraint(C); |
| 907 | C = isl::constraint::alloc_equality(LS); |
| 908 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 909 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 910 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 911 | MapTwo = MapTwo.add_constraint(C); |
| 912 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 913 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 914 | MapOne = MapOne.unite(MapTwo); |
| 915 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 916 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 917 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 918 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 919 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 920 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 921 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 922 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 923 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 924 | |
| 925 | // Access dimension folding might in certain cases increase the number of |
| 926 | // disjuncts in the memory access, which can possibly complicate the generated |
| 927 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 928 | if (!PollyPreciseFoldAccesses && |
| 929 | isl_map_n_basic_map(NewAccessRelation.get()) > |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 930 | isl_map_n_basic_map(AccessRelation.get())) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 931 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 932 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 933 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 936 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 937 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 938 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 939 | if (Size == 1) |
| 940 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 941 | |
| 942 | // Only one factor needs to be divisible. |
| 943 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 944 | for (auto *FactorExpr : MulExpr->operands()) |
| 945 | if (isDivisible(FactorExpr, Size, SE)) |
| 946 | return true; |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 951 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 952 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 953 | for (auto *OpExpr : NAryExpr->operands()) |
| 954 | if (!isDivisible(OpExpr, Size, SE)) |
| 955 | return false; |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 960 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 961 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 962 | return MulSCEV == Expr; |
| 963 | } |
| 964 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 965 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 966 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 967 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 968 | // Initialize the invalid domain which describes all iterations for which the |
| 969 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 970 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 971 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 972 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 973 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 974 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 975 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 976 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 977 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 978 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 979 | return; |
| 980 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 981 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 982 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 983 | // We overapproximate non-affine accesses with a possible access to the |
| 984 | // whole array. For read accesses it does not make a difference, if an |
| 985 | // access must or may happen. However, for write accesses it is important to |
| 986 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 987 | if (AccessRelation.is_null()) |
| 988 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 989 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 990 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 994 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 995 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 996 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 997 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 998 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 999 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 1000 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1003 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1004 | AccessRelation = AccessRelation.set_tuple_id( |
| 1005 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 1006 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 1007 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1008 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1009 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 1010 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1011 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1012 | AccessType AccType, Value *BaseAddress, |
| 1013 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1014 | ArrayRef<const SCEV *> Subscripts, |
| 1015 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 1016 | MemoryKind Kind) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1017 | : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(nullptr), |
| 1018 | BaseAddr(BaseAddress), ElementType(ElementType), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1019 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 1020 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1021 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1022 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1023 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1024 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1025 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1026 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1027 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1028 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1029 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1030 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1031 | : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), |
| 1032 | InvalidDomain(nullptr), AccessRelation(nullptr), |
| 1033 | NewAccessRelation(AccRel), FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 1034 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1035 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1036 | Sizes.push_back(nullptr); |
| 1037 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1038 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1039 | ElementType = SAI->getElementType(); |
| 1040 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1041 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1042 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1043 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1044 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1045 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1048 | MemoryAccess::~MemoryAccess() = default; |
| 1049 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1050 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1051 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1052 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 1053 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1056 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1057 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1058 | } |
| 1059 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1060 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1061 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1062 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1063 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1064 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1065 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1066 | else |
| 1067 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1068 | return OS; |
| 1069 | } |
| 1070 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1071 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1072 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1073 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1074 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1075 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1076 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1077 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1078 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1079 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1080 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1081 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1082 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1085 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1086 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (FAD) { |
| 1089 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1090 | OS << "] "; |
| 1091 | }; |
| 1092 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1093 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1094 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1095 | if (hasNewAccessRelation()) |
| 1096 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1099 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1100 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1101 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1102 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1103 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1104 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1105 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1106 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1107 | StmtDom = StmtDom.reset_tuple_id(); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1108 | isl::set NewInvalidDom = StmtDom.intersect(PWAC.second); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1109 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1110 | return PWAC.first; |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1113 | // Create a map in the size of the provided set domain, that maps from the |
| 1114 | // one element of the provided set domain to another element of the provided |
| 1115 | // set domain. |
| 1116 | // The mapping is limited to all points that are equal in all but the last |
| 1117 | // dimension and for which the last dimension of the input is strict smaller |
| 1118 | // than the last dimension of the output. |
| 1119 | // |
| 1120 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1121 | // |
| 1122 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1123 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1124 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1125 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1126 | isl::space Space = SetDomain.map_from_set(); |
| 1127 | isl::map Map = isl::map::universe(Space); |
| 1128 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Set all but the last dimension to be equal for the input and output |
| 1131 | // |
| 1132 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1133 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1134 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1135 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1136 | |
| 1137 | // Set the last dimension of the input to be strict smaller than the |
| 1138 | // last dimension of the output. |
| 1139 | // |
| 1140 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1141 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1142 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1145 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1146 | isl::map AccessRelation = getAccessRelation(); |
| 1147 | isl::space Space = Schedule.get_space().range(); |
| 1148 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1149 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1150 | Schedule = Schedule.reverse(); |
| 1151 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1152 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1153 | NextScatt = NextScatt.apply_range(Schedule); |
| 1154 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1155 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1156 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1157 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1158 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1159 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1162 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1163 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1164 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1165 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1166 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1167 | StrideX = isl::set::universe(Stride.get_space()); |
| 1168 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1169 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1170 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1171 | StrideWidth); |
| 1172 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1173 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1174 | return IsStrideX; |
| 1175 | } |
| 1176 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1177 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1178 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1181 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1182 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1185 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1186 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1189 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1190 | assert(NewAccess); |
| 1191 | |
| 1192 | #ifndef NDEBUG |
| 1193 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1194 | isl::space NewSpace = NewAccess.get_space(); |
| 1195 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1196 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1197 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1198 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1199 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1200 | // subdomain only. |
| 1201 | if (isRead()) { |
| 1202 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1203 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1204 | StmtDomain = |
| 1205 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1206 | isl::set NewDomain = NewAccess.domain(); |
| 1207 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1208 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1209 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1210 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1211 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1212 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1213 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1214 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1215 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1216 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1219 | InvariantEquivClassTy *EqClass = |
| 1220 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1221 | SAI->getBasePtr()); |
| 1222 | assert(EqClass && |
| 1223 | "Access functions to indirect arrays must have an invariant and " |
| 1224 | "hoisted base pointer"); |
| 1225 | } |
| 1226 | |
| 1227 | // Check whether access dimensions correspond to number of dimensions of the |
| 1228 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1229 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1230 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1231 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1232 | #endif |
| 1233 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1234 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1235 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1236 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1237 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1238 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1239 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1240 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1241 | |
| 1242 | return isl_set_is_subset(StmtDom.keep(), AccDom.keep()) == isl_bool_false; |
| 1243 | } |
| 1244 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1245 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1246 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1247 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1248 | isl::set Domain = getDomain(); |
| 1249 | if (Domain.is_empty()) |
| 1250 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1251 | auto Schedule = getParent()->getSchedule(); |
| 1252 | if (!Schedule) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1253 | return nullptr; |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1254 | Schedule = Schedule.intersect_domain(isl::union_set(Domain)); |
| 1255 | if (Schedule.is_empty()) |
| 1256 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1257 | isl::map M = M.from_union_map(Schedule); |
| 1258 | M = M.coalesce(); |
| 1259 | M = M.gist_domain(Domain); |
| 1260 | M = M.coalesce(); |
| 1261 | return M; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1262 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1263 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1264 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1265 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1266 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1267 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1270 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1271 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1272 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1273 | if (Access->isArrayKind()) { |
| 1274 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1275 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1276 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1277 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1278 | assert(!ValueWrites.lookup(AccessVal)); |
| 1279 | |
| 1280 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1281 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1282 | Value *AccessVal = Access->getAccessValue(); |
| 1283 | assert(!ValueReads.lookup(AccessVal)); |
| 1284 | |
| 1285 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1286 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1287 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1288 | assert(!PHIWrites.lookup(PHI)); |
| 1289 | |
| 1290 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1291 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1292 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1293 | assert(!PHIReads.lookup(PHI)); |
| 1294 | |
| 1295 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1298 | if (Prepend) { |
| 1299 | MemAccs.insert(MemAccs.begin(), Access); |
| 1300 | return; |
| 1301 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1302 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1305 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1306 | for (MemoryAccess *MA : *this) |
| 1307 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1308 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1309 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1310 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1311 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1314 | /// 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] | 1315 | static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet, |
| 1316 | void *User) { |
| 1317 | isl_set **BoundedParts = static_cast<isl_set **>(User); |
| 1318 | if (isl_basic_set_is_bounded(BSet)) |
| 1319 | *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet)); |
| 1320 | else |
| 1321 | isl_basic_set_free(BSet); |
| 1322 | return isl_stat_ok; |
| 1323 | } |
| 1324 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1325 | /// Return the bounded parts of @p S. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1326 | static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) { |
| 1327 | isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S)); |
| 1328 | isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts); |
| 1329 | isl_set_free(S); |
| 1330 | return BoundedParts; |
| 1331 | } |
| 1332 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1333 | /// 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] | 1334 | /// |
| 1335 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1336 | /// both with regards to the dimension @p Dim. |
| 1337 | static std::pair<__isl_give isl_set *, __isl_give isl_set *> |
| 1338 | partitionSetParts(__isl_take isl_set *S, unsigned Dim) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1339 | 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] | 1340 | S = isl_set_lower_bound_si(S, isl_dim_set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1341 | |
| 1342 | unsigned NumDimsS = isl_set_n_dim(S); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1343 | isl_set *OnlyDimS = isl_set_copy(S); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1346 | assert(NumDimsS >= Dim + 1); |
| 1347 | OnlyDimS = |
| 1348 | isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1); |
| 1349 | |
| 1350 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1351 | // as we are not interested in them. |
| 1352 | OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim); |
| 1353 | for (unsigned u = 0; u < Dim; u++) { |
| 1354 | isl_constraint *C = isl_inequality_alloc( |
| 1355 | isl_local_space_from_space(isl_set_get_space(OnlyDimS))); |
| 1356 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1); |
| 1357 | C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1); |
| 1358 | OnlyDimS = isl_set_add_constraint(OnlyDimS, C); |
| 1359 | } |
| 1360 | |
| 1361 | // Collect all bounded parts of OnlyDimS. |
| 1362 | isl_set *BoundedParts = collectBoundedParts(OnlyDimS); |
| 1363 | |
| 1364 | // Create the dimensions greater than Dim again. |
| 1365 | BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1, |
| 1366 | NumDimsS - Dim - 1); |
| 1367 | |
| 1368 | // Remove the artificial upper bound parameters again. |
| 1369 | BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim); |
| 1370 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1371 | isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1372 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1373 | } |
| 1374 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1375 | /// Set the dimension Ids from @p From in @p To. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1376 | static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From, |
| 1377 | __isl_take isl_set *To) { |
| 1378 | for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) { |
| 1379 | isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u); |
| 1380 | To = isl_set_set_dim_id(To, isl_dim_set, u, DimId); |
| 1381 | } |
| 1382 | return To; |
| 1383 | } |
| 1384 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1385 | /// 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] | 1386 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1387 | __isl_take isl_pw_aff *L, |
| 1388 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1389 | switch (Pred) { |
| 1390 | case ICmpInst::ICMP_EQ: |
| 1391 | return isl_pw_aff_eq_set(L, R); |
| 1392 | case ICmpInst::ICMP_NE: |
| 1393 | return isl_pw_aff_ne_set(L, R); |
| 1394 | case ICmpInst::ICMP_SLT: |
| 1395 | return isl_pw_aff_lt_set(L, R); |
| 1396 | case ICmpInst::ICMP_SLE: |
| 1397 | return isl_pw_aff_le_set(L, R); |
| 1398 | case ICmpInst::ICMP_SGT: |
| 1399 | return isl_pw_aff_gt_set(L, R); |
| 1400 | case ICmpInst::ICMP_SGE: |
| 1401 | return isl_pw_aff_ge_set(L, R); |
| 1402 | case ICmpInst::ICMP_ULT: |
| 1403 | return isl_pw_aff_lt_set(L, R); |
| 1404 | case ICmpInst::ICMP_UGT: |
| 1405 | return isl_pw_aff_gt_set(L, R); |
| 1406 | case ICmpInst::ICMP_ULE: |
| 1407 | return isl_pw_aff_le_set(L, R); |
| 1408 | case ICmpInst::ICMP_UGE: |
| 1409 | return isl_pw_aff_ge_set(L, R); |
| 1410 | default: |
| 1411 | llvm_unreachable("Non integer predicate not supported"); |
| 1412 | } |
| 1413 | } |
| 1414 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1415 | /// 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] | 1416 | /// |
| 1417 | /// Helper function that will make sure the dimensions of the result have the |
| 1418 | /// same isl_id's as the @p Domain. |
| 1419 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
| 1420 | __isl_take isl_pw_aff *L, |
| 1421 | __isl_take isl_pw_aff *R, |
| 1422 | __isl_keep isl_set *Domain) { |
| 1423 | isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R); |
| 1424 | return setDimensionIds(Domain, ConsequenceCondSet); |
| 1425 | } |
| 1426 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1427 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1428 | /// |
| 1429 | /// @param S The Scop in which @p BB resides in. |
| 1430 | /// @param BB The BB for which isl representation is to be |
| 1431 | /// computed. |
| 1432 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1433 | /// @param E The SCEV that should be translated. |
| 1434 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1435 | /// |
| 1436 | /// Note that this function will also adjust the invalid context accordingly. |
| 1437 | |
| 1438 | __isl_give isl_pw_aff * |
| 1439 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1440 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1441 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1442 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1443 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(PWAC.second); |
| 1444 | return PWAC.first.take(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1447 | /// 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] | 1448 | /// |
| 1449 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1450 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1451 | /// have as many elements as @p SI has successors. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1452 | bool buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1453 | __isl_keep isl_set *Domain, |
| 1454 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1455 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1456 | Value *Condition = getConditionFromTerminator(SI); |
| 1457 | assert(Condition && "No condition for switch"); |
| 1458 | |
| 1459 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1460 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1461 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1462 | |
| 1463 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1464 | ConditionSets.resize(NumSuccessors); |
| 1465 | for (auto &Case : SI->cases()) { |
| 1466 | unsigned Idx = Case.getSuccessorIndex(); |
| 1467 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1468 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1469 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1470 | isl_set *CaseConditionSet = |
| 1471 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain); |
| 1472 | ConditionSets[Idx] = isl_set_coalesce( |
| 1473 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1474 | } |
| 1475 | |
| 1476 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1477 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1478 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1479 | ConditionSetUnion = |
| 1480 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
| 1481 | ConditionSets[0] = setDimensionIds( |
| 1482 | Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion)); |
| 1483 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1484 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1485 | |
| 1486 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1489 | /// Build condition sets for unsigned ICmpInst(s). |
| 1490 | /// Special handling is required for unsigned operands to ensure that if |
| 1491 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1492 | /// it should wrap around. |
| 1493 | /// |
| 1494 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1495 | /// between TestVal and UpperBound, i.e, |
| 1496 | /// TestVal < UpperBound OR TestVal <= UpperBound |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1497 | __isl_give isl_set * |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1498 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1499 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1500 | const SCEV *SCEV_UpperBound, |
| 1501 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1502 | bool IsStrictUpperBound) { |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1503 | // Do not take NonNeg assumption on TestVal |
| 1504 | // as it might have MSB (Sign bit) set. |
| 1505 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1506 | // Take NonNeg assumption on UpperBound. |
| 1507 | isl_pw_aff *UpperBound = |
| 1508 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1509 | |
| 1510 | // 0 <= TestVal |
| 1511 | isl_set *First = |
| 1512 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1513 | isl_pw_aff_get_domain_space(TestVal))), |
| 1514 | isl_pw_aff_copy(TestVal)); |
| 1515 | |
| 1516 | isl_set *Second; |
| 1517 | if (IsStrictUpperBound) |
| 1518 | // TestVal < UpperBound |
| 1519 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1520 | else |
| 1521 | // TestVal <= UpperBound |
| 1522 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1523 | |
| 1524 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
| 1525 | ConsequenceCondSet = setDimensionIds(Domain, ConsequenceCondSet); |
| 1526 | return ConsequenceCondSet; |
| 1527 | } |
| 1528 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1529 | /// Build the conditions sets for the branch condition @p Condition in |
| 1530 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1531 | /// |
| 1532 | /// This will fill @p ConditionSets with the conditions under which control |
| 1533 | /// 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] | 1534 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1535 | /// context under which @p Condition is true/false will be returned as the |
| 1536 | /// new elements of @p ConditionSets. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1537 | bool buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1538 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1539 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1540 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1541 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1542 | isl_set *ConsequenceCondSet = nullptr; |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1543 | |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1544 | if (auto Load = dyn_cast<LoadInst>(Condition)) { |
| 1545 | const SCEV *LHSSCEV = SE.getSCEVAtScope(Load, L); |
| 1546 | const SCEV *RHSSCEV = SE.getZero(LHSSCEV->getType()); |
| 1547 | bool NonNeg = false; |
| 1548 | isl_pw_aff *LHS = getPwAff(S, BB, InvalidDomainMap, LHSSCEV, NonNeg); |
| 1549 | isl_pw_aff *RHS = getPwAff(S, BB, InvalidDomainMap, RHSSCEV, NonNeg); |
| 1550 | ConsequenceCondSet = |
| 1551 | buildConditionSet(ICmpInst::ICMP_SLE, LHS, RHS, Domain); |
| 1552 | } else if (auto *PHI = dyn_cast<PHINode>(Condition)) { |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1553 | auto *Unique = dyn_cast<ConstantInt>( |
| 1554 | getUniqueNonErrorValue(PHI, &S.getRegion(), *S.getLI(), *S.getDT())); |
| 1555 | |
| 1556 | if (Unique->isZero()) |
| 1557 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1558 | else |
| 1559 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1560 | } else if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1561 | if (CCond->isZero()) |
| 1562 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1563 | else |
| 1564 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1565 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1566 | auto Opcode = BinOp->getOpcode(); |
| 1567 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1568 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1569 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1570 | InvalidDomainMap, ConditionSets) && |
| 1571 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1572 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1573 | if (!Valid) { |
| 1574 | while (!ConditionSets.empty()) |
| 1575 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1576 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1577 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1578 | |
| 1579 | isl_set_free(ConditionSets.pop_back_val()); |
| 1580 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1581 | isl_set_free(ConditionSets.pop_back_val()); |
| 1582 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1583 | |
| 1584 | if (Opcode == Instruction::And) |
| 1585 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1586 | else |
| 1587 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1588 | } else { |
| 1589 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1590 | assert(ICond && |
| 1591 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1592 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1593 | LoopInfo &LI = *S.getLI(); |
| 1594 | DominatorTree &DT = *S.getDT(); |
| 1595 | Region &R = S.getRegion(); |
| 1596 | |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1597 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1598 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1599 | // to be set. The comparison is equal to a signed comparison under this |
| 1600 | // assumption. |
| 1601 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1602 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1603 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1604 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1605 | LeftOperand = tryForwardThroughPHI(LeftOperand, R, SE, LI, DT); |
| 1606 | RightOperand = tryForwardThroughPHI(RightOperand, R, SE, LI, DT); |
| 1607 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1608 | switch (ICond->getPredicate()) { |
| 1609 | case ICmpInst::ICMP_ULT: |
| 1610 | ConsequenceCondSet = |
| 1611 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1612 | RightOperand, InvalidDomainMap, true); |
| 1613 | break; |
| 1614 | case ICmpInst::ICMP_ULE: |
| 1615 | ConsequenceCondSet = |
| 1616 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1617 | RightOperand, InvalidDomainMap, false); |
| 1618 | break; |
| 1619 | case ICmpInst::ICMP_UGT: |
| 1620 | ConsequenceCondSet = |
| 1621 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1622 | LeftOperand, InvalidDomainMap, true); |
| 1623 | break; |
| 1624 | case ICmpInst::ICMP_UGE: |
| 1625 | ConsequenceCondSet = |
| 1626 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1627 | LeftOperand, InvalidDomainMap, false); |
| 1628 | break; |
| 1629 | default: |
| 1630 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1631 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
| 1632 | ConsequenceCondSet = |
| 1633 | buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain); |
| 1634 | break; |
| 1635 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1638 | // If no terminator was given we are only looking for parameter constraints |
| 1639 | // under which @p Condition is true/false. |
| 1640 | if (!TI) |
| 1641 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1642 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1643 | ConsequenceCondSet = isl_set_coalesce( |
| 1644 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1645 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1646 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1647 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1648 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1649 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1650 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1651 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1652 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1653 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1654 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1657 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1658 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1659 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1660 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1661 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1662 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | ConditionSets.push_back(ConsequenceCondSet); |
| 1666 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1667 | |
| 1668 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1671 | /// 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] | 1672 | /// |
| 1673 | /// This will fill @p ConditionSets with the conditions under which control |
| 1674 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1675 | /// have as many elements as @p TI has successors. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1676 | bool buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1677 | __isl_keep isl_set *Domain, |
| 1678 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1679 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1680 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1681 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1682 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1683 | |
| 1684 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1685 | |
| 1686 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1687 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1688 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1691 | Value *Condition = getConditionFromTerminator(TI); |
| 1692 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1693 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1694 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1695 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1698 | ScopStmt::ScopStmt(Scop &parent, Region &R, StringRef Name, |
| 1699 | Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1700 | std::vector<Instruction *> EntryBlockInstructions) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1701 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), R(&R), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1702 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1703 | Instructions(EntryBlockInstructions) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1704 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1705 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, |
| 1706 | Loop *SurroundingLoop, |
| 1707 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1708 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1709 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1710 | Instructions(Instructions) {} |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1711 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1712 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1713 | isl::set NewDomain) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1714 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), |
| 1715 | Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1716 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1717 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1718 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1719 | Domain = Domain.set_tuple_id(Id); |
| 1720 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1721 | auto *Access = |
| 1722 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1723 | parent.addAccessFunction(Access); |
| 1724 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1725 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1726 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1727 | parent.addAccessFunction(Access); |
| 1728 | addAccess(Access); |
| 1729 | } |
| 1730 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1731 | ScopStmt::~ScopStmt() = default; |
| 1732 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1733 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1734 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1735 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1736 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1737 | if (!S) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1738 | return {}; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1739 | auto Str = stringFromIslObj(S); |
| 1740 | isl_map_free(S); |
| 1741 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1744 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1745 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1746 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1747 | if (isBlockStmt()) |
| 1748 | return getBasicBlock(); |
| 1749 | return getRegion()->getEntry(); |
| 1750 | } |
| 1751 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1752 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1753 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1754 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1755 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1756 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1757 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1760 | isl::ctx ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1761 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1762 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1763 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1764 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1765 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1766 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1767 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1768 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1769 | OS << "Instructions {\n"; |
| 1770 | |
| 1771 | for (Instruction *Inst : Instructions) |
| 1772 | OS.indent(16) << *Inst << "\n"; |
| 1773 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1774 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1777 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1778 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1779 | OS.indent(12) << "Domain :=\n"; |
| 1780 | |
| 1781 | if (Domain) { |
| 1782 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1783 | } else |
| 1784 | OS.indent(16) << "n/a\n"; |
| 1785 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1786 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1787 | |
| 1788 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1789 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1790 | } else |
| 1791 | OS.indent(16) << "n/a\n"; |
| 1792 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1793 | for (MemoryAccess *Access : MemAccs) |
| 1794 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1795 | |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1796 | if (PrintInstructions) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1797 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1800 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1801 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1802 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1803 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1804 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1805 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1806 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1807 | (void)Found; |
| 1808 | assert(Found && "Expected access data not found"); |
| 1809 | } |
| 1810 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1811 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1812 | (void)Found; |
| 1813 | assert(Found && "Expected access data not found"); |
| 1814 | } |
| 1815 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1816 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1817 | (void)Found; |
| 1818 | assert(Found && "Expected access data not found"); |
| 1819 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1820 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1821 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1822 | (void)Found; |
| 1823 | assert(Found && "Expected access data not found"); |
| 1824 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1827 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1828 | // Remove the memory accesses from this statement together with all scalar |
| 1829 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1830 | // instruction, hence would not be removed by this function. However, it is |
| 1831 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1832 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1833 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1834 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1835 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1836 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1837 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1838 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1839 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1840 | Parent.removeAccessData(MA); |
| 1841 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1842 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1843 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1844 | MemAccs.end()); |
| 1845 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1848 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA) { |
| 1849 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 1850 | assert(MAIt != MemAccs.end()); |
| 1851 | MemAccs.erase(MAIt); |
| 1852 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1853 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1854 | Parent.removeAccessData(MA); |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1855 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1856 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 1857 | if (It != InstructionToAccess.end()) { |
| 1858 | It->second.remove(MA); |
| 1859 | if (It->second.empty()) |
| 1860 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1861 | } |
| 1862 | } |
| 1863 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 1864 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 1865 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 1866 | if (Access) |
| 1867 | return Access; |
| 1868 | |
| 1869 | ScopArrayInfo *SAI = |
| 1870 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 1871 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 1872 | true, {}, {}, V, MemoryKind::Value); |
| 1873 | Parent.addAccessFunction(Access); |
| 1874 | Access->buildAccessRelation(SAI); |
| 1875 | addAccess(Access); |
| 1876 | Parent.addAccessData(Access); |
| 1877 | return Access; |
| 1878 | } |
| 1879 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1880 | raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { |
| 1881 | S.print(OS, PollyPrintInstructions); |
| 1882 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1885 | //===----------------------------------------------------------------------===// |
| 1886 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1887 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1888 | void Scop::setContext(isl::set NewContext) { |
| 1889 | Context = NewContext.align_params(Context.get_space()); |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1892 | namespace { |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1893 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1894 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1895 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1896 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1897 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1898 | |
| 1899 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1900 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 1901 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1902 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1903 | |
| 1904 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1905 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1906 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 1907 | return SSPR.visit(E); |
| 1908 | } |
| 1909 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1910 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 1911 | auto *Start = visit(E->getStart()); |
| 1912 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 1913 | visit(E->getStepRecurrence(SE)), |
| 1914 | E->getLoop(), SCEV::FlagAnyWrap); |
| 1915 | return SE.getAddExpr(Start, AddRec); |
| 1916 | } |
| 1917 | |
| 1918 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 1919 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 1920 | return SE.getUnknown(NewValue); |
| 1921 | return E; |
| 1922 | } |
| 1923 | }; |
| 1924 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1925 | /// Check whether we should remap a SCEV expression. |
| 1926 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1927 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1928 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1929 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1930 | |
| 1931 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1932 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 1933 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1934 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 1935 | |
| 1936 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1937 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1938 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 1939 | SFIS.visitAll(E); |
| 1940 | return SFIS.FoundInside; |
| 1941 | } |
| 1942 | |
| 1943 | bool follow(const SCEV *E) { |
| 1944 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 1945 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 1946 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 1947 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 1948 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 1949 | } |
| 1950 | return !FoundInside; |
| 1951 | } |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1952 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1953 | bool isDone() { return FoundInside; } |
| 1954 | }; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1955 | |
| 1956 | } // end anonymous namespace |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1957 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1958 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1959 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 1960 | // doesn't like addition between an AddRec and an expression that |
| 1961 | // doesn't have a dominance relationship with it.) |
| 1962 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 1963 | return E; |
| 1964 | |
| 1965 | // Rewrite SCEV. |
| 1966 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1969 | // This table of function names is used to translate parameter names in more |
| 1970 | // human-readable names. This makes it easier to interpret Polly analysis |
| 1971 | // results. |
| 1972 | StringMap<std::string> KnownNames = { |
| 1973 | {"_Z13get_global_idj", "global_id"}, |
| 1974 | {"_Z12get_local_idj", "local_id"}, |
| 1975 | {"_Z15get_global_sizej", "global_size"}, |
| 1976 | {"_Z14get_local_sizej", "local_size"}, |
| 1977 | {"_Z12get_work_dimv", "work_dim"}, |
| 1978 | {"_Z17get_global_offsetj", "global_offset"}, |
| 1979 | {"_Z12get_group_idj", "group_id"}, |
| 1980 | {"_Z14get_num_groupsj", "num_groups"}, |
| 1981 | }; |
| 1982 | |
| 1983 | static std::string getCallParamName(CallInst *Call) { |
| 1984 | std::string Result; |
| 1985 | raw_string_ostream OS(Result); |
| 1986 | std::string Name = Call->getCalledFunction()->getName(); |
| 1987 | |
| 1988 | auto Iterator = KnownNames.find(Name); |
| 1989 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 1990 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1991 | OS << Name; |
| 1992 | for (auto &Operand : Call->arg_operands()) { |
| 1993 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 1994 | OS << "_" << Op->getValue(); |
| 1995 | } |
| 1996 | OS.flush(); |
| 1997 | return Result; |
| 1998 | } |
| 1999 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2000 | void Scop::createParameterId(const SCEV *Parameter) { |
| 2001 | assert(Parameters.count(Parameter)); |
| 2002 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2003 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2004 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2005 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2006 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 2007 | Value *Val = ValueParameter->getValue(); |
| 2008 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2009 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 2010 | if (Call && isConstCall(Call)) { |
| 2011 | ParameterName = getCallParamName(Call); |
| 2012 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2013 | // If this parameter references a specific Value and this value has a name |
| 2014 | // we use this name as it is likely to be unique and more useful than just |
| 2015 | // a number. |
| 2016 | if (Val->hasName()) |
| 2017 | ParameterName = Val->getName(); |
| 2018 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 2019 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 2020 | if (LoadOrigin->hasName()) { |
| 2021 | ParameterName += "_loaded_from_"; |
| 2022 | ParameterName += |
| 2023 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 2024 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 2025 | } |
| 2026 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 2027 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 2028 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 2029 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 2030 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2031 | isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2032 | const_cast<void *>((const void *)Parameter)); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2033 | ParameterIds[Parameter] = Id; |
| 2034 | } |
| 2035 | |
| 2036 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2037 | for (const SCEV *Parameter : NewParameters) { |
| 2038 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2039 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2040 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2041 | |
| 2042 | if (Parameters.insert(Parameter)) |
| 2043 | createParameterId(Parameter); |
| 2044 | } |
| 2045 | } |
| 2046 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 2047 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2048 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2049 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2050 | return ParameterIds.lookup(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2051 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2052 | |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2053 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2054 | isl_set *DomainContext = isl_union_set_params(getDomains().release()); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2055 | return isl::manage(isl_set_intersect_params(C.release(), DomainContext)); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2058 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2059 | return DT.dominates(BB, getEntry()); |
| 2060 | } |
| 2061 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2062 | void Scop::addUserAssumptions( |
| 2063 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2064 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2065 | for (auto &Assumption : AC.assumptions()) { |
| 2066 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2067 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2068 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2069 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2070 | bool InScop = contains(CI); |
| 2071 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2072 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2073 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2074 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2075 | auto *Val = CI->getArgOperand(0); |
| 2076 | ParameterSetTy DetectedParams; |
| 2077 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2078 | ORE.emit( |
| 2079 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 2080 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2081 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2082 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Collect all newly introduced parameters. |
| 2085 | ParameterSetTy NewParams; |
| 2086 | for (auto *Param : DetectedParams) { |
| 2087 | Param = extractConstantFactor(Param, *SE).second; |
| 2088 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2089 | if (Parameters.count(Param)) |
| 2090 | continue; |
| 2091 | NewParams.insert(Param); |
| 2092 | } |
| 2093 | |
| 2094 | SmallVector<isl_set *, 2> ConditionSets; |
| 2095 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2096 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2097 | auto *Dom = InScop ? DomainMap[BB].copy() : Context.copy(); |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2098 | assert(Dom && "Cannot propagate a nullptr."); |
| 2099 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 2100 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2101 | isl_set_free(Dom); |
| 2102 | |
| 2103 | if (!Valid) |
| 2104 | continue; |
| 2105 | |
| 2106 | isl_set *AssumptionCtx = nullptr; |
| 2107 | if (InScop) { |
| 2108 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2109 | isl_set_free(ConditionSets[0]); |
| 2110 | } else { |
| 2111 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2112 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2113 | } |
| 2114 | |
| 2115 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2116 | if (!NewParams.empty()) { |
| 2117 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2118 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2119 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2120 | isl_id_free(Id); |
| 2121 | |
| 2122 | if (!NewParams.count(Param)) |
| 2123 | continue; |
| 2124 | |
| 2125 | AssumptionCtx = |
| 2126 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2127 | } |
| 2128 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2129 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2130 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2131 | Context = Context.intersect(isl::manage(AssumptionCtx)); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2135 | void Scop::addUserContext() { |
| 2136 | if (UserContextStr.empty()) |
| 2137 | return; |
| 2138 | |
Hongbin Zheng | 8831eb7 | 2016-02-17 15:49:21 +0000 | [diff] [blame] | 2139 | isl_set *UserContext = |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2140 | isl_set_read_from_str(getIslCtx().get(), UserContextStr.c_str()); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2141 | isl_space *Space = getParamSpace().release(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2142 | if (isl_space_dim(Space, isl_dim_param) != |
| 2143 | isl_set_dim(UserContext, isl_dim_param)) { |
| 2144 | auto SpaceStr = isl_space_to_str(Space); |
| 2145 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2146 | << "number of dimensions than the computed context. Due to this " |
| 2147 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2148 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2149 | free(SpaceStr); |
| 2150 | isl_set_free(UserContext); |
| 2151 | isl_space_free(Space); |
| 2152 | return; |
| 2153 | } |
| 2154 | |
| 2155 | for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2156 | std::string NameContext = Context.get_dim_name(isl::dim::param, i); |
| 2157 | std::string NameUserContext = |
| 2158 | isl_set_get_dim_name(UserContext, isl_dim_param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2159 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2160 | if (NameContext != NameUserContext) { |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2161 | auto SpaceStr = isl_space_to_str(Space); |
| 2162 | errs() << "Error: the name of dimension " << i |
| 2163 | << " provided in -polly-context " |
| 2164 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2165 | << "context is '" << NameContext |
| 2166 | << "'. Due to this name mismatch, " |
| 2167 | << "the -polly-context option is ignored. Please provide " |
| 2168 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 2169 | free(SpaceStr); |
| 2170 | isl_set_free(UserContext); |
| 2171 | isl_space_free(Space); |
| 2172 | return; |
| 2173 | } |
| 2174 | |
| 2175 | UserContext = |
| 2176 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 2177 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 2178 | } |
| 2179 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2180 | Context = Context.intersect(isl::manage(UserContext)); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2181 | isl_space_free(Space); |
| 2182 | } |
| 2183 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2184 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2185 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2186 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2187 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2188 | for (LoadInst *LInst : RIL) { |
| 2189 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2190 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2191 | Type *Ty = LInst->getType(); |
| 2192 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2193 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2194 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2195 | continue; |
| 2196 | } |
| 2197 | |
| 2198 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2199 | InvariantEquivClasses.emplace_back( |
| 2200 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2204 | void Scop::buildContext() { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2205 | isl::space Space = isl::space::params_alloc(getIslCtx(), 0); |
| 2206 | Context = isl::set::universe(Space); |
| 2207 | InvalidContext = isl::set::empty(Space); |
| 2208 | AssumedContext = isl::set::universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2211 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2212 | unsigned PDim = 0; |
| 2213 | for (auto *Parameter : Parameters) { |
| 2214 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2215 | Context = addRangeBoundsToSet(Context, SRange, PDim++, isl::dim::param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2219 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 2220 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2221 | for (auto Array : Arrays) { |
| 2222 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2223 | // for its outermost dimension. Fortran arrays will have this since the |
| 2224 | // outermost dimension size can be picked up from their runtime description. |
| 2225 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2226 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2227 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2228 | if (!PwAff) |
| 2229 | continue; |
| 2230 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2231 | isl::id Id = |
| 2232 | isl::manage(isl_pw_aff_get_dim_id(PwAff.get(), isl_dim_param, 0)); |
| 2233 | assert(!Id.is_null() && |
| 2234 | "Invalid Id for PwAff expression in Fortran array"); |
| 2235 | Id.dump(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2236 | OutermostSizeIds.push_back(Id); |
| 2237 | } |
| 2238 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2239 | return OutermostSizeIds; |
| 2240 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2241 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2242 | // The FORTRAN array size parameters are known to be non-negative. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2243 | static isl::set boundFortranArrayParams(isl::set Context, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2244 | Scop::array_range Arrays) { |
| 2245 | std::vector<isl::id> OutermostSizeIds; |
| 2246 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2247 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2248 | for (isl::id Id : OutermostSizeIds) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2249 | int dim = Context.find_dim_by_id(isl::dim::param, Id); |
| 2250 | Context = Context.lower_bound_si(isl::dim::param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | return Context; |
| 2254 | } |
| 2255 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2256 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2257 | if (PollyIgnoreParamBounds) |
| 2258 | return; |
| 2259 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2260 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2261 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Align the parameters of all data structures to the model. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2264 | Context = Context.align_params(Space); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2265 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2266 | // Bound the size of the fortran array dimensions. |
| 2267 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2268 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2269 | // As all parameters are known add bounds to them. |
| 2270 | addParameterBounds(); |
| 2271 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2272 | for (ScopStmt &Stmt : *this) |
| 2273 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2274 | // Simplify the schedule according to the context too. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2275 | Schedule = Schedule.gist_domain_params(getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2278 | static isl::set simplifyAssumptionContext(isl::set AssumptionContext, |
| 2279 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2280 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2281 | // simplify the context with the constraints that are needed for anything to |
| 2282 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2283 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2284 | // domains, thus we cannot use the remaining domain to simplify the |
| 2285 | // assumptions. |
| 2286 | if (!S.hasErrorBlock()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2287 | auto DomainParameters = S.getDomains().params(); |
| 2288 | AssumptionContext = AssumptionContext.gist_params(DomainParameters); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2291 | AssumptionContext = AssumptionContext.gist_params(S.getContext()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2292 | return AssumptionContext; |
| 2293 | } |
| 2294 | |
| 2295 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2296 | // The parameter constraints of the iteration domains give us a set of |
| 2297 | // constraints that need to hold for all cases where at least a single |
| 2298 | // statement iteration is executed in the whole scop. We now simplify the |
| 2299 | // assumed context under the assumption that such constraints hold and at |
| 2300 | // least a single statement iteration is executed. For cases where no |
| 2301 | // statement instances are executed, the assumptions we have taken about |
| 2302 | // the executed code do not matter and can be changed. |
| 2303 | // |
| 2304 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2305 | // the set of statement instances that are executed. Otherwise we |
| 2306 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2307 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2308 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2309 | // performed. In such a case, modifying the run-time conditions and |
| 2310 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2311 | // to not be executed. |
| 2312 | // |
| 2313 | // Example: |
| 2314 | // |
| 2315 | // When delinearizing the following code: |
| 2316 | // |
| 2317 | // for (long i = 0; i < 100; i++) |
| 2318 | // for (long j = 0; j < m; j++) |
| 2319 | // A[i+p][j] = 1.0; |
| 2320 | // |
| 2321 | // 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] | 2322 | // 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] | 2323 | // 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] | 2324 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2325 | InvalidContext = InvalidContext.align_params(getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2328 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2329 | static isl::stat |
| 2330 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2331 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2332 | isl::pw_aff LastDimAff; |
| 2333 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2334 | unsigned Pos; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2335 | isl::ctx Ctx = Set.get_ctx(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2336 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2337 | Set = Set.remove_divs(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2338 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2339 | if (isl_set_n_basic_set(Set.get()) >= MaxDisjunctsInDomain) |
| 2340 | return isl::stat::error; |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2341 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2342 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2343 | // lexmax computation will take too long if this number is high. |
| 2344 | // |
| 2345 | // Experiments with a simple test case using an i7 4800MQ: |
| 2346 | // |
| 2347 | // #Parameters involved | Time (in sec) |
| 2348 | // 6 | 0.01 |
| 2349 | // 7 | 0.04 |
| 2350 | // 8 | 0.12 |
| 2351 | // 9 | 0.40 |
| 2352 | // 10 | 1.54 |
| 2353 | // 11 | 6.78 |
| 2354 | // 12 | 30.38 |
| 2355 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2356 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2357 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2358 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2359 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2360 | InvolvedParams++; |
| 2361 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2362 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2363 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 2366 | if (isl_set_n_basic_set(Set.get()) > RunTimeChecksMaxAccessDisjuncts) |
| 2367 | return isl::stat::error; |
| 2368 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2369 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2370 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2371 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2372 | if (isl_ctx_last_error(Ctx.get()) == isl_error_quota) |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2373 | return isl::stat::error; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2374 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2375 | MinPMA = MinPMA.coalesce(); |
| 2376 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2377 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2378 | // Adjust the last dimension of the maximal access by one as we want to |
| 2379 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2380 | // we test during code generation might now point after the end of the |
| 2381 | // allocated array but we will never dereference it anyway. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2382 | assert(MaxPMA.dim(isl::dim::out) && "Assumed at least one output dimension"); |
| 2383 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2384 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2385 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2386 | OneAff = OneAff.add_constant_si(1); |
| 2387 | LastDimAff = LastDimAff.add(OneAff); |
| 2388 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2389 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2390 | MinMaxAccesses.push_back(std::make_pair(MinPMA, MaxPMA)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2391 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2392 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2395 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2396 | isl_set *Domain = MA->getStatement()->getDomain().release(); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2397 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2398 | return isl_set_reset_tuple_id(Domain); |
| 2399 | } |
| 2400 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2401 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2402 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2403 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2404 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2405 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2406 | isl::union_set Domains = S.getDomains(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2407 | isl::union_map Accesses = isl::union_map::empty(S.getParamSpace()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2408 | |
| 2409 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 2410 | Accesses = Accesses.add_map(give(MA->getAccessRelation().release())); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2411 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2412 | Accesses = Accesses.intersect_domain(Domains); |
| 2413 | isl::union_set Locations = Accesses.range(); |
| 2414 | Locations = Locations.coalesce(); |
| 2415 | Locations = Locations.detect_equalities(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2416 | |
| 2417 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2418 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2419 | }; |
| 2420 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2423 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2424 | /// |
| 2425 | ///{ |
| 2426 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2427 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2428 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2429 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2430 | : RN->getNodeAs<BasicBlock>(); |
| 2431 | } |
| 2432 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2433 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2434 | static inline BasicBlock * |
| 2435 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2436 | if (RN->isSubRegion()) { |
| 2437 | assert(idx == 0); |
| 2438 | return RN->getNodeAs<Region>()->getExit(); |
| 2439 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2440 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2443 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2444 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2445 | if (!RN->isSubRegion()) { |
| 2446 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2447 | Loop *L = LI.getLoopFor(BB); |
| 2448 | |
| 2449 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2450 | // they are not part of an actual loop in the control flow graph. |
| 2451 | // Nevertheless, we handle certain unreachable statements that are common |
| 2452 | // when modeling run-time bounds checks as being part of the loop to be |
| 2453 | // able to model them and to later eliminate the run-time bounds checks. |
| 2454 | // |
| 2455 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2456 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2457 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2458 | // allows us to model the following code. |
| 2459 | // |
| 2460 | // for (i = 0; i < N; i++) { |
| 2461 | // if (i > 1024) |
| 2462 | // abort(); <- this abort might be translated to an |
| 2463 | // unreachable |
| 2464 | // |
| 2465 | // A[i] = ... |
| 2466 | // } |
| 2467 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2468 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2469 | return L; |
| 2470 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2471 | |
| 2472 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2473 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2474 | while (L && NonAffineSubRegion->contains(L)) |
| 2475 | L = L->getParentLoop(); |
| 2476 | return L; |
| 2477 | } |
| 2478 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2479 | /// Get the number of blocks in @p L. |
| 2480 | /// |
| 2481 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2482 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2483 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2484 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2485 | /// scop and represent run-time conditions which we want to model and |
| 2486 | /// subsequently speculate away. |
| 2487 | /// |
| 2488 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2489 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2490 | unsigned NumBlocks = L->getNumBlocks(); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2491 | SmallVector<BasicBlock *, 4> ExitBlocks; |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2492 | L->getExitBlocks(ExitBlocks); |
| 2493 | |
| 2494 | for (auto ExitBlock : ExitBlocks) { |
| 2495 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2496 | NumBlocks++; |
| 2497 | } |
| 2498 | return NumBlocks; |
| 2499 | } |
| 2500 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2501 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2502 | if (!RN->isSubRegion()) |
| 2503 | return 1; |
| 2504 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2505 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2506 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2509 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2510 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2511 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2512 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2513 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2514 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2515 | return true; |
| 2516 | return false; |
| 2517 | } |
| 2518 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2519 | ///} |
| 2520 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2521 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 2522 | unsigned Dim, Loop *L) { |
Michael Kruse | 88a2256 | 2016-03-29 07:50:52 +0000 | [diff] [blame] | 2523 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2524 | isl_id *DimId = |
| 2525 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 2526 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 2527 | } |
| 2528 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2529 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2530 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2533 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2534 | auto DIt = DomainMap.find(BB); |
| 2535 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2536 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2537 | |
| 2538 | auto &RI = *R.getRegionInfo(); |
| 2539 | auto *BBR = RI.getRegionFor(BB); |
| 2540 | while (BBR->getEntry() == BB) |
| 2541 | BBR = BBR->getParent(); |
| 2542 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2545 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2546 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2547 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2548 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2549 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2550 | int LD = getRelativeLoopDepth(L); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2551 | auto *S = isl_set_universe(isl_space_set_alloc(getIslCtx().get(), 0, LD + 1)); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2552 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2553 | while (LD-- >= 0) { |
| 2554 | S = addDomainDimId(S, LD + 1, L); |
| 2555 | L = L->getParentLoop(); |
| 2556 | } |
| 2557 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2558 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2559 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2560 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2561 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2562 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2563 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2564 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2565 | return false; |
| 2566 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2567 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2568 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2569 | |
| 2570 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2571 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2572 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2573 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2574 | // will cause problems when building up a ScopStmt for them. |
| 2575 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2576 | // 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] | 2577 | // can themselves not be constructed properly. To this end we will replace |
| 2578 | // the domains of error blocks and those only reachable via error blocks |
| 2579 | // 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] | 2580 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2581 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2582 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2583 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2584 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2585 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2588 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2589 | /// to be compatible to domains constructed for loop @p NewL. |
| 2590 | /// |
| 2591 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2592 | /// edge from @p OldL to @p NewL. |
| 2593 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2594 | __isl_take isl_set *Dom, |
| 2595 | Loop *OldL, Loop *NewL) { |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2596 | // If the loops are the same there is nothing to do. |
| 2597 | if (NewL == OldL) |
| 2598 | return Dom; |
| 2599 | |
| 2600 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2601 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2602 | // If both loops are non-affine loops there is nothing to do. |
| 2603 | if (OldDepth == -1 && NewDepth == -1) |
| 2604 | return Dom; |
| 2605 | |
| 2606 | // Distinguish three cases: |
| 2607 | // 1) The depth is the same but the loops are not. |
| 2608 | // => One loop was left one was entered. |
| 2609 | // 2) The depth increased from OldL to NewL. |
| 2610 | // => One loop was entered, none was left. |
| 2611 | // 3) The depth decreased from OldL to NewL. |
| 2612 | // => Loops were left were difference of the depths defines how many. |
| 2613 | if (OldDepth == NewDepth) { |
| 2614 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2615 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2616 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2617 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2618 | } else if (OldDepth < NewDepth) { |
| 2619 | assert(OldDepth + 1 == NewDepth); |
| 2620 | auto &R = S.getRegion(); |
| 2621 | (void)R; |
| 2622 | assert(NewL->getParentLoop() == OldL || |
| 2623 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2624 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
| 2625 | Dom = addDomainDimId(Dom, NewDepth, NewL); |
| 2626 | } else { |
| 2627 | assert(OldDepth > NewDepth); |
| 2628 | int Diff = OldDepth - NewDepth; |
| 2629 | int NumDim = isl_set_n_dim(Dom); |
| 2630 | assert(NumDim >= Diff); |
| 2631 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2632 | } |
| 2633 | |
| 2634 | return Dom; |
| 2635 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2636 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2637 | bool Scop::propagateInvalidStmtDomains( |
| 2638 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2639 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2640 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2641 | for (auto *RN : RTraversal) { |
| 2642 | |
| 2643 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2644 | // subregions. |
| 2645 | if (RN->isSubRegion()) { |
| 2646 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2647 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2648 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2649 | continue; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2654 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2655 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2656 | assert(Domain && "Cannot propagate a nullptr"); |
| 2657 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2658 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2659 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2660 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2661 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2662 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2663 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2664 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2665 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2666 | isl::set DomPar = Domain.params(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2667 | recordAssumption(ERRORBLOCK, DomPar, BB->getTerminator()->getDebugLoc(), |
| 2668 | AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2669 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2672 | if (InvalidDomain.is_empty()) { |
| 2673 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2674 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2677 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2678 | auto *TI = BB->getTerminator(); |
| 2679 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2680 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2681 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2682 | |
| 2683 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2684 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2685 | continue; |
| 2686 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2687 | // Skip backedges. |
| 2688 | if (DT.dominates(SuccBB, BB)) |
| 2689 | continue; |
| 2690 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2691 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2692 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2693 | auto *AdjustedInvalidDomain = adjustDomainDimensions( |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2694 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2695 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2696 | auto *SuccInvalidDomain = InvalidDomainMap[SuccBB].copy(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2697 | SuccInvalidDomain = |
| 2698 | isl_set_union(SuccInvalidDomain, AdjustedInvalidDomain); |
| 2699 | SuccInvalidDomain = isl_set_coalesce(SuccInvalidDomain); |
| 2700 | unsigned NumConjucts = isl_set_n_basic_set(SuccInvalidDomain); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2701 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2702 | InvalidDomainMap[SuccBB] = isl::manage(SuccInvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2703 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2704 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2705 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2706 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2707 | continue; |
| 2708 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2709 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2710 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2711 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2712 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2713 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2714 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2715 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2716 | |
| 2717 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2720 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2721 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2722 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2723 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2724 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2725 | // domain to the exit block of the region. Otherwise we are done. |
| 2726 | auto *RI = R.getRegionInfo(); |
| 2727 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2728 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2729 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2732 | // 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] | 2733 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2734 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2735 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2736 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2737 | BBLoop->getLoopLatches(LatchBBs); |
| 2738 | for (auto *LatchBB : LatchBBs) |
| 2739 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2740 | return; |
| 2741 | L = L->getParentLoop(); |
| 2742 | } |
| 2743 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2744 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2745 | assert(Domain && "Cannot propagate a nullptr"); |
| 2746 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2747 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2748 | |
| 2749 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2750 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2751 | isl::set AdjustedDomain = isl::manage( |
| 2752 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2753 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2754 | |
| 2755 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2756 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2757 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2758 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2759 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2760 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2761 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2762 | FinishedExitBlocks.insert(ExitBB); |
| 2763 | } |
| 2764 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2765 | bool Scop::buildDomainsWithBranchConstraints( |
| 2766 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2767 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2768 | // To create the domain for each block in R we iterate over all blocks and |
| 2769 | // subregions in R and propagate the conditions under which the current region |
| 2770 | // element is executed. To this end we iterate in reverse post order over R as |
| 2771 | // it ensures that we first visit all predecessors of a region node (either a |
| 2772 | // basic block or a subregion) before we visit the region node itself. |
| 2773 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2774 | // there we propagate the current domain to all successors, however we add the |
| 2775 | // condition that the successor is actually executed next. |
| 2776 | // As we are only interested in non-loop carried constraints here we can |
| 2777 | // simply skip loop back edges. |
| 2778 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2779 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2780 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2781 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2782 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2783 | // subregions. |
| 2784 | if (RN->isSubRegion()) { |
| 2785 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2786 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2787 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2788 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2789 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2790 | continue; |
| 2791 | } |
| 2792 | } |
| 2793 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2794 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2795 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2796 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2797 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2798 | TerminatorInst *TI = BB->getTerminator(); |
| 2799 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2800 | if (isa<UnreachableInst>(TI)) |
| 2801 | continue; |
| 2802 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2803 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2804 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2805 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2806 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2807 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2808 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2809 | // Propagate the domain from BB directly to blocks that have a superset |
| 2810 | // 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] | 2811 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2812 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2813 | |
| 2814 | // If all successors of BB have been set a domain through the propagation |
| 2815 | // above we do not need to build condition sets but can just skip this |
| 2816 | // block. However, it is important to note that this is a local property |
| 2817 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 2818 | // local variable. |
| 2819 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 2820 | return FinishedExitBlocks.count(SuccBB); |
| 2821 | }; |
| 2822 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 2823 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2824 | |
| 2825 | // Build the condition sets for the successor nodes of the current region |
| 2826 | // node. If it is a non-affine subregion we will always execute the single |
| 2827 | // exit node, hence the single entry node domain is the condition set. For |
| 2828 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2829 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2830 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2831 | ConditionSets.push_back(Domain.copy()); |
| 2832 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2833 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2834 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2835 | |
| 2836 | // Now iterate over the successors and set their initial domain based on |
| 2837 | // their condition set. We skip back edges here and have to be careful when |
| 2838 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 2839 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2840 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2841 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2842 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2843 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2844 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2845 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2846 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2847 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2848 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2849 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 2850 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2851 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2852 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2853 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2854 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2855 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2856 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2857 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2858 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2859 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2860 | CondSet = isl::manage( |
| 2861 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2862 | |
| 2863 | // Set the domain for the successor or merge it with an existing domain in |
| 2864 | // case there are multiple paths (without loop back edges) to the |
| 2865 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2866 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 2867 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2868 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2869 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2870 | } else { |
| 2871 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2872 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2873 | SuccDomain = CondSet; |
| 2874 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2875 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2876 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 2877 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2878 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2879 | // In case this happens we will clean up and bail. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2880 | if (isl_set_n_basic_set(SuccDomain.get()) < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2881 | continue; |
| 2882 | |
| 2883 | invalidate(COMPLEXITY, DebugLoc()); |
| 2884 | while (++u < ConditionSets.size()) |
| 2885 | isl_set_free(ConditionSets[u]); |
| 2886 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2887 | } |
| 2888 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2889 | |
| 2890 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2893 | isl::set Scop::getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain, |
| 2894 | DominatorTree &DT, |
| 2895 | LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2896 | // If @p BB is the ScopEntry we are done |
| 2897 | if (R.getEntry() == BB) |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2898 | return isl::set::universe(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2899 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2900 | // The region info of this function. |
| 2901 | auto &RI = *R.getRegionInfo(); |
| 2902 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2903 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2904 | |
| 2905 | // A domain to collect all predecessor domains, thus all conditions under |
| 2906 | // 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] | 2907 | isl::set PredDom = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2908 | |
| 2909 | // Set of regions of which the entry block domain has been propagated to BB. |
| 2910 | // all predecessors inside any of the regions can be skipped. |
| 2911 | SmallSet<Region *, 8> PropagatedRegions; |
| 2912 | |
| 2913 | for (auto *PredBB : predecessors(BB)) { |
| 2914 | // Skip backedges. |
| 2915 | if (DT.dominates(BB, PredBB)) |
| 2916 | continue; |
| 2917 | |
| 2918 | // If the predecessor is in a region we used for propagation we can skip it. |
| 2919 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 2920 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 2921 | PredBBInRegion)) { |
| 2922 | continue; |
| 2923 | } |
| 2924 | |
| 2925 | // Check if there is a valid region we can use for propagation, thus look |
| 2926 | // for a region that contains the predecessor and has @p BB as exit block. |
| 2927 | auto *PredR = RI.getRegionFor(PredBB); |
| 2928 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 2929 | PredR->getParent(); |
| 2930 | |
| 2931 | // If a valid region for propagation was found use the entry of that region |
| 2932 | // for propagation, otherwise the PredBB directly. |
| 2933 | if (PredR->getExit() == BB) { |
| 2934 | PredBB = PredR->getEntry(); |
| 2935 | PropagatedRegions.insert(PredR); |
| 2936 | } |
| 2937 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2938 | auto *PredBBDom = getDomainConditions(PredBB).release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2939 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 2940 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2941 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 2942 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2943 | PredDom = PredDom.unite(isl::manage(PredBBDom)); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | return PredDom; |
| 2947 | } |
| 2948 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2949 | bool Scop::propagateDomainConstraints( |
| 2950 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2951 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2952 | // Iterate over the region R and propagate the domain constrains from the |
| 2953 | // predecessors to the current node. In contrast to the |
| 2954 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 2955 | // information from the predecessors instead of pushing it to the successors. |
| 2956 | // Additionally, we assume the domains to be already present in the domain |
| 2957 | // map here. However, we iterate again in reverse post order so we know all |
| 2958 | // predecessors have been visited before a block or non-affine subregion is |
| 2959 | // visited. |
| 2960 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2961 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2962 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2963 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2964 | // subregions. |
| 2965 | if (RN->isSubRegion()) { |
| 2966 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2967 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2968 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2969 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2970 | continue; |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2975 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 2976 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2977 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2978 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2979 | isl::set PredDom = getPredecessorDomainConstraints(BB, Domain, DT, LI); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2980 | Domain = Domain.intersect(PredDom).coalesce(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2981 | Domain = Domain.align_params(getParamSpace()); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2982 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2983 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2984 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2985 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2986 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2987 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2988 | |
| 2989 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2992 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 2993 | /// |
| 2994 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 2995 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2996 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2997 | /// |
| 2998 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2999 | static __isl_give isl_map * |
| 3000 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 3001 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 3002 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
Tobias Grosser | f4fe34b | 2017-03-16 21:33:20 +0000 | [diff] [blame] | 3003 | 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] | 3004 | if (u != Dim) |
| 3005 | NextIterationMap = |
| 3006 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 3007 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 3008 | C = isl_constraint_set_constant_si(C, 1); |
| 3009 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 3010 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 3011 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 3012 | return NextIterationMap; |
| 3013 | } |
| 3014 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 3015 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 3016 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3017 | int LoopDepth = getRelativeLoopDepth(L); |
| 3018 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3019 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3020 | BasicBlock *HeaderBB = L->getHeader(); |
| 3021 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3022 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3023 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3024 | isl::map NextIterationMap = isl::manage( |
| 3025 | createNextIterationMap(HeaderBBDom.get_space().release(), LoopDepth)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3026 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3027 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3028 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3029 | SmallVector<BasicBlock *, 4> LatchBlocks; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3030 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3031 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3032 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3033 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3034 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 3035 | if (!LatchBBDom) |
| 3036 | continue; |
| 3037 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3038 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3039 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3040 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 3041 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 3042 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 3043 | |
| 3044 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3045 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3046 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 3047 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3048 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3049 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 3050 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3051 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3052 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3053 | // Free the non back edge condition set as we do not need it. |
| 3054 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3055 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3056 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3057 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3058 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3059 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3060 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3061 | BackedgeCondition = BackedgeCondition.project_out( |
| 3062 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3063 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3064 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3065 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3066 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3067 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3068 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3069 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3070 | isl::set UnionBackedgeConditionComplement = |
| 3071 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3072 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3073 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3074 | 0); |
| 3075 | UnionBackedgeConditionComplement = |
| 3076 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3077 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3078 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3079 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3080 | auto Parts = partitionSetParts(HeaderBBDom.copy(), LoopDepth); |
| 3081 | HeaderBBDom = isl::manage(Parts.second); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3082 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3083 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3084 | // the bounded assumptions to the context as they are already implied by the |
| 3085 | // <nsw> tag. |
| 3086 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 3087 | isl_set_free(Parts.first); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3088 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3091 | isl::set UnboundedCtx = isl::manage(Parts.first).params(); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3092 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3093 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3094 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3097 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3098 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3099 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3100 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3101 | if (!PointerBaseInst) |
| 3102 | return nullptr; |
| 3103 | |
| 3104 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3105 | if (!BasePtrStmt) |
| 3106 | return nullptr; |
| 3107 | |
| 3108 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3109 | } |
| 3110 | |
| 3111 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3112 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3113 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3114 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3115 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3116 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3117 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3118 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3119 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3120 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3121 | |
| 3122 | return false; |
| 3123 | } |
| 3124 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3125 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3126 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3127 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3128 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3129 | if (buildAliasGroups(AA)) { |
| 3130 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3131 | // collect statistics so we do it here explicitly. |
| 3132 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3133 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3134 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3135 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3136 | |
| 3137 | // If a problem occurs while building the alias groups we need to delete |
| 3138 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3139 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3140 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3141 | |
| 3142 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3143 | << " could not be created as the number of parameters involved " |
| 3144 | "is too high. The SCoP will be " |
| 3145 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3146 | "the maximal number of parameters but be advised that the " |
| 3147 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3148 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3151 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3152 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3153 | AliasSetTracker AST(AA); |
| 3154 | |
| 3155 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3156 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3157 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3158 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3159 | isl_set *StmtDomain = Stmt.getDomain().release(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3160 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3161 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3164 | if (StmtDomainEmpty) |
| 3165 | continue; |
| 3166 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3167 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3168 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3169 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3170 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3171 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3172 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3173 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3174 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3175 | else |
| 3176 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3177 | AST.add(Acc); |
| 3178 | } |
| 3179 | } |
| 3180 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3181 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3182 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3183 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3184 | continue; |
| 3185 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3186 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3187 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3188 | if (AG.size() < 2) |
| 3189 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3190 | AliasGroups.push_back(std::move(AG)); |
| 3191 | } |
| 3192 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3193 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3194 | } |
| 3195 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3196 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3197 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3198 | AliasGroupTy NewAG; |
| 3199 | AliasGroupTy &AG = AliasGroups[u]; |
| 3200 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3201 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3202 | while (AGI != AG.end()) { |
| 3203 | MemoryAccess *MA = *AGI; |
| 3204 | isl_set *MADomain = getAccessDomain(MA); |
| 3205 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3206 | NewAG.push_back(MA); |
| 3207 | AGI = AG.erase(AGI); |
| 3208 | isl_set_free(MADomain); |
| 3209 | } else { |
| 3210 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3211 | AGI++; |
| 3212 | } |
| 3213 | } |
| 3214 | if (NewAG.size() > 1) |
| 3215 | AliasGroups.push_back(std::move(NewAG)); |
| 3216 | isl_set_free(AGDomain); |
| 3217 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3221 | // To create sound alias checks we perform the following steps: |
| 3222 | // o) We partition each group into read only and non read only accesses. |
| 3223 | // o) For each group with more than one base pointer we then compute minimal |
| 3224 | // and maximal accesses to each array of a group in read only and non |
| 3225 | // read only partitions separately. |
| 3226 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3227 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3228 | |
| 3229 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3230 | |
| 3231 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3232 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3233 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3234 | if (!hasFeasibleRuntimeContext()) |
| 3235 | return false; |
| 3236 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3237 | { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3238 | IslMaxOperationsGuard MaxOpGuard(getIslCtx().get(), OptComputeOut); |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3239 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3240 | if (!Valid) |
| 3241 | return false; |
| 3242 | } |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3243 | if (isl_ctx_last_error(getIslCtx().get()) == isl_error_quota) { |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3244 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3245 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3246 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3247 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3248 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3249 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3252 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3253 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3254 | AliasGroupTy ReadOnlyAccesses; |
| 3255 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3256 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3257 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3258 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3259 | if (AliasGroup.size() < 2) |
| 3260 | return true; |
| 3261 | |
| 3262 | for (MemoryAccess *Access : AliasGroup) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3263 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias", |
| 3264 | Access->getAccessInstruction()) |
| 3265 | << "Possibly aliasing pointer, use restrict keyword."); |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3266 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3267 | if (HasWriteAccess.count(Array)) { |
| 3268 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3269 | ReadWriteAccesses.push_back(Access); |
| 3270 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3271 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3272 | ReadOnlyAccesses.push_back(Access); |
| 3273 | } |
| 3274 | } |
| 3275 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3276 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3277 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3278 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3279 | return true; |
| 3280 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3281 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3282 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3283 | return true; |
| 3284 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3285 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3286 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3287 | for (MemoryAccess *MA : AliasGroup) { |
| 3288 | if (!MA->isAffine()) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3289 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(), |
| 3290 | MA->getAccessInstruction()->getParent()); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3291 | return false; |
| 3292 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3296 | // their base pointers are available. |
| 3297 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3298 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3299 | addRequiredInvariantLoad( |
| 3300 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3301 | } |
| 3302 | |
| 3303 | MinMaxAliasGroups.emplace_back(); |
| 3304 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3305 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3306 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3307 | |
| 3308 | bool Valid; |
| 3309 | |
| 3310 | Valid = |
| 3311 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3312 | |
| 3313 | if (!Valid) |
| 3314 | return false; |
| 3315 | |
| 3316 | // Bail out if the number of values we need to compare is too large. |
| 3317 | // This is important as the number of comparisons grows quadratically with |
| 3318 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3319 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3320 | RunTimeChecksMaxArraysPerGroup) |
| 3321 | return false; |
| 3322 | |
| 3323 | Valid = |
| 3324 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3325 | |
| 3326 | if (!Valid) |
| 3327 | return false; |
| 3328 | |
| 3329 | return true; |
| 3330 | } |
| 3331 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3332 | /// 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] | 3333 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3334 | // Start with the smallest loop containing the entry and expand that |
| 3335 | // loop until it contains all blocks in the region. If there is a loop |
| 3336 | // containing all blocks in the region check if it is itself contained |
| 3337 | // and if so take the parent loop as it will be the smallest containing |
| 3338 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3339 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3340 | while (L) { |
| 3341 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3342 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3343 | AllContained &= L->contains(BB); |
| 3344 | if (AllContained) |
| 3345 | break; |
| 3346 | L = L->getParentLoop(); |
| 3347 | } |
| 3348 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3349 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3352 | int Scop::NextScopID = 0; |
| 3353 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3354 | std::string Scop::CurrentFunc; |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3355 | |
| 3356 | int Scop::getNextID(std::string ParentFunc) { |
| 3357 | if (ParentFunc != CurrentFunc) { |
| 3358 | CurrentFunc = ParentFunc; |
| 3359 | NextScopID = 0; |
| 3360 | } |
| 3361 | return NextScopID++; |
| 3362 | } |
| 3363 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3364 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 3365 | DominatorTree &DT, ScopDetection::DetectionContext &DC, |
| 3366 | OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3367 | : IslCtx(isl_ctx_alloc(), isl_ctx_free), SE(&ScalarEvolution), DT(&DT), |
| 3368 | R(R), name(R.getNameStr()), HasSingleExitEdge(R.getExitingBlock()), |
| 3369 | DC(DC), ORE(ORE), Affinator(this, LI), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3370 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3371 | if (IslOnErrorAbort) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3372 | isl_options_set_on_error(getIslCtx().get(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3373 | buildContext(); |
| 3374 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3375 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3376 | Scop::~Scop() = default; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3377 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3378 | void Scop::foldSizeConstantsToRight() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3379 | isl_union_set *Accessed = isl_union_map_range(getAccesses().release()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3380 | |
| 3381 | for (auto Array : arrays()) { |
| 3382 | if (Array->getNumberOfDimensions() <= 1) |
| 3383 | continue; |
| 3384 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3385 | isl_space *Space = Array->getSpace().release(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3386 | |
| 3387 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3388 | |
| 3389 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3390 | isl_space_free(Space); |
| 3391 | continue; |
| 3392 | } |
| 3393 | |
| 3394 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3395 | |
| 3396 | isl_map *Transform = |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3397 | isl_map_universe(isl_space_map_from_set(Array->getSpace().release())); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3398 | |
| 3399 | std::vector<int> Int; |
| 3400 | |
| 3401 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3402 | for (int i = 0; i < Dims; i++) { |
| 3403 | isl_set *DimOnly = |
| 3404 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3405 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3406 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3407 | |
| 3408 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3409 | |
| 3410 | if (i == Dims - 1) { |
| 3411 | Int.push_back(1); |
| 3412 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3413 | isl_basic_set_free(DimHull); |
| 3414 | continue; |
| 3415 | } |
| 3416 | |
| 3417 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3418 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3419 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3420 | isl_aff_free(Diff); |
| 3421 | |
| 3422 | int ValInt = 1; |
| 3423 | |
Eli Friedman | a75d53c | 2018-01-17 21:59:02 +0000 | [diff] [blame] | 3424 | if (isl_val_is_int(Val)) { |
| 3425 | auto ValAPInt = APIntFromVal(Val); |
| 3426 | if (ValAPInt.isSignedIntN(32)) |
| 3427 | ValInt = ValAPInt.getSExtValue(); |
| 3428 | } else { |
| 3429 | isl_val_free(Val); |
| 3430 | } |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3431 | |
| 3432 | Int.push_back(ValInt); |
| 3433 | |
| 3434 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3435 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3436 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3437 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3438 | Transform = isl_map_add_constraint(Transform, C); |
| 3439 | isl_basic_set_free(DimHull); |
| 3440 | continue; |
| 3441 | } |
| 3442 | |
| 3443 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3444 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3445 | |
| 3446 | int ValInt = 1; |
| 3447 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3448 | ValInt = 0; |
| 3449 | } |
| 3450 | |
| 3451 | Int.push_back(ValInt); |
| 3452 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3453 | isl_basic_set_free(DimHull); |
| 3454 | isl_basic_set_free(ZeroSet); |
| 3455 | } |
| 3456 | |
| 3457 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3458 | |
| 3459 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3460 | isl_set_free(Elements); |
| 3461 | isl_set_free(MappedElements); |
| 3462 | isl_map_free(Transform); |
| 3463 | continue; |
| 3464 | } |
| 3465 | |
| 3466 | isl_set_free(MappedElements); |
| 3467 | |
| 3468 | bool CanFold = true; |
| 3469 | |
| 3470 | if (Int[0] <= 1) |
| 3471 | CanFold = false; |
| 3472 | |
| 3473 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3474 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3475 | if (Int[0] != Int[i] && Int[i]) |
| 3476 | CanFold = false; |
| 3477 | |
| 3478 | if (!CanFold) { |
| 3479 | isl_set_free(Elements); |
| 3480 | isl_map_free(Transform); |
| 3481 | continue; |
| 3482 | } |
| 3483 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3484 | for (auto &Access : AccessFunctions) |
| 3485 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3486 | Access->setAccessRelation(Access->getAccessRelation().apply_range( |
Tobias Grosser | 718d04c | 2018-02-20 07:26:58 +0000 | [diff] [blame^] | 3487 | isl::manage_copy(Transform))); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3488 | |
| 3489 | isl_map_free(Transform); |
| 3490 | |
| 3491 | std::vector<const SCEV *> Sizes; |
| 3492 | for (unsigned i = 0; i < NumDims; i++) { |
| 3493 | auto Size = Array->getDimensionSize(i); |
| 3494 | |
| 3495 | if (i == NumDims - 1) |
| 3496 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3497 | Sizes.push_back(Size); |
| 3498 | } |
| 3499 | |
| 3500 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3501 | |
| 3502 | isl_set_free(Elements); |
| 3503 | } |
| 3504 | isl_union_set_free(Accessed); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3507 | void Scop::markFortranArrays() { |
| 3508 | for (ScopStmt &Stmt : Stmts) { |
| 3509 | for (MemoryAccess *MemAcc : Stmt) { |
| 3510 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3511 | if (!FAD) |
| 3512 | continue; |
| 3513 | |
| 3514 | // TODO: const_cast-ing to edit |
| 3515 | ScopArrayInfo *SAI = |
| 3516 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3517 | assert(SAI && "memory access into a Fortran array does not " |
| 3518 | "have an associated ScopArrayInfo"); |
| 3519 | SAI->applyAndSetFAD(FAD); |
| 3520 | } |
| 3521 | } |
| 3522 | } |
| 3523 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3524 | void Scop::finalizeAccesses() { |
| 3525 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3526 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3527 | foldAccessRelations(); |
| 3528 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3529 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3532 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3533 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3534 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3535 | for (ScopStmt &Stmt : *this) |
| 3536 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3537 | if (!Access->isArrayKind()) |
| 3538 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3539 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3540 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3541 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3542 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3543 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3544 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3545 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3546 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3547 | DivisibleSize /= 2; |
| 3548 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3549 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3552 | for (auto &Stmt : *this) |
| 3553 | for (auto &Access : Stmt) |
| 3554 | Access->updateDimensionality(); |
| 3555 | } |
| 3556 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3557 | void Scop::foldAccessRelations() { |
| 3558 | for (auto &Stmt : *this) |
| 3559 | for (auto &Access : Stmt) |
| 3560 | Access->foldAccessRelation(); |
| 3561 | } |
| 3562 | |
| 3563 | void Scop::assumeNoOutOfBounds() { |
| 3564 | for (auto &Stmt : *this) |
| 3565 | for (auto &Access : Stmt) |
| 3566 | Access->assumeNoOutOfBound(); |
| 3567 | } |
| 3568 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3569 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3570 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3571 | InstStmtMap.erase(Inst); |
| 3572 | |
| 3573 | if (Stmt.isRegionStmt()) { |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3574 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3575 | StmtMap.erase(BB); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3576 | // Skip entry basic block, as its instructions are already deleted as |
| 3577 | // part of the statement's instruction list. |
| 3578 | if (BB == Stmt.getEntryBlock()) |
| 3579 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3580 | for (Instruction &Inst : *BB) |
| 3581 | InstStmtMap.erase(&Inst); |
| 3582 | } |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3583 | } else { |
Michael Kruse | 0c6c555 | 2017-09-01 11:36:52 +0000 | [diff] [blame] | 3584 | auto StmtMapIt = StmtMap.find(Stmt.getBasicBlock()); |
| 3585 | if (StmtMapIt != StmtMap.end()) |
| 3586 | StmtMapIt->second.erase(std::remove(StmtMapIt->second.begin(), |
| 3587 | StmtMapIt->second.end(), &Stmt), |
| 3588 | StmtMapIt->second.end()); |
| 3589 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3590 | InstStmtMap.erase(Inst); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3591 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3592 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3593 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3594 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete) { |
| 3595 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3596 | if (!ShouldDelete(*StmtIt)) { |
| 3597 | StmtIt++; |
| 3598 | continue; |
| 3599 | } |
| 3600 | |
| 3601 | removeFromStmtMap(*StmtIt); |
| 3602 | StmtIt = Stmts.erase(StmtIt); |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | void Scop::removeStmtNotInDomainMap() { |
| 3607 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Tobias Grosser | 199ec4a | 2017-07-19 16:31:10 +0000 | [diff] [blame] | 3608 | return !this->DomainMap.lookup(Stmt.getEntryBlock()); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3609 | }; |
| 3610 | removeStmts(ShouldDelete); |
| 3611 | } |
| 3612 | |
| 3613 | void Scop::simplifySCoP(bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3614 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3615 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3616 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3617 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3618 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3619 | bool OnlyRead = true; |
| 3620 | for (MemoryAccess *MA : Stmt) { |
| 3621 | if (MA->isRead()) |
| 3622 | continue; |
| 3623 | |
| 3624 | OnlyRead = false; |
| 3625 | break; |
| 3626 | } |
| 3627 | |
| 3628 | RemoveStmt = OnlyRead; |
| 3629 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3630 | return RemoveStmt; |
| 3631 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3632 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3633 | removeStmts(ShouldDelete); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3636 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3637 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3638 | if (!LInst) |
| 3639 | return nullptr; |
| 3640 | |
| 3641 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3642 | LInst = cast<LoadInst>(Rep); |
| 3643 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3644 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3645 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3646 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3647 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3648 | continue; |
| 3649 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3650 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3651 | for (auto *MA : MAs) |
| 3652 | if (MA->getAccessInstruction() == Val) |
| 3653 | return &IAClass; |
| 3654 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3655 | |
| 3656 | return nullptr; |
| 3657 | } |
| 3658 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3659 | bool isAParameter(llvm::Value *maybeParam, const Function &F) { |
| 3660 | for (const llvm::Argument &Arg : F.args()) |
| 3661 | if (&Arg == maybeParam) |
| 3662 | return true; |
| 3663 | |
| 3664 | return false; |
Michael Kruse | 594386e | 2017-08-23 12:34:37 +0000 | [diff] [blame] | 3665 | } |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3666 | |
Tobias Grosser | 305d316 | 2017-08-07 00:10:11 +0000 | [diff] [blame] | 3667 | bool Scop::canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
| 3668 | bool MAInvalidCtxIsEmpty, |
| 3669 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3670 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3671 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3672 | if (PollyAllowDereferenceOfAllFunctionParams && |
| 3673 | isAParameter(LInst->getPointerOperand(), getFunction())) |
| 3674 | return true; |
| 3675 | |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3676 | // TODO: We can provide more information for better but more expensive |
| 3677 | // results. |
| 3678 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3679 | LInst->getAlignment(), DL)) |
| 3680 | return false; |
| 3681 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3682 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3683 | // |
Siddharth Bhat | 83fe6b5 | 2017-08-08 12:26:32 +0000 | [diff] [blame] | 3684 | // TODO: This is probably too conservative. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3685 | if (!NonHoistableCtxIsEmpty) |
| 3686 | return false; |
| 3687 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3688 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3689 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3690 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3691 | return true; |
| 3692 | |
| 3693 | // 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] | 3694 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3695 | // statement domain. |
| 3696 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3697 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3698 | return false; |
| 3699 | return true; |
| 3700 | } |
| 3701 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3702 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3703 | if (InvMAs.empty()) |
| 3704 | return; |
| 3705 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 3706 | isl::set StmtInvalidCtx = Stmt.getInvalidContext(); |
| 3707 | bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.is_empty(); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3708 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3709 | // Get the context under which the statement is executed but remove the error |
| 3710 | // context under which this statement is reached. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3711 | isl::set DomainCtx = Stmt.getDomain().params(); |
| 3712 | DomainCtx = DomainCtx.subtract(StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3713 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3714 | if (isl_set_n_basic_set(DomainCtx.get()) >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3715 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3716 | invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3717 | return; |
| 3718 | } |
| 3719 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3720 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3721 | // we could have cyclic dependences on the constraints under which the |
| 3722 | // hoisted loads are executed and we could not determine an order in which to |
| 3723 | // pre-load them. This happens because not only lower bounds are part of the |
| 3724 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3725 | for (auto &InvMA : InvMAs) { |
| 3726 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3727 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3728 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3729 | SetVector<Value *> Values; |
| 3730 | for (const SCEV *Parameter : Parameters) { |
| 3731 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3732 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3733 | if (!Values.count(AccInst)) |
| 3734 | continue; |
| 3735 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3736 | if (isl::id ParamId = getIdForParam(Parameter)) { |
| 3737 | int Dim = DomainCtx.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | b58ed8d | 2017-03-17 09:02:53 +0000 | [diff] [blame] | 3738 | if (Dim >= 0) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3739 | DomainCtx = DomainCtx.eliminate(isl::dim::param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3740 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3741 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3745 | for (auto &InvMA : InvMAs) { |
| 3746 | auto *MA = InvMA.MA; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3747 | isl::set NHCtx = InvMA.NonHoistableCtx; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3748 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3749 | // Check for another invariant access that accesses the same location as |
| 3750 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3751 | // class at the end of InvariantEquivClasses. |
| 3752 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3753 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3754 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3755 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3756 | isl::set MAInvalidCtx = MA->getInvalidContext(); |
| 3757 | bool NonHoistableCtxIsEmpty = NHCtx.is_empty(); |
| 3758 | bool MAInvalidCtxIsEmpty = MAInvalidCtx.is_empty(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3759 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3760 | isl::set MACtx; |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3761 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3762 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3763 | NonHoistableCtxIsEmpty)) { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3764 | MACtx = isl::set::universe(DomainCtx.get_space()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3765 | } else { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3766 | MACtx = DomainCtx; |
| 3767 | MACtx = MACtx.subtract(MAInvalidCtx.unite(NHCtx)); |
| 3768 | MACtx = MACtx.gist_params(getContext()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3771 | bool Consolidated = false; |
| 3772 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3773 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3774 | continue; |
| 3775 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3776 | // If the pointer and the type is equal check if the access function wrt. |
| 3777 | // to the domain is equal too. It can happen that the domain fixes |
| 3778 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3779 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3780 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3781 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3782 | if (!MAs.empty()) { |
| 3783 | auto *LastMA = MAs.front(); |
| 3784 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3785 | isl::set AR = MA->getAccessRelation().range(); |
| 3786 | isl::set LastAR = LastMA->getAccessRelation().range(); |
| 3787 | bool SameAR = AR.is_equal(LastAR); |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3788 | |
| 3789 | if (!SameAR) |
| 3790 | continue; |
| 3791 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3792 | |
| 3793 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3794 | MAs.push_front(MA); |
| 3795 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3796 | Consolidated = true; |
| 3797 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3798 | // Unify the execution context of the class and this statement. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3799 | isl::set IAClassDomainCtx = IAClass.ExecutionContext; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3800 | if (IAClassDomainCtx) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3801 | IAClassDomainCtx = IAClassDomainCtx.unite(MACtx).coalesce(); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3802 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3803 | IAClassDomainCtx = MACtx; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3804 | IAClass.ExecutionContext = IAClassDomainCtx; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3805 | break; |
| 3806 | } |
| 3807 | |
| 3808 | if (Consolidated) |
| 3809 | continue; |
| 3810 | |
| 3811 | // If we did not consolidate MA, thus did not find an equivalence class |
| 3812 | // for it, we create a new one. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3813 | InvariantEquivClasses.emplace_back( |
| 3814 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList{MA}, MACtx, Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3815 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 3818 | /// Check if an access range is too complex. |
| 3819 | /// |
| 3820 | /// An access range is too complex, if it contains either many disjuncts or |
| 3821 | /// very complex expressions. As a simple heuristic, we assume if a set to |
| 3822 | /// be too complex if the sum of existentially quantified dimensions and |
| 3823 | /// set dimensions is larger than a threshold. This reliably detects both |
| 3824 | /// sets with many disjuncts as well as sets with many divisions as they |
| 3825 | /// arise in h264. |
| 3826 | /// |
| 3827 | /// @param AccessRange The range to check for complexity. |
| 3828 | /// |
| 3829 | /// @returns True if the access range is too complex. |
| 3830 | static bool isAccessRangeTooComplex(isl::set AccessRange) { |
| 3831 | unsigned NumTotalDims = 0; |
| 3832 | |
| 3833 | auto CountDimensions = [&NumTotalDims](isl::basic_set BSet) -> isl::stat { |
| 3834 | NumTotalDims += BSet.dim(isl::dim::div); |
| 3835 | NumTotalDims += BSet.dim(isl::dim::set); |
| 3836 | return isl::stat::ok; |
| 3837 | }; |
| 3838 | |
| 3839 | AccessRange.foreach_basic_set(CountDimensions); |
| 3840 | |
| 3841 | if (NumTotalDims > MaxDimensionsInAccessRange) |
| 3842 | return true; |
| 3843 | |
| 3844 | return false; |
| 3845 | } |
| 3846 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3847 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3848 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 3849 | // zero iterators, are by construction invariant, though we |
| 3850 | // currently "hoist" them anyway. This is necessary because we allow |
| 3851 | // them to be treated as parameters (e.g., in conditions) and our code |
| 3852 | // generation would otherwise use the old value. |
| 3853 | |
| 3854 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 3855 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3856 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 3857 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 3858 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3859 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3860 | |
| 3861 | // Skip accesses that have an invariant base pointer which is defined but |
| 3862 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 3863 | // returns a pointer that is used as a base address. However, as we want |
| 3864 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 3865 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 3866 | // that has a base pointer origin has a base pointer that is loaded and |
| 3867 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 3868 | // no base pointer origin we check that the base pointer is defined |
| 3869 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3870 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3871 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3872 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3873 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 3874 | isl::map AccessRelation = give(Access->getAccessRelation().release()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3875 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3876 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3877 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3878 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3879 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3880 | AccessRelation = AccessRelation.intersect_domain(Stmt.getDomain()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3881 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3882 | |
| 3883 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 3884 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 3885 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3886 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3887 | } else if (BB != LI->getParent()) { |
| 3888 | // Skip accesses in non-affine subregions as they might not be executed |
| 3889 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3890 | return nullptr; |
| 3891 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3892 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3893 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3894 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 3895 | if (isAccessRangeTooComplex(AccessRelation.range())) |
| 3896 | return nullptr; |
| 3897 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3898 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 3899 | isl::set WrittenCtx = Written.params(); |
| 3900 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3901 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3902 | if (!IsWritten) |
| 3903 | return WrittenCtx; |
| 3904 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3905 | WrittenCtx = WrittenCtx.remove_divs(); |
| 3906 | bool TooComplex = |
| 3907 | isl_set_n_basic_set(WrittenCtx.get()) >= MaxDisjunctsInDomain; |
| 3908 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3909 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3910 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3911 | addAssumption(INVARIANTLOAD, WrittenCtx, LI->getDebugLoc(), AS_RESTRICTION, |
| 3912 | LI->getParent()); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3913 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3916 | void Scop::verifyInvariantLoads() { |
| 3917 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3918 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3919 | assert(LI && contains(LI)); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3920 | // If there exists a statement in the scop which has a memory access for |
| 3921 | // @p LI, then mark this scop as infeasible for optimization. |
| 3922 | for (ScopStmt &Stmt : Stmts) |
| 3923 | if (Stmt.getArrayAccessOrNULLFor(LI)) { |
| 3924 | invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent()); |
| 3925 | return; |
| 3926 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3930 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3931 | if (!PollyInvariantLoadHoisting) |
| 3932 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3933 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3934 | isl::union_map Writes = getWrites(); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3935 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3936 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3937 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3938 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3939 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
Tobias Grosser | d16f927 | 2017-08-06 17:25:14 +0000 | [diff] [blame] | 3940 | InvariantAccesses.push_back({Access, NHCtx}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3941 | |
| 3942 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 3943 | for (auto InvMA : InvariantAccesses) |
| 3944 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3945 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3946 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3949 | /// Find the canonical scop array info object for a set of invariant load |
| 3950 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 3951 | /// first load in the list of accesses which is used as base pointer of a |
| 3952 | /// scop array. |
| 3953 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 3954 | MemoryAccessList &Accesses) { |
| 3955 | for (MemoryAccess *Access : Accesses) { |
| 3956 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 3957 | Access->getAccessInstruction(), MemoryKind::Array); |
| 3958 | if (CanonicalArray) |
| 3959 | return CanonicalArray; |
| 3960 | } |
| 3961 | return nullptr; |
| 3962 | } |
| 3963 | |
| 3964 | /// Check if @p Array severs as base array in an invariant load. |
| 3965 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 3966 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 3967 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 3968 | if (Access2->getScopArrayInfo() == Array) |
| 3969 | return true; |
| 3970 | return false; |
| 3971 | } |
| 3972 | |
| 3973 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 3974 | /// with a reference to @p New. |
| 3975 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 3976 | const ScopArrayInfo *New) { |
| 3977 | for (ScopStmt &Stmt : *S) |
| 3978 | for (MemoryAccess *Access : Stmt) { |
| 3979 | if (Access->getLatestScopArrayInfo() != Old) |
| 3980 | continue; |
| 3981 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3982 | isl::id Id = New->getBasePtrId(); |
| 3983 | isl::map Map = Access->getAccessRelation(); |
| 3984 | Map = Map.set_tuple_id(isl::dim::out, Id); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3985 | Access->setAccessRelation(Map); |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | void Scop::canonicalizeDynamicBasePtrs() { |
| 3990 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 3991 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 3992 | |
| 3993 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 3994 | findCanonicalArray(this, BasePtrAccesses); |
| 3995 | |
| 3996 | if (!CanonicalBasePtrSAI) |
| 3997 | continue; |
| 3998 | |
| 3999 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 4000 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 4001 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 4002 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 4003 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 4004 | continue; |
| 4005 | |
| 4006 | // we currently do not canonicalize arrays where some accesses are |
| 4007 | // hoisted as invariant loads. If we would, we need to update the access |
| 4008 | // function of the invariant loads as well. However, as this is not a |
| 4009 | // very common situation, we leave this for now to avoid further |
| 4010 | // complexity increases. |
| 4011 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 4012 | continue; |
| 4013 | |
| 4014 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 4015 | } |
| 4016 | } |
| 4017 | } |
| 4018 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4019 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 4020 | ArrayRef<const SCEV *> Sizes, |
| 4021 | MemoryKind Kind, |
| 4022 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4023 | assert((BasePtr || BaseName) && |
| 4024 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 4025 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 4026 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 4027 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4028 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4029 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 4030 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4031 | DL, this, BaseName)); |
| 4032 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4033 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4034 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 4035 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 4036 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 4037 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4038 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 4039 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4040 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 4043 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 4044 | const std::string &BaseName, |
| 4045 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4046 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4047 | std::vector<const SCEV *> SCEVSizes; |
| 4048 | |
| 4049 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4050 | if (size) |
| 4051 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4052 | else |
| 4053 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4054 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4055 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4056 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4057 | return SAI; |
| 4058 | } |
| 4059 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4060 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4061 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4062 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4063 | return SAI; |
| 4064 | } |
| 4065 | |
| 4066 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4067 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4068 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4069 | return SAI; |
| 4070 | } |
| 4071 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4072 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4073 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4074 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4075 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4076 | return AssumedContext.to_str(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4077 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4078 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4079 | std::string Scop::getInvalidContextStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4080 | return InvalidContext.to_str(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4081 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4082 | |
| 4083 | std::string Scop::getNameStr() const { |
| 4084 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4085 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4086 | return EntryName + "---" + ExitName; |
| 4087 | } |
| 4088 | |
| 4089 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4090 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4091 | raw_string_ostream ExitStr(ExitName); |
| 4092 | raw_string_ostream EntryStr(EntryName); |
| 4093 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4094 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4095 | EntryStr.str(); |
| 4096 | |
| 4097 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4098 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4099 | ExitStr.str(); |
| 4100 | } else |
| 4101 | ExitName = "FunctionExit"; |
| 4102 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4103 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4106 | isl::set Scop::getContext() const { return Context; } |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4107 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4108 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4109 | isl::space Scop::getFullParamSpace() const { |
| 4110 | std::vector<isl::id> FortranIDs; |
| 4111 | FortranIDs = getFortranArrayIds(arrays()); |
| 4112 | |
| 4113 | isl::space Space = isl::space::params_alloc( |
| 4114 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 4115 | |
| 4116 | unsigned PDim = 0; |
| 4117 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 4118 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4119 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4120 | } |
| 4121 | |
| 4122 | for (isl::id Id : FortranIDs) |
| 4123 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4124 | |
| 4125 | return Space; |
| 4126 | } |
| 4127 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4128 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4129 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4130 | return AssumedContext; |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4133 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4134 | if (PollyProcessUnprofitable) |
| 4135 | return true; |
| 4136 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4137 | if (isEmpty()) |
| 4138 | return false; |
| 4139 | |
| 4140 | unsigned OptimizableStmtsOrLoops = 0; |
| 4141 | for (auto &Stmt : *this) { |
| 4142 | if (Stmt.getNumIterators() == 0) |
| 4143 | continue; |
| 4144 | |
| 4145 | bool ContainsArrayAccs = false; |
| 4146 | bool ContainsScalarAccs = false; |
| 4147 | for (auto *MA : Stmt) { |
| 4148 | if (MA->isRead()) |
| 4149 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4150 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4151 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4154 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4155 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4156 | } |
| 4157 | |
| 4158 | return OptimizableStmtsOrLoops > 1; |
| 4159 | } |
| 4160 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4161 | bool Scop::hasFeasibleRuntimeContext() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4162 | auto PositiveContext = getAssumedContext(); |
| 4163 | auto NegativeContext = getInvalidContext(); |
| 4164 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 4165 | // addNonEmptyDomainConstraints returns null if ScopStmts have a null domain |
| 4166 | if (!PositiveContext) |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4167 | return false; |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4168 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4169 | bool IsFeasible = !(PositiveContext.is_empty() || |
| 4170 | PositiveContext.is_subset(NegativeContext)); |
| 4171 | if (!IsFeasible) |
| 4172 | return false; |
| 4173 | |
| 4174 | auto DomainContext = getDomains().params(); |
| 4175 | IsFeasible = !DomainContext.is_subset(NegativeContext); |
| 4176 | IsFeasible &= !Context.is_subset(NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4177 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4178 | return IsFeasible; |
| 4179 | } |
| 4180 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4181 | static std::string toString(AssumptionKind Kind) { |
| 4182 | switch (Kind) { |
| 4183 | case ALIASING: |
| 4184 | return "No-aliasing"; |
| 4185 | case INBOUNDS: |
| 4186 | return "Inbounds"; |
| 4187 | case WRAPPING: |
| 4188 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4189 | case UNSIGNED: |
| 4190 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4191 | case COMPLEXITY: |
| 4192 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4193 | case PROFITABLE: |
| 4194 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4195 | case ERRORBLOCK: |
| 4196 | return "No-error"; |
| 4197 | case INFINITELOOP: |
| 4198 | return "Finite loop"; |
| 4199 | case INVARIANTLOAD: |
| 4200 | return "Invariant load"; |
| 4201 | case DELINEARIZATION: |
| 4202 | return "Delinearization"; |
| 4203 | } |
| 4204 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4205 | } |
| 4206 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4207 | bool Scop::isEffectiveAssumption(isl::set Set, AssumptionSign Sign) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4208 | if (Sign == AS_ASSUMPTION) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4209 | if (Context.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4210 | return false; |
| 4211 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4212 | if (AssumedContext.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4213 | return false; |
| 4214 | } else { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4215 | if (Set.is_disjoint(Context)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4216 | return false; |
| 4217 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4218 | if (Set.is_subset(InvalidContext)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4219 | return false; |
| 4220 | } |
| 4221 | return true; |
| 4222 | } |
| 4223 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4224 | bool Scop::trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4225 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4226 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4227 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4228 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4229 | // Do never emit trivial assumptions as they only clutter the output. |
| 4230 | if (!PollyRemarksMinimal) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4231 | isl::set Univ; |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4232 | if (Sign == AS_ASSUMPTION) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4233 | Univ = isl::set::universe(Set.get_space()); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4234 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4235 | bool IsTrivial = (Sign == AS_RESTRICTION && Set.is_empty()) || |
| 4236 | (Sign == AS_ASSUMPTION && Univ.is_equal(Set)); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4237 | |
| 4238 | if (IsTrivial) |
| 4239 | return false; |
| 4240 | } |
| 4241 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4242 | switch (Kind) { |
| 4243 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4244 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4245 | break; |
| 4246 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4247 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4248 | break; |
| 4249 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4250 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4251 | break; |
| 4252 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4253 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4254 | break; |
| 4255 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4256 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4257 | break; |
| 4258 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4259 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4262 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4263 | break; |
| 4264 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4265 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4266 | break; |
| 4267 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4268 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4269 | break; |
| 4270 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4271 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4272 | break; |
| 4273 | } |
| 4274 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4275 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4276 | std::string Msg = toString(Kind) + Suffix + Set.to_str(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4277 | if (BB) |
| 4278 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 4279 | << Msg); |
| 4280 | else |
| 4281 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 4282 | R.getEntry()) |
| 4283 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4284 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4287 | void Scop::addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4288 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4289 | // Simplify the assumptions/restrictions first. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4290 | Set = Set.gist_params(getContext()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4291 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4292 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4293 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4294 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4295 | if (Sign == AS_ASSUMPTION) |
| 4296 | AssumedContext = AssumedContext.intersect(Set).coalesce(); |
| 4297 | else |
| 4298 | InvalidContext = InvalidContext.unite(Set).coalesce(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4301 | void Scop::recordAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4302 | AssumptionSign Sign, BasicBlock *BB) { |
| 4303 | assert((Set.is_params() || BB) && |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4304 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4305 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | void Scop::addRecordedAssumptions() { |
| 4309 | while (!RecordedAssumptions.empty()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4310 | Assumption AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4311 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4312 | if (!AS.BB) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4313 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign, nullptr /* BasicBlock */); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4314 | continue; |
| 4315 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4316 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4317 | // If the domain was deleted the assumptions are void. |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4318 | isl_set *Dom = getDomainConditions(AS.BB).release(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4319 | if (!Dom) |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4320 | continue; |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4321 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4322 | // If a basic block was given use its domain to simplify the assumption. |
| 4323 | // In case of restrictions we know they only have to hold on the domain, |
| 4324 | // thus we can intersect them with the domain of the block. However, for |
| 4325 | // assumptions the domain has to imply them, thus: |
| 4326 | // _ _____ |
| 4327 | // Dom => S <==> A v B <==> A - B |
| 4328 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4329 | // 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] | 4330 | // assumption. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4331 | isl_set *S = AS.Set.copy(); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4332 | if (AS.Sign == AS_RESTRICTION) |
| 4333 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4334 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4335 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4336 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4337 | addAssumption(AS.Kind, isl::manage(S), AS.Loc, AS_RESTRICTION, AS.BB); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4341 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Michael Kruse | 860870b | 2017-08-30 11:52:03 +0000 | [diff] [blame] | 4342 | DEBUG(dbgs() << "Invalidate SCoP because of reason " << Kind << "\n"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4343 | addAssumption(Kind, isl::set::empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4346 | isl::set Scop::getInvalidContext() const { return InvalidContext; } |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4347 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4348 | void Scop::printContext(raw_ostream &OS) const { |
| 4349 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4350 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4351 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4352 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4353 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4354 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4355 | OS.indent(4) << "Invalid Context:\n"; |
| 4356 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4357 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4358 | unsigned Dim = 0; |
| 4359 | for (const SCEV *Parameter : Parameters) |
| 4360 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4363 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4364 | int noOfGroups = 0; |
| 4365 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4366 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4367 | noOfGroups += 1; |
| 4368 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4369 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4372 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4373 | if (MinMaxAliasGroups.empty()) { |
| 4374 | OS.indent(8) << "n/a\n"; |
| 4375 | return; |
| 4376 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4377 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4378 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4379 | |
| 4380 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4381 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4382 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4383 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4384 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4385 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4386 | } |
| 4387 | OS << " ]]\n"; |
| 4388 | } |
| 4389 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4390 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4391 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4392 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4393 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4394 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4395 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4396 | } |
| 4397 | OS << " ]]\n"; |
| 4398 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4402 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4403 | OS << "Statements {\n"; |
| 4404 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4405 | for (const ScopStmt &Stmt : *this) { |
| 4406 | OS.indent(4); |
| 4407 | Stmt.print(OS, PrintInstructions); |
| 4408 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4409 | |
| 4410 | OS.indent(4) << "}\n"; |
| 4411 | } |
| 4412 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4413 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4414 | OS << "Arrays {\n"; |
| 4415 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4416 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4417 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4418 | |
| 4419 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4420 | |
| 4421 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4422 | |
| 4423 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4424 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4425 | |
| 4426 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4429 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4430 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4431 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4432 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4433 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4434 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4435 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4436 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4437 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4438 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4439 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4440 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4441 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4442 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4443 | } |
| 4444 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4445 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4446 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4447 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4448 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4451 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 4452 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4453 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4454 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4455 | isl::ctx Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4456 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4457 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4458 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4459 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4460 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4461 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4462 | // 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] | 4463 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4464 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4465 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4466 | // TODO: We could use a heuristic and either use: |
| 4467 | // SCEVAffinator::takeNonNegativeAssumption |
| 4468 | // or |
| 4469 | // SCEVAffinator::interpretAsUnsigned |
| 4470 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4471 | if (NonNegative) |
| 4472 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4473 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4474 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4475 | |
| 4476 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4477 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4478 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4481 | isl::union_set Scop::getDomains() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4482 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx().get(), 0); |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4483 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4484 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4485 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4486 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4487 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4488 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4491 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4492 | PWACtx PWAC = getPwAff(E, BB); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 4493 | return PWAC.first; |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4496 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4497 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4498 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4499 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4500 | for (ScopStmt &Stmt : *this) { |
| 4501 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4502 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4503 | continue; |
| 4504 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4505 | isl::set Domain = Stmt.getDomain(); |
| 4506 | isl::map AccessDomain = MA->getAccessRelation(); |
| 4507 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 4508 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4509 | } |
| 4510 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 4511 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4512 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4515 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4516 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4519 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4520 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4523 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4524 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4527 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4528 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4531 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4532 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4533 | } |
| 4534 | |
Tobias Grosser | fa03cb7 | 2017-08-17 22:04:53 +0000 | [diff] [blame] | 4535 | isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { |
| 4536 | return getAccessesOfType( |
| 4537 | [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); |
| 4538 | } |
| 4539 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4540 | // Check whether @p Node is an extension node. |
| 4541 | // |
| 4542 | // @return true if @p Node is an extension node. |
| 4543 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4544 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4545 | return isl_bool_error; |
| 4546 | else |
| 4547 | return isl_bool_true; |
| 4548 | } |
| 4549 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4550 | bool Scop::containsExtensionNode(isl::schedule Schedule) { |
| 4551 | return isl_schedule_foreach_schedule_node_top_down( |
| 4552 | Schedule.keep(), isNotExtNode, nullptr) == isl_stat_error; |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4555 | isl::union_map Scop::getSchedule() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4556 | auto Tree = getScheduleTree(); |
| 4557 | if (containsExtensionNode(Tree)) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4558 | return nullptr; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4559 | |
| 4560 | return Tree.get_map(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4561 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4562 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4563 | isl::schedule Scop::getScheduleTree() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4564 | return Schedule.intersect_domain(getDomains()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4565 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4566 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4567 | void Scop::setSchedule(isl::union_map NewSchedule) { |
| 4568 | auto S = isl::schedule::from_domain(getDomains()); |
| 4569 | Schedule = S.insert_partial_schedule( |
| 4570 | isl::multi_union_pw_aff::from_union_map(NewSchedule)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4573 | void Scop::setScheduleTree(isl::schedule NewSchedule) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4574 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4577 | bool Scop::restrictDomains(isl::union_set Domain) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4578 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4579 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4580 | isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); |
| 4581 | isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4582 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4583 | if (StmtDomain.is_subset(NewStmtDomain)) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4584 | continue; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4585 | |
| 4586 | Changed = true; |
| 4587 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4588 | NewStmtDomain = NewStmtDomain.coalesce(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4589 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4590 | if (NewStmtDomain.is_empty()) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4591 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4592 | else |
| 4593 | Stmt.restrictDomain(isl::set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4594 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4595 | return Changed; |
| 4596 | } |
| 4597 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4598 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4599 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4600 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4601 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4602 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4603 | // # Example: |
| 4604 | // |
| 4605 | // Domain: { A[i,j]; B[i,j,k] } |
| 4606 | // N: 1 |
| 4607 | // |
| 4608 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4609 | // |
| 4610 | // @param USet A union set describing the elements for which to generate a |
| 4611 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4612 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4613 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4614 | 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] | 4615 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4616 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4617 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4618 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4619 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4620 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4621 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4622 | int Dim = S.dim(isl::dim::set); |
| 4623 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4624 | N, Dim - N); |
| 4625 | if (N > 1) |
| 4626 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4627 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4628 | Result = Result.add_pw_multi_aff(PMA); |
| 4629 | return isl::stat::ok; |
| 4630 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4631 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4632 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4633 | (void)Res; |
| 4634 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4635 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4636 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4637 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4640 | void Scop::addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, |
| 4641 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4642 | assert(BB && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4643 | Stmts.emplace_back(*this, *BB, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4644 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4645 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4646 | for (Instruction *Inst : Instructions) { |
| 4647 | assert(!InstStmtMap.count(Inst) && |
| 4648 | "Unexpected statement corresponding to the instruction."); |
| 4649 | InstStmtMap[Inst] = Stmt; |
| 4650 | } |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4653 | void Scop::addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4654 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4655 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4656 | Stmts.emplace_back(*this, *R, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4657 | auto *Stmt = &Stmts.back(); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4658 | |
| 4659 | for (Instruction *Inst : Instructions) { |
| 4660 | assert(!InstStmtMap.count(Inst) && |
| 4661 | "Unexpected statement corresponding to the instruction."); |
| 4662 | InstStmtMap[Inst] = Stmt; |
| 4663 | } |
| 4664 | |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4665 | for (BasicBlock *BB : R->blocks()) { |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4666 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4667 | if (BB == R->getEntry()) |
| 4668 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4669 | for (Instruction &Inst : *BB) { |
| 4670 | assert(!InstStmtMap.count(&Inst) && |
| 4671 | "Unexpected statement corresponding to the instruction."); |
| 4672 | InstStmtMap[&Inst] = Stmt; |
| 4673 | } |
| 4674 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4677 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 4678 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4679 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4680 | isl::set SourceDomain = SourceRel.domain(); |
| 4681 | isl::set TargetDomain = TargetRel.domain(); |
| 4682 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4683 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4684 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4685 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4686 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4687 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4688 | CopyStmtsNum++; |
| 4689 | return &(Stmts.back()); |
| 4690 | } |
| 4691 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4692 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4693 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4694 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4695 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4696 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4697 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4700 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4701 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4702 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4703 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4704 | /// |
| 4705 | /// Example: |
| 4706 | /// bb1(0) |
| 4707 | /// / \. |
| 4708 | /// bb2(1) bb3(2) |
| 4709 | /// \ / \. |
| 4710 | /// bb4(3) bb5(4) |
| 4711 | /// \ / |
| 4712 | /// bb6(5) |
| 4713 | /// |
| 4714 | /// Including loops requires additional processing. Whenever a loop header is |
| 4715 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4716 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4717 | /// this loop and complete the sequential schedule at this loop-level before |
| 4718 | /// processing about any other nodes. To implement this |
| 4719 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4720 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4721 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4722 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4723 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4724 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4725 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4726 | |
| 4727 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4728 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4729 | std::deque<RegionNode *> DelayList; |
| 4730 | bool LastRNWaiting = false; |
| 4731 | |
| 4732 | // Iterate over the region @p R in reverse post-order but queue |
| 4733 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4734 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4735 | // that we queued the last sub-region/block from the reverse post-order |
| 4736 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4737 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4738 | // the next queued sub-region/blocks. |
| 4739 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4740 | RegionNode *RN; |
| 4741 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4742 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4743 | RN = WorkList.front(); |
| 4744 | WorkList.pop_front(); |
| 4745 | LastRNWaiting = false; |
| 4746 | } else { |
| 4747 | RN = DelayList.front(); |
| 4748 | DelayList.pop_front(); |
| 4749 | } |
| 4750 | |
| 4751 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4752 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4753 | L = OuterScopLoop; |
| 4754 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4755 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4756 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4757 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4758 | LastRNWaiting = true; |
| 4759 | DelayList.push_back(RN); |
| 4760 | continue; |
| 4761 | } |
| 4762 | LoopStack.push_back({L, nullptr, 0}); |
| 4763 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4764 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4765 | } |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4768 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4769 | if (RN->isSubRegion()) { |
| 4770 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4771 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4772 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4773 | return; |
| 4774 | } |
| 4775 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4776 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4777 | assert(LoopStack.rbegin() != LoopStack.rend()); |
| 4778 | auto LoopData = LoopStack.rbegin(); |
| 4779 | LoopData->NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4780 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4781 | for (auto *Stmt : getStmtListFor(RN)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4782 | isl::union_set UDomain{Stmt->getDomain()}; |
| 4783 | auto StmtSchedule = isl::schedule::from_domain(UDomain); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4784 | LoopData->Schedule = combineInSequence(LoopData->Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4787 | // Check if we just processed the last node in this loop. If we did, finalize |
| 4788 | // the loop by: |
| 4789 | // |
| 4790 | // - adding new schedule dimensions |
| 4791 | // - folding the resulting schedule into the parent loop schedule |
| 4792 | // - dropping the loop schedule from the LoopStack. |
| 4793 | // |
| 4794 | // Then continue to check surrounding loops, which might also have been |
| 4795 | // completed by this node. |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4796 | size_t Dimension = LoopStack.size(); |
| 4797 | while (LoopData->L && |
| 4798 | LoopData->NumBlocksProcessed == getNumBlocksInLoop(LoopData->L)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4799 | isl::schedule Schedule = LoopData->Schedule; |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4800 | auto NumBlocksProcessed = LoopData->NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4801 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4802 | assert(std::next(LoopData) != LoopStack.rend()); |
| 4803 | ++LoopData; |
| 4804 | --Dimension; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4805 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4806 | if (Schedule) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4807 | isl::union_set Domain = Schedule.get_domain(); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4808 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, Dimension); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4809 | Schedule = Schedule.insert_partial_schedule(MUPA); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4810 | LoopData->Schedule = combineInSequence(LoopData->Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4811 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4812 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4813 | LoopData->NumBlocksProcessed += NumBlocksProcessed; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4814 | } |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4815 | // Now pop all loops processed up there from the LoopStack |
| 4816 | LoopStack.erase(LoopStack.begin() + Dimension, LoopStack.end()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4819 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 4820 | auto StmtMapIt = StmtMap.find(BB); |
| 4821 | if (StmtMapIt == StmtMap.end()) |
| 4822 | return {}; |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4823 | return StmtMapIt->second; |
| 4824 | } |
| 4825 | |
Michael Kruse | a230f22 | 2018-01-23 23:56:36 +0000 | [diff] [blame] | 4826 | ScopStmt *Scop::getIncomingStmtFor(const Use &U) const { |
| 4827 | auto *PHI = cast<PHINode>(U.getUser()); |
| 4828 | BasicBlock *IncomingBB = PHI->getIncomingBlock(U); |
| 4829 | |
| 4830 | // If the value is a non-synthesizable from the incoming block, use the |
| 4831 | // statement that contains it as user statement. |
| 4832 | if (auto *IncomingInst = dyn_cast<Instruction>(U.get())) { |
| 4833 | if (IncomingInst->getParent() == IncomingBB) { |
| 4834 | if (ScopStmt *IncomingStmt = getStmtFor(IncomingInst)) |
| 4835 | return IncomingStmt; |
| 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | // Otherwise, use the epilogue/last statement. |
| 4840 | return getLastStmtFor(IncomingBB); |
| 4841 | } |
| 4842 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4843 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 4844 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4845 | if (!StmtList.empty()) |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4846 | return StmtList.back(); |
| 4847 | return nullptr; |
| 4848 | } |
| 4849 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4850 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4851 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4852 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 4853 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4856 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 4857 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4860 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4861 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4862 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4863 | // outermostLoopInRegion always returns nullptr for top level regions |
| 4864 | if (R.isTopLevelRegion()) { |
| 4865 | // LoopInfo's depths start at 1, we start at 0 |
| 4866 | return L->getLoopDepth() - 1; |
| 4867 | } else { |
| 4868 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 4869 | assert(OuterLoop); |
| 4870 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 4871 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4874 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 4875 | for (auto &SAI : arrays()) { |
| 4876 | if (SAI->getName() == BaseName) |
| 4877 | return SAI; |
| 4878 | } |
| 4879 | return nullptr; |
| 4880 | } |
| 4881 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4882 | void Scop::addAccessData(MemoryAccess *Access) { |
| 4883 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 4884 | assert(SAI && "can only use after access relations have been constructed"); |
| 4885 | |
| 4886 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 4887 | ValueUseAccs[SAI].push_back(Access); |
| 4888 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 4889 | PHIIncomingAccs[SAI].push_back(Access); |
| 4890 | } |
| 4891 | |
| 4892 | void Scop::removeAccessData(MemoryAccess *Access) { |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4893 | if (Access->isOriginalValueKind() && Access->isWrite()) { |
| 4894 | ValueDefAccs.erase(Access->getAccessValue()); |
| 4895 | } else if (Access->isOriginalValueKind() && Access->isRead()) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4896 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
| 4897 | std::remove(Uses.begin(), Uses.end(), Access); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4898 | } else if (Access->isOriginalPHIKind() && Access->isRead()) { |
| 4899 | PHINode *PHI = cast<PHINode>(Access->getAccessInstruction()); |
| 4900 | PHIReadAccs.erase(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4901 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 4902 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
| 4903 | std::remove(Incomings.begin(), Incomings.end(), Access); |
| 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 4908 | assert(SAI->isValueKind()); |
| 4909 | |
| 4910 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 4911 | if (!Val) |
| 4912 | return nullptr; |
| 4913 | |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4914 | return ValueDefAccs.lookup(Val); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 4918 | assert(SAI->isValueKind()); |
| 4919 | auto It = ValueUseAccs.find(SAI); |
| 4920 | if (It == ValueUseAccs.end()) |
| 4921 | return {}; |
| 4922 | return It->second; |
| 4923 | } |
| 4924 | |
| 4925 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 4926 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4927 | |
| 4928 | if (SAI->isExitPHIKind()) |
| 4929 | return nullptr; |
| 4930 | |
| 4931 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4932 | return PHIReadAccs.lookup(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 4936 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4937 | auto It = PHIIncomingAccs.find(SAI); |
| 4938 | if (It == PHIIncomingAccs.end()) |
| 4939 | return {}; |
| 4940 | return It->second; |
| 4941 | } |
| 4942 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 4943 | bool Scop::isEscaping(Instruction *Inst) { |
| 4944 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 4945 | "values defined inside the SCoP"); |
| 4946 | |
| 4947 | for (Use &Use : Inst->uses()) { |
| 4948 | BasicBlock *UserBB = getUseBlock(Use); |
| 4949 | if (!contains(UserBB)) |
| 4950 | return true; |
| 4951 | |
| 4952 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 4953 | // move to a new basic block such that its incoming blocks are not in the |
| 4954 | // SCoP anymore. |
| 4955 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 4956 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 4957 | return true; |
| 4958 | } |
| 4959 | return false; |
| 4960 | } |
| 4961 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4962 | Scop::ScopStatistics Scop::getStatistics() const { |
| 4963 | ScopStatistics Result; |
| 4964 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
| 4965 | auto LoopStat = ScopDetection::countBeneficialLoops(&R, *SE, *getLI(), 0); |
| 4966 | |
| 4967 | int NumTotalLoops = LoopStat.NumLoops; |
| 4968 | Result.NumBoxedLoops = getBoxedLoops().size(); |
| 4969 | Result.NumAffineLoops = NumTotalLoops - Result.NumBoxedLoops; |
| 4970 | |
| 4971 | for (const ScopStmt &Stmt : *this) { |
| 4972 | isl::set Domain = Stmt.getDomain().intersect_params(getContext()); |
| 4973 | bool IsInLoop = Stmt.getNumIterators() >= 1; |
| 4974 | for (MemoryAccess *MA : Stmt) { |
| 4975 | if (!MA->isWrite()) |
| 4976 | continue; |
| 4977 | |
| 4978 | if (MA->isLatestValueKind()) { |
| 4979 | Result.NumValueWrites += 1; |
| 4980 | if (IsInLoop) |
| 4981 | Result.NumValueWritesInLoops += 1; |
| 4982 | } |
| 4983 | |
| 4984 | if (MA->isLatestAnyPHIKind()) { |
| 4985 | Result.NumPHIWrites += 1; |
| 4986 | if (IsInLoop) |
| 4987 | Result.NumPHIWritesInLoops += 1; |
| 4988 | } |
| 4989 | |
| 4990 | isl::set AccSet = |
| 4991 | MA->getAccessRelation().intersect_domain(Domain).range(); |
| 4992 | if (AccSet.is_singleton()) { |
| 4993 | Result.NumSingletonWrites += 1; |
| 4994 | if (IsInLoop) |
| 4995 | Result.NumSingletonWritesInLoops += 1; |
| 4996 | } |
| 4997 | } |
| 4998 | } |
| 4999 | #endif |
| 5000 | return Result; |
| 5001 | } |
| 5002 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 5003 | raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { |
| 5004 | scop.print(OS, PollyPrintInstructions); |
| 5005 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5008 | //===----------------------------------------------------------------------===// |
| 5009 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5010 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5011 | AU.addRequired<RegionInfoPass>(); |
| 5012 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5013 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5014 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5015 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5016 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5017 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5018 | AU.setPreservesAll(); |
| 5019 | } |
| 5020 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5021 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 5022 | Scop::ScopStatistics ScopStats) { |
| 5023 | assert(Stats.NumLoops == ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops); |
| 5024 | |
| 5025 | NumScops++; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5026 | NumLoopsInScop += Stats.NumLoops; |
| 5027 | MaxNumLoopsInScop = |
| 5028 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 5029 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5030 | if (Stats.MaxDepth == 1) |
| 5031 | NumScopsDepthOne++; |
| 5032 | else if (Stats.MaxDepth == 2) |
| 5033 | NumScopsDepthTwo++; |
| 5034 | else if (Stats.MaxDepth == 3) |
| 5035 | NumScopsDepthThree++; |
| 5036 | else if (Stats.MaxDepth == 4) |
| 5037 | NumScopsDepthFour++; |
| 5038 | else if (Stats.MaxDepth == 5) |
| 5039 | NumScopsDepthFive++; |
| 5040 | else |
| 5041 | NumScopsDepthLarger++; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5042 | |
| 5043 | NumAffineLoops += ScopStats.NumAffineLoops; |
| 5044 | NumBoxedLoops += ScopStats.NumBoxedLoops; |
| 5045 | |
| 5046 | NumValueWrites += ScopStats.NumValueWrites; |
| 5047 | NumValueWritesInLoops += ScopStats.NumValueWritesInLoops; |
| 5048 | NumPHIWrites += ScopStats.NumPHIWrites; |
| 5049 | NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops; |
| 5050 | NumSingletonWrites += ScopStats.NumSingletonWrites; |
| 5051 | NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5054 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5055 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5056 | |
| 5057 | if (!SD.isMaxRegionInScop(*R)) |
| 5058 | return false; |
| 5059 | |
| 5060 | Function *F = R->getEntry()->getParent(); |
| 5061 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5062 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5063 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5064 | auto const &DL = F->getParent()->getDataLayout(); |
| 5065 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5066 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5067 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5068 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5069 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5070 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5071 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5072 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5073 | if (S) { |
| 5074 | ScopDetection::LoopStats Stats = |
| 5075 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5076 | updateLoopCountStatistic(Stats, S->getStatistics()); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5077 | } |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5078 | #endif |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5079 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5080 | return false; |
| 5081 | } |
| 5082 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5083 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5084 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5085 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5086 | else |
| 5087 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5088 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5089 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5090 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5091 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5092 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5093 | |
| 5094 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5095 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5096 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5097 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5098 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5099 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5100 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5101 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5102 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5103 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5104 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5105 | "Polly - Create polyhedral description of Scops", false, |
| 5106 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5107 | |
| 5108 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5109 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5110 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5111 | AssumptionCache &AC, OptimizationRemarkEmitter &ORE) |
| 5112 | : DL(DL), SD(SD), SE(SE), LI(LI), AA(AA), DT(DT), AC(AC), ORE(ORE) { |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 5113 | recompute(); |
| 5114 | } |
| 5115 | |
| 5116 | void ScopInfo::recompute() { |
| 5117 | RegionToScopMap.clear(); |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5118 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5119 | /// function. |
| 5120 | for (auto &It : SD) { |
| 5121 | Region *R = const_cast<Region *>(It); |
| 5122 | if (!SD.isMaxRegionInScop(*R)) |
| 5123 | continue; |
| 5124 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5125 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5126 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5127 | if (!S) |
| 5128 | continue; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5129 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 5130 | ScopDetection::LoopStats Stats = |
| 5131 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5132 | updateLoopCountStatistic(Stats, S->getStatistics()); |
| 5133 | #endif |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5134 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5135 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5136 | (void)Inserted; |
| 5137 | } |
| 5138 | } |
| 5139 | |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 5140 | bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 5141 | FunctionAnalysisManager::Invalidator &Inv) { |
| 5142 | // Check whether the analysis, all analyses on functions have been preserved |
| 5143 | // or anything we're holding references to is being invalidated |
| 5144 | auto PAC = PA.getChecker<ScopInfoAnalysis>(); |
| 5145 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 5146 | Inv.invalidate<ScopAnalysis>(F, PA) || |
| 5147 | Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || |
| 5148 | Inv.invalidate<LoopAnalysis>(F, PA) || |
| 5149 | Inv.invalidate<AAManager>(F, PA) || |
| 5150 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 5151 | Inv.invalidate<AssumptionAnalysis>(F, PA); |
| 5152 | } |
| 5153 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5154 | AnalysisKey ScopInfoAnalysis::Key; |
| 5155 | |
| 5156 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5157 | FunctionAnalysisManager &FAM) { |
| 5158 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5159 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5160 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5161 | auto &AA = FAM.getResult<AAManager>(F); |
| 5162 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5163 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5164 | auto &DL = F.getParent()->getDataLayout(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5165 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 5166 | return {DL, SD, SE, LI, AA, DT, AC, ORE}; |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5170 | FunctionAnalysisManager &FAM) { |
| 5171 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 5172 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 5173 | // order here to keep the output persistent |
| 5174 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5175 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5176 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5177 | else |
| 5178 | Stream << "Invalid Scop!\n"; |
| 5179 | } |
| 5180 | return PreservedAnalyses::all(); |
| 5181 | } |
| 5182 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5183 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5184 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5185 | AU.addRequired<RegionInfoPass>(); |
| 5186 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5187 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5188 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5189 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5190 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5191 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5192 | AU.setPreservesAll(); |
| 5193 | } |
| 5194 | |
| 5195 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5196 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5197 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5198 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5199 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5200 | auto const &DL = F.getParent()->getDataLayout(); |
| 5201 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5202 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5203 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5204 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5205 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC, ORE}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5206 | return false; |
| 5207 | } |
| 5208 | |
| 5209 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5210 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5211 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5212 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5213 | else |
| 5214 | OS << "Invalid Scop!\n"; |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | char ScopInfoWrapperPass::ID = 0; |
| 5219 | |
| 5220 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5221 | return new ScopInfoWrapperPass(); |
| 5222 | } |
| 5223 | |
| 5224 | INITIALIZE_PASS_BEGIN( |
| 5225 | ScopInfoWrapperPass, "polly-function-scops", |
| 5226 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5227 | false); |
| 5228 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5229 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5230 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5231 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5232 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5233 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5234 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5235 | INITIALIZE_PASS_END( |
| 5236 | ScopInfoWrapperPass, "polly-function-scops", |
| 5237 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5238 | false) |