<div dir="ltr">I'm already rewriting the whole thing, just wanted to keep my patches against mainline small :)<div><br></div><div>It's actually badly N^3 in a few places, more than just the one you point out.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 26, 2016 at 4:25 PM, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Thu, May 26, 2016 at 4:17 PM, Daniel Berlin <span dir="ltr"><<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>This seems ... problematic, in a simple sense.</div><div><br></div><div>You've added a check that literally every instruction in between does not throw :)<br></div><div><br></div>It happens that the alias check is O(N) for memdep so it was all N^2 anyway, but for memoryssa it's O(1), and you've now made such a walk N^2 in those case.<div>This whole check should be in the outer loop of mergeLoads and mergeStores.<br></div><div><br></div><div>Instead of just a check of each inst that is a load, check if they are mayThrow, and if so, stop, because you can't merge any loads (or sink any stores) from that block.</div></div></blockquote><div><br></div></span><div>I was aware of this before I wrote any of the code :)  You'd need the check in both mergeStores and canSinkFromBlock.</div><div><br></div><div>I'd rather just find time to rewrite the whole thing. I think, it's more like (N^2)*M because mergeStores calls canSinkFromBlock which then does the canInstructionRangeModRef.</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><br></div><div><br><div><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">David Majnemer via llvm-commits</b> <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span><br>Date: Thu, May 26, 2016 at 12:11 AM<br>Subject: [llvm] r270828 - [MergedLoadStoreMotion] Don't transform across may-throw calls<br>To: <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br><br><br>Author: majnemer<br>
Date: Thu May 26 02:11:09 2016<br>
New Revision: 270828<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=270828&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=270828&view=rev</a><br>
Log:<br>
[MergedLoadStoreMotion] Don't transform across may-throw calls<br>
<br>
It is unsafe to hoist a load before a function call which may throw, the<br>
throw might prevent a pointer dereference.<br>
<br>
Likewise, it is unsafe to sink a store after a call which may throw.<br>
The caller might be able to observe the difference.<br>
<br>
This fixes PR27858.<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/InstMerge/exceptions.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp<br>
    llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp?rev=270828&r1=270827&r2=270828&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp?rev=270828&r1=270827&r2=270828&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp Thu May 26 02:11:09 2016<br>
@@ -79,7 +79,6 @@<br>
 #include "llvm/Analysis/Loads.h"<br>
 #include "llvm/Analysis/MemoryBuiltins.h"<br>
 #include "llvm/Analysis/MemoryDependenceAnalysis.h"<br>
-#include "llvm/Analysis/TargetLibraryInfo.h"<br>
 #include "llvm/IR/Metadata.h"<br>
 #include "llvm/IR/PatternMatch.h"<br>
 #include "llvm/Support/Debug.h"<br>
@@ -114,7 +113,6 @@ private:<br>
   // This transformation requires dominator postdominator info<br>
   void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
     AU.setPreservesCFG();<br>
-    AU.addRequired<TargetLibraryInfoWrapperPass>();<br>
     AU.addRequired<AAResultsWrapperPass>();<br>
     AU.addPreserved<GlobalsAAWrapperPass>();<br>
     AU.addPreserved<MemoryDependenceWrapperPass>();<br>
@@ -130,9 +128,9 @@ private:<br>
   BasicBlock *getDiamondTail(BasicBlock *BB);<br>
   bool isDiamondHead(BasicBlock *BB);<br>
   // Routines for hoisting loads<br>
-  bool isLoadHoistBarrierInRange(const Instruction& Start,<br>
-                                 const Instruction& End,<br>
-                                 LoadInst* LI);<br>
+  bool isLoadHoistBarrierInRange(const Instruction &Start,<br>
+                                 const Instruction &End, LoadInst *LI,<br>
+                                 bool SafeToLoadUnconditionally);<br>
   LoadInst *canHoistFromBlock(BasicBlock *BB, LoadInst *LI);<br>
   void hoistInstruction(BasicBlock *BB, Instruction *HoistCand,<br>
                         Instruction *ElseInst);<br>
@@ -166,7 +164,6 @@ FunctionPass *llvm::createMergedLoadStor<br>
 INITIALIZE_PASS_BEGIN(MergedLoadStoreMotion, "mldst-motion",<br>
                       "MergedLoadStoreMotion", false, false)<br>
 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)<br>
-INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)<br>
 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)<br>
 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)<br>
 INITIALIZE_PASS_END(MergedLoadStoreMotion, "mldst-motion",<br>
@@ -229,9 +226,14 @@ bool MergedLoadStoreMotion::isDiamondHea<br>
 /// being loaded or protect against the load from happening<br>
 /// it is considered a hoist barrier.<br>
 ///<br>
-bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(const Instruction& Start,<br>
-                                                      const Instruction& End,<br>
-                                                      LoadInst* LI) {<br>
+bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(<br>
+    const Instruction &Start, const Instruction &End, LoadInst *LI,<br>
+    bool SafeToLoadUnconditionally) {<br>
+  if (!SafeToLoadUnconditionally)<br>
+    for (const Instruction &Inst :<br>
+         make_range(Start.getIterator(), End.getIterator()))<br>
+      if (Inst.mayThrow())<br>
+        return true;<br>
   MemoryLocation Loc = MemoryLocation::get(LI);<br>
   return AA->canInstructionRangeModRef(Start, End, Loc, MRI_Mod);<br>
 }<br>
@@ -245,23 +247,28 @@ bool MergedLoadStoreMotion::isLoadHoistB<br>
 ///<br>
 LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,<br>
                                                    LoadInst *Load0) {<br>
-<br>
+  BasicBlock *BB0 = Load0->getParent();<br>
+  BasicBlock *Head = BB0->getSinglePredecessor();<br>
+  bool SafeToLoadUnconditionally = isSafeToLoadUnconditionally(<br>
+      Load0->getPointerOperand(), Load0->getAlignment(),<br>
+      Load0->getModule()->getDataLayout(),<br>
+      /*ScanFrom=*/Head->getTerminator());<br>
   for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end(); BBI != BBE;<br>
        ++BBI) {<br>
     Instruction *Inst = &*BBI;<br>
<br>
     // Only merge and hoist loads when their result in used only in BB<br>
-    if (!isa<LoadInst>(Inst) || Inst->isUsedOutsideOfBlock(BB1))<br>
+    auto *Load1 = dyn_cast<LoadInst>(Inst);<br>
+    if (!Load1 || Inst->isUsedOutsideOfBlock(BB1))<br>
       continue;<br>
<br>
-    auto *Load1 = cast<LoadInst>(Inst);<br>
-    BasicBlock *BB0 = Load0->getParent();<br>
-<br>
     MemoryLocation Loc0 = MemoryLocation::get(Load0);<br>
     MemoryLocation Loc1 = MemoryLocation::get(Load1);<br>
     if (AA->isMustAlias(Loc0, Loc1) && Load0->isSameOperationAs(Load1) &&<br>
-        !isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1) &&<br>
-        !isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0)) {<br>
+        !isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1,<br>
+                                   SafeToLoadUnconditionally) &&<br>
+        !isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0,<br>
+                                   SafeToLoadUnconditionally)) {<br>
       return Load1;<br>
     }<br>
   }<br>
@@ -387,6 +394,10 @@ bool MergedLoadStoreMotion::mergeLoads(B<br>
 bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,<br>
                                                       const Instruction &End,<br>
                                                       MemoryLocation Loc) {<br>
+  for (const Instruction &Inst :<br>
+       make_range(Start.getIterator(), End.getIterator()))<br>
+    if (Inst.mayThrow())<br>
+      return true;<br>
   return AA->canInstructionRangeModRef(Start, End, Loc, MRI_ModRef);<br>
 }<br>
<br>
@@ -403,18 +414,15 @@ StoreInst *MergedLoadStoreMotion::canSin<br>
        RBI != RBE; ++RBI) {<br>
     Instruction *Inst = &*RBI;<br>
<br>
-    if (!isa<StoreInst>(Inst))<br>
-       continue;<br>
-<br>
-    StoreInst *Store1 = cast<StoreInst>(Inst);<br>
+    auto *Store1 = dyn_cast<StoreInst>(Inst);<br>
+    if (!Store1)<br>
+      continue;<br>
<br>
     MemoryLocation Loc0 = MemoryLocation::get(Store0);<br>
     MemoryLocation Loc1 = MemoryLocation::get(Store1);<br>
     if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&<br>
-        !isStoreSinkBarrierInRange(*(std::next(BasicBlock::iterator(Store1))),<br>
-                                   BB1->back(), Loc1) &&<br>
-        !isStoreSinkBarrierInRange(*(std::next(BasicBlock::iterator(Store0))),<br>
-                                   BB0->back(), Loc0)) {<br>
+        !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&<br>
+        !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {<br>
       return Store1;<br>
     }<br>
   }<br>
<br>
Added: llvm/trunk/test/Transforms/InstMerge/exceptions.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstMerge/exceptions.ll?rev=270828&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstMerge/exceptions.ll?rev=270828&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstMerge/exceptions.ll (added)<br>
+++ llvm/trunk/test/Transforms/InstMerge/exceptions.ll Thu May 26 02:11:09 2016<br>
@@ -0,0 +1,57 @@<br>
+; RUN: opt -basicaa -memdep -mldst-motion -S < %s | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@r = common global i32 0, align 4<br>
+@s = common global i32 0, align 4<br>
+<br>
+; CHECK-LABEL: define void @test1(<br>
+define void @test1(i1 %cmp, i32* noalias %p) {<br>
+entry:<br>
+  br i1 %cmp, label %if.then, label %if.else<br>
+<br>
+if.then:                                          ; preds = %entry<br>
+  call void @may_throw()<br>
+  %arrayidx = getelementptr inbounds i32, i32* %p, i64 1<br>
+  %0 = load i32, i32* %arrayidx, align 4<br>
+  store i32 %0, i32* @r, align 4<br>
+  br label %if.end<br>
+; CHECK:       call void @may_throw()<br>
+; CHECK-NEXT:  %[[gep:.*]] = getelementptr inbounds i32, i32* %p, i64 1<br>
+; CHECK-NEXT:  %[[load:.*]] = load i32, i32* %[[gep]], align 4<br>
+; CHECK-NEXT:  store i32 %[[load]], i32* @r, align 4<br>
+<br>
+if.else:                                          ; preds = %entry<br>
+  %arrayidx1 = getelementptr inbounds i32, i32* %p, i64 1<br>
+  %1 = load i32, i32* %arrayidx1, align 4<br>
+  store i32 %1, i32* @s, align 4<br>
+  br label %if.end<br>
+<br>
+if.end:                                           ; preds = %if.else, %if.then<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-LABEL: define void @test2(<br>
+define void @test2(i1 %cmp, i32* noalias %p) {<br>
+entry:<br>
+  br i1 %cmp, label %if.then, label %if.else<br>
+<br>
+if.then:                                          ; preds = %entry<br>
+  %arrayidx = getelementptr inbounds i32, i32* %p, i64 1<br>
+  store i32 1, i32* %arrayidx, align 4<br>
+  call void @may_throw()<br>
+; CHECK:       %[[gep:.*]] = getelementptr inbounds i32, i32* %p, i64 1<br>
+; CHECK-NEXT:  store i32 1, i32* %[[gep]], align 4<br>
+; CHECK-NEXT:  call void @may_throw()<br>
+  br label %if.end<br>
+<br>
+if.else:                                          ; preds = %entry<br>
+  %arrayidx1 = getelementptr inbounds i32, i32* %p, i64 1<br>
+  store i32 2, i32* %arrayidx1, align 4<br>
+  br label %if.end<br>
+<br>
+if.end:                                           ; preds = %if.else, %if.then<br>
+  ret void<br>
+}<br>
+<br>
+declare void @may_throw()<br>
<br>
Modified: llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll?rev=270828&r1=270827&r2=270828&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll?rev=270828&r1=270827&r2=270828&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll (original)<br>
+++ llvm/trunk/test/Transforms/InstMerge/st_sink_no_barrier_call.ll Thu May 26 02:11:09 2016<br>
@@ -33,7 +33,7 @@ if.else:<br>
   %p3 = getelementptr inbounds %struct.node, %struct.node* %node.017, i32 0, i32 6<br>
   ; CHECK-NOT: store i32<br>
   store i32 %add, i32* %p3, align 4<br>
-  call i32 @foo(i32 5)                           ;not a barrier<br>
+  call i32 @foo(i32 5) nounwind                                  ;not a barrier<br>
   br label %if.end<br>
<br>
 ; CHECK: if.end<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</div><br></div></div></div></div></div></div>
</blockquote></div></div></div><br></div></div>
</blockquote></div><br></div>