OpaquePtr: add Type parameter to Loads analysis API.
This makes the functions in Loads.h require a type to be specified
independently of the pointer Value so that when pointers have no structure
other than address-space, it can still do its job.
Most callers had an obvious memory operation handy to provide this type, but a
SROA and ArgumentPromotion were doing more complicated analysis. They get
updated to merge the properties of the various instructions they were
considering.
llvm-svn: 365468
diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
index 8b77adc..95a9f31 100644
--- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
+++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
@@ -479,9 +479,9 @@
return NF;
}
-/// AllCallersPassInValidPointerForArgument - Return true if we can prove that
-/// all callees pass in a valid pointer for the specified function argument.
-static bool allCallersPassInValidPointerForArgument(Argument *Arg) {
+/// Return true if we can prove that all callees pass in a valid pointer for the
+/// specified function argument.
+static bool allCallersPassValidPointerForArgument(Argument *Arg, Type *Ty) {
Function *Callee = Arg->getParent();
const DataLayout &DL = Callee->getParent()->getDataLayout();
@@ -493,7 +493,7 @@
CallSite CS(U);
assert(CS && "Should only have direct calls!");
- if (!isDereferenceablePointer(CS.getArgument(ArgNo), DL))
+ if (!isDereferenceablePointer(CS.getArgument(ArgNo), Ty, DL))
return false;
}
return true;
@@ -566,7 +566,7 @@
/// This method limits promotion of aggregates to only promote up to three
/// elements of the aggregate in order to avoid exploding the number of
/// arguments passed in.
-static bool isSafeToPromoteArgument(Argument *Arg, bool isByVal, AAResults &AAR,
+static bool isSafeToPromoteArgument(Argument *Arg, Type *ByValTy, AAResults &AAR,
unsigned MaxElements) {
using GEPIndicesSet = std::set<IndicesVector>;
@@ -596,9 +596,28 @@
GEPIndicesSet ToPromote;
// If the pointer is always valid, any load with first index 0 is valid.
- if (isByVal || allCallersPassInValidPointerForArgument(Arg))
+
+ if (ByValTy)
SafeToUnconditionallyLoad.insert(IndicesVector(1, 0));
+ // Whenever a new underlying type for the operand is found, make sure it's
+ // consistent with the GEPs and loads we've already seen and, if necessary,
+ // use it to see if all incoming pointers are valid (which implies the 0-index
+ // is safe).
+ Type *BaseTy = ByValTy;
+ auto UpdateBaseTy = [&](Type *NewBaseTy) {
+ if (BaseTy)
+ return BaseTy == NewBaseTy;
+
+ BaseTy = NewBaseTy;
+ if (allCallersPassValidPointerForArgument(Arg, BaseTy)) {
+ assert(SafeToUnconditionallyLoad.empty());
+ SafeToUnconditionallyLoad.insert(IndicesVector(1, 0));
+ }
+
+ return true;
+ };
+
// First, iterate the entry block and mark loads of (geps of) arguments as
// safe.
BasicBlock &EntryBlock = Arg->getParent()->front();
@@ -621,6 +640,9 @@
// right away, can't promote this argument at all.
return false;
+ if (!UpdateBaseTy(GEP->getSourceElementType()))
+ return false;
+
// Indices checked out, mark them as safe
markIndicesSafe(Indices, SafeToUnconditionallyLoad);
Indices.clear();
@@ -628,6 +650,11 @@
} else if (V == Arg) {
// Direct loads are equivalent to a GEP with a single 0 index.
markIndicesSafe(IndicesVector(1, 0), SafeToUnconditionallyLoad);
+
+ if (BaseTy && LI->getType() != BaseTy)
+ return false;
+
+ BaseTy = LI->getType();
}
}
@@ -645,6 +672,9 @@
Loads.push_back(LI);
// Direct loads are equivalent to a GEP with a zero index and then a load.
Operands.push_back(0);
+
+ if (!UpdateBaseTy(LI->getType()))
+ return false;
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UR)) {
if (GEP->use_empty()) {
// Dead GEP's cause trouble later. Just remove them if we run into
@@ -653,9 +683,12 @@
// TODO: This runs the above loop over and over again for dead GEPs
// Couldn't we just do increment the UI iterator earlier and erase the
// use?
- return isSafeToPromoteArgument(Arg, isByVal, AAR, MaxElements);
+ return isSafeToPromoteArgument(Arg, ByValTy, AAR, MaxElements);
}
+ if (!UpdateBaseTy(GEP->getSourceElementType()))
+ return false;
+
// Ensure that all of the indices are constants.
for (User::op_iterator i = GEP->idx_begin(), e = GEP->idx_end(); i != e;
++i)
@@ -966,8 +999,9 @@
}
// Otherwise, see if we can promote the pointer to its value.
- if (isSafeToPromoteArgument(PtrArg, PtrArg->hasByValAttr(), AAR,
- MaxElements))
+ Type *ByValTy =
+ PtrArg->hasByValAttr() ? PtrArg->getParamByValType() : nullptr;
+ if (isSafeToPromoteArgument(PtrArg, ByValTy, AAR, MaxElements))
ArgsToPromote.insert(PtrArg);
}