[SimplifyCFG] Rewrite SinkThenElseCodeToEnd
commite093ba31a4b5bd4a10fa012fe3b82f28efe4fb3d
authorJames Molloy <james.molloy@arm.com>
Mon, 22 Aug 2016 17:40:23 +0000 (22 17:40 +0000)
committerJames Molloy <james.molloy@arm.com>
Mon, 22 Aug 2016 17:40:23 +0000 (22 17:40 +0000)
tree930eb40c600d3bf0cf63220f626a3f9aed9c4e91
parent8d91e9a431e55acea75b3bc19e671a667b4b62b5
[SimplifyCFG] Rewrite SinkThenElseCodeToEnd

The new version has several advantages:
  1) IMSHO it's more readable and neater
  2) It handles loads and stores properly
  3) It can handle any number of incoming blocks rather than just two. I'll be taking advantage of this in a followup patch.

With this change we can now finally sink load-modify-store idioms such as:

    if (a)
      return *b += 3;
    else
      return *b += 4;

    =>

    %z = load i32, i32* %y
    %.sink = select i1 %a, i32 5, i32 7
    %b = add i32 %z, %.sink
    store i32 %b, i32* %y
    ret i32 %b

When this works for switches it'll be even more powerful.

Round 4. This time we should handle all instructions correctly, and not replace any operands that need to be constant with variables.

This was really hard to determine safely, so the helper function should be put into the Instruction API. I'll do that as a followup.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279443 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Utils/SimplifyCFG.cpp
test/CodeGen/ARM/avoid-cpsr-rmw.ll
test/DebugInfo/ARM/single-constant-use-preserves-dbgloc.ll
test/Transforms/SimplifyCFG/AArch64/prefer-fma.ll
test/Transforms/SimplifyCFG/sink-common-code.ll