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