From: Ted Kremenek Date: Fri, 26 Sep 2008 05:52:45 +0000 (+0000) Subject: Examine VLA size expressions when computing liveness information. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84fa6b90abf73e8cc539c9947ed5a6286f588569;p=clang Examine VLA size expressions when computing liveness information. Fixes git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56645 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/LiveVariables.cpp b/lib/Analysis/LiveVariables.cpp index f5a7fb40c7..e82c1e0bc0 100644 --- a/lib/Analysis/LiveVariables.cpp +++ b/lib/Analysis/LiveVariables.cpp @@ -226,19 +226,41 @@ void TransferFuncs::VisitAssign(BinaryOperator* B) { Visit(B->getRHS()); } +static VariableArrayType* FindVA(Type* t) { + while (ArrayType* vt = dyn_cast(t)) { + if (VariableArrayType* vat = dyn_cast(vt)) + if (vat->getSizeExpr()) + return vat; + + t = vt->getElementType().getTypePtr(); + } + + return NULL; +} + void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { // Declarations effectively "kill" a variable since they cannot // possibly be live before they are declared. for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end(); DI != DE; ++DI) if (VarDecl* VD = dyn_cast(*DI)) { + // The initializer is evaluated after the variable comes into scope. + // Since this is a reverse dataflow analysis, we must evaluate the + // transfer function for this expression first. + if (Expr* Init = VD->getInit()) + Visit(Init); - // Update liveness information. + // Update liveness information by killing the VarDecl. unsigned bit = AD.getIdx(VD); LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit); - - if (Expr* Init = VD->getInit()) - Visit(Init); + + // If the type of VD is a VLA, then we must process its size expressions. + // These expressions are evaluated before the variable comes into scope, + // so in a reverse dataflow analysis we evaluate them last. + for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; + VA = FindVA(VA->getElementType().getTypePtr())) + Visit(VA->getSizeExpr()); + } } diff --git a/test/Analysis/dead-stores.c b/test/Analysis/dead-stores.c index d87a5591a3..b1f16b9c84 100644 --- a/test/Analysis/dead-stores.c +++ b/test/Analysis/dead-stores.c @@ -100,7 +100,7 @@ int f13(void) } // Filed with PR 2763. -int f41(int count) { +int f14(int count) { int index, nextLineIndex; for (index = 0; index < count; index = nextLineIndex+1) { nextLineIndex = index+1; // no-warning @@ -108,3 +108,10 @@ int f41(int count) { } return index; } + +// Test case for +void f15(unsigned x, unsigned y) { + int count = x * y; // no-warning + int z[count]; +} +