[llvm] r236905 - Switch lowering: cluster adjacent fall-through cases even at -O0

Hans Wennborg hans at hanshq.net
Fri May 8 14:23:40 PDT 2015


Author: hans
Date: Fri May  8 16:23:39 2015
New Revision: 236905

URL: http://llvm.org/viewvc/llvm-project?rev=236905&view=rev
Log:
Switch lowering: cluster adjacent fall-through cases even at -O0

It's cheap to do, and codegen is much faster if cases can be merged
into clusters.

Modified:
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
    llvm/trunk/test/CodeGen/X86/switch.ll

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=236905&r1=236904&r2=236905&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp Fri May  8 16:23:39 2015
@@ -8012,10 +8012,12 @@ void SelectionDAGBuilder::visitSwitch(co
 
   MachineBasicBlock *DefaultMBB = FuncInfo.MBBMap[SI.getDefaultDest()];
 
-  if (TM.getOptLevel() != CodeGenOpt::None) {
-    // Cluster adjacent cases with the same destination.
-    sortAndRangeify(Clusters);
+  // Cluster adjacent cases with the same destination. We do this at all
+  // optimization levels because it's cheap to do and will make codegen faster
+  // if there are many clusters.
+  sortAndRangeify(Clusters);
 
+  if (TM.getOptLevel() != CodeGenOpt::None) {
     // Replace an unreachable default with the most popular destination.
     // FIXME: Exploit unreachable default more aggressively.
     bool UnreachableDefault =

Modified: llvm/trunk/test/CodeGen/X86/switch.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/switch.ll?rev=236905&r1=236904&r2=236905&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/switch.ll (original)
+++ llvm/trunk/test/CodeGen/X86/switch.ll Fri May  8 16:23:39 2015
@@ -9,18 +9,19 @@ entry:
     i32 3, label %bb0
     i32 1, label %bb1
     i32 4, label %bb1
-    i32 5, label %bb0
+    i32 5, label %bb2
   ]
 bb0: tail call void @g(i32 0) br label %return
 bb1: tail call void @g(i32 1) br label %return
+bb2: tail call void @g(i32 1) br label %return
 return: ret void
 
 ; Should be lowered as straight compares in -O0 mode.
 ; NOOPT-LABEL: basic
-; NOOPT: subl $3, %eax
-; NOOPT: je
 ; NOOPT: subl $1, %eax
 ; NOOPT: je
+; NOOPT: subl $3, %eax
+; NOOPT: je
 ; NOOPT: subl $4, %eax
 ; NOOPT: je
 ; NOOPT: subl $5, %eax
@@ -58,6 +59,14 @@ return: ret void
 ; CHECK: jae
 ; CHECK: cmpl $3
 ; CHECK: ja
+
+; We do this even at -O0, because it's cheap and makes codegen faster.
+; NOOPT-LABEL: simple_ranges
+; NOOPT: subl $4
+; NOOPT: jb
+; NOOPT: addl $-100
+; NOOPT: subl $4
+; NOOPT: jb
 }
 
 





More information about the llvm-commits mailing list