1 //===--- Utils.cpp - Utility functions for the code generation --*- 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 contains utility functions for the code generation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "polly/CodeGen/Utils.h" 15 #include "polly/CodeGen/IRBuilder.h" 16 #include "polly/ScopInfo.h" 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 20 21 using namespace llvm; 22 23 BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) { 24 BasicBlock *StartBlock, *SplitBlock, *NewBlock; 25 Region &R = S.getRegion(); 26 PollyIRBuilder Builder(R.getEntry()); 27 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 28 RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo(); 29 LoopInfo &LI = P->getAnalysis<LoopInfo>(); 30 31 // Split the entry edge of the region and generate a new basic block on this 32 // edge. This function also updates ScopInfo and RegionInfo. 33 NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), P); 34 if (DT.dominates(R.getEntry(), NewBlock)) { 35 BasicBlock *OldBlock = R.getEntry(); 36 std::string OldName = OldBlock->getName(); 37 38 // Update ScopInfo. 39 for (ScopStmt *Stmt : S) 40 if (Stmt->getBasicBlock() == OldBlock) { 41 Stmt->setBasicBlock(NewBlock); 42 break; 43 } 44 45 // Update RegionInfo. 46 SplitBlock = OldBlock; 47 OldBlock->setName("polly.split"); 48 NewBlock->setName(OldName); 49 R.replaceEntryRecursive(NewBlock); 50 RI.setRegionFor(NewBlock, &R); 51 } else { 52 RI.setRegionFor(NewBlock, R.getParent()); 53 SplitBlock = NewBlock; 54 } 55 56 SplitBlock->setName("polly.split_new_and_old"); 57 Function *F = SplitBlock->getParent(); 58 StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F); 59 SplitBlock->getTerminator()->eraseFromParent(); 60 Builder.SetInsertPoint(SplitBlock); 61 Builder.CreateCondBr(RTC, StartBlock, R.getEntry()); 62 if (Loop *L = LI.getLoopFor(SplitBlock)) 63 L->addBasicBlockToLoop(StartBlock, LI.getBase()); 64 DT.addNewBlock(StartBlock, SplitBlock); 65 Builder.SetInsertPoint(StartBlock); 66 67 BasicBlock *MergeBlock; 68 69 if (R.getExit()->getSinglePredecessor()) 70 // No splitEdge required. A block with a single predecessor cannot have 71 // PHI nodes that would complicate life. 72 MergeBlock = R.getExit(); 73 else { 74 MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), P); 75 // SplitEdge will never split R.getExit(), as R.getExit() has more than 76 // one predecessor. Hence, mergeBlock is always a newly generated block. 77 R.replaceExitRecursive(MergeBlock); 78 RI.setRegionFor(MergeBlock, &R); 79 } 80 81 Builder.CreateBr(MergeBlock); 82 MergeBlock->setName("polly.merge_new_and_old"); 83 84 if (DT.dominates(SplitBlock, MergeBlock)) 85 DT.changeImmediateDominator(MergeBlock, SplitBlock); 86 return StartBlock; 87 } 88