Coverage Report

Created: 2017-04-27 19:33

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