This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Use canReplacePointersIfEqual to conditionally simplify '(ptr1 == ptr2) ? a : b'
Needs ReviewPublic

Authored by aqjune on Apr 23 2022, 1:24 AM.

Details

Summary

InstSimplify has a folding

(x == y ? a : b) => b

that is valid if the equality can lead to the conclusion.
However, if x and y are pointers, the condition must be more restrictive because x == y being true does not imply that they are truly equivalent values.

This patch fixes InstSimplify to conditionally perform the transformation if they are pointers, using canReplacePointersIfEqual.

Diff Detail

Event Timeline

aqjune created this revision.Apr 23 2022, 1:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2022, 1:24 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
aqjune requested review of this revision.Apr 23 2022, 1:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2022, 1:24 AM
aqjune added inline comments.Apr 23 2022, 1:25 AM
llvm/lib/Analysis/InstructionSimplify.cpp
4348

Refinement must not be used in this case; AreEqual is used instead therefore.

llvm/test/Transforms/InstSimplify/select-ptr-eq.ll
44

case3 and case4 are kept optimized.

I began to think that we might not need to make replacement of pointers 'directional' because it is too complex.
What about renaming canReplacePointersIfEqual in Loads.h to haveSameProvenance and only checking the equivalence of two pointers, which will make things simpler (including this patch)?

In theory, if inttoptr is involved, pointer replacement might be directional (it depends on the semantics of inttoptr); but we don't need to get that deep ATM.

haveSameProvenanceIfEqual seems like a better name, certainly.