2008-01-15 23:55:06 +00:00
|
|
|
//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
|
|
|
|
//
|
2008-01-23 19:59:44 +00:00
|
|
|
// The LLValM Compiler Infrastructure
|
2008-01-15 23:55:06 +00:00
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Constant Propagation via Graph Reachability
|
|
|
|
//
|
|
|
|
// This files defines a simple analysis that performs path-sensitive
|
|
|
|
// constant propagation within a function. An example use of this analysis
|
|
|
|
// is to perform simple checks for NULL dereferences.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/PathSensitive/GREngine.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2008-01-24 02:02:54 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-01-15 23:55:06 +00:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
|
|
|
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include "llvm/ADT/APSInt.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2008-01-16 17:56:25 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-01-23 19:59:44 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2008-01-15 23:55:06 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
using namespace clang;
|
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::cast;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-23 19:59:44 +00:00
|
|
|
/// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a
|
2008-01-15 23:55:06 +00:00
|
|
|
/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ValueKey {
|
2008-01-16 19:47:19 +00:00
|
|
|
uintptr_t Raw;
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-24 19:43:37 +00:00
|
|
|
enum Kind { IsSubExp=0x0, IsBlkExpr=0x1, IsDecl=0x2, Flags=0x3 };
|
2008-01-15 23:55:06 +00:00
|
|
|
inline void* getPtr() const { return reinterpret_cast<void*>(Raw & ~Flags); }
|
2008-01-23 19:59:44 +00:00
|
|
|
inline Kind getKind() const { return (Kind) (Raw & Flags); }
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
ValueKey(const ValueDecl* VD)
|
|
|
|
: Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {}
|
|
|
|
|
2008-01-24 19:43:37 +00:00
|
|
|
ValueKey(Stmt* S, bool isBlkExpr = false)
|
2008-01-23 19:59:44 +00:00
|
|
|
: Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExp)){}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
bool isSubExpr() const { return getKind() == IsSubExp; }
|
2008-01-23 19:59:44 +00:00
|
|
|
bool isDecl() const { return getKind() == IsDecl; }
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
inline void Profile(llvm::FoldingSetNodeID& ID) const {
|
2008-01-16 05:51:13 +00:00
|
|
|
ID.AddPointer(getPtr());
|
|
|
|
ID.AddInteger((unsigned) getKind());
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const ValueKey& X) const {
|
2008-01-24 19:43:37 +00:00
|
|
|
return getPtr() == X.getPtr();
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const ValueKey& X) const {
|
|
|
|
return !operator==(X);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const ValueKey& X) const {
|
|
|
|
Kind k = getKind(), Xk = X.getKind();
|
2008-01-24 19:43:37 +00:00
|
|
|
|
|
|
|
if (k == IsDecl) {
|
|
|
|
if (Xk != IsDecl)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (Xk == IsDecl)
|
|
|
|
return true;
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-24 19:43:37 +00:00
|
|
|
return getPtr() < X.getPtr();
|
2008-01-16 23:33:44 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// Machinery to get cast<> and dyn_cast<> working with ValueKey.
|
2008-01-15 23:55:06 +00:00
|
|
|
namespace llvm {
|
2008-01-23 19:59:44 +00:00
|
|
|
template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) {
|
|
|
|
return V.getKind() == ValueKey::IsDecl;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) {
|
|
|
|
return ((unsigned) V.getKind()) != ValueKey::IsDecl;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> {
|
2008-01-16 21:46:15 +00:00
|
|
|
typedef const ValueDecl* ret_type;
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
2008-01-23 19:59:44 +00:00
|
|
|
template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> {
|
2008-01-15 23:55:06 +00:00
|
|
|
typedef const Stmt* ret_type;
|
|
|
|
};
|
2008-01-23 19:59:44 +00:00
|
|
|
template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> {
|
2008-01-15 23:55:06 +00:00
|
|
|
typedef void* SimpleType;
|
2008-01-23 19:59:44 +00:00
|
|
|
static inline SimpleType getSimplifiedValue(const ValueKey &V) {
|
2008-01-15 23:55:06 +00:00
|
|
|
return V.getPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-23 19:59:44 +00:00
|
|
|
// ValueManager.
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef llvm::ImmutableSet<llvm::APSInt > APSIntSetTy;
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ValueManager {
|
|
|
|
APSIntSetTy::Factory APSIntSetFactory;
|
2008-01-24 02:02:54 +00:00
|
|
|
ASTContext* Ctx;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ValueManager() {}
|
|
|
|
~ValueManager() {}
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
void setContext(ASTContext* ctx) { Ctx = ctx; }
|
|
|
|
ASTContext* getContext() const { return Ctx; }
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
APSIntSetTy GetEmptyAPSIntSet() {
|
|
|
|
return APSIntSetFactory.GetEmptySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
APSIntSetTy AddToSet(const APSIntSetTy& Set, const llvm::APSInt& Val) {
|
|
|
|
return APSIntSetFactory.Add(Set, Val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Expression Values.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ExprValue {
|
|
|
|
public:
|
|
|
|
enum Kind { // L-Values.
|
|
|
|
LValueDeclKind = 0x0,
|
|
|
|
// Special "Invalid" value.
|
|
|
|
InvalidValueKind = 0x1,
|
|
|
|
// R-Values.
|
|
|
|
RValueMayEqualSetKind = 0x2,
|
|
|
|
// Note that the Lvalue and RValue "kinds" overlap;
|
|
|
|
// the "InvalidValue" class can be used either as
|
|
|
|
// an LValue or RValue.
|
|
|
|
MinLValueKind = 0x0, MaxLValueKind = 0x1,
|
|
|
|
MinRValueKind = 0x1, MaxRValueKind = 0x2 };
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum Kind kind;
|
|
|
|
void* Data;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ExprValue(Kind k, void* d) : kind(k), Data(d) {}
|
|
|
|
|
|
|
|
void* getRawPtr() const { return Data; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
~ExprValue() {};
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
ExprValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned) getKind());
|
|
|
|
ID.AddPointer(Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ExprValue& RHS) const {
|
|
|
|
return kind == RHS.kind && Data == RHS.Data;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
Kind getKind() const { return kind; }
|
|
|
|
bool isValid() const { return getKind() != InvalidValueKind; }
|
|
|
|
|
|
|
|
void print(std::ostream& OS) const;
|
|
|
|
void print() const { print(*llvm::cerr.stream()); }
|
|
|
|
|
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const ExprValue*) { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN InvalidValue : public ExprValue {
|
|
|
|
public:
|
|
|
|
InvalidValue() : ExprValue(InvalidValueKind, NULL) {}
|
|
|
|
|
|
|
|
static inline bool classof(const ExprValue* V) {
|
|
|
|
return V->getKind() == InvalidValueKind;
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-23 19:59:44 +00:00
|
|
|
// "R-Values": Interface.
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-01-23 19:59:44 +00:00
|
|
|
class VISIBILITY_HIDDEN RValue : public ExprValue {
|
|
|
|
protected:
|
|
|
|
RValue(Kind k, void* d) : ExprValue(k,d) {}
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue EvalAdd(ValueManager& ValMgr, const RValue& RHS) const;
|
|
|
|
RValue EvalSub(ValueManager& ValMgr, const RValue& RHS) const;
|
|
|
|
RValue EvalMul(ValueManager& ValMgr, const RValue& RHS) const;
|
2008-01-24 08:20:02 +00:00
|
|
|
RValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
static RValue GetRValue(ValueManager& ValMgr, const llvm::APSInt& V);
|
2008-01-24 08:20:02 +00:00
|
|
|
static RValue GetRValue(ValueManager& ValMgr, IntegerLiteral* I);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const ExprValue* V) {
|
|
|
|
return V->getKind() >= MinRValueKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN RValueMayEqualSet : public RValue {
|
|
|
|
public:
|
|
|
|
RValueMayEqualSet(const APSIntSetTy& S)
|
|
|
|
: RValue(RValueMayEqualSetKind, S.getRoot()) {}
|
|
|
|
|
|
|
|
APSIntSetTy GetValues() const {
|
|
|
|
return APSIntSetTy(reinterpret_cast<APSIntSetTy::TreeTy*>(getRawPtr()));
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
RValueMayEqualSet EvalAdd(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& V) const;
|
|
|
|
|
|
|
|
RValueMayEqualSet EvalSub(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& V) const;
|
|
|
|
|
|
|
|
RValueMayEqualSet EvalMul(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& V) const;
|
|
|
|
|
|
|
|
RValueMayEqualSet EvalCast(ValueManager& ValMgr, Expr* CastExpr) const;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 08:20:02 +00:00
|
|
|
RValueMayEqualSet EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const;
|
|
|
|
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const ExprValue* V) {
|
|
|
|
return V->getKind() == RValueMayEqualSetKind;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-24 02:02:54 +00:00
|
|
|
// Transfer functions: Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ExprValue ExprValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case RValueMayEqualSetKind:
|
|
|
|
return cast<RValueMayEqualSet>(this)->EvalCast(ValMgr, CastExpr);
|
|
|
|
default:
|
|
|
|
return InvalidValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RValueMayEqualSet
|
|
|
|
RValueMayEqualSet::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const {
|
|
|
|
QualType T = CastExpr->getType();
|
|
|
|
assert (T->isIntegerType());
|
|
|
|
|
|
|
|
APSIntSetTy S1 = GetValues();
|
|
|
|
APSIntSetTy S2 = ValMgr.GetEmptyAPSIntSet();
|
|
|
|
|
|
|
|
for (APSIntSetTy::iterator I=S1.begin(), E=S1.end(); I!=E; ++I) {
|
|
|
|
llvm::APSInt X = *I;
|
|
|
|
X.setIsSigned(T->isSignedIntegerType());
|
|
|
|
X.extOrTrunc(ValMgr.getContext()->getTypeSize(T,CastExpr->getLocStart()));
|
|
|
|
S2 = ValMgr.AddToSet(S2, X);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S2;
|
|
|
|
}
|
|
|
|
|
2008-01-24 08:20:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: Unary Operations over R-Values.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
RValue RValue::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case RValueMayEqualSetKind:
|
|
|
|
return cast<RValueMayEqualSet>(this)->EvalMinus(ValMgr, U);
|
|
|
|
default:
|
|
|
|
return cast<RValue>(InvalidValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RValueMayEqualSet
|
|
|
|
RValueMayEqualSet::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const {
|
|
|
|
|
|
|
|
assert (U->getType() == U->getSubExpr()->getType());
|
|
|
|
assert (U->getType()->isIntegerType());
|
|
|
|
|
|
|
|
APSIntSetTy S1 = GetValues();
|
|
|
|
APSIntSetTy S2 = ValMgr.GetEmptyAPSIntSet();
|
|
|
|
|
|
|
|
for (APSIntSetTy::iterator I=S1.begin(), E=S1.end(); I!=E; ++I) {
|
|
|
|
assert ((*I).isSigned());
|
|
|
|
|
|
|
|
// FIXME: Shouldn't operator- on APSInt return an APSInt with the proper
|
|
|
|
// sign?
|
|
|
|
llvm::APSInt X(-(*I));
|
|
|
|
X.setIsSigned(true);
|
|
|
|
|
|
|
|
S2 = ValMgr.AddToSet(S2, X);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: Binary Operations over R-Values.
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define RVALUE_DISPATCH_CASE(k1,k2,Op)\
|
|
|
|
case ((k1##Kind+(MaxRValueKind-MinRValueKind))+(k2##Kind - MinRValueKind)):\
|
2008-01-24 02:02:54 +00:00
|
|
|
return cast<k1>(*this).Eval##Op(ValMgr,cast<k2>(RHS));
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
#define RVALUE_DISPATCH(Op)\
|
|
|
|
switch (getKind()+(MaxRValueKind-MinRValueKind)+(RHS.getKind()-MinRValueKind)){\
|
|
|
|
RVALUE_DISPATCH_CASE(RValueMayEqualSet,RValueMayEqualSet,Op)\
|
|
|
|
default:\
|
|
|
|
assert (!isValid() || !RHS.isValid() && "Missing case.");\
|
|
|
|
break;\
|
|
|
|
}\
|
|
|
|
return cast<RValue>(InvalidValue());
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue RValue::EvalAdd(ValueManager& ValMgr, const RValue& RHS) const {
|
2008-01-23 19:59:44 +00:00
|
|
|
RVALUE_DISPATCH(Add)
|
|
|
|
}
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue RValue::EvalSub(ValueManager& ValMgr, const RValue& RHS) const {
|
2008-01-23 19:59:44 +00:00
|
|
|
RVALUE_DISPATCH(Sub)
|
|
|
|
}
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue RValue::EvalMul(ValueManager& ValMgr, const RValue& RHS) const {
|
2008-01-23 23:42:27 +00:00
|
|
|
RVALUE_DISPATCH(Mul)
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
#undef RVALUE_DISPATCH_CASE
|
|
|
|
#undef RVALUE_DISPATCH
|
|
|
|
|
|
|
|
RValueMayEqualSet
|
2008-01-24 02:02:54 +00:00
|
|
|
RValueMayEqualSet::EvalAdd(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& RHS) const {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
APSIntSetTy S1 = GetValues();
|
|
|
|
APSIntSetTy S2 = RHS.GetValues();
|
|
|
|
|
|
|
|
APSIntSetTy M = ValMgr.GetEmptyAPSIntSet();
|
|
|
|
|
|
|
|
for (APSIntSetTy::iterator I1=S1.begin(), E1=S2.end(); I1!=E1; ++I1)
|
2008-01-24 08:20:02 +00:00
|
|
|
for (APSIntSetTy::iterator I2=S2.begin(), E2=S2.end(); I2!=E2; ++I2) {
|
|
|
|
// FIXME: operator- on APSInt is really operator* on APInt, which loses
|
|
|
|
// the "signess" information (although the bits are correct).
|
|
|
|
const llvm::APSInt& X = *I1;
|
|
|
|
llvm::APSInt Y = X + *I2;
|
|
|
|
Y.setIsSigned(X.isSigned());
|
|
|
|
M = ValMgr.AddToSet(M, Y);
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
RValueMayEqualSet
|
2008-01-24 02:02:54 +00:00
|
|
|
RValueMayEqualSet::EvalSub(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& RHS) const {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
APSIntSetTy S1 = GetValues();
|
|
|
|
APSIntSetTy S2 = RHS.GetValues();
|
|
|
|
|
|
|
|
APSIntSetTy M = ValMgr.GetEmptyAPSIntSet();
|
|
|
|
|
|
|
|
for (APSIntSetTy::iterator I1=S1.begin(), E1=S2.end(); I1!=E1; ++I1)
|
2008-01-24 08:20:02 +00:00
|
|
|
for (APSIntSetTy::iterator I2=S2.begin(), E2=S2.end(); I2!=E2; ++I2) {
|
|
|
|
// FIXME: operator- on APSInt is really operator* on APInt, which loses
|
|
|
|
// the "signess" information (although the bits are correct).
|
|
|
|
const llvm::APSInt& X = *I1;
|
|
|
|
llvm::APSInt Y = X - *I2;
|
|
|
|
Y.setIsSigned(X.isSigned());
|
|
|
|
M = ValMgr.AddToSet(M, Y);
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2008-01-23 23:42:27 +00:00
|
|
|
RValueMayEqualSet
|
2008-01-24 02:02:54 +00:00
|
|
|
RValueMayEqualSet::EvalMul(ValueManager& ValMgr,
|
|
|
|
const RValueMayEqualSet& RHS) const {
|
2008-01-23 23:42:27 +00:00
|
|
|
|
|
|
|
APSIntSetTy S1 = GetValues();
|
|
|
|
APSIntSetTy S2 = RHS.GetValues();
|
|
|
|
|
|
|
|
APSIntSetTy M = ValMgr.GetEmptyAPSIntSet();
|
|
|
|
|
|
|
|
for (APSIntSetTy::iterator I1=S1.begin(), E1=S2.end(); I1!=E1; ++I1)
|
2008-01-24 08:20:02 +00:00
|
|
|
for (APSIntSetTy::iterator I2=S2.begin(), E2=S2.end(); I2!=E2; ++I2) {
|
|
|
|
// FIXME: operator* on APSInt is really operator* on APInt, which loses
|
|
|
|
// the "signess" information (although the bits are correct).
|
|
|
|
const llvm::APSInt& X = *I1;
|
|
|
|
llvm::APSInt Y = X * *I2;
|
|
|
|
Y.setIsSigned(X.isSigned());
|
|
|
|
M = ValMgr.AddToSet(M, Y);
|
|
|
|
}
|
2008-01-23 23:42:27 +00:00
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
RValue RValue::GetRValue(ValueManager& ValMgr, const llvm::APSInt& V) {
|
|
|
|
return RValueMayEqualSet(ValMgr.AddToSet(ValMgr.GetEmptyAPSIntSet(), V));
|
2008-01-24 08:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RValue RValue::GetRValue(ValueManager& ValMgr, IntegerLiteral* I) {
|
|
|
|
llvm::APSInt X(I->getValue());
|
|
|
|
X.setIsSigned(I->getType()->isSignedIntegerType());
|
|
|
|
return GetRValue(ValMgr, X);
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "L-Values".
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
class VISIBILITY_HIDDEN LValue : public ExprValue {
|
|
|
|
protected:
|
|
|
|
LValue(Kind k, void* D) : ExprValue(k, D) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const ExprValue* V) {
|
|
|
|
return V->getKind() <= MaxLValueKind;
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN LValueDecl : public LValue {
|
|
|
|
public:
|
|
|
|
LValueDecl(ValueDecl* vd) : LValue(LValueDeclKind,vd) {}
|
|
|
|
|
|
|
|
ValueDecl* getDecl() const {
|
|
|
|
return static_cast<ValueDecl*>(getRawPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const ExprValue* V) {
|
|
|
|
return V->getKind() == LValueDeclKind;
|
|
|
|
}
|
|
|
|
};
|
2008-01-15 23:55:06 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pretty-Printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ExprValue::print(std::ostream& Out) const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case InvalidValueKind:
|
|
|
|
Out << "Invalid"; break;
|
|
|
|
|
|
|
|
case RValueMayEqualSetKind: {
|
|
|
|
APSIntSetTy S = cast<RValueMayEqualSet>(this)->GetValues();
|
2008-01-23 22:30:44 +00:00
|
|
|
bool first = true;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
for (APSIntSetTy::iterator I=S.begin(), E=S.end(); I!=E; ++I) {
|
|
|
|
if (first) first = false;
|
|
|
|
else Out << " | ";
|
|
|
|
|
|
|
|
Out << (*I).toString();
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert (false && "Pretty-printed not implemented for this ExprValue.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueMapTy - A ImmutableMap type Stmt*/Decl* to ExprValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef llvm::ImmutableMap<ValueKey,ExprValue> ValueMapTy;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
template<>
|
|
|
|
struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
|
|
|
|
static inline void* toPtr(ValueMapTy M) {
|
|
|
|
return reinterpret_cast<void*>(M.getRoot());
|
|
|
|
}
|
|
|
|
static inline ValueMapTy toState(void* P) {
|
|
|
|
return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The Checker!
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN GRConstants {
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
public:
|
2008-01-23 19:59:44 +00:00
|
|
|
typedef ValueMapTy StateTy;
|
2008-01-15 23:55:06 +00:00
|
|
|
typedef GRNodeBuilder<GRConstants> NodeBuilder;
|
|
|
|
typedef ExplodedNode<StateTy> NodeTy;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
class NodeSet {
|
|
|
|
typedef llvm::SmallVector<NodeTy*,3> ImplTy;
|
|
|
|
ImplTy Impl;
|
|
|
|
public:
|
|
|
|
|
|
|
|
NodeSet() {}
|
|
|
|
NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); }
|
|
|
|
|
|
|
|
void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); }
|
|
|
|
|
|
|
|
typedef ImplTy::iterator iterator;
|
|
|
|
typedef ImplTy::const_iterator const_iterator;
|
|
|
|
|
|
|
|
unsigned size() const { return Impl.size(); }
|
|
|
|
|
|
|
|
iterator begin() { return Impl.begin(); }
|
|
|
|
iterator end() { return Impl.end(); }
|
|
|
|
|
|
|
|
const_iterator begin() const { return Impl.begin(); }
|
|
|
|
const_iterator end() const { return Impl.end(); }
|
|
|
|
};
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
protected:
|
2008-01-23 19:59:44 +00:00
|
|
|
/// Liveness - live-variables information the ValueDecl* and block-level
|
|
|
|
/// Expr* in the CFG. Used to prune out dead state.
|
2008-01-15 23:55:06 +00:00
|
|
|
LiveVariables* Liveness;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// Builder - The current GRNodeBuilder which is used when building the nodes
|
|
|
|
/// for a given statement.
|
2008-01-15 23:55:06 +00:00
|
|
|
NodeBuilder* Builder;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// StateMgr - Object that manages the data for all created states.
|
|
|
|
ValueMapTy::Factory StateMgr;
|
|
|
|
|
|
|
|
/// ValueMgr - Object that manages the data for all created ExprValues.
|
|
|
|
ValueManager ValMgr;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// cfg - the current CFG.
|
2008-01-15 23:55:06 +00:00
|
|
|
CFG* cfg;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
/// StmtEntryNode - The immediate predecessor node.
|
|
|
|
NodeTy* StmtEntryNode;
|
|
|
|
|
|
|
|
/// CurrentStmt - The current block-level statement.
|
|
|
|
Stmt* CurrentStmt;
|
|
|
|
|
|
|
|
bool StateCleaned;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
ASTContext* getContext() const { return ValMgr.getContext(); }
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-23 19:59:44 +00:00
|
|
|
GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL),
|
|
|
|
StmtEntryNode(NULL), CurrentStmt(NULL) {}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
~GRConstants() { delete Liveness; }
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// getCFG - Returns the CFG associated with this analysis.
|
2008-01-15 23:55:06 +00:00
|
|
|
CFG& getCFG() { assert (cfg); return *cfg; }
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// Initialize - Initialize the checker's state based on the specified
|
|
|
|
/// CFG. This results in liveness information being computed for
|
|
|
|
/// each block-level statement in the CFG.
|
2008-01-24 02:02:54 +00:00
|
|
|
void Initialize(CFG& c, ASTContext& ctx) {
|
|
|
|
cfg = &c;
|
|
|
|
ValMgr.setContext(&ctx);
|
2008-01-15 23:55:06 +00:00
|
|
|
Liveness = new LiveVariables(c);
|
|
|
|
Liveness->runOnCFG(c);
|
2008-01-17 18:25:22 +00:00
|
|
|
Liveness->runOnAllBlocks(c, NULL, true);
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// getInitialState - Return the initial state used for the root vertex
|
|
|
|
/// in the ExplodedGraph.
|
2008-01-15 23:55:06 +00:00
|
|
|
StateTy getInitialState() {
|
2008-01-16 00:53:15 +00:00
|
|
|
return StateMgr.GetEmptyMap();
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
/// ProcessStmt - Called by GREngine. Used to generate new successor
|
|
|
|
/// nodes by processing the 'effects' of a block-level statement.
|
2008-01-15 23:55:06 +00:00
|
|
|
void ProcessStmt(Stmt* S, NodeBuilder& builder);
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// RemoveDeadBindings - Return a new state that is the same as 'M' except
|
|
|
|
/// that all subexpression mappings are removed and that any
|
|
|
|
/// block-level expressions that are not live at 'S' also have their
|
|
|
|
/// mappings removed.
|
|
|
|
StateTy RemoveDeadBindings(Stmt* S, StateTy M);
|
|
|
|
|
2008-01-24 19:28:01 +00:00
|
|
|
StateTy SetValue(StateTy St, Stmt* S, const ExprValue& V);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
StateTy SetValue(StateTy St, const LValue& LV, const ExprValue& V);
|
2008-01-16 19:42:59 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
ExprValue GetValue(const StateTy& St, Stmt* S);
|
|
|
|
ExprValue GetValue(const StateTy& St, const LValue& LV);
|
|
|
|
LValue GetLValue(const StateTy& St, Stmt* S);
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// Visit - Transfer function logic for all statements. Dispatches to
|
|
|
|
/// other functions that handle specific kinds of statements.
|
|
|
|
void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
|
2008-01-24 02:02:54 +00:00
|
|
|
|
|
|
|
/// VisitCast - Transfer function logic for all casts (implicit and explicit).
|
|
|
|
void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
/// VisitUnaryOperator - Transfer function logic for unary operators.
|
|
|
|
void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// VisitBinaryOperator - Transfer function logic for binary operators.
|
|
|
|
void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) {
|
|
|
|
Builder = &builder;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
StmtEntryNode = builder.getLastNode();
|
|
|
|
CurrentStmt = S;
|
|
|
|
NodeSet Dst;
|
|
|
|
StateCleaned = false;
|
|
|
|
|
|
|
|
Visit(S, StmtEntryNode, Dst);
|
|
|
|
|
|
|
|
// If no nodes were generated, generate a new node that has all the
|
|
|
|
// dead mappings removed.
|
|
|
|
if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
|
|
|
|
StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
|
|
|
|
builder.generateNode(S, St, StmtEntryNode);
|
|
|
|
}
|
2008-01-18 00:41:32 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
CurrentStmt = NULL;
|
|
|
|
StmtEntryNode = NULL;
|
|
|
|
Builder = NULL;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 22:28:08 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
ExprValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
|
|
|
|
switch (LV.getKind()) {
|
|
|
|
case ExprValue::LValueDeclKind: {
|
|
|
|
StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
|
|
|
|
return T ? T->getValue().second : InvalidValue();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid LValue.");
|
2008-01-16 22:28:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
return InvalidValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
|
2008-01-24 00:50:08 +00:00
|
|
|
for (;;) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Stmt::ParenExprClass:
|
|
|
|
S = cast<ParenExpr>(S)->getSubExpr();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
|
2008-01-16 19:47:19 +00:00
|
|
|
|
2008-01-24 00:50:08 +00:00
|
|
|
case Stmt::IntegerLiteralClass:
|
2008-01-24 08:20:02 +00:00
|
|
|
return RValue::GetRValue(ValMgr, cast<IntegerLiteral>(S));
|
2008-01-24 02:02:54 +00:00
|
|
|
|
|
|
|
case Stmt::ImplicitCastExprClass: {
|
|
|
|
ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
|
|
|
|
if (C->getType() == C->getSubExpr()->getType()) {
|
|
|
|
S = C->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::CastExprClass: {
|
|
|
|
CastExpr* C = cast<CastExpr>(S);
|
|
|
|
if (C->getType() == C->getSubExpr()->getType()) {
|
|
|
|
S = C->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 00:50:08 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 19:43:37 +00:00
|
|
|
StateTy::TreeTy* T = St.SlimFind(S);
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
return T ? T->getValue().second : InvalidValue();
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
|
|
|
|
if (Expr* E = dyn_cast<Expr>(S))
|
|
|
|
S = E->IgnoreParens();
|
|
|
|
|
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
|
|
|
|
return LValueDecl(DR->getDecl());
|
|
|
|
|
|
|
|
return cast<LValue>(GetValue(St, S));
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
|
2008-01-24 19:28:01 +00:00
|
|
|
const ExprValue& V) {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
|
|
|
|
2008-01-24 19:28:01 +00:00
|
|
|
bool isBlkExpr = S == CurrentStmt && getCFG().isBlkExpr(S);
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
|
|
|
|
: St;
|
2008-01-16 19:42:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
|
|
|
|
const ExprValue& V) {
|
|
|
|
if (!LV.isValid())
|
|
|
|
return St;
|
|
|
|
|
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (LV.getKind()) {
|
|
|
|
case ExprValue::LValueDeclKind:
|
|
|
|
return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
|
|
|
|
: StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert ("SetValue for given LValue type not yet implemented.");
|
|
|
|
return St;
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
|
2008-01-18 00:41:32 +00:00
|
|
|
// Note: in the code below, we can assign a new map to M since the
|
|
|
|
// iterators are iterating over the tree of the *original* map.
|
|
|
|
StateTy::iterator I = M.begin(), E = M.end();
|
|
|
|
|
2008-01-24 19:43:37 +00:00
|
|
|
// Remove old bindings for subexpressions and "dead" block-level expressions.
|
|
|
|
for (; I!=E && !I.getKey().isDecl(); ++I) {
|
|
|
|
if (I.getKey().isSubExpr() || !Liveness->isLive(Loc,cast<Stmt>(I.getKey())))
|
|
|
|
M = StateMgr.Remove(M, I.getKey());
|
|
|
|
}
|
2008-01-18 00:41:32 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// Remove bindings for "dead" decls.
|
2008-01-24 19:43:37 +00:00
|
|
|
for (; I!=E ; ++I) {
|
|
|
|
assert (I.getKey().isDecl());
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
|
|
|
|
if (!Liveness->isLive(Loc, V))
|
|
|
|
M = StateMgr.Remove(M, I.getKey());
|
2008-01-24 19:43:37 +00:00
|
|
|
}
|
2008-01-17 00:52:48 +00:00
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::StateTy St) {
|
|
|
|
|
|
|
|
// If the state hasn't changed, don't generate a new node.
|
|
|
|
if (St == Pred->getState())
|
|
|
|
return;
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
Dst.Add(Builder->generateNode(S, St, Pred));
|
2008-01-16 00:53:15 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::NodeSet& Dst) {
|
|
|
|
|
|
|
|
QualType T = CastE->getType();
|
|
|
|
|
|
|
|
// Check for redundant casts.
|
|
|
|
if (E->getType() == T) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeSet S1;
|
|
|
|
Visit(E, Pred, S1);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
|
|
|
|
NodeTy* N = *I1;
|
|
|
|
StateTy St = N->getState();
|
|
|
|
const ExprValue& V = GetValue(St, E);
|
|
|
|
Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
void GRConstants::VisitUnaryOperator(UnaryOperator* U,
|
|
|
|
GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::NodeSet& Dst) {
|
|
|
|
NodeSet S1;
|
|
|
|
Visit(U->getSubExpr(), Pred, S1);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
|
|
|
|
NodeTy* N1 = *I1;
|
|
|
|
StateTy St = N1->getState();
|
|
|
|
|
|
|
|
switch (U->getOpcode()) {
|
|
|
|
case UnaryOperator::PostInc: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
|
|
|
RValue R1 = cast<RValue>(GetValue(St, L1));
|
|
|
|
|
|
|
|
QualType T = U->getType();
|
2008-01-24 19:00:57 +00:00
|
|
|
unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
|
|
|
|
llvm::APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
|
|
|
|
RValue R2 = RValue::GetRValue(ValMgr, One);
|
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
RValue Result = R1.EvalAdd(ValMgr, R2);
|
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PostDec: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
|
|
|
RValue R1 = cast<RValue>(GetValue(St, L1));
|
|
|
|
|
|
|
|
QualType T = U->getType();
|
2008-01-24 19:00:57 +00:00
|
|
|
unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
|
|
|
|
llvm::APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
|
|
|
|
RValue R2 = RValue::GetRValue(ValMgr, One);
|
2008-01-24 02:28:56 +00:00
|
|
|
|
|
|
|
RValue Result = R1.EvalSub(ValMgr, R2);
|
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PreInc: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
|
|
|
RValue R1 = cast<RValue>(GetValue(St, L1));
|
|
|
|
|
|
|
|
QualType T = U->getType();
|
2008-01-24 19:00:57 +00:00
|
|
|
unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
|
|
|
|
llvm::APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
|
2008-01-24 02:28:56 +00:00
|
|
|
RValue R2 = RValue::GetRValue(ValMgr, One);
|
|
|
|
|
|
|
|
RValue Result = R1.EvalAdd(ValMgr, R2);
|
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PreDec: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
|
|
|
RValue R1 = cast<RValue>(GetValue(St, L1));
|
|
|
|
|
|
|
|
QualType T = U->getType();
|
2008-01-24 19:00:57 +00:00
|
|
|
unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
|
|
|
|
llvm::APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
|
|
|
|
RValue R2 = RValue::GetRValue(ValMgr, One);
|
2008-01-24 02:28:56 +00:00
|
|
|
|
|
|
|
RValue Result = R1.EvalSub(ValMgr, R2);
|
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-24 08:20:02 +00:00
|
|
|
case UnaryOperator::Minus: {
|
|
|
|
const RValue& R1 = cast<RValue>(GetValue(St, U->getSubExpr()));
|
|
|
|
Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
default: ;
|
|
|
|
assert (false && "Not implemented.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void GRConstants::VisitBinaryOperator(BinaryOperator* B,
|
|
|
|
GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::NodeSet& Dst) {
|
|
|
|
NodeSet S1;
|
|
|
|
Visit(B->getLHS(), Pred, S1);
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
|
|
|
|
NodeTy* N1 = *I1;
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// When getting the value for the LHS, check if we are in an assignment.
|
|
|
|
// In such cases, we want to (initially) treat the LHS as an LValue,
|
|
|
|
// so we use GetLValue instead of GetValue so that DeclRefExpr's are
|
|
|
|
// evaluated to LValueDecl's instead of to an RValue.
|
|
|
|
const ExprValue& V1 =
|
|
|
|
B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
|
|
|
|
: GetValue(N1->getState(), B->getLHS());
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
NodeSet S2;
|
|
|
|
Visit(B->getRHS(), N1, S2);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
|
|
|
|
NodeTy* N2 = *I2;
|
|
|
|
StateTy St = N2->getState();
|
|
|
|
const ExprValue& V2 = GetValue(St, B->getRHS());
|
|
|
|
|
|
|
|
switch (B->getOpcode()) {
|
|
|
|
case BinaryOperator::Add: {
|
|
|
|
const RValue& R1 = cast<RValue>(V1);
|
|
|
|
const RValue& R2 = cast<RValue>(V2);
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.EvalAdd(ValMgr, R2)));
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BinaryOperator::Sub: {
|
|
|
|
const RValue& R1 = cast<RValue>(V1);
|
|
|
|
const RValue& R2 = cast<RValue>(V2);
|
2008-01-24 02:02:54 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.EvalSub(ValMgr, R2)));
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 23:42:27 +00:00
|
|
|
case BinaryOperator::Mul: {
|
|
|
|
const RValue& R1 = cast<RValue>(V1);
|
|
|
|
const RValue& R2 = cast<RValue>(V2);
|
2008-01-24 02:02:54 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.EvalMul(ValMgr, R2)));
|
2008-01-23 23:42:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
case BinaryOperator::Assign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
const RValue& R2 = cast<RValue>(V2);
|
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-23 23:38:00 +00:00
|
|
|
|
|
|
|
case BinaryOperator::AddAssign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
RValue R1 = cast<RValue>(GetValue(N1->getState(), L1));
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue Result = R1.EvalAdd(ValMgr, cast<RValue>(V2));
|
2008-01-23 23:38:00 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BinaryOperator::SubAssign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
RValue R1 = cast<RValue>(GetValue(N1->getState(), L1));
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue Result = R1.EvalSub(ValMgr, cast<RValue>(V2));
|
2008-01-23 23:38:00 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-23 23:42:27 +00:00
|
|
|
|
|
|
|
case BinaryOperator::MulAssign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
RValue R1 = cast<RValue>(GetValue(N1->getState(), L1));
|
2008-01-24 02:02:54 +00:00
|
|
|
RValue Result = R1.EvalMul(ValMgr, cast<RValue>(V2));
|
2008-01-23 23:42:27 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
Dst.Add(N2);
|
|
|
|
break;
|
|
|
|
}
|
2008-01-16 00:53:15 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::NodeSet& Dst) {
|
2008-01-16 18:18:48 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// FIXME: add metadata to the CFG so that we can disable
|
|
|
|
// this check when we KNOW that there is no block-level subexpression.
|
|
|
|
// The motivation is that this check requires a hashtable lookup.
|
2008-01-16 19:42:59 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Stmt::BinaryOperatorClass:
|
2008-01-23 23:38:00 +00:00
|
|
|
case Stmt::CompoundAssignOperatorClass:
|
2008-01-23 19:59:44 +00:00
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
case Stmt::UnaryOperatorClass:
|
|
|
|
VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
case Stmt::ParenExprClass:
|
|
|
|
Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
case Stmt::ImplicitCastExprClass: {
|
|
|
|
ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
|
|
|
|
VisitCast(C, C->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::CastExprClass: {
|
|
|
|
CastExpr* C = cast<CastExpr>(S);
|
|
|
|
VisitCast(C, C->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
default:
|
|
|
|
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
|
|
|
|
break;
|
2008-01-17 18:25:22 +00:00
|
|
|
}
|
2008-01-16 19:42:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 18:18:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Driver.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
|
|
|
|
public DefaultDOTGraphTraits {
|
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
static void PrintKind(std::ostringstream& Out, ValueKey::Kind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case ValueKey::IsSubExp: Out << "Sub-Expressions:\\l"; break;
|
|
|
|
case ValueKey::IsDecl: Out << "Variables:\\l"; break;
|
|
|
|
case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
|
|
|
|
default: assert (false && "Unknown ValueKey type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
|
|
|
|
std::ostringstream Out;
|
2008-01-23 22:30:44 +00:00
|
|
|
|
|
|
|
// Program Location.
|
2008-01-16 21:46:15 +00:00
|
|
|
ProgramPoint Loc = N->getLocation();
|
|
|
|
|
|
|
|
switch (Loc.getKind()) {
|
|
|
|
case ProgramPoint::BlockEntranceKind:
|
|
|
|
Out << "Block Entrance: B"
|
|
|
|
<< cast<BlockEntrance>(Loc).getBlock()->getBlockID();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProgramPoint::BlockExitKind:
|
|
|
|
assert (false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProgramPoint::PostStmtKind: {
|
|
|
|
const PostStmt& L = cast<PostStmt>(Loc);
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << "(" << (void*) L.getStmt() << ") ";
|
2008-01-16 21:46:15 +00:00
|
|
|
L.getStmt()->printPretty(Out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
const BlockEdge& E = cast<BlockEdge>(Loc);
|
|
|
|
Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
|
|
|
|
<< E.getDst()->getBlockID() << ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << "\\|";
|
2008-01-16 21:46:15 +00:00
|
|
|
|
|
|
|
GRConstants::StateTy M = N->getState();
|
|
|
|
bool isFirst = true;
|
2008-01-23 22:30:44 +00:00
|
|
|
ValueKey::Kind kind;
|
2008-01-16 21:46:15 +00:00
|
|
|
|
|
|
|
for (GRConstants::StateTy::iterator I=M.begin(), E=M.end(); I!=E; ++I) {
|
2008-01-23 22:30:44 +00:00
|
|
|
if (!isFirst) {
|
|
|
|
ValueKey::Kind newKind = I.getKey().getKind();
|
|
|
|
|
|
|
|
if (newKind != kind) {
|
|
|
|
Out << "\\l\\l";
|
|
|
|
PrintKind(Out, newKind);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out << "\\l";
|
|
|
|
|
|
|
|
kind = newKind;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kind = I.getKey().getKind();
|
|
|
|
PrintKind(Out, kind);
|
2008-01-16 21:46:15 +00:00
|
|
|
isFirst = false;
|
2008-01-23 22:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Out << ' ';
|
2008-01-16 21:46:15 +00:00
|
|
|
|
|
|
|
if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) {
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << V->getName();
|
2008-01-16 21:46:15 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Stmt* E = cast<Stmt>(I.getKey());
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << " (" << (void*) E << ") ";
|
|
|
|
E->printPretty(Out);
|
2008-01-16 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << " : ";
|
2008-01-23 19:59:44 +00:00
|
|
|
I.getData().print(Out);
|
2008-01-16 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << "\\l";
|
2008-01-16 21:46:15 +00:00
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
#endif
|
|
|
|
|
2008-01-16 18:18:48 +00:00
|
|
|
namespace clang {
|
2008-01-24 02:02:54 +00:00
|
|
|
void RunGRConstants(CFG& cfg, ASTContext& Ctx) {
|
|
|
|
GREngine<GRConstants> Engine(cfg, Ctx);
|
2008-01-16 18:18:48 +00:00
|
|
|
Engine.ExecuteWorkList();
|
2008-01-16 21:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
|
|
|
|
#endif
|
2008-01-16 18:18:48 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
} // end clang namespace
|