[DSE,MemorySSA] Check for throwing instrs between killing/killed def.
authorFlorian Hahn <flo@fhahn.com>
Fri, 4 Sep 2020 16:19:56 +0000 (17:19 +0100)
committerFlorian Hahn <flo@fhahn.com>
Fri, 4 Sep 2020 17:54:59 +0000 (18:54 +0100)
We also have to check all uses between the killing & killed def and
check if any of them is throwing.

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-malloc-free.ll
llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-throwing.ll

index 7e4ef1e..0296d20 100644 (file)
@@ -2023,6 +2023,14 @@ struct DSEState {
       if (isMemTerminator(DefLoc, UseInst))
         continue;
 
+      if (UseInst->mayThrow() && !isInvisibleToCallerBeforeRet(DefUO)) {
+        LLVM_DEBUG(dbgs() << "  ... found throwing instruction\n");
+        Cache.KnownReads.insert(UseAccess);
+        Cache.KnownReads.insert(StartAccess);
+        Cache.KnownReads.insert(EarlierAccess);
+        return None;
+      }
+
       // Uses which may read the original MemoryDef mean we cannot eliminate the
       // original MD. Stop walk.
       if (isReadClobber(DefLoc, UseInst)) {
index 46712cc..763362d 100644 (file)
@@ -37,10 +37,12 @@ bb3:
   ret void
 }
 
-
+; We cannot remove the store in the entry block, because @unknown_func could
+; unwind and the stored value could be read by the caller.
 define void @test17(i32* noalias %P) {
 ; CHECK-LABEL: @test17(
 ; CHECK-NEXT:    [[P2:%.*]] = bitcast i32* [[P:%.*]] to i8*
+; CHECK-NEXT:    store i32 1, i32* [[P]], align 4
 ; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[BB3:%.*]]
 ; CHECK:       bb1:
 ; CHECK-NEXT:    call void @unknown_func()
index d297ac3..f6031e8 100644 (file)
@@ -1,5 +1,4 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; XFAIL: *
 ; RUN: opt < %s -basic-aa -dse -enable-dse-memoryssa -S | FileCheck %s
 
 target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"