FunctionAttrs.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
  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. It does the
  13. // same with function arguments independently, marking them readonly/
  14. // readnone/nocapture. Finally, well-known library call declarations
  15. // are marked with all attributes that are consistent with the
  16. // function's standard definition. This pass is implemented as a
  17. // bottom-up traversal of the call-graph.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/Transforms/IPO.h"
  21. #include "llvm/ADT/SCCIterator.h"
  22. #include "llvm/ADT/SetVector.h"
  23. #include "llvm/ADT/SmallSet.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/Analysis/CallGraph.h"
  27. #include "llvm/Analysis/CallGraphSCCPass.h"
  28. #include "llvm/Analysis/CaptureTracking.h"
  29. #include "llvm/IR/GlobalVariable.h"
  30. #include "llvm/IR/InstIterator.h"
  31. #include "llvm/IR/IntrinsicInst.h"
  32. #include "llvm/IR/LLVMContext.h"
  33. #include "llvm/Analysis/TargetLibraryInfo.h"
  34. using namespace llvm;
  35. #define DEBUG_TYPE "functionattrs"
  36. STATISTIC(NumReadNone, "Number of functions marked readnone");
  37. STATISTIC(NumReadOnly, "Number of functions marked readonly");
  38. STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
  39. STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
  40. STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
  41. STATISTIC(NumNoAlias, "Number of function returns marked noalias");
  42. STATISTIC(NumAnnotated, "Number of attributes added to library functions");
  43. namespace {
  44. struct FunctionAttrs : public CallGraphSCCPass {
  45. static char ID; // Pass identification, replacement for typeid
  46. FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
  47. initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
  48. }
  49. // runOnSCC - Analyze the SCC, performing the transformation if possible.
  50. bool runOnSCC(CallGraphSCC &SCC) override;
  51. // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
  52. bool AddReadAttrs(const CallGraphSCC &SCC);
  53. // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
  54. bool AddArgumentAttrs(const CallGraphSCC &SCC);
  55. // IsFunctionMallocLike - Does this function allocate new memory?
  56. bool IsFunctionMallocLike(Function *F,
  57. SmallPtrSet<Function*, 8> &) const;
  58. // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
  59. bool AddNoAliasAttrs(const CallGraphSCC &SCC);
  60. // Utility methods used by inferPrototypeAttributes to add attributes
  61. // and maintain annotation statistics.
  62. void setDoesNotAccessMemory(Function &F) {
  63. if (!F.doesNotAccessMemory()) {
  64. F.setDoesNotAccessMemory();
  65. ++NumAnnotated;
  66. }
  67. }
  68. void setOnlyReadsMemory(Function &F) {
  69. if (!F.onlyReadsMemory()) {
  70. F.setOnlyReadsMemory();
  71. ++NumAnnotated;
  72. }
  73. }
  74. void setDoesNotThrow(Function &F) {
  75. if (!F.doesNotThrow()) {
  76. F.setDoesNotThrow();
  77. ++NumAnnotated;
  78. }
  79. }
  80. void setDoesNotCapture(Function &F, unsigned n) {
  81. if (!F.doesNotCapture(n)) {
  82. F.setDoesNotCapture(n);
  83. ++NumAnnotated;
  84. }
  85. }
  86. void setOnlyReadsMemory(Function &F, unsigned n) {
  87. if (!F.onlyReadsMemory(n)) {
  88. F.setOnlyReadsMemory(n);
  89. ++NumAnnotated;
  90. }
  91. }
  92. void setDoesNotAlias(Function &F, unsigned n) {
  93. if (!F.doesNotAlias(n)) {
  94. F.setDoesNotAlias(n);
  95. ++NumAnnotated;
  96. }
  97. }
  98. // inferPrototypeAttributes - Analyze the name and prototype of the
  99. // given function and set any applicable attributes. Returns true
  100. // if any attributes were set and false otherwise.
  101. bool inferPrototypeAttributes(Function &F);
  102. // annotateLibraryCalls - Adds attributes to well-known standard library
  103. // call declarations.
  104. bool annotateLibraryCalls(const CallGraphSCC &SCC);
  105. void getAnalysisUsage(AnalysisUsage &AU) const override {
  106. AU.setPreservesCFG();
  107. AU.addRequired<AliasAnalysis>();
  108. AU.addRequired<TargetLibraryInfoWrapperPass>();
  109. CallGraphSCCPass::getAnalysisUsage(AU);
  110. }
  111. private:
  112. AliasAnalysis *AA;
  113. TargetLibraryInfo *TLI;
  114. };
  115. }
  116. char FunctionAttrs::ID = 0;
  117. INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
  118. "Deduce function attributes", false, false)
  119. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  120. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  121. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  122. INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
  123. "Deduce function attributes", false, false)
  124. Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
  125. /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
  126. bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
  127. SmallPtrSet<Function*, 8> SCCNodes;
  128. // Fill SCCNodes with the elements of the SCC. Used for quickly
  129. // looking up whether a given CallGraphNode is in this SCC.
  130. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
  131. SCCNodes.insert((*I)->getFunction());
  132. // Check if any of the functions in the SCC read or write memory. If they
  133. // write memory then they can't be marked readnone or readonly.
  134. bool ReadsMemory = false;
  135. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  136. Function *F = (*I)->getFunction();
  137. if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
  138. // External node or node we don't want to optimize - assume it may write
  139. // memory and give up.
  140. return false;
  141. AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
  142. if (MRB == AliasAnalysis::DoesNotAccessMemory)
  143. // Already perfect!
  144. continue;
  145. // Definitions with weak linkage may be overridden at linktime with
  146. // something that writes memory, so treat them like declarations.
  147. if (F->isDeclaration() || F->mayBeOverridden()) {
  148. if (!AliasAnalysis::onlyReadsMemory(MRB))
  149. // May write memory. Just give up.
  150. return false;
  151. ReadsMemory = true;
  152. continue;
  153. }
  154. // Scan the function body for instructions that may read or write memory.
  155. for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
  156. Instruction *I = &*II;
  157. // Some instructions can be ignored even if they read or write memory.
  158. // Detect these now, skipping to the next instruction if one is found.
  159. CallSite CS(cast<Value>(I));
  160. if (CS) {
  161. // Ignore calls to functions in the same SCC.
  162. if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
  163. continue;
  164. AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
  165. // If the call doesn't access arbitrary memory, we may be able to
  166. // figure out something.
  167. if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
  168. // If the call does access argument pointees, check each argument.
  169. if (AliasAnalysis::doesAccessArgPointees(MRB))
  170. // Check whether all pointer arguments point to local memory, and
  171. // ignore calls that only access local memory.
  172. for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
  173. CI != CE; ++CI) {
  174. Value *Arg = *CI;
  175. if (Arg->getType()->isPointerTy()) {
  176. AAMDNodes AAInfo;
  177. I->getAAMetadata(AAInfo);
  178. MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
  179. if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
  180. if (MRB & AliasAnalysis::Mod)
  181. // Writes non-local memory. Give up.
  182. return false;
  183. if (MRB & AliasAnalysis::Ref)
  184. // Ok, it reads non-local memory.
  185. ReadsMemory = true;
  186. }
  187. }
  188. }
  189. continue;
  190. }
  191. // The call could access any memory. If that includes writes, give up.
  192. if (MRB & AliasAnalysis::Mod)
  193. return false;
  194. // If it reads, note it.
  195. if (MRB & AliasAnalysis::Ref)
  196. ReadsMemory = true;
  197. continue;
  198. } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  199. // Ignore non-volatile loads from local memory. (Atomic is okay here.)
  200. if (!LI->isVolatile()) {
  201. MemoryLocation Loc = MemoryLocation::get(LI);
  202. if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
  203. continue;
  204. }
  205. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  206. // Ignore non-volatile stores to local memory. (Atomic is okay here.)
  207. if (!SI->isVolatile()) {
  208. MemoryLocation Loc = MemoryLocation::get(SI);
  209. if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
  210. continue;
  211. }
  212. } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
  213. // Ignore vaargs on local memory.
  214. MemoryLocation Loc = MemoryLocation::get(VI);
  215. if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
  216. continue;
  217. }
  218. // Any remaining instructions need to be taken seriously! Check if they
  219. // read or write memory.
  220. if (I->mayWriteToMemory())
  221. // Writes memory. Just give up.
  222. return false;
  223. // If this instruction may read memory, remember that.
  224. ReadsMemory |= I->mayReadFromMemory();
  225. }
  226. }
  227. // Success! Functions in this SCC do not access memory, or only read memory.
  228. // Give them the appropriate attribute.
  229. bool MadeChange = false;
  230. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  231. Function *F = (*I)->getFunction();
  232. if (F->doesNotAccessMemory())
  233. // Already perfect!
  234. continue;
  235. if (F->onlyReadsMemory() && ReadsMemory)
  236. // No change.
  237. continue;
  238. MadeChange = true;
  239. // Clear out any existing attributes.
  240. AttrBuilder B;
  241. B.addAttribute(Attribute::ReadOnly)
  242. .addAttribute(Attribute::ReadNone);
  243. F->removeAttributes(AttributeSet::FunctionIndex,
  244. AttributeSet::get(F->getContext(),
  245. AttributeSet::FunctionIndex, B));
  246. // Add in the new attribute.
  247. F->addAttribute(AttributeSet::FunctionIndex,
  248. ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
  249. if (ReadsMemory)
  250. ++NumReadOnly;
  251. else
  252. ++NumReadNone;
  253. }
  254. return MadeChange;
  255. }
  256. namespace {
  257. // For a given pointer Argument, this retains a list of Arguments of functions
  258. // in the same SCC that the pointer data flows into. We use this to build an
  259. // SCC of the arguments.
  260. struct ArgumentGraphNode {
  261. Argument *Definition;
  262. SmallVector<ArgumentGraphNode*, 4> Uses;
  263. };
  264. class ArgumentGraph {
  265. // We store pointers to ArgumentGraphNode objects, so it's important that
  266. // that they not move around upon insert.
  267. typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
  268. ArgumentMapTy ArgumentMap;
  269. // There is no root node for the argument graph, in fact:
  270. // void f(int *x, int *y) { if (...) f(x, y); }
  271. // is an example where the graph is disconnected. The SCCIterator requires a
  272. // single entry point, so we maintain a fake ("synthetic") root node that
  273. // uses every node. Because the graph is directed and nothing points into
  274. // the root, it will not participate in any SCCs (except for its own).
  275. ArgumentGraphNode SyntheticRoot;
  276. public:
  277. ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
  278. typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
  279. iterator begin() { return SyntheticRoot.Uses.begin(); }
  280. iterator end() { return SyntheticRoot.Uses.end(); }
  281. ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
  282. ArgumentGraphNode *operator[](Argument *A) {
  283. ArgumentGraphNode &Node = ArgumentMap[A];
  284. Node.Definition = A;
  285. SyntheticRoot.Uses.push_back(&Node);
  286. return &Node;
  287. }
  288. };
  289. // This tracker checks whether callees are in the SCC, and if so it does not
  290. // consider that a capture, instead adding it to the "Uses" list and
  291. // continuing with the analysis.
  292. struct ArgumentUsesTracker : public CaptureTracker {
  293. ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
  294. : Captured(false), SCCNodes(SCCNodes) {}
  295. void tooManyUses() override { Captured = true; }
  296. bool captured(const Use *U) override {
  297. CallSite CS(U->getUser());
  298. if (!CS.getInstruction()) { Captured = true; return true; }
  299. Function *F = CS.getCalledFunction();
  300. if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
  301. bool Found = false;
  302. Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  303. for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
  304. PI != PE; ++PI, ++AI) {
  305. if (AI == AE) {
  306. assert(F->isVarArg() && "More params than args in non-varargs call");
  307. Captured = true;
  308. return true;
  309. }
  310. if (PI == U) {
  311. Uses.push_back(AI);
  312. Found = true;
  313. break;
  314. }
  315. }
  316. assert(Found && "Capturing call-site captured nothing?");
  317. (void)Found;
  318. return false;
  319. }
  320. bool Captured; // True only if certainly captured (used outside our SCC).
  321. SmallVector<Argument*, 4> Uses; // Uses within our SCC.
  322. const SmallPtrSet<Function*, 8> &SCCNodes;
  323. };
  324. }
  325. namespace llvm {
  326. template<> struct GraphTraits<ArgumentGraphNode*> {
  327. typedef ArgumentGraphNode NodeType;
  328. typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
  329. static inline NodeType *getEntryNode(NodeType *A) { return A; }
  330. static inline ChildIteratorType child_begin(NodeType *N) {
  331. return N->Uses.begin();
  332. }
  333. static inline ChildIteratorType child_end(NodeType *N) {
  334. return N->Uses.end();
  335. }
  336. };
  337. template<> struct GraphTraits<ArgumentGraph*>
  338. : public GraphTraits<ArgumentGraphNode*> {
  339. static NodeType *getEntryNode(ArgumentGraph *AG) {
  340. return AG->getEntryNode();
  341. }
  342. static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
  343. return AG->begin();
  344. }
  345. static ChildIteratorType nodes_end(ArgumentGraph *AG) {
  346. return AG->end();
  347. }
  348. };
  349. }
  350. // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
  351. static Attribute::AttrKind
  352. determinePointerReadAttrs(Argument *A,
  353. const SmallPtrSet<Argument*, 8> &SCCNodes) {
  354. SmallVector<Use*, 32> Worklist;
  355. SmallSet<Use*, 32> Visited;
  356. int Count = 0;
  357. // inalloca arguments are always clobbered by the call.
  358. if (A->hasInAllocaAttr())
  359. return Attribute::None;
  360. bool IsRead = false;
  361. // We don't need to track IsWritten. If A is written to, return immediately.
  362. for (Use &U : A->uses()) {
  363. if (Count++ >= 20)
  364. return Attribute::None;
  365. Visited.insert(&U);
  366. Worklist.push_back(&U);
  367. }
  368. while (!Worklist.empty()) {
  369. Use *U = Worklist.pop_back_val();
  370. Instruction *I = cast<Instruction>(U->getUser());
  371. Value *V = U->get();
  372. switch (I->getOpcode()) {
  373. case Instruction::BitCast:
  374. case Instruction::GetElementPtr:
  375. case Instruction::PHI:
  376. case Instruction::Select:
  377. case Instruction::AddrSpaceCast:
  378. // The original value is not read/written via this if the new value isn't.
  379. for (Use &UU : I->uses())
  380. if (Visited.insert(&UU).second)
  381. Worklist.push_back(&UU);
  382. break;
  383. case Instruction::Call:
  384. case Instruction::Invoke: {
  385. bool Captures = true;
  386. if (I->getType()->isVoidTy())
  387. Captures = false;
  388. auto AddUsersToWorklistIfCapturing = [&] {
  389. if (Captures)
  390. for (Use &UU : I->uses())
  391. if (Visited.insert(&UU).second)
  392. Worklist.push_back(&UU);
  393. };
  394. CallSite CS(I);
  395. if (CS.doesNotAccessMemory()) {
  396. AddUsersToWorklistIfCapturing();
  397. continue;
  398. }
  399. Function *F = CS.getCalledFunction();
  400. if (!F) {
  401. if (CS.onlyReadsMemory()) {
  402. IsRead = true;
  403. AddUsersToWorklistIfCapturing();
  404. continue;
  405. }
  406. return Attribute::None;
  407. }
  408. Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  409. CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
  410. for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
  411. if (A->get() == V) {
  412. if (AI == AE) {
  413. assert(F->isVarArg() &&
  414. "More params than args in non-varargs call.");
  415. return Attribute::None;
  416. }
  417. Captures &= !CS.doesNotCapture(A - B);
  418. if (SCCNodes.count(AI))
  419. continue;
  420. if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
  421. return Attribute::None;
  422. if (!CS.doesNotAccessMemory(A - B))
  423. IsRead = true;
  424. }
  425. }
  426. AddUsersToWorklistIfCapturing();
  427. break;
  428. }
  429. case Instruction::Load:
  430. IsRead = true;
  431. break;
  432. case Instruction::ICmp:
  433. case Instruction::Ret:
  434. break;
  435. default:
  436. return Attribute::None;
  437. }
  438. }
  439. return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
  440. }
  441. /// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
  442. bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
  443. bool Changed = false;
  444. SmallPtrSet<Function*, 8> SCCNodes;
  445. // Fill SCCNodes with the elements of the SCC. Used for quickly
  446. // looking up whether a given CallGraphNode is in this SCC.
  447. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  448. Function *F = (*I)->getFunction();
  449. if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
  450. !F->hasFnAttribute(Attribute::OptimizeNone))
  451. SCCNodes.insert(F);
  452. }
  453. ArgumentGraph AG;
  454. AttrBuilder B;
  455. B.addAttribute(Attribute::NoCapture);
  456. // Check each function in turn, determining which pointer arguments are not
  457. // captured.
  458. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  459. Function *F = (*I)->getFunction();
  460. if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
  461. // External node or function we're trying not to optimize - only a problem
  462. // for arguments that we pass to it.
  463. continue;
  464. // Definitions with weak linkage may be overridden at linktime with
  465. // something that captures pointers, so treat them like declarations.
  466. if (F->isDeclaration() || F->mayBeOverridden())
  467. continue;
  468. // Functions that are readonly (or readnone) and nounwind and don't return
  469. // a value can't capture arguments. Don't analyze them.
  470. if (F->onlyReadsMemory() && F->doesNotThrow() &&
  471. F->getReturnType()->isVoidTy()) {
  472. for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
  473. A != E; ++A) {
  474. if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
  475. A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
  476. ++NumNoCapture;
  477. Changed = true;
  478. }
  479. }
  480. continue;
  481. }
  482. for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
  483. A != E; ++A) {
  484. if (!A->getType()->isPointerTy()) continue;
  485. bool HasNonLocalUses = false;
  486. if (!A->hasNoCaptureAttr()) {
  487. ArgumentUsesTracker Tracker(SCCNodes);
  488. PointerMayBeCaptured(A, &Tracker);
  489. if (!Tracker.Captured) {
  490. if (Tracker.Uses.empty()) {
  491. // If it's trivially not captured, mark it nocapture now.
  492. A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
  493. ++NumNoCapture;
  494. Changed = true;
  495. } else {
  496. // If it's not trivially captured and not trivially not captured,
  497. // then it must be calling into another function in our SCC. Save
  498. // its particulars for Argument-SCC analysis later.
  499. ArgumentGraphNode *Node = AG[A];
  500. for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
  501. UE = Tracker.Uses.end(); UI != UE; ++UI) {
  502. Node->Uses.push_back(AG[*UI]);
  503. if (*UI != A)
  504. HasNonLocalUses = true;
  505. }
  506. }
  507. }
  508. // Otherwise, it's captured. Don't bother doing SCC analysis on it.
  509. }
  510. if (!HasNonLocalUses && !A->onlyReadsMemory()) {
  511. // Can we determine that it's readonly/readnone without doing an SCC?
  512. // Note that we don't allow any calls at all here, or else our result
  513. // will be dependent on the iteration order through the functions in the
  514. // SCC.
  515. SmallPtrSet<Argument*, 8> Self;
  516. Self.insert(A);
  517. Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
  518. if (R != Attribute::None) {
  519. AttrBuilder B;
  520. B.addAttribute(R);
  521. A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
  522. Changed = true;
  523. R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
  524. }
  525. }
  526. }
  527. }
  528. // The graph we've collected is partial because we stopped scanning for
  529. // argument uses once we solved the argument trivially. These partial nodes
  530. // show up as ArgumentGraphNode objects with an empty Uses list, and for
  531. // these nodes the final decision about whether they capture has already been
  532. // made. If the definition doesn't have a 'nocapture' attribute by now, it
  533. // captures.
  534. for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
  535. const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
  536. if (ArgumentSCC.size() == 1) {
  537. if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
  538. // eg. "void f(int* x) { if (...) f(x); }"
  539. if (ArgumentSCC[0]->Uses.size() == 1 &&
  540. ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
  541. Argument *A = ArgumentSCC[0]->Definition;
  542. A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
  543. ++NumNoCapture;
  544. Changed = true;
  545. }
  546. continue;
  547. }
  548. bool SCCCaptured = false;
  549. for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
  550. I != E && !SCCCaptured; ++I) {
  551. ArgumentGraphNode *Node = *I;
  552. if (Node->Uses.empty()) {
  553. if (!Node->Definition->hasNoCaptureAttr())
  554. SCCCaptured = true;
  555. }
  556. }
  557. if (SCCCaptured) continue;
  558. SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
  559. // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
  560. // quickly looking up whether a given Argument is in this ArgumentSCC.
  561. for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
  562. ArgumentSCCNodes.insert((*I)->Definition);
  563. }
  564. for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
  565. I != E && !SCCCaptured; ++I) {
  566. ArgumentGraphNode *N = *I;
  567. for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
  568. UE = N->Uses.end(); UI != UE; ++UI) {
  569. Argument *A = (*UI)->Definition;
  570. if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
  571. continue;
  572. SCCCaptured = true;
  573. break;
  574. }
  575. }
  576. if (SCCCaptured) continue;
  577. for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
  578. Argument *A = ArgumentSCC[i]->Definition;
  579. A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
  580. ++NumNoCapture;
  581. Changed = true;
  582. }
  583. // We also want to compute readonly/readnone. With a small number of false
  584. // negatives, we can assume that any pointer which is captured isn't going
  585. // to be provably readonly or readnone, since by definition we can't
  586. // analyze all uses of a captured pointer.
  587. //
  588. // The false negatives happen when the pointer is captured by a function
  589. // that promises readonly/readnone behaviour on the pointer, then the
  590. // pointer's lifetime ends before anything that writes to arbitrary memory.
  591. // Also, a readonly/readnone pointer may be returned, but returning a
  592. // pointer is capturing it.
  593. Attribute::AttrKind ReadAttr = Attribute::ReadNone;
  594. for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
  595. Argument *A = ArgumentSCC[i]->Definition;
  596. Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
  597. if (K == Attribute::ReadNone)
  598. continue;
  599. if (K == Attribute::ReadOnly) {
  600. ReadAttr = Attribute::ReadOnly;
  601. continue;
  602. }
  603. ReadAttr = K;
  604. break;
  605. }
  606. if (ReadAttr != Attribute::None) {
  607. AttrBuilder B, R;
  608. B.addAttribute(ReadAttr);
  609. R.addAttribute(Attribute::ReadOnly)
  610. .addAttribute(Attribute::ReadNone);
  611. for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
  612. Argument *A = ArgumentSCC[i]->Definition;
  613. // Clear out existing readonly/readnone attributes
  614. A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
  615. A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
  616. ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
  617. Changed = true;
  618. }
  619. }
  620. }
  621. return Changed;
  622. }
  623. /// IsFunctionMallocLike - A function is malloc-like if it returns either null
  624. /// or a pointer that doesn't alias any other pointer visible to the caller.
  625. bool FunctionAttrs::IsFunctionMallocLike(Function *F,
  626. SmallPtrSet<Function*, 8> &SCCNodes) const {
  627. SmallSetVector<Value *, 8> FlowsToReturn;
  628. for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
  629. if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
  630. FlowsToReturn.insert(Ret->getReturnValue());
  631. for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
  632. Value *RetVal = FlowsToReturn[i];
  633. if (Constant *C = dyn_cast<Constant>(RetVal)) {
  634. if (!C->isNullValue() && !isa<UndefValue>(C))
  635. return false;
  636. continue;
  637. }
  638. if (isa<Argument>(RetVal))
  639. return false;
  640. if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
  641. switch (RVI->getOpcode()) {
  642. // Extend the analysis by looking upwards.
  643. case Instruction::BitCast:
  644. case Instruction::GetElementPtr:
  645. case Instruction::AddrSpaceCast:
  646. FlowsToReturn.insert(RVI->getOperand(0));
  647. continue;
  648. case Instruction::Select: {
  649. SelectInst *SI = cast<SelectInst>(RVI);
  650. FlowsToReturn.insert(SI->getTrueValue());
  651. FlowsToReturn.insert(SI->getFalseValue());
  652. continue;
  653. }
  654. case Instruction::PHI: {
  655. PHINode *PN = cast<PHINode>(RVI);
  656. for (Value *IncValue : PN->incoming_values())
  657. FlowsToReturn.insert(IncValue);
  658. continue;
  659. }
  660. // Check whether the pointer came from an allocation.
  661. case Instruction::Alloca:
  662. break;
  663. case Instruction::Call:
  664. case Instruction::Invoke: {
  665. CallSite CS(RVI);
  666. if (CS.paramHasAttr(0, Attribute::NoAlias))
  667. break;
  668. if (CS.getCalledFunction() &&
  669. SCCNodes.count(CS.getCalledFunction()))
  670. break;
  671. } // fall-through
  672. default:
  673. return false; // Did not come from an allocation.
  674. }
  675. if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
  676. return false;
  677. }
  678. return true;
  679. }
  680. /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
  681. bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
  682. SmallPtrSet<Function*, 8> SCCNodes;
  683. // Fill SCCNodes with the elements of the SCC. Used for quickly
  684. // looking up whether a given CallGraphNode is in this SCC.
  685. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
  686. SCCNodes.insert((*I)->getFunction());
  687. // Check each function in turn, determining which functions return noalias
  688. // pointers.
  689. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  690. Function *F = (*I)->getFunction();
  691. if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
  692. // External node or node we don't want to optimize - skip it;
  693. return false;
  694. // Already noalias.
  695. if (F->doesNotAlias(0))
  696. continue;
  697. // Definitions with weak linkage may be overridden at linktime, so
  698. // treat them like declarations.
  699. if (F->isDeclaration() || F->mayBeOverridden())
  700. return false;
  701. // We annotate noalias return values, which are only applicable to
  702. // pointer types.
  703. if (!F->getReturnType()->isPointerTy())
  704. continue;
  705. if (!IsFunctionMallocLike(F, SCCNodes))
  706. return false;
  707. }
  708. bool MadeChange = false;
  709. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  710. Function *F = (*I)->getFunction();
  711. if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
  712. continue;
  713. F->setDoesNotAlias(0);
  714. ++NumNoAlias;
  715. MadeChange = true;
  716. }
  717. return MadeChange;
  718. }
  719. /// inferPrototypeAttributes - Analyze the name and prototype of the
  720. /// given function and set any applicable attributes. Returns true
  721. /// if any attributes were set and false otherwise.
  722. bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
  723. if (F.hasFnAttribute(Attribute::OptimizeNone))
  724. return false;
  725. FunctionType *FTy = F.getFunctionType();
  726. LibFunc::Func TheLibFunc;
  727. if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
  728. return false;
  729. switch (TheLibFunc) {
  730. case LibFunc::strlen:
  731. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  732. return false;
  733. setOnlyReadsMemory(F);
  734. setDoesNotThrow(F);
  735. setDoesNotCapture(F, 1);
  736. break;
  737. case LibFunc::strchr:
  738. case LibFunc::strrchr:
  739. if (FTy->getNumParams() != 2 ||
  740. !FTy->getParamType(0)->isPointerTy() ||
  741. !FTy->getParamType(1)->isIntegerTy())
  742. return false;
  743. setOnlyReadsMemory(F);
  744. setDoesNotThrow(F);
  745. break;
  746. case LibFunc::strtol:
  747. case LibFunc::strtod:
  748. case LibFunc::strtof:
  749. case LibFunc::strtoul:
  750. case LibFunc::strtoll:
  751. case LibFunc::strtold:
  752. case LibFunc::strtoull:
  753. if (FTy->getNumParams() < 2 ||
  754. !FTy->getParamType(1)->isPointerTy())
  755. return false;
  756. setDoesNotThrow(F);
  757. setDoesNotCapture(F, 2);
  758. setOnlyReadsMemory(F, 1);
  759. break;
  760. case LibFunc::strcpy:
  761. case LibFunc::stpcpy:
  762. case LibFunc::strcat:
  763. case LibFunc::strncat:
  764. case LibFunc::strncpy:
  765. case LibFunc::stpncpy:
  766. if (FTy->getNumParams() < 2 ||
  767. !FTy->getParamType(1)->isPointerTy())
  768. return false;
  769. setDoesNotThrow(F);
  770. setDoesNotCapture(F, 2);
  771. setOnlyReadsMemory(F, 2);
  772. break;
  773. case LibFunc::strxfrm:
  774. if (FTy->getNumParams() != 3 ||
  775. !FTy->getParamType(0)->isPointerTy() ||
  776. !FTy->getParamType(1)->isPointerTy())
  777. return false;
  778. setDoesNotThrow(F);
  779. setDoesNotCapture(F, 1);
  780. setDoesNotCapture(F, 2);
  781. setOnlyReadsMemory(F, 2);
  782. break;
  783. case LibFunc::strcmp: //0,1
  784. case LibFunc::strspn: // 0,1
  785. case LibFunc::strncmp: // 0,1
  786. case LibFunc::strcspn: //0,1
  787. case LibFunc::strcoll: //0,1
  788. case LibFunc::strcasecmp: // 0,1
  789. case LibFunc::strncasecmp: //
  790. if (FTy->getNumParams() < 2 ||
  791. !FTy->getParamType(0)->isPointerTy() ||
  792. !FTy->getParamType(1)->isPointerTy())
  793. return false;
  794. setOnlyReadsMemory(F);
  795. setDoesNotThrow(F);
  796. setDoesNotCapture(F, 1);
  797. setDoesNotCapture(F, 2);
  798. break;
  799. case LibFunc::strstr:
  800. case LibFunc::strpbrk:
  801. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  802. return false;
  803. setOnlyReadsMemory(F);
  804. setDoesNotThrow(F);
  805. setDoesNotCapture(F, 2);
  806. break;
  807. case LibFunc::strtok:
  808. case LibFunc::strtok_r:
  809. if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
  810. return false;
  811. setDoesNotThrow(F);
  812. setDoesNotCapture(F, 2);
  813. setOnlyReadsMemory(F, 2);
  814. break;
  815. case LibFunc::scanf:
  816. if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
  817. return false;
  818. setDoesNotThrow(F);
  819. setDoesNotCapture(F, 1);
  820. setOnlyReadsMemory(F, 1);
  821. break;
  822. case LibFunc::setbuf:
  823. case LibFunc::setvbuf:
  824. if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
  825. return false;
  826. setDoesNotThrow(F);
  827. setDoesNotCapture(F, 1);
  828. break;
  829. case LibFunc::strdup:
  830. case LibFunc::strndup:
  831. if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
  832. !FTy->getParamType(0)->isPointerTy())
  833. return false;
  834. setDoesNotThrow(F);
  835. setDoesNotAlias(F, 0);
  836. setDoesNotCapture(F, 1);
  837. setOnlyReadsMemory(F, 1);
  838. break;
  839. case LibFunc::stat:
  840. case LibFunc::statvfs:
  841. if (FTy->getNumParams() < 2 ||
  842. !FTy->getParamType(0)->isPointerTy() ||
  843. !FTy->getParamType(1)->isPointerTy())
  844. return false;
  845. setDoesNotThrow(F);
  846. setDoesNotCapture(F, 1);
  847. setDoesNotCapture(F, 2);
  848. setOnlyReadsMemory(F, 1);
  849. break;
  850. case LibFunc::sscanf:
  851. if (FTy->getNumParams() < 2 ||
  852. !FTy->getParamType(0)->isPointerTy() ||
  853. !FTy->getParamType(1)->isPointerTy())
  854. return false;
  855. setDoesNotThrow(F);
  856. setDoesNotCapture(F, 1);
  857. setDoesNotCapture(F, 2);
  858. setOnlyReadsMemory(F, 1);
  859. setOnlyReadsMemory(F, 2);
  860. break;
  861. case LibFunc::sprintf:
  862. if (FTy->getNumParams() < 2 ||
  863. !FTy->getParamType(0)->isPointerTy() ||
  864. !FTy->getParamType(1)->isPointerTy())
  865. return false;
  866. setDoesNotThrow(F);
  867. setDoesNotCapture(F, 1);
  868. setDoesNotCapture(F, 2);
  869. setOnlyReadsMemory(F, 2);
  870. break;
  871. case LibFunc::snprintf:
  872. if (FTy->getNumParams() != 3 ||
  873. !FTy->getParamType(0)->isPointerTy() ||
  874. !FTy->getParamType(2)->isPointerTy())
  875. return false;
  876. setDoesNotThrow(F);
  877. setDoesNotCapture(F, 1);
  878. setDoesNotCapture(F, 3);
  879. setOnlyReadsMemory(F, 3);
  880. break;
  881. case LibFunc::setitimer:
  882. if (FTy->getNumParams() != 3 ||
  883. !FTy->getParamType(1)->isPointerTy() ||
  884. !FTy->getParamType(2)->isPointerTy())
  885. return false;
  886. setDoesNotThrow(F);
  887. setDoesNotCapture(F, 2);
  888. setDoesNotCapture(F, 3);
  889. setOnlyReadsMemory(F, 2);
  890. break;
  891. case LibFunc::system:
  892. if (FTy->getNumParams() != 1 ||
  893. !FTy->getParamType(0)->isPointerTy())
  894. return false;
  895. // May throw; "system" is a valid pthread cancellation point.
  896. setDoesNotCapture(F, 1);
  897. setOnlyReadsMemory(F, 1);
  898. break;
  899. case LibFunc::malloc:
  900. if (FTy->getNumParams() != 1 ||
  901. !FTy->getReturnType()->isPointerTy())
  902. return false;
  903. setDoesNotThrow(F);
  904. setDoesNotAlias(F, 0);
  905. break;
  906. case LibFunc::memcmp:
  907. if (FTy->getNumParams() != 3 ||
  908. !FTy->getParamType(0)->isPointerTy() ||
  909. !FTy->getParamType(1)->isPointerTy())
  910. return false;
  911. setOnlyReadsMemory(F);
  912. setDoesNotThrow(F);
  913. setDoesNotCapture(F, 1);
  914. setDoesNotCapture(F, 2);
  915. break;
  916. case LibFunc::memchr:
  917. case LibFunc::memrchr:
  918. if (FTy->getNumParams() != 3)
  919. return false;
  920. setOnlyReadsMemory(F);
  921. setDoesNotThrow(F);
  922. break;
  923. case LibFunc::modf:
  924. case LibFunc::modff:
  925. case LibFunc::modfl:
  926. if (FTy->getNumParams() < 2 ||
  927. !FTy->getParamType(1)->isPointerTy())
  928. return false;
  929. setDoesNotThrow(F);
  930. setDoesNotCapture(F, 2);
  931. break;
  932. case LibFunc::memcpy:
  933. case LibFunc::memccpy:
  934. case LibFunc::memmove:
  935. if (FTy->getNumParams() < 2 ||
  936. !FTy->getParamType(1)->isPointerTy())
  937. return false;
  938. setDoesNotThrow(F);
  939. setDoesNotCapture(F, 2);
  940. setOnlyReadsMemory(F, 2);
  941. break;
  942. case LibFunc::memalign:
  943. if (!FTy->getReturnType()->isPointerTy())
  944. return false;
  945. setDoesNotAlias(F, 0);
  946. break;
  947. case LibFunc::mkdir:
  948. if (FTy->getNumParams() == 0 ||
  949. !FTy->getParamType(0)->isPointerTy())
  950. return false;
  951. setDoesNotThrow(F);
  952. setDoesNotCapture(F, 1);
  953. setOnlyReadsMemory(F, 1);
  954. break;
  955. case LibFunc::mktime:
  956. if (FTy->getNumParams() == 0 ||
  957. !FTy->getParamType(0)->isPointerTy())
  958. return false;
  959. setDoesNotThrow(F);
  960. setDoesNotCapture(F, 1);
  961. break;
  962. case LibFunc::realloc:
  963. if (FTy->getNumParams() != 2 ||
  964. !FTy->getParamType(0)->isPointerTy() ||
  965. !FTy->getReturnType()->isPointerTy())
  966. return false;
  967. setDoesNotThrow(F);
  968. setDoesNotAlias(F, 0);
  969. setDoesNotCapture(F, 1);
  970. break;
  971. case LibFunc::read:
  972. if (FTy->getNumParams() != 3 ||
  973. !FTy->getParamType(1)->isPointerTy())
  974. return false;
  975. // May throw; "read" is a valid pthread cancellation point.
  976. setDoesNotCapture(F, 2);
  977. break;
  978. case LibFunc::rewind:
  979. if (FTy->getNumParams() < 1 ||
  980. !FTy->getParamType(0)->isPointerTy())
  981. return false;
  982. setDoesNotThrow(F);
  983. setDoesNotCapture(F, 1);
  984. break;
  985. case LibFunc::rmdir:
  986. case LibFunc::remove:
  987. case LibFunc::realpath:
  988. if (FTy->getNumParams() < 1 ||
  989. !FTy->getParamType(0)->isPointerTy())
  990. return false;
  991. setDoesNotThrow(F);
  992. setDoesNotCapture(F, 1);
  993. setOnlyReadsMemory(F, 1);
  994. break;
  995. case LibFunc::rename:
  996. if (FTy->getNumParams() < 2 ||
  997. !FTy->getParamType(0)->isPointerTy() ||
  998. !FTy->getParamType(1)->isPointerTy())
  999. return false;
  1000. setDoesNotThrow(F);
  1001. setDoesNotCapture(F, 1);
  1002. setDoesNotCapture(F, 2);
  1003. setOnlyReadsMemory(F, 1);
  1004. setOnlyReadsMemory(F, 2);
  1005. break;
  1006. case LibFunc::readlink:
  1007. if (FTy->getNumParams() < 2 ||
  1008. !FTy->getParamType(0)->isPointerTy() ||
  1009. !FTy->getParamType(1)->isPointerTy())
  1010. return false;
  1011. setDoesNotThrow(F);
  1012. setDoesNotCapture(F, 1);
  1013. setDoesNotCapture(F, 2);
  1014. setOnlyReadsMemory(F, 1);
  1015. break;
  1016. case LibFunc::write:
  1017. if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
  1018. return false;
  1019. // May throw; "write" is a valid pthread cancellation point.
  1020. setDoesNotCapture(F, 2);
  1021. setOnlyReadsMemory(F, 2);
  1022. break;
  1023. case LibFunc::bcopy:
  1024. if (FTy->getNumParams() != 3 ||
  1025. !FTy->getParamType(0)->isPointerTy() ||
  1026. !FTy->getParamType(1)->isPointerTy())
  1027. return false;
  1028. setDoesNotThrow(F);
  1029. setDoesNotCapture(F, 1);
  1030. setDoesNotCapture(F, 2);
  1031. setOnlyReadsMemory(F, 1);
  1032. break;
  1033. case LibFunc::bcmp:
  1034. if (FTy->getNumParams() != 3 ||
  1035. !FTy->getParamType(0)->isPointerTy() ||
  1036. !FTy->getParamType(1)->isPointerTy())
  1037. return false;
  1038. setDoesNotThrow(F);
  1039. setOnlyReadsMemory(F);
  1040. setDoesNotCapture(F, 1);
  1041. setDoesNotCapture(F, 2);
  1042. break;
  1043. case LibFunc::bzero:
  1044. if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
  1045. return false;
  1046. setDoesNotThrow(F);
  1047. setDoesNotCapture(F, 1);
  1048. break;
  1049. case LibFunc::calloc:
  1050. if (FTy->getNumParams() != 2 ||
  1051. !FTy->getReturnType()->isPointerTy())
  1052. return false;
  1053. setDoesNotThrow(F);
  1054. setDoesNotAlias(F, 0);
  1055. break;
  1056. case LibFunc::chmod:
  1057. case LibFunc::chown:
  1058. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1059. return false;
  1060. setDoesNotThrow(F);
  1061. setDoesNotCapture(F, 1);
  1062. setOnlyReadsMemory(F, 1);
  1063. break;
  1064. case LibFunc::ctermid:
  1065. case LibFunc::clearerr:
  1066. case LibFunc::closedir:
  1067. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1068. return false;
  1069. setDoesNotThrow(F);
  1070. setDoesNotCapture(F, 1);
  1071. break;
  1072. case LibFunc::atoi:
  1073. case LibFunc::atol:
  1074. case LibFunc::atof:
  1075. case LibFunc::atoll:
  1076. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1077. return false;
  1078. setDoesNotThrow(F);
  1079. setOnlyReadsMemory(F);
  1080. setDoesNotCapture(F, 1);
  1081. break;
  1082. case LibFunc::access:
  1083. if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
  1084. return false;
  1085. setDoesNotThrow(F);
  1086. setDoesNotCapture(F, 1);
  1087. setOnlyReadsMemory(F, 1);
  1088. break;
  1089. case LibFunc::fopen:
  1090. if (FTy->getNumParams() != 2 ||
  1091. !FTy->getReturnType()->isPointerTy() ||
  1092. !FTy->getParamType(0)->isPointerTy() ||
  1093. !FTy->getParamType(1)->isPointerTy())
  1094. return false;
  1095. setDoesNotThrow(F);
  1096. setDoesNotAlias(F, 0);
  1097. setDoesNotCapture(F, 1);
  1098. setDoesNotCapture(F, 2);
  1099. setOnlyReadsMemory(F, 1);
  1100. setOnlyReadsMemory(F, 2);
  1101. break;
  1102. case LibFunc::fdopen:
  1103. if (FTy->getNumParams() != 2 ||
  1104. !FTy->getReturnType()->isPointerTy() ||
  1105. !FTy->getParamType(1)->isPointerTy())
  1106. return false;
  1107. setDoesNotThrow(F);
  1108. setDoesNotAlias(F, 0);
  1109. setDoesNotCapture(F, 2);
  1110. setOnlyReadsMemory(F, 2);
  1111. break;
  1112. case LibFunc::feof:
  1113. case LibFunc::free:
  1114. case LibFunc::fseek:
  1115. case LibFunc::ftell:
  1116. case LibFunc::fgetc:
  1117. case LibFunc::fseeko:
  1118. case LibFunc::ftello:
  1119. case LibFunc::fileno:
  1120. case LibFunc::fflush:
  1121. case LibFunc::fclose:
  1122. case LibFunc::fsetpos:
  1123. case LibFunc::flockfile:
  1124. case LibFunc::funlockfile:
  1125. case LibFunc::ftrylockfile:
  1126. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1127. return false;
  1128. setDoesNotThrow(F);
  1129. setDoesNotCapture(F, 1);
  1130. break;
  1131. case LibFunc::ferror:
  1132. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1133. return false;
  1134. setDoesNotThrow(F);
  1135. setDoesNotCapture(F, 1);
  1136. setOnlyReadsMemory(F);
  1137. break;
  1138. case LibFunc::fputc:
  1139. case LibFunc::fstat:
  1140. case LibFunc::frexp:
  1141. case LibFunc::frexpf:
  1142. case LibFunc::frexpl:
  1143. case LibFunc::fstatvfs:
  1144. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1145. return false;
  1146. setDoesNotThrow(F);
  1147. setDoesNotCapture(F, 2);
  1148. break;
  1149. case LibFunc::fgets:
  1150. if (FTy->getNumParams() != 3 ||
  1151. !FTy->getParamType(0)->isPointerTy() ||
  1152. !FTy->getParamType(2)->isPointerTy())
  1153. return false;
  1154. setDoesNotThrow(F);
  1155. setDoesNotCapture(F, 3);
  1156. break;
  1157. case LibFunc::fread:
  1158. if (FTy->getNumParams() != 4 ||
  1159. !FTy->getParamType(0)->isPointerTy() ||
  1160. !FTy->getParamType(3)->isPointerTy())
  1161. return false;
  1162. setDoesNotThrow(F);
  1163. setDoesNotCapture(F, 1);
  1164. setDoesNotCapture(F, 4);
  1165. break;
  1166. case LibFunc::fwrite:
  1167. if (FTy->getNumParams() != 4 ||
  1168. !FTy->getParamType(0)->isPointerTy() ||
  1169. !FTy->getParamType(3)->isPointerTy())
  1170. return false;
  1171. setDoesNotThrow(F);
  1172. setDoesNotCapture(F, 1);
  1173. setDoesNotCapture(F, 4);
  1174. break;
  1175. case LibFunc::fputs:
  1176. if (FTy->getNumParams() < 2 ||
  1177. !FTy->getParamType(0)->isPointerTy() ||
  1178. !FTy->getParamType(1)->isPointerTy())
  1179. return false;
  1180. setDoesNotThrow(F);
  1181. setDoesNotCapture(F, 1);
  1182. setDoesNotCapture(F, 2);
  1183. setOnlyReadsMemory(F, 1);
  1184. break;
  1185. case LibFunc::fscanf:
  1186. case LibFunc::fprintf:
  1187. if (FTy->getNumParams() < 2 ||
  1188. !FTy->getParamType(0)->isPointerTy() ||
  1189. !FTy->getParamType(1)->isPointerTy())
  1190. return false;
  1191. setDoesNotThrow(F);
  1192. setDoesNotCapture(F, 1);
  1193. setDoesNotCapture(F, 2);
  1194. setOnlyReadsMemory(F, 2);
  1195. break;
  1196. case LibFunc::fgetpos:
  1197. if (FTy->getNumParams() < 2 ||
  1198. !FTy->getParamType(0)->isPointerTy() ||
  1199. !FTy->getParamType(1)->isPointerTy())
  1200. return false;
  1201. setDoesNotThrow(F);
  1202. setDoesNotCapture(F, 1);
  1203. setDoesNotCapture(F, 2);
  1204. break;
  1205. case LibFunc::getc:
  1206. case LibFunc::getlogin_r:
  1207. case LibFunc::getc_unlocked:
  1208. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1209. return false;
  1210. setDoesNotThrow(F);
  1211. setDoesNotCapture(F, 1);
  1212. break;
  1213. case LibFunc::getenv:
  1214. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1215. return false;
  1216. setDoesNotThrow(F);
  1217. setOnlyReadsMemory(F);
  1218. setDoesNotCapture(F, 1);
  1219. break;
  1220. case LibFunc::gets:
  1221. case LibFunc::getchar:
  1222. setDoesNotThrow(F);
  1223. break;
  1224. case LibFunc::getitimer:
  1225. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1226. return false;
  1227. setDoesNotThrow(F);
  1228. setDoesNotCapture(F, 2);
  1229. break;
  1230. case LibFunc::getpwnam:
  1231. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1232. return false;
  1233. setDoesNotThrow(F);
  1234. setDoesNotCapture(F, 1);
  1235. setOnlyReadsMemory(F, 1);
  1236. break;
  1237. case LibFunc::ungetc:
  1238. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1239. return false;
  1240. setDoesNotThrow(F);
  1241. setDoesNotCapture(F, 2);
  1242. break;
  1243. case LibFunc::uname:
  1244. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1245. return false;
  1246. setDoesNotThrow(F);
  1247. setDoesNotCapture(F, 1);
  1248. break;
  1249. case LibFunc::unlink:
  1250. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1251. return false;
  1252. setDoesNotThrow(F);
  1253. setDoesNotCapture(F, 1);
  1254. setOnlyReadsMemory(F, 1);
  1255. break;
  1256. case LibFunc::unsetenv:
  1257. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1258. return false;
  1259. setDoesNotThrow(F);
  1260. setDoesNotCapture(F, 1);
  1261. setOnlyReadsMemory(F, 1);
  1262. break;
  1263. case LibFunc::utime:
  1264. case LibFunc::utimes:
  1265. if (FTy->getNumParams() != 2 ||
  1266. !FTy->getParamType(0)->isPointerTy() ||
  1267. !FTy->getParamType(1)->isPointerTy())
  1268. return false;
  1269. setDoesNotThrow(F);
  1270. setDoesNotCapture(F, 1);
  1271. setDoesNotCapture(F, 2);
  1272. setOnlyReadsMemory(F, 1);
  1273. setOnlyReadsMemory(F, 2);
  1274. break;
  1275. case LibFunc::putc:
  1276. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1277. return false;
  1278. setDoesNotThrow(F);
  1279. setDoesNotCapture(F, 2);
  1280. break;
  1281. case LibFunc::puts:
  1282. case LibFunc::printf:
  1283. case LibFunc::perror:
  1284. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1285. return false;
  1286. setDoesNotThrow(F);
  1287. setDoesNotCapture(F, 1);
  1288. setOnlyReadsMemory(F, 1);
  1289. break;
  1290. case LibFunc::pread:
  1291. if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
  1292. return false;
  1293. // May throw; "pread" is a valid pthread cancellation point.
  1294. setDoesNotCapture(F, 2);
  1295. break;
  1296. case LibFunc::pwrite:
  1297. if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
  1298. return false;
  1299. // May throw; "pwrite" is a valid pthread cancellation point.
  1300. setDoesNotCapture(F, 2);
  1301. setOnlyReadsMemory(F, 2);
  1302. break;
  1303. case LibFunc::putchar:
  1304. setDoesNotThrow(F);
  1305. break;
  1306. case LibFunc::popen:
  1307. if (FTy->getNumParams() != 2 ||
  1308. !FTy->getReturnType()->isPointerTy() ||
  1309. !FTy->getParamType(0)->isPointerTy() ||
  1310. !FTy->getParamType(1)->isPointerTy())
  1311. return false;
  1312. setDoesNotThrow(F);
  1313. setDoesNotAlias(F, 0);
  1314. setDoesNotCapture(F, 1);
  1315. setDoesNotCapture(F, 2);
  1316. setOnlyReadsMemory(F, 1);
  1317. setOnlyReadsMemory(F, 2);
  1318. break;
  1319. case LibFunc::pclose:
  1320. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1321. return false;
  1322. setDoesNotThrow(F);
  1323. setDoesNotCapture(F, 1);
  1324. break;
  1325. case LibFunc::vscanf:
  1326. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1327. return false;
  1328. setDoesNotThrow(F);
  1329. setDoesNotCapture(F, 1);
  1330. setOnlyReadsMemory(F, 1);
  1331. break;
  1332. case LibFunc::vsscanf:
  1333. if (FTy->getNumParams() != 3 ||
  1334. !FTy->getParamType(1)->isPointerTy() ||
  1335. !FTy->getParamType(2)->isPointerTy())
  1336. return false;
  1337. setDoesNotThrow(F);
  1338. setDoesNotCapture(F, 1);
  1339. setDoesNotCapture(F, 2);
  1340. setOnlyReadsMemory(F, 1);
  1341. setOnlyReadsMemory(F, 2);
  1342. break;
  1343. case LibFunc::vfscanf:
  1344. if (FTy->getNumParams() != 3 ||
  1345. !FTy->getParamType(1)->isPointerTy() ||
  1346. !FTy->getParamType(2)->isPointerTy())
  1347. return false;
  1348. setDoesNotThrow(F);
  1349. setDoesNotCapture(F, 1);
  1350. setDoesNotCapture(F, 2);
  1351. setOnlyReadsMemory(F, 2);
  1352. break;
  1353. case LibFunc::valloc:
  1354. if (!FTy->getReturnType()->isPointerTy())
  1355. return false;
  1356. setDoesNotThrow(F);
  1357. setDoesNotAlias(F, 0);
  1358. break;
  1359. case LibFunc::vprintf:
  1360. if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
  1361. return false;
  1362. setDoesNotThrow(F);
  1363. setDoesNotCapture(F, 1);
  1364. setOnlyReadsMemory(F, 1);
  1365. break;
  1366. case LibFunc::vfprintf:
  1367. case LibFunc::vsprintf:
  1368. if (FTy->getNumParams() != 3 ||
  1369. !FTy->getParamType(0)->isPointerTy() ||
  1370. !FTy->getParamType(1)->isPointerTy())
  1371. return false;
  1372. setDoesNotThrow(F);
  1373. setDoesNotCapture(F, 1);
  1374. setDoesNotCapture(F, 2);
  1375. setOnlyReadsMemory(F, 2);
  1376. break;
  1377. case LibFunc::vsnprintf:
  1378. if (FTy->getNumParams() != 4 ||
  1379. !FTy->getParamType(0)->isPointerTy() ||
  1380. !FTy->getParamType(2)->isPointerTy())
  1381. return false;
  1382. setDoesNotThrow(F);
  1383. setDoesNotCapture(F, 1);
  1384. setDoesNotCapture(F, 3);
  1385. setOnlyReadsMemory(F, 3);
  1386. break;
  1387. case LibFunc::open:
  1388. if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
  1389. return false;
  1390. // May throw; "open" is a valid pthread cancellation point.
  1391. setDoesNotCapture(F, 1);
  1392. setOnlyReadsMemory(F, 1);
  1393. break;
  1394. case LibFunc::opendir:
  1395. if (FTy->getNumParams() != 1 ||
  1396. !FTy->getReturnType()->isPointerTy() ||
  1397. !FTy->getParamType(0)->isPointerTy())
  1398. return false;
  1399. setDoesNotThrow(F);
  1400. setDoesNotAlias(F, 0);
  1401. setDoesNotCapture(F, 1);
  1402. setOnlyReadsMemory(F, 1);
  1403. break;
  1404. case LibFunc::tmpfile:
  1405. if (!FTy->getReturnType()->isPointerTy())
  1406. return false;
  1407. setDoesNotThrow(F);
  1408. setDoesNotAlias(F, 0);
  1409. break;
  1410. case LibFunc::times:
  1411. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1412. return false;
  1413. setDoesNotThrow(F);
  1414. setDoesNotCapture(F, 1);
  1415. break;
  1416. case LibFunc::htonl:
  1417. case LibFunc::htons:
  1418. case LibFunc::ntohl:
  1419. case LibFunc::ntohs:
  1420. setDoesNotThrow(F);
  1421. setDoesNotAccessMemory(F);
  1422. break;
  1423. case LibFunc::lstat:
  1424. if (FTy->getNumParams() != 2 ||
  1425. !FTy->getParamType(0)->isPointerTy() ||
  1426. !FTy->getParamType(1)->isPointerTy())
  1427. return false;
  1428. setDoesNotThrow(F);
  1429. setDoesNotCapture(F, 1);
  1430. setDoesNotCapture(F, 2);
  1431. setOnlyReadsMemory(F, 1);
  1432. break;
  1433. case LibFunc::lchown:
  1434. if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
  1435. return false;
  1436. setDoesNotThrow(F);
  1437. setDoesNotCapture(F, 1);
  1438. setOnlyReadsMemory(F, 1);
  1439. break;
  1440. case LibFunc::qsort:
  1441. if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
  1442. return false;
  1443. // May throw; places call through function pointer.
  1444. setDoesNotCapture(F, 4);
  1445. break;
  1446. case LibFunc::dunder_strdup:
  1447. case LibFunc::dunder_strndup:
  1448. if (FTy->getNumParams() < 1 ||
  1449. !FTy->getReturnType()->isPointerTy() ||
  1450. !FTy->getParamType(0)->isPointerTy())
  1451. return false;
  1452. setDoesNotThrow(F);
  1453. setDoesNotAlias(F, 0);
  1454. setDoesNotCapture(F, 1);
  1455. setOnlyReadsMemory(F, 1);
  1456. break;
  1457. case LibFunc::dunder_strtok_r:
  1458. if (FTy->getNumParams() != 3 ||
  1459. !FTy->getParamType(1)->isPointerTy())
  1460. return false;
  1461. setDoesNotThrow(F);
  1462. setDoesNotCapture(F, 2);
  1463. setOnlyReadsMemory(F, 2);
  1464. break;
  1465. case LibFunc::under_IO_getc:
  1466. if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
  1467. return false;
  1468. setDoesNotThrow(F);
  1469. setDoesNotCapture(F, 1);
  1470. break;
  1471. case LibFunc::under_IO_putc:
  1472. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1473. return false;
  1474. setDoesNotThrow(F);
  1475. setDoesNotCapture(F, 2);
  1476. break;
  1477. case LibFunc::dunder_isoc99_scanf:
  1478. if (FTy->getNumParams() < 1 ||
  1479. !FTy->getParamType(0)->isPointerTy())
  1480. return false;
  1481. setDoesNotThrow(F);
  1482. setDoesNotCapture(F, 1);
  1483. setOnlyReadsMemory(F, 1);
  1484. break;
  1485. case LibFunc::stat64:
  1486. case LibFunc::lstat64:
  1487. case LibFunc::statvfs64:
  1488. if (FTy->getNumParams() < 1 ||
  1489. !FTy->getParamType(0)->isPointerTy() ||
  1490. !FTy->getParamType(1)->isPointerTy())
  1491. return false;
  1492. setDoesNotThrow(F);
  1493. setDoesNotCapture(F, 1);
  1494. setDoesNotCapture(F, 2);
  1495. setOnlyReadsMemory(F, 1);
  1496. break;
  1497. case LibFunc::dunder_isoc99_sscanf:
  1498. if (FTy->getNumParams() < 1 ||
  1499. !FTy->getParamType(0)->isPointerTy() ||
  1500. !FTy->getParamType(1)->isPointerTy())
  1501. return false;
  1502. setDoesNotThrow(F);
  1503. setDoesNotCapture(F, 1);
  1504. setDoesNotCapture(F, 2);
  1505. setOnlyReadsMemory(F, 1);
  1506. setOnlyReadsMemory(F, 2);
  1507. break;
  1508. case LibFunc::fopen64:
  1509. if (FTy->getNumParams() != 2 ||
  1510. !FTy->getReturnType()->isPointerTy() ||
  1511. !FTy->getParamType(0)->isPointerTy() ||
  1512. !FTy->getParamType(1)->isPointerTy())
  1513. return false;
  1514. setDoesNotThrow(F);
  1515. setDoesNotAlias(F, 0);
  1516. setDoesNotCapture(F, 1);
  1517. setDoesNotCapture(F, 2);
  1518. setOnlyReadsMemory(F, 1);
  1519. setOnlyReadsMemory(F, 2);
  1520. break;
  1521. case LibFunc::fseeko64:
  1522. case LibFunc::ftello64:
  1523. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1524. return false;
  1525. setDoesNotThrow(F);
  1526. setDoesNotCapture(F, 1);
  1527. break;
  1528. case LibFunc::tmpfile64:
  1529. if (!FTy->getReturnType()->isPointerTy())
  1530. return false;
  1531. setDoesNotThrow(F);
  1532. setDoesNotAlias(F, 0);
  1533. break;
  1534. case LibFunc::fstat64:
  1535. case LibFunc::fstatvfs64:
  1536. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1537. return false;
  1538. setDoesNotThrow(F);
  1539. setDoesNotCapture(F, 2);
  1540. break;
  1541. case LibFunc::open64:
  1542. if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
  1543. return false;
  1544. // May throw; "open" is a valid pthread cancellation point.
  1545. setDoesNotCapture(F, 1);
  1546. setOnlyReadsMemory(F, 1);
  1547. break;
  1548. case LibFunc::gettimeofday:
  1549. if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
  1550. !FTy->getParamType(1)->isPointerTy())
  1551. return false;
  1552. // Currently some platforms have the restrict keyword on the arguments to
  1553. // gettimeofday. To be conservative, do not add noalias to gettimeofday's
  1554. // arguments.
  1555. setDoesNotThrow(F);
  1556. setDoesNotCapture(F, 1);
  1557. setDoesNotCapture(F, 2);
  1558. break;
  1559. default:
  1560. // Didn't mark any attributes.
  1561. return false;
  1562. }
  1563. return true;
  1564. }
  1565. /// annotateLibraryCalls - Adds attributes to well-known standard library
  1566. /// call declarations.
  1567. bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
  1568. bool MadeChange = false;
  1569. // Check each function in turn annotating well-known library function
  1570. // declarations with attributes.
  1571. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  1572. Function *F = (*I)->getFunction();
  1573. if (F && F->isDeclaration())
  1574. MadeChange |= inferPrototypeAttributes(*F);
  1575. }
  1576. return MadeChange;
  1577. }
  1578. bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
  1579. AA = &getAnalysis<AliasAnalysis>();
  1580. TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  1581. bool Changed = annotateLibraryCalls(SCC);
  1582. Changed |= AddReadAttrs(SCC);
  1583. Changed |= AddArgumentAttrs(SCC);
  1584. Changed |= AddNoAliasAttrs(SCC);
  1585. return Changed;
  1586. }