Coverage Report

Created: 2018-07-12 09:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/ValueLattice.h
Line
Count
Source (jump to first uncovered line)
1
//===- ValueLattice.h - Value constraint analysis ---------------*- 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
#ifndef LLVM_ANALYSIS_VALUELATTICE_H
11
#define LLVM_ANALYSIS_VALUELATTICE_H
12
13
#include "llvm/IR/ConstantRange.h"
14
#include "llvm/IR/Constants.h"
15
//
16
//===----------------------------------------------------------------------===//
17
//                               ValueLatticeElement
18
//===----------------------------------------------------------------------===//
19
20
/// This class represents lattice values for constants.
21
///
22
/// FIXME: This is basically just for bringup, this can be made a lot more rich
23
/// in the future.
24
///
25
26
namespace llvm {
27
class ValueLatticeElement {
28
  enum ValueLatticeElementTy {
29
    /// This Value has no known value yet.  As a result, this implies the
30
    /// producing instruction is dead.  Caution: We use this as the starting
31
    /// state in our local meet rules.  In this usage, it's taken to mean
32
    /// "nothing known yet".
33
    undefined,
34
35
    /// This Value has a specific constant value.  (For constant integers,
36
    /// constantrange is used instead.  Integer typed constantexprs can appear
37
    /// as constant.)
38
    constant,
39
40
    /// This Value is known to not have the specified value.  (For constant
41
    /// integers, constantrange is used instead.  As above, integer typed
42
    /// constantexprs can appear here.)
43
    notconstant,
44
45
    /// The Value falls within this range. (Used only for integer typed values.)
46
    constantrange,
47
48
    /// We can not precisely model the dynamic values this value might take.
49
    overdefined
50
  };
51
52
  ValueLatticeElementTy Tag;
53
54
  /// The union either stores a pointer to a constant or a constant range,
55
  /// associated to the lattice element. We have to ensure that Range is
56
  /// initialized or destroyed when changing state to or from constantrange.
57
  union {
58
    Constant *ConstVal;
59
    ConstantRange Range;
60
  };
61
62
public:
63
  // Const and Range are initialized on-demand.
64
273M
  ValueLatticeElement() : Tag(undefined) {}
65
66
  /// Custom destructor to ensure Range is properly destroyed, when the object
67
  /// is deallocated.
68
422M
  ~ValueLatticeElement() {
69
422M
    switch (Tag) {
70
422M
    case overdefined:
71
398M
    case undefined:
72
398M
    case constant:
73
398M
    case notconstant:
74
398M
      break;
75
398M
    case constantrange:
76
23.9M
      Range.~ConstantRange();
77
23.9M
      break;
78
422M
    };
79
422M
  }
80
81
  /// Custom copy constructor, to ensure Range gets initialized when
82
  /// copying a constant range lattice element.
83
149M
  ValueLatticeElement(const ValueLatticeElement &Other) : Tag(undefined) {
84
149M
    *this = Other;
85
149M
  }
86
87
  /// Custom assignment operator, to ensure Range gets initialized when
88
  /// assigning a constant range lattice element.
89
304M
  ValueLatticeElement &operator=(const ValueLatticeElement &Other) {
90
304M
    // If we change the state of this from constant range to non constant range,
91
304M
    // destroy Range.
92
304M
    if (isConstantRange() && 
!Other.isConstantRange()61.9k
)
93
1
      Range.~ConstantRange();
94
304M
95
304M
    // If we change the state of this from a valid ConstVal to another a state
96
304M
    // without a valid ConstVal, zero the pointer.
97
304M
    if ((isConstant() || 
isNotConstant()304M
) &&
!Other.isConstant()12.5k
&&
98
304M
        
!Other.isNotConstant()7.25k
)
99
0
      ConstVal = nullptr;
100
304M
101
304M
    switch (Other.Tag) {
102
304M
    case constantrange:
103
20.4M
      if (!isConstantRange())
104
20.4M
        new (&Range) ConstantRange(Other.Range);
105
61.9k
      else
106
61.9k
        Range = Other.Range;
107
20.4M
      break;
108
304M
    case constant:
109
40.8M
    case notconstant:
110
40.8M
      ConstVal = Other.ConstVal;
111
40.8M
      break;
112
242M
    case overdefined:
113
242M
    case undefined:
114
242M
      break;
115
304M
    }
116
304M
    Tag = Other.Tag;
117
304M
    return *this;
118
304M
  }
119
120
908k
  static ValueLatticeElement get(Constant *C) {
121
908k
    ValueLatticeElement Res;
122
908k
    if (!isa<UndefValue>(C))
123
905k
      Res.markConstant(C);
124
908k
    return Res;
125
908k
  }
126
3.76M
  static ValueLatticeElement getNot(Constant *C) {
127
3.76M
    ValueLatticeElement Res;
128
3.76M
    if (!isa<UndefValue>(C))
129
3.76M
      Res.markNotConstant(C);
130
3.76M
    return Res;
131
3.76M
  }
132
2.55M
  static ValueLatticeElement getRange(ConstantRange CR) {
133
2.55M
    ValueLatticeElement Res;
134
2.55M
    Res.markConstantRange(std::move(CR));
135
2.55M
    return Res;
136
2.55M
  }
137
103M
  static ValueLatticeElement getOverdefined() {
138
103M
    ValueLatticeElement Res;
139
103M
    Res.markOverdefined();
140
103M
    return Res;
141
103M
  }
142
143
70.0M
  bool isUndefined() const { return Tag == undefined; }
144
471M
  bool isConstant() const { return Tag == constant; }
145
427M
  bool isNotConstant() const { return Tag == notconstant; }
146
496M
  bool isConstantRange() const { return Tag == constantrange; }
147
254M
  bool isOverdefined() const { return Tag == overdefined; }
148
149
53.2k
  Constant *getConstant() const {
150
53.2k
    assert(isConstant() && "Cannot get the constant of a non-constant!");
151
53.2k
    return ConstVal;
152
53.2k
  }
153
154
3.93M
  Constant *getNotConstant() const {
155
3.93M
    assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
156
3.93M
    return ConstVal;
157
3.93M
  }
158
159
8.65M
  const ConstantRange &getConstantRange() const {
160
8.65M
    assert(isConstantRange() &&
161
8.65M
           "Cannot get the constant-range of a non-constant-range!");
162
8.65M
    return Range;
163
8.65M
  }
164
165
2.43M
  Optional<APInt> asConstantInteger() const {
166
2.43M
    if (isConstant() && 
isa<ConstantInt>(getConstant())428
) {
167
0
      return cast<ConstantInt>(getConstant())->getValue();
168
2.43M
    } else if (isConstantRange() && 
getConstantRange().isSingleElement()152k
) {
169
29.6k
      return *getConstantRange().getSingleElement();
170
29.6k
    }
171
2.40M
    return None;
172
2.40M
  }
173
174
private:
175
109M
  void markOverdefined() {
176
109M
    if (isOverdefined())
177
0
      return;
178
109M
    if (isConstant() || 
isNotConstant()109M
)
179
275k
      ConstVal = nullptr;
180
109M
    if (isConstantRange())
181
266k
      Range.~ConstantRange();
182
109M
    Tag = overdefined;
183
109M
  }
184
185
905k
  void markConstant(Constant *V) {
186
905k
    assert(V && "Marking constant with NULL");
187
905k
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
188
803k
      markConstantRange(ConstantRange(CI->getValue()));
189
803k
      return;
190
803k
    }
191
102k
    if (isa<UndefValue>(V))
192
0
      return;
193
102k
194
102k
    assert((!isConstant() || getConstant() == V) &&
195
102k
           "Marking constant with different value");
196
102k
    assert(isUndefined());
197
102k
    Tag = constant;
198
102k
    ConstVal = V;
199
102k
  }
200
201
3.76M
  void markNotConstant(Constant *V) {
202
3.76M
    assert(V && "Marking constant with NULL");
203
3.76M
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
204
457k
      markConstantRange(ConstantRange(CI->getValue() + 1, CI->getValue()));
205
457k
      return;
206
457k
    }
207
3.30M
    if (isa<UndefValue>(V))
208
0
      return;
209
3.30M
210
3.30M
    assert((!isConstant() || getConstant() != V) &&
211
3.30M
           "Marking constant !constant with same value");
212
3.30M
    assert((!isNotConstant() || getNotConstant() == V) &&
213
3.30M
           "Marking !constant with different value");
214
3.30M
    assert(isUndefined() || isConstant());
215
3.30M
    Tag = notconstant;
216
3.30M
    ConstVal = V;
217
3.30M
  }
218
219
4.04M
  void markConstantRange(ConstantRange NewR) {
220
4.04M
    if (isConstantRange()) {
221
228k
      if (NewR.isEmptySet())
222
0
        markOverdefined();
223
228k
      else {
224
228k
        Range = std::move(NewR);
225
228k
      }
226
228k
      return;
227
228k
    }
228
3.81M
229
3.81M
    assert(isUndefined());
230
3.81M
    if (NewR.isEmptySet())
231
36
      markOverdefined();
232
3.81M
    else {
233
3.81M
      Tag = constantrange;
234
3.81M
      new (&Range) ConstantRange(std::move(NewR));
235
3.81M
    }
236
3.81M
  }
237
238
public:
239
  /// Updates this object to approximate both this object and RHS. Returns
240
  /// true if this object has been changed.
241
15.2M
  bool mergeIn(const ValueLatticeElement &RHS, const DataLayout &DL) {
242
15.2M
    if (RHS.isUndefined() || 
isOverdefined()15.0M
)
243
589k
      return false;
244
14.6M
    if (RHS.isOverdefined()) {
245
6.56M
      markOverdefined();
246
6.56M
      return true;
247
6.56M
    }
248
8.11M
249
8.11M
    if (isUndefined()) {
250
5.21M
      *this = RHS;
251
5.21M
      return !RHS.isUndefined();
252
5.21M
    }
253
2.89M
254
2.89M
    if (isConstant()) {
255
28.7k
      if (RHS.isConstant() && 
getConstant() == RHS.getConstant()16.6k
)
256
13.0k
        return false;
257
15.7k
      markOverdefined();
258
15.7k
      return true;
259
15.7k
    }
260
2.87M
261
2.87M
    if (isNotConstant()) {
262
1.93M
      if (RHS.isNotConstant() && 
getNotConstant() == RHS.getNotConstant()1.92M
)
263
1.92M
        return false;
264
8.98k
      markOverdefined();
265
8.98k
      return true;
266
8.98k
    }
267
932k
268
932k
    assert(isConstantRange() && "New ValueLattice type?");
269
932k
    if (!RHS.isConstantRange()) {
270
0
      // We can get here if we've encountered a constantexpr of integer type
271
0
      // and merge it with a constantrange.
272
0
      markOverdefined();
273
0
      return true;
274
0
    }
275
932k
    ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
276
932k
    if (NewR.isFullSet())
277
122k
      markOverdefined();
278
809k
    else if (NewR == getConstantRange())
279
581k
      return false;
280
228k
    else
281
228k
      markConstantRange(std::move(NewR));
282
932k
    
return true350k
;
283
932k
  }
284
285
  ConstantInt *getConstantInt() const {
286
    assert(isConstant() && isa<ConstantInt>(getConstant()) &&
287
           "No integer constant");
288
    return cast<ConstantInt>(getConstant());
289
  }
290
291
  /// Compares this symbolic value with Other using Pred and returns either
292
  /// true, false or undef constants, or nullptr if the comparison cannot be
293
  /// evaluated.
294
  Constant *getCompare(CmpInst::Predicate Pred, Type *Ty,
295
293
                       const ValueLatticeElement &Other) const {
296
293
    if (isUndefined() || 
Other.isUndefined()280
)
297
14
      return UndefValue::get(Ty);
298
279
299
279
    if (isConstant() && 
Other.isConstant()6
)
300
6
      return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());
301
273
302
273
    // Integer constants are represented as ConstantRanges with single
303
273
    // elements.
304
273
    if (!isConstantRange() || 
!Other.isConstantRange()226
)
305
60
      return nullptr;
306
213
307
213
    const auto &CR = getConstantRange();
308
213
    const auto &OtherCR = Other.getConstantRange();
309
213
    if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR))
310
13
      return ConstantInt::getTrue(Ty);
311
200
    if (ConstantRange::makeSatisfyingICmpRegion(
312
200
            CmpInst::getInversePredicate(Pred), OtherCR)
313
200
            .contains(CR))
314
17
      return ConstantInt::getFalse(Ty);
315
183
316
183
    return nullptr;
317
183
  }
318
};
319
320
raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val);
321
322
} // end namespace llvm
323
#endif