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"
49 #include "llvm/Support/Compiler.h"
57 namespace threadSafety {
60 using llvm::StringRef;
61 using clang::SourceLocation;
65 #define TIL_OPCODE_DEF(X) COP_##X,
66 #include "clang/Analysis/Analyses/ThreadSafetyOps.def"
72 typedef clang::BinaryOperatorKind TIL_BinaryOpcode;
73 typedef clang::UnaryOperatorKind TIL_UnaryOpcode;
74 typedef clang::CastKind TIL_CastOpcode;
79 TRV_Lazy, // subexpression may need to be traversed lazily
80 TRV_Tail // subexpression occurs in a tail position
84 // Base class for AST nodes in the typed intermediate language.
87 TIL_Opcode opcode() const { return static_cast<TIL_Opcode>(Opcode); }
89 // Subclasses of SExpr must define the following:
91 // This(const This& E, ...) {
92 // copy constructor: construct copy of E, with some additional arguments.
95 // template <class V> typename V::R_SExpr traverse(V &Visitor) {
96 // traverse all subexpressions, following the traversal/rewriter interface
99 // template <class C> typename C::CType compare(CType* E, C& Cmp) {
100 // compare all subexpressions, following the comparator interface
103 void *operator new(size_t S, clang::threadSafety::til::MemRegionRef &R) {
104 return ::operator new(S, R);
108 SExpr(TIL_Opcode Op) : Opcode(Op), Reserved(0), Flags(0) {}
109 SExpr(const SExpr &E) : Opcode(E.Opcode), Reserved(0), Flags(E.Flags) {}
111 const unsigned char Opcode;
112 unsigned char Reserved;
113 unsigned short Flags;
116 SExpr() LLVM_DELETED_FUNCTION;
118 // SExpr objects must be created in an arena and cannot be deleted.
119 void *operator new(size_t) LLVM_DELETED_FUNCTION;
120 void operator delete(void *) LLVM_DELETED_FUNCTION;
124 // Class for owning references to SExprs.
125 // Includes attach/detach logic for counting variable references and lazy
126 // rewriting strategies.
129 SExprRef() : Ptr(nullptr) { }
130 SExprRef(std::nullptr_t P) : Ptr(nullptr) { }
131 SExprRef(SExprRef &&R) : Ptr(R.Ptr) { R.Ptr = nullptr; }
133 // Defined after Variable and Future, below.
134 inline SExprRef(SExpr *P);
137 SExpr *get() { return Ptr; }
138 const SExpr *get() const { return Ptr; }
140 SExpr *operator->() { return get(); }
141 const SExpr *operator->() const { return get(); }
143 SExpr &operator*() { return *Ptr; }
144 const SExpr &operator*() const { return *Ptr; }
146 bool operator==(const SExprRef &R) const { return Ptr == R.Ptr; }
147 bool operator!=(const SExprRef &R) const { return !operator==(R); }
148 bool operator==(const SExpr *P) const { return Ptr == P; }
149 bool operator!=(const SExpr *P) const { return !operator==(P); }
150 bool operator==(std::nullptr_t) const { return Ptr == nullptr; }
151 bool operator!=(std::nullptr_t) const { return Ptr != nullptr; }
153 inline void reset(SExpr *E);
156 inline void attach();
157 inline void detach();
163 // Contains various helper functions for SExprs.
164 namespace ThreadSafetyTIL {
165 inline bool isTrivial(SExpr *E) {
166 unsigned Op = E->opcode();
167 return Op == COP_Variable || Op == COP_Literal || Op == COP_LiteralPtr;
176 // A named variable, e.g. "x".
178 // There are two distinct places in which a Variable can appear in the AST.
179 // A variable declaration introduces a new variable, and can occur in 3 places:
180 // Let-expressions: (Let (x = t) u)
181 // Functions: (Function (x : t) u)
182 // Self-applicable functions (SFunction (x) t)
184 // If a variable occurs in any other location, it is a reference to an existing
185 // variable declaration -- e.g. 'x' in (x * y + z). To save space, we don't
186 // allocate a separate AST node for variable references; a reference is just a
187 // pointer to the original declaration.
188 class Variable : public SExpr {
190 static bool classof(const SExpr *E) { return E->opcode() == COP_Variable; }
192 // Let-variable, function parameter, or self-variable
199 // These are defined after SExprRef contructor, below
200 inline Variable(VariableKind K, SExpr *D = nullptr,
201 const clang::ValueDecl *Cvd = nullptr);
202 inline Variable(const clang::ValueDecl *Cvd, SExpr *D = nullptr);
203 inline Variable(const Variable &Vd, SExpr *D);
205 VariableKind kind() const { return static_cast<VariableKind>(Flags); }
207 const StringRef name() const { return Cvdecl ? Cvdecl->getName() : "_x"; }
208 const clang::ValueDecl *clangDecl() const { return Cvdecl; }
210 // Returns the definition (for let vars) or type (for parameter & self vars)
211 SExpr *definition() { return Definition.get(); }
213 void attachVar() const { ++NumUses; }
214 void detachVar() const { assert(NumUses > 0); --NumUses; }
216 unsigned getID() const { return Id; }
217 unsigned getBlockID() const { return BlockID; }
219 void setID(unsigned Bid, unsigned I) {
220 BlockID = static_cast<unsigned short>(Bid);
221 Id = static_cast<unsigned short>(I);
224 template <class V> typename V::R_SExpr traverse(V &Visitor) {
225 // This routine is only called for variable references.
226 return Visitor.reduceVariableRef(this);
229 template <class C> typename C::CType compare(Variable* E, C& Cmp) {
230 return Cmp.compareVariableRefs(this, E);
234 friend class Function;
235 friend class SFunction;
236 friend class BasicBlock;
238 // Function, SFunction, and BasicBlock will reset the kind.
239 void setKind(VariableKind K) { Flags = K; }
241 SExprRef Definition; // The TIL type or definition
242 const clang::ValueDecl *Cvdecl; // The clang declaration for this variable.
244 unsigned short BlockID;
246 mutable unsigned NumUses;
250 // Placeholder for an expression that has not yet been created.
251 // Used to implement lazy copy and rewriting strategies.
252 class Future : public SExpr {
254 static bool classof(const SExpr *E) { return E->opcode() == COP_Future; }
263 SExpr(COP_Future), Status(FS_pending), Result(nullptr), Location(nullptr)
266 virtual ~Future() LLVM_DELETED_FUNCTION;
269 // Registers the location in the AST where this future is stored.
270 // Forcing the future will automatically update the AST.
271 static inline void registerLocation(SExprRef *Member) {
272 if (Future *F = dyn_cast_or_null<Future>(Member->get()))
273 F->Location = Member;
276 // A lazy rewriting strategy should subclass Future and override this method.
277 virtual SExpr *create() { return nullptr; }
279 // Return the result of this future if it exists, otherwise return null.
280 SExpr *maybeGetResult() {
284 // Return the result of this future; forcing it if necessary.
291 return nullptr; // infinite loop; illegal recursion.
297 template <class V> typename V::R_SExpr traverse(V &Visitor) {
298 assert(Result && "Cannot traverse Future that has not been forced.");
299 return Visitor.traverse(Result);
302 template <class C> typename C::CType compare(Future* E, C& Cmp) {
303 if (!Result || !E->Result)
304 return Cmp.comparePointers(this, E);
305 return Cmp.compare(Result, E->Result);
317 void SExprRef::attach() {
321 TIL_Opcode Op = Ptr->opcode();
322 if (Op == COP_Variable) {
323 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) {
339 SExprRef::~SExprRef() {
343 void SExprRef::reset(SExpr *P) {
350 Variable::Variable(VariableKind K, SExpr *D, const clang::ValueDecl *Cvd)
351 : SExpr(COP_Variable), Definition(D), Cvdecl(Cvd),
352 BlockID(0), Id(0), NumUses(0) {
356 Variable::Variable(const clang::ValueDecl *Cvd, SExpr *D)
357 : SExpr(COP_Variable), Definition(D), Cvdecl(Cvd),
358 BlockID(0), Id(0), NumUses(0) {
362 Variable::Variable(const Variable &Vd, SExpr *D) // rewrite constructor
363 : SExpr(Vd), Definition(D), Cvdecl(Vd.Cvdecl),
364 BlockID(0), Id(0), NumUses(0) {
368 void Future::force() {
369 Status = FS_evaluating;
377 // Placeholder for C++ expressions that cannot be represented in the TIL.
378 class Undefined : public SExpr {
380 static bool classof(const SExpr *E) { return E->opcode() == COP_Undefined; }
382 Undefined(const clang::Stmt *S = nullptr) : SExpr(COP_Undefined), Cstmt(S) {}
383 Undefined(const Undefined &U) : SExpr(U), Cstmt(U.Cstmt) {}
385 template <class V> typename V::R_SExpr traverse(V &Visitor) {
386 return Visitor.reduceUndefined(*this);
389 template <class C> typename C::CType compare(Undefined* E, C& Cmp) {
390 return Cmp.comparePointers(Cstmt, E->Cstmt);
394 const clang::Stmt *Cstmt;
398 // Placeholder for a wildcard that matches any other expression.
399 class Wildcard : public SExpr {
401 static bool classof(const SExpr *E) { return E->opcode() == COP_Wildcard; }
403 Wildcard() : SExpr(COP_Wildcard) {}
404 Wildcard(const Wildcard &W) : SExpr(W) {}
406 template <class V> typename V::R_SExpr traverse(V &Visitor) {
407 return Visitor.reduceWildcard(*this);
410 template <class C> typename C::CType compare(Wildcard* E, C& Cmp) {
411 return Cmp.trueResult();
416 // Base class for literal values.
417 class Literal : public SExpr {
419 static bool classof(const SExpr *E) { return E->opcode() == COP_Literal; }
421 Literal(const clang::Expr *C) : SExpr(COP_Literal), Cexpr(C) {}
422 Literal(const Literal &L) : SExpr(L), Cexpr(L.Cexpr) {}
424 // The clang expression for this literal.
425 const clang::Expr *clangExpr() const { return Cexpr; }
427 template <class V> typename V::R_SExpr traverse(V &Visitor) {
428 return Visitor.reduceLiteral(*this);
431 template <class C> typename C::CType compare(Literal* E, C& Cmp) {
432 // TODO -- use value, not pointer equality
433 return Cmp.comparePointers(Cexpr, E->Cexpr);
437 const clang::Expr *Cexpr;
440 // Literal pointer to an object allocated in memory.
441 // At compile time, pointer literals are represented by symbolic names.
442 class LiteralPtr : public SExpr {
444 static bool classof(const SExpr *E) { return E->opcode() == COP_LiteralPtr; }
446 LiteralPtr(const clang::ValueDecl *D) : SExpr(COP_LiteralPtr), Cvdecl(D) {}
447 LiteralPtr(const LiteralPtr &R) : SExpr(R), Cvdecl(R.Cvdecl) {}
449 // The clang declaration for the value that this pointer points to.
450 const clang::ValueDecl *clangDecl() const { return Cvdecl; }
452 template <class V> typename V::R_SExpr traverse(V &Visitor) {
453 return Visitor.reduceLiteralPtr(*this);
456 template <class C> typename C::CType compare(LiteralPtr* E, C& Cmp) {
457 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
461 const clang::ValueDecl *Cvdecl;
464 // A function -- a.k.a. lambda abstraction.
465 // Functions with multiple arguments are created by currying,
466 // e.g. (function (x: Int) (function (y: Int) (add x y)))
467 class Function : public SExpr {
469 static bool classof(const SExpr *E) { return E->opcode() == COP_Function; }
471 Function(Variable *Vd, SExpr *Bd)
472 : SExpr(COP_Function), VarDecl(Vd), Body(Bd) {
473 Vd->setKind(Variable::VK_Fun);
475 Function(const Function &F, Variable *Vd, SExpr *Bd) // rewrite constructor
476 : SExpr(F), VarDecl(Vd), Body(Bd) {
477 Vd->setKind(Variable::VK_Fun);
480 Variable *variableDecl() { return VarDecl; }
481 const Variable *variableDecl() const { return VarDecl; }
483 SExpr *body() { return Body.get(); }
484 const SExpr *body() const { return Body.get(); }
486 template <class V> typename V::R_SExpr traverse(V &Visitor) {
487 // This is a variable declaration, so traverse the definition.
488 typename V::R_SExpr E0 = Visitor.traverse(VarDecl->Definition, TRV_Lazy);
489 // Tell the rewriter to enter the scope of the function.
490 Variable *Nvd = Visitor.enterScope(*VarDecl, E0);
491 typename V::R_SExpr E1 = Visitor.traverse(Body);
492 Visitor.exitScope(*VarDecl);
493 return Visitor.reduceFunction(*this, Nvd, E1);
496 template <class C> typename C::CType compare(Function* E, C& Cmp) {
497 typename C::CType Ct =
498 Cmp.compare(VarDecl->definition(), E->VarDecl->definition());
501 Cmp.enterScope(variableDecl(), E->variableDecl());
502 Ct = Cmp.compare(body(), E->body());
513 // A self-applicable function.
514 // A self-applicable function can be applied to itself. It's useful for
515 // implementing objects and late binding
516 class SFunction : public SExpr {
518 static bool classof(const SExpr *E) { return E->opcode() == COP_SFunction; }
520 SFunction(Variable *Vd, SExpr *B)
521 : SExpr(COP_SFunction), VarDecl(Vd), Body(B) {
522 assert(Vd->Definition == nullptr);
523 Vd->setKind(Variable::VK_SFun);
524 Vd->Definition.reset(this);
526 SFunction(const SFunction &F, Variable *Vd, SExpr *B) // rewrite constructor
527 : SExpr(F), VarDecl(Vd), Body(B) {
528 assert(Vd->Definition == nullptr);
529 Vd->setKind(Variable::VK_SFun);
530 Vd->Definition.reset(this);
533 Variable *variableDecl() { return VarDecl; }
534 const Variable *variableDecl() const { return VarDecl; }
536 SExpr *body() { return Body.get(); }
537 const SExpr *body() const { return Body.get(); }
539 template <class V> typename V::R_SExpr traverse(V &Visitor) {
540 // A self-variable points to the SFunction itself.
541 // A rewrite must introduce the variable with a null definition, and update
542 // it after 'this' has been rewritten.
543 Variable *Nvd = Visitor.enterScope(*VarDecl, nullptr /* def */);
544 typename V::R_SExpr E1 = Visitor.traverse(Body);
545 Visitor.exitScope(*VarDecl);
546 // A rewrite operation will call SFun constructor to set Vvd->Definition.
547 return Visitor.reduceSFunction(*this, Nvd, E1);
550 template <class C> typename C::CType compare(SFunction* E, C& Cmp) {
551 Cmp.enterScope(variableDecl(), E->variableDecl());
552 typename C::CType Ct = Cmp.compare(body(), E->body());
563 // A block of code -- e.g. the body of a function.
564 class Code : public SExpr {
566 static bool classof(const SExpr *E) { return E->opcode() == COP_Code; }
568 Code(SExpr *T, SExpr *B) : SExpr(COP_Code), ReturnType(T), Body(B) {}
569 Code(const Code &C, SExpr *T, SExpr *B) // rewrite constructor
570 : SExpr(C), ReturnType(T), Body(B) {}
572 SExpr *returnType() { return ReturnType.get(); }
573 const SExpr *returnType() const { return ReturnType.get(); }
575 SExpr *body() { return Body.get(); }
576 const SExpr *body() const { return Body.get(); }
578 template <class V> typename V::R_SExpr traverse(V &Visitor) {
579 typename V::R_SExpr Nt = Visitor.traverse(ReturnType, TRV_Lazy);
580 typename V::R_SExpr Nb = Visitor.traverse(Body, TRV_Lazy);
581 return Visitor.reduceCode(*this, Nt, Nb);
584 template <class C> typename C::CType compare(Code* E, C& Cmp) {
585 typename C::CType Ct = Cmp.compare(returnType(), E->returnType());
588 return Cmp.compare(body(), E->body());
597 // Apply an argument to a function
598 class Apply : public SExpr {
600 static bool classof(const SExpr *E) { return E->opcode() == COP_Apply; }
602 Apply(SExpr *F, SExpr *A) : SExpr(COP_Apply), Fun(F), Arg(A) {}
603 Apply(const Apply &A, SExpr *F, SExpr *Ar) // rewrite constructor
604 : SExpr(A), Fun(F), Arg(Ar)
607 SExpr *fun() { return Fun.get(); }
608 const SExpr *fun() const { return Fun.get(); }
610 SExpr *arg() { return Arg.get(); }
611 const SExpr *arg() const { return Arg.get(); }
613 template <class V> typename V::R_SExpr traverse(V &Visitor) {
614 typename V::R_SExpr Nf = Visitor.traverse(Fun);
615 typename V::R_SExpr Na = Visitor.traverse(Arg);
616 return Visitor.reduceApply(*this, Nf, Na);
619 template <class C> typename C::CType compare(Apply* E, C& Cmp) {
620 typename C::CType Ct = Cmp.compare(fun(), E->fun());
623 return Cmp.compare(arg(), E->arg());
632 // Apply a self-argument to a self-applicable function
633 class SApply : public SExpr {
635 static bool classof(const SExpr *E) { return E->opcode() == COP_SApply; }
637 SApply(SExpr *Sf, SExpr *A = nullptr) : SExpr(COP_SApply), Sfun(Sf), Arg(A) {}
638 SApply(SApply &A, SExpr *Sf, SExpr *Ar = nullptr) // rewrite constructor
639 : SExpr(A), Sfun(Sf), Arg(Ar) {}
641 SExpr *sfun() { return Sfun.get(); }
642 const SExpr *sfun() const { return Sfun.get(); }
644 SExpr *arg() { return Arg.get() ? Arg.get() : Sfun.get(); }
645 const SExpr *arg() const { return Arg.get() ? Arg.get() : Sfun.get(); }
647 bool isDelegation() const { return Arg == nullptr; }
649 template <class V> typename V::R_SExpr traverse(V &Visitor) {
650 typename V::R_SExpr Nf = Visitor.traverse(Sfun);
651 typename V::R_SExpr Na = Arg.get() ? Visitor.traverse(Arg) : nullptr;
652 return Visitor.reduceSApply(*this, Nf, Na);
655 template <class C> typename C::CType compare(SApply* E, C& Cmp) {
656 typename C::CType Ct = Cmp.compare(sfun(), E->sfun());
657 if (Cmp.notTrue(Ct) || (!arg() && !E->arg()))
659 return Cmp.compare(arg(), E->arg());
668 // Project a named slot from a C++ struct or class.
669 class Project : public SExpr {
671 static bool classof(const SExpr *E) { return E->opcode() == COP_Project; }
673 Project(SExpr *R, clang::ValueDecl *Cvd)
674 : SExpr(COP_Project), Rec(R), Cvdecl(Cvd) {}
675 Project(const Project &P, SExpr *R) : SExpr(P), Rec(R), Cvdecl(P.Cvdecl) {}
677 SExpr *record() { return Rec.get(); }
678 const SExpr *record() const { return Rec.get(); }
680 const clang::ValueDecl *clangValueDecl() const { return Cvdecl; }
682 StringRef slotName() const { return Cvdecl->getName(); }
684 template <class V> typename V::R_SExpr traverse(V &Visitor) {
685 typename V::R_SExpr Nr = Visitor.traverse(Rec);
686 return Visitor.reduceProject(*this, Nr);
689 template <class C> typename C::CType compare(Project* E, C& Cmp) {
690 typename C::CType Ct = Cmp.compare(record(), E->record());
693 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
698 clang::ValueDecl *Cvdecl;
702 // Call a function (after all arguments have been applied).
703 class Call : public SExpr {
705 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
707 Call(SExpr *T, const clang::CallExpr *Ce = nullptr)
708 : SExpr(COP_Call), Target(T), Cexpr(Ce) {}
709 Call(const Call &C, SExpr *T) : SExpr(C), Target(T), Cexpr(C.Cexpr) {}
711 SExpr *target() { return Target.get(); }
712 const SExpr *target() const { return Target.get(); }
714 const clang::CallExpr *clangCallExpr() const { return Cexpr; }
716 template <class V> typename V::R_SExpr traverse(V &Visitor) {
717 typename V::R_SExpr Nt = Visitor.traverse(Target);
718 return Visitor.reduceCall(*this, Nt);
721 template <class C> typename C::CType compare(Call* E, C& Cmp) {
722 return Cmp.compare(target(), E->target());
727 const clang::CallExpr *Cexpr;
731 // Allocate memory for a new value on the heap or stack.
732 class Alloc : public SExpr {
734 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
741 Alloc(SExpr *D, AllocKind K) : SExpr(COP_Alloc), Dtype(D) { Flags = K; }
742 Alloc(const Alloc &A, SExpr *Dt) : SExpr(A), Dtype(Dt) { Flags = A.kind(); }
744 AllocKind kind() const { return static_cast<AllocKind>(Flags); }
746 SExpr *dataType() { return Dtype.get(); }
747 const SExpr *dataType() const { return Dtype.get(); }
749 template <class V> typename V::R_SExpr traverse(V &Visitor) {
750 typename V::R_SExpr Nd = Visitor.traverse(Dtype);
751 return Visitor.reduceAlloc(*this, Nd);
754 template <class C> typename C::CType compare(Alloc* E, C& Cmp) {
755 typename C::CType Ct = Cmp.compareIntegers(kind(), E->kind());
758 return Cmp.compare(dataType(), E->dataType());
766 // Load a value from memory.
767 class Load : public SExpr {
769 static bool classof(const SExpr *E) { return E->opcode() == COP_Load; }
771 Load(SExpr *P) : SExpr(COP_Load), Ptr(P) {}
772 Load(const Load &L, SExpr *P) : SExpr(L), Ptr(P) {}
774 SExpr *pointer() { return Ptr.get(); }
775 const SExpr *pointer() const { return Ptr.get(); }
777 template <class V> typename V::R_SExpr traverse(V &Visitor) {
778 typename V::R_SExpr Np = Visitor.traverse(Ptr);
779 return Visitor.reduceLoad(*this, Np);
782 template <class C> typename C::CType compare(Load* E, C& Cmp) {
783 return Cmp.compare(pointer(), E->pointer());
791 // Store a value to memory.
792 class Store : public SExpr {
794 static bool classof(const SExpr *E) { return E->opcode() == COP_Store; }
796 Store(SExpr *P, SExpr *V) : SExpr(COP_Store), Dest(P), Source(V) {}
797 Store(const Store &S, SExpr *P, SExpr *V) : SExpr(S), Dest(P), Source(V) {}
799 SExpr *destination() { return Dest.get(); } // Address to store to
800 const SExpr *destination() const { return Dest.get(); }
802 SExpr *source() { return Source.get(); } // Value to store
803 const SExpr *source() const { return Source.get(); }
805 template <class V> typename V::R_SExpr traverse(V &Visitor) {
806 typename V::R_SExpr Np = Visitor.traverse(Dest);
807 typename V::R_SExpr Nv = Visitor.traverse(Source);
808 return Visitor.reduceStore(*this, Np, Nv);
811 template <class C> typename C::CType compare(Store* E, C& Cmp) {
812 typename C::CType Ct = Cmp.compare(destination(), E->destination());
815 return Cmp.compare(source(), E->source());
824 // Simple unary operation -- e.g. !, ~, etc.
825 class UnaryOp : public SExpr {
827 static bool classof(const SExpr *E) { return E->opcode() == COP_UnaryOp; }
829 UnaryOp(TIL_UnaryOpcode Op, SExpr *E) : SExpr(COP_UnaryOp), Expr0(E) {
832 UnaryOp(const UnaryOp &U, SExpr *E) : SExpr(U) { Flags = U.Flags; }
834 TIL_UnaryOpcode unaryOpcode() const {
835 return static_cast<TIL_UnaryOpcode>(Flags);
838 SExpr *expr() { return Expr0.get(); }
839 const SExpr *expr() const { return Expr0.get(); }
841 template <class V> typename V::R_SExpr traverse(V &Visitor) {
842 typename V::R_SExpr Ne = Visitor.traverse(Expr0);
843 return Visitor.reduceUnaryOp(*this, Ne);
846 template <class C> typename C::CType compare(UnaryOp* E, C& Cmp) {
847 typename C::CType Ct =
848 Cmp.compareIntegers(unaryOpcode(), E->unaryOpcode());
851 return Cmp.compare(expr(), E->expr());
859 // Simple binary operation -- e.g. +, -, etc.
860 class BinaryOp : public SExpr {
862 static bool classof(const SExpr *E) { return E->opcode() == COP_BinaryOp; }
864 BinaryOp(TIL_BinaryOpcode Op, SExpr *E0, SExpr *E1)
865 : SExpr(COP_BinaryOp), Expr0(E0), Expr1(E1) {
868 BinaryOp(const BinaryOp &B, SExpr *E0, SExpr *E1)
869 : SExpr(B), Expr0(E0), Expr1(E1) {
873 TIL_BinaryOpcode binaryOpcode() const {
874 return static_cast<TIL_BinaryOpcode>(Flags);
877 SExpr *expr0() { return Expr0.get(); }
878 const SExpr *expr0() const { return Expr0.get(); }
880 SExpr *expr1() { return Expr1.get(); }
881 const SExpr *expr1() const { return Expr1.get(); }
883 template <class V> typename V::R_SExpr traverse(V &Visitor) {
884 typename V::R_SExpr Ne0 = Visitor.traverse(Expr0);
885 typename V::R_SExpr Ne1 = Visitor.traverse(Expr1);
886 return Visitor.reduceBinaryOp(*this, Ne0, Ne1);
889 template <class C> typename C::CType compare(BinaryOp* E, C& Cmp) {
890 typename C::CType Ct =
891 Cmp.compareIntegers(binaryOpcode(), E->binaryOpcode());
894 Ct = Cmp.compare(expr0(), E->expr0());
897 return Cmp.compare(expr1(), E->expr1());
907 class Cast : public SExpr {
909 static bool classof(const SExpr *E) { return E->opcode() == COP_Cast; }
911 Cast(TIL_CastOpcode Op, SExpr *E) : SExpr(COP_Cast), Expr0(E) { Flags = Op; }
912 Cast(const Cast &C, SExpr *E) : SExpr(C), Expr0(E) { Flags = C.Flags; }
914 TIL_CastOpcode castOpcode() const {
915 return static_cast<TIL_CastOpcode>(Flags);
918 SExpr *expr() { return Expr0.get(); }
919 const SExpr *expr() const { return Expr0.get(); }
921 template <class V> typename V::R_SExpr traverse(V &Visitor) {
922 typename V::R_SExpr Ne = Visitor.traverse(Expr0);
923 return Visitor.reduceCast(*this, Ne);
926 template <class C> typename C::CType compare(Cast* E, C& Cmp) {
927 typename C::CType Ct =
928 Cmp.compareIntegers(castOpcode(), E->castOpcode());
931 return Cmp.compare(expr(), E->expr());
940 // An SCFG is a control-flow graph. It consists of a set of basic blocks, each
941 // of which terminates in a branch to another basic block. There is one
942 // entry point, and one exit point.
943 class SCFG : public SExpr {
945 typedef SimpleArray<BasicBlock *> BlockArray;
946 typedef BlockArray::iterator iterator;
947 typedef BlockArray::const_iterator const_iterator;
949 static bool classof(const SExpr *E) { return E->opcode() == COP_SCFG; }
951 SCFG(MemRegionRef A, unsigned Nblocks)
952 : SExpr(COP_SCFG), Blocks(A, Nblocks), Entry(nullptr), Exit(nullptr) {}
953 SCFG(const SCFG &Cfg, BlockArray &&Ba) // steals memory from Ba
954 : SExpr(COP_SCFG), Blocks(std::move(Ba)), Entry(nullptr), Exit(nullptr) {
955 // TODO: set entry and exit!
958 iterator begin() { return Blocks.begin(); }
959 iterator end() { return Blocks.end(); }
961 const_iterator begin() const { return cbegin(); }
962 const_iterator end() const { return cend(); }
964 const_iterator cbegin() const { return Blocks.cbegin(); }
965 const_iterator cend() const { return Blocks.cend(); }
967 const BasicBlock *entry() const { return Entry; }
968 BasicBlock *entry() { return Entry; }
969 const BasicBlock *exit() const { return Exit; }
970 BasicBlock *exit() { return Exit; }
972 void add(BasicBlock *BB) { Blocks.push_back(BB); }
973 void setEntry(BasicBlock *BB) { Entry = BB; }
974 void setExit(BasicBlock *BB) { Exit = BB; }
976 template <class V> typename V::R_SExpr traverse(V &Visitor);
978 template <class C> typename C::CType compare(SCFG *E, C &Cmp) {
979 // TODO -- implement CFG comparisons
980 return Cmp.comparePointers(this, E);
990 // A basic block is part of an SCFG, and can be treated as a function in
991 // continuation passing style. It consists of a sequence of phi nodes, which
992 // are "arguments" to the function, followed by a sequence of instructions.
993 // Both arguments and instructions define new variables. It ends with a
994 // branch or goto to another basic block in the same SCFG.
997 typedef SimpleArray<Variable*> VarArray;
999 BasicBlock(MemRegionRef A, unsigned Nargs, unsigned Nins,
1000 SExpr *Term = nullptr)
1001 : BlockID(0), Parent(nullptr), Args(A, Nargs), Instrs(A, Nins),
1003 BasicBlock(const BasicBlock &B, VarArray &&As, VarArray &&Is, SExpr *T)
1004 : BlockID(0), Parent(nullptr), Args(std::move(As)), Instrs(std::move(Is)),
1007 unsigned blockID() const { return BlockID; }
1008 const BasicBlock *parent() const { return Parent; }
1009 BasicBlock *parent() { return Parent; }
1011 const VarArray &arguments() const { return Args; }
1012 VarArray &arguments() { return Args; }
1014 const VarArray &instructions() const { return Instrs; }
1015 VarArray &instructions() { return Instrs; }
1017 const SExpr *terminator() const { return Terminator.get(); }
1018 SExpr *terminator() { return Terminator.get(); }
1020 void setParent(BasicBlock *P) { Parent = P; }
1021 void setBlockID(unsigned i) { BlockID = i; }
1022 void setTerminator(SExpr *E) { Terminator.reset(E); }
1023 void addArgument(Variable *V) { Args.push_back(V); }
1024 void addInstr(Variable *V) { Args.push_back(V); }
1026 template <class V> BasicBlock *traverse(V &Visitor) {
1027 typename V::template Container<Variable*> Nas(Visitor, Args.size());
1028 typename V::template Container<Variable*> Nis(Visitor, Instrs.size());
1030 for (auto *A : Args) {
1031 typename V::R_SExpr Ne = Visitor.traverse(A->Definition);
1032 Variable *Nvd = Visitor.enterScope(*A, Ne);
1035 for (auto *I : Instrs) {
1036 typename V::R_SExpr Ne = Visitor.traverse(I->Definition);
1037 Variable *Nvd = Visitor.enterScope(*I, Ne);
1040 typename V::R_SExpr Nt = Visitor.traverse(Terminator);
1042 // TODO: use reverse iterator
1043 for (unsigned J = 0, JN = Instrs.size(); J < JN; ++J)
1044 Visitor.exitScope(*Instrs[JN-J]);
1045 for (unsigned I = 0, IN = Instrs.size(); I < IN; ++I)
1046 Visitor.exitScope(*Args[IN-I]);
1048 return Visitor.reduceBasicBlock(*this, Nas, Nis, Nt);
1051 template <class C> typename C::CType compare(BasicBlock *E, C &Cmp) {
1052 // TODO: implement CFG comparisons
1053 return Cmp.comparePointers(this, E);
1060 BasicBlock *Parent; // The parent block is the enclosing lexical scope.
1061 // The parent dominates this block.
1062 VarArray Args; // Phi nodes
1064 SExprRef Terminator;
1068 typename V::R_SExpr SCFG::traverse(V &Visitor) {
1069 Visitor.enterCFG(*this);
1070 typename V::template Container<BasicBlock *> Bbs(Visitor, Blocks.size());
1071 for (auto *B : Blocks) {
1072 BasicBlock *Nbb = B->traverse(Visitor);
1075 Visitor.exitCFG(*this);
1076 return Visitor.reduceSCFG(*this, Bbs);
1079 class Phi : public SExpr {
1081 // TODO: change to SExprRef
1082 typedef SimpleArray<SExpr *> ValArray;
1084 static bool classof(const SExpr *E) { return E->opcode() == COP_Phi; }
1086 Phi(MemRegionRef A, unsigned Nvals) : SExpr(COP_Phi), Values(A, Nvals) {}
1087 Phi(const Phi &P, ValArray &&Vs) // steals memory of Vs
1088 : SExpr(COP_Phi), Values(std::move(Vs)) {}
1090 const ValArray &values() const { return Values; }
1091 ValArray &values() { return Values; }
1093 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1094 typename V::template Container<typename V::R_SExpr> Nvs(Visitor,
1096 for (auto *Val : Values) {
1097 typename V::R_SExpr Nv = Visitor.traverse(Val);
1100 return Visitor.reducePhi(*this, Nvs);
1103 template <class C> typename C::CType compare(Phi *E, C &Cmp) {
1104 // TODO -- implement CFG comparisons
1105 return Cmp.comparePointers(this, E);
1113 class Goto : public SExpr {
1115 static bool classof(const SExpr *E) { return E->opcode() == COP_Goto; }
1117 Goto(BasicBlock *B, unsigned Index)
1118 : SExpr(COP_Goto), TargetBlock(B), Index(0) {}
1119 Goto(const Goto &G, BasicBlock *B, unsigned I)
1120 : SExpr(COP_Goto), TargetBlock(B), Index(I) {}
1122 const BasicBlock *targetBlock() const { return TargetBlock; }
1123 BasicBlock *targetBlock() { return TargetBlock; }
1125 unsigned index() const { return Index; }
1127 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1128 // TODO -- rewrite indices properly
1129 BasicBlock *Ntb = Visitor.reduceBasicBlockRef(TargetBlock);
1130 return Visitor.reduceGoto(*this, Ntb, Index);
1133 template <class C> typename C::CType compare(Goto *E, C &Cmp) {
1134 // TODO -- implement CFG comparisons
1135 return Cmp.comparePointers(this, E);
1139 BasicBlock *TargetBlock;
1140 unsigned Index; // Index into Phi nodes of target block.
1144 class Branch : public SExpr {
1146 static bool classof(const SExpr *E) { return E->opcode() == COP_Branch; }
1148 Branch(SExpr *C, BasicBlock *T, BasicBlock *E)
1149 : SExpr(COP_Branch), Condition(C), ThenBlock(T), ElseBlock(E) {}
1150 Branch(const Branch &Br, SExpr *C, BasicBlock *T, BasicBlock *E)
1151 : SExpr(COP_Branch), Condition(C), ThenBlock(T), ElseBlock(E) {}
1153 const SExpr *condition() const { return Condition; }
1154 SExpr *condition() { return Condition; }
1155 const BasicBlock *thenBlock() const { return ThenBlock; }
1156 BasicBlock *thenBlock() { return ThenBlock; }
1157 const BasicBlock *elseBlock() const { return ElseBlock; }
1158 BasicBlock *elseBlock() { return ElseBlock; }
1160 template <class V> typename V::R_SExpr traverse(V &Visitor) {
1161 typename V::R_SExpr Nc = Visitor.traverse(Condition);
1162 BasicBlock *Ntb = Visitor.reduceBasicBlockRef(ThenBlock);
1163 BasicBlock *Nte = Visitor.reduceBasicBlockRef(ElseBlock);
1164 return Visitor.reduceBranch(*this, Nc, Ntb, Nte);
1167 template <class C> typename C::CType compare(Branch *E, C &Cmp) {
1168 // TODO -- implement CFG comparisons
1169 return Cmp.comparePointers(this, E);
1174 BasicBlock *ThenBlock;
1175 BasicBlock *ElseBlock;
1179 } // end namespace til
1180 } // end namespace threadSafety
1181 } // end namespace clang
1183 #endif // LLVM_CLANG_THREAD_SAFETY_TIL_H