1 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file holds routines to help analyse compare instructions
11 // and fold them into constants or other compare instructions
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
16 #define LLVM_ANALYSIS_CMPINSTANALYSIS_H
18 #include "llvm/IR/InstrTypes.h"
24 /// Encode a icmp predicate into a three bit mask. These bits are carefully
25 /// arranged to allow folding of expressions such as:
27 /// (A < B) | (A > B) --> (A != B)
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)
32 /// Three bits are used to represent the condition, as follows:
37 /// <=> Value Definition
38 /// 000 0 Always false
47 unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
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
56 Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
57 CmpInst::Predicate &NewICmpPred);
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);
63 /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
64 /// returned predicate is either == or !=. Returns false if decomposition
66 bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
67 Value *&X, APInt &Mask,
68 bool LookThroughTrunc = true);
70 } // end namespace llvm