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