Coverage Report

Created: 2018-09-17 19:50

/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
250M
  ValueLatticeElement() : Tag(undefined) {}
65
66
  /// Custom destructor to ensure Range is properly destroyed, when the object
67
  /// is deallocated.
68
322M
  ~ValueLatticeElement() {
69
322M
    switch (Tag) {
70
322M
    case overdefined:
71
296M
    case undefined:
72
296M
    case constant:
73
296M
    case notconstant:
74
296M
      break;
75
296M
    case constantrange:
76
26.1M
      Range.~ConstantRange();
77
26.1M
      break;
78
322M
    };
79
322M
  }
80
81
  /// Custom copy constructor, to ensure Range gets initialized when
82
  /// copying a constant range lattice element.
83
71.2M
  ValueLatticeElement(const ValueLatticeElement &Other) : Tag(undefined) {
84
71.2M
    *this = Other;
85
71.2M
  }
86
87
  /// Custom assignment operator, to ensure Range gets initialized when
88
  /// assigning a constant range lattice element.
89
213M
  ValueLatticeElement &operator=(const ValueLatticeElement &Other) {
90
213M
    // If we change the state of this from constant range to non constant range,
91
213M
    // destroy Range.
92
213M
    if (isConstantRange() && 
!Other.isConstantRange()63.4k
)
93
1
      Range.~ConstantRange();
94
213M
95
213M
    // If we change the state of this from a valid ConstVal to another a state
96
213M
    // without a valid ConstVal, zero the pointer.
97
213M
    if ((isConstant() || 
isNotConstant()213M
) &&
!Other.isConstant()13.3k
&&
98
213M
        
!Other.isNotConstant()7.89k
)
99
0
      ConstVal = nullptr;
100
213M
101
213M
    switch (Other.Tag) {
102
213M
    case constantrange:
103
20.8M
      if (!isConstantRange())
104
20.7M
        new (&Range) ConstantRange(Other.Range);
105
63.4k
      else
106
63.4k
        Range = Other.Range;
107
20.8M
      break;
108
213M
    case constant:
109
44.2M
    case notconstant:
110
44.2M
      ConstVal = Other.ConstVal;
111
44.2M
      break;
112
148M
    case overdefined:
113
148M
    case undefined:
114
148M
      break;
115
213M
    }
116
213M
    Tag = Other.Tag;
117
213M
    return *this;
118
213M
  }
119
120
3.17M
  static ValueLatticeElement get(Constant *C) {
121
3.17M
    ValueLatticeElement Res;
122
3.17M
    if (!isa<UndefValue>(C))
123
3.17M
      Res.markConstant(C);
124
3.17M
    return Res;
125
3.17M
  }
126
3.99M
  static ValueLatticeElement getNot(Constant *C) {
127
3.99M
    ValueLatticeElement Res;
128
3.99M
    if (!isa<UndefValue>(C))
129
3.99M
      Res.markNotConstant(C);
130
3.99M
    return Res;
131
3.99M
  }
132
2.61M
  static ValueLatticeElement getRange(ConstantRange CR) {
133
2.61M
    ValueLatticeElement Res;
134
2.61M
    Res.markConstantRange(std::move(CR));
135
2.61M
    return Res;
136
2.61M
  }
137
90.2M
  static ValueLatticeElement getOverdefined() {
138
90.2M
    ValueLatticeElement Res;
139
90.2M
    Res.markOverdefined();
140
90.2M
    return Res;
141
90.2M
  }
142
143
79.0M
  bool isUndefined() const { return Tag == undefined; }
144
373M
  bool isConstant() const { return Tag == constant; }
145
324M
  bool isNotConstant() const { return Tag == notconstant; }
146
400M
  bool isConstantRange() const { return Tag == constantrange; }
147
231M
  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.23M
  Constant *getNotConstant() const {
155
4.23M
    assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
156
4.23M
    return ConstVal;
157
4.23M
  }
158
159
8.96M
  const ConstantRange &getConstantRange() const {
160
8.96M
    assert(isConstantRange() &&
161
8.96M
           "Cannot get the constant-range of a non-constant-range!");
162
8.96M
    return Range;
163
8.96M
  }
164
165
2.38M
  Optional<APInt> asConstantInteger() const {
166
2.38M
    if (isConstant() && 
isa<ConstantInt>(getConstant())441
) {
167
0
      return cast<ConstantInt>(getConstant())->getValue();
168
2.38M
    } else if (isConstantRange() && 
getConstantRange().isSingleElement()117k
) {
169
773
      return *getConstantRange().getSingleElement();
170
773
    }
171
2.38M
    return None;
172
2.38M
  }
173
174
private:
175
97.1M
  void markOverdefined() {
176
97.1M
    if (isOverdefined())
177
0
      return;
178
97.1M
    if (isConstant() || 
isNotConstant()97.1M
)
179
294k
      ConstVal = nullptr;
180
97.1M
    if (isConstantRange())
181
270k
      Range.~ConstantRange();
182
97.1M
    Tag = overdefined;
183
97.1M
  }
184
185
3.17M
  void markConstant(Constant *V) {
186
3.17M
    assert(V && "Marking constant with NULL");
187
3.17M
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
188
2.51M
      markConstantRange(ConstantRange(CI->getValue()));
189
2.51M
      return;
190
2.51M
    }
191
662k
    if (isa<UndefValue>(V))
192
0
      return;
193
662k
194
662k
    assert((!isConstant() || getConstant() == V) &&
195
662k
           "Marking constant with different value");
196
662k
    assert(isUndefined());
197
662k
    Tag = constant;
198
662k
    ConstVal = V;
199
662k
  }
200
201
3.99M
  void markNotConstant(Constant *V) {
202
3.99M
    assert(V && "Marking constant with NULL");
203
3.99M
    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
204
466k
      markConstantRange(ConstantRange(CI->getValue() + 1, CI->getValue()));
205
466k
      return;
206
466k
    }
207
3.52M
    if (isa<UndefValue>(V))
208
0
      return;
209
3.52M
210
3.52M
    assert((!isConstant() || getConstant() != V) &&
211
3.52M
           "Marking constant !constant with same value");
212
3.52M
    assert((!isNotConstant() || getNotConstant() == V) &&
213
3.52M
           "Marking !constant with different value");
214
3.52M
    assert(isUndefined() || isConstant());
215
3.52M
    Tag = notconstant;
216
3.52M
    ConstVal = V;
217
3.52M
  }
218
219
5.82M
  void markConstantRange(ConstantRange NewR) {
220
5.82M
    if (isConstantRange()) {
221
233k
      if (NewR.isEmptySet())
222
0
        markOverdefined();
223
233k
      else {
224
233k
        Range = std::move(NewR);
225
233k
      }
226
233k
      return;
227
233k
    }
228
5.59M
229
5.59M
    assert(isUndefined());
230
5.59M
    if (NewR.isEmptySet())
231
48
      markOverdefined();
232
5.59M
    else {
233
5.59M
      Tag = constantrange;
234
5.59M
      new (&Range) ConstantRange(std::move(NewR));
235
5.59M
    }
236
5.59M
  }
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
603k
      return false;
244
15.5M
    if (RHS.isOverdefined()) {
245
6.82M
      markOverdefined();
246
6.82M
      return true;
247
6.82M
    }
248
8.68M
249
8.68M
    if (isUndefined()) {
250
5.60M
      *this = RHS;
251
5.60M
      return !RHS.isUndefined();
252
5.60M
    }
253
3.07M
254
3.07M
    if (isConstant()) {
255
27.9k
      if (RHS.isConstant() && 
getConstant() == RHS.getConstant()15.8k
)
256
12.1k
        return false;
257
15.7k
      markOverdefined();
258
15.7k
      return true;
259
15.7k
    }
260
3.04M
261
3.04M
    if (isNotConstant()) {
262
2.09M
      if (RHS.isNotConstant() && 
getNotConstant() == RHS.getNotConstant()2.08M
)
263
2.08M
        return false;
264
8.98k
      markOverdefined();
265
8.98k
      return true;
266
8.98k
    }
267
949k
268
949k
    assert(isConstantRange() && "New ValueLattice type?");
269
949k
    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
949k
    ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
276
949k
    if (NewR.isFullSet())
277
123k
      markOverdefined();
278
825k
    else if (NewR == getConstantRange())
279
592k
      return false;
280
233k
    else
281
233k
      markConstantRange(std::move(NewR));
282
949k
    
return true357k
;
283
949k
  }
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.64M
)
297
94.4k
      return UndefValue::get(Ty);
298
2.61M
299
2.61M
    if (isConstant() && 
Other.isConstant()6.56k
)
300
6.06k
      return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());
301
2.60M
302
2.60M
    // Integer constants are represented as ConstantRanges with single
303
2.60M
    // elements.
304
2.60M
    if (!isConstantRange() || 
!Other.isConstantRange()270k
)
305
2.50M
      return nullptr;
306
103k
307
103k
    const auto &CR = getConstantRange();
308
103k
    const auto &OtherCR = Other.getConstantRange();
309
103k
    if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR))
310
53.8k
      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
246
316
246
    return nullptr;
317
246
  }
318
};
319
320
raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val);
321
322
} // end namespace llvm
323
#endif