blob: 0c09974cf4e68080f415dc93b37b79ee2fc938a8 [file] [log] [blame]
Eugene Zelenkobc324332018-03-13 21:32:01 +00001//===- ThreadSafetyCommon.cpp ---------------------------------------------===//
DeLesley Hutchinsb2213912014-04-07 18:09:54 +00002//
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 Ballman28347a72014-04-09 21:12:04 +000014#include "clang/Analysis/Analyses/ThreadSafetyCommon.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000015#include "clang/AST/Attr.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000016#include "clang/AST/Decl.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000017#include "clang/AST/DeclCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000018#include "clang/AST/DeclGroup.h"
Benjamin Kramera7bcab72014-05-09 17:08:01 +000019#include "clang/AST/DeclObjC.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000020#include "clang/AST/Expr.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000021#include "clang/AST/ExprCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000022#include "clang/AST/OperationKinds.h"
23#include "clang/AST/Stmt.h"
24#include "clang/AST/Type.h"
DeLesley Hutchinsf7813c52014-04-08 22:21:22 +000025#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000026#include "clang/Analysis/CFG.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000027#include "clang/Basic/LLVM.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000028#include "clang/Basic/OperatorKinds.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000029#include "clang/Basic/Specifiers.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000030#include "llvm/ADT/StringRef.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000031#include "llvm/Support/Casting.h"
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000032#include <algorithm>
Eugene Zelenkobc324332018-03-13 21:32:01 +000033#include <cassert>
34#include <string>
35#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000036
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000037using namespace clang;
38using namespace threadSafety;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000039
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +000040// From ThreadSafetyUtil.h
Eugene Zelenkobc324332018-03-13 21:32:01 +000041std::string threadSafety::getSourceLiteralString(const Expr *CE) {
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +000042 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 Hutchinsf8b412a2014-04-21 23:18:18 +000063// Return true if E is a variable that points to an incomplete Phi node.
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000064static 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 Hutchinsf8b412a2014-04-21 23:18:18 +000067 return false;
68}
69
Eugene Zelenkobc324332018-03-13 21:32:01 +000070using CallingContext = SExprBuilder::CallingContext;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000071
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000072til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000073 auto It = SMap.find(S);
74 if (It != SMap.end())
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000075 return It->second;
Aaron Ballman3f993c12014-04-09 17:45:44 +000076 return nullptr;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000077}
78
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000079til::SCFG *SExprBuilder::buildCFG(CFGWalker &Walker) {
80 Walker.walk(*this);
81 return Scfg;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000082}
83
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000084static bool isCalleeArrow(const Expr *E) {
Eugene Zelenkobc324332018-03-13 21:32:01 +000085 const auto *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +000086 return ME ? ME->isArrow() : false;
87}
88
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000089/// Translate a clang expression in an attribute to a til::SExpr.
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +000090/// 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 Hutchins42665222014-08-04 16:10:59 +000096CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
97 const NamedDecl *D,
98 const Expr *DeclExp,
99 VarDecl *SelfDecl) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000100 // 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 Zelenkobc324332018-03-13 21:32:01 +0000108 if (const auto *ME = dyn_cast<MemberExpr>(DeclExp)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000109 Ctx.SelfArg = ME->getBase();
110 Ctx.SelfArrow = ME->isArrow();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000111 } else if (const auto *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000112 Ctx.SelfArg = CE->getImplicitObjectArgument();
113 Ctx.SelfArrow = isCalleeArrow(CE->getCallee());
114 Ctx.NumArgs = CE->getNumArgs();
115 Ctx.FunArgs = CE->getArgs();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000116 } else if (const auto *CE = dyn_cast<CallExpr>(DeclExp)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000117 Ctx.NumArgs = CE->getNumArgs();
118 Ctx.FunArgs = CE->getArgs();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000119 } else if (const auto *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000120 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 Prantl9fc8faf2018-05-09 01:00:01 +0000149/// Translate a clang expression in an attribute to a til::SExpr.
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000150// This assumes a CallingContext has already been created.
DeLesley Hutchins42665222014-08-04 16:10:59 +0000151CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
152 CallingContext *Ctx) {
153 if (!AttrExp)
154 return CapabilityExpr(nullptr, false);
155
Eugene Zelenkobc324332018-03-13 21:32:01 +0000156 if (const auto* SLit = dyn_cast<StringLiteral>(AttrExp)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000157 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 Hutchins42665222014-08-04 16:10:59 +0000160 return CapabilityExpr(new (Arena) til::Wildcard(), false);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000161 else
162 // Ignore other string literals for now.
DeLesley Hutchins42665222014-08-04 16:10:59 +0000163 return CapabilityExpr(nullptr, false);
164 }
165
166 bool Neg = false;
Eugene Zelenkobc324332018-03-13 21:32:01 +0000167 if (const auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
DeLesley Hutchins42665222014-08-04 16:10:59 +0000168 if (OE->getOperator() == OO_Exclaim) {
169 Neg = true;
170 AttrExp = OE->getArg(0);
171 }
172 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000173 else if (const auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
DeLesley Hutchins42665222014-08-04 16:10:59 +0000174 if (UO->getOpcode() == UO_LNot) {
175 Neg = true;
176 AttrExp = UO->getSubExpr();
177 }
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000178 }
179
180 til::SExpr *E = translate(AttrExp, Ctx);
181
DeLesley Hutchins42665222014-08-04 16:10:59 +0000182 // 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 Hutchinsea1f8332014-07-28 15:57:27 +0000187 // Hack to deal with smart pointers -- strip off top-level pointer casts.
Eugene Zelenkobc324332018-03-13 21:32:01 +0000188 if (const auto *CE = dyn_cast_or_null<til::Cast>(E)) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000189 if (CE->castOpcode() == til::CAST_objToPtr)
DeLesley Hutchins42665222014-08-04 16:10:59 +0000190 return CapabilityExpr(CE->expr(), Neg);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000191 }
DeLesley Hutchins42665222014-08-04 16:10:59 +0000192 return CapabilityExpr(E, Neg);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000193}
194
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000195// 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.
198til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000199 if (!S)
200 return nullptr;
201
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000202 // 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 Puchertb081f442018-09-19 23:57:38 +0000214 case Stmt::ObjCIvarRefExprClass:
215 return translateObjCIVarRefExpr(cast<ObjCIvarRefExpr>(S), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000216 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 Hutchinsf8b412a2014-04-21 23:18:18 +0000225 case Stmt::CompoundAssignOperatorClass:
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000226 return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
227
228 case Stmt::ArraySubscriptExprClass:
229 return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
230 case Stmt::ConditionalOperatorClass:
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000231 return translateAbstractConditionalOperator(
232 cast<ConditionalOperator>(S), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000233 case Stmt::BinaryConditionalOperatorClass:
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000234 return translateAbstractConditionalOperator(
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000235 cast<BinaryConditionalOperator>(S), Ctx);
236
237 // We treat these as no-ops
Bill Wendling7c44da22018-10-31 03:48:47 +0000238 case Stmt::ConstantExprClass:
239 return translate(cast<ConstantExpr>(S)->getSubExpr(), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000240 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 Smith4baaa5a2016-12-03 01:14:32 +0000246 case Stmt::MaterializeTemporaryExprClass:
247 return translate(cast<MaterializeTemporaryExpr>(S)->GetTemporaryExpr(),
248 Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000249
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 Hutchinsaab9aff2014-04-15 23:23:19 +0000261
262 case Stmt::DeclStmtClass:
263 return translateDeclStmt(cast<DeclStmt>(S), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000264 default:
265 break;
266 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000267 if (const auto *CE = dyn_cast<CastExpr>(S))
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000268 return translateCastExpr(CE, Ctx);
269
270 return new (Arena) til::Undefined(S);
271}
272
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000273til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
274 CallingContext *Ctx) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000275 const auto *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000276
277 // Function parameters require substitution and/or renaming.
Eugene Zelenkobc324332018-03-13 21:32:01 +0000278 if (const auto *PV = dyn_cast_or_null<ParmVarDecl>(VD)) {
279 const auto *FD =
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000280 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
281 unsigned I = PV->getFunctionScopeIndex();
282
283 if (Ctx && Ctx->FunArgs && FD == Ctx->AttrDecl->getCanonicalDecl()) {
284 // Substitute call arguments for references to function parameters
285 assert(I < Ctx->NumArgs);
286 return translate(Ctx->FunArgs[I], Ctx->Prev);
287 }
288 // Map the param back to the param of the original function declaration
289 // for consistent comparisons.
290 VD = FD->getParamDecl(I);
291 }
292
DeLesley Hutchinsdc0541f2015-09-29 15:25:51 +0000293 // For non-local variables, treat it as a reference to a named object.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000294 return new (Arena) til::LiteralPtr(VD);
295}
296
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000297til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
298 CallingContext *Ctx) {
299 // Substitute for 'this'
300 if (Ctx && Ctx->SelfArg)
301 return translate(Ctx->SelfArg, Ctx->Prev);
302 assert(SelfVar && "We have no variable for 'this'!");
303 return SelfVar;
304}
305
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000306static const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000307 if (const auto *V = dyn_cast<til::Variable>(E))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000308 return V->clangDecl();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000309 if (const auto *Ph = dyn_cast<til::Phi>(E))
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000310 return Ph->clangDecl();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000311 if (const auto *P = dyn_cast<til::Project>(E))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000312 return P->clangDecl();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000313 if (const auto *L = dyn_cast<til::LiteralPtr>(E))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000314 return L->clangDecl();
Hans Wennborgdcfba332015-10-06 23:40:43 +0000315 return nullptr;
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000316}
317
Aaron Puchertb081f442018-09-19 23:57:38 +0000318static bool hasAnyPointerType(const til::SExpr *E) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000319 auto *VD = getValueDeclFromSExpr(E);
Aaron Puchertb081f442018-09-19 23:57:38 +0000320 if (VD && VD->getType()->isAnyPointerType())
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000321 return true;
Eugene Zelenkobc324332018-03-13 21:32:01 +0000322 if (const auto *C = dyn_cast<til::Cast>(E))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000323 return C->castOpcode() == til::CAST_objToPtr;
324
325 return false;
326}
327
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000328// Grab the very first declaration of virtual method D
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000329static const CXXMethodDecl *getFirstVirtualDecl(const CXXMethodDecl *D) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000330 while (true) {
331 D = D->getCanonicalDecl();
Benjamin Krameracfa3392017-12-17 23:52:45 +0000332 auto OverriddenMethods = D->overridden_methods();
333 if (OverriddenMethods.begin() == OverriddenMethods.end())
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000334 return D; // Method does not override anything
Benjamin Krameracfa3392017-12-17 23:52:45 +0000335 // FIXME: this does not work with multiple inheritance.
336 D = *OverriddenMethods.begin();
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000337 }
338 return nullptr;
339}
340
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000341til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
342 CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000343 til::SExpr *BE = translate(ME->getBase(), Ctx);
344 til::SExpr *E = new (Arena) til::SApply(BE);
345
Eugene Zelenkobc324332018-03-13 21:32:01 +0000346 const auto *D = cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl());
347 if (const auto *VD = dyn_cast<CXXMethodDecl>(D))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000348 D = getFirstVirtualDecl(VD);
349
350 til::Project *P = new (Arena) til::Project(E, D);
Aaron Puchertb081f442018-09-19 23:57:38 +0000351 if (hasAnyPointerType(BE))
352 P->setArrow(true);
353 return P;
354}
355
356til::SExpr *SExprBuilder::translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
357 CallingContext *Ctx) {
358 til::SExpr *BE = translate(IVRE->getBase(), Ctx);
359 til::SExpr *E = new (Arena) til::SApply(BE);
360
361 const auto *D = cast<ObjCIvarDecl>(IVRE->getDecl()->getCanonicalDecl());
362
363 til::Project *P = new (Arena) til::Project(E, D);
364 if (hasAnyPointerType(BE))
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000365 P->setArrow(true);
366 return P;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000367}
368
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000369til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000370 CallingContext *Ctx,
371 const Expr *SelfE) {
372 if (CapabilityExprMode) {
373 // Handle LOCK_RETURNED
Aaron Puchertf6ccde72018-09-19 00:19:38 +0000374 if (const FunctionDecl *FD = CE->getDirectCallee()) {
375 FD = FD->getMostRecentDecl();
376 if (LockReturnedAttr *At = FD->getAttr<LockReturnedAttr>()) {
377 CallingContext LRCallCtx(Ctx);
378 LRCallCtx.AttrDecl = CE->getDirectCallee();
379 LRCallCtx.SelfArg = SelfE;
380 LRCallCtx.NumArgs = CE->getNumArgs();
381 LRCallCtx.FunArgs = CE->getArgs();
382 return const_cast<til::SExpr *>(
383 translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
384 }
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000385 }
386 }
387
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000388 til::SExpr *E = translate(CE->getCallee(), Ctx);
Aaron Ballman3f993c12014-04-09 17:45:44 +0000389 for (const auto *Arg : CE->arguments()) {
390 til::SExpr *A = translate(Arg, Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000391 E = new (Arena) til::Apply(E, A);
392 }
393 return new (Arena) til::Call(E, CE);
394}
395
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000396til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
397 const CXXMemberCallExpr *ME, CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000398 if (CapabilityExprMode) {
399 // Ignore calls to get() on smart pointers.
400 if (ME->getMethodDecl()->getNameAsString() == "get" &&
401 ME->getNumArgs() == 0) {
402 auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
403 return new (Arena) til::Cast(til::CAST_objToPtr, E);
404 // return E;
405 }
406 }
407 return translateCallExpr(cast<CallExpr>(ME), Ctx,
408 ME->getImplicitObjectArgument());
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000409}
410
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000411til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
412 const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000413 if (CapabilityExprMode) {
414 // Ignore operator * and operator -> on smart pointers.
415 OverloadedOperatorKind k = OCE->getOperator();
416 if (k == OO_Star || k == OO_Arrow) {
417 auto *E = translate(OCE->getArg(0), Ctx);
418 return new (Arena) til::Cast(til::CAST_objToPtr, E);
419 // return E;
420 }
421 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000422 return translateCallExpr(cast<CallExpr>(OCE), Ctx);
423}
424
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000425til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
426 CallingContext *Ctx) {
427 switch (UO->getOpcode()) {
428 case UO_PostInc:
429 case UO_PostDec:
430 case UO_PreInc:
431 case UO_PreDec:
432 return new (Arena) til::Undefined(UO);
433
Eugene Zelenkobc324332018-03-13 21:32:01 +0000434 case UO_AddrOf:
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000435 if (CapabilityExprMode) {
436 // interpret &Graph::mu_ as an existential.
Eugene Zelenkobc324332018-03-13 21:32:01 +0000437 if (const auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000438 if (DRE->getDecl()->isCXXInstanceMember()) {
439 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
440 // We interpret this syntax specially, as a wildcard.
441 auto *W = new (Arena) til::Wildcard();
442 return new (Arena) til::Project(W, DRE->getDecl());
443 }
444 }
445 }
446 // otherwise, & is a no-op
447 return translate(UO->getSubExpr(), Ctx);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000448
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000449 // We treat these as no-ops
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000450 case UO_Deref:
451 case UO_Plus:
452 return translate(UO->getSubExpr(), Ctx);
453
454 case UO_Minus:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000455 return new (Arena)
456 til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000457 case UO_Not:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000458 return new (Arena)
459 til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000460 case UO_LNot:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000461 return new (Arena)
462 til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
463
464 // Currently unsupported
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000465 case UO_Real:
466 case UO_Imag:
Aaron Ballman3f993c12014-04-09 17:45:44 +0000467 case UO_Extension:
Richard Smith9f690bd2015-10-27 06:02:45 +0000468 case UO_Coawait:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000469 return new (Arena) til::Undefined(UO);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000470 }
471 return new (Arena) til::Undefined(UO);
472}
473
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000474til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
475 const BinaryOperator *BO,
476 CallingContext *Ctx, bool Reverse) {
477 til::SExpr *E0 = translate(BO->getLHS(), Ctx);
478 til::SExpr *E1 = translate(BO->getRHS(), Ctx);
479 if (Reverse)
480 return new (Arena) til::BinaryOp(Op, E1, E0);
481 else
482 return new (Arena) til::BinaryOp(Op, E0, E1);
483}
484
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000485til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
486 const BinaryOperator *BO,
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000487 CallingContext *Ctx,
488 bool Assign) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000489 const Expr *LHS = BO->getLHS();
490 const Expr *RHS = BO->getRHS();
491 til::SExpr *E0 = translate(LHS, Ctx);
492 til::SExpr *E1 = translate(RHS, Ctx);
493
494 const ValueDecl *VD = nullptr;
495 til::SExpr *CV = nullptr;
Eugene Zelenkobc324332018-03-13 21:32:01 +0000496 if (const auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000497 VD = DRE->getDecl();
498 CV = lookupVarDecl(VD);
499 }
500
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000501 if (!Assign) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000502 til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
503 E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
504 E1 = addStatement(E1, nullptr, VD);
505 }
506 if (VD && CV)
507 return updateVarDecl(VD, E1);
508 return new (Arena) til::Store(E0, E1);
509}
510
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000511til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
512 CallingContext *Ctx) {
513 switch (BO->getOpcode()) {
514 case BO_PtrMemD:
515 case BO_PtrMemI:
516 return new (Arena) til::Undefined(BO);
517
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000518 case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx);
519 case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx);
520 case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx);
521 case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx);
522 case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx);
523 case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx);
524 case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx);
525 case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx);
526 case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true);
527 case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx);
528 case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true);
529 case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx);
530 case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx);
Richard Smithc70f1d62017-12-14 15:16:18 +0000531 case BO_Cmp: return translateBinOp(til::BOP_Cmp, BO, Ctx);
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000532 case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx);
533 case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx);
534 case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx);
535 case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
536 case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx);
Aaron Ballman3f993c12014-04-09 17:45:44 +0000537
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000538 case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true);
539 case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
540 case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
541 case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
542 case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
543 case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
544 case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
545 case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
546 case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
547 case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
548 case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000549
550 case BO_Comma:
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000551 // The clang CFG should have already processed both sides.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000552 return translate(BO->getRHS(), Ctx);
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000553 }
DeLesley Hutchins78340012014-04-22 14:51:04 +0000554 return new (Arena) til::Undefined(BO);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000555}
556
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000557til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
558 CallingContext *Ctx) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000559 CastKind K = CE->getCastKind();
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000560 switch (K) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000561 case CK_LValueToRValue: {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000562 if (const auto *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000563 til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
564 if (E0)
565 return E0;
566 }
567 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000568 return E0;
569 // FIXME!! -- get Load working properly
570 // return new (Arena) til::Load(E0);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000571 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000572 case CK_NoOp:
573 case CK_DerivedToBase:
574 case CK_UncheckedDerivedToBase:
575 case CK_ArrayToPointerDecay:
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000576 case CK_FunctionToPointerDecay: {
577 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000578 return E0;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000579 }
580 default: {
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000581 // FIXME: handle different kinds of casts.
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000582 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000583 if (CapabilityExprMode)
584 return E0;
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000585 return new (Arena) til::Cast(til::CAST_none, E0);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000586 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000587 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000588}
589
Aaron Ballman3f993c12014-04-09 17:45:44 +0000590til::SExpr *
591SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
592 CallingContext *Ctx) {
DeLesley Hutchinsf1a31162014-04-22 17:31:23 +0000593 til::SExpr *E0 = translate(E->getBase(), Ctx);
594 til::SExpr *E1 = translate(E->getIdx(), Ctx);
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000595 return new (Arena) til::ArrayIndex(E0, E1);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000596}
597
Aaron Ballman3f993c12014-04-09 17:45:44 +0000598til::SExpr *
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000599SExprBuilder::translateAbstractConditionalOperator(
600 const AbstractConditionalOperator *CO, CallingContext *Ctx) {
601 auto *C = translate(CO->getCond(), Ctx);
602 auto *T = translate(CO->getTrueExpr(), Ctx);
603 auto *E = translate(CO->getFalseExpr(), Ctx);
604 return new (Arena) til::IfThenElse(C, T, E);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000605}
606
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000607til::SExpr *
608SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
609 DeclGroupRef DGrp = S->getDeclGroup();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000610 for (auto I : DGrp) {
611 if (auto *VD = dyn_cast_or_null<VarDecl>(I)) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000612 Expr *E = VD->getInit();
613 til::SExpr* SE = translate(E, Ctx);
DeLesley Hutchins7e615c22014-04-09 22:39:43 +0000614
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000615 // Add local variables with trivial type to the variable map
616 QualType T = VD->getType();
Eugene Zelenkobc324332018-03-13 21:32:01 +0000617 if (T.isTrivialType(VD->getASTContext()))
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000618 return addVarDecl(VD, SE);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000619 else {
620 // TODO: add alloca
621 }
622 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000623 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000624 return nullptr;
625}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000626
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000627// If (E) is non-trivial, then add it to the current basic block, and
628// update the statement map so that S refers to E. Returns a new variable
629// that refers to E.
630// If E is trivial returns E.
631til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
632 const ValueDecl *VD) {
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000633 if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000634 return E;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000635 if (VD)
636 E = new (Arena) til::Variable(E, VD);
637 CurrentInstructions.push_back(E);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000638 if (S)
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000639 insertStmt(S, E);
640 return E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000641}
642
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000643// Returns the current value of VD, if known, and nullptr otherwise.
644til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000645 auto It = LVarIdxMap.find(VD);
646 if (It != LVarIdxMap.end()) {
647 assert(CurrentLVarMap[It->second].first == VD);
648 return CurrentLVarMap[It->second].second;
649 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000650 return nullptr;
651}
652
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000653// if E is a til::Variable, update its clangDecl.
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000654static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000655 if (!E)
656 return;
Eugene Zelenkobc324332018-03-13 21:32:01 +0000657 if (auto *V = dyn_cast<til::Variable>(E)) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000658 if (!V->clangDecl())
659 V->setClangDecl(VD);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000660 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000661}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000662
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000663// Adds a new variable declaration.
664til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
665 maybeUpdateVD(E, VD);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000666 LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
667 CurrentLVarMap.makeWritable();
668 CurrentLVarMap.push_back(std::make_pair(VD, E));
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000669 return E;
670}
671
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000672// Updates a current variable declaration. (E.g. by assignment)
673til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
674 maybeUpdateVD(E, VD);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000675 auto It = LVarIdxMap.find(VD);
676 if (It == LVarIdxMap.end()) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000677 til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
678 til::SExpr *St = new (Arena) til::Store(Ptr, E);
679 return St;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000680 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000681 CurrentLVarMap.makeWritable();
682 CurrentLVarMap.elem(It->second).second = E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000683 return E;
684}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000685
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000686// Make a Phi node in the current block for the i^th variable in CurrentVarMap.
687// If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
688// If E == null, this is a backedge and will be set later.
689void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
690 unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
691 assert(ArgIndex > 0 && ArgIndex < NPreds);
692
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000693 til::SExpr *CurrE = CurrentLVarMap[i].second;
694 if (CurrE->block() == CurrentBB) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000695 // We already have a Phi node in the current block,
696 // so just add the new variable to the Phi node.
Eugene Zelenkobc324332018-03-13 21:32:01 +0000697 auto *Ph = dyn_cast<til::Phi>(CurrE);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000698 assert(Ph && "Expecting Phi node.");
699 if (E)
700 Ph->values()[ArgIndex] = E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000701 return;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000702 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000703
704 // Make a new phi node: phi(..., E)
705 // All phi args up to the current index are set to the current value.
706 til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
707 Ph->values().setValues(NPreds, nullptr);
708 for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000709 Ph->values()[PIdx] = CurrE;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000710 if (E)
711 Ph->values()[ArgIndex] = E;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000712 Ph->setClangDecl(CurrentLVarMap[i].first);
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000713 // If E is from a back-edge, or either E or CurrE are incomplete, then
714 // mark this node as incomplete; we may need to remove it later.
Eugene Zelenkobc324332018-03-13 21:32:01 +0000715 if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE))
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000716 Ph->setStatus(til::Phi::PH_Incomplete);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000717
718 // Add Phi node to current block, and update CurrentLVarMap[i]
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000719 CurrentArguments.push_back(Ph);
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000720 if (Ph->status() == til::Phi::PH_Incomplete)
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000721 IncompleteArgs.push_back(Ph);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000722
723 CurrentLVarMap.makeWritable();
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000724 CurrentLVarMap.elem(i).second = Ph;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000725}
726
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000727// Merge values from Map into the current variable map.
728// This will construct Phi nodes in the current basic block as necessary.
729void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
730 assert(CurrentBlockInfo && "Not processing a block!");
731
732 if (!CurrentLVarMap.valid()) {
733 // Steal Map, using copy-on-write.
734 CurrentLVarMap = std::move(Map);
735 return;
736 }
737 if (CurrentLVarMap.sameAs(Map))
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000738 return; // Easy merge: maps from different predecessors are unchanged.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000739
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000740 unsigned NPreds = CurrentBB->numPredecessors();
741 unsigned ESz = CurrentLVarMap.size();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000742 unsigned MSz = Map.size();
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000743 unsigned Sz = std::min(ESz, MSz);
744
Eugene Zelenkobc324332018-03-13 21:32:01 +0000745 for (unsigned i = 0; i < Sz; ++i) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000746 if (CurrentLVarMap[i].first != Map[i].first) {
747 // We've reached the end of variables in common.
748 CurrentLVarMap.makeWritable();
749 CurrentLVarMap.downsize(i);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000750 break;
751 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000752 if (CurrentLVarMap[i].second != Map[i].second)
753 makePhiNodeVar(i, NPreds, Map[i].second);
DeLesley Hutchins11bb30872014-04-07 22:56:24 +0000754 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000755 if (ESz > MSz) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000756 CurrentLVarMap.makeWritable();
757 CurrentLVarMap.downsize(Map.size());
758 }
759}
760
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000761// Merge a back edge into the current variable map.
762// This will create phi nodes for all variables in the variable map.
763void SExprBuilder::mergeEntryMapBackEdge() {
764 // We don't have definitions for variables on the backedge, because we
765 // haven't gotten that far in the CFG. Thus, when encountering a back edge,
766 // we conservatively create Phi nodes for all variables. Unnecessary Phi
767 // nodes will be marked as incomplete, and stripped out at the end.
768 //
769 // An Phi node is unnecessary if it only refers to itself and one other
770 // variable, e.g. x = Phi(y, y, x) can be reduced to x = y.
771
772 assert(CurrentBlockInfo && "Not processing a block!");
773
774 if (CurrentBlockInfo->HasBackEdges)
775 return;
776 CurrentBlockInfo->HasBackEdges = true;
777
778 CurrentLVarMap.makeWritable();
779 unsigned Sz = CurrentLVarMap.size();
780 unsigned NPreds = CurrentBB->numPredecessors();
781
Eugene Zelenkobc324332018-03-13 21:32:01 +0000782 for (unsigned i = 0; i < Sz; ++i)
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000783 makePhiNodeVar(i, NPreds, nullptr);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000784}
785
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000786// Update the phi nodes that were initially created for a back edge
787// once the variable definitions have been computed.
788// I.e., merge the current variable map into the phi nodes for Blk.
789void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
790 til::BasicBlock *BB = lookupBlock(Blk);
791 unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
792 assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
793
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000794 for (til::SExpr *PE : BB->arguments()) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000795 auto *Ph = dyn_cast_or_null<til::Phi>(PE);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000796 assert(Ph && "Expecting Phi Node.");
797 assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000798
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000799 til::SExpr *E = lookupVarDecl(Ph->clangDecl());
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000800 assert(E && "Couldn't find local variable for Phi node.");
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000801 Ph->values()[ArgIndex] = E;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000802 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000803}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000804
Benjamin Kramera7bcab72014-05-09 17:08:01 +0000805void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000806 const CFGBlock *First) {
807 // Perform initial setup operations.
808 unsigned NBlocks = Cfg->getNumBlockIDs();
809 Scfg = new (Arena) til::SCFG(Arena, NBlocks);
810
811 // allocate all basic blocks immediately, to handle forward references.
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000812 BBInfo.resize(NBlocks);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000813 BlockMap.resize(NBlocks, nullptr);
814 // create map from clang blockID to til::BasicBlocks
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000815 for (auto *B : *Cfg) {
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000816 auto *BB = new (Arena) til::BasicBlock(Arena);
817 BB->reserveInstructions(B->size());
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000818 BlockMap[B->getBlockID()] = BB;
DeLesley Hutchins11bb30872014-04-07 22:56:24 +0000819 }
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000820
821 CurrentBB = lookupBlock(&Cfg->getEntry());
Benjamin Kramera7bcab72014-05-09 17:08:01 +0000822 auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
823 : cast<FunctionDecl>(D)->parameters();
824 for (auto *Pm : Parms) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000825 QualType T = Pm->getType();
826 if (!T.isTrivialType(Pm->getASTContext()))
827 continue;
828
829 // Add parameters to local variable map.
830 // FIXME: right now we emulate params with loads; that should be fixed.
831 til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
832 til::SExpr *Ld = new (Arena) til::Load(Lp);
833 til::SExpr *V = addStatement(Ld, nullptr, Pm);
834 addVarDecl(Pm, V);
835 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000836}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000837
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000838void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000839 // Initialize TIL basic block and add it to the CFG.
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000840 CurrentBB = lookupBlock(B);
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000841 CurrentBB->reservePredecessors(B->pred_size());
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000842 Scfg->add(CurrentBB);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000843
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000844 CurrentBlockInfo = &BBInfo[B->getBlockID()];
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000845
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000846 // CurrentLVarMap is moved to ExitMap on block exit.
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000847 // FIXME: the entry block will hold function parameters.
848 // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000849}
850
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000851void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000852 // Compute CurrentLVarMap on entry from ExitMaps of predecessors
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000853
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000854 CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000855 BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000856 assert(PredInfo->UnprocessedSuccessors > 0);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000857
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000858 if (--PredInfo->UnprocessedSuccessors == 0)
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000859 mergeEntryMap(std::move(PredInfo->ExitMap));
860 else
861 mergeEntryMap(PredInfo->ExitMap.clone());
862
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000863 ++CurrentBlockInfo->ProcessedPredecessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000864}
865
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000866void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000867 mergeEntryMapBackEdge();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000868}
869
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000870void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
871 // The merge*() methods have created arguments.
872 // Push those arguments onto the basic block.
873 CurrentBB->arguments().reserve(
874 static_cast<unsigned>(CurrentArguments.size()), Arena);
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000875 for (auto *A : CurrentArguments)
876 CurrentBB->addArgument(A);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000877}
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000878
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000879void SExprBuilder::handleStatement(const Stmt *S) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000880 til::SExpr *E = translate(S, nullptr);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000881 addStatement(E, S);
882}
883
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000884void SExprBuilder::handleDestructorCall(const VarDecl *VD,
885 const CXXDestructorDecl *DD) {
886 til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
887 til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
888 til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
889 til::SExpr *E = new (Arena) til::Call(Ap);
890 addStatement(E, nullptr);
891}
892
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000893void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000894 CurrentBB->instructions().reserve(
895 static_cast<unsigned>(CurrentInstructions.size()), Arena);
896 for (auto *V : CurrentInstructions)
897 CurrentBB->addInstruction(V);
898
899 // Create an appropriate terminator
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000900 unsigned N = B->succ_size();
901 auto It = B->succ_begin();
902 if (N == 1) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000903 til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000904 // TODO: set index
DeLesley Hutchinsb6031922014-05-29 21:24:16 +0000905 unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000906 auto *Tm = new (Arena) til::Goto(BB, Idx);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000907 CurrentBB->setTerminator(Tm);
908 }
909 else if (N == 2) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000910 til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000911 til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000912 ++It;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000913 til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000914 // FIXME: make sure these aren't critical edges.
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000915 auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000916 CurrentBB->setTerminator(Tm);
917 }
918}
919
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000920void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000921 ++CurrentBlockInfo->UnprocessedSuccessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000922}
923
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000924void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000925 mergePhiNodesBackEdge(Succ);
926 ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000927}
928
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000929void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000930 CurrentArguments.clear();
931 CurrentInstructions.clear();
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000932 CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000933 CurrentBB = nullptr;
934 CurrentBlockInfo = nullptr;
935}
936
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000937void SExprBuilder::exitCFG(const CFGBlock *Last) {
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000938 for (auto *Ph : IncompleteArgs) {
939 if (Ph->status() == til::Phi::PH_Incomplete)
940 simplifyIncompleteArg(Ph);
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000941 }
942
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000943 CurrentArguments.clear();
944 CurrentInstructions.clear();
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000945 IncompleteArgs.clear();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000946}
947
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000948/*
Aaron Puchert4e6afcf2018-09-21 23:46:35 +0000949namespace {
950
951class TILPrinter :
952 public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {};
953
954} // namespace
955
956namespace clang {
957namespace threadSafety {
958
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000959void printSCFG(CFGWalker &Walker) {
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000960 llvm::BumpPtrAllocator Bpa;
961 til::MemRegionRef Arena(&Bpa);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000962 SExprBuilder SxBuilder(Arena);
963 til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
964 TILPrinter::print(Scfg, llvm::errs());
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000965}
Aaron Puchert4e6afcf2018-09-21 23:46:35 +0000966
967} // namespace threadSafety
968} // namespace clang
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000969*/