FunctionAttrs.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. #if 0 // HLSL Change Starts - Exclude potentially duplicate 64bit versions
  1509. case LibFunc::fopen64:
  1510. if (FTy->getNumParams() != 2 ||
  1511. !FTy->getReturnType()->isPointerTy() ||
  1512. !FTy->getParamType(0)->isPointerTy() ||
  1513. !FTy->getParamType(1)->isPointerTy())
  1514. return false;
  1515. setDoesNotThrow(F);
  1516. setDoesNotAlias(F, 0);
  1517. setDoesNotCapture(F, 1);
  1518. setDoesNotCapture(F, 2);
  1519. setOnlyReadsMemory(F, 1);
  1520. setOnlyReadsMemory(F, 2);
  1521. break;
  1522. case LibFunc::fseeko64:
  1523. case LibFunc::ftello64:
  1524. if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
  1525. return false;
  1526. setDoesNotThrow(F);
  1527. setDoesNotCapture(F, 1);
  1528. break;
  1529. case LibFunc::tmpfile64:
  1530. if (!FTy->getReturnType()->isPointerTy())
  1531. return false;
  1532. setDoesNotThrow(F);
  1533. setDoesNotAlias(F, 0);
  1534. break;
  1535. #endif // HLSL Change Ends - Exclude potentially duplicate 64bit versions
  1536. case LibFunc::fstat64:
  1537. case LibFunc::fstatvfs64:
  1538. if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
  1539. return false;
  1540. setDoesNotThrow(F);
  1541. setDoesNotCapture(F, 2);
  1542. break;
  1543. case LibFunc::open64:
  1544. if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
  1545. return false;
  1546. // May throw; "open" is a valid pthread cancellation point.
  1547. setDoesNotCapture(F, 1);
  1548. setOnlyReadsMemory(F, 1);
  1549. break;
  1550. case LibFunc::gettimeofday:
  1551. if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
  1552. !FTy->getParamType(1)->isPointerTy())
  1553. return false;
  1554. // Currently some platforms have the restrict keyword on the arguments to
  1555. // gettimeofday. To be conservative, do not add noalias to gettimeofday's
  1556. // arguments.
  1557. setDoesNotThrow(F);
  1558. setDoesNotCapture(F, 1);
  1559. setDoesNotCapture(F, 2);
  1560. break;
  1561. default:
  1562. // Didn't mark any attributes.
  1563. return false;
  1564. }
  1565. return true;
  1566. }
  1567. /// annotateLibraryCalls - Adds attributes to well-known standard library
  1568. /// call declarations.
  1569. bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
  1570. bool MadeChange = false;
  1571. // Check each function in turn annotating well-known library function
  1572. // declarations with attributes.
  1573. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  1574. Function *F = (*I)->getFunction();
  1575. if (F && F->isDeclaration())
  1576. MadeChange |= inferPrototypeAttributes(*F);
  1577. }
  1578. return MadeChange;
  1579. }
  1580. bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
  1581. AA = &getAnalysis<AliasAnalysis>();
  1582. TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  1583. bool Changed = annotateLibraryCalls(SCC);
  1584. Changed |= AddReadAttrs(SCC);
  1585. Changed |= AddArgumentAttrs(SCC);
  1586. Changed |= AddNoAliasAttrs(SCC);
  1587. return Changed;
  1588. }