[llvm] r332430 - [ObjCARC] Prevent code motion into a catchswitch
Shoaib Meenai via llvm-commits
llvm-commits at lists.llvm.org
Tue May 15 21:52:19 PDT 2018
Author: smeenai
Date: Tue May 15 21:52:18 2018
New Revision: 332430
URL: http://llvm.org/viewvc/llvm-project?rev=332430&view=rev
Log:
[ObjCARC] Prevent code motion into a catchswitch
A catchswitch must be the only non-phi instruction in its basic block;
attempting to move a retain or release into a catchswitch basic block
will result in invalid IR. Explicitly mark a CFG hazard in this case to
prevent the code motion.
Differential Revision: https://reviews.llvm.org/D46482
Added:
llvm/trunk/test/Transforms/ObjCARC/opt-catchswitch.ll
Modified:
llvm/trunk/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp
Modified: llvm/trunk/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/ObjCARC/ObjCARCOpts.cpp?rev=332430&r1=332429&r2=332430&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/ObjCARC/ObjCARCOpts.cpp (original)
+++ llvm/trunk/lib/Transforms/ObjCARC/ObjCARCOpts.cpp Tue May 15 21:52:18 2018
@@ -1592,6 +1592,7 @@ bool ObjCARCOpt::PairUpRetainsAndRelease
assert(It != Retains.end());
const RRInfo &NewRetainRRI = It->second;
KnownSafeTD &= NewRetainRRI.KnownSafe;
+ CFGHazardAfflicted |= NewRetainRRI.CFGHazardAfflicted;
for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
auto Jt = Releases.find(NewRetainRelease);
if (Jt == Releases.end())
Modified: llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp?rev=332430&r1=332429&r2=332430&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp (original)
+++ llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp Tue May 15 21:52:18 2018
@@ -268,6 +268,11 @@ void BottomUpPtrState::HandlePotentialUs
if (isa<InvokeInst>(Inst)) {
const auto IP = BB->getFirstInsertionPt();
InsertAfter = IP == BB->end() ? std::prev(BB->end()) : IP;
+ if (isa<CatchSwitchInst>(InsertAfter))
+ // A catchswitch must be the only non-phi instruction in its basic
+ // block, so attempting to insert an instruction into such a block would
+ // produce invalid IR.
+ SetCFGHazardAfflicted(true);
} else {
InsertAfter = std::next(Inst->getIterator());
}
Added: llvm/trunk/test/Transforms/ObjCARC/opt-catchswitch.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ObjCARC/opt-catchswitch.ll?rev=332430&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/ObjCARC/opt-catchswitch.ll (added)
+++ llvm/trunk/test/Transforms/ObjCARC/opt-catchswitch.ll Tue May 15 21:52:18 2018
@@ -0,0 +1,54 @@
+; RUN: opt -S -objc-arc < %s | FileCheck %s
+
+target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
+target triple = "i686--windows-msvc"
+
+declare i8* @f(i8*, i8*)
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare dllimport i8* @objc_autoreleaseReturnValue(i8* returned)
+declare dllimport i8* @objc_retain(i8* returned)
+declare dllimport i8* @objc_retainAutoreleasedReturnValue(i8* returned)
+declare dllimport void @objc_release(i8*)
+
+define i8* @g(i8* %p, i8* %q) local_unnamed_addr personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
+entry:
+ %0 = tail call i8* @objc_retain(i8* %p) #0
+ %1 = tail call i8* @objc_retain(i8* %q) #0
+ %call = invoke i8* @f(i8* %p, i8* %q)
+ to label %invoke.cont unwind label %catch.dispatch, !clang.arc.no_objc_arc_exceptions !0
+
+catch.dispatch:
+ %2 = catchswitch within none [label %catch] unwind to caller
+
+catch:
+ %3 = catchpad within %2 [i8* null, i32 64, i8* null]
+ catchret from %3 to label %cleanup
+
+invoke.cont:
+ %4 = tail call i8* @objc_retainAutoreleasedReturnValue(i8* %call) #0
+ br label %cleanup
+
+cleanup:
+ %retval.0 = phi i8* [ %call, %invoke.cont ], [ null, %catch ]
+ tail call void @objc_release(i8* %q) #0, !clang.imprecise_release !0
+ tail call void @objc_release(i8* %p) #0, !clang.imprecise_release !0
+ %5 = tail call i8* @objc_autoreleaseReturnValue(i8* %retval.0) #0
+ ret i8* %retval.0
+}
+
+; CHECK-LABEL: entry:
+; CHECK-NEXT: %0 = tail call i8* @objc_retain(i8* %p) #0
+; CHECK-NEXT: %call = invoke i8* @f(i8* %p, i8* %q)
+; CHECK-NEXT: to label %invoke.cont unwind label %catch.dispatch
+
+; CHECK-LABEL: catch.dispatch:
+; CHECK-NEXT: %1 = catchswitch within none [label %catch] unwind to caller
+
+; CHECK-LABEL: cleanup:
+; CHECK: tail call void @objc_release(i8* %p) #0
+
+attributes #0 = { nounwind }
+
+!0 = !{}
More information about the llvm-commits
mailing list