Coverage Report

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