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