35#include "llvm/Support/Casting.h"
39#define DEBUG_TYPE "dataflow"
46 if (
Block ==
nullptr) {
50 if (!ACFG.isBlockReachable(*
Block))
52 if (
Block->getBlockID() == CurBlockID)
54 const auto &State = BlockToState[
Block->getBlockID()];
67 if (LHSValue == RHSValue && LHSValue)
75 if (
auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))
76 if (
auto *RHSBool = dyn_cast_or_null<BoolValue>(RHSValue))
77 return Env.
makeIff(*LHSBool, *RHSBool);
79 if (
auto *LHSPtr = dyn_cast_or_null<PointerValue>(LHSValue))
80 if (
auto *RHSPtr = dyn_cast_or_null<PointerValue>(RHSValue))
84 if (&LHSPtr->getPointeeLoc() == &RHSPtr->getPointeeLoc())
91 if (
auto *Top = llvm::dyn_cast<TopBoolValue>(&
V)) {
107 auto *B = dyn_cast_or_null<BoolValue>(Val);
112 if (&UnpackedVal == Val)
147 TransferVisitor(
const StmtToEnvMap &StmtToEnv, Environment &Env,
148 Environment::ValueModel &Model)
149 : StmtToEnv(StmtToEnv), Env(Env), Model(Model) {}
151 void VisitBinaryOperator(
const BinaryOperator *S) {
152 const Expr *LHS = S->getLHS();
153 assert(LHS !=
nullptr);
155 const Expr *RHS = S->getRHS();
156 assert(RHS !=
nullptr);
162 if (S->isCompoundAssignmentOp())
165 switch (S->getOpcode()) {
167 auto *LHSLoc = Env.getStorageLocation(*LHS);
168 if (LHSLoc ==
nullptr)
171 auto *RHSVal = Env.getValue(*RHS);
172 if (RHSVal ==
nullptr)
176 Env.setValue(*LHSLoc, *RHSVal);
179 Env.setStorageLocation(*S, *LHSLoc);
184 BoolValue &LHSVal = getLogicOperatorSubExprValue(*LHS);
185 BoolValue &RHSVal = getLogicOperatorSubExprValue(*RHS);
187 if (S->getOpcode() == BO_LAnd)
188 Env.setValue(*S, Env.makeAnd(LHSVal, RHSVal));
190 Env.setValue(*S, Env.makeOr(LHSVal, RHSVal));
196 Env.setValue(*S, S->getOpcode() == BO_EQ ? LHSEqRHSValue
197 : Env.makeNot(LHSEqRHSValue));
209 void VisitDeclRefExpr(
const DeclRefExpr *S) {
210 const ValueDecl *VD = S->getDecl();
211 assert(VD !=
nullptr);
225 auto *DeclLoc = Env.getStorageLocation(*VD);
226 if (DeclLoc ==
nullptr)
229 Env.setStorageLocation(*S, *DeclLoc);
232 void VisitDeclStmt(
const DeclStmt *S) {
240 void ProcessVarDecl(
const VarDecl &D) {
242 if (D.hasGlobalStorage())
248 if (D.getType()->isReferenceType() && Env.getStorageLocation(D) !=
nullptr)
251 assert(Env.getStorageLocation(D) ==
nullptr);
253 Env.setStorageLocation(D, Env.createObject(D));
258 if (
const auto *Decomp = dyn_cast<DecompositionDecl>(&D)) {
264 for (
const auto *B : Decomp->bindings()) {
265 if (
auto *ME = dyn_cast_or_null<MemberExpr>(B->getBinding())) {
266 auto *DE = dyn_cast_or_null<DeclRefExpr>(ME->getBase());
272 VisitDeclRefExpr(DE);
275 if (
auto *Loc = Env.getStorageLocation(*ME))
276 Env.setStorageLocation(*B, *Loc);
277 }
else if (
auto *VD = B->getHoldingVar()) {
285 auto *VDLoc = Env.getStorageLocation(*VD);
286 assert(VDLoc !=
nullptr);
287 Env.setStorageLocation(*B, *VDLoc);
293 void VisitCastExpr(
const CastExpr *S) {
294 const Expr *SubExpr = S->getSubExpr();
295 assert(SubExpr !=
nullptr);
297 switch (S->getCastKind()) {
298 case CK_IntegralToBoolean: {
302 if (
auto *SubExprVal =
303 dyn_cast_or_null<BoolValue>(Env.getValue(*SubExpr)))
304 Env.setValue(*S, *SubExprVal);
308 Env.setValue(*S, Env.makeAtomicBoolValue());
312 case CK_LValueToRValue: {
316 if (SubExprVal ==
nullptr)
319 Env.setValue(*S, *SubExprVal);
323 case CK_BaseToDerived: {
328 RecordStorageLocation *Loc =
nullptr;
329 if (S->getType()->isPointerType()) {
330 auto *PV = Env.get<PointerValue>(*SubExpr);
331 assert(PV !=
nullptr);
336 assert(S->getType()->isRecordType());
337 if (SubExpr->isGLValue()) {
338 Loc = Env.get<RecordStorageLocation>(*SubExpr);
340 Loc = &Env.getResultObjectLocation(*SubExpr);
349 QualType Derived = S->getType().getNonReferenceType();
350 if (Derived->isPointerType()) {
351 Derived = Derived->getPointeeType();
356 for (
const FieldDecl *Field :
357 Env.getDataflowAnalysisContext().getModeledFields(Derived)) {
358 assert(Field !=
nullptr);
359 QualType FieldType =
Field->getType();
360 if (FieldType->isReferenceType()) {
361 Loc->addChild(*Field,
nullptr);
363 Loc->addChild(*Field, &Env.createStorageLocation(FieldType));
366 for (
const auto &Entry :
367 Env.getDataflowAnalysisContext().getSyntheticFields(Derived)) {
368 Loc->addSyntheticField(Entry.getKey(),
369 Env.createStorageLocation(Entry.getValue()));
372 Env.initializeFieldsWithValues(*Loc, Derived);
377 case CK_IntegralCast:
383 case CK_UncheckedDerivedToBase:
384 case CK_DerivedToBase:
385 case CK_ConstructorConversion:
386 case CK_UserDefinedConversion:
394 case CK_NullToPointer: {
395 auto &NullPointerVal =
396 Env.getOrCreateNullPointerValue(S->getType()->getPointeeType());
397 Env.setValue(*S, NullPointerVal);
400 case CK_NullToMemberPointer:
404 case CK_FunctionToPointerDecay: {
405 StorageLocation *PointeeLoc = Env.getStorageLocation(*SubExpr);
406 if (PointeeLoc ==
nullptr)
409 Env.setValue(*S, Env.create<PointerValue>(*PointeeLoc));
412 case CK_BuiltinFnToFnPtr:
423 void VisitUnaryOperator(
const UnaryOperator *S) {
424 const Expr *SubExpr = S->getSubExpr();
425 assert(SubExpr !=
nullptr);
427 switch (S->getOpcode()) {
429 const auto *SubExprVal = Env.get<PointerValue>(*SubExpr);
430 if (SubExprVal ==
nullptr)
433 Env.setStorageLocation(*S, SubExprVal->getPointeeLoc());
438 if (S->getType()->isMemberPointerType())
441 if (StorageLocation *PointeeLoc = Env.getStorageLocation(*SubExpr))
442 Env.setValue(*S, Env.create<PointerValue>(*PointeeLoc));
446 auto *SubExprVal = dyn_cast_or_null<BoolValue>(Env.getValue(*SubExpr));
447 if (SubExprVal ==
nullptr)
450 Env.setValue(*S, Env.makeNot(*SubExprVal));
460 if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
461 Env.clearValue(*Loc);
469 if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
470 Env.clearValue(*Loc);
477 void VisitCXXThisExpr(
const CXXThisExpr *S) {
478 auto *ThisPointeeLoc = Env.getThisPointeeStorageLocation();
479 if (ThisPointeeLoc ==
nullptr)
484 Env.setValue(*S, Env.create<PointerValue>(*ThisPointeeLoc));
487 void VisitCXXNewExpr(
const CXXNewExpr *S) {
488 if (
Value *Val = Env.createValue(S->getType()))
489 Env.setValue(*S, *Val);
492 void VisitCXXDeleteExpr(
const CXXDeleteExpr *S) {
499 void VisitReturnStmt(
const ReturnStmt *S) {
500 if (!Env.getDataflowAnalysisContext().getOptions().ContextSensitiveOpts)
503 auto *
Ret = S->getRetValue();
507 if (
Ret->isPRValue()) {
508 if (
Ret->getType()->isRecordType())
511 auto *Val = Env.getValue(*Ret);
516 Env.setReturnValue(Val);
518 auto *Loc = Env.getStorageLocation(*Ret);
523 Env.setReturnStorageLocation(Loc);
527 void VisitMemberExpr(
const MemberExpr *S) {
528 ValueDecl *
Member = S->getMemberDecl();
529 assert(
Member !=
nullptr);
532 if (
Member->isFunctionOrFunctionTemplate())
539 if (
auto *D = dyn_cast<VarDecl>(
Member)) {
540 if (D->hasGlobalStorage()) {
541 auto *VarDeclLoc = Env.getStorageLocation(*D);
542 if (VarDeclLoc ==
nullptr)
545 Env.setStorageLocation(*S, *VarDeclLoc);
551 if (BaseLoc ==
nullptr)
554 auto *MemberLoc = BaseLoc->getChild(*
Member);
555 if (MemberLoc ==
nullptr)
557 Env.setStorageLocation(*S, *MemberLoc);
560 void VisitCXXDefaultArgExpr(
const CXXDefaultArgExpr *S) {
561 const Expr *ArgExpr = S->getExpr();
562 assert(ArgExpr !=
nullptr);
565 if (S->isPRValue() && S->getType()->isRecordType()) {
566 auto &Loc = Env.getResultObjectLocation(*S);
567 Env.initializeFieldsWithValues(Loc);
571 void VisitCXXDefaultInitExpr(
const CXXDefaultInitExpr *S) {
572 const Expr *InitExpr = S->getExpr();
573 assert(InitExpr !=
nullptr);
578 if (S->getType()->isRecordType() && S->isPRValue())
584 void VisitCXXConstructExpr(
const CXXConstructExpr *S) {
585 const CXXConstructorDecl *ConstructorDecl = S->getConstructor();
586 assert(ConstructorDecl !=
nullptr);
591 if (!S->getType()->isRecordType()) {
592 transferInlineCall(S, ConstructorDecl);
596 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
598 if (ConstructorDecl->isCopyOrMoveConstructor()) {
601 assert(S->getNumArgs() != 0);
603 const Expr *Arg = S->getArg(0);
604 assert(Arg !=
nullptr);
606 auto *ArgLoc = Env.get<RecordStorageLocation>(*Arg);
607 if (ArgLoc ==
nullptr)
625 Env.initializeFieldsWithValues(Loc, S->getType());
627 transferInlineCall(S, ConstructorDecl);
630 void VisitCXXOperatorCallExpr(
const CXXOperatorCallExpr *S) {
631 if (S->getOperator() == OO_Equal) {
632 assert(S->getNumArgs() == 2);
634 const Expr *Arg0 = S->getArg(0);
635 assert(Arg0 !=
nullptr);
637 const Expr *Arg1 = S->getArg(1);
638 assert(Arg1 !=
nullptr);
642 dyn_cast_or_null<CXXMethodDecl>(S->getDirectCallee());
645 if (!
Method->isCopyAssignmentOperator() &&
646 !
Method->isMoveAssignmentOperator())
649 RecordStorageLocation *LocSrc =
nullptr;
650 if (Arg1->isPRValue()) {
651 LocSrc = &Env.getResultObjectLocation(*Arg1);
653 LocSrc = Env.get<RecordStorageLocation>(*Arg1);
655 auto *LocDst = Env.get<RecordStorageLocation>(*Arg0);
657 if (LocSrc ==
nullptr || LocDst ==
nullptr)
665 if (S->getType().getCanonicalType().getUnqualifiedType() !=
666 LocDst->getType().getCanonicalType().getUnqualifiedType()) {
667 auto ReturnDecl = S->getType()->getAsCXXRecordDecl();
668 auto DstDecl = LocDst->getType()->getAsCXXRecordDecl();
669 if (ReturnDecl ==
nullptr || DstDecl ==
nullptr)
671 if (!DstDecl->isDerivedFrom(ReturnDecl))
676 Env.setStorageLocation(*S, *LocDst);
678 copyRecord(*LocDst, Env.getResultObjectLocation(*S), Env);
688 void VisitCXXRewrittenBinaryOperator(
const CXXRewrittenBinaryOperator *RBO) {
692 void VisitCallExpr(
const CallExpr *S) {
696 if (S->isCallToStdMove()) {
697 assert(S->getNumArgs() == 1);
699 const Expr *Arg = S->getArg(0);
700 assert(Arg !=
nullptr);
702 auto *ArgLoc = Env.getStorageLocation(*Arg);
703 if (ArgLoc ==
nullptr)
706 Env.setStorageLocation(*S, *ArgLoc);
707 }
else if (S->getDirectCallee() !=
nullptr &&
708 S->getDirectCallee()->getBuiltinID() ==
709 Builtin::BI__builtin_expect) {
710 assert(S->getNumArgs() > 0);
711 assert(S->getArg(0) !=
nullptr);
712 auto *ArgVal = Env.getValue(*S->getArg(0));
713 if (ArgVal ==
nullptr)
715 Env.setValue(*S, *ArgVal);
716 }
else if (
const FunctionDecl *F = S->getDirectCallee()) {
717 transferInlineCall(S, F);
721 if (S->getType()->isRecordType() && S->isPRValue()) {
722 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
723 Env.initializeFieldsWithValues(Loc);
728 void VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *S) {
729 const Expr *SubExpr = S->getSubExpr();
730 assert(SubExpr !=
nullptr);
732 StorageLocation &Loc = Env.createStorageLocation(*S);
733 Env.setStorageLocation(*S, Loc);
735 if (SubExpr->getType()->isRecordType())
740 if (
Value *SubExprVal = Env.getValue(*SubExpr))
741 Env.setValue(Loc, *SubExprVal);
744 void VisitCXXBindTemporaryExpr(
const CXXBindTemporaryExpr *S) {
745 const Expr *SubExpr = S->getSubExpr();
746 assert(SubExpr !=
nullptr);
751 void VisitConditionalOperator(
const ConditionalOperator *S) {
752 const Environment *TrueEnv = StmtToEnv.getEnvironment(*S->getTrueExpr());
753 const Environment *FalseEnv = StmtToEnv.getEnvironment(*S->getFalseExpr());
755 if (TrueEnv ==
nullptr || FalseEnv ==
nullptr) {
763 if (S->isGLValue()) {
764 StorageLocation *TrueLoc = TrueEnv->getStorageLocation(*S->getTrueExpr());
765 StorageLocation *FalseLoc =
766 FalseEnv->getStorageLocation(*S->getFalseExpr());
767 if (TrueLoc == FalseLoc && TrueLoc !=
nullptr)
768 Env.setStorageLocation(*S, *TrueLoc);
769 }
else if (!S->getType()->isRecordType()) {
786 S->getType(), TrueEnv->getValue(*S->getTrueExpr()), *TrueEnv,
787 FalseEnv->getValue(*S->getFalseExpr()), *FalseEnv, Env, Model))
788 Env.setValue(*S, *Val);
792 void VisitInitListExpr(
const InitListExpr *S) {
793 QualType
Type = S->getType();
795 if (!
Type->isRecordType()) {
798 if (!
Type->isArrayType() && S->getNumInits() == 1)
804 if (S->isSemanticForm() && S->isTransparent())
807 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
814 RecordInitListHelper InitListHelper(S);
816 for (
auto [Field,
Init] : InitListHelper.field_inits()) {
817 if (
Field->getType()->isRecordType())
819 if (
Field->getType()->isReferenceType()) {
820 assert(
Field->getType().getCanonicalType()->getPointeeType() ==
821 Init->getType().getCanonicalType());
822 Loc.setChild(*Field, &Env.createObject(
Field->getType(),
Init));
825 assert(
Field->getType().getCanonicalType().getUnqualifiedType() ==
826 Init->getType().getCanonicalType().getUnqualifiedType());
827 StorageLocation *FieldLoc = Loc.getChild(*Field);
829 assert(FieldLoc !=
nullptr);
832 Init->getType()->isPointerType())
834 &Env.getOrCreateNullPointerValue(
Init->getType()->getPointeeType());
836 Val = Env.createValue(
Field->getType());
838 Env.setValue(*FieldLoc, *Val);
841 for (
const auto &[FieldName, FieldLoc] : Loc.synthetic_fields()) {
842 QualType FieldType = FieldLoc->getType();
843 if (FieldType->isRecordType()) {
846 if (
Value *Val = Env.createValue(FieldType))
847 Env.setValue(*FieldLoc, *Val);
854 void VisitCXXBoolLiteralExpr(
const CXXBoolLiteralExpr *S) {
855 Env.setValue(*S, Env.getBoolLiteralValue(S->getValue()));
858 void VisitIntegerLiteral(
const IntegerLiteral *S) {
859 Env.setValue(*S, Env.getIntLiteralValue(S->getValue()));
862 void VisitParenExpr(
const ParenExpr *S) {
866 auto *SubExpr = S->getSubExpr();
867 assert(SubExpr !=
nullptr);
871 void VisitExprWithCleanups(
const ExprWithCleanups *S) {
875 auto *SubExpr = S->getSubExpr();
876 assert(SubExpr !=
nullptr);
882 BoolValue &getLogicOperatorSubExprValue(
const Expr &SubExpr) {
886 if (
const Environment *SubExprEnv = StmtToEnv.getEnvironment(SubExpr))
888 dyn_cast_or_null<BoolValue>(SubExprEnv->getValue(SubExpr)))
896 if (Env.getValue(SubExpr) ==
nullptr)
898 if (
auto *Val = dyn_cast_or_null<BoolValue>(Env.getValue(SubExpr)))
903 return Env.makeAtomicBoolValue();
908 template <
typename E>
909 void transferInlineCall(
const E *S,
const FunctionDecl *F) {
910 const auto &Options = Env.getDataflowAnalysisContext().getOptions();
911 if (!(Options.ContextSensitiveOpts &&
912 Env.canDescend(Options.ContextSensitiveOpts->Depth, F)))
915 const AdornedCFG *ACFG = Env.getDataflowAnalysisContext().getAdornedCFG(F);
923 auto ExitBlock = ACFG->getCFG().getExit().getBlockID();
925 auto CalleeEnv = Env.pushCall(S);
930 auto Analysis = NoopAnalysis(ACFG->getDecl().getASTContext(),
931 DataflowAnalysisOptions{Options});
933 auto BlockToOutputState =
935 assert(BlockToOutputState);
936 assert(ExitBlock < BlockToOutputState->size());
938 auto &ExitState = (*BlockToOutputState)[ExitBlock];
941 Env.popCall(S, ExitState->Env);
944 const StmtToEnvMap &StmtToEnv;
946 Environment::ValueModel &Model;
953 TransferVisitor(StmtToEnv, Env, Model).Visit(&S);
Defines enum values for all the target-independent builtin functions.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines an enumeration for C++ overloaded operators.
C Language Family Type Representation.
Represents a single basic block in a source-level CFG.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
This represents one expression.
Stmt - This represents one statement.
bool isNullPtrType() const
bool isRecordType() const
BoolValue & makeBoolValue(const Formula &)
Creates a BoolValue wrapping a particular formula.
Supplements Environment with non-standard comparison and join operations.
Holds the state of the program (store and heap) at a given program point.
BoolValue & makeIff(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value represents LHS <=> RHS.
StorageLocation * getStorageLocation(const ValueDecl &D) const
Returns the storage location assigned to D in the environment, or null if D isn't assigned a storage ...
BoolValue & makeAtomicBoolValue() const
Returns an atomic boolean value.
Value * getValue(const StorageLocation &Loc) const
Returns the value assigned to Loc in the environment or null if Loc isn't assigned a value in the env...
BoolValue & getBoolLiteralValue(bool Value) const
Returns a symbolic boolean value that models a boolean literal equal to Value
DataflowAnalysisContext & getDataflowAnalysisContext() const
Returns the DataflowAnalysisContext used by the environment.
static Value * joinValues(QualType Ty, Value *Val1, const Environment &Env1, Value *Val2, const Environment &Env2, Environment &JoinedEnv, Environment::ValueModel &Model)
Returns a value that approximates both Val1 and Val2, or null if no such value can be produced.
void setStorageLocation(const ValueDecl &D, StorageLocation &Loc)
Assigns Loc as the storage location of D in the environment.
void setValue(const StorageLocation &Loc, Value &Val)
Assigns Val as the value of Loc in the environment.
Maps statements to the environments of basic blocks that contain them.
const Environment * getEnvironment(const Stmt &S) const
Returns the environment of the basic block that contains S.
Base class for all values computed by abstract interpretation.
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env, Environment::ValueModel &Model)
Evaluates S and updates Env accordingly.
static void propagateValueOrStorageLocation(const Expr &From, const Expr &To, Environment &Env)
static void propagateStorageLocation(const Expr &From, const Expr &To, Environment &Env)
void copyRecord(RecordStorageLocation &Src, RecordStorageLocation &Dst, Environment &Env, QualType TypeToCopy=QualType())
Copies a record (struct, class, or union) from Src to Dst.
static void propagateValue(const Expr &From, const Expr &To, Environment &Env)
static Value * maybeUnpackLValueExpr(const Expr &E, Environment &Env)
static BoolValue & unpackValue(BoolValue &V, Environment &Env)
static BoolValue & evaluateBooleanEquality(const Expr &LHS, const Expr &RHS, Environment &Env)
RecordStorageLocation * getBaseObjectLocation(const MemberExpr &ME, const Environment &Env)
Returns the storage location for the base object of a MemberExpr, or null if none is defined in the e...
llvm::Expected< std::vector< std::optional< DataflowAnalysisState< typename AnalysisT::Lattice > > > > runDataflowAnalysis(const AdornedCFG &ACFG, AnalysisT &Analysis, const Environment &InitEnv, CFGEltCallbacks< AnalysisT > PostAnalysisCallbacks={}, std::int32_t MaxBlockVisits=kDefaultMaxBlockVisits)
Performs dataflow analysis and returns a mapping from basic block IDs to dataflow analysis states tha...
bool Ret(InterpState &S, CodePtr &PC)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
@ Type
The name was classified as a type.
U cast(CodeGen::Address addr)