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