[llvm] 4e3a074 - [SimplifyCFG] Reuse function comdat for switch lookup table (#190995)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 08:53:02 PDT 2026


Author: 8051Enthusiast
Date: 2026-04-15T15:52:57Z
New Revision: 4e3a074501cae5f132293d56f9987ded94ee72be

URL: https://github.com/llvm/llvm-project/commit/4e3a074501cae5f132293d56f9987ded94ee72be
DIFF: https://github.com/llvm/llvm-project/commit/4e3a074501cae5f132293d56f9987ded94ee72be.diff

LOG: [SimplifyCFG] Reuse function comdat for switch lookup table (#190995)

Fixes #190994.

As the switch table is extracted from the function, the table should be
removed when the function is removed, and therefore inherits the comdat
of the function.

Added: 
    llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-comdat.ll

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index e7a6c9354953a..57e404a145f77 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6962,6 +6962,7 @@ Value *SwitchReplacement::replaceSwitch(Value *Index, IRBuilder<> &Builder,
     // Set the alignment to that of an array items. We will be only loading one
     // value out of it.
     Table->setAlignment(DL.getPrefTypeAlign(ValueType));
+    Table->setComdat(Func->getComdat());
     Type *IndexTy = DL.getIndexType(Table->getType());
     auto *ArrayTy = cast<ArrayType>(Table->getValueType());
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-comdat.ll b/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-comdat.ll
new file mode 100644
index 0000000000000..51e91cf0de8bc
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-comdat.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: -p --check-globals all --version 6
+; RUN: opt -S -passes="simplifycfg<switch-to-lookup>" < %s | FileCheck %s
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+$foostring = comdat any
+
+ at .str = private unnamed_addr constant [4 x i8] c"foo\00", comdat($foostring), align 1
+ at .str1 = private unnamed_addr constant [4 x i8] c"bar\00", comdat($foostring), align 1
+ at .str2 = private unnamed_addr constant [4 x i8] c"baz\00", comdat($foostring), align 1
+ at .str3 = private unnamed_addr constant [4 x i8] c"qux\00", comdat($foostring), align 1
+ at .str4 = private unnamed_addr constant [6 x i8] c"error\00", comdat($foostring), align 1
+
+;.
+; CHECK: @.str = private unnamed_addr constant [4 x i8] c"foo\00", comdat($foostring), align 1
+; CHECK: @.str1 = private unnamed_addr constant [4 x i8] c"bar\00", comdat($foostring), align 1
+; CHECK: @.str2 = private unnamed_addr constant [4 x i8] c"baz\00", comdat($foostring), align 1
+; CHECK: @.str3 = private unnamed_addr constant [4 x i8] c"qux\00", comdat($foostring), align 1
+; CHECK: @.str4 = private unnamed_addr constant [6 x i8] c"error\00", comdat($foostring), align 1
+; CHECK: @switch.table.foostring = private unnamed_addr constant [4 x ptr] [ptr @.str, ptr @.str1, ptr @.str2, ptr @.str3], comdat($foostring), align 8
+;.
+define ptr @foostring(i32 %x) comdat {
+; CHECK-LABEL: define ptr @foostring(i32 %x) comdat {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    %0 = icmp ult i32 %x, 4
+; CHECK-NEXT:    br i1 %0, label %switch.lookup, label %return
+; CHECK:       switch.lookup:
+; CHECK-NEXT:    %1 = zext nneg i32 %x to i64
+; CHECK-NEXT:    %switch.gep = getelementptr inbounds [4 x ptr], ptr @switch.table.foostring, i64 0, i64 %1
+; CHECK-NEXT:    %switch.load = load ptr, ptr %switch.gep, align 8
+; CHECK-NEXT:    br label %return
+; CHECK:       return:
+; CHECK-NEXT:    %retval.0 = phi ptr [ %switch.load, %switch.lookup ], [ @.str4, %entry ]
+; CHECK-NEXT:    ret ptr %retval.0
+;
+entry:
+  switch i32 %x, label %sw.default [
+  i32 0, label %return
+  i32 1, label %sw.bb1
+  i32 2, label %sw.bb2
+  i32 3, label %sw.bb3
+  ]
+
+sw.bb1: br label %return
+sw.bb2: br label %return
+sw.bb3: br label %return
+sw.default: br label %return
+
+return:
+  %retval.0 = phi ptr [ @.str4, %sw.default ],
+  [ @.str3, %sw.bb3 ],
+  [ @.str2, %sw.bb2 ],
+  [ @.str1, %sw.bb1 ],
+  [ @.str, %entry ]
+  ret ptr %retval.0
+
+}
+


        


More information about the llvm-commits mailing list