Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===- ThreadSafetyCommon.cpp -----------------------------------*- C++ -*-===// |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implementation of the interfaces declared in ThreadSafetyCommon.h |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Aaron Ballman | 28347a7 | 2014-04-09 21:12:04 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/ThreadSafetyCommon.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/Attr.h" |
| 16 | #include "clang/AST/DeclCXX.h" |
Benjamin Kramer | a7bcab7 | 2014-05-09 17:08:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/AST/StmtCXX.h" |
DeLesley Hutchins | f7813c5 | 2014-04-08 22:21:22 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/ThreadSafetyTIL.h" |
DeLesley Hutchins | 7e615c2 | 2014-04-09 22:39:43 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/ThreadSafetyTraverse.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/AnalysisContext.h" |
| 23 | #include "clang/Analysis/CFG.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 24 | #include "clang/Basic/OperatorKinds.h" |
| 25 | #include "clang/Basic/SourceLocation.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringRef.h" |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 28 | |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace threadSafety; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 31 | |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 32 | // From ThreadSafetyUtil.h |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 33 | std::string threadSafety::getSourceLiteralString(const clang::Expr *CE) { |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 34 | switch (CE->getStmtClass()) { |
| 35 | case Stmt::IntegerLiteralClass: |
| 36 | return cast<IntegerLiteral>(CE)->getValue().toString(10, true); |
| 37 | case Stmt::StringLiteralClass: { |
| 38 | std::string ret("\""); |
| 39 | ret += cast<StringLiteral>(CE)->getString(); |
| 40 | ret += "\""; |
| 41 | return ret; |
| 42 | } |
| 43 | case Stmt::CharacterLiteralClass: |
| 44 | case Stmt::CXXNullPtrLiteralExprClass: |
| 45 | case Stmt::GNUNullExprClass: |
| 46 | case Stmt::CXXBoolLiteralExprClass: |
| 47 | case Stmt::FloatingLiteralClass: |
| 48 | case Stmt::ImaginaryLiteralClass: |
| 49 | case Stmt::ObjCStringLiteralClass: |
| 50 | default: |
| 51 | return "#lit"; |
| 52 | } |
| 53 | } |
| 54 | |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 55 | // Return true if E is a variable that points to an incomplete Phi node. |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 56 | static bool isIncompletePhi(const til::SExpr *E) { |
| 57 | if (const auto *Ph = dyn_cast<til::Phi>(E)) |
| 58 | return Ph->status() == til::Phi::PH_Incomplete; |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 62 | typedef SExprBuilder::CallingContext CallingContext; |
| 63 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 64 | til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) { |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 65 | auto It = SMap.find(S); |
| 66 | if (It != SMap.end()) |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 67 | return It->second; |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 68 | return nullptr; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 69 | } |
| 70 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 71 | til::SCFG *SExprBuilder::buildCFG(CFGWalker &Walker) { |
| 72 | Walker.walk(*this); |
| 73 | return Scfg; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 76 | static bool isCalleeArrow(const Expr *E) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 77 | const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts()); |
| 78 | return ME ? ME->isArrow() : false; |
| 79 | } |
| 80 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 81 | /// \brief Translate a clang expression in an attribute to a til::SExpr. |
| 82 | /// Constructs the context from D, DeclExp, and SelfDecl. |
| 83 | /// |
| 84 | /// \param AttrExp The expression to translate. |
| 85 | /// \param D The declaration to which the attribute is attached. |
| 86 | /// \param DeclExp An expression involving the Decl to which the attribute |
| 87 | /// is attached. E.g. the call to a function. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 88 | CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp, |
| 89 | const NamedDecl *D, |
| 90 | const Expr *DeclExp, |
| 91 | VarDecl *SelfDecl) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 92 | // If we are processing a raw attribute expression, with no substitutions. |
| 93 | if (!DeclExp) |
| 94 | return translateAttrExpr(AttrExp, nullptr); |
| 95 | |
| 96 | CallingContext Ctx(nullptr, D); |
| 97 | |
| 98 | // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute |
| 99 | // for formal parameters when we call buildMutexID later. |
| 100 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) { |
| 101 | Ctx.SelfArg = ME->getBase(); |
| 102 | Ctx.SelfArrow = ME->isArrow(); |
| 103 | } else if (const CXXMemberCallExpr *CE = |
| 104 | dyn_cast<CXXMemberCallExpr>(DeclExp)) { |
| 105 | Ctx.SelfArg = CE->getImplicitObjectArgument(); |
| 106 | Ctx.SelfArrow = isCalleeArrow(CE->getCallee()); |
| 107 | Ctx.NumArgs = CE->getNumArgs(); |
| 108 | Ctx.FunArgs = CE->getArgs(); |
| 109 | } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) { |
| 110 | Ctx.NumArgs = CE->getNumArgs(); |
| 111 | Ctx.FunArgs = CE->getArgs(); |
| 112 | } else if (const CXXConstructExpr *CE = |
| 113 | dyn_cast<CXXConstructExpr>(DeclExp)) { |
| 114 | Ctx.SelfArg = nullptr; // Will be set below |
| 115 | Ctx.NumArgs = CE->getNumArgs(); |
| 116 | Ctx.FunArgs = CE->getArgs(); |
| 117 | } else if (D && isa<CXXDestructorDecl>(D)) { |
| 118 | // There's no such thing as a "destructor call" in the AST. |
| 119 | Ctx.SelfArg = DeclExp; |
| 120 | } |
| 121 | |
| 122 | // Hack to handle constructors, where self cannot be recovered from |
| 123 | // the expression. |
| 124 | if (SelfDecl && !Ctx.SelfArg) { |
| 125 | DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue, |
| 126 | SelfDecl->getLocation()); |
| 127 | Ctx.SelfArg = &SelfDRE; |
| 128 | |
| 129 | // If the attribute has no arguments, then assume the argument is "this". |
| 130 | if (!AttrExp) |
| 131 | return translateAttrExpr(Ctx.SelfArg, nullptr); |
| 132 | else // For most attributes. |
| 133 | return translateAttrExpr(AttrExp, &Ctx); |
| 134 | } |
| 135 | |
| 136 | // If the attribute has no arguments, then assume the argument is "this". |
| 137 | if (!AttrExp) |
| 138 | return translateAttrExpr(Ctx.SelfArg, nullptr); |
| 139 | else // For most attributes. |
| 140 | return translateAttrExpr(AttrExp, &Ctx); |
| 141 | } |
| 142 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 143 | /// \brief Translate a clang expression in an attribute to a til::SExpr. |
| 144 | // This assumes a CallingContext has already been created. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 145 | CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp, |
| 146 | CallingContext *Ctx) { |
| 147 | if (!AttrExp) |
| 148 | return CapabilityExpr(nullptr, false); |
| 149 | |
| 150 | if (auto* SLit = dyn_cast<StringLiteral>(AttrExp)) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 151 | if (SLit->getString() == StringRef("*")) |
| 152 | // The "*" expr is a universal lock, which essentially turns off |
| 153 | // checks until it is removed from the lockset. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 154 | return CapabilityExpr(new (Arena) til::Wildcard(), false); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 155 | else |
| 156 | // Ignore other string literals for now. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 157 | return CapabilityExpr(nullptr, false); |
| 158 | } |
| 159 | |
| 160 | bool Neg = false; |
| 161 | if (auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) { |
| 162 | if (OE->getOperator() == OO_Exclaim) { |
| 163 | Neg = true; |
| 164 | AttrExp = OE->getArg(0); |
| 165 | } |
| 166 | } |
| 167 | else if (auto *UO = dyn_cast<UnaryOperator>(AttrExp)) { |
| 168 | if (UO->getOpcode() == UO_LNot) { |
| 169 | Neg = true; |
| 170 | AttrExp = UO->getSubExpr(); |
| 171 | } |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | til::SExpr *E = translate(AttrExp, Ctx); |
| 175 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 176 | // Trap mutex expressions like nullptr, or 0. |
| 177 | // Any literal value is nonsense. |
| 178 | if (!E || isa<til::Literal>(E)) |
| 179 | return CapabilityExpr(nullptr, false); |
| 180 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 181 | // Hack to deal with smart pointers -- strip off top-level pointer casts. |
| 182 | if (auto *CE = dyn_cast_or_null<til::Cast>(E)) { |
| 183 | if (CE->castOpcode() == til::CAST_objToPtr) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 184 | return CapabilityExpr(CE->expr(), Neg); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 185 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 186 | return CapabilityExpr(E, Neg); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 187 | } |
| 188 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 189 | // Translate a clang statement or expression to a TIL expression. |
| 190 | // Also performs substitution of variables; Ctx provides the context. |
| 191 | // Dispatches on the type of S. |
| 192 | til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) { |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 193 | if (!S) |
| 194 | return nullptr; |
| 195 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 196 | // Check if S has already been translated and cached. |
| 197 | // This handles the lookup of SSA names for DeclRefExprs here. |
| 198 | if (til::SExpr *E = lookupStmt(S)) |
| 199 | return E; |
| 200 | |
| 201 | switch (S->getStmtClass()) { |
| 202 | case Stmt::DeclRefExprClass: |
| 203 | return translateDeclRefExpr(cast<DeclRefExpr>(S), Ctx); |
| 204 | case Stmt::CXXThisExprClass: |
| 205 | return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx); |
| 206 | case Stmt::MemberExprClass: |
| 207 | return translateMemberExpr(cast<MemberExpr>(S), Ctx); |
| 208 | case Stmt::CallExprClass: |
| 209 | return translateCallExpr(cast<CallExpr>(S), Ctx); |
| 210 | case Stmt::CXXMemberCallExprClass: |
| 211 | return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx); |
| 212 | case Stmt::CXXOperatorCallExprClass: |
| 213 | return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx); |
| 214 | case Stmt::UnaryOperatorClass: |
| 215 | return translateUnaryOperator(cast<UnaryOperator>(S), Ctx); |
| 216 | case Stmt::BinaryOperatorClass: |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 217 | case Stmt::CompoundAssignOperatorClass: |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 218 | return translateBinaryOperator(cast<BinaryOperator>(S), Ctx); |
| 219 | |
| 220 | case Stmt::ArraySubscriptExprClass: |
| 221 | return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx); |
| 222 | case Stmt::ConditionalOperatorClass: |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 223 | return translateAbstractConditionalOperator( |
| 224 | cast<ConditionalOperator>(S), Ctx); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 225 | case Stmt::BinaryConditionalOperatorClass: |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 226 | return translateAbstractConditionalOperator( |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 227 | cast<BinaryConditionalOperator>(S), Ctx); |
| 228 | |
| 229 | // We treat these as no-ops |
| 230 | case Stmt::ParenExprClass: |
| 231 | return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx); |
| 232 | case Stmt::ExprWithCleanupsClass: |
| 233 | return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx); |
| 234 | case Stmt::CXXBindTemporaryExprClass: |
| 235 | return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx); |
| 236 | |
| 237 | // Collect all literals |
| 238 | case Stmt::CharacterLiteralClass: |
| 239 | case Stmt::CXXNullPtrLiteralExprClass: |
| 240 | case Stmt::GNUNullExprClass: |
| 241 | case Stmt::CXXBoolLiteralExprClass: |
| 242 | case Stmt::FloatingLiteralClass: |
| 243 | case Stmt::ImaginaryLiteralClass: |
| 244 | case Stmt::IntegerLiteralClass: |
| 245 | case Stmt::StringLiteralClass: |
| 246 | case Stmt::ObjCStringLiteralClass: |
| 247 | return new (Arena) til::Literal(cast<Expr>(S)); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 248 | |
| 249 | case Stmt::DeclStmtClass: |
| 250 | return translateDeclStmt(cast<DeclStmt>(S), Ctx); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | if (const CastExpr *CE = dyn_cast<CastExpr>(S)) |
| 255 | return translateCastExpr(CE, Ctx); |
| 256 | |
| 257 | return new (Arena) til::Undefined(S); |
| 258 | } |
| 259 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 260 | til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE, |
| 261 | CallingContext *Ctx) { |
| 262 | const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 263 | |
| 264 | // Function parameters require substitution and/or renaming. |
| 265 | if (const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(VD)) { |
| 266 | const FunctionDecl *FD = |
| 267 | cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl(); |
| 268 | unsigned I = PV->getFunctionScopeIndex(); |
| 269 | |
| 270 | if (Ctx && Ctx->FunArgs && FD == Ctx->AttrDecl->getCanonicalDecl()) { |
| 271 | // Substitute call arguments for references to function parameters |
| 272 | assert(I < Ctx->NumArgs); |
| 273 | return translate(Ctx->FunArgs[I], Ctx->Prev); |
| 274 | } |
| 275 | // Map the param back to the param of the original function declaration |
| 276 | // for consistent comparisons. |
| 277 | VD = FD->getParamDecl(I); |
| 278 | } |
| 279 | |
DeLesley Hutchins | dc0541f | 2015-09-29 15:25:51 +0000 | [diff] [blame] | 280 | // For non-local variables, treat it as a reference to a named object. |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 281 | return new (Arena) til::LiteralPtr(VD); |
| 282 | } |
| 283 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 284 | til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE, |
| 285 | CallingContext *Ctx) { |
| 286 | // Substitute for 'this' |
| 287 | if (Ctx && Ctx->SelfArg) |
| 288 | return translate(Ctx->SelfArg, Ctx->Prev); |
| 289 | assert(SelfVar && "We have no variable for 'this'!"); |
| 290 | return SelfVar; |
| 291 | } |
| 292 | |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 293 | static const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 294 | if (auto *V = dyn_cast<til::Variable>(E)) |
| 295 | return V->clangDecl(); |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 296 | if (auto *Ph = dyn_cast<til::Phi>(E)) |
| 297 | return Ph->clangDecl(); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 298 | if (auto *P = dyn_cast<til::Project>(E)) |
| 299 | return P->clangDecl(); |
| 300 | if (auto *L = dyn_cast<til::LiteralPtr>(E)) |
| 301 | return L->clangDecl(); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 302 | return nullptr; |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 305 | static bool hasCppPointerType(const til::SExpr *E) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 306 | auto *VD = getValueDeclFromSExpr(E); |
| 307 | if (VD && VD->getType()->isPointerType()) |
| 308 | return true; |
| 309 | if (auto *C = dyn_cast<til::Cast>(E)) |
| 310 | return C->castOpcode() == til::CAST_objToPtr; |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 315 | // Grab the very first declaration of virtual method D |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 316 | static const CXXMethodDecl *getFirstVirtualDecl(const CXXMethodDecl *D) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 317 | while (true) { |
| 318 | D = D->getCanonicalDecl(); |
| 319 | CXXMethodDecl::method_iterator I = D->begin_overridden_methods(), |
| 320 | E = D->end_overridden_methods(); |
| 321 | if (I == E) |
| 322 | return D; // Method does not override anything |
| 323 | D = *I; // FIXME: this does not work with multiple inheritance. |
| 324 | } |
| 325 | return nullptr; |
| 326 | } |
| 327 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 328 | til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME, |
| 329 | CallingContext *Ctx) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 330 | til::SExpr *BE = translate(ME->getBase(), Ctx); |
| 331 | til::SExpr *E = new (Arena) til::SApply(BE); |
| 332 | |
Richard Smith | dd55b95 | 2015-08-12 02:17:52 +0000 | [diff] [blame] | 333 | const ValueDecl *D = |
| 334 | cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl()); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 335 | if (auto *VD = dyn_cast<CXXMethodDecl>(D)) |
| 336 | D = getFirstVirtualDecl(VD); |
| 337 | |
| 338 | til::Project *P = new (Arena) til::Project(E, D); |
| 339 | if (hasCppPointerType(BE)) |
| 340 | P->setArrow(true); |
| 341 | return P; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 342 | } |
| 343 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 344 | til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE, |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 345 | CallingContext *Ctx, |
| 346 | const Expr *SelfE) { |
| 347 | if (CapabilityExprMode) { |
| 348 | // Handle LOCK_RETURNED |
| 349 | const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl(); |
| 350 | if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) { |
| 351 | CallingContext LRCallCtx(Ctx); |
| 352 | LRCallCtx.AttrDecl = CE->getDirectCallee(); |
| 353 | LRCallCtx.SelfArg = SelfE; |
| 354 | LRCallCtx.NumArgs = CE->getNumArgs(); |
| 355 | LRCallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 356 | return const_cast<til::SExpr*>( |
| 357 | translateAttrExpr(At->getArg(), &LRCallCtx).sexpr()); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 361 | til::SExpr *E = translate(CE->getCallee(), Ctx); |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 362 | for (const auto *Arg : CE->arguments()) { |
| 363 | til::SExpr *A = translate(Arg, Ctx); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 364 | E = new (Arena) til::Apply(E, A); |
| 365 | } |
| 366 | return new (Arena) til::Call(E, CE); |
| 367 | } |
| 368 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 369 | til::SExpr *SExprBuilder::translateCXXMemberCallExpr( |
| 370 | const CXXMemberCallExpr *ME, CallingContext *Ctx) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 371 | if (CapabilityExprMode) { |
| 372 | // Ignore calls to get() on smart pointers. |
| 373 | if (ME->getMethodDecl()->getNameAsString() == "get" && |
| 374 | ME->getNumArgs() == 0) { |
| 375 | auto *E = translate(ME->getImplicitObjectArgument(), Ctx); |
| 376 | return new (Arena) til::Cast(til::CAST_objToPtr, E); |
| 377 | // return E; |
| 378 | } |
| 379 | } |
| 380 | return translateCallExpr(cast<CallExpr>(ME), Ctx, |
| 381 | ME->getImplicitObjectArgument()); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 382 | } |
| 383 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 384 | til::SExpr *SExprBuilder::translateCXXOperatorCallExpr( |
| 385 | const CXXOperatorCallExpr *OCE, CallingContext *Ctx) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 386 | if (CapabilityExprMode) { |
| 387 | // Ignore operator * and operator -> on smart pointers. |
| 388 | OverloadedOperatorKind k = OCE->getOperator(); |
| 389 | if (k == OO_Star || k == OO_Arrow) { |
| 390 | auto *E = translate(OCE->getArg(0), Ctx); |
| 391 | return new (Arena) til::Cast(til::CAST_objToPtr, E); |
| 392 | // return E; |
| 393 | } |
| 394 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 395 | return translateCallExpr(cast<CallExpr>(OCE), Ctx); |
| 396 | } |
| 397 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 398 | til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO, |
| 399 | CallingContext *Ctx) { |
| 400 | switch (UO->getOpcode()) { |
| 401 | case UO_PostInc: |
| 402 | case UO_PostDec: |
| 403 | case UO_PreInc: |
| 404 | case UO_PreDec: |
| 405 | return new (Arena) til::Undefined(UO); |
| 406 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 407 | case UO_AddrOf: { |
| 408 | if (CapabilityExprMode) { |
| 409 | // interpret &Graph::mu_ as an existential. |
| 410 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) { |
| 411 | if (DRE->getDecl()->isCXXInstanceMember()) { |
| 412 | // This is a pointer-to-member expression, e.g. &MyClass::mu_. |
| 413 | // We interpret this syntax specially, as a wildcard. |
| 414 | auto *W = new (Arena) til::Wildcard(); |
| 415 | return new (Arena) til::Project(W, DRE->getDecl()); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | // otherwise, & is a no-op |
| 420 | return translate(UO->getSubExpr(), Ctx); |
| 421 | } |
| 422 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 423 | // We treat these as no-ops |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 424 | case UO_Deref: |
| 425 | case UO_Plus: |
| 426 | return translate(UO->getSubExpr(), Ctx); |
| 427 | |
| 428 | case UO_Minus: |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 429 | return new (Arena) |
| 430 | til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx)); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 431 | case UO_Not: |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 432 | return new (Arena) |
| 433 | til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx)); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 434 | case UO_LNot: |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 435 | return new (Arena) |
| 436 | til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx)); |
| 437 | |
| 438 | // Currently unsupported |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 439 | case UO_Real: |
| 440 | case UO_Imag: |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 441 | case UO_Extension: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 442 | case UO_Coawait: |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 443 | return new (Arena) til::Undefined(UO); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 444 | } |
| 445 | return new (Arena) til::Undefined(UO); |
| 446 | } |
| 447 | |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 448 | til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op, |
| 449 | const BinaryOperator *BO, |
| 450 | CallingContext *Ctx, bool Reverse) { |
| 451 | til::SExpr *E0 = translate(BO->getLHS(), Ctx); |
| 452 | til::SExpr *E1 = translate(BO->getRHS(), Ctx); |
| 453 | if (Reverse) |
| 454 | return new (Arena) til::BinaryOp(Op, E1, E0); |
| 455 | else |
| 456 | return new (Arena) til::BinaryOp(Op, E0, E1); |
| 457 | } |
| 458 | |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 459 | til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op, |
| 460 | const BinaryOperator *BO, |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 461 | CallingContext *Ctx, |
| 462 | bool Assign) { |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 463 | const Expr *LHS = BO->getLHS(); |
| 464 | const Expr *RHS = BO->getRHS(); |
| 465 | til::SExpr *E0 = translate(LHS, Ctx); |
| 466 | til::SExpr *E1 = translate(RHS, Ctx); |
| 467 | |
| 468 | const ValueDecl *VD = nullptr; |
| 469 | til::SExpr *CV = nullptr; |
| 470 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHS)) { |
| 471 | VD = DRE->getDecl(); |
| 472 | CV = lookupVarDecl(VD); |
| 473 | } |
| 474 | |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 475 | if (!Assign) { |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 476 | til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0); |
| 477 | E1 = new (Arena) til::BinaryOp(Op, Arg, E1); |
| 478 | E1 = addStatement(E1, nullptr, VD); |
| 479 | } |
| 480 | if (VD && CV) |
| 481 | return updateVarDecl(VD, E1); |
| 482 | return new (Arena) til::Store(E0, E1); |
| 483 | } |
| 484 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 485 | til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO, |
| 486 | CallingContext *Ctx) { |
| 487 | switch (BO->getOpcode()) { |
| 488 | case BO_PtrMemD: |
| 489 | case BO_PtrMemI: |
| 490 | return new (Arena) til::Undefined(BO); |
| 491 | |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 492 | case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx); |
| 493 | case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx); |
| 494 | case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx); |
| 495 | case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx); |
| 496 | case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx); |
| 497 | case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx); |
| 498 | case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx); |
| 499 | case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx); |
| 500 | case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true); |
| 501 | case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx); |
| 502 | case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true); |
| 503 | case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx); |
| 504 | case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx); |
| 505 | case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx); |
| 506 | case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx); |
| 507 | case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx); |
| 508 | case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx); |
| 509 | case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx); |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 510 | |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 511 | case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true); |
| 512 | case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx); |
| 513 | case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx); |
| 514 | case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx); |
| 515 | case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx); |
| 516 | case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx); |
| 517 | case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx); |
| 518 | case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx); |
| 519 | case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx); |
| 520 | case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx); |
| 521 | case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 522 | |
| 523 | case BO_Comma: |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 524 | // The clang CFG should have already processed both sides. |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 525 | return translate(BO->getRHS(), Ctx); |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 526 | } |
DeLesley Hutchins | 7834001 | 2014-04-22 14:51:04 +0000 | [diff] [blame] | 527 | return new (Arena) til::Undefined(BO); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 528 | } |
| 529 | |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 530 | til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE, |
| 531 | CallingContext *Ctx) { |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 532 | clang::CastKind K = CE->getCastKind(); |
| 533 | switch (K) { |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 534 | case CK_LValueToRValue: { |
| 535 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) { |
| 536 | til::SExpr *E0 = lookupVarDecl(DRE->getDecl()); |
| 537 | if (E0) |
| 538 | return E0; |
| 539 | } |
| 540 | til::SExpr *E0 = translate(CE->getSubExpr(), Ctx); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 541 | return E0; |
| 542 | // FIXME!! -- get Load working properly |
| 543 | // return new (Arena) til::Load(E0); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 544 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 545 | case CK_NoOp: |
| 546 | case CK_DerivedToBase: |
| 547 | case CK_UncheckedDerivedToBase: |
| 548 | case CK_ArrayToPointerDecay: |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 549 | case CK_FunctionToPointerDecay: { |
| 550 | til::SExpr *E0 = translate(CE->getSubExpr(), Ctx); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 551 | return E0; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 552 | } |
| 553 | default: { |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 554 | // FIXME: handle different kinds of casts. |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 555 | til::SExpr *E0 = translate(CE->getSubExpr(), Ctx); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 556 | if (CapabilityExprMode) |
| 557 | return E0; |
DeLesley Hutchins | f4b5e7c | 2014-05-15 00:50:36 +0000 | [diff] [blame] | 558 | return new (Arena) til::Cast(til::CAST_none, E0); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 559 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 560 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 563 | til::SExpr * |
| 564 | SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E, |
| 565 | CallingContext *Ctx) { |
DeLesley Hutchins | f1a3116 | 2014-04-22 17:31:23 +0000 | [diff] [blame] | 566 | til::SExpr *E0 = translate(E->getBase(), Ctx); |
| 567 | til::SExpr *E1 = translate(E->getIdx(), Ctx); |
DeLesley Hutchins | 44be81b | 2014-05-28 21:28:13 +0000 | [diff] [blame] | 568 | return new (Arena) til::ArrayIndex(E0, E1); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Aaron Ballman | 3f993c1 | 2014-04-09 17:45:44 +0000 | [diff] [blame] | 571 | til::SExpr * |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 572 | SExprBuilder::translateAbstractConditionalOperator( |
| 573 | const AbstractConditionalOperator *CO, CallingContext *Ctx) { |
| 574 | auto *C = translate(CO->getCond(), Ctx); |
| 575 | auto *T = translate(CO->getTrueExpr(), Ctx); |
| 576 | auto *E = translate(CO->getFalseExpr(), Ctx); |
| 577 | return new (Arena) til::IfThenElse(C, T, E); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 578 | } |
| 579 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 580 | til::SExpr * |
| 581 | SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) { |
| 582 | DeclGroupRef DGrp = S->getDeclGroup(); |
| 583 | for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) { |
| 584 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) { |
| 585 | Expr *E = VD->getInit(); |
| 586 | til::SExpr* SE = translate(E, Ctx); |
DeLesley Hutchins | 7e615c2 | 2014-04-09 22:39:43 +0000 | [diff] [blame] | 587 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 588 | // Add local variables with trivial type to the variable map |
| 589 | QualType T = VD->getType(); |
| 590 | if (T.isTrivialType(VD->getASTContext())) { |
| 591 | return addVarDecl(VD, SE); |
| 592 | } |
| 593 | else { |
| 594 | // TODO: add alloca |
| 595 | } |
| 596 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 597 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 598 | return nullptr; |
| 599 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 600 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 601 | // If (E) is non-trivial, then add it to the current basic block, and |
| 602 | // update the statement map so that S refers to E. Returns a new variable |
| 603 | // that refers to E. |
| 604 | // If E is trivial returns E. |
| 605 | til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S, |
| 606 | const ValueDecl *VD) { |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 607 | if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E)) |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 608 | return E; |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 609 | if (VD) |
| 610 | E = new (Arena) til::Variable(E, VD); |
| 611 | CurrentInstructions.push_back(E); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 612 | if (S) |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 613 | insertStmt(S, E); |
| 614 | return E; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 615 | } |
| 616 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 617 | // Returns the current value of VD, if known, and nullptr otherwise. |
| 618 | til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 619 | auto It = LVarIdxMap.find(VD); |
| 620 | if (It != LVarIdxMap.end()) { |
| 621 | assert(CurrentLVarMap[It->second].first == VD); |
| 622 | return CurrentLVarMap[It->second].second; |
| 623 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 624 | return nullptr; |
| 625 | } |
| 626 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 627 | // if E is a til::Variable, update its clangDecl. |
Benjamin Kramer | 66a97ee | 2015-03-09 14:19:54 +0000 | [diff] [blame] | 628 | static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) { |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 629 | if (!E) |
| 630 | return; |
| 631 | if (til::Variable *V = dyn_cast<til::Variable>(E)) { |
| 632 | if (!V->clangDecl()) |
| 633 | V->setClangDecl(VD); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 634 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 635 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 636 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 637 | // Adds a new variable declaration. |
| 638 | til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) { |
| 639 | maybeUpdateVD(E, VD); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 640 | LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size())); |
| 641 | CurrentLVarMap.makeWritable(); |
| 642 | CurrentLVarMap.push_back(std::make_pair(VD, E)); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 643 | return E; |
| 644 | } |
| 645 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 646 | // Updates a current variable declaration. (E.g. by assignment) |
| 647 | til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) { |
| 648 | maybeUpdateVD(E, VD); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 649 | auto It = LVarIdxMap.find(VD); |
| 650 | if (It == LVarIdxMap.end()) { |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 651 | til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD); |
| 652 | til::SExpr *St = new (Arena) til::Store(Ptr, E); |
| 653 | return St; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 654 | } |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 655 | CurrentLVarMap.makeWritable(); |
| 656 | CurrentLVarMap.elem(It->second).second = E; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 657 | return E; |
| 658 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 659 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 660 | // Make a Phi node in the current block for the i^th variable in CurrentVarMap. |
| 661 | // If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E. |
| 662 | // If E == null, this is a backedge and will be set later. |
| 663 | void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) { |
| 664 | unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors; |
| 665 | assert(ArgIndex > 0 && ArgIndex < NPreds); |
| 666 | |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 667 | til::SExpr *CurrE = CurrentLVarMap[i].second; |
| 668 | if (CurrE->block() == CurrentBB) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 669 | // We already have a Phi node in the current block, |
| 670 | // so just add the new variable to the Phi node. |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 671 | til::Phi *Ph = dyn_cast<til::Phi>(CurrE); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 672 | assert(Ph && "Expecting Phi node."); |
| 673 | if (E) |
| 674 | Ph->values()[ArgIndex] = E; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 675 | return; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 676 | } |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 677 | |
| 678 | // Make a new phi node: phi(..., E) |
| 679 | // All phi args up to the current index are set to the current value. |
| 680 | til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds); |
| 681 | Ph->values().setValues(NPreds, nullptr); |
| 682 | for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx) |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 683 | Ph->values()[PIdx] = CurrE; |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 684 | if (E) |
| 685 | Ph->values()[ArgIndex] = E; |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 686 | Ph->setClangDecl(CurrentLVarMap[i].first); |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 687 | // If E is from a back-edge, or either E or CurrE are incomplete, then |
| 688 | // mark this node as incomplete; we may need to remove it later. |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 689 | if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE)) { |
DeLesley Hutchins | a9db001 | 2014-04-19 03:54:41 +0000 | [diff] [blame] | 690 | Ph->setStatus(til::Phi::PH_Incomplete); |
| 691 | } |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 692 | |
| 693 | // Add Phi node to current block, and update CurrentLVarMap[i] |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 694 | CurrentArguments.push_back(Ph); |
DeLesley Hutchins | a9db001 | 2014-04-19 03:54:41 +0000 | [diff] [blame] | 695 | if (Ph->status() == til::Phi::PH_Incomplete) |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 696 | IncompleteArgs.push_back(Ph); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 697 | |
| 698 | CurrentLVarMap.makeWritable(); |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 699 | CurrentLVarMap.elem(i).second = Ph; |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 700 | } |
| 701 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 702 | // Merge values from Map into the current variable map. |
| 703 | // This will construct Phi nodes in the current basic block as necessary. |
| 704 | void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) { |
| 705 | assert(CurrentBlockInfo && "Not processing a block!"); |
| 706 | |
| 707 | if (!CurrentLVarMap.valid()) { |
| 708 | // Steal Map, using copy-on-write. |
| 709 | CurrentLVarMap = std::move(Map); |
| 710 | return; |
| 711 | } |
| 712 | if (CurrentLVarMap.sameAs(Map)) |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 713 | return; // Easy merge: maps from different predecessors are unchanged. |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 714 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 715 | unsigned NPreds = CurrentBB->numPredecessors(); |
| 716 | unsigned ESz = CurrentLVarMap.size(); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 717 | unsigned MSz = Map.size(); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 718 | unsigned Sz = std::min(ESz, MSz); |
| 719 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 720 | for (unsigned i=0; i<Sz; ++i) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 721 | if (CurrentLVarMap[i].first != Map[i].first) { |
| 722 | // We've reached the end of variables in common. |
| 723 | CurrentLVarMap.makeWritable(); |
| 724 | CurrentLVarMap.downsize(i); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 725 | break; |
| 726 | } |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 727 | if (CurrentLVarMap[i].second != Map[i].second) |
| 728 | makePhiNodeVar(i, NPreds, Map[i].second); |
DeLesley Hutchins | 11bb3087 | 2014-04-07 22:56:24 +0000 | [diff] [blame] | 729 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 730 | if (ESz > MSz) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 731 | CurrentLVarMap.makeWritable(); |
| 732 | CurrentLVarMap.downsize(Map.size()); |
| 733 | } |
| 734 | } |
| 735 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 736 | // Merge a back edge into the current variable map. |
| 737 | // This will create phi nodes for all variables in the variable map. |
| 738 | void SExprBuilder::mergeEntryMapBackEdge() { |
| 739 | // We don't have definitions for variables on the backedge, because we |
| 740 | // haven't gotten that far in the CFG. Thus, when encountering a back edge, |
| 741 | // we conservatively create Phi nodes for all variables. Unnecessary Phi |
| 742 | // nodes will be marked as incomplete, and stripped out at the end. |
| 743 | // |
| 744 | // An Phi node is unnecessary if it only refers to itself and one other |
| 745 | // variable, e.g. x = Phi(y, y, x) can be reduced to x = y. |
| 746 | |
| 747 | assert(CurrentBlockInfo && "Not processing a block!"); |
| 748 | |
| 749 | if (CurrentBlockInfo->HasBackEdges) |
| 750 | return; |
| 751 | CurrentBlockInfo->HasBackEdges = true; |
| 752 | |
| 753 | CurrentLVarMap.makeWritable(); |
| 754 | unsigned Sz = CurrentLVarMap.size(); |
| 755 | unsigned NPreds = CurrentBB->numPredecessors(); |
| 756 | |
| 757 | for (unsigned i=0; i < Sz; ++i) { |
| 758 | makePhiNodeVar(i, NPreds, nullptr); |
| 759 | } |
| 760 | } |
| 761 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 762 | // Update the phi nodes that were initially created for a back edge |
| 763 | // once the variable definitions have been computed. |
| 764 | // I.e., merge the current variable map into the phi nodes for Blk. |
| 765 | void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) { |
| 766 | til::BasicBlock *BB = lookupBlock(Blk); |
| 767 | unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors; |
| 768 | assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors()); |
| 769 | |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 770 | for (til::SExpr *PE : BB->arguments()) { |
| 771 | til::Phi *Ph = dyn_cast_or_null<til::Phi>(PE); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 772 | assert(Ph && "Expecting Phi Node."); |
| 773 | assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge."); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 774 | |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 775 | til::SExpr *E = lookupVarDecl(Ph->clangDecl()); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 776 | assert(E && "Couldn't find local variable for Phi node."); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 777 | Ph->values()[ArgIndex] = E; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 778 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 779 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 780 | |
Benjamin Kramer | a7bcab7 | 2014-05-09 17:08:01 +0000 | [diff] [blame] | 781 | void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D, |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 782 | const CFGBlock *First) { |
| 783 | // Perform initial setup operations. |
| 784 | unsigned NBlocks = Cfg->getNumBlockIDs(); |
| 785 | Scfg = new (Arena) til::SCFG(Arena, NBlocks); |
| 786 | |
| 787 | // allocate all basic blocks immediately, to handle forward references. |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 788 | BBInfo.resize(NBlocks); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 789 | BlockMap.resize(NBlocks, nullptr); |
| 790 | // create map from clang blockID to til::BasicBlocks |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 791 | for (auto *B : *Cfg) { |
DeLesley Hutchins | 44be81b | 2014-05-28 21:28:13 +0000 | [diff] [blame] | 792 | auto *BB = new (Arena) til::BasicBlock(Arena); |
| 793 | BB->reserveInstructions(B->size()); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 794 | BlockMap[B->getBlockID()] = BB; |
DeLesley Hutchins | 11bb3087 | 2014-04-07 22:56:24 +0000 | [diff] [blame] | 795 | } |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 796 | |
| 797 | CurrentBB = lookupBlock(&Cfg->getEntry()); |
Benjamin Kramer | a7bcab7 | 2014-05-09 17:08:01 +0000 | [diff] [blame] | 798 | auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters() |
| 799 | : cast<FunctionDecl>(D)->parameters(); |
| 800 | for (auto *Pm : Parms) { |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 801 | QualType T = Pm->getType(); |
| 802 | if (!T.isTrivialType(Pm->getASTContext())) |
| 803 | continue; |
| 804 | |
| 805 | // Add parameters to local variable map. |
| 806 | // FIXME: right now we emulate params with loads; that should be fixed. |
| 807 | til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm); |
| 808 | til::SExpr *Ld = new (Arena) til::Load(Lp); |
| 809 | til::SExpr *V = addStatement(Ld, nullptr, Pm); |
| 810 | addVarDecl(Pm, V); |
| 811 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 812 | } |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 813 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 814 | void SExprBuilder::enterCFGBlock(const CFGBlock *B) { |
| 815 | // Intialize TIL basic block and add it to the CFG. |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 816 | CurrentBB = lookupBlock(B); |
DeLesley Hutchins | 44be81b | 2014-05-28 21:28:13 +0000 | [diff] [blame] | 817 | CurrentBB->reservePredecessors(B->pred_size()); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 818 | Scfg->add(CurrentBB); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 819 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 820 | CurrentBlockInfo = &BBInfo[B->getBlockID()]; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 821 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 822 | // CurrentLVarMap is moved to ExitMap on block exit. |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 823 | // FIXME: the entry block will hold function parameters. |
| 824 | // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized."); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 825 | } |
| 826 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 827 | void SExprBuilder::handlePredecessor(const CFGBlock *Pred) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 828 | // Compute CurrentLVarMap on entry from ExitMaps of predecessors |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 829 | |
DeLesley Hutchins | 44be81b | 2014-05-28 21:28:13 +0000 | [diff] [blame] | 830 | CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 831 | BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()]; |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 832 | assert(PredInfo->UnprocessedSuccessors > 0); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 833 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 834 | if (--PredInfo->UnprocessedSuccessors == 0) |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 835 | mergeEntryMap(std::move(PredInfo->ExitMap)); |
| 836 | else |
| 837 | mergeEntryMap(PredInfo->ExitMap.clone()); |
| 838 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 839 | ++CurrentBlockInfo->ProcessedPredecessors; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 840 | } |
| 841 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 842 | void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 843 | mergeEntryMapBackEdge(); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 844 | } |
| 845 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 846 | void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) { |
| 847 | // The merge*() methods have created arguments. |
| 848 | // Push those arguments onto the basic block. |
| 849 | CurrentBB->arguments().reserve( |
| 850 | static_cast<unsigned>(CurrentArguments.size()), Arena); |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 851 | for (auto *A : CurrentArguments) |
| 852 | CurrentBB->addArgument(A); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 853 | } |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 854 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 855 | void SExprBuilder::handleStatement(const Stmt *S) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 856 | til::SExpr *E = translate(S, nullptr); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 857 | addStatement(E, S); |
| 858 | } |
| 859 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 860 | void SExprBuilder::handleDestructorCall(const VarDecl *VD, |
| 861 | const CXXDestructorDecl *DD) { |
| 862 | til::SExpr *Sf = new (Arena) til::LiteralPtr(VD); |
| 863 | til::SExpr *Dr = new (Arena) til::LiteralPtr(DD); |
| 864 | til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf); |
| 865 | til::SExpr *E = new (Arena) til::Call(Ap); |
| 866 | addStatement(E, nullptr); |
| 867 | } |
| 868 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 869 | void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 870 | CurrentBB->instructions().reserve( |
| 871 | static_cast<unsigned>(CurrentInstructions.size()), Arena); |
| 872 | for (auto *V : CurrentInstructions) |
| 873 | CurrentBB->addInstruction(V); |
| 874 | |
| 875 | // Create an appropriate terminator |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 876 | unsigned N = B->succ_size(); |
| 877 | auto It = B->succ_begin(); |
| 878 | if (N == 1) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 879 | til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 880 | // TODO: set index |
DeLesley Hutchins | b603192 | 2014-05-29 21:24:16 +0000 | [diff] [blame] | 881 | unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0; |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 882 | auto *Tm = new (Arena) til::Goto(BB, Idx); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 883 | CurrentBB->setTerminator(Tm); |
| 884 | } |
| 885 | else if (N == 2) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 886 | til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 887 | til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 888 | ++It; |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 889 | til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr; |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 890 | // FIXME: make sure these arent' critical edges. |
| 891 | auto *Tm = new (Arena) til::Branch(C, BB1, BB2); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 892 | CurrentBB->setTerminator(Tm); |
| 893 | } |
| 894 | } |
| 895 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 896 | void SExprBuilder::handleSuccessor(const CFGBlock *Succ) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 897 | ++CurrentBlockInfo->UnprocessedSuccessors; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 898 | } |
| 899 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 900 | void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) { |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 901 | mergePhiNodesBackEdge(Succ); |
| 902 | ++BBInfo[Succ->getBlockID()].ProcessedPredecessors; |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 903 | } |
| 904 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 905 | void SExprBuilder::exitCFGBlock(const CFGBlock *B) { |
DeLesley Hutchins | f8b412a | 2014-04-21 23:18:18 +0000 | [diff] [blame] | 906 | CurrentArguments.clear(); |
| 907 | CurrentInstructions.clear(); |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 908 | CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 909 | CurrentBB = nullptr; |
| 910 | CurrentBlockInfo = nullptr; |
| 911 | } |
| 912 | |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 913 | void SExprBuilder::exitCFG(const CFGBlock *Last) { |
DeLesley Hutchins | 4e38f10 | 2014-09-10 22:12:52 +0000 | [diff] [blame] | 914 | for (auto *Ph : IncompleteArgs) { |
| 915 | if (Ph->status() == til::Phi::PH_Incomplete) |
| 916 | simplifyIncompleteArg(Ph); |
DeLesley Hutchins | a9db001 | 2014-04-19 03:54:41 +0000 | [diff] [blame] | 917 | } |
| 918 | |
DeLesley Hutchins | ae497de | 2014-04-19 00:35:54 +0000 | [diff] [blame] | 919 | CurrentArguments.clear(); |
| 920 | CurrentInstructions.clear(); |
DeLesley Hutchins | a9db001 | 2014-04-19 03:54:41 +0000 | [diff] [blame] | 921 | IncompleteArgs.clear(); |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 922 | } |
| 923 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 924 | /* |
DeLesley Hutchins | aab9aff | 2014-04-15 23:23:19 +0000 | [diff] [blame] | 925 | void printSCFG(CFGWalker &Walker) { |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 926 | llvm::BumpPtrAllocator Bpa; |
| 927 | til::MemRegionRef Arena(&Bpa); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 928 | SExprBuilder SxBuilder(Arena); |
| 929 | til::SCFG *Scfg = SxBuilder.buildCFG(Walker); |
| 930 | TILPrinter::print(Scfg, llvm::errs()); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 931 | } |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 932 | */ |