Skip to content

Commit db56106

Browse files
committed
[GlobalOpt] Handle non-instruction MTI source (PR54572)
This was reusing a cast to GlobalVariable to check for an Instruction, which means we'll try to dereference a null pointer if it's not actually a GlobalVariable. We should be casting MTI->getSource() instead. I don't think this problem is really specific to opaque pointers, but it certainly makes it a lot easier to reproduce. Fixes llvm/llvm-project#54572.
1 parent 534b228 commit db56106

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ CleanupPointerRootUsers(GlobalVariable *GV,
230230
if (MemSrc && MemSrc->isConstant()) {
231231
Changed = true;
232232
MTI->eraseFromParent();
233-
} else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
233+
} else if (Instruction *I = dyn_cast<Instruction>(MTI->getSource())) {
234234
if (I->hasOneUse())
235235
Dead.push_back(std::make_pair(I, MTI));
236236
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
2+
; RUN: opt -S -globalopt < %s | FileCheck %s
3+
4+
@b = internal global ptr null
5+
@c = internal global [2 x ptr] zeroinitializer
6+
7+
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg)
8+
9+
;.
10+
; CHECK: @[[B:[a-zA-Z0-9_$"\\.-]+]] = internal unnamed_addr global ptr null
11+
; CHECK: @[[C:[a-zA-Z0-9_$"\\.-]+]] = internal unnamed_addr constant [2 x ptr] zeroinitializer
12+
;.
13+
define void @test() {
14+
; CHECK-LABEL: @test(
15+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr @b, ptr getelementptr inbounds ([2 x ptr], ptr @c, i64 0, i64 1), i64 8, i1 false)
16+
; CHECK-NEXT: ret void
17+
;
18+
call void @llvm.memcpy.p0.p0.i64(ptr @b, ptr getelementptr inbounds ([2 x ptr], ptr @c, i64 0, i64 1), i64 8, i1 false)
19+
ret void
20+
}
21+
;.
22+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { argmemonly nofree nounwind willreturn }
23+
;.

0 commit comments

Comments
 (0)