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 | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 4 | #include "llvm/Analysis/ScalarEvolution.h" |
| 5 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 6 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 7 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 8 | |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 11 | using namespace llvm; |
| 12 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 13 | #define DEBUG_TYPE "polly-scev-validator" |
| 14 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 15 | namespace SCEVType { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 16 | /// @brief The type of a SCEV |
| 17 | /// |
| 18 | /// To check for the validity of a SCEV we assign to each SCEV a type. The |
| 19 | /// possible types are INT, PARAM, IV and INVALID. The order of the types is |
| 20 | /// important. The subexpressions of SCEV with a type X can only have a type |
| 21 | /// that is smaller or equal than X. |
| 22 | enum TYPE { |
| 23 | // An integer value. |
| 24 | INT, |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 25 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 26 | // An expression that is constant during the execution of the Scop, |
| 27 | // but that may depend on parameters unknown at compile time. |
| 28 | PARAM, |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 29 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 30 | // An expression that may change during the execution of the SCoP. |
| 31 | IV, |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 32 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 33 | // An invalid expression. |
| 34 | INVALID |
| 35 | }; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 38 | /// @brief The result the validator returns for a SCEV expression. |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 39 | class ValidatorResult { |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 40 | /// @brief The type of the expression |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 41 | SCEVType::TYPE Type; |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 42 | |
| 43 | /// @brief The set of Parameters in the expression. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 44 | std::vector<const SCEV *> Parameters; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 45 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 46 | public: |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 47 | /// @brief The copy constructor |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 48 | ValidatorResult(const ValidatorResult &Source) { |
| 49 | Type = Source.Type; |
| 50 | Parameters = Source.Parameters; |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 51 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 52 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 53 | /// @brief Construct a result with a certain type and no parameters. |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 54 | ValidatorResult(SCEVType::TYPE Type) : Type(Type) { |
| 55 | assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter"); |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 56 | } |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 57 | |
| 58 | /// @brief Construct a result with a certain type and a single parameter. |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 59 | ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 60 | Parameters.push_back(Expr); |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 61 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 62 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 63 | /// @brief Get the type of the ValidatorResult. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 64 | SCEVType::TYPE getType() { return Type; } |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 65 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 66 | /// @brief Is the analyzed SCEV constant during the execution of the SCoP. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 67 | bool isConstant() { return Type == SCEVType::INT || Type == SCEVType::PARAM; } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 68 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 69 | /// @brief Is the analyzed SCEV valid. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 70 | bool isValid() { return Type != SCEVType::INVALID; } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 71 | |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 72 | /// @brief Is the analyzed SCEV of Type IV. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 73 | bool isIV() { return Type == SCEVType::IV; } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 74 | |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 75 | /// @brief Is the analyzed SCEV of Type INT. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 76 | bool isINT() { return Type == SCEVType::INT; } |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 77 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 78 | /// @brief Is the analyzed SCEV of Type PARAM. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 79 | bool isPARAM() { return Type == SCEVType::PARAM; } |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 80 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 81 | /// @brief Get the parameters of this validator result. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 82 | std::vector<const SCEV *> getParameters() { return Parameters; } |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 83 | |
Tobias Grosser | b1f07c5 | 2011-11-17 12:56:17 +0000 | [diff] [blame] | 84 | /// @brief Add the parameters of Source to this result. |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 85 | void addParamsFrom(const ValidatorResult &Source) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 86 | Parameters.insert(Parameters.end(), Source.Parameters.begin(), |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 87 | Source.Parameters.end()); |
| 88 | } |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 89 | |
| 90 | /// @brief Merge a result. |
| 91 | /// |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 92 | /// This means to merge the parameters and to set the Type to the most |
| 93 | /// specific Type that matches both. |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 94 | ValidatorResult &merge(const ValidatorResult &ToMerge) { |
Tobias Grosser | edb8a28 | 2011-11-17 12:56:21 +0000 | [diff] [blame] | 95 | Type = std::max(Type, ToMerge.Type); |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 96 | addParamsFrom(ToMerge); |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 97 | return *this; |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 98 | } |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 99 | |
| 100 | void print(raw_ostream &OS) { |
| 101 | switch (Type) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 102 | case SCEVType::INT: |
| 103 | OS << "SCEVType::INT"; |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 104 | break; |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 105 | case SCEVType::PARAM: |
| 106 | OS << "SCEVType::PARAM"; |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 107 | break; |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 108 | case SCEVType::IV: |
| 109 | OS << "SCEVType::IV"; |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 110 | break; |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 111 | case SCEVType::INVALID: |
| 112 | OS << "SCEVType::INVALID"; |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Tobias Grosser | 540757b | 2012-03-16 10:12:37 +0000 | [diff] [blame] | 118 | raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) { |
| 119 | VR.print(OS); |
| 120 | return OS; |
| 121 | } |
| 122 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 123 | /// Check if a SCEV is valid in a SCoP. |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 124 | struct SCEVValidator |
| 125 | : public SCEVVisitor<SCEVValidator, class ValidatorResult> { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 126 | private: |
| 127 | const Region *R; |
| 128 | ScalarEvolution &SE; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 129 | const Value *BaseAddress; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 130 | |
| 131 | public: |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 132 | SCEVValidator(const Region *R, ScalarEvolution &SE, const Value *BaseAddress) |
| 133 | : R(R), SE(SE), BaseAddress(BaseAddress) {} |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 134 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 135 | class ValidatorResult visitConstant(const SCEVConstant *Constant) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 136 | return ValidatorResult(SCEVType::INT); |
| 137 | } |
| 138 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 139 | class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 140 | ValidatorResult Op = visit(Expr->getOperand()); |
| 141 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 142 | switch (Op.getType()) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 143 | case SCEVType::INT: |
| 144 | case SCEVType::PARAM: |
| 145 | // We currently do not represent a truncate expression as an affine |
| 146 | // expression. If it is constant during Scop execution, we treat it as a |
| 147 | // parameter. |
| 148 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 149 | case SCEVType::IV: |
| 150 | DEBUG(dbgs() << "INVALID: Truncation of SCEVType::IV expression"); |
| 151 | return ValidatorResult(SCEVType::INVALID); |
| 152 | case SCEVType::INVALID: |
| 153 | return Op; |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 154 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 155 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 156 | llvm_unreachable("Unknown SCEVType"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 159 | class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 160 | |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 161 | // Pattern matching rules to capture some bit and modulo computations: |
| 162 | // |
| 163 | // EXP % 2^C <==> |
| 164 | // [A] (i + c) & (2^C - 1) ==> zext iC {c,+,1}<%for_i> to IXX |
| 165 | // [B] (p + q) & (2^C - 1) ==> zext iC (trunc iXX %p_add_q to iC) to iXX |
| 166 | // [C] (i + p) & (2^C - 1) ==> zext iC {p & (2^C - 1),+,1}<%for_i> to iXX |
| 167 | // ==> zext iC {trunc iXX %p to iC,+,1}<%for_i> to |
| 168 | |
| 169 | // Check for [A] and [C]. |
| 170 | const SCEV *OpS = Expr->getOperand(); |
| 171 | if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpS)) { |
| 172 | const SCEV *OpARStart = OpAR->getStart(); |
| 173 | |
| 174 | // Special case for [C]. |
| 175 | if (auto *OpARStartTR = dyn_cast<SCEVTruncateExpr>(OpARStart)) |
| 176 | OpARStart = OpARStartTR->getOperand(); |
| 177 | |
| 178 | ValidatorResult OpARStartVR = visit(OpARStart); |
| 179 | if (OpARStartVR.isConstant() && OpAR->getStepRecurrence(SE)->isOne()) |
| 180 | return OpARStartVR; |
| 181 | } |
| 182 | |
| 183 | // Check for [B]. |
| 184 | if (auto *OpTR = dyn_cast<SCEVTruncateExpr>(OpS)) { |
| 185 | ValidatorResult OpTRVR = visit(OpTR->getOperand()); |
| 186 | if (OpTRVR.isConstant()) |
| 187 | return OpTRVR; |
| 188 | } |
| 189 | |
| 190 | ValidatorResult Op = visit(OpS); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 191 | switch (Op.getType()) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 192 | case SCEVType::INT: |
| 193 | case SCEVType::PARAM: |
| 194 | // We currently do not represent a truncate expression as an affine |
| 195 | // expression. If it is constant during Scop execution, we treat it as a |
| 196 | // parameter. |
| 197 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 198 | case SCEVType::IV: |
| 199 | DEBUG(dbgs() << "INVALID: ZeroExtend of SCEVType::IV expression"); |
| 200 | return ValidatorResult(SCEVType::INVALID); |
| 201 | case SCEVType::INVALID: |
| 202 | return Op; |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 203 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 204 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 205 | llvm_unreachable("Unknown SCEVType"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 208 | class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 209 | // We currently allow only signed SCEV expressions. In the case of a |
| 210 | // signed value, a sign extend is a noop. |
| 211 | // |
| 212 | // TODO: Reconsider this when we add support for unsigned values. |
| 213 | return visit(Expr->getOperand()); |
| 214 | } |
| 215 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 216 | class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 217 | ValidatorResult Return(SCEVType::INT); |
| 218 | |
| 219 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 220 | ValidatorResult Op = visit(Expr->getOperand(i)); |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 221 | Return.merge(Op); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 222 | |
| 223 | // Early exit. |
| 224 | if (!Return.isValid()) |
| 225 | break; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // TODO: Check for NSW and NUW. |
| 229 | return Return; |
| 230 | } |
| 231 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 232 | class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 233 | ValidatorResult Return(SCEVType::INT); |
| 234 | |
Tobias Grosser | 3ed2600 | 2013-04-14 13:15:59 +0000 | [diff] [blame] | 235 | bool HasMultipleParams = false; |
| 236 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 237 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 238 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 239 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 240 | if (Op.isINT()) |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 241 | continue; |
| 242 | |
Tobias Grosser | ecb5092 | 2013-04-10 07:42:28 +0000 | [diff] [blame] | 243 | if (Op.isPARAM() && Return.isPARAM()) { |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 244 | HasMultipleParams = true; |
Tobias Grosser | ecb5092 | 2013-04-10 07:42:28 +0000 | [diff] [blame] | 245 | continue; |
| 246 | } |
| 247 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 248 | if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) { |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 249 | DEBUG(dbgs() << "INVALID: More than one non-int operand in MulExpr\n" |
| 250 | << "\tExpr: " << *Expr << "\n" |
| 251 | << "\tPrevious expression type: " << Return << "\n" |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 252 | << "\tNext operand (" << Op |
| 253 | << "): " << *Expr->getOperand(i) << "\n"); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 254 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 255 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 256 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 257 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 258 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Tobias Grosser | 3ed2600 | 2013-04-14 13:15:59 +0000 | [diff] [blame] | 261 | if (HasMultipleParams) |
| 262 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 263 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 264 | // TODO: Check for NSW and NUW. |
| 265 | return Return; |
| 266 | } |
| 267 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 268 | class ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 269 | ValidatorResult LHS = visit(Expr->getLHS()); |
| 270 | ValidatorResult RHS = visit(Expr->getRHS()); |
| 271 | |
Tobias Grosser | f9fbbdf | 2012-04-09 19:46:05 +0000 | [diff] [blame] | 272 | // We currently do not represent an unsigned division as an affine |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 273 | // expression. If the division is constant during Scop execution we treat it |
| 274 | // as a parameter, otherwise we bail out. |
| 275 | if (LHS.isConstant() && RHS.isConstant()) |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 276 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 277 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 278 | DEBUG(dbgs() << "INVALID: unsigned division of non-constant expressions"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 279 | return ValidatorResult(SCEVType::INVALID); |
| 280 | } |
| 281 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 282 | class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 283 | if (!Expr->isAffine()) { |
| 284 | DEBUG(dbgs() << "INVALID: AddRec is not affine"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 285 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 286 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 287 | |
| 288 | ValidatorResult Start = visit(Expr->getStart()); |
| 289 | ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE)); |
| 290 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 291 | if (!Start.isValid()) |
| 292 | return Start; |
| 293 | |
| 294 | if (!Recurrence.isValid()) |
| 295 | return Recurrence; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 296 | |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 297 | if (R->contains(Expr->getLoop())) { |
| 298 | if (Recurrence.isINT()) { |
| 299 | ValidatorResult Result(SCEVType::IV); |
| 300 | Result.addParamsFrom(Start); |
| 301 | return Result; |
| 302 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 303 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 304 | DEBUG(dbgs() << "INVALID: AddRec within scop has non-int" |
| 305 | "recurrence part"); |
Tobias Grosser | bcc0a0d | 2011-11-17 12:56:14 +0000 | [diff] [blame] | 306 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 309 | assert(Start.isConstant() && Recurrence.isConstant() && |
| 310 | "Expected 'Start' and 'Recurrence' to be constant"); |
Tobias Grosser | e42ddb9 | 2013-08-05 15:14:15 +0000 | [diff] [blame] | 311 | |
| 312 | // Directly generate ValidatorResult for Expr if 'start' is zero. |
| 313 | if (Expr->getStart()->isZero()) |
| 314 | return ValidatorResult(SCEVType::PARAM, Expr); |
| 315 | |
| 316 | // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' |
| 317 | // if 'start' is not zero. |
| 318 | const SCEV *ZeroStartExpr = SE.getAddRecExpr( |
| 319 | SE.getConstant(Expr->getStart()->getType(), 0), |
| 320 | Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap); |
| 321 | |
| 322 | ValidatorResult ZeroStartResult = |
| 323 | ValidatorResult(SCEVType::PARAM, ZeroStartExpr); |
| 324 | ZeroStartResult.addParamsFrom(Start); |
| 325 | |
| 326 | return ZeroStartResult; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 329 | class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
Tobias Grosser | 7b6f9ba | 2013-12-09 21:51:46 +0000 | [diff] [blame] | 330 | ValidatorResult Return(SCEVType::INT); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 331 | |
| 332 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 333 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 334 | |
| 335 | if (!Op.isValid()) |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 336 | return Op; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 337 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 338 | Return.merge(Op); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return Return; |
| 342 | } |
| 343 | |
Tobias Grosser | fa043f0 | 2011-11-17 12:56:19 +0000 | [diff] [blame] | 344 | class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 345 | // We do not support unsigned operations. If 'Expr' is constant during Scop |
| 346 | // execution we treat this as a parameter, otherwise we bail out. |
| 347 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { |
| 348 | ValidatorResult Op = visit(Expr->getOperand(i)); |
| 349 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 350 | if (!Op.isConstant()) { |
| 351 | DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 352 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 353 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Tobias Grosser | 371bada | 2012-03-16 10:16:28 +0000 | [diff] [blame] | 356 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 359 | ValidatorResult visitUnknown(const SCEVUnknown *Expr) { |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 360 | Value *V = Expr->getValue(); |
| 361 | |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 362 | // We currently only support integer types. It may be useful to support |
| 363 | // pointer types, e.g. to support code like: |
| 364 | // |
| 365 | // if (A) |
| 366 | // A[i] = 1; |
| 367 | // |
| 368 | // See test/CodeGen/20120316-InvalidCast.ll |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 369 | if (!Expr->getType()->isIntegerTy()) { |
| 370 | DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer type"); |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 371 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 372 | } |
Tobias Grosser | 3ec2abc | 2012-03-16 16:36:47 +0000 | [diff] [blame] | 373 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 374 | if (isa<UndefValue>(V)) { |
| 375 | DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 376 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 377 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 378 | |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 379 | if (auto *I = dyn_cast<Instruction>(Expr->getValue())) { |
| 380 | if (I->getOpcode() == Instruction::SRem) { |
| 381 | |
| 382 | ValidatorResult Op0 = visit(SE.getSCEV(I->getOperand(0))); |
| 383 | if (!Op0.isValid()) |
| 384 | return ValidatorResult(SCEVType::INVALID); |
| 385 | |
| 386 | ValidatorResult Op1 = visit(SE.getSCEV(I->getOperand(1))); |
| 387 | if (!Op1.isValid() || !Op1.isINT()) |
| 388 | return ValidatorResult(SCEVType::INVALID); |
| 389 | |
| 390 | Op0.merge(Op1); |
| 391 | return Op0; |
| 392 | } |
| 393 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 394 | if (R->contains(I)) { |
| 395 | DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction " |
| 396 | "within the region\n"); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 397 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 398 | } |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 399 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 400 | |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 401 | if (BaseAddress == V) { |
| 402 | DEBUG(dbgs() << "INVALID: UnknownExpr references BaseAddress\n"); |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 403 | return ValidatorResult(SCEVType::INVALID); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 404 | } |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 405 | |
| 406 | return ValidatorResult(SCEVType::PARAM, Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 407 | } |
| 408 | }; |
| 409 | |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 410 | /// @brief Check whether a SCEV refers to an SSA name defined inside a region. |
| 411 | /// |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 412 | struct SCEVInRegionDependences |
| 413 | : public SCEVVisitor<SCEVInRegionDependences, bool> { |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 414 | public: |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 415 | /// Returns true when the SCEV has SSA names defined in region R. |
| 416 | static bool hasDependences(const SCEV *S, const Region *R) { |
| 417 | SCEVInRegionDependences Ignore(R); |
| 418 | return Ignore.visit(S); |
| 419 | } |
| 420 | |
| 421 | SCEVInRegionDependences(const Region *R) : R(R) {} |
| 422 | |
| 423 | bool visit(const SCEV *Expr) { |
| 424 | return SCEVVisitor<SCEVInRegionDependences, bool>::visit(Expr); |
| 425 | } |
| 426 | |
| 427 | bool visitConstant(const SCEVConstant *Constant) { return false; } |
| 428 | |
| 429 | bool visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
| 430 | return visit(Expr->getOperand()); |
| 431 | } |
| 432 | |
| 433 | bool visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
| 434 | return visit(Expr->getOperand()); |
| 435 | } |
| 436 | |
| 437 | bool visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
| 438 | return visit(Expr->getOperand()); |
| 439 | } |
| 440 | |
| 441 | bool visitAddExpr(const SCEVAddExpr *Expr) { |
| 442 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) |
| 443 | if (visit(Expr->getOperand(i))) |
| 444 | return true; |
| 445 | |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | bool visitMulExpr(const SCEVMulExpr *Expr) { |
| 450 | for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) |
| 451 | if (visit(Expr->getOperand(i))) |
| 452 | return true; |
| 453 | |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | bool visitUDivExpr(const SCEVUDivExpr *Expr) { |
| 458 | if (visit(Expr->getLHS())) |
| 459 | return true; |
| 460 | |
| 461 | if (visit(Expr->getRHS())) |
| 462 | return true; |
| 463 | |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | bool visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
| 468 | if (visit(Expr->getStart())) |
| 469 | return true; |
| 470 | |
| 471 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 472 | if (visit(Expr->getOperand(i))) |
| 473 | return true; |
| 474 | |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | bool visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
| 479 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 480 | if (visit(Expr->getOperand(i))) |
| 481 | return true; |
| 482 | |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | bool visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
| 487 | for (size_t i = 0; i < Expr->getNumOperands(); ++i) |
| 488 | if (visit(Expr->getOperand(i))) |
| 489 | return true; |
| 490 | |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | bool visitUnknown(const SCEVUnknown *Expr) { |
| 495 | Instruction *Inst = dyn_cast<Instruction>(Expr->getValue()); |
| 496 | |
| 497 | // Return true when Inst is defined inside the region R. |
| 498 | if (Inst && R->contains(Inst)) |
| 499 | return true; |
| 500 | |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | private: |
| 505 | const Region *R; |
| 506 | }; |
| 507 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 508 | namespace polly { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 509 | bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R) { |
| 510 | return SCEVInRegionDependences::hasDependences(Expr, R); |
| 511 | } |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 512 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 513 | bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE, |
| 514 | const Value *BaseAddress) { |
| 515 | if (isa<SCEVCouldNotCompute>(Expr)) |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 516 | return false; |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 517 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 518 | SCEVValidator Validator(R, SE, BaseAddress); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 519 | DEBUG(dbgs() << "\n"; dbgs() << "Expr: " << *Expr << "\n"; |
| 520 | dbgs() << "Region: " << R->getNameStr() << "\n"; dbgs() << " -> "); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 521 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 522 | ValidatorResult Result = Validator.visit(Expr); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 523 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 524 | DEBUG(if (Result.isValid()) dbgs() << "VALID\n"; dbgs() << "\n";); |
Tobias Grosser | eeb776a | 2012-09-08 14:00:37 +0000 | [diff] [blame] | 525 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 526 | return Result.isValid(); |
| 527 | } |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 528 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 529 | std::vector<const SCEV *> getParamsInAffineExpr(const Region *R, |
| 530 | const SCEV *Expr, |
| 531 | ScalarEvolution &SE, |
| 532 | const Value *BaseAddress) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 533 | if (isa<SCEVCouldNotCompute>(Expr)) |
| 534 | return std::vector<const SCEV *>(); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 535 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 536 | SCEVValidator Validator(R, SE, BaseAddress); |
| 537 | ValidatorResult Result = Validator.visit(Expr); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 538 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 539 | return Result.getParameters(); |
| 540 | } |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 541 | } |