Skip to content

Commit ebab105

Browse files
committed
[IR] Don't strip through pointer to vector of pointer bitcasts
When using stripPointerCasts() and getUnderlyingObject(), don't strip through a bitcast from ptr to <1 x ptr>, which is not a no-op pointer cast. Calling code is generally not prepared to handle that situation, resulting in incorrect alias analysis results for example. Fixes #97600.
1 parent 97a2bd8 commit ebab105

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6403,9 +6403,10 @@ const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
64036403
V = GEP->getPointerOperand();
64046404
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
64056405
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6406-
V = cast<Operator>(V)->getOperand(0);
6407-
if (!V->getType()->isPointerTy())
6406+
Value *NewV = cast<Operator>(V)->getOperand(0);
6407+
if (!NewV->getType()->isPointerTy())
64086408
return V;
6409+
V = NewV;
64096410
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
64106411
if (GA->isInterposable())
64116412
return V;

llvm/lib/IR/Value.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,10 @@ static const Value *stripPointerCastsAndOffsets(
652652
}
653653
V = GEP->getPointerOperand();
654654
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
655-
V = cast<Operator>(V)->getOperand(0);
656-
if (!V->getType()->isPointerTy())
655+
Value *NewV = cast<Operator>(V)->getOperand(0);
656+
if (!NewV->getType()->isPointerTy())
657657
return V;
658+
V = NewV;
658659
} else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
659660
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
660661
// TODO: If we know an address space cast will not change the
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: opt -print-all-alias-modref-info -passes=aa-eval -disable-output < %s 2>&1 | FileCheck %s
2+
3+
; CHECK: MayAlias: i8* %b, i8* %p
4+
; CHECK: Just Ref: Ptr: i8* %p <-> %v1p = call <1 x ptr> @llvm.masked.load.v1p0.p0(ptr %a, i32 8, <1 x i1> %c, <1 x ptr> poison)
5+
; CHECK: Just Ref: Ptr: i8* %b <-> %v1p = call <1 x ptr> @llvm.masked.load.v1p0.p0(ptr %a, i32 8, <1 x i1> %c, <1 x ptr> poison)
6+
define void @test(ptr %a, ptr %b, <1 x i1> %c) {
7+
%v1p = call <1 x ptr> @llvm.masked.load.v1p0.p0(ptr %a, i32 8, <1 x i1> %c, <1 x ptr> poison)
8+
%p = bitcast <1 x ptr> %v1p to ptr
9+
load i8, ptr %p
10+
store i8 0, ptr %b
11+
ret void
12+
}

0 commit comments

Comments
 (0)