LLVM 20.0.0git
CmpInstAnalysis.h
Go to the documentation of this file.
1//===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
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// This file holds routines to help analyse compare instructions
10// and fold them into constants or other compare instructions
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
15#define LLVM_ANALYSIS_CMPINSTANALYSIS_H
16
17#include "llvm/ADT/APInt.h"
18#include "llvm/IR/InstrTypes.h"
19
20namespace llvm {
21 class Type;
22 class Value;
23
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
49 /// This is the complement of getICmpCode. It turns a predicate code into
50 /// either a constant true or false or the predicate for a new ICmp.
51 /// The sign is passed in to determine which kind of predicate to use in the
52 /// new ICmp instruction.
53 /// Non-NULL return value will be a true or false constant.
54 /// NULL return means a new ICmp is needed. The predicate is output in Pred.
55 Constant *getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
56 CmpInst::Predicate &Pred);
57
58 /// Return true if both predicates match sign or if at least one of them is an
59 /// equality comparison (which is signless).
61
62 /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate
63 /// into a four bit mask.
66 "Unexpected FCmp predicate!");
67 // Take advantage of the bit pattern of CmpInst::Predicate here.
68 // U L G E
69 static_assert(CmpInst::FCMP_FALSE == 0); // 0 0 0 0
70 static_assert(CmpInst::FCMP_OEQ == 1); // 0 0 0 1
71 static_assert(CmpInst::FCMP_OGT == 2); // 0 0 1 0
72 static_assert(CmpInst::FCMP_OGE == 3); // 0 0 1 1
73 static_assert(CmpInst::FCMP_OLT == 4); // 0 1 0 0
74 static_assert(CmpInst::FCMP_OLE == 5); // 0 1 0 1
75 static_assert(CmpInst::FCMP_ONE == 6); // 0 1 1 0
76 static_assert(CmpInst::FCMP_ORD == 7); // 0 1 1 1
77 static_assert(CmpInst::FCMP_UNO == 8); // 1 0 0 0
78 static_assert(CmpInst::FCMP_UEQ == 9); // 1 0 0 1
79 static_assert(CmpInst::FCMP_UGT == 10); // 1 0 1 0
80 static_assert(CmpInst::FCMP_UGE == 11); // 1 0 1 1
81 static_assert(CmpInst::FCMP_ULT == 12); // 1 1 0 0
82 static_assert(CmpInst::FCMP_ULE == 13); // 1 1 0 1
83 static_assert(CmpInst::FCMP_UNE == 14); // 1 1 1 0
84 static_assert(CmpInst::FCMP_TRUE == 15); // 1 1 1 1
85 return CC;
86 }
87
88 /// This is the complement of getFCmpCode. It turns a predicate code into
89 /// either a constant true or false or the predicate for a new FCmp.
90 /// Non-NULL return value will be a true or false constant.
91 /// NULL return means a new ICmp is needed. The predicate is output in Pred.
92 Constant *getPredForFCmpCode(unsigned Code, Type *OpTy,
93 CmpInst::Predicate &Pred);
94
95 /// Represents the operation icmp (X & Mask) pred C, where pred can only be
96 /// eq or ne.
102 };
103
104 /// Decompose an icmp into the form ((X & Mask) pred C) if possible.
105 /// Unless \p AllowNonZeroC is true, C will always be 0.
106 std::optional<DecomposedBitTest>
108 bool LookThroughTrunc = true,
109 bool AllowNonZeroC = false);
110
111} // end namespace llvm
112
113#endif
This file implements a class to represent arbitrary precision integral constant values and operations...
RelocType Type
Definition: COFFYAML.cpp:410
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Constant * getPredForFCmpCode(unsigned Code, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getFCmpCode.
bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2)
Return true if both predicates match sign or if at least one of them is an equality comparison (which...
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
unsigned getICmpCode(CmpInst::Predicate Pred)
Encode a icmp predicate into a three bit mask.
unsigned getFCmpCode(CmpInst::Predicate CC)
Similar to getICmpCode but for FCmpInst.
Constant * getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getICmpCode.
Represents the operation icmp (X & Mask) pred C, where pred can only be eq or ne.
CmpInst::Predicate Pred