llvm.org GIT mirror llvm / fd68750
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 Chris Lattner 12 years ago
2 changed file(s) with 52 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
137137 return new BasicAliasAnalysis();
138138 }
139139
140 // getUnderlyingObject - This traverses the use chain to figure out what object
141 // the specified value points to. If the value points to, or is derived from, a
142 // unique object or an argument, return it.
140 /// getUnderlyingObject - This traverses the use chain to figure out what object
141 /// the specified value points to. If the value points to, or is derived from,
142 /// a unique object or an argument, return it. This returns:
143 /// Arguments, GlobalVariables, Functions, Allocas, Mallocs.
143144 static const Value *getUnderlyingObject(const Value *V) {
144145 if (!isa(V->getType())) return 0;
145146
240241 //
241242 AliasAnalysis::ModRefResult
242243 BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
243 if (!isa(P))
244 if (const AllocationInst *AI =
245 dyn_cast_or_null(getUnderlyingObject(P))) {
244 if (!isa(P)) {
245 const Value *Object = getUnderlyingObject(P);
246 // Allocations and byval arguments are "new" objects.
247 if (isa(Object) ||
248 (isa(Object) && cast(Object)->hasByValAttr())) {
246249 // Okay, the pointer is to a stack allocated object. If we can prove that
247250 // the pointer never "escapes", then we know the call cannot clobber it,
248251 // because it simply can't get its address.
249 if (!AddressMightEscape(AI))
252 if (!AddressMightEscape(Object))
250253 return NoModRef;
251254
252255 // If this is a tail call and P points to a stack location, we know that
253256 // the tail call cannot access or modify the local stack.
254257 if (CallInst *CI = dyn_cast(CS.getInstruction()))
255 if (CI->isTailCall() && isa(AI))
258 if (CI->isTailCall() && !isa(Object))
256259 return NoModRef;
257260 }
261 }
258262
259263 // The AliasAnalysis base class has some smarts, lets use them.
260264 return AliasAnalysis::getModRefInfo(CS, P, Size);
261 }
262
263 static bool isNoAliasArgument(const Argument *Arg) {
264 const Function *Func = Arg->getParent();
265 const ParamAttrsList *Attr = Func->getParamAttrs();
266 if (Attr) {
267 unsigned Idx = 1;
268 for (Function::const_arg_iterator I = Func->arg_begin(),
269 E = Func->arg_end(); I != E; ++I, ++Idx) {
270 if (&(*I) == Arg &&
271 Attr->paramHasAttr(Idx, ParamAttr::NoAlias))
272 return true;
273 }
274 }
275 return false;
276265 }
277266
278267 // alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
316305
317306 // If they are two different objects, and one is a noalias argument
318307 // then they do not alias.
319 if (O1 != O2 && isNoAliasArgument(O1Arg))
308 if (O1 != O2 && O1Arg->hasNoAliasAttr())
320309 return NoAlias;
321
310
311 // Byval arguments can't alias globals or other arguments.
312 if (O1 != O2 && O1Arg->hasByValAttr()) return NoAlias;
313
322314 // Otherwise, nothing is known...
323315 }
324316
328320
329321 // If they are two different objects, and one is a noalias argument
330322 // then they do not alias.
331 if (O1 != O2 && isNoAliasArgument(O2Arg))
323 if (O1 != O2 && O2Arg->hasNoAliasAttr())
332324 return NoAlias;
333325
326 // Byval arguments can't alias globals or other arguments.
327 if (O1 != O2 && O2Arg->hasByValAttr()) return NoAlias;
328
334329 // Otherwise, nothing is known...
335330
336 } else if (O1 != O2) {
337 if (!isa(O1))
338 // If they are two different objects, and neither is an argument,
339 // we know that we have no alias...
340 return NoAlias;
331 } else if (O1 != O2 && !isa(O1)) {
332 // If they are two different objects, and neither is an argument,
333 // we know that we have no alias.
334 return NoAlias;
341335 }
342336
343337 // If they are the same object, they we can look at the indexes. If they
346340 // can't tell anything.
347341 }
348342
349
350 if (!isa(O1) && isa(V2))
351 return NoAlias; // Unique values don't alias null
343 // Unique values don't alias null, except non-byval arguments.
344 if (isa(V2)) {
345 if (const Argument *O1Arg = dyn_cast(O1)) {
346 if (O1Arg->hasByValAttr())
347 return NoAlias;
348 } else {
349 return NoAlias;
350 }
351 }
352352
353353 if (isa(O1) ||
354354 (isa(O1) &&
0 ; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {ret i32 1}
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"
2 target triple = "i686-apple-darwin8"
3 %struct.x = type { i32, i32, i32, i32 }
4 @g = weak global i32 0 ; [#uses=1]
5
6 define i32 @foo(%struct.x* byval %a) nounwind {
7 entry:
8 %tmp1 = tail call i32 (...)* @bar( %struct.x* %a ) nounwind ; [#uses=0]
9 %tmp2 = getelementptr %struct.x* %a, i32 0, i32 0 ; [#uses=2]
10 store i32 1, i32* %tmp2, align 4
11 store i32 2, i32* @g, align 4
12 %tmp4 = load i32* %tmp2, align 4 ; [#uses=1]
13 ret i32 %tmp4
14 }
15
16 declare i32 @bar(...)
17