CmpInstAnalysis.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
  2. //
  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
  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. #include "llvm/Analysis/CmpInstAnalysis.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/PatternMatch.h"
  17. using namespace llvm;
  18. unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
  19. ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
  20. : ICI->getPredicate();
  21. switch (Pred) {
  22. // False -> 0
  23. case ICmpInst::ICMP_UGT: return 1; // 001
  24. case ICmpInst::ICMP_SGT: return 1; // 001
  25. case ICmpInst::ICMP_EQ: return 2; // 010
  26. case ICmpInst::ICMP_UGE: return 3; // 011
  27. case ICmpInst::ICMP_SGE: return 3; // 011
  28. case ICmpInst::ICMP_ULT: return 4; // 100
  29. case ICmpInst::ICMP_SLT: return 4; // 100
  30. case ICmpInst::ICMP_NE: return 5; // 101
  31. case ICmpInst::ICMP_ULE: return 6; // 110
  32. case ICmpInst::ICMP_SLE: return 6; // 110
  33. // True -> 7
  34. default:
  35. llvm_unreachable("Invalid ICmp predicate!");
  36. }
  37. }
  38. Constant *llvm::getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
  39. CmpInst::Predicate &Pred) {
  40. switch (Code) {
  41. default: llvm_unreachable("Illegal ICmp code!");
  42. case 0: // False.
  43. return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
  44. case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
  45. case 2: Pred = ICmpInst::ICMP_EQ; break;
  46. case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
  47. case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
  48. case 5: Pred = ICmpInst::ICMP_NE; break;
  49. case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
  50. case 7: // True.
  51. return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
  52. }
  53. return nullptr;
  54. }
  55. bool llvm::predicatesFoldable(ICmpInst::Predicate P1, ICmpInst::Predicate P2) {
  56. return (CmpInst::isSigned(P1) == CmpInst::isSigned(P2)) ||
  57. (CmpInst::isSigned(P1) && ICmpInst::isEquality(P2)) ||
  58. (CmpInst::isSigned(P2) && ICmpInst::isEquality(P1));
  59. }
  60. bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
  61. CmpInst::Predicate &Pred,
  62. Value *&X, APInt &Mask, bool LookThruTrunc) {
  63. using namespace PatternMatch;
  64. const APInt *C;
  65. if (!match(RHS, m_APInt(C)))
  66. return false;
  67. switch (Pred) {
  68. default:
  69. return false;
  70. case ICmpInst::ICMP_SLT:
  71. // X < 0 is equivalent to (X & SignMask) != 0.
  72. if (!C->isZero())
  73. return false;
  74. Mask = APInt::getSignMask(C->getBitWidth());
  75. Pred = ICmpInst::ICMP_NE;
  76. break;
  77. case ICmpInst::ICMP_SLE:
  78. // X <= -1 is equivalent to (X & SignMask) != 0.
  79. if (!C->isAllOnes())
  80. return false;
  81. Mask = APInt::getSignMask(C->getBitWidth());
  82. Pred = ICmpInst::ICMP_NE;
  83. break;
  84. case ICmpInst::ICMP_SGT:
  85. // X > -1 is equivalent to (X & SignMask) == 0.
  86. if (!C->isAllOnes())
  87. return false;
  88. Mask = APInt::getSignMask(C->getBitWidth());
  89. Pred = ICmpInst::ICMP_EQ;
  90. break;
  91. case ICmpInst::ICMP_SGE:
  92. // X >= 0 is equivalent to (X & SignMask) == 0.
  93. if (!C->isZero())
  94. return false;
  95. Mask = APInt::getSignMask(C->getBitWidth());
  96. Pred = ICmpInst::ICMP_EQ;
  97. break;
  98. case ICmpInst::ICMP_ULT:
  99. // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
  100. if (!C->isPowerOf2())
  101. return false;
  102. Mask = -*C;
  103. Pred = ICmpInst::ICMP_EQ;
  104. break;
  105. case ICmpInst::ICMP_ULE:
  106. // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
  107. if (!(*C + 1).isPowerOf2())
  108. return false;
  109. Mask = ~*C;
  110. Pred = ICmpInst::ICMP_EQ;
  111. break;
  112. case ICmpInst::ICMP_UGT:
  113. // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
  114. if (!(*C + 1).isPowerOf2())
  115. return false;
  116. Mask = ~*C;
  117. Pred = ICmpInst::ICMP_NE;
  118. break;
  119. case ICmpInst::ICMP_UGE:
  120. // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
  121. if (!C->isPowerOf2())
  122. return false;
  123. Mask = -*C;
  124. Pred = ICmpInst::ICMP_NE;
  125. break;
  126. }
  127. if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
  128. Mask = Mask.zext(X->getType()->getScalarSizeInBits());
  129. } else {
  130. X = LHS;
  131. }
  132. return true;
  133. }