[PATCH] D85524: [Loads] Add canReplacePointersIfEqual helper.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 07:02:03 PDT 2020


fhahn created this revision.
fhahn added reviewers: nlopes, efriedma, hfinkel, aqjune.
Herald added subscribers: dexonsmith, hiraditya.
Herald added a project: LLVM.
fhahn requested review of this revision.

This patch adds an initial, incomeplete and unsound implementation of
canReplacePointersIfEqual to check if a pointer value A can be replaced
by another pointer value B, that are deemed to be equivalent through
some means (e.g. information from conditions).

Note that is in general not sound to blindly replace pointers based on
equality, for example if they are based on different underlying objects.

LLVM's memory model is not completely settled as of now; see
https://bugs.llvm.org/show_bug.cgi?id=34548 for a more detailed
discussion.

The initial version of canReplacePointersIfEqual only rejects a very
specific case: replacing a pointer with a constant expression that is
not dereferenceable. Such a replacement is problematic and can be
restricted relatively easily without impacting most code. Using it to
limit replacements in GVN/SCCP/CVP only results in small differences in
7 programs out of MultiSource/SPEC2000/SPEC2006 on X86 with -O3 -flto.

This patch is supposed to be an initial step to improve the current
situation and the helper should be made stricter in the future. But this
will require careful analysis of the impact on performance.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85524

Files:
  llvm/include/llvm/Analysis/Loads.h
  llvm/lib/Analysis/Loads.cpp


Index: llvm/lib/Analysis/Loads.cpp
===================================================================
--- llvm/lib/Analysis/Loads.cpp
+++ llvm/lib/Analysis/Loads.cpp
@@ -512,3 +512,17 @@
   // block.
   return nullptr;
 }
+
+bool llvm::canReplacePointersIfEqual(Value *A, Value *B, Instruction *CtxI) {
+  Type *Ty = A->getType();
+  assert(Ty == B->getType() && Ty->isPointerTy() &&
+         "values must have matching pointer types");
+
+  if (auto *C = dyn_cast<Constant>(B)) {
+    return C->isNullValue() ||
+           isDereferenceablePointer(B, Ty->getPointerElementType(),
+                                    CtxI->getModule()->getDataLayout(), CtxI);
+  }
+
+  return true;
+}
Index: llvm/include/llvm/Analysis/Loads.h
===================================================================
--- llvm/include/llvm/Analysis/Loads.h
+++ llvm/include/llvm/Analysis/Loads.h
@@ -155,6 +155,13 @@
                                  BasicBlock::iterator &ScanFrom,
                                  unsigned MaxInstsToScan, AAResults *AA,
                                  bool *IsLoadCSE, unsigned *NumScanedInst);
+
+/// Returns true if a pointer value \p A can be replace with another pointer
+/// value \B if they are deemed equal through some means (e.g. information from
+/// conditions). Note that the current implementations is incomplete and
+/// unsound. It does not reject all invalid cases yet and will be made stricter
+/// in the future.
+bool canReplacePointersIfEqual(Value *A, Value *B, Instruction *CtxI);
 }
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85524.283900.patch
Type: text/x-patch
Size: 1537 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200807/3a0677fd/attachment.bin>


More information about the llvm-commits mailing list