1 //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===// 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 implements the BlockGenerator and VectorBlockGenerator classes, 11 // which generate sequential code and vectorized code for a polyhedral 12 // statement, respectively. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "polly/CodeGen/BlockGenerators.h" 17 #include "polly/CodeGen/CodeGeneration.h" 18 #include "polly/CodeGen/IslExprBuilder.h" 19 #include "polly/CodeGen/RuntimeDebugBuilder.h" 20 #include "polly/Options.h" 21 #include "polly/ScopInfo.h" 22 #include "polly/Support/GICHelper.h" 23 #include "polly/Support/SCEVValidator.h" 24 #include "polly/Support/ScopHelper.h" 25 #include "llvm/Analysis/LoopInfo.h" 26 #include "llvm/Analysis/RegionInfo.h" 27 #include "llvm/Analysis/ScalarEvolution.h" 28 #include "llvm/IR/IntrinsicInst.h" 29 #include "llvm/IR/Module.h" 30 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 31 #include "llvm/Transforms/Utils/Local.h" 32 #include "isl/aff.h" 33 #include "isl/ast.h" 34 #include "isl/ast_build.h" 35 #include "isl/set.h" 36 #include <deque> 37 38 using namespace llvm; 39 using namespace polly; 40 41 static cl::opt<bool> Aligned("enable-polly-aligned", 42 cl::desc("Assumed aligned memory accesses."), 43 cl::Hidden, cl::init(false), cl::ZeroOrMore, 44 cl::cat(PollyCategory)); 45 46 static cl::opt<bool> DebugPrinting( 47 "polly-codegen-add-debug-printing", 48 cl::desc("Add printf calls that show the values loaded/stored."), 49 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 50 51 BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI, 52 ScalarEvolution &SE, DominatorTree &DT, 53 ScalarAllocaMapTy &ScalarMap, 54 ScalarAllocaMapTy &PHIOpMap, 55 EscapeUsersAllocaMapTy &EscapeMap, 56 ValueMapT &GlobalMap, 57 IslExprBuilder *ExprBuilder) 58 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT), 59 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap), 60 EscapeMap(EscapeMap), GlobalMap(GlobalMap) {} 61 62 Value *BlockGenerator::trySynthesizeNewValue(ScopStmt &Stmt, Value *Old, 63 ValueMapT &BBMap, 64 LoopToScevMapT <S, 65 Loop *L) const { 66 if (!SE.isSCEVable(Old->getType())) 67 return nullptr; 68 69 const SCEV *Scev = SE.getSCEVAtScope(Old, L); 70 if (!Scev) 71 return nullptr; 72 73 if (isa<SCEVCouldNotCompute>(Scev)) 74 return nullptr; 75 76 const SCEV *NewScev = apply(Scev, LTS, SE); 77 ValueMapT VTV; 78 VTV.insert(BBMap.begin(), BBMap.end()); 79 VTV.insert(GlobalMap.begin(), GlobalMap.end()); 80 81 Scop &S = *Stmt.getParent(); 82 const DataLayout &DL = 83 S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); 84 auto IP = Builder.GetInsertPoint(); 85 86 assert(IP != Builder.GetInsertBlock()->end() && 87 "Only instructions can be insert points for SCEVExpander"); 88 Value *Expanded = 89 expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), &*IP, &VTV); 90 91 BBMap[Old] = Expanded; 92 return Expanded; 93 } 94 95 Value *BlockGenerator::getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap, 96 LoopToScevMapT <S, Loop *L) const { 97 // Constants that do not reference any named value can always remain 98 // unchanged. Handle them early to avoid expensive map lookups. We do not take 99 // the fast-path for external constants which are referenced through globals 100 // as these may need to be rewritten when distributing code accross different 101 // LLVM modules. 102 if (isa<Constant>(Old) && !isa<GlobalValue>(Old)) 103 return Old; 104 105 // Inline asm is like a constant to us. 106 if (isa<InlineAsm>(Old)) 107 return Old; 108 109 if (Value *New = GlobalMap.lookup(Old)) { 110 if (Value *NewRemapped = GlobalMap.lookup(New)) 111 New = NewRemapped; 112 if (Old->getType()->getScalarSizeInBits() < 113 New->getType()->getScalarSizeInBits()) 114 New = Builder.CreateTruncOrBitCast(New, Old->getType()); 115 116 return New; 117 } 118 119 if (Value *New = BBMap.lookup(Old)) 120 return New; 121 122 if (Value *New = trySynthesizeNewValue(Stmt, Old, BBMap, LTS, L)) 123 return New; 124 125 // A scop-constant value defined by a global or a function parameter. 126 if (isa<GlobalValue>(Old) || isa<Argument>(Old)) 127 return Old; 128 129 // A scop-constant value defined by an instruction executed outside the scop. 130 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) 131 if (!Stmt.getParent()->getRegion().contains(Inst->getParent())) 132 return Old; 133 134 // The scalar dependence is neither available nor SCEVCodegenable. 135 llvm_unreachable("Unexpected scalar dependence in region!"); 136 return nullptr; 137 } 138 139 void BlockGenerator::copyInstScalar(ScopStmt &Stmt, Instruction *Inst, 140 ValueMapT &BBMap, LoopToScevMapT <S) { 141 // We do not generate debug intrinsics as we did not investigate how to 142 // copy them correctly. At the current state, they just crash the code 143 // generation as the meta-data operands are not correctly copied. 144 if (isa<DbgInfoIntrinsic>(Inst)) 145 return; 146 147 Instruction *NewInst = Inst->clone(); 148 149 // Replace old operands with the new ones. 150 for (Value *OldOperand : Inst->operands()) { 151 Value *NewOperand = 152 getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForStmt(Stmt)); 153 154 if (!NewOperand) { 155 assert(!isa<StoreInst>(NewInst) && 156 "Store instructions are always needed!"); 157 delete NewInst; 158 return; 159 } 160 161 NewInst->replaceUsesOfWith(OldOperand, NewOperand); 162 } 163 164 Builder.Insert(NewInst); 165 BBMap[Inst] = NewInst; 166 167 if (!NewInst->getType()->isVoidTy()) 168 NewInst->setName("p_" + Inst->getName()); 169 } 170 171 Value * 172 BlockGenerator::generateLocationAccessed(ScopStmt &Stmt, MemAccInst Inst, 173 ValueMapT &BBMap, LoopToScevMapT <S, 174 isl_id_to_ast_expr *NewAccesses) { 175 const MemoryAccess &MA = Stmt.getArrayAccessFor(Inst); 176 177 isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId()); 178 179 if (AccessExpr) { 180 AccessExpr = isl_ast_expr_address_of(AccessExpr); 181 auto Address = ExprBuilder->create(AccessExpr); 182 183 // Cast the address of this memory access to a pointer type that has the 184 // same element type as the original access, but uses the address space of 185 // the newly generated pointer. 186 auto OldPtrTy = MA.getAccessValue()->getType()->getPointerTo(); 187 auto NewPtrTy = Address->getType(); 188 OldPtrTy = PointerType::get(OldPtrTy->getElementType(), 189 NewPtrTy->getPointerAddressSpace()); 190 191 if (OldPtrTy != NewPtrTy) 192 Address = Builder.CreateBitOrPointerCast(Address, OldPtrTy); 193 return Address; 194 } 195 196 return getNewValue(Stmt, Inst.getPointerOperand(), BBMap, LTS, 197 getLoopForStmt(Stmt)); 198 } 199 200 Loop *BlockGenerator::getLoopForStmt(const ScopStmt &Stmt) const { 201 auto *StmtBB = Stmt.getEntryBlock(); 202 return LI.getLoopFor(StmtBB); 203 } 204 205 Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, LoadInst *Load, 206 ValueMapT &BBMap, LoopToScevMapT <S, 207 isl_id_to_ast_expr *NewAccesses) { 208 if (Value *PreloadLoad = GlobalMap.lookup(Load)) 209 return PreloadLoad; 210 211 Value *NewPointer = 212 generateLocationAccessed(Stmt, Load, BBMap, LTS, NewAccesses); 213 Value *ScalarLoad = Builder.CreateAlignedLoad( 214 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); 215 216 if (DebugPrinting) 217 RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer, 218 ": ", ScalarLoad, "\n"); 219 220 return ScalarLoad; 221 } 222 223 void BlockGenerator::generateScalarStore(ScopStmt &Stmt, StoreInst *Store, 224 ValueMapT &BBMap, LoopToScevMapT <S, 225 isl_id_to_ast_expr *NewAccesses) { 226 Value *NewPointer = 227 generateLocationAccessed(Stmt, Store, BBMap, LTS, NewAccesses); 228 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, LTS, 229 getLoopForStmt(Stmt)); 230 231 if (DebugPrinting) 232 RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to ", NewPointer, 233 ": ", ValueOperand, "\n"); 234 235 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment()); 236 } 237 238 bool BlockGenerator::canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst) { 239 Loop *L = getLoopForStmt(Stmt); 240 return (Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) && 241 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion(), L); 242 } 243 244 void BlockGenerator::copyInstruction(ScopStmt &Stmt, Instruction *Inst, 245 ValueMapT &BBMap, LoopToScevMapT <S, 246 isl_id_to_ast_expr *NewAccesses) { 247 // Terminator instructions control the control flow. They are explicitly 248 // expressed in the clast and do not need to be copied. 249 if (Inst->isTerminator()) 250 return; 251 252 // Synthesizable statements will be generated on-demand. 253 if (canSyntheziseInStmt(Stmt, Inst)) 254 return; 255 256 if (auto *Load = dyn_cast<LoadInst>(Inst)) { 257 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, LTS, NewAccesses); 258 // Compute NewLoad before its insertion in BBMap to make the insertion 259 // deterministic. 260 BBMap[Load] = NewLoad; 261 return; 262 } 263 264 if (auto *Store = dyn_cast<StoreInst>(Inst)) { 265 generateScalarStore(Stmt, Store, BBMap, LTS, NewAccesses); 266 return; 267 } 268 269 if (auto *PHI = dyn_cast<PHINode>(Inst)) { 270 copyPHIInstruction(Stmt, PHI, BBMap, LTS); 271 return; 272 } 273 274 // Skip some special intrinsics for which we do not adjust the semantics to 275 // the new schedule. All others are handled like every other instruction. 276 if (isIgnoredIntrinsic(Inst)) 277 return; 278 279 copyInstScalar(Stmt, Inst, BBMap, LTS); 280 } 281 282 void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, 283 isl_id_to_ast_expr *NewAccesses) { 284 assert(Stmt.isBlockStmt() && 285 "Only block statements can be copied by the block generator"); 286 287 ValueMapT BBMap; 288 289 BasicBlock *BB = Stmt.getBasicBlock(); 290 copyBB(Stmt, BB, BBMap, LTS, NewAccesses); 291 } 292 293 BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { 294 BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), 295 &*Builder.GetInsertPoint(), &DT, &LI); 296 CopyBB->setName("polly.stmt." + BB->getName()); 297 return CopyBB; 298 } 299 300 BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, 301 ValueMapT &BBMap, LoopToScevMapT <S, 302 isl_id_to_ast_expr *NewAccesses) { 303 BasicBlock *CopyBB = splitBB(BB); 304 Builder.SetInsertPoint(&CopyBB->front()); 305 generateScalarLoads(Stmt, BBMap); 306 307 copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses); 308 309 // After a basic block was copied store all scalars that escape this block in 310 // their alloca. 311 generateScalarStores(Stmt, LTS, BBMap); 312 return CopyBB; 313 } 314 315 void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB, 316 ValueMapT &BBMap, LoopToScevMapT <S, 317 isl_id_to_ast_expr *NewAccesses) { 318 EntryBB = &CopyBB->getParent()->getEntryBlock(); 319 320 for (Instruction &Inst : *BB) 321 copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses); 322 } 323 324 Value *BlockGenerator::getOrCreateAlloca(Value *ScalarBase, 325 ScalarAllocaMapTy &Map, 326 const char *NameExt) { 327 // If no alloca was found create one and insert it in the entry block. 328 if (!Map.count(ScalarBase)) { 329 auto *Ty = ScalarBase->getType(); 330 auto NewAddr = new AllocaInst(Ty, ScalarBase->getName() + NameExt); 331 EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); 332 NewAddr->insertBefore(&*EntryBB->getFirstInsertionPt()); 333 Map[ScalarBase] = NewAddr; 334 } 335 336 auto Addr = Map[ScalarBase]; 337 338 if (auto NewAddr = GlobalMap.lookup(Addr)) 339 return NewAddr; 340 341 return Addr; 342 } 343 344 Value *BlockGenerator::getOrCreateAlloca(const MemoryAccess &Access) { 345 if (Access.isPHIKind()) 346 return getOrCreatePHIAlloca(Access.getBaseAddr()); 347 else 348 return getOrCreateScalarAlloca(Access.getBaseAddr()); 349 } 350 351 Value *BlockGenerator::getOrCreateAlloca(const ScopArrayInfo *Array) { 352 if (Array->isPHIKind()) 353 return getOrCreatePHIAlloca(Array->getBasePtr()); 354 else 355 return getOrCreateScalarAlloca(Array->getBasePtr()); 356 } 357 358 Value *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) { 359 return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a"); 360 } 361 362 Value *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) { 363 return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops"); 364 } 365 366 void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst, 367 Value *Address) { 368 // If there are escape users we get the alloca for this instruction and put it 369 // in the EscapeMap for later finalization. Lastly, if the instruction was 370 // copied multiple times we already did this and can exit. 371 if (EscapeMap.count(Inst)) 372 return; 373 374 EscapeUserVectorTy EscapeUsers; 375 for (User *U : Inst->users()) { 376 377 // Non-instruction user will never escape. 378 Instruction *UI = dyn_cast<Instruction>(U); 379 if (!UI) 380 continue; 381 382 if (R.contains(UI)) 383 continue; 384 385 EscapeUsers.push_back(UI); 386 } 387 388 // Exit if no escape uses were found. 389 if (EscapeUsers.empty()) 390 return; 391 392 // Get or create an escape alloca for this instruction. 393 auto *ScalarAddr = Address ? Address : getOrCreateScalarAlloca(Inst); 394 395 // Remember that this instruction has escape uses and the escape alloca. 396 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers)); 397 } 398 399 void BlockGenerator::generateScalarLoads(ScopStmt &Stmt, ValueMapT &BBMap) { 400 for (MemoryAccess *MA : Stmt) { 401 if (MA->isArrayKind() || MA->isWrite()) 402 continue; 403 404 auto *Address = getOrCreateAlloca(*MA); 405 assert((!isa<Instruction>(Address) || 406 DT.dominates(cast<Instruction>(Address)->getParent(), 407 Builder.GetInsertBlock())) && 408 "Domination violation"); 409 BBMap[MA->getBaseAddr()] = 410 Builder.CreateLoad(Address, Address->getName() + ".reload"); 411 } 412 } 413 414 void BlockGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT <S, 415 ValueMapT &BBMap) { 416 Loop *L = LI.getLoopFor(Stmt.getBasicBlock()); 417 418 assert(Stmt.isBlockStmt() && "Region statements need to use the " 419 "generateScalarStores() function in the " 420 "RegionGenerator"); 421 422 for (MemoryAccess *MA : Stmt) { 423 if (MA->isArrayKind() || MA->isRead()) 424 continue; 425 426 Value *Val = MA->getAccessValue(); 427 if (MA->isAnyPHIKind()) { 428 assert(MA->getIncoming().size() >= 1 && 429 "Block statements have exactly one exiting block, or multiple but " 430 "with same incoming block and value"); 431 assert(std::all_of(MA->getIncoming().begin(), MA->getIncoming().end(), 432 [&](std::pair<BasicBlock *, Value *> p) -> bool { 433 return p.first == Stmt.getBasicBlock(); 434 }) && 435 "Incoming block must be statement's block"); 436 Val = MA->getIncoming()[0].second; 437 } 438 auto *Address = getOrCreateAlloca(*MA); 439 440 Val = getNewValue(Stmt, Val, BBMap, LTS, L); 441 assert((!isa<Instruction>(Val) || 442 DT.dominates(cast<Instruction>(Val)->getParent(), 443 Builder.GetInsertBlock())) && 444 "Domination violation"); 445 assert((!isa<Instruction>(Address) || 446 DT.dominates(cast<Instruction>(Address)->getParent(), 447 Builder.GetInsertBlock())) && 448 "Domination violation"); 449 Builder.CreateStore(Val, Address); 450 } 451 } 452 453 void BlockGenerator::createScalarInitialization(Scop &S) { 454 Region &R = S.getRegion(); 455 BasicBlock *ExitBB = R.getExit(); 456 457 // The split block __just before__ the region and optimized region. 458 BasicBlock *SplitBB = R.getEnteringBlock(); 459 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator()); 460 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!"); 461 462 // Get the start block of the __optimized__ region. 463 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0); 464 if (StartBB == R.getEntry()) 465 StartBB = SplitBBTerm->getSuccessor(1); 466 467 Builder.SetInsertPoint(StartBB->getTerminator()); 468 469 for (auto &Pair : S.arrays()) { 470 auto &Array = Pair.second; 471 if (Array->getNumberOfDimensions() != 0) 472 continue; 473 if (Array->isPHIKind()) { 474 // For PHI nodes, the only values we need to store are the ones that 475 // reach the PHI node from outside the region. In general there should 476 // only be one such incoming edge and this edge should enter through 477 // 'SplitBB'. 478 auto PHI = cast<PHINode>(Array->getBasePtr()); 479 480 for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++) 481 if (!R.contains(*BI) && *BI != SplitBB) 482 llvm_unreachable("Incoming edges from outside the scop should always " 483 "come from SplitBB"); 484 485 int Idx = PHI->getBasicBlockIndex(SplitBB); 486 if (Idx < 0) 487 continue; 488 489 Value *ScalarValue = PHI->getIncomingValue(Idx); 490 491 Builder.CreateStore(ScalarValue, getOrCreatePHIAlloca(PHI)); 492 continue; 493 } 494 495 auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); 496 497 if (Inst && R.contains(Inst)) 498 continue; 499 500 // PHI nodes that are not marked as such in their SAI object are either exit 501 // PHI nodes we model as common scalars but without initialization, or 502 // incoming phi nodes that need to be initialized. Check if the first is the 503 // case for Inst and do not create and initialize memory if so. 504 if (auto *PHI = dyn_cast_or_null<PHINode>(Inst)) 505 if (!S.hasSingleExitEdge() && PHI->getBasicBlockIndex(ExitBB) >= 0) 506 continue; 507 508 Builder.CreateStore(Array->getBasePtr(), 509 getOrCreateScalarAlloca(Array->getBasePtr())); 510 } 511 } 512 513 void BlockGenerator::createScalarFinalization(Region &R) { 514 // The exit block of the __unoptimized__ region. 515 BasicBlock *ExitBB = R.getExitingBlock(); 516 // The merge block __just after__ the region and the optimized region. 517 BasicBlock *MergeBB = R.getExit(); 518 519 // The exit block of the __optimized__ region. 520 BasicBlock *OptExitBB = *(pred_begin(MergeBB)); 521 if (OptExitBB == ExitBB) 522 OptExitBB = *(++pred_begin(MergeBB)); 523 524 Builder.SetInsertPoint(OptExitBB->getTerminator()); 525 for (const auto &EscapeMapping : EscapeMap) { 526 // Extract the escaping instruction and the escaping users as well as the 527 // alloca the instruction was demoted to. 528 Instruction *EscapeInst = EscapeMapping.getFirst(); 529 const auto &EscapeMappingValue = EscapeMapping.getSecond(); 530 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second; 531 Value *ScalarAddr = EscapeMappingValue.first; 532 533 // Reload the demoted instruction in the optimized version of the SCoP. 534 Value *EscapeInstReload = 535 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload"); 536 EscapeInstReload = 537 Builder.CreateBitOrPointerCast(EscapeInstReload, EscapeInst->getType()); 538 539 // Create the merge PHI that merges the optimized and unoptimized version. 540 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2, 541 EscapeInst->getName() + ".merge"); 542 MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); 543 544 // Add the respective values to the merge PHI. 545 MergePHI->addIncoming(EscapeInstReload, OptExitBB); 546 MergePHI->addIncoming(EscapeInst, ExitBB); 547 548 // The information of scalar evolution about the escaping instruction needs 549 // to be revoked so the new merged instruction will be used. 550 if (SE.isSCEVable(EscapeInst->getType())) 551 SE.forgetValue(EscapeInst); 552 553 // Replace all uses of the demoted instruction with the merge PHI. 554 for (Instruction *EUser : EscapeUsers) 555 EUser->replaceUsesOfWith(EscapeInst, MergePHI); 556 } 557 } 558 559 void BlockGenerator::findOutsideUsers(Scop &S) { 560 auto &R = S.getRegion(); 561 for (auto &Pair : S.arrays()) { 562 auto &Array = Pair.second; 563 564 if (Array->getNumberOfDimensions() != 0) 565 continue; 566 567 if (Array->isPHIKind()) 568 continue; 569 570 auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); 571 572 if (!Inst) 573 continue; 574 575 // Scop invariant hoisting moves some of the base pointers out of the scop. 576 // We can ignore these, as the invariant load hoisting already registers the 577 // relevant outside users. 578 if (!R.contains(Inst)) 579 continue; 580 581 handleOutsideUsers(R, Inst, nullptr); 582 } 583 } 584 585 void BlockGenerator::createExitPHINodeMerges(Scop &S) { 586 if (S.hasSingleExitEdge()) 587 return; 588 589 Region &R = S.getRegion(); 590 591 auto *ExitBB = R.getExitingBlock(); 592 auto *MergeBB = R.getExit(); 593 auto *AfterMergeBB = MergeBB->getSingleSuccessor(); 594 BasicBlock *OptExitBB = *(pred_begin(MergeBB)); 595 if (OptExitBB == ExitBB) 596 OptExitBB = *(++pred_begin(MergeBB)); 597 598 Builder.SetInsertPoint(OptExitBB->getTerminator()); 599 600 for (auto &Pair : S.arrays()) { 601 auto &SAI = Pair.second; 602 auto *Val = SAI->getBasePtr(); 603 604 // Only Value-like scalars need a merge PHI. Exit block PHIs receive either 605 // the original PHI's value or the reloaded incoming values from the 606 // generated code. An llvm::Value is merged between the original code's 607 // value or the generated one. 608 if (!SAI->isValueKind() && !SAI->isExitPHIKind()) 609 continue; 610 611 PHINode *PHI = dyn_cast<PHINode>(Val); 612 if (!PHI) 613 continue; 614 615 if (PHI->getParent() != AfterMergeBB) 616 continue; 617 618 std::string Name = PHI->getName(); 619 Value *ScalarAddr = getOrCreateScalarAlloca(PHI); 620 Value *Reload = Builder.CreateLoad(ScalarAddr, Name + ".ph.final_reload"); 621 Reload = Builder.CreateBitOrPointerCast(Reload, PHI->getType()); 622 Value *OriginalValue = PHI->getIncomingValueForBlock(MergeBB); 623 assert((!isa<Instruction>(OriginalValue) || 624 cast<Instruction>(OriginalValue)->getParent() != MergeBB) && 625 "Original value must no be one we just generated."); 626 auto *MergePHI = PHINode::Create(PHI->getType(), 2, Name + ".ph.merge"); 627 MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); 628 MergePHI->addIncoming(Reload, OptExitBB); 629 MergePHI->addIncoming(OriginalValue, ExitBB); 630 int Idx = PHI->getBasicBlockIndex(MergeBB); 631 PHI->setIncomingValue(Idx, MergePHI); 632 } 633 } 634 635 void BlockGenerator::finalizeSCoP(Scop &S) { 636 findOutsideUsers(S); 637 createScalarInitialization(S); 638 createExitPHINodeMerges(S); 639 createScalarFinalization(S.getRegion()); 640 } 641 642 VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen, 643 std::vector<LoopToScevMapT> &VLTS, 644 isl_map *Schedule) 645 : BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) { 646 assert(Schedule && "No statement domain provided"); 647 } 648 649 Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, Value *Old, 650 ValueMapT &VectorMap, 651 VectorValueMapT &ScalarMaps, 652 Loop *L) { 653 if (Value *NewValue = VectorMap.lookup(Old)) 654 return NewValue; 655 656 int Width = getVectorWidth(); 657 658 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); 659 660 for (int Lane = 0; Lane < Width; Lane++) 661 Vector = Builder.CreateInsertElement( 662 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L), 663 Builder.getInt32(Lane)); 664 665 VectorMap[Old] = Vector; 666 667 return Vector; 668 } 669 670 Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { 671 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); 672 assert(PointerTy && "PointerType expected"); 673 674 Type *ScalarType = PointerTy->getElementType(); 675 VectorType *VectorType = VectorType::get(ScalarType, Width); 676 677 return PointerType::getUnqual(VectorType); 678 } 679 680 Value *VectorBlockGenerator::generateStrideOneLoad( 681 ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, 682 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) { 683 unsigned VectorWidth = getVectorWidth(); 684 auto *Pointer = Load->getPointerOperand(); 685 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); 686 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; 687 688 Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[Offset], 689 VLTS[Offset], NewAccesses); 690 Value *VectorPtr = 691 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 692 LoadInst *VecLoad = 693 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); 694 if (!Aligned) 695 VecLoad->setAlignment(8); 696 697 if (NegativeStride) { 698 SmallVector<Constant *, 16> Indices; 699 for (int i = VectorWidth - 1; i >= 0; i--) 700 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); 701 Constant *SV = llvm::ConstantVector::get(Indices); 702 Value *RevVecLoad = Builder.CreateShuffleVector( 703 VecLoad, VecLoad, SV, Load->getName() + "_reverse"); 704 return RevVecLoad; 705 } 706 707 return VecLoad; 708 } 709 710 Value *VectorBlockGenerator::generateStrideZeroLoad( 711 ScopStmt &Stmt, LoadInst *Load, ValueMapT &BBMap, 712 __isl_keep isl_id_to_ast_expr *NewAccesses) { 713 auto *Pointer = Load->getPointerOperand(); 714 Type *VectorPtrType = getVectorPtrTy(Pointer, 1); 715 Value *NewPointer = 716 generateLocationAccessed(Stmt, Load, BBMap, VLTS[0], NewAccesses); 717 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, 718 Load->getName() + "_p_vec_p"); 719 LoadInst *ScalarLoad = 720 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); 721 722 if (!Aligned) 723 ScalarLoad->setAlignment(8); 724 725 Constant *SplatVector = Constant::getNullValue( 726 VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 727 728 Value *VectorLoad = Builder.CreateShuffleVector( 729 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); 730 return VectorLoad; 731 } 732 733 Value *VectorBlockGenerator::generateUnknownStrideLoad( 734 ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, 735 __isl_keep isl_id_to_ast_expr *NewAccesses) { 736 int VectorWidth = getVectorWidth(); 737 auto *Pointer = Load->getPointerOperand(); 738 VectorType *VectorType = VectorType::get( 739 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); 740 741 Value *Vector = UndefValue::get(VectorType); 742 743 for (int i = 0; i < VectorWidth; i++) { 744 Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[i], 745 VLTS[i], NewAccesses); 746 Value *ScalarLoad = 747 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 748 Vector = Builder.CreateInsertElement( 749 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); 750 } 751 752 return Vector; 753 } 754 755 void VectorBlockGenerator::generateLoad( 756 ScopStmt &Stmt, LoadInst *Load, ValueMapT &VectorMap, 757 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 758 if (Value *PreloadLoad = GlobalMap.lookup(Load)) { 759 VectorMap[Load] = Builder.CreateVectorSplat(getVectorWidth(), PreloadLoad, 760 Load->getName() + "_p"); 761 return; 762 } 763 764 if (!VectorType::isValidElementType(Load->getType())) { 765 for (int i = 0; i < getVectorWidth(); i++) 766 ScalarMaps[i][Load] = 767 generateScalarLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses); 768 return; 769 } 770 771 const MemoryAccess &Access = Stmt.getArrayAccessFor(Load); 772 773 // Make sure we have scalar values available to access the pointer to 774 // the data location. 775 extractScalarValues(Load, VectorMap, ScalarMaps); 776 777 Value *NewLoad; 778 if (Access.isStrideZero(isl_map_copy(Schedule))) 779 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses); 780 else if (Access.isStrideOne(isl_map_copy(Schedule))) 781 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses); 782 else if (Access.isStrideX(isl_map_copy(Schedule), -1)) 783 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true); 784 else 785 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses); 786 787 VectorMap[Load] = NewLoad; 788 } 789 790 void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, UnaryInstruction *Inst, 791 ValueMapT &VectorMap, 792 VectorValueMapT &ScalarMaps) { 793 int VectorWidth = getVectorWidth(); 794 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap, 795 ScalarMaps, getLoopForStmt(Stmt)); 796 797 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); 798 799 const CastInst *Cast = dyn_cast<CastInst>(Inst); 800 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); 801 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); 802 } 803 804 void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, BinaryOperator *Inst, 805 ValueMapT &VectorMap, 806 VectorValueMapT &ScalarMaps) { 807 Loop *L = getLoopForStmt(Stmt); 808 Value *OpZero = Inst->getOperand(0); 809 Value *OpOne = Inst->getOperand(1); 810 811 Value *NewOpZero, *NewOpOne; 812 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L); 813 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L); 814 815 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, 816 Inst->getName() + "p_vec"); 817 VectorMap[Inst] = NewInst; 818 } 819 820 void VectorBlockGenerator::copyStore( 821 ScopStmt &Stmt, StoreInst *Store, ValueMapT &VectorMap, 822 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 823 const MemoryAccess &Access = Stmt.getArrayAccessFor(Store); 824 825 auto *Pointer = Store->getPointerOperand(); 826 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap, 827 ScalarMaps, getLoopForStmt(Stmt)); 828 829 // Make sure we have scalar values available to access the pointer to 830 // the data location. 831 extractScalarValues(Store, VectorMap, ScalarMaps); 832 833 if (Access.isStrideOne(isl_map_copy(Schedule))) { 834 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); 835 Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[0], 836 VLTS[0], NewAccesses); 837 838 Value *VectorPtr = 839 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 840 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); 841 842 if (!Aligned) 843 Store->setAlignment(8); 844 } else { 845 for (unsigned i = 0; i < ScalarMaps.size(); i++) { 846 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); 847 Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[i], 848 VLTS[i], NewAccesses); 849 Builder.CreateStore(Scalar, NewPointer); 850 } 851 } 852 } 853 854 bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, 855 ValueMapT &VectorMap) { 856 for (Value *Operand : Inst->operands()) 857 if (VectorMap.count(Operand)) 858 return true; 859 return false; 860 } 861 862 bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, 863 ValueMapT &VectorMap, 864 VectorValueMapT &ScalarMaps) { 865 bool HasVectorOperand = false; 866 int VectorWidth = getVectorWidth(); 867 868 for (Value *Operand : Inst->operands()) { 869 ValueMapT::iterator VecOp = VectorMap.find(Operand); 870 871 if (VecOp == VectorMap.end()) 872 continue; 873 874 HasVectorOperand = true; 875 Value *NewVector = VecOp->second; 876 877 for (int i = 0; i < VectorWidth; ++i) { 878 ValueMapT &SM = ScalarMaps[i]; 879 880 // If there is one scalar extracted, all scalar elements should have 881 // already been extracted by the code here. So no need to check for the 882 // existance of all of them. 883 if (SM.count(Operand)) 884 break; 885 886 SM[Operand] = 887 Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); 888 } 889 } 890 891 return HasVectorOperand; 892 } 893 894 void VectorBlockGenerator::copyInstScalarized( 895 ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, 896 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 897 bool HasVectorOperand; 898 int VectorWidth = getVectorWidth(); 899 900 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); 901 902 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) 903 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane], 904 VLTS[VectorLane], NewAccesses); 905 906 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) 907 return; 908 909 // Make the result available as vector value. 910 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); 911 Value *Vector = UndefValue::get(VectorType); 912 913 for (int i = 0; i < VectorWidth; i++) 914 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], 915 Builder.getInt32(i)); 916 917 VectorMap[Inst] = Vector; 918 } 919 920 int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); } 921 922 void VectorBlockGenerator::copyInstruction( 923 ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, 924 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 925 // Terminator instructions control the control flow. They are explicitly 926 // expressed in the clast and do not need to be copied. 927 if (Inst->isTerminator()) 928 return; 929 930 if (canSyntheziseInStmt(Stmt, Inst)) 931 return; 932 933 if (auto *Load = dyn_cast<LoadInst>(Inst)) { 934 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses); 935 return; 936 } 937 938 if (hasVectorOperands(Inst, VectorMap)) { 939 if (auto *Store = dyn_cast<StoreInst>(Inst)) { 940 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses); 941 return; 942 } 943 944 if (auto *Unary = dyn_cast<UnaryInstruction>(Inst)) { 945 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps); 946 return; 947 } 948 949 if (auto *Binary = dyn_cast<BinaryOperator>(Inst)) { 950 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps); 951 return; 952 } 953 954 // Falltrough: We generate scalar instructions, if we don't know how to 955 // generate vector code. 956 } 957 958 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses); 959 } 960 961 void VectorBlockGenerator::generateScalarVectorLoads( 962 ScopStmt &Stmt, ValueMapT &VectorBlockMap) { 963 for (MemoryAccess *MA : Stmt) { 964 if (MA->isArrayKind() || MA->isWrite()) 965 continue; 966 967 auto *Address = getOrCreateAlloca(*MA); 968 Type *VectorPtrType = getVectorPtrTy(Address, 1); 969 Value *VectorPtr = Builder.CreateBitCast(Address, VectorPtrType, 970 Address->getName() + "_p_vec_p"); 971 auto *Val = Builder.CreateLoad(VectorPtr, Address->getName() + ".reload"); 972 Constant *SplatVector = Constant::getNullValue( 973 VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 974 975 Value *VectorVal = Builder.CreateShuffleVector( 976 Val, Val, SplatVector, Address->getName() + "_p_splat"); 977 VectorBlockMap[MA->getBaseAddr()] = VectorVal; 978 VectorVal->dump(); 979 } 980 } 981 982 void VectorBlockGenerator::verifyNoScalarStores(ScopStmt &Stmt) { 983 for (MemoryAccess *MA : Stmt) { 984 if (MA->isArrayKind() || MA->isRead()) 985 continue; 986 987 llvm_unreachable("Scalar stores not expected in vector loop"); 988 } 989 } 990 991 void VectorBlockGenerator::copyStmt( 992 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) { 993 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by " 994 "the vector block generator"); 995 996 BasicBlock *BB = Stmt.getBasicBlock(); 997 BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), 998 &*Builder.GetInsertPoint(), &DT, &LI); 999 CopyBB->setName("polly.stmt." + BB->getName()); 1000 Builder.SetInsertPoint(&CopyBB->front()); 1001 1002 // Create two maps that store the mapping from the original instructions of 1003 // the old basic block to their copies in the new basic block. Those maps 1004 // are basic block local. 1005 // 1006 // As vector code generation is supported there is one map for scalar values 1007 // and one for vector values. 1008 // 1009 // In case we just do scalar code generation, the vectorMap is not used and 1010 // the scalarMap has just one dimension, which contains the mapping. 1011 // 1012 // In case vector code generation is done, an instruction may either appear 1013 // in the vector map once (as it is calculating >vectorwidth< values at a 1014 // time. Or (if the values are calculated using scalar operations), it 1015 // appears once in every dimension of the scalarMap. 1016 VectorValueMapT ScalarBlockMap(getVectorWidth()); 1017 ValueMapT VectorBlockMap; 1018 1019 generateScalarVectorLoads(Stmt, VectorBlockMap); 1020 1021 for (Instruction &Inst : *BB) 1022 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses); 1023 1024 verifyNoScalarStores(Stmt); 1025 } 1026 1027 BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB, 1028 BasicBlock *BBCopy) { 1029 1030 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); 1031 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); 1032 1033 if (BBCopyIDom) 1034 DT.changeImmediateDominator(BBCopy, BBCopyIDom); 1035 1036 return BBCopyIDom; 1037 } 1038 1039 // This is to determine whether an llvm::Value (defined in @p BB) is usable when 1040 // leaving a subregion. The straight-forward DT.dominates(BB, R->getExitBlock()) 1041 // does not work in cases where the exit block has edges from outside the 1042 // region. In that case the llvm::Value would never be usable in in the exit 1043 // block. The RegionGenerator however creates an new exit block ('ExitBBCopy') 1044 // for the subregion's exiting edges only. We need to determine whether an 1045 // llvm::Value is usable in there. We do this by checking whether it dominates 1046 // all exiting blocks individually. 1047 static bool isDominatingSubregionExit(const DominatorTree &DT, Region *R, 1048 BasicBlock *BB) { 1049 for (auto ExitingBB : predecessors(R->getExit())) { 1050 // Check for non-subregion incoming edges. 1051 if (!R->contains(ExitingBB)) 1052 continue; 1053 1054 if (!DT.dominates(BB, ExitingBB)) 1055 return false; 1056 } 1057 1058 return true; 1059 } 1060 1061 // Find the direct dominator of the subregion's exit block if the subregion was 1062 // simplified. 1063 static BasicBlock *findExitDominator(DominatorTree &DT, Region *R) { 1064 BasicBlock *Common = nullptr; 1065 for (auto ExitingBB : predecessors(R->getExit())) { 1066 // Check for non-subregion incoming edges. 1067 if (!R->contains(ExitingBB)) 1068 continue; 1069 1070 // First exiting edge. 1071 if (!Common) { 1072 Common = ExitingBB; 1073 continue; 1074 } 1075 1076 Common = DT.findNearestCommonDominator(Common, ExitingBB); 1077 } 1078 1079 assert(Common && R->contains(Common)); 1080 return Common; 1081 } 1082 1083 void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, 1084 isl_id_to_ast_expr *IdToAstExp) { 1085 assert(Stmt.isRegionStmt() && 1086 "Only region statements can be copied by the region generator"); 1087 1088 Scop *S = Stmt.getParent(); 1089 1090 // Forget all old mappings. 1091 BlockMap.clear(); 1092 RegionMaps.clear(); 1093 IncompletePHINodeMap.clear(); 1094 1095 // Collection of all values related to this subregion. 1096 ValueMapT ValueMap; 1097 1098 // The region represented by the statement. 1099 Region *R = Stmt.getRegion(); 1100 1101 // Create a dedicated entry for the region where we can reload all demoted 1102 // inputs. 1103 BasicBlock *EntryBB = R->getEntry(); 1104 BasicBlock *EntryBBCopy = SplitBlock(Builder.GetInsertBlock(), 1105 &*Builder.GetInsertPoint(), &DT, &LI); 1106 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry"); 1107 Builder.SetInsertPoint(&EntryBBCopy->front()); 1108 1109 ValueMapT &EntryBBMap = RegionMaps[EntryBBCopy]; 1110 generateScalarLoads(Stmt, EntryBBMap); 1111 1112 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI) 1113 if (!R->contains(*PI)) 1114 BlockMap[*PI] = EntryBBCopy; 1115 1116 // Determine the original exit block of this subregion. If it the exit block 1117 // is also the scop's exit, it it has been changed to polly.merge_new_and_old. 1118 // We move one block back to find the original block. This only happens if the 1119 // scop required simplification. 1120 // If the whole scop consists of only this non-affine region, then they share 1121 // the same Region object, such that we cannot change the exit of one and not 1122 // the other. 1123 BasicBlock *ExitBB = R->getExit(); 1124 if (!S->hasSingleExitEdge() && ExitBB == S->getRegion().getExit()) 1125 ExitBB = *(++pred_begin(ExitBB)); 1126 1127 // Iterate over all blocks in the region in a breadth-first search. 1128 std::deque<BasicBlock *> Blocks; 1129 SmallPtrSet<BasicBlock *, 8> SeenBlocks; 1130 Blocks.push_back(EntryBB); 1131 SeenBlocks.insert(EntryBB); 1132 1133 while (!Blocks.empty()) { 1134 BasicBlock *BB = Blocks.front(); 1135 Blocks.pop_front(); 1136 1137 // First split the block and update dominance information. 1138 BasicBlock *BBCopy = splitBB(BB); 1139 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy); 1140 1141 // In order to remap PHI nodes we store also basic block mappings. 1142 BlockMap[BB] = BBCopy; 1143 1144 // Get the mapping for this block and initialize it with either the scalar 1145 // loads from the generated entering block (which dominates all blocks of 1146 // this subregion) or the maps of the immediate dominator, if part of the 1147 // subregion. The latter necessarily includes the former. 1148 ValueMapT *InitBBMap; 1149 if (BBCopyIDom) { 1150 assert(RegionMaps.count(BBCopyIDom)); 1151 InitBBMap = &RegionMaps[BBCopyIDom]; 1152 } else 1153 InitBBMap = &EntryBBMap; 1154 auto Inserted = RegionMaps.insert(std::make_pair(BBCopy, *InitBBMap)); 1155 ValueMapT &RegionMap = Inserted.first->second; 1156 1157 // Copy the block with the BlockGenerator. 1158 Builder.SetInsertPoint(&BBCopy->front()); 1159 copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp); 1160 1161 // In order to remap PHI nodes we store also basic block mappings. 1162 BlockMap[BB] = BBCopy; 1163 1164 // Add values to incomplete PHI nodes waiting for this block to be copied. 1165 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB]) 1166 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS); 1167 IncompletePHINodeMap[BB].clear(); 1168 1169 // And continue with new successors inside the region. 1170 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++) 1171 if (R->contains(*SI) && SeenBlocks.insert(*SI).second) 1172 Blocks.push_back(*SI); 1173 1174 // Remember value in case it is visible after this subregion. 1175 if (isDominatingSubregionExit(DT, R, BB)) 1176 ValueMap.insert(RegionMap.begin(), RegionMap.end()); 1177 } 1178 1179 // Now create a new dedicated region exit block and add it to the region map. 1180 BasicBlock *ExitBBCopy = SplitBlock(Builder.GetInsertBlock(), 1181 &*Builder.GetInsertPoint(), &DT, &LI); 1182 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit"); 1183 BlockMap[R->getExit()] = ExitBBCopy; 1184 1185 BasicBlock *ExitDomBBCopy = BlockMap.lookup(findExitDominator(DT, R)); 1186 assert(ExitDomBBCopy && "Common exit dominator must be within region; at " 1187 "least the entry node must match"); 1188 DT.changeImmediateDominator(ExitBBCopy, ExitDomBBCopy); 1189 1190 // As the block generator doesn't handle control flow we need to add the 1191 // region control flow by hand after all blocks have been copied. 1192 for (BasicBlock *BB : SeenBlocks) { 1193 1194 BasicBlock *BBCopy = BlockMap[BB]; 1195 TerminatorInst *TI = BB->getTerminator(); 1196 if (isa<UnreachableInst>(TI)) { 1197 while (!BBCopy->empty()) 1198 BBCopy->begin()->eraseFromParent(); 1199 new UnreachableInst(BBCopy->getContext(), BBCopy); 1200 continue; 1201 } 1202 1203 Instruction *BICopy = BBCopy->getTerminator(); 1204 1205 ValueMapT &RegionMap = RegionMaps[BBCopy]; 1206 RegionMap.insert(BlockMap.begin(), BlockMap.end()); 1207 1208 Builder.SetInsertPoint(BICopy); 1209 copyInstScalar(Stmt, TI, RegionMap, LTS); 1210 BICopy->eraseFromParent(); 1211 } 1212 1213 // Add counting PHI nodes to all loops in the region that can be used as 1214 // replacement for SCEVs refering to the old loop. 1215 for (BasicBlock *BB : SeenBlocks) { 1216 Loop *L = LI.getLoopFor(BB); 1217 if (L == nullptr || L->getHeader() != BB || !R->contains(L)) 1218 continue; 1219 1220 BasicBlock *BBCopy = BlockMap[BB]; 1221 Value *NullVal = Builder.getInt32(0); 1222 PHINode *LoopPHI = 1223 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv"); 1224 Instruction *LoopPHIInc = BinaryOperator::CreateAdd( 1225 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc"); 1226 LoopPHI->insertBefore(&BBCopy->front()); 1227 LoopPHIInc->insertBefore(BBCopy->getTerminator()); 1228 1229 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) { 1230 if (!R->contains(PredBB)) 1231 continue; 1232 if (L->contains(PredBB)) 1233 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]); 1234 else 1235 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]); 1236 } 1237 1238 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy))) 1239 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0) 1240 LoopPHI->addIncoming(NullVal, PredBBCopy); 1241 1242 LTS[L] = SE.getUnknown(LoopPHI); 1243 } 1244 1245 // Continue generating code in the exit block. 1246 Builder.SetInsertPoint(&*ExitBBCopy->getFirstInsertionPt()); 1247 1248 // Write values visible to other statements. 1249 generateScalarStores(Stmt, LTS, ValueMap); 1250 BlockMap.clear(); 1251 RegionMaps.clear(); 1252 IncompletePHINodeMap.clear(); 1253 } 1254 1255 PHINode *RegionGenerator::buildExitPHI(MemoryAccess *MA, LoopToScevMapT <S, 1256 ValueMapT &BBMap, Loop *L) { 1257 ScopStmt *Stmt = MA->getStatement(); 1258 Region *SubR = Stmt->getRegion(); 1259 auto Incoming = MA->getIncoming(); 1260 1261 PollyIRBuilder::InsertPointGuard IPGuard(Builder); 1262 PHINode *OrigPHI = cast<PHINode>(MA->getAccessInstruction()); 1263 BasicBlock *NewSubregionExit = Builder.GetInsertBlock(); 1264 1265 // This can happen if the subregion is simplified after the ScopStmts 1266 // have been created; simplification happens as part of CodeGeneration. 1267 if (OrigPHI->getParent() != SubR->getExit()) { 1268 BasicBlock *FormerExit = SubR->getExitingBlock(); 1269 if (FormerExit) 1270 NewSubregionExit = BlockMap.lookup(FormerExit); 1271 } 1272 1273 PHINode *NewPHI = PHINode::Create(OrigPHI->getType(), Incoming.size(), 1274 "polly." + OrigPHI->getName(), 1275 NewSubregionExit->getFirstNonPHI()); 1276 1277 // Add the incoming values to the PHI. 1278 for (auto &Pair : Incoming) { 1279 BasicBlock *OrigIncomingBlock = Pair.first; 1280 BasicBlock *NewIncomingBlock = BlockMap.lookup(OrigIncomingBlock); 1281 Builder.SetInsertPoint(NewIncomingBlock->getTerminator()); 1282 assert(RegionMaps.count(NewIncomingBlock)); 1283 ValueMapT *LocalBBMap = &RegionMaps[NewIncomingBlock]; 1284 1285 Value *OrigIncomingValue = Pair.second; 1286 Value *NewIncomingValue = 1287 getNewValue(*Stmt, OrigIncomingValue, *LocalBBMap, LTS, L); 1288 NewPHI->addIncoming(NewIncomingValue, NewIncomingBlock); 1289 } 1290 1291 return NewPHI; 1292 } 1293 1294 Value *RegionGenerator::getExitScalar(MemoryAccess *MA, LoopToScevMapT <S, 1295 ValueMapT &BBMap) { 1296 ScopStmt *Stmt = MA->getStatement(); 1297 1298 // TODO: Add some test cases that ensure this is really the right choice. 1299 Loop *L = LI.getLoopFor(Stmt->getRegion()->getExit()); 1300 1301 if (MA->isAnyPHIKind()) { 1302 auto Incoming = MA->getIncoming(); 1303 assert(!Incoming.empty() && 1304 "PHI WRITEs must have originate from at least one incoming block"); 1305 1306 // If there is only one incoming value, we do not need to create a PHI. 1307 if (Incoming.size() == 1) { 1308 Value *OldVal = Incoming[0].second; 1309 return getNewValue(*Stmt, OldVal, BBMap, LTS, L); 1310 } 1311 1312 return buildExitPHI(MA, LTS, BBMap, L); 1313 } 1314 1315 // MK_Value accesses leaving the subregion must dominate the exit block; just 1316 // pass the copied value 1317 Value *OldVal = MA->getAccessValue(); 1318 return getNewValue(*Stmt, OldVal, BBMap, LTS, L); 1319 } 1320 1321 void RegionGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT <S, 1322 ValueMapT &BBMap) { 1323 assert(Stmt.getRegion() && 1324 "Block statements need to use the generateScalarStores() " 1325 "function in the BlockGenerator"); 1326 1327 for (MemoryAccess *MA : Stmt) { 1328 if (MA->isArrayKind() || MA->isRead()) 1329 continue; 1330 1331 Value *NewVal = getExitScalar(MA, LTS, BBMap); 1332 Value *Address = getOrCreateAlloca(*MA); 1333 assert((!isa<Instruction>(NewVal) || 1334 DT.dominates(cast<Instruction>(NewVal)->getParent(), 1335 Builder.GetInsertBlock())) && 1336 "Domination violation"); 1337 assert((!isa<Instruction>(Address) || 1338 DT.dominates(cast<Instruction>(Address)->getParent(), 1339 Builder.GetInsertBlock())) && 1340 "Domination violation"); 1341 Builder.CreateStore(NewVal, Address); 1342 } 1343 } 1344 1345 void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI, 1346 PHINode *PHICopy, BasicBlock *IncomingBB, 1347 LoopToScevMapT <S) { 1348 Region *StmtR = Stmt.getRegion(); 1349 1350 // If the incoming block was not yet copied mark this PHI as incomplete. 1351 // Once the block will be copied the incoming value will be added. 1352 BasicBlock *BBCopy = BlockMap[IncomingBB]; 1353 if (!BBCopy) { 1354 assert(StmtR->contains(IncomingBB) && 1355 "Bad incoming block for PHI in non-affine region"); 1356 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy)); 1357 return; 1358 } 1359 1360 Value *OpCopy = nullptr; 1361 if (StmtR->contains(IncomingBB)) { 1362 assert(RegionMaps.count(BBCopy) && 1363 "Incoming PHI block did not have a BBMap"); 1364 ValueMapT &BBCopyMap = RegionMaps[BBCopy]; 1365 1366 Value *Op = PHI->getIncomingValueForBlock(IncomingBB); 1367 1368 BasicBlock *OldBlock = Builder.GetInsertBlock(); 1369 auto OldIP = Builder.GetInsertPoint(); 1370 Builder.SetInsertPoint(BBCopy->getTerminator()); 1371 OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForStmt(Stmt)); 1372 Builder.SetInsertPoint(OldBlock, OldIP); 1373 } else { 1374 1375 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0) 1376 return; 1377 1378 Value *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI)); 1379 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload", 1380 BlockMap[IncomingBB]->getTerminator()); 1381 } 1382 1383 assert(OpCopy && "Incoming PHI value was not copied properly"); 1384 assert(BBCopy && "Incoming PHI block was not copied properly"); 1385 PHICopy->addIncoming(OpCopy, BBCopy); 1386 } 1387 1388 void RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, PHINode *PHI, 1389 ValueMapT &BBMap, 1390 LoopToScevMapT <S) { 1391 unsigned NumIncoming = PHI->getNumIncomingValues(); 1392 PHINode *PHICopy = 1393 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName()); 1394 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI()); 1395 BBMap[PHI] = PHICopy; 1396 1397 for (unsigned u = 0; u < NumIncoming; u++) 1398 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), LTS); 1399 } 1400