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