[llvm-dev] [cfe-dev] CFG simplification question, and preservation of branching in the original code
Joan Lluch via llvm-dev
llvm-dev at lists.llvm.org
Sat Oct 5 06:09:46 PDT 2019
> On 4 Oct 2019, at 18:00, Finkel, Hal J. <hfinkel at anl.gov> wrote:
>
> Why not? It seems like we could pattern match the shifts back into selects/branches?
I don’t think that reverse pattern matching is the optimal approach in all cases. I have tried and certainly for some simple cases the undesired shifts can be reverted into selects producing better code. However, this does not work well for complex patterns or cases where second transforms have been applied on top of it.
One typical case is a sequence of ‘selects’ that depend on a single common condition. If one of these selects is combined into non-branching code, then the backend has a very hard work to track back the origin of that common condition. Therefore, for targets that convert all selects into branches, these two selects get almost inevitably converted into either two branching sequences, or one branch sequence followed or preceded by the non-branching code of the other select.
Scenarios like this are not a problem for pipelined processors with expensive branches and ’select’ like instructions because in this case the effort is put in removing branches by the insertion of speculative code which is usually cheaper. However, for simple processors where branches must be used and are cheap, we often want to do the opposite because the execution of speculative code or particular transforms is comparatively most costly than just branching.
Going back to the case, if these two selects arrived together to the backend, then they could be identified and folded into a block preceded by a single conditional branch, resulting in faster and shorter code. The latter is the purpose of some function in the CodeGen prepare pass, but this doesn’t fire because the two selects are not there to work with them to begin with.
Finally, generally speaking, I don’t regard complex reverse pattern matching as an elegant solution, compared with providing the right LLVM hooks for the affected targets, because this is like acknowledging that these targets are inferior, or not worth the effort to improve LLVM code. Also, whatever the way I look at it, I always find that it’s better to solve problems by working on the root causes rather than trying to apply late patches or corrections, which generally make things convoluted and difficult to maintain, not to mention suboptimal.
John
>
> On 10/4/19 5:46 AM, Joan Lluch via llvm-dev wrote:
>> Hi all,
>>
>> As a continuation of this thread, I was about to fill a bug report requesting the modification of DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND, in order to avoid the creation
>> of Shifts for targets with no native support. For example by generating a ‘select' equivalent to a<0 ? -1 : 0
>> instead of an arithmetic shift right. For targets with no multiple shifts or word extension instructions, the select is much cheaper.
>>
>> However, I found that the early InstCombine pass spoils such optimisation by creating shifts on their own as a transform of (supposedly) equivalent code.
>>
>> ...
>>
>> I strongly suggest the above gets fixed. HOWEVER, even after the DAG combiner is fixed, the issues will remain due to InstCombine doing essentially the same thing much earlier. Consider code like this:
>>
>> int test0( int a )
>> {
>> return a<0 ? -1 : 0;
>> }
>>
>> int test1( int a )
>> {
>> return a<0 ? 1 : 0;
>> }
>>
>> int test2( int a )
>> {
>> return a<0 ? 2 : 0;
>> }
>>
>> int test3( int a )
>> {
>> return a<0 ? 3 : 0;
>> }
>>
>> In all cases, InstCombine converts the above into Shifts, right into the IR, which the backend can’t do anything about.
>
> Why not? It seems like we could pattern match the shifts back into selects/branches?
>
> -Hal
>
>> ...
>>
>>>
>>
>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev <https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev>
> --
> Hal Finkel
> Lead, Compiler Technology and Programming Languages
> Leadership Computing Facility
> Argonne National Laboratory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191005/d25a3135/attachment.html>
More information about the llvm-dev
mailing list