llvm.org GIT mirror llvm / 20d6f09
Teach BasicAA::getModRefInfo(CallSite, CallSite) some tricks based on readnone/readonly functions. Teach memdep to look past readonly calls when analyzing deps for a readonly call. This allows elimination of a few more calls from 403.gcc: before: 63 gvn - Number of instructions PRE'd 153986 gvn - Number of instructions deleted 50069 gvn - Number of loads deleted after: 63 gvn - Number of instructions PRE'd 153991 gvn - Number of instructions deleted 50069 gvn - Number of loads deleted 5 calls isn't much, but this adds plumbing for the next change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60794 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
4 changed file(s) with 82 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
247247 bool isLoad,
248248 BasicBlock::iterator ScanIt,
249249 BasicBlock *BB);
250 MemDepResult getCallSiteDependencyFrom(CallSite C,
250 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
251251 BasicBlock::iterator ScanIt,
252252 BasicBlock *BB);
253253 void getNonLocalPointerDepFromBB(Value *Pointer, uint64_t Size,
263263 const Value *V2, unsigned V2Size);
264264
265265 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
266 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
267 return NoAA::getModRefInfo(CS1,CS2);
268 }
269
266 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
267
270268 /// hasNoModRefInfoForCalls - We can provide mod/ref information against
271269 /// non-escaping allocations.
272270 virtual bool hasNoModRefInfoForCalls() const { return false; }
348346
349347 // The AliasAnalysis base class has some smarts, lets use them.
350348 return AliasAnalysis::getModRefInfo(CS, P, Size);
349 }
350
351
352 AliasAnalysis::ModRefResult
353 BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
354 // If CS1 or CS2 are readnone, they don't interact.
355 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
356 if (CS1B == DoesNotAccessMemory) return NoModRef;
357
358 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
359 if (CS2B == DoesNotAccessMemory) return NoModRef;
360
361 // If they both only read from memory, just return ref.
362 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
363 return Ref;
364
365 // Otherwise, fall back to NoAA (mod+ref).
366 return NoAA::getModRefInfo(CS1, CS2);
351367 }
352368
353369
9898 /// getCallSiteDependencyFrom - Private helper for finding the local
9999 /// dependencies of a call site.
100100 MemDepResult MemoryDependenceAnalysis::
101 getCallSiteDependencyFrom(CallSite CS, BasicBlock::iterator ScanIt,
102 BasicBlock *BB) {
101 getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
102 BasicBlock::iterator ScanIt, BasicBlock *BB) {
103103 // Walk backwards through the block, looking for dependencies
104104 while (ScanIt != BB->begin()) {
105105 Instruction *Inst = --ScanIt;
121121 } else if (isa(Inst) || isa(Inst)) {
122122 CallSite InstCS = CallSite::get(Inst);
123123 // If these two calls do not interfere, look past it.
124 if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
124 switch (AA->getModRefInfo(CS, InstCS)) {
125 case AliasAnalysis::NoModRef:
126 // If the two calls don't interact (e.g. InstCS is readnone) keep
127 // scanning.
125128 continue;
126
127 // FIXME: If this is a ref/ref result, we should ignore it!
128 // X = strlen(P);
129 // Y = strlen(Q);
130 // Z = strlen(P); // Z = X
131
132 // If they interfere, we generally return clobber. However, if they are
133 // calls to the same read-only functions we return Def.
134 if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
135 CS.getCalledFunction() != InstCS.getCalledFunction())
129 case AliasAnalysis::Ref:
130 // If the two calls read the same memory locations and CS is a readonly
131 // function, then we have two cases: 1) the calls may not interfere with
132 // each other at all. 2) the calls may produce the same value. In case
133 // #1 we want to ignore the values, in case #2, we want to return Inst
134 // as a Def dependence. This allows us to CSE in cases like:
135 // X = strlen(P);
136 // memchr(...);
137 // Y = strlen(P); // Y = X
138 if (isReadOnlyCall) {
139 if (CS.getCalledFunction() != 0 &&
140 CS.getCalledFunction() == InstCS.getCalledFunction())
141 return MemDepResult::getDef(Inst);
142 // Ignore unrelated read/read call dependences.
143 continue;
144 }
145 // FALL THROUGH
146 default:
136147 return MemDepResult::getClobber(Inst);
137 return MemDepResult::getDef(Inst);
148 }
138149 } else {
139150 // Non-memory instruction.
140151 continue;
211222 }
212223
213224 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
214 // FIXME: If this is a load, we should ignore readonly calls!
215225 switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) {
216226 case AliasAnalysis::NoModRef:
217227 // If the call has no effect on the queried pointer, just ignore it.
288298 MemSize = TD->getTypeStoreSize(LI->getType());
289299 }
290300 } else if (isa(QueryInst) || isa(QueryInst)) {
291 LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
301 CallSite QueryCS = CallSite::get(QueryInst);
302 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
303 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
292304 QueryParent);
293305 } else if (FreeInst *FI = dyn_cast(QueryInst)) {
294306 MemPtr = FI->getPointerOperand();
366378 NumUncacheNonLocal++;
367379 }
368380
381 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
382 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
383
369384 // Visited checked first, vector in sorted order.
370385 SmallPtrSet Visited;
371386
416431 MemDepResult Dep;
417432
418433 if (ScanPos != DirtyBB->begin()) {
419 Dep = getCallSiteDependencyFrom(QueryCS, ScanPos, DirtyBB);
434 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
420435 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
421436 // No dependence found. If this is the entry block of the function, it is
422437 // a clobber, otherwise it is non-local.
0 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep {call.*strlen} | count 1
1 ; Should delete the second call to strlen even though the intervening strchr call exists.
2
3 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"
4 target triple = "i386-apple-darwin7"
5
6 define i8* @test(i8* %P, i8* %Q, i32 %x, i32 %y) nounwind readonly {
7 entry:
8 %0 = tail call i32 @strlen(i8* %P) nounwind readonly ; [#uses=2]
9 %1 = icmp eq i32 %0, 0 ; [#uses=1]
10 br i1 %1, label %bb, label %bb1
11
12 bb: ; preds = %entry
13 %2 = sdiv i32 %x, %y ; [#uses=1]
14 br label %bb1
15
16 bb1: ; preds = %bb, %entry
17 %x_addr.0 = phi i32 [ %2, %bb ], [ %x, %entry ] ; [#uses=1]
18 %3 = tail call i8* @strchr(i8* %Q, i32 97) nounwind readonly ; [#uses=1]
19 %4 = tail call i32 @strlen(i8* %P) nounwind readonly ; [#uses=1]
20 %5 = add i32 %x_addr.0, %0 ; [#uses=1]
21 %.sum = sub i32 %5, %4 ; [#uses=1]
22 %6 = getelementptr i8* %3, i32 %.sum ; [#uses=1]
23 ret i8* %6
24 }
25
26 declare i32 @strlen(i8*) nounwind readonly
27
28 declare i8* @strchr(i8*, i32) nounwind readonly