Coverage Report

Created: 2019-04-21 11:35

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