Skip to content

Addrspacecast fixes #12

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

Merged
merged 2 commits into from
Sep 23, 2016
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
5 changes: 4 additions & 1 deletion lib/Analysis/Loads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ static bool isDereferenceableAndAlignedPointer(
// then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
// aligned to Align bytes.

return isDereferenceableAndAlignedPointer(Base, Align, Offset + Size, DL,
// Offset and Size may have different bit widths if we have visited an
// addrspacecast, so we can't do arithmetic directly on the APInt values.
return isDereferenceableAndAlignedPointer(Base, Align,
Offset + Size.getSExtValue(), DL,
CtxI, DT, Visited);
}

Expand Down
10 changes: 8 additions & 2 deletions lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,11 +2833,17 @@ Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
break;

if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
APInt GEPOffset(BitWidth, 0);
// If one of the values we have visited is an addrspacecast, then
// the pointer type of this GEP may be different from the type
// of the Ptr parameter which was passed to this function. This
// means when we construct GEPOffset, we need to use the size
// of GEP's pointer type rather than the size of the original
// pointer type.
APInt GEPOffset(DL.getPointerTypeSizeInBits(Ptr->getType()), 0);
if (!GEP->accumulateConstantOffset(DL, GEPOffset))
break;

ByteOffset += GEPOffset;
ByteOffset += GEPOffset.getSExtValue();

Ptr = GEP->getPointerOperand();
} else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
Expand Down
21 changes: 21 additions & 0 deletions test/Analysis/ValueTracking/dereferenceable-and-aligned.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: opt < %s -licm -S | FileCheck %s

target datalayout = "e-p:32:32-p1:64:64-p4:64:64"

; Make sure isDereferenceableAndAlignePointer() doesn't crash when looking
; walking pointer defs with an addrspacecast that changes pointer size.
; CHECK-LABEL: @addrspacecast_crash
define void @addrspacecast_crash() {
bb:
%tmp = alloca [256 x i32]
br label %bb1

bb1:
%tmp2 = getelementptr inbounds [256 x i32], [256 x i32]* %tmp, i32 0, i32 36
%tmp3 = bitcast i32* %tmp2 to <4 x i32>*
%tmp4 = addrspacecast <4 x i32>* %tmp3 to <4 x i32> addrspace(4)*
%tmp5 = load <4 x i32>, <4 x i32> addrspace(4)* %tmp4
%tmp6 = xor <4 x i32> %tmp5, undef
store <4 x i32> %tmp6, <4 x i32> addrspace(1)* undef
br label %bb1
}
26 changes: 26 additions & 0 deletions test/Analysis/ValueTracking/get-pointer-base-with-const-off.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: opt -gvn -S < %s | FileCheck %s

; Make sure we don't crash when analyzing an addrspacecast in
; GetPointerBaseWithConstantOffset()

target datalayout = "e-p:32:32-p4:64:64"

define i32 @addrspacecast-crash() {
; CHECK-LABEL: @addrspacecast-crash
; CHECK: %tmp = alloca [25 x i64]
; CHECK: %tmp1 = getelementptr inbounds [25 x i64], [25 x i64]* %tmp, i32 0, i32 0
; CHECK: %tmp2 = addrspacecast i64* %tmp1 to <8 x i64> addrspace(4)*
; CHECK: store <8 x i64> zeroinitializer, <8 x i64> addrspace(4)* %tmp2
; CHECK-NOT: load
bb:
%tmp = alloca [25 x i64]
%tmp1 = getelementptr inbounds [25 x i64], [25 x i64]* %tmp, i32 0, i32 0
%tmp2 = addrspacecast i64* %tmp1 to <8 x i64> addrspace(4)*
%tmp3 = getelementptr inbounds <8 x i64>, <8 x i64> addrspace(4)* %tmp2, i64 0
store <8 x i64> zeroinitializer, <8 x i64> addrspace(4)* %tmp3
%tmp4 = getelementptr inbounds [25 x i64], [25 x i64]* %tmp, i32 0, i32 0
%tmp5 = addrspacecast i64* %tmp4 to i32 addrspace(4)*
%tmp6 = getelementptr inbounds i32, i32 addrspace(4)* %tmp5, i64 10
%tmp7 = load i32, i32 addrspace(4)* %tmp6, align 4
ret i32 %tmp7
}