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_TRANSFORMS_UTILS_CMPINSTANALYSIS_H 16 #define LLVM_TRANSFORMS_UTILS_CMPINSTANALYSIS_H 17 18 #include "llvm/IR/InstrTypes.h" 19 20 namespace llvm { 21 class ICmpInst; 22 class Value; 23 24 /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits 25 /// are carefully 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. 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 /// getICmpValue - This is the complement of getICmpCode, which turns an 50 /// opcode and two operands into either a constant true or false, or the 51 /// predicate for a new ICmp instruction. The sign is passed in to determine 52 /// which kind of 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 55 /// output in NewICmpPred. 56 Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, 57 CmpInst::Predicate &NewICmpPred); 58 59 /// PredicatesFoldable - Return true if both predicates match sign or if at 60 /// least one of them is an equality comparison (which is signless). 61 bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2); 62 63 } // end namespace llvm 64 65 #endif 66