Skip to content
Closed
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
2 changes: 2 additions & 0 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,8 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
// Figure out what objects these things are pointing to if we can.
const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);
removeRoundTripCasts(O1);
removeRoundTripCasts(O2);
Comment on lines +1605 to +1606
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic I'm wondering if a less general change could be considered correct. It sounds like the change in getUnderlyingObject would not be correct for every user, but for this specific user of getUnderlyingObject, could it be correct to strip away this pattern as this pseudocode would do?

The reason this would be useful would be because it could open up an easy NoAlias result.

Lets say we have the following instruction A, that we are comparing to instruction B in BasicAliasAnalysis. Each have a different underlying global value, but A's pointer is wrapped in the round trip cast.

%A= getelementptr i8, ptr inttoptr (i32 ptrtoint (ptr @globalmem to i32) to ptr), i64 0
%B = getelementptr i8, ptr @globalmem_2, i64 0

On line 1619 of this file, isIdentifiedObject is called on each underlying object. If we do not traverse through the inttoptr/ptrtoint round trip we do not identify the underlying object as a global value, and thus are not able to prove NoAlias with that check.

Is it possible this traversal is correct in this specific case, or is it the same problem as the more general optimizations?

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same problem as in the general case. You can't look through inttoptr/ptrtoint cast in any code that does provenance-based reasoning, which BasicAA does (e.g. via isIdentifiedObject).

It is possible to look through these casts, but you can only reason about the address of the pointer after you do. This means that in your example, we would not be able to prove these are NoAlias because the underlying object is different -- but we could prove that the accessed address ranges must be disjoint.

It's possible, but this would need substantial changes to BasicAA and the AA API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, that makes sense to me. Closing this PR. Appreciate the time!


// Null values in the default address space don't point to any object, so they
// don't alias any other pointer.
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6661,6 +6661,10 @@ const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
if (!NewV->getType()->isPointerTy())
return V;
V = NewV;
} else if (Operator::getOpcode(V) == Instruction::IntToPtr &&
Operator::getOpcode(cast<Operator>(V)->getOperand(0)) ==
Instruction::PtrToInt) {
V = cast<Operator>(cast<Operator>(V)->getOperand(0))->getOperand(0);
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
if (GA->isInterposable())
return V;
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/Analysis/ValueTrackingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,18 @@ TEST_F(ValueTrackingTest, ComputeConstantRange) {
}
}

TEST_F(ValueTrackingTest, GetUnderlyingObject) {
parseAssembly(R"(
@globalmem = external global i8
define void @test() {
%A = getelementptr i8, ptr @globalmem, i64 0
%A2 = getelementptr i8, ptr inttoptr (i32 ptrtoint (ptr @globalmem to i32) to ptr), i64 0
ret void
}
)");
EXPECT_EQ(getUnderlyingObject(A), getUnderlyingObject(A2));
}

struct FindAllocaForValueTestParams {
const char *IR;
bool AnyOffsetResult;
Expand Down
Loading