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