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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Create a polyhedral description for a static control flow region. |
| 10 | // |
| 11 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 12 | // detection derived from their LLVM-IR code. |
| 13 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 14 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 15 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 19 | #include "polly/ScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | #include "polly/LinkAllPasses.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 21 | #include "polly/Options.h" |
Michael Kruse | 73fa33b | 2016-06-28 01:37:28 +0000 | [diff] [blame] | 22 | #include "polly/ScopBuilder.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 23 | #include "polly/ScopDetection.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 24 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 25 | #include "polly/Support/ISLOStream.h" |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 26 | #include "polly/Support/ISLTools.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" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/PostOrderIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
| 34 | #include "llvm/ADT/SmallSet.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | e0f1541 | 2017-10-09 23:49:08 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/RegionIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 45 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 46 | #include "llvm/IR/ConstantRange.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 47 | #include "llvm/IR/DataLayout.h" |
| 48 | #include "llvm/IR/DebugLoc.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Dominators.h" |
| 50 | #include "llvm/IR/Function.h" |
| 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
| 53 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Module.h" |
| 55 | #include "llvm/IR/PassManager.h" |
| 56 | #include "llvm/IR/Type.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 57 | #include "llvm/IR/Value.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 58 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Compiler.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 60 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 61 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 62 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 63 | #include "isl/aff.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 64 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 65 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 66 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 67 | #include "isl/set.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 68 | #include <cassert> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 69 | |
| 70 | using namespace llvm; |
| 71 | using namespace polly; |
| 72 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 73 | #define DEBUG_TYPE "polly-scops" |
| 74 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 75 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 76 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 77 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 78 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 79 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 80 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 81 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 82 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 83 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 84 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 85 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 86 | "Number of delinearization assumptions taken."); |
| 87 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 88 | STATISTIC(NumScops, "Number of feasible SCoPs after ScopInfo"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 89 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 90 | STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after ScopInfo"); |
| 91 | STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after ScopInfo"); |
| 92 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 93 | STATISTIC(NumScopsDepthZero, "Number of scops with maximal loop depth 0"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 94 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 95 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 96 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 97 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 98 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 99 | STATISTIC(NumScopsDepthLarger, |
| 100 | "Number of scops with maximal loop depth 6 and larger"); |
| 101 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 102 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 103 | STATISTIC(NumValueWrites, "Number of scalar value writes after ScopInfo"); |
| 104 | STATISTIC( |
| 105 | NumValueWritesInLoops, |
| 106 | "Number of scalar value writes nested in affine loops after ScopInfo"); |
| 107 | STATISTIC(NumPHIWrites, "Number of scalar phi writes after ScopInfo"); |
| 108 | STATISTIC(NumPHIWritesInLoops, |
| 109 | "Number of scalar phi writes nested in affine loops after ScopInfo"); |
| 110 | STATISTIC(NumSingletonWrites, "Number of singleton writes after ScopInfo"); |
| 111 | STATISTIC(NumSingletonWritesInLoops, |
| 112 | "Number of singleton writes nested in affine loops after ScopInfo"); |
| 113 | |
Michael Kruse | bb824c6 | 2019-06-12 22:40:08 +0000 | [diff] [blame] | 114 | int const polly::MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 115 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 116 | // The number of disjunct in the context after which we stop to add more |
| 117 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 118 | // number of disjunct when adding non-convex sets to the context. |
| 119 | static int const MaxDisjunctsInContext = 4; |
| 120 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 121 | static cl::opt<bool> PollyRemarksMinimal( |
| 122 | "polly-remarks-minimal", |
| 123 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 124 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 125 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 126 | static cl::opt<bool> |
| 127 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 128 | cl::desc("Abort if an isl error is encountered"), |
| 129 | cl::init(true), cl::cat(PollyCategory)); |
| 130 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 131 | static cl::opt<bool> PollyPreciseInbounds( |
| 132 | "polly-precise-inbounds", |
| 133 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 134 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 135 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 136 | static cl::opt<bool> |
| 137 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 138 | cl::desc("Do not take inbounds assumptions at all"), |
| 139 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 140 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 141 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 142 | "polly-ignore-parameter-bounds", |
| 143 | cl::desc( |
| 144 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 145 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 146 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 147 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 148 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 149 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 150 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 151 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 152 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 153 | bool polly::UseInstructionNames; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 154 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 155 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 156 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 157 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 158 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 159 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 160 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 161 | static cl::opt<bool> PollyPrintInstructions( |
| 162 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 163 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 164 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 165 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 166 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 167 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 168 | int dim, isl::dim type) { |
| 169 | isl::val V; |
| 170 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 171 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 172 | // The upper and lower bound for a parameter value is derived either from |
| 173 | // the data type of the parameter or from the - possibly more restrictive - |
| 174 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 175 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 176 | S = S.lower_bound_val(type, dim, V); |
| 177 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 178 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 179 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 180 | if (Range.isFullSet()) |
| 181 | return S; |
| 182 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 183 | if (S.n_basic_set() > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 184 | return S; |
| 185 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 186 | // In case of signed wrapping, we can refine the set of valid values by |
| 187 | // excluding the part not covered by the wrapping range. |
| 188 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 189 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 190 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 191 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 192 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 193 | V = V.sub_ui(1); |
| 194 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 195 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 196 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 197 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 198 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 201 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 202 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 203 | if (!BasePtrLI) |
| 204 | return nullptr; |
| 205 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 206 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 207 | return nullptr; |
| 208 | |
| 209 | ScalarEvolution &SE = *S->getSE(); |
| 210 | |
| 211 | auto *OriginBaseSCEV = |
| 212 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 213 | if (!OriginBaseSCEV) |
| 214 | return nullptr; |
| 215 | |
| 216 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 217 | if (!OriginBaseSCEVUnknown) |
| 218 | return nullptr; |
| 219 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 220 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 221 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Tobias Grosser | 27db02b | 2017-08-06 17:25:05 +0000 | [diff] [blame] | 224 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 225 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 226 | const DataLayout &DL, Scop *S, |
| 227 | const char *BaseName) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 228 | : BasePtr(BasePtr), ElementType(ElementType), Kind(Kind), DL(DL), S(*S) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 229 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 230 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 231 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 232 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 233 | UseInstructionNames); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 234 | Id = isl::id::alloc(Ctx, BasePtrName, this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 235 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 236 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 237 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 238 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 239 | BasePtrOriginSAI = nullptr; |
| 240 | return; |
| 241 | } |
| 242 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 243 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 244 | if (BasePtrOriginSAI) |
| 245 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 248 | ScopArrayInfo::~ScopArrayInfo() = default; |
| 249 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 250 | isl::space ScopArrayInfo::getSpace() const { |
| 251 | auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); |
| 252 | Space = Space.set_tuple_id(isl::dim::set, Id); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 253 | return Space; |
| 254 | } |
| 255 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 256 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 257 | isl::union_set WriteSet = S.getWrites().range(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 258 | isl::space Space = getSpace(); |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 259 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 260 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 261 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 264 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 265 | if (Array->getElementType() != getElementType()) |
| 266 | return false; |
| 267 | |
| 268 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 269 | return false; |
| 270 | |
| 271 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 272 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 273 | return false; |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 278 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 279 | if (NewElementType == ElementType) |
| 280 | return; |
| 281 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 282 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 283 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 284 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 285 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 286 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 287 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 288 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 289 | ElementType = NewElementType; |
| 290 | } else { |
| 291 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 292 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 293 | } |
| 294 | } |
| 295 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 296 | /// Make the ScopArrayInfo model a Fortran Array |
| 297 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 298 | assert(FAD && "got invalid Fortran array descriptor"); |
| 299 | if (this->FAD) { |
| 300 | assert(this->FAD == FAD && |
| 301 | "receiving different array descriptors for same array"); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 306 | assert(!this->FAD); |
| 307 | this->FAD = FAD; |
| 308 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 309 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 310 | |
| 311 | std::string param_name = getName(); |
| 312 | param_name += "_fortranarr_size"; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 313 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name, this); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 314 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 315 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 316 | isl::pw_aff PwAff = |
| 317 | 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] | 318 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 319 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 322 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 323 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 324 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 325 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 326 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 327 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 328 | if (CheckConsistency) { |
| 329 | for (int i = 0; i < SharedDims; i++) { |
| 330 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 331 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 332 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 333 | return false; |
| 334 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 335 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 336 | if (DimensionSizes.size() >= NewSizes.size()) |
| 337 | return true; |
| 338 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 339 | |
| 340 | DimensionSizes.clear(); |
| 341 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 342 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 343 | DimensionSizesPw.clear(); |
| 344 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 345 | if (!Expr) { |
| 346 | DimensionSizesPw.push_back(nullptr); |
| 347 | continue; |
| 348 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 349 | isl::pw_aff Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 350 | DimensionSizesPw.push_back(Size); |
| 351 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 352 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 355 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 356 | |
| 357 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 358 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 361 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 362 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 363 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 364 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 365 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 366 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 367 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 368 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 369 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 370 | // If this is a Fortran array, then we can print the outermost dimension |
| 371 | // as a isl_pw_aff even though there is no SCEV information. |
| 372 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 373 | |
| 374 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 375 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 376 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 377 | u++; |
| 378 | } |
| 379 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 380 | OS << "["; |
| 381 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 382 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 383 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 384 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 385 | } else { |
| 386 | OS << *getDimensionSize(u); |
| 387 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 388 | |
| 389 | OS << "]"; |
| 390 | } |
| 391 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 392 | OS << ";"; |
| 393 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 394 | if (BasePtrOriginSAI) |
| 395 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 396 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 397 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 401 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 402 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 403 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 404 | return getFromId(Id); |
| 405 | } |
| 406 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 407 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 408 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 409 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 410 | return SAI; |
| 411 | } |
| 412 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 413 | void MemoryAccess::wrapConstantDimensions() { |
| 414 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 415 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 416 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 417 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 418 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 419 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 420 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 421 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 422 | |
| 423 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 424 | for (int i = DimsArray - 1; i > 0; i--) { |
| 425 | auto *DimSize = SAI->getDimensionSize(i); |
| 426 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 427 | |
| 428 | // This transformation is not applicable to dimensions with dynamic size. |
| 429 | if (!DimSizeCst) |
| 430 | continue; |
| 431 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 432 | // This transformation is not applicable to dimensions of size zero. |
| 433 | if (DimSize->isZero()) |
| 434 | continue; |
| 435 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 436 | isl::val DimSizeVal = |
| 437 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 438 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 439 | isl::aff PrevVar = |
| 440 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 441 | |
| 442 | // Compute: index % size |
| 443 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | cb0224a | 2017-08-06 15:56:45 +0000 | [diff] [blame] | 444 | isl::aff Modulo = Var.mod(DimSizeVal); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 445 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 446 | |
| 447 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 448 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 449 | Divide = Divide.floor(); |
| 450 | Divide = Divide.add(PrevVar); |
| 451 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 452 | |
| 453 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 454 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 455 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 459 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 460 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 461 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 462 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 465 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 466 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 467 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 468 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 469 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 470 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 471 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 472 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 473 | auto DimsMissing = DimsArray - DimsAccess; |
| 474 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 475 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 476 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 477 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 478 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 479 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 480 | isl::map Map = isl::map::from_domain_and_range( |
| 481 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 482 | |
| 483 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 484 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 485 | |
| 486 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 487 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 488 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 489 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 490 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 491 | // For the non delinearized arrays, divide the access function of the last |
| 492 | // subscript by the size of the elements in the array. |
| 493 | // |
| 494 | // A stride one array access in C expressed as A[i] is expressed in |
| 495 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 496 | // two subsequent values of 'i' index two values that are stored next to |
| 497 | // each other in memory. By this division we make this characteristic |
| 498 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 499 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 500 | // that divides the offsets of all accesses to this base pointer. |
| 501 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 502 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 503 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 506 | // We currently do this only if we added at least one dimension, which means |
| 507 | // some dimension's indices have not been specified, an indicator that some |
| 508 | // index values have been added together. |
| 509 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 510 | // expressions more complicated. |
| 511 | if (DimsMissing) |
| 512 | wrapConstantDimensions(); |
| 513 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 514 | if (!isAffine()) |
| 515 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 516 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 517 | // Introduce multi-element accesses in case the type loaded by this memory |
| 518 | // access is larger than the canonical element type of the array. |
| 519 | // |
| 520 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 521 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 522 | if (ElemBytes > ArrayElemSize) { |
| 523 | assert(ElemBytes % ArrayElemSize == 0 && |
| 524 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 525 | isl::map Map = isl::map::from_domain_and_range( |
| 526 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 527 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 528 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 529 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 530 | isl::constraint C; |
| 531 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 532 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 533 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 534 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 535 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 536 | C = isl::constraint::alloc_inequality(LS); |
| 537 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 538 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 539 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 540 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 541 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 542 | C = isl::constraint::alloc_inequality(LS); |
| 543 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 544 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 545 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 546 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 547 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 548 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 551 | const std::string |
| 552 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 553 | switch (RT) { |
| 554 | case MemoryAccess::RT_NONE: |
| 555 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 556 | "access which isn't a reduction"); |
| 557 | case MemoryAccess::RT_ADD: |
| 558 | return "+"; |
| 559 | case MemoryAccess::RT_MUL: |
| 560 | return "*"; |
| 561 | case MemoryAccess::RT_BOR: |
| 562 | return "|"; |
| 563 | case MemoryAccess::RT_BXOR: |
| 564 | return "^"; |
| 565 | case MemoryAccess::RT_BAND: |
| 566 | return "&"; |
| 567 | } |
| 568 | llvm_unreachable("Unknown reduction type"); |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 571 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 572 | isl::id ArrayId = getArrayId(); |
| 573 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 574 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 575 | return SAI; |
| 576 | } |
| 577 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 578 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 579 | isl::id ArrayId = getLatestArrayId(); |
| 580 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 581 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 582 | return SAI; |
| 583 | } |
| 584 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 585 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 586 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 589 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 590 | if (!hasNewAccessRelation()) |
| 591 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 592 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 595 | isl::map MemoryAccess::getAddressFunction() const { |
| 596 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 599 | isl::pw_multi_aff |
| 600 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 601 | isl::map Schedule, ScheduledAccRel; |
| 602 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 603 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 604 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 605 | USchedule = USchedule.intersect_domain(UDomain); |
| 606 | Schedule = isl::map::from_union_map(USchedule); |
| 607 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 608 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 611 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 612 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 615 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 616 | return AccessRelation.to_str(); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 619 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 620 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 623 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 624 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 627 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 628 | return NewAccessRelation.to_str(); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 631 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 632 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 635 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 636 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 637 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 638 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 639 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 640 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 641 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 644 | // Formalize no out-of-bound access assumption |
| 645 | // |
| 646 | // When delinearizing array accesses we optimistically assume that the |
| 647 | // delinearized accesses do not access out of bound locations (the subscript |
| 648 | // expression of each array evaluates for each statement instance that is |
| 649 | // executed to a value that is larger than zero and strictly smaller than the |
| 650 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 651 | // dimension for which we do not need to assume any upper bound. At this point |
| 652 | // we formalize this assumption to ensure that at code generation time the |
| 653 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 654 | // |
| 655 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 656 | // first build the set of data locations that are not within array bounds. We |
| 657 | // then apply the reverse access relation to obtain the set of iterations that |
| 658 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 659 | // that are actually executed by intersecting them with the domain of the |
| 660 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 661 | // parameters that may cause statement instances to be executed that may |
| 662 | // possibly yield out of bound memory accesses. The complement of these |
| 663 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 664 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 665 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 666 | if (PollyIgnoreInbounds) |
| 667 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 668 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 669 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 670 | isl::set Outside = isl::set::empty(Space); |
| 671 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 672 | isl::local_space LS(Space); |
| 673 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 674 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 675 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 676 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 677 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 678 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 679 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 680 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 681 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 682 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 685 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 686 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 687 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 688 | |
| 689 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 690 | // Doing so increases the set of parameter combinations that are assumed to |
| 691 | // not appear. This is always save, but may make the resulting run-time check |
| 692 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 693 | Outside = Outside.remove_divs(); |
| 694 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 695 | const auto &Loc = getAccessInstruction() |
| 696 | ? getAccessInstruction()->getDebugLoc() |
| 697 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 698 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 699 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 700 | Statement->getParent()->recordAssumption(INBOUNDS, Outside, Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 701 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 704 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 705 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 706 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 707 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 708 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 709 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 710 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 711 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 712 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 713 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 714 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 715 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 716 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 717 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 718 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 719 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 720 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 721 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 722 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 723 | LengthMap = LengthMap.sum(SubscriptMap); |
| 724 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 725 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 728 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 729 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 730 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 731 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 732 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 733 | return; |
| 734 | |
| 735 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 736 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 737 | return; |
| 738 | |
| 739 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 740 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 741 | return; |
| 742 | |
| 743 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 744 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 745 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 746 | |
| 747 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 748 | if (Range.isFullSet()) |
| 749 | return; |
| 750 | |
Nikita Popov | 6d855ea | 2019-03-27 18:19:33 +0000 | [diff] [blame] | 751 | if (Range.isUpperWrapped() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 752 | return; |
| 753 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 754 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 755 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 756 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 757 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 758 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 759 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 760 | |
| 761 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 762 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 763 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 764 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 765 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 766 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 767 | isl::set AccessRange = Relation.range(); |
| 768 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 769 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 770 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 773 | void MemoryAccess::foldAccessRelation() { |
| 774 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 775 | return; |
| 776 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 777 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 778 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 779 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 780 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 781 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 782 | isl::space Space; |
| 783 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 784 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 785 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 786 | isl::space SpaceSize = DimSize.get_space(); |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 787 | isl::id ParamId = SpaceSize.get_dim_id(isl::dim::param, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 788 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 789 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 790 | Space = Space.range().map_from_set(); |
| 791 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 792 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 793 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 794 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 795 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 796 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 797 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 798 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 799 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 800 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 801 | for (int j = 0; j < Size; ++j) |
| 802 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 803 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 804 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 805 | isl::local_space LS(Space); |
| 806 | isl::constraint C; |
| 807 | C = isl::constraint::alloc_equality(LS); |
| 808 | C = C.set_constant_si(-1); |
| 809 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 810 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 811 | MapTwo = MapTwo.add_constraint(C); |
| 812 | C = isl::constraint::alloc_equality(LS); |
| 813 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 814 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 815 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 816 | MapTwo = MapTwo.add_constraint(C); |
| 817 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 818 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 819 | MapOne = MapOne.unite(MapTwo); |
| 820 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 821 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 822 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 823 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 824 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 825 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 826 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 827 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 828 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 829 | |
| 830 | // Access dimension folding might in certain cases increase the number of |
| 831 | // disjuncts in the memory access, which can possibly complicate the generated |
| 832 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 833 | if (!PollyPreciseFoldAccesses && |
Tobias Grosser | 6ec6e1d | 2018-06-19 08:13:53 +0000 | [diff] [blame] | 834 | NewAccessRelation.n_basic_map() > AccessRelation.n_basic_map()) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 835 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 836 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 837 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 840 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 841 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 842 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 843 | // Initialize the invalid domain which describes all iterations for which the |
| 844 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 845 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 846 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 847 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 848 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 849 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 850 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 851 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 852 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 853 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 854 | return; |
| 855 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 856 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 857 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 858 | // We overapproximate non-affine accesses with a possible access to the |
| 859 | // whole array. For read accesses it does not make a difference, if an |
| 860 | // access must or may happen. However, for write accesses it is important to |
| 861 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 862 | if (AccessRelation.is_null()) |
| 863 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 864 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 865 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 866 | return; |
| 867 | } |
| 868 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 869 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 870 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 871 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 872 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 873 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 874 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 875 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 878 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 879 | AccessRelation = AccessRelation.set_tuple_id( |
| 880 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 881 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 882 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 883 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 884 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 885 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 886 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 887 | AccessType AccType, Value *BaseAddress, |
| 888 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 889 | ArrayRef<const SCEV *> Subscripts, |
| 890 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 891 | MemoryKind Kind) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 892 | : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(nullptr), |
| 893 | BaseAddr(BaseAddress), ElementType(ElementType), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 894 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 895 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 896 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 897 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 898 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 899 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 900 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 901 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 902 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 903 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 904 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 905 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 906 | : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), |
| 907 | InvalidDomain(nullptr), AccessRelation(nullptr), |
| 908 | NewAccessRelation(AccRel), FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 909 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 910 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 911 | Sizes.push_back(nullptr); |
| 912 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 913 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 914 | ElementType = SAI->getElementType(); |
| 915 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 916 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 917 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 918 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 919 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 920 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 923 | MemoryAccess::~MemoryAccess() = default; |
| 924 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 925 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 926 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 927 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 928 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 931 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 932 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 933 | } |
| 934 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 935 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 936 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 937 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 938 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 939 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 940 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 941 | else |
| 942 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 943 | return OS; |
| 944 | } |
| 945 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 946 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 947 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 948 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 949 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 950 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 951 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 952 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 953 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 954 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 955 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 956 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 957 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 958 | break; |
| 959 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 960 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 961 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 962 | |
| 963 | if (FAD) { |
| 964 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 965 | OS << "] "; |
| 966 | }; |
| 967 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 968 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 969 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 970 | if (hasNewAccessRelation()) |
| 971 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 974 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 975 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 976 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 977 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 978 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 979 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 980 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 981 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 982 | StmtDom = StmtDom.reset_tuple_id(); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 983 | isl::set NewInvalidDom = StmtDom.intersect(PWAC.second); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 984 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 985 | return PWAC.first; |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 988 | // Create a map in the size of the provided set domain, that maps from the |
| 989 | // one element of the provided set domain to another element of the provided |
| 990 | // set domain. |
| 991 | // The mapping is limited to all points that are equal in all but the last |
| 992 | // dimension and for which the last dimension of the input is strict smaller |
| 993 | // than the last dimension of the output. |
| 994 | // |
| 995 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 996 | // |
| 997 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 998 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 999 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1000 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1001 | isl::space Space = SetDomain.map_from_set(); |
| 1002 | isl::map Map = isl::map::universe(Space); |
| 1003 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1004 | |
| 1005 | // Set all but the last dimension to be equal for the input and output |
| 1006 | // |
| 1007 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1008 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1009 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1010 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1011 | |
| 1012 | // Set the last dimension of the input to be strict smaller than the |
| 1013 | // last dimension of the output. |
| 1014 | // |
| 1015 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1016 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1017 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1020 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1021 | isl::map AccessRelation = getAccessRelation(); |
| 1022 | isl::space Space = Schedule.get_space().range(); |
| 1023 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1024 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1025 | Schedule = Schedule.reverse(); |
| 1026 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1027 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1028 | NextScatt = NextScatt.apply_range(Schedule); |
| 1029 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1030 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1031 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1032 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1033 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1034 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1037 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1038 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1039 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1040 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1041 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1042 | StrideX = isl::set::universe(Stride.get_space()); |
| 1043 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1044 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1045 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1046 | StrideWidth); |
| 1047 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1048 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1049 | return IsStrideX; |
| 1050 | } |
| 1051 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1052 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1053 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1056 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1057 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1060 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1061 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1064 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1065 | assert(NewAccess); |
| 1066 | |
| 1067 | #ifndef NDEBUG |
| 1068 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1069 | isl::space NewSpace = NewAccess.get_space(); |
| 1070 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1071 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1072 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1073 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1074 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1075 | // subdomain only. |
| 1076 | if (isRead()) { |
| 1077 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1078 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1079 | StmtDomain = |
| 1080 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1081 | isl::set NewDomain = NewAccess.domain(); |
| 1082 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1083 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1084 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1085 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1086 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1087 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1088 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1089 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1090 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1091 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1092 | |
| 1093 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1094 | InvariantEquivClassTy *EqClass = |
| 1095 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1096 | SAI->getBasePtr()); |
| 1097 | assert(EqClass && |
| 1098 | "Access functions to indirect arrays must have an invariant and " |
| 1099 | "hoisted base pointer"); |
| 1100 | } |
| 1101 | |
| 1102 | // Check whether access dimensions correspond to number of dimensions of the |
| 1103 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1104 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1105 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1106 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1107 | #endif |
| 1108 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1109 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1110 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1111 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1112 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1113 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1114 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1115 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1116 | |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 1117 | return !StmtDom.is_subset(AccDom); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1120 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1121 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1122 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1123 | isl::set Domain = getDomain(); |
| 1124 | if (Domain.is_empty()) |
| 1125 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1126 | auto Schedule = getParent()->getSchedule(); |
| 1127 | if (!Schedule) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1128 | return nullptr; |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1129 | Schedule = Schedule.intersect_domain(isl::union_set(Domain)); |
| 1130 | if (Schedule.is_empty()) |
| 1131 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1132 | isl::map M = M.from_union_map(Schedule); |
| 1133 | M = M.coalesce(); |
| 1134 | M = M.gist_domain(Domain); |
| 1135 | M = M.coalesce(); |
| 1136 | return M; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1137 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1138 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1139 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1140 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1141 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1142 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1145 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1146 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1147 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1148 | if (Access->isArrayKind()) { |
| 1149 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1150 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1151 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1152 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1153 | assert(!ValueWrites.lookup(AccessVal)); |
| 1154 | |
| 1155 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1156 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1157 | Value *AccessVal = Access->getAccessValue(); |
| 1158 | assert(!ValueReads.lookup(AccessVal)); |
| 1159 | |
| 1160 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1161 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1162 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1163 | assert(!PHIWrites.lookup(PHI)); |
| 1164 | |
| 1165 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1166 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1167 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1168 | assert(!PHIReads.lookup(PHI)); |
| 1169 | |
| 1170 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1173 | if (Prepend) { |
| 1174 | MemAccs.insert(MemAccs.begin(), Access); |
| 1175 | return; |
| 1176 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1177 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1180 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1181 | for (MemoryAccess *MA : *this) |
| 1182 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1183 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1184 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1185 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1186 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1189 | ScopStmt::ScopStmt(Scop &parent, Region &R, StringRef Name, |
| 1190 | Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1191 | std::vector<Instruction *> EntryBlockInstructions) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1192 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), R(&R), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1193 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1194 | Instructions(EntryBlockInstructions) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1195 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1196 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, |
| 1197 | Loop *SurroundingLoop, |
| 1198 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1199 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1200 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1201 | Instructions(Instructions) {} |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1202 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1203 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1204 | isl::set NewDomain) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1205 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), |
| 1206 | Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1207 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1208 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1209 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1210 | Domain = Domain.set_tuple_id(Id); |
| 1211 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1212 | auto *Access = |
| 1213 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1214 | parent.addAccessFunction(Access); |
| 1215 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1216 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1217 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1218 | parent.addAccessFunction(Access); |
| 1219 | addAccess(Access); |
| 1220 | } |
| 1221 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1222 | ScopStmt::~ScopStmt() = default; |
| 1223 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1224 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1225 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1226 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1227 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1228 | if (!S) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1229 | return {}; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1230 | auto Str = stringFromIslObj(S); |
| 1231 | isl_map_free(S); |
| 1232 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1235 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1236 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1237 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1238 | if (isBlockStmt()) |
| 1239 | return getBasicBlock(); |
| 1240 | return getRegion()->getEntry(); |
| 1241 | } |
| 1242 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1243 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1244 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1245 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1246 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1247 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1248 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1251 | isl::ctx ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1252 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1253 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1254 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1255 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1256 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1257 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1258 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1259 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1260 | OS << "Instructions {\n"; |
| 1261 | |
| 1262 | for (Instruction *Inst : Instructions) |
| 1263 | OS.indent(16) << *Inst << "\n"; |
| 1264 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1265 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1268 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1269 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1270 | OS.indent(12) << "Domain :=\n"; |
| 1271 | |
| 1272 | if (Domain) { |
| 1273 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1274 | } else |
| 1275 | OS.indent(16) << "n/a\n"; |
| 1276 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1277 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1278 | |
| 1279 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1280 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1281 | } else |
| 1282 | OS.indent(16) << "n/a\n"; |
| 1283 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1284 | for (MemoryAccess *Access : MemAccs) |
| 1285 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1286 | |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1287 | if (PrintInstructions) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1288 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1291 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1292 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1293 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1294 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1295 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1296 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1297 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1298 | (void)Found; |
| 1299 | assert(Found && "Expected access data not found"); |
| 1300 | } |
| 1301 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1302 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1303 | (void)Found; |
| 1304 | assert(Found && "Expected access data not found"); |
| 1305 | } |
| 1306 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1307 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1308 | (void)Found; |
| 1309 | assert(Found && "Expected access data not found"); |
| 1310 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1311 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1312 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1313 | (void)Found; |
| 1314 | assert(Found && "Expected access data not found"); |
| 1315 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1318 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1319 | // Remove the memory accesses from this statement together with all scalar |
| 1320 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1321 | // instruction, hence would not be removed by this function. However, it is |
| 1322 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1323 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1324 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1325 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1326 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1327 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1328 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1329 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1330 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1331 | Parent.removeAccessData(MA); |
| 1332 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1333 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1334 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1335 | MemAccs.end()); |
| 1336 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1339 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting) { |
| 1340 | if (AfterHoisting) { |
| 1341 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 1342 | assert(MAIt != MemAccs.end()); |
| 1343 | MemAccs.erase(MAIt); |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1344 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1345 | removeAccessData(MA); |
| 1346 | Parent.removeAccessData(MA); |
| 1347 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1348 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1349 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 1350 | if (It != InstructionToAccess.end()) { |
| 1351 | It->second.remove(MA); |
| 1352 | if (It->second.empty()) |
| 1353 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1354 | } |
| 1355 | } |
| 1356 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 1357 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 1358 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 1359 | if (Access) |
| 1360 | return Access; |
| 1361 | |
| 1362 | ScopArrayInfo *SAI = |
| 1363 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 1364 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 1365 | true, {}, {}, V, MemoryKind::Value); |
| 1366 | Parent.addAccessFunction(Access); |
| 1367 | Access->buildAccessRelation(SAI); |
| 1368 | addAccess(Access); |
| 1369 | Parent.addAccessData(Access); |
| 1370 | return Access; |
| 1371 | } |
| 1372 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1373 | raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { |
| 1374 | S.print(OS, PollyPrintInstructions); |
| 1375 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1378 | //===----------------------------------------------------------------------===// |
| 1379 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1380 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1381 | void Scop::setContext(isl::set NewContext) { |
| 1382 | Context = NewContext.align_params(Context.get_space()); |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1385 | namespace { |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1386 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1387 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1388 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1389 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1390 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1391 | |
| 1392 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1393 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 1394 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1395 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1396 | |
| 1397 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1398 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1399 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 1400 | return SSPR.visit(E); |
| 1401 | } |
| 1402 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1403 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 1404 | auto *Start = visit(E->getStart()); |
| 1405 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 1406 | visit(E->getStepRecurrence(SE)), |
| 1407 | E->getLoop(), SCEV::FlagAnyWrap); |
| 1408 | return SE.getAddExpr(Start, AddRec); |
| 1409 | } |
| 1410 | |
| 1411 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 1412 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 1413 | return SE.getUnknown(NewValue); |
| 1414 | return E; |
| 1415 | } |
| 1416 | }; |
| 1417 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1418 | /// Check whether we should remap a SCEV expression. |
| 1419 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1420 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1421 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1422 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1423 | |
| 1424 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1425 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 1426 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1427 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 1428 | |
| 1429 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1430 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1431 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 1432 | SFIS.visitAll(E); |
| 1433 | return SFIS.FoundInside; |
| 1434 | } |
| 1435 | |
| 1436 | bool follow(const SCEV *E) { |
| 1437 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 1438 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 1439 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 1440 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 1441 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 1442 | } |
| 1443 | return !FoundInside; |
| 1444 | } |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1445 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1446 | bool isDone() { return FoundInside; } |
| 1447 | }; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1448 | } // end anonymous namespace |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1449 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1450 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1451 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 1452 | // doesn't like addition between an AddRec and an expression that |
| 1453 | // doesn't have a dominance relationship with it.) |
| 1454 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 1455 | return E; |
| 1456 | |
| 1457 | // Rewrite SCEV. |
| 1458 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1461 | // This table of function names is used to translate parameter names in more |
| 1462 | // human-readable names. This makes it easier to interpret Polly analysis |
| 1463 | // results. |
| 1464 | StringMap<std::string> KnownNames = { |
| 1465 | {"_Z13get_global_idj", "global_id"}, |
| 1466 | {"_Z12get_local_idj", "local_id"}, |
| 1467 | {"_Z15get_global_sizej", "global_size"}, |
| 1468 | {"_Z14get_local_sizej", "local_size"}, |
| 1469 | {"_Z12get_work_dimv", "work_dim"}, |
| 1470 | {"_Z17get_global_offsetj", "global_offset"}, |
| 1471 | {"_Z12get_group_idj", "group_id"}, |
| 1472 | {"_Z14get_num_groupsj", "num_groups"}, |
| 1473 | }; |
| 1474 | |
| 1475 | static std::string getCallParamName(CallInst *Call) { |
| 1476 | std::string Result; |
| 1477 | raw_string_ostream OS(Result); |
| 1478 | std::string Name = Call->getCalledFunction()->getName(); |
| 1479 | |
| 1480 | auto Iterator = KnownNames.find(Name); |
| 1481 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 1482 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1483 | OS << Name; |
| 1484 | for (auto &Operand : Call->arg_operands()) { |
| 1485 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 1486 | OS << "_" << Op->getValue(); |
| 1487 | } |
| 1488 | OS.flush(); |
| 1489 | return Result; |
| 1490 | } |
| 1491 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1492 | void Scop::createParameterId(const SCEV *Parameter) { |
| 1493 | assert(Parameters.count(Parameter)); |
| 1494 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1495 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1496 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1497 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1498 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1499 | Value *Val = ValueParameter->getValue(); |
| 1500 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1501 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1502 | if (Call && isConstCall(Call)) { |
| 1503 | ParameterName = getCallParamName(Call); |
| 1504 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1505 | // If this parameter references a specific Value and this value has a name |
| 1506 | // we use this name as it is likely to be unique and more useful than just |
| 1507 | // a number. |
| 1508 | if (Val->hasName()) |
| 1509 | ParameterName = Val->getName(); |
| 1510 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 1511 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 1512 | if (LoadOrigin->hasName()) { |
| 1513 | ParameterName += "_loaded_from_"; |
| 1514 | ParameterName += |
| 1515 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 1516 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1519 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1520 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 1521 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 1522 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1523 | isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 1524 | const_cast<void *>((const void *)Parameter)); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1525 | ParameterIds[Parameter] = Id; |
| 1526 | } |
| 1527 | |
| 1528 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 1529 | for (const SCEV *Parameter : NewParameters) { |
| 1530 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1531 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 1532 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 1533 | |
| 1534 | if (Parameters.insert(Parameter)) |
| 1535 | createParameterId(Parameter); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 1539 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1540 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1541 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 1542 | return ParameterIds.lookup(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1543 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1544 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 1545 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 1546 | return DT.dominates(BB, getEntry()); |
| 1547 | } |
| 1548 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1549 | void Scop::buildContext() { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1550 | isl::space Space = isl::space::params_alloc(getIslCtx(), 0); |
| 1551 | Context = isl::set::universe(Space); |
| 1552 | InvalidContext = isl::set::empty(Space); |
| 1553 | AssumedContext = isl::set::universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1556 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1557 | unsigned PDim = 0; |
| 1558 | for (auto *Parameter : Parameters) { |
| 1559 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1560 | Context = addRangeBoundsToSet(Context, SRange, PDim++, isl::dim::param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1564 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 1565 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1566 | for (auto Array : Arrays) { |
| 1567 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 1568 | // for its outermost dimension. Fortran arrays will have this since the |
| 1569 | // outermost dimension size can be picked up from their runtime description. |
| 1570 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 1571 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1572 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1573 | if (!PwAff) |
| 1574 | continue; |
| 1575 | |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 1576 | isl::id Id = PwAff.get_dim_id(isl::dim::param, 0); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1577 | assert(!Id.is_null() && |
| 1578 | "Invalid Id for PwAff expression in Fortran array"); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1579 | OutermostSizeIds.push_back(Id); |
| 1580 | } |
| 1581 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1582 | return OutermostSizeIds; |
| 1583 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1584 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1585 | // The FORTRAN array size parameters are known to be non-negative. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1586 | static isl::set boundFortranArrayParams(isl::set Context, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1587 | Scop::array_range Arrays) { |
| 1588 | std::vector<isl::id> OutermostSizeIds; |
| 1589 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1590 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1591 | for (isl::id Id : OutermostSizeIds) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1592 | int dim = Context.find_dim_by_id(isl::dim::param, Id); |
| 1593 | Context = Context.lower_bound_si(isl::dim::param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | return Context; |
| 1597 | } |
| 1598 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1599 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 1600 | if (PollyIgnoreParamBounds) |
| 1601 | return; |
| 1602 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1603 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1604 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Align the parameters of all data structures to the model. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1607 | Context = Context.align_params(Space); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1608 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1609 | // Bound the size of the fortran array dimensions. |
| 1610 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 1611 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 1612 | // As all parameters are known add bounds to them. |
| 1613 | addParameterBounds(); |
| 1614 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1615 | for (ScopStmt &Stmt : *this) |
| 1616 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 1617 | // Simplify the schedule according to the context too. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1618 | Schedule = Schedule.gist_domain_params(getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1621 | static isl::set simplifyAssumptionContext(isl::set AssumptionContext, |
| 1622 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 1623 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 1624 | // simplify the context with the constraints that are needed for anything to |
| 1625 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 1626 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 1627 | // domains, thus we cannot use the remaining domain to simplify the |
| 1628 | // assumptions. |
| 1629 | if (!S.hasErrorBlock()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1630 | auto DomainParameters = S.getDomains().params(); |
| 1631 | AssumptionContext = AssumptionContext.gist_params(DomainParameters); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1634 | AssumptionContext = AssumptionContext.gist_params(S.getContext()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 1635 | return AssumptionContext; |
| 1636 | } |
| 1637 | |
| 1638 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1639 | // The parameter constraints of the iteration domains give us a set of |
| 1640 | // constraints that need to hold for all cases where at least a single |
| 1641 | // statement iteration is executed in the whole scop. We now simplify the |
| 1642 | // assumed context under the assumption that such constraints hold and at |
| 1643 | // least a single statement iteration is executed. For cases where no |
| 1644 | // statement instances are executed, the assumptions we have taken about |
| 1645 | // the executed code do not matter and can be changed. |
| 1646 | // |
| 1647 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 1648 | // the set of statement instances that are executed. Otherwise we |
| 1649 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1650 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1651 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1652 | // performed. In such a case, modifying the run-time conditions and |
| 1653 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1654 | // to not be executed. |
| 1655 | // |
| 1656 | // Example: |
| 1657 | // |
| 1658 | // When delinearizing the following code: |
| 1659 | // |
| 1660 | // for (long i = 0; i < 100; i++) |
| 1661 | // for (long j = 0; j < m; j++) |
| 1662 | // A[i+p][j] = 1.0; |
| 1663 | // |
| 1664 | // 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] | 1665 | // 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] | 1666 | // 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] | 1667 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1668 | InvalidContext = InvalidContext.align_params(getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1671 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1672 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1675 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 1676 | auto DIt = DomainMap.find(BB); |
| 1677 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 1678 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 1679 | |
| 1680 | auto &RI = *R.getRegionInfo(); |
| 1681 | auto *BBR = RI.getRegionFor(BB); |
| 1682 | while (BBR->getEntry() == BB) |
| 1683 | BBR = BBR->getParent(); |
| 1684 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 1687 | int Scop::NextScopID = 0; |
| 1688 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1689 | std::string Scop::CurrentFunc; |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 1690 | |
| 1691 | int Scop::getNextID(std::string ParentFunc) { |
| 1692 | if (ParentFunc != CurrentFunc) { |
| 1693 | CurrentFunc = ParentFunc; |
| 1694 | NextScopID = 0; |
| 1695 | } |
| 1696 | return NextScopID++; |
| 1697 | } |
| 1698 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 1699 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1700 | DominatorTree &DT, ScopDetection::DetectionContext &DC, |
| 1701 | OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1702 | : IslCtx(isl_ctx_alloc(), isl_ctx_free), SE(&ScalarEvolution), DT(&DT), |
Philip Pfaffe | d477bb9 | 2018-05-15 14:53:25 +0000 | [diff] [blame] | 1703 | R(R), name(None), HasSingleExitEdge(R.getExitingBlock()), DC(DC), |
| 1704 | ORE(ORE), Affinator(this, LI), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 1705 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 1706 | if (IslOnErrorAbort) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1707 | isl_options_set_on_error(getIslCtx().get(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 1708 | buildContext(); |
| 1709 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1710 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1711 | Scop::~Scop() = default; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1712 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1713 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1714 | for (Instruction *Inst : Stmt.getInstructions()) |
| 1715 | InstStmtMap.erase(Inst); |
| 1716 | |
| 1717 | if (Stmt.isRegionStmt()) { |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 1718 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1719 | StmtMap.erase(BB); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1720 | // Skip entry basic block, as its instructions are already deleted as |
| 1721 | // part of the statement's instruction list. |
| 1722 | if (BB == Stmt.getEntryBlock()) |
| 1723 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 1724 | for (Instruction &Inst : *BB) |
| 1725 | InstStmtMap.erase(&Inst); |
| 1726 | } |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1727 | } else { |
Michael Kruse | 0c6c555 | 2017-09-01 11:36:52 +0000 | [diff] [blame] | 1728 | auto StmtMapIt = StmtMap.find(Stmt.getBasicBlock()); |
| 1729 | if (StmtMapIt != StmtMap.end()) |
| 1730 | StmtMapIt->second.erase(std::remove(StmtMapIt->second.begin(), |
| 1731 | StmtMapIt->second.end(), &Stmt), |
| 1732 | StmtMapIt->second.end()); |
| 1733 | for (Instruction *Inst : Stmt.getInstructions()) |
| 1734 | InstStmtMap.erase(Inst); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 1735 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1736 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1737 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1738 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete, |
| 1739 | bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1740 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 1741 | if (!ShouldDelete(*StmtIt)) { |
| 1742 | StmtIt++; |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1746 | // Start with removing all of the statement's accesses including erasing it |
| 1747 | // from all maps that are pointing to them. |
Michael Kruse | db6f71e | 2018-04-10 01:20:41 +0000 | [diff] [blame] | 1748 | // Make a temporary copy because removing MAs invalidates the iterator. |
| 1749 | SmallVector<MemoryAccess *, 16> MAList(StmtIt->begin(), StmtIt->end()); |
| 1750 | for (MemoryAccess *MA : MAList) |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1751 | StmtIt->removeSingleMemoryAccess(MA, AfterHoisting); |
| 1752 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1753 | removeFromStmtMap(*StmtIt); |
| 1754 | StmtIt = Stmts.erase(StmtIt); |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | void Scop::removeStmtNotInDomainMap() { |
| 1759 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Michael Kruse | 842bdd0 | 2018-08-01 22:28:32 +0000 | [diff] [blame] | 1760 | isl::set Domain = DomainMap.lookup(Stmt.getEntryBlock()); |
| 1761 | if (!Domain) |
| 1762 | return true; |
| 1763 | return Domain.is_empty(); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1764 | }; |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1765 | removeStmts(ShouldDelete, false); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | void Scop::simplifySCoP(bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1769 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Michael Kruse | 5369ea5 | 2018-04-20 18:55:44 +0000 | [diff] [blame] | 1770 | // Never delete statements that contain calls to debug functions. |
| 1771 | if (hasDebugCall(&Stmt)) |
| 1772 | return false; |
| 1773 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 1774 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 1775 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 1776 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 1777 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 1778 | bool OnlyRead = true; |
| 1779 | for (MemoryAccess *MA : Stmt) { |
| 1780 | if (MA->isRead()) |
| 1781 | continue; |
| 1782 | |
| 1783 | OnlyRead = false; |
| 1784 | break; |
| 1785 | } |
| 1786 | |
| 1787 | RemoveStmt = OnlyRead; |
| 1788 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 1789 | return RemoveStmt; |
| 1790 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 1791 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1792 | removeStmts(ShouldDelete, AfterHoisting); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 1795 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1796 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 1797 | if (!LInst) |
| 1798 | return nullptr; |
| 1799 | |
| 1800 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 1801 | LInst = cast<LoadInst>(Rep); |
| 1802 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 1803 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1804 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 1805 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 1806 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 1807 | continue; |
| 1808 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 1809 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 1810 | for (auto *MA : MAs) |
| 1811 | if (MA->getAccessInstruction() == Val) |
| 1812 | return &IAClass; |
| 1813 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1814 | |
| 1815 | return nullptr; |
| 1816 | } |
| 1817 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 1818 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 1819 | ArrayRef<const SCEV *> Sizes, |
| 1820 | MemoryKind Kind, |
| 1821 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 1822 | assert((BasePtr || BaseName) && |
| 1823 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 1824 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 1825 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 1826 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 1827 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 1828 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 1829 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 1830 | DL, this, BaseName)); |
| 1831 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 1832 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 1833 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 1834 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 1835 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 1836 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 1837 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 1838 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 1839 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 1842 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 1843 | const std::string &BaseName, |
| 1844 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 1845 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 1846 | std::vector<const SCEV *> SCEVSizes; |
| 1847 | |
| 1848 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 1849 | if (size) |
| 1850 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 1851 | else |
| 1852 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 1853 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1854 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 1855 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 1856 | return SAI; |
| 1857 | } |
| 1858 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 1859 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 1860 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 1861 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 1862 | return SAI; |
| 1863 | } |
| 1864 | |
| 1865 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 1866 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1867 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 1868 | return SAI; |
| 1869 | } |
| 1870 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1871 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 1872 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1873 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 1874 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1875 | return AssumedContext.to_str(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1876 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 1877 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 1878 | std::string Scop::getInvalidContextStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1879 | return InvalidContext.to_str(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 1880 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1881 | |
| 1882 | std::string Scop::getNameStr() const { |
| 1883 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 1884 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 1885 | return EntryName + "---" + ExitName; |
| 1886 | } |
| 1887 | |
| 1888 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 1889 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1890 | raw_string_ostream ExitStr(ExitName); |
| 1891 | raw_string_ostream EntryStr(EntryName); |
| 1892 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1893 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1894 | EntryStr.str(); |
| 1895 | |
| 1896 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1897 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1898 | ExitStr.str(); |
| 1899 | } else |
| 1900 | ExitName = "FunctionExit"; |
| 1901 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 1902 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1905 | isl::set Scop::getContext() const { return Context; } |
Dominik Adamski | b169e58 | 2019-08-06 21:25:35 +0000 | [diff] [blame] | 1906 | |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1907 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 1908 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1909 | isl::space Scop::getFullParamSpace() const { |
| 1910 | std::vector<isl::id> FortranIDs; |
| 1911 | FortranIDs = getFortranArrayIds(arrays()); |
| 1912 | |
| 1913 | isl::space Space = isl::space::params_alloc( |
| 1914 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 1915 | |
| 1916 | unsigned PDim = 0; |
| 1917 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 1918 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1919 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 1920 | } |
| 1921 | |
| 1922 | for (isl::id Id : FortranIDs) |
| 1923 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 1924 | |
| 1925 | return Space; |
| 1926 | } |
| 1927 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 1928 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 1929 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1930 | return AssumedContext; |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 1933 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 1934 | if (PollyProcessUnprofitable) |
| 1935 | return true; |
| 1936 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 1937 | if (isEmpty()) |
| 1938 | return false; |
| 1939 | |
| 1940 | unsigned OptimizableStmtsOrLoops = 0; |
| 1941 | for (auto &Stmt : *this) { |
| 1942 | if (Stmt.getNumIterators() == 0) |
| 1943 | continue; |
| 1944 | |
| 1945 | bool ContainsArrayAccs = false; |
| 1946 | bool ContainsScalarAccs = false; |
| 1947 | for (auto *MA : Stmt) { |
| 1948 | if (MA->isRead()) |
| 1949 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 1950 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 1951 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 1954 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 1955 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 1956 | } |
| 1957 | |
| 1958 | return OptimizableStmtsOrLoops > 1; |
| 1959 | } |
| 1960 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 1961 | bool Scop::hasFeasibleRuntimeContext() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1962 | auto PositiveContext = getAssumedContext(); |
| 1963 | auto NegativeContext = getInvalidContext(); |
| 1964 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 1965 | // addNonEmptyDomainConstraints returns null if ScopStmts have a null domain |
| 1966 | if (!PositiveContext) |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 1967 | return false; |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 1968 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1969 | bool IsFeasible = !(PositiveContext.is_empty() || |
| 1970 | PositiveContext.is_subset(NegativeContext)); |
| 1971 | if (!IsFeasible) |
| 1972 | return false; |
| 1973 | |
| 1974 | auto DomainContext = getDomains().params(); |
| 1975 | IsFeasible = !DomainContext.is_subset(NegativeContext); |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame] | 1976 | IsFeasible &= !getContext().is_subset(NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 1977 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 1978 | return IsFeasible; |
| 1979 | } |
| 1980 | |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame] | 1981 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
| 1982 | isl::set DomainContext = getDomains().params(); |
| 1983 | return C.intersect_params(DomainContext); |
| 1984 | } |
| 1985 | |
| 1986 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
| 1987 | Value *PointerBase = MA->getOriginalBaseAddr(); |
| 1988 | |
| 1989 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
| 1990 | if (!PointerBaseInst) |
| 1991 | return nullptr; |
| 1992 | |
| 1993 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 1994 | if (!BasePtrStmt) |
| 1995 | return nullptr; |
| 1996 | |
| 1997 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 1998 | } |
| 1999 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 2000 | static std::string toString(AssumptionKind Kind) { |
| 2001 | switch (Kind) { |
| 2002 | case ALIASING: |
| 2003 | return "No-aliasing"; |
| 2004 | case INBOUNDS: |
| 2005 | return "Inbounds"; |
| 2006 | case WRAPPING: |
| 2007 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 2008 | case UNSIGNED: |
| 2009 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 2010 | case COMPLEXITY: |
| 2011 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 2012 | case PROFITABLE: |
| 2013 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 2014 | case ERRORBLOCK: |
| 2015 | return "No-error"; |
| 2016 | case INFINITELOOP: |
| 2017 | return "Finite loop"; |
| 2018 | case INVARIANTLOAD: |
| 2019 | return "Invariant load"; |
| 2020 | case DELINEARIZATION: |
| 2021 | return "Delinearization"; |
| 2022 | } |
| 2023 | llvm_unreachable("Unknown AssumptionKind!"); |
| 2024 | } |
| 2025 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2026 | bool Scop::isEffectiveAssumption(isl::set Set, AssumptionSign Sign) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2027 | if (Sign == AS_ASSUMPTION) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2028 | if (Context.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2029 | return false; |
| 2030 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2031 | if (AssumedContext.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2032 | return false; |
| 2033 | } else { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2034 | if (Set.is_disjoint(Context)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2035 | return false; |
| 2036 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2037 | if (Set.is_subset(InvalidContext)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2038 | return false; |
| 2039 | } |
| 2040 | return true; |
| 2041 | } |
| 2042 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2043 | bool Scop::trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 2044 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 2045 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 2046 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 2047 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 2048 | // Do never emit trivial assumptions as they only clutter the output. |
| 2049 | if (!PollyRemarksMinimal) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2050 | isl::set Univ; |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 2051 | if (Sign == AS_ASSUMPTION) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2052 | Univ = isl::set::universe(Set.get_space()); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 2053 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2054 | bool IsTrivial = (Sign == AS_RESTRICTION && Set.is_empty()) || |
| 2055 | (Sign == AS_ASSUMPTION && Univ.is_equal(Set)); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 2056 | |
| 2057 | if (IsTrivial) |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2061 | switch (Kind) { |
| 2062 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2063 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2066 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2067 | break; |
| 2068 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2069 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2070 | break; |
| 2071 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2072 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2073 | break; |
| 2074 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2075 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2078 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2079 | break; |
| 2080 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2081 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2082 | break; |
| 2083 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2084 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2087 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2088 | break; |
| 2089 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 2090 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | } |
| 2093 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2094 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2095 | std::string Msg = toString(Kind) + Suffix + Set.to_str(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2096 | if (BB) |
| 2097 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 2098 | << Msg); |
| 2099 | else |
| 2100 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 2101 | R.getEntry()) |
| 2102 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2103 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2106 | void Scop::addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 2107 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 2108 | // Simplify the assumptions/restrictions first. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2109 | Set = Set.gist_params(getContext()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 2110 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2111 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2112 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 2113 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2114 | if (Sign == AS_ASSUMPTION) |
| 2115 | AssumedContext = AssumedContext.intersect(Set).coalesce(); |
| 2116 | else |
| 2117 | InvalidContext = InvalidContext.unite(Set).coalesce(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2120 | void Scop::recordAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 2121 | AssumptionSign Sign, BasicBlock *BB) { |
| 2122 | assert((Set.is_params() || BB) && |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 2123 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 2124 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2127 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 2128 | LLVM_DEBUG(dbgs() << "Invalidate SCoP because of reason " << Kind << "\n"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2129 | addAssumption(Kind, isl::set::empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2132 | isl::set Scop::getInvalidContext() const { return InvalidContext; } |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2133 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2134 | void Scop::printContext(raw_ostream &OS) const { |
| 2135 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2136 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2137 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2138 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2139 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2140 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 2141 | OS.indent(4) << "Invalid Context:\n"; |
| 2142 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2143 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2144 | unsigned Dim = 0; |
| 2145 | for (const SCEV *Parameter : Parameters) |
| 2146 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2149 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2150 | int noOfGroups = 0; |
| 2151 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2152 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2153 | noOfGroups += 1; |
| 2154 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2155 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2158 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2159 | if (MinMaxAliasGroups.empty()) { |
| 2160 | OS.indent(8) << "n/a\n"; |
| 2161 | return; |
| 2162 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2163 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2164 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2165 | |
| 2166 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2167 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2168 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2169 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2170 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 2171 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2172 | } |
| 2173 | OS << " ]]\n"; |
| 2174 | } |
| 2175 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2176 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2177 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2178 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2179 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2180 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 2181 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2182 | } |
| 2183 | OS << " ]]\n"; |
| 2184 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2188 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2189 | OS << "Statements {\n"; |
| 2190 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2191 | for (const ScopStmt &Stmt : *this) { |
| 2192 | OS.indent(4); |
| 2193 | Stmt.print(OS, PrintInstructions); |
| 2194 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2195 | |
| 2196 | OS.indent(4) << "}\n"; |
| 2197 | } |
| 2198 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2199 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 2200 | OS << "Arrays {\n"; |
| 2201 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 2202 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 2203 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2204 | |
| 2205 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 2206 | |
| 2207 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 2208 | |
| 2209 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 2210 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 2211 | |
| 2212 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2215 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 2216 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 2217 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 2218 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2219 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2220 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2221 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2222 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2223 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2224 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2225 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2226 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 2227 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2228 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2229 | } |
| 2230 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2231 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2232 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2233 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2234 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 2237 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 2238 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 2239 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2240 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2241 | isl::ctx Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2242 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 2243 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 2244 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 2245 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 2246 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 2247 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 2248 | // 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] | 2249 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 2250 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 2251 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 2252 | // TODO: We could use a heuristic and either use: |
| 2253 | // SCEVAffinator::takeNonNegativeAssumption |
| 2254 | // or |
| 2255 | // SCEVAffinator::interpretAsUnsigned |
| 2256 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 2257 | if (NonNegative) |
| 2258 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 2259 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 2260 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 2261 | |
| 2262 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2263 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 2264 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2267 | isl::union_set Scop::getDomains() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2268 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx().get(), 0); |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 2269 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 2270 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2271 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2272 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 2273 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2274 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2277 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 2278 | PWACtx PWAC = getPwAff(E, BB); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 2279 | return PWAC.first; |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2282 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2283 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2284 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2285 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2286 | for (ScopStmt &Stmt : *this) { |
| 2287 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2288 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2289 | continue; |
| 2290 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2291 | isl::set Domain = Stmt.getDomain(); |
| 2292 | isl::map AccessDomain = MA->getAccessRelation(); |
| 2293 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 2294 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 2297 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2298 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2301 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2302 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2305 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2306 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2309 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2310 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2313 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 2314 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 2317 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 2318 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 2319 | } |
| 2320 | |
Tobias Grosser | fa03cb7 | 2017-08-17 22:04:53 +0000 | [diff] [blame] | 2321 | isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { |
| 2322 | return getAccessesOfType( |
| 2323 | [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); |
| 2324 | } |
| 2325 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2326 | isl::union_map Scop::getSchedule() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2327 | auto Tree = getScheduleTree(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2328 | return Tree.get_map(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2329 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2330 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2331 | isl::schedule Scop::getScheduleTree() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2332 | return Schedule.intersect_domain(getDomains()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2333 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 2334 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2335 | void Scop::setSchedule(isl::union_map NewSchedule) { |
| 2336 | auto S = isl::schedule::from_domain(getDomains()); |
| 2337 | Schedule = S.insert_partial_schedule( |
| 2338 | isl::multi_union_pw_aff::from_union_map(NewSchedule)); |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame] | 2339 | ScheduleModified = true; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2342 | void Scop::setScheduleTree(isl::schedule NewSchedule) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2343 | Schedule = NewSchedule; |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame] | 2344 | ScheduleModified = true; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2347 | bool Scop::restrictDomains(isl::union_set Domain) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2348 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2349 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2350 | isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); |
| 2351 | isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2352 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2353 | if (StmtDomain.is_subset(NewStmtDomain)) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2354 | continue; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2355 | |
| 2356 | Changed = true; |
| 2357 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2358 | NewStmtDomain = NewStmtDomain.coalesce(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2359 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2360 | if (NewStmtDomain.is_empty()) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2361 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 2362 | else |
| 2363 | Stmt.restrictDomain(isl::set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2364 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2365 | return Changed; |
| 2366 | } |
| 2367 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2368 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 2369 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 2370 | void Scop::addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, |
| 2371 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 2372 | assert(BB && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 2373 | Stmts.emplace_back(*this, *BB, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 2374 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 2375 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 2376 | for (Instruction *Inst : Instructions) { |
| 2377 | assert(!InstStmtMap.count(Inst) && |
| 2378 | "Unexpected statement corresponding to the instruction."); |
| 2379 | InstStmtMap[Inst] = Stmt; |
| 2380 | } |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 2383 | void Scop::addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 2384 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 2385 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 2386 | Stmts.emplace_back(*this, *R, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 2387 | auto *Stmt = &Stmts.back(); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 2388 | |
| 2389 | for (Instruction *Inst : Instructions) { |
| 2390 | assert(!InstStmtMap.count(Inst) && |
| 2391 | "Unexpected statement corresponding to the instruction."); |
| 2392 | InstStmtMap[Inst] = Stmt; |
| 2393 | } |
| 2394 | |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 2395 | for (BasicBlock *BB : R->blocks()) { |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 2396 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 2397 | if (BB == R->getEntry()) |
| 2398 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 2399 | for (Instruction &Inst : *BB) { |
| 2400 | assert(!InstStmtMap.count(&Inst) && |
| 2401 | "Unexpected statement corresponding to the instruction."); |
| 2402 | InstStmtMap[&Inst] = Stmt; |
| 2403 | } |
| 2404 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 2407 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 2408 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 2409 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 2410 | isl::set SourceDomain = SourceRel.domain(); |
| 2411 | isl::set TargetDomain = TargetRel.domain(); |
| 2412 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 2413 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 2414 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 2415 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 2416 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 2417 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 2418 | CopyStmtsNum++; |
| 2419 | return &(Stmts.back()); |
| 2420 | } |
| 2421 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 2422 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 2423 | auto StmtMapIt = StmtMap.find(BB); |
| 2424 | if (StmtMapIt == StmtMap.end()) |
| 2425 | return {}; |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 2426 | return StmtMapIt->second; |
| 2427 | } |
| 2428 | |
Michael Kruse | a230f22 | 2018-01-23 23:56:36 +0000 | [diff] [blame] | 2429 | ScopStmt *Scop::getIncomingStmtFor(const Use &U) const { |
| 2430 | auto *PHI = cast<PHINode>(U.getUser()); |
| 2431 | BasicBlock *IncomingBB = PHI->getIncomingBlock(U); |
| 2432 | |
| 2433 | // If the value is a non-synthesizable from the incoming block, use the |
| 2434 | // statement that contains it as user statement. |
| 2435 | if (auto *IncomingInst = dyn_cast<Instruction>(U.get())) { |
| 2436 | if (IncomingInst->getParent() == IncomingBB) { |
| 2437 | if (ScopStmt *IncomingStmt = getStmtFor(IncomingInst)) |
| 2438 | return IncomingStmt; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | // Otherwise, use the epilogue/last statement. |
| 2443 | return getLastStmtFor(IncomingBB); |
| 2444 | } |
| 2445 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 2446 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 2447 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2448 | if (!StmtList.empty()) |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 2449 | return StmtList.back(); |
| 2450 | return nullptr; |
| 2451 | } |
| 2452 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 2453 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 2454 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 2455 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 2456 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 2459 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 2460 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2463 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 2464 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2465 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 2466 | // outermostLoopInRegion always returns nullptr for top level regions |
| 2467 | if (R.isTopLevelRegion()) { |
| 2468 | // LoopInfo's depths start at 1, we start at 0 |
| 2469 | return L->getLoopDepth() - 1; |
| 2470 | } else { |
| 2471 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 2472 | assert(OuterLoop); |
| 2473 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 2474 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 2477 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 2478 | for (auto &SAI : arrays()) { |
| 2479 | if (SAI->getName() == BaseName) |
| 2480 | return SAI; |
| 2481 | } |
| 2482 | return nullptr; |
| 2483 | } |
| 2484 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2485 | void Scop::addAccessData(MemoryAccess *Access) { |
| 2486 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 2487 | assert(SAI && "can only use after access relations have been constructed"); |
| 2488 | |
| 2489 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 2490 | ValueUseAccs[SAI].push_back(Access); |
| 2491 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 2492 | PHIIncomingAccs[SAI].push_back(Access); |
| 2493 | } |
| 2494 | |
| 2495 | void Scop::removeAccessData(MemoryAccess *Access) { |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 2496 | if (Access->isOriginalValueKind() && Access->isWrite()) { |
| 2497 | ValueDefAccs.erase(Access->getAccessValue()); |
| 2498 | } else if (Access->isOriginalValueKind() && Access->isRead()) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2499 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 2500 | auto NewEnd = std::remove(Uses.begin(), Uses.end(), Access); |
| 2501 | Uses.erase(NewEnd, Uses.end()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 2502 | } else if (Access->isOriginalPHIKind() && Access->isRead()) { |
| 2503 | PHINode *PHI = cast<PHINode>(Access->getAccessInstruction()); |
| 2504 | PHIReadAccs.erase(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2505 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 2506 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 2507 | auto NewEnd = std::remove(Incomings.begin(), Incomings.end(), Access); |
| 2508 | Incomings.erase(NewEnd, Incomings.end()); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 2513 | assert(SAI->isValueKind()); |
| 2514 | |
| 2515 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 2516 | if (!Val) |
| 2517 | return nullptr; |
| 2518 | |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 2519 | return ValueDefAccs.lookup(Val); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 2523 | assert(SAI->isValueKind()); |
| 2524 | auto It = ValueUseAccs.find(SAI); |
| 2525 | if (It == ValueUseAccs.end()) |
| 2526 | return {}; |
| 2527 | return It->second; |
| 2528 | } |
| 2529 | |
| 2530 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 2531 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 2532 | |
| 2533 | if (SAI->isExitPHIKind()) |
| 2534 | return nullptr; |
| 2535 | |
| 2536 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 2537 | return PHIReadAccs.lookup(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 2541 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 2542 | auto It = PHIIncomingAccs.find(SAI); |
| 2543 | if (It == PHIIncomingAccs.end()) |
| 2544 | return {}; |
| 2545 | return It->second; |
| 2546 | } |
| 2547 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 2548 | bool Scop::isEscaping(Instruction *Inst) { |
| 2549 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 2550 | "values defined inside the SCoP"); |
| 2551 | |
| 2552 | for (Use &Use : Inst->uses()) { |
| 2553 | BasicBlock *UserBB = getUseBlock(Use); |
| 2554 | if (!contains(UserBB)) |
| 2555 | return true; |
| 2556 | |
| 2557 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 2558 | // move to a new basic block such that its incoming blocks are not in the |
| 2559 | // SCoP anymore. |
| 2560 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 2561 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 2562 | return true; |
| 2563 | } |
| 2564 | return false; |
| 2565 | } |
| 2566 | |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame] | 2567 | void Scop::incrementNumberOfAliasingAssumptions(unsigned step) { |
| 2568 | AssumptionsAliasing += step; |
| 2569 | } |
| 2570 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2571 | Scop::ScopStatistics Scop::getStatistics() const { |
| 2572 | ScopStatistics Result; |
| 2573 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
| 2574 | auto LoopStat = ScopDetection::countBeneficialLoops(&R, *SE, *getLI(), 0); |
| 2575 | |
| 2576 | int NumTotalLoops = LoopStat.NumLoops; |
| 2577 | Result.NumBoxedLoops = getBoxedLoops().size(); |
| 2578 | Result.NumAffineLoops = NumTotalLoops - Result.NumBoxedLoops; |
| 2579 | |
| 2580 | for (const ScopStmt &Stmt : *this) { |
| 2581 | isl::set Domain = Stmt.getDomain().intersect_params(getContext()); |
| 2582 | bool IsInLoop = Stmt.getNumIterators() >= 1; |
| 2583 | for (MemoryAccess *MA : Stmt) { |
| 2584 | if (!MA->isWrite()) |
| 2585 | continue; |
| 2586 | |
| 2587 | if (MA->isLatestValueKind()) { |
| 2588 | Result.NumValueWrites += 1; |
| 2589 | if (IsInLoop) |
| 2590 | Result.NumValueWritesInLoops += 1; |
| 2591 | } |
| 2592 | |
| 2593 | if (MA->isLatestAnyPHIKind()) { |
| 2594 | Result.NumPHIWrites += 1; |
| 2595 | if (IsInLoop) |
| 2596 | Result.NumPHIWritesInLoops += 1; |
| 2597 | } |
| 2598 | |
| 2599 | isl::set AccSet = |
| 2600 | MA->getAccessRelation().intersect_domain(Domain).range(); |
| 2601 | if (AccSet.is_singleton()) { |
| 2602 | Result.NumSingletonWrites += 1; |
| 2603 | if (IsInLoop) |
| 2604 | Result.NumSingletonWritesInLoops += 1; |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | #endif |
| 2609 | return Result; |
| 2610 | } |
| 2611 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2612 | raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { |
| 2613 | scop.print(OS, PollyPrintInstructions); |
| 2614 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2617 | //===----------------------------------------------------------------------===// |
| 2618 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 2619 | AU.addRequired<LoopInfoWrapperPass>(); |
| 2620 | AU.addRequired<RegionInfoPass>(); |
| 2621 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 2622 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2623 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2624 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2625 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2626 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2627 | AU.setPreservesAll(); |
| 2628 | } |
| 2629 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2630 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 2631 | Scop::ScopStatistics ScopStats) { |
| 2632 | assert(Stats.NumLoops == ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops); |
| 2633 | |
| 2634 | NumScops++; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2635 | NumLoopsInScop += Stats.NumLoops; |
| 2636 | MaxNumLoopsInScop = |
| 2637 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 2638 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 2639 | if (Stats.MaxDepth == 0) |
| 2640 | NumScopsDepthZero++; |
| 2641 | else if (Stats.MaxDepth == 1) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2642 | NumScopsDepthOne++; |
| 2643 | else if (Stats.MaxDepth == 2) |
| 2644 | NumScopsDepthTwo++; |
| 2645 | else if (Stats.MaxDepth == 3) |
| 2646 | NumScopsDepthThree++; |
| 2647 | else if (Stats.MaxDepth == 4) |
| 2648 | NumScopsDepthFour++; |
| 2649 | else if (Stats.MaxDepth == 5) |
| 2650 | NumScopsDepthFive++; |
| 2651 | else |
| 2652 | NumScopsDepthLarger++; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2653 | |
| 2654 | NumAffineLoops += ScopStats.NumAffineLoops; |
| 2655 | NumBoxedLoops += ScopStats.NumBoxedLoops; |
| 2656 | |
| 2657 | NumValueWrites += ScopStats.NumValueWrites; |
| 2658 | NumValueWritesInLoops += ScopStats.NumValueWritesInLoops; |
| 2659 | NumPHIWrites += ScopStats.NumPHIWrites; |
| 2660 | NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops; |
| 2661 | NumSingletonWrites += ScopStats.NumSingletonWrites; |
| 2662 | NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2665 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2666 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2667 | |
| 2668 | if (!SD.isMaxRegionInScop(*R)) |
| 2669 | return false; |
| 2670 | |
| 2671 | Function *F = R->getEntry()->getParent(); |
| 2672 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 2673 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 2674 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 2675 | auto const &DL = F->getParent()->getDataLayout(); |
| 2676 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2677 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2678 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2679 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2680 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 2681 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2682 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2683 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2684 | if (S) { |
| 2685 | ScopDetection::LoopStats Stats = |
| 2686 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2687 | updateLoopCountStatistic(Stats, S->getStatistics()); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2688 | } |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2689 | #endif |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 2690 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2691 | return false; |
| 2692 | } |
| 2693 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2694 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 2695 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2696 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 2697 | else |
| 2698 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2699 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2700 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2701 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 2702 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2703 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 2704 | |
| 2705 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 2706 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 2707 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 2708 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2709 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 2710 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 2711 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 2712 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2713 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2714 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 2715 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 2716 | "Polly - Create polyhedral description of Scops", false, |
| 2717 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2718 | |
| 2719 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2720 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 2721 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2722 | AssumptionCache &AC, OptimizationRemarkEmitter &ORE) |
| 2723 | : 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] | 2724 | recompute(); |
| 2725 | } |
| 2726 | |
| 2727 | void ScopInfo::recompute() { |
| 2728 | RegionToScopMap.clear(); |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2729 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2730 | /// function. |
| 2731 | for (auto &It : SD) { |
| 2732 | Region *R = const_cast<Region *>(It); |
| 2733 | if (!SD.isMaxRegionInScop(*R)) |
| 2734 | continue; |
| 2735 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2736 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2737 | std::unique_ptr<Scop> S = SB.getScop(); |
| 2738 | if (!S) |
| 2739 | continue; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2740 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 2741 | ScopDetection::LoopStats Stats = |
| 2742 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 2743 | updateLoopCountStatistic(Stats, S->getStatistics()); |
| 2744 | #endif |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2745 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 2746 | assert(Inserted && "Building Scop for the same region twice!"); |
| 2747 | (void)Inserted; |
| 2748 | } |
| 2749 | } |
| 2750 | |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 2751 | bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 2752 | FunctionAnalysisManager::Invalidator &Inv) { |
| 2753 | // Check whether the analysis, all analyses on functions have been preserved |
| 2754 | // or anything we're holding references to is being invalidated |
| 2755 | auto PAC = PA.getChecker<ScopInfoAnalysis>(); |
| 2756 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 2757 | Inv.invalidate<ScopAnalysis>(F, PA) || |
| 2758 | Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || |
| 2759 | Inv.invalidate<LoopAnalysis>(F, PA) || |
| 2760 | Inv.invalidate<AAManager>(F, PA) || |
| 2761 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 2762 | Inv.invalidate<AssumptionAnalysis>(F, PA); |
| 2763 | } |
| 2764 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2765 | AnalysisKey ScopInfoAnalysis::Key; |
| 2766 | |
| 2767 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 2768 | FunctionAnalysisManager &FAM) { |
| 2769 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 2770 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 2771 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 2772 | auto &AA = FAM.getResult<AAManager>(F); |
| 2773 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 2774 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 2775 | auto &DL = F.getParent()->getDataLayout(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2776 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 2777 | return {DL, SD, SE, LI, AA, DT, AC, ORE}; |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 2781 | FunctionAnalysisManager &FAM) { |
| 2782 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 2783 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 2784 | // order here to keep the output persistent |
| 2785 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2786 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2787 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2788 | else |
| 2789 | Stream << "Invalid Scop!\n"; |
| 2790 | } |
| 2791 | return PreservedAnalyses::all(); |
| 2792 | } |
| 2793 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2794 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 2795 | AU.addRequired<LoopInfoWrapperPass>(); |
| 2796 | AU.addRequired<RegionInfoPass>(); |
| 2797 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 2798 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2799 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2800 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2801 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2802 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2803 | AU.setPreservesAll(); |
| 2804 | } |
| 2805 | |
| 2806 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2807 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2808 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 2809 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 2810 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 2811 | auto const &DL = F.getParent()->getDataLayout(); |
| 2812 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2813 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2814 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2815 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 2816 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC, ORE}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2817 | return false; |
| 2818 | } |
| 2819 | |
| 2820 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 2821 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2822 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 2823 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2824 | else |
| 2825 | OS << "Invalid Scop!\n"; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | char ScopInfoWrapperPass::ID = 0; |
| 2830 | |
| 2831 | Pass *polly::createScopInfoWrapperPassPass() { |
| 2832 | return new ScopInfoWrapperPass(); |
| 2833 | } |
| 2834 | |
| 2835 | INITIALIZE_PASS_BEGIN( |
| 2836 | ScopInfoWrapperPass, "polly-function-scops", |
| 2837 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 2838 | false); |
| 2839 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2840 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2841 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 2842 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 2843 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 2844 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 2845 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 2846 | INITIALIZE_PASS_END( |
| 2847 | ScopInfoWrapperPass, "polly-function-scops", |
| 2848 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 2849 | false) |