1 //===------ IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST---===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the IslNodeBuilder, a class to translate an isl AST into 11 // a LLVM-IR AST. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "polly/CodeGen/IslNodeBuilder.h" 16 #include "polly/CodeGen/BlockGenerators.h" 17 #include "polly/CodeGen/CodeGeneration.h" 18 #include "polly/CodeGen/IslAst.h" 19 #include "polly/CodeGen/IslExprBuilder.h" 20 #include "polly/CodeGen/LoopGenerators.h" 21 #include "polly/CodeGen/Utils.h" 22 #include "polly/Config/config.h" 23 #include "polly/DependenceInfo.h" 24 #include "polly/LinkAllPasses.h" 25 #include "polly/ScopInfo.h" 26 #include "polly/Support/GICHelper.h" 27 #include "polly/Support/SCEVValidator.h" 28 #include "polly/Support/ScopHelper.h" 29 #include "llvm/ADT/PostOrderIterator.h" 30 #include "llvm/ADT/SmallPtrSet.h" 31 #include "llvm/Analysis/LoopInfo.h" 32 #include "llvm/Analysis/PostDominators.h" 33 #include "llvm/IR/DataLayout.h" 34 #include "llvm/IR/Module.h" 35 #include "llvm/IR/Verifier.h" 36 #include "llvm/Support/CommandLine.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 39 #include "isl/aff.h" 40 #include "isl/ast.h" 41 #include "isl/ast_build.h" 42 #include "isl/list.h" 43 #include "isl/map.h" 44 #include "isl/set.h" 45 #include "isl/union_map.h" 46 #include "isl/union_set.h" 47 48 using namespace polly; 49 using namespace llvm; 50 51 // The maximal number of dimensions we allow during invariant load construction. 52 // More complex access ranges will result in very high compile time and are also 53 // unlikely to result in good code. This value is very high and should only 54 // trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006). 55 static int const MaxDimensionsInAccessRange = 9; 56 57 __isl_give isl_ast_expr * 58 IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For, 59 ICmpInst::Predicate &Predicate) { 60 isl_id *UBID, *IteratorID; 61 isl_ast_expr *Cond, *Iterator, *UB, *Arg0; 62 isl_ast_op_type Type; 63 64 Cond = isl_ast_node_for_get_cond(For); 65 Iterator = isl_ast_node_for_get_iterator(For); 66 isl_ast_expr_get_type(Cond); 67 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op && 68 "conditional expression is not an atomic upper bound"); 69 70 Type = isl_ast_expr_get_op_type(Cond); 71 72 switch (Type) { 73 case isl_ast_op_le: 74 Predicate = ICmpInst::ICMP_SLE; 75 break; 76 case isl_ast_op_lt: 77 Predicate = ICmpInst::ICMP_SLT; 78 break; 79 default: 80 llvm_unreachable("Unexpected comparision type in loop conditon"); 81 } 82 83 Arg0 = isl_ast_expr_get_op_arg(Cond, 0); 84 85 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id && 86 "conditional expression is not an atomic upper bound"); 87 88 UBID = isl_ast_expr_get_id(Arg0); 89 90 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id && 91 "Could not get the iterator"); 92 93 IteratorID = isl_ast_expr_get_id(Iterator); 94 95 assert(UBID == IteratorID && 96 "conditional expression is not an atomic upper bound"); 97 98 UB = isl_ast_expr_get_op_arg(Cond, 1); 99 100 isl_ast_expr_free(Cond); 101 isl_ast_expr_free(Iterator); 102 isl_ast_expr_free(Arg0); 103 isl_id_free(IteratorID); 104 isl_id_free(UBID); 105 106 return UB; 107 } 108 109 /// @brief Return true if a return value of Predicate is true for the value 110 /// represented by passed isl_ast_expr_int. 111 static bool checkIslAstExprInt(__isl_take isl_ast_expr *Expr, 112 isl_bool (*Predicate)(__isl_keep isl_val *)) { 113 if (isl_ast_expr_get_type(Expr) != isl_ast_expr_int) { 114 isl_ast_expr_free(Expr); 115 return false; 116 } 117 auto ExprVal = isl_ast_expr_get_val(Expr); 118 isl_ast_expr_free(Expr); 119 if (Predicate(ExprVal) != true) { 120 isl_val_free(ExprVal); 121 return false; 122 } 123 isl_val_free(ExprVal); 124 return true; 125 } 126 127 int IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) { 128 assert(isl_ast_node_get_type(For) == isl_ast_node_for); 129 auto Body = isl_ast_node_for_get_body(For); 130 131 // First, check if we can actually handle this code 132 switch (isl_ast_node_get_type(Body)) { 133 case isl_ast_node_user: 134 break; 135 case isl_ast_node_block: { 136 isl_ast_node_list *List = isl_ast_node_block_get_children(Body); 137 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) { 138 isl_ast_node *Node = isl_ast_node_list_get_ast_node(List, i); 139 int Type = isl_ast_node_get_type(Node); 140 isl_ast_node_free(Node); 141 if (Type != isl_ast_node_user) { 142 isl_ast_node_list_free(List); 143 isl_ast_node_free(Body); 144 return -1; 145 } 146 } 147 isl_ast_node_list_free(List); 148 break; 149 } 150 default: 151 isl_ast_node_free(Body); 152 return -1; 153 } 154 isl_ast_node_free(Body); 155 156 auto Init = isl_ast_node_for_get_init(For); 157 if (!checkIslAstExprInt(Init, isl_val_is_zero)) 158 return -1; 159 auto Inc = isl_ast_node_for_get_inc(For); 160 if (!checkIslAstExprInt(Inc, isl_val_is_one)) 161 return -1; 162 CmpInst::Predicate Predicate; 163 auto UB = getUpperBound(For, Predicate); 164 if (isl_ast_expr_get_type(UB) != isl_ast_expr_int) { 165 isl_ast_expr_free(UB); 166 return -1; 167 } 168 auto UpVal = isl_ast_expr_get_val(UB); 169 isl_ast_expr_free(UB); 170 int NumberIterations = isl_val_get_num_si(UpVal); 171 isl_val_free(UpVal); 172 if (NumberIterations < 0) 173 return -1; 174 if (Predicate == CmpInst::ICMP_SLT) 175 return NumberIterations; 176 else 177 return NumberIterations + 1; 178 } 179 180 struct SubtreeReferences { 181 LoopInfo &LI; 182 ScalarEvolution &SE; 183 Scop &S; 184 ValueMapT &GlobalMap; 185 SetVector<Value *> &Values; 186 SetVector<const SCEV *> &SCEVs; 187 BlockGenerator &BlockGen; 188 }; 189 190 /// @brief Extract the values and SCEVs needed to generate code for a block. 191 static int findReferencesInBlock(struct SubtreeReferences &References, 192 const ScopStmt *Stmt, const BasicBlock *BB) { 193 for (const Instruction &Inst : *BB) 194 for (Value *SrcVal : Inst.operands()) { 195 auto *Scope = References.LI.getLoopFor(BB); 196 if (canSynthesize(SrcVal, References.S, &References.LI, &References.SE, 197 Scope)) { 198 References.SCEVs.insert(References.SE.getSCEVAtScope(SrcVal, Scope)); 199 continue; 200 } else if (Value *NewVal = References.GlobalMap.lookup(SrcVal)) 201 References.Values.insert(NewVal); 202 } 203 return 0; 204 } 205 206 /// Extract the out-of-scop values and SCEVs referenced from a ScopStmt. 207 /// 208 /// This includes the SCEVUnknowns referenced by the SCEVs used in the 209 /// statement and the base pointers of the memory accesses. For scalar 210 /// statements we force the generation of alloca memory locations and list 211 /// these locations in the set of out-of-scop values as well. 212 /// 213 /// @param Stmt The statement for which to extract the information. 214 /// @param UserPtr A void pointer that can be casted to a SubtreeReferences 215 /// structure. 216 static isl_stat addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr) { 217 auto &References = *static_cast<struct SubtreeReferences *>(UserPtr); 218 219 if (Stmt->isBlockStmt()) 220 findReferencesInBlock(References, Stmt, Stmt->getBasicBlock()); 221 else { 222 assert(Stmt->isRegionStmt() && 223 "Stmt was neither block nor region statement"); 224 for (const BasicBlock *BB : Stmt->getRegion()->blocks()) 225 findReferencesInBlock(References, Stmt, BB); 226 } 227 228 for (auto &Access : *Stmt) { 229 if (Access->isArrayKind()) { 230 auto *BasePtr = Access->getScopArrayInfo()->getBasePtr(); 231 if (Instruction *OpInst = dyn_cast<Instruction>(BasePtr)) 232 if (Stmt->getParent()->contains(OpInst)) 233 continue; 234 235 References.Values.insert(BasePtr); 236 continue; 237 } 238 239 References.Values.insert(References.BlockGen.getOrCreateAlloca(*Access)); 240 } 241 242 return isl_stat_ok; 243 } 244 245 /// Extract the out-of-scop values and SCEVs referenced from a set describing 246 /// a ScopStmt. 247 /// 248 /// This includes the SCEVUnknowns referenced by the SCEVs used in the 249 /// statement and the base pointers of the memory accesses. For scalar 250 /// statements we force the generation of alloca memory locations and list 251 /// these locations in the set of out-of-scop values as well. 252 /// 253 /// @param Set A set which references the ScopStmt we are interested in. 254 /// @param UserPtr A void pointer that can be casted to a SubtreeReferences 255 /// structure. 256 static isl_stat addReferencesFromStmtSet(isl_set *Set, void *UserPtr) { 257 isl_id *Id = isl_set_get_tuple_id(Set); 258 auto *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id)); 259 isl_id_free(Id); 260 isl_set_free(Set); 261 return addReferencesFromStmt(Stmt, UserPtr); 262 } 263 264 /// Extract the out-of-scop values and SCEVs referenced from a union set 265 /// referencing multiple ScopStmts. 266 /// 267 /// This includes the SCEVUnknowns referenced by the SCEVs used in the 268 /// statement and the base pointers of the memory accesses. For scalar 269 /// statements we force the generation of alloca memory locations and list 270 /// these locations in the set of out-of-scop values as well. 271 /// 272 /// @param USet A union set referencing the ScopStmts we are interested 273 /// in. 274 /// @param References The SubtreeReferences data structure through which 275 /// results are returned and further information is 276 /// provided. 277 static void 278 addReferencesFromStmtUnionSet(isl_union_set *USet, 279 struct SubtreeReferences &References) { 280 isl_union_set_foreach_set(USet, addReferencesFromStmtSet, &References); 281 isl_union_set_free(USet); 282 } 283 284 __isl_give isl_union_map * 285 IslNodeBuilder::getScheduleForAstNode(__isl_keep isl_ast_node *For) { 286 return IslAstInfo::getSchedule(For); 287 } 288 289 void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For, 290 SetVector<Value *> &Values, 291 SetVector<const Loop *> &Loops) { 292 293 SetVector<const SCEV *> SCEVs; 294 struct SubtreeReferences References = { 295 LI, SE, S, ValueMap, Values, SCEVs, getBlockGenerator()}; 296 297 for (const auto &I : IDToValue) 298 Values.insert(I.second); 299 300 for (const auto &I : OutsideLoopIterations) 301 Values.insert(cast<SCEVUnknown>(I.second)->getValue()); 302 303 isl_union_set *Schedule = isl_union_map_domain(getScheduleForAstNode(For)); 304 addReferencesFromStmtUnionSet(Schedule, References); 305 306 for (const SCEV *Expr : SCEVs) { 307 findValues(Expr, SE, Values); 308 findLoops(Expr, Loops); 309 } 310 311 Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); }); 312 313 /// Remove loops that contain the scop or that are part of the scop, as they 314 /// are considered local. This leaves only loops that are before the scop, but 315 /// do not contain the scop itself. 316 Loops.remove_if([this](const Loop *L) { 317 return S.contains(L) || L->contains(S.getEntry()); 318 }); 319 } 320 321 void IslNodeBuilder::updateValues(ValueMapT &NewValues) { 322 SmallPtrSet<Value *, 5> Inserted; 323 324 for (const auto &I : IDToValue) { 325 IDToValue[I.first] = NewValues[I.second]; 326 Inserted.insert(I.second); 327 } 328 329 for (const auto &I : NewValues) { 330 if (Inserted.count(I.first)) 331 continue; 332 333 ValueMap[I.first] = I.second; 334 } 335 } 336 337 void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User, 338 std::vector<Value *> &IVS, 339 __isl_take isl_id *IteratorID, 340 __isl_take isl_union_map *Schedule) { 341 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); 342 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); 343 isl_id *Id = isl_ast_expr_get_id(StmtExpr); 344 isl_ast_expr_free(StmtExpr); 345 ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id); 346 std::vector<LoopToScevMapT> VLTS(IVS.size()); 347 348 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain()); 349 Schedule = isl_union_map_intersect_domain(Schedule, Domain); 350 isl_map *S = isl_map_from_union_map(Schedule); 351 352 auto *NewAccesses = createNewAccesses(Stmt, User); 353 createSubstitutionsVector(Expr, Stmt, VLTS, IVS, IteratorID); 354 VectorBlockGenerator::generate(BlockGen, *Stmt, VLTS, S, NewAccesses); 355 isl_id_to_ast_expr_free(NewAccesses); 356 isl_map_free(S); 357 isl_id_free(Id); 358 isl_ast_node_free(User); 359 } 360 361 void IslNodeBuilder::createMark(__isl_take isl_ast_node *Node) { 362 auto *Id = isl_ast_node_mark_get_id(Node); 363 auto Child = isl_ast_node_mark_get_node(Node); 364 isl_ast_node_free(Node); 365 // If a child node of a 'SIMD mark' is a loop that has a single iteration, 366 // it will be optimized away and we should skip it. 367 if (!strcmp(isl_id_get_name(Id), "SIMD") && 368 isl_ast_node_get_type(Child) == isl_ast_node_for) { 369 bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY; 370 int VectorWidth = getNumberOfIterations(Child); 371 if (Vector && 1 < VectorWidth && VectorWidth <= 16) 372 createForVector(Child, VectorWidth); 373 else 374 createForSequential(Child, true); 375 isl_id_free(Id); 376 return; 377 } 378 create(Child); 379 isl_id_free(Id); 380 } 381 382 void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, 383 int VectorWidth) { 384 isl_ast_node *Body = isl_ast_node_for_get_body(For); 385 isl_ast_expr *Init = isl_ast_node_for_get_init(For); 386 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); 387 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); 388 isl_id *IteratorID = isl_ast_expr_get_id(Iterator); 389 390 Value *ValueLB = ExprBuilder.create(Init); 391 Value *ValueInc = ExprBuilder.create(Inc); 392 393 ExprBuilder.unifyTypes(ValueLB, ValueInc); 394 395 std::vector<Value *> IVS(VectorWidth); 396 IVS[0] = ValueLB; 397 398 for (int i = 1; i < VectorWidth; i++) 399 IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); 400 401 isl_union_map *Schedule = getScheduleForAstNode(For); 402 assert(Schedule && "For statement annotation does not contain its schedule"); 403 404 IDToValue[IteratorID] = ValueLB; 405 406 switch (isl_ast_node_get_type(Body)) { 407 case isl_ast_node_user: 408 createUserVector(Body, IVS, isl_id_copy(IteratorID), 409 isl_union_map_copy(Schedule)); 410 break; 411 case isl_ast_node_block: { 412 isl_ast_node_list *List = isl_ast_node_block_get_children(Body); 413 414 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) 415 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, 416 isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); 417 418 isl_ast_node_free(Body); 419 isl_ast_node_list_free(List); 420 break; 421 } 422 default: 423 isl_ast_node_dump(Body); 424 llvm_unreachable("Unhandled isl_ast_node in vectorizer"); 425 } 426 427 IDToValue.erase(IDToValue.find(IteratorID)); 428 isl_id_free(IteratorID); 429 isl_union_map_free(Schedule); 430 431 isl_ast_node_free(For); 432 isl_ast_expr_free(Iterator); 433 } 434 435 void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For, 436 bool KnownParallel) { 437 isl_ast_node *Body; 438 isl_ast_expr *Init, *Inc, *Iterator, *UB; 439 isl_id *IteratorID; 440 Value *ValueLB, *ValueUB, *ValueInc; 441 BasicBlock *ExitBlock; 442 Value *IV; 443 CmpInst::Predicate Predicate; 444 bool Parallel; 445 446 Parallel = KnownParallel || (IslAstInfo::isParallel(For) && 447 !IslAstInfo::isReductionParallel(For)); 448 449 Body = isl_ast_node_for_get_body(For); 450 451 // isl_ast_node_for_is_degenerate(For) 452 // 453 // TODO: For degenerated loops we could generate a plain assignment. 454 // However, for now we just reuse the logic for normal loops, which will 455 // create a loop with a single iteration. 456 457 Init = isl_ast_node_for_get_init(For); 458 Inc = isl_ast_node_for_get_inc(For); 459 Iterator = isl_ast_node_for_get_iterator(For); 460 IteratorID = isl_ast_expr_get_id(Iterator); 461 UB = getUpperBound(For, Predicate); 462 463 ValueLB = ExprBuilder.create(Init); 464 ValueUB = ExprBuilder.create(UB); 465 ValueInc = ExprBuilder.create(Inc); 466 467 ExprBuilder.unifyTypes(ValueLB, ValueUB, ValueInc); 468 469 // If we can show that LB <Predicate> UB holds at least once, we can 470 // omit the GuardBB in front of the loop. 471 bool UseGuardBB = 472 !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB)); 473 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock, 474 Predicate, &Annotator, Parallel, UseGuardBB); 475 IDToValue[IteratorID] = IV; 476 477 create(Body); 478 479 Annotator.popLoop(Parallel); 480 481 IDToValue.erase(IDToValue.find(IteratorID)); 482 483 Builder.SetInsertPoint(&ExitBlock->front()); 484 485 isl_ast_node_free(For); 486 isl_ast_expr_free(Iterator); 487 isl_id_free(IteratorID); 488 } 489 490 /// @brief Remove the BBs contained in a (sub)function from the dominator tree. 491 /// 492 /// This function removes the basic blocks that are part of a subfunction from 493 /// the dominator tree. Specifically, when generating code it may happen that at 494 /// some point the code generation continues in a new sub-function (e.g., when 495 /// generating OpenMP code). The basic blocks that are created in this 496 /// sub-function are then still part of the dominator tree of the original 497 /// function, such that the dominator tree reaches over function boundaries. 498 /// This is not only incorrect, but also causes crashes. This function now 499 /// removes from the dominator tree all basic blocks that are dominated (and 500 /// consequently reachable) from the entry block of this (sub)function. 501 /// 502 /// FIXME: A LLVM (function or region) pass should not touch anything outside of 503 /// the function/region it runs on. Hence, the pure need for this function shows 504 /// that we do not comply to this rule. At the moment, this does not cause any 505 /// issues, but we should be aware that such issues may appear. Unfortunately 506 /// the current LLVM pass infrastructure does not allow to make Polly a module 507 /// or call-graph pass to solve this issue, as such a pass would not have access 508 /// to the per-function analyses passes needed by Polly. A future pass manager 509 /// infrastructure is supposed to enable such kind of access possibly allowing 510 /// us to create a cleaner solution here. 511 /// 512 /// FIXME: Instead of adding the dominance information and then dropping it 513 /// later on, we should try to just not add it in the first place. This requires 514 /// some careful testing to make sure this does not break in interaction with 515 /// the SCEVBuilder and SplitBlock which may rely on the dominator tree or 516 /// which may try to update it. 517 /// 518 /// @param F The function which contains the BBs to removed. 519 /// @param DT The dominator tree from which to remove the BBs. 520 static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) { 521 DomTreeNode *N = DT.getNode(&F->getEntryBlock()); 522 std::vector<BasicBlock *> Nodes; 523 524 // We can only remove an element from the dominator tree, if all its children 525 // have been removed. To ensure this we obtain the list of nodes to remove 526 // using a post-order tree traversal. 527 for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I) 528 Nodes.push_back(I->getBlock()); 529 530 for (BasicBlock *BB : Nodes) 531 DT.eraseNode(BB); 532 } 533 534 void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) { 535 isl_ast_node *Body; 536 isl_ast_expr *Init, *Inc, *Iterator, *UB; 537 isl_id *IteratorID; 538 Value *ValueLB, *ValueUB, *ValueInc; 539 Value *IV; 540 CmpInst::Predicate Predicate; 541 542 // The preamble of parallel code interacts different than normal code with 543 // e.g., scalar initialization. Therefore, we ensure the parallel code is 544 // separated from the last basic block. 545 BasicBlock *ParBB = SplitBlock(Builder.GetInsertBlock(), 546 &*Builder.GetInsertPoint(), &DT, &LI); 547 ParBB->setName("polly.parallel.for"); 548 Builder.SetInsertPoint(&ParBB->front()); 549 550 Body = isl_ast_node_for_get_body(For); 551 Init = isl_ast_node_for_get_init(For); 552 Inc = isl_ast_node_for_get_inc(For); 553 Iterator = isl_ast_node_for_get_iterator(For); 554 IteratorID = isl_ast_expr_get_id(Iterator); 555 UB = getUpperBound(For, Predicate); 556 557 ValueLB = ExprBuilder.create(Init); 558 ValueUB = ExprBuilder.create(UB); 559 ValueInc = ExprBuilder.create(Inc); 560 561 // OpenMP always uses SLE. In case the isl generated AST uses a SLT 562 // expression, we need to adjust the loop blound by one. 563 if (Predicate == CmpInst::ICMP_SLT) 564 ValueUB = Builder.CreateAdd( 565 ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType())); 566 567 ExprBuilder.unifyTypes(ValueLB, ValueUB, ValueInc); 568 569 BasicBlock::iterator LoopBody; 570 571 SetVector<Value *> SubtreeValues; 572 SetVector<const Loop *> Loops; 573 574 getReferencesInSubtree(For, SubtreeValues, Loops); 575 576 // Create for all loops we depend on values that contain the current loop 577 // iteration. These values are necessary to generate code for SCEVs that 578 // depend on such loops. As a result we need to pass them to the subfunction. 579 for (const Loop *L : Loops) { 580 const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), 581 SE.getUnknown(Builder.getInt64(1)), 582 L, SCEV::FlagAnyWrap); 583 Value *V = generateSCEV(OuterLIV); 584 OutsideLoopIterations[L] = SE.getUnknown(V); 585 SubtreeValues.insert(V); 586 } 587 588 ValueMapT NewValues; 589 ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL); 590 591 IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc, 592 SubtreeValues, NewValues, &LoopBody); 593 BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); 594 Builder.SetInsertPoint(&*LoopBody); 595 596 // Remember the parallel subfunction 597 ParallelSubfunctions.push_back(LoopBody->getFunction()); 598 599 // Save the current values. 600 auto ValueMapCopy = ValueMap; 601 IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue; 602 603 updateValues(NewValues); 604 IDToValue[IteratorID] = IV; 605 606 ValueMapT NewValuesReverse; 607 608 for (auto P : NewValues) 609 NewValuesReverse[P.second] = P.first; 610 611 Annotator.addAlternativeAliasBases(NewValuesReverse); 612 613 create(Body); 614 615 Annotator.resetAlternativeAliasBases(); 616 // Restore the original values. 617 ValueMap = ValueMapCopy; 618 IDToValue = IDToValueCopy; 619 620 Builder.SetInsertPoint(&*AfterLoop); 621 removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT); 622 623 for (const Loop *L : Loops) 624 OutsideLoopIterations.erase(L); 625 626 isl_ast_node_free(For); 627 isl_ast_expr_free(Iterator); 628 isl_id_free(IteratorID); 629 } 630 631 void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { 632 bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY; 633 634 if (Vector && IslAstInfo::isInnermostParallel(For) && 635 !IslAstInfo::isReductionParallel(For)) { 636 int VectorWidth = getNumberOfIterations(For); 637 if (1 < VectorWidth && VectorWidth <= 16) { 638 createForVector(For, VectorWidth); 639 return; 640 } 641 } 642 643 if (IslAstInfo::isExecutedInParallel(For)) { 644 createForParallel(For); 645 return; 646 } 647 createForSequential(For, false); 648 } 649 650 void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { 651 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); 652 653 Function *F = Builder.GetInsertBlock()->getParent(); 654 LLVMContext &Context = F->getContext(); 655 656 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), 657 &*Builder.GetInsertPoint(), &DT, &LI); 658 CondBB->setName("polly.cond"); 659 BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI); 660 MergeBB->setName("polly.merge"); 661 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); 662 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); 663 664 DT.addNewBlock(ThenBB, CondBB); 665 DT.addNewBlock(ElseBB, CondBB); 666 DT.changeImmediateDominator(MergeBB, CondBB); 667 668 Loop *L = LI.getLoopFor(CondBB); 669 if (L) { 670 L->addBasicBlockToLoop(ThenBB, LI); 671 L->addBasicBlockToLoop(ElseBB, LI); 672 } 673 674 CondBB->getTerminator()->eraseFromParent(); 675 676 Builder.SetInsertPoint(CondBB); 677 Value *Predicate = ExprBuilder.create(Cond); 678 Builder.CreateCondBr(Predicate, ThenBB, ElseBB); 679 Builder.SetInsertPoint(ThenBB); 680 Builder.CreateBr(MergeBB); 681 Builder.SetInsertPoint(ElseBB); 682 Builder.CreateBr(MergeBB); 683 Builder.SetInsertPoint(&ThenBB->front()); 684 685 create(isl_ast_node_if_get_then(If)); 686 687 Builder.SetInsertPoint(&ElseBB->front()); 688 689 if (isl_ast_node_if_has_else(If)) 690 create(isl_ast_node_if_get_else(If)); 691 692 Builder.SetInsertPoint(&MergeBB->front()); 693 694 isl_ast_node_free(If); 695 } 696 697 __isl_give isl_id_to_ast_expr * 698 IslNodeBuilder::createNewAccesses(ScopStmt *Stmt, 699 __isl_keep isl_ast_node *Node) { 700 isl_id_to_ast_expr *NewAccesses = 701 isl_id_to_ast_expr_alloc(Stmt->getParent()->getIslCtx(), 0); 702 703 auto *Build = IslAstInfo::getBuild(Node); 704 assert(Build && "Could not obtain isl_ast_build from user node"); 705 Stmt->setAstBuild(Build); 706 707 for (auto *MA : *Stmt) { 708 if (!MA->hasNewAccessRelation()) 709 continue; 710 711 auto Schedule = isl_ast_build_get_schedule(Build); 712 auto PWAccRel = MA->applyScheduleToAccessRelation(Schedule); 713 714 auto AccessExpr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); 715 NewAccesses = isl_id_to_ast_expr_set(NewAccesses, MA->getId(), AccessExpr); 716 } 717 718 return NewAccesses; 719 } 720 721 void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt, 722 LoopToScevMapT <S) { 723 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 724 "Expression of type 'op' expected"); 725 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && 726 "Opertation of type 'call' expected"); 727 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) { 728 isl_ast_expr *SubExpr; 729 Value *V; 730 731 SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1); 732 V = ExprBuilder.create(SubExpr); 733 ScalarEvolution *SE = Stmt->getParent()->getSE(); 734 LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V); 735 } 736 737 isl_ast_expr_free(Expr); 738 } 739 740 void IslNodeBuilder::createSubstitutionsVector( 741 __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, 742 std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS, 743 __isl_take isl_id *IteratorID) { 744 int i = 0; 745 746 Value *OldValue = IDToValue[IteratorID]; 747 for (Value *IV : IVS) { 748 IDToValue[IteratorID] = IV; 749 createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VLTS[i]); 750 i++; 751 } 752 753 IDToValue[IteratorID] = OldValue; 754 isl_id_free(IteratorID); 755 isl_ast_expr_free(Expr); 756 } 757 758 void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { 759 LoopToScevMapT LTS; 760 isl_id *Id; 761 ScopStmt *Stmt; 762 763 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); 764 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); 765 Id = isl_ast_expr_get_id(StmtExpr); 766 isl_ast_expr_free(StmtExpr); 767 768 LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end()); 769 770 Stmt = (ScopStmt *)isl_id_get_user(Id); 771 auto *NewAccesses = createNewAccesses(Stmt, User); 772 createSubstitutions(Expr, Stmt, LTS); 773 774 if (Stmt->isBlockStmt()) 775 BlockGen.copyStmt(*Stmt, LTS, NewAccesses); 776 else 777 RegionGen.copyStmt(*Stmt, LTS, NewAccesses); 778 779 isl_id_to_ast_expr_free(NewAccesses); 780 isl_ast_node_free(User); 781 isl_id_free(Id); 782 } 783 784 void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { 785 isl_ast_node_list *List = isl_ast_node_block_get_children(Block); 786 787 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) 788 create(isl_ast_node_list_get_ast_node(List, i)); 789 790 isl_ast_node_free(Block); 791 isl_ast_node_list_free(List); 792 } 793 794 void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { 795 switch (isl_ast_node_get_type(Node)) { 796 case isl_ast_node_error: 797 llvm_unreachable("code generation error"); 798 case isl_ast_node_mark: 799 createMark(Node); 800 return; 801 case isl_ast_node_for: 802 createFor(Node); 803 return; 804 case isl_ast_node_if: 805 createIf(Node); 806 return; 807 case isl_ast_node_user: 808 createUser(Node); 809 return; 810 case isl_ast_node_block: 811 createBlock(Node); 812 return; 813 } 814 815 llvm_unreachable("Unknown isl_ast_node type"); 816 } 817 818 bool IslNodeBuilder::materializeValue(isl_id *Id) { 819 // If the Id is already mapped, skip it. 820 if (!IDToValue.count(Id)) { 821 auto *ParamSCEV = (const SCEV *)isl_id_get_user(Id); 822 Value *V = nullptr; 823 824 // Parameters could refere to invariant loads that need to be 825 // preloaded before we can generate code for the parameter. Thus, 826 // check if any value refered to in ParamSCEV is an invariant load 827 // and if so make sure its equivalence class is preloaded. 828 SetVector<Value *> Values; 829 findValues(ParamSCEV, SE, Values); 830 for (auto *Val : Values) { 831 832 // Check if the value is an instruction in a dead block within the SCoP 833 // and if so do not code generate it. 834 if (auto *Inst = dyn_cast<Instruction>(Val)) { 835 if (S.contains(Inst)) { 836 bool IsDead = true; 837 838 // Check for "undef" loads first, then if there is a statement for 839 // the parent of Inst and lastly if the parent of Inst has an empty 840 // domain. In the first and last case the instruction is dead but if 841 // there is a statement or the domain is not empty Inst is not dead. 842 auto MemInst = MemAccInst::dyn_cast(Inst); 843 auto Address = MemInst ? MemInst.getPointerOperand() : nullptr; 844 if (Address && 845 SE.getUnknown(UndefValue::get(Address->getType())) == 846 SE.getPointerBase(SE.getSCEV(Address))) { 847 } else if (S.getStmtFor(Inst)) { 848 IsDead = false; 849 } else { 850 auto *Domain = S.getDomainConditions(Inst->getParent()); 851 IsDead = isl_set_is_empty(Domain); 852 isl_set_free(Domain); 853 } 854 855 if (IsDead) { 856 V = UndefValue::get(ParamSCEV->getType()); 857 break; 858 } 859 } 860 } 861 862 if (auto *IAClass = S.lookupInvariantEquivClass(Val)) { 863 864 // Check if this invariant access class is empty, hence if we never 865 // actually added a loads instruction to it. In that case it has no 866 // (meaningful) users and we should not try to code generate it. 867 if (std::get<1>(*IAClass).empty()) 868 V = UndefValue::get(ParamSCEV->getType()); 869 870 if (!preloadInvariantEquivClass(*IAClass)) { 871 isl_id_free(Id); 872 return false; 873 } 874 } 875 } 876 877 V = V ? V : generateSCEV(ParamSCEV); 878 IDToValue[Id] = V; 879 } 880 881 isl_id_free(Id); 882 return true; 883 } 884 885 bool IslNodeBuilder::materializeParameters(isl_set *Set, bool All) { 886 for (unsigned i = 0, e = isl_set_dim(Set, isl_dim_param); i < e; ++i) { 887 if (!All && !isl_set_involves_dims(Set, isl_dim_param, i, 1)) 888 continue; 889 isl_id *Id = isl_set_get_dim_id(Set, isl_dim_param, i); 890 if (!materializeValue(Id)) 891 return false; 892 } 893 return true; 894 } 895 896 /// @brief Add the number of dimensions in @p BS to @p U. 897 static isl_stat countTotalDims(isl_basic_set *BS, void *U) { 898 unsigned *NumTotalDim = static_cast<unsigned *>(U); 899 *NumTotalDim += isl_basic_set_total_dim(BS); 900 isl_basic_set_free(BS); 901 return isl_stat_ok; 902 } 903 904 Value *IslNodeBuilder::preloadUnconditionally(isl_set *AccessRange, 905 isl_ast_build *Build, 906 Instruction *AccInst) { 907 908 // TODO: This check could be performed in the ScopInfo already. 909 unsigned NumTotalDim = 0; 910 isl_set_foreach_basic_set(AccessRange, countTotalDims, &NumTotalDim); 911 if (NumTotalDim > MaxDimensionsInAccessRange) { 912 isl_set_free(AccessRange); 913 return nullptr; 914 } 915 916 isl_pw_multi_aff *PWAccRel = isl_pw_multi_aff_from_set(AccessRange); 917 isl_ast_expr *Access = 918 isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); 919 auto *Address = isl_ast_expr_address_of(Access); 920 auto *AddressValue = ExprBuilder.create(Address); 921 Value *PreloadVal; 922 923 // Correct the type as the SAI might have a different type than the user 924 // expects, especially if the base pointer is a struct. 925 Type *Ty = AccInst->getType(); 926 927 auto *Ptr = AddressValue; 928 auto Name = Ptr->getName(); 929 Ptr = Builder.CreatePointerCast(Ptr, Ty->getPointerTo(), Name + ".cast"); 930 PreloadVal = Builder.CreateLoad(Ptr, Name + ".load"); 931 if (LoadInst *PreloadInst = dyn_cast<LoadInst>(PreloadVal)) 932 PreloadInst->setAlignment(dyn_cast<LoadInst>(AccInst)->getAlignment()); 933 934 // TODO: This is only a hot fix for SCoP sequences that use the same load 935 // instruction contained and hoisted by one of the SCoPs. 936 if (SE.isSCEVable(Ty)) 937 SE.forgetValue(AccInst); 938 939 return PreloadVal; 940 } 941 942 Value *IslNodeBuilder::preloadInvariantLoad(const MemoryAccess &MA, 943 isl_set *Domain) { 944 945 isl_set *AccessRange = isl_map_range(MA.getAddressFunction()); 946 AccessRange = isl_set_gist_params(AccessRange, S.getContext()); 947 948 if (!materializeParameters(AccessRange, false)) { 949 isl_set_free(AccessRange); 950 isl_set_free(Domain); 951 return nullptr; 952 } 953 954 auto *Build = isl_ast_build_from_context(isl_set_universe(S.getParamSpace())); 955 isl_set *Universe = isl_set_universe(isl_set_get_space(Domain)); 956 bool AlwaysExecuted = isl_set_is_equal(Domain, Universe); 957 isl_set_free(Universe); 958 959 Instruction *AccInst = MA.getAccessInstruction(); 960 Type *AccInstTy = AccInst->getType(); 961 962 Value *PreloadVal = nullptr; 963 if (AlwaysExecuted) { 964 PreloadVal = preloadUnconditionally(AccessRange, Build, AccInst); 965 isl_ast_build_free(Build); 966 isl_set_free(Domain); 967 return PreloadVal; 968 } 969 970 if (!materializeParameters(Domain, false)) { 971 isl_ast_build_free(Build); 972 isl_set_free(AccessRange); 973 isl_set_free(Domain); 974 return nullptr; 975 } 976 977 isl_ast_expr *DomainCond = isl_ast_build_expr_from_set(Build, Domain); 978 Domain = nullptr; 979 980 ExprBuilder.setTrackOverflow(true); 981 Value *Cond = ExprBuilder.create(DomainCond); 982 Value *OverflowHappened = Builder.CreateNot(ExprBuilder.getOverflowState(), 983 "polly.preload.cond.overflown"); 984 Cond = Builder.CreateAnd(Cond, OverflowHappened, "polly.preload.cond.result"); 985 ExprBuilder.setTrackOverflow(false); 986 987 if (!Cond->getType()->isIntegerTy(1)) 988 Cond = Builder.CreateIsNotNull(Cond); 989 990 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), 991 &*Builder.GetInsertPoint(), &DT, &LI); 992 CondBB->setName("polly.preload.cond"); 993 994 BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI); 995 MergeBB->setName("polly.preload.merge"); 996 997 Function *F = Builder.GetInsertBlock()->getParent(); 998 LLVMContext &Context = F->getContext(); 999 BasicBlock *ExecBB = BasicBlock::Create(Context, "polly.preload.exec", F); 1000 1001 DT.addNewBlock(ExecBB, CondBB); 1002 if (Loop *L = LI.getLoopFor(CondBB)) 1003 L->addBasicBlockToLoop(ExecBB, LI); 1004 1005 auto *CondBBTerminator = CondBB->getTerminator(); 1006 Builder.SetInsertPoint(CondBBTerminator); 1007 Builder.CreateCondBr(Cond, ExecBB, MergeBB); 1008 CondBBTerminator->eraseFromParent(); 1009 1010 Builder.SetInsertPoint(ExecBB); 1011 Builder.CreateBr(MergeBB); 1012 1013 Builder.SetInsertPoint(ExecBB->getTerminator()); 1014 Value *PreAccInst = preloadUnconditionally(AccessRange, Build, AccInst); 1015 Builder.SetInsertPoint(MergeBB->getTerminator()); 1016 auto *MergePHI = Builder.CreatePHI( 1017 AccInstTy, 2, "polly.preload." + AccInst->getName() + ".merge"); 1018 PreloadVal = MergePHI; 1019 1020 if (!PreAccInst) { 1021 PreloadVal = nullptr; 1022 PreAccInst = UndefValue::get(AccInstTy); 1023 } 1024 1025 MergePHI->addIncoming(PreAccInst, ExecBB); 1026 MergePHI->addIncoming(Constant::getNullValue(AccInstTy), CondBB); 1027 1028 isl_ast_build_free(Build); 1029 return PreloadVal; 1030 } 1031 1032 bool IslNodeBuilder::preloadInvariantEquivClass( 1033 InvariantEquivClassTy &IAClass) { 1034 // For an equivalence class of invariant loads we pre-load the representing 1035 // element with the unified execution context. However, we have to map all 1036 // elements of the class to the one preloaded load as they are referenced 1037 // during the code generation and therefor need to be mapped. 1038 const MemoryAccessList &MAs = std::get<1>(IAClass); 1039 if (MAs.empty()) 1040 return true; 1041 1042 MemoryAccess *MA = MAs.front(); 1043 assert(MA->isArrayKind() && MA->isRead()); 1044 1045 // If the access function was already mapped, the preload of this equivalence 1046 // class was triggered earlier already and doesn't need to be done again. 1047 if (ValueMap.count(MA->getAccessInstruction())) 1048 return true; 1049 1050 // Check for recurrsion which can be caused by additional constraints, e.g., 1051 // non-finitie loop contraints. In such a case we have to bail out and insert 1052 // a "false" runtime check that will cause the original code to be executed. 1053 auto PtrId = std::make_pair(std::get<0>(IAClass), std::get<3>(IAClass)); 1054 if (!PreloadedPtrs.insert(PtrId).second) 1055 return false; 1056 1057 // The exectution context of the IAClass. 1058 isl_set *&ExecutionCtx = std::get<2>(IAClass); 1059 1060 // If the base pointer of this class is dependent on another one we have to 1061 // make sure it was preloaded already. 1062 auto *SAI = MA->getScopArrayInfo(); 1063 if (auto *BaseIAClass = S.lookupInvariantEquivClass(SAI->getBasePtr())) { 1064 if (!preloadInvariantEquivClass(*BaseIAClass)) 1065 return false; 1066 1067 // After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx and 1068 // we need to refine the ExecutionCtx. 1069 isl_set *BaseExecutionCtx = isl_set_copy(std::get<2>(*BaseIAClass)); 1070 ExecutionCtx = isl_set_intersect(ExecutionCtx, BaseExecutionCtx); 1071 } 1072 1073 Instruction *AccInst = MA->getAccessInstruction(); 1074 Type *AccInstTy = AccInst->getType(); 1075 1076 Value *PreloadVal = preloadInvariantLoad(*MA, isl_set_copy(ExecutionCtx)); 1077 if (!PreloadVal) 1078 return false; 1079 1080 for (const MemoryAccess *MA : MAs) { 1081 Instruction *MAAccInst = MA->getAccessInstruction(); 1082 assert(PreloadVal->getType() == MAAccInst->getType()); 1083 ValueMap[MAAccInst] = PreloadVal; 1084 } 1085 1086 if (SE.isSCEVable(AccInstTy)) { 1087 isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst)); 1088 if (ParamId) 1089 IDToValue[ParamId] = PreloadVal; 1090 isl_id_free(ParamId); 1091 } 1092 1093 BasicBlock *EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); 1094 auto *Alloca = new AllocaInst(AccInstTy, AccInst->getName() + ".preload.s2a"); 1095 Alloca->insertBefore(&*EntryBB->getFirstInsertionPt()); 1096 Builder.CreateStore(PreloadVal, Alloca); 1097 1098 for (auto *DerivedSAI : SAI->getDerivedSAIs()) { 1099 Value *BasePtr = DerivedSAI->getBasePtr(); 1100 1101 for (const MemoryAccess *MA : MAs) { 1102 // As the derived SAI information is quite coarse, any load from the 1103 // current SAI could be the base pointer of the derived SAI, however we 1104 // should only change the base pointer of the derived SAI if we actually 1105 // preloaded it. 1106 if (BasePtr == MA->getBaseAddr()) { 1107 assert(BasePtr->getType() == PreloadVal->getType()); 1108 DerivedSAI->setBasePtr(PreloadVal); 1109 } 1110 1111 // For scalar derived SAIs we remap the alloca used for the derived value. 1112 if (BasePtr == MA->getAccessInstruction()) { 1113 if (DerivedSAI->isPHIKind()) 1114 PHIOpMap[BasePtr] = Alloca; 1115 else 1116 ScalarMap[BasePtr] = Alloca; 1117 } 1118 } 1119 } 1120 1121 for (const MemoryAccess *MA : MAs) { 1122 1123 Instruction *MAAccInst = MA->getAccessInstruction(); 1124 // Use the escape system to get the correct value to users outside the SCoP. 1125 BlockGenerator::EscapeUserVectorTy EscapeUsers; 1126 for (auto *U : MAAccInst->users()) 1127 if (Instruction *UI = dyn_cast<Instruction>(U)) 1128 if (!S.contains(UI)) 1129 EscapeUsers.push_back(UI); 1130 1131 if (EscapeUsers.empty()) 1132 continue; 1133 1134 EscapeMap[MA->getAccessInstruction()] = 1135 std::make_pair(Alloca, std::move(EscapeUsers)); 1136 } 1137 1138 return true; 1139 } 1140 1141 bool IslNodeBuilder::preloadInvariantLoads() { 1142 1143 auto &InvariantEquivClasses = S.getInvariantAccesses(); 1144 if (InvariantEquivClasses.empty()) 1145 return true; 1146 1147 BasicBlock *PreLoadBB = SplitBlock(Builder.GetInsertBlock(), 1148 &*Builder.GetInsertPoint(), &DT, &LI); 1149 PreLoadBB->setName("polly.preload.begin"); 1150 Builder.SetInsertPoint(&PreLoadBB->front()); 1151 1152 for (auto &IAClass : InvariantEquivClasses) 1153 if (!preloadInvariantEquivClass(IAClass)) 1154 return false; 1155 1156 return true; 1157 } 1158 1159 void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { 1160 1161 // Materialize values for the parameters of the SCoP. 1162 materializeParameters(Context, /* all */ true); 1163 1164 // Generate values for the current loop iteration for all surrounding loops. 1165 // 1166 // We may also reference loops outside of the scop which do not contain the 1167 // scop itself, but as the number of such scops may be arbitrarily large we do 1168 // not generate code for them here, but only at the point of code generation 1169 // where these values are needed. 1170 Loop *L = LI.getLoopFor(S.getEntry()); 1171 1172 while (L != nullptr && S.contains(L)) 1173 L = L->getParentLoop(); 1174 1175 while (L != nullptr) { 1176 const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), 1177 SE.getUnknown(Builder.getInt64(1)), 1178 L, SCEV::FlagAnyWrap); 1179 Value *V = generateSCEV(OuterLIV); 1180 OutsideLoopIterations[L] = SE.getUnknown(V); 1181 L = L->getParentLoop(); 1182 } 1183 1184 isl_set_free(Context); 1185 } 1186 1187 Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) { 1188 Instruction *InsertLocation = &*--(Builder.GetInsertBlock()->end()); 1189 return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(), 1190 InsertLocation, &ValueMap); 1191 } 1192