xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/CmpPredicate.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- CmpPredicate.h - CmpInst Predicate with samesign information -------===//
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 // A CmpInst::Predicate with any samesign information (applicable to ICmpInst).
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_CMPPREDICATE_H
14 #define LLVM_IR_CMPPREDICATE_H
15 
16 #include "llvm/IR/InstrTypes.h"
17 #include "llvm/Support/Compiler.h"
18 
19 namespace llvm {
20 /// An abstraction over a floating-point predicate, and a pack of an integer
21 /// predicate with samesign information. Some functions in ICmpInst construct
22 /// and return this type in place of a Predicate.
23 class CmpPredicate {
24   CmpInst::Predicate Pred;
25   bool HasSameSign;
26 
27 public:
28   /// Default constructor.
CmpPredicate()29   CmpPredicate() : Pred(CmpInst::BAD_ICMP_PREDICATE), HasSameSign(false) {}
30 
31   /// Constructed implictly with a either Predicate and samesign information, or
32   /// just a Predicate, dropping samesign information.
33   CmpPredicate(CmpInst::Predicate Pred, bool HasSameSign = false)
Pred(Pred)34       : Pred(Pred), HasSameSign(HasSameSign) {
35     assert(!HasSameSign || CmpInst::isIntPredicate(Pred));
36   }
37 
38   /// Implictly converts to the underlying Predicate, dropping samesign
39   /// information.
Predicate()40   operator CmpInst::Predicate() const { return Pred; }
41 
42   /// Query samesign information, for optimizations.
hasSameSign()43   bool hasSameSign() const { return HasSameSign; }
44 
45   /// Drops samesign information. This is used when the samesign information
46   /// should be dropped explicitly.
dropSameSign()47   CmpInst::Predicate dropSameSign() const { return Pred; }
48 
49   /// Compares two CmpPredicates taking samesign into account and returns the
50   /// canonicalized CmpPredicate if they match. An alternative to operator==.
51   ///
52   /// For example,
53   ///   samesign ult + samesign ult -> samesign ult
54   ///   samesign ult + ult -> ult
55   ///   samesign ult + slt -> slt
56   ///   ult + ult -> ult
57   ///   ult + slt -> std::nullopt
58   LLVM_ABI static std::optional<CmpPredicate> getMatching(CmpPredicate A,
59                                                           CmpPredicate B);
60 
61   /// Attempts to return a signed CmpInst::Predicate from the CmpPredicate. If
62   /// the CmpPredicate has samesign, return ICmpInst::getSignedPredicate,
63   /// dropping samesign information. Otherwise, return the predicate, dropping
64   /// samesign information.
65   LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const;
66 
67   /// An operator== on the underlying Predicate.
68   bool operator==(CmpInst::Predicate P) const { return Pred == P; }
69   bool operator!=(CmpInst::Predicate P) const { return Pred != P; }
70 
71   /// There is no operator== defined on CmpPredicate. Use getMatching instead to
72   /// get the canonicalized matching CmpPredicate.
73   bool operator==(CmpPredicate) const = delete;
74   bool operator!=(CmpPredicate) const = delete;
75 
76   /// Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as
77   /// appropriate.
78   LLVM_ABI static CmpPredicate get(const CmpInst *Cmp);
79 
80   /// Get the swapped predicate of a CmpPredicate.
81   LLVM_ABI static CmpPredicate getSwapped(CmpPredicate P);
82 
83   /// Get the swapped predicate of a CmpInst.
84   LLVM_ABI static CmpPredicate getSwapped(const CmpInst *Cmp);
85 };
86 } // namespace llvm
87 
88 #endif
89