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