Coverage Report

Created: 2018-07-19 03:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/ValueTracking.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/Analysis/ValueTracking.h - Walk computations --------*- 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 routines that help analyze properties that chains of
11
// computations have.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_ANALYSIS_VALUETRACKING_H
16
#define LLVM_ANALYSIS_VALUETRACKING_H
17
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/Optional.h"
20
#include "llvm/IR/CallSite.h"
21
#include "llvm/IR/Constants.h"
22
#include "llvm/IR/Instruction.h"
23
#include "llvm/IR/Intrinsics.h"
24
#include <cassert>
25
#include <cstdint>
26
27
namespace llvm {
28
29
class AddOperator;
30
class APInt;
31
class AssumptionCache;
32
class DataLayout;
33
class DominatorTree;
34
class GEPOperator;
35
class IntrinsicInst;
36
struct KnownBits;
37
class Loop;
38
class LoopInfo;
39
class MDNode;
40
class OptimizationRemarkEmitter;
41
class StringRef;
42
class TargetLibraryInfo;
43
class Value;
44
45
  /// Determine which bits of V are known to be either zero or one and return
46
  /// them in the KnownZero/KnownOne bit sets.
47
  ///
48
  /// This function is defined on values with integer type, values with pointer
49
  /// type, and vectors of integers.  In the case
50
  /// where V is a vector, the known zero and known one values are the
51
  /// same width as the vector element, and the bit is set only if it is true
52
  /// for all of the elements in the vector.
53
  void computeKnownBits(const Value *V, KnownBits &Known,
54
                        const DataLayout &DL, unsigned Depth = 0,
55
                        AssumptionCache *AC = nullptr,
56
                        const Instruction *CxtI = nullptr,
57
                        const DominatorTree *DT = nullptr,
58
                        OptimizationRemarkEmitter *ORE = nullptr);
59
60
  /// Returns the known bits rather than passing by reference.
61
  KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
62
                             unsigned Depth = 0, AssumptionCache *AC = nullptr,
63
                             const Instruction *CxtI = nullptr,
64
                             const DominatorTree *DT = nullptr,
65
                             OptimizationRemarkEmitter *ORE = nullptr);
66
67
  /// Compute known bits from the range metadata.
68
  /// \p KnownZero the set of bits that are known to be zero
69
  /// \p KnownOne the set of bits that are known to be one
70
  void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
71
                                         KnownBits &Known);
72
73
  /// Return true if LHS and RHS have no common bits set.
74
  bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
75
                           const DataLayout &DL,
76
                           AssumptionCache *AC = nullptr,
77
                           const Instruction *CxtI = nullptr,
78
                           const DominatorTree *DT = nullptr);
79
80
  /// Return true if the given value is known to have exactly one bit set when
81
  /// defined. For vectors return true if every element is known to be a power
82
  /// of two when defined. Supports values with integer or pointer type and
83
  /// vectors of integers. If 'OrZero' is set, then return true if the given
84
  /// value is either a power of two or zero.
85
  bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
86
                              bool OrZero = false, unsigned Depth = 0,
87
                              AssumptionCache *AC = nullptr,
88
                              const Instruction *CxtI = nullptr,
89
                              const DominatorTree *DT = nullptr);
90
91
  bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI);
92
93
  /// Return true if the given value is known to be non-zero when defined. For
94
  /// vectors, return true if every element is known to be non-zero when
95
  /// defined. For pointers, if the context instruction and dominator tree are
96
  /// specified, perform context-sensitive analysis and return true if the
97
  /// pointer couldn't possibly be null at the specified instruction.
98
  /// Supports values with integer or pointer type and vectors of integers.
99
  bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0,
100
                      AssumptionCache *AC = nullptr,
101
                      const Instruction *CxtI = nullptr,
102
                      const DominatorTree *DT = nullptr);
103
104
  /// Return true if the two given values are negation.
105
  bool isKnownNegation(const Value *X, const Value *Y);
106
107
  /// Returns true if the give value is known to be non-negative.
108
  bool isKnownNonNegative(const Value *V, const DataLayout &DL,
109
                          unsigned Depth = 0,
110
                          AssumptionCache *AC = nullptr,
111
                          const Instruction *CxtI = nullptr,
112
                          const DominatorTree *DT = nullptr);
113
114
  /// Returns true if the given value is known be positive (i.e. non-negative
115
  /// and non-zero).
116
  bool isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth = 0,
117
                       AssumptionCache *AC = nullptr,
118
                       const Instruction *CxtI = nullptr,
119
                       const DominatorTree *DT = nullptr);
120
121
  /// Returns true if the given value is known be negative (i.e. non-positive
122
  /// and non-zero).
123
  bool isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth = 0,
124
                       AssumptionCache *AC = nullptr,
125
                       const Instruction *CxtI = nullptr,
126
                       const DominatorTree *DT = nullptr);
127
128
  /// Return true if the given values are known to be non-equal when defined.
129
  /// Supports scalar integer types only.
130
  bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL,
131
                      AssumptionCache *AC = nullptr,
132
                      const Instruction *CxtI = nullptr,
133
                      const DominatorTree *DT = nullptr);
134
135
  /// Return true if 'V & Mask' is known to be zero. We use this predicate to
136
  /// simplify operations downstream. Mask is known to be zero for bits that V
137
  /// cannot have.
138
  ///
139
  /// This function is defined on values with integer type, values with pointer
140
  /// type, and vectors of integers.  In the case
141
  /// where V is a vector, the mask, known zero, and known one values are the
142
  /// same width as the vector element, and the bit is set only if it is true
143
  /// for all of the elements in the vector.
144
  bool MaskedValueIsZero(const Value *V, const APInt &Mask,
145
                         const DataLayout &DL,
146
                         unsigned Depth = 0, AssumptionCache *AC = nullptr,
147
                         const Instruction *CxtI = nullptr,
148
                         const DominatorTree *DT = nullptr);
149
150
  /// Return the number of times the sign bit of the register is replicated into
151
  /// the other bits. We know that at least 1 bit is always equal to the sign
152
  /// bit (itself), but other cases can give us information. For example,
153
  /// immediately after an "ashr X, 2", we know that the top 3 bits are all
154
  /// equal to each other, so we return 3. For vectors, return the number of
155
  /// sign bits for the vector element with the mininum number of known sign
156
  /// bits.
157
  unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL,
158
                              unsigned Depth = 0, AssumptionCache *AC = nullptr,
159
                              const Instruction *CxtI = nullptr,
160
                              const DominatorTree *DT = nullptr);
161
162
  /// This function computes the integer multiple of Base that equals V. If
163
  /// successful, it returns true and returns the multiple in Multiple. If
164
  /// unsuccessful, it returns false. Also, if V can be simplified to an
165
  /// integer, then the simplified V is returned in Val. Look through sext only
166
  /// if LookThroughSExt=true.
167
  bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
168
                       bool LookThroughSExt = false,
169
                       unsigned Depth = 0);
170
171
  /// Map a call instruction to an intrinsic ID.  Libcalls which have equivalent
172
  /// intrinsics are treated as-if they were intrinsics.
173
  Intrinsic::ID getIntrinsicForCallSite(ImmutableCallSite ICS,
174
                                        const TargetLibraryInfo *TLI);
175
176
  /// Return true if we can prove that the specified FP value is never equal to
177
  /// -0.0.
178
  bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
179
                            unsigned Depth = 0);
180
181
  /// Return true if we can prove that the specified FP value is either NaN or
182
  /// never less than -0.0.
183
  ///
184
  ///      NaN --> true
185
  ///       +0 --> true
186
  ///       -0 --> true
187
  ///   x > +0 --> true
188
  ///   x < -0 --> false
189
  bool CannotBeOrderedLessThanZero(const Value *V, const TargetLibraryInfo *TLI);
190
191
  /// Return true if the floating-point scalar value is not a NaN or if the
192
  /// floating-point vector value has no NaN elements. Return false if a value
193
  /// could ever be NaN.
194
  bool isKnownNeverNaN(const Value *V);
195
196
  /// Return true if we can prove that the specified FP value's sign bit is 0.
197
  ///
198
  ///      NaN --> true/false (depending on the NaN's sign bit)
199
  ///       +0 --> true
200
  ///       -0 --> false
201
  ///   x > +0 --> true
202
  ///   x < -0 --> false
203
  bool SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI);
204
205
  /// If the specified value can be set by repeating the same byte in memory,
206
  /// return the i8 value that it is represented with. This is true for all i8
207
  /// values obviously, but is also true for i32 0, i32 -1, i16 0xF0F0, double
208
  /// 0.0 etc. If the value can't be handled with a repeated byte store (e.g.
209
  /// i16 0x1234), return null.
210
  Value *isBytewiseValue(Value *V);
211
212
  /// Given an aggregrate and an sequence of indices, see if the scalar value
213
  /// indexed is already around as a register, for example if it were inserted
214
  /// directly into the aggregrate.
215
  ///
216
  /// If InsertBefore is not null, this function will duplicate (modified)
217
  /// insertvalues when a part of a nested struct is extracted.
218
  Value *FindInsertedValue(Value *V,
219
                           ArrayRef<unsigned> idx_range,
220
                           Instruction *InsertBefore = nullptr);
221
222
  /// Analyze the specified pointer to see if it can be expressed as a base
223
  /// pointer plus a constant offset. Return the base and offset to the caller.
224
  Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
225
                                          const DataLayout &DL);
226
  inline const Value *GetPointerBaseWithConstantOffset(const Value *Ptr,
227
                                                       int64_t &Offset,
228
147k
                                                       const DataLayout &DL) {
229
147k
    return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset,
230
147k
                                            DL);
231
147k
  }
232
233
  /// Returns true if the GEP is based on a pointer to a string (array of
234
  // \p CharSize integers) and is indexing into this string.
235
  bool isGEPBasedOnPointerToString(const GEPOperator *GEP,
236
                                   unsigned CharSize = 8);
237
238
  /// Represents offset+length into a ConstantDataArray.
239
  struct ConstantDataArraySlice {
240
    /// ConstantDataArray pointer. nullptr indicates a zeroinitializer (a valid
241
    /// initializer, it just doesn't fit the ConstantDataArray interface).
242
    const ConstantDataArray *Array;
243
244
    /// Slice starts at this Offset.
245
    uint64_t Offset;
246
247
    /// Length of the slice.
248
    uint64_t Length;
249
250
    /// Moves the Offset and adjusts Length accordingly.
251
1.69k
    void move(uint64_t Delta) {
252
1.69k
      assert(Delta < Length);
253
1.69k
      Offset += Delta;
254
1.69k
      Length -= Delta;
255
1.69k
    }
256
257
    /// Convenience accessor for elements in the slice.
258
7.03k
    uint64_t operator[](unsigned I) const {
259
7.03k
      return Array==nullptr ? 
00
: Array->getElementAsInteger(I + Offset);
260
7.03k
    }
261
  };
262
263
  /// Returns true if the value \p V is a pointer into a ConstantDataArray.
264
  /// If successful \p Slice will point to a ConstantDataArray info object
265
  /// with an appropriate offset.
266
  bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice,
267
                                unsigned ElementSize, uint64_t Offset = 0);
268
269
  /// This function computes the length of a null-terminated C string pointed to
270
  /// by V. If successful, it returns true and returns the string in Str. If
271
  /// unsuccessful, it returns false. This does not include the trailing null
272
  /// character by default. If TrimAtNul is set to false, then this returns any
273
  /// trailing null characters as well as any other characters that come after
274
  /// it.
275
  bool getConstantStringInfo(const Value *V, StringRef &Str,
276
                             uint64_t Offset = 0, bool TrimAtNul = true);
277
278
  /// If we can compute the length of the string pointed to by the specified
279
  /// pointer, return 'len+1'.  If we can't, return 0.
280
  uint64_t GetStringLength(const Value *V, unsigned CharSize = 8);
281
282
  /// This function returns call pointer argument that is considered the same by
283
  /// aliasing rules. You CAN'T use it to replace one value with another.
284
  const Value *getArgumentAliasingToReturnedPointer(ImmutableCallSite CS);
285
13.9M
  inline Value *getArgumentAliasingToReturnedPointer(CallSite CS) {
286
13.9M
    return const_cast<Value *>(
287
13.9M
        getArgumentAliasingToReturnedPointer(ImmutableCallSite(CS)));
288
13.9M
  }
289
290
  // {launder,strip}.invariant.group returns pointer that aliases its argument,
291
  // and it only captures pointer by returning it.
292
  // These intrinsics are not marked as nocapture, because returning is
293
  // considered as capture. The arguments are not marked as returned neither,
294
  // because it would make it useless.
295
  bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
296
      ImmutableCallSite CS);
297
298
  /// This method strips off any GEP address adjustments and pointer casts from
299
  /// the specified value, returning the original object being addressed. Note
300
  /// that the returned value has pointer type if the specified value does. If
301
  /// the MaxLookup value is non-zero, it limits the number of instructions to
302
  /// be stripped off.
303
  Value *GetUnderlyingObject(Value *V, const DataLayout &DL,
304
                             unsigned MaxLookup = 6);
305
  inline const Value *GetUnderlyingObject(const Value *V, const DataLayout &DL,
306
213M
                                          unsigned MaxLookup = 6) {
307
213M
    return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup);
308
213M
  }
309
310
  /// This method is similar to GetUnderlyingObject except that it can
311
  /// look through phi and select instructions and return multiple objects.
312
  ///
313
  /// If LoopInfo is passed, loop phis are further analyzed.  If a pointer
314
  /// accesses different objects in each iteration, we don't look through the
315
  /// phi node. E.g. consider this loop nest:
316
  ///
317
  ///   int **A;
318
  ///   for (i)
319
  ///     for (j) {
320
  ///        A[i][j] = A[i-1][j] * B[j]
321
  ///     }
322
  ///
323
  /// This is transformed by Load-PRE to stash away A[i] for the next iteration
324
  /// of the outer loop:
325
  ///
326
  ///   Curr = A[0];          // Prev_0
327
  ///   for (i: 1..N) {
328
  ///     Prev = Curr;        // Prev = PHI (Prev_0, Curr)
329
  ///     Curr = A[i];
330
  ///     for (j: 0..N) {
331
  ///        Curr[j] = Prev[j] * B[j]
332
  ///     }
333
  ///   }
334
  ///
335
  /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
336
  /// should not assume that Curr and Prev share the same underlying object thus
337
  /// it shouldn't look through the phi above.
338
  void GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
339
                            const DataLayout &DL, LoopInfo *LI = nullptr,
340
                            unsigned MaxLookup = 6);
341
342
  /// This is a wrapper around GetUnderlyingObjects and adds support for basic
343
  /// ptrtoint+arithmetic+inttoptr sequences.
344
  bool getUnderlyingObjectsForCodeGen(const Value *V,
345
                            SmallVectorImpl<Value *> &Objects,
346
                            const DataLayout &DL);
347
348
  /// Return true if the only users of this pointer are lifetime markers.
349
  bool onlyUsedByLifetimeMarkers(const Value *V);
350
351
  /// Return true if the instruction does not have any effects besides
352
  /// calculating the result and does not have undefined behavior.
353
  ///
354
  /// This method never returns true for an instruction that returns true for
355
  /// mayHaveSideEffects; however, this method also does some other checks in
356
  /// addition. It checks for undefined behavior, like dividing by zero or
357
  /// loading from an invalid pointer (but not for undefined results, like a
358
  /// shift with a shift amount larger than the width of the result). It checks
359
  /// for malloc and alloca because speculatively executing them might cause a
360
  /// memory leak. It also returns false for instructions related to control
361
  /// flow, specifically terminators and PHI nodes.
362
  ///
363
  /// If the CtxI is specified this method performs context-sensitive analysis
364
  /// and returns true if it is safe to execute the instruction immediately
365
  /// before the CtxI.
366
  ///
367
  /// If the CtxI is NOT specified this method only looks at the instruction
368
  /// itself and its operands, so if this method returns true, it is safe to
369
  /// move the instruction as long as the correct dominance relationships for
370
  /// the operands and users hold.
371
  ///
372
  /// This method can return true for instructions that read memory;
373
  /// for such instructions, moving them may change the resulting value.
374
  bool isSafeToSpeculativelyExecute(const Value *V,
375
                                    const Instruction *CtxI = nullptr,
376
                                    const DominatorTree *DT = nullptr);
377
378
  /// Returns true if the result or effects of the given instructions \p I
379
  /// depend on or influence global memory.
380
  /// Memory dependence arises for example if the instruction reads from
381
  /// memory or may produce effects or undefined behaviour. Memory dependent
382
  /// instructions generally cannot be reorderd with respect to other memory
383
  /// dependent instructions or moved into non-dominated basic blocks.
384
  /// Instructions which just compute a value based on the values of their
385
  /// operands are not memory dependent.
386
  bool mayBeMemoryDependent(const Instruction &I);
387
388
  /// Return true if it is an intrinsic that cannot be speculated but also
389
  /// cannot trap.
390
  bool isAssumeLikeIntrinsic(const Instruction *I);
391
392
  /// Return true if it is valid to use the assumptions provided by an
393
  /// assume intrinsic, I, at the point in the control-flow identified by the
394
  /// context instruction, CxtI.
395
  bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
396
                               const DominatorTree *DT = nullptr);
397
398
  enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
399
400
  OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
401
                                               const Value *RHS,
402
                                               const DataLayout &DL,
403
                                               AssumptionCache *AC,
404
                                               const Instruction *CxtI,
405
                                               const DominatorTree *DT);
406
  OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
407
                                             const DataLayout &DL,
408
                                             AssumptionCache *AC,
409
                                             const Instruction *CxtI,
410
                                             const DominatorTree *DT);
411
  OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
412
                                               const Value *RHS,
413
                                               const DataLayout &DL,
414
                                               AssumptionCache *AC,
415
                                               const Instruction *CxtI,
416
                                               const DominatorTree *DT);
417
  OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
418
                                             const DataLayout &DL,
419
                                             AssumptionCache *AC = nullptr,
420
                                             const Instruction *CxtI = nullptr,
421
                                             const DominatorTree *DT = nullptr);
422
  /// This version also leverages the sign bit of Add if known.
423
  OverflowResult computeOverflowForSignedAdd(const AddOperator *Add,
424
                                             const DataLayout &DL,
425
                                             AssumptionCache *AC = nullptr,
426
                                             const Instruction *CxtI = nullptr,
427
                                             const DominatorTree *DT = nullptr);
428
  OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS,
429
                                               const DataLayout &DL,
430
                                               AssumptionCache *AC,
431
                                               const Instruction *CxtI,
432
                                               const DominatorTree *DT);
433
  OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
434
                                             const DataLayout &DL,
435
                                             AssumptionCache *AC,
436
                                             const Instruction *CxtI,
437
                                             const DominatorTree *DT);
438
439
  /// Returns true if the arithmetic part of the \p II 's result is
440
  /// used only along the paths control dependent on the computation
441
  /// not overflowing, \p II being an <op>.with.overflow intrinsic.
442
  bool isOverflowIntrinsicNoWrap(const IntrinsicInst *II,
443
                                 const DominatorTree &DT);
444
445
  /// Return true if this function can prove that the instruction I will
446
  /// always transfer execution to one of its successors (including the next
447
  /// instruction that follows within a basic block). E.g. this is not
448
  /// guaranteed for function calls that could loop infinitely.
449
  ///
450
  /// In other words, this function returns false for instructions that may
451
  /// transfer execution or fail to transfer execution in a way that is not
452
  /// captured in the CFG nor in the sequence of instructions within a basic
453
  /// block.
454
  ///
455
  /// Undefined behavior is assumed not to happen, so e.g. division is
456
  /// guaranteed to transfer execution to the following instruction even
457
  /// though division by zero might cause undefined behavior.
458
  bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I);
459
460
  /// Returns true if this block does not contain a potential implicit exit.
461
  /// This is equivelent to saying that all instructions within the basic block
462
  /// are guaranteed to transfer execution to their successor within the basic
463
  /// block. This has the same assumptions w.r.t. undefined behavior as the
464
  /// instruction variant of this function. 
465
  bool isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB);
466
467
  /// Return true if this function can prove that the instruction I
468
  /// is executed for every iteration of the loop L.
469
  ///
470
  /// Note that this currently only considers the loop header.
471
  bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
472
                                              const Loop *L);
473
474
  /// Return true if this function can prove that I is guaranteed to yield
475
  /// full-poison (all bits poison) if at least one of its operands are
476
  /// full-poison (all bits poison).
477
  ///
478
  /// The exact rules for how poison propagates through instructions have
479
  /// not been settled as of 2015-07-10, so this function is conservative
480
  /// and only considers poison to be propagated in uncontroversial
481
  /// cases. There is no attempt to track values that may be only partially
482
  /// poison.
483
  bool propagatesFullPoison(const Instruction *I);
484
485
  /// Return either nullptr or an operand of I such that I will trigger
486
  /// undefined behavior if I is executed and that operand has a full-poison
487
  /// value (all bits poison).
488
  const Value *getGuaranteedNonFullPoisonOp(const Instruction *I);
489
490
  /// Return true if this function can prove that if PoisonI is executed
491
  /// and yields a full-poison value (all bits poison), then that will
492
  /// trigger undefined behavior.
493
  ///
494
  /// Note that this currently only considers the basic block that is
495
  /// the parent of I.
496
  bool programUndefinedIfFullPoison(const Instruction *PoisonI);
497
498
  /// Specific patterns of select instructions we can match.
499
  enum SelectPatternFlavor {
500
    SPF_UNKNOWN = 0,
501
    SPF_SMIN,                   /// Signed minimum
502
    SPF_UMIN,                   /// Unsigned minimum
503
    SPF_SMAX,                   /// Signed maximum
504
    SPF_UMAX,                   /// Unsigned maximum
505
    SPF_FMINNUM,                /// Floating point minnum
506
    SPF_FMAXNUM,                /// Floating point maxnum
507
    SPF_ABS,                    /// Absolute value
508
    SPF_NABS                    /// Negated absolute value
509
  };
510
511
  /// Behavior when a floating point min/max is given one NaN and one
512
  /// non-NaN as input.
513
  enum SelectPatternNaNBehavior {
514
    SPNB_NA = 0,                /// NaN behavior not applicable.
515
    SPNB_RETURNS_NAN,           /// Given one NaN input, returns the NaN.
516
    SPNB_RETURNS_OTHER,         /// Given one NaN input, returns the non-NaN.
517
    SPNB_RETURNS_ANY            /// Given one NaN input, can return either (or
518
                                /// it has been determined that no operands can
519
                                /// be NaN).
520
  };
521
522
  struct SelectPatternResult {
523
    SelectPatternFlavor Flavor;
524
    SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is
525
                                          /// SPF_FMINNUM or SPF_FMAXNUM.
526
    bool Ordered;               /// When implementing this min/max pattern as
527
                                /// fcmp; select, does the fcmp have to be
528
                                /// ordered?
529
530
    /// Return true if \p SPF is a min or a max pattern.
531
18.5M
    static bool isMinOrMax(SelectPatternFlavor SPF) {
532
18.5M
      return SPF != SPF_UNKNOWN && 
SPF != SPF_ABS6.37M
&&
SPF != SPF_NABS5.57M
;
533
18.5M
    }
534
  };
535
536
  /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind
537
  /// and providing the out parameter results if we successfully match.
538
  ///
539
  /// For ABS/NABS, LHS will be set to the input to the abs idiom. RHS will be
540
  /// the negation instruction from the idiom.
541
  ///
542
  /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does
543
  /// not match that of the original select. If this is the case, the cast
544
  /// operation (one of Trunc,SExt,Zext) that must be done to transform the
545
  /// type of LHS and RHS into the type of V is returned in CastOp.
546
  ///
547
  /// For example:
548
  ///   %1 = icmp slt i32 %a, i32 4
549
  ///   %2 = sext i32 %a to i64
550
  ///   %3 = select i1 %1, i64 %2, i64 4
551
  ///
552
  /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
553
  ///
554
  SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
555
                                         Instruction::CastOps *CastOp = nullptr,
556
                                         unsigned Depth = 0);
557
  inline SelectPatternResult
558
  matchSelectPattern(const Value *V, const Value *&LHS, const Value *&RHS,
559
12.3M
                     Instruction::CastOps *CastOp = nullptr) {
560
12.3M
    Value *L = const_cast<Value*>(LHS);
561
12.3M
    Value *R = const_cast<Value*>(RHS);
562
12.3M
    auto Result = matchSelectPattern(const_cast<Value*>(V), L, R);
563
12.3M
    LHS = L;
564
12.3M
    RHS = R;
565
12.3M
    return Result;
566
12.3M
  }
567
568
  /// Return the canonical comparison predicate for the specified
569
  /// minimum/maximum flavor.
570
  CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF,
571
                                   bool Ordered = false);
572
573
  /// Return the inverse minimum/maximum flavor of the specified flavor.
574
  /// For example, signed minimum is the inverse of signed maximum.
575
  SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF);
576
577
  /// Return the canonical inverse comparison predicate for the specified
578
  /// minimum/maximum flavor.
579
  CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF);
580
581
  /// Return true if RHS is known to be implied true by LHS.  Return false if
582
  /// RHS is known to be implied false by LHS.  Otherwise, return None if no
583
  /// implication can be made.
584
  /// A & B must be i1 (boolean) values or a vector of such values. Note that
585
  /// the truth table for implication is the same as <=u on i1 values (but not
586
  /// <=s!).  The truth table for both is:
587
  ///    | T | F (B)
588
  ///  T | T | F
589
  ///  F | T | T
590
  /// (A)
591
  Optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS,
592
                                    const DataLayout &DL, bool LHSIsTrue = true,
593
                                    unsigned Depth = 0);
594
} // end namespace llvm
595
596
#endif // LLVM_ANALYSIS_VALUETRACKING_H