Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
CmpInstAnalysis.cpp
Go to the documentation of this file.
1//===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
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#include "llvm/Analysis/CmpInstAnalysis.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/PatternMatch.h"
18
19using namespacellvm;
20
21unsignedllvm::getICmpCode(CmpInst::Predicate Pred) {
22switch (Pred) {
23// False -> 0
24case ICmpInst::ICMP_UGT:return 1;// 001
25case ICmpInst::ICMP_SGT:return 1;// 001
26case ICmpInst::ICMP_EQ:return 2;// 010
27case ICmpInst::ICMP_UGE:return 3;// 011
28case ICmpInst::ICMP_SGE:return 3;// 011
29case ICmpInst::ICMP_ULT:return 4;// 100
30case ICmpInst::ICMP_SLT:return 4;// 100
31case ICmpInst::ICMP_NE:return 5;// 101
32case ICmpInst::ICMP_ULE:return 6;// 110
33case ICmpInst::ICMP_SLE:return 6;// 110
34// True -> 7
35default:
36llvm_unreachable("Invalid ICmp predicate!");
37 }
38}
39
40Constant *llvm::getPredForICmpCode(unsigned Code,bool Sign,Type *OpTy,
41CmpInst::Predicate &Pred) {
42switch (Code) {
43default:llvm_unreachable("Illegal ICmp code!");
44case 0:// False.
45return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
46case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;break;
47case 2: Pred = ICmpInst::ICMP_EQ;break;
48case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;break;
49case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;break;
50case 5: Pred = ICmpInst::ICMP_NE;break;
51case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;break;
52case 7:// True.
53return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
54 }
55returnnullptr;
56}
57
58boolllvm::predicatesFoldable(ICmpInst::Predicate P1,ICmpInst::Predicate P2) {
59return (CmpInst::isSigned(P1) ==CmpInst::isSigned(P2)) ||
60 (CmpInst::isSigned(P1) &&ICmpInst::isEquality(P2)) ||
61 (CmpInst::isSigned(P2) &&ICmpInst::isEquality(P1));
62}
63
64Constant *llvm::getPredForFCmpCode(unsigned Code,Type *OpTy,
65CmpInst::Predicate &Pred) {
66 Pred =static_cast<FCmpInst::Predicate>(Code);
67assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&
68"Unexpected FCmp predicate!");
69if (Pred == FCmpInst::FCMP_FALSE)
70return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
71if (Pred == FCmpInst::FCMP_TRUE)
72return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
73returnnullptr;
74}
75
76std::optional<DecomposedBitTest>
77llvm::decomposeBitTestICmp(Value *LHS,Value *RHS,CmpInst::Predicate Pred,
78bool LookThruTrunc,bool AllowNonZeroC) {
79using namespacePatternMatch;
80
81constAPInt *OrigC;
82if (!ICmpInst::isRelational(Pred) || !match(RHS,m_APIntAllowPoison(OrigC)))
83return std::nullopt;
84
85bool Inverted =false;
86if (ICmpInst::isGT(Pred) ||ICmpInst::isGE(Pred)) {
87 Inverted =true;
88 Pred = ICmpInst::getInversePredicate(Pred);
89 }
90
91APIntC = *OrigC;
92if (ICmpInst::isLE(Pred)) {
93if (ICmpInst::isSigned(Pred) ?C.isMaxSignedValue() :C.isMaxValue())
94return std::nullopt;
95 ++C;
96 Pred = ICmpInst::getStrictPredicate(Pred);
97 }
98
99DecomposedBitTest Result;
100switch (Pred) {
101default:
102llvm_unreachable("Unexpected predicate");
103case ICmpInst::ICMP_SLT: {
104// X < 0 is equivalent to (X & SignMask) != 0.
105if (C.isZero()) {
106 Result.Mask =APInt::getSignMask(C.getBitWidth());
107 Result.C =APInt::getZero(C.getBitWidth());
108 Result.Pred = ICmpInst::ICMP_NE;
109break;
110 }
111
112APInt FlippedSign =C ^APInt::getSignMask(C.getBitWidth());
113if (FlippedSign.isPowerOf2()) {
114// X s< 10000100 is equivalent to (X & 11111100 == 10000000)
115 Result.Mask = -FlippedSign;
116 Result.C =APInt::getSignMask(C.getBitWidth());
117 Result.Pred = ICmpInst::ICMP_EQ;
118break;
119 }
120
121if (FlippedSign.isNegatedPowerOf2()) {
122// X s< 01111100 is equivalent to (X & 11111100 != 01111100)
123 Result.Mask = FlippedSign;
124 Result.C =C;
125 Result.Pred = ICmpInst::ICMP_NE;
126break;
127 }
128
129return std::nullopt;
130 }
131case ICmpInst::ICMP_ULT:
132// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
133if (C.isPowerOf2()) {
134 Result.Mask = -C;
135 Result.C =APInt::getZero(C.getBitWidth());
136 Result.Pred = ICmpInst::ICMP_EQ;
137break;
138 }
139
140// X u< 11111100 is equivalent to (X & 11111100 != 11111100)
141if (C.isNegatedPowerOf2()) {
142 Result.Mask =C;
143 Result.C =C;
144 Result.Pred = ICmpInst::ICMP_NE;
145break;
146 }
147
148return std::nullopt;
149 }
150
151if (!AllowNonZeroC && !Result.C.isZero())
152return std::nullopt;
153
154if (Inverted)
155 Result.Pred = ICmpInst::getInversePredicate(Result.Pred);
156
157Value *X;
158if (LookThruTrunc &&match(LHS,m_Trunc(m_Value(X)))) {
159 Result.X =X;
160 Result.Mask = Result.Mask.zext(X->getType()->getScalarSizeInBits());
161 Result.C = Result.C.zext(X->getType()->getScalarSizeInBits());
162 }else {
163 Result.X =LHS;
164 }
165
166return Result;
167}
168
169std::optional<DecomposedBitTest>
170llvm::decomposeBitTest(Value *Cond,bool LookThruTrunc,bool AllowNonZeroC) {
171if (auto *ICmp = dyn_cast<ICmpInst>(Cond)) {
172// Don't allow pointers. Splat vectors are fine.
173if (!ICmp->getOperand(0)->getType()->isIntOrIntVectorTy())
174return std::nullopt;
175returndecomposeBitTestICmp(ICmp->getOperand(0), ICmp->getOperand(1),
176 ICmp->getPredicate(), LookThruTrunc,
177 AllowNonZeroC);
178 }
179
180return std::nullopt;
181}
CmpInstAnalysis.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Instructions.h
PatternMatch.h
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::isNegatedPowerOf2
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition:APInt.h:449
llvm::APInt::getSignMask
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition:APInt.h:229
llvm::APInt::isPowerOf2
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition:APInt.h:440
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition:APInt.h:200
llvm::CmpInst::makeCmpResultType
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition:InstrTypes.h:980
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition:InstrTypes.h:673
llvm::CmpInst::isSigned
bool isSigned() const
Definition:InstrTypes.h:928
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::ICmpInst::isGE
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
Definition:Instructions.h:1329
llvm::ICmpInst::isGT
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
Definition:Instructions.h:1317
llvm::ICmpInst::isEquality
bool isEquality() const
Return true if this predicate is either EQ or NE.
Definition:Instructions.h:1291
llvm::ICmpInst::isRelational
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition:Instructions.h:1305
llvm::ICmpInst::isLE
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
Definition:Instructions.h:1335
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::PatternMatch::m_Trunc
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
Definition:PatternMatch.h:2075
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition:PatternMatch.h:49
llvm::PatternMatch::m_APIntAllowPoison
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition:PatternMatch.h:305
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition:PatternMatch.h:92
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::getPredForFCmpCode
Constant * getPredForFCmpCode(unsigned Code, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getFCmpCode.
Definition:CmpInstAnalysis.cpp:64
llvm::predicatesFoldable
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...
Definition:CmpInstAnalysis.cpp:58
llvm::decomposeBitTest
std::optional< DecomposedBitTest > decomposeBitTest(Value *Cond, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
Definition:CmpInstAnalysis.cpp:170
llvm::decomposeBitTestICmp
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.
Definition:CmpInstAnalysis.cpp:77
llvm::getICmpCode
unsigned getICmpCode(CmpInst::Predicate Pred)
Encode a icmp predicate into a three bit mask.
Definition:CmpInstAnalysis.cpp:21
llvm::getPredForICmpCode
Constant * getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getICmpCode.
Definition:CmpInstAnalysis.cpp:40
llvm::DecomposedBitTest
Represents the operation icmp (X & Mask) pred C, where pred can only be eq or ne.
Definition:CmpInstAnalysis.h:97

Generated on Thu Jul 17 2025 10:49:36 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp