Coverage Report

Created: 2018-11-13 17:19

/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
251M
  ValueLatticeElement() : Tag(undefined) {}
65
66
  /// Custom destructor to ensure Range is properly destroyed, when the object
67
  /// is deallocated.
68
323M
  ~ValueLatticeElement() {
69
323M
    switch (Tag) {
70
323M
    case overdefined:
71
297M
    case undefined:
72
297M
    case constant:
73
297M
    case notconstant:
74
297M
      break;
75
297M
    case constantrange:
76
26.2M
      Range.~ConstantRange();
77
26.2M
      break;
78
323M
    };
79
323M
  }
80
81
  /// Custom copy constructor, to ensure Range gets initialized when
82
  /// copying a constant range lattice element.
83
71.5M
  ValueLatticeElement(const ValueLatticeElement &Other) : Tag(undefined) {
84
71.5M
    *this = Other;
85
71.5M
  }
86
87
  /// Custom assignment operator, to ensure Range gets initialized when
88
  /// assigning a constant range lattice element.
89
214M
  ValueLatticeElement &operator=(const ValueLatticeElement &Other) {
90
214M
    // If we change the state of this from constant range to non constant range,
91
214M
    // destroy Range.
92
214M
    if (isConstantRange() && 
!Other.isConstantRange()65.7k
)
93
1
      Range.~ConstantRange();
94
214M
95
214M
    // If we change the state of this from a valid ConstVal to another a state
96
214M
    // without a valid ConstVal, zero the pointer.
97
214M
    if ((isConstant() || 
isNotConstant()214M
) &&
!Other.isConstant()13.1k
&&
98
214M
        
!Other.isNotConstant()7.81k
)
99
0
      ConstVal = nullptr;
100
214M
101
214M
    switch (Other.Tag) {
102
214M
    case constantrange:
103
20.9M
      if (!isConstantRange())
104
20.9M
        new (&Range) ConstantRange(Other.Range);
105
65.7k
      else
106
65.7k
        Range = Other.Range;
107
20.9M
      break;
108
214M
    case constant:
109
44.5M
    case notconstant:
110
44.5M
      ConstVal = Other.ConstVal;
111
44.5M
      break;
112
148M
    case overdefined:
113
148M
    case undefined:
114
148M
      break;
115
214M
    }
116
214M
    Tag = Other.Tag;
117
214M
    return *this;
118
214M
  }
119
120
3.19M
  static ValueLatticeElement get(Constant *C) {
121
3.19M
    ValueLatticeElement Res;
122
3.19M
    if (!isa<UndefValue>(C))
123
3.18M
      Res.markConstant(C);
124
3.19M
    return Res;
125
3.19M
  }
126
4.00M
  static ValueLatticeElement getNot(Constant *C) {
127
4.00M
    ValueLatticeElement Res;
128
4.00M
    if (!isa<UndefValue>(C))
129
4.00M
      Res.markNotConstant(C);
130
4.00M
    return Res;
131
4.00M
  }
132
2.63M
  static ValueLatticeElement getRange(ConstantRange CR) {
133
2.63M
    ValueLatticeElement Res;
134
2.63M
    Res.markConstantRange(std::move(CR));
135
2.63M
    return Res;
136
2.63M
  }
137
90.5M
  static ValueLatticeElement getOverdefined() {
138
90.5M
    ValueLatticeElement Res;
139
90.5M
    Res.markOverdefined();
140
90.5M
    return Res;
141
90.5M
  }
142
143
79.3M
  bool isUndefined() const { return Tag == undefined; }
144
375M
  bool isConstant() const { return Tag == constant; }
145
325M
  bool isNotConstant() const { return Tag == notconstant; }
146
401M
  bool isConstantRange() const { return Tag == constantrange; }
147
232M
  bool isOverdefined() const { return Tag == overdefined; }
148
149
58.5k
  Constant *getConstant() const {
150
58.5k
    assert(isConstant() && "Cannot get the constant of a non-constant!");
151
58.5k
    return ConstVal;
152
58.5k
  }
153
154
4.27M
  Constant *getNotConstant() const {
155
4.27M
    assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
156
4.27M
    return ConstVal;
157
4.27M
  }
158
159
9.05M
  const ConstantRange &getConstantRange() const {
160
9.05M
    assert(isConstantRange() &&
161
9.05M
           "Cannot get the constant-range of a non-constant-range!");
162
9.05M
    return Range;
163
9.05M
  }
164
165
2.45M
  Optional<APInt> asConstantInteger() const {
166
2.45M
    if (isConstant() && 
isa<ConstantInt>(getConstant())421
) {
167
0
      return cast<ConstantInt>(getConstant())->getValue();
168
2.45M
    } else if (isConstantRange() && 
getConstantRange().isSingleElement()116k
) {
169
778
      return *getConstantRange().getSingleElement();
170
778
    }
171
2.45M
    return None;
172
2.45M
  }
173
174
private:
175
97.4M
  void markOverdefined() {
176
97.4M
    if (isOverdefined())
177
0
      return;
178
97.4M
    if (isConstant() || 
isNotConstant()97.4M
)
179
294k
      ConstVal = nullptr;
180
97.4M
    if (isConstantRange())
181
270k
      Range.~ConstantRange();
182
97.4M
    Tag = overdefined;
183
97.4M
  }
184
185
3.18M
  void markConstant(Constant *V) {
186
3.18M
    assert(V && "Marking constant with NULL");
187
3.18M
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
188
2.52M
      markConstantRange(ConstantRange(CI->getValue()));
189
2.52M
      return;
190
2.52M
    }
191
664k
    if (isa<UndefValue>(V))
192
0
      return;
193
664k
194
664k
    assert((!isConstant() || getConstant() == V) &&
195
664k
           "Marking constant with different value");
196
664k
    assert(isUndefined());
197
664k
    Tag = constant;
198
664k
    ConstVal = V;
199
664k
  }
200
201
4.00M
  void markNotConstant(Constant *V) {
202
4.00M
    assert(V && "Marking constant with NULL");
203
4.00M
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
204
473k
      markConstantRange(ConstantRange(CI->getValue() + 1, CI->getValue()));
205
473k
      return;
206
473k
    }
207
3.53M
    if (isa<UndefValue>(V))
208
0
      return;
209
3.53M
210
3.53M
    assert((!isConstant() || getConstant() != V) &&
211
3.53M
           "Marking constant !constant with same value");
212
3.53M
    assert((!isNotConstant() || getNotConstant() == V) &&
213
3.53M
           "Marking !constant with different value");
214
3.53M
    assert(isUndefined() || isConstant());
215
3.53M
    Tag = notconstant;
216
3.53M
    ConstVal = V;
217
3.53M
  }
218
219
5.86M
  void markConstantRange(ConstantRange NewR) {
220
5.86M
    if (isConstantRange()) {
221
235k
      if (NewR.isEmptySet())
222
0
        markOverdefined();
223
235k
      else {
224
235k
        Range = std::move(NewR);
225
235k
      }
226
235k
      return;
227
235k
    }
228
5.62M
229
5.62M
    assert(isUndefined());
230
5.62M
    if (NewR.isEmptySet())
231
48
      markOverdefined();
232
5.62M
    else {
233
5.62M
      Tag = constantrange;
234
5.62M
      new (&Range) ConstantRange(std::move(NewR));
235
5.62M
    }
236
5.62M
  }
237
238
public:
239
  /// Updates this object to approximate both this object and RHS. Returns
240
  /// true if this object has been changed.
241
16.1M
  bool mergeIn(const ValueLatticeElement &RHS, const DataLayout &DL) {
242
16.1M
    if (RHS.isUndefined() || 
isOverdefined()15.9M
)
243
604k
      return false;
244
15.5M
    if (RHS.isOverdefined()) {
245
6.82M
      markOverdefined();
246
6.82M
      return true;
247
6.82M
    }
248
8.74M
249
8.74M
    if (isUndefined()) {
250
5.64M
      *this = RHS;
251
5.64M
      return !RHS.isUndefined();
252
5.64M
    }
253
3.09M
254
3.09M
    if (isConstant()) {
255
27.9k
      if (RHS.isConstant() && 
getConstant() == RHS.getConstant()15.8k
)
256
12.1k
        return false;
257
15.8k
      markOverdefined();
258
15.8k
      return true;
259
15.8k
    }
260
3.07M
261
3.07M
    if (isNotConstant()) {
262
2.11M
      if (RHS.isNotConstant() && 
getNotConstant() == RHS.getNotConstant()2.10M
)
263
2.10M
        return false;
264
9.61k
      markOverdefined();
265
9.61k
      return true;
266
9.61k
    }
267
956k
268
956k
    assert(isConstantRange() && "New ValueLattice type?");
269
956k
    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
956k
    ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
276
956k
    if (NewR.isFullSet())
277
123k
      markOverdefined();
278
832k
    else if (NewR == getConstantRange())
279
596k
      return false;
280
235k
    else
281
235k
      markConstantRange(std::move(NewR));
282
956k
    
return true359k
;
283
956k
  }
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
2.71M
                       const ValueLatticeElement &Other) const {
296
2.71M
    if (isUndefined() || 
Other.isUndefined()2.65M
)
297
94.2k
      return UndefValue::get(Ty);
298
2.62M
299
2.62M
    if (isConstant() && 
Other.isConstant()6.59k
)
300
6.09k
      return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());
301
2.61M
302
2.61M
    // Integer constants are represented as ConstantRanges with single
303
2.61M
    // elements.
304
2.61M
    if (!isConstantRange() || 
!Other.isConstantRange()270k
)
305
2.51M
      return nullptr;
306
104k
307
104k
    const auto &CR = getConstantRange();
308
104k
    const auto &OtherCR = Other.getConstantRange();
309
104k
    if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR))
310
53.9k
      return ConstantInt::getTrue(Ty);
311
50.1k
    if (ConstantRange::makeSatisfyingICmpRegion(
312
50.1k
            CmpInst::getInversePredicate(Pred), OtherCR)
313
50.1k
            .contains(CR))
314
49.8k
      return ConstantInt::getFalse(Ty);
315
245
316
245
    return nullptr;
317
245
  }
318
};
319
320
raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val);
321
322
} // end namespace llvm
323
#endif