Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for llvm/llvm-project#57021 #79

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
return false;

ConstantArray *Array = dyn_cast<ConstantArray>(GV.getInitializer());
// If values are not pointers, do not generate a relative lookup table.
if (!Array || !Array->getType()->getElementType()->isPointerTy())
if (!Array)
return false;

// If values are not 64-bit pointers, do not generate a relative lookup table.
const DataLayout &DL = M.getDataLayout();
Type *ElemType = Array->getType()->getElementType();
if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)
return false;

for (const Use &Op : Array->operands()) {
Constant *ConstOp = cast<Constant>(&Op);
GlobalValue *GVOp;
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/Transforms/RelLookupTableConverter/X86/gnux32.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=rel-lookup-table-converter -relocation-model=pic -S | FileCheck %s
; REQUIRES: x86-registered-target

target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnux32"

@a = internal constant i32 0, align 4
@b = internal constant i32 0, align 4
@c = internal constant i32 0, align 4

@table = private unnamed_addr constant [3 x ptr] [ptr @a, ptr @b, ptr @c]

define ptr @test(i32 %cond) {
; CHECK-LABEL: @test(
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [3 x ptr], ptr @table, i32 0, i32 [[COND:%.*]]
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load ptr, ptr [[SWITCH_GEP]], align 8
; CHECK-NEXT: ret ptr [[SWITCH_LOAD]]
;
%switch.gep = getelementptr inbounds [3 x ptr], ptr @table, i32 0, i32 %cond
%switch.load = load ptr, ptr %switch.gep, align 8
ret ptr %switch.load
}