LLVM API Documentation

CmpInstAnalysis.h
Go to the documentation of this file.
00001 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file holds routines to help analyse compare instructions
00011 // and fold them into constants or other compare instructions
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_CMPINSTANALYSIS_H
00016 #define LLVM_TRANSFORMS_UTILS_CMPINSTANALYSIS_H
00017 
00018 #include "llvm/IR/InstrTypes.h"
00019 
00020 namespace llvm {
00021   class ICmpInst;
00022   class Value;
00023 
00024   /// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits
00025   /// are carefully arranged to allow folding of expressions such as:
00026   ///
00027   ///      (A < B) | (A > B) --> (A != B)
00028   ///
00029   /// Note that this is only valid if the first and second predicates have the
00030   /// same sign. Is illegal to do: (A u< B) | (A s> B)
00031   ///
00032   /// Three bits are used to represent the condition, as follows:
00033   ///   0  A > B
00034   ///   1  A == B
00035   ///   2  A < B
00036   ///
00037   /// <=>  Value  Definition
00038   /// 000     0   Always false
00039   /// 001     1   A >  B
00040   /// 010     2   A == B
00041   /// 011     3   A >= B
00042   /// 100     4   A <  B
00043   /// 101     5   A != B
00044   /// 110     6   A <= B
00045   /// 111     7   Always true
00046   ///
00047   unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
00048 
00049   /// getICmpValue - This is the complement of getICmpCode, which turns an
00050   /// opcode and two operands into either a constant true or false, or the
00051   /// predicate for a new ICmp instruction. The sign is passed in to determine
00052   /// which kind of predicate to use in the new icmp instruction.
00053   /// Non-NULL return value will be a true or false constant.
00054   /// NULL return means a new ICmp is needed.  The predicate for which is
00055   /// output in NewICmpPred.
00056   Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
00057                       CmpInst::Predicate &NewICmpPred);
00058 
00059   /// PredicatesFoldable - Return true if both predicates match sign or if at
00060   /// least one of them is an equality comparison (which is signless).
00061   bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2);
00062 
00063 } // end namespace llvm
00064 
00065 #endif