FunctionAttrs.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// This file implements interprocedural passes which walk the
  11. /// call-graph deducing and/or propagating function attributes.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/IPO/FunctionAttrs.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SCCIterator.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SetVector.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/Analysis/AssumptionCache.h"
  25. #include "llvm/Analysis/BasicAliasAnalysis.h"
  26. #include "llvm/Analysis/CFG.h"
  27. #include "llvm/Analysis/CGSCCPassManager.h"
  28. #include "llvm/Analysis/CallGraph.h"
  29. #include "llvm/Analysis/CallGraphSCCPass.h"
  30. #include "llvm/Analysis/CaptureTracking.h"
  31. #include "llvm/Analysis/LazyCallGraph.h"
  32. #include "llvm/Analysis/MemoryLocation.h"
  33. #include "llvm/Analysis/ValueTracking.h"
  34. #include "llvm/IR/Argument.h"
  35. #include "llvm/IR/Attributes.h"
  36. #include "llvm/IR/BasicBlock.h"
  37. #include "llvm/IR/Constant.h"
  38. #include "llvm/IR/Constants.h"
  39. #include "llvm/IR/Function.h"
  40. #include "llvm/IR/InstIterator.h"
  41. #include "llvm/IR/InstrTypes.h"
  42. #include "llvm/IR/Instruction.h"
  43. #include "llvm/IR/Instructions.h"
  44. #include "llvm/IR/IntrinsicInst.h"
  45. #include "llvm/IR/Metadata.h"
  46. #include "llvm/IR/ModuleSummaryIndex.h"
  47. #include "llvm/IR/PassManager.h"
  48. #include "llvm/IR/Type.h"
  49. #include "llvm/IR/Use.h"
  50. #include "llvm/IR/User.h"
  51. #include "llvm/IR/Value.h"
  52. #include "llvm/InitializePasses.h"
  53. #include "llvm/Pass.h"
  54. #include "llvm/Support/Casting.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Compiler.h"
  57. #include "llvm/Support/Debug.h"
  58. #include "llvm/Support/ErrorHandling.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include "llvm/Transforms/IPO.h"
  61. #include "llvm/Transforms/Utils/Local.h"
  62. #include <cassert>
  63. #include <iterator>
  64. #include <map>
  65. #include <optional>
  66. #include <vector>
  67. using namespace llvm;
  68. #define DEBUG_TYPE "function-attrs"
  69. STATISTIC(NumMemoryAttr, "Number of functions with improved memory attribute");
  70. STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
  71. STATISTIC(NumReturned, "Number of arguments marked returned");
  72. STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
  73. STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
  74. STATISTIC(NumWriteOnlyArg, "Number of arguments marked writeonly");
  75. STATISTIC(NumNoAlias, "Number of function returns marked noalias");
  76. STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
  77. STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
  78. STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
  79. STATISTIC(NumNoFree, "Number of functions marked as nofree");
  80. STATISTIC(NumWillReturn, "Number of functions marked as willreturn");
  81. STATISTIC(NumNoSync, "Number of functions marked as nosync");
  82. STATISTIC(NumThinLinkNoRecurse,
  83. "Number of functions marked as norecurse during thinlink");
  84. STATISTIC(NumThinLinkNoUnwind,
  85. "Number of functions marked as nounwind during thinlink");
  86. static cl::opt<bool> EnableNonnullArgPropagation(
  87. "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
  88. cl::desc("Try to propagate nonnull argument attributes from callsites to "
  89. "caller functions."));
  90. static cl::opt<bool> DisableNoUnwindInference(
  91. "disable-nounwind-inference", cl::Hidden,
  92. cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
  93. static cl::opt<bool> DisableNoFreeInference(
  94. "disable-nofree-inference", cl::Hidden,
  95. cl::desc("Stop inferring nofree attribute during function-attrs pass"));
  96. static cl::opt<bool> DisableThinLTOPropagation(
  97. "disable-thinlto-funcattrs", cl::init(true), cl::Hidden,
  98. cl::desc("Don't propagate function-attrs in thinLTO"));
  99. namespace {
  100. using SCCNodeSet = SmallSetVector<Function *, 8>;
  101. } // end anonymous namespace
  102. /// Returns the memory access attribute for function F using AAR for AA results,
  103. /// where SCCNodes is the current SCC.
  104. ///
  105. /// If ThisBody is true, this function may examine the function body and will
  106. /// return a result pertaining to this copy of the function. If it is false, the
  107. /// result will be based only on AA results for the function declaration; it
  108. /// will be assumed that some other (perhaps less optimized) version of the
  109. /// function may be selected at link time.
  110. static MemoryEffects checkFunctionMemoryAccess(Function &F, bool ThisBody,
  111. AAResults &AAR,
  112. const SCCNodeSet &SCCNodes) {
  113. MemoryEffects OrigME = AAR.getMemoryEffects(&F);
  114. if (OrigME.doesNotAccessMemory())
  115. // Already perfect!
  116. return OrigME;
  117. if (!ThisBody)
  118. return OrigME;
  119. MemoryEffects ME = MemoryEffects::none();
  120. // Inalloca and preallocated arguments are always clobbered by the call.
  121. if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
  122. F.getAttributes().hasAttrSomewhere(Attribute::Preallocated))
  123. ME |= MemoryEffects::argMemOnly(ModRefInfo::ModRef);
  124. auto AddLocAccess = [&](const MemoryLocation &Loc, ModRefInfo MR) {
  125. // Ignore accesses to known-invariant or local memory.
  126. MR &= AAR.getModRefInfoMask(Loc, /*IgnoreLocal=*/true);
  127. if (isNoModRef(MR))
  128. return;
  129. const Value *UO = getUnderlyingObject(Loc.Ptr);
  130. assert(!isa<AllocaInst>(UO) &&
  131. "Should have been handled by getModRefInfoMask()");
  132. if (isa<Argument>(UO)) {
  133. ME |= MemoryEffects::argMemOnly(MR);
  134. return;
  135. }
  136. // If it's not an identified object, it might be an argument.
  137. if (!isIdentifiedObject(UO))
  138. ME |= MemoryEffects::argMemOnly(MR);
  139. ME |= MemoryEffects(MemoryEffects::Other, MR);
  140. };
  141. // Scan the function body for instructions that may read or write memory.
  142. for (Instruction &I : instructions(F)) {
  143. // Some instructions can be ignored even if they read or write memory.
  144. // Detect these now, skipping to the next instruction if one is found.
  145. if (auto *Call = dyn_cast<CallBase>(&I)) {
  146. // Ignore calls to functions in the same SCC, as long as the call sites
  147. // don't have operand bundles. Calls with operand bundles are allowed to
  148. // have memory effects not described by the memory effects of the call
  149. // target.
  150. if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
  151. SCCNodes.count(Call->getCalledFunction()))
  152. continue;
  153. MemoryEffects CallME = AAR.getMemoryEffects(Call);
  154. // If the call doesn't access memory, we're done.
  155. if (CallME.doesNotAccessMemory())
  156. continue;
  157. // A pseudo probe call shouldn't change any function attribute since it
  158. // doesn't translate to a real instruction. It comes with a memory access
  159. // tag to prevent itself being removed by optimizations and not block
  160. // other instructions being optimized.
  161. if (isa<PseudoProbeInst>(I))
  162. continue;
  163. ME |= CallME.getWithoutLoc(MemoryEffects::ArgMem);
  164. // If the call accesses captured memory (currently part of "other") and
  165. // an argument is captured (currently not tracked), then it may also
  166. // access argument memory.
  167. ModRefInfo OtherMR = CallME.getModRef(MemoryEffects::Other);
  168. ME |= MemoryEffects::argMemOnly(OtherMR);
  169. // Check whether all pointer arguments point to local memory, and
  170. // ignore calls that only access local memory.
  171. ModRefInfo ArgMR = CallME.getModRef(MemoryEffects::ArgMem);
  172. if (ArgMR != ModRefInfo::NoModRef) {
  173. for (const Use &U : Call->args()) {
  174. const Value *Arg = U;
  175. if (!Arg->getType()->isPtrOrPtrVectorTy())
  176. continue;
  177. AddLocAccess(MemoryLocation::getBeforeOrAfter(Arg, I.getAAMetadata()), ArgMR);
  178. }
  179. }
  180. continue;
  181. }
  182. ModRefInfo MR = ModRefInfo::NoModRef;
  183. if (I.mayWriteToMemory())
  184. MR |= ModRefInfo::Mod;
  185. if (I.mayReadFromMemory())
  186. MR |= ModRefInfo::Ref;
  187. if (MR == ModRefInfo::NoModRef)
  188. continue;
  189. std::optional<MemoryLocation> Loc = MemoryLocation::getOrNone(&I);
  190. if (!Loc) {
  191. // If no location is known, conservatively assume anything can be
  192. // accessed.
  193. ME |= MemoryEffects(MR);
  194. continue;
  195. }
  196. // Volatile operations may access inaccessible memory.
  197. if (I.isVolatile())
  198. ME |= MemoryEffects::inaccessibleMemOnly(MR);
  199. AddLocAccess(*Loc, MR);
  200. }
  201. return OrigME & ME;
  202. }
  203. MemoryEffects llvm::computeFunctionBodyMemoryAccess(Function &F,
  204. AAResults &AAR) {
  205. return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
  206. }
  207. /// Deduce readonly/readnone/writeonly attributes for the SCC.
  208. template <typename AARGetterT>
  209. static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
  210. SmallSet<Function *, 8> &Changed) {
  211. MemoryEffects ME = MemoryEffects::none();
  212. for (Function *F : SCCNodes) {
  213. // Call the callable parameter to look up AA results for this function.
  214. AAResults &AAR = AARGetter(*F);
  215. // Non-exact function definitions may not be selected at link time, and an
  216. // alternative version that writes to memory may be selected. See the
  217. // comment on GlobalValue::isDefinitionExact for more details.
  218. ME |= checkFunctionMemoryAccess(*F, F->hasExactDefinition(), AAR, SCCNodes);
  219. // Reached bottom of the lattice, we will not be able to improve the result.
  220. if (ME == MemoryEffects::unknown())
  221. return;
  222. }
  223. for (Function *F : SCCNodes) {
  224. MemoryEffects OldME = F->getMemoryEffects();
  225. MemoryEffects NewME = ME & OldME;
  226. if (NewME != OldME) {
  227. ++NumMemoryAttr;
  228. F->setMemoryEffects(NewME);
  229. Changed.insert(F);
  230. }
  231. }
  232. }
  233. // Compute definitive function attributes for a function taking into account
  234. // prevailing definitions and linkage types
  235. static FunctionSummary *calculatePrevailingSummary(
  236. ValueInfo VI,
  237. DenseMap<ValueInfo, FunctionSummary *> &CachedPrevailingSummary,
  238. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  239. IsPrevailing) {
  240. if (CachedPrevailingSummary.count(VI))
  241. return CachedPrevailingSummary[VI];
  242. /// At this point, prevailing symbols have been resolved. The following leads
  243. /// to returning a conservative result:
  244. /// - Multiple instances with local linkage. Normally local linkage would be
  245. /// unique per module
  246. /// as the GUID includes the module path. We could have a guid alias if
  247. /// there wasn't any distinguishing path when each file was compiled, but
  248. /// that should be rare so we'll punt on those.
  249. /// These next 2 cases should not happen and will assert:
  250. /// - Multiple instances with external linkage. This should be caught in
  251. /// symbol resolution
  252. /// - Non-existent FunctionSummary for Aliasee. This presents a hole in our
  253. /// knowledge meaning we have to go conservative.
  254. /// Otherwise, we calculate attributes for a function as:
  255. /// 1. If we have a local linkage, take its attributes. If there's somehow
  256. /// multiple, bail and go conservative.
  257. /// 2. If we have an external/WeakODR/LinkOnceODR linkage check that it is
  258. /// prevailing, take its attributes.
  259. /// 3. If we have a Weak/LinkOnce linkage the copies can have semantic
  260. /// differences. However, if the prevailing copy is known it will be used
  261. /// so take its attributes. If the prevailing copy is in a native file
  262. /// all IR copies will be dead and propagation will go conservative.
  263. /// 4. AvailableExternally summaries without a prevailing copy are known to
  264. /// occur in a couple of circumstances:
  265. /// a. An internal function gets imported due to its caller getting
  266. /// imported, it becomes AvailableExternally but no prevailing
  267. /// definition exists. Because it has to get imported along with its
  268. /// caller the attributes will be captured by propagating on its
  269. /// caller.
  270. /// b. C++11 [temp.explicit]p10 can generate AvailableExternally
  271. /// definitions of explicitly instanced template declarations
  272. /// for inlining which are ultimately dropped from the TU. Since this
  273. /// is localized to the TU the attributes will have already made it to
  274. /// the callers.
  275. /// These are edge cases and already captured by their callers so we
  276. /// ignore these for now. If they become relevant to optimize in the
  277. /// future this can be revisited.
  278. /// 5. Otherwise, go conservative.
  279. CachedPrevailingSummary[VI] = nullptr;
  280. FunctionSummary *Local = nullptr;
  281. FunctionSummary *Prevailing = nullptr;
  282. for (const auto &GVS : VI.getSummaryList()) {
  283. if (!GVS->isLive())
  284. continue;
  285. FunctionSummary *FS = dyn_cast<FunctionSummary>(GVS->getBaseObject());
  286. // Virtual and Unknown (e.g. indirect) calls require going conservative
  287. if (!FS || FS->fflags().HasUnknownCall)
  288. return nullptr;
  289. const auto &Linkage = GVS->linkage();
  290. if (GlobalValue::isLocalLinkage(Linkage)) {
  291. if (Local) {
  292. LLVM_DEBUG(
  293. dbgs()
  294. << "ThinLTO FunctionAttrs: Multiple Local Linkage, bailing on "
  295. "function "
  296. << VI.name() << " from " << FS->modulePath() << ". Previous module "
  297. << Local->modulePath() << "\n");
  298. return nullptr;
  299. }
  300. Local = FS;
  301. } else if (GlobalValue::isExternalLinkage(Linkage)) {
  302. assert(IsPrevailing(VI.getGUID(), GVS.get()));
  303. Prevailing = FS;
  304. break;
  305. } else if (GlobalValue::isWeakODRLinkage(Linkage) ||
  306. GlobalValue::isLinkOnceODRLinkage(Linkage) ||
  307. GlobalValue::isWeakAnyLinkage(Linkage) ||
  308. GlobalValue::isLinkOnceAnyLinkage(Linkage)) {
  309. if (IsPrevailing(VI.getGUID(), GVS.get())) {
  310. Prevailing = FS;
  311. break;
  312. }
  313. } else if (GlobalValue::isAvailableExternallyLinkage(Linkage)) {
  314. // TODO: Handle these cases if they become meaningful
  315. continue;
  316. }
  317. }
  318. if (Local) {
  319. assert(!Prevailing);
  320. CachedPrevailingSummary[VI] = Local;
  321. } else if (Prevailing) {
  322. assert(!Local);
  323. CachedPrevailingSummary[VI] = Prevailing;
  324. }
  325. return CachedPrevailingSummary[VI];
  326. }
  327. bool llvm::thinLTOPropagateFunctionAttrs(
  328. ModuleSummaryIndex &Index,
  329. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  330. IsPrevailing) {
  331. // TODO: implement addNoAliasAttrs once
  332. // there's more information about the return type in the summary
  333. if (DisableThinLTOPropagation)
  334. return false;
  335. DenseMap<ValueInfo, FunctionSummary *> CachedPrevailingSummary;
  336. bool Changed = false;
  337. auto PropagateAttributes = [&](std::vector<ValueInfo> &SCCNodes) {
  338. // Assume we can propagate unless we discover otherwise
  339. FunctionSummary::FFlags InferredFlags;
  340. InferredFlags.NoRecurse = (SCCNodes.size() == 1);
  341. InferredFlags.NoUnwind = true;
  342. for (auto &V : SCCNodes) {
  343. FunctionSummary *CallerSummary =
  344. calculatePrevailingSummary(V, CachedPrevailingSummary, IsPrevailing);
  345. // Function summaries can fail to contain information such as declarations
  346. if (!CallerSummary)
  347. return;
  348. if (CallerSummary->fflags().MayThrow)
  349. InferredFlags.NoUnwind = false;
  350. for (const auto &Callee : CallerSummary->calls()) {
  351. FunctionSummary *CalleeSummary = calculatePrevailingSummary(
  352. Callee.first, CachedPrevailingSummary, IsPrevailing);
  353. if (!CalleeSummary)
  354. return;
  355. if (!CalleeSummary->fflags().NoRecurse)
  356. InferredFlags.NoRecurse = false;
  357. if (!CalleeSummary->fflags().NoUnwind)
  358. InferredFlags.NoUnwind = false;
  359. if (!InferredFlags.NoUnwind && !InferredFlags.NoRecurse)
  360. break;
  361. }
  362. }
  363. if (InferredFlags.NoUnwind || InferredFlags.NoRecurse) {
  364. Changed = true;
  365. for (auto &V : SCCNodes) {
  366. if (InferredFlags.NoRecurse) {
  367. LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoRecurse to "
  368. << V.name() << "\n");
  369. ++NumThinLinkNoRecurse;
  370. }
  371. if (InferredFlags.NoUnwind) {
  372. LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoUnwind to "
  373. << V.name() << "\n");
  374. ++NumThinLinkNoUnwind;
  375. }
  376. for (const auto &S : V.getSummaryList()) {
  377. if (auto *FS = dyn_cast<FunctionSummary>(S.get())) {
  378. if (InferredFlags.NoRecurse)
  379. FS->setNoRecurse();
  380. if (InferredFlags.NoUnwind)
  381. FS->setNoUnwind();
  382. }
  383. }
  384. }
  385. }
  386. };
  387. // Call propagation functions on each SCC in the Index
  388. for (scc_iterator<ModuleSummaryIndex *> I = scc_begin(&Index); !I.isAtEnd();
  389. ++I) {
  390. std::vector<ValueInfo> Nodes(*I);
  391. PropagateAttributes(Nodes);
  392. }
  393. return Changed;
  394. }
  395. namespace {
  396. /// For a given pointer Argument, this retains a list of Arguments of functions
  397. /// in the same SCC that the pointer data flows into. We use this to build an
  398. /// SCC of the arguments.
  399. struct ArgumentGraphNode {
  400. Argument *Definition;
  401. SmallVector<ArgumentGraphNode *, 4> Uses;
  402. };
  403. class ArgumentGraph {
  404. // We store pointers to ArgumentGraphNode objects, so it's important that
  405. // that they not move around upon insert.
  406. using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
  407. ArgumentMapTy ArgumentMap;
  408. // There is no root node for the argument graph, in fact:
  409. // void f(int *x, int *y) { if (...) f(x, y); }
  410. // is an example where the graph is disconnected. The SCCIterator requires a
  411. // single entry point, so we maintain a fake ("synthetic") root node that
  412. // uses every node. Because the graph is directed and nothing points into
  413. // the root, it will not participate in any SCCs (except for its own).
  414. ArgumentGraphNode SyntheticRoot;
  415. public:
  416. ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
  417. using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
  418. iterator begin() { return SyntheticRoot.Uses.begin(); }
  419. iterator end() { return SyntheticRoot.Uses.end(); }
  420. ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
  421. ArgumentGraphNode *operator[](Argument *A) {
  422. ArgumentGraphNode &Node = ArgumentMap[A];
  423. Node.Definition = A;
  424. SyntheticRoot.Uses.push_back(&Node);
  425. return &Node;
  426. }
  427. };
  428. /// This tracker checks whether callees are in the SCC, and if so it does not
  429. /// consider that a capture, instead adding it to the "Uses" list and
  430. /// continuing with the analysis.
  431. struct ArgumentUsesTracker : public CaptureTracker {
  432. ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
  433. void tooManyUses() override { Captured = true; }
  434. bool captured(const Use *U) override {
  435. CallBase *CB = dyn_cast<CallBase>(U->getUser());
  436. if (!CB) {
  437. Captured = true;
  438. return true;
  439. }
  440. Function *F = CB->getCalledFunction();
  441. if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
  442. Captured = true;
  443. return true;
  444. }
  445. assert(!CB->isCallee(U) && "callee operand reported captured?");
  446. const unsigned UseIndex = CB->getDataOperandNo(U);
  447. if (UseIndex >= CB->arg_size()) {
  448. // Data operand, but not a argument operand -- must be a bundle operand
  449. assert(CB->hasOperandBundles() && "Must be!");
  450. // CaptureTracking told us that we're being captured by an operand bundle
  451. // use. In this case it does not matter if the callee is within our SCC
  452. // or not -- we've been captured in some unknown way, and we have to be
  453. // conservative.
  454. Captured = true;
  455. return true;
  456. }
  457. if (UseIndex >= F->arg_size()) {
  458. assert(F->isVarArg() && "More params than args in non-varargs call");
  459. Captured = true;
  460. return true;
  461. }
  462. Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
  463. return false;
  464. }
  465. // True only if certainly captured (used outside our SCC).
  466. bool Captured = false;
  467. // Uses within our SCC.
  468. SmallVector<Argument *, 4> Uses;
  469. const SCCNodeSet &SCCNodes;
  470. };
  471. } // end anonymous namespace
  472. namespace llvm {
  473. template <> struct GraphTraits<ArgumentGraphNode *> {
  474. using NodeRef = ArgumentGraphNode *;
  475. using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
  476. static NodeRef getEntryNode(NodeRef A) { return A; }
  477. static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
  478. static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
  479. };
  480. template <>
  481. struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
  482. static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
  483. static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
  484. return AG->begin();
  485. }
  486. static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
  487. };
  488. } // end namespace llvm
  489. /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
  490. static Attribute::AttrKind
  491. determinePointerAccessAttrs(Argument *A,
  492. const SmallPtrSet<Argument *, 8> &SCCNodes) {
  493. SmallVector<Use *, 32> Worklist;
  494. SmallPtrSet<Use *, 32> Visited;
  495. // inalloca arguments are always clobbered by the call.
  496. if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
  497. return Attribute::None;
  498. bool IsRead = false;
  499. bool IsWrite = false;
  500. for (Use &U : A->uses()) {
  501. Visited.insert(&U);
  502. Worklist.push_back(&U);
  503. }
  504. while (!Worklist.empty()) {
  505. if (IsWrite && IsRead)
  506. // No point in searching further..
  507. return Attribute::None;
  508. Use *U = Worklist.pop_back_val();
  509. Instruction *I = cast<Instruction>(U->getUser());
  510. switch (I->getOpcode()) {
  511. case Instruction::BitCast:
  512. case Instruction::GetElementPtr:
  513. case Instruction::PHI:
  514. case Instruction::Select:
  515. case Instruction::AddrSpaceCast:
  516. // The original value is not read/written via this if the new value isn't.
  517. for (Use &UU : I->uses())
  518. if (Visited.insert(&UU).second)
  519. Worklist.push_back(&UU);
  520. break;
  521. case Instruction::Call:
  522. case Instruction::Invoke: {
  523. CallBase &CB = cast<CallBase>(*I);
  524. if (CB.isCallee(U)) {
  525. IsRead = true;
  526. // Note that indirect calls do not capture, see comment in
  527. // CaptureTracking for context
  528. continue;
  529. }
  530. // Given we've explictily handled the callee operand above, what's left
  531. // must be a data operand (e.g. argument or operand bundle)
  532. const unsigned UseIndex = CB.getDataOperandNo(U);
  533. if (!CB.doesNotCapture(UseIndex)) {
  534. if (!CB.onlyReadsMemory())
  535. // If the callee can save a copy into other memory, then simply
  536. // scanning uses of the call is insufficient. We have no way
  537. // of tracking copies of the pointer through memory to see
  538. // if a reloaded copy is written to, thus we must give up.
  539. return Attribute::None;
  540. // Push users for processing once we finish this one
  541. if (!I->getType()->isVoidTy())
  542. for (Use &UU : I->uses())
  543. if (Visited.insert(&UU).second)
  544. Worklist.push_back(&UU);
  545. }
  546. if (CB.doesNotAccessMemory())
  547. continue;
  548. if (Function *F = CB.getCalledFunction())
  549. if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&
  550. SCCNodes.count(F->getArg(UseIndex)))
  551. // This is an argument which is part of the speculative SCC. Note
  552. // that only operands corresponding to formal arguments of the callee
  553. // can participate in the speculation.
  554. break;
  555. // The accessors used on call site here do the right thing for calls and
  556. // invokes with operand bundles.
  557. if (CB.doesNotAccessMemory(UseIndex)) {
  558. /* nop */
  559. } else if (CB.onlyReadsMemory() || CB.onlyReadsMemory(UseIndex)) {
  560. IsRead = true;
  561. } else if (CB.hasFnAttr(Attribute::WriteOnly) ||
  562. CB.dataOperandHasImpliedAttr(UseIndex, Attribute::WriteOnly)) {
  563. IsWrite = true;
  564. } else {
  565. return Attribute::None;
  566. }
  567. break;
  568. }
  569. case Instruction::Load:
  570. // A volatile load has side effects beyond what readonly can be relied
  571. // upon.
  572. if (cast<LoadInst>(I)->isVolatile())
  573. return Attribute::None;
  574. IsRead = true;
  575. break;
  576. case Instruction::Store:
  577. if (cast<StoreInst>(I)->getValueOperand() == *U)
  578. // untrackable capture
  579. return Attribute::None;
  580. // A volatile store has side effects beyond what writeonly can be relied
  581. // upon.
  582. if (cast<StoreInst>(I)->isVolatile())
  583. return Attribute::None;
  584. IsWrite = true;
  585. break;
  586. case Instruction::ICmp:
  587. case Instruction::Ret:
  588. break;
  589. default:
  590. return Attribute::None;
  591. }
  592. }
  593. if (IsWrite && IsRead)
  594. return Attribute::None;
  595. else if (IsRead)
  596. return Attribute::ReadOnly;
  597. else if (IsWrite)
  598. return Attribute::WriteOnly;
  599. else
  600. return Attribute::ReadNone;
  601. }
  602. /// Deduce returned attributes for the SCC.
  603. static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes,
  604. SmallSet<Function *, 8> &Changed) {
  605. // Check each function in turn, determining if an argument is always returned.
  606. for (Function *F : SCCNodes) {
  607. // We can infer and propagate function attributes only when we know that the
  608. // definition we'll get at link time is *exactly* the definition we see now.
  609. // For more details, see GlobalValue::mayBeDerefined.
  610. if (!F->hasExactDefinition())
  611. continue;
  612. if (F->getReturnType()->isVoidTy())
  613. continue;
  614. // There is nothing to do if an argument is already marked as 'returned'.
  615. if (llvm::any_of(F->args(),
  616. [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
  617. continue;
  618. auto FindRetArg = [&]() -> Value * {
  619. Value *RetArg = nullptr;
  620. for (BasicBlock &BB : *F)
  621. if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
  622. // Note that stripPointerCasts should look through functions with
  623. // returned arguments.
  624. Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
  625. if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
  626. return nullptr;
  627. if (!RetArg)
  628. RetArg = RetVal;
  629. else if (RetArg != RetVal)
  630. return nullptr;
  631. }
  632. return RetArg;
  633. };
  634. if (Value *RetArg = FindRetArg()) {
  635. auto *A = cast<Argument>(RetArg);
  636. A->addAttr(Attribute::Returned);
  637. ++NumReturned;
  638. Changed.insert(F);
  639. }
  640. }
  641. }
  642. /// If a callsite has arguments that are also arguments to the parent function,
  643. /// try to propagate attributes from the callsite's arguments to the parent's
  644. /// arguments. This may be important because inlining can cause information loss
  645. /// when attribute knowledge disappears with the inlined call.
  646. static bool addArgumentAttrsFromCallsites(Function &F) {
  647. if (!EnableNonnullArgPropagation)
  648. return false;
  649. bool Changed = false;
  650. // For an argument attribute to transfer from a callsite to the parent, the
  651. // call must be guaranteed to execute every time the parent is called.
  652. // Conservatively, just check for calls in the entry block that are guaranteed
  653. // to execute.
  654. // TODO: This could be enhanced by testing if the callsite post-dominates the
  655. // entry block or by doing simple forward walks or backward walks to the
  656. // callsite.
  657. BasicBlock &Entry = F.getEntryBlock();
  658. for (Instruction &I : Entry) {
  659. if (auto *CB = dyn_cast<CallBase>(&I)) {
  660. if (auto *CalledFunc = CB->getCalledFunction()) {
  661. for (auto &CSArg : CalledFunc->args()) {
  662. if (!CSArg.hasNonNullAttr(/* AllowUndefOrPoison */ false))
  663. continue;
  664. // If the non-null callsite argument operand is an argument to 'F'
  665. // (the caller) and the call is guaranteed to execute, then the value
  666. // must be non-null throughout 'F'.
  667. auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));
  668. if (FArg && !FArg->hasNonNullAttr()) {
  669. FArg->addAttr(Attribute::NonNull);
  670. Changed = true;
  671. }
  672. }
  673. }
  674. }
  675. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  676. break;
  677. }
  678. return Changed;
  679. }
  680. static bool addAccessAttr(Argument *A, Attribute::AttrKind R) {
  681. assert((R == Attribute::ReadOnly || R == Attribute::ReadNone ||
  682. R == Attribute::WriteOnly)
  683. && "Must be an access attribute.");
  684. assert(A && "Argument must not be null.");
  685. // If the argument already has the attribute, nothing needs to be done.
  686. if (A->hasAttribute(R))
  687. return false;
  688. // Otherwise, remove potentially conflicting attribute, add the new one,
  689. // and update statistics.
  690. A->removeAttr(Attribute::WriteOnly);
  691. A->removeAttr(Attribute::ReadOnly);
  692. A->removeAttr(Attribute::ReadNone);
  693. A->addAttr(R);
  694. if (R == Attribute::ReadOnly)
  695. ++NumReadOnlyArg;
  696. else if (R == Attribute::WriteOnly)
  697. ++NumWriteOnlyArg;
  698. else
  699. ++NumReadNoneArg;
  700. return true;
  701. }
  702. /// Deduce nocapture attributes for the SCC.
  703. static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
  704. SmallSet<Function *, 8> &Changed) {
  705. ArgumentGraph AG;
  706. // Check each function in turn, determining which pointer arguments are not
  707. // captured.
  708. for (Function *F : SCCNodes) {
  709. // We can infer and propagate function attributes only when we know that the
  710. // definition we'll get at link time is *exactly* the definition we see now.
  711. // For more details, see GlobalValue::mayBeDerefined.
  712. if (!F->hasExactDefinition())
  713. continue;
  714. if (addArgumentAttrsFromCallsites(*F))
  715. Changed.insert(F);
  716. // Functions that are readonly (or readnone) and nounwind and don't return
  717. // a value can't capture arguments. Don't analyze them.
  718. if (F->onlyReadsMemory() && F->doesNotThrow() &&
  719. F->getReturnType()->isVoidTy()) {
  720. for (Argument &A : F->args()) {
  721. if (A.getType()->isPointerTy() && !A.hasNoCaptureAttr()) {
  722. A.addAttr(Attribute::NoCapture);
  723. ++NumNoCapture;
  724. Changed.insert(F);
  725. }
  726. }
  727. continue;
  728. }
  729. for (Argument &A : F->args()) {
  730. if (!A.getType()->isPointerTy())
  731. continue;
  732. bool HasNonLocalUses = false;
  733. if (!A.hasNoCaptureAttr()) {
  734. ArgumentUsesTracker Tracker(SCCNodes);
  735. PointerMayBeCaptured(&A, &Tracker);
  736. if (!Tracker.Captured) {
  737. if (Tracker.Uses.empty()) {
  738. // If it's trivially not captured, mark it nocapture now.
  739. A.addAttr(Attribute::NoCapture);
  740. ++NumNoCapture;
  741. Changed.insert(F);
  742. } else {
  743. // If it's not trivially captured and not trivially not captured,
  744. // then it must be calling into another function in our SCC. Save
  745. // its particulars for Argument-SCC analysis later.
  746. ArgumentGraphNode *Node = AG[&A];
  747. for (Argument *Use : Tracker.Uses) {
  748. Node->Uses.push_back(AG[Use]);
  749. if (Use != &A)
  750. HasNonLocalUses = true;
  751. }
  752. }
  753. }
  754. // Otherwise, it's captured. Don't bother doing SCC analysis on it.
  755. }
  756. if (!HasNonLocalUses && !A.onlyReadsMemory()) {
  757. // Can we determine that it's readonly/readnone/writeonly without doing
  758. // an SCC? Note that we don't allow any calls at all here, or else our
  759. // result will be dependent on the iteration order through the
  760. // functions in the SCC.
  761. SmallPtrSet<Argument *, 8> Self;
  762. Self.insert(&A);
  763. Attribute::AttrKind R = determinePointerAccessAttrs(&A, Self);
  764. if (R != Attribute::None)
  765. if (addAccessAttr(&A, R))
  766. Changed.insert(F);
  767. }
  768. }
  769. }
  770. // The graph we've collected is partial because we stopped scanning for
  771. // argument uses once we solved the argument trivially. These partial nodes
  772. // show up as ArgumentGraphNode objects with an empty Uses list, and for
  773. // these nodes the final decision about whether they capture has already been
  774. // made. If the definition doesn't have a 'nocapture' attribute by now, it
  775. // captures.
  776. for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
  777. const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
  778. if (ArgumentSCC.size() == 1) {
  779. if (!ArgumentSCC[0]->Definition)
  780. continue; // synthetic root node
  781. // eg. "void f(int* x) { if (...) f(x); }"
  782. if (ArgumentSCC[0]->Uses.size() == 1 &&
  783. ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
  784. Argument *A = ArgumentSCC[0]->Definition;
  785. A->addAttr(Attribute::NoCapture);
  786. ++NumNoCapture;
  787. Changed.insert(A->getParent());
  788. // Infer the access attributes given the new nocapture one
  789. SmallPtrSet<Argument *, 8> Self;
  790. Self.insert(&*A);
  791. Attribute::AttrKind R = determinePointerAccessAttrs(&*A, Self);
  792. if (R != Attribute::None)
  793. addAccessAttr(A, R);
  794. }
  795. continue;
  796. }
  797. bool SCCCaptured = false;
  798. for (ArgumentGraphNode *Node : ArgumentSCC) {
  799. if (Node->Uses.empty() && !Node->Definition->hasNoCaptureAttr()) {
  800. SCCCaptured = true;
  801. break;
  802. }
  803. }
  804. if (SCCCaptured)
  805. continue;
  806. SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
  807. // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
  808. // quickly looking up whether a given Argument is in this ArgumentSCC.
  809. for (ArgumentGraphNode *I : ArgumentSCC) {
  810. ArgumentSCCNodes.insert(I->Definition);
  811. }
  812. for (ArgumentGraphNode *N : ArgumentSCC) {
  813. for (ArgumentGraphNode *Use : N->Uses) {
  814. Argument *A = Use->Definition;
  815. if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
  816. continue;
  817. SCCCaptured = true;
  818. break;
  819. }
  820. if (SCCCaptured)
  821. break;
  822. }
  823. if (SCCCaptured)
  824. continue;
  825. for (ArgumentGraphNode *N : ArgumentSCC) {
  826. Argument *A = N->Definition;
  827. A->addAttr(Attribute::NoCapture);
  828. ++NumNoCapture;
  829. Changed.insert(A->getParent());
  830. }
  831. // We also want to compute readonly/readnone/writeonly. With a small number
  832. // of false negatives, we can assume that any pointer which is captured
  833. // isn't going to be provably readonly or readnone, since by definition
  834. // we can't analyze all uses of a captured pointer.
  835. //
  836. // The false negatives happen when the pointer is captured by a function
  837. // that promises readonly/readnone behaviour on the pointer, then the
  838. // pointer's lifetime ends before anything that writes to arbitrary memory.
  839. // Also, a readonly/readnone pointer may be returned, but returning a
  840. // pointer is capturing it.
  841. auto meetAccessAttr = [](Attribute::AttrKind A, Attribute::AttrKind B) {
  842. if (A == B)
  843. return A;
  844. if (A == Attribute::ReadNone)
  845. return B;
  846. if (B == Attribute::ReadNone)
  847. return A;
  848. return Attribute::None;
  849. };
  850. Attribute::AttrKind AccessAttr = Attribute::ReadNone;
  851. for (ArgumentGraphNode *N : ArgumentSCC) {
  852. Argument *A = N->Definition;
  853. Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);
  854. AccessAttr = meetAccessAttr(AccessAttr, K);
  855. if (AccessAttr == Attribute::None)
  856. break;
  857. }
  858. if (AccessAttr != Attribute::None) {
  859. for (ArgumentGraphNode *N : ArgumentSCC) {
  860. Argument *A = N->Definition;
  861. if (addAccessAttr(A, AccessAttr))
  862. Changed.insert(A->getParent());
  863. }
  864. }
  865. }
  866. }
  867. /// Tests whether a function is "malloc-like".
  868. ///
  869. /// A function is "malloc-like" if it returns either null or a pointer that
  870. /// doesn't alias any other pointer visible to the caller.
  871. static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
  872. SmallSetVector<Value *, 8> FlowsToReturn;
  873. for (BasicBlock &BB : *F)
  874. if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
  875. FlowsToReturn.insert(Ret->getReturnValue());
  876. for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
  877. Value *RetVal = FlowsToReturn[i];
  878. if (Constant *C = dyn_cast<Constant>(RetVal)) {
  879. if (!C->isNullValue() && !isa<UndefValue>(C))
  880. return false;
  881. continue;
  882. }
  883. if (isa<Argument>(RetVal))
  884. return false;
  885. if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
  886. switch (RVI->getOpcode()) {
  887. // Extend the analysis by looking upwards.
  888. case Instruction::BitCast:
  889. case Instruction::GetElementPtr:
  890. case Instruction::AddrSpaceCast:
  891. FlowsToReturn.insert(RVI->getOperand(0));
  892. continue;
  893. case Instruction::Select: {
  894. SelectInst *SI = cast<SelectInst>(RVI);
  895. FlowsToReturn.insert(SI->getTrueValue());
  896. FlowsToReturn.insert(SI->getFalseValue());
  897. continue;
  898. }
  899. case Instruction::PHI: {
  900. PHINode *PN = cast<PHINode>(RVI);
  901. for (Value *IncValue : PN->incoming_values())
  902. FlowsToReturn.insert(IncValue);
  903. continue;
  904. }
  905. // Check whether the pointer came from an allocation.
  906. case Instruction::Alloca:
  907. break;
  908. case Instruction::Call:
  909. case Instruction::Invoke: {
  910. CallBase &CB = cast<CallBase>(*RVI);
  911. if (CB.hasRetAttr(Attribute::NoAlias))
  912. break;
  913. if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))
  914. break;
  915. [[fallthrough]];
  916. }
  917. default:
  918. return false; // Did not come from an allocation.
  919. }
  920. if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
  921. return false;
  922. }
  923. return true;
  924. }
  925. /// Deduce noalias attributes for the SCC.
  926. static void addNoAliasAttrs(const SCCNodeSet &SCCNodes,
  927. SmallSet<Function *, 8> &Changed) {
  928. // Check each function in turn, determining which functions return noalias
  929. // pointers.
  930. for (Function *F : SCCNodes) {
  931. // Already noalias.
  932. if (F->returnDoesNotAlias())
  933. continue;
  934. // We can infer and propagate function attributes only when we know that the
  935. // definition we'll get at link time is *exactly* the definition we see now.
  936. // For more details, see GlobalValue::mayBeDerefined.
  937. if (!F->hasExactDefinition())
  938. return;
  939. // We annotate noalias return values, which are only applicable to
  940. // pointer types.
  941. if (!F->getReturnType()->isPointerTy())
  942. continue;
  943. if (!isFunctionMallocLike(F, SCCNodes))
  944. return;
  945. }
  946. for (Function *F : SCCNodes) {
  947. if (F->returnDoesNotAlias() ||
  948. !F->getReturnType()->isPointerTy())
  949. continue;
  950. F->setReturnDoesNotAlias();
  951. ++NumNoAlias;
  952. Changed.insert(F);
  953. }
  954. }
  955. /// Tests whether this function is known to not return null.
  956. ///
  957. /// Requires that the function returns a pointer.
  958. ///
  959. /// Returns true if it believes the function will not return a null, and sets
  960. /// \p Speculative based on whether the returned conclusion is a speculative
  961. /// conclusion due to SCC calls.
  962. static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
  963. bool &Speculative) {
  964. assert(F->getReturnType()->isPointerTy() &&
  965. "nonnull only meaningful on pointer types");
  966. Speculative = false;
  967. SmallSetVector<Value *, 8> FlowsToReturn;
  968. for (BasicBlock &BB : *F)
  969. if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
  970. FlowsToReturn.insert(Ret->getReturnValue());
  971. auto &DL = F->getParent()->getDataLayout();
  972. for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
  973. Value *RetVal = FlowsToReturn[i];
  974. // If this value is locally known to be non-null, we're good
  975. if (isKnownNonZero(RetVal, DL))
  976. continue;
  977. // Otherwise, we need to look upwards since we can't make any local
  978. // conclusions.
  979. Instruction *RVI = dyn_cast<Instruction>(RetVal);
  980. if (!RVI)
  981. return false;
  982. switch (RVI->getOpcode()) {
  983. // Extend the analysis by looking upwards.
  984. case Instruction::BitCast:
  985. case Instruction::GetElementPtr:
  986. case Instruction::AddrSpaceCast:
  987. FlowsToReturn.insert(RVI->getOperand(0));
  988. continue;
  989. case Instruction::Select: {
  990. SelectInst *SI = cast<SelectInst>(RVI);
  991. FlowsToReturn.insert(SI->getTrueValue());
  992. FlowsToReturn.insert(SI->getFalseValue());
  993. continue;
  994. }
  995. case Instruction::PHI: {
  996. PHINode *PN = cast<PHINode>(RVI);
  997. for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  998. FlowsToReturn.insert(PN->getIncomingValue(i));
  999. continue;
  1000. }
  1001. case Instruction::Call:
  1002. case Instruction::Invoke: {
  1003. CallBase &CB = cast<CallBase>(*RVI);
  1004. Function *Callee = CB.getCalledFunction();
  1005. // A call to a node within the SCC is assumed to return null until
  1006. // proven otherwise
  1007. if (Callee && SCCNodes.count(Callee)) {
  1008. Speculative = true;
  1009. continue;
  1010. }
  1011. return false;
  1012. }
  1013. default:
  1014. return false; // Unknown source, may be null
  1015. };
  1016. llvm_unreachable("should have either continued or returned");
  1017. }
  1018. return true;
  1019. }
  1020. /// Deduce nonnull attributes for the SCC.
  1021. static void addNonNullAttrs(const SCCNodeSet &SCCNodes,
  1022. SmallSet<Function *, 8> &Changed) {
  1023. // Speculative that all functions in the SCC return only nonnull
  1024. // pointers. We may refute this as we analyze functions.
  1025. bool SCCReturnsNonNull = true;
  1026. // Check each function in turn, determining which functions return nonnull
  1027. // pointers.
  1028. for (Function *F : SCCNodes) {
  1029. // Already nonnull.
  1030. if (F->getAttributes().hasRetAttr(Attribute::NonNull))
  1031. continue;
  1032. // We can infer and propagate function attributes only when we know that the
  1033. // definition we'll get at link time is *exactly* the definition we see now.
  1034. // For more details, see GlobalValue::mayBeDerefined.
  1035. if (!F->hasExactDefinition())
  1036. return;
  1037. // We annotate nonnull return values, which are only applicable to
  1038. // pointer types.
  1039. if (!F->getReturnType()->isPointerTy())
  1040. continue;
  1041. bool Speculative = false;
  1042. if (isReturnNonNull(F, SCCNodes, Speculative)) {
  1043. if (!Speculative) {
  1044. // Mark the function eagerly since we may discover a function
  1045. // which prevents us from speculating about the entire SCC
  1046. LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
  1047. << " as nonnull\n");
  1048. F->addRetAttr(Attribute::NonNull);
  1049. ++NumNonNullReturn;
  1050. Changed.insert(F);
  1051. }
  1052. continue;
  1053. }
  1054. // At least one function returns something which could be null, can't
  1055. // speculate any more.
  1056. SCCReturnsNonNull = false;
  1057. }
  1058. if (SCCReturnsNonNull) {
  1059. for (Function *F : SCCNodes) {
  1060. if (F->getAttributes().hasRetAttr(Attribute::NonNull) ||
  1061. !F->getReturnType()->isPointerTy())
  1062. continue;
  1063. LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
  1064. F->addRetAttr(Attribute::NonNull);
  1065. ++NumNonNullReturn;
  1066. Changed.insert(F);
  1067. }
  1068. }
  1069. }
  1070. namespace {
  1071. /// Collects a set of attribute inference requests and performs them all in one
  1072. /// go on a single SCC Node. Inference involves scanning function bodies
  1073. /// looking for instructions that violate attribute assumptions.
  1074. /// As soon as all the bodies are fine we are free to set the attribute.
  1075. /// Customization of inference for individual attributes is performed by
  1076. /// providing a handful of predicates for each attribute.
  1077. class AttributeInferer {
  1078. public:
  1079. /// Describes a request for inference of a single attribute.
  1080. struct InferenceDescriptor {
  1081. /// Returns true if this function does not have to be handled.
  1082. /// General intent for this predicate is to provide an optimization
  1083. /// for functions that do not need this attribute inference at all
  1084. /// (say, for functions that already have the attribute).
  1085. std::function<bool(const Function &)> SkipFunction;
  1086. /// Returns true if this instruction violates attribute assumptions.
  1087. std::function<bool(Instruction &)> InstrBreaksAttribute;
  1088. /// Sets the inferred attribute for this function.
  1089. std::function<void(Function &)> SetAttribute;
  1090. /// Attribute we derive.
  1091. Attribute::AttrKind AKind;
  1092. /// If true, only "exact" definitions can be used to infer this attribute.
  1093. /// See GlobalValue::isDefinitionExact.
  1094. bool RequiresExactDefinition;
  1095. InferenceDescriptor(Attribute::AttrKind AK,
  1096. std::function<bool(const Function &)> SkipFunc,
  1097. std::function<bool(Instruction &)> InstrScan,
  1098. std::function<void(Function &)> SetAttr,
  1099. bool ReqExactDef)
  1100. : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
  1101. SetAttribute(SetAttr), AKind(AK),
  1102. RequiresExactDefinition(ReqExactDef) {}
  1103. };
  1104. private:
  1105. SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
  1106. public:
  1107. void registerAttrInference(InferenceDescriptor AttrInference) {
  1108. InferenceDescriptors.push_back(AttrInference);
  1109. }
  1110. void run(const SCCNodeSet &SCCNodes, SmallSet<Function *, 8> &Changed);
  1111. };
  1112. /// Perform all the requested attribute inference actions according to the
  1113. /// attribute predicates stored before.
  1114. void AttributeInferer::run(const SCCNodeSet &SCCNodes,
  1115. SmallSet<Function *, 8> &Changed) {
  1116. SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
  1117. // Go through all the functions in SCC and check corresponding attribute
  1118. // assumptions for each of them. Attributes that are invalid for this SCC
  1119. // will be removed from InferInSCC.
  1120. for (Function *F : SCCNodes) {
  1121. // No attributes whose assumptions are still valid - done.
  1122. if (InferInSCC.empty())
  1123. return;
  1124. // Check if our attributes ever need scanning/can be scanned.
  1125. llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
  1126. if (ID.SkipFunction(*F))
  1127. return false;
  1128. // Remove from further inference (invalidate) when visiting a function
  1129. // that has no instructions to scan/has an unsuitable definition.
  1130. return F->isDeclaration() ||
  1131. (ID.RequiresExactDefinition && !F->hasExactDefinition());
  1132. });
  1133. // For each attribute still in InferInSCC that doesn't explicitly skip F,
  1134. // set up the F instructions scan to verify assumptions of the attribute.
  1135. SmallVector<InferenceDescriptor, 4> InferInThisFunc;
  1136. llvm::copy_if(
  1137. InferInSCC, std::back_inserter(InferInThisFunc),
  1138. [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
  1139. if (InferInThisFunc.empty())
  1140. continue;
  1141. // Start instruction scan.
  1142. for (Instruction &I : instructions(*F)) {
  1143. llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
  1144. if (!ID.InstrBreaksAttribute(I))
  1145. return false;
  1146. // Remove attribute from further inference on any other functions
  1147. // because attribute assumptions have just been violated.
  1148. llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
  1149. return D.AKind == ID.AKind;
  1150. });
  1151. // Remove attribute from the rest of current instruction scan.
  1152. return true;
  1153. });
  1154. if (InferInThisFunc.empty())
  1155. break;
  1156. }
  1157. }
  1158. if (InferInSCC.empty())
  1159. return;
  1160. for (Function *F : SCCNodes)
  1161. // At this point InferInSCC contains only functions that were either:
  1162. // - explicitly skipped from scan/inference, or
  1163. // - verified to have no instructions that break attribute assumptions.
  1164. // Hence we just go and force the attribute for all non-skipped functions.
  1165. for (auto &ID : InferInSCC) {
  1166. if (ID.SkipFunction(*F))
  1167. continue;
  1168. Changed.insert(F);
  1169. ID.SetAttribute(*F);
  1170. }
  1171. }
  1172. struct SCCNodesResult {
  1173. SCCNodeSet SCCNodes;
  1174. bool HasUnknownCall;
  1175. };
  1176. } // end anonymous namespace
  1177. /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
  1178. static bool InstrBreaksNonConvergent(Instruction &I,
  1179. const SCCNodeSet &SCCNodes) {
  1180. const CallBase *CB = dyn_cast<CallBase>(&I);
  1181. // Breaks non-convergent assumption if CS is a convergent call to a function
  1182. // not in the SCC.
  1183. return CB && CB->isConvergent() &&
  1184. !SCCNodes.contains(CB->getCalledFunction());
  1185. }
  1186. /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
  1187. static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
  1188. if (!I.mayThrow())
  1189. return false;
  1190. if (const auto *CI = dyn_cast<CallInst>(&I)) {
  1191. if (Function *Callee = CI->getCalledFunction()) {
  1192. // I is a may-throw call to a function inside our SCC. This doesn't
  1193. // invalidate our current working assumption that the SCC is no-throw; we
  1194. // just have to scan that other function.
  1195. if (SCCNodes.contains(Callee))
  1196. return false;
  1197. }
  1198. }
  1199. return true;
  1200. }
  1201. /// Helper for NoFree inference predicate InstrBreaksAttribute.
  1202. static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
  1203. CallBase *CB = dyn_cast<CallBase>(&I);
  1204. if (!CB)
  1205. return false;
  1206. if (CB->hasFnAttr(Attribute::NoFree))
  1207. return false;
  1208. // Speculatively assume in SCC.
  1209. if (Function *Callee = CB->getCalledFunction())
  1210. if (SCCNodes.contains(Callee))
  1211. return false;
  1212. return true;
  1213. }
  1214. /// Attempt to remove convergent function attribute when possible.
  1215. ///
  1216. /// Returns true if any changes to function attributes were made.
  1217. static void inferConvergent(const SCCNodeSet &SCCNodes,
  1218. SmallSet<Function *, 8> &Changed) {
  1219. AttributeInferer AI;
  1220. // Request to remove the convergent attribute from all functions in the SCC
  1221. // if every callsite within the SCC is not convergent (except for calls
  1222. // to functions within the SCC).
  1223. // Note: Removal of the attr from the callsites will happen in
  1224. // InstCombineCalls separately.
  1225. AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
  1226. Attribute::Convergent,
  1227. // Skip non-convergent functions.
  1228. [](const Function &F) { return !F.isConvergent(); },
  1229. // Instructions that break non-convergent assumption.
  1230. [SCCNodes](Instruction &I) {
  1231. return InstrBreaksNonConvergent(I, SCCNodes);
  1232. },
  1233. [](Function &F) {
  1234. LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
  1235. << "\n");
  1236. F.setNotConvergent();
  1237. },
  1238. /* RequiresExactDefinition= */ false});
  1239. // Perform all the requested attribute inference actions.
  1240. AI.run(SCCNodes, Changed);
  1241. }
  1242. /// Infer attributes from all functions in the SCC by scanning every
  1243. /// instruction for compliance to the attribute assumptions. Currently it
  1244. /// does:
  1245. /// - addition of NoUnwind attribute
  1246. ///
  1247. /// Returns true if any changes to function attributes were made.
  1248. static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,
  1249. SmallSet<Function *, 8> &Changed) {
  1250. AttributeInferer AI;
  1251. if (!DisableNoUnwindInference)
  1252. // Request to infer nounwind attribute for all the functions in the SCC if
  1253. // every callsite within the SCC is not throwing (except for calls to
  1254. // functions within the SCC). Note that nounwind attribute suffers from
  1255. // derefinement - results may change depending on how functions are
  1256. // optimized. Thus it can be inferred only from exact definitions.
  1257. AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
  1258. Attribute::NoUnwind,
  1259. // Skip non-throwing functions.
  1260. [](const Function &F) { return F.doesNotThrow(); },
  1261. // Instructions that break non-throwing assumption.
  1262. [&SCCNodes](Instruction &I) {
  1263. return InstrBreaksNonThrowing(I, SCCNodes);
  1264. },
  1265. [](Function &F) {
  1266. LLVM_DEBUG(dbgs()
  1267. << "Adding nounwind attr to fn " << F.getName() << "\n");
  1268. F.setDoesNotThrow();
  1269. ++NumNoUnwind;
  1270. },
  1271. /* RequiresExactDefinition= */ true});
  1272. if (!DisableNoFreeInference)
  1273. // Request to infer nofree attribute for all the functions in the SCC if
  1274. // every callsite within the SCC does not directly or indirectly free
  1275. // memory (except for calls to functions within the SCC). Note that nofree
  1276. // attribute suffers from derefinement - results may change depending on
  1277. // how functions are optimized. Thus it can be inferred only from exact
  1278. // definitions.
  1279. AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
  1280. Attribute::NoFree,
  1281. // Skip functions known not to free memory.
  1282. [](const Function &F) { return F.doesNotFreeMemory(); },
  1283. // Instructions that break non-deallocating assumption.
  1284. [&SCCNodes](Instruction &I) {
  1285. return InstrBreaksNoFree(I, SCCNodes);
  1286. },
  1287. [](Function &F) {
  1288. LLVM_DEBUG(dbgs()
  1289. << "Adding nofree attr to fn " << F.getName() << "\n");
  1290. F.setDoesNotFreeMemory();
  1291. ++NumNoFree;
  1292. },
  1293. /* RequiresExactDefinition= */ true});
  1294. // Perform all the requested attribute inference actions.
  1295. AI.run(SCCNodes, Changed);
  1296. }
  1297. static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
  1298. SmallSet<Function *, 8> &Changed) {
  1299. // Try and identify functions that do not recurse.
  1300. // If the SCC contains multiple nodes we know for sure there is recursion.
  1301. if (SCCNodes.size() != 1)
  1302. return;
  1303. Function *F = *SCCNodes.begin();
  1304. if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
  1305. return;
  1306. // If all of the calls in F are identifiable and are to norecurse functions, F
  1307. // is norecurse. This check also detects self-recursion as F is not currently
  1308. // marked norecurse, so any called from F to F will not be marked norecurse.
  1309. for (auto &BB : *F)
  1310. for (auto &I : BB.instructionsWithoutDebug())
  1311. if (auto *CB = dyn_cast<CallBase>(&I)) {
  1312. Function *Callee = CB->getCalledFunction();
  1313. if (!Callee || Callee == F || !Callee->doesNotRecurse())
  1314. // Function calls a potentially recursive function.
  1315. return;
  1316. }
  1317. // Every call was to a non-recursive function other than this function, and
  1318. // we have no indirect recursion as the SCC size is one. This function cannot
  1319. // recurse.
  1320. F->setDoesNotRecurse();
  1321. ++NumNoRecurse;
  1322. Changed.insert(F);
  1323. }
  1324. static bool instructionDoesNotReturn(Instruction &I) {
  1325. if (auto *CB = dyn_cast<CallBase>(&I))
  1326. return CB->hasFnAttr(Attribute::NoReturn);
  1327. return false;
  1328. }
  1329. // A basic block can only return if it terminates with a ReturnInst and does not
  1330. // contain calls to noreturn functions.
  1331. static bool basicBlockCanReturn(BasicBlock &BB) {
  1332. if (!isa<ReturnInst>(BB.getTerminator()))
  1333. return false;
  1334. return none_of(BB, instructionDoesNotReturn);
  1335. }
  1336. // FIXME: this doesn't handle recursion.
  1337. static bool canReturn(Function &F) {
  1338. SmallVector<BasicBlock *, 16> Worklist;
  1339. SmallPtrSet<BasicBlock *, 16> Visited;
  1340. Visited.insert(&F.front());
  1341. Worklist.push_back(&F.front());
  1342. do {
  1343. BasicBlock *BB = Worklist.pop_back_val();
  1344. if (basicBlockCanReturn(*BB))
  1345. return true;
  1346. for (BasicBlock *Succ : successors(BB))
  1347. if (Visited.insert(Succ).second)
  1348. Worklist.push_back(Succ);
  1349. } while (!Worklist.empty());
  1350. return false;
  1351. }
  1352. // Set the noreturn function attribute if possible.
  1353. static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
  1354. SmallSet<Function *, 8> &Changed) {
  1355. for (Function *F : SCCNodes) {
  1356. if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
  1357. F->doesNotReturn())
  1358. continue;
  1359. if (!canReturn(*F)) {
  1360. F->setDoesNotReturn();
  1361. Changed.insert(F);
  1362. }
  1363. }
  1364. }
  1365. static bool functionWillReturn(const Function &F) {
  1366. // We can infer and propagate function attributes only when we know that the
  1367. // definition we'll get at link time is *exactly* the definition we see now.
  1368. // For more details, see GlobalValue::mayBeDerefined.
  1369. if (!F.hasExactDefinition())
  1370. return false;
  1371. // Must-progress function without side-effects must return.
  1372. if (F.mustProgress() && F.onlyReadsMemory())
  1373. return true;
  1374. // Can only analyze functions with a definition.
  1375. if (F.isDeclaration())
  1376. return false;
  1377. // Functions with loops require more sophisticated analysis, as the loop
  1378. // may be infinite. For now, don't try to handle them.
  1379. SmallVector<std::pair<const BasicBlock *, const BasicBlock *>> Backedges;
  1380. FindFunctionBackedges(F, Backedges);
  1381. if (!Backedges.empty())
  1382. return false;
  1383. // If there are no loops, then the function is willreturn if all calls in
  1384. // it are willreturn.
  1385. return all_of(instructions(F), [](const Instruction &I) {
  1386. return I.willReturn();
  1387. });
  1388. }
  1389. // Set the willreturn function attribute if possible.
  1390. static void addWillReturn(const SCCNodeSet &SCCNodes,
  1391. SmallSet<Function *, 8> &Changed) {
  1392. for (Function *F : SCCNodes) {
  1393. if (!F || F->willReturn() || !functionWillReturn(*F))
  1394. continue;
  1395. F->setWillReturn();
  1396. NumWillReturn++;
  1397. Changed.insert(F);
  1398. }
  1399. }
  1400. // Return true if this is an atomic which has an ordering stronger than
  1401. // unordered. Note that this is different than the predicate we use in
  1402. // Attributor. Here we chose to be conservative and consider monotonic
  1403. // operations potentially synchronizing. We generally don't do much with
  1404. // monotonic operations, so this is simply risk reduction.
  1405. static bool isOrderedAtomic(Instruction *I) {
  1406. if (!I->isAtomic())
  1407. return false;
  1408. if (auto *FI = dyn_cast<FenceInst>(I))
  1409. // All legal orderings for fence are stronger than monotonic.
  1410. return FI->getSyncScopeID() != SyncScope::SingleThread;
  1411. else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))
  1412. return true;
  1413. else if (auto *SI = dyn_cast<StoreInst>(I))
  1414. return !SI->isUnordered();
  1415. else if (auto *LI = dyn_cast<LoadInst>(I))
  1416. return !LI->isUnordered();
  1417. else {
  1418. llvm_unreachable("unknown atomic instruction?");
  1419. }
  1420. }
  1421. static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {
  1422. // Volatile may synchronize
  1423. if (I.isVolatile())
  1424. return true;
  1425. // An ordered atomic may synchronize. (See comment about on monotonic.)
  1426. if (isOrderedAtomic(&I))
  1427. return true;
  1428. auto *CB = dyn_cast<CallBase>(&I);
  1429. if (!CB)
  1430. // Non call site cases covered by the two checks above
  1431. return false;
  1432. if (CB->hasFnAttr(Attribute::NoSync))
  1433. return false;
  1434. // Non volatile memset/memcpy/memmoves are nosync
  1435. // NOTE: Only intrinsics with volatile flags should be handled here. All
  1436. // others should be marked in Intrinsics.td.
  1437. if (auto *MI = dyn_cast<MemIntrinsic>(&I))
  1438. if (!MI->isVolatile())
  1439. return false;
  1440. // Speculatively assume in SCC.
  1441. if (Function *Callee = CB->getCalledFunction())
  1442. if (SCCNodes.contains(Callee))
  1443. return false;
  1444. return true;
  1445. }
  1446. // Infer the nosync attribute.
  1447. static void addNoSyncAttr(const SCCNodeSet &SCCNodes,
  1448. SmallSet<Function *, 8> &Changed) {
  1449. AttributeInferer AI;
  1450. AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
  1451. Attribute::NoSync,
  1452. // Skip already marked functions.
  1453. [](const Function &F) { return F.hasNoSync(); },
  1454. // Instructions that break nosync assumption.
  1455. [&SCCNodes](Instruction &I) {
  1456. return InstrBreaksNoSync(I, SCCNodes);
  1457. },
  1458. [](Function &F) {
  1459. LLVM_DEBUG(dbgs()
  1460. << "Adding nosync attr to fn " << F.getName() << "\n");
  1461. F.setNoSync();
  1462. ++NumNoSync;
  1463. },
  1464. /* RequiresExactDefinition= */ true});
  1465. AI.run(SCCNodes, Changed);
  1466. }
  1467. static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
  1468. SCCNodesResult Res;
  1469. Res.HasUnknownCall = false;
  1470. for (Function *F : Functions) {
  1471. if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
  1472. F->isPresplitCoroutine()) {
  1473. // Treat any function we're trying not to optimize as if it were an
  1474. // indirect call and omit it from the node set used below.
  1475. Res.HasUnknownCall = true;
  1476. continue;
  1477. }
  1478. // Track whether any functions in this SCC have an unknown call edge.
  1479. // Note: if this is ever a performance hit, we can common it with
  1480. // subsequent routines which also do scans over the instructions of the
  1481. // function.
  1482. if (!Res.HasUnknownCall) {
  1483. for (Instruction &I : instructions(*F)) {
  1484. if (auto *CB = dyn_cast<CallBase>(&I)) {
  1485. if (!CB->getCalledFunction()) {
  1486. Res.HasUnknownCall = true;
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. }
  1492. Res.SCCNodes.insert(F);
  1493. }
  1494. return Res;
  1495. }
  1496. template <typename AARGetterT>
  1497. static SmallSet<Function *, 8>
  1498. deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter) {
  1499. SCCNodesResult Nodes = createSCCNodeSet(Functions);
  1500. // Bail if the SCC only contains optnone functions.
  1501. if (Nodes.SCCNodes.empty())
  1502. return {};
  1503. SmallSet<Function *, 8> Changed;
  1504. addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);
  1505. addMemoryAttrs(Nodes.SCCNodes, AARGetter, Changed);
  1506. addArgumentAttrs(Nodes.SCCNodes, Changed);
  1507. inferConvergent(Nodes.SCCNodes, Changed);
  1508. addNoReturnAttrs(Nodes.SCCNodes, Changed);
  1509. addWillReturn(Nodes.SCCNodes, Changed);
  1510. // If we have no external nodes participating in the SCC, we can deduce some
  1511. // more precise attributes as well.
  1512. if (!Nodes.HasUnknownCall) {
  1513. addNoAliasAttrs(Nodes.SCCNodes, Changed);
  1514. addNonNullAttrs(Nodes.SCCNodes, Changed);
  1515. inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
  1516. addNoRecurseAttrs(Nodes.SCCNodes, Changed);
  1517. }
  1518. addNoSyncAttr(Nodes.SCCNodes, Changed);
  1519. // Finally, infer the maximal set of attributes from the ones we've inferred
  1520. // above. This is handling the cases where one attribute on a signature
  1521. // implies another, but for implementation reasons the inference rule for
  1522. // the later is missing (or simply less sophisticated).
  1523. for (Function *F : Nodes.SCCNodes)
  1524. if (F)
  1525. if (inferAttributesFromOthers(*F))
  1526. Changed.insert(F);
  1527. return Changed;
  1528. }
  1529. PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
  1530. CGSCCAnalysisManager &AM,
  1531. LazyCallGraph &CG,
  1532. CGSCCUpdateResult &) {
  1533. FunctionAnalysisManager &FAM =
  1534. AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
  1535. // We pass a lambda into functions to wire them up to the analysis manager
  1536. // for getting function analyses.
  1537. auto AARGetter = [&](Function &F) -> AAResults & {
  1538. return FAM.getResult<AAManager>(F);
  1539. };
  1540. SmallVector<Function *, 8> Functions;
  1541. for (LazyCallGraph::Node &N : C) {
  1542. Functions.push_back(&N.getFunction());
  1543. }
  1544. auto ChangedFunctions = deriveAttrsInPostOrder(Functions, AARGetter);
  1545. if (ChangedFunctions.empty())
  1546. return PreservedAnalyses::all();
  1547. // Invalidate analyses for modified functions so that we don't have to
  1548. // invalidate all analyses for all functions in this SCC.
  1549. PreservedAnalyses FuncPA;
  1550. // We haven't changed the CFG for modified functions.
  1551. FuncPA.preserveSet<CFGAnalyses>();
  1552. for (Function *Changed : ChangedFunctions) {
  1553. FAM.invalidate(*Changed, FuncPA);
  1554. // Also invalidate any direct callers of changed functions since analyses
  1555. // may care about attributes of direct callees. For example, MemorySSA cares
  1556. // about whether or not a call's callee modifies memory and queries that
  1557. // through function attributes.
  1558. for (auto *U : Changed->users()) {
  1559. if (auto *Call = dyn_cast<CallBase>(U)) {
  1560. if (Call->getCalledFunction() == Changed)
  1561. FAM.invalidate(*Call->getFunction(), FuncPA);
  1562. }
  1563. }
  1564. }
  1565. PreservedAnalyses PA;
  1566. // We have not added or removed functions.
  1567. PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
  1568. // We already invalidated all relevant function analyses above.
  1569. PA.preserveSet<AllAnalysesOn<Function>>();
  1570. return PA;
  1571. }
  1572. namespace {
  1573. struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
  1574. // Pass identification, replacement for typeid
  1575. static char ID;
  1576. PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
  1577. initializePostOrderFunctionAttrsLegacyPassPass(
  1578. *PassRegistry::getPassRegistry());
  1579. }
  1580. bool runOnSCC(CallGraphSCC &SCC) override;
  1581. void getAnalysisUsage(AnalysisUsage &AU) const override {
  1582. AU.setPreservesCFG();
  1583. AU.addRequired<AssumptionCacheTracker>();
  1584. getAAResultsAnalysisUsage(AU);
  1585. CallGraphSCCPass::getAnalysisUsage(AU);
  1586. }
  1587. };
  1588. } // end anonymous namespace
  1589. char PostOrderFunctionAttrsLegacyPass::ID = 0;
  1590. INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "function-attrs",
  1591. "Deduce function attributes", false, false)
  1592. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  1593. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  1594. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  1595. INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "function-attrs",
  1596. "Deduce function attributes", false, false)
  1597. Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
  1598. return new PostOrderFunctionAttrsLegacyPass();
  1599. }
  1600. template <typename AARGetterT>
  1601. static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
  1602. SmallVector<Function *, 8> Functions;
  1603. for (CallGraphNode *I : SCC) {
  1604. Functions.push_back(I->getFunction());
  1605. }
  1606. return !deriveAttrsInPostOrder(Functions, AARGetter).empty();
  1607. }
  1608. bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
  1609. if (skipSCC(SCC))
  1610. return false;
  1611. return runImpl(SCC, LegacyAARGetter(*this));
  1612. }
  1613. namespace {
  1614. struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
  1615. // Pass identification, replacement for typeid
  1616. static char ID;
  1617. ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
  1618. initializeReversePostOrderFunctionAttrsLegacyPassPass(
  1619. *PassRegistry::getPassRegistry());
  1620. }
  1621. bool runOnModule(Module &M) override;
  1622. void getAnalysisUsage(AnalysisUsage &AU) const override {
  1623. AU.setPreservesCFG();
  1624. AU.addRequired<CallGraphWrapperPass>();
  1625. AU.addPreserved<CallGraphWrapperPass>();
  1626. }
  1627. };
  1628. } // end anonymous namespace
  1629. char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
  1630. INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass,
  1631. "rpo-function-attrs", "Deduce function attributes in RPO",
  1632. false, false)
  1633. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  1634. INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass,
  1635. "rpo-function-attrs", "Deduce function attributes in RPO",
  1636. false, false)
  1637. Pass *llvm::createReversePostOrderFunctionAttrsPass() {
  1638. return new ReversePostOrderFunctionAttrsLegacyPass();
  1639. }
  1640. static bool addNoRecurseAttrsTopDown(Function &F) {
  1641. // We check the preconditions for the function prior to calling this to avoid
  1642. // the cost of building up a reversible post-order list. We assert them here
  1643. // to make sure none of the invariants this relies on were violated.
  1644. assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
  1645. assert(!F.doesNotRecurse() &&
  1646. "This function has already been deduced as norecurs!");
  1647. assert(F.hasInternalLinkage() &&
  1648. "Can only do top-down deduction for internal linkage functions!");
  1649. // If F is internal and all of its uses are calls from a non-recursive
  1650. // functions, then none of its calls could in fact recurse without going
  1651. // through a function marked norecurse, and so we can mark this function too
  1652. // as norecurse. Note that the uses must actually be calls -- otherwise
  1653. // a pointer to this function could be returned from a norecurse function but
  1654. // this function could be recursively (indirectly) called. Note that this
  1655. // also detects if F is directly recursive as F is not yet marked as
  1656. // a norecurse function.
  1657. for (auto &U : F.uses()) {
  1658. auto *I = dyn_cast<Instruction>(U.getUser());
  1659. if (!I)
  1660. return false;
  1661. CallBase *CB = dyn_cast<CallBase>(I);
  1662. if (!CB || !CB->isCallee(&U) ||
  1663. !CB->getParent()->getParent()->doesNotRecurse())
  1664. return false;
  1665. }
  1666. F.setDoesNotRecurse();
  1667. ++NumNoRecurse;
  1668. return true;
  1669. }
  1670. static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
  1671. // We only have a post-order SCC traversal (because SCCs are inherently
  1672. // discovered in post-order), so we accumulate them in a vector and then walk
  1673. // it in reverse. This is simpler than using the RPO iterator infrastructure
  1674. // because we need to combine SCC detection and the PO walk of the call
  1675. // graph. We can also cheat egregiously because we're primarily interested in
  1676. // synthesizing norecurse and so we can only save the singular SCCs as SCCs
  1677. // with multiple functions in them will clearly be recursive.
  1678. SmallVector<Function *, 16> Worklist;
  1679. for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
  1680. if (I->size() != 1)
  1681. continue;
  1682. Function *F = I->front()->getFunction();
  1683. if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
  1684. F->hasInternalLinkage())
  1685. Worklist.push_back(F);
  1686. }
  1687. bool Changed = false;
  1688. for (auto *F : llvm::reverse(Worklist))
  1689. Changed |= addNoRecurseAttrsTopDown(*F);
  1690. return Changed;
  1691. }
  1692. bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
  1693. if (skipModule(M))
  1694. return false;
  1695. auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
  1696. return deduceFunctionAttributeInRPO(M, CG);
  1697. }
  1698. PreservedAnalyses
  1699. ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
  1700. auto &CG = AM.getResult<CallGraphAnalysis>(M);
  1701. if (!deduceFunctionAttributeInRPO(M, CG))
  1702. return PreservedAnalyses::all();
  1703. PreservedAnalyses PA;
  1704. PA.preserve<CallGraphAnalysis>();
  1705. return PA;
  1706. }