Coverage Report

Created: 2017-10-03 07:32

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