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 | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 15 | #include "polly/CodeGen/IRBuilder.h" |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 16 | #include "polly/ScopInfo.h" |
Tobias Grosser | 472d3b7 | 2014-02-24 00:50:49 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.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 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 23 | BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) { |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 24 | BasicBlock *StartBlock, *SplitBlock, *NewBlock; |
| 25 | Region &R = S.getRegion(); |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 26 | PollyIRBuilder Builder(R.getEntry()); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 27 | DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 28 | RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo(); |
| 29 | LoopInfo &LI = P->getAnalysis<LoopInfo>(); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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. |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 33 | NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), P); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 34 | if (DT.dominates(R.getEntry(), NewBlock)) { |
| 35 | BasicBlock *OldBlock = R.getEntry(); |
| 36 | std::string OldName = OldBlock->getName(); |
| 37 | |
| 38 | // Update ScopInfo. |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 39 | for (ScopStmt *Stmt : S) |
| 40 | if (Stmt->getBasicBlock() == OldBlock) { |
| 41 | Stmt->setBasicBlock(NewBlock); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 42 | break; |
| 43 | } |
| 44 | |
| 45 | // Update RegionInfo. |
| 46 | SplitBlock = OldBlock; |
| 47 | OldBlock->setName("polly.split"); |
| 48 | NewBlock->setName(OldName); |
Tobias Grosser | 03fc9ac | 2013-04-10 06:55:20 +0000 | [diff] [blame] | 49 | R.replaceEntryRecursive(NewBlock); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 61 | Builder.CreateCondBr(RTC, StartBlock, R.getEntry()); |
Tobias Grosser | 472d3b7 | 2014-02-24 00:50:49 +0000 | [diff] [blame] | 62 | if (Loop *L = LI.getLoopFor(SplitBlock)) |
| 63 | L->addBasicBlockToLoop(StartBlock, LI.getBase()); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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 { |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 74 | MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), P); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 75 | // SplitEdge will never split R.getExit(), as R.getExit() has more than |
| 76 | // one predecessor. Hence, mergeBlock is always a newly generated block. |
Tobias Grosser | 03fc9ac | 2013-04-10 06:55:20 +0000 | [diff] [blame] | 77 | R.replaceExitRecursive(MergeBlock); |
| 78 | RI.setRegionFor(MergeBlock, &R); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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 | } |