LLVM 24.0.0git
CombinerHelperCompares.cpp
Go to the documentation of this file.
1//===- CombinerHelperCompares.cpp------------------------------------------===//
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 implements CombinerHelper for G_ICMP.
10//
11//===----------------------------------------------------------------------===//
23
24#define DEBUG_TYPE "gi-combiner"
25
26using namespace llvm;
27
28bool CombinerHelper::constantFoldICmp(const GICmp &ICmp,
29 const GIConstant &LHSCst,
30 const GIConstant &RHSCst,
31 BuildFnTy &MatchInfo) const {
33 return false;
34
35 Register Dst = ICmp.getReg(0);
36 LLT DstTy = MRI.getType(Dst);
37
39 return false;
40
41 CmpInst::Predicate Pred = ICmp.getCond();
42 APInt LHS = LHSCst.getScalarValue();
43 APInt RHS = RHSCst.getScalarValue();
44
45 bool Result = ICmpInst::compare(LHS, RHS, Pred);
46
47 MatchInfo = [=](MachineIRBuilder &B) {
48 if (Result)
49 B.buildConstant(Dst, getICmpTrueVal(getTargetLowering(),
50 /*IsVector=*/DstTy.isVector(),
51 /*IsFP=*/false));
52 else
53 B.buildConstant(Dst, 0);
54 };
55
56 return true;
57}
58
59bool CombinerHelper::constantFoldFCmp(const GFCmp &FCmp,
60 const GFConstant &LHSCst,
61 const GFConstant &RHSCst,
62 BuildFnTy &MatchInfo) const {
64 return false;
65
66 Register Dst = FCmp.getReg(0);
67 LLT DstTy = MRI.getType(Dst);
68
70 return false;
71
72 CmpInst::Predicate Pred = FCmp.getCond();
73 APFloat LHS = LHSCst.getScalarValue();
74 APFloat RHS = RHSCst.getScalarValue();
75
76 bool Result = FCmpInst::compare(LHS, RHS, Pred);
77
78 MatchInfo = [=](MachineIRBuilder &B) {
79 if (Result)
80 B.buildConstant(Dst, getICmpTrueVal(getTargetLowering(),
81 /*IsVector=*/DstTy.isVector(),
82 /*IsFP=*/true));
83 else
84 B.buildConstant(Dst, 0);
85 };
86
87 return true;
88}
89
91 BuildFnTy &MatchInfo) const {
92 const GICmp *Cmp = cast<GICmp>(&MI);
93
94 Register Dst = Cmp->getReg(0);
95 Register LHS = Cmp->getLHSReg();
96 Register RHS = Cmp->getRHSReg();
97
98 CmpInst::Predicate Pred = Cmp->getCond();
99 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
100 if (auto CLHS = GIConstant::getConstant(LHS, MRI)) {
101 if (auto CRHS = GIConstant::getConstant(RHS, MRI))
102 return constantFoldICmp(*Cmp, *CLHS, *CRHS, MatchInfo);
103
104 // If we have a constant, make sure it is on the RHS.
105 std::swap(LHS, RHS);
106 Pred = CmpInst::getSwappedPredicate(Pred);
107
108 MatchInfo = [=](MachineIRBuilder &B) { B.buildICmp(Pred, Dst, LHS, RHS); };
109 return true;
110 }
111
112 return false;
113}
114
116 BuildFnTy &MatchInfo) const {
117 const GFCmp *Cmp = cast<GFCmp>(&MI);
118
119 Register Dst = Cmp->getReg(0);
120 Register LHS = Cmp->getLHSReg();
121 Register RHS = Cmp->getRHSReg();
122
123 CmpInst::Predicate Pred = Cmp->getCond();
124 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
125
126 if (auto CLHS = GFConstant::getConstant(LHS, MRI)) {
127 if (auto CRHS = GFConstant::getConstant(RHS, MRI))
128 return constantFoldFCmp(*Cmp, *CLHS, *CRHS, MatchInfo);
129
130 // If we have a constant, make sure it is on the RHS.
131 std::swap(LHS, RHS);
132 Pred = CmpInst::getSwappedPredicate(Pred);
133
134 MatchInfo = [=](MachineIRBuilder &B) {
135 B.buildFCmp(Pred, Dst, LHS, RHS, Cmp->getFlags());
136 };
137 return true;
138 }
139
140 return false;
141}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This contains common combine transformations that may be used in a combine pass,or by the target else...
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
Interface for Targets to specify which operations they can successfully select and how the others sho...
This file declares the MachineIRBuilder class.
Promote Memory to Register
Definition Mem2Reg.cpp:110
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:740
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
LLVM_ABI const TargetLowering & getTargetLowering() const
LLVM_ABI bool matchCanonicalizeFCmp(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool isConstantLegalOrBeforeLegalizer(const LLT Ty) const
MachineRegisterInfo & MRI
LLVM_ABI bool matchCanonicalizeICmp(const MachineInstr &MI, BuildFnTy &MatchInfo) const
static LLVM_ABI bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
CmpInst::Predicate getCond() const
Represent a G_FCMP.
An floating-point-like constant.
Definition Utils.h:692
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2037
GFConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:711
LLVM_ABI APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2030
Represent a G_ICMP.
An integer-like constant.
Definition Utils.h:653
LLVM_ABI APInt getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:1990
static LLVM_ABI std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1997
GIConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:669
Register getReg(unsigned Idx) const
Access the Idx'th operand as a register and return it.
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isVector() const
Helper class to build MachineInstr.
Representation of each machine instruction.
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This is an optimization pass for GlobalISel generic memory operations.
std::function< void(MachineIRBuilder &)> BuildFnTy
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
Definition Utils.cpp:1629
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880