Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/SafeStackColoring.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SafeStackColoring.cpp - SafeStack frame coloring -------------------===//
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
#include "SafeStackColoring.h"
10
#include "llvm/ADT/BitVector.h"
11
#include "llvm/ADT/DenseMap.h"
12
#include "llvm/ADT/DepthFirstIterator.h"
13
#include "llvm/ADT/SmallVector.h"
14
#include "llvm/Config/llvm-config.h"
15
#include "llvm/IR/BasicBlock.h"
16
#include "llvm/IR/CFG.h"
17
#include "llvm/IR/Instruction.h"
18
#include "llvm/IR/Instructions.h"
19
#include "llvm/IR/IntrinsicInst.h"
20
#include "llvm/IR/Intrinsics.h"
21
#include "llvm/IR/User.h"
22
#include "llvm/Support/Casting.h"
23
#include "llvm/Support/CommandLine.h"
24
#include "llvm/Support/Compiler.h"
25
#include "llvm/Support/Debug.h"
26
#include "llvm/Support/raw_ostream.h"
27
#include <cassert>
28
#include <tuple>
29
#include <utility>
30
31
using namespace llvm;
32
using namespace llvm::safestack;
33
34
#define DEBUG_TYPE "safestackcoloring"
35
36
// Disabled by default due to PR32143.
37
static cl::opt<bool> ClColoring("safe-stack-coloring",
38
                                cl::desc("enable safe stack coloring"),
39
                                cl::Hidden, cl::init(false));
40
41
232
const StackColoring::LiveRange &StackColoring::getLiveRange(AllocaInst *AI) {
42
232
  const auto IT = AllocaNumbering.find(AI);
43
232
  assert(IT != AllocaNumbering.end());
44
232
  return LiveRanges[IT->second];
45
232
}
46
47
448
bool StackColoring::readMarker(Instruction *I, bool *IsStart) {
48
448
  if (!I->isLifetimeStartOrEnd())
49
268
    return false;
50
180
51
180
  auto *II = cast<IntrinsicInst>(I);
52
180
  *IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
53
180
  return true;
54
180
}
55
56
152
void StackColoring::removeAllMarkers() {
57
180
  for (auto *I : Markers) {
58
180
    auto *Op = dyn_cast<Instruction>(I->getOperand(1));
59
180
    I->eraseFromParent();
60
180
    // Remove the operand bitcast, too, if it has no more uses left.
61
180
    if (Op && Op->use_empty())
62
78
      Op->eraseFromParent();
63
180
  }
64
152
}
65
66
152
void StackColoring::collectMarkers() {
67
152
  InterestingAllocas.resize(NumAllocas);
68
152
  DenseMap<BasicBlock *, SmallDenseMap<Instruction *, Marker>> BBMarkerSet;
69
152
70
152
  // Compute the set of start/end markers per basic block.
71
384
  for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; 
++AllocaNo232
) {
72
232
    AllocaInst *AI = Allocas[AllocaNo];
73
232
    SmallVector<Instruction *, 8> WorkList;
74
232
    WorkList.push_back(AI);
75
560
    while (!WorkList.empty()) {
76
328
      Instruction *I = WorkList.pop_back_val();
77
544
      for (User *U : I->users()) {
78
544
        if (auto *BI = dyn_cast<BitCastInst>(U)) {
79
96
          WorkList.push_back(BI);
80
96
          continue;
81
96
        }
82
448
        auto *UI = dyn_cast<Instruction>(U);
83
448
        if (!UI)
84
0
          continue;
85
448
        bool IsStart;
86
448
        if (!readMarker(UI, &IsStart))
87
268
          continue;
88
180
        if (IsStart)
89
95
          InterestingAllocas.set(AllocaNo);
90
180
        BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
91
180
        Markers.push_back(UI);
92
180
      }
93
328
    }
94
232
  }
95
152
96
152
  // Compute instruction numbering. Only the following instructions are
97
152
  // considered:
98
152
  // * Basic block entries
99
152
  // * Lifetime markers
100
152
  // For each basic block, compute
101
152
  // * the list of markers in the instruction order
102
152
  // * the sets of allocas whose lifetime starts or ends in this BB
103
152
  LLVM_DEBUG(dbgs() << "Instructions:\n");
104
152
  unsigned InstNo = 0;
105
257
  for (BasicBlock *BB : depth_first(&F)) {
106
257
    LLVM_DEBUG(dbgs() << "  " << InstNo << ": BB " << BB->getName() << "\n");
107
257
    unsigned BBStart = InstNo++;
108
257
109
257
    BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
110
257
    BlockInfo.Begin.resize(NumAllocas);
111
257
    BlockInfo.End.resize(NumAllocas);
112
257
    BlockInfo.LiveIn.resize(NumAllocas);
113
257
    BlockInfo.LiveOut.resize(NumAllocas);
114
257
115
257
    auto &BlockMarkerSet = BBMarkerSet[BB];
116
257
    if (BlockMarkerSet.empty()) {
117
187
      unsigned BBEnd = InstNo;
118
187
      BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
119
187
      continue;
120
187
    }
121
70
122
180
    
auto ProcessMarker = [&](Instruction *I, const Marker &M) 70
{
123
180
      LLVM_DEBUG(dbgs() << "  " << InstNo << ":  "
124
180
                        << (M.IsStart ? "start " : "end   ") << M.AllocaNo
125
180
                        << ", " << *I << "\n");
126
180
127
180
      BBMarkers[BB].push_back({InstNo, M});
128
180
129
180
      InstructionNumbering[I] = InstNo++;
130
180
131
180
      if (M.IsStart) {
132
95
        if (BlockInfo.End.test(M.AllocaNo))
133
2
          BlockInfo.End.reset(M.AllocaNo);
134
95
        BlockInfo.Begin.set(M.AllocaNo);
135
95
      } else {
136
85
        if (BlockInfo.Begin.test(M.AllocaNo))
137
67
          BlockInfo.Begin.reset(M.AllocaNo);
138
85
        BlockInfo.End.set(M.AllocaNo);
139
85
      }
140
180
    };
141
70
142
70
    if (BlockMarkerSet.size() == 1) {
143
26
      ProcessMarker(BlockMarkerSet.begin()->getFirst(),
144
26
                    BlockMarkerSet.begin()->getSecond());
145
44
    } else {
146
44
      // Scan the BB to determine the marker order.
147
459
      for (Instruction &I : *BB) {
148
459
        auto It = BlockMarkerSet.find(&I);
149
459
        if (It == BlockMarkerSet.end())
150
305
          continue;
151
154
        ProcessMarker(&I, It->getSecond());
152
154
      }
153
44
    }
154
70
155
70
    unsigned BBEnd = InstNo;
156
70
    BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
157
70
  }
158
152
  NumInst = InstNo;
159
152
}
160
161
37
void StackColoring::calculateLocalLiveness() {
162
37
  bool changed = true;
163
90
  while (changed) {
164
53
    changed = false;
165
53
166
138
    for (BasicBlock *BB : depth_first(&F)) {
167
138
      BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
168
138
169
138
      // Compute LiveIn by unioning together the LiveOut sets of all preds.
170
138
      BitVector LocalLiveIn;
171
138
      for (auto *PredBB : predecessors(BB)) {
172
106
        LivenessMap::const_iterator I = BlockLiveness.find(PredBB);
173
106
        // If a predecessor is unreachable, ignore it.
174
106
        if (I == BlockLiveness.end())
175
1
          continue;
176
105
        LocalLiveIn |= I->second.LiveOut;
177
105
      }
178
138
179
138
      // Compute LiveOut by subtracting out lifetimes that end in this
180
138
      // block, then adding in lifetimes that begin in this block.  If
181
138
      // we have both BEGIN and END markers in the same basic block
182
138
      // then we know that the BEGIN marker comes after the END,
183
138
      // because we already handle the case where the BEGIN comes
184
138
      // before the END when collecting the markers (and building the
185
138
      // BEGIN/END vectors).
186
138
      BitVector LocalLiveOut = LocalLiveIn;
187
138
      LocalLiveOut.reset(BlockInfo.End);
188
138
      LocalLiveOut |= BlockInfo.Begin;
189
138
190
138
      // Update block LiveIn set, noting whether it has changed.
191
138
      if (LocalLiveIn.test(BlockInfo.LiveIn)) {
192
30
        changed = true;
193
30
        BlockInfo.LiveIn |= LocalLiveIn;
194
30
      }
195
138
196
138
      // Update block LiveOut set, noting whether it has changed.
197
138
      if (LocalLiveOut.test(BlockInfo.LiveOut)) {
198
38
        changed = true;
199
38
        BlockInfo.LiveOut |= LocalLiveOut;
200
38
      }
201
138
    }
202
53
  } // while changed.
203
37
}
204
205
37
void StackColoring::calculateLiveIntervals() {
206
84
  for (auto IT : BlockLiveness) {
207
84
    BasicBlock *BB = IT.getFirst();
208
84
    BlockLifetimeInfo &BlockInfo = IT.getSecond();
209
84
    unsigned BBStart, BBEnd;
210
84
    std::tie(BBStart, BBEnd) = BlockInstRange[BB];
211
84
212
84
    BitVector Started, Ended;
213
84
    Started.resize(NumAllocas);
214
84
    Ended.resize(NumAllocas);
215
84
    SmallVector<unsigned, 8> Start;
216
84
    Start.resize(NumAllocas);
217
84
218
84
    // LiveIn ranges start at the first instruction.
219
379
    for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; 
++AllocaNo295
) {
220
295
      if (BlockInfo.LiveIn.test(AllocaNo)) {
221
60
        Started.set(AllocaNo);
222
60
        Start[AllocaNo] = BBStart;
223
60
      }
224
295
    }
225
84
226
180
    for (auto &It : BBMarkers[BB]) {
227
180
      unsigned InstNo = It.first;
228
180
      bool IsStart = It.second.IsStart;
229
180
      unsigned AllocaNo = It.second.AllocaNo;
230
180
231
180
      if (IsStart) {
232
95
        assert(!Started.test(AllocaNo) || Start[AllocaNo] == BBStart);
233
95
        if (!Started.test(AllocaNo)) {
234
93
          Started.set(AllocaNo);
235
93
          Ended.reset(AllocaNo);
236
93
          Start[AllocaNo] = InstNo;
237
93
        }
238
95
      } else {
239
85
        assert(!Ended.test(AllocaNo));
240
85
        if (Started.test(AllocaNo)) {
241
83
          LiveRanges[AllocaNo].AddRange(Start[AllocaNo], InstNo);
242
83
          Started.reset(AllocaNo);
243
83
        }
244
85
        Ended.set(AllocaNo);
245
85
      }
246
180
    }
247
84
248
379
    for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; 
++AllocaNo295
)
249
295
      if (Started.test(AllocaNo))
250
70
        LiveRanges[AllocaNo].AddRange(Start[AllocaNo], BBEnd);
251
84
  }
252
37
}
253
254
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
255
LLVM_DUMP_METHOD void StackColoring::dumpAllocas() {
256
  dbgs() << "Allocas:\n";
257
  for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
258
    dbgs() << "  " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n";
259
}
260
261
LLVM_DUMP_METHOD void StackColoring::dumpBlockLiveness() {
262
  dbgs() << "Block liveness:\n";
263
  for (auto IT : BlockLiveness) {
264
    BasicBlock *BB = IT.getFirst();
265
    BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
266
    auto BlockRange = BlockInstRange[BB];
267
    dbgs() << "  BB [" << BlockRange.first << ", " << BlockRange.second
268
           << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End
269
           << ", livein " << BlockInfo.LiveIn << ", liveout "
270
           << BlockInfo.LiveOut << "\n";
271
  }
272
}
273
274
LLVM_DUMP_METHOD void StackColoring::dumpLiveRanges() {
275
  dbgs() << "Alloca liveness:\n";
276
  for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
277
    LiveRange &Range = LiveRanges[AllocaNo];
278
    dbgs() << "  " << AllocaNo << ": " << Range << "\n";
279
  }
280
}
281
#endif
282
283
152
void StackColoring::run() {
284
152
  LLVM_DEBUG(dumpAllocas());
285
152
286
384
  for (unsigned I = 0; I < NumAllocas; 
++I232
)
287
232
    AllocaNumbering[Allocas[I]] = I;
288
152
  LiveRanges.resize(NumAllocas);
289
152
290
152
  collectMarkers();
291
152
292
152
  if (!ClColoring) {
293
136
    for (auto &R : LiveRanges) {
294
136
      R.SetMaximum(1);
295
136
      R.AddRange(0, 1);
296
136
    }
297
115
    return;
298
115
  }
299
37
300
37
  for (auto &R : LiveRanges)
301
96
    R.SetMaximum(NumInst);
302
133
  for (unsigned I = 0; I < NumAllocas; 
++I96
)
303
96
    if (!InterestingAllocas.test(I))
304
5
      LiveRanges[I] = getFullLiveRange();
305
37
306
37
  calculateLocalLiveness();
307
37
  LLVM_DEBUG(dumpBlockLiveness());
308
37
  calculateLiveIntervals();
309
37
  LLVM_DEBUG(dumpLiveRanges());
310
37
}