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