1 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
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 #include "llvm/Analysis/CmpInstAnalysis.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/PatternMatch.h"
22 unsigned llvm::getICmpCode(const ICmpInst
*ICI
, bool InvertPred
) {
23 ICmpInst::Predicate Pred
= InvertPred
? ICI
->getInversePredicate()
24 : ICI
->getPredicate();
27 case ICmpInst::ICMP_UGT
: return 1; // 001
28 case ICmpInst::ICMP_SGT
: return 1; // 001
29 case ICmpInst::ICMP_EQ
: return 2; // 010
30 case ICmpInst::ICMP_UGE
: return 3; // 011
31 case ICmpInst::ICMP_SGE
: return 3; // 011
32 case ICmpInst::ICMP_ULT
: return 4; // 100
33 case ICmpInst::ICMP_SLT
: return 4; // 100
34 case ICmpInst::ICMP_NE
: return 5; // 101
35 case ICmpInst::ICMP_ULE
: return 6; // 110
36 case ICmpInst::ICMP_SLE
: return 6; // 110
39 llvm_unreachable("Invalid ICmp predicate!");
43 Value
*llvm::getICmpValue(bool Sign
, unsigned Code
, Value
*LHS
, Value
*RHS
,
44 CmpInst::Predicate
&NewICmpPred
) {
46 default: llvm_unreachable("Illegal ICmp code!");
48 return ConstantInt::get(CmpInst::makeCmpResultType(LHS
->getType()), 0);
49 case 1: NewICmpPred
= Sign
? ICmpInst::ICMP_SGT
: ICmpInst::ICMP_UGT
; break;
50 case 2: NewICmpPred
= ICmpInst::ICMP_EQ
; break;
51 case 3: NewICmpPred
= Sign
? ICmpInst::ICMP_SGE
: ICmpInst::ICMP_UGE
; break;
52 case 4: NewICmpPred
= Sign
? ICmpInst::ICMP_SLT
: ICmpInst::ICMP_ULT
; break;
53 case 5: NewICmpPred
= ICmpInst::ICMP_NE
; break;
54 case 6: NewICmpPred
= Sign
? ICmpInst::ICMP_SLE
: ICmpInst::ICMP_ULE
; break;
56 return ConstantInt::get(CmpInst::makeCmpResultType(LHS
->getType()), 1);
61 bool llvm::PredicatesFoldable(ICmpInst::Predicate p1
, ICmpInst::Predicate p2
) {
62 return (CmpInst::isSigned(p1
) == CmpInst::isSigned(p2
)) ||
63 (CmpInst::isSigned(p1
) && ICmpInst::isEquality(p2
)) ||
64 (CmpInst::isSigned(p2
) && ICmpInst::isEquality(p1
));
67 bool llvm::decomposeBitTestICmp(Value
*LHS
, Value
*RHS
,
68 CmpInst::Predicate
&Pred
,
69 Value
*&X
, APInt
&Mask
, bool LookThruTrunc
) {
70 using namespace PatternMatch
;
73 if (!match(RHS
, m_APInt(C
)))
79 case ICmpInst::ICMP_SLT
:
80 // X < 0 is equivalent to (X & SignMask) != 0.
81 if (!C
->isNullValue())
83 Mask
= APInt::getSignMask(C
->getBitWidth());
84 Pred
= ICmpInst::ICMP_NE
;
86 case ICmpInst::ICMP_SLE
:
87 // X <= -1 is equivalent to (X & SignMask) != 0.
88 if (!C
->isAllOnesValue())
90 Mask
= APInt::getSignMask(C
->getBitWidth());
91 Pred
= ICmpInst::ICMP_NE
;
93 case ICmpInst::ICMP_SGT
:
94 // X > -1 is equivalent to (X & SignMask) == 0.
95 if (!C
->isAllOnesValue())
97 Mask
= APInt::getSignMask(C
->getBitWidth());
98 Pred
= ICmpInst::ICMP_EQ
;
100 case ICmpInst::ICMP_SGE
:
101 // X >= 0 is equivalent to (X & SignMask) == 0.
102 if (!C
->isNullValue())
104 Mask
= APInt::getSignMask(C
->getBitWidth());
105 Pred
= ICmpInst::ICMP_EQ
;
107 case ICmpInst::ICMP_ULT
:
108 // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
109 if (!C
->isPowerOf2())
112 Pred
= ICmpInst::ICMP_EQ
;
114 case ICmpInst::ICMP_ULE
:
115 // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
116 if (!(*C
+ 1).isPowerOf2())
119 Pred
= ICmpInst::ICMP_EQ
;
121 case ICmpInst::ICMP_UGT
:
122 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
123 if (!(*C
+ 1).isPowerOf2())
126 Pred
= ICmpInst::ICMP_NE
;
128 case ICmpInst::ICMP_UGE
:
129 // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
130 if (!C
->isPowerOf2())
133 Pred
= ICmpInst::ICMP_NE
;
137 if (LookThruTrunc
&& match(LHS
, m_Trunc(m_Value(X
)))) {
138 Mask
= Mask
.zext(X
->getType()->getScalarSizeInBits());