[llvm] [SimplifyCFG] Add optimization for switches of powers of two (PR #70977)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 18:06:10 PST 2023


================
@@ -0,0 +1,90 @@
+; RUN: opt %s -passes='simplifycfg<switch-to-lookup>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+; Check that the range of switch of powers of two is reduced 
+define i32 @switch_of_powers(i32 %x) {
+; CHECK-LABEL: switch_of_powers
+; CHECK-LABEL: entry
+; CHECK: %{{.*}} = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+; CHECK-NOT: switch i32 %x
+
+entry:
+  switch i32 %x, label %def [
+    i32 1,  label %bb1
+    i32 8,  label %bb2
+    i32 16, label %bb3
+    i32 32, label %bb4
+    i32 64, label %bb5
+  ]
+
+
+def: unreachable
----------------
DianQK wrote:

Please do not use the `def` abbreviation. This will introduce an ambiguity with `define`.

https://github.com/llvm/llvm-project/pull/70977


More information about the llvm-commits mailing list