CmpInstAnalysis.cpp 5.0 KB

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