Skip to content

Commit eeb6ffc

Browse files
committed
IR: introduce ICmpInst::PredicateSign
Introduce CmpInst::PredicateSign, an abstraction over a floating-point predicate, and a pack of an integer predicate with samesign information, in order to ease extending large portions of the codebase that take a CmpInst::Predicate to respect the samesign flag. We have chosen to demonstrate the utility of this new abstraction by migrating ValueTracking, InstructionSimplify, and InstCombine from CmpInst::Predicate to CmpInst::PredicateSign. There should be no functional changes, as we don't perform any extra optimizations with samesign in this patch. The design approach taken by this patch allows for unaudited callers of APIs that take a CmpInst::PredicateSign to silently drop the samesign information; it does not pose a correctness issue, and allows us to migrate the codebase piece-wise.
1 parent 9568f88 commit eeb6ffc

File tree

11 files changed

+151
-109
lines changed

11 files changed

+151
-109
lines changed

llvm/include/llvm/Analysis/InstSimplifyFolder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/ArrayRef.h"
2323
#include "llvm/Analysis/InstructionSimplify.h"
2424
#include "llvm/Analysis/TargetFolder.h"
25+
#include "llvm/IR/CmpPredicate.h"
2526
#include "llvm/IR/IRBuilderFolder.h"
2627
#include "llvm/IR/Instruction.h"
2728

llvm/include/llvm/Analysis/InstructionSimplify.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ template <class T, unsigned n> class SmallSetVector;
5151
class TargetLibraryInfo;
5252
class Type;
5353
class Value;
54+
class CmpPredicate;
5455

5556
// NOTE: the explicit multiple argument versions of these functions are
5657
// deprecated.
@@ -152,11 +153,11 @@ Value *simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
152153
Value *simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
153154

154155
/// Given operands for an ICmpInst, fold the result or return null.
155-
Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
156+
Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
156157
const SimplifyQuery &Q);
157158

158159
/// Given operands for an FCmpInst, fold the result or return null.
159-
Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
160+
Value *simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
160161
FastMathFlags FMF, const SimplifyQuery &Q);
161162

162163
/// Given operands for a SelectInst, fold the result or return null.
@@ -200,7 +201,7 @@ Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef<int> Mask,
200201
//=== Helper functions for higher up the class hierarchy.
201202

202203
/// Given operands for a CmpInst, fold the result or return null.
203-
Value *simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
204+
Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
204205
const SimplifyQuery &Q);
205206

206207
/// Given operand for a UnaryOperator, fold the result or return null.

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,7 @@ std::optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS,
12451245
const DataLayout &DL,
12461246
bool LHSIsTrue = true,
12471247
unsigned Depth = 0);
1248-
std::optional<bool> isImpliedCondition(const Value *LHS,
1249-
CmpInst::Predicate RHSPred,
1248+
std::optional<bool> isImpliedCondition(const Value *LHS, CmpPredicate RHSPred,
12501249
const Value *RHSOp0, const Value *RHSOp1,
12511250
const DataLayout &DL,
12521251
bool LHSIsTrue = true,
@@ -1257,8 +1256,8 @@ std::optional<bool> isImpliedCondition(const Value *LHS,
12571256
std::optional<bool> isImpliedByDomCondition(const Value *Cond,
12581257
const Instruction *ContextI,
12591258
const DataLayout &DL);
1260-
std::optional<bool> isImpliedByDomCondition(CmpInst::Predicate Pred,
1261-
const Value *LHS, const Value *RHS,
1259+
std::optional<bool> isImpliedByDomCondition(CmpPredicate Pred, const Value *LHS,
1260+
const Value *RHS,
12621261
const Instruction *ContextI,
12631262
const DataLayout &DL);
12641263

llvm/include/llvm/IR/CmpPredicate.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- CmpPredicate.h - CmpInst Predicate with samesign information -------===//
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+
// A CmpInst::Predicate with any samesign information (applicable to ICmpInst).
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_IR_CMPPREDICATE_H
14+
#define LLVM_IR_CMPPREDICATE_H
15+
16+
#include "llvm/IR/InstrTypes.h"
17+
18+
namespace llvm {
19+
/// An abstraction over a floating-point predicate, and a pack of an integer
20+
/// predicate with samesign information. The getCmpPredicate() family of
21+
/// functions in ICmpInst construct and return this type. It is also implictly
22+
/// constructed with a Predicate, dropping samesign information.
23+
class CmpPredicate {
24+
CmpInst::Predicate Pred;
25+
bool HasSameSign;
26+
27+
public:
28+
CmpPredicate(CmpInst::Predicate Pred, bool HasSameSign = false)
29+
: Pred(Pred), HasSameSign(HasSameSign) {}
30+
31+
operator CmpInst::Predicate() { return Pred; }
32+
33+
bool hasSameSign() {
34+
assert(CmpInst::isIntPredicate(Pred));
35+
return HasSameSign;
36+
}
37+
};
38+
} // namespace llvm
39+
40+
#endif

llvm/include/llvm/IR/Instructions.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/iterator.h"
2525
#include "llvm/ADT/iterator_range.h"
2626
#include "llvm/IR/CFG.h"
27+
#include "llvm/IR/CmpPredicate.h"
2728
#include "llvm/IR/Constant.h"
2829
#include "llvm/IR/DerivedTypes.h"
2930
#include "llvm/IR/GEPNoWrapFlags.h"
@@ -1203,6 +1204,21 @@ class ICmpInst: public CmpInst {
12031204
#endif
12041205
}
12051206

1207+
/// @returns the predicate along with samesign information.
1208+
CmpPredicate getCmpPredicate() const {
1209+
return {getPredicate(), hasSameSign()};
1210+
}
1211+
1212+
/// @returns the inverse predicate along with samesign information.
1213+
CmpPredicate getInverseCmpPredicate() const {
1214+
return {getInversePredicate(), hasSameSign()};
1215+
}
1216+
1217+
/// @returns the swapped predicate along with samesign information.
1218+
CmpPredicate getSwappedCmpPredicate() const {
1219+
return {getSwappedPredicate(), hasSameSign()};
1220+
}
1221+
12061222
/// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
12071223
/// @returns the predicate that would be the result if the operand were
12081224
/// regarded as signed.

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
157157
/// conditional branch or select to create a compare with a canonical
158158
/// (inverted) predicate which is then more likely to be matched with other
159159
/// values.
160-
static bool isCanonicalPredicate(CmpInst::Predicate Pred) {
160+
static bool isCanonicalPredicate(CmpPredicate Pred) {
161161
switch (Pred) {
162162
case CmpInst::ICMP_NE:
163163
case CmpInst::ICMP_ULE:
@@ -185,10 +185,9 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
185185
}
186186

187187
std::optional<std::pair<
188-
CmpInst::Predicate,
189-
Constant *>> static getFlippedStrictnessPredicateAndConstant(CmpInst::
190-
Predicate
191-
Pred,
188+
CmpPredicate,
189+
Constant *>> static getFlippedStrictnessPredicateAndConstant(CmpPredicate
190+
Pred,
192191
Constant *C);
193192

194193
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI) {

0 commit comments

Comments
 (0)