Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 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 | |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/CmpInstAnalysis.h" |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/Instructions.h" |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) { |
| 23 | ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate() |
| 24 | : ICI->getPredicate(); |
| 25 | switch (Pred) { |
| 26 | // False -> 0 |
| 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 |
| 37 | // True -> 7 |
| 38 | default: |
| 39 | llvm_unreachable("Invalid ICmp predicate!"); |
| 40 | } |
| 41 | } |
| 42 | |
Sanjay Patel | 04cb4ca | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 43 | Constant *llvm::getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, |
| 44 | CmpInst::Predicate &Pred) { |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 45 | switch (Code) { |
| 46 | default: llvm_unreachable("Illegal ICmp code!"); |
| 47 | case 0: // False. |
Sanjay Patel | 04cb4ca | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 48 | return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0); |
| 49 | case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break; |
| 50 | case 2: Pred = ICmpInst::ICMP_EQ; break; |
| 51 | case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break; |
| 52 | case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break; |
| 53 | case 5: Pred = ICmpInst::ICMP_NE; break; |
| 54 | case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 55 | case 7: // True. |
Sanjay Patel | 04cb4ca | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 56 | return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1); |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 57 | } |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
Sanjay Patel | 79a6444 | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 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)); |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 67 | bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, |
| 68 | CmpInst::Predicate &Pred, |
Craig Topper | 85fcd34 | 2017-09-01 21:27:34 +0000 | [diff] [blame] | 69 | Value *&X, APInt &Mask, bool LookThruTrunc) { |
| 70 | using namespace PatternMatch; |
| 71 | |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 72 | const APInt *C; |
Craig Topper | 85fcd34 | 2017-09-01 21:27:34 +0000 | [diff] [blame] | 73 | if (!match(RHS, m_APInt(C))) |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 74 | return false; |
| 75 | |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 76 | switch (Pred) { |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 77 | default: |
| 78 | return false; |
| 79 | case ICmpInst::ICMP_SLT: |
| 80 | // X < 0 is equivalent to (X & SignMask) != 0. |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 81 | if (!C->isNullValue()) |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 82 | return false; |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 83 | Mask = APInt::getSignMask(C->getBitWidth()); |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 84 | Pred = ICmpInst::ICMP_NE; |
| 85 | break; |
Craig Topper | 2650c76 | 2017-08-14 22:11:43 +0000 | [diff] [blame] | 86 | case ICmpInst::ICMP_SLE: |
| 87 | // X <= -1 is equivalent to (X & SignMask) != 0. |
| 88 | if (!C->isAllOnesValue()) |
| 89 | return false; |
| 90 | Mask = APInt::getSignMask(C->getBitWidth()); |
| 91 | Pred = ICmpInst::ICMP_NE; |
| 92 | break; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 93 | case ICmpInst::ICMP_SGT: |
| 94 | // X > -1 is equivalent to (X & SignMask) == 0. |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 95 | if (!C->isAllOnesValue()) |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 96 | return false; |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 97 | Mask = APInt::getSignMask(C->getBitWidth()); |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 98 | Pred = ICmpInst::ICMP_EQ; |
| 99 | break; |
Craig Topper | 2650c76 | 2017-08-14 22:11:43 +0000 | [diff] [blame] | 100 | case ICmpInst::ICMP_SGE: |
| 101 | // X >= 0 is equivalent to (X & SignMask) == 0. |
| 102 | if (!C->isNullValue()) |
| 103 | return false; |
| 104 | Mask = APInt::getSignMask(C->getBitWidth()); |
| 105 | Pred = ICmpInst::ICMP_EQ; |
| 106 | break; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 107 | case ICmpInst::ICMP_ULT: |
| 108 | // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0. |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 109 | if (!C->isPowerOf2()) |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 110 | return false; |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 111 | Mask = -*C; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 112 | Pred = ICmpInst::ICMP_EQ; |
| 113 | break; |
Craig Topper | 2650c76 | 2017-08-14 22:11:43 +0000 | [diff] [blame] | 114 | case ICmpInst::ICMP_ULE: |
| 115 | // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0. |
| 116 | if (!(*C + 1).isPowerOf2()) |
| 117 | return false; |
| 118 | Mask = ~*C; |
| 119 | Pred = ICmpInst::ICMP_EQ; |
| 120 | break; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 121 | case ICmpInst::ICMP_UGT: |
| 122 | // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0. |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 123 | if (!(*C + 1).isPowerOf2()) |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 124 | return false; |
Craig Topper | fc52a9c | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 125 | Mask = ~*C; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 126 | Pred = ICmpInst::ICMP_NE; |
| 127 | break; |
Craig Topper | 2650c76 | 2017-08-14 22:11:43 +0000 | [diff] [blame] | 128 | case ICmpInst::ICMP_UGE: |
| 129 | // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0. |
| 130 | if (!C->isPowerOf2()) |
| 131 | return false; |
| 132 | Mask = -*C; |
| 133 | Pred = ICmpInst::ICMP_NE; |
| 134 | break; |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Craig Topper | 85fcd34 | 2017-09-01 21:27:34 +0000 | [diff] [blame] | 137 | if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) { |
| 138 | Mask = Mask.zext(X->getType()->getScalarSizeInBits()); |
| 139 | } else { |
| 140 | X = LHS; |
| 141 | } |
| 142 | |
Craig Topper | 74b28d6 | 2017-08-14 19:09:32 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |