Coverage Report

Created: 2017-08-18 19:41

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/CodeGen/Utils.cpp
Line
Count
Source (jump to first uncovered line)
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"
15
#include "polly/CodeGen/IRBuilder.h"
16
#include "polly/ScopInfo.h"
17
#include "llvm/Analysis/LoopInfo.h"
18
#include "llvm/Analysis/RegionInfo.h"
19
#include "llvm/Support/Debug.h"
20
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21
22
using namespace llvm;
23
24
// 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.
33
static BasicBlock *splitEdge(BasicBlock *Prev, BasicBlock *Succ,
34
                             const char *Suffix, DominatorTree *DT,
35
837
                             LoopInfo *LI, RegionInfo *RI) {
36
837
  assert(Prev && Succ);
37
837
38
837
  // Before:
39
837
  //   \    /     /   //
40
837
  //    Prev     /    //
41
837
  //     |  \___/     //
42
837
  //     |   ___      //
43
837
  //     |  /   \     //
44
837
  //    Succ     \    //
45
837
  //   /    \     \   //
46
837
47
837
  // The algorithm to update DominatorTree and LoopInfo of
48
837
  // llvm::SplitCriticalEdge is more efficient than
49
837
  // llvm::SplitBlockPredecessors, which is more general. In the future we might
50
837
  // either modify llvm::SplitCriticalEdge to allow skipping the critical edge
51
837
  // check; or Copy&Pase it here.
52
837
  BasicBlock *MiddleBlock = SplitBlockPredecessors(
53
837
      Succ, ArrayRef<BasicBlock *>(Prev), Suffix, DT, LI);
54
837
55
837
  if (
RI837
)
{837
56
837
    Region *PrevRegion = RI->getRegionFor(Prev);
57
837
    Region *SuccRegion = RI->getRegionFor(Succ);
58
837
    if (
PrevRegion->contains(MiddleBlock)837
)
{837
59
837
      RI->setRegionFor(MiddleBlock, PrevRegion);
60
837
    } else {
61
0
      RI->setRegionFor(MiddleBlock, SuccRegion);
62
837
    }
63
837
  }
64
837
65
837
  // After:
66
837
  //   \    /     /   //
67
837
  //    Prev     /    //
68
837
  //     |  \___/     //
69
837
  //     |            //
70
837
  // MiddleBlock      //
71
837
  //     |   ___      //
72
837
  //     |  /   \     //
73
837
  //    Succ     \    //
74
837
  //   /    \     \   //
75
837
76
837
  return MiddleBlock;
77
837
}
78
79
std::pair<polly::BBPair, BranchInst *>
80
polly::executeScopConditionally(Scop &S, Value *RTC, DominatorTree &DT,
81
279
                                RegionInfo &RI, LoopInfo &LI) {
82
279
  Region &R = S.getRegion();
83
279
  PollyIRBuilder Builder(S.getEntry());
84
279
85
279
  // Before:
86
279
  //
87
279
  //      \   /      //
88
279
  //    EnteringBB   //
89
279
  //   _____|_____   //
90
279
  //  /  EntryBB  \  //
91
279
  //  |  (region) |  //
92
279
  //  \_ExitingBB_/  //
93
279
  //        |        //
94
279
  //      ExitBB     //
95
279
  //      /    \     //
96
279
97
279
  // Create a fork block.
98
279
  BasicBlock *EnteringBB = S.getEnteringBlock();
99
279
  BasicBlock *EntryBB = S.getEntry();
100
279
  assert(EnteringBB && "Must be a simple region");
101
279
  BasicBlock *SplitBlock =
102
279
      splitEdge(EnteringBB, EntryBB, ".split_new_and_old", &DT, &LI, &RI);
103
279
  SplitBlock->setName("polly.split_new_and_old");
104
279
105
279
  // If EntryBB is the exit block of the region that includes Prev, exclude
106
279
  // SplitBlock from that region by making it itself the exit block. This is
107
279
  // trivially possible because there is just one edge to EnteringBB.
108
279
  // This is necessary because we will add an outgoing edge from SplitBlock,
109
279
  // which would violate the single exit block requirement of PrevRegion.
110
279
  Region *PrevRegion = RI.getRegionFor(EnteringBB);
111
282
  while (
PrevRegion->getExit() == EntryBB282
)
{3
112
3
    PrevRegion->replaceExit(SplitBlock);
113
3
    PrevRegion = PrevRegion->getParent();
114
279
  }
115
279
  RI.setRegionFor(SplitBlock, PrevRegion);
116
279
117
279
  // Create a join block
118
279
  BasicBlock *ExitingBB = S.getExitingBlock();
119
279
  BasicBlock *ExitBB = S.getExit();
120
279
  assert(ExitingBB && "Must be a simple region");
121
279
  BasicBlock *MergeBlock =
122
279
      splitEdge(ExitingBB, ExitBB, ".merge_new_and_old", &DT, &LI, &RI);
123
279
  MergeBlock->setName("polly.merge_new_and_old");
124
279
125
279
  // Exclude the join block from the region.
126
279
  R.replaceExitRecursive(MergeBlock);
127
279
  RI.setRegionFor(MergeBlock, R.getParent());
128
279
129
279
  //      \   /      //
130
279
  //    EnteringBB   //
131
279
  //        |        //
132
279
  //    SplitBlock   //
133
279
  //   _____|_____   //
134
279
  //  /  EntryBB  \  //
135
279
  //  |  (region) |  //
136
279
  //  \_ExitingBB_/  //
137
279
  //        |        //
138
279
  //    MergeBlock   //
139
279
  //        |        //
140
279
  //      ExitBB     //
141
279
  //      /    \     //
142
279
143
279
  // Create the start and exiting block.
144
279
  Function *F = SplitBlock->getParent();
145
279
  BasicBlock *StartBlock =
146
279
      BasicBlock::Create(F->getContext(), "polly.start", F);
147
279
  BasicBlock *ExitingBlock =
148
279
      BasicBlock::Create(F->getContext(), "polly.exiting", F);
149
279
  SplitBlock->getTerminator()->eraseFromParent();
150
279
  Builder.SetInsertPoint(SplitBlock);
151
279
  BranchInst *CondBr = Builder.CreateCondBr(RTC, StartBlock, S.getEntry());
152
279
153
279
  if (Loop *
L279
= LI.getLoopFor(SplitBlock))
{23
154
23
    L->addBasicBlockToLoop(StartBlock, LI);
155
23
    L->addBasicBlockToLoop(ExitingBlock, LI);
156
279
  }
157
279
  DT.addNewBlock(StartBlock, SplitBlock);
158
279
  DT.addNewBlock(ExitingBlock, StartBlock);
159
279
  RI.setRegionFor(StartBlock, RI.getRegionFor(SplitBlock));
160
279
  RI.setRegionFor(ExitingBlock, RI.getRegionFor(SplitBlock));
161
279
162
279
  //      \   /                    //
163
279
  //    EnteringBB                 //
164
279
  //        |                      //
165
279
  //    SplitBlock---------\       //
166
279
  //   _____|_____         |       //
167
279
  //  /  EntryBB  \    StartBlock  //
168
279
  //  |  (region) |        |       //
169
279
  //  \_ExitingBB_/   ExitingBlock //
170
279
  //        |                      //
171
279
  //    MergeBlock                 //
172
279
  //        |                      //
173
279
  //      ExitBB                   //
174
279
  //      /    \                   //
175
279
176
279
  // Connect start block to exiting block.
177
279
  Builder.SetInsertPoint(StartBlock);
178
279
  Builder.CreateBr(ExitingBlock);
179
279
  DT.changeImmediateDominator(ExitingBlock, StartBlock);
180
279
181
279
  // Connect exiting block to join block.
182
279
  Builder.SetInsertPoint(ExitingBlock);
183
279
  Builder.CreateBr(MergeBlock);
184
279
  DT.changeImmediateDominator(MergeBlock, SplitBlock);
185
279
186
279
  //      \   /                    //
187
279
  //    EnteringBB                 //
188
279
  //        |                      //
189
279
  //    SplitBlock---------\       //
190
279
  //   _____|_____         |       //
191
279
  //  /  EntryBB  \    StartBlock  //
192
279
  //  |  (region) |        |       //
193
279
  //  \_ExitingBB_/   ExitingBlock //
194
279
  //        |              |       //
195
279
  //    MergeBlock---------/       //
196
279
  //        |                      //
197
279
  //      ExitBB                   //
198
279
  //      /    \                   //
199
279
  //
200
279
201
279
  // Split the edge between SplitBlock and EntryBB, to avoid a critical edge.
202
279
  splitEdge(SplitBlock, EntryBB, ".pre_entry_bb", &DT, &LI, &RI);
203
279
204
279
  //      \   /                    //
205
279
  //    EnteringBB                 //
206
279
  //        |                      //
207
279
  //    SplitBlock---------\       //
208
279
  //        |              |       //
209
279
  //    PreEntryBB         |       //
210
279
  //   _____|_____         |       //
211
279
  //  /  EntryBB  \    StartBlock  //
212
279
  //  |  (region) |        |       //
213
279
  //  \_ExitingBB_/   ExitingBlock //
214
279
  //        |              |       //
215
279
  //    MergeBlock---------/       //
216
279
  //        |                      //
217
279
  //      ExitBB                   //
218
279
  //      /    \                   //
219
279
220
279
  return std::make_pair(std::make_pair(StartBlock, ExitingBlock), CondBr);
221
279
}