llvm.org GIT mirror llvm / fcc2170
[InstCombine][InstSimplify] 'git add' two files that moved in r310869. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310870 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 2 years ago
2 changed file(s) with 208 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file holds routines to help analyse compare instructions
10 // and fold them into constants or other compare instructions
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
15 #define LLVM_ANALYSIS_CMPINSTANALYSIS_H
16
17 #include "llvm/IR/InstrTypes.h"
18
19 namespace llvm {
20 class ICmpInst;
21 class Value;
22
23 /// Encode a icmp predicate into a three bit mask. These bits are carefully
24 /// arranged to allow folding of expressions such as:
25 ///
26 /// (A < B) | (A > B) --> (A != B)
27 ///
28 /// Note that this is only valid if the first and second predicates have the
29 /// same sign. It is illegal to do: (A u< B) | (A s> B)
30 ///
31 /// Three bits are used to represent the condition, as follows:
32 /// 0 A > B
33 /// 1 A == B
34 /// 2 A < B
35 ///
36 /// <=> Value Definition
37 /// 000 0 Always false
38 /// 001 1 A > B
39 /// 010 2 A == B
40 /// 011 3 A >= B
41 /// 100 4 A < B
42 /// 101 5 A != B
43 /// 110 6 A <= B
44 /// 111 7 Always true
45 ///
46 unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
47
48 /// This is the complement of getICmpCode, which turns an opcode and two
49 /// operands into either a constant true or false, or the predicate for a new
50 /// ICmp instruction. The sign is passed in to determine which kind of
51 /// predicate to use in the new icmp instruction.
52 /// Non-NULL return value will be a true or false constant.
53 /// NULL return means a new ICmp is needed. The predicate for which is output
54 /// in NewICmpPred.
55 Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
56 CmpInst::Predicate &NewICmpPred);
57
58 /// Return true if both predicates match sign or if at least one of them is an
59 /// equality comparison (which is signless).
60 bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2);
61
62 /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
63 /// returned predicate is either == or !=. Returns false if decomposition
64 /// fails.
65 bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
66 Value *&X, APInt &Mask);
67
68 } // end namespace llvm
69
70 #endif
0 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file holds routines to help analyse compare instructions
10 // and fold them into constants or other compare instructions
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/CmpInstAnalysis.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/PatternMatch.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(Value *LHS, Value *RHS,
67 CmpInst::Predicate &Pred,
68 Value *&X, APInt &Mask) {
69 const APInt *C;
70 if (!match(RHS, PatternMatch::m_APInt(C)))
71 return false;
72
73 switch (Pred) {
74 default:
75 return false;
76 case ICmpInst::ICMP_SLT:
77 // X < 0 is equivalent to (X & SignMask) != 0.
78 if (!C->isNullValue())
79 return false;
80 Mask = APInt::getSignMask(C->getBitWidth());
81 Pred = ICmpInst::ICMP_NE;
82 break;
83 case ICmpInst::ICMP_SLE:
84 // X <= -1 is equivalent to (X & SignMask) != 0.
85 if (!C->isAllOnesValue())
86 return false;
87 Mask = APInt::getSignMask(C->getBitWidth());
88 Pred = ICmpInst::ICMP_NE;
89 break;
90 case ICmpInst::ICMP_SGT:
91 // X > -1 is equivalent to (X & SignMask) == 0.
92 if (!C->isAllOnesValue())
93 return false;
94 Mask = APInt::getSignMask(C->getBitWidth());
95 Pred = ICmpInst::ICMP_EQ;
96 break;
97 case ICmpInst::ICMP_SGE:
98 // X >= 0 is equivalent to (X & SignMask) == 0.
99 if (!C->isNullValue())
100 return false;
101 Mask = APInt::getSignMask(C->getBitWidth());
102 Pred = ICmpInst::ICMP_EQ;
103 break;
104 case ICmpInst::ICMP_ULT:
105 // X
106 if (!C->isPowerOf2())
107 return false;
108 Mask = -*C;
109 Pred = ICmpInst::ICMP_EQ;
110 break;
111 case ICmpInst::ICMP_ULE:
112 // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
113 if (!(*C + 1).isPowerOf2())
114 return false;
115 Mask = ~*C;
116 Pred = ICmpInst::ICMP_EQ;
117 break;
118 case ICmpInst::ICMP_UGT:
119 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
120 if (!(*C + 1).isPowerOf2())
121 return false;
122 Mask = ~*C;
123 Pred = ICmpInst::ICMP_NE;
124 break;
125 case ICmpInst::ICMP_UGE:
126 // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
127 if (!C->isPowerOf2())
128 return false;
129 Mask = -*C;
130 Pred = ICmpInst::ICMP_NE;
131 break;
132 }
133
134 X = LHS;
135 return true;
136 }