Skip to content

Commit 8758e5f

Browse files
authored
[ConstantFolding] Fix handling of index width != pointer width (#130608)
Per LangRef: > The offsets are then added to the low bits of the base address up to the index type width, with silently-wrapping two’s complement arithmetic. If the pointer size is larger than the index size, this means that the bits outside the index type width will not be affected. The transform as implemented was doubly wrong, because it just truncated the original base pointer to the index width, losing the top bits entirely. Make sure we preserve the bits and use wrapping arithmetic within the low bits.
1 parent 4a4444c commit 8758e5f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,18 +943,21 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
943943

944944
// If the base value for this address is a literal integer value, fold the
945945
// getelementptr to the resulting integer value casted to the pointer type.
946-
APInt BasePtr(BitWidth, 0);
946+
APInt BasePtr(DL.getPointerTypeSizeInBits(Ptr->getType()), 0);
947947
if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
948948
if (CE->getOpcode() == Instruction::IntToPtr) {
949949
if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
950-
BasePtr = Base->getValue().zextOrTrunc(BitWidth);
950+
BasePtr = Base->getValue().zextOrTrunc(BasePtr.getBitWidth());
951951
}
952952
}
953953

954954
auto *PTy = cast<PointerType>(Ptr->getType());
955955
if ((Ptr->isNullValue() || BasePtr != 0) &&
956956
!DL.isNonIntegralPointerType(PTy)) {
957-
Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
957+
// If the index size is smaller than the pointer size, add to the low
958+
// bits only.
959+
BasePtr.insertBits(BasePtr.trunc(BitWidth) + Offset, 0);
960+
Constant *C = ConstantInt::get(Ptr->getContext(), BasePtr);
958961
return ConstantExpr::getIntToPtr(C, ResTy);
959962
}
960963

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
3+
4+
target datalayout = "p:16:16:16:8"
5+
6+
; The GEP should only modify the low 8 bits of the pointer.
7+
define ptr @test() {
8+
; CHECK-LABEL: define ptr @test() {
9+
; CHECK-NEXT: ret ptr inttoptr (i16 -256 to ptr)
10+
;
11+
%base = inttoptr i16 -1 to ptr
12+
%gep = getelementptr i8, ptr %base, i8 1
13+
ret ptr %gep
14+
}

0 commit comments

Comments
 (0)