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" |
Johannes Doerfert | f32d651 | 2015-03-01 18:45:58 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 24 | BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) { |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 25 | BasicBlock *StartBlock, *SplitBlock, *NewBlock; |
| 26 | Region &R = S.getRegion(); |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 27 | PollyIRBuilder Builder(R.getEntry()); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 28 | DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 29 | RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo(); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 30 | LoopInfo &LI = P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
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. |
Chandler Carruth | 62975f5 | 2015-01-19 12:37:33 +0000 | [diff] [blame] | 34 | NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), &DT, &LI); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 35 | if (DT.dominates(R.getEntry(), NewBlock)) { |
| 36 | BasicBlock *OldBlock = R.getEntry(); |
| 37 | std::string OldName = OldBlock->getName(); |
| 38 | |
| 39 | // Update ScopInfo. |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame^] | 40 | for (ScopStmt &Stmt : S) |
| 41 | if (Stmt.getBasicBlock() == OldBlock) { |
| 42 | Stmt.setBasicBlock(NewBlock); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 62 | Builder.CreateCondBr(RTC, StartBlock, R.getEntry()); |
Tobias Grosser | 472d3b7 | 2014-02-24 00:50:49 +0000 | [diff] [blame] | 63 | if (Loop *L = LI.getLoopFor(SplitBlock)) |
Chandler Carruth | 6adcf56 | 2015-01-18 01:47:30 +0000 | [diff] [blame] | 64 | L->addBasicBlockToLoop(StartBlock, LI); |
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 { |
Chandler Carruth | 62975f5 | 2015-01-19 12:37:33 +0000 | [diff] [blame] | 75 | MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), &DT, &LI); |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 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 | } |