]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Utils/CmpInstAnalysis.cpp
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Utils / CmpInstAnalysis.cpp
1 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
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 #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18
19 using namespace llvm;
20
21 unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
22   ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
23                                         : ICI->getPredicate();
24   switch (Pred) {
25       // False -> 0
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
36       // True -> 7
37     default:
38       llvm_unreachable("Invalid ICmp predicate!");
39   }
40 }
41
42 Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
43                           CmpInst::Predicate &NewICmpPred) {
44   switch (Code) {
45     default: llvm_unreachable("Illegal ICmp code!");
46     case 0: // False.
47       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
48     case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
49     case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
50     case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
51     case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
52     case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
53     case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
54     case 7: // True.
55       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
56   }
57   return nullptr;
58 }
59
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));
64 }
65
66 bool llvm::decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred,
67                                 Value *&X, Value *&Y, Value *&Z) {
68   ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1));
69   if (!C)
70     return false;
71
72   switch (I->getPredicate()) {
73   default:
74     return false;
75   case ICmpInst::ICMP_SLT:
76     // X < 0 is equivalent to (X & SignMask) != 0.
77     if (!C->isZero())
78       return false;
79     Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
80     Pred = ICmpInst::ICMP_NE;
81     break;
82   case ICmpInst::ICMP_SGT:
83     // X > -1 is equivalent to (X & SignMask) == 0.
84     if (!C->isAllOnesValue())
85       return false;
86     Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
87     Pred = ICmpInst::ICMP_EQ;
88     break;
89   case ICmpInst::ICMP_ULT:
90     // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
91     if (!C->getValue().isPowerOf2())
92       return false;
93     Y = ConstantInt::get(I->getContext(), -C->getValue());
94     Pred = ICmpInst::ICMP_EQ;
95     break;
96   case ICmpInst::ICMP_UGT:
97     // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
98     if (!(C->getValue() + 1).isPowerOf2())
99       return false;
100     Y = ConstantInt::get(I->getContext(), ~C->getValue());
101     Pred = ICmpInst::ICMP_NE;
102     break;
103   }
104
105   X = I->getOperand(0);
106   Z = ConstantInt::getNullValue(C->getType());
107   return true;
108 }