CmpInstAnalysis.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file holds routines to help analyse compare instructions
  15. // and fold them into constants or other compare instructions
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
  19. #define LLVM_ANALYSIS_CMPINSTANALYSIS_H
  20. #include "llvm/IR/InstrTypes.h"
  21. namespace llvm {
  22. class Type;
  23. class Value;
  24. /// Encode a icmp predicate into a three bit mask. These bits are carefully
  25. /// arranged to allow folding of expressions such as:
  26. ///
  27. /// (A < B) | (A > B) --> (A != B)
  28. ///
  29. /// Note that this is only valid if the first and second predicates have the
  30. /// same sign. It is illegal to do: (A u< B) | (A s> B)
  31. ///
  32. /// Three bits are used to represent the condition, as follows:
  33. /// 0 A > B
  34. /// 1 A == B
  35. /// 2 A < B
  36. ///
  37. /// <=> Value Definition
  38. /// 000 0 Always false
  39. /// 001 1 A > B
  40. /// 010 2 A == B
  41. /// 011 3 A >= B
  42. /// 100 4 A < B
  43. /// 101 5 A != B
  44. /// 110 6 A <= B
  45. /// 111 7 Always true
  46. ///
  47. unsigned getICmpCode(CmpInst::Predicate Pred);
  48. /// This is the complement of getICmpCode. It turns a predicate code into
  49. /// either a constant true or false or the predicate for a new ICmp.
  50. /// The sign is passed in to determine which kind of predicate to use in the
  51. /// new ICmp instruction.
  52. /// Non-NULL return value will be a true or false constant.
  53. /// NULL return means a new ICmp is needed. The predicate is output in Pred.
  54. Constant *getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
  55. CmpInst::Predicate &Pred);
  56. /// Return true if both predicates match sign or if at least one of them is an
  57. /// equality comparison (which is signless).
  58. bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2);
  59. /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate
  60. /// into a four bit mask.
  61. inline unsigned getFCmpCode(CmpInst::Predicate CC) {
  62. assert(CmpInst::FCMP_FALSE <= CC && CC <= CmpInst::FCMP_TRUE &&
  63. "Unexpected FCmp predicate!");
  64. // Take advantage of the bit pattern of CmpInst::Predicate here.
  65. // U L G E
  66. static_assert(CmpInst::FCMP_FALSE == 0); // 0 0 0 0
  67. static_assert(CmpInst::FCMP_OEQ == 1); // 0 0 0 1
  68. static_assert(CmpInst::FCMP_OGT == 2); // 0 0 1 0
  69. static_assert(CmpInst::FCMP_OGE == 3); // 0 0 1 1
  70. static_assert(CmpInst::FCMP_OLT == 4); // 0 1 0 0
  71. static_assert(CmpInst::FCMP_OLE == 5); // 0 1 0 1
  72. static_assert(CmpInst::FCMP_ONE == 6); // 0 1 1 0
  73. static_assert(CmpInst::FCMP_ORD == 7); // 0 1 1 1
  74. static_assert(CmpInst::FCMP_UNO == 8); // 1 0 0 0
  75. static_assert(CmpInst::FCMP_UEQ == 9); // 1 0 0 1
  76. static_assert(CmpInst::FCMP_UGT == 10); // 1 0 1 0
  77. static_assert(CmpInst::FCMP_UGE == 11); // 1 0 1 1
  78. static_assert(CmpInst::FCMP_ULT == 12); // 1 1 0 0
  79. static_assert(CmpInst::FCMP_ULE == 13); // 1 1 0 1
  80. static_assert(CmpInst::FCMP_UNE == 14); // 1 1 1 0
  81. static_assert(CmpInst::FCMP_TRUE == 15); // 1 1 1 1
  82. return CC;
  83. }
  84. /// This is the complement of getFCmpCode. It turns a predicate code into
  85. /// either a constant true or false or the predicate for a new FCmp.
  86. /// Non-NULL return value will be a true or false constant.
  87. /// NULL return means a new ICmp is needed. The predicate is output in Pred.
  88. Constant *getPredForFCmpCode(unsigned Code, Type *OpTy,
  89. CmpInst::Predicate &Pred);
  90. /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
  91. /// returned predicate is either == or !=. Returns false if decomposition
  92. /// fails.
  93. bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
  94. Value *&X, APInt &Mask,
  95. bool LookThroughTrunc = true);
  96. } // end namespace llvm
  97. #endif
  98. #ifdef __GNUC__
  99. #pragma GCC diagnostic pop
  100. #endif