| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 2019 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "CopyPropagation.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "A simply copy propagation pass. \ |
| 27 | Copy propagation only happens to: \n\ |
| 28 | * variables \n\ |
| 29 | * member expressions \n\ |
| 30 | * array subscript expressions \n\ |
| 31 | Only those value obtained through simple \ |
| 32 | assignment or initilizer will be propagated. \ |
| 33 | Here \"simple assignment\" is defined as: \n\ |
| 34 | x = y \n\ |
| 35 | where both x and y are either variables, member expressions \ |
| 36 | or array subscript expressions (y could be a constant). \ |
| 37 | For example, x = foo() is not considered as a simple assignment. \ |
| 38 | Copy propagation of LHS will stop upon any non-simple assignment. \ |
| 39 | Therefore, in the above example, foo() will not be propagated. \n"; |
| 40 | |
| 41 | static RegisterTransformation<CopyPropagation> |
| 42 | Trans("copy-propagation", DescriptionMsg); |
| 43 | |
| 44 | namespace { |
| 45 | class ArraySubscriptVisitor : public |
| 46 | RecursiveASTVisitor<ArraySubscriptVisitor> { |
| 47 | public: |
| 48 | explicit ArraySubscriptVisitor(const VarDecl *VD) |
| 49 | : ReferencedVD(VD), HasReference(false) |
| 50 | { } |
| 51 | |
| 52 | bool VisitVarDecl(VarDecl *VD); |
| 53 | |
| 54 | bool hasReferencedVD() { |
| 55 | return HasReference; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | const VarDecl *ReferencedVD; |
| 60 | |
| 61 | bool HasReference; |
| 62 | }; |
| 63 | |
| 64 | bool ArraySubscriptVisitor::VisitVarDecl(VarDecl *VD) { |
| 65 | if (VD->getCanonicalDecl() == ReferencedVD) |
| 66 | HasReference = true; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | } // End anonymous namespace |
| 71 | |
| 72 | class CopyPropCollectionVisitor : public |
| 73 | RecursiveASTVisitor<CopyPropCollectionVisitor> { |
| 74 | public: |
| 75 | |
| 76 | explicit CopyPropCollectionVisitor(CopyPropagation *Instance) |
| 77 | : ConsumerInstance(Instance), |
| 78 | BeingWritten(false), |
| 79 | BeingAddrTaken(false), |
| 80 | BeingIncDec(false), |
| 81 | BeingPartial(false) |
| 82 | { } |
| 83 | |
| 84 | bool VisitVarDecl(VarDecl *VD); |
| 85 | |
| 86 | bool VisitBinaryOperator(BinaryOperator *BO); |
| 87 | |
| 88 | bool VisitUnaryOperator(UnaryOperator *UO); |
| 89 | |
| 90 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 91 | |
| 92 | bool VisitMemberExpr(MemberExpr *ME); |
| 93 | |
| 94 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE); |
| 95 | |
| 96 | private: |
| 97 | |
| 98 | void resetFlags(void); |
| 99 | |
| 100 | CopyPropagation *ConsumerInstance; |
| 101 | |
| 102 | // Indicate if a var/memexpr/arraysubexpr is being written. |
| 103 | // Set by updateExpr and reset by VisitDeclRefExpr |
| 104 | bool BeingWritten; |
| 105 | |
| 106 | bool BeingAddrTaken; |
| 107 | |
| 108 | // It will be true for ++i, --i |
| 109 | // In this case, we cannot copy-propagate a constant to i |
| 110 | bool BeingIncDec; |
| 111 | |
| 112 | // For MemberExpr and ArraySubscriptExpr, we don't want to |
| 113 | // track the partial element. For example, a[0][0][0], |
| 114 | // VisitArraySubscriptExpr will visit: |
| 115 | // a[0][0][0] |
| 116 | // a[0][0] |
| 117 | // a[0] |
| 118 | // We only want to track the first one. This flag will be |
| 119 | // reset when we reach a. |
| 120 | bool BeingPartial; |
| 121 | }; |
| 122 | |
| 123 | void CopyPropCollectionVisitor::resetFlags(void) |
| 124 | { |
| 125 | BeingWritten = false; |
| 126 | BeingAddrTaken = false; |
| 127 | BeingIncDec = false; |
| 128 | BeingPartial = false; |
| 129 | } |
| 130 | |
| 131 | bool CopyPropCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 132 | { |
| 133 | if (!VD->hasInit()) |
| 134 | return true; |
| 135 | |
| 136 | const Expr *Init = VD->getInit(); |
| 137 | |
| 138 | if (ConsumerInstance->isValidExpr(Init)) { |
| 139 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 140 | ConsumerInstance->VarToExpr[CanonicalVD] = Init; |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool CopyPropCollectionVisitor::VisitBinaryOperator(BinaryOperator *BO) |
| 147 | { |
| 148 | if (!BO->isAssignmentOp() && !BO->isCompoundAssignmentOp()) |
| 149 | return true; |
| 150 | |
| 151 | const Expr *Lhs = BO->getLHS()->IgnoreParenCasts(); |
| 152 | if (!ConsumerInstance->isValidLhs(Lhs)) |
| 153 | return true; |
| 154 | |
| 155 | const Expr *Rhs = BO->getRHS()->IgnoreParenCasts(); |
| 156 | |
| 157 | BeingWritten = true; |
| 158 | if (!ConsumerInstance->isValidExpr(Rhs)) { |
| 159 | ConsumerInstance->invalidateExpr(Lhs); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | ConsumerInstance->updateExpr(Lhs, Rhs); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool CopyPropCollectionVisitor::VisitUnaryOperator(UnaryOperator *UO) |
| 168 | { |
| 169 | UnaryOperator::Opcode Op = UO->getOpcode(); |
| 170 | |
| 171 | if (Op == UO_AddrOf) { |
| 172 | BeingAddrTaken = true; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | if (UO->isIncrementDecrementOp()) |
| 177 | BeingIncDec = true; |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | bool CopyPropCollectionVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 183 | { |
| 184 | if (BeingWritten || BeingAddrTaken || BeingPartial) { |
| 185 | resetFlags(); |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 190 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 191 | // DRE could refer to FunctionDecl, etc |
| 192 | if (!VD) |
| 193 | return true; |
| 194 | |
| 195 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 196 | |
| 197 | const Expr *CopyE = ConsumerInstance->VarToExpr[CanonicalVD]; |
| 198 | if (!CopyE || (BeingIncDec && ConsumerInstance->isConstantExpr(CopyE))) { |
| 199 | BeingIncDec = false; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | ConsumerInstance->addOneDominatedExpr(CopyE, DRE); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool CopyPropCollectionVisitor::VisitMemberExpr(MemberExpr *ME) |
| 208 | { |
| 209 | if (BeingWritten || BeingAddrTaken || BeingPartial) { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (!BeingPartial) |
| 214 | BeingPartial = true; |
| 215 | |
| 216 | const Expr *CopyE = ConsumerInstance->MemberExprToExpr[ME]; |
| 217 | if (!CopyE) { |
| 218 | if (!ConsumerInstance->VisitedMEAndASE.count(ME)) { |
| 219 | CopyE = ConsumerInstance->getMemberExprElem(ME); |
| 220 | } |
| 221 | else { |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (!CopyE || (BeingIncDec && ConsumerInstance->isConstantExpr(CopyE))) { |
| 227 | BeingIncDec = false; |
| 228 | BeingPartial = false; |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | ConsumerInstance->addOneDominatedExpr(CopyE, ME); |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool |
| 237 | CopyPropCollectionVisitor::VisitArraySubscriptExpr(ArraySubscriptExpr *ASE) |
| 238 | { |
| 239 | if (BeingWritten || BeingAddrTaken || BeingPartial) { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | if (!BeingPartial) |
| 244 | BeingPartial = true; |
| 245 | |
| 246 | const Expr *CopyE = ConsumerInstance->ArraySubToExpr[ASE]; |
| 247 | if (!CopyE) { |
| 248 | if (!ConsumerInstance->VisitedMEAndASE.count(ASE)) { |
| 249 | CopyE = ConsumerInstance->getArraySubscriptElem(ASE); |
| 250 | } |
| 251 | else { |
| 252 | return true; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!CopyE || (BeingIncDec && ConsumerInstance->isConstantExpr(CopyE))) { |
| 257 | BeingPartial = false; |
| 258 | BeingIncDec = false; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | ConsumerInstance->addOneDominatedExpr(CopyE, ASE); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | void CopyPropagation::Initialize(ASTContext &context) |
| 267 | { |
| 268 | Transformation::Initialize(context); |
| 269 | CollectionVisitor = new CopyPropCollectionVisitor(this); |
| 270 | } |
| 271 | |
| 272 | bool CopyPropagation::HandleTopLevelDecl(DeclGroupRef D) |
| 273 | { |
| 274 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 275 | CollectionVisitor->TraverseDecl(*I); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | void CopyPropagation::HandleTranslationUnit(ASTContext &Ctx) |
| 280 | { |
| 281 | if (QueryInstanceOnly) |
| 282 | return; |
| 283 | |
| 284 | if (TransformationCounter > ValidInstanceNum) { |
| 285 | TransError = TransMaxInstanceError; |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | TransAssert(CollectionVisitor && "NULL CollectionVisitor!"); |
| 290 | |
| 291 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 292 | TransAssert(TheCopyExpr && "NULL TheCopyExpr!"); |
| 293 | |
| 294 | doCopyPropagation(); |
| 295 | |
| 296 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 297 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 298 | TransError = TransInternalError; |
| 299 | } |
| 300 | |
| 301 | // Note we skip InitListExpr, so some likely valid cases won't be handled: |
| 302 | // int i = {1}; |
| 303 | // But this kind of code almost couldn't happen, |
| 304 | // because c_delta will remove {} pair. |
| 305 | bool CopyPropagation::isValidExpr(const Expr *Exp) |
| 306 | { |
| 307 | const Expr *E = Exp->IgnoreParenCasts(); |
| 308 | |
| 309 | switch(E->getStmtClass()) { |
| 310 | case Expr::FloatingLiteralClass: |
| 311 | case Expr::StringLiteralClass: |
| 312 | case Expr::IntegerLiteralClass: |
| 313 | case Expr::GNUNullExprClass: |
| 314 | case Expr::CharacterLiteralClass: |
| 315 | case Expr::DeclRefExprClass: |
| 316 | case Expr::MemberExprClass: |
| 317 | case Expr::ArraySubscriptExprClass: // Fall-through |
| 318 | return true; |
| 319 | |
| 320 | default: |
| 321 | return false; |
| 322 | } |
| 323 | TransAssert(0 && "Unreachable code!"); |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool CopyPropagation::isValidLhs(const Expr *Lhs) |
| 328 | { |
| 329 | const Expr *E = Lhs->IgnoreParenCasts(); |
| 330 | |
| 331 | switch(E->getStmtClass()) { |
| 332 | case Expr::DeclRefExprClass: |
| 333 | case Expr::MemberExprClass: |
| 334 | case Expr::ArraySubscriptExprClass: // Fall-through |
| 335 | return true; |
| 336 | |
| 337 | default: |
| 338 | return false; |
| 339 | } |
| 340 | TransAssert(0 && "Unreachable code!"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | bool CopyPropagation::isConstantExpr(const Expr *Exp) |
| 345 | { |
| 346 | const Expr *E = Exp->IgnoreParenCasts(); |
| 347 | |
| 348 | switch(E->getStmtClass()) { |
| 349 | case Expr::FloatingLiteralClass: |
| 350 | case Expr::StringLiteralClass: |
| 351 | case Expr::IntegerLiteralClass: |
| 352 | case Expr::GNUNullExprClass: |
| 353 | case Expr::CharacterLiteralClass: // Fall-through |
| 354 | return true; |
| 355 | |
| 356 | default: |
| 357 | return false; |
| 358 | } |
| 359 | TransAssert(0 && "Unreachable code!"); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | void CopyPropagation::updateExpr(const Expr *E, const Expr *CopyE) |
| 364 | { |
| 365 | switch (E->getStmtClass()) { |
| 366 | case Expr::DeclRefExprClass: { |
| 367 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 368 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 369 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 370 | |
| 371 | TransAssert(VD && "Bad VD!"); |
| 372 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 373 | VarToExpr[CanonicalVD] = CopyE; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | case Expr::MemberExprClass: { |
| 378 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 379 | MemberExprToExpr[ME] = CopyE; |
| 380 | VisitedMEAndASE.insert(E); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | case Expr::ArraySubscriptExprClass: { |
| 385 | const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E); |
| 386 | ArraySubToExpr[ASE] = CopyE; |
| 387 | VisitedMEAndASE.insert(E); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | default: |
| 392 | TransAssert(0 && "Uncatched Expr!"); |
| 393 | } |
| 394 | |
| 395 | TransAssert(0 && "Unreachable code!"); |
| 396 | } |
| 397 | |
| 398 | void CopyPropagation::invalidateExpr(const Expr *E) |
| 399 | { |
| 400 | updateExpr(E, NULL); |
| 401 | } |
| 402 | |
| 403 | const VarDecl *CopyPropagation::getCanonicalRefVarDecl(const Expr *E) |
| 404 | { |
| 405 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 406 | if (!DRE) |
| 407 | return NULL; |
| 408 | |
| 409 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 410 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 411 | // DRE could refer to FunctionDecl, etc |
| 412 | if (!VD) |
| 413 | return NULL; |
| 414 | |
| 415 | return VD->getCanonicalDecl(); |
| 416 | } |
| 417 | |
| 418 | bool CopyPropagation::isRefToTheSameVar(const Expr *CopyE, |
| 419 | const Expr *DominatedE) |
| 420 | { |
| 421 | const VarDecl *DominatedVD = getCanonicalRefVarDecl(DominatedE); |
| 422 | if (!DominatedVD) |
| 423 | return false; |
| 424 | const VarDecl *CopyVD = getCanonicalRefVarDecl(CopyE); |
| 425 | if (CopyVD) |
| 426 | return CopyVD == DominatedVD; |
| 427 | if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(CopyE)) { |
| 428 | ArraySubscriptVisitor V(DominatedVD); |
| 429 | V.TraverseStmt(const_cast<Expr*>(ASE->getIdx())); |
| 430 | return !V.hasReferencedVD(); |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | bool CopyPropagation::hasSameStringRep(const Expr *CopyE, |
| 436 | const Expr *DominatedE) |
| 437 | { |
| 438 | std::string CopyStr, DominatedStr; |
| 439 | RewriteHelper->getExprString(CopyE, CopyStr); |
| 440 | RewriteHelper->getExprString(DominatedE, DominatedStr); |
| 441 | return (CopyStr == DominatedStr); |
| 442 | } |
| 443 | |
| 444 | void CopyPropagation::addOneDominatedExpr(const Expr *CopyE, |
| 445 | const Expr *DominatedE) |
| 446 | { |
| 447 | if (isInIncludedFile(CopyE) || isInIncludedFile(DominatedE)) |
| 448 | return; |
| 449 | |
| 450 | if ((CopyE == DominatedE) || isRefToTheSameVar(CopyE, DominatedE) || |
| 451 | hasSameStringRep(CopyE, DominatedE)) |
| 452 | return; |
| 453 | |
| 454 | ExprSet *ESet = DominatedMap[CopyE]; |
| 455 | if (!ESet) { |
| 456 | ESet = new ExprSet(); |
| 457 | TransAssert(ESet && "Couldn't new ExprSet"); |
| 458 | DominatedMap[CopyE] = ESet; |
| 459 | |
| 460 | ValidInstanceNum++; |
| 461 | if (TransformationCounter == ValidInstanceNum) |
| 462 | TheCopyExpr = CopyE; |
| 463 | } |
| 464 | ESet->insert(DominatedE); |
| 465 | } |
| 466 | |
| 467 | void CopyPropagation::doCopyPropagation(void) |
| 468 | { |
| 469 | std::string CopyStr(""); |
| 470 | RewriteHelper->getExprString(TheCopyExpr, CopyStr); |
| 471 | ExprSet *ESet = DominatedMap[TheCopyExpr]; |
| 472 | TransAssert(ESet && "Empty Expr Set!"); |
| 473 | for (ExprSet::iterator I = ESet->begin(), E = ESet->end(); I != E; ++I) { |
| 474 | RewriteHelper->replaceExpr((*I), CopyStr); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | CopyPropagation::~CopyPropagation(void) |
| 479 | { |
| 480 | delete CollectionVisitor; |
| 481 | |
| 482 | for (ExprToExprsMap::iterator I = DominatedMap.begin(), |
| 483 | E = DominatedMap.end(); I != E; ++I) { |
| 484 | delete (*I).second; |
| 485 | } |
| 486 | } |
| 487 |