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