[llvm] r276240 - [GVNHoist] Don't wrongly preserve TBAA

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 20 22:59:54 PDT 2016


Author: majnemer
Date: Thu Jul 21 00:59:53 2016
New Revision: 276240

URL: http://llvm.org/viewvc/llvm-project?rev=276240&view=rev
Log:
[GVNHoist] Don't wrongly preserve TBAA

We hoisted loads/stores without taking into account which can cause
miscompiles.

Added:
    llvm/trunk/test/Transforms/GVN/hoist-md.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp?rev=276240&r1=276239&r2=276240&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp Thu Jul 21 00:59:53 2016
@@ -603,12 +603,20 @@ public:
     // Copy the gep before moving the ld/st.
     Instruction *ClonedGep = Gep->clone();
     ClonedGep->insertBefore(HoistPt->getTerminator());
+    // Conservatively discard any optimization hints, they may differ on the
+    // other paths.
+    ClonedGep->dropUnknownNonDebugMetadata();
+    ClonedGep->clearSubclassOptionalData();
     Repl->replaceUsesOfWith(Gep, ClonedGep);
 
     // Also copy Val.
     if (Val) {
       Instruction *ClonedVal = Val->clone();
       ClonedVal->insertBefore(HoistPt->getTerminator());
+      // Conservatively discard any optimization hints, they may differ on the
+      // other paths.
+      ClonedVal->dropUnknownNonDebugMetadata();
+      ClonedVal->clearSubclassOptionalData();
       Repl->replaceUsesOfWith(Val, ClonedVal);
     }
 
@@ -647,6 +655,9 @@ public:
             !makeOperandsAvailable(Repl, HoistPt))
           continue;
         Repl->moveBefore(HoistPt->getTerminator());
+        // TBAA may differ on one of the other paths, we need to get rid of
+        // anything which might conflict.
+        Repl->dropUnknownNonDebugMetadata();
       }
 
       if (isa<LoadInst>(Repl))
@@ -668,6 +679,7 @@ public:
             ++NumStoresRemoved;
           else if (isa<CallInst>(Repl))
             ++NumCallsRemoved;
+          Repl->intersectOptionalDataWith(I);
           I->replaceAllUsesWith(Repl);
           I->eraseFromParent();
         }

Added: llvm/trunk/test/Transforms/GVN/hoist-md.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/hoist-md.ll?rev=276240&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GVN/hoist-md.ll (added)
+++ llvm/trunk/test/Transforms/GVN/hoist-md.ll Thu Jul 21 00:59:53 2016
@@ -0,0 +1,29 @@
+; RUN: opt -S -gvn-hoist < %s | FileCheck %s
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @test1(i1 %b, i32* %x) {
+entry:
+  br i1 %b, label %if.then, label %if.else
+
+if.then:                                          ; preds = %entry
+  store i32 2, i32* %x, align 4, !tbaa !1
+  br label %if.end
+
+if.else:                                          ; preds = %entry
+  store i32 2, i32* %x, align 4, !tbaa !5
+  br label %if.end
+
+if.end:                                           ; preds = %if.else, %if.then
+  ret void
+}
+; CHECK-LABEL: define void @test1(
+; CHECK: store i32 2, i32* %x, align 4
+; CHECK-NEXT: br i1 %b
+
+!1 = !{!2, !2, i64 0}
+!2 = !{!"int", !3, i64 0}
+!3 = !{!"omnipotent char", !4, i64 0}
+!4 = !{!"Simple C++ TBAA"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"_ZTS1e", !3, i64 0}




More information about the llvm-commits mailing list