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