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

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


fhahn updated this revision to Diff 283903.
fhahn added a comment.

Directly take DataLayout as argument, as CtxI can be null.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85524/new/

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, const DataLayout &DL,
+                                     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(), DL, 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,14 @@
                                  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, const DataLayout &DL,
+                               Instruction *CtxI);
 }
 
 #endif


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


More information about the llvm-commits mailing list