Skip to content

Commit 639388a

Browse files
committed
[RelLookupTableConverter] Bail on invalid pointer size (x32)
The RelLookupTableConverter pass currently only supports 64-bit pointers. This is currently enforced using an isArch64Bit() check on the target triple. However, we consider x32 to be a 64-bit target, even though the pointers are 32-bit. (And independently of that specific example, there may be address spaces with different pointer sizes.) As such, add an additional guard for the size of the pointers that are actually part of the lookup table. Differential Revision: https://reviews.llvm.org/D131399 (cherry picked from commit 4ac0078)
1 parent 72c97cc commit 639388a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
5757
return false;
5858

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

63+
// If values are not 64-bit pointers, do not generate a relative lookup table.
6464
const DataLayout &DL = M.getDataLayout();
65+
Type *ElemType = Array->getType()->getElementType();
66+
if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)
67+
return false;
68+
6569
for (const Use &Op : Array->operands()) {
6670
Constant *ConstOp = cast<Constant>(&Op);
6771
GlobalValue *GVOp;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=rel-lookup-table-converter -relocation-model=pic -S | FileCheck %s
3+
; REQUIRES: x86-registered-target
4+
5+
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"
6+
target triple = "x86_64-unknown-linux-gnux32"
7+
8+
@a = internal constant i32 0, align 4
9+
@b = internal constant i32 0, align 4
10+
@c = internal constant i32 0, align 4
11+
12+
@table = private unnamed_addr constant [3 x ptr] [ptr @a, ptr @b, ptr @c]
13+
14+
define ptr @test(i32 %cond) {
15+
; CHECK-LABEL: @test(
16+
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [3 x ptr], ptr @table, i32 0, i32 [[COND:%.*]]
17+
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load ptr, ptr [[SWITCH_GEP]], align 8
18+
; CHECK-NEXT: ret ptr [[SWITCH_LOAD]]
19+
;
20+
%switch.gep = getelementptr inbounds [3 x ptr], ptr @table, i32 0, i32 %cond
21+
%switch.load = load ptr, ptr %switch.gep, align 8
22+
ret ptr %switch.load
23+
}

0 commit comments

Comments
 (0)