[llvm-dev] Questions about jump threading optimization and what we can do

Michael Kruse via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 4 08:46:57 PST 2020


Ideally, with only 2 loop iteration with different code being executed
per execution, it is fully unrolled (-loop-unroll). But for this to
happen, LoopUnroll needs to know the iteration count from
ScalarEvolution. Your code is structured a bit differently than the
typical for-loop, making it a bit more difficult. Canonicalization
(-simplifycfg -loop-rotate -loop-simplify -indvarsimplify) should make
it recognizable.

Michael

Am Mo., 3. Feb. 2020 um 16:01 Uhr schrieb Karl Rehm <klrehm123 at gmail.com>:
>
> Ok, thanks for the clarification. So looking into ScalarEvolution is what I'd look into if I wanted to optimize this a bit more? I'm mainly interested in how I'd go about "de-looping" this.
>
> On Mon, Feb 3, 2020 at 4:55 PM Michael Kruse <llvmdev at meinersbur.de> wrote:
>>
>> Your code and the one you have seen in
>> https://bugs.llvm.org/show_bug.cgi?id=44679 are structurally
>> different. In that latter, setting n to a constant value causes a
>> branch taken in the next iteration. Your's is a more classic for-loop
>> from 0 to 2. It is ScalarEvolution's job to find this out. SimplyCFG
>> job is just to normalize the IR into a form understood by
>> ScalarEvolution. That is, ScalarEvolution is the primary engine
>> enabling this.
>>
>> Michael
>>
>> Am Mo., 3. Feb. 2020 um 14:51 Uhr schrieb Karl Rehm <klrehm123 at gmail.com>:
>> >
>> > Hm. I assumed that JumpThreading would be the primary factor in optimizing code like this. Guess not. I'll need to look into SimplifyCFG to see what prevents it from doing the same thing to the other loop: https://godbolt.org/z/F6NjdG
>> >
>> > On Mon, Feb 3, 2020 at 3:09 PM Michael Kruse <llvmdev at meinersbur.de> wrote:
>> >>
>> >> I assumed the LLVM-IR behind the godbolt link represented the C code
>> >> you used before.
>> >>
>> >> SimplifyCFG actually helps the loop being removed:
>> >> https://godbolt.org/z/5v7SXh
>> >>
>> >> But this doesn't even involve JumpThreading.
>> >>
>> >> Michael


More information about the llvm-dev mailing list