CmpInstAnalysis.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Instructions.h"
  17. using namespace llvm;
  18. /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits
  19. /// are carefully arranged to allow folding of expressions such as:
  20. ///
  21. /// (A < B) | (A > B) --> (A != B)
  22. ///
  23. /// Note that this is only valid if the first and second predicates have the
  24. /// same sign. Is illegal to do: (A u< B) | (A s> B)
  25. ///
  26. /// Three bits are used to represent the condition, as follows:
  27. /// 0 A > B
  28. /// 1 A == B
  29. /// 2 A < B
  30. ///
  31. /// <=> Value Definition
  32. /// 000 0 Always false
  33. /// 001 1 A > B
  34. /// 010 2 A == B
  35. /// 011 3 A >= B
  36. /// 100 4 A < B
  37. /// 101 5 A != B
  38. /// 110 6 A <= B
  39. /// 111 7 Always true
  40. ///
  41. unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
  42. ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
  43. : ICI->getPredicate();
  44. switch (Pred) {
  45. // False -> 0
  46. case ICmpInst::ICMP_UGT: return 1; // 001
  47. case ICmpInst::ICMP_SGT: return 1; // 001
  48. case ICmpInst::ICMP_EQ: return 2; // 010
  49. case ICmpInst::ICMP_UGE: return 3; // 011
  50. case ICmpInst::ICMP_SGE: return 3; // 011
  51. case ICmpInst::ICMP_ULT: return 4; // 100
  52. case ICmpInst::ICMP_SLT: return 4; // 100
  53. case ICmpInst::ICMP_NE: return 5; // 101
  54. case ICmpInst::ICMP_ULE: return 6; // 110
  55. case ICmpInst::ICMP_SLE: return 6; // 110
  56. // True -> 7
  57. default:
  58. llvm_unreachable("Invalid ICmp predicate!");
  59. }
  60. }
  61. /// getICmpValue - This is the complement of getICmpCode, which turns an
  62. /// opcode and two operands into either a constant true or false, or the
  63. /// predicate for a new ICmp instruction. The sign is passed in to determine
  64. /// which kind of predicate to use in the new icmp instruction.
  65. /// Non-NULL return value will be a true or false constant.
  66. /// NULL return means a new ICmp is needed. The predicate for which is
  67. /// output in NewICmpPred.
  68. Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
  69. CmpInst::Predicate &NewICmpPred) {
  70. switch (Code) {
  71. default: llvm_unreachable("Illegal ICmp code!");
  72. case 0: // False.
  73. return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
  74. case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
  75. case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
  76. case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
  77. case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
  78. case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
  79. case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
  80. case 7: // True.
  81. return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
  82. }
  83. return nullptr;
  84. }
  85. /// PredicatesFoldable - Return true if both predicates match sign or if at
  86. /// least one of them is an equality comparison (which is signless).
  87. bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
  88. return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
  89. (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
  90. (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
  91. }