Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:57

0001 //===- CmpPredicate.h - CmpInst Predicate with samesign information -------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // A CmpInst::Predicate with any samesign information (applicable to ICmpInst).
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_CMPPREDICATE_H
0014 #define LLVM_IR_CMPPREDICATE_H
0015 
0016 #include "llvm/IR/InstrTypes.h"
0017 
0018 namespace llvm {
0019 /// An abstraction over a floating-point predicate, and a pack of an integer
0020 /// predicate with samesign information. Some functions in ICmpInst construct
0021 /// and return this type in place of a Predicate.
0022 class CmpPredicate {
0023   CmpInst::Predicate Pred;
0024   bool HasSameSign;
0025 
0026 public:
0027   /// Default constructor.
0028   CmpPredicate() : Pred(CmpInst::BAD_ICMP_PREDICATE), HasSameSign(false) {}
0029 
0030   /// Constructed implictly with a either Predicate and samesign information, or
0031   /// just a Predicate, dropping samesign information.
0032   CmpPredicate(CmpInst::Predicate Pred, bool HasSameSign = false)
0033       : Pred(Pred), HasSameSign(HasSameSign) {
0034     assert(!HasSameSign || CmpInst::isIntPredicate(Pred));
0035   }
0036 
0037   /// Implictly converts to the underlying Predicate, dropping samesign
0038   /// information.
0039   operator CmpInst::Predicate() const { return Pred; }
0040 
0041   /// Query samesign information, for optimizations.
0042   bool hasSameSign() const { return HasSameSign; }
0043 
0044   /// Compares two CmpPredicates taking samesign into account and returns the
0045   /// canonicalized CmpPredicate if they match. An alternative to operator==.
0046   ///
0047   /// For example,
0048   ///   samesign ult + samesign ult -> samesign ult
0049   ///   samesign ult + ult -> ult
0050   ///   samesign ult + slt -> slt
0051   ///   ult + ult -> ult
0052   ///   ult + slt -> std::nullopt
0053   static std::optional<CmpPredicate> getMatching(CmpPredicate A,
0054                                                  CmpPredicate B);
0055 
0056   /// Attempts to return a signed CmpInst::Predicate from the CmpPredicate. If
0057   /// the CmpPredicate has samesign, return ICmpInst::getSignedPredicate,
0058   /// dropping samesign information. Otherwise, return the predicate, dropping
0059   /// samesign information.
0060   CmpInst::Predicate getPreferredSignedPredicate() const;
0061 
0062   /// An operator== on the underlying Predicate.
0063   bool operator==(CmpInst::Predicate P) const { return Pred == P; }
0064   bool operator!=(CmpInst::Predicate P) const { return Pred != P; }
0065 
0066   /// There is no operator== defined on CmpPredicate. Use getMatching instead to
0067   /// get the canonicalized matching CmpPredicate.
0068   bool operator==(CmpPredicate) const = delete;
0069   bool operator!=(CmpPredicate) const = delete;
0070 
0071   /// Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as
0072   /// appropriate.
0073   static CmpPredicate get(const CmpInst *Cmp);
0074 
0075   /// Get the swapped predicate of a CmpPredicate.
0076   static CmpPredicate getSwapped(CmpPredicate P);
0077 
0078   /// Get the swapped predicate of a CmpInst.
0079   static CmpPredicate getSwapped(const CmpInst *Cmp);
0080 };
0081 } // namespace llvm
0082 
0083 #endif