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