DataLayout missing in isDereferenceablePointer()

Hi,

I've been debugging this issue for many hours, and would like a sanity
check. The issue is that DL (DataLayout) is nullptr inside
isDereferenceablePointer() no matter what I try. Why is DataLayout
ever nullptr (shouldn't it get defaults?), and what should I do to
make it non-nullptr?

Thanks.

Ram

Have you tried passing a non-null DataLayout into isDereferenceablePointer?

How are you trying to call it? Do you have a DataLayout?

-eric

Eric Christopher wrote:

How are you trying to call it? Do you have a DataLayout?

In test/Analysis/ValueTracking/memory-dereferenceable.ll, just change
byval to dereferenceable(8), and %dparam won't match (see
lib/IR/Value.cpp:521 for the logic that is supposed to fire). How do I
get it to pass? I tried introducing a target-triple and
target-datalayout, but it didn't help.

Ram,

You need to change MemDerefPrinter to pass in the DataLayout to
isDereferenceablePointer. The test passes with this change:

(NB: this may not be the right way to solve the problem)

diff --git a/lib/Analysis/MemDerefPrinter.cpp b/lib/Analysis/MemDerefPrinter.cpp
index 64cec18..d239cdf 100644
--- a/lib/Analysis/MemDerefPrinter.cpp
+++ b/lib/Analysis/MemDerefPrinter.cpp
@@ -36,22 +36,23 @@ namespace {
char MemDerefPrinter::ID = 0;
INITIALIZE_PASS(MemDerefPrinter, "print-memderefs",
                 "Memory Dereferenciblity of pointers in function", false, true)

FunctionPass *llvm::createMemDerefPrinter() {
   return new MemDerefPrinter();
}

bool MemDerefPrinter::runOnFunction(Function &F) {
+ const DataLayout *DL = F.getDataLayout();
   for (auto &I: inst_range(F)) {
     if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
       Value *PO = LI->getPointerOperand();
- if (PO->isDereferenceablePointer(nullptr))
+ if (PO->isDereferenceablePointer(DL))
         Vec.push_back(PO);
     }
   }
   return false;
}

void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
   OS << "The following are dereferenceable:\n";
   for (auto &V: Vec) {
diff --git a/test/Analysis/ValueTracking/memory-dereferenceable.ll
b/test/Analysis/ValueTracking/memory-dereferenceable.ll
index 1ec3fef..f4ca532 100644
--- a/test/Analysis/ValueTracking/memory-dereferenceable.ll
+++ b/test/Analysis/ValueTracking/memory-dereferenceable.ll
@@ -1,19 +1,21 @@
; RUN: opt -print-memderefs -analyze -S <%s | FileCheck %s

; Uses the print-deref (+ analyze to print) pass to run
; isDereferenceablePointer() on many load instruction operands

+target datalayout = "e"

Sanjoy,

Thanks; this particular case was my stupidity (I wrote
-print-memderefs), but I was originally having issues with deref not
propagating in InstCombine -- I suppose I have to follow the same
approach.

Sanjoy Das wrote:

(NB: this may not be the right way to solve the problem)

It would be nice if someone can tell me if this is the way it's meant
to be done.