Fix replacing all the users of objc weak runtime routines
authorDan Gohman <gohman@apple.com>
Fri, 18 May 2012 22:17:29 +0000 (22:17 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 18 May 2012 22:17:29 +0000 (22:17 +0000)
when deleting them. rdar://11434915.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157080 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/ObjCARC.cpp
test/Transforms/ObjCARC/weak-dce.ll [new file with mode: 0644]

index ce4a195d95b4f50a059d0a1fca9394f25b5f144b..c4ab42d263f28ed3bedef85a278381c16a0e2257 100644 (file)
@@ -3486,8 +3486,18 @@ void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
       for (Value::use_iterator UI = Alloca->use_begin(),
            UE = Alloca->use_end(); UI != UE; ) {
         CallInst *UserInst = cast<CallInst>(*UI++);
-        if (!UserInst->use_empty())
-          UserInst->replaceAllUsesWith(UserInst->getArgOperand(0));
+        switch (GetBasicInstructionClass(UserInst)) {
+        case IC_InitWeak:
+        case IC_StoreWeak:
+          // These functions return their second argument.
+          UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
+          break;
+        case IC_DestroyWeak:
+          // No return value.
+          break;
+        default:
+          break;
+        }
         UserInst->eraseFromParent();
       }
       Alloca->eraseFromParent();
diff --git a/test/Transforms/ObjCARC/weak-dce.ll b/test/Transforms/ObjCARC/weak-dce.ll
new file mode 100644 (file)
index 0000000..f094671
--- /dev/null
@@ -0,0 +1,46 @@
+; RUN: opt -S -basicaa -objc-arc < %s | FileCheck %s
+; rdar://11434915
+
+; Delete the weak calls and replace them with just the net retain.
+
+;      CHECK: define void @test0(i8* %p) {
+; CHECK-NEXT: call i8* @objc_retain(i8* %p)
+; CHECK-NEXT: ret void
+
+define void @test0(i8* %p) {
+  %weakBlock = alloca i8*, align 8
+  %tmp7 = call i8* @objc_initWeak(i8** %weakBlock, i8* %p) nounwind
+  %tmp26 = call i8* @objc_loadWeakRetained(i8** %weakBlock) nounwind
+  call void @objc_destroyWeak(i8** %weakBlock) nounwind
+  ret void
+}
+
+;      CHECK: define i8* @test1(i8* %p) {
+; CHECK-NEXT: call i8* @objc_retain(i8* %p)
+; CHECK-NEXT: ret i8* %p
+
+define i8* @test1(i8* %p) {
+  %weakBlock = alloca i8*, align 8
+  %tmp7 = call i8* @objc_initWeak(i8** %weakBlock, i8* %p) nounwind
+  %tmp26 = call i8* @objc_loadWeakRetained(i8** %weakBlock) nounwind
+  call void @objc_destroyWeak(i8** %weakBlock) nounwind
+  ret i8* %tmp26
+}
+
+;      CHECK: define i8* @test2(i8* %p, i8* %q) {
+; CHECK-NEXT: call i8* @objc_retain(i8* %q)
+; CHECK-NEXT: ret i8* %q
+
+define i8* @test2(i8* %p, i8* %q) {
+  %weakBlock = alloca i8*, align 8
+  %tmp7 = call i8* @objc_initWeak(i8** %weakBlock, i8* %p) nounwind
+  %tmp19 = call i8* @objc_storeWeak(i8** %weakBlock, i8* %q) nounwind
+  %tmp26 = call i8* @objc_loadWeakRetained(i8** %weakBlock) nounwind
+  call void @objc_destroyWeak(i8** %weakBlock) nounwind
+  ret i8* %tmp26
+}
+
+declare i8* @objc_initWeak(i8**, i8*)
+declare void @objc_destroyWeak(i8**)
+declare i8* @objc_loadWeakRetained(i8**)
+declare i8* @objc_storeWeak(i8** %weakBlock, i8* %q)