Skip to content

Commit f1106ef

Browse files
committed
[InstCombine] Remove computeKnownBits() fold for returns
We try to fold constant computeKnownBits() with context for return instructions only. Otherwise, we rely on SimplifyDemandedBits() to fold instructions with constant known bits. The presence of this special fold for returns is dangerous, because it makes our tests lie about what works and what doesn't. Tests are usually written by returning the result we're interested in, but will go through this separate code path that is not used for anything else. This patch removes the special fold. This primarily regresses patterns of the style "assume(x); return x". The responsibility of handling such patterns lies with passes like EarlyCSE/GVN anyway, which will do this reliably, and not just for returns. Differential Revision: https://reviews.llvm.org/D151099
1 parent c4efcd6 commit f1106ef

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,25 +2471,7 @@ static bool isMustTailCall(Value *V) {
24712471
}
24722472

24732473
Instruction *InstCombinerImpl::visitReturnInst(ReturnInst &RI) {
2474-
if (RI.getNumOperands() == 0) // ret void
2475-
return nullptr;
2476-
2477-
Value *ResultOp = RI.getOperand(0);
2478-
Type *VTy = ResultOp->getType();
2479-
if (!VTy->isIntegerTy() || isa<Constant>(ResultOp))
2480-
return nullptr;
2481-
2482-
// Don't replace result of musttail calls.
2483-
if (isMustTailCall(ResultOp))
2484-
return nullptr;
2485-
2486-
// There might be assume intrinsics dominating this return that completely
2487-
// determine the value. If so, constant fold it.
2488-
KnownBits Known = computeKnownBits(ResultOp, 0, &RI);
2489-
if (Known.isConstant())
2490-
return replaceOperand(RI, 0,
2491-
Constant::getIntegerValue(VTy, Known.getConstant()));
2492-
2474+
// Nothing for now.
24932475
return nullptr;
24942476
}
24952477

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,DEFAULT %s
3-
; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,BUNDLES %s
2+
; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,DEFAULT %s
3+
; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,BUNDLES %s
44

55
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
66
target triple = "x86_64-unknown-linux-gnu"
@@ -60,7 +60,7 @@ define i32 @simple(i32 %a) #1 {
6060
; CHECK-LABEL: @simple(
6161
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 4
6262
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
63-
; CHECK-NEXT: ret i32 4
63+
; CHECK-NEXT: ret i32 [[A]]
6464
;
6565
%cmp = icmp eq i32 %a, 4
6666
tail call void @llvm.assume(i1 %cmp)
@@ -204,7 +204,8 @@ define i32 @icmp1(i32 %a) #0 {
204204
; CHECK-LABEL: @icmp1(
205205
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 5
206206
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
207-
; CHECK-NEXT: ret i32 1
207+
; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32
208+
; CHECK-NEXT: ret i32 [[CONV]]
208209
;
209210
%cmp = icmp sgt i32 %a, 5
210211
tail call void @llvm.assume(i1 %cmp)
@@ -231,7 +232,7 @@ define i1 @assume_not(i1 %cond) {
231232
; CHECK-LABEL: @assume_not(
232233
; CHECK-NEXT: [[NOTCOND:%.*]] = xor i1 [[COND:%.*]], true
233234
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
234-
; CHECK-NEXT: ret i1 false
235+
; CHECK-NEXT: ret i1 [[COND]]
235236
;
236237
%notcond = xor i1 %cond, true
237238
call void @llvm.assume(i1 %notcond)
@@ -382,10 +383,7 @@ define i1 @nonnull5(ptr %a) {
382383

383384
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
384385
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
385-
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], 3
386386
; CHECK-NEXT: tail call void @llvm.assume(i1 false)
387-
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND1]], 0
388-
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
389387
; CHECK-NEXT: ret i32 0
390388
;
391389
%and1 = and i32 %b, 3
@@ -451,7 +449,7 @@ define i1 @nonnull3A(ptr %a, i1 %control) {
451449
; DEFAULT: taken:
452450
; DEFAULT-NEXT: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
453451
; DEFAULT-NEXT: call void @llvm.assume(i1 [[CMP]])
454-
; DEFAULT-NEXT: ret i1 true
452+
; DEFAULT-NEXT: ret i1 [[CMP]]
455453
; DEFAULT: not_taken:
456454
; DEFAULT-NEXT: [[RVAL_2:%.*]] = icmp sgt ptr [[LOAD]], null
457455
; DEFAULT-NEXT: ret i1 [[RVAL_2]]
@@ -487,7 +485,7 @@ define i1 @nonnull3B(ptr %a, i1 %control) {
487485
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[A:%.*]], align 8
488486
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
489487
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]]) [ "nonnull"(ptr [[LOAD]]) ]
490-
; CHECK-NEXT: ret i1 true
488+
; CHECK-NEXT: ret i1 [[CMP]]
491489
; CHECK: not_taken:
492490
; CHECK-NEXT: ret i1 [[CONTROL]]
493491
;

llvm/test/Transforms/InstCombine/known-phi-br.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ define i64 @limit_i64_eq_7(i64 %x) {
1515
; CHECK: body:
1616
; CHECK-NEXT: br label [[END]]
1717
; CHECK: end:
18-
; CHECK-NEXT: ret i64 7
18+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 7, [[BODY]] ]
19+
; CHECK-NEXT: ret i64 [[RES]]
1920
;
2021
entry:
2122
%cmp = icmp eq i64 %x, 7
@@ -37,7 +38,8 @@ define i64 @limit_i64_ne_255(i64 %x) {
3738
; CHECK: body:
3839
; CHECK-NEXT: br label [[END]]
3940
; CHECK: end:
40-
; CHECK-NEXT: ret i64 255
41+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 255, [[BODY]] ]
42+
; CHECK-NEXT: ret i64 [[RES]]
4143
;
4244
entry:
4345
%cmp = icmp ne i64 %x, 255

llvm/test/Transforms/InstCombine/zext-or-icmp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ define i1 @PR51762(ptr %i, i32 %t0, i16 %t1, ptr %p, ptr %d, ptr %f, i32 %p2, i1
251251
; CHECK-NEXT: store i32 [[SROA38]], ptr [[D]], align 8
252252
; CHECK-NEXT: [[R:%.*]] = icmp ult i64 [[INSERT_INSERT41]], [[CONV19]]
253253
; CHECK-NEXT: call void @llvm.assume(i1 [[R]])
254-
; CHECK-NEXT: ret i1 true
254+
; CHECK-NEXT: ret i1 [[R]]
255255
;
256256
entry:
257257
br label %for.cond

0 commit comments

Comments
 (0)