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