Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Detect the maximal Scops of a function. |
| 11 | // |
| 12 | // A static control part (Scop) is a subgraph of the control flow graph (CFG) |
| 13 | // that only has statically known control flow and can therefore be described |
| 14 | // within the polyhedral model. |
| 15 | // |
| 16 | // Every Scop fullfills these restrictions: |
| 17 | // |
| 18 | // * It is a single entry single exit region |
| 19 | // |
| 20 | // * Only affine linear bounds in the loops |
| 21 | // |
| 22 | // Every natural loop in a Scop must have a number of loop iterations that can |
| 23 | // be described as an affine linear function in surrounding loop iterators or |
| 24 | // parameters. (A parameter is a scalar that does not change its value during |
| 25 | // execution of the Scop). |
| 26 | // |
| 27 | // * Only comparisons of affine linear expressions in conditions |
| 28 | // |
| 29 | // * All loops and conditions perfectly nested |
| 30 | // |
| 31 | // The control flow needs to be structured such that it could be written using |
| 32 | // just 'for' and 'if' statements, without the need for any 'goto', 'break' or |
| 33 | // 'continue'. |
| 34 | // |
| 35 | // * Side effect free functions call |
| 36 | // |
| 37 | // Only function calls and intrinsics that do not have side effects are allowed |
| 38 | // (readnone). |
| 39 | // |
| 40 | // The Scop detection finds the largest Scops by checking if the largest |
| 41 | // region is a Scop. If this is not the case, its canonical subregions are |
| 42 | // checked until a region is a Scop. It is now tried to extend this Scop by |
| 43 | // creating a larger non canonical region. |
| 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 47 | #include "polly/CodeGen/BlockGenerators.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 48 | #include "polly/CodeGen/CodeGeneration.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 49 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 50 | #include "polly/Options.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 51 | #include "polly/ScopDetection.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 52 | #include "polly/ScopDetectionDiagnostic.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 53 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 54 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | a63b7ce | 2015-05-03 05:21:36 +0000 | [diff] [blame] | 55 | #include "polly/Support/ScopLocation.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" |
| 57 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/LoopInfo.h" |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/PostDominators.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 60 | #include "llvm/Analysis/RegionIterator.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 61 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 63 | #include "llvm/IR/DebugInfo.h" |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 64 | #include "llvm/IR/DiagnosticInfo.h" |
| 65 | #include "llvm/IR/DiagnosticPrinter.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 66 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 67 | #include "llvm/IR/LLVMContext.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 69 | #include <set> |
| 70 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 71 | using namespace llvm; |
| 72 | using namespace polly; |
| 73 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 74 | #define DEBUG_TYPE "polly-detect" |
| 75 | |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 76 | static cl::opt<bool> DetectUnprofitable("polly-detect-unprofitable", |
| 77 | cl::desc("Detect unprofitable scops"), |
| 78 | cl::Hidden, cl::init(false), |
| 79 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 80 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 81 | static cl::opt<std::string> OnlyFunction( |
| 82 | "polly-only-func", |
| 83 | cl::desc("Only run on functions that contain a certain string"), |
| 84 | cl::value_desc("string"), cl::ValueRequired, cl::init(""), |
| 85 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 86 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 87 | static cl::opt<std::string> OnlyRegion( |
| 88 | "polly-only-region", |
| 89 | cl::desc("Only run on certain regions (The provided identifier must " |
| 90 | "appear in the name of the region's entry block"), |
| 91 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 92 | cl::cat(PollyCategory)); |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 93 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 94 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 95 | IgnoreAliasing("polly-ignore-aliasing", |
| 96 | cl::desc("Ignore possible aliasing of the array bases"), |
| 97 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 98 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 99 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 100 | bool polly::PollyUseRuntimeAliasChecks; |
| 101 | static cl::opt<bool, true> XPollyUseRuntimeAliasChecks( |
| 102 | "polly-use-runtime-alias-checks", |
| 103 | cl::desc("Use runtime alias checks to resolve possible aliasing."), |
| 104 | cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore, |
| 105 | cl::init(true), cl::cat(PollyCategory)); |
| 106 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 107 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 108 | ReportLevel("polly-report", |
| 109 | cl::desc("Print information about the activities of Polly"), |
| 110 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 111 | |
| 112 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 113 | AllowNonAffine("polly-allow-nonaffine", |
| 114 | cl::desc("Allow non affine access functions in arrays"), |
| 115 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 116 | cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 117 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 118 | static cl::opt<bool> AllowNonAffineSubRegions( |
| 119 | "polly-allow-nonaffine-branches", |
| 120 | cl::desc("Allow non affine conditions for branches"), cl::Hidden, |
Johannes Doerfert | a36842f | 2015-02-26 11:09:24 +0000 | [diff] [blame] | 121 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 122 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 123 | static cl::opt<bool> |
| 124 | AllowNonAffineSubLoops("polly-allow-nonaffine-loops", |
| 125 | cl::desc("Allow non affine conditions for loops"), |
| 126 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 127 | cl::cat(PollyCategory)); |
| 128 | |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 129 | static cl::opt<bool> AllowUnsigned("polly-allow-unsigned", |
| 130 | cl::desc("Allow unsigned expressions"), |
| 131 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 132 | cl::cat(PollyCategory)); |
| 133 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 134 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 135 | TrackFailures("polly-detect-track-failures", |
| 136 | cl::desc("Track failure strings in detecting scop regions"), |
| 137 | cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore, |
Andreas Simbuerger | 3efe40b | 2014-08-17 10:09:03 +0000 | [diff] [blame] | 138 | cl::init(true), cl::cat(PollyCategory)); |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 139 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 140 | static cl::opt<bool> KeepGoing("polly-detect-keep-going", |
| 141 | cl::desc("Do not fail on the first error."), |
| 142 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 143 | cl::cat(PollyCategory)); |
| 144 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 145 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 146 | PollyDelinearizeX("polly-delinearize", |
| 147 | cl::desc("Delinearize array access functions"), |
| 148 | cl::location(PollyDelinearize), cl::Hidden, |
Tobias Grosser | 6973cb6 | 2015-03-08 15:21:18 +0000 | [diff] [blame] | 149 | cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory)); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 150 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 151 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 152 | VerifyScops("polly-detect-verify", |
| 153 | cl::desc("Verify the detected SCoPs after each transformation"), |
| 154 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 155 | cl::cat(PollyCategory)); |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 156 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 157 | static cl::opt<bool> AllowNonSCEVBackedgeTakenCount( |
| 158 | "polly-allow-non-scev-backedge-taken-count", |
| 159 | cl::desc("Allow loops even if SCEV cannot provide a trip count"), |
| 160 | cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 161 | |
Johannes Doerfert | e526de5 | 2015-09-21 19:10:11 +0000 | [diff] [blame] | 162 | /// @brief The minimal trip count under which loops are considered unprofitable. |
| 163 | static const unsigned MIN_LOOP_TRIP_COUNT = 8; |
| 164 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 165 | bool polly::PollyTrackFailures = false; |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 166 | bool polly::PollyDelinearize = false; |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 167 | StringRef polly::PollySkipFnAttr = "polly.skip.fn"; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 168 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 169 | //===----------------------------------------------------------------------===// |
| 170 | // Statistics. |
| 171 | |
| 172 | STATISTIC(ValidRegion, "Number of regions that a valid part of Scop"); |
| 173 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 174 | class DiagnosticScopFound : public DiagnosticInfo { |
| 175 | private: |
| 176 | static int PluginDiagnosticKind; |
| 177 | |
| 178 | Function &F; |
| 179 | std::string FileName; |
| 180 | unsigned EntryLine, ExitLine; |
| 181 | |
| 182 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 183 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 184 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 185 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 186 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 187 | |
| 188 | virtual void print(DiagnosticPrinter &DP) const; |
| 189 | |
| 190 | static bool classof(const DiagnosticInfo *DI) { |
| 191 | return DI->getKind() == PluginDiagnosticKind; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | int DiagnosticScopFound::PluginDiagnosticKind = 10; |
| 196 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 197 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 198 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 199 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 200 | |
| 201 | if (FileName.empty()) { |
| 202 | DP << "Scop location is unknown. Compile with debug info " |
| 203 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 204 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 208 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 209 | } |
| 210 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | // ScopDetection. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 213 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 214 | ScopDetection::ScopDetection() : FunctionPass(ID) { |
| 215 | if (!PollyUseRuntimeAliasChecks) |
| 216 | return; |
| 217 | |
Johannes Doerfert | 928229f | 2014-09-29 17:06:29 +0000 | [diff] [blame] | 218 | // Disable runtime alias checks if we ignore aliasing all together. |
| 219 | if (IgnoreAliasing) { |
| 220 | PollyUseRuntimeAliasChecks = false; |
| 221 | return; |
| 222 | } |
| 223 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 224 | if (AllowNonAffine) { |
| 225 | DEBUG(errs() << "WARNING: We disable runtime alias checks as non affine " |
| 226 | "accesses are enabled.\n"); |
| 227 | PollyUseRuntimeAliasChecks = false; |
| 228 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 231 | template <class RR, typename... Args> |
| 232 | inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert, |
| 233 | Args &&... Arguments) const { |
| 234 | |
| 235 | if (!Context.Verifying) { |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 236 | RejectLog &Log = Context.Log; |
| 237 | std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 238 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 239 | if (PollyTrackFailures) |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 240 | Log.report(RejectReason); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 241 | |
| 242 | DEBUG(dbgs() << RejectReason->getMessage()); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 243 | DEBUG(dbgs() << "\n"); |
| 244 | } else { |
| 245 | assert(!Assert && "Verification of detected scop failed"); |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 251 | bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const { |
| 252 | if (!ValidRegions.count(&R)) |
| 253 | return false; |
| 254 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 255 | if (Verify) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 256 | BoxedLoopsSetTy DummyBoxedLoopsSet; |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 257 | NonAffineSubRegionSetTy DummyNonAffineSubRegionSet; |
| 258 | DetectionContext Context(const_cast<Region &>(R), *AA, |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 259 | DummyNonAffineSubRegionSet, DummyBoxedLoopsSet, |
| 260 | false /*verifying*/); |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 261 | return isValidRegion(Context); |
| 262 | } |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 263 | |
| 264 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 267 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 268 | if (!RejectLogs.count(R)) |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 269 | return ""; |
| 270 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 271 | // Get the first error we found. Even in keep-going mode, this is the first |
| 272 | // reason that caused the candidate to be rejected. |
| 273 | RejectLog Errors = RejectLogs.at(R); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 274 | |
| 275 | // This can happen when we marked a region invalid, but didn't track |
| 276 | // an error for it. |
| 277 | if (Errors.size() == 0) |
| 278 | return ""; |
| 279 | |
| 280 | RejectReasonPtr RR = *Errors.begin(); |
| 281 | return RR->getMessage(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 284 | bool ScopDetection::addOverApproximatedRegion(Region *AR, |
| 285 | DetectionContext &Context) const { |
| 286 | |
| 287 | // If we already know about Ar we can exit. |
| 288 | if (!Context.NonAffineSubRegionSet.insert(AR)) |
| 289 | return true; |
| 290 | |
| 291 | // All loops in the region have to be overapproximated too if there |
| 292 | // are accesses that depend on the iteration count. |
| 293 | for (BasicBlock *BB : AR->blocks()) { |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 294 | Loop *L = LI->getLoopFor(BB); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 295 | if (AR->contains(L)) |
| 296 | Context.BoxedLoopsSet.insert(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 297 | } |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 298 | |
| 299 | return (AllowNonAffineSubLoops || Context.BoxedLoopsSet.empty()); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 302 | bool ScopDetection::isValidCFG(BasicBlock &BB, |
| 303 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 304 | Region &CurRegion = Context.CurRegion; |
| 305 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 306 | TerminatorInst *TI = BB.getTerminator(); |
| 307 | |
| 308 | // Return instructions are only valid if the region is the top level region. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 309 | if (isa<ReturnInst>(TI) && !CurRegion.getExit() && TI->getNumOperands() == 0) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 310 | return true; |
| 311 | |
| 312 | BranchInst *Br = dyn_cast<BranchInst>(TI); |
| 313 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 314 | if (!Br) |
| 315 | return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 316 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 317 | if (Br->isUnconditional()) |
| 318 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 319 | |
| 320 | Value *Condition = Br->getCondition(); |
| 321 | |
| 322 | // UndefValue is not allowed as condition. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 323 | if (isa<UndefValue>(Condition)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 324 | return invalid<ReportUndefCond>(Context, /*Assert=*/true, Br, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 325 | |
| 326 | // Only Constant and ICmpInst are allowed as condition. |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 327 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 328 | if (!AllowNonAffineSubRegions || |
| 329 | !addOverApproximatedRegion(RI->getRegionFor(&BB), Context)) |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 330 | return invalid<ReportInvalidCond>(Context, /*Assert=*/true, Br, &BB); |
| 331 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 332 | |
| 333 | // Allow perfectly nested conditions. |
| 334 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 335 | |
| 336 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 337 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 338 | // in the code generation. |
| 339 | // |
| 340 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 341 | // well. |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 342 | if (ICmp->isUnsigned() && !AllowUnsigned) |
Tobias Grosser | a8512b1 | 2015-05-20 15:37:11 +0000 | [diff] [blame] | 343 | return invalid<ReportUnsignedCond>(Context, /*Assert=*/true, Br, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 344 | |
| 345 | // Are both operands of the ICmp affine? |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 346 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 347 | isa<UndefValue>(ICmp->getOperand(1))) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 348 | return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 349 | |
Johannes Doerfert | 7ca8dc2 | 2015-09-09 14:19:04 +0000 | [diff] [blame] | 350 | // TODO: FIXME: IslExprBuilder is not capable of producing valid code |
| 351 | // for arbitrary pointer expressions at the moment. Until |
| 352 | // this is fixed we disallow pointer expressions completely. |
| 353 | if (ICmp->getOperand(0)->getType()->isPointerTy()) |
| 354 | return false; |
| 355 | |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 356 | Loop *L = LI->getLoopFor(ICmp->getParent()); |
| 357 | const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L); |
| 358 | const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 359 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 360 | if (!isAffineExpr(&CurRegion, LHS, *SE) || |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 361 | !isAffineExpr(&CurRegion, RHS, *SE)) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 362 | if (!AllowNonAffineSubRegions || |
| 363 | !addOverApproximatedRegion(RI->getRegionFor(&BB), Context)) |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 364 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, |
| 365 | RHS, ICmp); |
| 366 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | |
| 372 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 373 | if (CI.doesNotReturn()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 374 | return false; |
| 375 | |
| 376 | if (CI.doesNotAccessMemory()) |
| 377 | return true; |
| 378 | |
| 379 | Function *CalledFunction = CI.getCalledFunction(); |
| 380 | |
| 381 | // Indirect calls are not supported. |
| 382 | if (CalledFunction == 0) |
| 383 | return false; |
| 384 | |
Tobias Grosser | 9c0ffe3 | 2015-08-30 16:57:15 +0000 | [diff] [blame] | 385 | if (isIgnoredIntrinsic(&CI)) |
| 386 | return true; |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 387 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 391 | bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const { |
| 392 | // A reference to function argument or constant value is invariant. |
| 393 | if (isa<Argument>(Val) || isa<Constant>(Val)) |
| 394 | return true; |
| 395 | |
| 396 | const Instruction *I = dyn_cast<Instruction>(&Val); |
| 397 | if (!I) |
| 398 | return false; |
| 399 | |
| 400 | if (!Reg.contains(I)) |
| 401 | return true; |
| 402 | |
| 403 | if (I->mayHaveSideEffects()) |
| 404 | return false; |
| 405 | |
| 406 | // When Val is a Phi node, it is likely not invariant. We do not check whether |
| 407 | // Phi nodes are actually invariant, we assume that Phi nodes are usually not |
| 408 | // invariant. Recursively checking the operators of Phi nodes would lead to |
| 409 | // infinite recursion. |
| 410 | if (isa<PHINode>(*I)) |
| 411 | return false; |
| 412 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 413 | for (const Use &Operand : I->operands()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 414 | if (!isInvariant(*Operand, Reg)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 415 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 416 | |
| 417 | // When the instruction is a load instruction, check that no write to memory |
| 418 | // in the region aliases with the load. |
| 419 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | bdb4a39 | 2015-06-04 03:49:46 +0000 | [diff] [blame] | 420 | auto Loc = MemoryLocation::get(LI); |
Johannes Doerfert | ca08c44 | 2015-02-21 16:18:28 +0000 | [diff] [blame] | 421 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 422 | // Check if any basic block in the region can modify the location pointed to |
| 423 | // by 'Loc'. If so, 'Val' is (likely) not invariant in the region. |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 424 | for (const BasicBlock *BB : Reg.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 425 | if (AA->canBasicBlockModify(*BB, Loc)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 426 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 432 | MapInsnToMemAcc InsnToMemAcc; |
| 433 | |
Sebastian Pop | b57c099 | 2014-05-12 20:24:26 +0000 | [diff] [blame] | 434 | bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 435 | Region &CurRegion = Context.CurRegion; |
| 436 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 437 | for (const SCEVUnknown *BasePointer : Context.NonAffineAccesses) { |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 438 | Value *BaseValue = BasePointer->getValue(); |
Tobias Grosser | a5c092d | 2015-06-04 16:03:16 +0000 | [diff] [blame] | 439 | auto Shape = std::shared_ptr<ArrayShape>(new ArrayShape(BasePointer)); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 440 | bool BasePtrHasNonAffine = false; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 441 | |
| 442 | // First step: collect parametric terms in all array references. |
| 443 | SmallVector<const SCEV *, 4> Terms; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 444 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 445 | if (auto *AF = dyn_cast<SCEVAddRecExpr>(Pair.second)) |
Tobias Grosser | 23bceb2 | 2015-06-29 14:44:17 +0000 | [diff] [blame] | 446 | SE->collectParametricTerms(AF, Terms); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 447 | |
| 448 | // In case the outermost expression is a plain add, we check if any of its |
| 449 | // terms has the form 4 * %inst * %param * %param ..., aka a term that |
| 450 | // contains a product between a parameter and an instruction that is |
| 451 | // inside the scop. Such instructions, if allowed at all, are instructions |
| 452 | // SCEV can not represent, but Polly is still looking through. As a |
| 453 | // result, these instructions can depend on induction variables and are |
| 454 | // most likely no array sizes. However, terms that are multiplied with |
| 455 | // them are likely candidates for array sizes. |
| 456 | if (auto *AF = dyn_cast<SCEVAddExpr>(Pair.second)) { |
| 457 | for (auto Op : AF->operands()) { |
| 458 | if (auto *AF2 = dyn_cast<SCEVAddRecExpr>(Op)) |
| 459 | SE->collectParametricTerms(AF2, Terms); |
| 460 | if (auto *AF2 = dyn_cast<SCEVMulExpr>(Op)) { |
| 461 | SmallVector<const SCEV *, 0> Operands; |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 462 | |
| 463 | for (auto *MulOp : AF2->operands()) { |
| 464 | if (auto *Const = dyn_cast<SCEVConstant>(MulOp)) |
| 465 | Operands.push_back(Const); |
| 466 | if (auto *Unknown = dyn_cast<SCEVUnknown>(MulOp)) { |
| 467 | if (auto *Inst = dyn_cast<Instruction>(Unknown->getValue())) { |
| 468 | if (!Context.CurRegion.contains(Inst)) |
| 469 | Operands.push_back(MulOp); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 470 | |
| 471 | } else { |
| 472 | Operands.push_back(MulOp); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | Terms.push_back(SE->getMulExpr(Operands)); |
| 477 | } |
| 478 | } |
| 479 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 480 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 481 | |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 482 | // Second step: find array shape. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 483 | SE->findArrayDimensions(Terms, Shape->DelinearizedSizes, |
| 484 | Context.ElementSize[BasePointer]); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 485 | |
Johannes Doerfert | 8983031 | 2015-05-03 16:03:01 +0000 | [diff] [blame] | 486 | if (!AllowNonAffine) |
Tobias Grosser | 80e237b | 2015-07-29 13:52:05 +0000 | [diff] [blame] | 487 | for (const SCEV *DelinearizedSize : Shape->DelinearizedSizes) { |
| 488 | if (auto *Unknown = dyn_cast<SCEVUnknown>(DelinearizedSize)) { |
| 489 | auto *value = dyn_cast<Value>(Unknown->getValue()); |
| 490 | if (isa<UndefValue>(value)) { |
| 491 | invalid<ReportDifferentArrayElementSize>( |
| 492 | Context, /*Assert=*/true, |
| 493 | Context.Accesses[BasePointer].front().first, BaseValue); |
| 494 | return false; |
| 495 | } |
| 496 | } |
Johannes Doerfert | 8983031 | 2015-05-03 16:03:01 +0000 | [diff] [blame] | 497 | if (hasScalarDepsInsideRegion(DelinearizedSize, &CurRegion)) |
| 498 | invalid<ReportNonAffineAccess>( |
| 499 | Context, /*Assert=*/true, DelinearizedSize, |
| 500 | Context.Accesses[BasePointer].front().first, BaseValue); |
Tobias Grosser | 80e237b | 2015-07-29 13:52:05 +0000 | [diff] [blame] | 501 | } |
Johannes Doerfert | 8983031 | 2015-05-03 16:03:01 +0000 | [diff] [blame] | 502 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 503 | // No array shape derived. |
| 504 | if (Shape->DelinearizedSizes.empty()) { |
| 505 | if (AllowNonAffine) |
| 506 | continue; |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 507 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 508 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 509 | const Instruction *Insn = Pair.first; |
| 510 | const SCEV *AF = Pair.second; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 511 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 512 | if (!isAffineExpr(&CurRegion, AF, *SE, BaseValue)) { |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 513 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn, |
| 514 | BaseValue); |
| 515 | if (!KeepGoing) |
| 516 | return false; |
| 517 | } |
| 518 | } |
| 519 | continue; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 520 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 521 | |
| 522 | // Third step: compute the access functions for each subscript. |
| 523 | // |
| 524 | // We first store the resulting memory accesses in TempMemoryAccesses. Only |
| 525 | // if the access functions for all memory accesses have been successfully |
| 526 | // delinearized we continue. Otherwise, we either report a failure or, if |
| 527 | // non-affine accesses are allowed, we drop the information. In case the |
| 528 | // information is dropped the memory accesses need to be overapproximated |
| 529 | // when translated to a polyhedral representation. |
| 530 | MapInsnToMemAcc TempMemoryAccesses; |
| 531 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 532 | const Instruction *Insn = Pair.first; |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 533 | auto *AF = Pair.second; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 534 | bool IsNonAffine = false; |
Tobias Grosser | d8308fb | 2015-06-05 05:52:15 +0000 | [diff] [blame] | 535 | TempMemoryAccesses.insert(std::make_pair(Insn, MemAcc(Insn, Shape))); |
Tobias Grosser | a5c092d | 2015-06-04 16:03:16 +0000 | [diff] [blame] | 536 | MemAcc *Acc = &TempMemoryAccesses.find(Insn)->second; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 537 | |
| 538 | if (!AF) { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 539 | if (isAffineExpr(&CurRegion, Pair.second, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 540 | Acc->DelinearizedSubscripts.push_back(Pair.second); |
| 541 | else |
| 542 | IsNonAffine = true; |
| 543 | } else { |
Tobias Grosser | 23bceb2 | 2015-06-29 14:44:17 +0000 | [diff] [blame] | 544 | SE->computeAccessFunctions(AF, Acc->DelinearizedSubscripts, |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 545 | Shape->DelinearizedSizes); |
| 546 | if (Acc->DelinearizedSubscripts.size() == 0) |
| 547 | IsNonAffine = true; |
| 548 | for (const SCEV *S : Acc->DelinearizedSubscripts) |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 549 | if (!isAffineExpr(&CurRegion, S, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 550 | IsNonAffine = true; |
| 551 | } |
| 552 | |
| 553 | // (Possibly) report non affine access |
| 554 | if (IsNonAffine) { |
| 555 | BasePtrHasNonAffine = true; |
| 556 | if (!AllowNonAffine) |
Tobias Grosser | 021eaef | 2015-01-08 19:03:10 +0000 | [diff] [blame] | 557 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second, |
| 558 | Insn, BaseValue); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 559 | if (!KeepGoing && !AllowNonAffine) |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (!BasePtrHasNonAffine) |
| 565 | InsnToMemAcc.insert(TempMemoryAccesses.begin(), TempMemoryAccesses.end()); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 566 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 567 | return true; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 570 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 571 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 572 | Region &CurRegion = Context.CurRegion; |
| 573 | |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 574 | Value *Ptr = getPointerOperand(Inst); |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 575 | Loop *L = LI->getLoopFor(Inst.getParent()); |
| 576 | const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 577 | const SCEVUnknown *BasePointer; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 578 | Value *BaseValue; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 579 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 580 | BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction)); |
| 581 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 582 | if (!BasePointer) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 583 | return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 584 | |
| 585 | BaseValue = BasePointer->getValue(); |
| 586 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 587 | if (isa<UndefValue>(BaseValue)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 588 | return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 589 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 590 | // Check that the base address of the access is invariant in the current |
| 591 | // region. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 592 | if (!isInvariant(*BaseValue, CurRegion)) |
Tobias Grosser | ab2227a | 2014-01-28 13:43:24 +0000 | [diff] [blame] | 593 | // Verification of this property is difficult as the independent blocks |
| 594 | // pass may introduce aliasing that we did not have when running the |
| 595 | // scop detection. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 596 | return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue, |
| 597 | &Inst); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 598 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 599 | AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer); |
| 600 | |
Tobias Grosser | bcd4eff | 2014-09-13 14:47:40 +0000 | [diff] [blame] | 601 | const SCEV *Size = SE->getElementSize(&Inst); |
| 602 | if (Context.ElementSize.count(BasePointer)) { |
| 603 | if (Context.ElementSize[BasePointer] != Size) |
| 604 | return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true, |
| 605 | &Inst, BaseValue); |
| 606 | } else { |
| 607 | Context.ElementSize[BasePointer] = Size; |
| 608 | } |
| 609 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 610 | bool isVariantInNonAffineLoop = false; |
| 611 | SetVector<const Loop *> Loops; |
| 612 | findLoops(AccessFunction, Loops); |
| 613 | for (const Loop *L : Loops) |
| 614 | if (Context.BoxedLoopsSet.count(L)) |
| 615 | isVariantInNonAffineLoop = true; |
| 616 | |
| 617 | if (PollyDelinearize && !isVariantInNonAffineLoop) { |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 618 | Context.Accesses[BasePointer].push_back({&Inst, AccessFunction}); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 619 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 620 | if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 621 | Context.NonAffineAccesses.insert(BasePointer); |
| 622 | } else if (!AllowNonAffine) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 623 | if (isVariantInNonAffineLoop || |
| 624 | !isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue)) |
Sebastian Pop | cd3bb59 | 2014-04-10 16:08:11 +0000 | [diff] [blame] | 625 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, |
Andreas Simbuerger | d46b935 | 2014-08-17 10:09:11 +0000 | [diff] [blame] | 626 | AccessFunction, &Inst, BaseValue); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 627 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 628 | |
| 629 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 630 | // created by IndependentBlocks Pass. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 631 | if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BaseValue)) |
| 632 | return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 633 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 634 | if (IgnoreAliasing) |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 635 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 636 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 637 | // Check if the base pointer of the memory access does alias with |
| 638 | // any other pointer. This cannot be handled at the moment. |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 639 | AAMDNodes AATags; |
| 640 | Inst.getAAMetadata(AATags); |
| 641 | AliasSet &AS = Context.AST.getAliasSetForPointer( |
Chandler Carruth | afa4ea7 | 2015-06-17 08:29:32 +0000 | [diff] [blame] | 642 | BaseValue, MemoryLocation::UnknownSize, AATags); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 643 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 644 | // INVALID triggers an assertion in verifying mode, if it detects that a |
| 645 | // SCoP was detected by SCoP detection and that this SCoP was invalidated by |
| 646 | // a pass that stated it would preserve the SCoPs. We disable this check as |
| 647 | // the independent blocks pass may create memory references which seem to |
| 648 | // alias, if -basicaa is not available. They actually do not, but as we can |
| 649 | // not proof this without -basicaa we would fail. We disable this check to |
| 650 | // not cause irrelevant verification failures. |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 651 | if (!AS.isMustAlias()) { |
| 652 | if (PollyUseRuntimeAliasChecks) { |
| 653 | bool CanBuildRunTimeCheck = true; |
| 654 | // The run-time alias check places code that involves the base pointer at |
| 655 | // the beginning of the SCoP. This breaks if the base pointer is defined |
| 656 | // inside the scop. Hence, we can only create a run-time check if we are |
| 657 | // sure the base pointer is not an instruction defined inside the scop. |
| 658 | for (const auto &Ptr : AS) { |
| 659 | Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue()); |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 660 | if (Inst && CurRegion.contains(Inst)) { |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 661 | CanBuildRunTimeCheck = false; |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (CanBuildRunTimeCheck) |
| 667 | return true; |
| 668 | } |
Andreas Simbuerger | e2c9243 | 2014-06-26 10:19:57 +0000 | [diff] [blame] | 669 | return invalid<ReportAlias>(Context, /*Assert=*/false, &Inst, AS); |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 670 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 675 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 676 | DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 677 | // We only check the call instruction but not invoke instruction. |
| 678 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 679 | if (isValidCallInst(*CI)) |
| 680 | return true; |
| 681 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 682 | return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 686 | if (!isa<AllocaInst>(Inst)) |
| 687 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 688 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 689 | return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // Check the access function. |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 693 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) { |
| 694 | Context.hasStores |= isa<StoreInst>(Inst); |
| 695 | Context.hasLoads |= isa<LoadInst>(Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 696 | return isValidMemoryAccess(Inst, Context); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 697 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 698 | |
| 699 | // We do not know this instruction, therefore we assume it is invalid. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 700 | return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 703 | bool ScopDetection::canUseISLTripCount(Loop *L, |
| 704 | DetectionContext &Context) const { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 705 | // Ensure the loop has valid exiting blocks, otherwise we need to |
| 706 | // overapproximate it as a boxed loop. |
| 707 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 708 | L->getExitingBlocks(ExitingBlocks); |
| 709 | for (BasicBlock *ExitingBB : ExitingBlocks) { |
| 710 | if (!isValidCFG(*ExitingBB, Context)) |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 711 | return false; |
| 712 | } |
| 713 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 714 | // We can use ISL to compute the trip count of L. |
| 715 | return true; |
| 716 | } |
| 717 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 718 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 719 | if (canUseISLTripCount(L, Context)) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 720 | Context.hasAffineLoops = true; |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 721 | return true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 724 | if (AllowNonAffineSubLoops && AllowNonAffineSubRegions) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 725 | Region *R = RI->getRegionFor(L->getHeader()); |
| 726 | if (R->contains(L)) |
| 727 | if (addOverApproximatedRegion(R, Context)) |
| 728 | return true; |
| 729 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 730 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 731 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 732 | return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 735 | /// @brief Return the number of loops in @p L (incl. @p L) that have a trip |
Johannes Doerfert | e526de5 | 2015-09-21 19:10:11 +0000 | [diff] [blame] | 736 | /// count that is not known to be less than MIN_LOOP_TRIP_COUNT. |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 737 | static unsigned countBeneficialLoops(Loop *L, ScalarEvolution &SE) { |
| 738 | auto *TripCount = SE.getBackedgeTakenCount(L); |
| 739 | |
| 740 | auto count = 1; |
| 741 | if (auto *TripCountC = dyn_cast<SCEVConstant>(TripCount)) |
Johannes Doerfert | e526de5 | 2015-09-21 19:10:11 +0000 | [diff] [blame] | 742 | if (TripCountC->getValue()->getZExtValue() < MIN_LOOP_TRIP_COUNT) |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 743 | count -= 1; |
| 744 | |
| 745 | for (auto &SubLoop : *L) |
| 746 | count += countBeneficialLoops(SubLoop, SE); |
| 747 | |
| 748 | return count; |
| 749 | } |
| 750 | |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 751 | bool ScopDetection::hasMoreThanOneLoop(Region *R) const { |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 752 | auto LoopNum = 0; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 753 | |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 754 | auto L = LI->getLoopFor(R->getEntry()); |
| 755 | L = L ? R->outermostLoopInRegion(L) : nullptr; |
| 756 | L = L ? L->getParentLoop() : nullptr; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 757 | |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 758 | auto SubLoops = |
| 759 | L ? L->getSubLoopsVector() : std::vector<Loop *>(LI->begin(), LI->end()); |
| 760 | |
| 761 | for (auto &SubLoop : SubLoops) |
| 762 | if (R->contains(SubLoop)) { |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 763 | LoopNum += countBeneficialLoops(SubLoop, *SE); |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 764 | |
| 765 | if (LoopNum >= 2) |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 766 | return true; |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 767 | } |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 771 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 772 | // Initial no valid region was found (greater than R) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 773 | std::unique_ptr<Region> LastValidRegion; |
| 774 | auto ExpandedRegion = std::unique_ptr<Region>(R.getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 775 | |
| 776 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 777 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 778 | while (ExpandedRegion) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 779 | DetectionContext Context( |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 780 | *ExpandedRegion, *AA, NonAffineSubRegionMap[ExpandedRegion.get()], |
| 781 | BoxedLoopsMap[ExpandedRegion.get()], false /* verifying */); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 782 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 783 | // Only expand when we did not collect errors. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 784 | |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame] | 785 | if (!Context.Log.hasErrors()) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 786 | // If the exit is valid check all blocks |
| 787 | // - if true, a valid region was found => store it + keep expanding |
| 788 | // - if false, .tbd. => stop (should this really end the loop?) |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 789 | if (!allBlocksValid(Context) || Context.Log.hasErrors()) |
| 790 | break; |
| 791 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 792 | // Store this region, because it is the greatest valid (encountered so |
| 793 | // far). |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 794 | LastValidRegion = std::move(ExpandedRegion); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 795 | |
| 796 | // Create and test the next greater region (if any) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 797 | ExpandedRegion = |
| 798 | std::unique_ptr<Region>(LastValidRegion->getExpandedRegion()); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 799 | |
| 800 | } else { |
| 801 | // Create and test the next greater region (if any) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 802 | ExpandedRegion = |
| 803 | std::unique_ptr<Region>(ExpandedRegion->getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 804 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 807 | DEBUG({ |
| 808 | if (LastValidRegion) |
| 809 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 810 | else |
| 811 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 812 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 813 | |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 814 | return LastValidRegion.release(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 815 | } |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 816 | static bool regionWithoutLoops(Region &R, LoopInfo *LI) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 817 | for (const BasicBlock *BB : R.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 818 | if (R.contains(LI->getLoopFor(BB))) |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 819 | return false; |
| 820 | |
| 821 | return true; |
| 822 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 823 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 824 | // Remove all direct and indirect children of region R from the region set Regs, |
| 825 | // but do not recurse further if the first child has been found. |
| 826 | // |
| 827 | // Return the number of regions erased from Regs. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 828 | static unsigned eraseAllChildren(ScopDetection::RegionSet &Regs, |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 829 | const Region &R) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 830 | unsigned Count = 0; |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 831 | for (auto &SubRegion : R) { |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 832 | if (Regs.count(SubRegion.get())) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 833 | ++Count; |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 834 | Regs.remove(SubRegion.get()); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 835 | } else { |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 836 | Count += eraseAllChildren(Regs, *SubRegion); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | return Count; |
| 840 | } |
| 841 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 842 | void ScopDetection::findScops(Region &R) { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 843 | DetectionContext Context(R, *AA, NonAffineSubRegionMap[&R], BoxedLoopsMap[&R], |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 844 | false /*verifying*/); |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 845 | |
| 846 | bool RegionIsValid = false; |
Tobias Grosser | 02e6589 | 2015-09-08 19:46:41 +0000 | [diff] [blame] | 847 | if (!DetectUnprofitable && regionWithoutLoops(R, LI)) |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 848 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R); |
| 849 | else |
| 850 | RegionIsValid = isValidRegion(Context); |
| 851 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 852 | bool HasErrors = !RegionIsValid || Context.Log.size() > 0; |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 853 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 854 | if (PollyTrackFailures && HasErrors) |
| 855 | RejectLogs.insert(std::make_pair(&R, Context.Log)); |
| 856 | |
| 857 | if (!HasErrors) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 858 | ++ValidRegion; |
| 859 | ValidRegions.insert(&R); |
| 860 | return; |
| 861 | } |
| 862 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 863 | for (auto &SubRegion : R) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 864 | findScops(*SubRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 865 | |
| 866 | // Try to expand regions. |
| 867 | // |
| 868 | // As the region tree normally only contains canonical regions, non canonical |
| 869 | // regions that form a Scop are not found. Therefore, those non canonical |
| 870 | // regions are checked by expanding the canonical ones. |
| 871 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 872 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 873 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 874 | for (auto &SubRegion : R) |
| 875 | ToExpand.push_back(SubRegion.get()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 876 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 877 | for (Region *CurrentRegion : ToExpand) { |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 878 | // Skip regions that had errors. |
| 879 | bool HadErrors = RejectLogs.hasErrors(CurrentRegion); |
| 880 | if (HadErrors) |
| 881 | continue; |
| 882 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 883 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 884 | // an already expanded region. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 885 | if (!ValidRegions.count(CurrentRegion)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 886 | continue; |
| 887 | |
| 888 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 889 | |
| 890 | if (!ExpandedR) |
| 891 | continue; |
| 892 | |
| 893 | R.addSubRegion(ExpandedR, true); |
| 894 | ValidRegions.insert(ExpandedR); |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 895 | ValidRegions.remove(CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 896 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 897 | // Erase all (direct and indirect) children of ExpandedR from the valid |
| 898 | // regions and update the number of valid regions. |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 899 | ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 904 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 905 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 906 | for (const BasicBlock *BB : CurRegion.blocks()) { |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 907 | Loop *L = LI->getLoopFor(BB); |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 908 | if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing)) |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 909 | return false; |
| 910 | } |
| 911 | |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 912 | for (BasicBlock *BB : CurRegion.blocks()) { |
| 913 | // Do not check exception blocks as we will never include them in the SCoP. |
| 914 | if (isErrorBlock(*BB)) |
| 915 | continue; |
| 916 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 917 | if (!isValidCFG(*BB, Context) && !KeepGoing) |
Sebastian Pop | 9e3d2dd | 2013-06-11 22:20:27 +0000 | [diff] [blame] | 918 | return false; |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 919 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 920 | if (!isValidInstruction(*I, Context) && !KeepGoing) |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 921 | return false; |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 922 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 923 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 924 | if (!hasAffineMemoryAccesses(Context)) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 925 | return false; |
| 926 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 927 | return true; |
| 928 | } |
| 929 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 930 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 931 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 932 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 933 | DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 934 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 935 | if (CurRegion.isTopLevelRegion()) { |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 936 | DEBUG(dbgs() << "Top level region is invalid\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 940 | if (!CurRegion.getEntry()->getName().count(OnlyRegion)) { |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 941 | DEBUG({ |
| 942 | dbgs() << "Region entry does not match -polly-region-only"; |
| 943 | dbgs() << "\n"; |
| 944 | }); |
| 945 | return false; |
| 946 | } |
| 947 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 948 | if (!CurRegion.getEnteringBlock()) { |
| 949 | BasicBlock *entry = CurRegion.getEntry(); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 950 | Loop *L = LI->getLoopFor(entry); |
| 951 | |
Johannes Doerfert | f32f5f2 | 2015-09-28 01:30:37 +0000 | [diff] [blame] | 952 | if (L && !L->isLoopSimplifyForm()) |
| 953 | return invalid<ReportSimpleLoop>(Context, /*Assert=*/true); |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 956 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 957 | // to insert alloca instruction there when translate scalar to array. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 958 | if (CurRegion.getEntry() == |
| 959 | &(CurRegion.getEntry()->getParent()->getEntryBlock())) |
| 960 | return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 961 | |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 962 | if (!DetectUnprofitable && !hasMoreThanOneLoop(&CurRegion)) |
| 963 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
| 964 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 965 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 966 | return false; |
| 967 | |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 968 | // We can probably not do a lot on scops that only write or only read |
| 969 | // data. |
| 970 | if (!DetectUnprofitable && (!Context.hasStores || !Context.hasLoads)) |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 971 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 972 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 973 | // Check if there was at least one non-overapproximated loop in the region or |
| 974 | // we allow regions without loops. |
Tobias Grosser | 02e6589 | 2015-09-08 19:46:41 +0000 | [diff] [blame] | 975 | if (!DetectUnprofitable && !Context.hasAffineLoops) |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 976 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
| 977 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 978 | DEBUG(dbgs() << "OK\n"); |
| 979 | return true; |
| 980 | } |
| 981 | |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 982 | void ScopDetection::markFunctionAsInvalid(Function *F) const { |
| 983 | F->addFnAttr(PollySkipFnAttr); |
| 984 | } |
| 985 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 986 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 987 | return !F.hasFnAttribute(PollySkipFnAttr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 990 | void ScopDetection::printLocations(llvm::Function &F) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 991 | for (const Region *R : *this) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 992 | unsigned LineEntry, LineExit; |
| 993 | std::string FileName; |
| 994 | |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 995 | getDebugLocation(R, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 996 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 997 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Daniel Jasper | 8a1dea0 | 2014-10-27 19:45:31 +0000 | [diff] [blame] | 1001 | void ScopDetection::emitMissedRemarksForValidRegions( |
| 1002 | const Function &F, const RegionSet &ValidRegions) { |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 1003 | for (const Region *R : ValidRegions) { |
| 1004 | const Region *Parent = R->getParent(); |
| 1005 | if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent)) |
| 1006 | emitRejectionRemarks(F, RejectLogs.at(Parent)); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | void ScopDetection::emitMissedRemarksForLeaves(const Function &F, |
| 1011 | const Region *R) { |
| 1012 | for (const std::unique_ptr<Region> &Child : *R) { |
| 1013 | bool IsValid = ValidRegions.count(Child.get()); |
| 1014 | if (IsValid) |
| 1015 | continue; |
| 1016 | |
| 1017 | bool IsLeaf = Child->begin() == Child->end(); |
| 1018 | if (!IsLeaf) |
| 1019 | emitMissedRemarksForLeaves(F, Child.get()); |
| 1020 | else { |
| 1021 | if (RejectLogs.count(Child.get())) { |
| 1022 | emitRejectionRemarks(F, RejectLogs.at(Child.get())); |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1028 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1029 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1030 | RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); |
Tobias Grosser | 02e6589 | 2015-09-08 19:46:41 +0000 | [diff] [blame] | 1031 | if (!DetectUnprofitable && LI->empty()) |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 1032 | return false; |
| 1033 | |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1034 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 1035 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1036 | Region *TopRegion = RI->getTopLevelRegion(); |
| 1037 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 1038 | releaseMemory(); |
| 1039 | |
Tobias Grosser | a3ab27e | 2014-05-07 11:23:32 +0000 | [diff] [blame] | 1040 | if (OnlyFunction != "" && !F.getName().count(OnlyFunction)) |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 1043 | if (!isValidFunction(F)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1044 | return false; |
| 1045 | |
| 1046 | findScops(*TopRegion); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1047 | |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 1048 | // Only makes sense when we tracked errors. |
| 1049 | if (PollyTrackFailures) { |
| 1050 | emitMissedRemarksForValidRegions(F, ValidRegions); |
| 1051 | emitMissedRemarksForLeaves(F, TopRegion); |
| 1052 | } |
| 1053 | |
| 1054 | for (const Region *R : ValidRegions) |
| 1055 | emitValidRemarks(F, R); |
| 1056 | |
Johannes Doerfert | a05214f | 2014-10-15 23:24:28 +0000 | [diff] [blame] | 1057 | if (ReportLevel) |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 1058 | printLocations(F); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1059 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1060 | return false; |
| 1061 | } |
| 1062 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1063 | bool ScopDetection::isNonAffineSubRegion(const Region *SubR, |
| 1064 | const Region *ScopR) const { |
| 1065 | return NonAffineSubRegionMap.lookup(ScopR).count(SubR); |
| 1066 | } |
| 1067 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1068 | const ScopDetection::BoxedLoopsSetTy * |
| 1069 | ScopDetection::getBoxedLoops(const Region *R) const { |
| 1070 | auto BLMIt = BoxedLoopsMap.find(R); |
| 1071 | if (BLMIt == BoxedLoopsMap.end()) |
| 1072 | return nullptr; |
| 1073 | return &BLMIt->second; |
| 1074 | } |
| 1075 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1076 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 1077 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1078 | |
| 1079 | BoxedLoopsSetTy DummyBoxedLoopsSet; |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1080 | NonAffineSubRegionSetTy DummyNonAffineSubRegionSet; |
| 1081 | DetectionContext Context(const_cast<Region &>(R), *AA, |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1082 | DummyNonAffineSubRegionSet, DummyBoxedLoopsSet, |
| 1083 | true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1084 | isValidRegion(Context); |
| 1085 | } |
| 1086 | |
| 1087 | void polly::ScopDetection::verifyAnalysis() const { |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 1088 | if (!VerifyScops) |
| 1089 | return; |
| 1090 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1091 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1092 | verifyRegion(*R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1096 | AU.addRequired<LoopInfoWrapperPass>(); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 1097 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1098 | // We also need AA and RegionInfo when we are verifying analysis. |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1099 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1100 | AU.addRequiredTransitive<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1101 | AU.setPreservesAll(); |
| 1102 | } |
| 1103 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1104 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1105 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1106 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1107 | |
| 1108 | OS << "\n"; |
| 1109 | } |
| 1110 | |
| 1111 | void ScopDetection::releaseMemory() { |
| 1112 | ValidRegions.clear(); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 1113 | RejectLogs.clear(); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1114 | NonAffineSubRegionMap.clear(); |
Tobias Grosser | 4b6aa6e | 2015-04-18 11:01:25 +0000 | [diff] [blame] | 1115 | InsnToMemAcc.clear(); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 1116 | |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 1117 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | char ScopDetection::ID = 0; |
| 1121 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1122 | Pass *polly::createScopDetectionPass() { return new ScopDetection(); } |
| 1123 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1124 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 1125 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1126 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1127 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1128 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1129 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 1130 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1131 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 1132 | "Polly - Detect static control parts (SCoPs)", false, false) |