1 //===- ThreadSafetyTIL.h ---------------------------------------*- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple intermediate language that is used by the
11 // thread safety analysis (See ThreadSafety.cpp). The thread safety analysis
12 // works by comparing mutex expressions, e.g.
14 // class A { Mutex mu; int dat GUARDED_BY(this->mu); }
18 // (*b).a.mu.lock(); // locks (*b).a.mu
19 // b->a.dat = 0; // substitute &b->a for 'this';
20 // // requires lock on (&b->a)->mu
21 // (b->a.mu).unlock(); // unlocks (b->a.mu)
24 // As illustrated by the above example, clang Exprs are not well-suited to
25 // represent mutex expressions directly, since there is no easy way to compare
26 // Exprs for equivalence. The thread safety analysis thus lowers clang Exprs
27 // into a simple intermediate language (IL). The IL supports:
29 // (1) comparisons for semantic equality of expressions
30 // (2) SSA renaming of variables
31 // (3) wildcards and pattern matching over expressions
32 // (4) hash-based expression lookup
34 // The IL is currently very experimental, is intended only for use within
35 // the thread safety analysis, and is subject to change without notice.
36 // After the API stabilizes and matures, it may be appropriate to make this
37 // more generally available to other analyses.
39 // UNDER CONSTRUCTION. USE AT YOUR OWN RISK.
41 //===----------------------------------------------------------------------===//
43 #ifndef LLVM_CLANG_THREAD_SAFETY_TIL_H
44 #define LLVM_CLANG_THREAD_SAFETY_TIL_H
46 #include "clang/Analysis/Analyses/ThreadSafetyUtil.h"
47 #include "clang/AST/ExprCXX.h"
48 #include "llvm/ADT/StringRef.h"
56 namespace threadSafety {
59 using llvm::StringRef;
60 using clang::SourceLocation;
64 #define TIL_OPCODE_DEF(X) COP_##X,
65 #include "clang/Analysis/Analyses/ThreadSafetyOps.def"
71 typedef clang::BinaryOperatorKind TIL_BinaryOpcode;
72 typedef clang::UnaryOperatorKind TIL_UnaryOpcode;
73 typedef clang::CastKind TIL_CastOpcode;
78 TRV_Lazy, // subexpression may need to be traversed lazily
79 TRV_Tail // subexpression occurs in a tail position
83 // Base class for AST nodes in the typed intermediate language.
86 TIL_Opcode opcode() const { return static_cast<TIL_Opcode>(Opcode); }
88 // Subclasses of SExpr must define the following:
90 // This(const This& E, ...) {
91 // copy constructor: construct copy of E, with some additional arguments.
94 // template <class V> typename V::R_SExpr traverse(V &Visitor) {
95 // traverse all subexpressions, following the traversal/rewriter interface
98 // template <class C> typename C::CType compare(CType* E, C& Cmp) {
99 // compare all subexpressions, following the comparator interface
102 void *operator new(size_t S, clang::threadSafety::til::MemRegionRef &R) {
103 return ::operator new(S, R);
107 SExpr(TIL_Opcode Op) : Opcode(Op), Reserved(0), Flags(0) {}
108 SExpr(const SExpr &E) : Opcode(E.Opcode), Reserved(0), Flags(E.Flags) {}
110 const unsigned char Opcode;
111 unsigned char Reserved;
112 unsigned short Flags;
117 // SExpr objects must be created in an arena and cannot be deleted.
118 void *operator new(size_t) = delete;
119 void operator delete(void *) = delete;
123 // Class for owning references to SExprs.
124 // Includes attach/detach logic for counting variable references and lazy
125 // rewriting strategies.
128 SExprRef() : Ptr(nullptr) { }
129 SExprRef(std::nullptr_t P) : Ptr(nullptr) { }
130 SExprRef(SExprRef &&R) : Ptr(R.Ptr) { R.Ptr = nullptr; }
132 // Defined after Variable and Future, below.
133 inline SExprRef(SExpr *P);
136 SExpr *get() { return Ptr; }
137 const SExpr *get() const { return Ptr; }
139 SExpr *operator->() { return get(); }
140 const SExpr *operator->() const { return get(); }
142 SExpr &operator*() { return *Ptr; }
143 const SExpr &operator*() const { return *Ptr; }
145 bool operator==(const SExprRef &R) const { return Ptr == R.Ptr; }
146 bool operator!=(const SExprRef &R) const { return !operator==(R); }
147 bool operator==(const SExpr *P) const { return Ptr == P; }
148 bool operator!=(const SExpr *P) const { return !operator==(P); }
149 bool operator==(std::nullptr_t) const { return Ptr == nullptr; }
150 bool operator!=(std::nullptr_t) const { return Ptr != nullptr; }
152 inline void reset(SExpr *E);
155 inline void attach();
156 inline void detach();
162 // Contains various helper functions for SExprs.
163 namespace ThreadSafetyTIL {
164 inline bool isTrivial(SExpr *E) {
165 unsigned Op = E->opcode();
166 return Op == COP_Variable || Op == COP_Literal || Op == COP_LiteralPtr;
175 // A named variable, e.g. "x".
177 // There are two distinct places in which a Variable can appear in the AST.
178 // A variable declaration introduces a new variable, and can occur in 3 places:
179 // Let-expressions: (Let (x = t) u)
180 // Functions: (Function (x : t) u)
181 // Self-applicable functions (SFunction (x) t)
183 // If a variable occurs in any other location, it is a reference to an existing
184 // variable declaration -- e.g. 'x' in (x * y + z). To save space, we don't
185 // allocate a separate AST node for variable references; a reference is just a
186 // pointer to the original declaration.
187 class Variable : public SExpr {
189 static bool classof(const SExpr *E) { return E->opcode() == COP_Variable; }
191 // Let-variable, function parameter, or self-variable
198 // These are defined after SExprRef contructor, below
199 inline Variable(VariableKind K, SExpr *D = nullptr,
200 const clang::ValueDecl *Cvd = nullptr);
201 inline Variable(const clang::ValueDecl *Cvd, SExpr *D = nullptr);
202 inline Variable(const Variable &Vd, SExpr *D);
204 VariableKind kind() const { return static_cast<VariableKind>(Flags); }
206 const StringRef name() const { return Cvdecl ? Cvdecl->getName() : "_x"; }
207 const clang::ValueDecl *clangDecl() const { return Cvdecl; }
209 // Returns the definition (for let vars) or type (for parameter & self vars)
210 SExpr *definition() { return Definition.get(); }
212 void attachVar() const { ++NumUses; }
213 void detachVar() const { assert(NumUses > 0); --NumUses; }
215 unsigned getID() const { return Id; }
216 unsigned getBlockID() const { return BlockID; }
218 void setID(unsigned Bid, unsigned I) {
219 BlockID = static_cast<unsigned short>(Bid);
220 Id = static_cast<unsigned short>(I);
223 template <class V> typename V::R_SExpr traverse(V &Visitor) {
224 // This routine is only called for variable references.
225 return Visitor.reduceVariableRef(this);
228 template <class C> typename C::CType compare(Variable* E, C& Cmp) {
229 return Cmp.compareVariableRefs(this, E);
233 friend class Function;
234 friend class SFunction;
235 friend class BasicBlock;
237 // Function, SFunction, and BasicBlock will reset the kind.
238 void setKind(VariableKind K) { Flags = K; }
240 SExprRef Definition; // The TIL type or definition
241 const clang::ValueDecl *Cvdecl; // The clang declaration for this variable.
243 unsigned short BlockID;
245 mutable unsigned NumUses;
249 // Placeholder for an expression that has not yet been created.
250 // Used to implement lazy copy and rewriting strategies.
251 class Future : public SExpr {
253 static bool classof(const SExpr *E) { return E->opcode() == COP_Future; }
262 SExpr(COP_Future), Status(FS_pending), Result(nullptr), Location(nullptr)
264 virtual ~Future() = delete;
266 // Registers the location in the AST where this future is stored.
267 // Forcing the future will automatically update the AST.
268 static inline void registerLocation(SExprRef *Member) {
269 if (Future *F = dyn_cast_or_null<Future>(Member->get()))
270 F->Location = Member;
273 // A lazy rewriting strategy should subclass Future and override this method.
274 virtual SExpr *create() { return nullptr; }
276 // Return the result of this future if it exists, otherwise return null.
277 SExpr *maybeGetResult() {
281 // Return the result of this future; forcing it if necessary.
288 return nullptr; // infinite loop; illegal recursion.
294 template <class V> typename V::R_SExpr traverse(V &Visitor) {
295 assert(Result && "Cannot traverse Future that has not been forced.");
296 return Visitor.traverse(Result);
299 template <class C> typename C::CType compare(Future* E, C& Cmp) {
300 if (!Result || !E->Result)
301 return Cmp.comparePointers(this, E);
302 return Cmp.compare(Result, E->Result);
316 void SExprRef::attach() {
320 TIL_Opcode Op = Ptr->opcode();
321 if (Op == COP_Variable) {
322 cast<Variable>(Ptr)->attachVar();
324 else if (Op == COP_Future) {
325 cast<Future>(Ptr)->registerLocation(this);
329 void SExprRef::detach() {
330 if (Ptr && Ptr->opcode() == COP_Variable) {
331 cast<Variable>(Ptr)->detachVar();
335 SExprRef::SExprRef(SExpr *P) : Ptr(P) {
340 SExprRef::~SExprRef() {
344 void SExprRef::reset(SExpr *P) {
353 Variable::Variable(VariableKind K, SExpr *D, const clang::ValueDecl *Cvd)
354 : SExpr(COP_Variable), Definition(D), Cvdecl(Cvd),
355 BlockID(0), Id(0), NumUses(0) {
359 Variable::Variable(const clang::ValueDecl *Cvd, SExpr *D)
360 : SExpr(COP_Variable), Definition(D), Cvdecl(Cvd),
361 BlockID(0), Id(0), NumUses(0) {
365 Variable::Variable(const Variable &Vd, SExpr *D) // rewrite constructor
366 : SExpr(Vd), Definition(D), Cvdecl(Vd.Cvdecl),
367 BlockID(0), Id(0), NumUses(0) {
372 void Future::force() {
373 Status = FS_evaluating;
384 // Placeholder for C++ expressions that cannot be represented in the TIL.
385 class Undefined : public SExpr {
387 static bool classof(const SExpr *E) { return E->opcode() == COP_Undefined; }
389 Undefined(const clang::Stmt *S = nullptr) : SExpr(COP_Undefined), Cstmt(S) {}
390 Undefined(const Undefined &U) : SExpr(U), Cstmt(U.Cstmt) {}
392 template <class V> typename V::R_SExpr traverse(V &Visitor) {
393 return Visitor.reduceUndefined(*this);
396 template <class C> typename C::CType compare(Undefined* E, C& Cmp) {
397 return Cmp.comparePointers(Cstmt, E->Cstmt);
401 const clang::Stmt *Cstmt;
405 // Placeholder for a wildcard that matches any other expression.
406 class Wildcard : public SExpr {
408 static bool classof(const SExpr *E) { return E->opcode() == COP_Wildcard; }
410 Wildcard() : SExpr(COP_Wildcard) {}
411 Wildcard(const Wildcard &W) : SExpr(W) {}
413 template <class V> typename V::R_SExpr traverse(V &Visitor) {
414 return Visitor.reduceWildcard(*this);
417 template <class C> typename C::CType compare(Wildcard* E, C& Cmp) {
418 return Cmp.trueResult();
423 // Base class for literal values.
424 class Literal : public SExpr {
426 static bool classof(const SExpr *E) { return E->opcode() == COP_Literal; }
428 Literal(const clang::Expr *C) : SExpr(COP_Literal), Cexpr(C) {}
429 Literal(const Literal &L) : SExpr(L), Cexpr(L.Cexpr) {}
431 // The clang expression for this literal.
432 const clang::Expr *clangExpr() { return Cexpr; }
434 template <class V> typename V::R_SExpr traverse(V &Visitor) {
435 return Visitor.reduceLiteral(*this);
438 template <class C> typename C::CType compare(Literal* E, C& Cmp) {
439 // TODO -- use value, not pointer equality
440 return Cmp.comparePointers(Cexpr, E->Cexpr);
444 const clang::Expr *Cexpr;
448 // Literal pointer to an object allocated in memory.
449 // At compile time, pointer literals are represented by symbolic names.
450 class LiteralPtr : public SExpr {
452 static bool classof(const SExpr *E) { return E->opcode() == COP_LiteralPtr; }
454 LiteralPtr(const clang::ValueDecl *D) : SExpr(COP_LiteralPtr), Cvdecl(D) {}
455 LiteralPtr(const LiteralPtr &R) : SExpr(R), Cvdecl(R.Cvdecl) {}
457 // The clang declaration for the value that this pointer points to.
458 const clang::ValueDecl *clangDecl() { return Cvdecl; }
460 template <class V> typename V::R_SExpr traverse(V &Visitor) {
461 return Visitor.reduceLiteralPtr(*this);
464 template <class C> typename C::CType compare(LiteralPtr* E, C& Cmp) {
465 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
469 const clang::ValueDecl *Cvdecl;
476 // A function -- a.k.a. lambda abstraction.
477 // Functions with multiple arguments are created by currying,
478 // e.g. (function (x: Int) (function (y: Int) (add x y)))
479 class Function : public SExpr {
481 static bool classof(const SExpr *E) { return E->opcode() == COP_Function; }
483 Function(Variable *Vd, SExpr *Bd)
484 : SExpr(COP_Function), VarDecl(Vd), Body(Bd) {
485 Vd->setKind(Variable::VK_Fun);
487 Function(const Function &F, Variable *Vd, SExpr *Bd) // rewrite constructor
488 : SExpr(F), VarDecl(Vd), Body(Bd) {
489 Vd->setKind(Variable::VK_Fun);
492 Variable *variableDecl() { return VarDecl; }
493 const Variable *variableDecl() const { return VarDecl; }
495 SExpr *body() { return Body.get(); }
496 const SExpr *body() const { return Body.get(); }
498 template <class V> typename V::R_SExpr traverse(V &Visitor) {
499 // This is a variable declaration, so traverse the definition.
500 typename V::R_SExpr E0 = Visitor.traverse(VarDecl->Definition, TRV_Lazy);
501 // Tell the rewriter to enter the scope of the function.
502 Variable *Nvd = Visitor.enterScope(*VarDecl, E0);
503 typename V::R_SExpr E1 = Visitor.traverse(Body);
504 Visitor.exitScope(*VarDecl);
505 return Visitor.reduceFunction(*this, Nvd, E1);
508 template <class C> typename C::CType compare(Function* E, C& Cmp) {
509 typename C::CType Ct =
510 Cmp.compare(VarDecl->definition(), E->VarDecl->definition());
513 Cmp.enterScope(variableDecl(), E->variableDecl());
514 Ct = Cmp.compare(body(), E->body());
525 // A self-applicable function.
526 // A self-applicable function can be applied to itself. It's useful for
527 // implementing objects and late binding
528 class SFunction : public SExpr {
530 static bool classof(const SExpr *E) { return E->opcode() == COP_SFunction; }
532 SFunction(Variable *Vd, SExpr *B)
533 : SExpr(COP_SFunction), VarDecl(Vd), Body(B) {
534 assert(Vd->Definition == nullptr);
535 Vd->setKind(Variable::VK_SFun);
536 Vd->Definition.reset(this);
538 SFunction(const SFunction &F, Variable *Vd, SExpr *B) // rewrite constructor
542 assert(Vd->Definition == nullptr);
543 Vd->setKind(Variable::VK_SFun);
544 Vd->Definition.reset(this);
547 Variable *variableDecl() { return VarDecl; }
548 const Variable *variableDecl() const { return VarDecl; }
550 SExpr *body() { return Body.get(); }
551 const SExpr *body() const { return Body.get(); }
553 template <class V> typename V::R_SExpr traverse(V &Visitor) {
554 // A self-variable points to the SFunction itself.
555 // A rewrite must introduce the variable with a null definition, and update
556 // it after 'this' has been rewritten.
557 Variable *Nvd = Visitor.enterScope(*VarDecl, nullptr /* def */);
558 typename V::R_SExpr E1 = Visitor.traverse(Body);
559 Visitor.exitScope(*VarDecl);
560 // A rewrite operation will call SFun constructor to set Vvd->Definition.
561 return Visitor.reduceSFunction(*this, Nvd, E1);
564 template <class C> typename C::CType compare(SFunction* E, C& Cmp) {
565 Cmp.enterScope(variableDecl(), E->variableDecl());
566 typename C::CType Ct = Cmp.compare(body(), E->body());
577 // A block of code -- e.g. the body of a function.
578 class Code : public SExpr {
580 static bool classof(const SExpr *E) { return E->opcode() == COP_Code; }
582 Code(SExpr *T, SExpr *B) : SExpr(COP_Code), ReturnType(T), Body(B) {}
583 Code(const Code &C, SExpr *T, SExpr *B) // rewrite constructor
584 : SExpr(C), ReturnType(T), Body(B) {}
586 SExpr *returnType() { return ReturnType.get(); }
587 const SExpr *returnType() const { return ReturnType.get(); }
589 SExpr *body() { return Body.get(); }
590 const SExpr *body() const { return Body.get(); }
592 template <class V> typename V::R_SExpr traverse(V &Visitor) {
593 typename V::R_SExpr Nt = Visitor.traverse(ReturnType, TRV_Lazy);
594 typename V::R_SExpr Nb = Visitor.traverse(Body, TRV_Lazy);
595 return Visitor.reduceCode(*this, Nt, Nb);
598 template <class C> typename C::CType compare(Code* E, C& Cmp) {
599 typename C::CType Ct = Cmp.compare(returnType(), E->returnType());
602 return Cmp.compare(body(), E->body());
611 // Apply an argument to a function
612 class Apply : public SExpr {
614 static bool classof(const SExpr *E) { return E->opcode() == COP_Apply; }
616 Apply(SExpr *F, SExpr *A) : SExpr(COP_Apply), Fun(F), Arg(A) {}
617 Apply(const Apply &A, SExpr *F, SExpr *Ar) // rewrite constructor
618 : SExpr(A), Fun(F), Arg(Ar)
621 SExpr *fun() { return Fun.get(); }
622 const SExpr *fun() const { return Fun.get(); }
624 SExpr *arg() { return Arg.get(); }
625 const SExpr *arg() const { return Arg.get(); }
627 template <class V> typename V::R_SExpr traverse(V &Visitor) {
628 typename V::R_SExpr Nf = Visitor.traverse(Fun);
629 typename V::R_SExpr Na = Visitor.traverse(Arg);
630 return Visitor.reduceApply(*this, Nf, Na);
633 template <class C> typename C::CType compare(Apply* E, C& Cmp) {
634 typename C::CType Ct = Cmp.compare(fun(), E->fun());
637 return Cmp.compare(arg(), E->arg());
646 // Apply a self-argument to a self-applicable function
647 class SApply : public SExpr {
649 static bool classof(const SExpr *E) { return E->opcode() == COP_SApply; }
651 SApply(SExpr *Sf, SExpr *A = nullptr)
652 : SExpr(COP_SApply), Sfun(Sf), Arg(A)
654 SApply(SApply &A, SExpr *Sf, SExpr *Ar = nullptr) // rewrite constructor
655 : SExpr(A), Sfun(Sf), Arg(Ar)
658 SExpr *sfun() { return Sfun.get(); }
659 const SExpr *sfun() const { return Sfun.get(); }
661 SExpr *arg() { return Arg.get() ? Arg.get() : Sfun.get(); }
662 const SExpr *arg() const { return Arg.get() ? Arg.get() : Sfun.get(); }
664 bool isDelegation() const { return Arg == nullptr; }
666 template <class V> typename V::R_SExpr traverse(V &Visitor) {
667 typename V::R_SExpr Nf = Visitor.traverse(Sfun);
668 typename V::R_SExpr Na = Arg.get() ? Visitor.traverse(Arg) : nullptr;
669 return Visitor.reduceSApply(*this, Nf, Na);
672 template <class C> typename C::CType compare(SApply* E, C& Cmp) {
673 typename C::CType Ct = Cmp.compare(sfun(), E->sfun());
674 if (Cmp.notTrue(Ct) || (!arg() && !E->arg()))
676 return Cmp.compare(arg(), E->arg());
685 // Project a named slot from a C++ struct or class.
686 class Project : public SExpr {
688 static bool classof(const SExpr *E) { return E->opcode() == COP_Project; }
690 Project(SExpr *R, clang::ValueDecl *Cvd)
691 : SExpr(COP_Project), Rec(R), Cvdecl(Cvd) {}
692 Project(const Project &P, SExpr *R) : SExpr(P), Rec(R), Cvdecl(P.Cvdecl) {}
694 SExpr *record() { return Rec.get(); }
695 const SExpr *record() const { return Rec.get(); }
697 const clang::ValueDecl *clangValueDecl() const { return Cvdecl; }
699 StringRef slotName() const { return Cvdecl->getName(); }
701 template <class V> typename V::R_SExpr traverse(V &Visitor) {
702 typename V::R_SExpr Nr = Visitor.traverse(Rec);
703 return Visitor.reduceProject(*this, Nr);
706 template <class C> typename C::CType compare(Project* E, C& Cmp) {
707 typename C::CType Ct = Cmp.compare(record(), E->record());
710 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
715 clang::ValueDecl *Cvdecl;
719 // Call a function (after all arguments have been applied).
720 class Call : public SExpr {
722 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
724 Call(SExpr *T, const clang::CallExpr *Ce = nullptr)
725 : SExpr(COP_Call), Target(T), Cexpr(Ce) {}
726 Call(const Call &C, SExpr *T) : SExpr(C), Target(T), Cexpr(C.Cexpr) {}
728 SExpr *target() { return Target.get(); }
729 const SExpr *target() const { return Target.get(); }
731 const clang::CallExpr *clangCallExpr() { return Cexpr; }
733 template <class V> typename V::R_SExpr traverse(V &Visitor) {
734 typename V::R_SExpr Nt = Visitor.traverse(Target);
735 return Visitor.reduceCall(*this, Nt);
738 template <class C> typename C::CType compare(Call* E, C& Cmp) {
739 return Cmp.compare(target(), E->target());
744 const clang::CallExpr *Cexpr;
748 // Allocate memory for a new value on the heap or stack.
749 class Alloc : public SExpr {
751 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
758 Alloc(SExpr* D, AllocKind K) : SExpr(COP_Alloc), Dtype(D) {
761 Alloc(const Alloc &A, SExpr* Dt) : SExpr(A), Dtype(Dt) {
765 AllocKind kind() const { return static_cast<AllocKind>(Flags); }
767 SExpr* dataType() { return Dtype.get(); }
768 const SExpr* dataType() const { return Dtype.get(); }
770 template <class V> typename V::R_SExpr traverse(V &Visitor) {
771 typename V::R_SExpr Nd = Visitor.traverse(Dtype);
772 return Visitor.reduceAlloc(*this, Nd);
775 template <class C> typename C::CType compare(Alloc* E, C& Cmp) {
776 typename C::CType Ct = Cmp.compareIntegers(kind(), E->kind());
779 return Cmp.compare(dataType(), E->dataType());
787 // Load a value from memory.
788 class Load : public SExpr {
790 static bool classof(const SExpr *E) { return E->opcode() == COP_Load; }
792 Load(SExpr *P) : SExpr(COP_Load), Ptr(P) {}
793 Load(const Load &L, SExpr *P) : SExpr(L), Ptr(P) {}
795 SExpr *pointer() { return Ptr.get(); }
796 const SExpr *pointer() const { return Ptr.get(); }
798 template <class V> typename V::R_SExpr traverse(V &Visitor) {
799 typename V::R_SExpr Np = Visitor.traverse(Ptr);
800 return Visitor.reduceLoad(*this, Np);
803 template <class C> typename C::CType compare(Load* E, C& Cmp) {
804 return Cmp.compare(pointer(), E->pointer());
812 // Store a value to memory.
813 class Store : public SExpr {
815 static bool classof(const SExpr *E) { return E->opcode() == COP_Store; }
817 Store(SExpr *P, SExpr *V) : SExpr(COP_Store), Dest(P), Source(V) {}
818 Store(const Store &S, SExpr *P, SExpr *V) : SExpr(S), Dest(P), Source(V) {}
820 SExpr *destination() { return Dest.get(); } // Address to store to
821 const SExpr *destination() const { return Dest.get(); }
823 SExpr *source() { return Source.get(); } // Value to store
824 const SExpr *source() const { return Source.get(); }
826 template <class V> typename V::R_SExpr traverse(V &Visitor) {
827 typename V::R_SExpr Np = Visitor.traverse(Dest);
828 typename V::R_SExpr Nv = Visitor.traverse(Source);
829 return Visitor.reduceStore(*this, Np, Nv);
832 template <class C> typename C::CType compare(Store* E, C& Cmp) {
833 typename C::CType Ct = Cmp.compare(destination(), E->destination());
836 return Cmp.compare(source(), E->source());
844 // Simple unary operation -- e.g. !, ~, etc.
845 class UnaryOp : public SExpr {
847 static bool classof(const SExpr *E) { return E->opcode() == COP_UnaryOp; }
849 UnaryOp(TIL_UnaryOpcode Op, SExpr *E) : SExpr(COP_UnaryOp), Expr0(E) {
852 UnaryOp(const UnaryOp &U, SExpr *E) : SExpr(U) { Flags = U.Flags; }
854 TIL_UnaryOpcode unaryOpcode() { return static_cast<TIL_UnaryOpcode>(Flags); }
856 SExpr *expr() { return Expr0.get(); }
857 const SExpr *expr() const { return Expr0.get(); }
859 template <class V> typename V::R_SExpr traverse(V &Visitor) {
860 typename V::R_SExpr Ne = Visitor.traverse(Expr0);
861 return Visitor.reduceUnaryOp(*this, Ne);
864 template <class C> typename C::CType compare(UnaryOp* E, C& Cmp) {
865 typename C::CType Ct =
866 Cmp.compareIntegers(unaryOpcode(), E->unaryOpcode());
869 return Cmp.compare(expr(), E->expr());
877 // Simple binary operation -- e.g. +, -, etc.
878 class BinaryOp : public SExpr {
880 static bool classof(const SExpr *E) { return E->opcode() == COP_BinaryOp; }
882 BinaryOp(TIL_BinaryOpcode Op, SExpr *E0, SExpr *E1)
883 : SExpr(COP_BinaryOp), Expr0(E0), Expr1(E1) {
886 BinaryOp(const BinaryOp &B, SExpr *E0, SExpr *E1)
887 : SExpr(B), Expr0(E0), Expr1(E1) {
891 TIL_BinaryOpcode binaryOpcode() {
892 return static_cast<TIL_BinaryOpcode>(Flags);
895 SExpr *expr0() { return Expr0.get(); }
896 const SExpr *expr0() const { return Expr0.get(); }
898 SExpr *expr1() { return Expr1.get(); }
899 const SExpr *expr1() const { return Expr1.get(); }
901 template <class V> typename V::R_SExpr traverse(V &Visitor) {
902 typename V::R_SExpr Ne0 = Visitor.traverse(Expr0);
903 typename V::R_SExpr Ne1 = Visitor.traverse(Expr1);
904 return Visitor.reduceBinaryOp(*this, Ne0, Ne1);
907 template <class C> typename C::CType compare(BinaryOp* E, C& Cmp) {
908 typename C::CType Ct =
909 Cmp.compareIntegers(binaryOpcode(), E->binaryOpcode());
912 Ct = Cmp.compare(expr0(), E->expr0());
915 return Cmp.compare(expr1(), E->expr1());
925 class Cast : public SExpr {
927 static bool classof(const SExpr *E) { return E->opcode() == COP_Cast; }
929 Cast(TIL_CastOpcode Op, SExpr *E) : SExpr(COP_Cast), Expr0(E) { Flags = Op; }
930 Cast(const Cast &C, SExpr *E) : SExpr(C), Expr0(E) { Flags = C.Flags; }
932 TIL_BinaryOpcode castOpcode() {
933 return static_cast<TIL_BinaryOpcode>(Flags);
936 SExpr *expr() { return Expr0.get(); }
937 const SExpr *expr() const { return Expr0.get(); }
939 template <class V> typename V::R_SExpr traverse(V &Visitor) {
940 typename V::R_SExpr Ne = Visitor.traverse(Expr0);
941 return Visitor.reduceCast(*this, Ne);
944 template <class C> typename C::CType compare(Cast* E, C& Cmp) {
945 typename C::CType Ct =
946 Cmp.compareIntegers(castOpcode(), E->castOpcode());
949 return Cmp.compare(expr(), E->expr());
962 // An SCFG is a control-flow graph. It consists of a set of basic blocks, each
963 // of which terminates in a branch to another basic block. There is one
964 // entry point, and one exit point.
965 class SCFG : public SExpr {
967 typedef SimpleArray<BasicBlock*> BlockArray;
969 static bool classof(const SExpr *E) { return E->opcode() == COP_SCFG; }
971 SCFG(MemRegionRef A, unsigned Nblocks)
972 : SExpr(COP_SCFG), Blocks(A, Nblocks), Entry(nullptr), Exit(nullptr) {}
973 SCFG(const SCFG &Cfg, BlockArray &&Ba) // steals memory from Ba
975 Blocks(std::move(Ba)),
978 // TODO: set entry and exit!
981 typedef BlockArray::iterator iterator;
982 typedef BlockArray::const_iterator const_iterator;
984 iterator begin() { return Blocks.begin(); }
985 iterator end() { return Blocks.end(); }
987 const_iterator cbegin() const { return Blocks.cbegin(); }
988 const_iterator cend() const { return Blocks.cend(); }
990 BasicBlock *entry() const { return Entry; }
991 BasicBlock *exit() const { return Exit; }
993 void add(BasicBlock *BB) { Blocks.push_back(BB); }
994 void setEntry(BasicBlock *BB) { Entry = BB; }
995 void setExit(BasicBlock *BB) { Exit = BB; }
997 template <class V> typename V::R_SExpr traverse(V &Visitor);
999 template <class C> typename C::CType compare(SCFG* E, C& Cmp) {
1000 // TODO -- implement CFG comparisons
1001 return Cmp.comparePointers(this, E);
1011 // A basic block is part of an SCFG, and can be treated as a function in
1012 // continuation passing style. It consists of a sequence of phi nodes, which
1013 // are "arguments" to the function, followed by a sequence of instructions.
1014 // Both arguments and instructions define new variables. It ends with a
1015 // branch or goto to another basic block in the same SCFG.
1018 typedef SimpleArray<Variable*> VarArray;
1020 BasicBlock(MemRegionRef A, unsigned Nargs, unsigned Nins,
1021 SExpr *Term = nullptr)
1022 : BlockID(0), Parent(nullptr), Args(A, Nargs), Instrs(A, Nins),
1024 BasicBlock(const BasicBlock &B, VarArray &&As, VarArray &&Is, SExpr *T)
1025 : BlockID(0), Parent(nullptr),
1026 Args(std::move(As)), Instrs(std::move(Is)), Terminator(T)
1029 unsigned blockID() const { return BlockID; }
1030 BasicBlock *parent() const { return Parent; }
1032 const VarArray &arguments() const { return Args; }
1033 VarArray &arguments() { return Args; }
1035 const VarArray &instructions() const { return Instrs; }
1036 VarArray &instructions() { return Instrs; }
1038 const SExpr *terminator() const { return Terminator.get(); }
1039 SExpr *terminator() { return Terminator.get(); }
1041 void setParent(BasicBlock *P) { Parent = P; }
1042 void setBlockID(unsigned i) { BlockID = i; }
1043 void setTerminator(SExpr *E) { Terminator.reset(E); }
1044 void addArgument(Variable *V) { Args.push_back(V); }
1045 void addInstr(Variable *V) { Args.push_back(V); }
1047 template <class V> BasicBlock *traverse(V &Visitor) {
1048 typename V::template Container<Variable*> Nas(Visitor, Args.size());
1049 typename V::template Container<Variable*> Nis(Visitor, Instrs.size());
1051 for (auto A : Args) {
1052 typename V::R_SExpr Ne = Visitor.traverse(A->Definition);
1053 Variable *Nvd = Visitor.enterScope(*A, Ne);
1056 for (auto I : Instrs) {
1057 typename V::R_SExpr Ne = Visitor.traverse(I->Definition);
1058 Variable *Nvd = Visitor.enterScope(*I, Ne);
1061 typename V::R_SExpr Nt = Visitor.traverse(Terminator);
1063 // TODO: use reverse iterator
1064 for (unsigned J = 0, JN = Instrs.size(); J < JN; ++J)
1065 Visitor.exitScope(*Instrs[JN-J]);
1066 for (unsigned I = 0, IN = Instrs.size(); I < IN; ++I)
1067 Visitor.exitScope(*Args[IN-I]);
1069 return Visitor.reduceBasicBlock(*this, Nas, Nis, Nt);
1072 template <class C> typename C::CType compare(BasicBlock* E, C& Cmp) {
1073 // TODO: implement CFG comparisons
1074 return Cmp.comparePointers(this, E);
1081 BasicBlock *Parent; // The parent block is the enclosing lexical scope.
1082 // The parent dominates this block.
1083 VarArray Args; // Phi nodes
1085 SExprRef Terminator;
1090 typename V::R_SExpr SCFG::traverse(V &Visitor) {
1091 Visitor.enterCFG(*this);
1092 typename V::template Container<BasicBlock *> Bbs(Visitor, Blocks.size());
1093 for (auto B : Blocks) {
1094 BasicBlock *Nbb = B->traverse(Visitor);
1097 Visitor.exitCFG(*this);
1098 return Visitor.reduceSCFG(*this, Bbs);
1103 class Phi : public SExpr {
1105 // TODO: change to SExprRef
1106 typedef SimpleArray<SExpr*> ValArray;
1108 static bool classof(const SExpr *E) { return E->opcode() == COP_Phi; }
1110 Phi(MemRegionRef A, unsigned Nvals)
1111 : SExpr(COP_Phi), Values(A, Nvals)
1113 Phi(const Phi &P, ValArray &&Vs) // steals memory of Vs
1114 : SExpr(COP_Phi), Values(std::move(Vs))
1117 const ValArray &values() const { return Values; }
1118 ValArray &values() { return Values; }
1120 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1121 typename V::template Container<typename V::R_SExpr> Nvs(Visitor,
1123 for (auto Val : Values) {
1124 typename V::R_SExpr Nv = Visitor.traverse(Val);
1127 return Visitor.reducePhi(*this, Nvs);
1130 template <class C> typename C::CType compare(Phi* E, C& Cmp) {
1131 // TODO -- implement CFG comparisons
1132 return Cmp.comparePointers(this, E);
1140 class Goto : public SExpr {
1142 static bool classof(const SExpr *E) { return E->opcode() == COP_Goto; }
1144 Goto(BasicBlock *B, unsigned Index)
1145 : SExpr(COP_Goto), TargetBlock(B), Index(0) {}
1146 Goto(const Goto &G, BasicBlock *B, unsigned I)
1147 : SExpr(COP_Goto), TargetBlock(B), Index(I) {}
1149 BasicBlock *targetBlock() const { return TargetBlock; }
1150 unsigned index() const { return Index; }
1152 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1153 // TODO -- rewrite indices properly
1154 BasicBlock *Ntb = Visitor.reduceBasicBlockRef(TargetBlock);
1155 return Visitor.reduceGoto(*this, Ntb, Index);
1158 template <class C> typename C::CType compare(Goto* E, C& Cmp) {
1159 // TODO -- implement CFG comparisons
1160 return Cmp.comparePointers(this, E);
1164 BasicBlock *TargetBlock;
1165 unsigned Index; // Index into Phi nodes of target block.
1169 class Branch : public SExpr {
1171 static bool classof(const SExpr *E) { return E->opcode() == COP_Branch; }
1173 Branch(SExpr *C, BasicBlock *T, BasicBlock *E)
1174 : SExpr(COP_Branch), Condition(C), ThenBlock(T), ElseBlock(E) {}
1175 Branch(const Branch &Br, SExpr *C, BasicBlock *T, BasicBlock *E)
1176 : SExpr(COP_Branch), Condition(C), ThenBlock(T), ElseBlock(E) {}
1178 SExpr *condition() { return Condition; }
1179 BasicBlock *thenBlock() { return ThenBlock; }
1180 BasicBlock *elseBlock() { return ElseBlock; }
1182 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1183 typename V::R_SExpr Nc = Visitor.traverse(Condition);
1184 BasicBlock *Ntb = Visitor.reduceBasicBlockRef(ThenBlock);
1185 BasicBlock *Nte = Visitor.reduceBasicBlockRef(ElseBlock);
1186 return Visitor.reduceBranch(*this, Nc, Ntb, Nte);
1189 template <class C> typename C::CType compare(Branch* E, C& Cmp) {
1190 // TODO -- implement CFG comparisons
1191 return Cmp.comparePointers(this, E);
1196 BasicBlock *ThenBlock;
1197 BasicBlock *ElseBlock;
1201 } // end namespace til
1202 } // end namespace threadSafety
1203 } // end namespace clang
1205 #endif // LLVM_CLANG_THREAD_SAFETY_TIL_H