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