llvm.org GIT mirror llvm / 0bcb8fb
Add some shortcuts in LazyValueInfo to reduce compile time of Correlated Value Propagation. The patch is to partially fix PR10584. Correlated Value Propagation queries LVI to check non-null for pointer params of each callsite. If we know the def of param is an alloca instruction, we know it is non-null and can return early from LVI. Similarly, CVP queries LVI to check whether pointer for each mem access is constant. If the def of the pointer is an alloca instruction, we know it is not a constant pointer. These shortcuts can reduce the cost of CVP significantly. Differential Revision: https://reviews.llvm.org/D18066 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281586 91177308-0d34-0410-b5e6-96231b3b80d8 Wei Mi 3 years ago
2 changed file(s) with 77 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
16001600 return LazyValueInfo(&AC, &TLI, DT);
16011601 }
16021602
1603 /// Returns true if we can statically tell that this value will never be a
1604 /// "useful" constant. In practice, this means we've got something like an
1605 /// alloca or a malloc call for which a comparison against a constant can
1606 /// only be guarding dead code. Note that we are potentially giving up some
1607 /// precision in dead code (a constant result) in favour of avoiding a
1608 /// expensive search for a easily answered common query.
1609 static bool isKnownNonConstant(Value *V) {
1610 V = V->stripPointerCasts();
1611 // The return val of alloc cannot be a Constant.
1612 if (isa(V))
1613 return true;
1614 return false;
1615 }
1616
16031617 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
16041618 Instruction *CxtI) {
1619 // Bail out early if V is known not to be a Constant.
1620 if (isKnownNonConstant(V))
1621 return nullptr;
1622
16051623 const DataLayout &DL = BB->getModule()->getDataLayout();
16061624 LVILatticeVal Result =
16071625 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
17371755 LazyValueInfo::Tristate
17381756 LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
17391757 Instruction *CxtI) {
1758 // Is or is not NonNull are common predicates being queried. If
1759 // isKnownNonNull can tell us the result of the predicate, we can
1760 // return it quickly. But this is only a fastpath, and falling
1761 // through would still be correct.
1762 if (V->getType()->isPointerTy() && C->isNullValue() &&
1763 isKnownNonNull(V->stripPointerCasts())) {
1764 if (Pred == ICmpInst::ICMP_EQ)
1765 return LazyValueInfo::False;
1766 else if (Pred == ICmpInst::ICMP_NE)
1767 return LazyValueInfo::True;
1768 }
17401769 const DataLayout &DL = CxtI->getModule()->getDataLayout();
17411770 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
17421771 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
0 ; RUN: opt -S -correlated-propagation -debug-only=lazy-value-info <%s 2>&1 | FileCheck %s
1 ;
2 ; Shortcut in Correlated Value Propagation ensures not to take Lazy Value Info
3 ; analysis for %a.i and %tmp because %a.i is defined by alloca and %tmp is
4 ; defined by alloca + bitcast. We know the ret value of alloca is nonnull.
5 ;
6 ; CHECK-NOT: LVI Getting edge value %a.i = alloca i64, align 8 at 'for.body'
7 ; CHECK-NOT: LVI Getting edge value %tmp = bitcast i64* %a.i to i8* from 'for.cond' to 'for.body'
8 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
9 target triple = "x86_64-unknown-linux-gnu"
10
11 @.str = private unnamed_addr constant [8 x i8] c"a = %l\0A\00", align 1
12
13 ; Function Attrs: argmemonly nounwind
14 declare void @llvm.lifetime.start(i64, i8* nocapture)
15
16 declare void @hoo(i64*)
17
18 declare i32 @printf(i8* nocapture readonly, ...)
19
20 ; Function Attrs: argmemonly nounwind
21 declare void @llvm.lifetime.end(i64, i8* nocapture)
22
23 define void @goo(i32 %N, i64* %b) {
24 entry:
25 %a.i = alloca i64, align 8
26 %tmp = bitcast i64* %a.i to i8*
27 %c = getelementptr inbounds i64, i64* %b, i64 0
28 br label %for.cond
29
30 for.cond: ; preds = %for.body, %entry
31 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
32 %cmp = icmp slt i32 %i.0, %N
33 br i1 %cmp, label %for.body, label %for.end
34
35 for.body: ; preds = %for.cond
36 call void @llvm.lifetime.start(i64 8, i8* %tmp)
37 call void @hoo(i64* %a.i)
38 call void @hoo(i64* %c)
39 %tmp1 = load volatile i64, i64* %a.i, align 8
40 %call.i = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %tmp1)
41 call void @llvm.lifetime.end(i64 8, i8* %tmp)
42 %inc = add nsw i32 %i.0, 1
43 br label %for.cond
44
45 for.end: ; preds = %for.cond
46 ret void
47 }