On Tue, Oct 2, 2012 at 4:26 PM, Sean Silva <span dir="ltr"><<a href="mailto:silvas@purdue.edu" target="_blank" class="cremed">silvas@purdue.edu</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> +  template <typename UnaryPredicate><br>
> +  bool remove_if(UnaryPredicate P) {<br>
> +    typename vector_type::iterator B = std::remove_if(vector_.begin(),<br>
> +                                                      vector_.end(), P),<br>
> +                                   E = vector_.end();<br>
> +    if (B == E)<br>
> +      return false;<br>
> +    for (typename vector_type::iterator I = B; I != E; ++I)<br>
> +      set_.erase(*I);<br>
> +    vector_.erase(B, E);<br>
> +    return true;<br>
> +  }<br>
> +<br>
<br>
</div>I don't think that remove_if is allowed to be used like this.<br>
<br>
>From 25.3.8 [alg.remove]:<br>
Note: each element in the range [ret,last), where ret is the returned<br>
value, has a valid but unspecified state, because the algorithms can<br>
eliminate elements by swapping with or moving from elements that were<br>
originally in that range.<br>
<br>
<br>
I think you can use stable_partition() to do what you want here, but<br>
that's pretty expensive (comparatively; it works by recursively<br>
rotating in specific ways; n*log n but also the constant factor<br>
compared with just a linear scan of an array). The simplest<br>
alternative is probably to just open code remove_if(), and do the<br>
deletion inside the open-coded remove_if() loop as you "delete"<br>
elements (i.e. overwrite them).<br></blockquote><div><br></div><div>Thanks, this is a great point.</div><div><br></div><div>No need to write out remove_if -- std::partition() should be just as efficient and provides the guarantee we need.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888"><br>
--Sean Silva<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Tue, Oct 2, 2012 at 6:46 PM, Chandler Carruth <<a href="mailto:chandlerc@gmail.com" class="cremed">chandlerc@gmail.com</a>> wrote:<br>
> Author: chandlerc<br>
> Date: Tue Oct  2 17:46:45 2012<br>
> New Revision: 165065<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=165065&view=rev" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=165065&view=rev</a><br>
> Log:<br>
> Teach the new SROA to handle cases where an alloca that has already been<br>
> scheduled for processing on the worklist eventually gets deleted while<br>
> we are processing another alloca, fixing the original test case in<br>
> PR13990.<br>
><br>
> To facilitate this, add a remove_if helper to the SetVector abstraction.<br>
> It's not easy to use the standard abstractions for this because of the<br>
> specifics of SetVectors types and implementation.<br>
><br>
> Finally, a nice small test case is included. Thanks to Benjamin for the<br>
> fantastic reduced test case here! All I had to do was delete some empty<br>
> basic blocks!<br>
><br>
> Modified:<br>
>     llvm/trunk/include/llvm/ADT/SetVector.h<br>
>     llvm/trunk/lib/Transforms/Scalar/SROA.cpp<br>
>     llvm/trunk/test/Transforms/SROA/basictest.ll<br>
><br>
> Modified: llvm/trunk/include/llvm/ADT/SetVector.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SetVector.h?rev=165065&r1=165064&r2=165065&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SetVector.h?rev=165065&r1=165064&r2=165065&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/include/llvm/ADT/SetVector.h (original)<br>
> +++ llvm/trunk/include/llvm/ADT/SetVector.h Tue Oct  2 17:46:45 2012<br>
> @@ -126,6 +126,32 @@<br>
>      return false;<br>
>    }<br>
><br>
> +  /// \brief Remove items from the set vector based on a predicate function.<br>
> +  ///<br>
> +  /// This is intended to be equivalent to the following code, if we could<br>
> +  /// write it:<br>
> +  ///<br>
> +  /// \code<br>
> +  ///   V.erase(std::remove_if(V.begin(), V.end(), P), V.end());<br>
> +  /// \endcode<br>
> +  ///<br>
> +  /// However, SetVector doesn't expose non-const iterators, making any<br>
> +  /// algorithm like remove_if impossible to use.<br>
> +  ///<br>
> +  /// \returns true if any element is removed.<br>
> +  template <typename UnaryPredicate><br>
> +  bool remove_if(UnaryPredicate P) {<br>
> +    typename vector_type::iterator B = std::remove_if(vector_.begin(),<br>
> +                                                      vector_.end(), P),<br>
> +                                   E = vector_.end();<br>
> +    if (B == E)<br>
> +      return false;<br>
> +    for (typename vector_type::iterator I = B; I != E; ++I)<br>
> +      set_.erase(*I);<br>
> +    vector_.erase(B, E);<br>
> +    return true;<br>
> +  }<br>
> +<br>
><br>
>    /// \brief Count the number of elements of a given key in the SetVector.<br>
>    /// \returns 0 if the element is not in the SetVector, 1 if it is.<br>
><br>
> Modified: llvm/trunk/lib/Transforms/Scalar/SROA.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SROA.cpp?rev=165065&r1=165064&r2=165065&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SROA.cpp?rev=165065&r1=165064&r2=165065&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/lib/Transforms/Scalar/SROA.cpp (original)<br>
> +++ llvm/trunk/lib/Transforms/Scalar/SROA.cpp Tue Oct  2 17:46:45 2012<br>
> @@ -3302,7 +3302,11 @@<br>
>    while (!Worklist.empty()) {<br>
>      Changed |= runOnAlloca(*Worklist.pop_back_val());<br>
>      deleteDeadInstructions(DeletedAllocas);<br>
> +<br>
> +    // Remove the deleted allocas from various lists so that we don't try to<br>
> +    // continue processing them.<br>
>      if (!DeletedAllocas.empty()) {<br>
> +      Worklist.remove_if(IsAllocaInSet(DeletedAllocas));<br>
>        PromotableAllocas.erase(std::remove_if(PromotableAllocas.begin(),<br>
>                                               PromotableAllocas.end(),<br>
>                                               IsAllocaInSet(DeletedAllocas)),<br>
><br>
> Modified: llvm/trunk/test/Transforms/SROA/basictest.ll<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SROA/basictest.ll?rev=165065&r1=165064&r2=165065&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SROA/basictest.ll?rev=165065&r1=165064&r2=165065&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/test/Transforms/SROA/basictest.ll (original)<br>
> +++ llvm/trunk/test/Transforms/SROA/basictest.ll Tue Oct  2 17:46:45 2012<br>
> @@ -897,3 +897,32 @@<br>
>    %tmp2 = load i8* %gep<br>
>    ret void<br>
>  }<br>
> +<br>
> +define void @PR13990() {<br>
> +; Ensure we can handle cases where processing one alloca causes the other<br>
> +; alloca to become dead and get deleted. This might crash or fail under<br>
> +; Valgrind if we regress.<br>
> +; CHECK: @PR13990<br>
> +; CHECK-NOT: alloca<br>
> +; CHECK: unreachable<br>
> +; CHECK: unreachable<br>
> +<br>
> +entry:<br>
> +  %tmp1 = alloca i8*<br>
> +  %tmp2 = alloca i8*<br>
> +  br i1 undef, label %bb1, label %bb2<br>
> +<br>
> +bb1:<br>
> +  store i8* undef, i8** %tmp2<br>
> +  br i1 undef, label %bb2, label %bb3<br>
> +<br>
> +bb2:<br>
> +  %tmp50 = select i1 undef, i8** %tmp2, i8** %tmp1<br>
> +  br i1 undef, label %bb3, label %bb4<br>
> +<br>
> +bb3:<br>
> +  unreachable<br>
> +<br>
> +bb4:<br>
> +  unreachable<br>
> +}<br>
><br>
><br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@cs.uiuc.edu" class="cremed">llvm-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" class="cremed">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br></div>