[llvm-bugs] [Bug 46039] New: opt -simplifycfg -lowerswitch makes code worse

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 22 07:24:20 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46039

            Bug ID: 46039
           Summary: opt -simplifycfg -lowerswitch makes code worse
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Transformation Utilities
          Assignee: unassignedbugs at nondot.org
          Reporter: jay.foad at gmail.com
                CC: llvm-bugs at lists.llvm.org

This example starts out as "if (x==1) ... else if (x==2) ...". First
-simplifycfg converts the ifs to a switch. Then -lowerswitch converts the
switch back to ifs, but it comes up with worse code than we started with: "if
(x<2) { if (x==1) ... } else { if (x==2) ... }". This seems like a missed
optimization in -lowerswitch.

$ cat a.ll
define void @f(i32 %x, i32* %p) {
bb0:
  %c1 = icmp eq i32 %x, 1
  br i1 %c1, label %bb1, label %bb2
bb1:
  store i32 33, i32* %p
  br label %bb4
bb2:
  %c2 = icmp eq i32 %x, 2
  br i1 %c2, label %bb3, label %bb4
bb3:
  store i32 66, i32* %p
  br label %bb4
bb4:
  ret void
}

$ opt -simplifycfg -lowerswitch a.ll -S -o -
; ModuleID = 'a.ll'
source_filename = "a.ll"

define void @f(i32 %x, i32* %p) {
bb0:
  br label %NodeBlock

NodeBlock:                                        ; preds = %bb0
  %Pivot = icmp slt i32 %x, 2
  br i1 %Pivot, label %LeafBlock, label %LeafBlock1

LeafBlock1:                                       ; preds = %NodeBlock
  %SwitchLeaf2 = icmp eq i32 %x, 2
  br i1 %SwitchLeaf2, label %bb3, label %NewDefault

LeafBlock:                                        ; preds = %NodeBlock
  %SwitchLeaf = icmp eq i32 %x, 1
  br i1 %SwitchLeaf, label %bb1, label %NewDefault

bb1:                                              ; preds = %LeafBlock
  store i32 33, i32* %p, align 4
  br label %bb4

bb3:                                              ; preds = %LeafBlock1
  store i32 66, i32* %p, align 4
  br label %bb4

NewDefault:                                       ; preds = %LeafBlock1,
%LeafBlock
  br label %bb4

bb4:                                              ; preds = %NewDefault, %bb3,
%bb1
  ret void
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200522/b5f9c83c/attachment.html>


More information about the llvm-bugs mailing list