Factor out some duplicated code, implement the rest of the cases in

BasicAA/2003-12-11-ConstExprGEP.ll


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10412 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2003-12-11 23:20:16 +00:00
parent de454ffb5b
commit 4a83088b30

View File

@@ -100,6 +100,26 @@ static const User *isGEP(const Value *V) {
return 0; return 0;
} }
static const Value *GetGEPOperands(const Value *V, std::vector<Value*> &GEPOps){
assert(GEPOps.empty() && "Expect empty list to populate!");
GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
cast<User>(V)->op_end());
// Accumulate all of the chained indexes into the operand array
V = cast<User>(V)->getOperand(0);
while (const User *G = isGEP(V)) {
if (!isa<Constant>(GEPOps[0]) ||
!cast<Constant>(GEPOps[0])->isNullValue())
break; // Don't handle folding arbitrary pointer offsets yet...
GEPOps.erase(GEPOps.begin()); // Drop the zero index
GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
V = G->getOperand(0);
}
return V;
}
// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such // alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
// as array references. Note that this function is heavily tail recursive. // as array references. Note that this function is heavily tail recursive.
// Hopefully we have a smart C++ compiler. :) // Hopefully we have a smart C++ compiler. :)
@@ -192,30 +212,9 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
// non-aliasing. // non-aliasing.
// Collect all of the chained GEP operands together into one simple place // Collect all of the chained GEP operands together into one simple place
std::vector<Value*> GEP1Ops(cast<User>(V1)->op_begin()+1, std::vector<Value*> GEP1Ops, GEP2Ops;
cast<User>(V1)->op_end()); BasePtr1 = GetGEPOperands(V1, GEP1Ops);
std::vector<Value*> GEP2Ops(cast<User>(V2)->op_begin()+1, BasePtr2 = GetGEPOperands(V2, GEP2Ops);
cast<User>(V2)->op_end());
// Accumulate all of the chained indexes into the operand arrays
BasePtr1 = cast<User>(V1)->getOperand(0);
BasePtr2 = cast<User>(V2)->getOperand(0);
while (const User *G = isGEP(BasePtr1)) {
if (!isa<Constant>(GEP1Ops[0]) ||
!cast<Constant>(GEP1Ops[0])->isNullValue())
break; // Don't handle folding arbitrary pointer offsets yet...
GEP1Ops.erase(GEP1Ops.begin());
GEP1Ops.insert(GEP1Ops.begin(), G->op_begin()+1, G->op_end());
BasePtr1 = G->getOperand(0);
}
while (const User *G = isGEP(BasePtr2)) {
if (!isa<Constant>(GEP2Ops[0]) ||
!cast<Constant>(GEP2Ops[0])->isNullValue())
break; // Don't handle folding arbitrary pointer offsets yet...
GEP2Ops.erase(GEP2Ops.begin());
GEP2Ops.insert(GEP2Ops.begin(), G->op_begin()+1, G->op_end());
BasePtr2 = G->getOperand(0);
}
AliasResult GAlias = AliasResult GAlias =
CheckGEPInstructions(BasePtr1->getType(), GEP1Ops, V1Size, CheckGEPInstructions(BasePtr1->getType(), GEP1Ops, V1Size,
@@ -229,21 +228,24 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
// instruction. If one pointer is a GEP with a non-zero index of the other // instruction. If one pointer is a GEP with a non-zero index of the other
// pointer, we know they cannot alias. // pointer, we know they cannot alias.
// //
if (isa<GetElementPtrInst>(V2)) { if (isGEP(V2)) {
std::swap(V1, V2); std::swap(V1, V2);
std::swap(V1Size, V2Size); std::swap(V1Size, V2Size);
} }
if (V1Size != ~0U && V2Size != ~0U) if (V1Size != ~0U && V2Size != ~0U)
if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V1)) { if (const User *GEP = isGEP(V1)) {
AliasResult R = alias(GEP->getOperand(0), V1Size, V2, V2Size); std::vector<Value*> GEPOperands;
const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
if (R == MustAlias) { if (R == MustAlias) {
// If there is at least one non-zero constant index, we know they cannot // If there is at least one non-zero constant index, we know they cannot
// alias. // alias.
bool ConstantFound = false; bool ConstantFound = false;
bool AllZerosFound = true; bool AllZerosFound = true;
for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
if (const Constant *C = dyn_cast<Constant>(GEP->getOperand(i))) { if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
if (!C->isNullValue()) { if (!C->isNullValue()) {
ConstantFound = true; ConstantFound = true;
AllZerosFound = false; AllZerosFound = false;
@@ -266,17 +268,13 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
// the size of the argument... build an index vector that is equal to // the size of the argument... build an index vector that is equal to
// the arguments provided, except substitute 0's for any variable // the arguments provided, except substitute 0's for any variable
// indexes we find... // indexes we find...
for (unsigned i = 0; i != GEPOperands.size(); ++i)
std::vector<Value*> Indices; if (!isa<Constant>(GEPOperands[i]) ||
Indices.reserve(GEP->getNumOperands()-1); isa<ConstantExpr>(GEPOperands[i]))
for (unsigned i = 1; i != GEP->getNumOperands(); ++i) GEPOperands[i] =Constant::getNullValue(GEPOperands[i]->getType());
if (const Constant *C = dyn_cast<Constant>(GEP->getOperand(i))) int64_t Offset = getTargetData().getIndexedOffset(BasePtr->getType(),
Indices.push_back((Value*)C); GEPOperands);
else if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
Indices.push_back(Constant::getNullValue(Type::LongTy));
const Type *Ty = GEP->getOperand(0)->getType();
int Offset = getTargetData().getIndexedOffset(Ty, Indices);
if (Offset >= (int)V2Size || Offset <= -(int)V1Size)
return NoAlias; return NoAlias;
} }
} }
@@ -326,7 +324,7 @@ CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
// If we have seen all constant operands, and run out of indexes on one of the // If we have seen all constant operands, and run out of indexes on one of the
// getelementptrs, check to see if the tail of the leftover one is all zeros. // getelementptrs, check to see if the tail of the leftover one is all zeros.
// If so, return mustalias. // If so, return mustalias.
if (UnequalOper == MinOperands && MinOperands != MaxOperands) { if (UnequalOper == MinOperands) {
if (GEP1Ops.size() < GEP2Ops.size()) std::swap(GEP1Ops, GEP2Ops); if (GEP1Ops.size() < GEP2Ops.size()) std::swap(GEP1Ops, GEP2Ops);
bool AllAreZeros = true; bool AllAreZeros = true;
@@ -433,7 +431,6 @@ CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
// We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
// Loop over the rest of the operands... // Loop over the rest of the operands...
for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) { for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
const Value *Op1 = i < GEP1Ops.size() ? GEP1Ops[i] : 0; const Value *Op1 = i < GEP1Ops.size() ? GEP1Ops[i] : 0;