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