Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 1 | |
| 2 | #include "polly/Support/SCEVValidator.h" |
| 3 | |
| 4 | #include "llvm/Analysis/ScalarEvolution.h" |
| 5 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 6 | #include "llvm/Analysis/RegionInfo.h" |
| 7 | |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 10 | using namespace llvm; |
| 11 | |
| 12 | namespace SCEVType { |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 13 | /// @brief The type of a SCEV |
| 14 | /// |
| 15 | /// To check for the validity of a SCEV we assign to each SCEV a type. The |
| 16 | /// possible types are INT, PARAM, IV and INVALID. The order of the types is |
| 17 | /// important. The subexpressions of SCEV with a type X can only have a type |
| 18 | /// that is smaller or equal than X. |
| 19 | enum TYPE { |
| 20 | // An integer value. |
| 21 | INT, |
| 22 | |
| 23 | // An expression that is constant during the execution of the Scop, |
| 24 | // but that may depend on parameters unknown at compile time. |
| 25 | PARAM, |
| 26 | |
| 27 | // An expression that may change during the execution of the SCoP. |
| 28 | IV, |
| 29 | |
| 30 | // An invalid expression. |
| 31 | INVALID |
| 32 | }; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 35 | /// @brief The result the validator returns for a SCEV expression. |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 36 | class ValidatorResult { |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 37 | /// @brief The type of the expression |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 38 | SCEVType::TYPE Type; |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 39 | |
| 40 | /// @brief The set of Parameters in the expression. |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 41 | std::vector<const SCEV*> Parameters; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 42 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 43 | public: |
| 44 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 45 | /// @brief Create an invalid result. |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 46 | ValidatorResult() : Type(SCEVType::INVALID) {}; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 47 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 48 | /// @brief The copy constructor |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 49 | ValidatorResult(const ValidatorResult &Source) { |
| 50 | Type = Source.Type; |
| 51 | Parameters = Source.Parameters; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 54 | /// @brief Construct a result with a certain type and no parameters. |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 55 | ValidatorResult(SCEVType::TYPE Type) : Type(Type) { |
| 56 | assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter"); |
| 57 | }; |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 58 | |
| 59 | /// @brief Construct a result with a certain type and a single parameter. |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 60 | ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 61 | Parameters.push_back(Expr); |
| 62 | }; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 63 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 64 | /// @brief Is the analyzed SCEV constant during the execution of the SCoP. |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 65 | bool isConstant() { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 66 | return Type == SCEVType::INT || Type == SCEVType::PARAM; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 69 | /// @brief Is the analyzed SCEV valid. |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 70 | bool isValid() { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 71 | return Type != SCEVType::INVALID; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 74 | /// @brief Is the analyzed SCEV of Type IV. |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 75 | bool isIV() { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 76 | return Type == SCEVType::IV; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 79 | /// @brief Is the analyzed SCEV of Type INT. |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 80 | bool isINT() { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 81 | return Type == SCEVType::INT; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 82 | } |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 83 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 84 | /// @brief Get the parameters of this validator result. |
| 85 | std::vector<const SCEV*> getParameters() { |
| 86 | return Parameters; |
| 87 | } |
| 88 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 89 | /// @brief Add the parameters of Source to this result. |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 90 | void addParamsFrom(class ValidatorResult &Source) { |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 91 | Parameters.insert(Parameters.end(), |
| 92 | Source.Parameters.begin(), |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 93 | Source.Parameters.end()); |
| 94 | } |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 95 | |
| 96 | /// @brief Merge a result. |
| 97 | /// |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 98 | /// This means to merge the parameters and to set the Type to the most |
| 99 | /// specific Type that matches both. |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 100 | void merge(class ValidatorResult &ToMerge) { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 101 | Type = std::max(Type, ToMerge.Type); |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 102 | addParamsFrom(ToMerge); |
| 103 | } |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 104 | |
| 105 | void print(raw_ostream &OS) { |
| 106 | switch (Type) { |
| 107 | case SCEVType::INT: |
| 108 | OS << "SCEVType::INT\n"; |
| 109 | break; |
| 110 | case SCEVType::PARAM: |
| 111 | OS << "SCEVType::PARAM\n"; |
| 112 | break; |
| 113 | case SCEVType::IV: |
| 114 | OS << "SCEVType::IV\n"; |
| 115 | break; |
| 116 | case SCEVType::INVALID: |
| 117 | OS << "SCEVType::INVALID\n"; |
| 118 | break; |
| 119 | } |
| 120 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 123 | raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) { |
| 124 | VR.print(OS); |
| 125 | return OS; |
| 126 | } |
| 127 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 128 | /// Check if a SCEV is valid in a SCoP. |
| 129 | struct SCEVValidator |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 130 | : public SCEVVisitor<SCEVValidator, class ValidatorResult> { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 131 | private: |
| 132 | const Region *R; |
| 133 | ScalarEvolution &SE; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 134 | const Value *BaseAddress; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 135 | |
| 136 | public: |
| 137 | SCEVValidator(const Region *R, ScalarEvolution &SE, |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 138 | const Value *BaseAddress) : R(R), SE(SE), |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 139 | BaseAddress(BaseAddress) {}; |
| 140 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 141 | class ValidatorResult visitConstant(const SCEVConstant *Constant) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 142 | return ValidatorResult(SCEVType::INT); |
| 143 | } |
| 144 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 145 | class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 146 | ValidatorResult Op = visit(Expr->getOperand()); |
| 147 | |
| 148 | // We currently do not represent a truncate expression as an affine |
| 149 | // expression. If it is constant during Scop execution, we treat it as a |
| 150 | // parameter, otherwise we bail out. |
| 151 | if (Op.isConstant()) |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 152 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 153 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 154 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 157 | class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 158 | ValidatorResult Op = visit(Expr->getOperand()); |
| 159 | |
| 160 | // We currently do not represent a zero extend expression as an affine |
| 161 | // expression. If it is constant during Scop execution, we treat it as a |
| 162 | // parameter, otherwise we bail out. |
| 163 | if (Op.isConstant()) |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 164 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 165 | |
| 166 | return ValidatorResult(SCEVType::INVALID); |
| 167 | } |
| 168 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 169 | class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 170 | // We currently allow only signed SCEV expressions. In the case of a |
| 171 | // signed value, a sign extend is a noop. |
| 172 | // |
| 173 | // TODO: Reconsider this when we add support for unsigned values. |
| 174 | return visit(Expr->getOperand()); |
| 175 | } |
| 176 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 177 | class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 178 | ValidatorResult Return(SCEVType::INT); |
| 179 | |
| 180 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 181 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 182 | |
| 183 | if (!Op.isValid()) |
| 184 | return ValidatorResult(SCEVType::INVALID); |
| 185 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 186 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // TODO: Check for NSW and NUW. |
| 190 | return Return; |
| 191 | } |
| 192 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 193 | class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 194 | ValidatorResult Return(SCEVType::INT); |
| 195 | |
| 196 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 197 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 198 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 199 | if (Op.isINT()) |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 200 | continue; |
| 201 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 202 | if (!Op.isValid() || !Return.isINT()) |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 203 | return ValidatorResult(SCEVType::INVALID); |
| 204 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 205 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // TODO: Check for NSW and NUW. |
| 209 | return Return; |
| 210 | } |
| 211 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 212 | class ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 213 | ValidatorResult LHS = visit(Expr->getLHS()); |
| 214 | ValidatorResult RHS = visit(Expr->getRHS()); |
| 215 | |
Tobias Grosser | f9fbbdf | 2012-04-09 19:46:05 +0000 | [diff] [blame^] | 216 | // We currently do not represent an unsigned division as an affine |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 217 | // expression. If the division is constant during Scop execution we treat it |
| 218 | // as a parameter, otherwise we bail out. |
| 219 | if (LHS.isConstant() && RHS.isConstant()) |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 220 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 221 | |
| 222 | return ValidatorResult(SCEVType::INVALID); |
| 223 | } |
| 224 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 225 | class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 226 | if (!Expr->isAffine()) |
| 227 | return ValidatorResult(SCEVType::INVALID); |
| 228 | |
| 229 | ValidatorResult Start = visit(Expr->getStart()); |
| 230 | ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE)); |
| 231 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 232 | if (!Start.isValid() || !Recurrence.isConstant()) |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 233 | return ValidatorResult(SCEVType::INVALID); |
| 234 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 235 | if (R->contains(Expr->getLoop())) { |
| 236 | if (Recurrence.isINT()) { |
| 237 | ValidatorResult Result(SCEVType::IV); |
| 238 | Result.addParamsFrom(Start); |
| 239 | return Result; |
| 240 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 241 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 242 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 245 | if (Start.isConstant()) |
| 246 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 247 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 248 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 251 | class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 252 | ValidatorResult Return(SCEVType::INT, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 253 | |
| 254 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 255 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 256 | |
| 257 | if (!Op.isValid()) |
| 258 | return ValidatorResult(SCEVType::INVALID); |
| 259 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 260 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return Return; |
| 264 | } |
| 265 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 266 | class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 267 | // We do not support unsigned operations. If 'Expr' is constant during Scop |
| 268 | // execution we treat this as a parameter, otherwise we bail out. |
| 269 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 270 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 271 | |
| 272 | if (!Op.isConstant()) |
| 273 | return ValidatorResult(SCEVType::INVALID); |
| 274 | } |
| 275 | |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 276 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 279 | ValidatorResult visitUnknown(const SCEVUnknown *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 280 | Value *V = Expr->getValue(); |
| 281 | |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 282 | // We currently only support integer types. It may be useful to support |
| 283 | // pointer types, e.g. to support code like: |
| 284 | // |
| 285 | // if (A) |
| 286 | // A[i] = 1; |
| 287 | // |
| 288 | // See test/CodeGen/20120316-InvalidCast.ll |
| 289 | if (!Expr->getType()->isIntegerTy()) |
| 290 | return ValidatorResult(SCEVType::INVALID); |
| 291 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 292 | if (isa<UndefValue>(V)) |
| 293 | return ValidatorResult(SCEVType::INVALID); |
| 294 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 295 | if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) |
| 296 | if (R->contains(I)) |
| 297 | return ValidatorResult(SCEVType::INVALID); |
| 298 | |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 299 | if (BaseAddress == V) |
| 300 | return ValidatorResult(SCEVType::INVALID); |
| 301 | |
| 302 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 303 | } |
| 304 | }; |
| 305 | |
| 306 | namespace polly { |
| 307 | bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE, |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 308 | const Value *BaseAddress) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 309 | if (isa<SCEVCouldNotCompute>(Expr)) |
| 310 | return false; |
| 311 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 312 | SCEVValidator Validator(R, SE, BaseAddress); |
| 313 | ValidatorResult Result = Validator.visit(Expr); |
| 314 | |
| 315 | return Result.isValid(); |
| 316 | } |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 317 | |
| 318 | std::vector<const SCEV*> getParamsInAffineExpr(const Region *R, |
| 319 | const SCEV *Expr, |
| 320 | ScalarEvolution &SE, |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 321 | const Value *BaseAddress) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 322 | if (isa<SCEVCouldNotCompute>(Expr)) |
| 323 | return std::vector<const SCEV*>(); |
| 324 | |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 325 | SCEVValidator Validator(R, SE, BaseAddress); |
| 326 | ValidatorResult Result = Validator.visit(Expr); |
| 327 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 328 | return Result.getParameters(); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 329 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |