blob: ffe95ea22a425e305770639dd515a6684bd6f593 [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===- ThreadSafetyCommon.cpp -----------------------------------*- C++ -*-===//
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"
16#include "clang/AST/DeclCXX.h"
Benjamin Kramera7bcab72014-05-09 17:08:01 +000017#include "clang/AST/DeclObjC.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000018#include "clang/AST/ExprCXX.h"
19#include "clang/AST/StmtCXX.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000020#include "clang/Analysis/Analyses/PostOrderCFGView.h"
DeLesley Hutchinsf7813c52014-04-08 22:21:22 +000021#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
DeLesley Hutchins7e615c22014-04-09 22:39:43 +000022#include "clang/Analysis/Analyses/ThreadSafetyTraverse.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000023#include "clang/Analysis/AnalysisContext.h"
24#include "clang/Analysis/CFG.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000025#include "clang/Basic/OperatorKinds.h"
26#include "clang/Basic/SourceLocation.h"
27#include "clang/Basic/SourceManager.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000028#include "llvm/ADT/DenseMap.h"
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000031#include <algorithm>
32#include <climits>
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000033#include <vector>
Hans Wennborgdcfba332015-10-06 23:40:43 +000034
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000035using namespace clang;
36using namespace threadSafety;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000037
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +000038// From ThreadSafetyUtil.h
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000039std::string threadSafety::getSourceLiteralString(const clang::Expr *CE) {
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +000040 switch (CE->getStmtClass()) {
41 case Stmt::IntegerLiteralClass:
42 return cast<IntegerLiteral>(CE)->getValue().toString(10, true);
43 case Stmt::StringLiteralClass: {
44 std::string ret("\"");
45 ret += cast<StringLiteral>(CE)->getString();
46 ret += "\"";
47 return ret;
48 }
49 case Stmt::CharacterLiteralClass:
50 case Stmt::CXXNullPtrLiteralExprClass:
51 case Stmt::GNUNullExprClass:
52 case Stmt::CXXBoolLiteralExprClass:
53 case Stmt::FloatingLiteralClass:
54 case Stmt::ImaginaryLiteralClass:
55 case Stmt::ObjCStringLiteralClass:
56 default:
57 return "#lit";
58 }
59}
60
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +000061// Return true if E is a variable that points to an incomplete Phi node.
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000062static bool isIncompletePhi(const til::SExpr *E) {
63 if (const auto *Ph = dyn_cast<til::Phi>(E))
64 return Ph->status() == til::Phi::PH_Incomplete;
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +000065 return false;
66}
67
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000068typedef SExprBuilder::CallingContext CallingContext;
69
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000070til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000071 auto It = SMap.find(S);
72 if (It != SMap.end())
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000073 return It->second;
Aaron Ballman3f993c12014-04-09 17:45:44 +000074 return nullptr;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000075}
76
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +000077til::SCFG *SExprBuilder::buildCFG(CFGWalker &Walker) {
78 Walker.walk(*this);
79 return Scfg;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +000080}
81
Benjamin Kramer66a97ee2015-03-09 14:19:54 +000082static bool isCalleeArrow(const Expr *E) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +000083 const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
84 return ME ? ME->isArrow() : false;
85}
86
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +000087/// \brief Translate a clang expression in an attribute to a til::SExpr.
88/// Constructs the context from D, DeclExp, and SelfDecl.
89///
90/// \param AttrExp The expression to translate.
91/// \param D The declaration to which the attribute is attached.
92/// \param DeclExp An expression involving the Decl to which the attribute
93/// is attached. E.g. the call to a function.
DeLesley Hutchins42665222014-08-04 16:10:59 +000094CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
95 const NamedDecl *D,
96 const Expr *DeclExp,
97 VarDecl *SelfDecl) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +000098 // If we are processing a raw attribute expression, with no substitutions.
99 if (!DeclExp)
100 return translateAttrExpr(AttrExp, nullptr);
101
102 CallingContext Ctx(nullptr, D);
103
104 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
105 // for formal parameters when we call buildMutexID later.
106 if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
107 Ctx.SelfArg = ME->getBase();
108 Ctx.SelfArrow = ME->isArrow();
109 } else if (const CXXMemberCallExpr *CE =
110 dyn_cast<CXXMemberCallExpr>(DeclExp)) {
111 Ctx.SelfArg = CE->getImplicitObjectArgument();
112 Ctx.SelfArrow = isCalleeArrow(CE->getCallee());
113 Ctx.NumArgs = CE->getNumArgs();
114 Ctx.FunArgs = CE->getArgs();
115 } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
116 Ctx.NumArgs = CE->getNumArgs();
117 Ctx.FunArgs = CE->getArgs();
118 } else if (const CXXConstructExpr *CE =
119 dyn_cast<CXXConstructExpr>(DeclExp)) {
120 Ctx.SelfArg = nullptr; // Will be set below
121 Ctx.NumArgs = CE->getNumArgs();
122 Ctx.FunArgs = CE->getArgs();
123 } else if (D && isa<CXXDestructorDecl>(D)) {
124 // There's no such thing as a "destructor call" in the AST.
125 Ctx.SelfArg = DeclExp;
126 }
127
128 // Hack to handle constructors, where self cannot be recovered from
129 // the expression.
130 if (SelfDecl && !Ctx.SelfArg) {
131 DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue,
132 SelfDecl->getLocation());
133 Ctx.SelfArg = &SelfDRE;
134
135 // If the attribute has no arguments, then assume the argument is "this".
136 if (!AttrExp)
137 return translateAttrExpr(Ctx.SelfArg, nullptr);
138 else // For most attributes.
139 return translateAttrExpr(AttrExp, &Ctx);
140 }
141
142 // If the attribute has no arguments, then assume the argument is "this".
143 if (!AttrExp)
144 return translateAttrExpr(Ctx.SelfArg, nullptr);
145 else // For most attributes.
146 return translateAttrExpr(AttrExp, &Ctx);
147}
148
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000149/// \brief Translate a clang expression in an attribute to a til::SExpr.
150// 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
156 if (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;
167 if (auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
168 if (OE->getOperator() == OO_Exclaim) {
169 Neg = true;
170 AttrExp = OE->getArg(0);
171 }
172 }
173 else if (auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
174 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.
188 if (auto *CE = dyn_cast_or_null<til::Cast>(E)) {
189 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);
214 case Stmt::CallExprClass:
215 return translateCallExpr(cast<CallExpr>(S), Ctx);
216 case Stmt::CXXMemberCallExprClass:
217 return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx);
218 case Stmt::CXXOperatorCallExprClass:
219 return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx);
220 case Stmt::UnaryOperatorClass:
221 return translateUnaryOperator(cast<UnaryOperator>(S), Ctx);
222 case Stmt::BinaryOperatorClass:
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000223 case Stmt::CompoundAssignOperatorClass:
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000224 return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
225
226 case Stmt::ArraySubscriptExprClass:
227 return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
228 case Stmt::ConditionalOperatorClass:
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000229 return translateAbstractConditionalOperator(
230 cast<ConditionalOperator>(S), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000231 case Stmt::BinaryConditionalOperatorClass:
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000232 return translateAbstractConditionalOperator(
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000233 cast<BinaryConditionalOperator>(S), Ctx);
234
235 // We treat these as no-ops
236 case Stmt::ParenExprClass:
237 return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx);
238 case Stmt::ExprWithCleanupsClass:
239 return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx);
240 case Stmt::CXXBindTemporaryExprClass:
241 return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx);
242
243 // Collect all literals
244 case Stmt::CharacterLiteralClass:
245 case Stmt::CXXNullPtrLiteralExprClass:
246 case Stmt::GNUNullExprClass:
247 case Stmt::CXXBoolLiteralExprClass:
248 case Stmt::FloatingLiteralClass:
249 case Stmt::ImaginaryLiteralClass:
250 case Stmt::IntegerLiteralClass:
251 case Stmt::StringLiteralClass:
252 case Stmt::ObjCStringLiteralClass:
253 return new (Arena) til::Literal(cast<Expr>(S));
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000254
255 case Stmt::DeclStmtClass:
256 return translateDeclStmt(cast<DeclStmt>(S), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000257 default:
258 break;
259 }
260 if (const CastExpr *CE = dyn_cast<CastExpr>(S))
261 return translateCastExpr(CE, Ctx);
262
263 return new (Arena) til::Undefined(S);
264}
265
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000266til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
267 CallingContext *Ctx) {
268 const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
269
270 // Function parameters require substitution and/or renaming.
271 if (const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(VD)) {
272 const FunctionDecl *FD =
273 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
274 unsigned I = PV->getFunctionScopeIndex();
275
276 if (Ctx && Ctx->FunArgs && FD == Ctx->AttrDecl->getCanonicalDecl()) {
277 // Substitute call arguments for references to function parameters
278 assert(I < Ctx->NumArgs);
279 return translate(Ctx->FunArgs[I], Ctx->Prev);
280 }
281 // Map the param back to the param of the original function declaration
282 // for consistent comparisons.
283 VD = FD->getParamDecl(I);
284 }
285
DeLesley Hutchinsdc0541f2015-09-29 15:25:51 +0000286 // For non-local variables, treat it as a reference to a named object.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000287 return new (Arena) til::LiteralPtr(VD);
288}
289
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000290til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
291 CallingContext *Ctx) {
292 // Substitute for 'this'
293 if (Ctx && Ctx->SelfArg)
294 return translate(Ctx->SelfArg, Ctx->Prev);
295 assert(SelfVar && "We have no variable for 'this'!");
296 return SelfVar;
297}
298
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000299static const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000300 if (auto *V = dyn_cast<til::Variable>(E))
301 return V->clangDecl();
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000302 if (auto *Ph = dyn_cast<til::Phi>(E))
303 return Ph->clangDecl();
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000304 if (auto *P = dyn_cast<til::Project>(E))
305 return P->clangDecl();
306 if (auto *L = dyn_cast<til::LiteralPtr>(E))
307 return L->clangDecl();
Hans Wennborgdcfba332015-10-06 23:40:43 +0000308 return nullptr;
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000309}
310
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000311static bool hasCppPointerType(const til::SExpr *E) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000312 auto *VD = getValueDeclFromSExpr(E);
313 if (VD && VD->getType()->isPointerType())
314 return true;
315 if (auto *C = dyn_cast<til::Cast>(E))
316 return C->castOpcode() == til::CAST_objToPtr;
317
318 return false;
319}
320
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000321// Grab the very first declaration of virtual method D
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000322static const CXXMethodDecl *getFirstVirtualDecl(const CXXMethodDecl *D) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000323 while (true) {
324 D = D->getCanonicalDecl();
325 CXXMethodDecl::method_iterator I = D->begin_overridden_methods(),
326 E = D->end_overridden_methods();
327 if (I == E)
328 return D; // Method does not override anything
329 D = *I; // FIXME: this does not work with multiple inheritance.
330 }
331 return nullptr;
332}
333
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000334til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
335 CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000336 til::SExpr *BE = translate(ME->getBase(), Ctx);
337 til::SExpr *E = new (Arena) til::SApply(BE);
338
Richard Smithdd55b952015-08-12 02:17:52 +0000339 const ValueDecl *D =
340 cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl());
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000341 if (auto *VD = dyn_cast<CXXMethodDecl>(D))
342 D = getFirstVirtualDecl(VD);
343
344 til::Project *P = new (Arena) til::Project(E, D);
345 if (hasCppPointerType(BE))
346 P->setArrow(true);
347 return P;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000348}
349
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000350til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000351 CallingContext *Ctx,
352 const Expr *SelfE) {
353 if (CapabilityExprMode) {
354 // Handle LOCK_RETURNED
355 const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl();
356 if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
357 CallingContext LRCallCtx(Ctx);
358 LRCallCtx.AttrDecl = CE->getDirectCallee();
359 LRCallCtx.SelfArg = SelfE;
360 LRCallCtx.NumArgs = CE->getNumArgs();
361 LRCallCtx.FunArgs = CE->getArgs();
DeLesley Hutchins42665222014-08-04 16:10:59 +0000362 return const_cast<til::SExpr*>(
363 translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000364 }
365 }
366
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000367 til::SExpr *E = translate(CE->getCallee(), Ctx);
Aaron Ballman3f993c12014-04-09 17:45:44 +0000368 for (const auto *Arg : CE->arguments()) {
369 til::SExpr *A = translate(Arg, Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000370 E = new (Arena) til::Apply(E, A);
371 }
372 return new (Arena) til::Call(E, CE);
373}
374
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000375til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
376 const CXXMemberCallExpr *ME, CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000377 if (CapabilityExprMode) {
378 // Ignore calls to get() on smart pointers.
379 if (ME->getMethodDecl()->getNameAsString() == "get" &&
380 ME->getNumArgs() == 0) {
381 auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
382 return new (Arena) til::Cast(til::CAST_objToPtr, E);
383 // return E;
384 }
385 }
386 return translateCallExpr(cast<CallExpr>(ME), Ctx,
387 ME->getImplicitObjectArgument());
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000388}
389
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000390til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
391 const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000392 if (CapabilityExprMode) {
393 // Ignore operator * and operator -> on smart pointers.
394 OverloadedOperatorKind k = OCE->getOperator();
395 if (k == OO_Star || k == OO_Arrow) {
396 auto *E = translate(OCE->getArg(0), Ctx);
397 return new (Arena) til::Cast(til::CAST_objToPtr, E);
398 // return E;
399 }
400 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000401 return translateCallExpr(cast<CallExpr>(OCE), Ctx);
402}
403
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000404til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
405 CallingContext *Ctx) {
406 switch (UO->getOpcode()) {
407 case UO_PostInc:
408 case UO_PostDec:
409 case UO_PreInc:
410 case UO_PreDec:
411 return new (Arena) til::Undefined(UO);
412
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000413 case UO_AddrOf: {
414 if (CapabilityExprMode) {
415 // interpret &Graph::mu_ as an existential.
416 if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
417 if (DRE->getDecl()->isCXXInstanceMember()) {
418 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
419 // We interpret this syntax specially, as a wildcard.
420 auto *W = new (Arena) til::Wildcard();
421 return new (Arena) til::Project(W, DRE->getDecl());
422 }
423 }
424 }
425 // otherwise, & is a no-op
426 return translate(UO->getSubExpr(), Ctx);
427 }
428
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000429 // We treat these as no-ops
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000430 case UO_Deref:
431 case UO_Plus:
432 return translate(UO->getSubExpr(), Ctx);
433
434 case UO_Minus:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000435 return new (Arena)
436 til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000437 case UO_Not:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000438 return new (Arena)
439 til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000440 case UO_LNot:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000441 return new (Arena)
442 til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
443
444 // Currently unsupported
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000445 case UO_Real:
446 case UO_Imag:
Aaron Ballman3f993c12014-04-09 17:45:44 +0000447 case UO_Extension:
Richard Smith9f690bd2015-10-27 06:02:45 +0000448 case UO_Coawait:
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000449 return new (Arena) til::Undefined(UO);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000450 }
451 return new (Arena) til::Undefined(UO);
452}
453
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000454til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
455 const BinaryOperator *BO,
456 CallingContext *Ctx, bool Reverse) {
457 til::SExpr *E0 = translate(BO->getLHS(), Ctx);
458 til::SExpr *E1 = translate(BO->getRHS(), Ctx);
459 if (Reverse)
460 return new (Arena) til::BinaryOp(Op, E1, E0);
461 else
462 return new (Arena) til::BinaryOp(Op, E0, E1);
463}
464
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000465til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
466 const BinaryOperator *BO,
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000467 CallingContext *Ctx,
468 bool Assign) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000469 const Expr *LHS = BO->getLHS();
470 const Expr *RHS = BO->getRHS();
471 til::SExpr *E0 = translate(LHS, Ctx);
472 til::SExpr *E1 = translate(RHS, Ctx);
473
474 const ValueDecl *VD = nullptr;
475 til::SExpr *CV = nullptr;
476 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHS)) {
477 VD = DRE->getDecl();
478 CV = lookupVarDecl(VD);
479 }
480
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000481 if (!Assign) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000482 til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
483 E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
484 E1 = addStatement(E1, nullptr, VD);
485 }
486 if (VD && CV)
487 return updateVarDecl(VD, E1);
488 return new (Arena) til::Store(E0, E1);
489}
490
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000491til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
492 CallingContext *Ctx) {
493 switch (BO->getOpcode()) {
494 case BO_PtrMemD:
495 case BO_PtrMemI:
496 return new (Arena) til::Undefined(BO);
497
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000498 case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx);
499 case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx);
500 case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx);
501 case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx);
502 case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx);
503 case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx);
504 case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx);
505 case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx);
506 case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true);
507 case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx);
508 case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true);
509 case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx);
510 case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx);
511 case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx);
512 case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx);
513 case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx);
514 case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
515 case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx);
Aaron Ballman3f993c12014-04-09 17:45:44 +0000516
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000517 case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true);
518 case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
519 case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
520 case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
521 case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
522 case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
523 case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
524 case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
525 case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
526 case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
527 case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000528
529 case BO_Comma:
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000530 // The clang CFG should have already processed both sides.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000531 return translate(BO->getRHS(), Ctx);
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000532 }
DeLesley Hutchins78340012014-04-22 14:51:04 +0000533 return new (Arena) til::Undefined(BO);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000534}
535
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000536til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
537 CallingContext *Ctx) {
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000538 clang::CastKind K = CE->getCastKind();
539 switch (K) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000540 case CK_LValueToRValue: {
541 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
542 til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
543 if (E0)
544 return E0;
545 }
546 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000547 return E0;
548 // FIXME!! -- get Load working properly
549 // return new (Arena) til::Load(E0);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000550 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000551 case CK_NoOp:
552 case CK_DerivedToBase:
553 case CK_UncheckedDerivedToBase:
554 case CK_ArrayToPointerDecay:
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000555 case CK_FunctionToPointerDecay: {
556 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000557 return E0;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000558 }
559 default: {
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000560 // FIXME: handle different kinds of casts.
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000561 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000562 if (CapabilityExprMode)
563 return E0;
DeLesley Hutchinsf4b5e7c2014-05-15 00:50:36 +0000564 return new (Arena) til::Cast(til::CAST_none, E0);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000565 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000566 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000567}
568
Aaron Ballman3f993c12014-04-09 17:45:44 +0000569til::SExpr *
570SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
571 CallingContext *Ctx) {
DeLesley Hutchinsf1a31162014-04-22 17:31:23 +0000572 til::SExpr *E0 = translate(E->getBase(), Ctx);
573 til::SExpr *E1 = translate(E->getIdx(), Ctx);
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000574 return new (Arena) til::ArrayIndex(E0, E1);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000575}
576
Aaron Ballman3f993c12014-04-09 17:45:44 +0000577til::SExpr *
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000578SExprBuilder::translateAbstractConditionalOperator(
579 const AbstractConditionalOperator *CO, CallingContext *Ctx) {
580 auto *C = translate(CO->getCond(), Ctx);
581 auto *T = translate(CO->getTrueExpr(), Ctx);
582 auto *E = translate(CO->getFalseExpr(), Ctx);
583 return new (Arena) til::IfThenElse(C, T, E);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000584}
585
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000586til::SExpr *
587SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
588 DeclGroupRef DGrp = S->getDeclGroup();
589 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
590 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
591 Expr *E = VD->getInit();
592 til::SExpr* SE = translate(E, Ctx);
DeLesley Hutchins7e615c22014-04-09 22:39:43 +0000593
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000594 // Add local variables with trivial type to the variable map
595 QualType T = VD->getType();
596 if (T.isTrivialType(VD->getASTContext())) {
597 return addVarDecl(VD, SE);
598 }
599 else {
600 // TODO: add alloca
601 }
602 }
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000603 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000604 return nullptr;
605}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000606
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000607// If (E) is non-trivial, then add it to the current basic block, and
608// update the statement map so that S refers to E. Returns a new variable
609// that refers to E.
610// If E is trivial returns E.
611til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
612 const ValueDecl *VD) {
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000613 if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000614 return E;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000615 if (VD)
616 E = new (Arena) til::Variable(E, VD);
617 CurrentInstructions.push_back(E);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000618 if (S)
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000619 insertStmt(S, E);
620 return E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000621}
622
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000623// Returns the current value of VD, if known, and nullptr otherwise.
624til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000625 auto It = LVarIdxMap.find(VD);
626 if (It != LVarIdxMap.end()) {
627 assert(CurrentLVarMap[It->second].first == VD);
628 return CurrentLVarMap[It->second].second;
629 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000630 return nullptr;
631}
632
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000633// if E is a til::Variable, update its clangDecl.
Benjamin Kramer66a97ee2015-03-09 14:19:54 +0000634static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000635 if (!E)
636 return;
637 if (til::Variable *V = dyn_cast<til::Variable>(E)) {
638 if (!V->clangDecl())
639 V->setClangDecl(VD);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000640 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000641}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000642
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000643// Adds a new variable declaration.
644til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
645 maybeUpdateVD(E, VD);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000646 LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
647 CurrentLVarMap.makeWritable();
648 CurrentLVarMap.push_back(std::make_pair(VD, E));
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000649 return E;
650}
651
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000652// Updates a current variable declaration. (E.g. by assignment)
653til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
654 maybeUpdateVD(E, VD);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000655 auto It = LVarIdxMap.find(VD);
656 if (It == LVarIdxMap.end()) {
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000657 til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
658 til::SExpr *St = new (Arena) til::Store(Ptr, E);
659 return St;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000660 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000661 CurrentLVarMap.makeWritable();
662 CurrentLVarMap.elem(It->second).second = E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000663 return E;
664}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000665
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000666// Make a Phi node in the current block for the i^th variable in CurrentVarMap.
667// If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
668// If E == null, this is a backedge and will be set later.
669void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
670 unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
671 assert(ArgIndex > 0 && ArgIndex < NPreds);
672
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000673 til::SExpr *CurrE = CurrentLVarMap[i].second;
674 if (CurrE->block() == CurrentBB) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000675 // We already have a Phi node in the current block,
676 // so just add the new variable to the Phi node.
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000677 til::Phi *Ph = dyn_cast<til::Phi>(CurrE);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000678 assert(Ph && "Expecting Phi node.");
679 if (E)
680 Ph->values()[ArgIndex] = E;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000681 return;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000682 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000683
684 // Make a new phi node: phi(..., E)
685 // All phi args up to the current index are set to the current value.
686 til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
687 Ph->values().setValues(NPreds, nullptr);
688 for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000689 Ph->values()[PIdx] = CurrE;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000690 if (E)
691 Ph->values()[ArgIndex] = E;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000692 Ph->setClangDecl(CurrentLVarMap[i].first);
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000693 // If E is from a back-edge, or either E or CurrE are incomplete, then
694 // mark this node as incomplete; we may need to remove it later.
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000695 if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE)) {
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000696 Ph->setStatus(til::Phi::PH_Incomplete);
697 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000698
699 // Add Phi node to current block, and update CurrentLVarMap[i]
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000700 CurrentArguments.push_back(Ph);
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000701 if (Ph->status() == til::Phi::PH_Incomplete)
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000702 IncompleteArgs.push_back(Ph);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000703
704 CurrentLVarMap.makeWritable();
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000705 CurrentLVarMap.elem(i).second = Ph;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000706}
707
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000708// Merge values from Map into the current variable map.
709// This will construct Phi nodes in the current basic block as necessary.
710void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
711 assert(CurrentBlockInfo && "Not processing a block!");
712
713 if (!CurrentLVarMap.valid()) {
714 // Steal Map, using copy-on-write.
715 CurrentLVarMap = std::move(Map);
716 return;
717 }
718 if (CurrentLVarMap.sameAs(Map))
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000719 return; // Easy merge: maps from different predecessors are unchanged.
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000720
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000721 unsigned NPreds = CurrentBB->numPredecessors();
722 unsigned ESz = CurrentLVarMap.size();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000723 unsigned MSz = Map.size();
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000724 unsigned Sz = std::min(ESz, MSz);
725
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000726 for (unsigned i=0; i<Sz; ++i) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000727 if (CurrentLVarMap[i].first != Map[i].first) {
728 // We've reached the end of variables in common.
729 CurrentLVarMap.makeWritable();
730 CurrentLVarMap.downsize(i);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000731 break;
732 }
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000733 if (CurrentLVarMap[i].second != Map[i].second)
734 makePhiNodeVar(i, NPreds, Map[i].second);
DeLesley Hutchins11bb30872014-04-07 22:56:24 +0000735 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000736 if (ESz > MSz) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000737 CurrentLVarMap.makeWritable();
738 CurrentLVarMap.downsize(Map.size());
739 }
740}
741
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000742// Merge a back edge into the current variable map.
743// This will create phi nodes for all variables in the variable map.
744void SExprBuilder::mergeEntryMapBackEdge() {
745 // We don't have definitions for variables on the backedge, because we
746 // haven't gotten that far in the CFG. Thus, when encountering a back edge,
747 // we conservatively create Phi nodes for all variables. Unnecessary Phi
748 // nodes will be marked as incomplete, and stripped out at the end.
749 //
750 // An Phi node is unnecessary if it only refers to itself and one other
751 // variable, e.g. x = Phi(y, y, x) can be reduced to x = y.
752
753 assert(CurrentBlockInfo && "Not processing a block!");
754
755 if (CurrentBlockInfo->HasBackEdges)
756 return;
757 CurrentBlockInfo->HasBackEdges = true;
758
759 CurrentLVarMap.makeWritable();
760 unsigned Sz = CurrentLVarMap.size();
761 unsigned NPreds = CurrentBB->numPredecessors();
762
763 for (unsigned i=0; i < Sz; ++i) {
764 makePhiNodeVar(i, NPreds, nullptr);
765 }
766}
767
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000768// Update the phi nodes that were initially created for a back edge
769// once the variable definitions have been computed.
770// I.e., merge the current variable map into the phi nodes for Blk.
771void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
772 til::BasicBlock *BB = lookupBlock(Blk);
773 unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
774 assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
775
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000776 for (til::SExpr *PE : BB->arguments()) {
777 til::Phi *Ph = dyn_cast_or_null<til::Phi>(PE);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000778 assert(Ph && "Expecting Phi Node.");
779 assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000780
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000781 til::SExpr *E = lookupVarDecl(Ph->clangDecl());
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000782 assert(E && "Couldn't find local variable for Phi node.");
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000783 Ph->values()[ArgIndex] = E;
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000784 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000785}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000786
Benjamin Kramera7bcab72014-05-09 17:08:01 +0000787void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000788 const CFGBlock *First) {
789 // Perform initial setup operations.
790 unsigned NBlocks = Cfg->getNumBlockIDs();
791 Scfg = new (Arena) til::SCFG(Arena, NBlocks);
792
793 // allocate all basic blocks immediately, to handle forward references.
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000794 BBInfo.resize(NBlocks);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000795 BlockMap.resize(NBlocks, nullptr);
796 // create map from clang blockID to til::BasicBlocks
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000797 for (auto *B : *Cfg) {
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000798 auto *BB = new (Arena) til::BasicBlock(Arena);
799 BB->reserveInstructions(B->size());
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000800 BlockMap[B->getBlockID()] = BB;
DeLesley Hutchins11bb30872014-04-07 22:56:24 +0000801 }
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000802
803 CurrentBB = lookupBlock(&Cfg->getEntry());
Benjamin Kramera7bcab72014-05-09 17:08:01 +0000804 auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
805 : cast<FunctionDecl>(D)->parameters();
806 for (auto *Pm : Parms) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000807 QualType T = Pm->getType();
808 if (!T.isTrivialType(Pm->getASTContext()))
809 continue;
810
811 // Add parameters to local variable map.
812 // FIXME: right now we emulate params with loads; that should be fixed.
813 til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
814 til::SExpr *Ld = new (Arena) til::Load(Lp);
815 til::SExpr *V = addStatement(Ld, nullptr, Pm);
816 addVarDecl(Pm, V);
817 }
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000818}
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000819
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000820void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
821 // Intialize TIL basic block and add it to the CFG.
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000822 CurrentBB = lookupBlock(B);
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000823 CurrentBB->reservePredecessors(B->pred_size());
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000824 Scfg->add(CurrentBB);
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000825
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000826 CurrentBlockInfo = &BBInfo[B->getBlockID()];
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000827
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000828 // CurrentLVarMap is moved to ExitMap on block exit.
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000829 // FIXME: the entry block will hold function parameters.
830 // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000831}
832
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000833void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000834 // Compute CurrentLVarMap on entry from ExitMaps of predecessors
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000835
DeLesley Hutchins44be81b2014-05-28 21:28:13 +0000836 CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000837 BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000838 assert(PredInfo->UnprocessedSuccessors > 0);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000839
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000840 if (--PredInfo->UnprocessedSuccessors == 0)
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000841 mergeEntryMap(std::move(PredInfo->ExitMap));
842 else
843 mergeEntryMap(PredInfo->ExitMap.clone());
844
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000845 ++CurrentBlockInfo->ProcessedPredecessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000846}
847
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000848void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000849 mergeEntryMapBackEdge();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000850}
851
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000852void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
853 // The merge*() methods have created arguments.
854 // Push those arguments onto the basic block.
855 CurrentBB->arguments().reserve(
856 static_cast<unsigned>(CurrentArguments.size()), Arena);
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000857 for (auto *A : CurrentArguments)
858 CurrentBB->addArgument(A);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000859}
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000860
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000861void SExprBuilder::handleStatement(const Stmt *S) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000862 til::SExpr *E = translate(S, nullptr);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000863 addStatement(E, S);
864}
865
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000866void SExprBuilder::handleDestructorCall(const VarDecl *VD,
867 const CXXDestructorDecl *DD) {
868 til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
869 til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
870 til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
871 til::SExpr *E = new (Arena) til::Call(Ap);
872 addStatement(E, nullptr);
873}
874
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000875void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000876 CurrentBB->instructions().reserve(
877 static_cast<unsigned>(CurrentInstructions.size()), Arena);
878 for (auto *V : CurrentInstructions)
879 CurrentBB->addInstruction(V);
880
881 // Create an appropriate terminator
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000882 unsigned N = B->succ_size();
883 auto It = B->succ_begin();
884 if (N == 1) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000885 til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000886 // TODO: set index
DeLesley Hutchinsb6031922014-05-29 21:24:16 +0000887 unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000888 auto *Tm = new (Arena) til::Goto(BB, Idx);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000889 CurrentBB->setTerminator(Tm);
890 }
891 else if (N == 2) {
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000892 til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000893 til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000894 ++It;
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000895 til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000896 // FIXME: make sure these arent' critical edges.
897 auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000898 CurrentBB->setTerminator(Tm);
899 }
900}
901
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000902void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000903 ++CurrentBlockInfo->UnprocessedSuccessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000904}
905
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000906void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000907 mergePhiNodesBackEdge(Succ);
908 ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000909}
910
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000911void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
DeLesley Hutchinsf8b412a2014-04-21 23:18:18 +0000912 CurrentArguments.clear();
913 CurrentInstructions.clear();
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000914 CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000915 CurrentBB = nullptr;
916 CurrentBlockInfo = nullptr;
917}
918
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000919void SExprBuilder::exitCFG(const CFGBlock *Last) {
DeLesley Hutchins4e38f102014-09-10 22:12:52 +0000920 for (auto *Ph : IncompleteArgs) {
921 if (Ph->status() == til::Phi::PH_Incomplete)
922 simplifyIncompleteArg(Ph);
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000923 }
924
DeLesley Hutchinsae497de2014-04-19 00:35:54 +0000925 CurrentArguments.clear();
926 CurrentInstructions.clear();
DeLesley Hutchinsa9db0012014-04-19 03:54:41 +0000927 IncompleteArgs.clear();
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000928}
929
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000930/*
DeLesley Hutchinsaab9aff2014-04-15 23:23:19 +0000931void printSCFG(CFGWalker &Walker) {
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000932 llvm::BumpPtrAllocator Bpa;
933 til::MemRegionRef Arena(&Bpa);
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000934 SExprBuilder SxBuilder(Arena);
935 til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
936 TILPrinter::print(Scfg, llvm::errs());
DeLesley Hutchinsb2213912014-04-07 18:09:54 +0000937}
DeLesley Hutchinsea1f8332014-07-28 15:57:27 +0000938*/