diff --git a/llvm/lib/Transforms/Scalar/GVNHoist.cpp b/llvm/lib/Transforms/Scalar/GVNHoist.cpp index bec0a56512428..6d2b25cf60134 100644 --- a/llvm/lib/Transforms/Scalar/GVNHoist.cpp +++ b/llvm/lib/Transforms/Scalar/GVNHoist.cpp @@ -534,7 +534,7 @@ class GVNHoist { if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D)) if (auto *UD = dyn_cast(D)) - if (firstInBB(NewPt, UD->getMemoryInst())) + if (!firstInBB(UD->getMemoryInst(), NewPt)) // Cannot move the load or store to NewPt above its definition in D. return false; diff --git a/llvm/test/Transforms/GVNHoist/pr36787.ll b/llvm/test/Transforms/GVNHoist/pr36787.ll new file mode 100644 index 0000000000000..487e2bd335cee --- /dev/null +++ b/llvm/test/Transforms/GVNHoist/pr36787.ll @@ -0,0 +1,76 @@ +; RUN: opt < %s -gvn-hoist -S | FileCheck %s + +@g = external constant i8* + +declare i32 @gxx_personality(...) +declare void @f0() +declare void @f1() +declare void @f2() + +; Make sure opt won't crash and that the load +; is not hoisted from label6 to label4 + +;CHECK-LABEL: @func + +define void @func() personality i8* bitcast (i32 (...)* @gxx_personality to i8*) { + invoke void @f0() + to label %3 unwind label %1 + +;