Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 1 | |
| 2 | #include "polly/Support/SCEVValidator.h" |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 3 | #include "polly/ScopInfo.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame^] | 4 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 5 | #include "llvm/Analysis/ScalarEvolution.h" |
| 6 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 7 | #include "llvm/Support/Debug.h" |
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 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 12 | #define DEBUG_TYPE "polly-scev-validator" |
| 13 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 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. |
Johannes Doerfert | ecc33a1 | 2015-02-26 19:33:42 +0000 | [diff] [blame] | 84 | void addParamsFrom(const 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. |
Johannes Doerfert | ecc33a1 | 2015-02-26 19:33:42 +0000 | [diff] [blame] | 93 | void merge(const 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 | 58032cb | 2013-06-23 01:29:29 +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 | f4daf34 | 2014-08-16 09:08:55 +0000 | [diff] [blame] | 158 | ValidatorResult Op = visit(Expr->getOperand()); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 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 | |
Tobias Grosser | 3ed2600 | 2013-04-14 13:15:59 +0000 | [diff] [blame] | 204 | bool HasMultipleParams = false; |
| 205 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 206 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 207 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 208 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 209 | if (Op.isINT()) |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
Tobias Grosser | ecb5092 | 2013-04-10 07:42:28 +0000 | [diff] [blame] | 212 | if (Op.isPARAM() && Return.isPARAM()) { |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 213 | HasMultipleParams = true; |
Tobias Grosser | ecb5092 | 2013-04-10 07:42:28 +0000 | [diff] [blame] | 214 | continue; |
| 215 | } |
| 216 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 217 | if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) { |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 218 | DEBUG(dbgs() << "INVALID: More than one non-int operand in MulExpr\n" |
| 219 | << "\tExpr: " << *Expr << "\n" |
| 220 | << "\tPrevious expression type: " << Return << "\n" |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 221 | << "\tNext operand (" << Op |
| 222 | << "): " << *Expr->getOperand(i) << "\n"); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 223 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 224 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 225 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 226 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 227 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Tobias Grosser | 2a586c3 | 2015-04-05 14:57:50 +0000 | [diff] [blame] | 230 | if (HasMultipleParams && Return.isValid()) |
Tobias Grosser | 3ed2600 | 2013-04-14 13:15:59 +0000 | [diff] [blame] | 231 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 232 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 233 | // TODO: Check for NSW and NUW. |
| 234 | return Return; |
| 235 | } |
| 236 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 237 | class ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 238 | ValidatorResult LHS = visit(Expr->getLHS()); |
| 239 | ValidatorResult RHS = visit(Expr->getRHS()); |
| 240 | |
Tobias Grosser | f9fbbdf | 2012-04-09 19:46:05 +0000 | [diff] [blame] | 241 | // We currently do not represent an unsigned division as an affine |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 242 | // expression. If the division is constant during Scop execution we treat it |
| 243 | // as a parameter, otherwise we bail out. |
| 244 | if (LHS.isConstant() && RHS.isConstant()) |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 245 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 246 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 247 | DEBUG(dbgs() << "INVALID: unsigned division of non-constant expressions"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 248 | return ValidatorResult(SCEVType::INVALID); |
| 249 | } |
| 250 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 251 | class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 252 | if (!Expr->isAffine()) { |
| 253 | DEBUG(dbgs() << "INVALID: AddRec is not affine"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 254 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 255 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 256 | |
| 257 | ValidatorResult Start = visit(Expr->getStart()); |
| 258 | ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE)); |
| 259 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 260 | if (!Start.isValid()) |
| 261 | return Start; |
| 262 | |
| 263 | if (!Recurrence.isValid()) |
| 264 | return Recurrence; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 265 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 266 | if (R->contains(Expr->getLoop())) { |
| 267 | if (Recurrence.isINT()) { |
| 268 | ValidatorResult Result(SCEVType::IV); |
| 269 | Result.addParamsFrom(Start); |
| 270 | return Result; |
| 271 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 272 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 273 | DEBUG(dbgs() << "INVALID: AddRec within scop has non-int" |
| 274 | "recurrence part"); |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 275 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 278 | assert(Start.isConstant() && Recurrence.isConstant() && |
| 279 | "Expected 'Start' and 'Recurrence' to be constant"); |
Tobias Grosser | e42ddb9 | 2013-08-05 15:14:15 +0000 | [diff] [blame] | 280 | |
| 281 | // Directly generate ValidatorResult for Expr if 'start' is zero. |
| 282 | if (Expr->getStart()->isZero()) |
| 283 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 284 | |
| 285 | // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' |
| 286 | // if 'start' is not zero. |
| 287 | const SCEV *ZeroStartExpr = SE.getAddRecExpr( |
| 288 | SE.getConstant(Expr->getStart()->getType(), 0), |
Johannes Doerfert | d5d8f67 | 2015-04-26 19:55:21 +0000 | [diff] [blame] | 289 | Expr->getStepRecurrence(SE), Expr->getLoop(), Expr->getNoWrapFlags()); |
Tobias Grosser | e42ddb9 | 2013-08-05 15:14:15 +0000 | [diff] [blame] | 290 | |
| 291 | ValidatorResult ZeroStartResult = |
| 292 | ValidatorResult(SCEVType::PARAM, ZeroStartExpr); |
| 293 | ZeroStartResult.addParamsFrom(Start); |
| 294 | |
| 295 | return ZeroStartResult; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 298 | class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
Tobias Grosser | 7b6f9ba | 2013-12-09 21:51:46 +0000 | [diff] [blame] | 299 | ValidatorResult Return(SCEVType::INT); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 300 | |
| 301 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 302 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 303 | |
| 304 | if (!Op.isValid()) |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 305 | return Op; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 306 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 307 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return Return; |
| 311 | } |
| 312 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 313 | class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 314 | // We do not support unsigned operations. If 'Expr' is constant during Scop |
| 315 | // execution we treat this as a parameter, otherwise we bail out. |
| 316 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 317 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 318 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 319 | if (!Op.isConstant()) { |
| 320 | DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 321 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 322 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 325 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Johannes Doerfert | b9d1888 | 2015-02-11 14:54:50 +0000 | [diff] [blame] | 328 | ValidatorResult visitGenericInst(Instruction *I, const SCEV *S) { |
| 329 | if (R->contains(I)) { |
| 330 | DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction " |
| 331 | "within the region\n"); |
| 332 | return ValidatorResult(SCEVType::INVALID); |
| 333 | } |
| 334 | |
| 335 | return ValidatorResult(SCEVType::PARAM, S); |
| 336 | } |
| 337 | |
| 338 | ValidatorResult visitSDivInstruction(Instruction *SDiv, const SCEV *S) { |
| 339 | assert(SDiv->getOpcode() == Instruction::SDiv && |
| 340 | "Assumed SDiv instruction!"); |
| 341 | |
| 342 | auto *Divisor = SDiv->getOperand(1); |
| 343 | auto *CI = dyn_cast<ConstantInt>(Divisor); |
| 344 | if (!CI) |
| 345 | return visitGenericInst(SDiv, S); |
| 346 | |
| 347 | auto *Dividend = SDiv->getOperand(0); |
| 348 | auto *DividendSCEV = SE.getSCEV(Dividend); |
| 349 | return visit(DividendSCEV); |
| 350 | } |
| 351 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 352 | ValidatorResult visitUnknown(const SCEVUnknown *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 353 | Value *V = Expr->getValue(); |
| 354 | |
Tobias Grosser | 55bc4c0 | 2015-01-08 19:26:53 +0000 | [diff] [blame] | 355 | if (!(Expr->getType()->isIntegerTy() || Expr->getType()->isPointerTy())) { |
| 356 | DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer or pointer type"); |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 357 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 358 | } |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 359 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 360 | if (isa<UndefValue>(V)) { |
| 361 | DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 362 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 363 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 364 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 365 | if (BaseAddress == V) { |
| 366 | DEBUG(dbgs() << "INVALID: UnknownExpr references BaseAddress\n"); |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 367 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 368 | } |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 369 | |
Johannes Doerfert | b9d1888 | 2015-02-11 14:54:50 +0000 | [diff] [blame] | 370 | if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) { |
| 371 | switch (I->getOpcode()) { |
| 372 | case Instruction::SDiv: |
| 373 | return visitSDivInstruction(I, Expr); |
| 374 | default: |
| 375 | return visitGenericInst(I, Expr); |
| 376 | } |
| 377 | } |
| 378 | |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 379 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 380 | } |
| 381 | }; |
| 382 | |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 383 | /// @brief Check whether a SCEV refers to an SSA name defined inside a region. |
| 384 | /// |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 385 | struct SCEVInRegionDependences |
| 386 | : public SCEVVisitor<SCEVInRegionDependences, bool> { |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 387 | public: |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 388 | /// Returns true when the SCEV has SSA names defined in region R. |
| 389 | static bool hasDependences(const SCEV *S, const Region *R) { |
| 390 | SCEVInRegionDependences Ignore(R); |
| 391 | return Ignore.visit(S); |
| 392 | } |
| 393 | |
| 394 | SCEVInRegionDependences(const Region *R) : R(R) {} |
| 395 | |
| 396 | bool visit(const SCEV *Expr) { |
| 397 | return SCEVVisitor<SCEVInRegionDependences, bool>::visit(Expr); |
| 398 | } |
| 399 | |
| 400 | bool visitConstant(const SCEVConstant *Constant) { return false; } |
| 401 | |
| 402 | bool visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
| 403 | return visit(Expr->getOperand()); |
| 404 | } |
| 405 | |
| 406 | bool visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
| 407 | return visit(Expr->getOperand()); |
| 408 | } |
| 409 | |
| 410 | bool visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
| 411 | return visit(Expr->getOperand()); |
| 412 | } |
| 413 | |
| 414 | bool visitAddExpr(const SCEVAddExpr *Expr) { |
| 415 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) |
| 416 | if (visit(Expr->getOperand(i))) |
| 417 | return true; |
| 418 | |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | bool visitMulExpr(const SCEVMulExpr *Expr) { |
| 423 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) |
| 424 | if (visit(Expr->getOperand(i))) |
| 425 | return true; |
| 426 | |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | bool visitUDivExpr(const SCEVUDivExpr *Expr) { |
| 431 | if (visit(Expr->getLHS())) |
| 432 | return true; |
| 433 | |
| 434 | if (visit(Expr->getRHS())) |
| 435 | return true; |
| 436 | |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | bool visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
| 441 | if (visit(Expr->getStart())) |
| 442 | return true; |
| 443 | |
| 444 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 445 | if (visit(Expr->getOperand(i))) |
| 446 | return true; |
| 447 | |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | bool visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
| 452 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 453 | if (visit(Expr->getOperand(i))) |
| 454 | return true; |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | bool visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
| 460 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 461 | if (visit(Expr->getOperand(i))) |
| 462 | return true; |
| 463 | |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | bool visitUnknown(const SCEVUnknown *Expr) { |
| 468 | Instruction *Inst = dyn_cast<Instruction>(Expr->getValue()); |
| 469 | |
| 470 | // Return true when Inst is defined inside the region R. |
| 471 | if (Inst && R->contains(Inst)) |
| 472 | return true; |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | private: |
| 478 | const Region *R; |
| 479 | }; |
| 480 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 481 | namespace polly { |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 482 | /// Find all loops referenced in SCEVAddRecExprs. |
| 483 | class SCEVFindLoops { |
| 484 | SetVector<const Loop *> &Loops; |
| 485 | |
| 486 | public: |
| 487 | SCEVFindLoops(SetVector<const Loop *> &Loops) : Loops(Loops) {} |
| 488 | |
| 489 | bool follow(const SCEV *S) { |
| 490 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) |
| 491 | Loops.insert(AddRec->getLoop()); |
| 492 | return true; |
| 493 | } |
| 494 | bool isDone() { return false; } |
| 495 | }; |
| 496 | |
| 497 | void findLoops(const SCEV *Expr, SetVector<const Loop *> &Loops) { |
| 498 | SCEVFindLoops FindLoops(Loops); |
| 499 | SCEVTraversal<SCEVFindLoops> ST(FindLoops); |
| 500 | ST.visitAll(Expr); |
| 501 | } |
| 502 | |
| 503 | /// Find all values referenced in SCEVUnknowns. |
| 504 | class SCEVFindValues { |
| 505 | SetVector<Value *> &Values; |
| 506 | |
| 507 | public: |
| 508 | SCEVFindValues(SetVector<Value *> &Values) : Values(Values) {} |
| 509 | |
| 510 | bool follow(const SCEV *S) { |
| 511 | if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(S)) |
| 512 | Values.insert(Unknown->getValue()); |
| 513 | return true; |
| 514 | } |
| 515 | bool isDone() { return false; } |
| 516 | }; |
| 517 | |
| 518 | void findValues(const SCEV *Expr, SetVector<Value *> &Values) { |
| 519 | SCEVFindValues FindValues(Values); |
| 520 | SCEVTraversal<SCEVFindValues> ST(FindValues); |
| 521 | ST.visitAll(Expr); |
| 522 | } |
| 523 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 524 | bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R) { |
| 525 | return SCEVInRegionDependences::hasDependences(Expr, R); |
| 526 | } |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 527 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 528 | bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE, |
| 529 | const Value *BaseAddress) { |
| 530 | if (isa<SCEVCouldNotCompute>(Expr)) |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 531 | return false; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 532 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 533 | SCEVValidator Validator(R, SE, BaseAddress); |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 534 | DEBUG({ |
| 535 | dbgs() << "\n"; |
| 536 | dbgs() << "Expr: " << *Expr << "\n"; |
| 537 | dbgs() << "Region: " << R->getNameStr() << "\n"; |
| 538 | dbgs() << " -> "; |
| 539 | }); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 540 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 541 | ValidatorResult Result = Validator.visit(Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 542 | |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 543 | DEBUG({ |
| 544 | if (Result.isValid()) |
| 545 | dbgs() << "VALID\n"; |
| 546 | dbgs() << "\n"; |
| 547 | }); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 548 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 549 | return Result.isValid(); |
| 550 | } |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 551 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 552 | std::vector<const SCEV *> getParamsInAffineExpr(const Region *R, |
| 553 | const SCEV *Expr, |
| 554 | ScalarEvolution &SE, |
| 555 | const Value *BaseAddress) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 556 | if (isa<SCEVCouldNotCompute>(Expr)) |
| 557 | return std::vector<const SCEV *>(); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 558 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 559 | SCEVValidator Validator(R, SE, BaseAddress); |
| 560 | ValidatorResult Result = Validator.visit(Expr); |
Johannes Doerfert | 8983031 | 2015-05-03 16:03:01 +0000 | [diff] [blame] | 561 | assert(Result.isValid() && "Requested parameters for an invalid SCEV!"); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 562 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 563 | return Result.getParameters(); |
| 564 | } |
Johannes Doerfert | be40996 | 2015-03-29 20:45:09 +0000 | [diff] [blame] | 565 | |
| 566 | std::pair<const SCEV *, const SCEV *> |
| 567 | extractConstantFactor(const SCEV *S, ScalarEvolution &SE) { |
| 568 | |
| 569 | const SCEV *LeftOver = SE.getConstant(S->getType(), 1); |
| 570 | const SCEV *ConstPart = SE.getConstant(S->getType(), 1); |
| 571 | |
| 572 | const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S); |
| 573 | if (!M) |
| 574 | return std::make_pair(ConstPart, S); |
| 575 | |
| 576 | for (const SCEV *Op : M->operands()) |
| 577 | if (isa<SCEVConstant>(Op)) |
| 578 | ConstPart = SE.getMulExpr(ConstPart, Op); |
| 579 | else |
| 580 | LeftOver = SE.getMulExpr(LeftOver, Op); |
| 581 | |
| 582 | return std::make_pair(ConstPart, LeftOver); |
| 583 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 584 | } |