1 1.1 joerg //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs ----------*-==// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // This file implements Live Variables analysis for source-level CFGs. 10 1.1 joerg // 11 1.1 joerg //===----------------------------------------------------------------------===// 12 1.1 joerg 13 1.1 joerg #include "clang/Analysis/Analyses/LiveVariables.h" 14 1.1 joerg #include "clang/AST/Stmt.h" 15 1.1 joerg #include "clang/AST/StmtVisitor.h" 16 1.1 joerg #include "clang/Analysis/AnalysisDeclContext.h" 17 1.1 joerg #include "clang/Analysis/CFG.h" 18 1.1.1.2 joerg #include "clang/Analysis/FlowSensitive/DataflowWorklist.h" 19 1.1 joerg #include "llvm/ADT/DenseMap.h" 20 1.1 joerg #include "llvm/Support/raw_ostream.h" 21 1.1 joerg #include <algorithm> 22 1.1 joerg #include <vector> 23 1.1 joerg 24 1.1 joerg using namespace clang; 25 1.1 joerg 26 1.1 joerg namespace { 27 1.1 joerg class LiveVariablesImpl { 28 1.1 joerg public: 29 1.1 joerg AnalysisDeclContext &analysisContext; 30 1.1.1.2 joerg llvm::ImmutableSet<const Expr *>::Factory ESetFact; 31 1.1 joerg llvm::ImmutableSet<const VarDecl *>::Factory DSetFact; 32 1.1 joerg llvm::ImmutableSet<const BindingDecl *>::Factory BSetFact; 33 1.1 joerg llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness; 34 1.1 joerg llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness; 35 1.1 joerg llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness; 36 1.1 joerg llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment; 37 1.1 joerg const bool killAtAssign; 38 1.1 joerg 39 1.1 joerg LiveVariables::LivenessValues 40 1.1 joerg merge(LiveVariables::LivenessValues valsA, 41 1.1 joerg LiveVariables::LivenessValues valsB); 42 1.1 joerg 43 1.1 joerg LiveVariables::LivenessValues 44 1.1 joerg runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val, 45 1.1 joerg LiveVariables::Observer *obs = nullptr); 46 1.1 joerg 47 1.1 joerg void dumpBlockLiveness(const SourceManager& M); 48 1.1.1.2 joerg void dumpExprLiveness(const SourceManager& M); 49 1.1 joerg 50 1.1 joerg LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign) 51 1.1.1.2 joerg : analysisContext(ac), 52 1.1.1.2 joerg ESetFact(false), // Do not canonicalize ImmutableSets by default. 53 1.1.1.2 joerg DSetFact(false), // This is a *major* performance win. 54 1.1.1.2 joerg BSetFact(false), killAtAssign(KillAtAssign) {} 55 1.1 joerg }; 56 1.1.1.2 joerg } // namespace 57 1.1 joerg 58 1.1 joerg static LiveVariablesImpl &getImpl(void *x) { 59 1.1 joerg return *((LiveVariablesImpl *) x); 60 1.1 joerg } 61 1.1 joerg 62 1.1 joerg //===----------------------------------------------------------------------===// 63 1.1 joerg // Operations and queries on LivenessValues. 64 1.1 joerg //===----------------------------------------------------------------------===// 65 1.1 joerg 66 1.1.1.2 joerg bool LiveVariables::LivenessValues::isLive(const Expr *E) const { 67 1.1.1.2 joerg return liveExprs.contains(E); 68 1.1 joerg } 69 1.1 joerg 70 1.1 joerg bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const { 71 1.1 joerg if (const auto *DD = dyn_cast<DecompositionDecl>(D)) { 72 1.1 joerg bool alive = false; 73 1.1 joerg for (const BindingDecl *BD : DD->bindings()) 74 1.1 joerg alive |= liveBindings.contains(BD); 75 1.1 joerg return alive; 76 1.1 joerg } 77 1.1 joerg return liveDecls.contains(D); 78 1.1 joerg } 79 1.1 joerg 80 1.1 joerg namespace { 81 1.1 joerg template <typename SET> 82 1.1 joerg SET mergeSets(SET A, SET B) { 83 1.1 joerg if (A.isEmpty()) 84 1.1 joerg return B; 85 1.1 joerg 86 1.1 joerg for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) { 87 1.1 joerg A = A.add(*it); 88 1.1 joerg } 89 1.1 joerg return A; 90 1.1 joerg } 91 1.1.1.2 joerg } // namespace 92 1.1 joerg 93 1.1 joerg void LiveVariables::Observer::anchor() { } 94 1.1 joerg 95 1.1 joerg LiveVariables::LivenessValues 96 1.1 joerg LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA, 97 1.1 joerg LiveVariables::LivenessValues valsB) { 98 1.1 joerg 99 1.1.1.2 joerg llvm::ImmutableSetRef<const Expr *> SSetRefA( 100 1.1.1.2 joerg valsA.liveExprs.getRootWithoutRetain(), ESetFact.getTreeFactory()), 101 1.1.1.2 joerg SSetRefB(valsB.liveExprs.getRootWithoutRetain(), 102 1.1.1.2 joerg ESetFact.getTreeFactory()); 103 1.1 joerg 104 1.1 joerg llvm::ImmutableSetRef<const VarDecl *> 105 1.1 joerg DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()), 106 1.1 joerg DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()); 107 1.1 joerg 108 1.1 joerg llvm::ImmutableSetRef<const BindingDecl *> 109 1.1 joerg BSetRefA(valsA.liveBindings.getRootWithoutRetain(), BSetFact.getTreeFactory()), 110 1.1 joerg BSetRefB(valsB.liveBindings.getRootWithoutRetain(), BSetFact.getTreeFactory()); 111 1.1 joerg 112 1.1 joerg SSetRefA = mergeSets(SSetRefA, SSetRefB); 113 1.1 joerg DSetRefA = mergeSets(DSetRefA, DSetRefB); 114 1.1 joerg BSetRefA = mergeSets(BSetRefA, BSetRefB); 115 1.1 joerg 116 1.1 joerg // asImmutableSet() canonicalizes the tree, allowing us to do an easy 117 1.1 joerg // comparison afterwards. 118 1.1 joerg return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(), 119 1.1 joerg DSetRefA.asImmutableSet(), 120 1.1 joerg BSetRefA.asImmutableSet()); 121 1.1 joerg } 122 1.1 joerg 123 1.1 joerg bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const { 124 1.1.1.2 joerg return liveExprs == V.liveExprs && liveDecls == V.liveDecls; 125 1.1 joerg } 126 1.1 joerg 127 1.1 joerg //===----------------------------------------------------------------------===// 128 1.1 joerg // Query methods. 129 1.1 joerg //===----------------------------------------------------------------------===// 130 1.1 joerg 131 1.1 joerg static bool isAlwaysAlive(const VarDecl *D) { 132 1.1 joerg return D->hasGlobalStorage(); 133 1.1 joerg } 134 1.1 joerg 135 1.1 joerg bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) { 136 1.1 joerg return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D); 137 1.1 joerg } 138 1.1 joerg 139 1.1 joerg bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) { 140 1.1 joerg return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D); 141 1.1 joerg } 142 1.1 joerg 143 1.1.1.2 joerg bool LiveVariables::isLive(const Stmt *Loc, const Expr *Val) { 144 1.1.1.2 joerg return getImpl(impl).stmtsToLiveness[Loc].isLive(Val); 145 1.1 joerg } 146 1.1 joerg 147 1.1 joerg //===----------------------------------------------------------------------===// 148 1.1 joerg // Dataflow computation. 149 1.1 joerg //===----------------------------------------------------------------------===// 150 1.1 joerg 151 1.1 joerg namespace { 152 1.1 joerg class TransferFunctions : public StmtVisitor<TransferFunctions> { 153 1.1 joerg LiveVariablesImpl &LV; 154 1.1 joerg LiveVariables::LivenessValues &val; 155 1.1 joerg LiveVariables::Observer *observer; 156 1.1 joerg const CFGBlock *currentBlock; 157 1.1 joerg public: 158 1.1 joerg TransferFunctions(LiveVariablesImpl &im, 159 1.1 joerg LiveVariables::LivenessValues &Val, 160 1.1 joerg LiveVariables::Observer *Observer, 161 1.1 joerg const CFGBlock *CurrentBlock) 162 1.1 joerg : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {} 163 1.1 joerg 164 1.1 joerg void VisitBinaryOperator(BinaryOperator *BO); 165 1.1 joerg void VisitBlockExpr(BlockExpr *BE); 166 1.1 joerg void VisitDeclRefExpr(DeclRefExpr *DR); 167 1.1 joerg void VisitDeclStmt(DeclStmt *DS); 168 1.1 joerg void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS); 169 1.1 joerg void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE); 170 1.1 joerg void VisitUnaryOperator(UnaryOperator *UO); 171 1.1 joerg void Visit(Stmt *S); 172 1.1 joerg }; 173 1.1.1.2 joerg } // namespace 174 1.1 joerg 175 1.1 joerg static const VariableArrayType *FindVA(QualType Ty) { 176 1.1 joerg const Type *ty = Ty.getTypePtr(); 177 1.1 joerg while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) { 178 1.1 joerg if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT)) 179 1.1 joerg if (VAT->getSizeExpr()) 180 1.1 joerg return VAT; 181 1.1 joerg 182 1.1 joerg ty = VT->getElementType().getTypePtr(); 183 1.1 joerg } 184 1.1 joerg 185 1.1 joerg return nullptr; 186 1.1 joerg } 187 1.1 joerg 188 1.1.1.2 joerg static const Expr *LookThroughExpr(const Expr *E) { 189 1.1.1.2 joerg while (E) { 190 1.1.1.2 joerg if (const Expr *Ex = dyn_cast<Expr>(E)) 191 1.1.1.2 joerg E = Ex->IgnoreParens(); 192 1.1.1.2 joerg if (const FullExpr *FE = dyn_cast<FullExpr>(E)) { 193 1.1.1.2 joerg E = FE->getSubExpr(); 194 1.1 joerg continue; 195 1.1 joerg } 196 1.1.1.2 joerg if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { 197 1.1.1.2 joerg E = OVE->getSourceExpr(); 198 1.1 joerg continue; 199 1.1 joerg } 200 1.1 joerg break; 201 1.1 joerg } 202 1.1.1.2 joerg return E; 203 1.1 joerg } 204 1.1 joerg 205 1.1.1.2 joerg static void AddLiveExpr(llvm::ImmutableSet<const Expr *> &Set, 206 1.1.1.2 joerg llvm::ImmutableSet<const Expr *>::Factory &F, 207 1.1.1.2 joerg const Expr *E) { 208 1.1.1.2 joerg Set = F.add(Set, LookThroughExpr(E)); 209 1.1 joerg } 210 1.1 joerg 211 1.1 joerg void TransferFunctions::Visit(Stmt *S) { 212 1.1 joerg if (observer) 213 1.1 joerg observer->observeStmt(S, currentBlock, val); 214 1.1 joerg 215 1.1 joerg StmtVisitor<TransferFunctions>::Visit(S); 216 1.1 joerg 217 1.1.1.2 joerg if (const auto *E = dyn_cast<Expr>(S)) { 218 1.1.1.2 joerg val.liveExprs = LV.ESetFact.remove(val.liveExprs, E); 219 1.1 joerg } 220 1.1 joerg 221 1.1 joerg // Mark all children expressions live. 222 1.1 joerg 223 1.1 joerg switch (S->getStmtClass()) { 224 1.1 joerg default: 225 1.1 joerg break; 226 1.1 joerg case Stmt::StmtExprClass: { 227 1.1 joerg // For statement expressions, look through the compound statement. 228 1.1 joerg S = cast<StmtExpr>(S)->getSubStmt(); 229 1.1 joerg break; 230 1.1 joerg } 231 1.1 joerg case Stmt::CXXMemberCallExprClass: { 232 1.1 joerg // Include the implicit "this" pointer as being live. 233 1.1 joerg CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S); 234 1.1 joerg if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) { 235 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, ImplicitObj); 236 1.1 joerg } 237 1.1 joerg break; 238 1.1 joerg } 239 1.1 joerg case Stmt::ObjCMessageExprClass: { 240 1.1 joerg // In calls to super, include the implicit "self" pointer as being live. 241 1.1 joerg ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S); 242 1.1 joerg if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance) 243 1.1 joerg val.liveDecls = LV.DSetFact.add(val.liveDecls, 244 1.1 joerg LV.analysisContext.getSelfDecl()); 245 1.1 joerg break; 246 1.1 joerg } 247 1.1 joerg case Stmt::DeclStmtClass: { 248 1.1 joerg const DeclStmt *DS = cast<DeclStmt>(S); 249 1.1 joerg if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) { 250 1.1 joerg for (const VariableArrayType* VA = FindVA(VD->getType()); 251 1.1 joerg VA != nullptr; VA = FindVA(VA->getElementType())) { 252 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, VA->getSizeExpr()); 253 1.1 joerg } 254 1.1 joerg } 255 1.1 joerg break; 256 1.1 joerg } 257 1.1 joerg case Stmt::PseudoObjectExprClass: { 258 1.1 joerg // A pseudo-object operation only directly consumes its result 259 1.1 joerg // expression. 260 1.1 joerg Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr(); 261 1.1 joerg if (!child) return; 262 1.1 joerg if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child)) 263 1.1 joerg child = OV->getSourceExpr(); 264 1.1 joerg child = child->IgnoreParens(); 265 1.1.1.2 joerg val.liveExprs = LV.ESetFact.add(val.liveExprs, child); 266 1.1 joerg return; 267 1.1 joerg } 268 1.1 joerg 269 1.1 joerg // FIXME: These cases eventually shouldn't be needed. 270 1.1 joerg case Stmt::ExprWithCleanupsClass: { 271 1.1 joerg S = cast<ExprWithCleanups>(S)->getSubExpr(); 272 1.1 joerg break; 273 1.1 joerg } 274 1.1 joerg case Stmt::CXXBindTemporaryExprClass: { 275 1.1 joerg S = cast<CXXBindTemporaryExpr>(S)->getSubExpr(); 276 1.1 joerg break; 277 1.1 joerg } 278 1.1 joerg case Stmt::UnaryExprOrTypeTraitExprClass: { 279 1.1 joerg // No need to unconditionally visit subexpressions. 280 1.1 joerg return; 281 1.1 joerg } 282 1.1 joerg case Stmt::IfStmtClass: { 283 1.1 joerg // If one of the branches is an expression rather than a compound 284 1.1 joerg // statement, it will be bad if we mark it as live at the terminator 285 1.1 joerg // of the if-statement (i.e., immediately after the condition expression). 286 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, cast<IfStmt>(S)->getCond()); 287 1.1 joerg return; 288 1.1 joerg } 289 1.1 joerg case Stmt::WhileStmtClass: { 290 1.1 joerg // If the loop body is an expression rather than a compound statement, 291 1.1 joerg // it will be bad if we mark it as live at the terminator of the loop 292 1.1 joerg // (i.e., immediately after the condition expression). 293 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, cast<WhileStmt>(S)->getCond()); 294 1.1 joerg return; 295 1.1 joerg } 296 1.1 joerg case Stmt::DoStmtClass: { 297 1.1 joerg // If the loop body is an expression rather than a compound statement, 298 1.1 joerg // it will be bad if we mark it as live at the terminator of the loop 299 1.1 joerg // (i.e., immediately after the condition expression). 300 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, cast<DoStmt>(S)->getCond()); 301 1.1 joerg return; 302 1.1 joerg } 303 1.1 joerg case Stmt::ForStmtClass: { 304 1.1 joerg // If the loop body is an expression rather than a compound statement, 305 1.1 joerg // it will be bad if we mark it as live at the terminator of the loop 306 1.1 joerg // (i.e., immediately after the condition expression). 307 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, cast<ForStmt>(S)->getCond()); 308 1.1 joerg return; 309 1.1 joerg } 310 1.1 joerg 311 1.1 joerg } 312 1.1 joerg 313 1.1.1.2 joerg // HACK + FIXME: What is this? One could only guess that this is an attempt to 314 1.1.1.2 joerg // fish for live values, for example, arguments from a call expression. 315 1.1.1.2 joerg // Maybe we could take inspiration from UninitializedVariable analysis? 316 1.1 joerg for (Stmt *Child : S->children()) { 317 1.1.1.2 joerg if (const auto *E = dyn_cast_or_null<Expr>(Child)) 318 1.1.1.2 joerg AddLiveExpr(val.liveExprs, LV.ESetFact, E); 319 1.1 joerg } 320 1.1 joerg } 321 1.1 joerg 322 1.1 joerg static bool writeShouldKill(const VarDecl *VD) { 323 1.1 joerg return VD && !VD->getType()->isReferenceType() && 324 1.1 joerg !isAlwaysAlive(VD); 325 1.1 joerg } 326 1.1 joerg 327 1.1 joerg void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) { 328 1.1.1.2 joerg if (LV.killAtAssign && B->getOpcode() == BO_Assign) { 329 1.1.1.2 joerg if (const auto *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens())) { 330 1.1.1.2 joerg LV.inAssignment[DR] = 1; 331 1.1.1.2 joerg } 332 1.1.1.2 joerg } 333 1.1 joerg if (B->isAssignmentOp()) { 334 1.1 joerg if (!LV.killAtAssign) 335 1.1 joerg return; 336 1.1 joerg 337 1.1 joerg // Assigning to a variable? 338 1.1 joerg Expr *LHS = B->getLHS()->IgnoreParens(); 339 1.1 joerg 340 1.1 joerg if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) { 341 1.1 joerg const Decl* D = DR->getDecl(); 342 1.1 joerg bool Killed = false; 343 1.1 joerg 344 1.1 joerg if (const BindingDecl* BD = dyn_cast<BindingDecl>(D)) { 345 1.1 joerg Killed = !BD->getType()->isReferenceType(); 346 1.1 joerg if (Killed) 347 1.1 joerg val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD); 348 1.1 joerg } else if (const auto *VD = dyn_cast<VarDecl>(D)) { 349 1.1 joerg Killed = writeShouldKill(VD); 350 1.1 joerg if (Killed) 351 1.1 joerg val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 352 1.1 joerg 353 1.1 joerg } 354 1.1 joerg 355 1.1 joerg if (Killed && observer) 356 1.1 joerg observer->observerKill(DR); 357 1.1 joerg } 358 1.1 joerg } 359 1.1 joerg } 360 1.1 joerg 361 1.1 joerg void TransferFunctions::VisitBlockExpr(BlockExpr *BE) { 362 1.1 joerg for (const VarDecl *VD : 363 1.1 joerg LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) { 364 1.1 joerg if (isAlwaysAlive(VD)) 365 1.1 joerg continue; 366 1.1 joerg val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); 367 1.1 joerg } 368 1.1 joerg } 369 1.1 joerg 370 1.1 joerg void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) { 371 1.1 joerg const Decl* D = DR->getDecl(); 372 1.1 joerg bool InAssignment = LV.inAssignment[DR]; 373 1.1 joerg if (const auto *BD = dyn_cast<BindingDecl>(D)) { 374 1.1 joerg if (!InAssignment) 375 1.1 joerg val.liveBindings = LV.BSetFact.add(val.liveBindings, BD); 376 1.1 joerg } else if (const auto *VD = dyn_cast<VarDecl>(D)) { 377 1.1 joerg if (!InAssignment && !isAlwaysAlive(VD)) 378 1.1 joerg val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); 379 1.1 joerg } 380 1.1 joerg } 381 1.1 joerg 382 1.1 joerg void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { 383 1.1 joerg for (const auto *DI : DS->decls()) { 384 1.1 joerg if (const auto *DD = dyn_cast<DecompositionDecl>(DI)) { 385 1.1 joerg for (const auto *BD : DD->bindings()) 386 1.1 joerg val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD); 387 1.1 joerg } else if (const auto *VD = dyn_cast<VarDecl>(DI)) { 388 1.1 joerg if (!isAlwaysAlive(VD)) 389 1.1 joerg val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 390 1.1 joerg } 391 1.1 joerg } 392 1.1 joerg } 393 1.1 joerg 394 1.1 joerg void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) { 395 1.1 joerg // Kill the iteration variable. 396 1.1 joerg DeclRefExpr *DR = nullptr; 397 1.1 joerg const VarDecl *VD = nullptr; 398 1.1 joerg 399 1.1 joerg Stmt *element = OS->getElement(); 400 1.1 joerg if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) { 401 1.1 joerg VD = cast<VarDecl>(DS->getSingleDecl()); 402 1.1 joerg } 403 1.1 joerg else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) { 404 1.1 joerg VD = cast<VarDecl>(DR->getDecl()); 405 1.1 joerg } 406 1.1 joerg 407 1.1 joerg if (VD) { 408 1.1 joerg val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 409 1.1 joerg if (observer && DR) 410 1.1 joerg observer->observerKill(DR); 411 1.1 joerg } 412 1.1 joerg } 413 1.1 joerg 414 1.1 joerg void TransferFunctions:: 415 1.1 joerg VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE) 416 1.1 joerg { 417 1.1 joerg // While sizeof(var) doesn't technically extend the liveness of 'var', it 418 1.1 joerg // does extent the liveness of metadata if 'var' is a VariableArrayType. 419 1.1 joerg // We handle that special case here. 420 1.1 joerg if (UE->getKind() != UETT_SizeOf || UE->isArgumentType()) 421 1.1 joerg return; 422 1.1 joerg 423 1.1 joerg const Expr *subEx = UE->getArgumentExpr(); 424 1.1 joerg if (subEx->getType()->isVariableArrayType()) { 425 1.1 joerg assert(subEx->isLValue()); 426 1.1.1.2 joerg val.liveExprs = LV.ESetFact.add(val.liveExprs, subEx->IgnoreParens()); 427 1.1 joerg } 428 1.1 joerg } 429 1.1 joerg 430 1.1 joerg void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) { 431 1.1 joerg // Treat ++/-- as a kill. 432 1.1 joerg // Note we don't actually have to do anything if we don't have an observer, 433 1.1 joerg // since a ++/-- acts as both a kill and a "use". 434 1.1 joerg if (!observer) 435 1.1 joerg return; 436 1.1 joerg 437 1.1 joerg switch (UO->getOpcode()) { 438 1.1 joerg default: 439 1.1 joerg return; 440 1.1 joerg case UO_PostInc: 441 1.1 joerg case UO_PostDec: 442 1.1 joerg case UO_PreInc: 443 1.1 joerg case UO_PreDec: 444 1.1 joerg break; 445 1.1 joerg } 446 1.1 joerg 447 1.1 joerg if (auto *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) { 448 1.1 joerg const Decl *D = DR->getDecl(); 449 1.1 joerg if (isa<VarDecl>(D) || isa<BindingDecl>(D)) { 450 1.1 joerg // Treat ++/-- as a kill. 451 1.1 joerg observer->observerKill(DR); 452 1.1 joerg } 453 1.1 joerg } 454 1.1 joerg } 455 1.1 joerg 456 1.1 joerg LiveVariables::LivenessValues 457 1.1 joerg LiveVariablesImpl::runOnBlock(const CFGBlock *block, 458 1.1 joerg LiveVariables::LivenessValues val, 459 1.1 joerg LiveVariables::Observer *obs) { 460 1.1 joerg 461 1.1 joerg TransferFunctions TF(*this, val, obs, block); 462 1.1 joerg 463 1.1 joerg // Visit the terminator (if any). 464 1.1 joerg if (const Stmt *term = block->getTerminatorStmt()) 465 1.1 joerg TF.Visit(const_cast<Stmt*>(term)); 466 1.1 joerg 467 1.1 joerg // Apply the transfer function for all Stmts in the block. 468 1.1 joerg for (CFGBlock::const_reverse_iterator it = block->rbegin(), 469 1.1 joerg ei = block->rend(); it != ei; ++it) { 470 1.1 joerg const CFGElement &elem = *it; 471 1.1 joerg 472 1.1 joerg if (Optional<CFGAutomaticObjDtor> Dtor = 473 1.1 joerg elem.getAs<CFGAutomaticObjDtor>()) { 474 1.1 joerg val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl()); 475 1.1 joerg continue; 476 1.1 joerg } 477 1.1 joerg 478 1.1 joerg if (!elem.getAs<CFGStmt>()) 479 1.1 joerg continue; 480 1.1 joerg 481 1.1 joerg const Stmt *S = elem.castAs<CFGStmt>().getStmt(); 482 1.1 joerg TF.Visit(const_cast<Stmt*>(S)); 483 1.1 joerg stmtsToLiveness[S] = val; 484 1.1 joerg } 485 1.1 joerg return val; 486 1.1 joerg } 487 1.1 joerg 488 1.1 joerg void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) { 489 1.1 joerg const CFG *cfg = getImpl(impl).analysisContext.getCFG(); 490 1.1 joerg for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) 491 1.1 joerg getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs); 492 1.1 joerg } 493 1.1 joerg 494 1.1 joerg LiveVariables::LiveVariables(void *im) : impl(im) {} 495 1.1 joerg 496 1.1 joerg LiveVariables::~LiveVariables() { 497 1.1 joerg delete (LiveVariablesImpl*) impl; 498 1.1 joerg } 499 1.1 joerg 500 1.1.1.2 joerg std::unique_ptr<LiveVariables> 501 1.1.1.2 joerg LiveVariables::computeLiveness(AnalysisDeclContext &AC, bool killAtAssign) { 502 1.1 joerg 503 1.1 joerg // No CFG? Bail out. 504 1.1 joerg CFG *cfg = AC.getCFG(); 505 1.1 joerg if (!cfg) 506 1.1 joerg return nullptr; 507 1.1 joerg 508 1.1 joerg // The analysis currently has scalability issues for very large CFGs. 509 1.1 joerg // Bail out if it looks too large. 510 1.1 joerg if (cfg->getNumBlockIDs() > 300000) 511 1.1 joerg return nullptr; 512 1.1 joerg 513 1.1 joerg LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign); 514 1.1 joerg 515 1.1 joerg // Construct the dataflow worklist. Enqueue the exit block as the 516 1.1 joerg // start of the analysis. 517 1.1.1.2 joerg BackwardDataflowWorklist worklist(*cfg, AC); 518 1.1 joerg llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs()); 519 1.1 joerg 520 1.1 joerg // FIXME: we should enqueue using post order. 521 1.1.1.2 joerg for (const CFGBlock *B : cfg->nodes()) { 522 1.1.1.2 joerg worklist.enqueueBlock(B); 523 1.1 joerg } 524 1.1 joerg 525 1.1 joerg while (const CFGBlock *block = worklist.dequeue()) { 526 1.1 joerg // Determine if the block's end value has changed. If not, we 527 1.1 joerg // have nothing left to do for this block. 528 1.1 joerg LivenessValues &prevVal = LV->blocksEndToLiveness[block]; 529 1.1 joerg 530 1.1 joerg // Merge the values of all successor blocks. 531 1.1 joerg LivenessValues val; 532 1.1 joerg for (CFGBlock::const_succ_iterator it = block->succ_begin(), 533 1.1 joerg ei = block->succ_end(); it != ei; ++it) { 534 1.1 joerg if (const CFGBlock *succ = *it) { 535 1.1 joerg val = LV->merge(val, LV->blocksBeginToLiveness[succ]); 536 1.1 joerg } 537 1.1 joerg } 538 1.1 joerg 539 1.1 joerg if (!everAnalyzedBlock[block->getBlockID()]) 540 1.1 joerg everAnalyzedBlock[block->getBlockID()] = true; 541 1.1 joerg else if (prevVal.equals(val)) 542 1.1 joerg continue; 543 1.1 joerg 544 1.1 joerg prevVal = val; 545 1.1 joerg 546 1.1 joerg // Update the dataflow value for the start of this block. 547 1.1 joerg LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val); 548 1.1 joerg 549 1.1 joerg // Enqueue the value to the predecessors. 550 1.1 joerg worklist.enqueuePredecessors(block); 551 1.1 joerg } 552 1.1 joerg 553 1.1.1.2 joerg return std::unique_ptr<LiveVariables>(new LiveVariables(LV)); 554 1.1 joerg } 555 1.1 joerg 556 1.1 joerg void LiveVariables::dumpBlockLiveness(const SourceManager &M) { 557 1.1 joerg getImpl(impl).dumpBlockLiveness(M); 558 1.1 joerg } 559 1.1 joerg 560 1.1 joerg void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) { 561 1.1 joerg std::vector<const CFGBlock *> vec; 562 1.1 joerg for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator 563 1.1 joerg it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end(); 564 1.1 joerg it != ei; ++it) { 565 1.1 joerg vec.push_back(it->first); 566 1.1 joerg } 567 1.1 joerg llvm::sort(vec, [](const CFGBlock *A, const CFGBlock *B) { 568 1.1 joerg return A->getBlockID() < B->getBlockID(); 569 1.1 joerg }); 570 1.1 joerg 571 1.1 joerg std::vector<const VarDecl*> declVec; 572 1.1 joerg 573 1.1 joerg for (std::vector<const CFGBlock *>::iterator 574 1.1 joerg it = vec.begin(), ei = vec.end(); it != ei; ++it) { 575 1.1 joerg llvm::errs() << "\n[ B" << (*it)->getBlockID() 576 1.1 joerg << " (live variables at block exit) ]\n"; 577 1.1 joerg 578 1.1 joerg LiveVariables::LivenessValues vals = blocksEndToLiveness[*it]; 579 1.1 joerg declVec.clear(); 580 1.1 joerg 581 1.1 joerg for (llvm::ImmutableSet<const VarDecl *>::iterator si = 582 1.1 joerg vals.liveDecls.begin(), 583 1.1 joerg se = vals.liveDecls.end(); si != se; ++si) { 584 1.1 joerg declVec.push_back(*si); 585 1.1 joerg } 586 1.1 joerg 587 1.1 joerg llvm::sort(declVec, [](const Decl *A, const Decl *B) { 588 1.1 joerg return A->getBeginLoc() < B->getBeginLoc(); 589 1.1 joerg }); 590 1.1 joerg 591 1.1 joerg for (std::vector<const VarDecl*>::iterator di = declVec.begin(), 592 1.1 joerg de = declVec.end(); di != de; ++di) { 593 1.1 joerg llvm::errs() << " " << (*di)->getDeclName().getAsString() 594 1.1 joerg << " <"; 595 1.1 joerg (*di)->getLocation().print(llvm::errs(), M); 596 1.1 joerg llvm::errs() << ">\n"; 597 1.1 joerg } 598 1.1 joerg } 599 1.1 joerg llvm::errs() << "\n"; 600 1.1 joerg } 601 1.1 joerg 602 1.1.1.2 joerg void LiveVariables::dumpExprLiveness(const SourceManager &M) { 603 1.1.1.2 joerg getImpl(impl).dumpExprLiveness(M); 604 1.1 joerg } 605 1.1 joerg 606 1.1.1.2 joerg void LiveVariablesImpl::dumpExprLiveness(const SourceManager &M) { 607 1.1 joerg // Don't iterate over blockEndsToLiveness directly because it's not sorted. 608 1.1.1.2 joerg for (const CFGBlock *B : *analysisContext.getCFG()) { 609 1.1 joerg 610 1.1.1.2 joerg llvm::errs() << "\n[ B" << B->getBlockID() 611 1.1.1.2 joerg << " (live expressions at block exit) ]\n"; 612 1.1.1.2 joerg for (const Expr *E : blocksEndToLiveness[B].liveExprs) { 613 1.1 joerg llvm::errs() << "\n"; 614 1.1.1.2 joerg E->dump(); 615 1.1 joerg } 616 1.1 joerg llvm::errs() << "\n"; 617 1.1 joerg } 618 1.1 joerg } 619 1.1 joerg 620 1.1 joerg const void *LiveVariables::getTag() { static int x; return &x; } 621 1.1 joerg const void *RelaxedLiveVariables::getTag() { static int x; return &x; } 622