[PATCH] D37053: Insert switch tables into the program memory address space (2/4)

Dylan McKay via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 01:54:41 PDT 2017


dylanmckay created this revision.
Herald added a subscriber: wdng.

This means that on Harvard architectures like AVR, we will now place
these tables into program memory, rather than RAM.


https://reviews.llvm.org/D37053

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/CodeGen/AVR/switch-lookup-table-in-progmem-space.ll
  tools/llvm-stress/llvm-stress.cpp


Index: tools/llvm-stress/llvm-stress.cpp
===================================================================
--- tools/llvm-stress/llvm-stress.cpp
+++ tools/llvm-stress/llvm-stress.cpp
@@ -137,7 +137,8 @@
     Type::getInt8Ty(Context)
   };
 
-  auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false);
+  auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false,
+                                   M->getDataLayout().getProgramAddressSpace());
   // Pick a unique name to describe the input parameters
   Twine Name = "autogen_SD" + Twine{SeedCL};
   auto *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, Name, M);
Index: test/CodeGen/AVR/switch-lookup-table-in-progmem-space.ll
===================================================================
--- /dev/null
+++ test/CodeGen/AVR/switch-lookup-table-in-progmem-space.ll
@@ -0,0 +1,62 @@
+; RUN: opt -S -mtriple=avr -latesimplifycfg < %s | FileCheck %s
+
+target datalayout = "e-p:16:16:16-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-n8-P1"
+
+; Switch lookup tables should be placed in program memory (address space 1).
+
+%MyType = type { i8, [0 x i8], [0 x i8] }
+
+define i8 @foo(i8 %arg) {
+start:
+  %_0 = alloca %MyType
+
+; CHECK: @switch.table.foo = {{.*}}addrspace(1) constant {{.*}}
+  switch i8 %arg, label %bb7 [
+    i8 0, label %bb1
+    i8 1, label %bb2
+    i8 2, label %bb3
+    i8 3, label %bb4
+    i8 4, label %bb5
+    i8 14, label %bb6
+  ]
+
+bb1:                                              ; preds = %start
+  %tmp = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp
+  br label %bb8
+
+bb2:                                              ; preds = %start
+  %tmp1 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp1
+  br label %bb8
+
+bb3:                                              ; preds = %start
+  %tmp2 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp2
+  br label %bb8
+
+bb4:                                              ; preds = %start
+  %tmp3 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp3
+  br label %bb8
+
+bb5:                                              ; preds = %start
+  %tmp4 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp4
+  br label %bb8
+
+bb6:                                              ; preds = %start
+  %tmp5 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 1, i8* %tmp5
+  br label %bb8
+
+bb7:                                              ; preds = %start
+  %tmp6 = getelementptr inbounds %MyType, %MyType* %_0, i32 0, i32 0
+  store i8 0, i8* %tmp6
+  br label %bb8
+
+bb8:                                              ; preds = %bb7, %bb6, %bb5, %bb4, %bb3, %bb2, %bb1
+  %tmp7 = bitcast %MyType* %_0 to i8*
+  %tmp8 = load i8, i8* %tmp7, align 1
+  ret i8 %tmp8
+}
Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4945,7 +4945,10 @@
 
   Array = new GlobalVariable(M, ArrayTy, /*constant=*/true,
                              GlobalVariable::PrivateLinkage, Initializer,
-                             "switch.table." + FuncName);
+                             "switch.table." + FuncName,
+                             /*InsertBefore=*/nullptr,
+                             GlobalValue::NotThreadLocal,
+                             DL.getProgramAddressSpace());
   Array->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
   Kind = ArrayKind;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37053.112311.patch
Type: text/x-patch
Size: 3682 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170823/72f17bed/attachment.bin>


More information about the llvm-commits mailing list