| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 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 "SimpleInliner.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Mangle.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
| 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | |
| 25 | #include "TransformationManager.h" |
| 26 | #include "CommonStatementVisitor.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | static const char *DescriptionMsg = |
| 31 | "A really simple inliner. \ |
| 32 | This transformation does a simple source-to-source \ |
| 33 | inlining. To avoid the abuse of inlining, I put \ |
| 34 | some constraints on the size of a function which \ |
| 35 | can be inlined - if a function has less than 10 statements, \ |
| 36 | then it's legitimate. \n\ |
| 37 | \n\ |
| 38 | Steps of inlining: \n\ |
| 39 | * create a tmp var for function return value; \n\ |
| 40 | * create a new block which is a copy of the inlined function; \n\ |
| 41 | * at the top of this newly block, inlined function's parameters \ |
| 42 | will be declared as local vars with callexpr's arguments as their \ |
| 43 | initialization values (if any) \n\ |
| 44 | * inside this newly block, replace all return statements as \ |
| 45 | assignment statements, where the LHS is the created tmp var \ |
| 46 | (Note that if the inlined function returns void, then \ |
| 47 | this step is skipped) \n\ |
| 48 | * replace the callexpr with tmp var above \n\ |
| 49 | \n\ |
| 50 | Each transformation iteration only transforms one callexpr, \ |
| 51 | also it will keep the inlined function body unchanged. \ |
| 52 | If the inlined body has no reference anymore, c_delta \ |
| 53 | will remove it entirely. \n"; |
| 54 | |
| 55 | static RegisterTransformation<SimpleInliner> |
| 56 | Trans("simple-inliner", DescriptionMsg); |
| 57 | |
| 58 | class SimpleInlinerCollectionVisitor : public |
| 59 | RecursiveASTVisitor<SimpleInlinerCollectionVisitor> { |
| 60 | |
| 61 | public: |
| 62 | |
| 63 | explicit SimpleInlinerCollectionVisitor(SimpleInliner *Instance) |
| 64 | : ConsumerInstance(Instance), |
| 65 | NumStmts(0) |
| 66 | { } |
| 67 | |
| 68 | bool VisitStmt(Stmt *S); |
| 69 | |
| 70 | bool VisitCallExpr(CallExpr *CE); |
| 71 | |
| 72 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init); |
| 73 | |
| 74 | bool TraverseParmVarDecl(ParmVarDecl *PV); |
| 75 | |
| 76 | unsigned int getNumStmts(void) { |
| 77 | return NumStmts; |
| 78 | } |
| 79 | |
| 80 | void setNumStmts(unsigned int Num) { |
| 81 | NumStmts = Num; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | |
| 86 | SimpleInliner *ConsumerInstance; |
| 87 | |
| 88 | unsigned int NumStmts; |
| 89 | }; |
| 90 | |
| 91 | class SimpleInlinerFunctionVisitor : public |
| 92 | RecursiveASTVisitor<SimpleInlinerFunctionVisitor> { |
| 93 | |
| 94 | public: |
| 95 | |
| 96 | explicit SimpleInlinerFunctionVisitor(SimpleInliner *Instance) |
| 97 | : ConsumerInstance(Instance) |
| 98 | { } |
| 99 | |
| 100 | bool VisitReturnStmt(ReturnStmt *RS); |
| 101 | |
| 102 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 103 | |
| 104 | private: |
| 105 | |
| 106 | SimpleInliner *ConsumerInstance; |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | class SimpleInlinerFunctionStmtVisitor : public |
| 111 | RecursiveASTVisitor<SimpleInlinerFunctionStmtVisitor> { |
| 112 | public: |
| 113 | |
| 114 | explicit SimpleInlinerFunctionStmtVisitor(SimpleInliner *Instance) |
| 115 | : ConsumerInstance(Instance) |
| 116 | { } |
| 117 | |
| 118 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 119 | |
| 120 | private: |
| 121 | SimpleInliner *ConsumerInstance; |
| 122 | |
| 123 | }; |
| 124 | |
| 125 | class SimpleInlinerStmtVisitor : public |
| 126 | CommonStatementVisitor<SimpleInlinerStmtVisitor> { |
| 127 | |
| 128 | public: |
| 129 | |
| 130 | explicit SimpleInlinerStmtVisitor(SimpleInliner *Instance) |
| 131 | : ConsumerInstance(Instance) |
| 132 | { } |
| 133 | |
| 134 | bool VisitCallExpr(CallExpr *CallE); |
| 135 | |
| 136 | private: |
| 137 | |
| 138 | SimpleInliner *ConsumerInstance; |
| 139 | }; |
| 140 | |
| 141 | bool SimpleInlinerCollectionVisitor::VisitStmt(Stmt *S) |
| 142 | { |
| 143 | Stmt::StmtClass SC = S->getStmtClass(); |
| 144 | switch (SC) { |
| 145 | case Stmt::BreakStmtClass: |
| 146 | case Stmt::CompoundStmtClass: |
| 147 | case Stmt::ContinueStmtClass: |
| 148 | case Stmt::DeclStmtClass: |
| 149 | case Stmt::DoStmtClass: |
| 150 | case Stmt::ForStmtClass: |
| 151 | case Stmt::GotoStmtClass: |
| 152 | case Stmt::IndirectGotoStmtClass: |
| 153 | case Stmt::IfStmtClass: |
| 154 | case Stmt::ReturnStmtClass: |
| 155 | case Stmt::CaseStmtClass: |
| 156 | case Stmt::SwitchStmtClass: |
| 157 | case Stmt::WhileStmtClass: |
| 158 | case Stmt::BinaryOperatorClass: |
| 159 | NumStmts++; |
| 160 | break; |
| 161 | default: |
| 162 | break; |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool SimpleInlinerCollectionVisitor::VisitCallExpr(CallExpr *CE) |
| 168 | { |
| 169 | FunctionDecl *FD = CE->getDirectCallee(); |
| 170 | if (!FD) |
| 171 | return true; |
| 172 | |
| 173 | ConsumerInstance->AllCallExprs.push_back(CE); |
| 174 | ConsumerInstance->CalleeToCallerMap[CE] = ConsumerInstance->CurrentFD; |
| 175 | |
| 176 | FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 177 | unsigned int NumCalls = ConsumerInstance->FunctionDeclNumCalls[CanonicalFD]; |
| 178 | NumCalls++; |
| 179 | ConsumerInstance->FunctionDeclNumCalls[CanonicalFD] = NumCalls; |
| 180 | |
| 181 | NumStmts++; |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | // Overload the default traverse function, because we cannot inline |
| 186 | // Ctor's initializer |
| 187 | bool SimpleInlinerCollectionVisitor::TraverseConstructorInitializer( |
| 188 | CXXCtorInitializer *Init) |
| 189 | { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool SimpleInlinerCollectionVisitor::TraverseParmVarDecl(ParmVarDecl *PV) |
| 194 | { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | bool SimpleInlinerFunctionVisitor::VisitReturnStmt(ReturnStmt *RS) |
| 199 | { |
| 200 | ConsumerInstance->ReturnStmts.push_back(RS); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool SimpleInlinerFunctionVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 205 | { |
| 206 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 207 | const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(OrigDecl); |
| 208 | if (PD) |
| 209 | ConsumerInstance->ParmRefs.push_back(DRE); |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | bool SimpleInlinerFunctionStmtVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 214 | { |
| 215 | if (ConsumerInstance->isInIncludedFile(FD) || |
| 216 | !FD->isThisDeclarationADefinition()) |
| 217 | return true; |
| 218 | |
| 219 | if (FD->getBeginLoc().isInvalid() || FD->getEndLoc().isInvalid()) |
| 220 | return true; |
| 221 | ConsumerInstance->CurrentFD = FD; |
| 222 | ConsumerInstance->CollectionVisitor->setNumStmts(0); |
| 223 | ConsumerInstance->CollectionVisitor->TraverseDecl(FD); |
| 224 | |
| 225 | ConsumerInstance-> |
| 226 | MangledNameToFuncDeclMap[ConsumerInstance->getMangledName(FD)] = FD; |
| 227 | |
| 228 | if (!FD->isVariadic()) { |
| 229 | ConsumerInstance->FunctionDeclNumStmts[FD->getCanonicalDecl()] = |
| 230 | ConsumerInstance->CollectionVisitor->getNumStmts(); |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool SimpleInlinerStmtVisitor::VisitCallExpr(CallExpr *CallE) |
| 236 | { |
| 237 | if (ConsumerInstance->TheCallExpr == CallE) { |
| 238 | ConsumerInstance->TheStmt = CurrentStmt; |
| 239 | ConsumerInstance->NeedParen = NeedParen; |
| 240 | // Stop recursion |
| 241 | return false; |
| 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | void SimpleInliner::Initialize(ASTContext &context) |
| 247 | { |
| 248 | Transformation::Initialize(context); |
| 249 | NameQueryWrap = |
| 250 | new TransNameQueryWrap(RewriteHelper->getTmpVarNamePrefix()); |
| 251 | CollectionVisitor = new SimpleInlinerCollectionVisitor(this); |
| 252 | FunctionVisitor = new SimpleInlinerFunctionVisitor(this); |
| 253 | FunctionStmtVisitor = new SimpleInlinerFunctionStmtVisitor(this); |
| 254 | StmtVisitor = new SimpleInlinerStmtVisitor(this); |
| 255 | if (context.getTargetInfo().getTriple().isOSWindows()) { |
| 256 | MangleCtx = |
| 257 | MicrosoftMangleContext::create(context, context.getDiagnostics()); |
| 258 | } else { |
| 259 | MangleCtx = ItaniumMangleContext::create(context, context.getDiagnostics()); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | bool SimpleInliner::HandleTopLevelDecl(DeclGroupRef D) |
| 264 | { |
| 265 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 266 | FunctionStmtVisitor->TraverseDecl(*I); |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | void SimpleInliner::HandleTranslationUnit(ASTContext &Ctx) |
| 272 | { |
| 273 | doAnalysis(); |
| 274 | if (QueryInstanceOnly) |
| 275 | return; |
| 276 | |
| 277 | if (TransformationCounter > ValidInstanceNum) { |
| 278 | TransError = TransMaxInstanceError; |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | TransAssert(CurrentFD && "NULL CurrentFD!"); |
| 283 | TransAssert(TheCallExpr && "NULL TheCallExpr!"); |
| 284 | |
| 285 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 286 | |
| 287 | NameQueryWrap->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 288 | NamePostfix = NameQueryWrap->getMaxNamePostfix() + 1; |
| 289 | |
| 290 | if (FunctionDecl *AliaseeFD = getAliaseeFunctionDecl(CurrentFD)) { |
| 291 | FunctionVisitor->TraverseDecl(AliaseeFD); |
| 292 | } else { |
| 293 | FunctionVisitor->TraverseDecl(CurrentFD); |
| 294 | } |
| 295 | StmtVisitor->TraverseDecl(TheCaller); |
| 296 | |
| 297 | TransAssert(TheStmt && "NULL TheStmt!"); |
| 298 | replaceCallExpr(); |
| 299 | |
| 300 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 301 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 302 | TransError = TransInternalError; |
| 303 | } |
| 304 | |
| 305 | std::string SimpleInliner::getMangledName(FunctionDecl *FD) { |
| 306 | if (TransformationManager::isCLangOpt()) { |
| 307 | return FD->getNameAsString(); |
| 308 | } else { |
| 309 | std::string S; |
| 310 | llvm::raw_string_ostream Stream(S); |
| 311 | MangleCtx->mangleName(FD, Stream); |
| 312 | return Stream.str(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | bool SimpleInliner::isValidArgExpr(const Expr *E) |
| 317 | { |
| 318 | TransAssert(E && "NULL Expr!"); |
| 319 | switch(E->getStmtClass()) { |
| 320 | case Expr::FloatingLiteralClass: |
| 321 | case Expr::StringLiteralClass: |
| 322 | case Expr::IntegerLiteralClass: |
| 323 | case Expr::GNUNullExprClass: |
| 324 | case Expr::CharacterLiteralClass: // Fall-through |
| 325 | return true; |
| 326 | |
| 327 | case Expr::ParenExprClass: |
| 328 | return isValidArgExpr(cast<ParenExpr>(E)->getSubExpr()); |
| 329 | |
| 330 | case Expr::ImplicitCastExprClass: |
| 331 | case Expr::CStyleCastExprClass: // Fall-through |
| 332 | return isValidArgExpr(cast<CastExpr>(E)->getSubExpr()); |
| 333 | |
| 334 | case Expr::MemberExprClass: |
| 335 | return true; |
| 336 | |
| 337 | case Expr::ArraySubscriptExprClass: { |
| 338 | const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E); |
| 339 | return isValidArgExpr(AE->getIdx()); |
| 340 | } |
| 341 | |
| 342 | case Expr::DeclRefExprClass: |
| 343 | return true; |
| 344 | |
| 345 | default: |
| 346 | return false; |
| 347 | } |
| 348 | TransAssert(0 && "Unreachable code!"); |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | bool SimpleInliner::hasValidArgExprs(const CallExpr *CE) |
| 353 | { |
| 354 | for(CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 355 | I != E; ++I) { |
| 356 | const Expr *Exp = *I; |
| 357 | if (!isValidArgExpr(Exp)) |
| 358 | return false; |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | void SimpleInliner::getValidFunctionDecls(void) |
| 364 | { |
| 365 | for (FunctionDeclToNumStmtsMap::iterator I = FunctionDeclNumStmts.begin(), |
| 366 | E = FunctionDeclNumStmts.end(); I != E; ++I) { |
| 367 | FunctionDecl *FD = (*I).first; |
| 368 | if (FD->hasDefiningAttr()) { |
| 369 | // skip bad alias |
| 370 | if (!getAliaseeFunctionDecl(FD)) { |
| 371 | continue; |
| 372 | } |
| 373 | } |
| 374 | unsigned int NumStmts = (*I).second; |
| 375 | unsigned int NumCalls = FunctionDeclNumCalls[FD]; |
| 376 | |
| 377 | if (((NumCalls == 1) && (NumStmts <= SingleMaxNumStmts)) || |
| 378 | ((NumCalls > 1) && (NumStmts <= MaxNumStmts))) { |
| 379 | ValidFunctionDecls.insert(FD); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void SimpleInliner::doAnalysis(void) |
| 385 | { |
| 386 | getValidFunctionDecls(); |
| 387 | |
| 388 | for (SmallVector<CallExpr *, 10>::iterator CI = AllCallExprs.begin(), |
| 389 | CE = AllCallExprs.end(); CI != CE; ++CI) { |
| 390 | |
| 391 | FunctionDecl *CalleeDecl = (*CI)->getDirectCallee(); |
| 392 | TransAssert(CalleeDecl && "Bad CalleeDecl!"); |
| 393 | FunctionDecl *CanonicalDecl = CalleeDecl->getCanonicalDecl(); |
| 394 | if (!ValidFunctionDecls.count(CanonicalDecl)) |
| 395 | continue; |
| 396 | // skip recursive call |
| 397 | if (CalleeDecl->getCanonicalDecl() == CalleeToCallerMap[(*CI)]) |
| 398 | continue; |
| 399 | |
| 400 | if (!hasValidArgExprs(*CI)) |
| 401 | continue; |
| 402 | |
| 403 | ValidInstanceNum++; |
| 404 | if (TransformationCounter == ValidInstanceNum) { |
| 405 | // It's possible the direct callee is not a definition |
| 406 | if (!CalleeDecl->isThisDeclarationADefinition()) { |
| 407 | CalleeDecl = CalleeDecl->getCanonicalDecl(); |
| 408 | for(FunctionDecl::redecl_iterator RI = CalleeDecl->redecls_begin(), |
| 409 | RE = CalleeDecl->redecls_end(); RI != RE; ++RI) { |
| 410 | if ((*RI)->isThisDeclarationADefinition()) { |
| 411 | CalleeDecl = (*RI); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | TransAssert(CalleeDecl->isThisDeclarationADefinition() && |
| 417 | "Bad CalleeDecl!"); |
| 418 | CurrentFD = CalleeDecl; |
| 419 | TheCaller = CalleeToCallerMap[(*CI)]; |
| 420 | TransAssert(TheCaller && "NULL TheCaller!"); |
| 421 | TheCallExpr = (*CI); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | std::string SimpleInliner::getNewTmpName(void) |
| 427 | { |
| 428 | std::stringstream SS; |
| 429 | SS << RewriteHelper->getTmpVarNamePrefix() << NamePostfix; |
| 430 | NamePostfix++; |
| 431 | return SS.str(); |
| 432 | } |
| 433 | |
| 434 | void SimpleInliner::createReturnVar(void) |
| 435 | { |
| 436 | const Type *FDType = CurrentFD->getReturnType().getTypePtr(); |
| 437 | const Type *CallExprType = |
| 438 | TheCallExpr->getCallReturnType(CurrentFD->getASTContext()).getTypePtr(); |
| 439 | |
| 440 | // We don't need tmp var |
| 441 | if (FDType->isVoidType() && CallExprType->isVoidType()) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | TmpVarName = getNewTmpName(); |
| 446 | std::string VarStr = TmpVarName; |
| 447 | CurrentFD->getReturnType().getAsStringInternal(VarStr, |
| 448 | Context->getPrintingPolicy()); |
| 449 | VarStr += ";"; |
| 450 | RewriteHelper->addLocalVarToFunc(VarStr, TheCaller); |
| 451 | } |
| 452 | |
| 453 | bool SimpleInliner::hasNameClash(const std::string &ParmName, const Expr *E) |
| 454 | { |
| 455 | E = E->IgnoreParenCasts(); |
| 456 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 457 | if (!DRE) |
| 458 | return false; |
| 459 | return ParmName == DRE->getDecl()->getNameAsString(); |
| 460 | } |
| 461 | |
| 462 | void SimpleInliner::generateParamStrings(void) |
| 463 | { |
| 464 | unsigned int ArgNum = TheCallExpr->getNumArgs(); |
| 465 | FunctionDecl *FD = TheCallExpr->getDirectCallee(); |
| 466 | unsigned int Idx; |
| 467 | |
| 468 | for(Idx = 0; Idx < FD->getNumParams(); ++Idx) { |
| 469 | const ParmVarDecl *PD = FD->getParamDecl(Idx); |
| 470 | std::string ParmStr = PD->getNameAsString(); |
| 471 | if (Idx < ArgNum) { |
| 472 | const Expr *Arg = TheCallExpr->getArg(Idx); |
| 473 | std::string ArgStr; |
| 474 | RewriteHelper->getExprString(Arg, ArgStr); |
| 475 | |
| 476 | // create a new tmp for parms with name clash |
| 477 | if (hasNameClash(ParmStr, Arg)) { |
| 478 | std::string TmpName = getNewTmpName(); |
| 479 | std::string NewParmStr = TmpName; |
| 480 | PD->getType().getAsStringInternal(NewParmStr, |
| 481 | Context->getPrintingPolicy()); |
| 482 | ParmsWithNameClash.push_back(NewParmStr + " = " + ArgStr + ";\n"); |
| 483 | ArgStr = TmpName; |
| 484 | } |
| 485 | PD->getType().getAsStringInternal(ParmStr, |
| 486 | Context->getPrintingPolicy()); |
| 487 | ParmStr += " = " + ArgStr; |
| 488 | } |
| 489 | else { |
| 490 | PD->getType().getAsStringInternal(ParmStr, |
| 491 | Context->getPrintingPolicy()); |
| 492 | } |
| 493 | ParmStr += ";\n"; |
| 494 | ParmStrings.push_back(ParmStr); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void SimpleInliner::insertReturnStmt |
| 499 | (std::vector< std::pair<ReturnStmt *, int> > &SortedReturnStmts, |
| 500 | ReturnStmt *RS, int Off) |
| 501 | { |
| 502 | std::pair<ReturnStmt *, int> ReturnStmtOffPair(RS, Off); |
| 503 | if (SortedReturnStmts.empty()) { |
| 504 | SortedReturnStmts.push_back(ReturnStmtOffPair); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | std::vector< std::pair<ReturnStmt *, int> >::iterator I, E; |
| 509 | for(I = SortedReturnStmts.begin(), E = SortedReturnStmts.end(); I != E; ++I) { |
| 510 | int TmpOff = (*I).second; |
| 511 | if (Off < TmpOff) |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | if (I == E) |
| 516 | SortedReturnStmts.push_back(ReturnStmtOffPair); |
| 517 | else |
| 518 | SortedReturnStmts.insert(I, ReturnStmtOffPair); |
| 519 | } |
| 520 | |
| 521 | void SimpleInliner::sortReturnStmtsByOffs(const char *StartBuf, |
| 522 | std::vector< std::pair<ReturnStmt *, int> > &SortedReturnStmts) |
| 523 | { |
| 524 | for (ReturnStmtsVector::iterator I = ReturnStmts.begin(), |
| 525 | E = ReturnStmts.end(); I != E; ++I) { |
| 526 | ReturnStmt *RS = (*I); |
| 527 | SourceLocation RSLocStart = RS->getBeginLoc(); |
| 528 | const char *RSStartBuf = SrcManager->getCharacterData(RSLocStart); |
| 529 | int Off = RSStartBuf - StartBuf; |
| 530 | TransAssert((Off >= 0) && "Bad Offset!"); |
| 531 | insertReturnStmt(SortedReturnStmts, RS, Off); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | FunctionDecl *SimpleInliner::getAliaseeFunctionDecl(FunctionDecl *FD) { |
| 536 | if (const Attr *A = FD->getDefiningAttr()) { |
| 537 | const AliasAttr *Alias = cast<AliasAttr>(A); |
| 538 | llvm::StringRef Aliasee = Alias->getAliasee(); |
| 539 | auto I = MangledNameToFuncDeclMap.find(Aliasee.str()); |
| 540 | if (I == MangledNameToFuncDeclMap.end()) |
| 541 | return nullptr; |
| 542 | else |
| 543 | return I->second; |
| 544 | } else { |
| 545 | return FD; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void SimpleInliner::copyFunctionBody(void) |
| 550 | { |
| 551 | Stmt *Body; |
| 552 | FunctionDecl *AliaseeFD = getAliaseeFunctionDecl(CurrentFD); |
| 553 | if (AliaseeFD) { |
| 554 | Body = AliaseeFD->getBody(); |
| 555 | } else { |
| 556 | Body = CurrentFD->getBody(); |
| 557 | } |
| 558 | TransAssert(Body && "NULL Body!"); |
| 559 | |
| 560 | std::string FuncBodyStr(""); |
| 561 | RewriteHelper->getStmtString(Body, FuncBodyStr); |
| 562 | TransAssert(FuncBodyStr[0] == '{'); |
| 563 | |
| 564 | SourceLocation StartLoc = Body->getBeginLoc(); |
| 565 | const char *StartBuf = SrcManager->getCharacterData(StartLoc); |
| 566 | |
| 567 | std::vector< std::pair<ReturnStmt *, int> > SortedReturnStmts; |
| 568 | sortReturnStmtsByOffs(StartBuf, SortedReturnStmts); |
| 569 | |
| 570 | // Now we start rewriting |
| 571 | int Delta = 1; // skip the first { symbol |
| 572 | for(SmallVector<std::string, 10>::iterator I = ParmStrings.begin(), |
| 573 | E = ParmStrings.end(); I != E; ++I) { |
| 574 | std::string PStr = (*I); |
| 575 | FuncBodyStr.insert(Delta, PStr); |
| 576 | Delta += PStr.size(); |
| 577 | } |
| 578 | |
| 579 | // restore the effect of { |
| 580 | Delta--; |
| 581 | int ReturnSZ = 6; |
| 582 | std::string TmpVarStr = TmpVarName + " = "; |
| 583 | int TmpVarNameSize = static_cast<int>(TmpVarStr.size()); |
| 584 | |
| 585 | for(std::vector< std::pair<ReturnStmt *, int> >::iterator |
| 586 | I = SortedReturnStmts.begin(), E = SortedReturnStmts.end(); |
| 587 | I != E; ++I) { |
| 588 | |
| 589 | ReturnStmt *RS = (*I).first; |
| 590 | int Off = (*I).second + Delta; |
| 591 | Expr *Exp = RS->getRetValue(); |
| 592 | if (Exp) { |
| 593 | const Type *T = Exp->getType().getTypePtr(); |
| 594 | if (!T->isVoidType()) { |
| 595 | FuncBodyStr.replace(Off, ReturnSZ, TmpVarStr); |
| 596 | Delta += (TmpVarNameSize - ReturnSZ); |
| 597 | continue; |
| 598 | } |
| 599 | } |
| 600 | FuncBodyStr.replace(Off, ReturnSZ, ""); |
| 601 | Delta -= ReturnSZ; |
| 602 | } |
| 603 | |
| 604 | if (ParmsWithNameClash.size()) { |
| 605 | std::string ExtraStr; |
| 606 | for (auto Parm : ParmsWithNameClash) { |
| 607 | ExtraStr += Parm; |
| 608 | } |
| 609 | FuncBodyStr = "{\n" + ExtraStr + FuncBodyStr + "}\n"; |
| 610 | } |
| 611 | |
| 612 | RewriteHelper->addStringBeforeStmt(TheStmt, FuncBodyStr, NeedParen); |
| 613 | } |
| 614 | |
| 615 | void SimpleInliner::removeFunctionBody(void) |
| 616 | { |
| 617 | if (FunctionDecl *FD = CurrentFD->getInstantiatedFromMemberFunction()) { |
| 618 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 619 | // CurrentFD is instantiated from a member function. Extend the source |
| 620 | // begin to the outer template keyword. |
| 621 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
| 622 | CXXMethodDecl *MostRecent = MD->getMostRecentDecl(); |
| 623 | if (MostRecent != MD) { |
| 624 | TheRewriter.RemoveText(MostRecent->getSourceRange()); |
| 625 | return; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (FunctionTemplateDecl *FTD = CurrentFD->getPrimaryTemplate()) { |
| 631 | if (FTD->getBeginLoc().isValid() && FTD->getEndLoc().isValid()) { |
| 632 | TheRewriter.RemoveText(FTD->getSourceRange()); |
| 633 | return; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | SourceRange FDRange = CurrentFD->getSourceRange(); |
| 638 | TheRewriter.RemoveText(FDRange); |
| 639 | } |
| 640 | |
| 641 | void SimpleInliner::replaceCallExpr(void) |
| 642 | { |
| 643 | // Create a new tmp var for return value |
| 644 | createReturnVar(); |
| 645 | // reset ParmsWithNameClash |
| 646 | ParmsWithNameClash.clear(); |
| 647 | generateParamStrings(); |
| 648 | copyFunctionBody(); |
| 649 | RewriteHelper->replaceExprNotInclude(TheCallExpr, TmpVarName); |
| 650 | |
| 651 | FunctionDecl *CanonicalFD = CurrentFD->getCanonicalDecl(); |
| 652 | if (FunctionDeclNumCalls[CanonicalFD] == 1) |
| 653 | removeFunctionBody(); |
| 654 | } |
| 655 | |
| 656 | SimpleInliner::~SimpleInliner(void) |
| 657 | { |
| 658 | delete NameQueryWrap; |
| 659 | delete CollectionVisitor; |
| 660 | delete FunctionVisitor; |
| 661 | delete FunctionStmtVisitor; |
| 662 | delete StmtVisitor; |
| 663 | } |
| 664 |