Skip to content

Commit 4027e2f

Browse files
resistorarichardson
andauthoredDec 10, 2024··
CallPromotionUtils: Correctly use IndexSize when determining the bit width of pointer offsets. (#119138)
Co-authored-by: Alexander Richardson <[email protected]>
1 parent c487381 commit 4027e2f

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed
 

‎llvm/lib/Transforms/Utils/CallPromotionUtils.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -692,14 +692,14 @@ bool llvm::tryPromoteCall(CallBase &CB) {
692692
if (!VTableEntryLoad)
693693
return false; // Not a vtable entry load.
694694
Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand();
695-
APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0);
695+
APInt VTableOffset(DL.getIndexTypeSizeInBits(VTableEntryPtr->getType()), 0);
696696
Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets(
697697
DL, VTableOffset, /* AllowNonInbounds */ true);
698698
LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
699699
if (!VTablePtrLoad)
700700
return false; // Not a vtable load.
701701
Value *Object = VTablePtrLoad->getPointerOperand();
702-
APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0);
702+
APInt ObjectOffset(DL.getIndexTypeSizeInBits(Object->getType()), 0);
703703
Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
704704
DL, ObjectOffset, /* AllowNonInbounds */ true);
705705
if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
@@ -712,7 +712,7 @@ bool llvm::tryPromoteCall(CallBase &CB) {
712712
VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr);
713713
if (!VTablePtr)
714714
return false; // No vtable found.
715-
APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0);
715+
APInt VTableOffsetGVBase(DL.getIndexTypeSizeInBits(VTablePtr->getType()), 0);
716716
Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets(
717717
DL, VTableOffsetGVBase, /* AllowNonInbounds */ true);
718718
GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes
2+
; RUN: opt -S -passes=inline < %s | FileCheck %s
3+
4+
;; Check that we correctly use the index size when accumulating offsets during CallPromotion
5+
6+
target datalayout = "p200:128:128:128:64-A200-P200-G200"
7+
8+
define void @test(ptr addrspace(200) %arg1, ptr addrspace(200) %arg2) local_unnamed_addr addrspace(200) {
9+
; CHECK-LABEL: define {{[^@]+}}@test
10+
; CHECK-SAME: (ptr addrspace(200) [[ARG1:%.*]], ptr addrspace(200) [[ARG2:%.*]]) local_unnamed_addr addrspace(200) {
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: [[TMP0:%.*]] = load ptr addrspace(200), ptr addrspace(200) [[ARG2]], align 16
13+
; CHECK-NEXT: call addrspace(200) void [[TMP0]](ptr addrspace(200) [[ARG1]])
14+
; CHECK-NEXT: ret void
15+
;
16+
entry:
17+
call void @call_fnptr(ptr addrspace(200) %arg1, ptr addrspace(200) %arg2)
18+
ret void
19+
}
20+
21+
define internal void @call_fnptr(ptr addrspace(200) %this, ptr addrspace(200) %arg) unnamed_addr addrspace(200) align 2 {
22+
entry:
23+
%0 = load ptr addrspace(200), ptr addrspace(200) %arg, align 16
24+
call void %0(ptr addrspace(200) %this)
25+
ret void
26+
}
27+
28+
define void @test2(ptr addrspace(200) %this) local_unnamed_addr addrspace(200) {
29+
; CHECK-LABEL: define {{[^@]+}}@test2
30+
; CHECK-SAME: (ptr addrspace(200) [[THIS:%.*]]) local_unnamed_addr addrspace(200) {
31+
; CHECK-NEXT: entry:
32+
; CHECK-NEXT: [[VTABLE_I:%.*]] = load ptr addrspace(200), ptr addrspace(200) [[THIS]], align 16
33+
; CHECK-NEXT: [[FN_I:%.*]] = load ptr addrspace(200), ptr addrspace(200) [[VTABLE_I]], align 16
34+
; CHECK-NEXT: call addrspace(200) void [[FN_I]](ptr addrspace(200) [[THIS]])
35+
; CHECK-NEXT: ret void
36+
;
37+
entry:
38+
call void @call_via_vtable(ptr addrspace(200) %this)
39+
ret void
40+
}
41+
42+
define internal void @call_via_vtable(ptr addrspace(200) %this) unnamed_addr addrspace(200) {
43+
entry:
44+
%vtable = load ptr addrspace(200), ptr addrspace(200) %this, align 16
45+
%fn = load ptr addrspace(200), ptr addrspace(200) %vtable, align 16
46+
call void %fn(ptr addrspace(200) %this)
47+
ret void
48+
}

0 commit comments

Comments
 (0)
Please sign in to comment.