[llvm] [GVN] Restrict equality propagation for pointers (PR #82458)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 18 17:57:57 PDT 2024
================
@@ -708,22 +708,66 @@ Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BatchAAResults &AA,
return Available;
}
-bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
- Instruction *CtxI) {
- Type *Ty = A->getType();
- assert(Ty == B->getType() && Ty->isPointerTy() &&
- "values must have matching pointer types");
-
- // NOTE: The checks in the function are incomplete and currently miss illegal
- // cases! The current implementation is a starting point and the
- // implementation should be made stricter over time.
- if (auto *C = dyn_cast<Constant>(B)) {
- // Do not allow replacing a pointer with a constant pointer, unless it is
- // either null or at least one byte is dereferenceable.
- APInt OneByte(DL.getPointerTypeSizeInBits(Ty), 1);
- return C->isNullValue() ||
- isDereferenceableAndAlignedPointer(B, Align(1), OneByte, DL, CtxI);
- }
+// Returns true if a use is either in an ICmp/PtrToInt or a Phi/Select that only
+// feeds into them.
+static bool isPointerUseReplacable(const Use &U, int MaxLookup = 6) {
+ if (MaxLookup == 0)
+ return false;
+
+ const User *User = U.getUser();
+ if (isa<ICmpInst>(User))
+ return true;
+ if (isa<PtrToIntInst>(User))
+ return true;
+ if (isa<PHINode, SelectInst>(User) &&
+ all_of(User->uses(), [&](const Use &Use) {
+ return isPointerUseReplacable(Use, MaxLookup - 1);
+ }))
+ return true;
+
+ return false;
+}
+
+static const DataLayout &getDLFromVal(const Value *V) {
----------------
efriedma-quic wrote:
The check for GlobalValue here looks a bit suspect; can you actually get a GlobalValue here? If you can, I'd be concerned about other constants. If you can't, you can just drop it, I guess?
It's probably a bit easier to understand what's going on if you just explicitly pass in the DataLayout; that way, the functions are more general, and you don't need to document the restrictions.
https://github.com/llvm/llvm-project/pull/82458
More information about the llvm-commits
mailing list