Home | History | Annotate | Download | only in Analysis
      1 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- 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 holds routines to help analyse compare instructions
     11 // and fold them into constants or other compare instructions
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
     16 #define LLVM_ANALYSIS_CMPINSTANALYSIS_H
     17 
     18 #include "llvm/IR/InstrTypes.h"
     19 
     20 namespace llvm {
     21   class ICmpInst;
     22   class Value;
     23 
     24   /// Encode a icmp predicate into a three bit mask. These bits are carefully
     25   /// arranged to allow folding of expressions such as:
     26   ///
     27   ///      (A < B) | (A > B) --> (A != B)
     28   ///
     29   /// Note that this is only valid if the first and second predicates have the
     30   /// same sign. It is illegal to do: (A u< B) | (A s> B)
     31   ///
     32   /// Three bits are used to represent the condition, as follows:
     33   ///   0  A > B
     34   ///   1  A == B
     35   ///   2  A < B
     36   ///
     37   /// <=>  Value  Definition
     38   /// 000     0   Always false
     39   /// 001     1   A >  B
     40   /// 010     2   A == B
     41   /// 011     3   A >= B
     42   /// 100     4   A <  B
     43   /// 101     5   A != B
     44   /// 110     6   A <= B
     45   /// 111     7   Always true
     46   ///
     47   unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
     48 
     49   /// This is the complement of getICmpCode, which turns an opcode and two
     50   /// operands into either a constant true or false, or the predicate for a new
     51   /// ICmp instruction. The sign is passed in to determine which kind of
     52   /// predicate to use in the new icmp instruction.
     53   /// Non-NULL return value will be a true or false constant.
     54   /// NULL return means a new ICmp is needed. The predicate for which is output
     55   /// in NewICmpPred.
     56   Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
     57                       CmpInst::Predicate &NewICmpPred);
     58 
     59   /// Return true if both predicates match sign or if at least one of them is an
     60   /// equality comparison (which is signless).
     61   bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2);
     62 
     63   /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
     64   /// returned predicate is either == or !=. Returns false if decomposition
     65   /// fails.
     66   bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
     67                             Value *&X, APInt &Mask,
     68                             bool LookThroughTrunc = true);
     69 
     70 } // end namespace llvm
     71 
     72 #endif
     73