Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Create a polyhedral description for a static control flow region. |
| 11 | // |
| 12 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 13 | // detection derived from their LLVM-IR code. |
| 14 | // |
| 15 | // This represantation is shared among several tools in the polyhedral |
| 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "polly/ScopInfo.h" |
| 21 | |
| 22 | #include "polly/TempScopInfo.h" |
| 23 | #include "polly/LinkAllPasses.h" |
| 24 | #include "polly/Support/GICHelper.h" |
| 25 | #include "polly/Support/ScopHelper.h" |
| 26 | |
| 27 | #include "llvm/Analysis/LoopInfo.h" |
| 28 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 29 | #include "llvm/Analysis/RegionIterator.h" |
| 30 | #include "llvm/Assembly/Writer.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include "llvm/ADT/SetVector.h" |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | |
| 35 | #define DEBUG_TYPE "polly-scops" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | |
| 38 | #include "isl/constraint.h" |
| 39 | #include "isl/set.h" |
| 40 | #include "isl/map.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 41 | #include "isl/aff.h" |
| 42 | #include "isl/printer.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 43 | #include "isl/local_space.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 44 | #include <sstream> |
| 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | using namespace llvm; |
| 49 | using namespace polly; |
| 50 | |
| 51 | STATISTIC(ScopFound, "Number of valid Scops"); |
| 52 | STATISTIC(RichScopFound, "Number of Scops containing a loop"); |
| 53 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 54 | /// Convert an int into a string. |
| 55 | static std::string convertInt(int number) |
| 56 | { |
| 57 | if (number == 0) |
| 58 | return "0"; |
| 59 | std::string temp = ""; |
| 60 | std::string returnvalue = ""; |
| 61 | while (number > 0) |
| 62 | { |
| 63 | temp += number % 10 + 48; |
| 64 | number /= 10; |
| 65 | } |
| 66 | for (unsigned i = 0; i < temp.length(); i++) |
| 67 | returnvalue+=temp[temp.length() - i - 1]; |
| 68 | return returnvalue; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 71 | /// Translate a SCEVExpression into an isl_pw_aff object. |
| 72 | struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff*> { |
| 73 | private: |
| 74 | isl_ctx *ctx; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 75 | int NbLoopSpaces; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 76 | const Scop *scop; |
| 77 | |
| 78 | /// baseAdress is set if we analyze a memory access. It holds the base address |
| 79 | /// of this memory access. |
| 80 | const Value *baseAddress; |
| 81 | |
| 82 | public: |
| 83 | static isl_pw_aff *getPwAff(const ScopStmt *stmt, const SCEV *scev, |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 84 | const Value *baseAddress = 0) { |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 85 | SCEVAffinator Affinator(stmt, baseAddress); |
| 86 | return Affinator.visit(scev); |
| 87 | } |
| 88 | |
| 89 | isl_pw_aff *visit(const SCEV *scev) { |
| 90 | // In case the scev is contained in our list of parameters, we do not |
| 91 | // further analyze this expression, but create a new parameter in the |
| 92 | // isl_pw_aff. This allows us to treat subexpressions that we cannot |
| 93 | // translate into an piecewise affine expression, as constant parameters of |
| 94 | // the piecewise affine expression. |
| 95 | int i = 0; |
| 96 | for (Scop::const_param_iterator PI = scop->param_begin(), |
| 97 | PE = scop->param_end(); PI != PE; ++PI) { |
| 98 | if (*PI == scev) { |
| 99 | isl_id *ID = isl_id_alloc(ctx, ("p" + convertInt(i)).c_str(), |
| 100 | (void *) scev); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 101 | isl_space *Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces); |
| 102 | Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 103 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 104 | isl_set *Domain = isl_set_universe(isl_space_copy(Space)); |
| 105 | isl_aff *Affine = isl_aff_zero_on_domain( |
| 106 | isl_local_space_from_space(Space)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 107 | Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1); |
| 108 | |
| 109 | return isl_pw_aff_alloc(Domain, Affine); |
| 110 | } |
| 111 | i++; |
| 112 | } |
| 113 | |
| 114 | return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev); |
| 115 | } |
| 116 | |
| 117 | SCEVAffinator(const ScopStmt *stmt, const Value *baseAddress) : |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 118 | ctx(stmt->getIslCtx()), |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 119 | NbLoopSpaces(stmt->getNumIterators()), |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 120 | scop(stmt->getParent()), |
| 121 | baseAddress(baseAddress) {}; |
| 122 | |
| 123 | __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) { |
| 124 | ConstantInt *Value = Constant->getValue(); |
| 125 | isl_int v; |
| 126 | isl_int_init(v); |
| 127 | |
| 128 | // LLVM does not define if an integer value is interpreted as a signed or |
| 129 | // unsigned value. Hence, without further information, it is unknown how |
| 130 | // this value needs to be converted to GMP. At the moment, we only support |
| 131 | // signed operations. So we just interpret it as signed. Later, there are |
| 132 | // two options: |
| 133 | // |
| 134 | // 1. We always interpret any value as signed and convert the values on |
| 135 | // demand. |
| 136 | // 2. We pass down the signedness of the calculation and use it to interpret |
| 137 | // this constant correctly. |
| 138 | MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true); |
| 139 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 140 | isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces); |
| 141 | isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space)); |
| 142 | isl_aff *Affine = isl_aff_zero_on_domain(ls); |
| 143 | isl_set *Domain = isl_set_universe(Space); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 144 | |
| 145 | Affine = isl_aff_add_constant(Affine, v); |
| 146 | isl_int_clear(v); |
| 147 | |
| 148 | return isl_pw_aff_alloc(Domain, Affine); |
| 149 | } |
| 150 | |
| 151 | __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr* Expr) { |
| 152 | assert(0 && "Not yet supported"); |
| 153 | } |
| 154 | |
| 155 | __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) { |
| 156 | assert(0 && "Not yet supported"); |
| 157 | } |
| 158 | |
| 159 | __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr* Expr) { |
| 160 | // Assuming the value is signed, a sign extension is basically a noop. |
| 161 | // TODO: Reconsider this as soon as we support unsigned values. |
| 162 | return visit(Expr->getOperand()); |
| 163 | } |
| 164 | |
| 165 | __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr* Expr) { |
| 166 | isl_pw_aff *Sum = visit(Expr->getOperand(0)); |
| 167 | |
| 168 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 169 | isl_pw_aff *NextSummand = visit(Expr->getOperand(i)); |
| 170 | Sum = isl_pw_aff_add(Sum, NextSummand); |
| 171 | } |
| 172 | |
| 173 | // TODO: Check for NSW and NUW. |
| 174 | |
| 175 | return Sum; |
| 176 | } |
| 177 | |
| 178 | __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr* Expr) { |
| 179 | isl_pw_aff *Product = visit(Expr->getOperand(0)); |
| 180 | |
| 181 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 182 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 183 | |
| 184 | if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) { |
| 185 | isl_pw_aff_free(Product); |
| 186 | isl_pw_aff_free(NextOperand); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | Product = isl_pw_aff_mul(Product, NextOperand); |
| 191 | } |
| 192 | |
| 193 | // TODO: Check for NSW and NUW. |
| 194 | return Product; |
| 195 | } |
| 196 | |
| 197 | __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr* Expr) { |
| 198 | assert(0 && "Not yet supported"); |
| 199 | } |
| 200 | |
| 201 | int getLoopDepth(const Loop *L) { |
| 202 | Loop *outerLoop = |
| 203 | scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L)); |
| 204 | return L->getLoopDepth() - outerLoop->getLoopDepth(); |
| 205 | } |
| 206 | |
| 207 | __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr* Expr) { |
| 208 | assert(Expr->isAffine() && "Only affine AddRecurrences allowed"); |
| 209 | |
| 210 | isl_pw_aff *Start = visit(Expr->getStart()); |
| 211 | isl_pw_aff *Step = visit(Expr->getOperand(1)); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 212 | isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces); |
| 213 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 214 | |
| 215 | int loopDimension = getLoopDepth(Expr->getLoop()); |
| 216 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 217 | isl_aff *LAff = isl_aff_set_coefficient_si( |
| 218 | isl_aff_zero_on_domain (LocalSpace), isl_dim_in, loopDimension, 1); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 219 | isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff); |
| 220 | |
| 221 | // TODO: Do we need to check for NSW and NUW? |
| 222 | return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff)); |
| 223 | } |
| 224 | |
| 225 | __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr* Expr) { |
| 226 | isl_pw_aff *Max = visit(Expr->getOperand(0)); |
| 227 | |
| 228 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 229 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 230 | Max = isl_pw_aff_max(Max, NextOperand); |
| 231 | } |
| 232 | |
| 233 | return Max; |
| 234 | } |
| 235 | |
| 236 | __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr* Expr) { |
| 237 | assert(0 && "Not yet supported"); |
| 238 | } |
| 239 | |
| 240 | __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown* Expr) { |
| 241 | Value *Value = Expr->getValue(); |
| 242 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 243 | isl_space *Space; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 244 | |
| 245 | /// If baseAddress is set, we ignore its Value object in the scev and do not |
| 246 | /// add it to the isl_pw_aff. This is because it is regarded as defining the |
| 247 | /// name of an array, in contrast to its array subscript. |
| 248 | if (baseAddress != Value) { |
| 249 | isl_id *ID = isl_id_alloc(ctx, Value->getNameStr().c_str(), Value); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 250 | Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces); |
| 251 | Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 252 | } else { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 253 | Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 256 | isl_set *Domain = isl_set_universe(isl_space_copy(Space)); |
| 257 | isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 258 | |
| 259 | if (baseAddress != Value) |
| 260 | Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1); |
| 261 | |
| 262 | return isl_pw_aff_alloc(Domain, Affine); |
| 263 | } |
| 264 | }; |
| 265 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | |
| 268 | MemoryAccess::~MemoryAccess() { |
Tobias Grosser | 54a86e6 | 2011-08-18 06:31:46 +0000 | [diff] [blame] | 269 | isl_map_free(AccessRelation); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 270 | isl_map_free(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void replace(std::string& str, const std::string& find, |
| 274 | const std::string& replace) { |
| 275 | size_t pos = 0; |
| 276 | while((pos = str.find(find, pos)) != std::string::npos) |
| 277 | { |
| 278 | str.replace(pos, find.length(), replace); |
| 279 | pos += replace.length(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void makeIslCompatible(std::string& str) { |
Tobias Grosser | eec4d56e | 2011-08-20 11:11:14 +0000 | [diff] [blame] | 284 | str.erase(0, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 285 | replace(str, ".", "_"); |
Tobias Grosser | 3b660f8 | 2011-08-03 00:12:11 +0000 | [diff] [blame] | 286 | replace(str, "\"", "_"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void MemoryAccess::setBaseName() { |
| 290 | raw_string_ostream OS(BaseName); |
| 291 | WriteAsOperand(OS, getBaseAddr(), false); |
| 292 | BaseName = OS.str(); |
| 293 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 294 | makeIslCompatible(BaseName); |
| 295 | BaseName = "MemRef_" + BaseName; |
| 296 | } |
| 297 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 298 | isl_map *MemoryAccess::getAccessRelation() const { |
| 299 | return isl_map_copy(AccessRelation); |
| 300 | } |
| 301 | |
| 302 | std::string MemoryAccess::getAccessRelationStr() const { |
| 303 | return stringFromIslObj(AccessRelation); |
| 304 | } |
| 305 | |
| 306 | isl_map *MemoryAccess::getNewAccessRelation() const { |
| 307 | return isl_map_copy(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 311 | isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0, |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 312 | Statement->getNumIterators(), 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 313 | setBaseName(); |
| 314 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 315 | Space = isl_space_set_tuple_name(Space, isl_dim_out, getBaseName().c_str()); |
| 316 | Space = isl_space_set_tuple_name(Space, isl_dim_in, Statement->getBaseName()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 317 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 318 | return isl_basic_map_universe(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) { |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 322 | newAccessRelation = NULL; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 323 | BaseAddr = AffFunc.getBaseAddr(); |
| 324 | Type = AffFunc.isRead() ? Read : Write; |
| 325 | statement = Statement; |
| 326 | |
| 327 | setBaseName(); |
| 328 | |
Tobias Grosser | 7d4cee4 | 2011-08-19 23:34:28 +0000 | [diff] [blame] | 329 | isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, AffFunc.OriginalSCEV, |
| 330 | AffFunc.getBaseAddr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 331 | |
Tobias Grosser | 7d4cee4 | 2011-08-19 23:34:28 +0000 | [diff] [blame] | 332 | // Devide the access function by the size of the elements in the array. |
| 333 | // |
| 334 | // A stride one array access in C expressed as A[i] is expressed in LLVM-IR |
| 335 | // as something like A[i * elementsize]. This hides the fact that two |
| 336 | // subsequent values of 'i' index two values that are stored next to each |
| 337 | // other in memory. By this devision we make this characteristic obvious |
| 338 | // again. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 339 | isl_int v; |
| 340 | isl_int_init(v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 341 | isl_int_set_si(v, AffFunc.getElemSizeInBytes()); |
Tobias Grosser | 7d4cee4 | 2011-08-19 23:34:28 +0000 | [diff] [blame] | 342 | Affine = isl_pw_aff_scale_down(Affine, v); |
| 343 | isl_int_clear(v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 344 | |
Tobias Grosser | 7d4cee4 | 2011-08-19 23:34:28 +0000 | [diff] [blame] | 345 | AccessRelation = isl_map_from_pw_aff(Affine); |
| 346 | AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in, |
| 347 | Statement->getBaseName()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 348 | AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out, |
| 349 | getBaseName().c_str()); |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 350 | |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 351 | isl_space *ParamSpace = Statement->getParent()->getParamSpace(); |
| 352 | AccessRelation = isl_map_align_params(AccessRelation, ParamSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) { |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 356 | newAccessRelation = NULL; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 357 | BaseAddr = BaseAddress; |
| 358 | Type = Read; |
| 359 | statement = Statement; |
| 360 | |
| 361 | isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement); |
| 362 | AccessRelation = isl_map_from_basic_map(BasicAccessMap); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 363 | isl_space *ParamSpace = Statement->getParent()->getParamSpace(); |
| 364 | AccessRelation = isl_map_align_params(AccessRelation, ParamSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void MemoryAccess::print(raw_ostream &OS) const { |
| 368 | OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n"; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 369 | OS.indent(16) << getAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void MemoryAccess::dump() const { |
| 373 | print(errs()); |
| 374 | } |
| 375 | |
| 376 | // Create a map in the size of the provided set domain, that maps from the |
| 377 | // one element of the provided set domain to another element of the provided |
| 378 | // set domain. |
| 379 | // The mapping is limited to all points that are equal in all but the last |
| 380 | // dimension and for which the last dimension of the input is strict smaller |
| 381 | // than the last dimension of the output. |
| 382 | // |
| 383 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 384 | // |
| 385 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 386 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 387 | // |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 388 | static isl_map *getEqualAndLarger(isl_space *setDomain) { |
| 389 | isl_space *mapDomain = isl_space_map_from_set(setDomain); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 390 | isl_basic_map *bmap = isl_basic_map_universe(mapDomain); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 391 | isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 392 | |
| 393 | // Set all but the last dimension to be equal for the input and output |
| 394 | // |
| 395 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 396 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
| 397 | for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) { |
| 398 | isl_int v; |
| 399 | isl_int_init(v); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 400 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 401 | |
| 402 | isl_int_set_si(v, 1); |
| 403 | isl_constraint_set_coefficient(c, isl_dim_in, i, v); |
| 404 | isl_int_set_si(v, -1); |
| 405 | isl_constraint_set_coefficient(c, isl_dim_out, i, v); |
| 406 | |
| 407 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 408 | |
| 409 | isl_int_clear(v); |
| 410 | } |
| 411 | |
| 412 | // Set the last dimension of the input to be strict smaller than the |
| 413 | // last dimension of the output. |
| 414 | // |
| 415 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
| 416 | // |
| 417 | unsigned lastDimension = isl_basic_map_n_in(bmap) - 1; |
| 418 | isl_int v; |
| 419 | isl_int_init(v); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 420 | isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 421 | isl_int_set_si(v, -1); |
| 422 | isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v); |
| 423 | isl_int_set_si(v, 1); |
| 424 | isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v); |
| 425 | isl_int_set_si(v, -1); |
| 426 | isl_constraint_set_constant(c, v); |
| 427 | isl_int_clear(v); |
| 428 | |
| 429 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 430 | |
| 431 | return isl_map_from_basic_map(bmap); |
| 432 | } |
| 433 | |
| 434 | isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 435 | isl_map *accessRelation = getAccessRelation(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 436 | isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset)); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 437 | isl_map *scattering = getStatement()->getScattering(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 438 | |
| 439 | scattering = isl_map_reverse(scattering); |
| 440 | int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain); |
| 441 | scattering = isl_map_project_out(scattering, isl_dim_in, |
| 442 | isl_set_n_dim(scatteringDomain), |
| 443 | difference); |
| 444 | |
| 445 | // Remove all names of the scattering dimensions, as the names may be lost |
| 446 | // anyways during the project. This leads to consistent results. |
| 447 | scattering = isl_map_set_tuple_name(scattering, isl_dim_in, ""); |
| 448 | scatteringDomain = isl_set_set_tuple_name(scatteringDomain, ""); |
| 449 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 450 | isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 451 | nextScatt = isl_map_lexmin(nextScatt); |
| 452 | |
| 453 | scattering = isl_map_intersect_domain(scattering, scatteringDomain); |
| 454 | |
| 455 | nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering)); |
| 456 | nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation)); |
| 457 | nextScatt = isl_map_apply_domain(nextScatt, scattering); |
| 458 | nextScatt = isl_map_apply_domain(nextScatt, accessRelation); |
| 459 | |
| 460 | return isl_map_deltas(nextScatt); |
| 461 | } |
| 462 | |
| 463 | bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const { |
| 464 | isl_set *stride = getStride(domainSubset); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 465 | isl_space *StrideSpace = isl_set_get_space(stride); |
| 466 | isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace); |
| 467 | isl_constraint *c = isl_equality_alloc(StrideLS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 468 | |
| 469 | isl_int v; |
| 470 | isl_int_init(v); |
| 471 | isl_int_set_si(v, 1); |
| 472 | isl_constraint_set_coefficient(c, isl_dim_set, 0, v); |
| 473 | isl_int_set_si(v, 0); |
| 474 | isl_constraint_set_constant(c, v); |
| 475 | isl_int_clear(v); |
| 476 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 477 | isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 478 | |
| 479 | bset = isl_basic_set_add_constraint(bset, c); |
| 480 | isl_set *strideZero = isl_set_from_basic_set(bset); |
| 481 | |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 482 | bool isStrideZero = isl_set_is_equal(stride, strideZero); |
| 483 | |
| 484 | isl_set_free(strideZero); |
| 485 | isl_set_free(stride); |
| 486 | |
| 487 | return isStrideZero; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const { |
| 491 | isl_set *stride = getStride(domainSubset); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 492 | isl_space *StrideSpace = isl_set_get_space(stride); |
| 493 | isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace); |
| 494 | isl_constraint *c = isl_equality_alloc(StrideLSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 495 | |
| 496 | isl_int v; |
| 497 | isl_int_init(v); |
| 498 | isl_int_set_si(v, 1); |
| 499 | isl_constraint_set_coefficient(c, isl_dim_set, 0, v); |
| 500 | isl_int_set_si(v, -1); |
| 501 | isl_constraint_set_constant(c, v); |
| 502 | isl_int_clear(v); |
| 503 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 504 | isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 505 | |
| 506 | bset = isl_basic_set_add_constraint(bset, c); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 507 | isl_set *strideOne = isl_set_from_basic_set(bset); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 508 | |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 509 | bool isStrideOne = isl_set_is_equal(stride, strideOne); |
| 510 | |
| 511 | isl_set_free(strideOne); |
| 512 | isl_set_free(stride); |
| 513 | |
| 514 | return isStrideOne; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 517 | void MemoryAccess::setNewAccessRelation(isl_map *newAccess) { |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 518 | isl_map_free(newAccessRelation); |
Raghesh Aloor | 7a04f4f | 2011-08-03 13:47:59 +0000 | [diff] [blame] | 519 | newAccessRelation = newAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 520 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 521 | |
| 522 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 523 | |
| 524 | isl_map *ScopStmt::getScattering() const { |
| 525 | return isl_map_copy(Scattering); |
| 526 | } |
| 527 | |
| 528 | void ScopStmt::setScattering(isl_map *NewScattering) { |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 529 | isl_map_free(Scattering); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 530 | Scattering = NewScattering; |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 533 | void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) { |
| 534 | unsigned NumberOfIterators = getNumIterators(); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 535 | unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1; |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 536 | isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators, |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 537 | ScatSpace); |
| 538 | Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering"); |
| 539 | Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName()); |
| 540 | isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space)); |
| 541 | isl_basic_map *bmap = isl_basic_map_universe(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 542 | isl_int v; |
| 543 | isl_int_init(v); |
| 544 | |
| 545 | // Loop dimensions. |
| 546 | for (unsigned i = 0; i < NumberOfIterators; ++i) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 547 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 548 | isl_int_set_si(v, 1); |
| 549 | isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v); |
| 550 | isl_int_set_si(v, -1); |
| 551 | isl_constraint_set_coefficient(c, isl_dim_in, i, v); |
| 552 | |
| 553 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 554 | } |
| 555 | |
| 556 | // Constant dimensions |
| 557 | for (unsigned i = 0; i < NumberOfIterators + 1; ++i) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 558 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 559 | isl_int_set_si(v, -1); |
| 560 | isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v); |
| 561 | isl_int_set_si(v, Scatter[i]); |
| 562 | isl_constraint_set_constant(c, v); |
| 563 | |
| 564 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 565 | } |
| 566 | |
| 567 | // Fill scattering dimensions. |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 568 | for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) { |
| 569 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 570 | isl_int_set_si(v, 1); |
| 571 | isl_constraint_set_coefficient(c, isl_dim_out, i, v); |
| 572 | isl_int_set_si(v, 0); |
| 573 | isl_constraint_set_constant(c, v); |
| 574 | |
| 575 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 576 | } |
| 577 | |
| 578 | isl_int_clear(v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 579 | Scattering = isl_map_from_basic_map(bmap); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 580 | Scattering = isl_map_align_params(Scattering, Parent.getParamSpace()); |
Tobias Grosser | 0ad4caa | 2011-10-08 00:35:17 +0000 | [diff] [blame^] | 581 | isl_local_space_free(LSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) { |
| 585 | const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB); |
| 586 | |
| 587 | for (AccFuncSetType::const_iterator I = AccFuncs->begin(), |
| 588 | E = AccFuncs->end(); I != E; ++I) { |
| 589 | MemAccs.push_back(new MemoryAccess(I->first, this)); |
| 590 | InstructionToAccess[I->second] = MemAccs.back(); |
| 591 | } |
| 592 | } |
| 593 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 594 | __isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp, |
| 595 | __isl_take isl_space *Space) |
| 596 | const { |
| 597 | |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 598 | isl_pw_aff *LHS = SCEVAffinator::getPwAff(this, Comp.getLHS()->OriginalSCEV, |
| 599 | 0); |
| 600 | isl_pw_aff *RHS = SCEVAffinator::getPwAff(this, Comp.getRHS()->OriginalSCEV, |
| 601 | 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 602 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 603 | isl_set *Set; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 604 | |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 605 | switch (Comp.getPred()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 606 | case ICmpInst::ICMP_EQ: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 607 | Set = isl_pw_aff_eq_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 608 | break; |
| 609 | case ICmpInst::ICMP_NE: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 610 | Set = isl_pw_aff_ne_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 611 | break; |
| 612 | case ICmpInst::ICMP_SLT: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 613 | Set = isl_pw_aff_lt_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 614 | break; |
| 615 | case ICmpInst::ICMP_SLE: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 616 | Set = isl_pw_aff_le_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 617 | break; |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 618 | case ICmpInst::ICMP_SGT: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 619 | Set = isl_pw_aff_gt_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 620 | break; |
| 621 | case ICmpInst::ICMP_SGE: |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 622 | Set = isl_pw_aff_ge_set(LHS, RHS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 623 | break; |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 624 | case ICmpInst::ICMP_ULT: |
| 625 | case ICmpInst::ICMP_UGT: |
| 626 | case ICmpInst::ICMP_ULE: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 627 | case ICmpInst::ICMP_UGE: |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 628 | llvm_unreachable("Unsigned comparisons not yet supported"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 629 | default: |
| 630 | llvm_unreachable("Non integer predicate not supported"); |
| 631 | } |
| 632 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 633 | return Set; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 636 | __isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain, |
| 637 | TempScop &tempScop) const { |
| 638 | isl_space *Space; |
| 639 | isl_local_space *LocalSpace; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 640 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 641 | Space = isl_set_get_space(Domain); |
| 642 | LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 643 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 644 | for (int i = 0, e = getNumIterators(); i != e; ++i) { |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 645 | isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace)); |
| 646 | isl_pw_aff *IV = isl_pw_aff_from_aff( |
| 647 | isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 648 | |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 649 | // 0 <= IV. |
| 650 | isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV)); |
| 651 | Domain = isl_set_intersect(Domain, LowerBound); |
| 652 | |
| 653 | // IV <= LatchExecutions. |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 654 | const Loop *L = getLoopForDimension(i); |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 655 | const SCEV *LatchExecutions = tempScop.getLoopBound(L).OriginalSCEV; |
| 656 | isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions); |
| 657 | isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 658 | Domain = isl_set_intersect(Domain, UpperBoundSet); |
| 659 | } |
| 660 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 661 | isl_local_space_free(LocalSpace); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 662 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 665 | __isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain, |
| 666 | TempScop &tempScop, |
| 667 | const Region &CurRegion) |
| 668 | const { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 669 | isl_space *Space = isl_set_get_space(Domain); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 670 | const Region *TopRegion = tempScop.getMaxRegion().getParent(), |
| 671 | *CurrentRegion = &CurRegion; |
| 672 | const BasicBlock *BranchingBB = BB; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 673 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 674 | do { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 675 | if (BranchingBB != CurrentRegion->getEntry()) { |
| 676 | if (const BBCond *Condition = tempScop.getBBCond(BranchingBB)) |
| 677 | for (BBCond::const_iterator CI = Condition->begin(), |
| 678 | CE = Condition->end(); CI != CE; ++CI) { |
| 679 | isl_set *ConditionSet = buildConditionSet(*CI, Space); |
| 680 | Domain = isl_set_intersect(Domain, ConditionSet); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 683 | BranchingBB = CurrentRegion->getEntry(); |
| 684 | CurrentRegion = CurrentRegion->getParent(); |
| 685 | } while (TopRegion != CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 686 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 687 | isl_space_free(Space); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 688 | |
| 689 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 692 | __isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop, |
| 693 | const Region &CurRegion) const { |
| 694 | isl_space *Space; |
| 695 | isl_set *Domain; |
| 696 | |
| 697 | Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators()); |
| 698 | |
| 699 | Domain = isl_set_universe(Space); |
| 700 | Domain = isl_set_align_params(Domain, Parent.getParamSpace()); |
| 701 | Domain = addLoopBoundsToDomain(Domain, tempScop); |
| 702 | Domain = addConditionsToDomain(Domain, tempScop, CurRegion); |
| 703 | Domain = isl_set_set_tuple_name(Domain, getBaseName()); |
| 704 | |
| 705 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, |
| 709 | const Region &CurRegion, BasicBlock &bb, |
| 710 | SmallVectorImpl<Loop*> &NestLoops, |
| 711 | SmallVectorImpl<unsigned> &Scatter) |
| 712 | : Parent(parent), BB(&bb), IVS(NestLoops.size()) { |
| 713 | // Setup the induction variables. |
| 714 | for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) { |
| 715 | PHINode *PN = NestLoops[i]->getCanonicalInductionVariable(); |
| 716 | assert(PN && "Non canonical IV in Scop!"); |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 717 | IVS[i] = std::make_pair(PN, NestLoops[i]); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | raw_string_ostream OS(BaseName); |
| 721 | WriteAsOperand(OS, &bb, false); |
| 722 | BaseName = OS.str(); |
| 723 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 724 | makeIslCompatible(BaseName); |
| 725 | BaseName = "Stmt_" + BaseName; |
| 726 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 727 | Domain = buildDomain(tempScop, CurRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 728 | buildScattering(Scatter); |
| 729 | buildAccesses(tempScop, CurRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter) |
| 733 | : Parent(parent), BB(NULL), IVS(0) { |
| 734 | |
| 735 | BaseName = "FinalRead"; |
| 736 | |
| 737 | // Build iteration domain. |
| 738 | std::string IterationDomainString = "{[i0] : i0 = 0}"; |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 739 | Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 740 | Domain = isl_set_set_tuple_name(Domain, getBaseName()); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 741 | Domain = isl_set_align_params(Domain, parent.getParamSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 742 | |
| 743 | // Build scattering. |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 744 | unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1; |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 745 | isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 746 | Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering"); |
| 747 | Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName()); |
| 748 | isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 749 | isl_int v; |
| 750 | isl_int_init(v); |
| 751 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 752 | isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 753 | isl_int_set_si(v, -1); |
| 754 | isl_constraint_set_coefficient(c, isl_dim_out, 0, v); |
| 755 | |
| 756 | // TODO: This is incorrect. We should not use a very large number to ensure |
| 757 | // that this statement is executed last. |
| 758 | isl_int_set_si(v, 200000000); |
| 759 | isl_constraint_set_constant(c, v); |
| 760 | |
| 761 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 762 | isl_int_clear(v); |
| 763 | Scattering = isl_map_from_basic_map(bmap); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 764 | Scattering = isl_map_align_params(Scattering, parent.getParamSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 765 | |
| 766 | // Build memory accesses, use SetVector to keep the order of memory accesses |
| 767 | // and prevent the same memory access inserted more than once. |
| 768 | SetVector<const Value*> BaseAddressSet; |
| 769 | |
| 770 | for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE; |
| 771 | ++SI) { |
| 772 | ScopStmt *Stmt = *SI; |
| 773 | |
| 774 | for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(), |
| 775 | E = Stmt->memacc_end(); I != E; ++I) |
| 776 | BaseAddressSet.insert((*I)->getBaseAddr()); |
| 777 | } |
| 778 | |
| 779 | for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(), |
| 780 | BE = BaseAddressSet.end(); BI != BE; ++BI) |
| 781 | MemAccs.push_back(new MemoryAccess(*BI, this)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | std::string ScopStmt::getDomainStr() const { |
Tobias Grosser | 4da8d9f | 2011-10-06 00:03:59 +0000 | [diff] [blame] | 785 | return stringFromIslObj(Domain); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | std::string ScopStmt::getScatteringStr() const { |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 789 | return stringFromIslObj(Scattering); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | unsigned ScopStmt::getNumParams() const { |
| 793 | return Parent.getNumParams(); |
| 794 | } |
| 795 | |
| 796 | unsigned ScopStmt::getNumIterators() const { |
| 797 | // The final read has one dimension with one element. |
| 798 | if (!BB) |
| 799 | return 1; |
| 800 | |
| 801 | return IVS.size(); |
| 802 | } |
| 803 | |
| 804 | unsigned ScopStmt::getNumScattering() const { |
| 805 | return isl_map_dim(Scattering, isl_dim_out); |
| 806 | } |
| 807 | |
| 808 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 809 | |
| 810 | const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension) |
| 811 | const { |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 812 | return IVS[Dimension].first; |
| 813 | } |
| 814 | |
| 815 | const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
| 816 | return IVS[Dimension].second; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension) |
| 820 | const { |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 821 | PHINode *PN = |
| 822 | const_cast<PHINode*>(getInductionVariableForDimension(Dimension)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 823 | return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN)); |
| 824 | } |
| 825 | |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 826 | isl_ctx *ScopStmt::getIslCtx() const { |
| 827 | return Parent.getIslCtx(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 830 | isl_set *ScopStmt::getDomain() const { |
| 831 | return isl_set_copy(Domain); |
| 832 | } |
| 833 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 834 | ScopStmt::~ScopStmt() { |
| 835 | while (!MemAccs.empty()) { |
| 836 | delete MemAccs.back(); |
| 837 | MemAccs.pop_back(); |
| 838 | } |
| 839 | |
| 840 | isl_set_free(Domain); |
| 841 | isl_map_free(Scattering); |
| 842 | } |
| 843 | |
| 844 | void ScopStmt::print(raw_ostream &OS) const { |
| 845 | OS << "\t" << getBaseName() << "\n"; |
| 846 | |
| 847 | OS.indent(12) << "Domain :=\n"; |
| 848 | |
| 849 | if (Domain) { |
| 850 | OS.indent(16) << getDomainStr() << ";\n"; |
| 851 | } else |
| 852 | OS.indent(16) << "n/a\n"; |
| 853 | |
| 854 | OS.indent(12) << "Scattering :=\n"; |
| 855 | |
| 856 | if (Domain) { |
| 857 | OS.indent(16) << getScatteringStr() << ";\n"; |
| 858 | } else |
| 859 | OS.indent(16) << "n/a\n"; |
| 860 | |
| 861 | for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end(); |
| 862 | I != E; ++I) |
| 863 | (*I)->print(OS); |
| 864 | } |
| 865 | |
| 866 | void ScopStmt::dump() const { print(dbgs()); } |
| 867 | |
| 868 | //===----------------------------------------------------------------------===// |
| 869 | /// Scop class implement |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 870 | |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 871 | void Scop::buildContext(isl_ctx *IslCtx, ParamSetType *ParamSet) { |
| 872 | isl_space *Space = isl_space_params_alloc(IslCtx, ParamSet->size()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 873 | |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 874 | int i = 0; |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 875 | for (ParamSetType::iterator PI = ParamSet->begin(), PE = ParamSet->end(); |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 876 | PI != PE; ++PI) { |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 877 | const SCEV *Parameter = *PI; |
| 878 | Parameters.push_back(Parameter); |
| 879 | std::string ParameterName = "p" + convertInt(i); |
| 880 | isl_id *id = isl_id_alloc(IslCtx, ParameterName.c_str(), |
| 881 | (void *) Parameter); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 882 | Space = isl_space_set_dim_id(Space, isl_dim_param, i, id); |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 883 | i++; |
| 884 | } |
| 885 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 886 | // TODO: Insert relations between parameters. |
| 887 | // TODO: Insert constraints on parameters. |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 888 | Context = isl_set_universe (Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution, |
| 892 | isl_ctx *Context) |
| 893 | : SE(&ScalarEvolution), R(tempScop.getMaxRegion()), |
| 894 | MaxLoopDepth(tempScop.getMaxLoopDepth()) { |
| 895 | buildContext(Context, &tempScop.getParamSet()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 896 | |
| 897 | SmallVector<Loop*, 8> NestLoops; |
| 898 | SmallVector<unsigned, 8> Scatter; |
| 899 | |
| 900 | Scatter.assign(MaxLoopDepth + 1, 0); |
| 901 | |
| 902 | // Build the iteration domain, access functions and scattering functions |
| 903 | // traversing the region tree. |
| 904 | buildScop(tempScop, getRegion(), NestLoops, Scatter, LI); |
| 905 | Stmts.push_back(new ScopStmt(*this, Scatter)); |
| 906 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 907 | assert(NestLoops.empty() && "NestLoops not empty at top level!"); |
| 908 | } |
| 909 | |
| 910 | Scop::~Scop() { |
| 911 | isl_set_free(Context); |
| 912 | |
| 913 | // Free the statements; |
| 914 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 915 | delete *I; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | std::string Scop::getContextStr() const { |
Tobias Grosser | 4da8d9f | 2011-10-06 00:03:59 +0000 | [diff] [blame] | 919 | return stringFromIslObj(Context); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | std::string Scop::getNameStr() const { |
| 923 | std::string ExitName, EntryName; |
| 924 | raw_string_ostream ExitStr(ExitName); |
| 925 | raw_string_ostream EntryStr(EntryName); |
| 926 | |
| 927 | WriteAsOperand(EntryStr, R.getEntry(), false); |
| 928 | EntryStr.str(); |
| 929 | |
| 930 | if (R.getExit()) { |
| 931 | WriteAsOperand(ExitStr, R.getExit(), false); |
| 932 | ExitStr.str(); |
| 933 | } else |
| 934 | ExitName = "FunctionExit"; |
| 935 | |
| 936 | return EntryName + "---" + ExitName; |
| 937 | } |
| 938 | |
Tobias Grosser | 4da8d9f | 2011-10-06 00:03:59 +0000 | [diff] [blame] | 939 | __isl_give isl_set *Scop::getContext() const { |
| 940 | return isl_set_copy(Context); |
| 941 | } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 942 | __isl_give isl_space *Scop::getParamSpace() const { |
| 943 | return isl_set_get_space(this->Context); |
| 944 | } |
| 945 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 946 | void Scop::printContext(raw_ostream &OS) const { |
| 947 | OS << "Context:\n"; |
| 948 | |
| 949 | if (!Context) { |
| 950 | OS.indent(4) << "n/a\n\n"; |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | OS.indent(4) << getContextStr() << "\n"; |
| 955 | } |
| 956 | |
| 957 | void Scop::printStatements(raw_ostream &OS) const { |
| 958 | OS << "Statements {\n"; |
| 959 | |
| 960 | for (const_iterator SI = begin(), SE = end();SI != SE; ++SI) |
| 961 | OS.indent(4) << (**SI); |
| 962 | |
| 963 | OS.indent(4) << "}\n"; |
| 964 | } |
| 965 | |
| 966 | |
| 967 | void Scop::print(raw_ostream &OS) const { |
| 968 | printContext(OS.indent(4)); |
| 969 | printStatements(OS.indent(4)); |
| 970 | } |
| 971 | |
| 972 | void Scop::dump() const { print(dbgs()); } |
| 973 | |
Tobias Grosser | 3c69fab | 2011-10-06 00:03:54 +0000 | [diff] [blame] | 974 | isl_ctx *Scop::getIslCtx() const { return isl_set_get_ctx(Context); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 975 | |
| 976 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 977 | |
| 978 | bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) { |
| 979 | if (tempScop.getAccessFunctions(BB)) |
| 980 | return false; |
| 981 | |
| 982 | return true; |
| 983 | } |
| 984 | |
| 985 | void Scop::buildScop(TempScop &tempScop, |
| 986 | const Region &CurRegion, |
| 987 | SmallVectorImpl<Loop*> &NestLoops, |
| 988 | SmallVectorImpl<unsigned> &Scatter, |
| 989 | LoopInfo &LI) { |
| 990 | Loop *L = castToLoop(CurRegion, LI); |
| 991 | |
| 992 | if (L) |
| 993 | NestLoops.push_back(L); |
| 994 | |
| 995 | unsigned loopDepth = NestLoops.size(); |
| 996 | assert(Scatter.size() > loopDepth && "Scatter not big enough!"); |
| 997 | |
| 998 | for (Region::const_element_iterator I = CurRegion.element_begin(), |
| 999 | E = CurRegion.element_end(); I != E; ++I) |
| 1000 | if (I->isSubRegion()) |
| 1001 | buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI); |
| 1002 | else { |
| 1003 | BasicBlock *BB = I->getNodeAs<BasicBlock>(); |
| 1004 | |
| 1005 | if (isTrivialBB(BB, tempScop)) |
| 1006 | continue; |
| 1007 | |
| 1008 | Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, |
| 1009 | Scatter)); |
| 1010 | |
| 1011 | // Increasing the Scattering function is OK for the moment, because |
| 1012 | // we are using a depth first iterator and the program is well structured. |
| 1013 | ++Scatter[loopDepth]; |
| 1014 | } |
| 1015 | |
| 1016 | if (!L) |
| 1017 | return; |
| 1018 | |
| 1019 | // Exiting a loop region. |
| 1020 | Scatter[loopDepth] = 0; |
| 1021 | NestLoops.pop_back(); |
| 1022 | ++Scatter[loopDepth-1]; |
| 1023 | } |
| 1024 | |
| 1025 | //===----------------------------------------------------------------------===// |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1026 | ScopInfo::ScopInfo() : RegionPass(ID), scop(0) { |
| 1027 | ctx = isl_ctx_alloc(); |
| 1028 | } |
| 1029 | |
| 1030 | ScopInfo::~ScopInfo() { |
| 1031 | clear(); |
| 1032 | isl_ctx_free(ctx); |
| 1033 | } |
| 1034 | |
| 1035 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1036 | |
| 1037 | void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1038 | AU.addRequired<LoopInfo>(); |
| 1039 | AU.addRequired<RegionInfo>(); |
| 1040 | AU.addRequired<ScalarEvolution>(); |
| 1041 | AU.addRequired<TempScopInfo>(); |
| 1042 | AU.setPreservesAll(); |
| 1043 | } |
| 1044 | |
| 1045 | bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) { |
| 1046 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 1047 | ScalarEvolution &SE = getAnalysis<ScalarEvolution>(); |
| 1048 | |
| 1049 | TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R); |
| 1050 | |
| 1051 | // This region is no Scop. |
| 1052 | if (!tempScop) { |
| 1053 | scop = 0; |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
| 1057 | // Statistics. |
| 1058 | ++ScopFound; |
| 1059 | if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound; |
| 1060 | |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1061 | scop = new Scop(*tempScop, LI, SE, ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | char ScopInfo::ID = 0; |
| 1067 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1068 | INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops", |
| 1069 | "Polly - Create polyhedral description of Scops", false, |
| 1070 | false) |
| 1071 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 1072 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 1073 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 1074 | INITIALIZE_PASS_DEPENDENCY(TempScopInfo) |
| 1075 | INITIALIZE_PASS_END(ScopInfo, "polly-scops", |
| 1076 | "Polly - Create polyhedral description of Scops", false, |
| 1077 | false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1078 | |
| 1079 | Pass *polly::createScopInfoPass() { |
| 1080 | return new ScopInfo(); |
| 1081 | } |