Skip to content

Commit 138fcc5

Browse files
committed
[IRBuilder] Migrate icmp-folding to value-based FoldICmp.
Depends on D116935. Reviewed By: nikic, lebedev.ri Differential Revision: https://reviews.llvm.org/D116969
1 parent 3fd9c90 commit 138fcc5

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

llvm/include/llvm/Analysis/InstSimplifyFolder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class InstSimplifyFolder final : public IRBuilderFolder {
5656
return SimplifyOrInst(LHS, RHS, SQ);
5757
}
5858

59+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
60+
return SimplifyICmpInst(P, LHS, RHS, SQ);
61+
}
62+
5963
//===--------------------------------------------------------------------===//
6064
// Binary Operators
6165
//===--------------------------------------------------------------------===//
@@ -239,10 +243,6 @@ class InstSimplifyFolder final : public IRBuilderFolder {
239243
// Compare Instructions
240244
//===--------------------------------------------------------------------===//
241245

242-
Value *CreateICmp(CmpInst::Predicate P, Constant *LHS,
243-
Constant *RHS) const override {
244-
return ConstFolder.CreateICmp(P, LHS, RHS);
245-
}
246246
Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
247247
Constant *RHS) const override {
248248
return ConstFolder.CreateFCmp(P, LHS, RHS);

llvm/include/llvm/Analysis/TargetFolder.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ class TargetFolder final : public IRBuilderFolder {
6565
return nullptr;
6666
}
6767

68+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
69+
auto *LC = dyn_cast<Constant>(LHS);
70+
auto *RC = dyn_cast<Constant>(RHS);
71+
if (LC && RC)
72+
return ConstantExpr::getCompare(P, LC, RC);
73+
return nullptr;
74+
}
75+
6876
//===--------------------------------------------------------------------===//
6977
// Binary Operators
7078
//===--------------------------------------------------------------------===//
@@ -247,10 +255,6 @@ class TargetFolder final : public IRBuilderFolder {
247255
// Compare Instructions
248256
//===--------------------------------------------------------------------===//
249257

250-
Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
251-
Constant *RHS) const override {
252-
return Fold(ConstantExpr::getCompare(P, LHS, RHS));
253-
}
254258
Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
255259
Constant *RHS) const override {
256260
return Fold(ConstantExpr::getCompare(P, LHS, RHS));

llvm/include/llvm/IR/ConstantFolder.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class ConstantFolder final : public IRBuilderFolder {
5454
return nullptr;
5555
}
5656

57+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
58+
auto *LC = dyn_cast<Constant>(LHS);
59+
auto *RC = dyn_cast<Constant>(RHS);
60+
if (LC && RC)
61+
return ConstantExpr::getCompare(P, LC, RC);
62+
return nullptr;
63+
}
64+
5765
//===--------------------------------------------------------------------===//
5866
// Binary Operators
5967
//===--------------------------------------------------------------------===//
@@ -254,11 +262,6 @@ class ConstantFolder final : public IRBuilderFolder {
254262
// Compare Instructions
255263
//===--------------------------------------------------------------------===//
256264

257-
Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
258-
Constant *RHS) const override {
259-
return ConstantExpr::getCompare(P, LHS, RHS);
260-
}
261-
262265
Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
263266
Constant *RHS) const override {
264267
return ConstantExpr::getCompare(P, LHS, RHS);

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,9 +2219,8 @@ class IRBuilderBase {
22192219

22202220
Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
22212221
const Twine &Name = "") {
2222-
if (auto *LC = dyn_cast<Constant>(LHS))
2223-
if (auto *RC = dyn_cast<Constant>(RHS))
2224-
return Insert(Folder.CreateICmp(P, LC, RC), Name);
2222+
if (auto *V = Folder.FoldICmp(P, LHS, RHS))
2223+
return V;
22252224
return Insert(new ICmpInst(P, LHS, RHS), Name);
22262225
}
22272226

llvm/include/llvm/IR/IRBuilderFolder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class IRBuilderFolder {
3535
bool HasNSW = false) const = 0;
3636
virtual Value *FoldOr(Value *LHS, Value *RHS) const = 0;
3737

38+
virtual Value *FoldICmp(CmpInst::Predicate P, Value *LHS,
39+
Value *RHS) const = 0;
40+
3841
//===--------------------------------------------------------------------===//
3942
// Binary Operators
4043
//===--------------------------------------------------------------------===//
@@ -121,8 +124,6 @@ class IRBuilderFolder {
121124
// Compare Instructions
122125
//===--------------------------------------------------------------------===//
123126

124-
virtual Value *CreateICmp(CmpInst::Predicate P, Constant *LHS,
125-
Constant *RHS) const = 0;
126127
virtual Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
127128
Constant *RHS) const = 0;
128129

llvm/include/llvm/IR/NoFolder.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class NoFolder final : public IRBuilderFolder {
5050

5151
Value *FoldOr(Value *LHS, Value *RHS) const override { return nullptr; }
5252

53+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
54+
return nullptr;
55+
}
56+
5357
//===--------------------------------------------------------------------===//
5458
// Binary Operators
5559
//===--------------------------------------------------------------------===//
@@ -270,11 +274,6 @@ class NoFolder final : public IRBuilderFolder {
270274
// Compare Instructions
271275
//===--------------------------------------------------------------------===//
272276

273-
Instruction *CreateICmp(CmpInst::Predicate P,
274-
Constant *LHS, Constant *RHS) const override {
275-
return new ICmpInst(P, LHS, RHS);
276-
}
277-
278277
Instruction *CreateFCmp(CmpInst::Predicate P,
279278
Constant *LHS, Constant *RHS) const override {
280279
return new FCmpInst(P, LHS, RHS);

llvm/test/Transforms/LoopVectorize/pr30654-phiscev-sext-trunc.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ define void @doit2(i32 %n, i32 %step) local_unnamed_addr {
177177
; CHECK-NEXT: [[MUL_RESULT:%.*]] = extractvalue { i8, i1 } [[MUL]], 0
178178
; CHECK-NEXT: [[MUL_OVERFLOW:%.*]] = extractvalue { i8, i1 } [[MUL]], 1
179179
; CHECK-NEXT: [[TMP7:%.*]] = sub i8 0, [[MUL_RESULT]]
180-
; CHECK-NEXT: [[TMP8:%.*]] = icmp ult i8 [[MUL_RESULT]], 0
181180
; CHECK-NEXT: [[TMP9:%.*]] = icmp ugt i8 [[TMP7]], 0
182-
; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[TMP3]], i1 [[TMP9]], i1 [[TMP8]]
181+
; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[TMP3]], i1 [[TMP9]], i1 false
183182
; CHECK-NEXT: [[TMP14:%.*]] = or i1 [[TMP10]], [[MUL_OVERFLOW]]
184183
; CHECK-NEXT: [[TMP11:%.*]] = icmp ugt i64 [[TMP0]], 255
185184
; CHECK-NEXT: [[TMP12:%.*]] = icmp ne i8 [[TMP1]], 0

llvm/test/Transforms/LoopVersioning/lcssa.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,8 @@ define void @fill_no_null_opt(i8** %ls1.20, i8** %ls2.21, i8* %cse3.22) #0 {
5858
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
5959
; CHECK-NEXT: [[SCEVGEP3:%.*]] = getelementptr i8, i8* [[LS1_20_PROMOTED]], i64 -1
6060
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i8, i8* [[SCEVGEP3]], i64 0
61-
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8* [[TMP0]], [[SCEVGEP3]]
6261
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, i8* [[LS1_20_PROMOTED]], i64 0
63-
; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i8* [[TMP2]], [[LS1_20_PROMOTED]]
64-
; CHECK-NEXT: [[TMP4:%.*]] = or i1 [[TMP1]], [[TMP3]]
65-
; CHECK-NEXT: [[LVER_SAFE:%.*]] = or i1 [[FOUND_CONFLICT]], [[TMP4]]
66-
; CHECK-NEXT: br i1 [[LVER_SAFE]], label %bb1.ph.lver.orig, label %bb1.ph
62+
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %bb1.ph.lver.orig, label %bb1.ph
6763
; CHECK: bb1.ph.lver.orig:
6864
;
6965
bb1.ph:

0 commit comments

Comments
 (0)