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 in the llvm repository for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple Typed Intermediate Language, or TIL, that is used
11 // by the thread safety analysis (See ThreadSafety.cpp). The TIL is intended
12 // to be largely independent of clang, in the hope that the analysis can be
13 // reused for other non-C++ languages. All dependencies on clang/llvm should
14 // go in ThreadSafetyUtil.h.
16 // Thread safety analysis works by comparing mutex expressions, e.g.
18 // class A { Mutex mu; int dat GUARDED_BY(this->mu); }
22 // (*b).a.mu.lock(); // locks (*b).a.mu
23 // b->a.dat = 0; // substitute &b->a for 'this';
24 // // requires lock on (&b->a)->mu
25 // (b->a.mu).unlock(); // unlocks (b->a.mu)
28 // As illustrated by the above example, clang Exprs are not well-suited to
29 // represent mutex expressions directly, since there is no easy way to compare
30 // Exprs for equivalence. The thread safety analysis thus lowers clang Exprs
31 // into a simple intermediate language (IL). The IL supports:
33 // (1) comparisons for semantic equality of expressions
34 // (2) SSA renaming of variables
35 // (3) wildcards and pattern matching over expressions
36 // (4) hash-based expression lookup
38 // The TIL is currently very experimental, is intended only for use within
39 // the thread safety analysis, and is subject to change without notice.
40 // After the API stabilizes and matures, it may be appropriate to make this
41 // more generally available to other analyses.
43 // UNDER CONSTRUCTION. USE AT YOUR OWN RISK.
45 //===----------------------------------------------------------------------===//
47 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTIL_H
48 #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTIL_H
50 #include "clang/AST/Decl.h"
51 #include "clang/Analysis/Analyses/ThreadSafetyUtil.h"
52 #include "clang/Basic/LLVM.h"
53 #include "llvm/ADT/ArrayRef.h"
54 #include "llvm/ADT/None.h"
55 #include "llvm/ADT/Optional.h"
56 #include "llvm/ADT/StringRef.h"
57 #include "llvm/Support/Casting.h"
58 #include "llvm/Support/raw_ostream.h"
73 namespace threadSafety {
78 /// Enum for the different distinct classes of SExpr
80 #define TIL_OPCODE_DEF(X) COP_##X,
81 #include "ThreadSafetyOps.def"
85 /// Opcode for unary arithmetic operations.
86 enum TIL_UnaryOpcode : unsigned char {
92 /// Opcode for binary arithmetic operations.
93 enum TIL_BinaryOpcode : unsigned char {
109 BOP_LogicAnd, // && (no short-circuit)
110 BOP_LogicOr // || (no short-circuit)
113 /// Opcode for cast operations.
114 enum TIL_CastOpcode : unsigned char {
117 // Extend precision of numeric type
120 // Truncate precision of numeric type
123 // Convert to floating point type
126 // Convert to integer type
129 // Convert smart pointer to pointer (C++ only)
133 const TIL_Opcode COP_Min = COP_Future;
134 const TIL_Opcode COP_Max = COP_Branch;
135 const TIL_UnaryOpcode UOP_Min = UOP_Minus;
136 const TIL_UnaryOpcode UOP_Max = UOP_LogicNot;
137 const TIL_BinaryOpcode BOP_Min = BOP_Add;
138 const TIL_BinaryOpcode BOP_Max = BOP_LogicOr;
139 const TIL_CastOpcode CAST_Min = CAST_none;
140 const TIL_CastOpcode CAST_Max = CAST_toInt;
142 /// Return the name of a unary opcode.
143 StringRef getUnaryOpcodeString(TIL_UnaryOpcode Op);
145 /// Return the name of a binary opcode.
146 StringRef getBinaryOpcodeString(TIL_BinaryOpcode Op);
148 /// ValueTypes are data types that can actually be held in registers.
149 /// All variables and expressions must have a value type.
150 /// Pointer types are further subdivided into the various heap-allocated
151 /// types, such as functions, records, etc.
152 /// Structured types that are passed by value (e.g. complex numbers)
153 /// require special handling; they use BT_ValueRef, and size ST_0.
155 enum BaseType : unsigned char {
160 BT_String, // String literals
165 enum SizeType : unsigned char {
175 ValueType(BaseType B, SizeType Sz, bool S, unsigned char VS)
176 : Base(B), Size(Sz), Signed(S), VectSize(VS) {}
178 inline static SizeType getSizeType(unsigned nbytes);
181 inline static ValueType getValueType();
187 // 0 for scalar, otherwise num elements in vector
188 unsigned char VectSize;
191 inline ValueType::SizeType ValueType::getSizeType(unsigned nbytes) {
194 case 2: return ST_16;
195 case 4: return ST_32;
196 case 8: return ST_64;
197 case 16: return ST_128;
198 default: return ST_0;
203 inline ValueType ValueType::getValueType<void>() {
204 return ValueType(BT_Void, ST_0, false, 0);
208 inline ValueType ValueType::getValueType<bool>() {
209 return ValueType(BT_Bool, ST_1, false, 0);
213 inline ValueType ValueType::getValueType<int8_t>() {
214 return ValueType(BT_Int, ST_8, true, 0);
218 inline ValueType ValueType::getValueType<uint8_t>() {
219 return ValueType(BT_Int, ST_8, false, 0);
223 inline ValueType ValueType::getValueType<int16_t>() {
224 return ValueType(BT_Int, ST_16, true, 0);
228 inline ValueType ValueType::getValueType<uint16_t>() {
229 return ValueType(BT_Int, ST_16, false, 0);
233 inline ValueType ValueType::getValueType<int32_t>() {
234 return ValueType(BT_Int, ST_32, true, 0);
238 inline ValueType ValueType::getValueType<uint32_t>() {
239 return ValueType(BT_Int, ST_32, false, 0);
243 inline ValueType ValueType::getValueType<int64_t>() {
244 return ValueType(BT_Int, ST_64, true, 0);
248 inline ValueType ValueType::getValueType<uint64_t>() {
249 return ValueType(BT_Int, ST_64, false, 0);
253 inline ValueType ValueType::getValueType<float>() {
254 return ValueType(BT_Float, ST_32, true, 0);
258 inline ValueType ValueType::getValueType<double>() {
259 return ValueType(BT_Float, ST_64, true, 0);
263 inline ValueType ValueType::getValueType<long double>() {
264 return ValueType(BT_Float, ST_128, true, 0);
268 inline ValueType ValueType::getValueType<StringRef>() {
269 return ValueType(BT_String, getSizeType(sizeof(StringRef)), false, 0);
273 inline ValueType ValueType::getValueType<void*>() {
274 return ValueType(BT_Pointer, getSizeType(sizeof(void*)), false, 0);
277 /// Base class for AST nodes in the typed intermediate language.
282 TIL_Opcode opcode() const { return static_cast<TIL_Opcode>(Opcode); }
284 // Subclasses of SExpr must define the following:
286 // This(const This& E, ...) {
287 // copy constructor: construct copy of E, with some additional arguments.
290 // template <class V>
291 // typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
292 // traverse all subexpressions, following the traversal/rewriter interface.
295 // template <class C> typename C::CType compare(CType* E, C& Cmp) {
296 // compare all subexpressions, following the comparator interface
298 void *operator new(size_t S, MemRegionRef &R) {
299 return ::operator new(S, R);
302 /// SExpr objects must be created in an arena.
303 void *operator new(size_t) = delete;
305 /// SExpr objects cannot be deleted.
306 // This declaration is public to workaround a gcc bug that breaks building
307 // with REQUIRES_EH=1.
308 void operator delete(void *) = delete;
310 /// Returns the instruction ID for this expression.
311 /// All basic block instructions have a unique ID (i.e. virtual register).
312 unsigned id() const { return SExprID; }
314 /// Returns the block, if this is an instruction in a basic block,
315 /// otherwise returns null.
316 BasicBlock *block() const { return Block; }
318 /// Set the basic block and instruction ID for this expression.
319 void setID(BasicBlock *B, unsigned id) { Block = B; SExprID = id; }
322 SExpr(TIL_Opcode Op) : Opcode(Op) {}
323 SExpr(const SExpr &E) : Opcode(E.Opcode), Flags(E.Flags) {}
325 const unsigned char Opcode;
326 unsigned char Reserved = 0;
327 unsigned short Flags = 0;
328 unsigned SExprID = 0;
329 BasicBlock *Block = nullptr;
332 // Contains various helper functions for SExprs.
333 namespace ThreadSafetyTIL {
335 inline bool isTrivial(const SExpr *E) {
336 unsigned Op = E->opcode();
337 return Op == COP_Variable || Op == COP_Literal || Op == COP_LiteralPtr;
340 } // namespace ThreadSafetyTIL
342 // Nodes which declare variables
344 /// A named variable, e.g. "x".
346 /// There are two distinct places in which a Variable can appear in the AST.
347 /// A variable declaration introduces a new variable, and can occur in 3 places:
348 /// Let-expressions: (Let (x = t) u)
349 /// Functions: (Function (x : t) u)
350 /// Self-applicable functions (SFunction (x) t)
352 /// If a variable occurs in any other location, it is a reference to an existing
353 /// variable declaration -- e.g. 'x' in (x * y + z). To save space, we don't
354 /// allocate a separate AST node for variable references; a reference is just a
355 /// pointer to the original declaration.
356 class Variable : public SExpr {
362 /// Function parameter
365 /// SFunction (self) parameter
369 Variable(StringRef s, SExpr *D = nullptr)
370 : SExpr(COP_Variable), Name(s), Definition(D) {
374 Variable(SExpr *D, const ValueDecl *Cvd = nullptr)
375 : SExpr(COP_Variable), Name(Cvd ? Cvd->getName() : "_x"),
376 Definition(D), Cvdecl(Cvd) {
380 Variable(const Variable &Vd, SExpr *D) // rewrite constructor
381 : SExpr(Vd), Name(Vd.Name), Definition(D), Cvdecl(Vd.Cvdecl) {
385 static bool classof(const SExpr *E) { return E->opcode() == COP_Variable; }
387 /// Return the kind of variable (let, function param, or self)
388 VariableKind kind() const { return static_cast<VariableKind>(Flags); }
390 /// Return the name of the variable, if any.
391 StringRef name() const { return Name; }
393 /// Return the clang declaration for this variable, if any.
394 const ValueDecl *clangDecl() const { return Cvdecl; }
396 /// Return the definition of the variable.
397 /// For let-vars, this is the setting expression.
398 /// For function and self parameters, it is the type of the variable.
399 SExpr *definition() { return Definition; }
400 const SExpr *definition() const { return Definition; }
402 void setName(StringRef S) { Name = S; }
403 void setKind(VariableKind K) { Flags = K; }
404 void setDefinition(SExpr *E) { Definition = E; }
405 void setClangDecl(const ValueDecl *VD) { Cvdecl = VD; }
408 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
409 // This routine is only called for variable references.
410 return Vs.reduceVariableRef(this);
414 typename C::CType compare(const Variable* E, C& Cmp) const {
415 return Cmp.compareVariableRefs(this, E);
419 friend class BasicBlock;
420 friend class Function;
422 friend class SFunction;
424 // The name of the variable.
427 // The TIL type or definition.
430 // The clang declaration for this variable.
431 const ValueDecl *Cvdecl = nullptr;
434 /// Placeholder for an expression that has not yet been created.
435 /// Used to implement lazy copy and rewriting strategies.
436 class Future : public SExpr {
444 Future() : SExpr(COP_Future) {}
445 virtual ~Future() = delete;
447 static bool classof(const SExpr *E) { return E->opcode() == COP_Future; }
449 // A lazy rewriting strategy should subclass Future and override this method.
450 virtual SExpr *compute() { return nullptr; }
452 // Return the result of this future if it exists, otherwise return null.
453 SExpr *maybeGetResult() const { return Result; }
455 // Return the result of this future; forcing it if necessary.
461 return nullptr; // infinite loop; illegal recursion.
468 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
469 assert(Result && "Cannot traverse Future that has not been forced.");
470 return Vs.traverse(Result, Ctx);
474 typename C::CType compare(const Future* E, C& Cmp) const {
475 if (!Result || !E->Result)
476 return Cmp.comparePointers(this, E);
477 return Cmp.compare(Result, E->Result);
483 FutureStatus Status = FS_pending;
484 SExpr *Result = nullptr;
487 /// Placeholder for expressions that cannot be represented in the TIL.
488 class Undefined : public SExpr {
490 Undefined(const Stmt *S = nullptr) : SExpr(COP_Undefined), Cstmt(S) {}
491 Undefined(const Undefined &U) : SExpr(U), Cstmt(U.Cstmt) {}
493 static bool classof(const SExpr *E) { return E->opcode() == COP_Undefined; }
496 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
497 return Vs.reduceUndefined(*this);
501 typename C::CType compare(const Undefined* E, C& Cmp) const {
502 return Cmp.trueResult();
509 /// Placeholder for a wildcard that matches any other expression.
510 class Wildcard : public SExpr {
512 Wildcard() : SExpr(COP_Wildcard) {}
513 Wildcard(const Wildcard &) = default;
515 static bool classof(const SExpr *E) { return E->opcode() == COP_Wildcard; }
517 template <class V> typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
518 return Vs.reduceWildcard(*this);
522 typename C::CType compare(const Wildcard* E, C& Cmp) const {
523 return Cmp.trueResult();
527 template <class T> class LiteralT;
529 // Base class for literal values.
530 class Literal : public SExpr {
532 Literal(const Expr *C)
533 : SExpr(COP_Literal), ValType(ValueType::getValueType<void>()), Cexpr(C) {}
534 Literal(ValueType VT) : SExpr(COP_Literal), ValType(VT) {}
535 Literal(const Literal &) = default;
537 static bool classof(const SExpr *E) { return E->opcode() == COP_Literal; }
539 // The clang expression for this literal.
540 const Expr *clangExpr() const { return Cexpr; }
542 ValueType valueType() const { return ValType; }
544 template<class T> const LiteralT<T>& as() const {
545 return *static_cast<const LiteralT<T>*>(this);
547 template<class T> LiteralT<T>& as() {
548 return *static_cast<LiteralT<T>*>(this);
551 template <class V> typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx);
554 typename C::CType compare(const Literal* E, C& Cmp) const {
555 // TODO: defer actual comparison to LiteralT
556 return Cmp.trueResult();
560 const ValueType ValType;
561 const Expr *Cexpr = nullptr;
564 // Derived class for literal values, which stores the actual value.
566 class LiteralT : public Literal {
568 LiteralT(T Dat) : Literal(ValueType::getValueType<T>()), Val(Dat) {}
569 LiteralT(const LiteralT<T> &L) : Literal(L), Val(L.Val) {}
571 T value() const { return Val;}
572 T& value() { return Val; }
579 typename V::R_SExpr Literal::traverse(V &Vs, typename V::R_Ctx Ctx) {
581 return Vs.reduceLiteral(*this);
583 switch (ValType.Base) {
584 case ValueType::BT_Void:
586 case ValueType::BT_Bool:
587 return Vs.reduceLiteralT(as<bool>());
588 case ValueType::BT_Int: {
589 switch (ValType.Size) {
590 case ValueType::ST_8:
592 return Vs.reduceLiteralT(as<int8_t>());
594 return Vs.reduceLiteralT(as<uint8_t>());
595 case ValueType::ST_16:
597 return Vs.reduceLiteralT(as<int16_t>());
599 return Vs.reduceLiteralT(as<uint16_t>());
600 case ValueType::ST_32:
602 return Vs.reduceLiteralT(as<int32_t>());
604 return Vs.reduceLiteralT(as<uint32_t>());
605 case ValueType::ST_64:
607 return Vs.reduceLiteralT(as<int64_t>());
609 return Vs.reduceLiteralT(as<uint64_t>());
614 case ValueType::BT_Float: {
615 switch (ValType.Size) {
616 case ValueType::ST_32:
617 return Vs.reduceLiteralT(as<float>());
618 case ValueType::ST_64:
619 return Vs.reduceLiteralT(as<double>());
624 case ValueType::BT_String:
625 return Vs.reduceLiteralT(as<StringRef>());
626 case ValueType::BT_Pointer:
627 return Vs.reduceLiteralT(as<void*>());
628 case ValueType::BT_ValueRef:
631 return Vs.reduceLiteral(*this);
634 /// A Literal pointer to an object allocated in memory.
635 /// At compile time, pointer literals are represented by symbolic names.
636 class LiteralPtr : public SExpr {
638 LiteralPtr(const ValueDecl *D) : SExpr(COP_LiteralPtr), Cvdecl(D) {}
639 LiteralPtr(const LiteralPtr &) = default;
641 static bool classof(const SExpr *E) { return E->opcode() == COP_LiteralPtr; }
643 // The clang declaration for the value that this pointer points to.
644 const ValueDecl *clangDecl() const { return Cvdecl; }
647 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
648 return Vs.reduceLiteralPtr(*this);
652 typename C::CType compare(const LiteralPtr* E, C& Cmp) const {
653 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
657 const ValueDecl *Cvdecl;
660 /// A function -- a.k.a. lambda abstraction.
661 /// Functions with multiple arguments are created by currying,
662 /// e.g. (Function (x: Int) (Function (y: Int) (Code { return x + y })))
663 class Function : public SExpr {
665 Function(Variable *Vd, SExpr *Bd)
666 : SExpr(COP_Function), VarDecl(Vd), Body(Bd) {
667 Vd->setKind(Variable::VK_Fun);
670 Function(const Function &F, Variable *Vd, SExpr *Bd) // rewrite constructor
671 : SExpr(F), VarDecl(Vd), Body(Bd) {
672 Vd->setKind(Variable::VK_Fun);
675 static bool classof(const SExpr *E) { return E->opcode() == COP_Function; }
677 Variable *variableDecl() { return VarDecl; }
678 const Variable *variableDecl() const { return VarDecl; }
680 SExpr *body() { return Body; }
681 const SExpr *body() const { return Body; }
684 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
685 // This is a variable declaration, so traverse the definition.
686 auto E0 = Vs.traverse(VarDecl->Definition, Vs.typeCtx(Ctx));
687 // Tell the rewriter to enter the scope of the function.
688 Variable *Nvd = Vs.enterScope(*VarDecl, E0);
689 auto E1 = Vs.traverse(Body, Vs.declCtx(Ctx));
690 Vs.exitScope(*VarDecl);
691 return Vs.reduceFunction(*this, Nvd, E1);
695 typename C::CType compare(const Function* E, C& Cmp) const {
696 typename C::CType Ct =
697 Cmp.compare(VarDecl->definition(), E->VarDecl->definition());
700 Cmp.enterScope(variableDecl(), E->variableDecl());
701 Ct = Cmp.compare(body(), E->body());
711 /// A self-applicable function.
712 /// A self-applicable function can be applied to itself. It's useful for
713 /// implementing objects and late binding.
714 class SFunction : public SExpr {
716 SFunction(Variable *Vd, SExpr *B)
717 : SExpr(COP_SFunction), VarDecl(Vd), Body(B) {
718 assert(Vd->Definition == nullptr);
719 Vd->setKind(Variable::VK_SFun);
720 Vd->Definition = this;
723 SFunction(const SFunction &F, Variable *Vd, SExpr *B) // rewrite constructor
724 : SExpr(F), VarDecl(Vd), Body(B) {
725 assert(Vd->Definition == nullptr);
726 Vd->setKind(Variable::VK_SFun);
727 Vd->Definition = this;
730 static bool classof(const SExpr *E) { return E->opcode() == COP_SFunction; }
732 Variable *variableDecl() { return VarDecl; }
733 const Variable *variableDecl() const { return VarDecl; }
735 SExpr *body() { return Body; }
736 const SExpr *body() const { return Body; }
739 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
740 // A self-variable points to the SFunction itself.
741 // A rewrite must introduce the variable with a null definition, and update
742 // it after 'this' has been rewritten.
743 Variable *Nvd = Vs.enterScope(*VarDecl, nullptr);
744 auto E1 = Vs.traverse(Body, Vs.declCtx(Ctx));
745 Vs.exitScope(*VarDecl);
746 // A rewrite operation will call SFun constructor to set Vvd->Definition.
747 return Vs.reduceSFunction(*this, Nvd, E1);
751 typename C::CType compare(const SFunction* E, C& Cmp) const {
752 Cmp.enterScope(variableDecl(), E->variableDecl());
753 typename C::CType Ct = Cmp.compare(body(), E->body());
763 /// A block of code -- e.g. the body of a function.
764 class Code : public SExpr {
766 Code(SExpr *T, SExpr *B) : SExpr(COP_Code), ReturnType(T), Body(B) {}
767 Code(const Code &C, SExpr *T, SExpr *B) // rewrite constructor
768 : SExpr(C), ReturnType(T), Body(B) {}
770 static bool classof(const SExpr *E) { return E->opcode() == COP_Code; }
772 SExpr *returnType() { return ReturnType; }
773 const SExpr *returnType() const { return ReturnType; }
775 SExpr *body() { return Body; }
776 const SExpr *body() const { return Body; }
779 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
780 auto Nt = Vs.traverse(ReturnType, Vs.typeCtx(Ctx));
781 auto Nb = Vs.traverse(Body, Vs.lazyCtx(Ctx));
782 return Vs.reduceCode(*this, Nt, Nb);
786 typename C::CType compare(const Code* E, C& Cmp) const {
787 typename C::CType Ct = Cmp.compare(returnType(), E->returnType());
790 return Cmp.compare(body(), E->body());
798 /// A typed, writable location in memory
799 class Field : public SExpr {
801 Field(SExpr *R, SExpr *B) : SExpr(COP_Field), Range(R), Body(B) {}
802 Field(const Field &C, SExpr *R, SExpr *B) // rewrite constructor
803 : SExpr(C), Range(R), Body(B) {}
805 static bool classof(const SExpr *E) { return E->opcode() == COP_Field; }
807 SExpr *range() { return Range; }
808 const SExpr *range() const { return Range; }
810 SExpr *body() { return Body; }
811 const SExpr *body() const { return Body; }
814 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
815 auto Nr = Vs.traverse(Range, Vs.typeCtx(Ctx));
816 auto Nb = Vs.traverse(Body, Vs.lazyCtx(Ctx));
817 return Vs.reduceField(*this, Nr, Nb);
821 typename C::CType compare(const Field* E, C& Cmp) const {
822 typename C::CType Ct = Cmp.compare(range(), E->range());
825 return Cmp.compare(body(), E->body());
833 /// Apply an argument to a function.
834 /// Note that this does not actually call the function. Functions are curried,
835 /// so this returns a closure in which the first parameter has been applied.
836 /// Once all parameters have been applied, Call can be used to invoke the
838 class Apply : public SExpr {
840 Apply(SExpr *F, SExpr *A) : SExpr(COP_Apply), Fun(F), Arg(A) {}
841 Apply(const Apply &A, SExpr *F, SExpr *Ar) // rewrite constructor
842 : SExpr(A), Fun(F), Arg(Ar) {}
844 static bool classof(const SExpr *E) { return E->opcode() == COP_Apply; }
846 SExpr *fun() { return Fun; }
847 const SExpr *fun() const { return Fun; }
849 SExpr *arg() { return Arg; }
850 const SExpr *arg() const { return Arg; }
853 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
854 auto Nf = Vs.traverse(Fun, Vs.subExprCtx(Ctx));
855 auto Na = Vs.traverse(Arg, Vs.subExprCtx(Ctx));
856 return Vs.reduceApply(*this, Nf, Na);
860 typename C::CType compare(const Apply* E, C& Cmp) const {
861 typename C::CType Ct = Cmp.compare(fun(), E->fun());
864 return Cmp.compare(arg(), E->arg());
872 /// Apply a self-argument to a self-applicable function.
873 class SApply : public SExpr {
875 SApply(SExpr *Sf, SExpr *A = nullptr) : SExpr(COP_SApply), Sfun(Sf), Arg(A) {}
876 SApply(SApply &A, SExpr *Sf, SExpr *Ar = nullptr) // rewrite constructor
877 : SExpr(A), Sfun(Sf), Arg(Ar) {}
879 static bool classof(const SExpr *E) { return E->opcode() == COP_SApply; }
881 SExpr *sfun() { return Sfun; }
882 const SExpr *sfun() const { return Sfun; }
884 SExpr *arg() { return Arg ? Arg : Sfun; }
885 const SExpr *arg() const { return Arg ? Arg : Sfun; }
887 bool isDelegation() const { return Arg != nullptr; }
890 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
891 auto Nf = Vs.traverse(Sfun, Vs.subExprCtx(Ctx));
892 typename V::R_SExpr Na = Arg ? Vs.traverse(Arg, Vs.subExprCtx(Ctx))
894 return Vs.reduceSApply(*this, Nf, Na);
898 typename C::CType compare(const SApply* E, C& Cmp) const {
899 typename C::CType Ct = Cmp.compare(sfun(), E->sfun());
900 if (Cmp.notTrue(Ct) || (!arg() && !E->arg()))
902 return Cmp.compare(arg(), E->arg());
910 /// Project a named slot from a C++ struct or class.
911 class Project : public SExpr {
913 Project(SExpr *R, const ValueDecl *Cvd)
914 : SExpr(COP_Project), Rec(R), Cvdecl(Cvd) {
915 assert(Cvd && "ValueDecl must not be null");
918 static bool classof(const SExpr *E) { return E->opcode() == COP_Project; }
920 SExpr *record() { return Rec; }
921 const SExpr *record() const { return Rec; }
923 const ValueDecl *clangDecl() const { return Cvdecl; }
925 bool isArrow() const { return (Flags & 0x01) != 0; }
927 void setArrow(bool b) {
928 if (b) Flags |= 0x01;
929 else Flags &= 0xFFFE;
932 StringRef slotName() const {
933 if (Cvdecl->getDeclName().isIdentifier())
934 return Cvdecl->getName();
937 llvm::raw_string_ostream OS(*SlotName);
938 Cvdecl->printName(OS);
944 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
945 auto Nr = Vs.traverse(Rec, Vs.subExprCtx(Ctx));
946 return Vs.reduceProject(*this, Nr);
950 typename C::CType compare(const Project* E, C& Cmp) const {
951 typename C::CType Ct = Cmp.compare(record(), E->record());
954 return Cmp.comparePointers(Cvdecl, E->Cvdecl);
959 mutable llvm::Optional<std::string> SlotName;
960 const ValueDecl *Cvdecl;
963 /// Call a function (after all arguments have been applied).
964 class Call : public SExpr {
966 Call(SExpr *T, const CallExpr *Ce = nullptr)
967 : SExpr(COP_Call), Target(T), Cexpr(Ce) {}
968 Call(const Call &C, SExpr *T) : SExpr(C), Target(T), Cexpr(C.Cexpr) {}
970 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
972 SExpr *target() { return Target; }
973 const SExpr *target() const { return Target; }
975 const CallExpr *clangCallExpr() const { return Cexpr; }
978 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
979 auto Nt = Vs.traverse(Target, Vs.subExprCtx(Ctx));
980 return Vs.reduceCall(*this, Nt);
984 typename C::CType compare(const Call* E, C& Cmp) const {
985 return Cmp.compare(target(), E->target());
990 const CallExpr *Cexpr;
993 /// Allocate memory for a new value on the heap or stack.
994 class Alloc : public SExpr {
1001 Alloc(SExpr *D, AllocKind K) : SExpr(COP_Alloc), Dtype(D) { Flags = K; }
1002 Alloc(const Alloc &A, SExpr *Dt) : SExpr(A), Dtype(Dt) { Flags = A.kind(); }
1004 static bool classof(const SExpr *E) { return E->opcode() == COP_Call; }
1006 AllocKind kind() const { return static_cast<AllocKind>(Flags); }
1008 SExpr *dataType() { return Dtype; }
1009 const SExpr *dataType() const { return Dtype; }
1012 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1013 auto Nd = Vs.traverse(Dtype, Vs.declCtx(Ctx));
1014 return Vs.reduceAlloc(*this, Nd);
1018 typename C::CType compare(const Alloc* E, C& Cmp) const {
1019 typename C::CType Ct = Cmp.compareIntegers(kind(), E->kind());
1020 if (Cmp.notTrue(Ct))
1022 return Cmp.compare(dataType(), E->dataType());
1029 /// Load a value from memory.
1030 class Load : public SExpr {
1032 Load(SExpr *P) : SExpr(COP_Load), Ptr(P) {}
1033 Load(const Load &L, SExpr *P) : SExpr(L), Ptr(P) {}
1035 static bool classof(const SExpr *E) { return E->opcode() == COP_Load; }
1037 SExpr *pointer() { return Ptr; }
1038 const SExpr *pointer() const { return Ptr; }
1041 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1042 auto Np = Vs.traverse(Ptr, Vs.subExprCtx(Ctx));
1043 return Vs.reduceLoad(*this, Np);
1047 typename C::CType compare(const Load* E, C& Cmp) const {
1048 return Cmp.compare(pointer(), E->pointer());
1055 /// Store a value to memory.
1056 /// The destination is a pointer to a field, the source is the value to store.
1057 class Store : public SExpr {
1059 Store(SExpr *P, SExpr *V) : SExpr(COP_Store), Dest(P), Source(V) {}
1060 Store(const Store &S, SExpr *P, SExpr *V) : SExpr(S), Dest(P), Source(V) {}
1062 static bool classof(const SExpr *E) { return E->opcode() == COP_Store; }
1064 SExpr *destination() { return Dest; } // Address to store to
1065 const SExpr *destination() const { return Dest; }
1067 SExpr *source() { return Source; } // Value to store
1068 const SExpr *source() const { return Source; }
1071 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1072 auto Np = Vs.traverse(Dest, Vs.subExprCtx(Ctx));
1073 auto Nv = Vs.traverse(Source, Vs.subExprCtx(Ctx));
1074 return Vs.reduceStore(*this, Np, Nv);
1078 typename C::CType compare(const Store* E, C& Cmp) const {
1079 typename C::CType Ct = Cmp.compare(destination(), E->destination());
1080 if (Cmp.notTrue(Ct))
1082 return Cmp.compare(source(), E->source());
1090 /// If p is a reference to an array, then p[i] is a reference to the i'th
1091 /// element of the array.
1092 class ArrayIndex : public SExpr {
1094 ArrayIndex(SExpr *A, SExpr *N) : SExpr(COP_ArrayIndex), Array(A), Index(N) {}
1095 ArrayIndex(const ArrayIndex &E, SExpr *A, SExpr *N)
1096 : SExpr(E), Array(A), Index(N) {}
1098 static bool classof(const SExpr *E) { return E->opcode() == COP_ArrayIndex; }
1100 SExpr *array() { return Array; }
1101 const SExpr *array() const { return Array; }
1103 SExpr *index() { return Index; }
1104 const SExpr *index() const { return Index; }
1107 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1108 auto Na = Vs.traverse(Array, Vs.subExprCtx(Ctx));
1109 auto Ni = Vs.traverse(Index, Vs.subExprCtx(Ctx));
1110 return Vs.reduceArrayIndex(*this, Na, Ni);
1114 typename C::CType compare(const ArrayIndex* E, C& Cmp) const {
1115 typename C::CType Ct = Cmp.compare(array(), E->array());
1116 if (Cmp.notTrue(Ct))
1118 return Cmp.compare(index(), E->index());
1126 /// Pointer arithmetic, restricted to arrays only.
1127 /// If p is a reference to an array, then p + n, where n is an integer, is
1128 /// a reference to a subarray.
1129 class ArrayAdd : public SExpr {
1131 ArrayAdd(SExpr *A, SExpr *N) : SExpr(COP_ArrayAdd), Array(A), Index(N) {}
1132 ArrayAdd(const ArrayAdd &E, SExpr *A, SExpr *N)
1133 : SExpr(E), Array(A), Index(N) {}
1135 static bool classof(const SExpr *E) { return E->opcode() == COP_ArrayAdd; }
1137 SExpr *array() { return Array; }
1138 const SExpr *array() const { return Array; }
1140 SExpr *index() { return Index; }
1141 const SExpr *index() const { return Index; }
1144 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1145 auto Na = Vs.traverse(Array, Vs.subExprCtx(Ctx));
1146 auto Ni = Vs.traverse(Index, Vs.subExprCtx(Ctx));
1147 return Vs.reduceArrayAdd(*this, Na, Ni);
1151 typename C::CType compare(const ArrayAdd* E, C& Cmp) const {
1152 typename C::CType Ct = Cmp.compare(array(), E->array());
1153 if (Cmp.notTrue(Ct))
1155 return Cmp.compare(index(), E->index());
1163 /// Simple arithmetic unary operations, e.g. negate and not.
1164 /// These operations have no side-effects.
1165 class UnaryOp : public SExpr {
1167 UnaryOp(TIL_UnaryOpcode Op, SExpr *E) : SExpr(COP_UnaryOp), Expr0(E) {
1171 UnaryOp(const UnaryOp &U, SExpr *E) : SExpr(U), Expr0(E) { Flags = U.Flags; }
1173 static bool classof(const SExpr *E) { return E->opcode() == COP_UnaryOp; }
1175 TIL_UnaryOpcode unaryOpcode() const {
1176 return static_cast<TIL_UnaryOpcode>(Flags);
1179 SExpr *expr() { return Expr0; }
1180 const SExpr *expr() const { return Expr0; }
1183 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1184 auto Ne = Vs.traverse(Expr0, Vs.subExprCtx(Ctx));
1185 return Vs.reduceUnaryOp(*this, Ne);
1189 typename C::CType compare(const UnaryOp* E, C& Cmp) const {
1190 typename C::CType Ct =
1191 Cmp.compareIntegers(unaryOpcode(), E->unaryOpcode());
1192 if (Cmp.notTrue(Ct))
1194 return Cmp.compare(expr(), E->expr());
1201 /// Simple arithmetic binary operations, e.g. +, -, etc.
1202 /// These operations have no side effects.
1203 class BinaryOp : public SExpr {
1205 BinaryOp(TIL_BinaryOpcode Op, SExpr *E0, SExpr *E1)
1206 : SExpr(COP_BinaryOp), Expr0(E0), Expr1(E1) {
1210 BinaryOp(const BinaryOp &B, SExpr *E0, SExpr *E1)
1211 : SExpr(B), Expr0(E0), Expr1(E1) {
1215 static bool classof(const SExpr *E) { return E->opcode() == COP_BinaryOp; }
1217 TIL_BinaryOpcode binaryOpcode() const {
1218 return static_cast<TIL_BinaryOpcode>(Flags);
1221 SExpr *expr0() { return Expr0; }
1222 const SExpr *expr0() const { return Expr0; }
1224 SExpr *expr1() { return Expr1; }
1225 const SExpr *expr1() const { return Expr1; }
1228 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1229 auto Ne0 = Vs.traverse(Expr0, Vs.subExprCtx(Ctx));
1230 auto Ne1 = Vs.traverse(Expr1, Vs.subExprCtx(Ctx));
1231 return Vs.reduceBinaryOp(*this, Ne0, Ne1);
1235 typename C::CType compare(const BinaryOp* E, C& Cmp) const {
1236 typename C::CType Ct =
1237 Cmp.compareIntegers(binaryOpcode(), E->binaryOpcode());
1238 if (Cmp.notTrue(Ct))
1240 Ct = Cmp.compare(expr0(), E->expr0());
1241 if (Cmp.notTrue(Ct))
1243 return Cmp.compare(expr1(), E->expr1());
1251 /// Cast expressions.
1252 /// Cast expressions are essentially unary operations, but we treat them
1253 /// as a distinct AST node because they only change the type of the result.
1254 class Cast : public SExpr {
1256 Cast(TIL_CastOpcode Op, SExpr *E) : SExpr(COP_Cast), Expr0(E) { Flags = Op; }
1257 Cast(const Cast &C, SExpr *E) : SExpr(C), Expr0(E) { Flags = C.Flags; }
1259 static bool classof(const SExpr *E) { return E->opcode() == COP_Cast; }
1261 TIL_CastOpcode castOpcode() const {
1262 return static_cast<TIL_CastOpcode>(Flags);
1265 SExpr *expr() { return Expr0; }
1266 const SExpr *expr() const { return Expr0; }
1269 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1270 auto Ne = Vs.traverse(Expr0, Vs.subExprCtx(Ctx));
1271 return Vs.reduceCast(*this, Ne);
1275 typename C::CType compare(const Cast* E, C& Cmp) const {
1276 typename C::CType Ct =
1277 Cmp.compareIntegers(castOpcode(), E->castOpcode());
1278 if (Cmp.notTrue(Ct))
1280 return Cmp.compare(expr(), E->expr());
1289 /// Phi Node, for code in SSA form.
1290 /// Each Phi node has an array of possible values that it can take,
1291 /// depending on where control flow comes from.
1292 class Phi : public SExpr {
1294 using ValArray = SimpleArray<SExpr *>;
1296 // In minimal SSA form, all Phi nodes are MultiVal.
1297 // During conversion to SSA, incomplete Phi nodes may be introduced, which
1298 // are later determined to be SingleVal, and are thus redundant.
1300 PH_MultiVal = 0, // Phi node has multiple distinct values. (Normal)
1301 PH_SingleVal, // Phi node has one distinct value, and can be eliminated
1302 PH_Incomplete // Phi node is incomplete
1305 Phi() : SExpr(COP_Phi) {}
1306 Phi(MemRegionRef A, unsigned Nvals) : SExpr(COP_Phi), Values(A, Nvals) {}
1307 Phi(const Phi &P, ValArray &&Vs) : SExpr(P), Values(std::move(Vs)) {}
1309 static bool classof(const SExpr *E) { return E->opcode() == COP_Phi; }
1311 const ValArray &values() const { return Values; }
1312 ValArray &values() { return Values; }
1314 Status status() const { return static_cast<Status>(Flags); }
1315 void setStatus(Status s) { Flags = s; }
1317 /// Return the clang declaration of the variable for this Phi node, if any.
1318 const ValueDecl *clangDecl() const { return Cvdecl; }
1320 /// Set the clang variable associated with this Phi node.
1321 void setClangDecl(const ValueDecl *Cvd) { Cvdecl = Cvd; }
1324 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1325 typename V::template Container<typename V::R_SExpr>
1326 Nvs(Vs, Values.size());
1328 for (const auto *Val : Values)
1329 Nvs.push_back( Vs.traverse(Val, Vs.subExprCtx(Ctx)) );
1330 return Vs.reducePhi(*this, Nvs);
1334 typename C::CType compare(const Phi *E, C &Cmp) const {
1335 // TODO: implement CFG comparisons
1336 return Cmp.comparePointers(this, E);
1341 const ValueDecl* Cvdecl = nullptr;
1344 /// Base class for basic block terminators: Branch, Goto, and Return.
1345 class Terminator : public SExpr {
1347 Terminator(TIL_Opcode Op) : SExpr(Op) {}
1348 Terminator(const SExpr &E) : SExpr(E) {}
1351 static bool classof(const SExpr *E) {
1352 return E->opcode() >= COP_Goto && E->opcode() <= COP_Return;
1355 /// Return the list of basic blocks that this terminator can branch to.
1356 ArrayRef<BasicBlock *> successors();
1358 ArrayRef<BasicBlock *> successors() const {
1359 return const_cast<Terminator*>(this)->successors();
1363 /// Jump to another basic block.
1364 /// A goto instruction is essentially a tail-recursive call into another
1365 /// block. In addition to the block pointer, it specifies an index into the
1366 /// phi nodes of that block. The index can be used to retrieve the "arguments"
1368 class Goto : public Terminator {
1370 Goto(BasicBlock *B, unsigned I)
1371 : Terminator(COP_Goto), TargetBlock(B), Index(I) {}
1372 Goto(const Goto &G, BasicBlock *B, unsigned I)
1373 : Terminator(COP_Goto), TargetBlock(B), Index(I) {}
1375 static bool classof(const SExpr *E) { return E->opcode() == COP_Goto; }
1377 const BasicBlock *targetBlock() const { return TargetBlock; }
1378 BasicBlock *targetBlock() { return TargetBlock; }
1380 /// Returns the index into the
1381 unsigned index() const { return Index; }
1383 /// Return the list of basic blocks that this terminator can branch to.
1384 ArrayRef<BasicBlock *> successors() { return TargetBlock; }
1387 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1388 BasicBlock *Ntb = Vs.reduceBasicBlockRef(TargetBlock);
1389 return Vs.reduceGoto(*this, Ntb);
1393 typename C::CType compare(const Goto *E, C &Cmp) const {
1394 // TODO: implement CFG comparisons
1395 return Cmp.comparePointers(this, E);
1399 BasicBlock *TargetBlock;
1403 /// A conditional branch to two other blocks.
1404 /// Note that unlike Goto, Branch does not have an index. The target blocks
1405 /// must be child-blocks, and cannot have Phi nodes.
1406 class Branch : public Terminator {
1408 Branch(SExpr *C, BasicBlock *T, BasicBlock *E)
1409 : Terminator(COP_Branch), Condition(C) {
1414 Branch(const Branch &Br, SExpr *C, BasicBlock *T, BasicBlock *E)
1415 : Terminator(Br), Condition(C) {
1420 static bool classof(const SExpr *E) { return E->opcode() == COP_Branch; }
1422 const SExpr *condition() const { return Condition; }
1423 SExpr *condition() { return Condition; }
1425 const BasicBlock *thenBlock() const { return Branches[0]; }
1426 BasicBlock *thenBlock() { return Branches[0]; }
1428 const BasicBlock *elseBlock() const { return Branches[1]; }
1429 BasicBlock *elseBlock() { return Branches[1]; }
1431 /// Return the list of basic blocks that this terminator can branch to.
1432 ArrayRef<BasicBlock*> successors() {
1433 return llvm::makeArrayRef(Branches);
1437 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1438 auto Nc = Vs.traverse(Condition, Vs.subExprCtx(Ctx));
1439 BasicBlock *Ntb = Vs.reduceBasicBlockRef(Branches[0]);
1440 BasicBlock *Nte = Vs.reduceBasicBlockRef(Branches[1]);
1441 return Vs.reduceBranch(*this, Nc, Ntb, Nte);
1445 typename C::CType compare(const Branch *E, C &Cmp) const {
1446 // TODO: implement CFG comparisons
1447 return Cmp.comparePointers(this, E);
1452 BasicBlock *Branches[2];
1455 /// Return from the enclosing function, passing the return value to the caller.
1456 /// Only the exit block should end with a return statement.
1457 class Return : public Terminator {
1459 Return(SExpr* Rval) : Terminator(COP_Return), Retval(Rval) {}
1460 Return(const Return &R, SExpr* Rval) : Terminator(R), Retval(Rval) {}
1462 static bool classof(const SExpr *E) { return E->opcode() == COP_Return; }
1464 /// Return an empty list.
1465 ArrayRef<BasicBlock *> successors() { return None; }
1467 SExpr *returnValue() { return Retval; }
1468 const SExpr *returnValue() const { return Retval; }
1471 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1472 auto Ne = Vs.traverse(Retval, Vs.subExprCtx(Ctx));
1473 return Vs.reduceReturn(*this, Ne);
1477 typename C::CType compare(const Return *E, C &Cmp) const {
1478 return Cmp.compare(Retval, E->Retval);
1485 inline ArrayRef<BasicBlock*> Terminator::successors() {
1487 case COP_Goto: return cast<Goto>(this)->successors();
1488 case COP_Branch: return cast<Branch>(this)->successors();
1489 case COP_Return: return cast<Return>(this)->successors();
1495 /// A basic block is part of an SCFG. It can be treated as a function in
1496 /// continuation passing style. A block consists of a sequence of phi nodes,
1497 /// which are "arguments" to the function, followed by a sequence of
1498 /// instructions. It ends with a Terminator, which is a Branch or Goto to
1499 /// another basic block in the same SCFG.
1500 class BasicBlock : public SExpr {
1502 using InstrArray = SimpleArray<SExpr *>;
1503 using BlockArray = SimpleArray<BasicBlock *>;
1505 // TopologyNodes are used to overlay tree structures on top of the CFG,
1506 // such as dominator and postdominator trees. Each block is assigned an
1507 // ID in the tree according to a depth-first search. Tree traversals are
1508 // always up, towards the parents.
1509 struct TopologyNode {
1512 // Includes this node, so must be > 1.
1513 int SizeOfSubTree = 0;
1515 // Pointer to parent.
1516 BasicBlock *Parent = nullptr;
1518 TopologyNode() = default;
1520 bool isParentOf(const TopologyNode& OtherNode) {
1521 return OtherNode.NodeID > NodeID &&
1522 OtherNode.NodeID < NodeID + SizeOfSubTree;
1525 bool isParentOfOrEqual(const TopologyNode& OtherNode) {
1526 return OtherNode.NodeID >= NodeID &&
1527 OtherNode.NodeID < NodeID + SizeOfSubTree;
1531 explicit BasicBlock(MemRegionRef A)
1532 : SExpr(COP_BasicBlock), Arena(A), BlockID(0), Visited(false) {}
1533 BasicBlock(BasicBlock &B, MemRegionRef A, InstrArray &&As, InstrArray &&Is,
1535 : SExpr(COP_BasicBlock), Arena(A), BlockID(0), Visited(false),
1536 Args(std::move(As)), Instrs(std::move(Is)), TermInstr(T) {}
1538 static bool classof(const SExpr *E) { return E->opcode() == COP_BasicBlock; }
1540 /// Returns the block ID. Every block has a unique ID in the CFG.
1541 int blockID() const { return BlockID; }
1543 /// Returns the number of predecessors.
1544 size_t numPredecessors() const { return Predecessors.size(); }
1545 size_t numSuccessors() const { return successors().size(); }
1547 const SCFG* cfg() const { return CFGPtr; }
1548 SCFG* cfg() { return CFGPtr; }
1550 const BasicBlock *parent() const { return DominatorNode.Parent; }
1551 BasicBlock *parent() { return DominatorNode.Parent; }
1553 const InstrArray &arguments() const { return Args; }
1554 InstrArray &arguments() { return Args; }
1556 InstrArray &instructions() { return Instrs; }
1557 const InstrArray &instructions() const { return Instrs; }
1559 /// Returns a list of predecessors.
1560 /// The order of predecessors in the list is important; each phi node has
1561 /// exactly one argument for each precessor, in the same order.
1562 BlockArray &predecessors() { return Predecessors; }
1563 const BlockArray &predecessors() const { return Predecessors; }
1565 ArrayRef<BasicBlock*> successors() { return TermInstr->successors(); }
1566 ArrayRef<BasicBlock*> successors() const { return TermInstr->successors(); }
1568 const Terminator *terminator() const { return TermInstr; }
1569 Terminator *terminator() { return TermInstr; }
1571 void setTerminator(Terminator *E) { TermInstr = E; }
1573 bool Dominates(const BasicBlock &Other) {
1574 return DominatorNode.isParentOfOrEqual(Other.DominatorNode);
1577 bool PostDominates(const BasicBlock &Other) {
1578 return PostDominatorNode.isParentOfOrEqual(Other.PostDominatorNode);
1581 /// Add a new argument.
1582 void addArgument(Phi *V) {
1583 Args.reserveCheck(1, Arena);
1587 /// Add a new instruction.
1588 void addInstruction(SExpr *V) {
1589 Instrs.reserveCheck(1, Arena);
1590 Instrs.push_back(V);
1593 // Add a new predecessor, and return the phi-node index for it.
1594 // Will add an argument to all phi-nodes, initialized to nullptr.
1595 unsigned addPredecessor(BasicBlock *Pred);
1597 // Reserve space for Nargs arguments.
1598 void reserveArguments(unsigned Nargs) { Args.reserve(Nargs, Arena); }
1600 // Reserve space for Nins instructions.
1601 void reserveInstructions(unsigned Nins) { Instrs.reserve(Nins, Arena); }
1603 // Reserve space for NumPreds predecessors, including space in phi nodes.
1604 void reservePredecessors(unsigned NumPreds);
1606 /// Return the index of BB, or Predecessors.size if BB is not a predecessor.
1607 unsigned findPredecessorIndex(const BasicBlock *BB) const {
1608 auto I = std::find(Predecessors.cbegin(), Predecessors.cend(), BB);
1609 return std::distance(Predecessors.cbegin(), I);
1613 typename V::R_BasicBlock traverse(V &Vs, typename V::R_Ctx Ctx) {
1614 typename V::template Container<SExpr*> Nas(Vs, Args.size());
1615 typename V::template Container<SExpr*> Nis(Vs, Instrs.size());
1617 // Entering the basic block should do any scope initialization.
1618 Vs.enterBasicBlock(*this);
1620 for (const auto *E : Args) {
1621 auto Ne = Vs.traverse(E, Vs.subExprCtx(Ctx));
1624 for (const auto *E : Instrs) {
1625 auto Ne = Vs.traverse(E, Vs.subExprCtx(Ctx));
1628 auto Nt = Vs.traverse(TermInstr, Ctx);
1630 // Exiting the basic block should handle any scope cleanup.
1631 Vs.exitBasicBlock(*this);
1633 return Vs.reduceBasicBlock(*this, Nas, Nis, Nt);
1637 typename C::CType compare(const BasicBlock *E, C &Cmp) const {
1638 // TODO: implement CFG comparisons
1639 return Cmp.comparePointers(this, E);
1645 // assign unique ids to all instructions
1646 int renumberInstrs(int id);
1648 int topologicalSort(SimpleArray<BasicBlock *> &Blocks, int ID);
1649 int topologicalFinalSort(SimpleArray<BasicBlock *> &Blocks, int ID);
1650 void computeDominator();
1651 void computePostDominator();
1653 // The arena used to allocate this block.
1656 // The CFG that contains this block.
1657 SCFG *CFGPtr = nullptr;
1659 // Unique ID for this BB in the containing CFG. IDs are in topological order.
1662 // Bit to determine if a block has been visited during a traversal.
1665 // Predecessor blocks in the CFG.
1666 BlockArray Predecessors;
1668 // Phi nodes. One argument per predecessor.
1674 // Terminating instruction.
1675 Terminator *TermInstr = nullptr;
1677 // The dominator tree.
1678 TopologyNode DominatorNode;
1680 // The post-dominator tree.
1681 TopologyNode PostDominatorNode;
1684 /// An SCFG is a control-flow graph. It consists of a set of basic blocks,
1685 /// each of which terminates in a branch to another basic block. There is one
1686 /// entry point, and one exit point.
1687 class SCFG : public SExpr {
1689 using BlockArray = SimpleArray<BasicBlock *>;
1690 using iterator = BlockArray::iterator;
1691 using const_iterator = BlockArray::const_iterator;
1693 SCFG(MemRegionRef A, unsigned Nblocks)
1694 : SExpr(COP_SCFG), Arena(A), Blocks(A, Nblocks) {
1695 Entry = new (A) BasicBlock(A);
1696 Exit = new (A) BasicBlock(A);
1697 auto *V = new (A) Phi();
1698 Exit->addArgument(V);
1699 Exit->setTerminator(new (A) Return(V));
1704 SCFG(const SCFG &Cfg, BlockArray &&Ba) // steals memory from Ba
1705 : SExpr(COP_SCFG), Arena(Cfg.Arena), Blocks(std::move(Ba)) {
1706 // TODO: set entry and exit!
1709 static bool classof(const SExpr *E) { return E->opcode() == COP_SCFG; }
1711 /// Return true if this CFG is valid.
1712 bool valid() const { return Entry && Exit && Blocks.size() > 0; }
1714 /// Return true if this CFG has been normalized.
1715 /// After normalization, blocks are in topological order, and block and
1716 /// instruction IDs have been assigned.
1717 bool normal() const { return Normal; }
1719 iterator begin() { return Blocks.begin(); }
1720 iterator end() { return Blocks.end(); }
1722 const_iterator begin() const { return cbegin(); }
1723 const_iterator end() const { return cend(); }
1725 const_iterator cbegin() const { return Blocks.cbegin(); }
1726 const_iterator cend() const { return Blocks.cend(); }
1728 const BasicBlock *entry() const { return Entry; }
1729 BasicBlock *entry() { return Entry; }
1730 const BasicBlock *exit() const { return Exit; }
1731 BasicBlock *exit() { return Exit; }
1733 /// Return the number of blocks in the CFG.
1734 /// Block::blockID() will return a number less than numBlocks();
1735 size_t numBlocks() const { return Blocks.size(); }
1737 /// Return the total number of instructions in the CFG.
1738 /// This is useful for building instruction side-tables;
1739 /// A call to SExpr::id() will return a number less than numInstructions().
1740 unsigned numInstructions() { return NumInstructions; }
1742 inline void add(BasicBlock *BB) {
1743 assert(BB->CFGPtr == nullptr);
1745 Blocks.reserveCheck(1, Arena);
1746 Blocks.push_back(BB);
1749 void setEntry(BasicBlock *BB) { Entry = BB; }
1750 void setExit(BasicBlock *BB) { Exit = BB; }
1752 void computeNormalForm();
1755 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1757 typename V::template Container<BasicBlock *> Bbs(Vs, Blocks.size());
1759 for (const auto *B : Blocks) {
1760 Bbs.push_back( B->traverse(Vs, Vs.subExprCtx(Ctx)) );
1763 return Vs.reduceSCFG(*this, Bbs);
1767 typename C::CType compare(const SCFG *E, C &Cmp) const {
1768 // TODO: implement CFG comparisons
1769 return Cmp.comparePointers(this, E);
1773 // assign unique ids to all instructions
1774 void renumberInstrs();
1778 BasicBlock *Entry = nullptr;
1779 BasicBlock *Exit = nullptr;
1780 unsigned NumInstructions = 0;
1781 bool Normal = false;
1784 /// An identifier, e.g. 'foo' or 'x'.
1785 /// This is a pseduo-term; it will be lowered to a variable or projection.
1786 class Identifier : public SExpr {
1788 Identifier(StringRef Id): SExpr(COP_Identifier), Name(Id) {}
1789 Identifier(const Identifier &) = default;
1791 static bool classof(const SExpr *E) { return E->opcode() == COP_Identifier; }
1793 StringRef name() const { return Name; }
1796 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1797 return Vs.reduceIdentifier(*this);
1801 typename C::CType compare(const Identifier* E, C& Cmp) const {
1802 return Cmp.compareStrings(name(), E->name());
1809 /// An if-then-else expression.
1810 /// This is a pseduo-term; it will be lowered to a branch in a CFG.
1811 class IfThenElse : public SExpr {
1813 IfThenElse(SExpr *C, SExpr *T, SExpr *E)
1814 : SExpr(COP_IfThenElse), Condition(C), ThenExpr(T), ElseExpr(E) {}
1815 IfThenElse(const IfThenElse &I, SExpr *C, SExpr *T, SExpr *E)
1816 : SExpr(I), Condition(C), ThenExpr(T), ElseExpr(E) {}
1818 static bool classof(const SExpr *E) { return E->opcode() == COP_IfThenElse; }
1820 SExpr *condition() { return Condition; } // Address to store to
1821 const SExpr *condition() const { return Condition; }
1823 SExpr *thenExpr() { return ThenExpr; } // Value to store
1824 const SExpr *thenExpr() const { return ThenExpr; }
1826 SExpr *elseExpr() { return ElseExpr; } // Value to store
1827 const SExpr *elseExpr() const { return ElseExpr; }
1830 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1831 auto Nc = Vs.traverse(Condition, Vs.subExprCtx(Ctx));
1832 auto Nt = Vs.traverse(ThenExpr, Vs.subExprCtx(Ctx));
1833 auto Ne = Vs.traverse(ElseExpr, Vs.subExprCtx(Ctx));
1834 return Vs.reduceIfThenElse(*this, Nc, Nt, Ne);
1838 typename C::CType compare(const IfThenElse* E, C& Cmp) const {
1839 typename C::CType Ct = Cmp.compare(condition(), E->condition());
1840 if (Cmp.notTrue(Ct))
1842 Ct = Cmp.compare(thenExpr(), E->thenExpr());
1843 if (Cmp.notTrue(Ct))
1845 return Cmp.compare(elseExpr(), E->elseExpr());
1854 /// A let-expression, e.g. let x=t; u.
1855 /// This is a pseduo-term; it will be lowered to instructions in a CFG.
1856 class Let : public SExpr {
1858 Let(Variable *Vd, SExpr *Bd) : SExpr(COP_Let), VarDecl(Vd), Body(Bd) {
1859 Vd->setKind(Variable::VK_Let);
1862 Let(const Let &L, Variable *Vd, SExpr *Bd) : SExpr(L), VarDecl(Vd), Body(Bd) {
1863 Vd->setKind(Variable::VK_Let);
1866 static bool classof(const SExpr *E) { return E->opcode() == COP_Let; }
1868 Variable *variableDecl() { return VarDecl; }
1869 const Variable *variableDecl() const { return VarDecl; }
1871 SExpr *body() { return Body; }
1872 const SExpr *body() const { return Body; }
1875 typename V::R_SExpr traverse(V &Vs, typename V::R_Ctx Ctx) {
1876 // This is a variable declaration, so traverse the definition.
1877 auto E0 = Vs.traverse(VarDecl->Definition, Vs.subExprCtx(Ctx));
1878 // Tell the rewriter to enter the scope of the let variable.
1879 Variable *Nvd = Vs.enterScope(*VarDecl, E0);
1880 auto E1 = Vs.traverse(Body, Ctx);
1881 Vs.exitScope(*VarDecl);
1882 return Vs.reduceLet(*this, Nvd, E1);
1886 typename C::CType compare(const Let* E, C& Cmp) const {
1887 typename C::CType Ct =
1888 Cmp.compare(VarDecl->definition(), E->VarDecl->definition());
1889 if (Cmp.notTrue(Ct))
1891 Cmp.enterScope(variableDecl(), E->variableDecl());
1892 Ct = Cmp.compare(body(), E->body());
1902 const SExpr *getCanonicalVal(const SExpr *E);
1903 SExpr* simplifyToCanonicalVal(SExpr *E);
1904 void simplifyIncompleteArg(til::Phi *Ph);
1907 } // namespace threadSafety
1909 } // namespace clang
1911 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTIL_H