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
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 else
52 B.buildConstant(Dst, 0);
53 };
54
55 return true;
56}
57
58bool CombinerHelper::constantFoldFCmp(const GFCmp &FCmp,
59 const GFConstant &LHSCst,
60 const GFConstant &RHSCst,
61 BuildFnTy &MatchInfo) const {
63 return false;
64
65 Register Dst = FCmp.getReg(0);
66 LLT DstTy = MRI.getType(Dst);
67
69 return false;
70
72 APFloat LHS = LHSCst.getScalarValue();
73 APFloat RHS = RHSCst.getScalarValue();
74
76
77 MatchInfo = [=](MachineIRBuilder &B) {
78 if (Result)
79 B.buildConstant(Dst, getICmpTrueVal(getTargetLowering(),
80 /*IsVector=*/DstTy.isVector()));
81 else
82 B.buildConstant(Dst, 0);
83 };
84
85 return true;
86}
87
89 BuildFnTy &MatchInfo) const {
90 const GICmp *Cmp = cast<GICmp>(&MI);
91
92 Register Dst = Cmp->getReg(0);
93 Register LHS = Cmp->getLHSReg();
94 Register RHS = Cmp->getRHSReg();
95
96 CmpInst::Predicate Pred = Cmp->getCond();
97 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
98 if (auto CLHS = GIConstant::getConstant(LHS, MRI)) {
99 if (auto CRHS = GIConstant::getConstant(RHS, MRI))
100 return constantFoldICmp(*Cmp, *CLHS, *CRHS, MatchInfo);
101
102 // If we have a constant, make sure it is on the RHS.
103 std::swap(LHS, RHS);
104 Pred = CmpInst::getSwappedPredicate(Pred);
105
106 MatchInfo = [=](MachineIRBuilder &B) { B.buildICmp(Pred, Dst, LHS, RHS); };
107 return true;
108 }
109
110 return false;
111}
112
114 BuildFnTy &MatchInfo) const {
115 const GFCmp *Cmp = cast<GFCmp>(&MI);
116
117 Register Dst = Cmp->getReg(0);
118 Register LHS = Cmp->getLHSReg();
119 Register RHS = Cmp->getRHSReg();
120
121 CmpInst::Predicate Pred = Cmp->getCond();
122 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
123
124 if (auto CLHS = GFConstant::getConstant(LHS, MRI)) {
125 if (auto CRHS = GFConstant::getConstant(RHS, MRI))
126 return constantFoldFCmp(*Cmp, *CLHS, *CRHS, MatchInfo);
127
128 // If we have a constant, make sure it is on the RHS.
129 std::swap(LHS, RHS);
130 Pred = CmpInst::getSwappedPredicate(Pred);
131
132 MatchInfo = [=](MachineIRBuilder &B) {
133 B.buildFCmp(Pred, Dst, LHS, RHS, Cmp->getFlags());
134 };
135 return true;
136 }
137
138 return false;
139}
SDValue Pred
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
MachineRegisterInfo & MRI
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:659
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2203
GFConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:678
LLVM_ABI APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2196
Represent a G_ICMP.
An integer-like constant.
Definition Utils.h:620
LLVM_ABI APInt getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2156
static LLVM_ABI std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2163
GIConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:636
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.
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
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector)
Returns an integer representing true, as defined by the TargetBooleanContents.
Definition Utils.cpp:1787
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881