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