Coverage Report

Created: 2018-10-23 03:11

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