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-30 23:03:39 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.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-25 22:55:56 +00:00
|
|
|
#include <functional>
|
|
|
|
|
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-25 22:55:56 +00:00
|
|
|
using llvm::APSInt;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
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-28 22:09:13 +00:00
|
|
|
|
2008-01-29 17:27:31 +00:00
|
|
|
class SymbolID {
|
|
|
|
unsigned Data;
|
|
|
|
public:
|
|
|
|
SymbolID() : Data(~0) {}
|
|
|
|
SymbolID(unsigned x) : Data(x) {}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-29 17:27:31 +00:00
|
|
|
bool isInitialized() const { return Data != (unsigned) ~0; }
|
|
|
|
operator unsigned() const { assert (isInitialized()); return Data; }
|
|
|
|
};
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
class VISIBILITY_HIDDEN ValueKey {
|
2008-01-28 22:09:13 +00:00
|
|
|
uintptr_t Raw;
|
2008-01-25 23:43:12 +00:00
|
|
|
void operator=(const ValueKey& RHS); // Do not implement.
|
2008-01-28 22:09:13 +00:00
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-28 22:09:13 +00:00
|
|
|
enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
|
|
|
|
IsSymbol=0x3, // Symbol Bindings.
|
|
|
|
Flags=0x3 };
|
|
|
|
|
|
|
|
inline Kind getKind() const {
|
|
|
|
return (Kind) (Raw & Flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* getPtr() const {
|
|
|
|
assert (getKind() != IsSymbol);
|
|
|
|
return reinterpret_cast<void*>(Raw & ~Flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SymbolID getSymbolID() const {
|
|
|
|
assert (getKind() == IsSymbol);
|
2008-01-29 17:27:31 +00:00
|
|
|
return Raw >> 2;
|
2008-01-28 22:09:13 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
ValueKey(const ValueDecl* VD)
|
2008-01-28 22:09:13 +00:00
|
|
|
: Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
|
|
|
|
assert(VD && "ValueDecl cannot be NULL.");
|
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-24 19:43:37 +00:00
|
|
|
ValueKey(Stmt* S, bool isBlkExpr = false)
|
2008-01-25 23:43:12 +00:00
|
|
|
: Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
|
2008-01-28 22:09:13 +00:00
|
|
|
assert(S && "Tracked statement cannot be NULL.");
|
2008-01-25 23:43:12 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ValueKey(SymbolID V)
|
|
|
|
: Raw((V << 2) | IsSymbol) {}
|
|
|
|
|
|
|
|
bool isSymbol() const { return getKind() == IsSymbol; }
|
2008-01-24 22:44:24 +00:00
|
|
|
bool isSubExpr() const { return getKind() == IsSubExpr; }
|
2008-01-29 05:25:31 +00:00
|
|
|
bool isBlkExpr() const { return getKind() == IsBlkExpr; }
|
2008-01-28 22:09:13 +00:00
|
|
|
bool isDecl() const { return getKind() == IsDecl; }
|
2008-01-29 05:25:31 +00:00
|
|
|
bool isStmt() const { return getKind() <= IsBlkExpr; }
|
2008-01-15 23:55:06 +00:00
|
|
|
|
|
|
|
inline void Profile(llvm::FoldingSetNodeID& ID) const {
|
2008-01-28 22:09:13 +00:00
|
|
|
ID.AddInteger(isSymbol() ? 1 : 0);
|
|
|
|
|
|
|
|
if (isSymbol())
|
2008-01-28 22:25:21 +00:00
|
|
|
ID.AddInteger(getSymbolID());
|
2008-01-28 22:09:13 +00:00
|
|
|
else
|
|
|
|
ID.AddPointer(getPtr());
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const ValueKey& X) const {
|
2008-01-28 22:09:13 +00:00
|
|
|
return isSymbol() ? getSymbolID() == X.getSymbolID()
|
|
|
|
: 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 {
|
2008-01-28 22:09:13 +00:00
|
|
|
if (isSymbol())
|
|
|
|
return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
|
2008-01-24 19:43:37 +00:00
|
|
|
|
2008-01-28 22:09:13 +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) {
|
2008-01-28 22:09:13 +00:00
|
|
|
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-29 17:27:31 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolManager.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN SymbolData {
|
|
|
|
uintptr_t Data;
|
|
|
|
public:
|
|
|
|
enum Kind { ParmKind = 0x0, Mask = 0x3 };
|
|
|
|
|
|
|
|
SymbolData(ParmVarDecl* D)
|
|
|
|
: Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
|
|
|
|
|
|
|
|
inline Kind getKind() const { return (Kind) (Data & Mask); }
|
|
|
|
inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
|
|
|
|
inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Machinery to get cast<> and dyn_cast<> working with SymbolData.
|
|
|
|
namespace llvm {
|
|
|
|
template<> inline bool isa<ParmVarDecl,SymbolData>(const SymbolData& V) {
|
|
|
|
return V.getKind() == SymbolData::ParmKind;
|
|
|
|
}
|
|
|
|
template<> struct VISIBILITY_HIDDEN cast_retty_impl<ParmVarDecl,SymbolData> {
|
|
|
|
typedef const ParmVarDecl* ret_type;
|
|
|
|
};
|
|
|
|
template<> struct VISIBILITY_HIDDEN simplify_type<SymbolData> {
|
|
|
|
typedef void* SimpleType;
|
|
|
|
static inline SimpleType getSimplifiedValue(const SymbolData &V) {
|
|
|
|
return V.getPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN SymbolManager {
|
|
|
|
std::vector<SymbolData> SymbolToData;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<void*,SymbolID> MapTy;
|
|
|
|
MapTy DataToSymbol;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SymbolData getSymbolData(SymbolID id) const {
|
|
|
|
assert (id < SymbolToData.size());
|
|
|
|
return SymbolToData[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolID getSymbol(ParmVarDecl* D);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
SymbolID SymbolManager::getSymbol(ParmVarDecl* D) {
|
|
|
|
SymbolID& X = DataToSymbol[D];
|
|
|
|
|
|
|
|
if (!X.isInitialized()) {
|
|
|
|
X = SymbolToData.size();
|
|
|
|
SymbolToData.push_back(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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 {
|
|
|
|
|
2008-01-25 22:55:56 +00:00
|
|
|
typedef llvm::ImmutableSet<APSInt > APSIntSetTy;
|
|
|
|
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
class VISIBILITY_HIDDEN ValueManager {
|
2008-01-29 00:33:40 +00:00
|
|
|
ASTContext& Ctx;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy;
|
|
|
|
APSIntSetTy APSIntSet;
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator BPAlloc;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
public:
|
2008-01-29 00:33:40 +00:00
|
|
|
ValueManager(ASTContext& ctx) : Ctx(ctx) {}
|
2008-01-28 22:09:13 +00:00
|
|
|
~ValueManager();
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-29 00:33:40 +00:00
|
|
|
ASTContext& getContext() const { return Ctx; }
|
2008-01-29 19:43:15 +00:00
|
|
|
APSInt& getValue(const APSInt& X);
|
|
|
|
APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
|
|
|
|
APSInt& getValue(uint64_t X, QualType T,
|
|
|
|
SourceLocation Loc = SourceLocation());
|
2008-01-23 19:59:44 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ValueManager::~ValueManager() {
|
|
|
|
// Note that the dstor for the contents of APSIntSet will never be called,
|
|
|
|
// so we iterate over the set and invoke the dstor for each APSInt. This
|
|
|
|
// frees an aux. memory allocated to represent very large constants.
|
|
|
|
for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
|
|
|
|
I->getValue().~APSInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
APSInt& ValueManager::getValue(const APSInt& X) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
void* InsertPos;
|
|
|
|
typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
|
2008-01-25 23:43:12 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
X.Profile(ID);
|
|
|
|
FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
if (!P) {
|
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
|
|
|
new (P) FoldNodeTy(X);
|
|
|
|
APSIntSet.InsertNode(P, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *P;
|
2008-01-25 22:55:56 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) {
|
|
|
|
APSInt V(BitWidth, isUnsigned);
|
|
|
|
V = X;
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) {
|
|
|
|
unsigned bits = Ctx.getTypeSize(T, Loc);
|
|
|
|
APSInt V(bits, T->isUnsignedIntegerType());
|
|
|
|
V = X;
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Expression Values.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-24 23:19:54 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
namespace {
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
class VISIBILITY_HIDDEN RValue {
|
2008-01-23 19:59:44 +00:00
|
|
|
public:
|
2008-01-28 22:51:57 +00:00
|
|
|
enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
|
2008-01-30 18:54:06 +00:00
|
|
|
UninitializedKind=0x2, InvalidKind=0x3 };
|
|
|
|
|
|
|
|
enum { BaseBits = 2, BaseMask = 0x3 };
|
2008-01-24 23:19:54 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
private:
|
2008-01-28 22:25:21 +00:00
|
|
|
void* Data;
|
2008-01-24 23:19:54 +00:00
|
|
|
unsigned Kind;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
protected:
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue(const void* d, bool isLValue, unsigned ValKind)
|
2008-01-28 22:25:21 +00:00
|
|
|
: Data(const_cast<void*>(d)),
|
2008-01-30 18:54:06 +00:00
|
|
|
Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:51:57 +00:00
|
|
|
explicit RValue(BaseKind k)
|
|
|
|
: Data(0), Kind(k) {}
|
2008-01-28 22:25:21 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
void* getRawPtr() const {
|
|
|
|
return reinterpret_cast<void*>(Data);
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
public:
|
2008-01-28 22:09:13 +00:00
|
|
|
~RValue() {};
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-01-24 23:19:54 +00:00
|
|
|
unsigned getRawKind() const { return Kind; }
|
2008-01-30 18:54:06 +00:00
|
|
|
BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
|
|
|
|
unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
|
2008-01-24 23:19:54 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
ID.AddInteger((unsigned) getRawKind());
|
2008-01-28 22:09:13 +00:00
|
|
|
ID.AddPointer(reinterpret_cast<void*>(Data));
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
bool operator==(const RValue& RHS) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-24 23:19:54 +00:00
|
|
|
inline bool isValid() const { return getRawKind() != InvalidKind; }
|
|
|
|
inline bool isInvalid() const { return getRawKind() == InvalidKind; }
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
void print(std::ostream& OS) const;
|
|
|
|
void print() const { print(*llvm::cerr.stream()); }
|
|
|
|
|
|
|
|
// Implement isa<T> support.
|
2008-01-28 22:09:13 +00:00
|
|
|
static inline bool classof(const RValue*) { return true; }
|
2008-01-23 19:59:44 +00:00
|
|
|
};
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
class VISIBILITY_HIDDEN InvalidValue : public RValue {
|
2008-01-23 19:59:44 +00:00
|
|
|
public:
|
2008-01-28 22:51:57 +00:00
|
|
|
InvalidValue() : RValue(InvalidKind) {}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
static inline bool classof(const RValue* V) {
|
2008-01-24 23:19:54 +00:00
|
|
|
return V->getBaseKind() == InvalidKind;
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
};
|
2008-01-28 22:51:57 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN UninitializedValue : public RValue {
|
|
|
|
public:
|
|
|
|
UninitializedValue() : RValue(UninitializedKind) {}
|
|
|
|
|
|
|
|
static inline bool classof(const RValue* V) {
|
|
|
|
return V->getBaseKind() == UninitializedKind;
|
|
|
|
}
|
|
|
|
};
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
class VISIBILITY_HIDDEN NonLValue : public RValue {
|
2008-01-23 19:59:44 +00:00
|
|
|
protected:
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-24 23:19:54 +00:00
|
|
|
void print(std::ostream& Out) const;
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Arithmetic operators.
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Equality operators.
|
|
|
|
NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
|
|
|
|
NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
|
2008-01-29 17:27:31 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Utility methods to create NonLValues.
|
|
|
|
static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
|
|
|
|
SourceLocation Loc = SourceLocation());
|
|
|
|
|
|
|
|
static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
|
2008-01-29 17:27:31 +00:00
|
|
|
static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
|
|
|
|
return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// Implement isa<T> support.
|
2008-01-28 22:09:13 +00:00
|
|
|
static inline bool classof(const RValue* V) {
|
2008-01-28 22:51:57 +00:00
|
|
|
return V->getBaseKind() >= NonLValueKind;
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
};
|
2008-01-29 19:43:15 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN LValue : public RValue {
|
|
|
|
protected:
|
|
|
|
LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// Equality operators.
|
|
|
|
NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
|
|
|
|
NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
|
|
|
|
|
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const RValue* V) {
|
|
|
|
return V->getBaseKind() == LValueKind;
|
|
|
|
}
|
|
|
|
};
|
2008-01-24 23:19:54 +00:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-28 22:09:13 +00:00
|
|
|
// LValues.
|
2008-01-24 23:19:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
enum { LValueDeclKind, NumLValueKind };
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN LValueDecl : public LValue {
|
2008-01-23 19:59:44 +00:00
|
|
|
public:
|
2008-01-28 22:09:13 +00:00
|
|
|
LValueDecl(const ValueDecl* vd)
|
|
|
|
: LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ValueDecl* getDecl() const {
|
|
|
|
return static_cast<ValueDecl*>(getRawPtr());
|
2008-01-25 22:55:56 +00:00
|
|
|
}
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
inline bool operator==(const LValueDecl& R) const {
|
|
|
|
return getDecl() == R.getDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const LValueDecl& R) const {
|
|
|
|
return getDecl() != R.getDecl();
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
// Implement isa<T> support.
|
|
|
|
static inline bool classof(const RValue* V) {
|
|
|
|
return V->getSubKind() == LValueDeclKind;
|
2008-01-25 22:55:56 +00:00
|
|
|
}
|
2008-01-28 22:09:13 +00:00
|
|
|
};
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
} // end anonymous namespace
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Non-LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:25:21 +00:00
|
|
|
enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
|
|
|
|
NumNonLValueKind };
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
|
|
|
|
public:
|
|
|
|
SymbolicNonLValue(unsigned SymID)
|
|
|
|
: NonLValue(SymbolicNonLValueKind,
|
|
|
|
reinterpret_cast<void*>((uintptr_t) SymID)) {}
|
|
|
|
|
|
|
|
SymbolID getSymbolID() const {
|
|
|
|
return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool classof(const RValue* V) {
|
|
|
|
return V->getSubKind() == SymbolicNonLValueKind;
|
|
|
|
}
|
|
|
|
};
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
|
2008-01-25 22:55:56 +00:00
|
|
|
public:
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
const APSInt& getValue() const {
|
|
|
|
return *static_cast<APSInt*>(getRawPtr());
|
|
|
|
}
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Arithmetic operators.
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
return ValMgr.getValue(getValue() + V.getValue());
|
|
|
|
}
|
2008-01-29 19:43:15 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
return ValMgr.getValue(getValue() - V.getValue());
|
2008-01-25 22:55:56 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
return ValMgr.getValue(getValue() * V.getValue());
|
|
|
|
}
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
return ValMgr.getValue(getValue() / V.getValue());
|
|
|
|
}
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
return ValMgr.getValue(getValue() % V.getValue());
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
|
|
|
|
assert (U->getType() == U->getSubExpr()->getType());
|
|
|
|
assert (U->getType()->isIntegerType());
|
|
|
|
return ValMgr.getValue(-getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Casting.
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
|
|
|
|
assert (CastExpr->getType()->isIntegerType());
|
|
|
|
|
|
|
|
APSInt X(getValue());
|
2008-01-29 00:33:40 +00:00
|
|
|
X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
|
|
|
|
CastExpr->getLocStart()));
|
2008-01-28 22:09:13 +00:00
|
|
|
return ValMgr.getValue(X);
|
|
|
|
}
|
2008-01-24 08:20:02 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Equality operators.
|
|
|
|
|
|
|
|
ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
const APSInt& Val = getValue();
|
|
|
|
return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
|
|
|
|
Val.getBitWidth(), Val.isUnsigned());
|
|
|
|
}
|
|
|
|
|
|
|
|
ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
|
|
|
|
const APSInt& Val = getValue();
|
|
|
|
return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
|
|
|
|
Val.getBitWidth(), Val.isUnsigned());
|
2008-01-28 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// Implement isa<T> support.
|
2008-01-28 22:09:13 +00:00
|
|
|
static inline bool classof(const RValue* V) {
|
|
|
|
return V->getSubKind() == ConcreteIntKind;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
};
|
2008-01-25 22:55:56 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-29 19:43:15 +00:00
|
|
|
// Transfer function dispatch for Non-LValues.
|
2008-01-24 02:02:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (getSubKind()) {
|
2008-01-28 22:09:13 +00:00
|
|
|
case ConcreteIntKind:
|
|
|
|
return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
|
2008-01-24 02:02:54 +00:00
|
|
|
default:
|
|
|
|
return InvalidValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (getSubKind()) {
|
2008-01-28 22:09:13 +00:00
|
|
|
case ConcreteIntKind:
|
|
|
|
return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
|
2008-01-24 08:20:02 +00:00
|
|
|
default:
|
2008-01-28 22:09:13 +00:00
|
|
|
return cast<NonLValue>(InvalidValue());
|
2008-01-24 08:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
|
2008-01-28 22:09:13 +00:00
|
|
|
case (k1##Kind*NumNonLValueKind+k2##Kind):\
|
|
|
|
return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
#define NONLVALUE_DISPATCH(Op)\
|
2008-01-28 22:09:13 +00:00
|
|
|
switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
|
2008-01-23 19:59:44 +00:00
|
|
|
default:\
|
2008-01-28 22:51:57 +00:00
|
|
|
if (getBaseKind() == UninitializedKind ||\
|
|
|
|
RHS.getBaseKind() == UninitializedKind)\
|
|
|
|
return cast<NonLValue>(UninitializedValue());\
|
2008-01-23 19:59:44 +00:00
|
|
|
assert (!isValid() || !RHS.isValid() && "Missing case.");\
|
|
|
|
break;\
|
|
|
|
}\
|
2008-01-28 22:09:13 +00:00
|
|
|
return cast<NonLValue>(InvalidValue());
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH(Add)
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH(Sub)
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH(Mul)
|
2008-01-23 23:42:27 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH(Div)
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
|
2008-01-29 19:43:15 +00:00
|
|
|
NONLVALUE_DISPATCH(Rem)
|
|
|
|
}
|
|
|
|
|
|
|
|
NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
|
|
|
|
NONLVALUE_DISPATCH(EQ)
|
2008-01-24 08:20:02 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
|
|
|
|
NONLVALUE_DISPATCH(NE)
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NONLVALUE_DISPATCH_CASE
|
|
|
|
#undef NONLVALUE_DISPATCH
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function dispatch for LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
|
|
|
|
if (getSubKind() != RHS.getSubKind())
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, false);
|
|
|
|
|
|
|
|
switch (getSubKind()) {
|
|
|
|
default:
|
|
|
|
assert(false && "EQ not implemented for this LValue.");
|
|
|
|
return cast<NonLValue>(InvalidValue());
|
|
|
|
|
|
|
|
case LValueDeclKind: {
|
|
|
|
bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS);
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
|
|
|
|
if (getSubKind() != RHS.getSubKind())
|
2008-01-29 21:27:49 +00:00
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, true);
|
2008-01-29 19:43:15 +00:00
|
|
|
|
|
|
|
switch (getSubKind()) {
|
|
|
|
default:
|
|
|
|
assert(false && "EQ not implemented for this LValue.");
|
|
|
|
return cast<NonLValue>(InvalidValue());
|
|
|
|
|
|
|
|
case LValueDeclKind: {
|
|
|
|
bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-28 22:09:13 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-29 19:43:15 +00:00
|
|
|
// Utility methods for constructing Non-LValues.
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
|
|
|
return ConcreteInt(ValMgr.getValue(X, T, Loc));
|
2008-01-28 22:09:13 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
|
|
|
|
return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
|
|
|
|
I->getType()->isUnsignedIntegerType())));
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 17:27:31 +00:00
|
|
|
NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
|
|
|
|
return SymbolicNonLValue(SymMgr.getSymbol(D));
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pretty-Printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
void RValue::print(std::ostream& Out) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (getBaseKind()) {
|
|
|
|
case InvalidKind:
|
|
|
|
Out << "Invalid";
|
|
|
|
break;
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
case NonLValueKind:
|
|
|
|
cast<NonLValue>(this)->print(Out);
|
2008-01-24 23:19:54 +00:00
|
|
|
break;
|
2008-01-29 17:27:31 +00:00
|
|
|
|
2008-01-24 23:19:54 +00:00
|
|
|
case LValueKind:
|
|
|
|
assert (false && "FIXME: LValue printing not implemented.");
|
|
|
|
break;
|
|
|
|
|
2008-01-28 22:51:57 +00:00
|
|
|
case UninitializedKind:
|
|
|
|
Out << "Uninitialized";
|
|
|
|
break;
|
|
|
|
|
2008-01-24 23:19:54 +00:00
|
|
|
default:
|
2008-01-28 22:09:13 +00:00
|
|
|
assert (false && "Invalid RValue.");
|
2008-01-24 23:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
void NonLValue::print(std::ostream& Out) const {
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (getSubKind()) {
|
2008-01-28 22:09:13 +00:00
|
|
|
case ConcreteIntKind:
|
|
|
|
Out << cast<ConcreteInt>(this)->getValue().toString();
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
|
2008-01-29 17:27:31 +00:00
|
|
|
case SymbolicNonLValueKind:
|
2008-01-30 18:54:06 +00:00
|
|
|
Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID();
|
2008-01-29 17:27:31 +00:00
|
|
|
break;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
default:
|
2008-01-28 22:09:13 +00:00
|
|
|
assert (false && "Pretty-printed not implemented for this NonLValue.");
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-28 22:09:13 +00:00
|
|
|
// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
|
2008-01-23 19:59:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
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-30 23:03:39 +00:00
|
|
|
typedef ValueMapTy StateTy;
|
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-28 22:09:13 +00:00
|
|
|
// The Checker.
|
2008-01-30 23:03:39 +00:00
|
|
|
//
|
|
|
|
// FIXME: This checker logic should be eventually broken into two components.
|
|
|
|
// The first is the "meta"-level checking logic; the code that
|
|
|
|
// does the Stmt visitation, fetching values from the map, etc.
|
|
|
|
// The second part does the actual state manipulation. This way we
|
|
|
|
// get more of a separate of concerns of these two pieces, with the
|
|
|
|
// latter potentially being refactored back into the main checking
|
|
|
|
// logic.
|
2008-01-15 23:55:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
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-29 22:56:11 +00:00
|
|
|
typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
|
|
|
|
typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
|
2008-01-29 00:33:40 +00:00
|
|
|
typedef ExplodedGraph<GRConstants> GraphTy;
|
|
|
|
typedef GraphTy::NodeTy NodeTy;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
class NodeSet {
|
|
|
|
typedef llvm::SmallVector<NodeTy*,3> ImplTy;
|
|
|
|
ImplTy Impl;
|
|
|
|
public:
|
|
|
|
|
|
|
|
NodeSet() {}
|
2008-01-30 23:03:39 +00:00
|
|
|
NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-30 23:03:39 +00:00
|
|
|
void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
typedef ImplTy::iterator iterator;
|
|
|
|
typedef ImplTy::const_iterator const_iterator;
|
|
|
|
|
|
|
|
unsigned size() const { return Impl.size(); }
|
2008-01-24 20:55:43 +00:00
|
|
|
bool empty() const { return Impl.empty(); }
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
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-29 00:33:40 +00:00
|
|
|
/// G - the simulation graph.
|
|
|
|
GraphTy& G;
|
|
|
|
|
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-29 05:13:23 +00:00
|
|
|
LiveVariables Liveness;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 22:11:49 +00:00
|
|
|
/// Builder - The current GRStmtNodeBuilder which is used when building the nodes
|
2008-01-23 19:59:44 +00:00
|
|
|
/// for a given statement.
|
2008-01-29 22:56:11 +00:00
|
|
|
StmtNodeBuilder* Builder;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// StateMgr - Object that manages the data for all created states.
|
|
|
|
ValueMapTy::Factory StateMgr;
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
/// ValueMgr - Object that manages the data for all created RValues.
|
2008-01-23 19:59:44 +00:00
|
|
|
ValueManager ValMgr;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 17:27:31 +00:00
|
|
|
/// SymMgr - Object that manages the symbol information.
|
|
|
|
SymbolManager SymMgr;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
/// StmtEntryNode - The immediate predecessor node.
|
|
|
|
NodeTy* StmtEntryNode;
|
|
|
|
|
|
|
|
/// CurrentStmt - The current block-level statement.
|
|
|
|
Stmt* CurrentStmt;
|
|
|
|
|
2008-01-30 23:03:39 +00:00
|
|
|
/// UninitBranches - Nodes in the ExplodedGraph that result from
|
|
|
|
/// taking a branch based on an uninitialized value.
|
|
|
|
typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
|
|
|
|
UninitBranchesTy UninitBranches;
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
bool StateCleaned;
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 00:33:40 +00:00
|
|
|
ASTContext& getContext() const { return G.getContext(); }
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-01-15 23:55:06 +00:00
|
|
|
public:
|
2008-01-29 05:13:23 +00:00
|
|
|
GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
|
|
|
|
Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
|
|
|
|
CurrentStmt(NULL) {
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-01-29 00:33:40 +00:00
|
|
|
// Compute liveness information.
|
2008-01-29 05:13:23 +00:00
|
|
|
Liveness.runOnCFG(G.getCFG());
|
|
|
|
Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
2008-01-29 00:33:40 +00:00
|
|
|
|
|
|
|
/// getCFG - Returns the CFG associated with this analysis.
|
|
|
|
CFG& getCFG() { return G.getCFG(); }
|
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-29 00:33:40 +00:00
|
|
|
StateTy St = StateMgr.GetEmptyMap();
|
2008-01-29 00:43:03 +00:00
|
|
|
|
|
|
|
// Iterate the parameters.
|
|
|
|
FunctionDecl& F = G.getFunctionDecl();
|
|
|
|
|
|
|
|
for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
|
|
|
|
I!=E; ++I) {
|
|
|
|
|
|
|
|
// For now we only support symbolic values for non-pointer types.
|
|
|
|
if ((*I)->getType()->isPointerType() ||
|
|
|
|
(*I)->getType()->isReferenceType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// FIXME: Set these values to a symbol, not Uninitialized.
|
2008-01-29 17:27:31 +00:00
|
|
|
St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
|
2008-01-29 00:43:03 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 00:33:40 +00:00
|
|
|
return St;
|
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-29 22:56:11 +00:00
|
|
|
void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
|
|
|
|
|
|
|
|
/// ProcessBranch - Called by GREngine. Used to generate successor
|
|
|
|
/// nodes by processing the 'effects' of a branch condition.
|
2008-01-29 23:32:35 +00:00
|
|
|
void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
|
2008-01-15 23:55:06 +00:00
|
|
|
|
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-28 22:09:13 +00:00
|
|
|
StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
|
2008-01-24 20:55:43 +00:00
|
|
|
return SetValue(St, const_cast<Stmt*>(S), V);
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
|
2008-01-16 19:42:59 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue GetValue(const StateTy& St, Stmt* S);
|
|
|
|
inline RValue GetValue(const StateTy& St, const Stmt* S) {
|
2008-01-24 20:55:43 +00:00
|
|
|
return GetValue(St, const_cast<Stmt*>(S));
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue GetValue(const StateTy& St, const LValue& LV);
|
2008-01-23 19:59:44 +00:00
|
|
|
LValue GetLValue(const StateTy& St, Stmt* S);
|
2008-01-30 23:03:39 +00:00
|
|
|
|
|
|
|
/// Assume - Create new state by assuming that a given expression
|
|
|
|
/// is true or false.
|
|
|
|
inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
|
|
|
|
bool& isFeasible) {
|
|
|
|
if (isa<LValue>(Cond))
|
|
|
|
return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
|
|
|
|
else
|
|
|
|
return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
|
|
|
|
}
|
|
|
|
|
|
|
|
StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
|
|
|
|
StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
|
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.
|
2008-01-24 20:55:43 +00:00
|
|
|
void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
|
|
|
|
|
|
|
|
/// VisitDeclStmt - Transfer function logic for DeclStmts.
|
|
|
|
void VisitDeclStmt(DeclStmt* DS, 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-29 23:32:35 +00:00
|
|
|
void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
|
|
|
|
BranchNodeBuilder& builder) {
|
2008-01-30 23:03:39 +00:00
|
|
|
|
|
|
|
StateTy PrevState = builder.getState();
|
|
|
|
|
|
|
|
// Remove old bindings for subexpressions.
|
|
|
|
for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
|
|
|
|
if (I.getKey().isSubExpr())
|
|
|
|
PrevState = StateMgr.Remove(PrevState, I.getKey());
|
|
|
|
|
|
|
|
RValue V = GetValue(PrevState, Condition);
|
|
|
|
|
|
|
|
switch (V.getBaseKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RValue::InvalidKind:
|
|
|
|
builder.generateNode(PrevState, true);
|
|
|
|
builder.generateNode(PrevState, false);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case RValue::UninitializedKind: {
|
|
|
|
NodeTy* N = builder.generateNode(PrevState, true);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UninitBranches.insert(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the true branch.
|
|
|
|
bool isFeasible = true;
|
|
|
|
StateTy St = Assume(PrevState, V, true, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible) builder.generateNode(St, true);
|
|
|
|
else {
|
|
|
|
builder.markInfeasible(true);
|
|
|
|
isFeasible = true;
|
|
|
|
}
|
2008-01-29 23:32:35 +00:00
|
|
|
|
2008-01-30 23:03:39 +00:00
|
|
|
// Process the false branch.
|
|
|
|
St = Assume(PrevState, V, false, isFeasible);
|
2008-01-29 23:32:35 +00:00
|
|
|
|
2008-01-30 23:03:39 +00:00
|
|
|
if (isFeasible) builder.generateNode(St, false);
|
|
|
|
else builder.markInfeasible(false);
|
|
|
|
|
2008-01-29 23:32:35 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 22:56:11 +00:00
|
|
|
void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
|
2008-01-15 23:55:06 +00:00
|
|
|
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-28 22:09:13 +00:00
|
|
|
RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (LV.getSubKind()) {
|
|
|
|
case LValueDeclKind: {
|
2008-01-23 19:59:44 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
|
2008-01-24 00:50:08 +00:00
|
|
|
for (;;) {
|
|
|
|
switch (S->getStmtClass()) {
|
2008-01-28 22:09:13 +00:00
|
|
|
|
|
|
|
// ParenExprs are no-ops.
|
|
|
|
|
2008-01-24 00:50:08 +00:00
|
|
|
case Stmt::ParenExprClass:
|
|
|
|
S = cast<ParenExpr>(S)->getSubExpr();
|
|
|
|
continue;
|
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
// DeclRefExprs can either evaluate to an LValue or a Non-LValue
|
|
|
|
// (assuming an implicit "load") depending on the context. In this
|
|
|
|
// context we assume that we are retrieving the value contained
|
|
|
|
// within the referenced variables.
|
|
|
|
|
2008-01-24 00:50:08 +00:00
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
|
2008-01-16 19:47:19 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
// Integer literals evaluate to an RValue. Simply retrieve the
|
|
|
|
// RValue for the literal.
|
|
|
|
|
2008-01-24 00:50:08 +00:00
|
|
|
case Stmt::IntegerLiteralClass:
|
2008-01-28 22:09:13 +00:00
|
|
|
return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
|
|
|
|
|
|
|
|
// Casts where the source and target type are the same
|
|
|
|
// are no-ops. We blast through these to get the descendant
|
|
|
|
// subexpression that has a value.
|
|
|
|
|
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;
|
|
|
|
}
|
2008-01-28 22:09:13 +00:00
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
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-28 22:09:13 +00:00
|
|
|
// Handle all other Stmt* using a lookup.
|
|
|
|
|
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) {
|
2008-01-28 22:09:13 +00:00
|
|
|
while (ParenExpr* P = dyn_cast<ParenExpr>(S))
|
|
|
|
S = P->getSubExpr();
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
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-28 22:09:13 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
|
2008-01-28 22:09:13 +00:00
|
|
|
const RValue& V) {
|
2008-01-25 23:43:12 +00:00
|
|
|
assert (S);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
|
|
|
|
2008-01-24 22:27:20 +00:00
|
|
|
bool isBlkExpr = false;
|
|
|
|
|
|
|
|
if (S == CurrentStmt) {
|
|
|
|
isBlkExpr = getCFG().isBlkExpr(S);
|
|
|
|
|
|
|
|
if (!isBlkExpr)
|
|
|
|
return St;
|
|
|
|
}
|
2008-01-24 19:28:01 +00:00
|
|
|
|
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,
|
2008-01-28 22:09:13 +00:00
|
|
|
const RValue& V) {
|
2008-01-23 19:59:44 +00:00
|
|
|
if (!LV.isValid())
|
|
|
|
return St;
|
|
|
|
|
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
|
|
|
|
2008-01-24 23:19:54 +00:00
|
|
|
switch (LV.getSubKind()) {
|
|
|
|
case LValueDeclKind:
|
2008-01-23 19:59:44 +00:00
|
|
|
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-29 05:25:31 +00:00
|
|
|
|
|
|
|
for (; I!=E && !I.getKey().isSymbol(); ++I) {
|
|
|
|
// Remove old bindings for subexpressions and "dead"
|
|
|
|
// block-level expressions.
|
|
|
|
if (I.getKey().isSubExpr() ||
|
|
|
|
I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
|
2008-01-24 19:43:37 +00:00
|
|
|
M = StateMgr.Remove(M, I.getKey());
|
2008-01-29 05:25:31 +00:00
|
|
|
}
|
|
|
|
else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
|
|
|
|
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-18 00:41:32 +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();
|
2008-01-28 22:09:13 +00:00
|
|
|
const RValue& V = GetValue(St, E);
|
|
|
|
Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
|
2008-01-24 02:02:54 +00:00
|
|
|
}
|
2008-01-24 20:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
|
|
|
|
GRConstants::NodeSet& Dst) {
|
|
|
|
|
|
|
|
StateTy St = Pred->getState();
|
|
|
|
|
|
|
|
for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
|
2008-01-28 22:51:57 +00:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
const Expr* E = VD->getInit();
|
|
|
|
St = SetValue(St, LValueDecl(VD),
|
|
|
|
E ? GetValue(St, E) : UninitializedValue());
|
|
|
|
}
|
2008-01-24 20:55:43 +00:00
|
|
|
|
|
|
|
Nodify(Dst, DS, Pred, St);
|
|
|
|
|
|
|
|
if (Dst.empty())
|
|
|
|
Dst.Add(Pred);
|
|
|
|
}
|
2008-01-24 02:02:54 +00:00
|
|
|
|
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());
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
|
|
|
|
U->getLocStart());
|
2008-01-24 19:00:57 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue Result = R1.Add(ValMgr, R2);
|
2008-01-24 02:28:56 +00:00
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PostDec: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
|
|
|
|
U->getLocStart());
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue Result = R1.Sub(ValMgr, R2);
|
2008-01-24 02:28:56 +00:00
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PreInc: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
|
|
|
|
U->getLocStart());
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue Result = R1.Add(ValMgr, R2);
|
2008-01-24 02:28:56 +00:00
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::PreDec: {
|
|
|
|
const LValue& L1 = GetLValue(St, U->getSubExpr());
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
|
2008-01-29 19:43:15 +00:00
|
|
|
NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
|
|
|
|
U->getLocStart());
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue Result = R1.Sub(ValMgr, R2);
|
2008-01-24 02:28:56 +00:00
|
|
|
Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-24 08:20:02 +00:00
|
|
|
case UnaryOperator::Minus: {
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
|
|
|
|
Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
|
2008-01-24 08:20:02 +00:00
|
|
|
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
|
2008-01-28 22:09:13 +00:00
|
|
|
// evaluated to LValueDecl's instead of to an NonLValue.
|
|
|
|
const RValue& V1 =
|
2008-01-23 19:59:44 +00:00
|
|
|
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();
|
2008-01-28 22:09:13 +00:00
|
|
|
const RValue& V2 = GetValue(St, B->getRHS());
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
switch (B->getOpcode()) {
|
2008-01-29 19:43:15 +00:00
|
|
|
default:
|
|
|
|
Dst.Add(N2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Arithmetic opreators.
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
case BinaryOperator::Add: {
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-01-28 22:09:13 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BinaryOperator::Sub: {
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 23:42:27 +00:00
|
|
|
case BinaryOperator::Mul: {
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
|
2008-01-23 23:42:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-25 22:55:56 +00:00
|
|
|
case BinaryOperator::Div: {
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
|
2008-01-25 22:55:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:26:15 +00:00
|
|
|
case BinaryOperator::Rem: {
|
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
|
|
|
Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-29 19:43:15 +00:00
|
|
|
// Assignment operators.
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
case BinaryOperator::Assign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
2008-01-28 22:09:13 +00:00
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
2008-01-23 19:59:44 +00:00
|
|
|
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);
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
|
|
|
|
NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(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);
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
|
|
|
|
NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(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);
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
|
|
|
|
NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
|
2008-01-23 23:42:27 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-25 23:45:34 +00:00
|
|
|
|
|
|
|
case BinaryOperator::DivAssign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
2008-01-28 22:09:13 +00:00
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
|
|
|
|
NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
|
2008-01-25 23:45:34 +00:00
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-28 22:28:54 +00:00
|
|
|
|
|
|
|
case BinaryOperator::RemAssign: {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
|
|
|
|
NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
|
|
|
|
Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-29 19:43:15 +00:00
|
|
|
|
|
|
|
// Equality operators.
|
|
|
|
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
// FIXME: should we allow XX.EQ() to return a set of values,
|
|
|
|
// allowing state bifurcation? In such cases, they will also
|
|
|
|
// modify the state (meaning that a new state will be returned
|
|
|
|
// as well).
|
|
|
|
assert (B->getType() == getContext().IntTy);
|
|
|
|
|
|
|
|
if (isa<LValue>(V1)) {
|
|
|
|
const LValue& L1 = cast<LValue>(V1);
|
|
|
|
const LValue& L2 = cast<LValue>(V2);
|
|
|
|
St = SetValue(St, B, L1.EQ(ValMgr, L2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const NonLValue& R1 = cast<NonLValue>(V1);
|
|
|
|
const NonLValue& R2 = cast<NonLValue>(V2);
|
|
|
|
St = SetValue(St, B, R1.EQ(ValMgr, R2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Nodify(Dst, B, N2, St);
|
2008-01-23 19:59:44 +00:00
|
|
|
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-24 20:55:43 +00:00
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
VisitDeclStmt(cast<DeclStmt>(S), 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-30 23:03:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "Assume" logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
|
|
|
|
bool& isFeasible) {
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
|
|
|
StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
|
|
|
|
bool& isFeasible) {
|
|
|
|
|
|
|
|
switch (Cond.getSubKind()) {
|
|
|
|
default:
|
|
|
|
assert (false && "'Assume' not implemented for this NonLValue.");
|
|
|
|
return St;
|
|
|
|
|
|
|
|
case ConcreteIntKind: {
|
|
|
|
bool b = cast<ConcreteInt>(Cond).getValue() != 0;
|
|
|
|
isFeasible = b ? Assumption : !Assumption;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-24 22:27:20 +00:00
|
|
|
|
|
|
|
static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
|
2008-01-23 22:30:44 +00:00
|
|
|
switch (kind) {
|
2008-01-24 22:44:24 +00:00
|
|
|
case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
|
2008-01-23 22:30:44 +00:00
|
|
|
case ValueKey::IsDecl: Out << "Variables:\\l"; break;
|
|
|
|
case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
|
|
|
|
default: assert (false && "Unknown ValueKey type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-24 22:27:20 +00:00
|
|
|
static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
|
|
|
|
ValueKey::Kind kind, bool isFirstGroup = false) {
|
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
|
|
|
|
if (I.getKey().getKind() != kind)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isFirst) {
|
|
|
|
if (!isFirstGroup) Out << "\\l\\l";
|
|
|
|
PrintKindLabel(Out, kind);
|
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out << "\\l";
|
|
|
|
|
|
|
|
Out << ' ';
|
|
|
|
|
|
|
|
if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
|
|
|
|
Out << V->getName();
|
|
|
|
else {
|
|
|
|
Stmt* E = cast<Stmt>(I.getKey());
|
|
|
|
Out << " (" << (void*) E << ") ";
|
|
|
|
E->printPretty(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-24 22:27:20 +00:00
|
|
|
Out << L.getStmt()->getStmtClassName() << ':'
|
|
|
|
<< (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-30 23:03:39 +00:00
|
|
|
|
|
|
|
if (Stmt* T = E.getSrc()->getTerminator()) {
|
|
|
|
Out << "\\|Terminator: ";
|
|
|
|
E.getSrc()->printTerminator(Out);
|
|
|
|
|
|
|
|
if (isa<SwitchStmt>(T)) {
|
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out << "\\lCondition: ";
|
|
|
|
if (*E.getSrc()->succ_begin() == E.getDst())
|
|
|
|
Out << "true";
|
|
|
|
else
|
|
|
|
Out << "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\\l";
|
|
|
|
}
|
2008-01-16 21:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << "\\|";
|
2008-01-16 21:46:15 +00:00
|
|
|
|
2008-01-24 22:27:20 +00:00
|
|
|
PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
|
|
|
|
PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
|
2008-01-24 22:44:24 +00:00
|
|
|
PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
|
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-29 00:33:40 +00:00
|
|
|
void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
|
|
|
|
GREngine<GRConstants> Engine(cfg, FD, 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
|