CmpInstAnalysis.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 ICmpInst;
  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(const ICmpInst *ICI, bool InvertPred = false);
  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. /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
  60. /// returned predicate is either == or !=. Returns false if decomposition
  61. /// fails.
  62. bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
  63. Value *&X, APInt &Mask,
  64. bool LookThroughTrunc = true);
  65. } // end namespace llvm
  66. #endif
  67. #ifdef __GNUC__
  68. #pragma GCC diagnostic pop
  69. #endif