1*0b57cec5SDimitry Andric //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file holds routines to help analyse compare instructions
10*0b57cec5SDimitry Andric // and fold them into constants or other compare instructions
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/Analysis/CmpInstAnalysis.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric 
getICmpCode(const ICmpInst * ICI,bool InvertPred)21*0b57cec5SDimitry Andric unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
22*0b57cec5SDimitry Andric   ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
23*0b57cec5SDimitry Andric                                         : ICI->getPredicate();
24*0b57cec5SDimitry Andric   switch (Pred) {
25*0b57cec5SDimitry Andric       // False -> 0
26*0b57cec5SDimitry Andric     case ICmpInst::ICMP_UGT: return 1;  // 001
27*0b57cec5SDimitry Andric     case ICmpInst::ICMP_SGT: return 1;  // 001
28*0b57cec5SDimitry Andric     case ICmpInst::ICMP_EQ:  return 2;  // 010
29*0b57cec5SDimitry Andric     case ICmpInst::ICMP_UGE: return 3;  // 011
30*0b57cec5SDimitry Andric     case ICmpInst::ICMP_SGE: return 3;  // 011
31*0b57cec5SDimitry Andric     case ICmpInst::ICMP_ULT: return 4;  // 100
32*0b57cec5SDimitry Andric     case ICmpInst::ICMP_SLT: return 4;  // 100
33*0b57cec5SDimitry Andric     case ICmpInst::ICMP_NE:  return 5;  // 101
34*0b57cec5SDimitry Andric     case ICmpInst::ICMP_ULE: return 6;  // 110
35*0b57cec5SDimitry Andric     case ICmpInst::ICMP_SLE: return 6;  // 110
36*0b57cec5SDimitry Andric       // True -> 7
37*0b57cec5SDimitry Andric     default:
38*0b57cec5SDimitry Andric       llvm_unreachable("Invalid ICmp predicate!");
39*0b57cec5SDimitry Andric   }
40*0b57cec5SDimitry Andric }
41*0b57cec5SDimitry Andric 
getPredForICmpCode(unsigned Code,bool Sign,Type * OpTy,CmpInst::Predicate & Pred)42*0b57cec5SDimitry Andric Constant *llvm::getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
43*0b57cec5SDimitry Andric                                    CmpInst::Predicate &Pred) {
44*0b57cec5SDimitry Andric   switch (Code) {
45*0b57cec5SDimitry Andric     default: llvm_unreachable("Illegal ICmp code!");
46*0b57cec5SDimitry Andric     case 0: // False.
47*0b57cec5SDimitry Andric       return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
48*0b57cec5SDimitry Andric     case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
49*0b57cec5SDimitry Andric     case 2: Pred = ICmpInst::ICMP_EQ; break;
50*0b57cec5SDimitry Andric     case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
51*0b57cec5SDimitry Andric     case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
52*0b57cec5SDimitry Andric     case 5: Pred = ICmpInst::ICMP_NE; break;
53*0b57cec5SDimitry Andric     case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
54*0b57cec5SDimitry Andric     case 7: // True.
55*0b57cec5SDimitry Andric       return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
56*0b57cec5SDimitry Andric   }
57*0b57cec5SDimitry Andric   return nullptr;
58*0b57cec5SDimitry Andric }
59*0b57cec5SDimitry Andric 
predicatesFoldable(ICmpInst::Predicate P1,ICmpInst::Predicate P2)60*0b57cec5SDimitry Andric bool llvm::predicatesFoldable(ICmpInst::Predicate P1, ICmpInst::Predicate P2) {
61*0b57cec5SDimitry Andric   return (CmpInst::isSigned(P1) == CmpInst::isSigned(P2)) ||
62*0b57cec5SDimitry Andric          (CmpInst::isSigned(P1) && ICmpInst::isEquality(P2)) ||
63*0b57cec5SDimitry Andric          (CmpInst::isSigned(P2) && ICmpInst::isEquality(P1));
64*0b57cec5SDimitry Andric }
65*0b57cec5SDimitry Andric 
decomposeBitTestICmp(Value * LHS,Value * RHS,CmpInst::Predicate & Pred,Value * & X,APInt & Mask,bool LookThruTrunc)66*0b57cec5SDimitry Andric bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
67*0b57cec5SDimitry Andric                                 CmpInst::Predicate &Pred,
68*0b57cec5SDimitry Andric                                 Value *&X, APInt &Mask, bool LookThruTrunc) {
69*0b57cec5SDimitry Andric   using namespace PatternMatch;
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   const APInt *C;
72*0b57cec5SDimitry Andric   if (!match(RHS, m_APInt(C)))
73*0b57cec5SDimitry Andric     return false;
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric   switch (Pred) {
76*0b57cec5SDimitry Andric   default:
77*0b57cec5SDimitry Andric     return false;
78*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SLT:
79*0b57cec5SDimitry Andric     // X < 0 is equivalent to (X & SignMask) != 0.
80*0b57cec5SDimitry Andric     if (!C->isNullValue())
81*0b57cec5SDimitry Andric       return false;
82*0b57cec5SDimitry Andric     Mask = APInt::getSignMask(C->getBitWidth());
83*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_NE;
84*0b57cec5SDimitry Andric     break;
85*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SLE:
86*0b57cec5SDimitry Andric     // X <= -1 is equivalent to (X & SignMask) != 0.
87*0b57cec5SDimitry Andric     if (!C->isAllOnesValue())
88*0b57cec5SDimitry Andric       return false;
89*0b57cec5SDimitry Andric     Mask = APInt::getSignMask(C->getBitWidth());
90*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_NE;
91*0b57cec5SDimitry Andric     break;
92*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SGT:
93*0b57cec5SDimitry Andric     // X > -1 is equivalent to (X & SignMask) == 0.
94*0b57cec5SDimitry Andric     if (!C->isAllOnesValue())
95*0b57cec5SDimitry Andric       return false;
96*0b57cec5SDimitry Andric     Mask = APInt::getSignMask(C->getBitWidth());
97*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_EQ;
98*0b57cec5SDimitry Andric     break;
99*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SGE:
100*0b57cec5SDimitry Andric     // X >= 0 is equivalent to (X & SignMask) == 0.
101*0b57cec5SDimitry Andric     if (!C->isNullValue())
102*0b57cec5SDimitry Andric       return false;
103*0b57cec5SDimitry Andric     Mask = APInt::getSignMask(C->getBitWidth());
104*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_EQ;
105*0b57cec5SDimitry Andric     break;
106*0b57cec5SDimitry Andric   case ICmpInst::ICMP_ULT:
107*0b57cec5SDimitry Andric     // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
108*0b57cec5SDimitry Andric     if (!C->isPowerOf2())
109*0b57cec5SDimitry Andric       return false;
110*0b57cec5SDimitry Andric     Mask = -*C;
111*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_EQ;
112*0b57cec5SDimitry Andric     break;
113*0b57cec5SDimitry Andric   case ICmpInst::ICMP_ULE:
114*0b57cec5SDimitry Andric     // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
115*0b57cec5SDimitry Andric     if (!(*C + 1).isPowerOf2())
116*0b57cec5SDimitry Andric       return false;
117*0b57cec5SDimitry Andric     Mask = ~*C;
118*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_EQ;
119*0b57cec5SDimitry Andric     break;
120*0b57cec5SDimitry Andric   case ICmpInst::ICMP_UGT:
121*0b57cec5SDimitry Andric     // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
122*0b57cec5SDimitry Andric     if (!(*C + 1).isPowerOf2())
123*0b57cec5SDimitry Andric       return false;
124*0b57cec5SDimitry Andric     Mask = ~*C;
125*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_NE;
126*0b57cec5SDimitry Andric     break;
127*0b57cec5SDimitry Andric   case ICmpInst::ICMP_UGE:
128*0b57cec5SDimitry Andric     // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
129*0b57cec5SDimitry Andric     if (!C->isPowerOf2())
130*0b57cec5SDimitry Andric       return false;
131*0b57cec5SDimitry Andric     Mask = -*C;
132*0b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_NE;
133*0b57cec5SDimitry Andric     break;
134*0b57cec5SDimitry Andric   }
135*0b57cec5SDimitry Andric 
136*0b57cec5SDimitry Andric   if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
137*0b57cec5SDimitry Andric     Mask = Mask.zext(X->getType()->getScalarSizeInBits());
138*0b57cec5SDimitry Andric   } else {
139*0b57cec5SDimitry Andric     X = LHS;
140*0b57cec5SDimitry Andric   }
141*0b57cec5SDimitry Andric 
142*0b57cec5SDimitry Andric   return true;
143*0b57cec5SDimitry Andric }
144