From 539c9b99c27c0fe4f22a0498bc65334e4aa72ef0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 26 Nov 2009 02:11:08 +0000 Subject: [PATCH] Use GEPOperator more pervasively to simplify code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@89914 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/BasicAliasAnalysis.cpp | 56 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp index ff65e33ad50..56200c560d8 100644 --- a/lib/Analysis/BasicAliasAnalysis.cpp +++ b/lib/Analysis/BasicAliasAnalysis.cpp @@ -38,24 +38,24 @@ using namespace llvm; // Useful predicates //===----------------------------------------------------------------------===// -static const Value *GetGEPOperands(const Value *V, +static const Value *GetGEPOperands(const GEPOperator *V, SmallVector &GEPOps) { assert(GEPOps.empty() && "Expect empty list to populate!"); - GEPOps.insert(GEPOps.end(), cast(V)->op_begin()+1, - cast(V)->op_end()); + GEPOps.insert(GEPOps.end(), V->op_begin()+1, V->op_end()); - // Accumulate all of the chained indexes into the operand array - V = cast(V)->getOperand(0); - - while (const GEPOperator *G = dyn_cast(V)) { - if (!isa(GEPOps[0]) || isa(GEPOps[0]) || - !cast(GEPOps[0])->isNullValue()) - break; // Don't handle folding arbitrary pointer offsets yet... + // Accumulate all of the chained indexes into the operand array. + Value *BasePtr = V->getOperand(0); + while (1) { + V = dyn_cast(BasePtr); + if (V == 0) return BasePtr; + + // Don't handle folding arbitrary pointer offsets yet. + if (!isa(GEPOps[0]) || !cast(GEPOps[0])->isNullValue()) + return BasePtr; + GEPOps.erase(GEPOps.begin()); // Drop the zero index - GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end()); - V = G->getOperand(0); + GEPOps.insert(GEPOps.begin(), V->op_begin()+1, V->op_end()); } - return V; } /// isKnownNonNull - Return true if we know that the specified value is never @@ -219,7 +219,7 @@ namespace { // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP // instruction against another. - AliasResult aliasGEP(const Value *V1, unsigned V1Size, + AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size, const Value *V2, unsigned V2Size); // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI @@ -405,21 +405,19 @@ BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { return NoAA::getModRefInfo(CS1, CS2); } -// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction -// against another. -// +/// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction +/// against another pointer. We know that V1 is a GEP, but we don't know +/// anything about V2. +/// AliasAnalysis::AliasResult -BasicAliasAnalysis::aliasGEP(const Value *V1, unsigned V1Size, +BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size, const Value *V2, unsigned V2Size) { // If we have two gep instructions with must-alias'ing base pointers, figure // out if the indexes to the GEP tell us anything about the derived pointer. // Note that we also handle chains of getelementptr instructions as well as // constant expression getelementptrs here. // - if (isa(V1) && isa(V2)) { - const User *GEP1 = cast(V1); - const User *GEP2 = cast(V2); - + if (const GEPOperator *GEP2 = dyn_cast(V2)) { // If V1 and V2 are identical GEPs, just recurse down on both of them. // This allows us to analyze things like: // P = gep A, 0, i, 1 @@ -438,13 +436,13 @@ BasicAliasAnalysis::aliasGEP(const Value *V1, unsigned V1Size, while (isa(GEP1->getOperand(0)) && GEP1->getOperand(1) == Constant::getNullValue(GEP1->getOperand(1)->getType())) - GEP1 = cast(GEP1->getOperand(0)); + GEP1 = cast(GEP1->getOperand(0)); const Value *BasePtr1 = GEP1->getOperand(0); while (isa(GEP2->getOperand(0)) && GEP2->getOperand(1) == Constant::getNullValue(GEP2->getOperand(1)->getType())) - GEP2 = cast(GEP2->getOperand(0)); + GEP2 = cast(GEP2->getOperand(0)); const Value *BasePtr2 = GEP2->getOperand(0); // Do the base pointers alias? @@ -457,8 +455,8 @@ BasicAliasAnalysis::aliasGEP(const Value *V1, unsigned V1Size, // Collect all of the chained GEP operands together into one simple place SmallVector GEP1Ops, GEP2Ops; - BasePtr1 = GetGEPOperands(V1, GEP1Ops); - BasePtr2 = GetGEPOperands(V2, GEP2Ops); + BasePtr1 = GetGEPOperands(GEP1, GEP1Ops); + BasePtr2 = GetGEPOperands(GEP2, GEP2Ops); // If GetGEPOperands were able to fold to the same must-aliased pointer, // do the comparison. @@ -482,7 +480,7 @@ BasicAliasAnalysis::aliasGEP(const Value *V1, unsigned V1Size, return MayAlias; SmallVector GEPOperands; - const Value *BasePtr = GetGEPOperands(V1, GEPOperands); + const Value *BasePtr = GetGEPOperands(GEP1, GEPOperands); AliasResult R = aliasCheck(BasePtr, ~0U, V2, V2Size); if (R != MustAlias) @@ -719,8 +717,8 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size, std::swap(V1, V2); std::swap(V1Size, V2Size); } - if (isa(V1)) - return aliasGEP(V1, V1Size, V2, V2Size); + if (const GEPOperator *GV1 = dyn_cast(V1)) + return aliasGEP(GV1, V1Size, V2, V2Size); if (isa(V2) && !isa(V1)) { std::swap(V1, V2); -- 2.34.1