Coverage Report

Created: 2018-02-20 12:54

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