1 //===- FunctionAttrs.cpp - Pass which marks functions readnone or readonly ===// 2 // 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 // This file implements a simple interprocedural pass which walks the 11 // call-graph, looking for functions which do not access or only read 12 // non-local memory, and marking them readnone/readonly. In addition, 13 // it marks function arguments (of pointer type) 'nocapture' if a call 14 // to the function does not create any copies of the pointer value that 15 // outlive the call. This more or less means that the pointer is only 16 // dereferenced, and not returned from the function or stored in a global. 17 // This pass is implemented as a bottom-up traversal of the call-graph. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #define DEBUG_TYPE "functionattrs" 22 #include "llvm/Transforms/IPO.h" 23 #include "llvm/CallGraphSCCPass.h" 24 #include "llvm/GlobalVariable.h" 25 #include "llvm/IntrinsicInst.h" 26 #include "llvm/LLVMContext.h" 27 #include "llvm/Analysis/AliasAnalysis.h" 28 #include "llvm/Analysis/CallGraph.h" 29 #include "llvm/Analysis/CaptureTracking.h" 30 #include "llvm/ADT/SCCIterator.h" 31 #include "llvm/ADT/SmallSet.h" 32 #include "llvm/ADT/Statistic.h" 33 #include "llvm/ADT/UniqueVector.h" 34 #include "llvm/Support/InstIterator.h" 35 using namespace llvm; 36 37 STATISTIC(NumReadNone, "Number of functions marked readnone"); 38 STATISTIC(NumReadOnly, "Number of functions marked readonly"); 39 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); 40 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); 41 42 namespace { 43 struct FunctionAttrs : public CallGraphSCCPass { 44 static char ID; // Pass identification, replacement for typeid 45 FunctionAttrs() : CallGraphSCCPass(ID), AA(0) { 46 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); 47 } 48 49 // runOnSCC - Analyze the SCC, performing the transformation if possible. 50 bool runOnSCC(CallGraphSCC &SCC); 51 52 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 53 bool AddReadAttrs(const CallGraphSCC &SCC); 54 55 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. 56 bool AddNoCaptureAttrs(const CallGraphSCC &SCC); 57 58 // IsFunctionMallocLike - Does this function allocate new memory? 59 bool IsFunctionMallocLike(Function *F, 60 SmallPtrSet<Function*, 8> &) const; 61 62 // AddNoAliasAttrs - Deduce noalias attributes for the SCC. 63 bool AddNoAliasAttrs(const CallGraphSCC &SCC); 64 65 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 66 AU.setPreservesCFG(); 67 AU.addRequired<AliasAnalysis>(); 68 CallGraphSCCPass::getAnalysisUsage(AU); 69 } 70 71 private: 72 AliasAnalysis *AA; 73 }; 74 } 75 76 char FunctionAttrs::ID = 0; 77 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", 78 "Deduce function attributes", false, false) 79 INITIALIZE_AG_DEPENDENCY(CallGraph) 80 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", 81 "Deduce function attributes", false, false) 82 83 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } 84 85 86 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 87 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { 88 SmallPtrSet<Function*, 8> SCCNodes; 89 90 // Fill SCCNodes with the elements of the SCC. Used for quickly 91 // looking up whether a given CallGraphNode is in this SCC. 92 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 93 SCCNodes.insert((*I)->getFunction()); 94 95 // Check if any of the functions in the SCC read or write memory. If they 96 // write memory then they can't be marked readnone or readonly. 97 bool ReadsMemory = false; 98 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 99 Function *F = (*I)->getFunction(); 100 101 if (F == 0) 102 // External node - may write memory. Just give up. 103 return false; 104 105 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F); 106 if (MRB == AliasAnalysis::DoesNotAccessMemory) 107 // Already perfect! 108 continue; 109 110 // Definitions with weak linkage may be overridden at linktime with 111 // something that writes memory, so treat them like declarations. 112 if (F->isDeclaration() || F->mayBeOverridden()) { 113 if (!AliasAnalysis::onlyReadsMemory(MRB)) 114 // May write memory. Just give up. 115 return false; 116 117 ReadsMemory = true; 118 continue; 119 } 120 121 // Scan the function body for instructions that may read or write memory. 122 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { 123 Instruction *I = &*II; 124 125 // Some instructions can be ignored even if they read or write memory. 126 // Detect these now, skipping to the next instruction if one is found. 127 CallSite CS(cast<Value>(I)); 128 if (CS) { 129 // Ignore calls to functions in the same SCC. 130 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) 131 continue; 132 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS); 133 // If the call doesn't access arbitrary memory, we may be able to 134 // figure out something. 135 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { 136 // If the call does access argument pointees, check each argument. 137 if (AliasAnalysis::doesAccessArgPointees(MRB)) 138 // Check whether all pointer arguments point to local memory, and 139 // ignore calls that only access local memory. 140 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 141 CI != CE; ++CI) { 142 Value *Arg = *CI; 143 if (Arg->getType()->isPointerTy()) { 144 AliasAnalysis::Location Loc(Arg, 145 AliasAnalysis::UnknownSize, 146 I->getMetadata(LLVMContext::MD_tbaa)); 147 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) { 148 if (MRB & AliasAnalysis::Mod) 149 // Writes non-local memory. Give up. 150 return false; 151 if (MRB & AliasAnalysis::Ref) 152 // Ok, it reads non-local memory. 153 ReadsMemory = true; 154 } 155 } 156 } 157 continue; 158 } 159 // The call could access any memory. If that includes writes, give up. 160 if (MRB & AliasAnalysis::Mod) 161 return false; 162 // If it reads, note it. 163 if (MRB & AliasAnalysis::Ref) 164 ReadsMemory = true; 165 continue; 166 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 167 // Ignore non-volatile loads from local memory. (Atomic is okay here.) 168 if (!LI->isVolatile()) { 169 AliasAnalysis::Location Loc = AA->getLocation(LI); 170 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 171 continue; 172 } 173 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 174 // Ignore non-volatile stores to local memory. (Atomic is okay here.) 175 if (!SI->isVolatile()) { 176 AliasAnalysis::Location Loc = AA->getLocation(SI); 177 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 178 continue; 179 } 180 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { 181 // Ignore vaargs on local memory. 182 AliasAnalysis::Location Loc = AA->getLocation(VI); 183 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 184 continue; 185 } 186 187 // Any remaining instructions need to be taken seriously! Check if they 188 // read or write memory. 189 if (I->mayWriteToMemory()) 190 // Writes memory. Just give up. 191 return false; 192 193 // If this instruction may read memory, remember that. 194 ReadsMemory |= I->mayReadFromMemory(); 195 } 196 } 197 198 // Success! Functions in this SCC do not access memory, or only read memory. 199 // Give them the appropriate attribute. 200 bool MadeChange = false; 201 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 202 Function *F = (*I)->getFunction(); 203 204 if (F->doesNotAccessMemory()) 205 // Already perfect! 206 continue; 207 208 if (F->onlyReadsMemory() && ReadsMemory) 209 // No change. 210 continue; 211 212 MadeChange = true; 213 214 // Clear out any existing attributes. 215 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); 216 217 // Add in the new attribute. 218 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); 219 220 if (ReadsMemory) 221 ++NumReadOnly; 222 else 223 ++NumReadNone; 224 } 225 226 return MadeChange; 227 } 228 229 namespace { 230 // For a given pointer Argument, this retains a list of Arguments of functions 231 // in the same SCC that the pointer data flows into. We use this to build an 232 // SCC of the arguments. 233 struct ArgumentGraphNode { 234 Argument *Definition; 235 SmallVector<ArgumentGraphNode*, 4> Uses; 236 }; 237 238 class ArgumentGraph { 239 // We store pointers to ArgumentGraphNode objects, so it's important that 240 // that they not move around upon insert. 241 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy; 242 243 ArgumentMapTy ArgumentMap; 244 245 // There is no root node for the argument graph, in fact: 246 // void f(int *x, int *y) { if (...) f(x, y); } 247 // is an example where the graph is disconnected. The SCCIterator requires a 248 // single entry point, so we maintain a fake ("synthetic") root node that 249 // uses every node. Because the graph is directed and nothing points into 250 // the root, it will not participate in any SCCs (except for its own). 251 ArgumentGraphNode SyntheticRoot; 252 253 public: 254 ArgumentGraph() { SyntheticRoot.Definition = 0; } 255 256 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator; 257 258 iterator begin() { return SyntheticRoot.Uses.begin(); } 259 iterator end() { return SyntheticRoot.Uses.end(); } 260 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } 261 262 ArgumentGraphNode *operator[](Argument *A) { 263 ArgumentGraphNode &Node = ArgumentMap[A]; 264 Node.Definition = A; 265 SyntheticRoot.Uses.push_back(&Node); 266 return &Node; 267 } 268 }; 269 270 // This tracker checks whether callees are in the SCC, and if so it does not 271 // consider that a capture, instead adding it to the "Uses" list and 272 // continuing with the analysis. 273 struct ArgumentUsesTracker : public CaptureTracker { 274 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes) 275 : Captured(false), SCCNodes(SCCNodes) {} 276 277 void tooManyUses() { Captured = true; } 278 279 bool shouldExplore(Use *U) { return true; } 280 281 bool captured(Use *U) { 282 CallSite CS(U->getUser()); 283 if (!CS.getInstruction()) { Captured = true; return true; } 284 285 Function *F = CS.getCalledFunction(); 286 if (!F || !SCCNodes.count(F)) { Captured = true; return true; } 287 288 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 289 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); 290 PI != PE; ++PI, ++AI) { 291 if (AI == AE) { 292 assert(F->isVarArg() && "More params than args in non-varargs call"); 293 Captured = true; 294 return true; 295 } 296 if (PI == U) { 297 Uses.push_back(AI); 298 break; 299 } 300 } 301 assert(!Uses.empty() && "Capturing call-site captured nothing?"); 302 return false; 303 } 304 305 bool Captured; // True only if certainly captured (used outside our SCC). 306 SmallVector<Argument*, 4> Uses; // Uses within our SCC. 307 308 const SmallPtrSet<Function*, 8> &SCCNodes; 309 }; 310 } 311 312 namespace llvm { 313 template<> struct GraphTraits<ArgumentGraphNode*> { 314 typedef ArgumentGraphNode NodeType; 315 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType; 316 317 static inline NodeType *getEntryNode(NodeType *A) { return A; } 318 static inline ChildIteratorType child_begin(NodeType *N) { 319 return N->Uses.begin(); 320 } 321 static inline ChildIteratorType child_end(NodeType *N) { 322 return N->Uses.end(); 323 } 324 }; 325 template<> struct GraphTraits<ArgumentGraph*> 326 : public GraphTraits<ArgumentGraphNode*> { 327 static NodeType *getEntryNode(ArgumentGraph *AG) { 328 return AG->getEntryNode(); 329 } 330 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { 331 return AG->begin(); 332 } 333 static ChildIteratorType nodes_end(ArgumentGraph *AG) { 334 return AG->end(); 335 } 336 }; 337 } 338 339 /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. 340 bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) { 341 bool Changed = false; 342 343 SmallPtrSet<Function*, 8> SCCNodes; 344 345 // Fill SCCNodes with the elements of the SCC. Used for quickly 346 // looking up whether a given CallGraphNode is in this SCC. 347 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 348 Function *F = (*I)->getFunction(); 349 if (F && !F->isDeclaration() && !F->mayBeOverridden()) 350 SCCNodes.insert(F); 351 } 352 353 ArgumentGraph AG; 354 355 // Check each function in turn, determining which pointer arguments are not 356 // captured. 357 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 358 Function *F = (*I)->getFunction(); 359 360 if (F == 0) 361 // External node - only a problem for arguments that we pass to it. 362 continue; 363 364 // Definitions with weak linkage may be overridden at linktime with 365 // something that captures pointers, so treat them like declarations. 366 if (F->isDeclaration() || F->mayBeOverridden()) 367 continue; 368 369 // Functions that are readonly (or readnone) and nounwind and don't return 370 // a value can't capture arguments. Don't analyze them. 371 if (F->onlyReadsMemory() && F->doesNotThrow() && 372 F->getReturnType()->isVoidTy()) { 373 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); 374 A != E; ++A) { 375 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 376 A->addAttr(Attribute::NoCapture); 377 ++NumNoCapture; 378 Changed = true; 379 } 380 } 381 continue; 382 } 383 384 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A) 385 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 386 ArgumentUsesTracker Tracker(SCCNodes); 387 PointerMayBeCaptured(A, &Tracker); 388 if (!Tracker.Captured) { 389 if (Tracker.Uses.empty()) { 390 // If it's trivially not captured, mark it nocapture now. 391 A->addAttr(Attribute::NoCapture); 392 ++NumNoCapture; 393 Changed = true; 394 } else { 395 // If it's not trivially captured and not trivially not captured, 396 // then it must be calling into another function in our SCC. Save 397 // its particulars for Argument-SCC analysis later. 398 ArgumentGraphNode *Node = AG[A]; 399 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(), 400 UE = Tracker.Uses.end(); UI != UE; ++UI) 401 Node->Uses.push_back(AG[*UI]); 402 } 403 } 404 // Otherwise, it's captured. Don't bother doing SCC analysis on it. 405 } 406 } 407 408 // The graph we've collected is partial because we stopped scanning for 409 // argument uses once we solved the argument trivially. These partial nodes 410 // show up as ArgumentGraphNode objects with an empty Uses list, and for 411 // these nodes the final decision about whether they capture has already been 412 // made. If the definition doesn't have a 'nocapture' attribute by now, it 413 // captures. 414 415 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG), E = scc_end(&AG); 416 I != E; ++I) { 417 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I; 418 if (ArgumentSCC.size() == 1) { 419 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node 420 421 // eg. "void f(int* x) { if (...) f(x); }" 422 if (ArgumentSCC[0]->Uses.size() == 1 && 423 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { 424 ArgumentSCC[0]->Definition->addAttr(Attribute::NoCapture); 425 ++NumNoCapture; 426 Changed = true; 427 } 428 continue; 429 } 430 431 bool SCCCaptured = false; 432 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), 433 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) { 434 ArgumentGraphNode *Node = *I; 435 if (Node->Uses.empty()) { 436 if (!Node->Definition->hasNoCaptureAttr()) 437 SCCCaptured = true; 438 } 439 } 440 if (SCCCaptured) continue; 441 442 SmallPtrSet<Argument*, 8> ArgumentSCCNodes; 443 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for 444 // quickly looking up whether a given Argument is in this ArgumentSCC. 445 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), 446 E = ArgumentSCC.end(); I != E; ++I) { 447 ArgumentSCCNodes.insert((*I)->Definition); 448 } 449 450 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), 451 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) { 452 ArgumentGraphNode *N = *I; 453 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(), 454 UE = N->Uses.end(); UI != UE; ++UI) { 455 Argument *A = (*UI)->Definition; 456 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) 457 continue; 458 SCCCaptured = true; 459 break; 460 } 461 } 462 if (SCCCaptured) continue; 463 464 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 465 Argument *A = ArgumentSCC[i]->Definition; 466 A->addAttr(Attribute::NoCapture); 467 ++NumNoCapture; 468 Changed = true; 469 } 470 } 471 472 return Changed; 473 } 474 475 /// IsFunctionMallocLike - A function is malloc-like if it returns either null 476 /// or a pointer that doesn't alias any other pointer visible to the caller. 477 bool FunctionAttrs::IsFunctionMallocLike(Function *F, 478 SmallPtrSet<Function*, 8> &SCCNodes) const { 479 UniqueVector<Value *> FlowsToReturn; 480 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) 481 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) 482 FlowsToReturn.insert(Ret->getReturnValue()); 483 484 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 485 Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved. 486 487 if (Constant *C = dyn_cast<Constant>(RetVal)) { 488 if (!C->isNullValue() && !isa<UndefValue>(C)) 489 return false; 490 491 continue; 492 } 493 494 if (isa<Argument>(RetVal)) 495 return false; 496 497 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) 498 switch (RVI->getOpcode()) { 499 // Extend the analysis by looking upwards. 500 case Instruction::BitCast: 501 case Instruction::GetElementPtr: 502 FlowsToReturn.insert(RVI->getOperand(0)); 503 continue; 504 case Instruction::Select: { 505 SelectInst *SI = cast<SelectInst>(RVI); 506 FlowsToReturn.insert(SI->getTrueValue()); 507 FlowsToReturn.insert(SI->getFalseValue()); 508 continue; 509 } 510 case Instruction::PHI: { 511 PHINode *PN = cast<PHINode>(RVI); 512 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 513 FlowsToReturn.insert(PN->getIncomingValue(i)); 514 continue; 515 } 516 517 // Check whether the pointer came from an allocation. 518 case Instruction::Alloca: 519 break; 520 case Instruction::Call: 521 case Instruction::Invoke: { 522 CallSite CS(RVI); 523 if (CS.paramHasAttr(0, Attribute::NoAlias)) 524 break; 525 if (CS.getCalledFunction() && 526 SCCNodes.count(CS.getCalledFunction())) 527 break; 528 } // fall-through 529 default: 530 return false; // Did not come from an allocation. 531 } 532 533 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) 534 return false; 535 } 536 537 return true; 538 } 539 540 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. 541 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { 542 SmallPtrSet<Function*, 8> SCCNodes; 543 544 // Fill SCCNodes with the elements of the SCC. Used for quickly 545 // looking up whether a given CallGraphNode is in this SCC. 546 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 547 SCCNodes.insert((*I)->getFunction()); 548 549 // Check each function in turn, determining which functions return noalias 550 // pointers. 551 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 552 Function *F = (*I)->getFunction(); 553 554 if (F == 0) 555 // External node - skip it; 556 return false; 557 558 // Already noalias. 559 if (F->doesNotAlias(0)) 560 continue; 561 562 // Definitions with weak linkage may be overridden at linktime, so 563 // treat them like declarations. 564 if (F->isDeclaration() || F->mayBeOverridden()) 565 return false; 566 567 // We annotate noalias return values, which are only applicable to 568 // pointer types. 569 if (!F->getReturnType()->isPointerTy()) 570 continue; 571 572 if (!IsFunctionMallocLike(F, SCCNodes)) 573 return false; 574 } 575 576 bool MadeChange = false; 577 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 578 Function *F = (*I)->getFunction(); 579 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) 580 continue; 581 582 F->setDoesNotAlias(0); 583 ++NumNoAlias; 584 MadeChange = true; 585 } 586 587 return MadeChange; 588 } 589 590 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { 591 AA = &getAnalysis<AliasAnalysis>(); 592 593 bool Changed = AddReadAttrs(SCC); 594 Changed |= AddNoCaptureAttrs(SCC); 595 Changed |= AddNoAliasAttrs(SCC); 596 return Changed; 597 } 598