blob: 890d90192e2a0949b4663aa765fdc3f4cd133086 [file] [log] [blame]
Tobias Grosser3a275d22012-05-29 09:11:54 +00001//===--- 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 Grosser5103ba72014-03-04 14:58:49 +000015#include "polly/CodeGen/IRBuilder.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000016#include "polly/ScopInfo.h"
Tobias Grosser472d3b72014-02-24 00:50:49 +000017#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000018#include "llvm/Analysis/RegionInfo.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000019#include "llvm/Support/Debug.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21
22using namespace llvm;
23
Michael Kruse22370882015-08-11 14:39:21 +000024// Alternative to llvm::SplitCriticalEdge.
25//
26// Creates a new block which branches to Succ. The edge to split is redirected
27// to the new block.
28//
29// The issue with llvm::SplitCriticalEdge is that it does nothing if the edge is
30// not critical.
31// The issue with llvm::SplitEdge is that it does not always create the middle
32// block, but reuses Prev/Succ if it can. We always want a new middle block.
33static BasicBlock *splitEdge(BasicBlock *Prev, BasicBlock *Succ,
34 const char *Suffix, DominatorTree *DT,
35 LoopInfo *LI, RegionInfo *RI) {
36 assert(Prev && Succ);
37
38 // Before:
39 // \ / / //
40 // Prev / //
41 // | \___/ //
42 // | ___ //
43 // | / \ //
44 // Succ \ //
45 // / \ \ //
46
47 // The algorithm to update DominatorTree and LoopInfo of
48 // llvm::SplitCriticalEdge is more efficient than
49 // llvm::SplitBlockPredecessors, which is more general. In the future we might
50 // either modify llvm::SplitCriticalEdge to allow skipping the critical edge
51 // check; or Copy&Pase it here.
52 BasicBlock *MiddleBlock = SplitBlockPredecessors(
53 Succ, ArrayRef<BasicBlock *>(Prev), Suffix, DT, LI);
54
55 if (RI) {
56 Region *PrevRegion = RI->getRegionFor(Prev);
57 Region *SuccRegion = RI->getRegionFor(Succ);
58 if (PrevRegion->contains(MiddleBlock)) {
59 RI->setRegionFor(MiddleBlock, PrevRegion);
60 } else {
61 RI->setRegionFor(MiddleBlock, SuccRegion);
62 }
63 }
64
65 // After:
66 // \ / / //
67 // Prev / //
68 // | \___/ //
69 // | //
70 // MiddleBlock //
71 // | ___ //
72 // | / \ //
73 // Succ \ //
74 // / \ \ //
75
76 return MiddleBlock;
77}
78
Johannes Doerfert38262242014-09-10 14:50:23 +000079BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) {
Tobias Grosser3a275d22012-05-29 09:11:54 +000080 Region &R = S.getRegion();
Johannes Doerfertef744432016-05-23 12:42:38 +000081 PollyIRBuilder Builder(S.getEntry());
Johannes Doerfert38262242014-09-10 14:50:23 +000082 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
83 RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo();
Chandler Carruthf5579872015-01-17 14:16:56 +000084 LoopInfo &LI = P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tobias Grosser3a275d22012-05-29 09:11:54 +000085
Michael Kruse22370882015-08-11 14:39:21 +000086 // Before:
87 //
88 // \ / //
89 // EnteringBB //
90 // _____|_____ //
91 // / EntryBB \ //
92 // | (region) | //
93 // \_ExitingBB_/ //
94 // | //
95 // ExitBB //
96 // / \ //
Tobias Grosser3a275d22012-05-29 09:11:54 +000097
Michael Kruse22370882015-08-11 14:39:21 +000098 // Create a fork block.
Johannes Doerfertef744432016-05-23 12:42:38 +000099 BasicBlock *EnteringBB = S.getEnteringBlock();
100 BasicBlock *EntryBB = S.getEntry();
Michael Kruse22370882015-08-11 14:39:21 +0000101 assert(EnteringBB && "Must be a simple region");
102 BasicBlock *SplitBlock =
103 splitEdge(EnteringBB, EntryBB, ".split_new_and_old", &DT, &LI, &RI);
Tobias Grosser3a275d22012-05-29 09:11:54 +0000104 SplitBlock->setName("polly.split_new_and_old");
Michael Kruse22370882015-08-11 14:39:21 +0000105
Michael Krused2b03602015-08-18 13:14:42 +0000106 // If EntryBB is the exit block of the region that includes Prev, exclude
107 // SplitBlock from that region by making it itself the exit block. This is
108 // trivially possible because there is just one edge to EnteringBB.
109 // This is necessary because we will add an outgoing edge from SplitBlock,
110 // which would violate the single exit block requirement of PrevRegion.
111 Region *PrevRegion = RI.getRegionFor(EnteringBB);
112 while (PrevRegion->getExit() == EntryBB) {
113 PrevRegion->replaceExit(SplitBlock);
114 PrevRegion = PrevRegion->getParent();
115 }
116 RI.setRegionFor(SplitBlock, PrevRegion);
117
Michael Kruse22370882015-08-11 14:39:21 +0000118 // Create a join block
Johannes Doerfertef744432016-05-23 12:42:38 +0000119 BasicBlock *ExitingBB = S.getExitingBlock();
120 BasicBlock *ExitBB = S.getExit();
Michael Kruse22370882015-08-11 14:39:21 +0000121 assert(ExitingBB && "Must be a simple region");
122 BasicBlock *MergeBlock =
123 splitEdge(ExitingBB, ExitBB, ".merge_new_and_old", &DT, &LI, &RI);
124 MergeBlock->setName("polly.merge_new_and_old");
125
126 // Exclude the join block from the region.
127 R.replaceExitRecursive(MergeBlock);
128 RI.setRegionFor(MergeBlock, R.getParent());
129
130 // \ / //
131 // EnteringBB //
132 // | //
133 // SplitBlock //
134 // _____|_____ //
135 // / EntryBB \ //
136 // | (region) | //
137 // \_ExitingBB_/ //
138 // | //
139 // MergeBlock //
140 // | //
141 // ExitBB //
142 // / \ //
143
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000144 // Create the start and exiting block.
Tobias Grosser3a275d22012-05-29 09:11:54 +0000145 Function *F = SplitBlock->getParent();
Michael Kruse22370882015-08-11 14:39:21 +0000146 BasicBlock *StartBlock =
147 BasicBlock::Create(F->getContext(), "polly.start", F);
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000148 BasicBlock *ExitingBlock =
149 BasicBlock::Create(F->getContext(), "polly.exiting", F);
Tobias Grosser3a275d22012-05-29 09:11:54 +0000150 SplitBlock->getTerminator()->eraseFromParent();
151 Builder.SetInsertPoint(SplitBlock);
Johannes Doerfertef744432016-05-23 12:42:38 +0000152 Builder.CreateCondBr(RTC, StartBlock, S.getEntry());
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000153 if (Loop *L = LI.getLoopFor(SplitBlock)) {
Chandler Carruth6adcf562015-01-18 01:47:30 +0000154 L->addBasicBlockToLoop(StartBlock, LI);
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000155 L->addBasicBlockToLoop(ExitingBlock, LI);
156 }
Tobias Grosser3a275d22012-05-29 09:11:54 +0000157 DT.addNewBlock(StartBlock, SplitBlock);
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000158 DT.addNewBlock(ExitingBlock, StartBlock);
Michael Kruse22370882015-08-11 14:39:21 +0000159 RI.setRegionFor(StartBlock, RI.getRegionFor(SplitBlock));
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000160 RI.setRegionFor(ExitingBlock, RI.getRegionFor(SplitBlock));
Michael Kruse22370882015-08-11 14:39:21 +0000161
162 // \ / //
163 // EnteringBB //
164 // | //
165 // SplitBlock---------\ //
166 // _____|_____ | //
167 // / EntryBB \ StartBlock //
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000168 // | (region) | | //
169 // \_ExitingBB_/ ExitingBlock //
Michael Kruse22370882015-08-11 14:39:21 +0000170 // | //
171 // MergeBlock //
172 // | //
173 // ExitBB //
174 // / \ //
175
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000176 // Connect start block to exiting block.
Tobias Grosser3a275d22012-05-29 09:11:54 +0000177 Builder.SetInsertPoint(StartBlock);
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000178 Builder.CreateBr(ExitingBlock);
179 DT.changeImmediateDominator(ExitingBlock, StartBlock);
180
181 // Connect exiting block to join block.
182 Builder.SetInsertPoint(ExitingBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +0000183 Builder.CreateBr(MergeBlock);
Michael Kruse22370882015-08-11 14:39:21 +0000184 DT.changeImmediateDominator(MergeBlock, SplitBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +0000185
Michael Kruse22370882015-08-11 14:39:21 +0000186 // \ / //
187 // EnteringBB //
188 // | //
189 // SplitBlock---------\ //
190 // _____|_____ | //
191 // / EntryBB \ StartBlock //
192 // | (region) | | //
Tobias Grosser2d3d4ec2015-12-09 11:38:22 +0000193 // \_ExitingBB_/ ExitingBlock //
Michael Kruse22370882015-08-11 14:39:21 +0000194 // | | //
195 // MergeBlock---------/ //
196 // | //
197 // ExitBB //
198 // / \ //
199
Tobias Grosser3a275d22012-05-29 09:11:54 +0000200 return StartBlock;
201}