1 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file holds routines to help analyse compare instructions
10 // and fold them into constants or other compare instructions
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/CmpInstAnalysis.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/PatternMatch.h"
21 unsigned llvm::getICmpCode(const ICmpInst
*ICI
, bool InvertPred
) {
22 ICmpInst::Predicate Pred
= InvertPred
? ICI
->getInversePredicate()
23 : ICI
->getPredicate();
26 case ICmpInst::ICMP_UGT
: return 1; // 001
27 case ICmpInst::ICMP_SGT
: return 1; // 001
28 case ICmpInst::ICMP_EQ
: return 2; // 010
29 case ICmpInst::ICMP_UGE
: return 3; // 011
30 case ICmpInst::ICMP_SGE
: return 3; // 011
31 case ICmpInst::ICMP_ULT
: return 4; // 100
32 case ICmpInst::ICMP_SLT
: return 4; // 100
33 case ICmpInst::ICMP_NE
: return 5; // 101
34 case ICmpInst::ICMP_ULE
: return 6; // 110
35 case ICmpInst::ICMP_SLE
: return 6; // 110
38 llvm_unreachable("Invalid ICmp predicate!");
42 Constant
*llvm::getPredForICmpCode(unsigned Code
, bool Sign
, Type
*OpTy
,
43 CmpInst::Predicate
&Pred
) {
45 default: llvm_unreachable("Illegal ICmp code!");
47 return ConstantInt::get(CmpInst::makeCmpResultType(OpTy
), 0);
48 case 1: Pred
= Sign
? ICmpInst::ICMP_SGT
: ICmpInst::ICMP_UGT
; break;
49 case 2: Pred
= ICmpInst::ICMP_EQ
; break;
50 case 3: Pred
= Sign
? ICmpInst::ICMP_SGE
: ICmpInst::ICMP_UGE
; break;
51 case 4: Pred
= Sign
? ICmpInst::ICMP_SLT
: ICmpInst::ICMP_ULT
; break;
52 case 5: Pred
= ICmpInst::ICMP_NE
; break;
53 case 6: Pred
= Sign
? ICmpInst::ICMP_SLE
: ICmpInst::ICMP_ULE
; break;
55 return ConstantInt::get(CmpInst::makeCmpResultType(OpTy
), 1);
60 bool llvm::predicatesFoldable(ICmpInst::Predicate P1
, ICmpInst::Predicate P2
) {
61 return (CmpInst::isSigned(P1
) == CmpInst::isSigned(P2
)) ||
62 (CmpInst::isSigned(P1
) && ICmpInst::isEquality(P2
)) ||
63 (CmpInst::isSigned(P2
) && ICmpInst::isEquality(P1
));
66 bool llvm::decomposeBitTestICmp(Value
*LHS
, Value
*RHS
,
67 CmpInst::Predicate
&Pred
,
68 Value
*&X
, APInt
&Mask
, bool LookThruTrunc
) {
69 using namespace PatternMatch
;
72 if (!match(RHS
, m_APInt(C
)))
78 case ICmpInst::ICMP_SLT
:
79 // X < 0 is equivalent to (X & SignMask) != 0.
80 if (!C
->isNullValue())
82 Mask
= APInt::getSignMask(C
->getBitWidth());
83 Pred
= ICmpInst::ICMP_NE
;
85 case ICmpInst::ICMP_SLE
:
86 // X <= -1 is equivalent to (X & SignMask) != 0.
87 if (!C
->isAllOnesValue())
89 Mask
= APInt::getSignMask(C
->getBitWidth());
90 Pred
= ICmpInst::ICMP_NE
;
92 case ICmpInst::ICMP_SGT
:
93 // X > -1 is equivalent to (X & SignMask) == 0.
94 if (!C
->isAllOnesValue())
96 Mask
= APInt::getSignMask(C
->getBitWidth());
97 Pred
= ICmpInst::ICMP_EQ
;
99 case ICmpInst::ICMP_SGE
:
100 // X >= 0 is equivalent to (X & SignMask) == 0.
101 if (!C
->isNullValue())
103 Mask
= APInt::getSignMask(C
->getBitWidth());
104 Pred
= ICmpInst::ICMP_EQ
;
106 case ICmpInst::ICMP_ULT
:
107 // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
108 if (!C
->isPowerOf2())
111 Pred
= ICmpInst::ICMP_EQ
;
113 case ICmpInst::ICMP_ULE
:
114 // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
115 if (!(*C
+ 1).isPowerOf2())
118 Pred
= ICmpInst::ICMP_EQ
;
120 case ICmpInst::ICMP_UGT
:
121 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
122 if (!(*C
+ 1).isPowerOf2())
125 Pred
= ICmpInst::ICMP_NE
;
127 case ICmpInst::ICMP_UGE
:
128 // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
129 if (!C
->isPowerOf2())
132 Pred
= ICmpInst::ICMP_NE
;
136 if (LookThruTrunc
&& match(LHS
, m_Trunc(m_Value(X
)))) {
137 Mask
= Mask
.zext(X
->getType()->getScalarSizeInBits());