From: Chris Lattner Date: Thu, 24 Jan 2008 18:00:32 +0000 (+0000) Subject: Teach basicaa that 'byval' arguments define a new memory location that X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=fd687500384803311556571989dc14cd84786904;p=oota-llvm.git Teach basicaa that 'byval' arguments define a new memory location that can't be aliased to other known objects. This allows us to know that byval pointer args don't alias globals, etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46315 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp index 95278020a25..381298b86ea 100644 --- a/lib/Analysis/BasicAliasAnalysis.cpp +++ b/lib/Analysis/BasicAliasAnalysis.cpp @@ -138,9 +138,10 @@ ImmutablePass *llvm::createBasicAliasAnalysisPass() { return new BasicAliasAnalysis(); } -// getUnderlyingObject - This traverses the use chain to figure out what object -// the specified value points to. If the value points to, or is derived from, a -// unique object or an argument, return it. +/// getUnderlyingObject - This traverses the use chain to figure out what object +/// the specified value points to. If the value points to, or is derived from, +/// a unique object or an argument, return it. This returns: +/// Arguments, GlobalVariables, Functions, Allocas, Mallocs. static const Value *getUnderlyingObject(const Value *V) { if (!isa(V->getType())) return 0; @@ -241,41 +242,29 @@ static bool AddressMightEscape(const Value *V) { // AliasAnalysis::ModRefResult BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { - if (!isa(P)) - if (const AllocationInst *AI = - dyn_cast_or_null(getUnderlyingObject(P))) { + if (!isa(P)) { + const Value *Object = getUnderlyingObject(P); + // Allocations and byval arguments are "new" objects. + if (isa(Object) || + (isa(Object) && cast(Object)->hasByValAttr())) { // Okay, the pointer is to a stack allocated object. If we can prove that // the pointer never "escapes", then we know the call cannot clobber it, // because it simply can't get its address. - if (!AddressMightEscape(AI)) + if (!AddressMightEscape(Object)) return NoModRef; // If this is a tail call and P points to a stack location, we know that // the tail call cannot access or modify the local stack. if (CallInst *CI = dyn_cast(CS.getInstruction())) - if (CI->isTailCall() && isa(AI)) + if (CI->isTailCall() && !isa(Object)) return NoModRef; } + } // The AliasAnalysis base class has some smarts, lets use them. return AliasAnalysis::getModRefInfo(CS, P, Size); } -static bool isNoAliasArgument(const Argument *Arg) { - const Function *Func = Arg->getParent(); - const ParamAttrsList *Attr = Func->getParamAttrs(); - if (Attr) { - unsigned Idx = 1; - for (Function::const_arg_iterator I = Func->arg_begin(), - E = Func->arg_end(); I != E; ++I, ++Idx) { - if (&(*I) == Arg && - Attr->paramHasAttr(Idx, ParamAttr::NoAlias)) - return true; - } - } - return false; -} - // 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. // Hopefully we have a smart C++ compiler. :) @@ -317,9 +306,12 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size, // If they are two different objects, and one is a noalias argument // then they do not alias. - if (O1 != O2 && isNoAliasArgument(O1Arg)) + if (O1 != O2 && O1Arg->hasNoAliasAttr()) return NoAlias; - + + // Byval arguments can't alias globals or other arguments. + if (O1 != O2 && O1Arg->hasByValAttr()) return NoAlias; + // Otherwise, nothing is known... } @@ -329,16 +321,18 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size, // If they are two different objects, and one is a noalias argument // then they do not alias. - if (O1 != O2 && isNoAliasArgument(O2Arg)) + if (O1 != O2 && O2Arg->hasNoAliasAttr()) return NoAlias; + // Byval arguments can't alias globals or other arguments. + if (O1 != O2 && O2Arg->hasByValAttr()) return NoAlias; + // Otherwise, nothing is known... - } else if (O1 != O2) { - if (!isa(O1)) - // If they are two different objects, and neither is an argument, - // we know that we have no alias... - return NoAlias; + } else if (O1 != O2 && !isa(O1)) { + // If they are two different objects, and neither is an argument, + // we know that we have no alias. + return NoAlias; } // If they are the same object, they we can look at the indexes. If they @@ -347,9 +341,15 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size, // can't tell anything. } - - if (!isa(O1) && isa(V2)) - return NoAlias; // Unique values don't alias null + // Unique values don't alias null, except non-byval arguments. + if (isa(V2)) { + if (const Argument *O1Arg = dyn_cast(O1)) { + if (O1Arg->hasByValAttr()) + return NoAlias; + } else { + return NoAlias; + } + } if (isa(O1) || (isa(O1) && diff --git a/test/Analysis/BasicAA/byval.ll b/test/Analysis/BasicAA/byval.ll new file mode 100644 index 00000000000..f0644198b7d --- /dev/null +++ b/test/Analysis/BasicAA/byval.ll @@ -0,0 +1,18 @@ +; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {ret i32 1} +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" +target triple = "i686-apple-darwin8" + %struct.x = type { i32, i32, i32, i32 } +@g = weak global i32 0 ; [#uses=1] + +define i32 @foo(%struct.x* byval %a) nounwind { +entry: + %tmp1 = tail call i32 (...)* @bar( %struct.x* %a ) nounwind ; [#uses=0] + %tmp2 = getelementptr %struct.x* %a, i32 0, i32 0 ; [#uses=2] + store i32 1, i32* %tmp2, align 4 + store i32 2, i32* @g, align 4 + %tmp4 = load i32* %tmp2, align 4 ; [#uses=1] + ret i32 %tmp4 +} + +declare i32 @bar(...) +