1 //===- ScopInfo.cpp -------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Create a polyhedral description for a static control flow region. 10 // 11 // The pass creates a polyhedral description of the Scops detected by the Scop 12 // detection derived from their LLVM-IR code. 13 // 14 // This representation is shared among several tools in the polyhedral 15 // community, which are e.g. Cloog, Pluto, Loopo, Graphite. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "polly/ScopInfo.h" 20 #include "polly/LinkAllPasses.h" 21 #include "polly/Options.h" 22 #include "polly/ScopBuilder.h" 23 #include "polly/ScopDetection.h" 24 #include "polly/Support/GICHelper.h" 25 #include "polly/Support/ISLOStream.h" 26 #include "polly/Support/ISLTools.h" 27 #include "polly/Support/SCEVAffinator.h" 28 #include "polly/Support/SCEVValidator.h" 29 #include "polly/Support/ScopHelper.h" 30 #include "llvm/ADT/APInt.h" 31 #include "llvm/ADT/ArrayRef.h" 32 #include "llvm/ADT/PostOrderIterator.h" 33 #include "llvm/ADT/Sequence.h" 34 #include "llvm/ADT/SmallPtrSet.h" 35 #include "llvm/ADT/SmallSet.h" 36 #include "llvm/ADT/Statistic.h" 37 #include "llvm/Analysis/AliasAnalysis.h" 38 #include "llvm/Analysis/AssumptionCache.h" 39 #include "llvm/Analysis/Loads.h" 40 #include "llvm/Analysis/LoopInfo.h" 41 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 42 #include "llvm/Analysis/RegionInfo.h" 43 #include "llvm/Analysis/RegionIterator.h" 44 #include "llvm/Analysis/ScalarEvolution.h" 45 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 46 #include "llvm/IR/BasicBlock.h" 47 #include "llvm/IR/ConstantRange.h" 48 #include "llvm/IR/DataLayout.h" 49 #include "llvm/IR/DebugLoc.h" 50 #include "llvm/IR/Dominators.h" 51 #include "llvm/IR/Function.h" 52 #include "llvm/IR/InstrTypes.h" 53 #include "llvm/IR/Instruction.h" 54 #include "llvm/IR/Instructions.h" 55 #include "llvm/IR/Module.h" 56 #include "llvm/IR/PassManager.h" 57 #include "llvm/IR/Type.h" 58 #include "llvm/IR/Value.h" 59 #include "llvm/InitializePasses.h" 60 #include "llvm/Support/Compiler.h" 61 #include "llvm/Support/Debug.h" 62 #include "llvm/Support/ErrorHandling.h" 63 #include "llvm/Support/raw_ostream.h" 64 #include "isl/aff.h" 65 #include "isl/local_space.h" 66 #include "isl/map.h" 67 #include "isl/options.h" 68 #include "isl/set.h" 69 #include <cassert> 70 71 using namespace llvm; 72 using namespace polly; 73 74 #define DEBUG_TYPE "polly-scops" 75 76 STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); 77 STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); 78 STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); 79 STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); 80 STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); 81 STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); 82 STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); 83 STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); 84 STATISTIC(AssumptionsInvariantLoad, 85 "Number of invariant loads assumptions taken."); 86 STATISTIC(AssumptionsDelinearization, 87 "Number of delinearization assumptions taken."); 88 89 STATISTIC(NumScops, "Number of feasible SCoPs after ScopInfo"); 90 STATISTIC(NumLoopsInScop, "Number of loops in scops"); 91 STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after ScopInfo"); 92 STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after ScopInfo"); 93 94 STATISTIC(NumScopsDepthZero, "Number of scops with maximal loop depth 0"); 95 STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); 96 STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); 97 STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); 98 STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); 99 STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); 100 STATISTIC(NumScopsDepthLarger, 101 "Number of scops with maximal loop depth 6 and larger"); 102 STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); 103 104 STATISTIC(NumValueWrites, "Number of scalar value writes after ScopInfo"); 105 STATISTIC( 106 NumValueWritesInLoops, 107 "Number of scalar value writes nested in affine loops after ScopInfo"); 108 STATISTIC(NumPHIWrites, "Number of scalar phi writes after ScopInfo"); 109 STATISTIC(NumPHIWritesInLoops, 110 "Number of scalar phi writes nested in affine loops after ScopInfo"); 111 STATISTIC(NumSingletonWrites, "Number of singleton writes after ScopInfo"); 112 STATISTIC(NumSingletonWritesInLoops, 113 "Number of singleton writes nested in affine loops after ScopInfo"); 114 115 int const polly::MaxDisjunctsInDomain = 20; 116 117 // The number of disjunct in the context after which we stop to add more 118 // disjuncts. This parameter is there to avoid exponential growth in the 119 // number of disjunct when adding non-convex sets to the context. 120 static int const MaxDisjunctsInContext = 4; 121 122 // Be a bit more generous for the defined behavior context which is used less 123 // often. 124 static int const MaxDisjunktsInDefinedBehaviourContext = 8; 125 126 static cl::opt<bool> PollyRemarksMinimal( 127 "polly-remarks-minimal", 128 cl::desc("Do not emit remarks about assumptions that are known"), 129 cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); 130 131 static cl::opt<bool> 132 IslOnErrorAbort("polly-on-isl-error-abort", 133 cl::desc("Abort if an isl error is encountered"), 134 cl::init(true), cl::cat(PollyCategory)); 135 136 static cl::opt<bool> PollyPreciseInbounds( 137 "polly-precise-inbounds", 138 cl::desc("Take more precise inbounds assumptions (do not scale well)"), 139 cl::Hidden, cl::init(false), cl::cat(PollyCategory)); 140 141 static cl::opt<bool> PollyIgnoreParamBounds( 142 "polly-ignore-parameter-bounds", 143 cl::desc( 144 "Do not add parameter bounds and do no gist simplify sets accordingly"), 145 cl::Hidden, cl::init(false), cl::cat(PollyCategory)); 146 147 static cl::opt<bool> PollyPreciseFoldAccesses( 148 "polly-precise-fold-accesses", 149 cl::desc("Fold memory accesses to model more possible delinearizations " 150 "(does not scale well)"), 151 cl::Hidden, cl::init(false), cl::cat(PollyCategory)); 152 153 bool polly::UseInstructionNames; 154 155 static cl::opt<bool, true> XUseInstructionNames( 156 "polly-use-llvm-names", 157 cl::desc("Use LLVM-IR names when deriving statement names"), 158 cl::location(UseInstructionNames), cl::Hidden, cl::init(false), 159 cl::ZeroOrMore, cl::cat(PollyCategory)); 160 161 static cl::opt<bool> PollyPrintInstructions( 162 "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), 163 cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); 164 165 static cl::list<std::string> IslArgs("polly-isl-arg", 166 cl::value_desc("argument"), 167 cl::desc("Option passed to ISL"), 168 cl::ZeroOrMore, cl::cat(PollyCategory)); 169 170 //===----------------------------------------------------------------------===// 171 172 static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, 173 int dim, isl::dim type) { 174 isl::val V; 175 isl::ctx Ctx = S.get_ctx(); 176 177 // The upper and lower bound for a parameter value is derived either from 178 // the data type of the parameter or from the - possibly more restrictive - 179 // range metadata. 180 V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); 181 S = S.lower_bound_val(type, dim, V); 182 V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); 183 S = S.upper_bound_val(type, dim, V); 184 185 if (Range.isFullSet()) 186 return S; 187 188 if (S.n_basic_set() > MaxDisjunctsInContext) 189 return S; 190 191 // In case of signed wrapping, we can refine the set of valid values by 192 // excluding the part not covered by the wrapping range. 193 if (Range.isSignWrappedSet()) { 194 V = valFromAPInt(Ctx.get(), Range.getLower(), true); 195 isl::set SLB = S.lower_bound_val(type, dim, V); 196 197 V = valFromAPInt(Ctx.get(), Range.getUpper(), true); 198 V = V.sub_ui(1); 199 isl::set SUB = S.upper_bound_val(type, dim, V); 200 S = SLB.unite(SUB); 201 } 202 203 return S; 204 } 205 206 static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { 207 LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); 208 if (!BasePtrLI) 209 return nullptr; 210 211 if (!S->contains(BasePtrLI)) 212 return nullptr; 213 214 ScalarEvolution &SE = *S->getSE(); 215 216 auto *OriginBaseSCEV = 217 SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); 218 if (!OriginBaseSCEV) 219 return nullptr; 220 221 auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); 222 if (!OriginBaseSCEVUnknown) 223 return nullptr; 224 225 return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), 226 MemoryKind::Array); 227 } 228 229 ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, 230 ArrayRef<const SCEV *> Sizes, MemoryKind Kind, 231 const DataLayout &DL, Scop *S, 232 const char *BaseName) 233 : BasePtr(BasePtr), ElementType(ElementType), Kind(Kind), DL(DL), S(*S) { 234 std::string BasePtrName = 235 BaseName ? BaseName 236 : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), 237 Kind == MemoryKind::PHI ? "__phi" : "", 238 UseInstructionNames); 239 Id = isl::id::alloc(Ctx, BasePtrName, this); 240 241 updateSizes(Sizes); 242 243 if (!BasePtr || Kind != MemoryKind::Array) { 244 BasePtrOriginSAI = nullptr; 245 return; 246 } 247 248 BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); 249 if (BasePtrOriginSAI) 250 const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); 251 } 252 253 ScopArrayInfo::~ScopArrayInfo() = default; 254 255 isl::space ScopArrayInfo::getSpace() const { 256 auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); 257 Space = Space.set_tuple_id(isl::dim::set, Id); 258 return Space; 259 } 260 261 bool ScopArrayInfo::isReadOnly() { 262 isl::union_set WriteSet = S.getWrites().range(); 263 isl::space Space = getSpace(); 264 WriteSet = WriteSet.extract_set(Space); 265 266 return bool(WriteSet.is_empty()); 267 } 268 269 bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { 270 if (Array->getElementType() != getElementType()) 271 return false; 272 273 if (Array->getNumberOfDimensions() != getNumberOfDimensions()) 274 return false; 275 276 for (unsigned i = 0; i < getNumberOfDimensions(); i++) 277 if (Array->getDimensionSize(i) != getDimensionSize(i)) 278 return false; 279 280 return true; 281 } 282 283 void ScopArrayInfo::updateElementType(Type *NewElementType) { 284 if (NewElementType == ElementType) 285 return; 286 287 auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); 288 auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); 289 290 if (NewElementSize == OldElementSize || NewElementSize == 0) 291 return; 292 293 if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { 294 ElementType = NewElementType; 295 } else { 296 auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); 297 ElementType = IntegerType::get(ElementType->getContext(), GCD); 298 } 299 } 300 301 /// Make the ScopArrayInfo model a Fortran Array 302 void ScopArrayInfo::applyAndSetFAD(Value *FAD) { 303 assert(FAD && "got invalid Fortran array descriptor"); 304 if (this->FAD) { 305 assert(this->FAD == FAD && 306 "receiving different array descriptors for same array"); 307 return; 308 } 309 310 assert(DimensionSizesPw.size() > 0 && DimensionSizesPw[0].is_null()); 311 assert(!this->FAD); 312 this->FAD = FAD; 313 314 isl::space Space(S.getIslCtx(), 1, 0); 315 316 std::string param_name = getName(); 317 param_name += "_fortranarr_size"; 318 isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name, this); 319 320 Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); 321 isl::pw_aff PwAff = 322 isl::aff::var_on_domain(isl::local_space(Space), isl::dim::param, 0); 323 324 DimensionSizesPw[0] = PwAff; 325 } 326 327 bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, 328 bool CheckConsistency) { 329 int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); 330 int ExtraDimsNew = NewSizes.size() - SharedDims; 331 int ExtraDimsOld = DimensionSizes.size() - SharedDims; 332 333 if (CheckConsistency) { 334 for (int i = 0; i < SharedDims; i++) { 335 auto *NewSize = NewSizes[i + ExtraDimsNew]; 336 auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; 337 if (NewSize && KnownSize && NewSize != KnownSize) 338 return false; 339 } 340 341 if (DimensionSizes.size() >= NewSizes.size()) 342 return true; 343 } 344 345 DimensionSizes.clear(); 346 DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), 347 NewSizes.end()); 348 DimensionSizesPw.clear(); 349 for (const SCEV *Expr : DimensionSizes) { 350 if (!Expr) { 351 DimensionSizesPw.push_back(isl::pw_aff()); 352 continue; 353 } 354 isl::pw_aff Size = S.getPwAffOnly(Expr); 355 DimensionSizesPw.push_back(Size); 356 } 357 return true; 358 } 359 360 std::string ScopArrayInfo::getName() const { return Id.get_name(); } 361 362 int ScopArrayInfo::getElemSizeInBytes() const { 363 return DL.getTypeAllocSize(ElementType); 364 } 365 366 isl::id ScopArrayInfo::getBasePtrId() const { return Id; } 367 368 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 369 LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } 370 #endif 371 372 void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { 373 OS.indent(8) << *getElementType() << " " << getName(); 374 unsigned u = 0; 375 // If this is a Fortran array, then we can print the outermost dimension 376 // as a isl_pw_aff even though there is no SCEV information. 377 bool IsOutermostSizeKnown = SizeAsPwAff && FAD; 378 379 if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && 380 !getDimensionSize(0)) { 381 OS << "[*]"; 382 u++; 383 } 384 for (; u < getNumberOfDimensions(); u++) { 385 OS << "["; 386 387 if (SizeAsPwAff) { 388 isl::pw_aff Size = getDimensionSizePw(u); 389 OS << " " << Size << " "; 390 } else { 391 OS << *getDimensionSize(u); 392 } 393 394 OS << "]"; 395 } 396 397 OS << ";"; 398 399 if (BasePtrOriginSAI) 400 OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; 401 402 OS << " // Element size " << getElemSizeInBytes() << "\n"; 403 } 404 405 const ScopArrayInfo * 406 ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { 407 isl::id Id = PMA.get_tuple_id(isl::dim::out); 408 assert(!Id.is_null() && "Output dimension didn't have an ID"); 409 return getFromId(Id); 410 } 411 412 const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { 413 void *User = Id.get_user(); 414 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); 415 return SAI; 416 } 417 418 void MemoryAccess::wrapConstantDimensions() { 419 auto *SAI = getScopArrayInfo(); 420 isl::space ArraySpace = SAI->getSpace(); 421 isl::ctx Ctx = ArraySpace.get_ctx(); 422 unsigned DimsArray = SAI->getNumberOfDimensions(); 423 424 isl::multi_aff DivModAff = isl::multi_aff::identity( 425 ArraySpace.map_from_domain_and_range(ArraySpace)); 426 isl::local_space LArraySpace = isl::local_space(ArraySpace); 427 428 // Begin with last dimension, to iteratively carry into higher dimensions. 429 for (int i = DimsArray - 1; i > 0; i--) { 430 auto *DimSize = SAI->getDimensionSize(i); 431 auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); 432 433 // This transformation is not applicable to dimensions with dynamic size. 434 if (!DimSizeCst) 435 continue; 436 437 // This transformation is not applicable to dimensions of size zero. 438 if (DimSize->isZero()) 439 continue; 440 441 isl::val DimSizeVal = 442 valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); 443 isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); 444 isl::aff PrevVar = 445 isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); 446 447 // Compute: index % size 448 // Modulo must apply in the divide of the previous iteration, if any. 449 isl::aff Modulo = Var.mod(DimSizeVal); 450 Modulo = Modulo.pullback(DivModAff); 451 452 // Compute: floor(index / size) 453 isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); 454 Divide = Divide.floor(); 455 Divide = Divide.add(PrevVar); 456 Divide = Divide.pullback(DivModAff); 457 458 // Apply Modulo and Divide. 459 DivModAff = DivModAff.set_aff(i, Modulo); 460 DivModAff = DivModAff.set_aff(i - 1, Divide); 461 } 462 463 // Apply all modulo/divides on the accesses. 464 isl::map Relation = AccessRelation; 465 Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); 466 Relation = Relation.detect_equalities(); 467 AccessRelation = Relation; 468 } 469 470 void MemoryAccess::updateDimensionality() { 471 auto *SAI = getScopArrayInfo(); 472 isl::space ArraySpace = SAI->getSpace(); 473 isl::space AccessSpace = AccessRelation.get_space().range(); 474 isl::ctx Ctx = ArraySpace.get_ctx(); 475 476 auto DimsArray = ArraySpace.dim(isl::dim::set); 477 auto DimsAccess = AccessSpace.dim(isl::dim::set); 478 auto DimsMissing = DimsArray - DimsAccess; 479 480 auto *BB = getStatement()->getEntryBlock(); 481 auto &DL = BB->getModule()->getDataLayout(); 482 unsigned ArrayElemSize = SAI->getElemSizeInBytes(); 483 unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); 484 485 isl::map Map = isl::map::from_domain_and_range( 486 isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); 487 488 for (auto i : seq<isl_size>(0, DimsMissing)) 489 Map = Map.fix_si(isl::dim::out, i, 0); 490 491 for (auto i : seq<isl_size>(DimsMissing, DimsArray)) 492 Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); 493 494 AccessRelation = AccessRelation.apply_range(Map); 495 496 // For the non delinearized arrays, divide the access function of the last 497 // subscript by the size of the elements in the array. 498 // 499 // A stride one array access in C expressed as A[i] is expressed in 500 // LLVM-IR as something like A[i * elementsize]. This hides the fact that 501 // two subsequent values of 'i' index two values that are stored next to 502 // each other in memory. By this division we make this characteristic 503 // obvious again. If the base pointer was accessed with offsets not divisible 504 // by the accesses element size, we will have chosen a smaller ArrayElemSize 505 // that divides the offsets of all accesses to this base pointer. 506 if (DimsAccess == 1) { 507 isl::val V = isl::val(Ctx, ArrayElemSize); 508 AccessRelation = AccessRelation.floordiv_val(V); 509 } 510 511 // We currently do this only if we added at least one dimension, which means 512 // some dimension's indices have not been specified, an indicator that some 513 // index values have been added together. 514 // TODO: Investigate general usefulness; Effect on unit tests is to make index 515 // expressions more complicated. 516 if (DimsMissing) 517 wrapConstantDimensions(); 518 519 if (!isAffine()) 520 computeBoundsOnAccessRelation(ArrayElemSize); 521 522 // Introduce multi-element accesses in case the type loaded by this memory 523 // access is larger than the canonical element type of the array. 524 // 525 // An access ((float *)A)[i] to an array char *A is modeled as 526 // {[i] -> A[o] : 4 i <= o <= 4 i + 3 527 if (ElemBytes > ArrayElemSize) { 528 assert(ElemBytes % ArrayElemSize == 0 && 529 "Loaded element size should be multiple of canonical element size"); 530 isl::map Map = isl::map::from_domain_and_range( 531 isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); 532 for (auto i : seq<isl_size>(0, DimsArray - 1)) 533 Map = Map.equate(isl::dim::in, i, isl::dim::out, i); 534 535 isl::constraint C; 536 isl::local_space LS; 537 538 LS = isl::local_space(Map.get_space()); 539 int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); 540 541 C = isl::constraint::alloc_inequality(LS); 542 C = C.set_constant_val(isl::val(Ctx, Num - 1)); 543 C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); 544 C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); 545 Map = Map.add_constraint(C); 546 547 C = isl::constraint::alloc_inequality(LS); 548 C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); 549 C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); 550 C = C.set_constant_val(isl::val(Ctx, 0)); 551 Map = Map.add_constraint(C); 552 AccessRelation = AccessRelation.apply_range(Map); 553 } 554 } 555 556 const std::string 557 MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { 558 switch (RT) { 559 case MemoryAccess::RT_NONE: 560 llvm_unreachable("Requested a reduction operator string for a memory " 561 "access which isn't a reduction"); 562 case MemoryAccess::RT_ADD: 563 return "+"; 564 case MemoryAccess::RT_MUL: 565 return "*"; 566 case MemoryAccess::RT_BOR: 567 return "|"; 568 case MemoryAccess::RT_BXOR: 569 return "^"; 570 case MemoryAccess::RT_BAND: 571 return "&"; 572 } 573 llvm_unreachable("Unknown reduction type"); 574 } 575 576 const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { 577 isl::id ArrayId = getArrayId(); 578 void *User = ArrayId.get_user(); 579 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); 580 return SAI; 581 } 582 583 const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { 584 isl::id ArrayId = getLatestArrayId(); 585 void *User = ArrayId.get_user(); 586 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); 587 return SAI; 588 } 589 590 isl::id MemoryAccess::getOriginalArrayId() const { 591 return AccessRelation.get_tuple_id(isl::dim::out); 592 } 593 594 isl::id MemoryAccess::getLatestArrayId() const { 595 if (!hasNewAccessRelation()) 596 return getOriginalArrayId(); 597 return NewAccessRelation.get_tuple_id(isl::dim::out); 598 } 599 600 isl::map MemoryAccess::getAddressFunction() const { 601 return getAccessRelation().lexmin(); 602 } 603 604 isl::pw_multi_aff 605 MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { 606 isl::map Schedule, ScheduledAccRel; 607 isl::union_set UDomain; 608 609 UDomain = getStatement()->getDomain(); 610 USchedule = USchedule.intersect_domain(UDomain); 611 Schedule = isl::map::from_union_map(USchedule); 612 ScheduledAccRel = getAddressFunction().apply_domain(Schedule); 613 return isl::pw_multi_aff::from_map(ScheduledAccRel); 614 } 615 616 isl::map MemoryAccess::getOriginalAccessRelation() const { 617 return AccessRelation; 618 } 619 620 std::string MemoryAccess::getOriginalAccessRelationStr() const { 621 return stringFromIslObj(AccessRelation); 622 } 623 624 isl::space MemoryAccess::getOriginalAccessRelationSpace() const { 625 return AccessRelation.get_space(); 626 } 627 628 isl::map MemoryAccess::getNewAccessRelation() const { 629 return NewAccessRelation; 630 } 631 632 std::string MemoryAccess::getNewAccessRelationStr() const { 633 return stringFromIslObj(NewAccessRelation); 634 } 635 636 std::string MemoryAccess::getAccessRelationStr() const { 637 return stringFromIslObj(getAccessRelation()); 638 } 639 640 isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { 641 isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); 642 Space = Space.align_params(Statement->getDomainSpace()); 643 644 return isl::basic_map::from_domain_and_range( 645 isl::basic_set::universe(Statement->getDomainSpace()), 646 isl::basic_set::universe(Space)); 647 } 648 649 // Formalize no out-of-bound access assumption 650 // 651 // When delinearizing array accesses we optimistically assume that the 652 // delinearized accesses do not access out of bound locations (the subscript 653 // expression of each array evaluates for each statement instance that is 654 // executed to a value that is larger than zero and strictly smaller than the 655 // size of the corresponding dimension). The only exception is the outermost 656 // dimension for which we do not need to assume any upper bound. At this point 657 // we formalize this assumption to ensure that at code generation time the 658 // relevant run-time checks can be generated. 659 // 660 // To find the set of constraints necessary to avoid out of bound accesses, we 661 // first build the set of data locations that are not within array bounds. We 662 // then apply the reverse access relation to obtain the set of iterations that 663 // may contain invalid accesses and reduce this set of iterations to the ones 664 // that are actually executed by intersecting them with the domain of the 665 // statement. If we now project out all loop dimensions, we obtain a set of 666 // parameters that may cause statement instances to be executed that may 667 // possibly yield out of bound memory accesses. The complement of these 668 // constraints is the set of constraints that needs to be assumed to ensure such 669 // statement instances are never executed. 670 isl::set MemoryAccess::assumeNoOutOfBound() { 671 auto *SAI = getScopArrayInfo(); 672 isl::space Space = getOriginalAccessRelationSpace().range(); 673 isl::set Outside = isl::set::empty(Space); 674 for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { 675 isl::local_space LS(Space); 676 isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); 677 isl::pw_aff Zero = isl::pw_aff(LS); 678 679 isl::set DimOutside = Var.lt_set(Zero); 680 isl::pw_aff SizeE = SAI->getDimensionSizePw(i); 681 SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); 682 SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); 683 DimOutside = DimOutside.unite(SizeE.le_set(Var)); 684 685 Outside = Outside.unite(DimOutside); 686 } 687 688 Outside = Outside.apply(getAccessRelation().reverse()); 689 Outside = Outside.intersect(Statement->getDomain()); 690 Outside = Outside.params(); 691 692 // Remove divs to avoid the construction of overly complicated assumptions. 693 // Doing so increases the set of parameter combinations that are assumed to 694 // not appear. This is always save, but may make the resulting run-time check 695 // bail out more often than strictly necessary. 696 Outside = Outside.remove_divs(); 697 Outside = Outside.complement(); 698 699 if (!PollyPreciseInbounds) 700 Outside = Outside.gist_params(Statement->getDomain().params()); 701 return Outside; 702 } 703 704 void MemoryAccess::buildMemIntrinsicAccessRelation() { 705 assert(isMemoryIntrinsic()); 706 assert(Subscripts.size() == 2 && Sizes.size() == 1); 707 708 isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); 709 isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); 710 711 isl::map LengthMap; 712 if (Subscripts[1] == nullptr) { 713 LengthMap = isl::map::universe(SubscriptMap.get_space()); 714 } else { 715 isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); 716 LengthMap = isl::map::from_pw_aff(LengthPWA); 717 isl::space RangeSpace = LengthMap.get_space().range(); 718 LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); 719 } 720 LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); 721 LengthMap = LengthMap.align_params(SubscriptMap.get_space()); 722 SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); 723 LengthMap = LengthMap.sum(SubscriptMap); 724 AccessRelation = 725 LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); 726 } 727 728 void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { 729 ScalarEvolution *SE = Statement->getParent()->getSE(); 730 731 auto MAI = MemAccInst(getAccessInstruction()); 732 if (isa<MemIntrinsic>(MAI)) 733 return; 734 735 Value *Ptr = MAI.getPointerOperand(); 736 if (!Ptr || !SE->isSCEVable(Ptr->getType())) 737 return; 738 739 auto *PtrSCEV = SE->getSCEV(Ptr); 740 if (isa<SCEVCouldNotCompute>(PtrSCEV)) 741 return; 742 743 auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); 744 if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) 745 PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); 746 747 const ConstantRange &Range = SE->getSignedRange(PtrSCEV); 748 if (Range.isFullSet()) 749 return; 750 751 if (Range.isUpperWrapped() || Range.isSignWrappedSet()) 752 return; 753 754 bool isWrapping = Range.isSignWrappedSet(); 755 756 unsigned BW = Range.getBitWidth(); 757 const auto One = APInt(BW, 1); 758 const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); 759 const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); 760 761 auto Min = LB.sdiv(APInt(BW, ElementSize)); 762 auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; 763 764 assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); 765 766 isl::map Relation = AccessRelation; 767 isl::set AccessRange = Relation.range(); 768 AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, 769 isl::dim::set); 770 AccessRelation = Relation.intersect_range(AccessRange); 771 } 772 773 void MemoryAccess::foldAccessRelation() { 774 if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) 775 return; 776 777 int Size = Subscripts.size(); 778 779 isl::map NewAccessRelation = AccessRelation; 780 781 for (int i = Size - 2; i >= 0; --i) { 782 isl::space Space; 783 isl::map MapOne, MapTwo; 784 isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); 785 786 isl::space SpaceSize = DimSize.get_space(); 787 isl::id ParamId = SpaceSize.get_dim_id(isl::dim::param, 0); 788 789 Space = AccessRelation.get_space(); 790 Space = Space.range().map_from_set(); 791 Space = Space.align_params(SpaceSize); 792 793 int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); 794 795 MapOne = isl::map::universe(Space); 796 for (int j = 0; j < Size; ++j) 797 MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); 798 MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); 799 800 MapTwo = isl::map::universe(Space); 801 for (int j = 0; j < Size; ++j) 802 if (j < i || j > i + 1) 803 MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); 804 805 isl::local_space LS(Space); 806 isl::constraint C; 807 C = isl::constraint::alloc_equality(LS); 808 C = C.set_constant_si(-1); 809 C = C.set_coefficient_si(isl::dim::in, i, 1); 810 C = C.set_coefficient_si(isl::dim::out, i, -1); 811 MapTwo = MapTwo.add_constraint(C); 812 C = isl::constraint::alloc_equality(LS); 813 C = C.set_coefficient_si(isl::dim::in, i + 1, 1); 814 C = C.set_coefficient_si(isl::dim::out, i + 1, -1); 815 C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); 816 MapTwo = MapTwo.add_constraint(C); 817 MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); 818 819 MapOne = MapOne.unite(MapTwo); 820 NewAccessRelation = NewAccessRelation.apply_range(MapOne); 821 } 822 823 isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); 824 isl::space Space = Statement->getDomainSpace(); 825 NewAccessRelation = NewAccessRelation.set_tuple_id( 826 isl::dim::in, Space.get_tuple_id(isl::dim::set)); 827 NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); 828 NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); 829 830 // Access dimension folding might in certain cases increase the number of 831 // disjuncts in the memory access, which can possibly complicate the generated 832 // run-time checks and can lead to costly compilation. 833 if (!PollyPreciseFoldAccesses && 834 NewAccessRelation.n_basic_map() > AccessRelation.n_basic_map()) { 835 } else { 836 AccessRelation = NewAccessRelation; 837 } 838 } 839 840 void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { 841 assert(AccessRelation.is_null() && "AccessRelation already built"); 842 843 // Initialize the invalid domain which describes all iterations for which the 844 // access relation is not modeled correctly. 845 isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); 846 InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); 847 848 isl::ctx Ctx = Id.get_ctx(); 849 isl::id BaseAddrId = SAI->getBasePtrId(); 850 851 if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { 852 buildMemIntrinsicAccessRelation(); 853 AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); 854 return; 855 } 856 857 if (!isAffine()) { 858 // We overapproximate non-affine accesses with a possible access to the 859 // whole array. For read accesses it does not make a difference, if an 860 // access must or may happen. However, for write accesses it is important to 861 // differentiate between writes that must happen and writes that may happen. 862 if (AccessRelation.is_null()) 863 AccessRelation = createBasicAccessMap(Statement); 864 865 AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); 866 return; 867 } 868 869 isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); 870 AccessRelation = isl::map::universe(Space); 871 872 for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { 873 isl::pw_aff Affine = getPwAff(Subscripts[i]); 874 isl::map SubscriptMap = isl::map::from_pw_aff(Affine); 875 AccessRelation = AccessRelation.flat_range_product(SubscriptMap); 876 } 877 878 Space = Statement->getDomainSpace(); 879 AccessRelation = AccessRelation.set_tuple_id( 880 isl::dim::in, Space.get_tuple_id(isl::dim::set)); 881 AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); 882 883 AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); 884 } 885 886 MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, 887 AccessType AccType, Value *BaseAddress, 888 Type *ElementType, bool Affine, 889 ArrayRef<const SCEV *> Subscripts, 890 ArrayRef<const SCEV *> Sizes, Value *AccessValue, 891 MemoryKind Kind) 892 : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(), 893 BaseAddr(BaseAddress), ElementType(ElementType), 894 Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), 895 AccessValue(AccessValue), IsAffine(Affine), 896 Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(), 897 NewAccessRelation(), FAD(nullptr) { 898 static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; 899 const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); 900 901 std::string IdName = Stmt->getBaseName() + Access; 902 Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); 903 } 904 905 MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) 906 : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), 907 InvalidDomain(), AccessRelation(), NewAccessRelation(AccRel), 908 FAD(nullptr) { 909 isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); 910 auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); 911 Sizes.push_back(nullptr); 912 for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) 913 Sizes.push_back(SAI->getDimensionSize(i)); 914 ElementType = SAI->getElementType(); 915 BaseAddr = SAI->getBasePtr(); 916 static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; 917 const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); 918 919 std::string IdName = Stmt->getBaseName() + Access; 920 Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); 921 } 922 923 MemoryAccess::~MemoryAccess() = default; 924 925 void MemoryAccess::realignParams() { 926 isl::set Ctx = Statement->getParent()->getContext(); 927 InvalidDomain = InvalidDomain.gist_params(Ctx); 928 AccessRelation = AccessRelation.gist_params(Ctx); 929 930 // Predictable parameter order is required for JSON imports. Ensure alignment 931 // by explicitly calling align_params. 932 isl::space CtxSpace = Ctx.get_space(); 933 InvalidDomain = InvalidDomain.align_params(CtxSpace); 934 AccessRelation = AccessRelation.align_params(CtxSpace); 935 } 936 937 const std::string MemoryAccess::getReductionOperatorStr() const { 938 return MemoryAccess::getReductionOperatorStr(getReductionType()); 939 } 940 941 isl::id MemoryAccess::getId() const { return Id; } 942 943 raw_ostream &polly::operator<<(raw_ostream &OS, 944 MemoryAccess::ReductionType RT) { 945 if (RT == MemoryAccess::RT_NONE) 946 OS << "NONE"; 947 else 948 OS << MemoryAccess::getReductionOperatorStr(RT); 949 return OS; 950 } 951 952 void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } 953 954 void MemoryAccess::print(raw_ostream &OS) const { 955 switch (AccType) { 956 case READ: 957 OS.indent(12) << "ReadAccess :=\t"; 958 break; 959 case MUST_WRITE: 960 OS.indent(12) << "MustWriteAccess :=\t"; 961 break; 962 case MAY_WRITE: 963 OS.indent(12) << "MayWriteAccess :=\t"; 964 break; 965 } 966 967 OS << "[Reduction Type: " << getReductionType() << "] "; 968 969 if (FAD) { 970 OS << "[Fortran array descriptor: " << FAD->getName(); 971 OS << "] "; 972 }; 973 974 OS << "[Scalar: " << isScalarKind() << "]\n"; 975 OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; 976 if (hasNewAccessRelation()) 977 OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; 978 } 979 980 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 981 LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } 982 #endif 983 984 isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { 985 auto *Stmt = getStatement(); 986 PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); 987 isl::set StmtDom = getStatement()->getDomain(); 988 StmtDom = StmtDom.reset_tuple_id(); 989 isl::set NewInvalidDom = StmtDom.intersect(PWAC.second); 990 InvalidDomain = InvalidDomain.unite(NewInvalidDom); 991 return PWAC.first; 992 } 993 994 // Create a map in the size of the provided set domain, that maps from the 995 // one element of the provided set domain to another element of the provided 996 // set domain. 997 // The mapping is limited to all points that are equal in all but the last 998 // dimension and for which the last dimension of the input is strict smaller 999 // than the last dimension of the output. 1000 // 1001 // getEqualAndLarger(set[i0, i1, ..., iX]): 1002 // 1003 // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] 1004 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX 1005 // 1006 static isl::map getEqualAndLarger(isl::space SetDomain) { 1007 isl::space Space = SetDomain.map_from_set(); 1008 isl::map Map = isl::map::universe(Space); 1009 unsigned lastDimension = Map.dim(isl::dim::in) - 1; 1010 1011 // Set all but the last dimension to be equal for the input and output 1012 // 1013 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] 1014 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) 1015 for (unsigned i = 0; i < lastDimension; ++i) 1016 Map = Map.equate(isl::dim::in, i, isl::dim::out, i); 1017 1018 // Set the last dimension of the input to be strict smaller than the 1019 // last dimension of the output. 1020 // 1021 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX 1022 Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); 1023 return Map; 1024 } 1025 1026 isl::set MemoryAccess::getStride(isl::map Schedule) const { 1027 isl::map AccessRelation = getAccessRelation(); 1028 isl::space Space = Schedule.get_space().range(); 1029 isl::map NextScatt = getEqualAndLarger(Space); 1030 1031 Schedule = Schedule.reverse(); 1032 NextScatt = NextScatt.lexmin(); 1033 1034 NextScatt = NextScatt.apply_range(Schedule); 1035 NextScatt = NextScatt.apply_range(AccessRelation); 1036 NextScatt = NextScatt.apply_domain(Schedule); 1037 NextScatt = NextScatt.apply_domain(AccessRelation); 1038 1039 isl::set Deltas = NextScatt.deltas(); 1040 return Deltas; 1041 } 1042 1043 bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { 1044 isl::set Stride, StrideX; 1045 bool IsStrideX; 1046 1047 Stride = getStride(Schedule); 1048 StrideX = isl::set::universe(Stride.get_space()); 1049 for (auto i : seq<isl_size>(0, StrideX.dim(isl::dim::set) - 1)) 1050 StrideX = StrideX.fix_si(isl::dim::set, i, 0); 1051 StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, 1052 StrideWidth); 1053 IsStrideX = Stride.is_subset(StrideX); 1054 1055 return IsStrideX; 1056 } 1057 1058 bool MemoryAccess::isStrideZero(isl::map Schedule) const { 1059 return isStrideX(Schedule, 0); 1060 } 1061 1062 bool MemoryAccess::isStrideOne(isl::map Schedule) const { 1063 return isStrideX(Schedule, 1); 1064 } 1065 1066 void MemoryAccess::setAccessRelation(isl::map NewAccess) { 1067 AccessRelation = NewAccess; 1068 } 1069 1070 void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { 1071 assert(!NewAccess.is_null()); 1072 1073 #ifndef NDEBUG 1074 // Check domain space compatibility. 1075 isl::space NewSpace = NewAccess.get_space(); 1076 isl::space NewDomainSpace = NewSpace.domain(); 1077 isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); 1078 assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); 1079 1080 // Reads must be executed unconditionally. Writes might be executed in a 1081 // subdomain only. 1082 if (isRead()) { 1083 // Check whether there is an access for every statement instance. 1084 isl::set StmtDomain = getStatement()->getDomain(); 1085 isl::set DefinedContext = 1086 getStatement()->getParent()->getBestKnownDefinedBehaviorContext(); 1087 StmtDomain = StmtDomain.intersect_params(DefinedContext); 1088 isl::set NewDomain = NewAccess.domain(); 1089 assert(!StmtDomain.is_subset(NewDomain).is_false() && 1090 "Partial READ accesses not supported"); 1091 } 1092 1093 isl::space NewAccessSpace = NewAccess.get_space(); 1094 assert(NewAccessSpace.has_tuple_id(isl::dim::set) && 1095 "Must specify the array that is accessed"); 1096 isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); 1097 auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); 1098 assert(SAI && "Must set a ScopArrayInfo"); 1099 1100 if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { 1101 InvariantEquivClassTy *EqClass = 1102 getStatement()->getParent()->lookupInvariantEquivClass( 1103 SAI->getBasePtr()); 1104 assert(EqClass && 1105 "Access functions to indirect arrays must have an invariant and " 1106 "hoisted base pointer"); 1107 } 1108 1109 // Check whether access dimensions correspond to number of dimensions of the 1110 // accesses array. 1111 isl_size Dims = SAI->getNumberOfDimensions(); 1112 assert(NewAccessSpace.dim(isl::dim::set) == Dims && 1113 "Access dims must match array dims"); 1114 #endif 1115 1116 NewAccess = NewAccess.gist_params(getStatement()->getParent()->getContext()); 1117 NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); 1118 NewAccessRelation = NewAccess; 1119 } 1120 1121 bool MemoryAccess::isLatestPartialAccess() const { 1122 isl::set StmtDom = getStatement()->getDomain(); 1123 isl::set AccDom = getLatestAccessRelation().domain(); 1124 1125 return !StmtDom.is_subset(AccDom); 1126 } 1127 1128 //===----------------------------------------------------------------------===// 1129 1130 isl::map ScopStmt::getSchedule() const { 1131 isl::set Domain = getDomain(); 1132 if (Domain.is_empty()) 1133 return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); 1134 auto Schedule = getParent()->getSchedule(); 1135 if (Schedule.is_null()) 1136 return {}; 1137 Schedule = Schedule.intersect_domain(isl::union_set(Domain)); 1138 if (Schedule.is_empty()) 1139 return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); 1140 isl::map M = M.from_union_map(Schedule); 1141 M = M.coalesce(); 1142 M = M.gist_domain(Domain); 1143 M = M.coalesce(); 1144 return M; 1145 } 1146 1147 void ScopStmt::restrictDomain(isl::set NewDomain) { 1148 assert(NewDomain.is_subset(Domain) && 1149 "New domain is not a subset of old domain!"); 1150 Domain = NewDomain; 1151 } 1152 1153 void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { 1154 Instruction *AccessInst = Access->getAccessInstruction(); 1155 1156 if (Access->isArrayKind()) { 1157 MemoryAccessList &MAL = InstructionToAccess[AccessInst]; 1158 MAL.emplace_front(Access); 1159 } else if (Access->isValueKind() && Access->isWrite()) { 1160 Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); 1161 assert(!ValueWrites.lookup(AccessVal)); 1162 1163 ValueWrites[AccessVal] = Access; 1164 } else if (Access->isValueKind() && Access->isRead()) { 1165 Value *AccessVal = Access->getAccessValue(); 1166 assert(!ValueReads.lookup(AccessVal)); 1167 1168 ValueReads[AccessVal] = Access; 1169 } else if (Access->isAnyPHIKind() && Access->isWrite()) { 1170 PHINode *PHI = cast<PHINode>(Access->getAccessValue()); 1171 assert(!PHIWrites.lookup(PHI)); 1172 1173 PHIWrites[PHI] = Access; 1174 } else if (Access->isAnyPHIKind() && Access->isRead()) { 1175 PHINode *PHI = cast<PHINode>(Access->getAccessValue()); 1176 assert(!PHIReads.lookup(PHI)); 1177 1178 PHIReads[PHI] = Access; 1179 } 1180 1181 if (Prepend) { 1182 MemAccs.insert(MemAccs.begin(), Access); 1183 return; 1184 } 1185 MemAccs.push_back(Access); 1186 } 1187 1188 void ScopStmt::realignParams() { 1189 for (MemoryAccess *MA : *this) 1190 MA->realignParams(); 1191 1192 isl::set Ctx = Parent.getContext(); 1193 InvalidDomain = InvalidDomain.gist_params(Ctx); 1194 Domain = Domain.gist_params(Ctx); 1195 1196 // Predictable parameter order is required for JSON imports. Ensure alignment 1197 // by explicitly calling align_params. 1198 isl::space CtxSpace = Ctx.get_space(); 1199 InvalidDomain = InvalidDomain.align_params(CtxSpace); 1200 Domain = Domain.align_params(CtxSpace); 1201 } 1202 1203 ScopStmt::ScopStmt(Scop &parent, Region &R, StringRef Name, 1204 Loop *SurroundingLoop, 1205 std::vector<Instruction *> EntryBlockInstructions) 1206 : Parent(parent), InvalidDomain(), Domain(), R(&R), Build(), BaseName(Name), 1207 SurroundingLoop(SurroundingLoop), Instructions(EntryBlockInstructions) {} 1208 1209 ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, 1210 Loop *SurroundingLoop, 1211 std::vector<Instruction *> Instructions) 1212 : Parent(parent), InvalidDomain(), Domain(), BB(&bb), Build(), 1213 BaseName(Name), SurroundingLoop(SurroundingLoop), 1214 Instructions(Instructions) {} 1215 1216 ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, 1217 isl::set NewDomain) 1218 : Parent(parent), InvalidDomain(), Domain(NewDomain), Build() { 1219 BaseName = getIslCompatibleName("CopyStmt_", "", 1220 std::to_string(parent.getCopyStmtsNum())); 1221 isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); 1222 Domain = Domain.set_tuple_id(Id); 1223 TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); 1224 auto *Access = 1225 new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); 1226 parent.addAccessFunction(Access); 1227 addAccess(Access); 1228 SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); 1229 Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); 1230 parent.addAccessFunction(Access); 1231 addAccess(Access); 1232 } 1233 1234 ScopStmt::~ScopStmt() = default; 1235 1236 std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } 1237 1238 std::string ScopStmt::getScheduleStr() const { 1239 return stringFromIslObj(getSchedule()); 1240 } 1241 1242 void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } 1243 1244 BasicBlock *ScopStmt::getEntryBlock() const { 1245 if (isBlockStmt()) 1246 return getBasicBlock(); 1247 return getRegion()->getEntry(); 1248 } 1249 1250 unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } 1251 1252 const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } 1253 1254 Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { 1255 return NestLoops[Dimension]; 1256 } 1257 1258 isl::ctx ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } 1259 1260 isl::set ScopStmt::getDomain() const { return Domain; } 1261 1262 isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } 1263 1264 isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } 1265 1266 void ScopStmt::printInstructions(raw_ostream &OS) const { 1267 OS << "Instructions {\n"; 1268 1269 for (Instruction *Inst : Instructions) 1270 OS.indent(16) << *Inst << "\n"; 1271 1272 OS.indent(12) << "}\n"; 1273 } 1274 1275 void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { 1276 OS << "\t" << getBaseName() << "\n"; 1277 OS.indent(12) << "Domain :=\n"; 1278 1279 if (!Domain.is_null()) { 1280 OS.indent(16) << getDomainStr() << ";\n"; 1281 } else 1282 OS.indent(16) << "n/a\n"; 1283 1284 OS.indent(12) << "Schedule :=\n"; 1285 1286 if (!Domain.is_null()) { 1287 OS.indent(16) << getScheduleStr() << ";\n"; 1288 } else 1289 OS.indent(16) << "n/a\n"; 1290 1291 for (MemoryAccess *Access : MemAccs) 1292 Access->print(OS); 1293 1294 if (PrintInstructions) 1295 printInstructions(OS.indent(12)); 1296 } 1297 1298 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1299 LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } 1300 #endif 1301 1302 void ScopStmt::removeAccessData(MemoryAccess *MA) { 1303 if (MA->isRead() && MA->isOriginalValueKind()) { 1304 bool Found = ValueReads.erase(MA->getAccessValue()); 1305 (void)Found; 1306 assert(Found && "Expected access data not found"); 1307 } 1308 if (MA->isWrite() && MA->isOriginalValueKind()) { 1309 bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); 1310 (void)Found; 1311 assert(Found && "Expected access data not found"); 1312 } 1313 if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { 1314 bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); 1315 (void)Found; 1316 assert(Found && "Expected access data not found"); 1317 } 1318 if (MA->isRead() && MA->isOriginalAnyPHIKind()) { 1319 bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); 1320 (void)Found; 1321 assert(Found && "Expected access data not found"); 1322 } 1323 } 1324 1325 void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { 1326 // Remove the memory accesses from this statement together with all scalar 1327 // accesses that were caused by it. MemoryKind::Value READs have no access 1328 // instruction, hence would not be removed by this function. However, it is 1329 // only used for invariant LoadInst accesses, its arguments are always affine, 1330 // hence synthesizable, and therefore there are no MemoryKind::Value READ 1331 // accesses to be removed. 1332 auto Predicate = [&](MemoryAccess *Acc) { 1333 return Acc->getAccessInstruction() == MA->getAccessInstruction(); 1334 }; 1335 for (auto *MA : MemAccs) { 1336 if (Predicate(MA)) { 1337 removeAccessData(MA); 1338 Parent.removeAccessData(MA); 1339 } 1340 } 1341 MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), 1342 MemAccs.end()); 1343 InstructionToAccess.erase(MA->getAccessInstruction()); 1344 } 1345 1346 void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting) { 1347 if (AfterHoisting) { 1348 auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); 1349 assert(MAIt != MemAccs.end()); 1350 MemAccs.erase(MAIt); 1351 1352 removeAccessData(MA); 1353 Parent.removeAccessData(MA); 1354 } 1355 1356 auto It = InstructionToAccess.find(MA->getAccessInstruction()); 1357 if (It != InstructionToAccess.end()) { 1358 It->second.remove(MA); 1359 if (It->second.empty()) 1360 InstructionToAccess.erase(MA->getAccessInstruction()); 1361 } 1362 } 1363 1364 MemoryAccess *ScopStmt::ensureValueRead(Value *V) { 1365 MemoryAccess *Access = lookupInputAccessOf(V); 1366 if (Access) 1367 return Access; 1368 1369 ScopArrayInfo *SAI = 1370 Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); 1371 Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), 1372 true, {}, {}, V, MemoryKind::Value); 1373 Parent.addAccessFunction(Access); 1374 Access->buildAccessRelation(SAI); 1375 addAccess(Access); 1376 Parent.addAccessData(Access); 1377 return Access; 1378 } 1379 1380 raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { 1381 S.print(OS, PollyPrintInstructions); 1382 return OS; 1383 } 1384 1385 //===----------------------------------------------------------------------===// 1386 /// Scop class implement 1387 1388 void Scop::setContext(isl::set NewContext) { 1389 Context = NewContext.align_params(Context.get_space()); 1390 } 1391 1392 namespace { 1393 1394 /// Remap parameter values but keep AddRecs valid wrt. invariant loads. 1395 struct SCEVSensitiveParameterRewriter 1396 : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { 1397 const ValueToValueMap &VMap; 1398 1399 public: 1400 SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, 1401 ScalarEvolution &SE) 1402 : SCEVRewriteVisitor(SE), VMap(VMap) {} 1403 1404 static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, 1405 const ValueToValueMap &VMap) { 1406 SCEVSensitiveParameterRewriter SSPR(VMap, SE); 1407 return SSPR.visit(E); 1408 } 1409 1410 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { 1411 auto *Start = visit(E->getStart()); 1412 auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), 1413 visit(E->getStepRecurrence(SE)), 1414 E->getLoop(), SCEV::FlagAnyWrap); 1415 return SE.getAddExpr(Start, AddRec); 1416 } 1417 1418 const SCEV *visitUnknown(const SCEVUnknown *E) { 1419 if (auto *NewValue = VMap.lookup(E->getValue())) 1420 return SE.getUnknown(NewValue); 1421 return E; 1422 } 1423 }; 1424 1425 /// Check whether we should remap a SCEV expression. 1426 struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { 1427 const ValueToValueMap &VMap; 1428 bool FoundInside = false; 1429 const Scop *S; 1430 1431 public: 1432 SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, 1433 const Scop *S) 1434 : SCEVTraversal(*this), VMap(VMap), S(S) {} 1435 1436 static bool hasVariant(const SCEV *E, ScalarEvolution &SE, 1437 const ValueToValueMap &VMap, const Scop *S) { 1438 SCEVFindInsideScop SFIS(VMap, SE, S); 1439 SFIS.visitAll(E); 1440 return SFIS.FoundInside; 1441 } 1442 1443 bool follow(const SCEV *E) { 1444 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { 1445 FoundInside |= S->getRegion().contains(AddRec->getLoop()); 1446 } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { 1447 if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) 1448 FoundInside |= S->getRegion().contains(I) && !VMap.count(I); 1449 } 1450 return !FoundInside; 1451 } 1452 1453 bool isDone() { return FoundInside; } 1454 }; 1455 } // end anonymous namespace 1456 1457 const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { 1458 // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution 1459 // doesn't like addition between an AddRec and an expression that 1460 // doesn't have a dominance relationship with it.) 1461 if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) 1462 return E; 1463 1464 // Rewrite SCEV. 1465 return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); 1466 } 1467 1468 // This table of function names is used to translate parameter names in more 1469 // human-readable names. This makes it easier to interpret Polly analysis 1470 // results. 1471 StringMap<std::string> KnownNames = { 1472 {"_Z13get_global_idj", "global_id"}, 1473 {"_Z12get_local_idj", "local_id"}, 1474 {"_Z15get_global_sizej", "global_size"}, 1475 {"_Z14get_local_sizej", "local_size"}, 1476 {"_Z12get_work_dimv", "work_dim"}, 1477 {"_Z17get_global_offsetj", "global_offset"}, 1478 {"_Z12get_group_idj", "group_id"}, 1479 {"_Z14get_num_groupsj", "num_groups"}, 1480 }; 1481 1482 static std::string getCallParamName(CallInst *Call) { 1483 std::string Result; 1484 raw_string_ostream OS(Result); 1485 std::string Name = Call->getCalledFunction()->getName().str(); 1486 1487 auto Iterator = KnownNames.find(Name); 1488 if (Iterator != KnownNames.end()) 1489 Name = "__" + Iterator->getValue(); 1490 OS << Name; 1491 for (auto &Operand : Call->arg_operands()) { 1492 ConstantInt *Op = cast<ConstantInt>(&Operand); 1493 OS << "_" << Op->getValue(); 1494 } 1495 OS.flush(); 1496 return Result; 1497 } 1498 1499 void Scop::createParameterId(const SCEV *Parameter) { 1500 assert(Parameters.count(Parameter)); 1501 assert(!ParameterIds.count(Parameter)); 1502 1503 std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); 1504 1505 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { 1506 Value *Val = ValueParameter->getValue(); 1507 CallInst *Call = dyn_cast<CallInst>(Val); 1508 1509 if (Call && isConstCall(Call)) { 1510 ParameterName = getCallParamName(Call); 1511 } else if (UseInstructionNames) { 1512 // If this parameter references a specific Value and this value has a name 1513 // we use this name as it is likely to be unique and more useful than just 1514 // a number. 1515 if (Val->hasName()) 1516 ParameterName = Val->getName().str(); 1517 else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { 1518 auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); 1519 if (LoadOrigin->hasName()) { 1520 ParameterName += "_loaded_from_"; 1521 ParameterName += 1522 LI->getPointerOperand()->stripInBoundsOffsets()->getName(); 1523 } 1524 } 1525 } 1526 1527 ParameterName = getIslCompatibleName("", ParameterName, ""); 1528 } 1529 1530 isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, 1531 const_cast<void *>((const void *)Parameter)); 1532 ParameterIds[Parameter] = Id; 1533 } 1534 1535 void Scop::addParams(const ParameterSetTy &NewParameters) { 1536 for (const SCEV *Parameter : NewParameters) { 1537 // Normalize the SCEV to get the representing element for an invariant load. 1538 Parameter = extractConstantFactor(Parameter, *SE).second; 1539 Parameter = getRepresentingInvariantLoadSCEV(Parameter); 1540 1541 if (Parameters.insert(Parameter)) 1542 createParameterId(Parameter); 1543 } 1544 } 1545 1546 isl::id Scop::getIdForParam(const SCEV *Parameter) const { 1547 // Normalize the SCEV to get the representing element for an invariant load. 1548 Parameter = getRepresentingInvariantLoadSCEV(Parameter); 1549 return ParameterIds.lookup(Parameter); 1550 } 1551 1552 bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { 1553 return DT.dominates(BB, getEntry()); 1554 } 1555 1556 void Scop::buildContext() { 1557 isl::space Space = isl::space::params_alloc(getIslCtx(), 0); 1558 Context = isl::set::universe(Space); 1559 InvalidContext = isl::set::empty(Space); 1560 AssumedContext = isl::set::universe(Space); 1561 DefinedBehaviorContext = isl::set::universe(Space); 1562 } 1563 1564 void Scop::addParameterBounds() { 1565 unsigned PDim = 0; 1566 for (auto *Parameter : Parameters) { 1567 ConstantRange SRange = SE->getSignedRange(Parameter); 1568 Context = addRangeBoundsToSet(Context, SRange, PDim++, isl::dim::param); 1569 } 1570 intersectDefinedBehavior(Context, AS_ASSUMPTION); 1571 } 1572 1573 static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { 1574 std::vector<isl::id> OutermostSizeIds; 1575 for (auto Array : Arrays) { 1576 // To check if an array is a Fortran array, we check if it has a isl_pw_aff 1577 // for its outermost dimension. Fortran arrays will have this since the 1578 // outermost dimension size can be picked up from their runtime description. 1579 // TODO: actually need to check if it has a FAD, but for now this works. 1580 if (Array->getNumberOfDimensions() > 0) { 1581 isl::pw_aff PwAff = Array->getDimensionSizePw(0); 1582 if (PwAff.is_null()) 1583 continue; 1584 1585 isl::id Id = PwAff.get_dim_id(isl::dim::param, 0); 1586 assert(!Id.is_null() && 1587 "Invalid Id for PwAff expression in Fortran array"); 1588 OutermostSizeIds.push_back(Id); 1589 } 1590 } 1591 return OutermostSizeIds; 1592 } 1593 1594 // The FORTRAN array size parameters are known to be non-negative. 1595 static isl::set boundFortranArrayParams(isl::set Context, 1596 Scop::array_range Arrays) { 1597 std::vector<isl::id> OutermostSizeIds; 1598 OutermostSizeIds = getFortranArrayIds(Arrays); 1599 1600 for (isl::id Id : OutermostSizeIds) { 1601 int dim = Context.find_dim_by_id(isl::dim::param, Id); 1602 Context = Context.lower_bound_si(isl::dim::param, dim, 0); 1603 } 1604 1605 return Context; 1606 } 1607 1608 void Scop::realignParams() { 1609 if (PollyIgnoreParamBounds) 1610 return; 1611 1612 // Add all parameters into a common model. 1613 isl::space Space = getFullParamSpace(); 1614 1615 // Align the parameters of all data structures to the model. 1616 Context = Context.align_params(Space); 1617 AssumedContext = AssumedContext.align_params(Space); 1618 InvalidContext = InvalidContext.align_params(Space); 1619 1620 // Bound the size of the fortran array dimensions. 1621 Context = boundFortranArrayParams(Context, arrays()); 1622 1623 // As all parameters are known add bounds to them. 1624 addParameterBounds(); 1625 1626 for (ScopStmt &Stmt : *this) 1627 Stmt.realignParams(); 1628 // Simplify the schedule according to the context too. 1629 Schedule = Schedule.gist_domain_params(getContext()); 1630 1631 // Predictable parameter order is required for JSON imports. Ensure alignment 1632 // by explicitly calling align_params. 1633 Schedule = Schedule.align_params(Space); 1634 } 1635 1636 static isl::set simplifyAssumptionContext(isl::set AssumptionContext, 1637 const Scop &S) { 1638 // If we have modeled all blocks in the SCoP that have side effects we can 1639 // simplify the context with the constraints that are needed for anything to 1640 // be executed at all. However, if we have error blocks in the SCoP we already 1641 // assumed some parameter combinations cannot occur and removed them from the 1642 // domains, thus we cannot use the remaining domain to simplify the 1643 // assumptions. 1644 if (!S.hasErrorBlock()) { 1645 auto DomainParameters = S.getDomains().params(); 1646 AssumptionContext = AssumptionContext.gist_params(DomainParameters); 1647 } 1648 1649 AssumptionContext = AssumptionContext.gist_params(S.getContext()); 1650 return AssumptionContext; 1651 } 1652 1653 void Scop::simplifyContexts() { 1654 // The parameter constraints of the iteration domains give us a set of 1655 // constraints that need to hold for all cases where at least a single 1656 // statement iteration is executed in the whole scop. We now simplify the 1657 // assumed context under the assumption that such constraints hold and at 1658 // least a single statement iteration is executed. For cases where no 1659 // statement instances are executed, the assumptions we have taken about 1660 // the executed code do not matter and can be changed. 1661 // 1662 // WARNING: This only holds if the assumptions we have taken do not reduce 1663 // the set of statement instances that are executed. Otherwise we 1664 // may run into a case where the iteration domains suggest that 1665 // for a certain set of parameter constraints no code is executed, 1666 // but in the original program some computation would have been 1667 // performed. In such a case, modifying the run-time conditions and 1668 // possibly influencing the run-time check may cause certain scops 1669 // to not be executed. 1670 // 1671 // Example: 1672 // 1673 // When delinearizing the following code: 1674 // 1675 // for (long i = 0; i < 100; i++) 1676 // for (long j = 0; j < m; j++) 1677 // A[i+p][j] = 1.0; 1678 // 1679 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as 1680 // otherwise we would access out of bound data. Now, knowing that code is 1681 // only executed for the case m >= 0, it is sufficient to assume p >= 0. 1682 AssumedContext = simplifyAssumptionContext(AssumedContext, *this); 1683 InvalidContext = InvalidContext.align_params(getParamSpace()); 1684 simplify(DefinedBehaviorContext); 1685 DefinedBehaviorContext = DefinedBehaviorContext.align_params(getParamSpace()); 1686 } 1687 1688 isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { 1689 return getDomainConditions(Stmt->getEntryBlock()); 1690 } 1691 1692 isl::set Scop::getDomainConditions(BasicBlock *BB) const { 1693 auto DIt = DomainMap.find(BB); 1694 if (DIt != DomainMap.end()) 1695 return DIt->getSecond(); 1696 1697 auto &RI = *R.getRegionInfo(); 1698 auto *BBR = RI.getRegionFor(BB); 1699 while (BBR->getEntry() == BB) 1700 BBR = BBR->getParent(); 1701 return getDomainConditions(BBR->getEntry()); 1702 } 1703 1704 Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, 1705 DominatorTree &DT, ScopDetection::DetectionContext &DC, 1706 OptimizationRemarkEmitter &ORE, int ID) 1707 : IslCtx(isl_ctx_alloc(), isl_ctx_free), SE(&ScalarEvolution), DT(&DT), 1708 R(R), name(None), HasSingleExitEdge(R.getExitingBlock()), DC(DC), 1709 ORE(ORE), Affinator(this, LI), ID(ID) { 1710 SmallVector<char *, 8> IslArgv; 1711 IslArgv.reserve(1 + IslArgs.size()); 1712 1713 // Substitute for program name. 1714 IslArgv.push_back(const_cast<char *>("-polly-isl-arg")); 1715 1716 for (std::string &Arg : IslArgs) 1717 IslArgv.push_back(const_cast<char *>(Arg.c_str())); 1718 1719 // Abort if unknown argument is passed. 1720 // Note that "-V" (print isl version) will always call exit(0), so we cannot 1721 // avoid ISL aborting the program at this point. 1722 unsigned IslParseFlags = ISL_ARG_ALL; 1723 1724 isl_ctx_parse_options(IslCtx.get(), IslArgv.size(), IslArgv.data(), 1725 IslParseFlags); 1726 1727 if (IslOnErrorAbort) 1728 isl_options_set_on_error(getIslCtx().get(), ISL_ON_ERROR_ABORT); 1729 buildContext(); 1730 } 1731 1732 Scop::~Scop() = default; 1733 1734 void Scop::removeFromStmtMap(ScopStmt &Stmt) { 1735 for (Instruction *Inst : Stmt.getInstructions()) 1736 InstStmtMap.erase(Inst); 1737 1738 if (Stmt.isRegionStmt()) { 1739 for (BasicBlock *BB : Stmt.getRegion()->blocks()) { 1740 StmtMap.erase(BB); 1741 // Skip entry basic block, as its instructions are already deleted as 1742 // part of the statement's instruction list. 1743 if (BB == Stmt.getEntryBlock()) 1744 continue; 1745 for (Instruction &Inst : *BB) 1746 InstStmtMap.erase(&Inst); 1747 } 1748 } else { 1749 auto StmtMapIt = StmtMap.find(Stmt.getBasicBlock()); 1750 if (StmtMapIt != StmtMap.end()) 1751 StmtMapIt->second.erase(std::remove(StmtMapIt->second.begin(), 1752 StmtMapIt->second.end(), &Stmt), 1753 StmtMapIt->second.end()); 1754 for (Instruction *Inst : Stmt.getInstructions()) 1755 InstStmtMap.erase(Inst); 1756 } 1757 } 1758 1759 void Scop::removeStmts(function_ref<bool(ScopStmt &)> ShouldDelete, 1760 bool AfterHoisting) { 1761 for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { 1762 if (!ShouldDelete(*StmtIt)) { 1763 StmtIt++; 1764 continue; 1765 } 1766 1767 // Start with removing all of the statement's accesses including erasing it 1768 // from all maps that are pointing to them. 1769 // Make a temporary copy because removing MAs invalidates the iterator. 1770 SmallVector<MemoryAccess *, 16> MAList(StmtIt->begin(), StmtIt->end()); 1771 for (MemoryAccess *MA : MAList) 1772 StmtIt->removeSingleMemoryAccess(MA, AfterHoisting); 1773 1774 removeFromStmtMap(*StmtIt); 1775 StmtIt = Stmts.erase(StmtIt); 1776 } 1777 } 1778 1779 void Scop::removeStmtNotInDomainMap() { 1780 removeStmts([this](ScopStmt &Stmt) -> bool { 1781 isl::set Domain = DomainMap.lookup(Stmt.getEntryBlock()); 1782 if (Domain.is_null()) 1783 return true; 1784 return Domain.is_empty(); 1785 }); 1786 } 1787 1788 void Scop::simplifySCoP(bool AfterHoisting) { 1789 removeStmts( 1790 [AfterHoisting](ScopStmt &Stmt) -> bool { 1791 // Never delete statements that contain calls to debug functions. 1792 if (hasDebugCall(&Stmt)) 1793 return false; 1794 1795 bool RemoveStmt = Stmt.isEmpty(); 1796 1797 // Remove read only statements only after invariant load hoisting. 1798 if (!RemoveStmt && AfterHoisting) { 1799 bool OnlyRead = true; 1800 for (MemoryAccess *MA : Stmt) { 1801 if (MA->isRead()) 1802 continue; 1803 1804 OnlyRead = false; 1805 break; 1806 } 1807 1808 RemoveStmt = OnlyRead; 1809 } 1810 return RemoveStmt; 1811 }, 1812 AfterHoisting); 1813 } 1814 1815 InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { 1816 LoadInst *LInst = dyn_cast<LoadInst>(Val); 1817 if (!LInst) 1818 return nullptr; 1819 1820 if (Value *Rep = InvEquivClassVMap.lookup(LInst)) 1821 LInst = cast<LoadInst>(Rep); 1822 1823 Type *Ty = LInst->getType(); 1824 const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); 1825 for (auto &IAClass : InvariantEquivClasses) { 1826 if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) 1827 continue; 1828 1829 auto &MAs = IAClass.InvariantAccesses; 1830 for (auto *MA : MAs) 1831 if (MA->getAccessInstruction() == Val) 1832 return &IAClass; 1833 } 1834 1835 return nullptr; 1836 } 1837 1838 ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, 1839 ArrayRef<const SCEV *> Sizes, 1840 MemoryKind Kind, 1841 const char *BaseName) { 1842 assert((BasePtr || BaseName) && 1843 "BasePtr and BaseName can not be nullptr at the same time."); 1844 assert(!(BasePtr && BaseName) && "BaseName is redundant."); 1845 auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] 1846 : ScopArrayNameMap[BaseName]; 1847 if (!SAI) { 1848 auto &DL = getFunction().getParent()->getDataLayout(); 1849 SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, 1850 DL, this, BaseName)); 1851 ScopArrayInfoSet.insert(SAI.get()); 1852 } else { 1853 SAI->updateElementType(ElementType); 1854 // In case of mismatching array sizes, we bail out by setting the run-time 1855 // context to false. 1856 if (!SAI->updateSizes(Sizes)) 1857 invalidate(DELINEARIZATION, DebugLoc()); 1858 } 1859 return SAI.get(); 1860 } 1861 1862 ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, 1863 const std::string &BaseName, 1864 const std::vector<unsigned> &Sizes) { 1865 auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); 1866 std::vector<const SCEV *> SCEVSizes; 1867 1868 for (auto size : Sizes) 1869 if (size) 1870 SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); 1871 else 1872 SCEVSizes.push_back(nullptr); 1873 1874 auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, 1875 MemoryKind::Array, BaseName.c_str()); 1876 return SAI; 1877 } 1878 1879 ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, MemoryKind Kind) { 1880 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); 1881 return SAI; 1882 } 1883 1884 ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { 1885 auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); 1886 assert(SAI && "No ScopArrayInfo available for this base pointer"); 1887 return SAI; 1888 } 1889 1890 std::string Scop::getContextStr() const { 1891 return stringFromIslObj(getContext()); 1892 } 1893 1894 std::string Scop::getAssumedContextStr() const { 1895 assert(!AssumedContext.is_null() && "Assumed context not yet built"); 1896 return stringFromIslObj(AssumedContext); 1897 } 1898 1899 std::string Scop::getInvalidContextStr() const { 1900 return stringFromIslObj(InvalidContext); 1901 } 1902 1903 std::string Scop::getNameStr() const { 1904 std::string ExitName, EntryName; 1905 std::tie(EntryName, ExitName) = getEntryExitStr(); 1906 return EntryName + "---" + ExitName; 1907 } 1908 1909 std::pair<std::string, std::string> Scop::getEntryExitStr() const { 1910 std::string ExitName, EntryName; 1911 raw_string_ostream ExitStr(ExitName); 1912 raw_string_ostream EntryStr(EntryName); 1913 1914 R.getEntry()->printAsOperand(EntryStr, false); 1915 EntryStr.str(); 1916 1917 if (R.getExit()) { 1918 R.getExit()->printAsOperand(ExitStr, false); 1919 ExitStr.str(); 1920 } else 1921 ExitName = "FunctionExit"; 1922 1923 return std::make_pair(EntryName, ExitName); 1924 } 1925 1926 isl::set Scop::getContext() const { return Context; } 1927 1928 isl::space Scop::getParamSpace() const { return getContext().get_space(); } 1929 1930 isl::space Scop::getFullParamSpace() const { 1931 std::vector<isl::id> FortranIDs; 1932 FortranIDs = getFortranArrayIds(arrays()); 1933 1934 isl::space Space = isl::space::params_alloc( 1935 getIslCtx(), ParameterIds.size() + FortranIDs.size()); 1936 1937 unsigned PDim = 0; 1938 for (const SCEV *Parameter : Parameters) { 1939 isl::id Id = getIdForParam(Parameter); 1940 Space = Space.set_dim_id(isl::dim::param, PDim++, Id); 1941 } 1942 1943 for (isl::id Id : FortranIDs) 1944 Space = Space.set_dim_id(isl::dim::param, PDim++, Id); 1945 1946 return Space; 1947 } 1948 1949 isl::set Scop::getAssumedContext() const { 1950 assert(!AssumedContext.is_null() && "Assumed context not yet built"); 1951 return AssumedContext; 1952 } 1953 1954 bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { 1955 if (PollyProcessUnprofitable) 1956 return true; 1957 1958 if (isEmpty()) 1959 return false; 1960 1961 unsigned OptimizableStmtsOrLoops = 0; 1962 for (auto &Stmt : *this) { 1963 if (Stmt.getNumIterators() == 0) 1964 continue; 1965 1966 bool ContainsArrayAccs = false; 1967 bool ContainsScalarAccs = false; 1968 for (auto *MA : Stmt) { 1969 if (MA->isRead()) 1970 continue; 1971 ContainsArrayAccs |= MA->isLatestArrayKind(); 1972 ContainsScalarAccs |= MA->isLatestScalarKind(); 1973 } 1974 1975 if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) 1976 OptimizableStmtsOrLoops += Stmt.getNumIterators(); 1977 } 1978 1979 return OptimizableStmtsOrLoops > 1; 1980 } 1981 1982 bool Scop::hasFeasibleRuntimeContext() const { 1983 if (Stmts.empty()) 1984 return false; 1985 1986 isl::set PositiveContext = getAssumedContext(); 1987 isl::set NegativeContext = getInvalidContext(); 1988 PositiveContext = PositiveContext.intersect_params(Context); 1989 PositiveContext = PositiveContext.intersect_params(getDomains().params()); 1990 return PositiveContext.is_empty().is_false() && 1991 PositiveContext.is_subset(NegativeContext).is_false(); 1992 } 1993 1994 MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { 1995 Value *PointerBase = MA->getOriginalBaseAddr(); 1996 1997 auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); 1998 if (!PointerBaseInst) 1999 return nullptr; 2000 2001 auto *BasePtrStmt = getStmtFor(PointerBaseInst); 2002 if (!BasePtrStmt) 2003 return nullptr; 2004 2005 return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); 2006 } 2007 2008 static std::string toString(AssumptionKind Kind) { 2009 switch (Kind) { 2010 case ALIASING: 2011 return "No-aliasing"; 2012 case INBOUNDS: 2013 return "Inbounds"; 2014 case WRAPPING: 2015 return "No-overflows"; 2016 case UNSIGNED: 2017 return "Signed-unsigned"; 2018 case COMPLEXITY: 2019 return "Low complexity"; 2020 case PROFITABLE: 2021 return "Profitable"; 2022 case ERRORBLOCK: 2023 return "No-error"; 2024 case INFINITELOOP: 2025 return "Finite loop"; 2026 case INVARIANTLOAD: 2027 return "Invariant load"; 2028 case DELINEARIZATION: 2029 return "Delinearization"; 2030 } 2031 llvm_unreachable("Unknown AssumptionKind!"); 2032 } 2033 2034 bool Scop::isEffectiveAssumption(isl::set Set, AssumptionSign Sign) { 2035 if (Sign == AS_ASSUMPTION) { 2036 if (Context.is_subset(Set)) 2037 return false; 2038 2039 if (AssumedContext.is_subset(Set)) 2040 return false; 2041 } else { 2042 if (Set.is_disjoint(Context)) 2043 return false; 2044 2045 if (Set.is_subset(InvalidContext)) 2046 return false; 2047 } 2048 return true; 2049 } 2050 2051 bool Scop::trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, 2052 AssumptionSign Sign, BasicBlock *BB) { 2053 if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) 2054 return false; 2055 2056 // Do never emit trivial assumptions as they only clutter the output. 2057 if (!PollyRemarksMinimal) { 2058 isl::set Univ; 2059 if (Sign == AS_ASSUMPTION) 2060 Univ = isl::set::universe(Set.get_space()); 2061 2062 bool IsTrivial = (Sign == AS_RESTRICTION && Set.is_empty()) || 2063 (Sign == AS_ASSUMPTION && Univ.is_equal(Set)); 2064 2065 if (IsTrivial) 2066 return false; 2067 } 2068 2069 switch (Kind) { 2070 case ALIASING: 2071 AssumptionsAliasing++; 2072 break; 2073 case INBOUNDS: 2074 AssumptionsInbounds++; 2075 break; 2076 case WRAPPING: 2077 AssumptionsWrapping++; 2078 break; 2079 case UNSIGNED: 2080 AssumptionsUnsigned++; 2081 break; 2082 case COMPLEXITY: 2083 AssumptionsComplexity++; 2084 break; 2085 case PROFITABLE: 2086 AssumptionsUnprofitable++; 2087 break; 2088 case ERRORBLOCK: 2089 AssumptionsErrorBlock++; 2090 break; 2091 case INFINITELOOP: 2092 AssumptionsInfiniteLoop++; 2093 break; 2094 case INVARIANTLOAD: 2095 AssumptionsInvariantLoad++; 2096 break; 2097 case DELINEARIZATION: 2098 AssumptionsDelinearization++; 2099 break; 2100 } 2101 2102 auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; 2103 std::string Msg = toString(Kind) + Suffix + stringFromIslObj(Set); 2104 if (BB) 2105 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) 2106 << Msg); 2107 else 2108 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, 2109 R.getEntry()) 2110 << Msg); 2111 return true; 2112 } 2113 2114 void Scop::addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, 2115 AssumptionSign Sign, BasicBlock *BB, 2116 bool RequiresRTC) { 2117 // Simplify the assumptions/restrictions first. 2118 Set = Set.gist_params(getContext()); 2119 intersectDefinedBehavior(Set, Sign); 2120 2121 if (!RequiresRTC) 2122 return; 2123 2124 if (!trackAssumption(Kind, Set, Loc, Sign, BB)) 2125 return; 2126 2127 if (Sign == AS_ASSUMPTION) 2128 AssumedContext = AssumedContext.intersect(Set).coalesce(); 2129 else 2130 InvalidContext = InvalidContext.unite(Set).coalesce(); 2131 } 2132 2133 void Scop::intersectDefinedBehavior(isl::set Set, AssumptionSign Sign) { 2134 if (DefinedBehaviorContext.is_null()) 2135 return; 2136 2137 if (Sign == AS_ASSUMPTION) 2138 DefinedBehaviorContext = DefinedBehaviorContext.intersect(Set); 2139 else 2140 DefinedBehaviorContext = DefinedBehaviorContext.subtract(Set); 2141 2142 // Limit the complexity of the context. If complexity is exceeded, simplify 2143 // the set and check again. 2144 if (DefinedBehaviorContext.n_basic_set() > 2145 MaxDisjunktsInDefinedBehaviourContext) { 2146 simplify(DefinedBehaviorContext); 2147 if (DefinedBehaviorContext.n_basic_set() > 2148 MaxDisjunktsInDefinedBehaviourContext) 2149 DefinedBehaviorContext = {}; 2150 } 2151 } 2152 2153 void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { 2154 LLVM_DEBUG(dbgs() << "Invalidate SCoP because of reason " << Kind << "\n"); 2155 addAssumption(Kind, isl::set::empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); 2156 } 2157 2158 isl::set Scop::getInvalidContext() const { return InvalidContext; } 2159 2160 void Scop::printContext(raw_ostream &OS) const { 2161 OS << "Context:\n"; 2162 OS.indent(4) << Context << "\n"; 2163 2164 OS.indent(4) << "Assumed Context:\n"; 2165 OS.indent(4) << AssumedContext << "\n"; 2166 2167 OS.indent(4) << "Invalid Context:\n"; 2168 OS.indent(4) << InvalidContext << "\n"; 2169 2170 OS.indent(4) << "Defined Behavior Context:\n"; 2171 if (!DefinedBehaviorContext.is_null()) 2172 OS.indent(4) << DefinedBehaviorContext << "\n"; 2173 else 2174 OS.indent(4) << "<unavailable>\n"; 2175 2176 unsigned Dim = 0; 2177 for (const SCEV *Parameter : Parameters) 2178 OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; 2179 } 2180 2181 void Scop::printAliasAssumptions(raw_ostream &OS) const { 2182 int noOfGroups = 0; 2183 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { 2184 if (Pair.second.size() == 0) 2185 noOfGroups += 1; 2186 else 2187 noOfGroups += Pair.second.size(); 2188 } 2189 2190 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; 2191 if (MinMaxAliasGroups.empty()) { 2192 OS.indent(8) << "n/a\n"; 2193 return; 2194 } 2195 2196 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { 2197 2198 // If the group has no read only accesses print the write accesses. 2199 if (Pair.second.empty()) { 2200 OS.indent(8) << "[["; 2201 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { 2202 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second 2203 << ">"; 2204 } 2205 OS << " ]]\n"; 2206 } 2207 2208 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { 2209 OS.indent(8) << "[["; 2210 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; 2211 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { 2212 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second 2213 << ">"; 2214 } 2215 OS << " ]]\n"; 2216 } 2217 } 2218 } 2219 2220 void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { 2221 OS << "Statements {\n"; 2222 2223 for (const ScopStmt &Stmt : *this) { 2224 OS.indent(4); 2225 Stmt.print(OS, PrintInstructions); 2226 } 2227 2228 OS.indent(4) << "}\n"; 2229 } 2230 2231 void Scop::printArrayInfo(raw_ostream &OS) const { 2232 OS << "Arrays {\n"; 2233 2234 for (auto &Array : arrays()) 2235 Array->print(OS); 2236 2237 OS.indent(4) << "}\n"; 2238 2239 OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; 2240 2241 for (auto &Array : arrays()) 2242 Array->print(OS, /* SizeAsPwAff */ true); 2243 2244 OS.indent(4) << "}\n"; 2245 } 2246 2247 void Scop::print(raw_ostream &OS, bool PrintInstructions) const { 2248 OS.indent(4) << "Function: " << getFunction().getName() << "\n"; 2249 OS.indent(4) << "Region: " << getNameStr() << "\n"; 2250 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; 2251 OS.indent(4) << "Invariant Accesses: {\n"; 2252 for (const auto &IAClass : InvariantEquivClasses) { 2253 const auto &MAs = IAClass.InvariantAccesses; 2254 if (MAs.empty()) { 2255 OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; 2256 } else { 2257 MAs.front()->print(OS); 2258 OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext 2259 << "\n"; 2260 } 2261 } 2262 OS.indent(4) << "}\n"; 2263 printContext(OS.indent(4)); 2264 printArrayInfo(OS.indent(4)); 2265 printAliasAssumptions(OS); 2266 printStatements(OS.indent(4), PrintInstructions); 2267 } 2268 2269 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2270 LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } 2271 #endif 2272 2273 isl::ctx Scop::getIslCtx() const { return IslCtx.get(); } 2274 2275 __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, 2276 bool NonNegative, 2277 RecordedAssumptionsTy *RecordedAssumptions) { 2278 // First try to use the SCEVAffinator to generate a piecewise defined 2279 // affine function from @p E in the context of @p BB. If that tasks becomes to 2280 // complex the affinator might return a nullptr. In such a case we invalidate 2281 // the SCoP and return a dummy value. This way we do not need to add error 2282 // handling code to all users of this function. 2283 auto PWAC = Affinator.getPwAff(E, BB, RecordedAssumptions); 2284 if (!PWAC.first.is_null()) { 2285 // TODO: We could use a heuristic and either use: 2286 // SCEVAffinator::takeNonNegativeAssumption 2287 // or 2288 // SCEVAffinator::interpretAsUnsigned 2289 // to deal with unsigned or "NonNegative" SCEVs. 2290 if (NonNegative) 2291 Affinator.takeNonNegativeAssumption(PWAC, RecordedAssumptions); 2292 return PWAC; 2293 } 2294 2295 auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); 2296 invalidate(COMPLEXITY, DL, BB); 2297 return Affinator.getPwAff(SE->getZero(E->getType()), BB, RecordedAssumptions); 2298 } 2299 2300 isl::union_set Scop::getDomains() const { 2301 isl_space *EmptySpace = isl_space_params_alloc(getIslCtx().get(), 0); 2302 isl_union_set *Domain = isl_union_set_empty(EmptySpace); 2303 2304 for (const ScopStmt &Stmt : *this) 2305 Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); 2306 2307 return isl::manage(Domain); 2308 } 2309 2310 isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB, 2311 RecordedAssumptionsTy *RecordedAssumptions) { 2312 PWACtx PWAC = getPwAff(E, BB, RecordedAssumptions); 2313 return PWAC.first; 2314 } 2315 2316 isl::union_map 2317 Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { 2318 isl::union_map Accesses = isl::union_map::empty(getParamSpace()); 2319 2320 for (ScopStmt &Stmt : *this) { 2321 for (MemoryAccess *MA : Stmt) { 2322 if (!Predicate(*MA)) 2323 continue; 2324 2325 isl::set Domain = Stmt.getDomain(); 2326 isl::map AccessDomain = MA->getAccessRelation(); 2327 AccessDomain = AccessDomain.intersect_domain(Domain); 2328 Accesses = Accesses.add_map(AccessDomain); 2329 } 2330 } 2331 2332 return Accesses.coalesce(); 2333 } 2334 2335 isl::union_map Scop::getMustWrites() { 2336 return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); 2337 } 2338 2339 isl::union_map Scop::getMayWrites() { 2340 return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); 2341 } 2342 2343 isl::union_map Scop::getWrites() { 2344 return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); 2345 } 2346 2347 isl::union_map Scop::getReads() { 2348 return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); 2349 } 2350 2351 isl::union_map Scop::getAccesses() { 2352 return getAccessesOfType([](MemoryAccess &MA) { return true; }); 2353 } 2354 2355 isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { 2356 return getAccessesOfType( 2357 [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); 2358 } 2359 2360 isl::union_map Scop::getSchedule() const { 2361 auto Tree = getScheduleTree(); 2362 return Tree.get_map(); 2363 } 2364 2365 isl::schedule Scop::getScheduleTree() const { 2366 return Schedule.intersect_domain(getDomains()); 2367 } 2368 2369 void Scop::setSchedule(isl::union_map NewSchedule) { 2370 auto S = isl::schedule::from_domain(getDomains()); 2371 Schedule = S.insert_partial_schedule( 2372 isl::multi_union_pw_aff::from_union_map(NewSchedule)); 2373 ScheduleModified = true; 2374 } 2375 2376 void Scop::setScheduleTree(isl::schedule NewSchedule) { 2377 Schedule = NewSchedule; 2378 ScheduleModified = true; 2379 } 2380 2381 bool Scop::restrictDomains(isl::union_set Domain) { 2382 bool Changed = false; 2383 for (ScopStmt &Stmt : *this) { 2384 isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); 2385 isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); 2386 2387 if (StmtDomain.is_subset(NewStmtDomain)) 2388 continue; 2389 2390 Changed = true; 2391 2392 NewStmtDomain = NewStmtDomain.coalesce(); 2393 2394 if (NewStmtDomain.is_empty()) 2395 Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); 2396 else 2397 Stmt.restrictDomain(isl::set(NewStmtDomain)); 2398 } 2399 return Changed; 2400 } 2401 2402 ScalarEvolution *Scop::getSE() const { return SE; } 2403 2404 void Scop::addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, 2405 std::vector<Instruction *> Instructions) { 2406 assert(BB && "Unexpected nullptr!"); 2407 Stmts.emplace_back(*this, *BB, Name, SurroundingLoop, Instructions); 2408 auto *Stmt = &Stmts.back(); 2409 StmtMap[BB].push_back(Stmt); 2410 for (Instruction *Inst : Instructions) { 2411 assert(!InstStmtMap.count(Inst) && 2412 "Unexpected statement corresponding to the instruction."); 2413 InstStmtMap[Inst] = Stmt; 2414 } 2415 } 2416 2417 void Scop::addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop, 2418 std::vector<Instruction *> Instructions) { 2419 assert(R && "Unexpected nullptr!"); 2420 Stmts.emplace_back(*this, *R, Name, SurroundingLoop, Instructions); 2421 auto *Stmt = &Stmts.back(); 2422 2423 for (Instruction *Inst : Instructions) { 2424 assert(!InstStmtMap.count(Inst) && 2425 "Unexpected statement corresponding to the instruction."); 2426 InstStmtMap[Inst] = Stmt; 2427 } 2428 2429 for (BasicBlock *BB : R->blocks()) { 2430 StmtMap[BB].push_back(Stmt); 2431 if (BB == R->getEntry()) 2432 continue; 2433 for (Instruction &Inst : *BB) { 2434 assert(!InstStmtMap.count(&Inst) && 2435 "Unexpected statement corresponding to the instruction."); 2436 InstStmtMap[&Inst] = Stmt; 2437 } 2438 } 2439 } 2440 2441 ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, 2442 isl::set Domain) { 2443 #ifndef NDEBUG 2444 isl::set SourceDomain = SourceRel.domain(); 2445 isl::set TargetDomain = TargetRel.domain(); 2446 assert(Domain.is_subset(TargetDomain) && 2447 "Target access not defined for complete statement domain"); 2448 assert(Domain.is_subset(SourceDomain) && 2449 "Source access not defined for complete statement domain"); 2450 #endif 2451 Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); 2452 CopyStmtsNum++; 2453 return &(Stmts.back()); 2454 } 2455 2456 ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { 2457 auto StmtMapIt = StmtMap.find(BB); 2458 if (StmtMapIt == StmtMap.end()) 2459 return {}; 2460 return StmtMapIt->second; 2461 } 2462 2463 ScopStmt *Scop::getIncomingStmtFor(const Use &U) const { 2464 auto *PHI = cast<PHINode>(U.getUser()); 2465 BasicBlock *IncomingBB = PHI->getIncomingBlock(U); 2466 2467 // If the value is a non-synthesizable from the incoming block, use the 2468 // statement that contains it as user statement. 2469 if (auto *IncomingInst = dyn_cast<Instruction>(U.get())) { 2470 if (IncomingInst->getParent() == IncomingBB) { 2471 if (ScopStmt *IncomingStmt = getStmtFor(IncomingInst)) 2472 return IncomingStmt; 2473 } 2474 } 2475 2476 // Otherwise, use the epilogue/last statement. 2477 return getLastStmtFor(IncomingBB); 2478 } 2479 2480 ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { 2481 ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); 2482 if (!StmtList.empty()) 2483 return StmtList.back(); 2484 return nullptr; 2485 } 2486 2487 ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { 2488 if (RN->isSubRegion()) 2489 return getStmtListFor(RN->getNodeAs<Region>()); 2490 return getStmtListFor(RN->getNodeAs<BasicBlock>()); 2491 } 2492 2493 ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { 2494 return getStmtListFor(R->getEntry()); 2495 } 2496 2497 int Scop::getRelativeLoopDepth(const Loop *L) const { 2498 if (!L || !R.contains(L)) 2499 return -1; 2500 // outermostLoopInRegion always returns nullptr for top level regions 2501 if (R.isTopLevelRegion()) { 2502 // LoopInfo's depths start at 1, we start at 0 2503 return L->getLoopDepth() - 1; 2504 } else { 2505 Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); 2506 assert(OuterLoop); 2507 return L->getLoopDepth() - OuterLoop->getLoopDepth(); 2508 } 2509 } 2510 2511 ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { 2512 for (auto &SAI : arrays()) { 2513 if (SAI->getName() == BaseName) 2514 return SAI; 2515 } 2516 return nullptr; 2517 } 2518 2519 void Scop::addAccessData(MemoryAccess *Access) { 2520 const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); 2521 assert(SAI && "can only use after access relations have been constructed"); 2522 2523 if (Access->isOriginalValueKind() && Access->isRead()) 2524 ValueUseAccs[SAI].push_back(Access); 2525 else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) 2526 PHIIncomingAccs[SAI].push_back(Access); 2527 } 2528 2529 void Scop::removeAccessData(MemoryAccess *Access) { 2530 if (Access->isOriginalValueKind() && Access->isWrite()) { 2531 ValueDefAccs.erase(Access->getAccessValue()); 2532 } else if (Access->isOriginalValueKind() && Access->isRead()) { 2533 auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; 2534 auto NewEnd = std::remove(Uses.begin(), Uses.end(), Access); 2535 Uses.erase(NewEnd, Uses.end()); 2536 } else if (Access->isOriginalPHIKind() && Access->isRead()) { 2537 PHINode *PHI = cast<PHINode>(Access->getAccessInstruction()); 2538 PHIReadAccs.erase(PHI); 2539 } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { 2540 auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; 2541 auto NewEnd = std::remove(Incomings.begin(), Incomings.end(), Access); 2542 Incomings.erase(NewEnd, Incomings.end()); 2543 } 2544 } 2545 2546 MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { 2547 assert(SAI->isValueKind()); 2548 2549 Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); 2550 if (!Val) 2551 return nullptr; 2552 2553 return ValueDefAccs.lookup(Val); 2554 } 2555 2556 ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { 2557 assert(SAI->isValueKind()); 2558 auto It = ValueUseAccs.find(SAI); 2559 if (It == ValueUseAccs.end()) 2560 return {}; 2561 return It->second; 2562 } 2563 2564 MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { 2565 assert(SAI->isPHIKind() || SAI->isExitPHIKind()); 2566 2567 if (SAI->isExitPHIKind()) 2568 return nullptr; 2569 2570 PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); 2571 return PHIReadAccs.lookup(PHI); 2572 } 2573 2574 ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { 2575 assert(SAI->isPHIKind() || SAI->isExitPHIKind()); 2576 auto It = PHIIncomingAccs.find(SAI); 2577 if (It == PHIIncomingAccs.end()) 2578 return {}; 2579 return It->second; 2580 } 2581 2582 bool Scop::isEscaping(Instruction *Inst) { 2583 assert(contains(Inst) && "The concept of escaping makes only sense for " 2584 "values defined inside the SCoP"); 2585 2586 for (Use &Use : Inst->uses()) { 2587 BasicBlock *UserBB = getUseBlock(Use); 2588 if (!contains(UserBB)) 2589 return true; 2590 2591 // When the SCoP region exit needs to be simplified, PHIs in the region exit 2592 // move to a new basic block such that its incoming blocks are not in the 2593 // SCoP anymore. 2594 if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && 2595 isExit(cast<PHINode>(Use.getUser())->getParent())) 2596 return true; 2597 } 2598 return false; 2599 } 2600 2601 void Scop::incrementNumberOfAliasingAssumptions(unsigned step) { 2602 AssumptionsAliasing += step; 2603 } 2604 2605 Scop::ScopStatistics Scop::getStatistics() const { 2606 ScopStatistics Result; 2607 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 2608 auto LoopStat = ScopDetection::countBeneficialLoops(&R, *SE, *getLI(), 0); 2609 2610 int NumTotalLoops = LoopStat.NumLoops; 2611 Result.NumBoxedLoops = getBoxedLoops().size(); 2612 Result.NumAffineLoops = NumTotalLoops - Result.NumBoxedLoops; 2613 2614 for (const ScopStmt &Stmt : *this) { 2615 isl::set Domain = Stmt.getDomain().intersect_params(getContext()); 2616 bool IsInLoop = Stmt.getNumIterators() >= 1; 2617 for (MemoryAccess *MA : Stmt) { 2618 if (!MA->isWrite()) 2619 continue; 2620 2621 if (MA->isLatestValueKind()) { 2622 Result.NumValueWrites += 1; 2623 if (IsInLoop) 2624 Result.NumValueWritesInLoops += 1; 2625 } 2626 2627 if (MA->isLatestAnyPHIKind()) { 2628 Result.NumPHIWrites += 1; 2629 if (IsInLoop) 2630 Result.NumPHIWritesInLoops += 1; 2631 } 2632 2633 isl::set AccSet = 2634 MA->getAccessRelation().intersect_domain(Domain).range(); 2635 if (AccSet.is_singleton()) { 2636 Result.NumSingletonWrites += 1; 2637 if (IsInLoop) 2638 Result.NumSingletonWritesInLoops += 1; 2639 } 2640 } 2641 } 2642 #endif 2643 return Result; 2644 } 2645 2646 raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { 2647 scop.print(OS, PollyPrintInstructions); 2648 return OS; 2649 } 2650 2651 //===----------------------------------------------------------------------===// 2652 void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { 2653 AU.addRequired<LoopInfoWrapperPass>(); 2654 AU.addRequired<RegionInfoPass>(); 2655 AU.addRequired<DominatorTreeWrapperPass>(); 2656 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); 2657 AU.addRequiredTransitive<ScopDetectionWrapperPass>(); 2658 AU.addRequired<AAResultsWrapperPass>(); 2659 AU.addRequired<AssumptionCacheTracker>(); 2660 AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 2661 AU.setPreservesAll(); 2662 } 2663 2664 void updateLoopCountStatistic(ScopDetection::LoopStats Stats, 2665 Scop::ScopStatistics ScopStats) { 2666 assert(Stats.NumLoops == ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops); 2667 2668 NumScops++; 2669 NumLoopsInScop += Stats.NumLoops; 2670 MaxNumLoopsInScop = 2671 std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); 2672 2673 if (Stats.MaxDepth == 0) 2674 NumScopsDepthZero++; 2675 else if (Stats.MaxDepth == 1) 2676 NumScopsDepthOne++; 2677 else if (Stats.MaxDepth == 2) 2678 NumScopsDepthTwo++; 2679 else if (Stats.MaxDepth == 3) 2680 NumScopsDepthThree++; 2681 else if (Stats.MaxDepth == 4) 2682 NumScopsDepthFour++; 2683 else if (Stats.MaxDepth == 5) 2684 NumScopsDepthFive++; 2685 else 2686 NumScopsDepthLarger++; 2687 2688 NumAffineLoops += ScopStats.NumAffineLoops; 2689 NumBoxedLoops += ScopStats.NumBoxedLoops; 2690 2691 NumValueWrites += ScopStats.NumValueWrites; 2692 NumValueWritesInLoops += ScopStats.NumValueWritesInLoops; 2693 NumPHIWrites += ScopStats.NumPHIWrites; 2694 NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops; 2695 NumSingletonWrites += ScopStats.NumSingletonWrites; 2696 NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops; 2697 } 2698 2699 bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { 2700 auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); 2701 2702 if (!SD.isMaxRegionInScop(*R)) 2703 return false; 2704 2705 Function *F = R->getEntry()->getParent(); 2706 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 2707 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 2708 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); 2709 auto const &DL = F->getParent()->getDataLayout(); 2710 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 2711 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); 2712 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); 2713 2714 ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); 2715 S = SB.getScop(); // take ownership of scop object 2716 2717 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 2718 if (S) { 2719 ScopDetection::LoopStats Stats = 2720 ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); 2721 updateLoopCountStatistic(Stats, S->getStatistics()); 2722 } 2723 #endif 2724 2725 return false; 2726 } 2727 2728 void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { 2729 if (S) 2730 S->print(OS, PollyPrintInstructions); 2731 else 2732 OS << "Invalid Scop!\n"; 2733 } 2734 2735 char ScopInfoRegionPass::ID = 0; 2736 2737 Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } 2738 2739 INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", 2740 "Polly - Create polyhedral description of Scops", false, 2741 false); 2742 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); 2743 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); 2744 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 2745 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 2746 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 2747 INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); 2748 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 2749 INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", 2750 "Polly - Create polyhedral description of Scops", false, 2751 false) 2752 2753 //===----------------------------------------------------------------------===// 2754 ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, 2755 LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, 2756 AssumptionCache &AC, OptimizationRemarkEmitter &ORE) 2757 : DL(DL), SD(SD), SE(SE), LI(LI), AA(AA), DT(DT), AC(AC), ORE(ORE) { 2758 recompute(); 2759 } 2760 2761 void ScopInfo::recompute() { 2762 RegionToScopMap.clear(); 2763 /// Create polyhedral description of scops for all the valid regions of a 2764 /// function. 2765 for (auto &It : SD) { 2766 Region *R = const_cast<Region *>(It); 2767 if (!SD.isMaxRegionInScop(*R)) 2768 continue; 2769 2770 ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); 2771 std::unique_ptr<Scop> S = SB.getScop(); 2772 if (!S) 2773 continue; 2774 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) 2775 ScopDetection::LoopStats Stats = 2776 ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); 2777 updateLoopCountStatistic(Stats, S->getStatistics()); 2778 #endif 2779 bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; 2780 assert(Inserted && "Building Scop for the same region twice!"); 2781 (void)Inserted; 2782 } 2783 } 2784 2785 bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, 2786 FunctionAnalysisManager::Invalidator &Inv) { 2787 // Check whether the analysis, all analyses on functions have been preserved 2788 // or anything we're holding references to is being invalidated 2789 auto PAC = PA.getChecker<ScopInfoAnalysis>(); 2790 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || 2791 Inv.invalidate<ScopAnalysis>(F, PA) || 2792 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || 2793 Inv.invalidate<LoopAnalysis>(F, PA) || 2794 Inv.invalidate<AAManager>(F, PA) || 2795 Inv.invalidate<DominatorTreeAnalysis>(F, PA) || 2796 Inv.invalidate<AssumptionAnalysis>(F, PA); 2797 } 2798 2799 AnalysisKey ScopInfoAnalysis::Key; 2800 2801 ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, 2802 FunctionAnalysisManager &FAM) { 2803 auto &SD = FAM.getResult<ScopAnalysis>(F); 2804 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); 2805 auto &LI = FAM.getResult<LoopAnalysis>(F); 2806 auto &AA = FAM.getResult<AAManager>(F); 2807 auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); 2808 auto &AC = FAM.getResult<AssumptionAnalysis>(F); 2809 auto &DL = F.getParent()->getDataLayout(); 2810 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); 2811 return {DL, SD, SE, LI, AA, DT, AC, ORE}; 2812 } 2813 2814 PreservedAnalyses ScopInfoPrinterPass::run(Function &F, 2815 FunctionAnalysisManager &FAM) { 2816 auto &SI = FAM.getResult<ScopInfoAnalysis>(F); 2817 // Since the legacy PM processes Scops in bottom up, we print them in reverse 2818 // order here to keep the output persistent 2819 for (auto &It : reverse(SI)) { 2820 if (It.second) 2821 It.second->print(Stream, PollyPrintInstructions); 2822 else 2823 Stream << "Invalid Scop!\n"; 2824 } 2825 return PreservedAnalyses::all(); 2826 } 2827 2828 void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 2829 AU.addRequired<LoopInfoWrapperPass>(); 2830 AU.addRequired<RegionInfoPass>(); 2831 AU.addRequired<DominatorTreeWrapperPass>(); 2832 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); 2833 AU.addRequiredTransitive<ScopDetectionWrapperPass>(); 2834 AU.addRequired<AAResultsWrapperPass>(); 2835 AU.addRequired<AssumptionCacheTracker>(); 2836 AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 2837 AU.setPreservesAll(); 2838 } 2839 2840 bool ScopInfoWrapperPass::runOnFunction(Function &F) { 2841 auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); 2842 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 2843 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 2844 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); 2845 auto const &DL = F.getParent()->getDataLayout(); 2846 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 2847 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 2848 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); 2849 2850 Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC, ORE}); 2851 return false; 2852 } 2853 2854 void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { 2855 for (auto &It : *Result) { 2856 if (It.second) 2857 It.second->print(OS, PollyPrintInstructions); 2858 else 2859 OS << "Invalid Scop!\n"; 2860 } 2861 } 2862 2863 char ScopInfoWrapperPass::ID = 0; 2864 2865 Pass *polly::createScopInfoWrapperPassPass() { 2866 return new ScopInfoWrapperPass(); 2867 } 2868 2869 INITIALIZE_PASS_BEGIN( 2870 ScopInfoWrapperPass, "polly-function-scops", 2871 "Polly - Create polyhedral description of all Scops of a function", false, 2872 false); 2873 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); 2874 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); 2875 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 2876 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 2877 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 2878 INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); 2879 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 2880 INITIALIZE_PASS_END( 2881 ScopInfoWrapperPass, "polly-function-scops", 2882 "Polly - Create polyhedral description of all Scops of a function", false, 2883 false) 2884