Skip to content

Commit 52cc97a

Browse files
committed
[CodeGenPrepare] Zap the argument of llvm.assume when deleting it
We know that the argument is mostly likely dead, so we can purge it early. Otherwise it would make it to codegen, and can block further optimizations.
1 parent 8bd895c commit 52cc97a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,12 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool &ModifiedDT) {
20442044
switch (II->getIntrinsicID()) {
20452045
default: break;
20462046
case Intrinsic::assume: {
2047+
Value *Operand = II->getOperand(0);
20472048
II->eraseFromParent();
2049+
// Prune the operand, it's most likely dead.
2050+
RecursivelyDeleteTriviallyDeadInstructions(
2051+
Operand, TLInfo, nullptr,
2052+
[&](Value *V) { removeAllAssertingVHReferences(V); });
20482053
return true;
20492054
}
20502055

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -codegenprepare -S -mtriple=x86_64-linux < %s | FileCheck %s
3+
4+
define i32 @test1(i8* %d) nounwind {
5+
; CHECK-LABEL: @test1(
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: [[L:%.*]] = load i8, i8* [[D:%.*]], align 1
8+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[L]], 0
9+
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[IF_END:%.*]]
10+
; CHECK: if.end:
11+
; CHECK-NEXT: br label [[EXIT]]
12+
; CHECK: exit:
13+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i8 [[L]], 0
14+
; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[TMP0]] to i32
15+
; CHECK-NEXT: ret i32 [[CONV]]
16+
;
17+
entry:
18+
%l = load i8, i8* %d
19+
%cmp = icmp eq i8 %l, 0
20+
br i1 %cmp, label %exit, label %if.end
21+
22+
if.end:
23+
%gep = getelementptr i8, i8* %d, i32 42
24+
%call = call i64 @foo(i8* %gep) nounwind readonly
25+
%cmp2 = icmp ne i64 %call, 0
26+
call void @llvm.assume(i1 %cmp2)
27+
br label %exit
28+
29+
exit:
30+
%conv = zext i1 %cmp to i32
31+
ret i32 %conv
32+
}
33+
34+
declare i64 @foo(i8*) nounwind readonly
35+
declare void @llvm.assume(i1 noundef) nounwind willreturn

0 commit comments

Comments
 (0)