1234285Sdim//===-- CmpInstAnalysis.h - Utils to help fold compare insts ------===//
2234285Sdim//
3234285Sdim//                     The LLVM Compiler Infrastructure
4234285Sdim//
5234285Sdim// This file is distributed under the University of Illinois Open Source
6234285Sdim// License. See LICENSE.TXT for details.
7234285Sdim//
8234285Sdim//===----------------------------------------------------------------------===//
9234285Sdim//
10234285Sdim// This file holds routines to help analyse compare instructions
11234285Sdim// and fold them into constants or other compare instructions
12234285Sdim//
13234285Sdim//===----------------------------------------------------------------------===//
14234285Sdim
15234285Sdim#ifndef LLVM_TRANSFORMS_UTILS_CMPINSTANALYSIS_H
16234285Sdim#define LLVM_TRANSFORMS_UTILS_CMPINSTANALYSIS_H
17234285Sdim
18249423Sdim#include "llvm/IR/InstrTypes.h"
19234285Sdim
20234285Sdimnamespace llvm {
21234285Sdim  class ICmpInst;
22234285Sdim  class Value;
23234285Sdim
24234285Sdim  /// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits
25234285Sdim  /// are carefully arranged to allow folding of expressions such as:
26234285Sdim  ///
27234285Sdim  ///      (A < B) | (A > B) --> (A != B)
28234285Sdim  ///
29234285Sdim  /// Note that this is only valid if the first and second predicates have the
30234285Sdim  /// same sign. Is illegal to do: (A u< B) | (A s> B)
31234285Sdim  ///
32234285Sdim  /// Three bits are used to represent the condition, as follows:
33234285Sdim  ///   0  A > B
34234285Sdim  ///   1  A == B
35234285Sdim  ///   2  A < B
36234285Sdim  ///
37234285Sdim  /// <=>  Value  Definition
38234285Sdim  /// 000     0   Always false
39234285Sdim  /// 001     1   A >  B
40234285Sdim  /// 010     2   A == B
41234285Sdim  /// 011     3   A >= B
42234285Sdim  /// 100     4   A <  B
43234285Sdim  /// 101     5   A != B
44234285Sdim  /// 110     6   A <= B
45234285Sdim  /// 111     7   Always true
46234285Sdim  ///
47234285Sdim  unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
48234285Sdim
49234285Sdim  /// getICmpValue - This is the complement of getICmpCode, which turns an
50234285Sdim  /// opcode and two operands into either a constant true or false, or the
51234285Sdim  /// predicate for a new ICmp instruction. The sign is passed in to determine
52234285Sdim  /// which kind of predicate to use in the new icmp instruction.
53234285Sdim  /// Non-NULL return value will be a true or false constant.
54234285Sdim  /// NULL return means a new ICmp is needed.  The predicate for which is
55234285Sdim  /// output in NewICmpPred.
56234285Sdim  Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
57234285Sdim                      CmpInst::Predicate &NewICmpPred);
58234285Sdim
59234285Sdim  /// PredicatesFoldable - Return true if both predicates match sign or if at
60234285Sdim  /// least one of them is an equality comparison (which is signless).
61234285Sdim  bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2);
62234285Sdim
63234285Sdim} // end namespace llvm
64234285Sdim
65234285Sdim#endif
66234285Sdim
67